#textdomain wesnoth-ei
[scenario]
    id=17a_The_Duel
    name= _ "The Duel"
    map_file=17a_The_Duel.map
    turns=unlimited
    victory_when_enemies_defeated=no
    next_scenario=18_Epilogue

    [time]
        id=indoors
        name= _ "Indoors"
        image=misc/time-schedules/schedule-indoors.png
        red=-60
        green=-45 # gloomier than normal indoors
        blue=-25
    [/time]

    {INTRO_AND_SCENARIO_MUSIC weight_of_revenge.ogg into_the_shadows.ogg}
    {EXTRA_SCENARIO_MUSIC battle-epic.ogg}

    [story]
        [part]
            #po: a human messenger
            story= _ "As the messenger passed out of the city gates, silence settled over the valley of the Weldyn. Children buried their fathers, mothers entombed their sons; soldiers slept where they stood, spears still clutched behind pale knuckles. The only sound was the incessant drum-march of skeletal feet, ever-growing outside the walls."
            music=silence.ogg
        [/part]
        [part]
            story= _ "Finally, the gates creaked open once again. Eyes looked on, eager, anxious, terrified, as the messenger rode toward the castle..."
            music=silence.ogg
        [/part]
        [part]
            story= _ "News spread quickly. Mal-Ravanal had accepted the challenge. Dacyn and six of Weldyn’s finest were to duel the lich in the abandoned catacombs under the city..."
            music=silence.ogg
        [/part]
    [/story]
    {EI_TRACK {JOURNEY_17_NEW} }

    [side]
        id=Dacyn
        type=Fallen Mage
        save_id=Gweddry
        side=1
        controller=human
        team_name=good
        user_team_name=_"Wesnothians"
        gold=0 # set gold to 120 when the match starts, so we ignore any carryover
        income=-2 # -2 actually means 0 income
        {FLAG_VARIANT loyalist}
        defeat_condition=never # gweddry is allowed to die
    [/side]

    [side]
        side=2
        {CHARACTER_STATS_RAVANAL}
        recruit=
        controller=ai
        gold=0
        team_name=bad
        user_team_name=_"Undead"
        facing=sw
        {FLAG_VARIANT undead}
    [/side]
    {SILENTLY_LIMIT_LEADER_MOVES 2 1}
    {REANIMATION_IMPLEMENTATION}
    {GHOSTPLAGUE_IMPLEMENTATION}

    [event]
        name=prestart
        {VARIABLE ravanal_grouping defensive}
    [/event]
    [event]
        name=side 2 turn refresh
        first_time_only=no

        # done this way to avoid CI errors, it doesn't like variables in the macro
        [if]
            {VARIABLE_CONDITIONAL ravanal_grouping equals defensive}
            [then]
                {RESET_SIDE_AI 2 defensive 0.99 0.01}
            [/then]
            [else]
                {RESET_SIDE_AI 2 no 0.99 0.01}
            [/else]
        [/if]

        {MODIFY_SIDE_AI (2) (leader_aggression=0.5)}

        # for the first 2 turns, group up nice and pretty
        # then run in and attack
        [if]
            [lua]
                code=<< return wesnoth.current.turn<=2 >>
            [/lua]

            [then]
                {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 1 (x,y,radius=20,7,3)})}
                {DISABLE_ATTACKING (2)}
            [/then]

            [else]
                {ENABLE_ATTACKING (2)} # reset_side_ai doesn't cover this, and I'm afraid to add it in case I break something else
            [/else]
        [/if]
    [/event]

    [event]
        name=prestart
        {RECALL_XY Dacyn 6 15}
        [modify_unit]
            [filter]
                id=Dacyn
            [/filter]
            canrecruit=yes
            [object]
                [effect]
                    apply_to=overlay
                    add=misc/leader-gold.png
                [/effect]
            [/object]
        [/modify_unit]
        [set_extra_recruit]
            id=Dacyn
            extra_recruit={REGULAR_RECRUIT_LIST}
        [/set_extra_recruit]

        [objectives]
            side=1
            [objective]
                description= _ "Defeat Mal-Ravanal"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Dacyn"
                condition=lose
            [/objective]
        [/objectives]
    [/event]

    # start mal-ravanal's soldiers with high XP, so they level quickly and get a full heal
#define RAVANAL_VETERAN_RECRUIT FACE SIDE TYPE X Y
    {MODIFY_UNIT id=Mal-Ravanal facing {FACE}}
    {RAVANAL_ATTACK}
    {GENERIC_UNIT {SIDE} {TYPE} {X} {Y}} {FACING sw} {ANIMATE}
    {STORE_UNIT_VAR (x,y={X},{Y}) max_experience veteran_max_experience}
    {CLEAR_VARIABLE veteran_max_experience,min_experience,random}
#enddef

    [event]
        name=start
        [modify_side]
            side=1
            gold=120 # so we don't have ugly carryover
        [/modify_side]

        {KILL id=Konrad} # We don't want him to appear on the recall list
        [recall]
            id=Dacyn
            show=no
            x,y=7,15
            facing=ne
        [/recall]
        {MODIFY_UNIT id=Dacyn moves 0} # so that he can't leave the keep and let you recall 1 extra
        {MODIFY_UNIT id=Dacyn attacks_left 0}
        {MODIFY_UNIT id=Dacyn canrecruit yes}

        [message]
            speaker=Mal-Ravanal
            message= _ "Dacyn, I’m surprised you came. I remember you being smarter than that!"
        [/message]
        [message]
            speaker=Dacyn
            message= _ "I and I alone will put an end to your foul machinations! You have grown strong in the darkness, but I have become stronger (cough)(cough) still!"
        [/message]
        [message]
            speaker=Mal-Ravanal
            message= _ "A-ha haha! And how exactly do you plan to do that? Come now, you must know that you’re outmatched."
        [/message]

        # Mal-Ravanal gets to go first
        # no scaling, because ravanal's renaimation ability is a major source of difficulty, and it already scales
        {RAVANAL_VETERAN_RECRUIT se 2 (Necromancer) 24 5}
        {RAVANAL_VETERAN_RECRUIT se 2 (Lich) 25 6}
        {RAVANAL_VETERAN_RECRUIT se 2 (Banebow) 25 7}
        {RAVANAL_VETERAN_RECRUIT sw 2 (Draug) 24 7}
        {RAVANAL_VETERAN_RECRUIT sw 2 (Death Knight) 23 7}
        {RAVANAL_VETERAN_RECRUIT sw 2 (Spectre) 23 6} # this guy ends up attacking first, which is good because he's low-damage-high-durability

        [message]
            speaker=narrator
            message= _ "The rules of the duel are these:
You may recruit or recall up to 6 units.
At the end of your first turn, your keep will disappear, and you must battle with whatever troops you have at that time."
            image=wesnoth-icon.png
        [/message]
    [/event]

    # remove keeps; prevent recruiting
    [event]
        name=side 2 turn 1
        {MODIFY_TERRAIN Cud^Li 6 15} # beam of light weakens mal-ravanal (though it's only relevant when comboed with dacyn's obscures),
        {MODIFY_TERRAIN Cud^Li 24 6} # to deliberately make him a little weaker than he is in S18b

        # remove upkeep. No balance effect, but it looks pretty to have 0 gold instead of -##
        [modify_unit]
            [filter]
                side=1
            [/filter]
            [object]
                duration=scenario
                [effect]
                    apply_to=loyal
                [/effect]
            [/object]
        [/modify_unit]
    [/event]

    # if Mal-Ravanal is low on troops, have him summon some more
    [event]
        name=new turn
        first_time_only=no

        {CALCULATE_ARMY_STRENGTH side=2 2} # <- does counts positive gold, but mal-ravanal is almost certainly negative
        [if]
            {VARIABLE_CONDITIONAL army_strength less_than 12}
            [then]
                [if]
                    {VARIABLE_CONDITIONAL summon_cooldown greater_than 0}
                    [then]
                        {VARIABLE_OP summon_cooldown sub 1}
                    [/then]

                    [else]
                        [fire_event]
                            name=summon
                        [/fire_event]
                        {VARIABLE summon_cooldown 3} # he can summon every 4 (not 3) turns
                    [/else]
                [/if]
                {VARIABLE ravanal_grouping no} # once he summons, stop playing defensive
            [/then]
        [/if]
    [/event]

    # initially he only summons 3, but each time he summons more and more
    # no scaling with difficulty, because reanimation already scales
    [event]
        name=prestart
        {VARIABLE summon_count 3}
    [/event]
    [event]
        name=summon
        first_time_only=no

        [repeat]
            times=$summon_count
            [do]
                {VARIABLE_OP new_summon_type rand "Revenant,Deathblade,Bone Shooter,Bone Shooter,Wraith,Wraith"}
                [store_locations]
                    [not]
                        [filter] # not on an existing unit
                        [/filter]
                    [/not]
                    x=16,21,28,26,22,14, 6,10
                    y= 4, 4, 6,10,14,17,11, 7
                    variable=new_summon_locations
                [/store_locations]

                {VARIABLE_OP loci rand "1..$new_summon_locations.length"}
                {VARIABLE_OP loci sub 1}

                {RAVANAL_ATTACK}
                [unit]
                    x,y=$new_summon_locations[$loci].x,$new_summon_locations[$loci].y
                    type=$new_summon_type
                    side=2
                    animate=yes
                [/unit]
            [/do]
        [/repeat]
        {CLEAR_VARIABLE new_summon_type,new_summon_locations,loci}
        {VARIABLE_OP summon_count add 1}

        [fire_event]
            name=summon_explanation
        [/fire_event]
    [/event]

    [event]
        name=summon_explanation
        {FIND_NEARBY_NOT_DACYN_PREFER_HEROES 21 8 99}
        [message]
            speaker=$found_unit.id
            message= _ "Wait — what just happened? More undead creatures just came out of the ground! That’s not allowed!"
        [/message]
        [message]
            speaker=Mal-Ravanal
            message= _ "The rules say nothing about bringing up warriors who were already here. Feel free to reanimate some too, if you can."
        [/message]
        [event]
            name=summon_explanation
            [event]
                name=summon_explanation
                [message]
                    speaker=Mal-Ravanal
                    message= _ "Tsk, tsk, I begin to tire of this."
                [/message]
                {FIND_NEARBY_NOT_DACYN_PREFER_HEROES 21 8 99}
                [message]
                    speaker=$found_unit.id
                    message= _ "More reinforcements come every time!"
                [/message]
            [/event]
        [/event]
    [/event]

    [event]
        name=die
        first_time_only=no
        # for some reason, reanimated ghosts have no id/side, so use if-then instead of a filter
        [if]
            {VARIABLE_CONDITIONAL unit.id equals Mal-Ravanal}
            [then]
            [/then]

            [else]
                [if]
                    {VARIABLE_CONDITIONAL unit.side equals 1}
                    [then]
                    [/then]

                    [else]
                        {VARIABLE killed_something yes}
                    [/else]
                [/if]
            [/else]
        [/if]
    [/event]
    [event]
        name=last breath

        [filter]
            id=Mal-Ravanal
        [/filter]

        [if]
            {VARIABLE_CONDITIONAL killed_something not_equals yes}
            [then]
                [set_achievement]
                    content_for=eastern_invasion
                    id=ei_S17a
                [/set_achievement]
            [/then]
        [/if]
    [/event]
    [event]
        name=victory
        {CLEAR_VARIABLE killed_something}
        {CLEAR_VARIABLE ravanal_grouping}
    [/event]

    # put this after the other Mal-Ravanal "last breath" event
    {campaigns/Eastern_Invasion/utils/final_battle/final_battle.cfg}

    {HERODEATH_GWEDDRY_EXPENDABLE}
    {HERODEATH_OWAEC_EXPENDABLE}
    {HERODEATH_DACYN_FINALBATTLE}
    {HERODEATH_ADDOGIN}
    {HERODEATH_TERRAENT}
    {HERODEATH_HAHID_FINALBATTLE}
    {HERODEATH_DOLBURRAS}
    {HERODEATH_GRUG}
    {HERODEATH_GAENNELL}
    {HERODEATH_YANNIC}

    {LATE_DEFEAT}
[/scenario]
