#textdomain wesnoth-ei
[scenario]
    id=99_Empire
    name= _ "Empire"
    next_scenario=null
    map_file=99_Empire.map
    victory_when_enemies_defeated=no
    turns=unlimited
    {UNDERGROUND}

    {SCENARIO_MUSIC "northerners.ogg"}
    {EXTRA_SCENARIO_MUSIC "knolls.ogg"}
    {EXTRA_SCENARIO_MUSIC "siege_of_laurelmor.ogg"}

    # wmllint: local spelling Dra-Nak
    [story]
        [part]
            story= _ "Congratulations! By sparing Dra-Nak in S11, ‘Captured’, sparing Mortic in S12 ‘Evacuation’, and completing the difficult S17b ‘All-In’, you’ve unlocked this bonus scenario!"
        [/part]
    [/story]

    #--------------------
    # CHIEF DRA-NAK
    #--------------------
    [side]
        color=white
        type=Orcish Sovereign
        id=Chief Dra-Nak
        name= _ "Chief Dra-Nak"
        profile=portraits/orcs/ruler-2.webp
        [modifications]
            {TRAIT_INTELLIGENT}
            {TRAIT_STRONG}
        [/modifications]
        recruit=Orcish Grunt,Orcish Assassin,Orcish Archer
        facing=nw
        side=1
        canrecruit=yes
        controller=human
        hitpoints=64
        gold=0
        team_name=orcs
        user_team_name=_"Clan Whitefang"
        {FLAG_VARIANT6 ragged}
    [/side]
    [event] # no upkeep for Dra-Nak (and the humans, who have no income)
        name=start,recruit,recall
        first_time_only=no
        [modify_unit]
            [filter]
                side=1,5
            [/filter]

            [object]
                [effect]
                    apply_to=loyal
                [/effect]
            [/object]
        [/modify_unit]
    [/event]

    #--------------------
    # DRAKES
    #--------------------
    [side]
        side=2
        controller=ai
        recruit=Drake Burner # plus LIMIT_CONTEMPORANEOUS_RECRUITS units
        gold={ON_DIFFICULTY 0 0 0}
        income={ON_DIFFICULTY 2 10 18}
        team_name=drakes
        user_team_name=_"Drakes"
        type=Drake Flameheart
        id=Mortic
        name=_"Mortic"
        canrecruit=yes
        facing=sw
        experience=23 # since he probably got some XP from the fights in S09 and S12
        [modifications]
            {TRAIT_RESILIENT}
            {TRAIT_STRONG}
        [/modifications]
        {GENERIC_UNIT 3 (Drake Burner)  46 12}
#ifndef EASY
        {GENERIC_UNIT 3 (Drake Fighter) 45 12}
        {GENERIC_UNIT 3 (Drake Burner)  46 10} #endif
#ifdef HARD
        {GENERIC_UNIT 3 (Drake Burner)  46 13}
        {GENERIC_UNIT 3 (Drake Clasher) 45 11} #endif
    [/side]
    {SILENTLY_LIMIT_LEADER_MOVES 2 2}

    [side]
        side=3
        controller=ai
        recruit=Drake Clasher # plus LIMIT_CONTEMPORANEOUS_RECRUITS units
        gold={ON_DIFFICULTY 0 0 0}
        income={ON_DIFFICULTY 2 10 18}
        team_name=drakes
        user_team_name=_"Drakes"
        type=Drake Arbiter
        id=Goag
        name=_"Goag"
        canrecruit=yes
        facing=sw
        [modifications]
            {TRAIT_RESILIENT}
            {TRAIT_QUICK}
        [/modifications]
        {GENERIC_UNIT 3 (Drake Clasher) 46 26}
#ifndef EASY
        {GENERIC_UNIT 3 (Drake Clasher) 45 25}
        {GENERIC_UNIT 3 (Drake Fighter) 46 24} #endif
#ifdef HARD
        {GENERIC_UNIT 3 (Drake Glider)  45 26}
        {GENERIC_UNIT 3 (Drake Clasher) 45 24} #endif
    [/side]
    {SILENTLY_LIMIT_LEADER_MOVES 3 1}

    [side]
        side=4
        controller=ai
        recruit=Drake Fighter # plus LIMIT_CONTEMPORANEOUS_RECRUITS units
        gold={ON_DIFFICULTY 0 0 0}
        income={ON_DIFFICULTY 2 10 18}
        team_name=drakes
        user_team_name=_"Drakes"
        type=Drake Warrior
        id=Gra'ritos
        name=_"Gra’ritos"
        canrecruit=yes
        facing=ne
        [modifications]
            {TRAIT_INTELLIGENT}
            {TRAIT_QUICK}
        [/modifications]
#ifndef EASY
        {GENERIC_UNIT 3 (Drake Burner) 14 25} #endif
#ifdef HARD
        {GENERIC_UNIT 3 (Drake Fighter) 14 26} #endif
    [/side]
    {SILENTLY_LIMIT_LEADER_MOVES 4 1}

    {LIMIT_CONTEMPORANEOUS_RECRUITS 2,3,4 "Drake Glider"  2} # everyone gets 2 of each unit not on their recruit list (including guardians)
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2,3   "Drake Fighter" 2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 3,4   "Drake Burner"  2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2,4   "Drake Clasher" 2}

    [event]
        name=side 2 turn
        first_time_only=no
        {RESET_SIDE_AI 2,3,4 defensive 0.9 0.1}
        {MODIFY_SIDE_AI (2) (
            [avoid] # avoid side 3's leader
                x=41-99
                y=19-99
            [/avoid]
        )}
        {MODIFY_SIDE_AI (3) (
            [avoid] # avoid side 2's leader
                x=38-99
                y= 0-18
            [/avoid]
        )}

        {RETREAT_WHEN_WEAK 2 0-9 (
            # includes 1 leader and 4 guardians!
            {GOAL_LOCATION 99 (x,y=43,14)}
            {GOAL_LOCATION 99 (x,y=44,14)}
        ) }
        {RETREAT_WHEN_WEAK 3 0-9 (
            # includes 1 leader and 4 guardians!
            {GOAL_LOCATION 99 (x,y=44,23)}
            {GOAL_LOCATION 99 (x,y=43,24)}
        )}
        {RETREAT_WHEN_WEAK 4 0-9 (
            # includes 1 leader and 4 guardians!
            {GOAL_LOCATION 99 (x,y=13,26)}
        )}
    [/event]

    #--------------------
    # HUMANS
    #--------------------
    [side]
        side=5
        controller=ai
        recruit=Spearman,Bowman
        team_name=humans
        user_team_name=_"Survivors"
        gold={ON_DIFFICULTY 40 60 80} # reduced scaling; the humans are more helpful than harmful
        income=-2
        hidden=yes
        no_leader=yes
    [/side]
    [event]
        name=side 5 turn
        first_time_only=no
        {RESET_SIDE_AI 5 no 0.8 0.25}
        {MODIFY_SIDE_AI (5) (retreat_factor=0)} # otherwise units get stuck sometimes waiting for healing
        {MODIFY_SIDE_AI (5) ({GOAL_LOCATION 999 (x,y=46,25)})} # push southeast
        # for some reason using an avoid makes the units freeze. Modify terrain instead (yes this is ugly)
        {MODIFY_TERRAIN Re^Xo 21 21} {MODIFY_TERRAIN Re^Xo 22 20}
        {MODIFY_TERRAIN Ur^Xo 27 22} {MODIFY_TERRAIN Uu^Xo 28 21}
        {MODIFY_TERRAIN Re^Xo 34 21}
        {MODIFY_TERRAIN Uu^Xo 36-37 21} {MODIFY_TERRAIN Uh^Xo 38-39 21}
    [/event]
    [event]
        name=side 5 turn end
        first_time_only=no
        {MODIFY_TERRAIN Re 21 21} {MODIFY_TERRAIN Re 22 20}
        {MODIFY_TERRAIN Ur 27 22} {MODIFY_TERRAIN Uu 28 21}
        {MODIFY_TERRAIN Re 34 21}
        {MODIFY_TERRAIN Uu 36-37 21} {MODIFY_TERRAIN Uh 38-39 21}
    [/event]

    #--------------------
    # PRISONERS
    #--------------------
    [side]
        side=6
        controller=null
        team_name=drakes,humans
        user_team_name=_"Tribute"
        hidden=yes
    [/side]
    [side]
        color=white
        side=7
        controller=null
        team_name=orcs
        user_team_name=_"Prison Guards"
        hidden=yes
        {NAMED_UNIT 7 (Orcish Assassin) 16 11 "Tobo"    _"Tobo"    ()} {FACING se}
        {NAMED_UNIT 7 (Orcish Grunt)    21 10 "Unik"    _"Unik"    ()} {FACING sw}
        {NAMED_UNIT 7 (Orcish Archer)   22 8  "Shubru"  _"Shubru"  ()} {FACING se}
        {NAMED_UNIT 7 (Orcish Archer)   26 8  "Grugnuk" _"Grugnuk" ()} {FACING sw}
    [/side]

    #--------------------
    # START THE SCENARIO
    #--------------------
    [event]
        name=prestart
        [time_area]
            x= 0-7,  8-12,13-16,17,   18,   19,   20,   21,      39,  39,   40,  40,   41,  41,   42,  42,   43-99
            y=17-99,18-99,20-99,24-99,24-99,25-99,25-99,28-99,   0-13,27-29,0-15,27-29,0-16,27-29,0-16,23-99,0-99
            {DEFAULT_SCHEDULE} # it's no longer winter
        [/time_area]

        {PLACE_IMAGE scenery/dwarven-keep-tile.png 30 15}
        {PLACE_IMAGE items/whitefang-flag.png 28 15}
        {PLACE_IMAGE items/whitefang-flag.png 32 15}
        {PLACE_IMAGE items/bones.png 30 17}

        {PLACE_IMAGE items/burial.png 22 13}
        {PLACE_IMAGE items/burial.png 26 11}

        {PLACE_IMAGE items/chest-open.png 33 11}

        #         {PLACE_IMAGE items/bones.png 12 3}
        #         {PLACE_IMAGE items/bones.png 9 3}

        {PLACE_IMAGE items/barrel.png 14 15}
        {PLACE_IMAGE items/box.png 15 15}
        {PLACE_IMAGE items/axe.png 15 16}
        {PLACE_IMAGE scenery/trash.png 16 16}
        {PLACE_IMAGE items/dummy.png 16 15}

        {PLACE_IMAGE items/armor.png 15 19}
        {PLACE_IMAGE items/bow.png 15 18}
        {PLACE_IMAGE items/barrel.png 14 18}
        {PLACE_IMAGE "items/barrel.png~FL()" 16 19}
        {PLACE_IMAGE "items/sword.png~FL()" 16 18}
        {PLACE_IMAGE items/barrel.png 17 19}

        {PLACE_IMAGE scenery/trash.png 30 24}

        # drake "guards" (actually player reinforcements after killing leaders)
        {NOTRAIT_UNIT 2 (Drake Burner)   43 11}  {FACING sw}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_RESILIENT}
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 2 (Fire Drake)     41  9}  {FACING se}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_QUICK }
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 2 (Drake Burner)   42  8}  {FACING se}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_QUICK }
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 2 (Drake Glider)   45  8}  {FACING sw}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 3 (Drake Thrasher) 41 27}  {FACING ne}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 3 (Drake Burner)   43 28}  {FACING nw}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_INTELLIGENT}
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 3 (Drake Clasher)  44 27}  {FACING nw}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_INTELLIGENT}
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 3 (Drake Clasher)  45 20}  {FACING sw}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_QUICK }
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 4 (Drake Fighter)  12 22}  {FACING sw}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_INTELLIGENT}
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 4 (Drake Fighter)   9 22}  {FACING se}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_QUICK }
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 4 (Drake Fighter)  12 27}  {FACING ne}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        {NOTRAIT_UNIT 4 (Drake Glider)   17 28}  {FACING nw}{GUARDIAN}
        [+unit]
            [modifications]
                {TRAIT_LOYAL}
            [/modifications]
        [/unit]
        [+unit]
            [modifications]
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        # main units
        {LOYAL_UNIT   1 (Orcish Slayer)      39 19} {FACING ne}
        [+unit]
            hitpoints=11
        [/unit]

        {GENERIC_UNIT 1 (Orcish Warrior)     26 16} {FACING se}
        [+unit]
            hitpoints=42
        [/unit]

        {GENERIC_UNIT 1 (Orcish Archer)      34 16} {FACING nw}
        [+unit]
            hitpoints=13
        [/unit]

        {GENERIC_UNIT 1 (Orcish Grunt)       35 15} {FACING sw}
        {GENERIC_UNIT 1 (Orcish Grunt)       29 14} {FACING se}

        # northeast units
        {GENERIC_UNIT 1 (Orcish Crossbowman) 39 15} {FACING ne}
        [+unit]
            hitpoints=39
        [/unit]

        {GENERIC_UNIT 2 (Drake Burner)       40 14} {FACING sw}
        [+unit]
            hitpoints=35
        [/unit]

        {GENERIC_UNIT 2 (Drake Clasher)      40 15} {FACING sw}
        [+unit]
            hitpoints=9
        [/unit]

        {GENERIC_UNIT 2 (Fire Drake)         42 15} {FACING sw}

        {GENERIC_UNIT 1 (Orcish Grunt)       38 18} {FACING ne}
        [+unit]
            hitpoints=31
        [/unit]

        {GENERIC_UNIT 2 (Drake Clasher)      40 18} {FACING sw}
        [+unit]
            hitpoints=39
        [/unit]
        {MODIFY_UNIT x,y=40,18 status.poisoned yes}

        # southeast units
        {GENERIC_UNIT 1 (Orcish Grunt)       39 22} {FACING se}
        [+unit]
            hitpoints=18
        [/unit]

        {GENERIC_UNIT 3 (Drake Clasher)      40 22} {FACING nw}
        [+unit]
            hitpoints=7
        [/unit]

        {GENERIC_UNIT 3 (Drake Glider)       40 23} {FACING nw}
        {GENERIC_UNIT 3 (Drake Clasher)      43 23} {FACING nw}

        # southwest units
        {GENERIC_UNIT 1 (Orcish Archer)      26 20} {FACING sw}
        [+unit]
            hitpoints=19
        [/unit]

        {GENERIC_UNIT 4 (Drake Fighter)      25 21} {FACING ne}
        [+unit]
            hitpoints=3
        [/unit]

        {GENERIC_UNIT 4 (Drake Glider)       28 23} {FACING nw}
        [+unit]
            hitpoints=31
        [/unit]

        {GENERIC_UNIT 4 (Drake Fighter)      27 24} {FACING nw}

        {GENERIC_UNIT 4 (Drake Fighter)      17 23} {FACING ne}
        {GENERIC_UNIT 4 (Drake Burner)       18 22} {FACING ne}
        {GENERIC_UNIT 4 (Drake Fighter)      18 23} {FACING ne}

        # capture villages that start with a unit on them
        [capture_village]
            side=2
            [filter]
                side=2
            [/filter]
        [/capture_village]
        [capture_village]
            side=3
            [filter]
                side=3
            [/filter]
        [/capture_village]
        [capture_village]
            side=4
            [filter]
                side=4
            [/filter]
        [/capture_village]
    [/event]

    [event]
        name=start
        [scroll_to]
            x,y=30,15
            immediate=yes
        [/scroll_to]

        {MOVE_UNIT (id=Chief Dra-Nak) 28 18}
        [message]
            speaker=Chief Dra-Nak
            message= _ "Filthy..."
        [/message]
        {MOVE_UNIT (id=Chief Dra-Nak) 29 17}
        [message]
            speaker=Chief Dra-Nak
            message= _ "Stinking..."
        [/message]
        {MOVE_UNIT (id=Chief Dra-Nak) 30 15}
        [message]
            speaker=Chief Dra-Nak
            message= _ "TRAITORS!!!"
        [/message]
        {MODIFY_UNIT (id=Chief Dra-Nak) facing se}
        [message]
            speaker=Chief Dra-Nak
            message= _ "Drakes, human, dwarves... vermin, all of them! I offered them a chance at peace, at purpose! A chance to be part of something greater than themselves — my great golden Empire!!! And this is the thanks I get?"
        [/message]

        {MOVE_UNIT (type=Orcish Slayer) 32 17}
        [message]
            type=Orcish Slayer
            message= _ "Chief, more wyrms are attacking the east tunnels! There’s too many of them! What do we do?"
        [/message]
        [message]
            speaker=Chief Dra-Nak
            message= _ "I’m not going to let everything I’ve worked for fall apart so easily! These vermin are WEAK! We are STRONG!"
        [/message]
        [message]
            type=Orcish Slayer
            message= _ "Okay, but that’s not really a battle pla-"
        [/message]
        [message]
            speaker=Chief Dra-Nak
            message= _ "Capture their warriors and sell them for gold. Break their leaders and bend their followers to my will. I’ll slay anyone who resists and build a new empire on top of their steaming corpses!"
        [/message]

        [objectives]
            side=1
            [objective]
                description= _ "Defeat all drake leaders"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Chief Dra-Nak"
                condition=lose
            [/objective]
            [note]
                description= _ "You are fighting in your home and therefore pay no upkeep."
            [/note]
            [note]
                description= _ "Reduce enemies to 0 hp to capture them and gain gold. The prisons have limited space."
            [/note]
            [note]
                description= _ "Surviving enemies will (mostly) join you when you defeat their leader."
            [/note]
        [/objectives]
    [/event]

    #--------------------
    # GOLD
    #--------------------
    [event]
        name=prestart
        {PLACE_IMAGE items/gold-coins-small.png 34 10}
        [event]
            name=moveto

            [filter]
                side=1
                x,y=34,10
            [/filter]

            [gold]
                side=1
                amount=31
            [/gold]
            [sound]
                name=coins.ogg
            [/sound]

            [message]
                speaker=unit
                message= _ "I’ve emptied the last of our treasury! Only 31 pieces, but better than nothing."
            [/message]
            {REMOVE_IMAGE $unit.x $unit.y}
        [/event]
    [/event]

#define PARTIAL_CHANGE_SIDES SIDE
    {KILL (side,canrecruit={SIDE},yes   ) ANIMATE=yes} # immediately kill the leader, so he doesn't get caught up in this
    {KILL (side,role=      {SIDE},leader) ANIMATE=yes}

    {VARIABLE side {SIDE}}
    [if]
        {VARIABLE_CONDITIONAL side equals 2}
        [then]
            #po: said by Dra-Nak as an insult to Mortic in the latter's last-breath event
            {VARIABLE taunt_message _"Some ‘leader’."                                                    }
        [/then]
    [/if]
    [if]
        {VARIABLE_CONDITIONAL side equals 3}
        [then]
            #po: said by Dra-Nak as an insult to Goag in the latter's last-breath event
            {VARIABLE taunt_message _"That’ll teach him. Where was that loyalty when he used to obey me?"}
        [/then]
    [/if]
    [if]
        {VARIABLE_CONDITIONAL side equals 4}
        [then]
            #po: said by Dra-Nak as an insult to Gra’ritos in the latter's last-breath event
            {VARIABLE taunt_message _"What happened to dying slow? So much for him."                     }
        [/then]
    [/if]
    [if]
        {VARIABLE_CONDITIONAL side equals 5}
        [then]
            {VARIABLE taunt_message _"They really thought they could get away? Haw..."                   }
        [/then]
    [/if]
    [message]
        speaker=Chief Dra-Nak
        message=$taunt_message
    [/message]

    [if]
        [have_unit]
            side={SIDE}
            count=1-99
        [/have_unit]

        [have_unit]
            side=2,3,4
            canrecruit=yes # only trigger this if it's NOT the final leader
        [/have_unit]

        [then]
            # joinme message
            [if]
                {VARIABLE_CONDITIONAL side equals 2}
                [then]
                    {VARIABLE joinme_message _"He called himself your leader, but now you see him for the weakling he really was. You can either join him or fight alongside me and pray I show you mercy."}
                [/then]
            [/if]
            [if]
                {VARIABLE_CONDITIONAL side equals 3}
                [then]
                    {VARIABLE joinme_message _"As for the rest of you — pledge yourselves to me, or die a slow painful death!"}
                [/then]
            [/if]
            [if]
                {VARIABLE_CONDITIONAL side equals 4}
                [then]
                    {VARIABLE joinme_message _"You might as well give it up! Resistance is pointless."}
                [/then]
            [/if]
            [if]
                {VARIABLE_CONDITIONAL side equals 5}
                [then]
                    {VARIABLE joinme_message _"Nobody escapes me. Join me and I’ll treat you fairly, resist me and you’ll die painfully."}
                [/then]
            [/if]
            [message]
                speaker=Chief Dra-Nak
                message=$joinme_message
            [/message]

            # defector message
            [if]{VARIABLE_CONDITIONAL side equals 2}
                [then]
                    {VARIABLE defector_message _"Mortic was strong.
Alone I am weak.
I will serve you."}
                [/then]
            [/if]
            [if]
                {VARIABLE_CONDITIONAL side equals 3}
                [then]
                    {VARIABLE defector_message _"I crave not death.
I will myself to you."}
                [/then]
            [/if]
            [if]
                {VARIABLE_CONDITIONAL side equals 4}
                [then]
                    {VARIABLE defector_message _"These orcs enrage me,
But I possess no strength to resist.
I will serve you."}
                [/then]
            [/if]
            [if]
                {VARIABLE_CONDITIONAL side equals 5}
                [then]
                    {VARIABLE defector_message _"Yes, yes, just please don’t hurt me!"}
                [/then]
            [/if]
            [message]
                side={SIDE}
                message=$defector_message
            [/message]

            # change side
            [store_unit]
                [filter]
                    side={SIDE}
                [/filter]

                kill=no
                variable=units
            [/store_unit]
            {VARIABLE i 0}
            [foreach]
                array=units
                variable=unit
                [do]
                    [if]
                        {VARIABLE_CONDITIONAL i equals 1} # 1/2 of survivors join Dra-Nak
                        [then]
                            {VARIABLE_OP i sub 1}
                        [/then]

                        [else]
                            {MODIFY_UNIT id=$unit.id side 1}
                            {VARIABLE_OP i add 1}
                        [/else]
                    [/if]
                [/do]
            [/foreach]

            # stubborn message
            [if]
                {VARIABLE_CONDITIONAL side equals 2}
                [then]
                    {VARIABLE stubborn_message _"I shall never again obey orc.
Mortic fought and died.
I shall fight as well."}
                [/then]
            [/if]
            [if]
                {VARIABLE_CONDITIONAL side equals 3}
                [then]
                    {VARIABLE stubborn_message _"I am always loyal,
but never to prey.
I fight until death."}
                [/then]
            [/if]
            [if]
                {VARIABLE_CONDITIONAL side equals 4}
                [then]
                    {VARIABLE stubborn_message _"I grieve.
May the end come swiftly."}
                [/then]
            [/if]
            [if]
                {VARIABLE_CONDITIONAL side equals 5}
                [then]
                    {VARIABLE stubborn_message _"You traitors, how could you! I’ll never betray Wesnoth!"}
                [/then]
            [/if]
            [message]
                side={SIDE}
                message=$stubborn_message
                image_pos=right
            [/message]
        [/then]
    [/if]
#enddef

    #--------------------
    # MORTIC DIES
    #--------------------
    [event]
        name=attack

        [filter]
            side=1
        [/filter]

        [filter_second]
            id=Mortic
        [/filter_second]

        [message]
            speaker=Chief Dra-Nak
            message= _ "Surrender or die, your choice!"
        [/message]
        [message]
            speaker=Mortic
            message= _ "Surrender brings certain death.
Battle may yet bring victory.
There is no choice."
        [/message]
        [message]
            speaker=Chief Dra-Nak
            message= _ "Die it is then!"
        [/message]
    [/event]
    [event]
        name=last breath

        [filter]
            id=Mortic
        [/filter]

        [filter_second]
            side=1
        [/filter_second]

        [message]
            speaker=Mortic
            #po: Mortic's last breath
            message= _ "Surrender brings death.
Battle brings death.
All is death..."
        [/message]
        {PARTIAL_CHANGE_SIDES 2}
    [/event]

    #--------------------
    # GOAG DIES
    #--------------------
    [event]
        name=attack

        [filter]
            side=1
        [/filter]

        [filter_second]
            id=Goag
        [/filter_second]

        [message]
            speaker=Chief Dra-Nak
            message= _ "The reaper’s knocking at your door, wyrm! Give up now and maybe I’ll kill you quickly."
        [/message]
        [message]
            speaker=Goag
            message= _ "Mortic commanded me to fight.
So the Aspirant ordered, so I will fight,
Until the end."
        [/message]
    [/event]
    [event]
        name=die

        [filter]
            id=Goag
        [/filter]

        [filter_second]
            side=1
        [/filter_second]

        {PARTIAL_CHANGE_SIDES 3}
    [/event]

    #--------------------
    # GRA'RITOS DIES
    #--------------------
    [event]
        name=attack

        [filter]
            side=1
        [/filter]

        [filter_second]
            id=Gra'ritos
        [/filter_second]

        [message]
            speaker=Gra'ritos
            #po: to Dra-Nak
            message= _ "You are Prey.
I am Hunter.
I will not fall easily."
        [/message]
        [message]
            speaker=Chief Dra-Nak
            message= _ "Good, I’ll enjoy making it slow."
        [/message]
    [/event]
    [event]
        name=die

        [filter]
            id=Gra'ritos
        [/filter]

        [filter_second]
            side=1
        [/filter_second]

        {PARTIAL_CHANGE_SIDES 4}
    [/event]

    #--------------------
    # HUMAN LEADER DIES
    #--------------------
    [event]
        name=attack

        [filter]
            side=1
        [/filter]

        [filter_second]
            role=leader
        [/filter_second]

        [message]
            speaker=Chief Dra-Nak
            #po: as the west bridge is broken, the Wesnothians who were left behind are trying to get though the orc's caves and out the east side
            message= _ "Haw, back so soon? Too bad your ‘friends’ abandoned you on MY side of the river. But it’s your lucky day, I’m a fair orc. Surrender now and I might be merciful."
        [/message]
        [message]
            role=leader
            message= _ "We’ll never give in to you! For Wesnoth!!"
        [/message]
    [/event]
    [event]
        name=last breath

        [filter]
            role=leader
        [/filter]

        [message]
            role=leader
            message= _ "This foolish plan was my idea... I’m so, so sorry everyone..."
        [/message]
    [/event]
    [event]
        name=die

        [filter]
            id=Gra'ritos
        [/filter]

        [filter_second]
            side=5
        [/filter_second]

        [message]
            role=leader
            message= _ "Keep pushing forward! Stick to the south tunnels! We can do this!"
        [/message]
    [/event]
    [event]
        name=die

        [filter]
            role=leader
        [/filter]

        [filter_second]
            side=1
        [/filter_second]

        {PARTIAL_CHANGE_SIDES 5}
    [/event]

    #--------------------
    # IMPRISON ENEMIES
    #--------------------
    [event]
        name=explain_imprison
        [message]
            speaker=Chief Dra-Nak
            #po: the necromancers are apparently paying gold immediately when each prisoner is captured
            message= _ "That’s right, bleed them down and lock them up! I need gold to win this fight!"
        [/message]
        [sound]
            name=gold.ogg
        [/sound]
    [/event]
    [event]
        name=explain_prisons1
        [message]
            speaker=Unik
            message= _ "Chief, I only have space for so many prisoners. As cells get crowded, prisoners will start to get sick and we won’t get paid as much."
        [/message]
    [/event]
    [event]
        name=explain_prisons2
        [message]
            speaker=Unik
            #po: the hole where the escape in S11 started
            message= _ "Things are starting to get really crowded over here Chief. Shame the fourth cell has a hole in the back..."
        [/message]
    [/event]
    [event]
        name=explain_prisons3
        [message]
            speaker=Unik
            message= _ "That’s it boss, the prisoners are packed as tight as they’ll go! Hope you got enough gold to finish off the drakes."
        [/message]
    [/event]

#define IMPRISON_UNIT X Y
    [if]
        [not]
            [have_unit]
                x,y={X},{Y}
            [/have_unit]
        [/not]

        [then]
            {RANDOM 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
            {GENERIC_UNIT 6 $unit.type {X} {Y}}

            [store_side]
                side=$unit.side
                variable=side
            [/store_side]
            [modify_unit]
                [filter]
                    x,y={X},{Y}
                [/filter]
                hitpoints=$random
                [object]
                    id=imprisoned
                    [effect]
                        apply_to=remove_attacks
                        [not]
                            name=fire breath
                        [/not]
                    [/effect]
                    [effect]
                        apply_to=new_attack
                        [filter]
                            [not]
                                trait=strong
                            [/not]
                        [/filter]
                        name=unarmed
                        description= _ "unarmed"
                        icon=attacks/fist-human.png
                        range=melee
                        type=impact
                        damage=4
                        number=3
                    [/effect]
                    # same as the merman civilian
                    [effect]
                        apply_to=new_attack
                        [filter]
                            trait=strong
                        [/filter]
                        name=unarmed
                        description= _ "unarmed"
                        icon=attacks/fist-human.png
                        range=melee
                        type=impact
                        range=melee
                        type=impact
                        damage=5
                        number=3
                    [/effect]
                    # same as the merman civilian
                [/object]
            [/modify_unit]
            [gold]
                side=1
                amount=$bounty
            [/gold]
            [floating_text]
                x,y=$unit.x,$unit.y
                text=$bounty_text
            [/floating_text]
            [return]
            [/return]
        [/then]
    [/if]
#enddef
    [event]
        name=die
        first_time_only=no
        [filter]
            canrecruit=no

            [not]
                role=leader
            [/not]

            [not]
                side=1
            [/not]
        [/filter]
        [filter_condition]
            {VARIABLE_CONDITIONAL prisons_captured not_equals yes}
        [/filter_condition]

        [filter_second]
            side=1
        [/filter_second]

        [fire_event]
            name=explain_imprison
        [/fire_event]

        {VARIABLE bounty 10} {VARIABLE bounty_text "<span color='#00FF00' size='xx-small'>Full Bounty</span>
    <span color='#FFD700' size='x-small'>+$bounty gold</span>"}
        {IMPRISON_UNIT 21 7}
        {IMPRISON_UNIT 19 7}
        {IMPRISON_UNIT 15 10}
        {IMPRISON_UNIT 29 8}
        {IMPRISON_UNIT 31 8}
        {IMPRISON_UNIT 21 7}
        {IMPRISON_UNIT 14 8}
        {IMPRISON_UNIT 15 8}
        {IMPRISON_UNIT 19 8}
        [fire_event]
            name=explain_prisons1
        [/fire_event]

        {VARIABLE bounty 7} {VARIABLE bounty_text "<span color='#FE5000' size='xx-small'>Medium Bounty</span>
    <span color='#FFD700' size='x-small'>  +$bounty gold</span>"}
        {IMPRISON_UNIT 30 6}
        {IMPRISON_UNIT 13 10}
        {IMPRISON_UNIT 18 6}
        {IMPRISON_UNIT 21 6}
        {IMPRISON_UNIT 28 7}
        {IMPRISON_UNIT 30 7}
        {IMPRISON_UNIT 16 9}
        {IMPRISON_UNIT 16 8}
        {IMPRISON_UNIT 20 6}
        [fire_event]
            name=explain_prisons2
        [/fire_event]

        {VARIABLE bounty 4} {VARIABLE bounty_text "<span color='#FF0000' size='xx-small'>Small Bounty</span>
    <span color='#FFD700' size='x-small'> +$bounty gold</span>"}
        {IMPRISON_UNIT 28 7}
        {IMPRISON_UNIT 29 7}
        {IMPRISON_UNIT 30 8}
        {IMPRISON_UNIT 14 7}
        {IMPRISON_UNIT 14 9}
        {IMPRISON_UNIT 20 7}
        {IMPRISON_UNIT 15 7}
        {IMPRISON_UNIT 19 6}
        {IMPRISON_UNIT 14 10}
        {IMPRISON_UNIT 13 9}
        {IMPRISON_UNIT 31 7}
        {IMPRISON_UNIT 29 6}

        [fire_event]
            name=explain_prisons3
        [/fire_event]
        {IMPRISON_UNIT 18 7}
    [/event]

    #--------------------
    # JAILER DIES
    #--------------------
    [event]
        name=die
        [filter]
            side=7
            [not]
                x,y=21,10
            [/not]
        [/filter]
        [message]
            speaker=Chief Dra-Nak
            message= _ "Get away from my jail cells! Filthy traitors, there goes my income..."
        [/message]
        [message]
            speaker=Chief Dra-Nak
            message= _ "Fine then, I’m done taking prisoners. From here on out, you either submit or die!"
        [/message]
        {VARIABLE prisons_captured yes}
    [/event]
    [event]
        name=die
        [filter]
            side,x,y=7,16,11
        [/filter]
        {MODIFY_TERRAIN Re^Pr/o 16 10}
        {MODIFY_UNIT side,x,y=6,13-16,7-10 side 2}
        [remove_object]
            side=2
            object_id=imprisoned
        [/remove_object]
    [/event]
    [event]
        name=die
        [filter]
            side,x,y=7,22,8
        [/filter]
        {MODIFY_TERRAIN Re^Pr/o 21 8}
        {MODIFY_UNIT side,x,y=6,18-21,6-8 side 2}
        [remove_object]
            side=2
            object_id=imprisoned
        [/remove_object]
    [/event]
    [event]
        name=die
        [filter]
            side,x,y=7,26,8
        [/filter]
        {MODIFY_TERRAIN Re^Pr\o 28 8}
        {MODIFY_UNIT side,x,y=6,28-31,6-8 side 2}
        [remove_object]
            side=2
            object_id=imprisoned
        [/remove_object]
    [/event]

    #--------------------
    # HUMAN ARRIVAL (if there are any)
    #--------------------
    [event]
        name=side 5 turn end
        [filter_condition]
            [variable]
                name=gweddrys_remnants.length
                greater_than=0
            [/variable]
        [/filter_condition]

        # move units to recall list so they're easier to work with
        [foreach]
            array=gweddrys_remnants
            [do]
                [unstore_unit]
                    variable=this_item
                    x,y=recall,recall
                [/unstore_unit]
            [/do]
        [/foreach]
        {MODIFY_UNIT x,y=recall,recall canrecruit no} # just in case; we don't want any special leaders messing things up
        {MODIFY_UNIT x,y=recall,recall side 5}

        # add a couple L1 units so the leader can never be completely alone
        {GENERIC_UNIT 5 (Spearman) recall recall}
        {GENERIC_UNIT 5 (Bowman)   recall recall}

#define RECALL_IF_EMPTY FLT X Y
    [if]
        [not]
            [have_unit]
                x,y={X},{Y}
            [/have_unit]
        [/not]

        [then]
            [recall]
                x,y={X},{Y}
                {FLT}
            [/recall]
        [/then]
    [/if]
#enddef
        # spawn a unit that's pretty durable vs drake fighters/gliders as a leader
        {RECALL_IF_EMPTY (type=Cavalier)      4 28}
        {RECALL_IF_EMPTY (type=Grand Knight)  4 28}
        {RECALL_IF_EMPTY (type=Paladin)       4 28}
        {RECALL_IF_EMPTY (type=Royal Guard)   4 28}
        {RECALL_IF_EMPTY (type=Halberdier)    4 28}
        {RECALL_IF_EMPTY (type=Iron Mauler)   4 28}
        {RECALL_IF_EMPTY (type=Mage of Light) 4 28}
        {RECALL_IF_EMPTY (level=3-99)         4 28}
        {RECALL_IF_EMPTY (level=2)            4 28}
        {MODIFY_UNIT x,y=4,28 role leader}
        [modify_unit] # can't recruit yet, not until side 4's leader dies
            [filter]
                role=leader
            [/filter]

            [object]
                [effect]
                    apply_to=overlay
                    add=misc/leader-crown.png
                [/effect]
                [effect]
                    apply_to=hitpoints
                    increase_total=30 # make the leader extra-durable, especially if we get a squishy one like a white mage
                    heal_full=yes
                [/effect]
            [/object]
        [/modify_unit]
        [event]
            name=die

            [filter]
                side=4
                canrecruit=yes
            [/filter]

            {MODIFY_UNIT role=leader canrecruit yes}
        [/event]

        [message]
            role=leader
            message= _ "Come on, don’t give up! Owaec’s destroyed the bridge so we’ll have to fight our way back through the orcish tunnels."
        [/message]

        # spawn a handful of escorts
        # reduced difficulty scaling, or else it's unlikely they can take the drake keep on non-hard difficulties
        # the intention is that if you left behind many high-level units, the AI can defeat the drakes
        # but if you didn't leave behind many units, the AI won't be able to
        {RECALL_IF_EMPTY level=3-99 3 28}  {RECALL_IF_EMPTY level=2 3 28}  {RECALL_IF_EMPTY level=0-1 3 28}
        {RECALL_IF_EMPTY level=3-99 4 27}  {RECALL_IF_EMPTY level=2 4 27}  {RECALL_IF_EMPTY level=0-1 4 27}
        {RECALL_IF_EMPTY level=3-99 6 28}  {RECALL_IF_EMPTY level=2 6 28}  {RECALL_IF_EMPTY level=0-1 6 28}
#ifndef EASY
        {RECALL_IF_EMPTY level=3-99 6 27}  {RECALL_IF_EMPTY level=2 6 27}  {RECALL_IF_EMPTY level=0-1 6 27} #endif
#ifdef HARD
        {RECALL_IF_EMPTY level=3-99 7 28}  {RECALL_IF_EMPTY level=2 7 28}  {RECALL_IF_EMPTY level=0-1 7 28} #endif
        [message]
            role=leader
            message= _ "I know this looks grim, but we have to try. It’s our only chance. If we can just get through to the eastern exit, we might be able to escape!"
        [/message]

        [modify_side]
            side=5
            hidden=no
        [/modify_side]
    [/event]

    #--------------------
    # HUMAN LEADER ATTACKS
    #--------------------
    # when humans run out of gold, the leader leaves his keep and pushes east
    [event]
        name=side 5 turn
        first_time_only=no
        [store_gold]
            side=5
        [/store_gold]
        [if]
            [variable]
                name=gold
                less_than=14
            [/variable]
            # spearman is 14 gold

            [then]
                {MODIFY_UNIT role=leader canrecruit no}
            [/then]
        [/if]
    [/event]

    #--------------------
    # HUMANS ESCAPE
    #--------------------
    # if humans reach the east map edge by some miracle (and defeat the drake leader there), they escape
#define ESCAPE MESSAGE
    [filter]
        side=5
        x=46
    [/filter]
    [filter_condition]
        [have_unit]
            side=3
            canrecruit=yes
            count=0
        [/have_unit]
    [/filter_condition]
    [message]
        speaker=unit
        message={MESSAGE}
    [/message]
    {KILL id=$unit.id}
    [fire_event]
        name=someone_escaped
        [primary_unit]
            id=$unit.id
        [/primary_unit]
    [/fire_event]
#enddef
    [event]
        name=moveto
        {ESCAPE _"Thank the Light, I’ve made it! Please please please don’t chase me..."}
        [event]
            name=moveto
            {ESCAPE _"Yes, YES! Get me out of here."}
            [event]
                name=moveto
                {ESCAPE _"Ha ha, you’ll never catch me now!"}
                [event]
                    name=moveto
                    {ESCAPE _"Whew, that was a close one!"}
                    [event]
                        name=moveto
                        first_time_only=no
                        {ESCAPE _"I’ve escaped!"}
                    [/event]
                [/event]
            [/event]
        [/event]
    [/event]

    #--------------------
    # VICTORY
    #--------------------
    [event]
        name=die,someone_escaped
        [filter_condition]
            [not]
                [have_unit]
                    side=2
                    canrecruit=yes
                [/have_unit]
            [/not]

            [not]
                [have_unit]
                    side=3
                    canrecruit=yes
                [/have_unit]
            [/not]

            [not]
                [have_unit]
                    side=4
                    canrecruit=yes
                [/have_unit]
            [/not]

            [not]
                [have_unit]
                    side=5
                    role=leader
                [/have_unit]
            [/not]
        [/filter_condition]
        [endlevel]
            result=victory
        [/endlevel]
    [/event]

    [event]
        name=victory

        [set_achievement]
            content_for=eastern_invasion
            id=ei_S99
        [/set_achievement]

        [message]
            speaker=Chief Dra-Nak
            message= _ "Well done, grunts. No more revolts, no more rebellion, and lots of prisoners..."
        [/message]
        [message]
            speaker=Chief Dra-Nak
            #po: all the prisoners
            message= _ "Now kill them all."
        [/message]

        # kill some drakes/humans (if any exist)
        {REPEAT 5 (
            [store_unit]
                [filter]
                    [not]
                        race=orc
                    [/not]

                    [filter_adjacent]
                        side=1
                        race=orc
                    [/filter_adjacent]
                [/filter]
                variable=victim
            [/store_unit]
            [store_unit]
                [filter]
                    side=1
                    race=orc
                    [filter_adjacent]
                        id=$victim.id
                    [/filter_adjacent]
                [/filter]
                variable=killer
            [/store_unit]
            [animate_unit]
                [filter]
                    id=$killer.id
                [/filter]
                flag=attack
                hits=yes
                [facing]
                    x,y=$victim.x,$victim.y
                [/facing]
            [/animate_unit]
            {KILL id=$victim.id ANIMATE=yes}
        )}
        {CLEAR_VARIABLE victim,killer}

        # kill some prisoners
        {MODIFY_TERRAIN Re^Pr/o 16 10}
        [redraw]
        [/redraw]
        {MOVE_UNIT id=Tobo 16 10}
        [animate_unit]
            [filter]
                id=Tobo
            [/filter]
            flag=attack
            hits=yes
            [primary_attack]
                range=ranged
            [/primary_attack]
            [facing]
                x,y=15,10
            [/facing]
        [/animate_unit]

        {FIND_NEARBY (
            [not]
                race=orc
            [/not]
        ) 15 10 99}
        {KILL id=$found_unit.id ANIMATE=yes}

        {MODIFY_TERRAIN Re^Pr/o 21 8}
        [redraw]
        [/redraw]
        {MOVE_UNIT id=Shubru 21 8}
        [animate_unit]
            [filter]
                id=Shubru
            [/filter]
            flag=attack
            hits=yes
            [primary_attack]
                range=ranged
            [/primary_attack]
            [facing]
                x,y=21,7
            [/facing]
        [/animate_unit]

        {FIND_NEARBY (
            [not]
                race=orc
            [/not]
        ) 21 7 99}  {KILL id=$found_unit.id ANIMATE=yes}

        [animate_unit]
            [filter]
                id=Tobo
            [/filter]
            flag=attack
            hits=yes
            [primary_attack]
                range=ranged
            [/primary_attack]
            [facing]
                x,y=16,9
            [/facing]
        [/animate_unit]

        {FIND_NEARBY (
            [not]
                race=orc
            [/not]
        ) 16 9 99}  {KILL id=$found_unit.id ANIMATE=yes}

        # jailer protests
        [message]
            speaker=Unik
            #po: already paid them for the prisoners
            message= _ "But Chief, the necromancers already paid us! We can’t just-"
        [/message]
        [message]
            speaker=Chief Dra-Nak
            #po: all the necromancers
            message= _ "Then I’ll kill them all too! I’m done working with others!"
        [/message]
        {KILL_COUNT 4 (
            [not]
                race=orc
            [/not]
        ) ANIMATE=yes} # kill everyone left alive
        {KILL_COUNT 99 (
            [not]
                race=orc
            [/not]
        )}

        # villan monologue
        [message]
            speaker=Chief Dra-Nak
            message= _ "I tried being merciful. I tried working toward peace. I tried building an empire where all races could be a part of something great, and all I asked in return was simple tribute."
        [/message]
        [message]
            speaker=Chief Dra-Nak
            message= _ "Mercy no more. I’ll build a new empire on the smoking rubble of the old, an empire for orcs and orcs alone, an empire to unite all the scattered tribes under one banner!"
        [/message]
        [message]
            speaker=Chief Dra-Nak
            message= _ "And when I’m ready, we will march across the world and make it ours."
        [/message]
    [/event]

    #--------------------
    # DEFEAT
    #--------------------
    # Dra-Nak dies
    [event]
        name=last breath
        [filter]
            id=Chief Dra-Nak
        [/filter]
        [message]
            speaker=Chief Dra-Nak
            #po: the chief's last breath
            message= _ "FILTHY... Rotten... ...traitors... ..."
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]
[/scenario]

#undef RECALL_IF_EMPTY
