require 'herorealms'
require 'decks'
require 'stdlib'
require 'timeoutai'
require 'hardai_2'
require 'aggressiveai'
--=======================================================================================================
--This lua file builds on lesson #1 but not lesson #2/3, I reused some elements from lesson #2 to save time.
--The 2nd player is an AI bot with a random character/build and player 1 would have a custom loadout.
--
--Instead of making a custom action as in lesson #2, a custom champion is made. This lesson will also take
--a first look at "targeted effects" which is how the game engine handles giving a player the option
--to pick from one or more valid targets. Part of that involves covering selectors.
--=======================================================================================================
function setupGame(g)
	registerCards(g, {
        --Anytime you make a new card/skill/ability/armor you need to put them here to be registered before you can use it.
        the_bait_carddef()
	})

    standardSetup(g, {
        description = "Lesson #4: Custom Champion Cards<br>Created by WardenSlayer.",
        playerOrder = { plid1, plid2 },
        ai = ai.CreateKillSwitchAi(createAggressiveAI(),  createHardAi2()),
        timeoutAi = createTimeoutAi(),
        opponents = { { plid1, plid2 } },
        players = {
            {
                id = plid1,
                startDraw = 3,
                name = "Lua Lesson Warrior",
                avatar="assassin",
                health = 50,
                cards = {
                    deck = {
                        --Here the champion item card is added to player 1's deck
                        { qty=4, card=the_bait_carddef()},
                        { qty=8, card=gold_carddef() },
                    },
                    skills = {
                        {qty = 1, card = thief_lift_carddef() },
                        {qty = 1, card = fighter_devastating_blow_carddef()},
                        {qty = 1, card = ranger_hunters_cloak_carddef()},
                    },
                    buffs = {
                        drawCardsCountAtTurnEndDef(5),
                        discardCardsAtTurnStartDef(),
                        fatigueCount(40, 1, "FatigueP1")
                    }
                }
            }, 
            {
                --Player 2 is an AI controlled player. They will have a random class/build each time
                --The AI is always the same level as the player's character.
                id = plid2,
                isAi = true,
                startDraw = 5,
				init = {
                    fromEnv = plid2
                },
                cards = {
					buffs = {
                        drawCardsCountAtTurnEndDef(5),
                        discardCardsAtTurnStartDef(),
						fatigueCount(40, 1, "FatigueP2"),
					}
                }
            },          
        }
    })
end

--=======================================================================================================
--When making a custom card of any type, it starts with a declaration function like this.
--There are 2 main steps to making a custom card.
--     1) Designing the layout of the card (to include the actual abilities the card has)
--     2) Coding the functionality of the card
function the_bait_carddef()
    --This first section creates a card layout for use later on
	local cardLayout = createLayout({
        --This is the display text on he card title
		name = "The Bait",
        --This sets the art that displays on card (you can find these in the art section of the Lua Doc)
		art = "art/T_Elf",
        --This sets the card frame/border art that displays on the card (you can find these in the frames section of the Lua Doc)
		frame = "frames/Wild_Champion_CardFrame",
        --This is the card type display text that appears at the top center of the card
		cardTypeLabel = "Champion",
        --This part generates the card ability text that tells you what the card does
        --This xmlText was generated by using the card designer
        xmlText = [[
            <vlayout>
                <hlayout flexibleheight="3">
                <tmpro text="{expend}" fontsize="50" flexiblewidth="2" />
                <tmpro text="{combat_3}" fontsize="55" flexiblewidth="8" />
                </hlayout>
                <divider />
                <hlayout flexibleheight="2">
                <tmpro text="{scrap}" fontsize="50" flexiblewidth="2" />
                <tmpro text="&lt;space=.5em/&gt;Draw 2 cards and then discard a card from your hand." fontsize="18" flexiblewidth="10" />
                </hlayout>
            </vlayout>
     ]]
	})
    --This next section populated the object data for the card
    --and is how you control what the card can do
	return createChampionDef({
        --The cards ID which can be used t identify it in other places
		id = "the_bait",
        --The card's name but not the one you see on the card image.
		name = "The Bait",
        --If you were to buy the card how much would it cost
		acquireCost = 0,
        --How much health does the champion have? 
        --Determines the number shown on the champion's lower right corner
		health = 3,
        --This marks a champion as a guard (true) or non-guard (false)
		isGuard = true,
        --Here you define what types the card has so it can interact with other cards
        --that do different things based on type
            --championType for champions, noStealType is for starter cards or cards you dont want a thief to be able to steal
            --other examples (like elfType or felineType) can be found in the card subtypes section of the Lua document)
        types = { championType, noStealType, elfType },
        --Here is where you define the faction the card has. It will now trigger the
        --faction abilities of the type listed here.
		factions = {wildFaction},
        --Here we finally use the layout created above to set the visual display for the card
		layout = cardLayout,
		abilities = {
			createAbility({
				id = "baitMain",
                --Auto trigger means it triggers as soon as it played (other trigger options can be found in the Lua Doc)
				trigger = autoTrigger,
                --If the champion can get prepared, should the ability work 
                --more than once in the same turn? (the opposite would be "singleActivation")
				activations = multipleActivations,
                --After this ability is used, the champion will be expended
				cost = expendCost,
				effect = gainCombatEffect(3)
			}),
            createAbility({
				id = "baitSacrifice",
                --requires player interaction to trigger
				trigger = uiTrigger,
                --The champion will be sacrificed to activate this ability
				cost = sacrificeSelfCost,
                effect = drawCardsEffect(2)
                            --The draw 2 above happens first and then the targeted effect
                            --because ".seq" is used to sequence effects after each other.
                            .seq(pushTargetedEffect({
                                --The text to show the user in game to help them pick a valid target.
                                desc="Discard a card from your hand.",
                                --How few choices must the user choose.
                                min=1,
                                --The maximum choices a user can choose.
                                max=1,
                                --How to determine what is a valid target.
                                  --selectLoc = pick cards in this area
                                    --loc = combines a player identifier with a location identifier.
                                        --currentPid = the game alias for the player whose turn it currently is.
                                        --handPloc = the hand location (cards in the hand)
                                --End result = select all the cards in the current player's hand.
                                validTargets=selectLoc(loc(currentPid, handPloc)),
                                --This is what will happen to all the targets that were chosen.
                                targetEffect=discardTarget(),
                                --This is only for AI games. It tells the AI to discard their cheapest card.
                                tags = { "cheapest" }
                            }))
			})
		},
	})
end

--=======================================================================================================
function endGame(g)
end

--=======================================================================================================


function setupMeta(meta)
    meta.name = "lesson_4"
    meta.minLevel = 0
    meta.maxLevel = 0
    meta.introbackground = ""
    meta.introheader = ""
    meta.introdescription = ""
    meta.path = "C:/Users/xTheC/Desktop/Git Repositories/Hero-Realms-Lua-Lessons/Lua Lesson #4/lesson_4.lua"
     meta.features = {
}

end