#textdomain wesnoth-ei
[scenario]
    id=08_Training_the_Ogres
    name= _ "Capturing the Ogres"
    next_scenario=09_Xenophobia
    map_data="{campaigns/Eastern_Invasion/maps/08_Training_the_Ogres.map}"
    victory_when_enemies_defeated=no
    turns=-1
    {DEFAULT_SCHEDULE}

    {INTRO_AND_SCENARIO_MUSIC revelation.ogg traveling_minstrels.ogg}
    {EXTRA_SCENARIO_MUSIC journeys_end.ogg}

    {campaigns/Eastern_Invasion/utils/deaths.cfg}

    {BIGMAP_08}

#define MAP_EDGES_SLF
    [filter_adjacent_location]
        count=1-5
    [/filter_adjacent_location]
#enddef

    [side]
        type=Lieutenant
        id=Gweddry
        name= _ "Gweddry"
        side=1
        canrecruit=no
        controller=human
        team_name=wesnothians
        user_team_name=_"Wesnothians"
        gold=100
        {FLAG_VARIANT loyalist}
    [/side]

    [side]
        side=2
        controller=ai
        no_leader=yes
        team_name=ogres
        user_team_name=_"Ogres"
        [ai]
            aggression=1.0

            {MODIFY_AI_ADD_CANDIDATE_ACTION 2 main_loop (
                [candidate_action]
                    engine=lua
                    name=ogres_flee
                    id=ogres_flee
                    max_score=110000
                    location="campaigns/Eastern_Invasion/ai/ca_ogres_flee.lua"
                [/candidate_action]
            )}
        [/ai]
    [/side]

    [event]
        name=prestart

        {SCATTER_UNITS {ON_DIFFICULTY 8 7 6} "Ogre" 0 (
            x=18-24
            y=11-14
            [not]
                [filter]
                [/filter]
            [/not]
        ) (
            side=2
            generate_name=yes
            random_traits=yes
        )}

        [objectives]
            side=1
            [objective]
                description= _ "Capture as many ogres as you can" + "<span foreground='white'> <small>" + _"(captured ogres will be made available for recall)" + "</small></span>"
                condition=win
                show_turn_counter=yes
            [/objective]
            [objective]
                description= _ "Death of Gweddry"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Dacyn"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Owaec"
                condition=lose
            [/objective]

            [note]
                description= _ "An ogre is captured when it starts a turn being unable to move more than one hex."
            [/note]
            [note]
                description= _ "If an ogre reaches the edge of the map, it will escape."
            [/note]

            [gold_carryover]
                carryover_percentage=40
            [/gold_carryover]
        [/objectives]
    [/event]

    [event]
        name=start

        [recall]
            id=Dacyn
            x,y=20,19
        [/recall]
        [recall]
            id=Owaec
            x,y=22,19
        [/recall]

        [message]
            speaker=Gweddry
            message= _ "Look, this valley is inhabited by a tribe of ogres. We should try to capture some to train."
        [/message]
        [message]
            race=ogre
            message= _ "Humans are coming! Run!"
        [/message]
        [message]
            speaker=Owaec
            message= _ "Looks like they are fleeing. I think they will surrender easily if we can surround them."
        [/message]
        [message]
            speaker=Dacyn
            message= _ "All right, we have to capture them one by one."
        [/message]
    [/event]

    [event]
        name=moveto
        first_time_only=no

        [filter]
            race=ogre
            [filter_location]
                {MAP_EDGES_SLF}
            [/filter_location]
        [/filter]

        [set_variables]
            name=ogre_escape_msgs

            [value]
                message= _ "Waah! Run!" # wmllint: no spellcheck
            [/value]

            [value]
                message= _ "Run for your lives!"
            [/value]

            [value]
                message= _ "Help!"
            [/value]
        [/set_variables]

        [if]
            [variable]
                name=next_ogre_escape_msg
                equals=$empty
            [/variable]

            [then]
                {VARIABLE next_ogre_escape_msg 0}
            [/then]

            [else]
                {VARIABLE_OP next_ogre_escape_msg add 1}
                {VARIABLE_OP next_ogre_escape_msg modulo $ogre_escape_msgs.length}
            [/else]
        [/if]

        [message]
            speaker=unit
            message=$ogre_escape_msgs[$next_ogre_escape_msg].message
        [/message]

        [kill]
            x,y=$x1,$y1
        [/kill]

        {CLEAR_VARIABLE ogre_escape_msgs}
    [/event]

    [event]
        name=moveto

        [filter]
            race=ogre
            [filter_location]
                {MAP_EDGES_SLF}
            [/filter_location]
        [/filter]

        [message]
            speaker=Gweddry
            message= _ "We have let one escape. Let’s hope they don’t all!"
        [/message]
    [/event]

    [event]
        name=side 2 turn
        first_time_only=no

        # This stores all ogres which have no free adjacent locations which
        # aren't adjacent to a side 1 unit; that is, the ones which are
        # ZoC-trapped so that they can't move more than one hex.
        [store_unit]
            [filter]
                side=2

                [filter_location]
                    [not]
                        [filter_adjacent_location]
                            [not]
                                [filter]
                                    side=1
                                [/filter]
                            [/not]

                            [not]
                                [filter_adjacent_location]
                                    [filter]
                                        side=1
                                    [/filter]
                                [/filter_adjacent_location]
                            [/not]

                            [not]
                                terrain=*^X*
                            [/not]
                        [/filter_adjacent_location]
                    [/not]
                [/filter_location]
            [/filter]

            kill=no
            variable=ogres_to_capture
        [/store_unit]

        {FOREACH ogres_to_capture i}
            {VARIABLE ogre_name $ogres_to_capture[$i].name}

            [set_variables]
                name=ogre_capture_msgs

                [value]
                    message= _ "$ogre_name surrender!"
                [/value]

                [value]
                    message= _ "Don't hurt $ogre_name|!"
                [/value]

                [value]
                    message= _ "$ogre_name will be good! Promise!"
                [/value]
            [/set_variables]

            [if]
                [variable]
                    name=next_ogre_capture_msg
                    equals=$empty
                [/variable]

                [then]
                    {VARIABLE next_ogre_capture_msg 0}
                [/then]

                [else]
                    {VARIABLE_OP next_ogre_capture_msg add 1}
                    {VARIABLE_OP next_ogre_capture_msg modulo $ogre_capture_msgs.length}
                [/else]
            [/if]

            [message]
                x,y=$ogres_to_capture[$i].x,$ogres_to_capture[$i].y
                message=$ogre_capture_msgs[$next_ogre_capture_msg].message
            [/message]

            [kill]
                x,y=$ogres_to_capture[$i].x,$ogres_to_capture[$i].y
            [/kill]

            {VARIABLE ogres_to_capture[$i].side 1}

            [unstore_unit]
                variable=ogres_to_capture[$i]
                x,y=recall,recall
            [/unstore_unit]
        {NEXT i}

        {CLEAR_VARIABLE ogres_to_capture,ogre_name,ogre_capture_msgs}
    [/event]

    [event]
        name=new turn

        [filter_condition]
            [not]
                [have_unit]
                    side=2
                [/have_unit]
            [/not]
        [/filter_condition]

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

    [event]
        name=time over

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

    [event]
        name=victory

        [store_unit]
            [filter]
                side=1
                race=ogre
            [/filter]

            kill=no
            variable=ogres
        [/store_unit]

        [switch]
            variable=ogres.length

            [case]
                value=0

                [message]
                    speaker=Gweddry
                    message= _ "We failed to capture any of them!"
                [/message]
            [/case]

            [case]
                value=1

                [message]
                    speaker=Gweddry
                    message= _ "We only captured one ogre."
                [/message]
            [/case]

            [case]
                value=2

                [message]
                    speaker=Gweddry
                    message= _ "We failed to capture more than two of the ogres."
                [/message]
            [/case]

            [case]
                value=3

                [message]
                    speaker=Gweddry
                    message= _ "We captured three ogres! I hope they will prove useful."
                [/message]
            [/case]

            [else]
                [message]
                    speaker=Gweddry
                    message= _ "We managed to capture many ogres! They will make a good addition to our troops."
                [/message]
            [/else]
        [/switch]

        {CLEAR_VARIABLE ogres,next_ogre_escape_msg,next_ogre_capture_msg}
    [/event]

    {FOREIGN_DEFEAT}
[/scenario]
