#textdomain wesnoth-ei
[scenario]
    id=17a_The_Duel
    name= _ "The Duel"
    map_data="{campaigns/Eastern_Invasion/maps/17a_The_Duel.map}"
    turns=-1
    next_scenario=18_Epilogue

    {UNDERGROUND}

    {INTRO_AND_SCENARIO_MUSIC underground.ogg the_deep_path.ogg}
    {EXTRA_SCENARIO_MUSIC the_dangerous_symphony.ogg}
    {EXTRA_SCENARIO_MUSIC legends_of_the_north.ogg}

    {campaigns/Eastern_Invasion/utils/final_battle.cfg}

    [side]
        type=Lieutenant
        id=Gweddry
        name= _ "Gweddry"
        side=1
        canrecruit=yes
        controller=human
        team_name=good
        user_team_name=_"Wesnothians"
        gold=200
        {FLAG_VARIANT loyalist}
    [/side]

    [side]
        type=Lich Lord
        id=Mal-Ravanal
        name= _ "Mal-Ravanal"
        side=2
        canrecruit=yes
        recruit=
        controller=ai
        gold=0
        team_name=bad
        user_team_name=_"Undead"
        profile=portraits/mal-ravanal.png
        [ai]
            aggression=0.6
            caution=0.2
            leader_value=20

            [avoid]
                x=20
                y=4
            [/avoid]

            [avoid]
                x=28
                y=8
            [/avoid]
        [/ai]
        {FLAG_VARIANT undead}
    [/side]

    [event]
        name=prestart
        [objectives]
            side=1
            [objective]
                description= _ "Defeat Mal-Ravanal while Dacyn is nearby"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Gweddry"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Dacyn"
                condition=lose
            [/objective]
            {IS_LAST_SCENARIO}
        [/objectives]
    [/event]

    [event]
        name=start

        # We don't want him to appear on the recall list
        [kill]
            id=Konrad
        [/kill]

        [recall]
            id=Dacyn
            animate=no
        [/recall]
        {MODIFY_UNIT id=Dacyn moves 0}
        {MODIFY_UNIT id=Dacyn attacks_left 0}

        [message]
            speaker=Mal-Ravanal
            message= _ "I have waited a long time for this day to come. I shall have my revenge, Dacyn!"
        [/message]
        [message]
            speaker=Dacyn
            message= _ "Indeed, it is time to end this."
        [/message]

        [message]
            speaker=narrator
            #wmllint: display on
            message= _ "The rules of the duel are these:
You may recruit or recall up to 5 units.
At the end of your first turn, your keep will disappear, and you must battle with whatever troops you have at that time."
            #wmllint: display off
            image=wesnoth-icon.png
        [/message]
        [message]
            speaker=Dacyn
            message= _ "When we attack Mal-Ravanal I will need to be nearby with the Null Stone to prevent his teleportation. We cannot afford to let him escape."
        [/message]
    [/event]

#define DUEL_UNIT TYPE X Y
    [unit]
        type={TYPE}
        side=2
        x={X}
        y={Y}
        random_traits=yes
        animate=yes
        moves=0
    [/unit]
#enddef

    [event]
        name=side 2 turn 1
        {DUEL_UNIT (Necromancer) 24 5}
        {DUEL_UNIT (Lich) 25 6}
        {DUEL_UNIT (Deathblade) 25 7}
        {DUEL_UNIT (Draug) 24 7}
        {DUEL_UNIT (Death Knight) 23 7}
        {DUEL_UNIT (Spectre) 23 6}
    [/event]

    [event]
        name=turn 2
        [terrain]
            x,y=6,15
            terrain=Cud
        [/terrain]

        [terrain]
            x,y=24,6
            terrain=Cud
        [/terrain]
    [/event]

    # NOTE: the player will be recalling a keep full of level 3 units with magic, arcane,
    #       and impact attacks. Mal-Ravanal needs to cheat a fair bit just to even the odds
    [event]
        name=turn 5,turn 11,turn 17,turn 23,turn 30,turn 38,turn 47,turn 57,turn 68,turn 80
        first_time_only=no

        [fire_event]
            name=summon
        [/fire_event]
    [/event]

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

        [if]
            [have_unit]
                side=2
#ifdef HARD
                count=1-4
#else
                count=1-3
#endif
            [/have_unit]
            [then]
                [fire_event]
                    name=summon
                [/fire_event]
            [/then]
        [/if]
    [/event]

    #Mal-Ravanal summons 3 additional undead on the battlefield
    [event]
        name=summon
        first_time_only=no

        {VARIABLE i 0}

        [animate_unit]
            [filter]
                id=Mal-Ravanal
            [/filter]
            flag=summon
        [/animate_unit]

        [while]
            [variable]
                name=i
                less_than=3
            [/variable]

            [do]
#ifdef EASY
                {VARIABLE_OP new_summon_type rand "Bone Shooter,Bone Shooter,Revenant,Revenant,Necrophage,Necrophage,Wraith"}
#endif
#ifdef NORMAL
                {VARIABLE_OP new_summon_type rand "Bone Shooter,Revenant,Necrophage,Wraith,Deathblade"}
#endif
#ifdef HARD
                {VARIABLE_OP new_summon_type rand "Bone Shooter,Revenant,Necrophage,Wraith,Shadow,Banebow,Spectre,Ghast,Deathblade,Draug"}
#endif

                [store_locations]
                    [not]
                        [filter]
                            # matches all units
                        [/filter]
                    [/not]
                    [and]
                        terrain=Uu
                    [/and]

                    variable=new_summon_locations
                [/store_locations]

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

                [unit]
                    x,y=$new_summon_locations[$loci].x,$new_summon_locations[$loci].y
                    type=$new_summon_type
                    side=2
                    animate=yes
                [/unit]

                {VARIABLE_OP i add 1}
            [/do]
        [/while]
        {CLEAR_VARIABLE new_summon_type,new_summon_locations,i,loci}

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

    [event]
        name=summon_explanation
        [message]
            speaker=Gweddry
            message= _ "Wait — what just happened? He called more warriors out of the ground! That’s not allowed!"
        [/message]
        [message]
            speaker=Mal-Ravanal
            message= _ "Stop talking and fight! The rules say nothing about bringing up warriors that were already here."
        [/message]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Gweddry
        [/filter]
        [message]
            speaker=Gweddry
            message= _ "No! This cannot be the way it was meant to end!"
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Dacyn
        [/filter]
        [message]
            speaker=Dacyn
            message= _ "Wesnoth, I have failed you..."
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    {LATE_DEFEAT}
[/scenario]
