#textdomain wesnoth-ei

#
# this is the first truly LARGE battle of the campaign
# by suddenly scaling up this much, we ensure that a player gets to use their entire recall list and (probably) have gold left over for new recruits
# this avoids issues some campaigns have where you amass a larger recall list than is ever needed
#
# I tried to make allies genuinely useful. They have not-awful scripted AI, and have enough income to make protecting them worthwhile
# and since their units are low-level, the enemy AI usually prefers to attack them over your recalls
#
# worst case, the central island is incredibly defendable. +25% lawful bonus, with castle and moat
# plus Konrad, who's both an incredibly powerful support unit (L5 leadership) and a terrifying combatant in his own right
# of course, that means you give up any chance of keeping generals alive for S18b
#

[scenario]
    id=16_Eleventh_Hour
    name= _ "Eleventh Hour"
    next_scenario=17a_The_Duel
    map_file=16_Eleventh_Hour.map
    turns=19

    {DEFAULT_SCHEDULE_MORNING}

    {SCENARIO_MUSIC legends_of_the_north.ogg} # music is also handled further down, after a period of silence
    {EXTRA_SCENARIO_MUSIC vengeful.ogg}
    {EXTRA_SCENARIO_MUSIC siege_of_laurelmor.ogg}

    {EI_TRACK {JOURNEY_17_NEW} }

    [side]
        id=Konrad
        type=King of Wesnoth
        save_id=Gweddry
        side=1
        canrecruit=yes
        controller=human
        team_name=good
        user_team_name=_"Wesnothians"
        {GOLD 100 75 50} # you're expected to have a large early finish bonus from S14; maybe 300-600 gold
        # you get a lot of income, so even with no carryover you can still recruit enough to defend the central island
        {FLAG_VARIANT loyalist}
    [/side]
    {STARTING_VILLAGES_ALL 1}

#define ALLY_AI_STUFF
    controller=ai
    canrecruit=yes
    gold={ON_DIFFICULTY 50 100 150} # otherwise they basically win the game for you on lower difficulties (I think they still do this on easy, but that's ok)
    income={ON_DIFFICULTY 8 15 23}
    team_name=good
    user_team_name=_"Wesnothians"
    # deliberately including weak recruits, so the ally armies look impressive but are inefficient
    recruit=Heavy Infantryman,Mage,Spearman,Fencer,Swordsman,Bowman,Cavalryman
    [ai]
        leader_aggression=-9
    [/ai]
    {FLAG_VARIANT loyalist}
#enddef

    [side]
        type=General
        id=Halrad
        name= _ "Halrad"
        unrenamable=yes
        side=2
        profile=portraits/humans/marshal-2.webp~FL(horiz)
        [modifications]
            {TRAIT_LOYAL_DUMMY}
            {TRAIT_STRONG}
        [/modifications]
        {ALLY_AI_STUFF}
        {GENERIC_UNIT 2 (Longbowman) 28 8}
        {GENERIC_UNIT 2 (Heavy Infantryman) 32 8}
        {GENERIC_UNIT 2 (Swordsman) 16 13}
        {GENERIC_UNIT 2 (Heavy Infantryman) 44 13}

        {GENERIC_UNIT 2 (Spearman) 21 10}
        {GENERIC_UNIT 2 (Bowman) 39 11}
    [/side]
    {STARTING_VILLAGES 2 1}
    {STARTING_VILLAGES_AREA 2 17 13 0} # important for AI
    {STARTING_VILLAGES_AREA 2 43 13 0} # important for AI

    [side]
        type=General
        id=Halric
        name= _ "Halric"
        unrenamable=yes
        side=3
        profile=portraits/humans/marshal.webp
        [modifications]
            {TRAIT_LOYAL_DUMMY}
            {TRAIT_RESILIENT}
        [/modifications]
        {ALLY_AI_STUFF}
        {GENERIC_UNIT 3 (Swordsman) 47 16}
        {GENERIC_UNIT 3 (Mage) 47 29}
        {GENERIC_UNIT 3 (Heavy Infantryman) 45 32}
        {GENERIC_UNIT 3 (Longbowman) 32 35}
        {GENERIC_UNIT 3 (Spearman) 33 35} # important for AI

        {GENERIC_UNIT 3 (Fencer) 49 22}
        {GENERIC_UNIT 3 (Bowman) 40 33}
    [/side]
    {STARTING_VILLAGES 3 1}
    {STARTING_VILLAGES_AREA 3 47 17 0} # important for AI
    {STARTING_VILLAGES_AREA 3 33 35 0} # important for AI

    [side]
        type=General
        id=Halrod
        name= _ "Halrod"
        unrenamable=yes
        side=4
        color=brightorange
        profile=portraits/humans/general.webp
        [modifications]
            {TRAIT_LOYAL_DUMMY}
            {TRAIT_INTELLIGENT}
        [/modifications]
        {ALLY_AI_STUFF}
        {GENERIC_UNIT 4 (Mage) 13 16}
        {GENERIC_UNIT 4 (Heavy Infantryman) 13 29}
        {GENERIC_UNIT 4 (Swordsman) 15 32}
        {GENERIC_UNIT 4 (Mage) 28 35}
        {GENERIC_UNIT 4 (Bowman) 27 36} # important for AI

        {GENERIC_UNIT 4 (Spearman) 21 35}
        {GENERIC_UNIT 4 (Fencer) 10 21}
    [/side]
    {STARTING_VILLAGES 4 1}
    {STARTING_VILLAGES_AREA 4 13 17 0} # important for AI
    {STARTING_VILLAGES_AREA 4 27 35 0} # important for AI

#define COUNT_XY_RADIUS COUNT X Y RADIUS
    # this comment's whitespace is deliberate
    count={COUNT}
    [filter_location]
        x,y={X},{Y}
        radius={RADIUS}
    [/filter_location]
#enddef
    [event]
        name=side 1 turn end
        first_time_only=no
        {RESET_SIDE_AI 2,3,4 no -1 1} # no grouping helps with my GOALs
        {MODIFY_AI_DELETE_CANDIDATE_ACTION 2,3,4 main_loop leader_shares_keep}

        # AVOID THE MOAT
        # this was painful to write, so please don't change the terrain
        {VARIABLE avoid234_riverX "0-9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,49,49,50-99"}
        {VARIABLE avoid234_riverY "0-99,0-20,24-99,0-17,27-99,0-15,28-99,0-15,30-99,0-13,31-99,0-13,33-99,0-11,33-99,0-11,34-99,0-10,34-99,0-10,35-99,0-9,35-99,0-9,36-99,0-8,36-99,0-9,37-99,0-8,36-99,0-9,37-99,0-8,36-99,0-8,37-99,0-7,36-99,0-8,37-99,0-7,36-99,0-8,37-99,0-7,36-99,0-8,37-99,0-8,36-99,0-9,36-99,0-8,36-99,0-9,36-99,0-9,35-99,0-10,35-99,0-10,34-99,0-10,34-99,0-10,34-99,0-11,34-99,0-11,33-99,0-13,33-99,0-14,31-99,0-15,30-99,0-15,28-99,0-21,27-99,0-99"}

        # NORTH BRIDGE
        # if this bridge is secured, ignore the sector completely
        # if this bridge is lost, focus on defending our leader
        [if]
            [have_unit]
                side=6
                canrecruit=yes
                [filter_location]
                    x,y=28,2
                    radius=5
                [/filter_location]
            [/have_unit]

            [then]
                [if]
                    [have_unit]
                        side=1,2,3,4 {COUNT_XY_RADIUS 2-99 30 7 2}
                    [/have_unit]

                    [then]
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 4 (x,y=28,8 )})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 3 (x,y=30,10)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 4 (x,y=32,8 )})}
                        {VARIABLE avoid2_northX ""}
                        {VARIABLE avoid2_northY ""}
                    [/then]
                    [else]
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 2 (x,y=29,11)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 3 (x,y=30,10)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 2 (x,y=31,11)})}
                        {VARIABLE avoid2_northX ",23-26,27,  28,  29,  30, 31,  32,  33,  34-37"} #expand our avoid zones
                        {VARIABLE avoid2_northY ",0-10, 0-11,0-10,0-10,0-9,0-10,0-10,0-11, 0-10"}
                    [/else]
                [/if]
            [/then]
        [/if]

        # SOUTHEAST BRIDGE
        # if this bridge is secured, ignore the sector completely
        # if this bridge is lost, focus on defending our leader
        [if]
            [have_unit]
                side=7
                canrecruit=yes
                [filter_location]
                    x,y=57,32
                    radius=5
                [/filter_location]
            [/have_unit]

            [then]
                [if]
                    [have_unit]
                        side=1,2,3,4 {COUNT_XY_RADIUS 2-99 47 31 2}
                    [/have_unit]

                    [then]
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 4 (x,y=47,29)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 3 (x,y=44,29)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 4 (x,y=45,32)})}
                        {VARIABLE avoid3_southeastX ""}
                        {VARIABLE avoid3_southeastY ""}
                    [/then]
                    [else]
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 2 (x,y=44,28)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 3 (x,y=44,29)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 2 (x,y=43,30)})}
                        {VARIABLE avoid3_southeastX ",41,   42,   43,   44,   45,   46,   47,   48,   49   "}
                        {VARIABLE avoid3_southeastY ",33-99,31-99,31-99,30-99,27-99,26-99,26-99,25-99,25-99"}
                    [/else]
                [/if]
            [/then]
        [/if]

        # SOUTHWEST BRIDGE
        # if this bridge is secured, ignore the sector completely
        # if this bridge is lost, focus on defending our leader
        [if]
            [have_unit]
                side=5
                canrecruit=yes
                [filter_location]
                    x,y=7,35
                    radius=5
                [/filter_location]
            [/have_unit]

            [then]
                [if]
                    [have_unit]
                        side=1,2,3,4 {COUNT_XY_RADIUS 2-99 13 31 2}
                    [/have_unit]

                    [then]
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 4 (x,y=15,32)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 3 (x,y=16,29)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 4 (x,y=13,29)})}
                        {VARIABLE avoid4_southwestX ""}
                        {VARIABLE avoid4_southwestY ""}
                    [/then]
                    [else]
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 2 (x,y=16,28)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 3 (x,y=16,29)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 2 (x,y=17,30)})}
                        {VARIABLE avoid4_southwestX ",12,   13,   14,   15,   16,   17,   18   "}
                        {VARIABLE avoid4_southwestY ",26-99,27-99,26-99,27-99,30-99,31-99,31-99"}
                    [/else]
                [/if]
            [/then]
        [/if]

        # NORTHEAST BRIDGE
        # if this bridge is secured, ignore the sector completely
        # if this bridge is lost, retreat to our home ramparts
        [if]
            [have_unit]
                side=7
                canrecruit=yes
                [filter_location]
                    x,y=56,9
                    radius=5
                [/filter_location]
            [/have_unit]

            [then]
                [if]
                    [have_unit]
                        side=1,2,3,4 {COUNT_XY_RADIUS 2-99 46 14 3}
                    [/have_unit]

                    [then]
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 4 (x,y=44,13)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 3 (x,y=43,13)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 3 (x,y=44,12)})}
                        {VARIABLE avoid2_northeastX ""}
                        {VARIABLE avoid2_northeastY ""}

                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 4 (x,y=47,16)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 3 (x,y=47,17)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 3 (x,y=48,16)})}
                        {VARIABLE avoid3_northeastX ""}
                        {VARIABLE avoid3_northeastY ""}
                    [/then]
                    [else]
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 3 (x,y=33,14)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 2 (x,y=33,15)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 2 (x,y=32,15)})}
                        {VARIABLE avoid2_northeastX ",33,34-41"} #expand our avoid zones
                        {VARIABLE avoid2_northeastY ",12, 0-99"}

                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 3 (x,y=42,25)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 2 (x,y=40,25)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 2 (x,y=41,25)})}
                        {VARIABLE avoid3_northeastX ",39, 0-99,45-99,47-99"}
                        {VARIABLE avoid3_northeastY ",25,18-24,18-26,18-27"}
                    [/else]
                [/if]
            [/then]
        [/if]

        # SOUTH BRIDGE
        # if this bridge is secured, ignore the sector completely
        # if this bridge is lost, retreat to our home ramparts
        [if]
            [have_unit]
                side=5
                canrecruit=yes
                [filter_location]
                    x,y=27,42
                    radius=5
                [/filter_location]
            [/have_unit]

            [then]
                [if]
                    [have_unit]
                        side=1,2,3,4 {COUNT_XY_RADIUS 2-99 30 36 3}
                    [/have_unit]

                    [then]
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 3 (x,y=32,35)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 2 (x,y=33,35)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 2 (x,y=33,36)})}
                        {VARIABLE avoid3_southX ""}
                        {VARIABLE avoid3_southY ""}

                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 3 (x,y=28,35)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 2 (x,y=27,36)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 2 (x,y=27,37)})}
                        {VARIABLE avoid4_southX ""}
                        {VARIABLE avoid4_southY ""}
                    [/then]
                    [else]
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 3 (x,y=39,30)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 2 (x,y=38,29)})}
                        {MODIFY_SIDE_AI (3) ({GOAL_LOCATION 2 (x,y=38,28)})}
                        {VARIABLE avoid3_southX ",33-37,38,   39,   40,   41   "}
                        {VARIABLE avoid3_southY ", 0-99,30-99,31-99,31-99,32-99"}

                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 3 (x,y=20,30)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 2 (x,y=22,29)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 2 (x,y=22,28)})}
                        {VARIABLE avoid4_southX ",23-27,22,   21,   20,   19   "}
                        {VARIABLE avoid4_southY ", 0-99,30-99,31-99,31-99,32-99"}
                    [/else]
                [/if]
            [/then]
        [/if]

        # NORTHWEST BRIDGE
        # if this bridge is secured, ignore the sector completely
        # if this bridge is lost, retreat to our home ramparts
        [if]
            [have_unit]
                side=6
                canrecruit=yes
                [filter_location]
                    x,y=6,13
                    radius=5
                [/filter_location]
            [/have_unit]

            [then]
                [if]
                    [have_unit]
                        side=1,2,3,4 {COUNT_XY_RADIUS 2-99 14 14 3}
                    [/have_unit]

                    [then]
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 4 (x,y=13,16)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 3 (x,y=12,16)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 3 (x,y=12,17)})}
                        {VARIABLE avoid4_northwestX ""}
                        {VARIABLE avoid4_northwestY ""}

                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 4 (x,y=16,13)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 3 (x,y=16,12)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 3 (x,y=17,12)})}
                        {VARIABLE avoid2_northwestX ""}
                        {VARIABLE avoid2_northwestY ""}
                    [/then]
                    [else]
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 3 (x,y=18,25)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 2 (x,y=19,25)})}
                        {MODIFY_SIDE_AI (4) ({GOAL_LOCATION 2 (x,y=20,25)})}
                        {VARIABLE avoid4_northwestX ", 0-99, 0-17, 0-15"}
                        {VARIABLE avoid4_northwestY ",18-24,18-25,18-26"}

                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 3 (x,y=27,14)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 2 (x,y=27,15)})}
                        {MODIFY_SIDE_AI (2) ({GOAL_LOCATION 2 (x,y=28,15)})}
                        {VARIABLE avoid2_northwestX ",27,19-26"} #expand our avoid zones
                        {VARIABLE avoid2_northwestY ",12, 0-99"}
                    [/else]
                [/if]
            [/then]
        [/if]

        # AVOIDS
        # AIs can only have 1 avoid tag at once, so merge them all together here
        {MODIFY_SIDE_AI (2) (
            [avoid]
                x="$avoid234_riverX $avoid2_northX $avoid2_northeastX $avoid2_northwestX"
                y="$avoid234_riverY $avoid2_northY $avoid2_northeastY $avoid2_northwestY"
            [/avoid]
        )}
        {MODIFY_SIDE_AI (3) (
            [avoid]
                x="$avoid234_riverX $avoid3_northeastX $avoid3_southeastX $avoid3_southX"
                y="$avoid234_riverY $avoid3_northeastY $avoid3_southeastY $avoid3_southY"
            [/avoid]
        )}
        {MODIFY_SIDE_AI (4) (
            [avoid]
                x="$avoid234_riverX $avoid4_northwestX $avoid4_southwestX $avoid4_southX"
                y="$avoid234_riverY $avoid4_northwestY $avoid4_southwestY $avoid4_southY"
            [/avoid]
        )}

        {CLEAR_VARIABLE avoid234_riverX,avoid234_riverY} # no spaces
        {CLEAR_VARIABLE avoid2_northX,avoid2_northY}
        {CLEAR_VARIABLE avoid2_northeastX,avoid2_northeastY}
        {CLEAR_VARIABLE avoid2_northwestX,avoid2_northwestY}
        {CLEAR_VARIABLE avoid3_northeastX,avoid3_northeastY}
        {CLEAR_VARIABLE avoid3_southeastX,avoid3_southeastY}
        {CLEAR_VARIABLE avoid3_southX,avoid3_southY}
        {CLEAR_VARIABLE avoid4_northwestX,avoid4_northwestY}
        {CLEAR_VARIABLE avoid4_southwestX,avoid4_southwestY}
        {CLEAR_VARIABLE avoid4_southX,avoid4_southY}

        #---------------------
        # IF NO ENEMIES TO FIGHT
        #---------------------
        # if we somehow kill 3 enemy leaders and a general has nothing left to do, reset back to vanilla AI

        # NORTH GENERAL
        [if]
            [not]
                [have_unit]
                    side=6
                    canrecruit=yes
                    [filter_location]
                        x,y=6,13
                        radius=5
                    [/filter_location]
                [/have_unit]
            [/not]

            [not]
                [have_unit]
                    side=6
                    canrecruit=yes
                    [filter_location]
                        x,y=28,2
                        radius=5
                    [/filter_location]
                [/have_unit]
            [/not]

            [not]
                [have_unit]
                    side=7
                    canrecruit=yes
                    [filter_location]
                        x,y=56,9
                        radius=5
                    [/filter_location]
                [/have_unit]
            [/not]

            [then]
                {MODIFY_UNIT side=2 side 3}
                {RESET_SIDE_AI 2 defensive -1 1}
                {MODIFY_SIDE_AI (2) (
                    [avoid] # reset doesn't reset avoids. It probably should, but at this point I don't want to make any sweeping changes
                        x,y=1,1
                    [/avoid]
                )}
            [/then]
        [/if]

        # SOUTHEAST GENERAL
        [if]
            [not]
                [have_unit]
                    side=7
                    canrecruit=yes
                    [filter_location]
                        x,y=56,9
                        radius=5
                    [/filter_location]
                [/have_unit]
            [/not]

            [not]
                [have_unit]
                    side=7
                    canrecruit=yes
                    [filter_location]
                        x,y=56,32
                        radius=5
                    [/filter_location]
                [/have_unit]
            [/not]

            [not]
                [have_unit]
                    side=5
                    canrecruit=yes
                    [filter_location]
                        x,y=27,42
                        radius=5
                    [/filter_location]
                [/have_unit]
            [/not]

            [then]
                {RESET_SIDE_AI 3 defensive -1 1}
                {MODIFY_SIDE_AI (3) (
                    [avoid] # reset doesn't reset avoids. It probably should, but at this point I don't want to make any sweeping changes
                        x,y=1,1
                    [/avoid]
                )}
            [/then]
        [/if]

        # SOUTHWEST GENERAL
        [if]
            [not]
                [have_unit]
                    side=5
                    canrecruit=yes
                    [filter_location]
                        x,y=27,42
                        radius=5
                    [/filter_location]
                [/have_unit]
            [/not]

            [not]
                [have_unit]
                    side=5
                    canrecruit=yes
                    [filter_location]
                        x,y=7,35
                        radius=5
                    [/filter_location]
                [/have_unit]
            [/not]

            [not]
                [have_unit]
                    side=6
                    canrecruit=yes
                    [filter_location]
                        x,y=7,13
                        radius=5
                    [/filter_location]
                [/have_unit]
            [/not]

            [then]
                {RESET_SIDE_AI 4 defensive -1 1}
                {MODIFY_SIDE_AI (4) (
                    [avoid] # reset doesn't reset avoids. It probably should, but at this point I don't want to make any sweeping changes
                        x,y=1,1
                    [/avoid]
                )}
            [/then]
        [/if]
    [/event]

#define INVADER_AI_STUFF
    color=teal
    controller=ai
    team_name=undead
    user_team_name=_"Undead"
    {GOLD 120 360 600}
    {INCOME 12 32 48}
    village_gold=0    #anti-snowballing - sacrificing the city streets should buy the player extra time, not screw him over because undead gets more income
    village_support=0 #anti-snowballing
    {FLAG_VARIANT undead}
#enddef
#define RETREAT_INVADER_AI
    {DISABLE_ATTACKING (5,6,7)} # need to disable these CAs, or else nothing else is very effective. For some reason this isn't necessary on other scenarios
    {MODIFY_SIDE_AI (5,6,7) ({GOAL_AVOID_SIDE_UNLESS_DEFENDING_LEADER 99 (5,6,7) (1,2,3,4) 2})} # avoid staying too close
    {MODIFY_SIDE_AI (5,6,7) ({GOAL_SEEK_SIDE                          88         (1,2,3,4) 5})} # but seek staying close enough
    {MODIFY_SIDE_AI (5,6,7) ({GOAL_SEEK_SIDE                          5          (5,6,7)   0})}
    {MODIFY_SIDE_AI (5,6,7) (aggression=-9)}
    {MODIFY_SIDE_AI (5,6,7) (caution=9)}
    {MODIFY_SIDE_AI (5,6,7) (retreat_factor=0)}
    [event] # we disabled the attack CAs, but some units end up adjacent to humans and look stupid if they don't attack
        name=side 8 turn refresh
        # use upkeep as a proxy to store the old side
        {MODIFY_UNIT (side,upkeep=5,full) upkeep 5} {MODIFY_UNIT (side,upkeep=6,full) upkeep 6} {MODIFY_UNIT (side,upkeep=7,full) upkeep 7}
        {MODIFY_UNIT (side,upkeep=5,5)    side   8} {MODIFY_UNIT (side,upkeep=6,6)    side   8} {MODIFY_UNIT (side,upkeep=7,7)    side   8}
        {MODIFY_UNIT (side=8) attacks_left 1} # ownership was changed after turn refresh, so manually restore their attack (but not their movement)
    [/event]
    [event]
        name=side 8 turn end
        # restore sides back to normal
        {MODIFY_UNIT side,upkeep=8,5 side 5} {MODIFY_UNIT side,upkeep=8,6 side 6} {MODIFY_UNIT side,upkeep=8,7 side 7}

        # restore upkeep back to normal
        {MODIFY_UNIT side,upkeep,level=5,5,0 upkeep 0} {MODIFY_UNIT side,upkeep,level=5,5,1 upkeep 1}
        {MODIFY_UNIT side,upkeep,level=5,5,2 upkeep 2} {MODIFY_UNIT side,upkeep,level=5,5,3 upkeep 3}

        {MODIFY_UNIT side,upkeep,level=6,6,0 upkeep 0} {MODIFY_UNIT side,upkeep,level=6,6,1 upkeep 1}
        {MODIFY_UNIT side,upkeep,level=6,6,2 upkeep 2} {MODIFY_UNIT side,upkeep,level=6,6,3 upkeep 3}

        {MODIFY_UNIT side,upkeep,level=7,7,0 upkeep 0} {MODIFY_UNIT side,upkeep,level=7,7,1 upkeep 1}
        {MODIFY_UNIT side,upkeep,level=7,7,2 upkeep 2} {MODIFY_UNIT side,upkeep,level=7,7,3 upkeep 3}
    [/event]
#enddef
#define RESET_INVADER_AI
    {ENABLE_ATTACKING (5,6,7)}
    {RESET_SIDE_AI (5,6,7) offensive 0.8 0.25}
    #     {MODIFY_SIDE_AI (5,6,7) (village_value=0)} # disabled; if the player kills a leader and takes their villages, we want to try to retake those and deny the player their income
    {MODIFY_SIDE_AI (5,6,7) (retreat_factor=0.25)}
    {MODIFY_AI_ADD_GOAL (5,6,7,8,9) (
        [goal]
            [criteria]
                id=Konrad
            [/criteria]
            value=5
        [/goal]
    )}
#enddef

    [side]
        type=Lich
        id=Mel Guthrak
        name= _ "Mel Guthrak"
        canrecruit=yes
        side=5
        recruit=Ghost,Wraith,Shadow,Skeleton Rider,Bone Knight,Chocobone,Dark Adept,Vampire Bat
        {INVADER_AI_STUFF}
        [unit]
            x,y=27,42
            type=Death Knight
            name= _ "Nakeg-alvan"
            id=Nakeg-alvan
            canrecruit=yes
        [/unit]
        {LOYAL_UNIT 5 (Chocobone) 8 38} {GUARDIAN}
        {LOYAL_UNIT 5 (Bone Knight) 12 38} {GUARDIAN}
        {LOYAL_UNIT 5 (Shadow) 4 35} {GUARDIAN}
        {LOYAL_UNIT 5 (Dark Adept) 3 32} {GUARDIAN}
        {LOYAL_UNIT 5 (Chocobone) 2 29} {GUARDIAN}
        {LOYAL_UNIT 5 (Spectre) 8 32} {GUARDIAN}
        [+unit]
            max_moves=3
        [/unit]

        {LOYAL_UNIT 5 (Wraith) 30 40} {GUARDIAN}
        [+unit]
            max_moves=2
        [/unit]

        {LOYAL_UNIT 5 (Spectre) 35 43} {GUARDIAN}
        [+unit]
            max_moves=6
        [/unit]

        {LOYAL_UNIT 5 (Chocobone) 38 39} {GUARDIAN}
        [+unit]
            max_moves=7
        [/unit]

        {LOYAL_UNIT 5 (Dark Adept) 26 44} {GUARDIAN}
        {LOYAL_UNIT 5 (Bone Knight) 24 43} {GUARDIAN}
    [/side]
    {STARTING_VILLAGES_AREA 5 37 42 5}
    {STARTING_VILLAGES_AREA 5 27 42 5}
    {STARTING_VILLAGES_AREA 5  9 39 5}
    {STARTING_VILLAGES_AREA 5  8 33 5}
    {STARTING_VILLAGES_AREA 5  2 25 5}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 5 Wraith     2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 5 Shadow     2} #(including 1x loyal)
    {LIMIT_CONTEMPORANEOUS_RECRUITS 5 Chocobone  2} #(including 1x loyal)
    {LIMIT_CONTEMPORANEOUS_RECRUITS 5 (Vampire Bat)  3}

    [side]
        type=Lich
        id=Mal-Xakralan
        name= _ "Mal-Xakralan"
        canrecruit=yes
        side=6
        recruit=Skeleton,Revenant,Deathblade,Skeleton Archer,Bone Shooter,Dark Adept,Vampire Bat
        {INVADER_AI_STUFF}
        [unit]
            x,y=6,13
            type=Death Knight
            name= _ "Nalu-alvan"
            id=Nalu-alvan
            canrecruit=yes
        [/unit]
        {LOYAL_UNIT 6 (Draug) 7 11} {GUARDIAN}
        {LOYAL_UNIT 6 (Deathblade) 5 9} {GUARDIAN}
        {LOYAL_UNIT 6 (Banebow) 3 10} {GUARDIAN}
        {LOYAL_UNIT 6 (Dark Adept) 3 12} {GUARDIAN}
        {LOYAL_UNIT 6 (Bone Shooter) 5 16} {GUARDIAN}

        {LOYAL_UNIT 6 (Revenant) 34 3} {GUARDIAN}
        [+unit]
            max_moves=4
        [/unit]

        {LOYAL_UNIT 6 (Dark Adept) 32 2} {GUARDIAN}
        [+unit]
            max_moves=3
        [/unit]

        {LOYAL_UNIT 6 (Banebow) 25 2} {GUARDIAN}
        {LOYAL_UNIT 6 (Draug) 26 4} {GUARDIAN}
    [/side]
    {STARTING_VILLAGES_AREA 6 32 3 5}
    {STARTING_VILLAGES_AREA 6 15 4 5}
    {STARTING_VILLAGES_AREA 6 6 13 5}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 5 (Vampire Bat)  3}

    [side]
        side=7
        type=Necromancer
        id=Mal-Analla
        name= _ "Mal-Analla"
        canrecruit=yes
        gender=female
        [modifications]
            {TRAIT_QUICK}
            {TRAIT_STRONG}
        [/modifications]
        recruit=Dark Sorcerer,Ghoul,Necrophage,Walking Corpse,Soulless,Dark Adept,Vampire Bat
        {INVADER_AI_STUFF}
        {LOYAL_UNIT 7 (Soulless) 55 13} {GUARDIAN}
        {LOYAL_UNIT 7 (Dark Sorcerer) 56 12} {GUARDIAN}
        {LOYAL_UNIT 7 (Necrophage) 51 7} {GUARDIAN}
        {LOYAL_UNIT 7 (Soulless) 55 7} {GUARDIAN}
        {LOYAL_UNIT 7 (Soulless) 50 8} {GUARDIAN}
        {LOYAL_UNIT 7 (Soulless) 53 9} {GUARDIAN}
        {LOYAL_UNIT 7 (Ghast) 57 6} {GUARDIAN}

        {LOYAL_UNIT 7 (Ghast) 54 34} {GUARDIAN}
        {LOYAL_UNIT 7 (Necrophage) 55 30} {GUARDIAN}
        {LOYAL_UNIT 7 (Dark Adept) 56 34} {GUARDIAN}
        {LOYAL_UNIT 7 (Dark Sorcerer) 59 30} {GUARDIAN}
        {LOYAL_UNIT 7 (Soulless) 60 27} {GUARDIAN}
        {LOYAL_UNIT 7 (Soulless) 58 35} {GUARDIAN}
        {LOYAL_UNIT 7 (Ghoul) 53 37} {GUARDIAN}
    [/side]
    {STARTING_VILLAGES_AREA 7 56 9 8}
    {STARTING_VILLAGES_AREA 7 59 21 5}
    {STARTING_VILLAGES 7 8}
    {STARTING_VILLAGES_AREA 7 56 12 5}
    {STARTING_VILLAGES_AREA 7 48 4 5}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 5 (Vampire Bat)  3}
    [event]
        name=prestart
        [if]
            {VARIABLE_CONDITIONAL skraat_dead not_equals yes}
            [not]
                [have_unit]
                    x,y=56,9
                [/have_unit]
            [/not]

            [then]
                [unit]
                    side=7
                    x,y=56,9
                    canrecruit=yes
                    type=Necromancer
                    id=Mal-Skraat
                    name=_"Mal-Skraat"
                    [modifications]
                        {TEAM_COLOR_OVERRIDE () darkred}
                        {TRAIT_INTELLIGENT}
                        {TRAIT_STRONG}
                    [/modifications]
                [/unit]
                {NAMED_UNIT 7 (Soulless) 58 10 "sibling_corpse" (_"Mal-Kallat") ()} {GUARDIAN} {FACING sw}
            [/then]
        [/if]
        [if]
            {VARIABLE_CONDITIONAL kallat_dead not_equals yes}
            [not]
                [have_unit]
                    x,y=56,9
                [/have_unit]
            [/not]

            [then]
                [unit]
                    side=7
                    x,y=56,9
                    canrecruit=yes
                    type=Necromancer
                    id=Mal-Kallat
                    name=_"Mal-Kallat"
                    gender=female
                    [modifications]
                        {TEAM_COLOR_OVERRIDE () darkred}
                        {TRAIT_INTELLIGENT}
                        {TRAIT_QUICK}
                    [/modifications]
                [/unit]
                {NAMED_UNIT 7 (Soulless) 58 10 "sibling_corpse" (_"Mal-Skraat") ()} {GUARDIAN} {FACING sw}
            [/then]
        [/if]
        [modify_unit]
            [filter]
                id=sibling_corpse
            [/filter]

            [object]
                [effect]
                    apply_to=overlay
                    add=misc/hero-icon.png
                [/effect]
            [/object]
        [/modify_unit]

        [if]
            [not]
                [have_unit]
                    x,y=56,9
                [/have_unit]
            [/not]
            [then]
                [unit]
                    side=7
                    x,y=56,9
                    type=Necromancer
                    name= _ "Mal-Saerav"
                    id=Mal-Saerav
                    canrecruit=yes
                    [modifications]
                        {TRAIT_RESILIENT}
                        {TRAIT_QUICK}
                    [/modifications]
                [/unit]
            [/then]
        [/if]
    [/event]
    [event]
        name=turn 3
        [message]
            speaker=Mal-Skraat
            message= _ "Gweddry, do you recognize me? Do you remember in the Estmarks, when you mercilessly cut down my defenseless sister? You escaped me once before, but now I will have my revenge!"
        [/message]
        [message]
            speaker=Mal-Kallat
            message= _ "Gweddry, do you recognize me? Do you remember in the Estmarks, when you mercilessly cut down my defenseless brother? I will kill you for what you have done."
        [/message]
        [message]
            speaker=sibling_corpse
            #po: Mal-Skraat or Mal-Kallat is still alive, this is their dead brother/sister reanimated as a Soulless
            message= _ "Rev... enge..."
        [/message]
    [/event]

    # dummy side
    [side]
        no_leader=yes
        hidden=yes
        side=8
        {INVADER_AI_STUFF}
    [/side]

    [event]
        name=prestart

        {RESET_INVADER_AI} # initialize AI

        {SILENTLY_LIMIT_LEADER_MOVES 2 1}
        {SILENTLY_LIMIT_LEADER_MOVES 3 1}
        {SILENTLY_LIMIT_LEADER_MOVES 4 1}
        {SILENTLY_LIMIT_LEADER_MOVES 5 1}
        {SILENTLY_LIMIT_LEADER_MOVES 6 1}
        {SILENTLY_LIMIT_LEADER_MOVES 7 1}

        {PLACE_IMAGE "scenery/throne-small.png" 30 22}
        [time_area]
            x,y=30,22
            radius=2
            [time]
                id=indoors
                name= _ "Indoors"
                image=misc/time-schedules/schedule-indoors.png
                lawful_bonus=25
            [/time]
        [/time_area]

        {PLACE_IMAGE scenery/dwarven-keep-tile.png 20 19}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 12 20}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 17 23}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 12 24}

        {PLACE_IMAGE scenery/dwarven-keep-tile.png 24 14}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 22 10}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 38 11}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 37 15}

        {PLACE_IMAGE scenery/dwarven-keep-tile.png 40 19}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 47 21}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 46 24}

        {PLACE_IMAGE scenery/dwarven-keep-tile.png 34 30}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 38 33}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 25 30}
        {PLACE_IMAGE scenery/dwarven-keep-tile.png 22 34}

        {PLACE_IMAGE "scenery/statue-haldric.png~GS()~FL(horiz)" 26 22}
        {PLACE_IMAGE "scenery/statue-delfador.png~GS()"          34 22}

        {PLACE_IMAGE scenery/shipwreck-1.png 43 39}
        {PLACE_IMAGE scenery/wreck-2.png     41 39}
        {PLACE_IMAGE scenery/wreck.png       44 42}
        {PLACE_IMAGE scenery/wreck-3.png     43 44}

        {BRAZIER_ILLUMINATION_LONG_NIGHT 26 21}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 26 23}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 34 21}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 34 23}

        {BRAZIER_ILLUMINATION_LONG_NIGHT 29 17}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 31 17}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 23 25}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 24 26}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 37 25}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 36 26}

        {BRAZIER_ILLUMINATION_LONG_NIGHT 30  8}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 44 15}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 40 17}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 46 30}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 30 34}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 30 31}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 14 30}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 16 15}
        {BRAZIER_ILLUMINATION_LONG_NIGHT 20 17}

        {SET_LABEL 30 22 _"Garard’s Hold"}
        {SET_LABEL 30 13 _"Merchants’ Guildhall"}
        {SET_LABEL 41 28 _"Basilica of Li’sar"}
        {SET_LABEL 19 28 _"Hall of Warriors"}

        [modify_unit]
            [filter]
                id=Gweddry,Owaec
            [/filter]

            [object]
                [effect]
                    apply_to=overlay
                    remove=misc/leader-crown.png
                [/effect]

                [effect]
                    apply_to=overlay
                    add=misc/leader-expendable.png
                [/effect]
            [/object]
        [/modify_unit]

        [objectives]
            side=1
            [objective]
                description= _ "Survive the undead assault"
                condition=win
                show_turn_counter=yes
            [/objective]
            [objective]
                description= _ "Protect generals Halrad, Halric, and Halrod"
                condition=win
                [show_if]
                    [have_unit]
                        id=Halrad,Halric,Halrod
                    [/have_unit]
                [/show_if]
                {BONUS_OBJECTIVE_CAPTION}
            [/objective]
            [objective]
                description= _ "Death of Dacyn"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Konrad II"
                condition=lose
            [/objective]

            [note]
                description= _ "Konrad II will not leave the center island."
            [/note]
            [note]
                description= _ "Generals will reinforce each bridgehead while they/you have defenders there, falling back to their castle otherwise."
            [/note]
            [note]
                description= _ "If you decide not to duel Mal-Ravanal, surviving generals will aid you in the next scenario."
            [/note]

            [gold_carryover]
                bonus=yes
                carryover_percentage=40
            [/gold_carryover]
        [/objectives]

        {RECALL_XY Konrad 30 22}
        {PUT_TO_RECALL_LIST id=Gweddry}
        [set_extra_recruit]
            id=Konrad
            extra_recruit={REGULAR_RECRUIT_LIST}
        [/set_extra_recruit]
    [/event]

    #start event
    [event]
        name=start

        {SCROLL_TO 30 8}
        [message]
            speaker=Halrad
            message= _ "Undead approach at the northern crossings. My forces are in position to engage the invaders."
        [/message]
        {SCROLL_TO 48 29}
        [message]
            speaker=Halric
            message= _ "The southeastern bridges are secure; hostiles inbound."
        [/message]
        {SCROLL_TO 13 29}
        [message]
            speaker=Halrod
            message= _ "My fortifications are manned and ready. Brace for attack from the southwest."
        [/message]

        {SCROLL_TO 30 22}

        {RECALL_XY Gweddry 32 23}
        {RECALL_XY Dacyn 28 23} {MODIFY_UNIT id=Dacyn facing sw}
        {RECALL_XY Owaec 30 20}
        {RECALL_XY Addogin 30 23}
        {RECALL_XY Terraent 30 23}
        {RECALL_XY Grug 30 23}
        {RECALL_XY Dolburras 30 23}
        {RECALL_XY (Hahid al-Ali) 30 23}
        {RECALL_XY Gaennell 30 23}
        [modify_unit]
            [filter]
                id=Addogin
            [/filter]

            [object]
                duration=scenario
                [effect]
                    apply_to=movement_costs
                    [movement_costs] # fugitive's concealment works on urban tiles. Let him move fast on those too - why not?
                        mountains=-2
                    [/movement_costs]
                [/effect]
            [/object]
        [/modify_unit]

        [message]
            speaker=Gweddry
            # TODO: the scenario's schedule has changed, this is currently being said at DEFAULT_SCHEDULE_MORNING.
            message= _ "Night approaches, and with it the undead hordes. I stand ready."
        [/message]
        [message]
            speaker=Owaec
            message= _ "Let us show these undead the might of Wesnoth!"
        [/message]
        [message]
            speaker=Konrad
            message= _ "And so it comes to this, here at what seems the end of days. Prepare for battle!"
        [/message]
    [/event]

    #--------------------
    # YANNIC RETURNS
    #--------------------
    [event]
        name=side 1 turn end
        [if]
            {VARIABLE_CONDITIONAL Yannic.length greater_than 0}
            [then]
                {UNSTORE_AND_RECALL Yannic Yannic 40 16} # side 2 right now
                {TEAM_COLOR_OVERRIDE id=Yannic brown}
                [modify_unit]
                    [filter]
                        id=Yannic
                    [/filter]

                    canrecruit=yes
                    experience="$($this_unit.max_experience-29)" # since he's been fighting a lot since we last saw him
                    [object]
                        [effect]
                            apply_to=overlay
                            remove=misc/leader-gold.png
                        [/effect]

                        [effect]
                            apply_to=overlay
                            add=misc/leader-expendable.png
                        [/effect]
                    [/object]
                    [filter_recall]
                        [not]
                            trait=undead
                        [/not]
                    [/filter_recall]
                [/modify_unit]
                [set_extra_recruit]
                    id=Yannic
                    extra_recruit={REGULAR_RECRUIT_LIST}
                [/set_extra_recruit]

                {MOVE_UNIT id=Yannic 33 20}
                [message]
                    speaker=Yannic
                    message= _ "So it is true, Owaec and Gweddry survived! When your forces had not returned to Wesnoth by winter I feared the worst. I am relieved to see you alive and well! This is the first good news I’ve heard of the war in quite some time."
                [/message]
                [message]
                    speaker=Yannic
                    message= _ "I’m afraid my Soradoc company was nearly wiped out in the massacre at the Plains, but the few of us who remain place ourselves at your service!"
                [/message]
                {MODIFY_UNIT id=Yannic side 1}
#define BONUS_UNIT X Y TYPE FACE XP COLOR TRAIT1 TRAIT2
    # no traits, because units with traits may sometimes have quick and may sometimes not have quick
    # this causes civilians to randomly block certain castle hexes that the player can use to recruit
    {NOTRAIT_UNIT 1 ({TYPE}) {X} {Y}}{ANIMATE}  {FACING {FACE}}
    [+unit]
        [modifications]
            {TRAIT1}
            {TRAIT2}
        [/modifications]
    [/unit]

    {TEAM_COLOR_OVERRIDE (x,y={X},{Y}) {COLOR}}
    [modify_unit]
        [filter]
            x,y={X},{Y}
        [/filter]

        experience={XP} # using formulas for high-XP units, in case unit XP values get rebalanced
    [/modify_unit]
#enddef
                {BONUS_UNIT 32 19 (Horseman)   sw "$($this_unit.max_experience -6)" brown   {TRAIT_RESILIENT}   {TRAIT_QUICK}     }
                {MODIFY_UNIT x,y=32,19 name _"Tarek"} # same guy from S07b
                {BONUS_UNIT 33 19 (Dragoon)    sw 21                                brown   {TRAIT_STRONG}      {TRAIT_RESILIENT} }
                {BONUS_UNIT 31 18 (Spearman)   sw "$($this_unit.max_experience -6)" brown   {TRAIT_QUICK}       {TRAIT_STRONG}    }
                {BONUS_UNIT 30 17 (Spearman)   sw "$($this_unit.max_experience-12)" brown   {TRAIT_INTELLIGENT} {TRAIT_RESILIENT} }
                {BONUS_UNIT 32 17 (Longbowman) sw 19                                brown   {TRAIT_RESILIENT}   {TRAIT_STRONG}    }
            [/then]
        [/if]
    [/event]

    #--------------------
    # TOWNSFOLK
    #--------------------
#define TOWNSFOLK X Y IMAGE TO_X TO_Y
    [event]
        name=prestart
        {NOTRAIT_UNIT 2 (Townsfolk) {X} {Y}}
        {STORE_UNIT_VAR x,y={X},{Y} id townsfolk_id}
        [object]
            [filter]
                x,y={X},{Y}
            [/filter]

            [effect]
                apply_to=image_mod
                add="O(0)~CROP(0,0,72,72)~BLIT(units/townsfolk/{IMAGE})"
            [/effect]
        [/object]
        [micro_ai]
            side=2
            action=add
            ai_type=goto

            [filter]
                id=$townsfolk_id
            [/filter]

            [filter_location]
                x,y={TO_X},{TO_Y}
            [/filter_location]
        [/micro_ai]
        [event]
            name=moveto
            delayed_variable_substitution=no

            [filter]
                id,x,y=$townsfolk_id,{TO_X}
            [/filter]

            {KILL id=$townsfolk_id}
        [/event]
        {CLEAR_VARIABLE townsfolk_id}
    [/event]
#enddef
    {TOWNSFOLK 19 15 scribe.png  26 18} # northwest
    {TOWNSFOLK 21 20 dandy.png   18 20}
    {TOWNSFOLK 22 12 worker.png  27 15}
    {TOWNSFOLK 26 11 worker.png  27 14}

    {TOWNSFOLK 26 28 noble+female.png  34 25} # south
    {TOWNSFOLK 33 30 worker.png  22 31}
    {TOWNSFOLK 39 32 worker.png  37 28}
    {TOWNSFOLK 29 31 worker+female.png 22 31}

    {TOWNSFOLK 37 19 madame.png  34 18} # northeast
    {TOWNSFOLK 39 23 noble.png   35 26}
    {TOWNSFOLK 42 14 crier.png   43 20}
    {TOWNSFOLK 37 13 worker+female.png   34 15}

    #--------------------
    # FLAVOR
    #--------------------
    [event]
        name=moveto

        [filter]
            id=Addogin
        [/filter]

        [message]
            speaker=Addogin
            message= _ "So this is Weldyn? Didn’t know cities got this big. Never thought I’d end up fightin’ here, of all places."
        [/message]
    [/event]
    [event]
        name=moveto

        [filter]
            id=Gaennell
        [/filter]

        [message]
            speaker=Mel Guthrak
            message= _ "Gaennell, you traitor! <b>I</b> am your master, not some petty king of men. Return to me and face your punishment."
        [/message]
        [message]
            speaker=Gaennell
            message= _ "You tried to have me killed over a petty accident! I’m not going to grovel at your feet and suffer your abuse any more."
        [/message]
    [/event]
    [event]
        name=attack

        [filter]
            id=Yannic
        [/filter]

        [message]
            speaker=Yannic
            message= _ "For my fallen brothers at Soradoc and the Plains!"
        [/message]
    [/event]
    [event]
        name=attack

        [filter]
            id=Konrad
        [/filter]

        [filter_second]
            [not]
                id=Mal-Ravanal
            [/not]
        [/filter_second]

        [filter_condition]
            {VARIABLE_CONDITIONAL konrad_spoke not_equals yes}
        [/filter_condition]
        # reused in S17b, if he doesn't speak here
        [message]
            speaker=Konrad
            message= _ "My legacy will not be one of failure! With all the might of the throne of Wesnoth, I will strike you down!"
        [/message]
        {VARIABLE konrad_spoke yes}
    [/event]

    #--------------------
    # CAPTURE A GENERAL'S VILLAGE
    #--------------------
    [event]
        name=capture
        [filter]
            side=1
        [/filter]
        [filter_condition]
            [variable]
                name=owner_side
                less_than_equal_to=4
            [/variable]
            [lua] # only at first, in case the player wants to take them all immediately
                code = << return wesnoth.current.turn<=5 >>
            [/lua]
        [/filter_condition]

        {FIND_NEARBY (side,canrecruit=$owner_side,yes) 30 22 99}
        [message]
            speaker=$found_unit.id
            #po: one of the AI generals when the player takes their village
            message= _ "Sire, you are of course welcome to capture any of my villages if you need them, but please know that it will make it more difficult to heal my injured soldiers."
        [/message]
    [/event]

    #--------------------
    # FRIST RETREAT
    #--------------------
    [event]
        name=turn 5
        {RETREAT_INVADER_AI}
    [/event]
    [event]
        name=turn 6
        {RESET_INVADER_AI}
    [/event]
    #--------------------
    # MAL-RAVANAL ARRIVES
    #--------------------
    [event]
        name=turn 6 # dawn. "side turn" ensures that the "new turn" doesn't trigger until next turn
        [message]
            speaker=Konrad
            #po: "light of dawn" is merely because it's that time of day
            message= _ "Stand strong, men of Wesnoth! Rally under the light of dawn, and drive the undead back!"
        [/message]

        # ravanal arrives
        {FIND_NEARBY (type=Skeleton,Skeleton Archer,Revenant,Bone Shooter,Draug,Banebow,Chocobone) 43 1 10}
        {SPAWN_IF_UNFOUND 5 (Revenant) 33 3}

        {RAVANAL_POSSESS 43 1 (x,y=$found_unit.x,$found_unit.y) (sw)}
        {CLEANUP_SEARCH}
        {REPLACE_SCENARIO_MUSIC silence.ogg}
        [message]
            speaker=Mal-Ravanal
            message= _ "Oh hello, am I interrupting something?"
        [/message]
        [message]
            speaker=Mal-Ravanal
            #po: according to the turn counter, it's dawn; Mal-Ravanal is claiming to be able to control that
            message= _ "It’s been fun watching this little squabble unfold, but it’s time to move things along. Night will now fall, and the city with it."
        [/message]

        # wizard's duel
        [message]
            speaker=Dacyn
            message= _ "Not if I stop you first! I defeated you before, and I will do so again!"
        [/message]

        {MODIFY_UNIT id=Dacyn facing nw}
        {DACYN_LIGHTNING}
        {COLOR_ADJUST 127 64 127}
        [delay]
            time=50
        [/delay]

        {MODIFY_UNIT id=Dacyn facing se}
        {DACYN_LIGHTNING}
        {COLOR_ADJUST -127 -191 -127}
        [delay]
            time=100
        [/delay]

        {COLOR_ADJUST 127 64 127}
        [delay]
            time=100
        [/delay]

        {COLOR_ADJUST 0 0 0}

        [message]
            speaker=Mal-Ravanal
            message= _ "Ah Dacyn, picked up a few tricks, have we? But your understanding is a bit shallow. Observe how it is done."
        [/message]
        [message]
            speaker=Mal-Ravanal
            message= _ "Bleed black, the fabric of firmament.
Open wide, the maw of the darkness between worlds.
Swallow the day and swallow the sun,
And leave eternal night to remain."
        [/message]
        {MODIFY_UNIT id=Mal-Ravanal facing sw}
        {RAVANAL_LIGHTNING}
        {COLOR_ADJUST 127 64 127}
        [delay]
            time=50
        [/delay]

        {MODIFY_UNIT id=Mal-Ravanal facing ne}
        {RAVANAL_LIGHTNING}
        {COLOR_ADJUST -127 -191 -127}
        [delay]
            time=50
        [/delay]

        {MODIFY_UNIT id=Mal-Ravanal facing nw}
        {RAVANAL_LIGHTNING}
        {COLOR_ADJUST 127 64 127}
        [delay]
            time=50
        [/delay]

        {MODIFY_UNIT id=Mal-Ravanal facing se}
        {RAVANAL_LIGHTNING}
        {COLOR_ADJUST 127 64 127}
        [delay]
            time=50
        [/delay]

        {COLOR_ADJUST -127 -191 -127}
        [delay]
            time=100
        [/delay]

        {COLOR_ADJUST 0 0 0}

        # darkness, fog, and extra gold
        [modify_side]
            side=1
            fog=yes
        [/modify_side]
        {RAVANAL_DEPOSSESS 43 1}
        [replace_schedule]
            {FIRST_WATCH}
            {FIRST_WATCH}
            {FIRST_WATCH}
            {FIRST_WATCH}
            {FIRST_WATCH}
            {FIRST_WATCH}
            {SECOND_WATCH}
            {SECOND_WATCH}
            {SECOND_WATCH}
            {SECOND_WATCH}
            {SECOND_WATCH}
            {DAWN}
            {DAWN}
            {MORNING}
            {MORNING} # turn 20
        [/replace_schedule]
        [gold]
            side=5,6,7
            amount={ON_DIFFICULTY 80 240 400}
        [/gold]
        [redraw]
            clear_shroud=yes
        [/redraw]
        [sound]
            name=magic-faeriefire.ogg
        [/sound]

        [delay]
            time=1500
        [/delay]

        # exclamations of shock
        [message]
            speaker=Grug
            message= _ "Dark scary! Not like!"
        [/message]
        [message]
            speaker=Dolburras
            message= _ "What manner o’ dark sorcery be this?"
        [/message]
        [if]
            [have_unit]
                id=Gweddry
            [/have_unit]

            [then]
                [message]
                    speaker=Gweddry
                    #po: as well as changing the time-of-day schedule, fog of war has just been enabled
                    message= _ "I’m nearly blind in this fog, but I think the undead have been reinforced!"
                [/message]
            [/then]
            [else]
                [if]
                    [have_unit]
                        id=Owaec
                    [/have_unit]

                    [then]
                        [message]
                            speaker=Owaec
                            message= _ "This cursed fog has clouded my sight, but I think I hear the sounds of reinforcing undead!"
                        [/message]
                    [/then]
                    [else]
                        {FIND_NEARBY (side,canrecruit=2-4,yes) 30 22 99} # one of the generals, if any is alive
                        [message]
                            speaker=$found_unit.id
                            message= _ "Look sharp; they’re reinforcing under the cover of fog."
                        [/message]
                    [/else]
                [/if]
            [/else]
        [/if]
        [message]
            speaker=Terraent
            message= _ "By the light of all that is holy, we must not fail!"
        [/message]

        {REPLACE_SCENARIO_MUSIC the_dangerous_symphony.ogg}
        {APPEND_MUSIC heavens-remix.ogg}
        {APPEND_MUSIC vengeful.ogg}
        {APPEND_MUSIC siege_of_laurelmor.ogg}

        [event]
            name=new turn
            [event]
                name=new turn
                [scroll_to_unit]
                    id=Dacyn
                [/scroll_to_unit]
                [delay]
                    time=500
                [/delay]

                [message]
                    speaker=Dacyn
                    caption= _ "Unknown"
                    image=units/unknown-unit1.png
                    #po: speaker is one of the voices from the amulet, speaking to Dacyn
                    message= _ "<span size='small' font-style='italic'>You see? Mal-Ravanal has grown too powerful. Wesnoth is helpless!</span>"
                [/message]
                [message]
                    speaker=Dacyn
                    caption= _ "Unknown 2"
                    image=units/unknown-unit2.png
                    #po: speaker is one of the voices from the amulet, speaking to Dacyn
                    message= _ "<span size='small' font-style='italic'>Use the Amulet. There is no other way! Use us!</span>"
                [/message]
                [message]
                    speaker=Dacyn
                    #po: after this, Dacyn gets unpoisonable, undrainable, unplagueable and changes to a Fallen Mage
                    message= _ "I must... defeat Ravan! I must be strong enough to stand alone... I alone must wield this fullness of power!"
                [/message]
                [sound]
                    name=magic-twilight.ogg
                [/sound]

                [remove_trait]
                    id=Dacyn
                    trait_id=intelligent
                [/remove_trait]
                [modify_unit]
                    [filter]
                        id=Dacyn
                    [/filter]

                    {TRAIT_AUDACIOUS}
                    [object]
                        id=rotting
                        duration=forever
                        [effect]
                            apply_to=status
                            add=unpoisonable
                        [/effect]
                        [effect]
                            apply_to=status
                            add=undrainable
                        [/effect]
                        [effect]
                            apply_to=status
                            add=unplagueable
                        [/effect]
                    [/object]
                [/modify_unit]
                {STORE_UNIT_VAR id=Dacyn experience dacyn_xp}
                {ADVANCE_UNIT id=Dacyn (Fallen Mage)}
                {MODIFY_UNIT id=Dacyn experience $dacyn_xp}
                {CLEAR_VARIABLE dacyn_xp}
            [/event]
        [/event]
    [/event]

    #--------------------
    # SECOND RETREAT
    #--------------------
    [event]
        name=turn 9
        [gold]
            side=5,6,7
            amount={ON_DIFFICULTY 60 180 300}
        [/gold]
    [/event]
    [event]
        name=turn 10
        {RETREAT_INVADER_AI}
        [event]
            name=side 4 turn end
            {FIND_NEARBY (side,canrecruit=2-4,yes) 30 22 99} # one of the generals, if any is alive
            [message]
                speaker=$found_unit.id
                message= _ "Careful! They’re regrouping for another wave."
            [/message]
        [/event]
    [/event]
    [event]
        name=turn 11
        {RESET_INVADER_AI}
    [/event]

    #--------------------
    # THIRD RETREAT
    #--------------------
    [event]
        name=turn 14
        [gold]
            side=5,6,7
            amount={ON_DIFFICULTY 120 360 600}
        [/gold]
    [/event]
    [event]
        name=turn 15
        {RETREAT_INVADER_AI}
    [/event]
    [event]
        name=turn 16
        {RESET_INVADER_AI}
    [/event]

    #--------------------
    # DAWN COMES, CLEAR THE FOG
    #--------------------
    [event]
        name=turn 17
        [if]
            [have_unit]
                id=Terraent
            [/have_unit]

            [then]
                [message]
                    speaker=Terraent
                    message= _ "The first rays of the blessed dawn pierce through the fog! The lich’s foul spell begins to fail!"
                [/message]
            [/then]
            [else]
                [if]
                    [have_unit]
                        id=Owaec
                    [/have_unit]

                    [then]
                        [message]
                            speaker=Owaec
                            message= _ "Take heart, for victory is near at hand! The first rays of sunlight have pierced through Mal-Ravanal’s night fog."
                        [/message]
                    [/then]
                    [else]
                        [if]
                            [have_unit]
                                id=Gweddry
                            [/have_unit]

                            [then]
                                [message]
                                    speaker=Gweddry
                                    message= _ "Look, the first rays of sunlight have pierced through the fog! Mal-Ravanal’s spell has begun to fail. I only wish Owaec were here to see it..."
                                [/message]
                            [/then]
                            [else]
                                [message]
                                    speaker=Dacyn
                                    #po: Gweddry and Owaec are dead during this scenario, and Terraent isn't here either
                                    message= _ "The sun has pierced through the night’s fog; Mal-Ravanal’s spell begins to wane. I need neither Gweddry nor Owaec. I will finish this battle on my own."
                                [/message]
                            [/else]
                        [/if]
                    [/else]
                [/if]
            [/else]
        [/if]
        [modify_side]
            side=1
            fog=no
        [/modify_side]
    [/event]

    #--------------------
    # UNDEAD START TO DIE
    #--------------------
#define KILL_UNDEAD_NEAR_HUMANS SIDE ANIMATE RADIUS
    [kill]
        side={SIDE}
        trait=undead
        canrecruit=no
        [filter_location]
            [filter]
                side=1,2,3,4
            [/filter]
            radius={RADIUS}
        [/filter_location]
        animate={ANIMATE}
    [/kill]
#enddef
    [event]
        name=side 5 turn 18
        {KILL_UNDEAD_NEAR_HUMANS 5 yes 3}
        [message]
            speaker=Dacyn
            #po: any undead unit from side 5 (except the leader) that was within 3 hexes of a living unit has died
            #po: followed immediately after this line by similar for sides 6 and 7.
            message= _ "Mal-Ravanal’s necromancers have overexerted themselves. They are struggling to maintain all their minions."
        [/message]
    [/event]
    [event]
        name=side 6 turn 18
        {KILL_UNDEAD_NEAR_HUMANS 6 yes 3}
    [/event]
    [event]
        name=side 7 turn 18
        {KILL_UNDEAD_NEAR_HUMANS 7 yes 5} # lots of corpses
    [/event]

    #--------------------
    # FINAL RETREAT, VICTORY
    #--------------------
    [event]
        name=side 5 turn 19
        {KILL_UNDEAD_NEAR_HUMANS 5 yes 6}
        {KILL_UNDEAD_NEAR_HUMANS 6 yes 6}
        {KILL_UNDEAD_NEAR_HUMANS 7 yes 6} # lots of corpses

        {DISABLE_ATTACKING (5,6,7)}
        {MODIFY_SIDE_AI (5,6,7) ({GOAL_LOCATION 9 (
            x=6, 28,56,57,27,7 # back to the leaders
            y=13,2, 9, 32,42,35
            radius=6
        )}) }
        {MODIFY_SIDE_AI (5,6,7) (aggression=-9)}
        {MODIFY_SIDE_AI (5,6,7) (caution=9)}
        {MODIFY_SIDE_AI (5,6,7) (retreat_factor=0)}
    [/event]
    [event]
        name=side 5 turn 19
        [if]
            [have_unit]
                id=Gweddry
            [/have_unit]

            [then]
                [message]
                    speaker=Gweddry
                    message= _ "The undead are falling back! Thank the Light; it’s finally over."
                [/message]
            [/then]
            [else]
                [if]
                    [have_unit]
                        id=Owaec
                    [/have_unit]

                    [then]
                        [message]
                            speaker=Owaec
                            message= _ "Gweddry has not died in vain; the undead resolve is broken!"
                        [/message]
                    [/then]
                    [else]
                        [message]
                            speaker=Konrad
                            message= _ "At last, the undead end their assault."
                        [/message]
                    [/else]
                [/if]
            [/else]
        [/if]
    [/event]
    [event]
        name=side 6 turn 19
        {KILL_UNDEAD_NEAR_HUMANS 5 yes 6}
        {KILL_UNDEAD_NEAR_HUMANS 6 yes 6}
        {KILL_UNDEAD_NEAR_HUMANS 7 yes 6} # lots of corpses
    [/event]
    [event]
        name=side 7 turn 19
        {KILL_UNDEAD_NEAR_HUMANS 5 yes 6}
        {KILL_UNDEAD_NEAR_HUMANS 6 yes 6}
        {KILL_UNDEAD_NEAR_HUMANS 7 yes 6} # lots of corpses
    [/event]

    # Konrad refuses to flee his castle
    [event]
        name=enter hex
        first_time_only=no
        [filter]
            id=Konrad
            [filter_location]
                x=0-27, 28,  28,    29,  29,    30,  30,    31,  31,    32,  32,    33-99
                y=0-99, 0-20,24-99, 0-20,25-99, 0-19,25-99, 0-20,25-99, 0-20,24-99, 0-99
            [/filter_location]
        [/filter]
        [cancel_action]
        [/cancel_action]
        [message]
            speaker=Konrad
            #po: the scenario doesn't let the player move Konrad away from his castle
            message= _ "I will not abandon my post!"
        [/message]
        {MOVE_UNIT (id=Konrad) $x2 $y2}
        [allow_undo]
        [/allow_undo]
    [/event]

    # statue flavor
    [event]
        name=moveto
        [filter]
            side=1
            x,y=26,22
        [/filter]
        [message]
            speaker=narrator
            #po: plaque on a statue
            message= _ "<i>Haldric the First, Dragonslayer, first king of Wesnoth.</i>"
            image=wesnoth-icon.png
        [/message]
        [message]
            speaker=Konrad
            message= _ "My father was named after him, and the royal sceptre hails from his line. Sadly, the histories say that he became paranoid in his old age."
        [/message]
    [/event]
    [event]
        name=moveto
        [filter]
            side=1
            x,y=34,22
        [/filter]
        [message]
            speaker=narrator
            #po: plaque on a statue
            message= _ "<i>Delfador the Great, sage of Wesnoth, restorer of the throne.</i>"
            image=wesnoth-icon.png
        [/message]
        [message]
            speaker=Dacyn
            message= _ "My predecessor, ‘the Great’. I intend to be greater."
        [/message]
    [/event]

    # death lines
    [event]
        name=last breath
        [filter]
            id=Mel Guthrak
        [/filter]
        [message]
            speaker=Mel Guthrak
            #po: last breath of Mel Guthrak
            message= _ "Wha- but how?"
        [/message]
        [message]
            speaker=Gaennell
            #po: last breath of Mel Guthrak
            message= _ "Good riddance..."
        [/message]
        [message]
            speaker=Halrod
            #po: last breath of Mel Guthrak
            message= _ "Impressive tactics! I’ll begin redeploying towards other threats."
        [/message]
    [/event]
    [event]
        name=last breath
        [filter]
            id=Nakeg-alvan
        [/filter]
        [message]
            speaker=Nakeg-alvan
            message= _ "You waste... your effort..."
        [/message]
        [message]
            speaker=Halrod
            message= _ "Well done! This will free my soldiers to defend other sectors of the city."
        [/message]
    [/event]
    [event]
        name=last breath
        [filter]
            id=Nalu-alvan
        [/filter]
        [message]
            speaker=Nalu-alvan
            message= _ "Your victory... is meaningless..."
        [/message]
        [message]
            speaker=Halrad
            message= _ "Excellent work! I’ll send my soldiers to the other fronts."
        [/message]
    [/event]
    [event]
        name=last breath
        [filter]
            id=Mal-Xakralan
        [/filter]
        [message]
            speaker=Mal-Xakralan
            message= _ "Mal-Ravanal will make you suffer for this!"
        [/message]
        [message]
            speaker=Halrad
            message= _ "Impressive tactics! I’ll begin redeploying towards other threats."
        [/message]
    [/event]
    [event]
        name=die # so it's after the skraat/kallat-specific lines
        [filter]
            id=Mal-Saerav,Mal-Skraat,Mal-Kallat
        [/filter]
        [message]
            speaker=Halric
            message= _ "Well done! This will free my soldiers to defend other sectors of the city."
        [/message]
    [/event]
    [event]
        name=last breath
        [filter]
            id=Mal-Analla
        [/filter]
        [message]
            speaker=Mal-Analla
            message= _ "No! I was supposed to... become immortal..."
        [/message]
        [message]
            speaker=Halric
            message= _ "Excellent work! I’ll send my soldiers to the other fronts."
        [/message]
    [/event]

    # Mal-Skraat / Mal-Kallat events
    [event]
        name=last breath
        [filter]
            id=Mal-Skraat,Mal-Kallat
        [/filter]
        [message]
            speaker=unit
            message= _ "No! Now I will never have my revenge..."
        [/message]
    [/event]
    [event]
        name=last breath
        [filter]
            id=sibling_corpse
        [/filter]
        [message]
            speaker=unit
            #po: last breath of the reanimated (Soulless) Mal-Skraat or Mal-Kallat
            message= _ "Gruhhh..."
        [/message]
    [/event]

    # you can win either by waiting till the turns run out or by defeating the enemies
    # (though defeating all enemies is very unlikely at high difficulties)
    [event]
        name=time over
        [fire_event]
            name=choice
        [/fire_event]
    [/event]
    [event]
        name=enemies defeated
        [fire_event]
            name=choice
        [/fire_event]
    [/event]
    [event]
        name=choice
        [if]
            {VARIABLE_CONDITIONAL failed_achievement not_equals yes}
            [then]
                [set_achievement]
                    content_for=eastern_invasion
                    id=ei_S16
                [/set_achievement]
            [/then]
        [/if]
        [if]
            [have_unit]
                id=Owaec
            [/have_unit]

            [then]
                [if]
                    [have_unit]
                        id=Gweddry
                    [/have_unit]

                    #--------------------
                    # EVERYONE IS ALIVE
                    #--------------------
                    [then]
                        [message]
                            speaker=Owaec
                            message= _ "Huzzah! Even Mal-Ravanal’s infernal darkness could not dim the spirits of our men! My friends, we have won a great victory today."
                        [/message]
                        [message]
                            speaker=Owaec
                            message= _ "Now we must press our advantage! Let us strike the undead while they are in disarray and banish Mal-Ravanal’s dark soul once and for all!"
                        [/message]
                        [message]
                            speaker=Dacyn
                            message= _ "I will not put the fate of all Irdya in the hands of a vengeful Clansman and Weldyn’s tattered armies. I must face Mal-Ravanal in a duel, and I must be the one to strike the infernal lich down! I will trust none other than myself with such responsibility."
                        [/message]
                        [message]
                            speaker=Konrad
                            message= _ "Both of you alike speak wisdom. This is a difficult decision, and I have little time in which to make it."
                        [/message]
                        [message]
                            speaker=Konrad
                            message= _ "Lieutenant Gweddry, you are familiar with the situation, and you know both Owaec and Dacyn well. Whose plan do you suggest I follow?"
                        [/message]
                        [message]
                            speaker=Gweddry
                            message= _ "I think we should..."
                            [option]
                                label= _ "challenge Mal-Ravanal to a duel. <i>(normal)</i>"
                                [command]
                                    [message]
                                        speaker=Konrad
                                        message= _ "I agree. Dacyn, send the message at once."
                                    [/message]
                                    [endlevel]
                                        result=victory
                                        next_scenario=17a_The_Duel
                                        {NEW_GOLD_CARRYOVER 0} # no need for extra gold here
                                    [/endlevel]
                                [/command]
                            [/option]
                            [option]
                                label= _ "defeat Mal-Ravanal in pitched battle. <i>(challenging)</i>"
                                [command]
                                    [message]
                                        speaker=Konrad
                                        message= _ "I agree. Owaec, begin preparing our armies."
                                    [/message]
                                    [endlevel]
                                        result=victory
                                        next_scenario=17b_All-In
                                        {NEW_GOLD_CARRYOVER 40}
                                    [/endlevel]
                                [/command]
                            [/option]
                        [/message]
                    [/then]

                    #--------------------
                    # OWAEC ALIVE, GWEDDRY DEAD
                    #--------------------
                    [else]
                        [message]
                            speaker=Owaec
                            message= _ "Huzzah! Even Mal-Ravanal’s infernal darkness could not dim the spirits of our men! My friends, we have won a great victory today. Alas, if only Gweddry were here..."
                        [/message]
                        [message]
                            speaker=Owaec
                            message= _ "... I am certain he would tell us to muster our resolve and regroup to press our advantage! Let us strike the undead while they are in disarray and banish Mal-Ravanal’s dark soul once and for all!"
                        [/message]
                        [message]
                            speaker=Dacyn
                            message= _ "I will not put the fate of all Irdya in the hands of a vengeful Clansman and Weldyn’s tattered armies. I must face Mal-Ravanal in a duel, and I must be the one to strike the infernal lich down! I will trust none other than myself with such responsibility."
                        [/message]
                        [message]
                            speaker=Konrad
                            message= _ "Both of you alike speak wisdom. This is a difficult decision, and I have little time in which to make it. I wish that Gweddry still lived to advise me, but it seems I must decide on my own."
                        [/message]
                        [message]
                            speaker=Konrad
                            message= _ "By my command, we will..."
                            [option]
                                label= _ "challenge Mal-Ravanal to a duel. <i>(normal)</i>"
                                [command]
                                    [message]
                                        speaker=Dacyn
                                        message= _ "Excellent, I will send the message at once."
                                    [/message]
                                    [endlevel]
                                        result=victory
                                        next_scenario=17a_The_Duel
                                        {NEW_GOLD_CARRYOVER 40}
                                    [/endlevel]
                                [/command]
                            [/option]
                            [option]
                                label= _ "defeat Mal-Ravanal in pitched battle. <i>(challenging)</i>"
                                [command]
                                    [message]
                                        speaker=Owaec
                                        message= _ "Excellent, I will begin preparing our armies!"
                                    [/message]
                                    [endlevel]
                                        result=victory
                                        next_scenario=17b_All-In
                                        {NEW_GOLD_CARRYOVER 40}
                                    [/endlevel]
                                [/command]
                            [/option]
                        [/message]
                    [/else]
                [/if]
            [/then]

            [else]
                [if]
                    [have_unit]
                        id=Gweddry
                    [/have_unit]

                    #--------------------
                    # OWAEC DEAD, GWEDDRY ALIVE
                    #--------------------
                    [then]
                        [message]
                            speaker=Gweddry
                            #po: although there's a lot of dead troops, this is mainly "to mourn for Owaec"
                            message= _ "The city grows still at last. I wish there was time to mourn, but we still must decide how to deal with Mal-Ravanal."
                        [/message]
                        [message]
                            speaker=Gweddry
                            message= _ "Owaec had wished for us to strike at the undead and isolate Mal-Ravanal by force of arms. With their armies in disarray after the failed attack on Weldyn, this may be our best opportunity."
                        [/message]
                        [message]
                            speaker=Dacyn
                            message= _ "I will not put the fate of all Irdya in the hands of Weldyn’s feeble defenders! I must face Mal-Ravanal in a duel, and I must be the one to strike the infernal lich down! I will trust none other than myself with such responsibility."
                        [/message]
                        [message]
                            speaker=Konrad
                            message= _ "Both of you alike speak wisdom. This is a difficult decision, and I have little time in which to make it."
                        [/message]
                        [message]
                            speaker=Konrad
                            message= _ "By my command, we will..."
                            [option]
                                label= _ "challenge Mal-Ravanal to a duel. <i>(normal)</i>"
                                [command]
                                    [message]
                                        speaker=Dacyn
                                        message= _ "Excellent, I will send the message at once."
                                    [/message]
                                    [endlevel]
                                        result=victory
                                        next_scenario=17a_The_Duel
                                        {NEW_GOLD_CARRYOVER 40}
                                    [/endlevel]
                                [/command]
                            [/option]
                            [option]
                                label= _ "defeat Mal-Ravanal in pitched battle. <i>(challenging)</i>"
                                [command]
                                    [message]
                                        speaker=Gweddry
                                        message= _ "I will begin preparing our armies."
                                    [/message]
                                    [endlevel]
                                        result=victory
                                        next_scenario=17b_All-In
                                        {NEW_GOLD_CARRYOVER 40}
                                    [/endlevel]
                                [/command]
                            [/option]
                        [/message]
                    [/then]

                    #--------------------
                    # GWEDDRY AND OWAEC BOTH DEAD
                    #--------------------
                    [else]
                        [message]
                            speaker=Konrad
                            message= _ "The city grows still at last. This battle is won, but with our best commanders dead we have little hope of surviving another attack."
                        [/message]
                        [message]
                            speaker=Konrad
                            message= _ "Owaec had wished for us to strike at the undead and isolate Mal-Ravanal by force of arms. With their armies in disarray after the failed attack on Weldyn, this may be our best opportunity."
                        [/message]
                        [message]
                            speaker=Dacyn
                            message= _ "We cannot put the fate of all Irdya in the hands of Weldyn’s feeble defenders! I must face Mal-Ravanal in a duel, and I must be the one to strike the infernal lich down! I will trust none other than myself with such responsibility."
                        [/message]
                        [message]
                            speaker=Konrad
                            message= _ "This is a difficult decision, and I have little time in which to make it. I wish that Gweddry or Owaec still lived to advise me, but it seems I must decide on my own."
                        [/message]
                        [message]
                            speaker=Konrad
                            message= _ "By my command, we will..."
                            [option]
                                label= _ "challenge Mal-Ravanal to a duel. <i>(regular)</i>"
                                [command]
                                    [message]
                                        speaker=Dacyn
                                        message= _ "Excellent, I will send the message at once."
                                    [/message]
                                    [endlevel]
                                        result=victory
                                        next_scenario=17a_The_Duel
                                        {NEW_GOLD_CARRYOVER 40}
                                    [/endlevel]
                                [/command]
                            [/option]
                            [option]
                                #po: both Gweddry and Owaec are dead, so this goes from "challenging" to "very hard"
                                label= _ "defeat Mal-Ravanal in pitched battle. <i>(very hard)</i>"
                                [command]
                                    [message]
                                        speaker=Dacyn
                                        message= _ "This is an unwise decision, but I will aid our armies as best I can."
                                    [/message]
                                    [endlevel]
                                        result=victory
                                        next_scenario=17b_All-In
                                        {NEW_GOLD_CARRYOVER 40}
                                    [/endlevel]
                                [/command]
                            [/option]
                        [/message]
                    [/else]
                [/if]
            [/else]
        [/if]
    [/event]

    [event]
        name=side 1 turn end
        [if]
            [have_unit]
                trait=TRAIT_{ID_CRYSTAL_QUIVER},TRAIT_{ID_HOLY_AMULET_1},TRAIT_{ID_HOLY_AMULET_2},TRAIT_{ID_HOLY_AMULET_3},TRAIT_{ID_SENTINEL},TRAIT_{ID_YETIBURGER},TRAIT_{ID_BANEBLADE},TRAIT_{ID_BARKSKIN},TRAIT_{ID_INVISIBILITY},TRAIT_{ID_PLAGUE_STAFF},TRAIT_ant_ambrosia01,TRAIT_ant_ambrosia02,TRAIT_ant_ambrosia03,TRAIT_ant_ambrosia04,TRAIT_ant_ambrosia05,TRAIT_ant_ambrosia06,TRAIT_ant_ambrosia07,TRAIT_ant_ambrosia08,TRAIT_ant_ambrosia09,TRAIT_ant_ambrosia10,TRAIT_ant_ambrosia11,TRAIT_ant_ambrosia12,TRAIT_ant_ambrosia13,TRAIT_ant_ambrosia14,TRAIT_ant_ambrosia15,TRAIT_ant_ambrosia16,TRAIT_ant_ambrosia17,TRAIT_ant_ambrosia18,TRAIT_ant_ambrosia19,TRAIT_ant_ambrosia20
            [/have_unit]

            [then]
                {VARIABLE failed_achievement yes}
            [/then]
        [/if]
    [/event]
    [event]
        name=recall
        first_time_only=no

        [filter]
            side=1
        [/filter]

        [filter_condition]
            {VARIABLE_CONDITIONAL failed_achievement not_equals yes}
        [/filter_condition]

        {VARIABLE failed_achievement yes}
        [allow_undo]
        [/allow_undo]
        [on_undo]
            {VARIABLE failed_achievement no}
        [/on_undo]
    [/event]

    # How many generals are still alive? Halrad NW, Halric NE, Halrod S
    [event]
        name=victory

        {CLEAR_VARIABLE failed_achievement,skraat_dead,kallat_dead}

        [if]
            [have_unit]
                id=Halrad
            [/have_unit]

            [then]
                [store_unit]
                    [filter]
                        id=Halrad
                    [/filter]
                    variable=Halrad
                    kill=yes
                [/store_unit]
            [/then]
            [else]
                [set_variable]
                    name=Halrad
                    value=""
                [/set_variable]
            [/else]
        [/if]
        [if]
            [have_unit]
                id=Halric
            [/have_unit]

            [then]
                [store_unit]
                    [filter]
                        id=Halric
                    [/filter]
                    variable=Halric
                    kill=yes
                [/store_unit]
            [/then]
            [else]
                [set_variable]
                    name=Halric
                    value=""
                [/set_variable]
            [/else]
        [/if]
        [if]
            [have_unit]
                id=Halrod
            [/have_unit]

            [then]
                [store_unit]
                    [filter]
                        id=Halrod
                    [/filter]
                    variable=Halrod
                    kill=yes
                [/store_unit]
            [/then]
            [else]
                [set_variable]
                    name=Halrod
                    value=""
                [/set_variable]
            [/else]
        [/if]
    [/event]

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

    {HERODEATH_KONRAD}
    {HERODEATH_YANNIC}
    {HERODEATH_HALRAD}
    {HERODEATH_HALRIC}
    {HERODEATH_HALROD}
[/scenario]

#undef INVADER_AI_STUFF
#undef BONUS_UNIT
