#textdomain wesnoth-ei
[scenario]
    id=Mal-Ravanals_Capital
    name= _ "Mal-Ravanal's Capital"
    map_data="{campaigns/Eastern_Invasion/maps/Mal-Ravanals_Capital.map}"
    turns=26

    {DAWN}
    {MORNING}
    {AFTERNOON}
    {DUSK}
    {DUSK}
    {FIRST_WATCH}
    {FIRST_WATCH}
    {SECOND_WATCH}
    {SECOND_WATCH}

    next_scenario=Northern_Outpost

    {INTRO_AND_SCENARIO_MUSIC underground.ogg the_deep_path.ogg}
    {EXTRA_SCENARIO_MUSIC heroes_rite.ogg}
    {EXTRA_SCENARIO_MUSIC underground.ogg}

    {campaigns/Eastern_Invasion/utils/deaths.cfg}

    [story]
        [part]
            show_title=yes
            {TO_MAL_RAVANALS_CAPITOL}
        [/part]
    [/story]

    [side]
        type=Lieutenant
        id=Gweddry
        name= _ "Gweddry"
        side=1
        canrecruit=yes
        controller=human
        team_name=wesnothians
        user_team_name=_"Wesnothians"
        {GOLD 200 150 100}
    [/side]

    [side]
        type=Dark Sorcerer
        id=Mal-Marak
        name= _ "Mal-Marak"
        side=2
        canrecruit=yes
        recruit=Walking Corpse,Skeleton,Skeleton Archer
        [ai]
            {NO_SCOUTS}
            recruitment_pattern=fighter,archer,fighter
        [/ai]
        {GOLD 100 100 125}
        {FLAG_VARIANT undead}
        team_name=undead
        user_team_name=_"Undead"
    [/side]

    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 Skeleton 3}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 2 "Skeleton Archer" 2}

    [side]
        type=Dark Sorcerer
        id=Mal-Galkar
        name= _ "Mal-Galkar"
        side=3
        canrecruit=yes
        recruit=Walking Corpse,Vampire Bat,Skeleton,Skeleton Archer
        [ai]
            {NO_SCOUTS}
            recruitment_pattern=scout,fighter,archer,fighter,archer
        [/ai]
        {GOLD 100 100 125}
        {FLAG_VARIANT undead}
        team_name=undead
        user_team_name=_"Undead"
    [/side]

    {LIMIT_CONTEMPORANEOUS_RECRUITS 3 Skeleton 2}
    {LIMIT_CONTEMPORANEOUS_RECRUITS 3 "Vampire Bat" 2}

    [side]
        type=Death Knight
        id=Ravanal-Guard
        name= _ "Rava-Krodaz"
        recruit=Revenant,Bone Shooter
        side=4
        canrecruit=yes
        facing=sw
        {GOLD 300 400 600}
        {FLAG_VARIANT undead}
        team_name=undead
        user_team_name=_"Undead"
        [ai]
            aggression=-0.2
        [/ai]
    [/side]

    {STARTING_VILLAGES 4 9}

    [side]
        type=Lich
        id=Mal-Ravanal
        name= _ "Mal-Ravanal"
        recruit=Dark Adept,Bone Shooter,Revenant,Wraith,Shadow
        side=5
        canrecruit=yes
        facing=sw
        profile=portraits/mal-ravanal.png
        {GOLD 400 500 600}
        {FLAG_VARIANT undead}
        team_name=undead
        user_team_name=_"Undead"
        [ai]
            passive_leader=yes
        [/ai]
    [/side]

    {STARTING_VILLAGES 5 16}

    {LIMIT_CONTEMPORANEOUS_RECRUITS 5 Shadow 1}

    # the prisoners' side
    [side]
        side=6
        no_leader=yes
        team_name=wesnothians
        user_team_name= _ "Wesnothians"
        [ai]
            ai_algorithm=idle_ai
        [/ai]
    [/side]

    [event]
        name=prestart
        [objectives]
            side=1
            [objective]
                description= _ "Escape from the capital by killing one of the two dark sorcerers"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Gweddry"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Dacyn"
                condition=lose
            [/objective]

            note={EARLY_FINISH_BONUS_NOTE}+{NEW_GOLD_CARRYOVER_NOTE_40}
        [/objectives]

        # initializing of the knight prisoners: per each Rr^Xo hex, create one
        # imprisoned knight (one of them a paladin)
        [store_locations]
            terrain=Rr^Xo

            variable=prison_locations
        [/store_locations]

        {VARIABLE paladin_i "1..$prison_locations.length"}
        {VARIABLE_OP paladin_i add -1}

        {FOREACH prison_locations i}
            [item]
                x,y=$prison_locations[$i].x,$prison_locations[$i].y
                halo=items/horse-cage.png
            [/item]

            [store_locations]
                terrain=Re

                [filter_adjacent_location]
                    x,y=$prison_locations[$i].x,$prison_locations[$i].y
                [/filter_adjacent_location]

                variable=guard_location
            [/store_locations]

            {GENERIC_UNIT 4 Revenant $guard_location.x $guard_location.y}
            [+unit]
                ai_special=guardian
                role=prison guard
                [variables]
                    prison_x=$prison_locations[$i].x
                    prison_y=$prison_locations[$i].y
                [/variables]
            [/unit]

            [if]
                [variable]
                    name=i
                    numerical_equals=$paladin_i
                [/variable]

                [then]
                    [unit]
                        type=Paladin
                        side=6
                        id=Terraent
                        name= _ "Terraent"
                        x,y=$prison_locations[$i].x,$prison_locations[$i].y
                        random_traits=no
                        facing=sw
                        [modifications]
                            {TRAIT_LOYAL}
                        [/modifications]
                        {IS_LOYAL}
                    [/unit]
                [/then]

                [else]
                    {GENERIC_UNIT 6 Knight $prison_locations[$i].x $prison_locations[$i].y}
                    [+unit]
                        facing=sw
                    [/unit]
                [/else]
            [/if]
        {NEXT i}

        {CLEAR_VARIABLE prison_locations,guard_location,paladin_i}
    [/event]

    [event]
        name=start
        [recall]
            id=Dacyn
        [/recall]
        [message]
            speaker=Gweddry
            message= _ "The Bitter Swamp's ill reputation is, it seems, well merited. Mal-Ravanal's capital lies before us!"
        [/message]

        [message]
            speaker=Dacyn
            message= _ "Yes, but look! The undead forces are closing in behind us. As I said before, we cannot kill Mal-Ravanal. It was foolish to come this far, we must turn back!"
        [/message]

        [message]
            speaker=Gweddry
            message= _ "Perhaps. To retreat, we must kill one of these dark sorcerers that follow us. But, perhaps we can kill this Mal-Ravanal still. I might wish to try."
        [/message]

        [message]
            speaker=Mal-Ravanal
            message= _ "These humans have dared foolishly to come this far into my land. Crush them!"
        [/message]
    [/event]

    # this makes sure that the player nor the undead can attack the prisoners,
    # note that side turn instead of turn refresh is used to prevent the paladin
    # from healing his guard
    [event]
        name=side turn
        first_time_only=no

        [if]
            [variable]
                name=side_number
                equals=2
            [/variable]

            [then]
                [modify_side]
                    side=6
                    team_name=undead
                [/modify_side]
            [/then]
        [/if]

        [if]
            [variable]
                name=side_number
                equals=6
            [/variable]

            [then]
                [modify_side]
                    side=6
                    team_name=wesnothians
                [/modify_side]
            [/then]
        [/if]
    [/event]

    # this makes sure that the prisoners can't attack anyone and that the guards
    # will not move
    [event]
        name=turn refresh
        first_time_only=no

        [if]
            [variable]
                name=side_number
                equals=4
            [/variable]

            [then]
                {MODIFY_UNIT (role=prison guard) moves 0}
            [/then]
        [/if]

        [if]
            [variable]
                name=side_number
                equals=6
            [/variable]

            [then]
                [store_unit]
                    [filter]
                        side=6
                    [/filter]

                    kill=yes
                    variable=prisoners
                [/store_unit]

                {FOREACH prisoners i}
                    {VARIABLE prisoners[$i].moves 0}
                    {VARIABLE prisoners[$i].attacks_left 0}

                    [unstore_unit]
                        variable=prisoners[$i]
                        find_vacant=no
                    [/unstore_unit]
                {NEXT i}

                {CLEAR_VARIABLE prisoners}
            [/then]
        [/if]
    [/event]

    [event]
        name=die
        first_time_only=no

        [filter]
            role=prison guard
        [/filter]

        [store_unit]
            [filter]
                x,y=$unit.variables.prison_x,$unit.variables.prison_y
            [/filter]

            kill=yes
            variable=freed_prisoner
        [/store_unit]

        {VARIABLE freed_prisoner.side 1}
        {VARIABLE freed_prisoner.moves $freed_prisoner.max_moves}
        {VARIABLE freed_prisoner.attacks_left 1}

        [unstore_unit]
            variable=freed_prisoner
            find_vacant=no
        [/unstore_unit]

        [terrain]
            x,y=$freed_prisoner.x,$freed_prisoner.y
            terrain=Rr
        [/terrain]

        [removeitem]
            x,y=$freed_prisoner.x,$freed_prisoner.y
        [/removeitem]

        [if]
            [have_unit]
                [and]
                    role=prison guard

                    [or]
                        role=former prison guard
                    [/or]
                [/and]

                count=5
            [/have_unit]

            [then]
                [message]
                    speaker=$freed_prisoner.id
                    message= _ "Thank you for destroying that skeleton warrior! Every time you kill one of the Revenants, one of us knights is let free!"
                [/message]
                [message]
                    speaker=Gweddry
                    message= _ "Why? Are you the undead's prisoners?"
                [/message]
                [message]
                    speaker=$freed_prisoner.id
                    message= _ "Yes. My allies and I were questing when we were ambushed by undead. The guard is playing a sick game - whenever one of their warriors dies, one of us is let free, but whenever you lose a fighter, they kill one of us."
                [/message]
                [message]
                    speaker=Dacyn
                    message= _ "This sounds dangerous. We will try to free as many of you as possible before we escape from here."
                [/message]
                [message]
                    speaker=$freed_prisoner.id
                    message= _ "There are five of us still locked up in the jail. All of us will join you if you can get us free."
                [/message]
            [/then]
        [/if]

        {CLEAR_VARIABLE freed_prisoner}
    [/event]

    # this makes a random guard kill their prisoner whenever one of the player's
    # units dies
    [event]
        name=die
        first_time_only=no

        [filter]
            side=1
        [/filter]

        [if]
            [have_unit]
                side=6
            [/have_unit]

            [then]
                # this makes sure that all the knights are killed before the paladin
                [if]
                    [have_unit]
                        type=Knight
                        side=6
                    [/have_unit]

                    [then]
                        [store_unit]
                            [filter]
                                type=Knight
                                side=6
                            [/filter]

                            kill=no
                            variable=killable_prisoners
                        [/store_unit]
                    [/then]

                    [else]
                        [store_unit]
                            [filter]
                                side=6
                            [/filter]

                            kill=no
                            variable=killable_prisoners
                        [/store_unit]
                    [/else]
                [/if]

                {VARIABLE_OP random_prisoner_i rand "1..$killable_prisoners.length"}
                {VARIABLE_OP random_prisoner_i add -1}

                [store_unit]
                    [filter]
                        side=4
                        role=prison guard
                        [filter_adjacent]
                            x,y=$killable_prisoners[$random_prisoner_i].x,$killable_prisoners[$random_prisoner_i].y
                        [/filter_adjacent]
                    [/filter]

                    kill=yes
                    variable=stored_executioner
                [/store_unit]

                # the guard of the killed prisoner is now free to act normally
                {CLEAR_VARIABLE stored_executioner.ai_special,stored_executioner.status.guardian}

                {VARIABLE stored_executioner.role "former prison guard"}

                [unstore_unit]
                    variable=stored_executioner
                [/unstore_unit]

                [animate_unit]
                    flag=attack

                    [filter]
                        x,y=$stored_executioner.x,$stored_executioner.y
                    [/filter]

                    [primary_attack]
                        name=axe
                    [/primary_attack]

                    hits=yes

                    [facing]
                        x,y=$killable_prisoners[$random_prisoner_i].x,$killable_prisoners[$random_prisoner_i].y
                    [/facing]
                [/animate_unit]

                [kill]
                    x,y=$killable_prisoners[$random_prisoner_i].x,$killable_prisoners[$random_prisoner_i].y
                    animate=yes
                    fire_event=no
                [/kill]

                [terrain]
                    x,y=$killable_prisoners[$random_prisoner_i].x,$killable_prisoners[$random_prisoner_i].y
                    terrain=Rr
                [/terrain]

                [removeitem]
                    x,y=$killable_prisoners[$random_prisoner_i].x,$killable_prisoners[$random_prisoner_i].y
                [/removeitem]

                {CLEAR_VARIABLE killable_prisoners,random_prisoner_i,stored_executioner}
            [/then]
        [/if]
    [/event]

    #impossible to kill Mal-Ravanal
    [event]
        name=attack
        first_time_only=no
        [filter_second]
            id=Mal-Ravanal
        [/filter_second]
        [message]
            speaker=Mal-Ravanal
            message= _ "You dare to attack <i>me</i>!? Go back to your master!"
        [/message]
        [message]
            speaker=Gweddry
            message= _ "This is not good; we are unable to attack Mal-Ravanal at all, for his magic is too strong! We must turn back, it was foolish to press the attack."
        [/message]
        [teleport]
            [filter]
                x,y=$x1,$y1
                side=1
            [/filter]
            x=19
            y=13
        [/teleport]
    [/event]

    #when you kill one of the necros you win
    [event]
        name=die
        [filter]
            id=Mal-Marak
        [/filter]
        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]

    [event]
        name=die
        [filter]
            id=Mal-Galkar
        [/filter]
        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]

    [event]
        name=victory
        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message=_ "Gweddry's men retreated swiftly from the Bitter Swamp, fleeing north and west as if their heels were winged. Finding a low pass through the northern Estmarks, they were greatly relieved to see the valley of the Weldyn open before them on the other side."
        [/message]
    [/event]

    {FOREIGN_DEFEAT}
[/scenario]
