#textdomain wesnoth-dm
[scenario]
    id=21_Clash_at_the_Manor
    name= _ "Clash at the Manor"
    next_scenario="22_Face_of_the_Enemy"
    victory_when_enemies_defeated=no
    {MEMOIRS_MAP 21_Clash_at_the_Manor.map}
    {TURNS 40 32 30}
    {DEFAULT_SCHEDULE}

    {INTRO_AND_SCENARIO_MUSIC nunc_dimittis.ogg elvish-theme.ogg}
    {EXTRA_SCENARIO_MUSIC the_king_is_dead.ogg}
    {EXTRA_SCENARIO_MUSIC frantic.ogg}
    {EXTRA_SCENARIO_MUSIC the_city_falls.ogg}

    {DM_TRACK ( show_title=yes {BOOK_STAGE2} )}

    [story]
        [part]
            # Asheviere was already having secret talks with
            # the orcs about the early HttT events and Lionel’s mission
            #po: "demesne" = the part of a feudal lord's lands reserved for
            #po: personal use.
            #po: The "Sceptre" spelling is intentional.
            story=_ "Upon his return to Weldyn, Delfador discovered that the queen had left with a strong escort to visit her family’s demesne and her ailing parents. Lionel, the King’s most trusted general, had been sent at Asheviere’s request on an errand to Knalga to retrieve the Sceptre of Fire. That mission would cost Lionel his life; and neither Lionel nor Delfador knew that Lionel’s mission had been compromised from the start."
        [/part]
        [part]
            story= _ "Although the King did reprimand Eldred for his actions, Delfador could sense that the military commanders were solidly behind Eldred and his mother. Asheviere had flattered them and beguiled them with promises of glory and plunder in a coming war."
            delay=4000
        [/part]
        [part]
            story= _ "Delfador was granted an audience with the King and expressed his concerns about the missing Book, but the King put off a decision until Asheviere returned to Weldyn, and ordered Delfador to take no further action. And for once, Delfador knew he could not obey the King’s order."
            delay=4000
        [/part]
        [part]
            story= _ "Since he could not ask even his personal guard to go against the King’s wishes, he turned to Kalenz and the elves. Traveling by night to remain unseen they reached Asheviere’s family demesne."
            delay=4000
        [/part]
    [/story]
    {DM_TRACK {BOOK_STAGE3}}

    #### Delfador's code ####
    #wmllint: validate-off
    [side]
        {OLD_DELFADOR}
        side=1
        save_id=Player
        persistent=yes
        controller=human
        recruit={ELVES}
        team_name=allies
        user_team_name= _ "Rebels"
        {GOLD 130 100 100}
        {FLAG_VARIANT long}
    [/side]
    #wmllint: validate-on

    # wmllint: recognize Kalenz
    # wmllint: recognize Chantal
    [event]
        name=prestart
        {ELVISH_NPCS_REAPPEAR}
        {MODIFY_UNIT (side=1) facing ne}
        [object]
            silent=yes
            duration=scenario
            [filter]
                id=Kalenz
            [/filter]
            [effect]
                apply_to=attack
                name=sword
                #textdomain wesnoth-units
                set_description=_"greatsword"
                #textdomain wesnoth-dm
                set_icon=attacks/greatsword-orcish.png
            [/effect]
        [/object]
    [/event]
    #### /Delfador's code ####

    [side]
        type=General
        id=Rhuwin
        name=_ "Rhuwin"
        facing=sw
        side=2
        canrecruit=yes
        controller=ai
        team_name=enemies
        user_team_name= _ "Loyalists"
        recruit=Heavy Infantryman,Spearman,Bowman,Swordsman,Pikeman,Longbowman,Shock Trooper,Royal Guard
        {GOLD 130 180 230}
        {INCOME 2 6 10}
        [ai]
            #recruitment_pattern=fighter,archer,mixed fighter,scout
            aggression=1.0
            caution=0.0
            [goal]
                name=protect_unit
                [criteria]
                    side=2
                    canrecruit=yes
                [/criteria]
                value=3.0
            [/goal]
        [/ai]
        {FLAG_VARIANT loyalist}
    [/side]

    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 (Royal Guard) 1}

    [event]
        name=prestart
        [objectives]
            side=1
            [objective]
                description= _ "Kill all enemy units"
                condition=win
            [/objective]
            [objective]
                description= _ "Enemy unit reaches a signpost"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Delfador"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Kalenz"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Chantal"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

            [gold_carryover]
                bonus=yes
                carryover_percentage=80
            [/gold_carryover]

            [note]
                description= _ "Enemy rider units will go for a signpost"
                [show_if]
                    [have_unit]
                        id=Rhuwin
                        side=2
                    [/have_unit]
                [/show_if]
            [/note]
            [note]
                description= _ "All enemy units will go for a signpost"
                [show_if]
                    [not]
                        [have_unit]
                            id=Rhuwin
                            side=2
                        [/have_unit]
                    [/not]
                [/show_if]
            [/note]
        [/objectives]
    [/event]

    [event]
        name=start

        [message]
            id=Delfador
            message= _ "I am grieved to have to resort to this. Yet the book must be recovered at all costs and no one should be left alive."
        [/message]
        [message]
            id=Kalenz
            message= _ "The book’s curse is already weighing heavy upon you, friend. But we cannot falter. We have acquired orcish weapons, as you have requested. Nobody will be able to link this to you or the elves."
        [/message]
        [message]
            id=Kalenz
            message= _ "Make sure everybody hears the orders. Nobody must escape."
        [/message]
        [message]
            id=Rhuwin
            message= _ "Elves! And that traitor, Delfador is with them! Riders, quickly, go alert the queen. And guards, prepare to strike them down!"
        [/message]

        {HIGHLIGHT_IMAGE 1  19 scenery/signpost.png ()}
        {HIGHLIGHT_IMAGE 18 33 scenery/signpost.png ()}
    [/event]

    [event]
        name=recruit,recall
        first_time_only=no
        [filter]
            race=elf
            [not]
                type_adv_tree=Elvish Shaman
            [/not]
        [/filter]
        [object]
            silent=yes
            duration=scenario
            [effect]
                [filter]
                    [not]
                        type=Elvish Hero, Elvish Champion, Elvish Marshal
                    [/not]
                [/filter]
                apply_to=attack
                name=sword
                set_icon=attacks/sword-orcish.png
            [/effect]
            [effect]
                [filter]
                    type=Elvish Hero, Elvish Champion, Elvish Marshal
                [/filter]
                apply_to=attack
                name=sword
                #textdomain wesnoth-units
                set_description=_"greatsword"
                #textdomain wesnoth-dm
                set_icon=attacks/greatsword-orcish.png
            [/effect]
            [effect]
                [filter]
                    [not]
                        type_adv_tree=Elvish Marksman
                    [/not]
                [/filter]
                apply_to=attack
                name=bow
                set_icon=attacks/bow-orcish.png
            [/effect]
            [effect]
                [filter]
                    type_adv_tree=Elvish Marksman
                [/filter]
                apply_to=attack
                name=longbow
                #textdomain wesnoth-units
                set_description=_"bow"
                #textdomain wesnoth-dm
                set_icon=attacks/bow-orcish.png
            [/effect]
        [/object]
    [/event]

    [event]
        name=side 2 turn 1 refresh
        # place riders and set them to head for the signposts
        # deduct unit cost from side gold so they look like they were recruited
        [unit]
            type={ON_DIFFICULTY Cavalryman Dragoon Dragoon}
            id=rider1
            side=2
            x,y=27,19
            goto_x,goto_y=1,19
            moves=0
            animate=yes
        [/unit]
        [unit]
            type={ON_DIFFICULTY Cavalryman Cavalryman Dragoon}
            id=rider2
            side=2
            x,y=29,21
            goto_x,goto_y=18,33
            moves=0
            animate=yes
        [/unit]
        [store_unit]
            [filter]
                id=rider1
            [/filter]
            variable=rider1
        [/store_unit]
        [store_unit]
            [filter]
                id=rider2
            [/filter]
            variable=rider2
        [/store_unit]
        [store_side]
            side=2
        [/store_side]
        [modify_side]
            side=2
            gold="$($side.gold - $rider1.cost - $rider2.cost)"
        [/modify_side]
        {CLEAR_VARIABLE rider1,rider2,side}
    [/event]

    # now that the enemy has made the initial recruitment, allow additional riders
    # to be recruited when enough gold is accumulated
    [event]
        name=side 2 turn 2
        [allow_recruit]
            type=Cavalryman,Dragoon
            side=2
        [/allow_recruit]
    [/event]

    [event]
        name=recruit
        first_time_only=no
        [filter]
            type=Cavalryman,Dragoon
        [/filter]
        [set_variable]
            name=sentry
            rand=1..2
        [/set_variable]

        [if]
            [variable]
                name=sentry
                equals=1
            [/variable]
            [then]
                {MODIFY_UNIT (x,y=$x1,$y1) (goto_x) (1)  }
                {MODIFY_UNIT (x,y=$x1,$y1) (goto_y) (19) }
            [/then]
        [/if]
        [if]
            [variable]
                name=sentry
                equals=2
            [/variable]
            [then]
                {MODIFY_UNIT (x,y=$x1,$y1) (goto_x) (18) }
                {MODIFY_UNIT (x,y=$x1,$y1) (goto_y) (33) }
            [/then]
        [/if]
    [/event]

    # new objectives, find the book
    [event]
        name=new-objectives
        [message]
            id=Delfador
            message= _ "The guards are slain. Now we must look for the book."
        [/message]
        [objectives]
            side=1
            [objective]
                description= _ "Find the secret door to enter the manor"
                condition=win
            [/objective]

            {TURNS_RUN_OUT}

            [gold_carryover]
                bonus=yes
                carryover_percentage=80
            [/gold_carryover]
        [/objectives]
        # Create some new events for when we move to the right spot.
        [event]
            name=moveto
            [filter]
                side=1
                x,y=32,8
            [/filter]

            [music]
                name=northerners.ogg
                play_once=yes
            [/music]

            [message]
                id=$unit.id
                message= _ "This looks like a secret door. Let’s see where it leads..."
            [/message]
            [terrain]
                x=32
                y=9
                terrain=Uu
            [/terrain]
            [endlevel]
                result=victory
            [/endlevel]
        [/event]
    [/event]

    [event]
        name=last breath
        [filter]
            id=Rhuwin
        [/filter]

        [if]
            [have_unit]
                side=2
                [not]
                    id=Rhuwin
                [/not]
            [/have_unit]
            [then]
                [message]
                    speaker=Rhuwin
                    message= _ "Everyone, quickly, go alert the queen!"
                [/message]
                # loop through all remaining units and have them go to the nearest exit
                [store_unit]
                    [filter]
                        side=2
                    [/filter]
                    variable=escapees
                [/store_unit]
                [foreach]
                    array=escapees
                    [do]
                        [find_path]
                            [traveler]
                                id=$this_item.id
                            [/traveler]
                            [destination]
                                x,y=1,19
                            [/destination]
                            variable=exit1
                            allow_multiple_turns=yes
                            check_zoc=no
                        [/find_path]
                        [find_path]
                            [traveler]
                                id=$this_item.id
                            [/traveler]
                            [destination]
                                x,y=18,33
                            [/destination]
                            variable=exit2
                            allow_multiple_turns=yes
                            check_zoc=no
                        [/find_path]
                        [if]
                            {VARIABLE_CONDITIONAL exit1.hexes less_than $exit2.hexes}
                            [then]
                                {MODIFY_UNIT (id=$this_item.id) (goto_x) (1)  }
                                {MODIFY_UNIT (id=$this_item.id) (goto_y) (19) }
                            [/then]
                            [else]
                                {MODIFY_UNIT (id=$this_item.id) (goto_x) (18) }
                                {MODIFY_UNIT (id=$this_item.id) (goto_y) (33) }
                            [/else]
                        [/if]
                        {CLEAR_VARIABLE exit1,exit2}
                    [/do]
                [/foreach]
                {CLEAR_VARIABLE escapees}
            [/then]
        [/if]
    [/event]

    [event]
        name=die
        [filter]
            id=Rhuwin
        [/filter]
        [filter_condition]
            [have_unit]
                side=2
            [/have_unit]
        [/filter_condition]
        [show_objectives]
        [/show_objectives]
    [/event]

    [event]
        name=die
        first_time_only=no
        [if]
            [not]
                [have_unit]
                    side=2
                [/have_unit]
            [/not]
            [then]
                [fire_event]
                    # Factored like this so the inner events
                    # won't trigger a false positive from wmllint.
                    name=new-objectives
                [/fire_event]
            [/then]
        [/if]
    [/event]

    [event]
        name=moveto
        [filter]
            side=2
            x,y=1,19
        [/filter]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]
    [event]
        name=moveto
        [filter]
            side=2
            x,y=18,33
        [/filter]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    [event]
        name=time over
        [message]
            id=Delfador
            message= _ "Reinforcements on the horizon. We are too late!"
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]
    {MEMOIRS_DEATHS}
[/scenario]
