#textdomain wesnoth-l
[scenario]
    id=04_Unlawful_Orders
    name= _ "Unlawful Orders"
    next_scenario=05_Hide_and_Seek
    victory_when_enemies_defeated=yes
    map_file=04_Unlawful_Orders.map
    {TURNS 26 22 18}
    {DEFAULT_SCHEDULE}

    {INTRO_AND_SCENARIO_MUSIC "transience.ogg" "the_king_is_dead.ogg"}
    {EXTRA_SCENARIO_MUSIC "vengeful.ogg"}
    {EXTRA_SCENARIO_MUSIC "knalgan_theme.ogg"}
    {EXTRA_SCENARIO_MUSIC "battle.ogg"}

    [label]
        x,y=16,15
        text= _ "Elensefar"
    [/label]

    [label]
        x,y=8,13
        text= _ "Port of Elensefar"
    [/label]

    {LIBERTY_BIGMAP {JOURNEY_04_NEW} }

    [side]
        type=Bandit
        id=Baldras
        side=1
        color=black
        canrecruit=yes
        controller=human
        recruit=Thug,Footpad,Poacher
        {GOLD 175 150 125}
        team_name=good_guys
        user_team_name=_"Rebels"
    [/side]

    # wmllint: directory spelling Maddock
    [side]
        type=General
        id=Lord Maddock
        name= _ "Lord Maddock"
        profile=portraits/maddock.webp
        side=2
        color=teal
        canrecruit=yes
        controller=ai
        recruit=Spearman,Horseman,Cavalryman,Heavy Infantryman,Mage,Bowman
        [ai]
            recruitment_pattern=scout,mixed fighter,fighter,archer,fighter
            leader_value=3
            village_value=0.2
        [/ai]
        {GOLD 110 100 90}
        team_name=good_guys
        user_team_name=_"Rebels"
        {FLAG_VARIANT loyalist}
    [/side]

    {STARTING_VILLAGES 2 15}

    [side]
        type=General
        id=Kestrel
        # intentionally no name
        generate_name=no
        side=3
        color=darkred
        canrecruit=yes
        controller=ai
        recruit=Spearman,Horseman,Cavalryman,Heavy Infantryman,Bowman
        [ai]
            caution=0.0
            aggression=1.0
            recruitment_pattern=scout,fighter,fighter,archer,archer
            leader_value=20
        [/ai]
        {GOLD 160 200 240}
        user_team_name=_"Weldyn"
        {FLAG_VARIANT loyalist}
    [/side]

    {STARTING_VILLAGES 3 5}

    # side 3 is all nameless
    [event]
        name=recruit
        first_time_only=no
        [filter_second]
            side=3
        [/filter_second]

        [modify_unit]
            [filter]
                x,y=$x1,$y1
            [/filter]
            name=
        [/modify_unit]
    [/event]

    #
    # Prestart events
    #
    [event]
        name=prestart

        {PLACE_IMAGE items/chest-plain-closed.png 6 4}
        {PLACE_IMAGE items/grain-sheaf.png 7 30}
        {PLACE_IMAGE items/straw-bale1.png 24 27}

        {PLACE_IMAGE scenery/tent-fancy-red.png 18 16}
        {PLACE_IMAGE scenery/temple1.png 14 9}
        {PLACE_IMAGE scenery/lighthouse.png 6 12}
        {PLACE_IMAGE scenery/lighthouse.png 7 16}
        {PLACE_IMAGE scenery/well.png 21 28}
        {PLACE_IMAGE scenery/rock4.png 21 3}
        {PLACE_IMAGE scenery/leanto.png 5 36}

        [objectives]
            side=1
            [objective]
                description= _ "Defeat the enemy general"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Baldras"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Harper"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Maddock"
                condition=lose
            [/objective]

            {TURNS_RUN_OUT}

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

        [store_unit]
            [filter]
                id=Baldras
            [/filter]

            kill=yes
            variable=stored_Baldras
        [/store_unit]

        {VARIABLE stored_Baldras.facing sw}

        {LOYAL_UNIT 3 Lieutenant 19 36}
        [+unit]
            generate_name=no
            [modifications]
                {TRAIT_QUICK}
            [/modifications]
        [/unit]
    [/event]

    #
    # Starting conversation and actions
    #
    [event]
        name=start

        [move_unit_fake]
            side=1
            type=Boat
            x=25,23
            y= 9,11
        [/move_unit_fake]

        [item]
            x,y=23,11
            image=units/transport/boat.png~FL(horiz)
        [/item]

        [move_unit_fake]
            side=1
            type=$stored_Baldras.type
            x=23,21
            y=11,11
        [/move_unit_fake]

        [unstore_unit]
            variable=stored_Baldras
        [/unstore_unit]

        [recall]
            id=Harper
        [/recall]
        [role]
            type=Huntsman,Ranger,Fugitive,Highwayman,Outlaw,Trapper,Bandit,Footpad,Poacher,Thug
            role=Advisor
            [auto_recall][/auto_recall]
            [not]
                id=Harper,Baldras
            [/not]
        [/role]

        [message]
            speaker=Baldras
            message= _ "Maddock, I be coming to report—"
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "I am already aware of your altercation in Dallben, Baldras. Your dissent was not wise."
        [/message]
        [message]
            speaker=Baldras
            # po: pigdirt is slang for bullshit or hogwash
            message= _ "Dissent? Maddock, I’m not trusting no strange riders who show up out of nowhere claimin’ to be from the Queen. Seems like a bunch of pigdirt to me."
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "That was partially my fault, since I was not able to send a messenger to you in time. I shall have to inform you now. Five weeks ago, at the Ford of Abez, Queen Asheviere’s men allied themselves with the northern orcs and betrayed King Garard II."
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "Since Prince Eldred died in the battle, the Queen took the throne and has been consolidating her power. Part of that has been bringing the elders from the local villages here, forcing them to swear allegiance to her. That was the purpose of that patrol."
        [/message]
        [message]
            speaker=Baldras
            message= _ "Annuvin province be ruled by Elensefar! We has no duty to Weldyn."
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "You do now. I do not really need to tell you what will happen should you refuse Her Majesty’s offer."
        [/message]
        [message]
            speaker=Baldras
            message= _ "Grow a spine, Maddock. If they come to ya and make you swear fealty to this Asheviere, would ya really give up that easy?"
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "Well, they do not come within the walls of my city. There is a large garrison south at the great fortress of Halstead, but even should they mobilize all their troops, I doubt they could take Elensefar. It is true that for now, the majority of my forces are still returning from the battle of Abez, but unless we are attacked within the next few days, I shall be able to hold the city."
        [/message]
        [message]
            speaker=Baldras
            message= _ "And what abouts us? Throw us like dogs to those Wesnothians?"
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "We have an uneasy peace to maintain. The treaty between Elensefar and Wesnoth is ancient, and I do not think Her Majesty means to break it. At the same time, I have some concessions that I shall have to make. I cannot afford a war with Weldyn, as you can surely understand."
        [/message]
        [message]
            speaker=Harper
            message= _ "But we can’t do nothing! The Queen even be sending them beast orcs against us, that’s how evil she is! Surely you can’t stand for this..."
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "Yes, I am aware of Queen Asheviere’s propensity for hiring orcs to supplement her forces. That does not mean I could do anything about it, even if I were so inclined. The best advice I can give you is to just apologize. Even if they may want your head, Baldras, you still have the chance to preserve your village. Think of the bigger picture."
        [/message]
        [message]
            speaker=Baldras
            message= _ "The bigger picture of you idly sittin’ here waiting till them Wesnothians come to take the city right from your hands."
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "I very much doubt they would be bold enough to attack us outright—"
        [/message]
        [message]
            speaker=Baldras
            message= _ "Head out of your behind, Maddock. Open your eyes! Look south and tell me that again!"
        [/message]

        [scroll_to_unit]
            id=Kestrel
        [/scroll_to_unit]

        [delay]
            time=1000
        [/delay]

        [message]
            speaker=Lord Maddock
            message= _ "An invading army? No, something is not right with those soldiers... but I presume we have no time to ponder that. To arms!"
        [/message]

        # prevent Rothel from suiciding ahead of time
        [modify_unit]
            [filter]
                id=Rothel
            [/filter]
            moves=0
        [/modify_unit]

        {CLEAR_VARIABLE stored_Baldras}
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x,y=6,4
        [/filter]

        [message]
            speaker=unit
            message= _ "Hey, look! Some buried treasure!"
        [/message]

        [sound]
            name=gold.ogg
        [/sound]

        [gold]
            side=1
            amount=33
        [/gold]

        {REMOVE_IMAGE 6 4}
        {PLACE_IMAGE items/chest-plain-open.png 6 4}
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x,y=5,36
        [/filter]

        [message]
            speaker=unit
            message= _ "Looks like someone left some supplies here."
        [/message]

        [sound]
            name=gold.ogg
        [/sound]

        [gold]
            side=1
            amount=17
        [/gold]
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x,y=21,3
            [not]
                id=Baldras
            [/not]
        [/filter]

        [message]
            speaker=unit
            message= _ "It says, “Here lies Daneth, child of the Light, loyal defender of Lord and Land.” Whoever that was."
        [/message]
        [message]
            speaker=Baldras
            message= _ "Maddock’s firstborn son."
        [/message]
        [message]
            speaker=unit
            message= _ "Oh."
        [/message]
    [/event]

    [event]
        name=moveto
        [filter]
            id=Harper
            x,y=21,28
        [/filter]

        [message]
            speaker=Harper
            message= _ "That’s a deep well! Wonder how far down it goes. Guess I’ll chuck a rock down and find out."
        [/message]
        [message]
            speaker=Harper
            message= _ "...
...
..."
        [/message]
        [message]
            speaker=Harper
            message= _ "Plunk!"
        [/message]

        [store_unit]
            [filter]
                x,y=21,28
            [/filter]
            variable=stored_unit
        [/store_unit]

        {VARIABLE_OP stored_unit.experience add 2}

        [unstore_unit]
            variable=stored_unit
        [/unstore_unit]

        {CLEAR_VARIABLE stored_unit}
    [/event]

    [event]
        name=attack
        [filter]
            side=3
        [/filter]
        [filter_condition]
            [variable]
                name=turn_number
                less_than=4
            [/variable]
        [/filter_condition]

        [message]
            speaker=Lord Maddock
            message= _ "Hold there, soldier! Can we not come to terms without bloodshed?"
        [/message]
        [message]
            speaker=unit
            message= _ "..."
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "Hmm. Something is quite wrong indeed..."
        [/message]
    [/event]

    #
    # Special event - the human enemies become undead at night
    #

    [event]
        name=turn 4

        [message]
            speaker=narrator
            message= _ "<i>The sun sets...</i>"
        [/message]

        [message]
            speaker=narrator
            message= _ "Baldras and Harper watch, petrified, as the skin and flesh of their adversaries begin to rot away."
            image="wesnoth-icon.png"
        [/message]

        {INCIDENTAL_MUSIC the_city_falls.ogg}
    [/event]

    [event]
        name=prestart

        [set_variables]
            name=undead_transformation

            [value]
                human_type=Spearman
                undead_type=Skeleton
            [/value]
            [value]
                human_type=Swordsman
                undead_type=Deathblade
            [/value]
            [value]
                human_type=Pikeman
                undead_type=Revenant
            [/value]
            [value]
                human_type=Bowman
                undead_type=Skeleton Archer
            [/value]
            [value]
                human_type=Longbowman
                undead_type=Bone Shooter
            [/value]
            [value]
                human_type=Heavy Infantryman
                undead_type=Ghoul
            [/value]
            [value]
                human_type=Shock Trooper
                undead_type=Necrophage
            [/value]
            [value]
                human_type=Cavalryman
                undead_type=Skeleton Rider
            [/value]
            [value]
                human_type=Dragoon
                undead_type=Bone Knight
            [/value]
            [value]
                human_type=Lieutenant
                undead_type=Death Squire
            [/value]
            [value]
                human_type=General
                undead_type=Death Knight
            [/value]
            [value]
                human_type=Horseman
                undead_type=Skeleton Rider
            [/value]
            [value]
                human_type=Knight
                undead_type=Bone Knight
            [/value]
            [value]
                human_type=Lancer
                undead_type=Bone Knight
            [/value]
        [/set_variables]
    [/event]

#define FALLBACK
    [if]
        [variable]
            name=type_to_turn_into
            equals=$null
        [/variable]
        [then]
            [wml_message]
                logger=error
                message=_"Could not convert a $stored_changers[$i].type, please report!"
            [/wml_message]
            {VARIABLE type_to_turn_into $stored_changers[$i].type}
        [/then]
    [/if]
#enddef
    [event]
        name=new turn
        first_time_only=no

        {VARIABLE temp $turn_number}
        {VARIABLE_OP temp add 2}
        {VARIABLE_OP temp modulo 6}

        [if]
            [variable]
                name=temp
                equals=0
            [/variable]

            [variable]
                name=turn_number
                greater_than_equal_to=4
            [/variable]

            [then]
                {FLASH 255 100 100 ()}

                [terrain_mask]
                    x,y=16,33
                    mask="{campaigns/Liberty/masks/04_Unlawful_Orders_1.mask}"
                    alignment=even
                [/terrain_mask]

                [redraw]
                [/redraw]

                [disallow_recruit]
                    side=3
                    type=Spearman,Horseman,Cavalryman,Heavy Infantryman,Bowman
                [/disallow_recruit]
                [allow_recruit]
                    side=3
                    type=Skeleton,Skeleton Rider,Skeleton Archer
                [/allow_recruit]

                [store_unit]
                    [filter]
                        side=3
                    [/filter]

                    kill=yes
                    variable=stored_changers
                [/store_unit]

                [for]
                    array=stored_changers
                    variable=i
                    [do]
                        [for]
                            array=undead_transformation
                            variable=j
                            [do]
                                [if]
                                    [variable]
                                        name=stored_changers[$i].type
                                        equals=$undead_transformation[$j].human_type
                                    [/variable]

                                    [then]
                                        {VARIABLE type_to_turn_into $undead_transformation[$j].undead_type}
                                        {VARIABLE j 999}
                                    [/then]
                                [/if]
                            [/do]
                        [/for]

                        {FALLBACK}
                        [unit]
                            type=$type_to_turn_into
                            side=3
                            x,y=$stored_changers[$i].x,$stored_changers[$i].y
                            id=$stored_changers[$i].id
                            generate_name=no
                            experience=$stored_changers[$i].experience
                            hitpoints=$stored_changers[$i].hitpoints
                            canrecruit=$stored_changers[$i].canrecruit
                            [variables]
                                [insert_tag]
                                    name=stored_trait
                                    variable=stored_changers[$i].modifications.trait
                                [/insert_tag]

                                turned_to_undead_at_least_once=yes
                            [/variables]
                            to_variable=changer
                        [/unit]
                        {CLEAR_VARIABLE type_to_turn_into}

                        [if]
                            [variable]
                                name=changer.hitpoints
                                greater_than=$changer.max_hitpoints
                            [/variable]

                            [then]
                                {VARIABLE changer.hitpoints $changer.max_hitpoints}
                            [/then]
                        [/if]

                        [unstore_unit]
                            variable=changer
                        [/unstore_unit]
                    [/do]
                [/for]

                {CLEAR_VARIABLE stored_changers,changer}
            [/then]
        [/if]
    [/event]

    [event]
        name=new turn
        first_time_only=no

        {VARIABLE temp $turn_number}
        {VARIABLE_OP temp modulo 6}

        [if]
            [variable]
                name=temp
                equals=1
            [/variable]

            [variable]
                name=turn_number
                greater_than_equal_to=4
            [/variable]

            [then]
                {FLASH 255 255 100 ()}

                [terrain_mask]
                    x,y=16,33
                    mask="{campaigns/Liberty/masks/04_Unlawful_Orders_2.mask}"
                    alignment=even
                [/terrain_mask]

                [redraw]
                [/redraw]

                [disallow_recruit]
                    side=3
                    type=Skeleton,Skeleton Rider,Skeleton Archer
                [/disallow_recruit]
                [allow_recruit]
                    side=3
                    type=Spearman,Horseman,Cavalryman,Heavy Infantryman,Bowman
                [/allow_recruit]

                [store_unit]
                    [filter]
                        side=3
                    [/filter]

                    kill=yes
                    variable=stored_changers
                [/store_unit]

                [for]
                    array=stored_changers
                    [do]
                        [for]
                            array=undead_transformation
                            variable=j
                            [do]
                                [if]
                                    [variable]
                                        name=stored_changers[$i].type
                                        equals=$undead_transformation[$j].undead_type
                                    [/variable]

                                    [then]
                                        {VARIABLE type_to_turn_into $undead_transformation[$j].human_type}
                                        [break][/break]
                                    [/then]
                                [/if]
                            [/do]
                        [/for]

                        {FALLBACK}
                        [if]
                            [variable]
                                name=stored_changers[$i].variables.turned_to_undead_at_least_once
                                equals=yes
                            [/variable]

                            [then]
                                [unit]
                                    type=$type_to_turn_into
                                    side=3
                                    x,y=$stored_changers[$i].x,$stored_changers[$i].y
                                    id=$stored_changers[$i].id
                                    generate_name=no
                                    experience=$stored_changers[$i].experience
                                    hitpoints=$stored_changers[$i].hitpoints
                                    canrecruit=$stored_changers[$i].canrecruit
                                    [modifications]
                                        [insert_tag]
                                            name=trait
                                            variable=stored_changers[$i].variables.stored_trait
                                        [/insert_tag]
                                    [/modifications]
                                    [variables]
                                        turned_to_undead_at_least_once=yes
                                    [/variables]
                                    to_variable=changer
                                [/unit]
                            [/then]

                            [else]
                                [unit]
                                    type=$type_to_turn_into
                                    side=3
                                    x,y=$stored_changers[$i].x,$stored_changers[$i].y
                                    experience=$stored_changers[$i].experience
                                    hitpoints=$stored_changers[$i].hitpoints
                                    canrecruit=$stored_changers[$i].canrecruit
                                    random_traits=yes
                                    generate_name=no
                                    to_variable=changer
                                [/unit]
                            [/else]
                        [/if]
                        {CLEAR_VARIABLE type_to_turn_into}

                        [if]
                            [variable]
                                name=changer.hitpoints
                                greater_than=$changer.max_hitpoints
                            [/variable]

                            [then]
                                {VARIABLE changer.hitpoints $changer.max_hitpoints}
                            [/then]
                        [/if]

                        [unstore_unit]
                            variable=changer
                        [/unstore_unit]
                    [/do]
                [/for]

                {CLEAR_VARIABLE stored_changers,changer}
            [/then]
        [/if]
    [/event]
#undef FALLBACK

    [event]
        name=turn 4
        [message]
            speaker=Harper
            message= _ "Them things look like evil spirits! Are they those undead you told me about, Uncle?"
        [/message]
        [message]
            speaker=Baldras
            message= _ "They bears some likeness, but these men almost seem half alive. This be some kind of truly dreadful sorcery."
        [/message]
    [/event]

    [event]
        name=turn 7
        {INCIDENTAL_MUSIC nunc_dimittis.ogg}
        [message]
            speaker=Lord Maddock
            message= _ "So, their human aspect returns. Whatever these creatures are, we shall crush them all under the light of the sun."
        [/message]
    [/event]

    #
    # Victory
    #
    [event]
        name=last breath
        [filter]
            id=Kestrel
        [/filter]

        [if]
            [have_location]
                time_of_day_id=dawn,morning,afternoon
            [/have_location]
            [then]
                [modify_unit]
                    [filter]
                        id=Kestrel
                    [/filter]
                    name= _ "General Kestrel"
                [/modify_unit]
            [/then]
        [/if]

        [message]
            speaker=second_unit
            message= _ "Who are you? What are you?"
        [/message]
        [message]
            speaker=Kestrel
            message= _ "We... are accursed. We were the soldiers of Prince Eldred, the King’s son and betrayer... when the Prince was slain at the hands of the mage Delfador, those of us who bore witness to it were warped into these twisted forms. Now, our mortal bodies pass... into another plane of hellish existence..."
        [/message]

        [if]
            [have_location]
                time_of_day_id=dawn,morning,afternoon
            [/have_location]
            [then]
                [set_achievement]
                    content_for=liberty
                    id="liberty_nameless"
                [/set_achievement]
            [/then]
        [/if]

        [kill]
            id=Kestrel
            animate=yes
            fire_event=no
        [/kill]

        [message]
            speaker=Lord Maddock
            message= _ "Such is the price for treachery."
        [/message]
        [message]
            speaker=Baldras
            message= _ "Maddock, we helped you defend your city. Will you help us now?"
        [/message]
        # he really doesn't want to help, but...
        [message]
            speaker=Lord Maddock
            message= _ "..."
        [/message]
        # guess he still has a little bit of a conscience
        [message]
            speaker=Lord Maddock
            message= _ "<i>Listen carefully. I shall not openly speak against Asheviere seeing as some of her spies are in the city... however, rest assured, I certainly do not trust the Queen and her questionable tactics.</i>"
        [/message]
        [message]
            speaker=Baldras
            message= _ "<i>What can ya offer us then?</i>"
        [/message]
        [message]
            speaker=Lord Maddock
            message= _ "<i>Well, I do not normally make my relationship with them known, but I can make an exception. Southeast of here, in Carcyn, you may find assistance from one of my... contacts. Their methods should be better suited to your needs. As for your homes, I cannot offer you protection against Weldyn, but I can arrange for some roads to be closed to delay their patrols. That is the most I can do for you.</i>"
        [/message]
        [message]
            speaker=Baldras
            message= _ "<i>Hmph. Clear that you won’t help us fight, but I gots no more time to be stayin’ here. If we is going to resist the Crown a second time, we needs ourselves a good plan. Hope that you’re not sending us to our deaths, Maddock.</i>"
        [/message]

        {CLEAR_VARIABLE undead_transformation}
        {CLEAR_VARIABLE temp}

        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]

    #
    # Loss conditions - turns run out
    #
    [event]
        name=time over
        [message]
            speaker=Baldras
            message= _ "More troops be coming from the south! With all them reinforcements, we gots no chance to defend Elensefar!"
        [/message]
        [endlevel]
            result=defeat
        [/endlevel]
    [/event]

    [event]
        name=defeat

        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message=_ "Asheviere’s armies assembled and struck before the nascent rebellion could even be planned. Without the necessary forces, Annuvin soon fell under Weldyn’s absolute control."
        [/message]
    [/event]

    {LIBERTY_DEATHS}
[/scenario]
