#textdomain wesnoth-ei
[scenario]
    id=07a_Capturing_the_Ogres
    name= _ "Capturing the Ogres"
    next_scenario=08_Xenophobia
    map_file=07a_Capturing_the_Ogres.map
    victory_when_enemies_defeated=no
    turns=unlimited
    {NORTHERN_SCHEDULE}

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

    {EI_TRACK {JOURNEY_07A_NEW} }

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

    # wmllint: validate-off
    [side]
        side=1
        controller=human
        gold=0
        team_name=good
        user_team_name= _ "Wesnothians"

        {FLAG_VARIANT loyalist}

        # wmllint: recognize Gweddry
        {CHARACTER_STATS_GWEDDRY}
    [/side]
    # wmllint: validate-on

    [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]

#define RECALL_OR_SPAWN X Y FACING
    [store_unit]
        [filter]
            side=1
            type_adv_tree=Horseman,Cavalryman,Fencer,Dune Rider,Dune Skirmisher,Footpad
            [not]
                {EVERYWHERE}
            [/not]
        [/filter]
        kill=no
        variable=to_recall
    [/store_unit]
    [if]
        {VARIABLE_CONDITIONAL to_recall.length greater_than 0}
        [then]
            {RECALL_XY $to_recall[0].id {X} {Y}}
        [/then]

        [else]
            {GENERIC_UNIT 1 Cavalryman  {X} {Y}}
        [/else]
    [/if]
    {MODIFY_UNIT x,y={X},{Y} facing {FACING}}
    {CLEAR_VARIABLE to_recall}
#enddef
    [event]
        name=prestart

        # no recruiting, just recall Gweddry's fastest units
        # (spawn cavalrymen/horsement if not enough fast units)
        {RECALL_XY Terraent 27 23}
        {RECALL_XY Addogin 27 23}
        {RECALL_XY (Hahid al-Ali) 27 23}
        [if]
            [not]
                [have_unit]
                    x,y=27,23
                [/have_unit]
            [/not]

            [then]
                {RECALL_OR_SPAWN 27 23 nw}
            [/then]
        [/if]

        {RECALL_OR_SPAWN 24 20 ne}
        {RECALL_OR_SPAWN 27 21 nw}
        {RECALL_OR_SPAWN 25 23 ne}
        {RECALL_OR_SPAWN 28 22 nw}
        [modify_unit]
            [filter]
                side=1
                [filter_wml]
                    upkeep=full
                [/filter_wml]
            [/filter]
            [object]
                duration=scenario
                [effect]
                    apply_to=loyal
                [/effect]
            [/object]
        [/modify_unit]

        {GENERIC_UNIT 2 (Ogre) 19 12}
        {GENERIC_UNIT 2 (Great Ogre) 20 12}
        {GENERIC_UNIT 2 (Young Ogre) 21 12}
        {GENERIC_UNIT 2 (Ogre) 21 14}
        {GENERIC_UNIT 2 (Young Ogre) 23 13}
        {GENERIC_UNIT 2 (Young Ogre) 23 14}
        {GENERIC_UNIT 2 (Young Ogre) 25 12}
        {GENERIC_UNIT 2 (Young Ogre) 24 12}
        {GENERIC_UNIT 2 (Ogre) 26 13}

        [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, Dacyn, or 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=100
            [/gold_carryover]
        [/objectives]
    [/event]

    [event]
        name=start

        {RECALL_XY Dacyn 24 22}
        {RECALL_XY Owaec 26 19}
        {RECALL_XY Dolburras 28 19}

        [message]
            speaker=Gweddry
            message= _ "Look, this valley is inhabited by a tribe of ogres. They do not seem very bright..."
        [/message]
        [message]
            race=ogre
            message= _ "Guh... human? Human come! Run!"
        [/message]
        [message]
            speaker=Dacyn
            message= _ "Excellent, a source of slaves. If we surround and capture them, we can use them for risky missions and preserve our more valuable soldiers."
        [/message]
        [message]
            speaker=Owaec
            message= _ "Slaves?! No, these shall be great warriors fighting alongside us! My Clansmen shall 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]
                #po: ogres have intentionally bad grammar
                message= _ "Me run life for!"
            [/value]
            [value]
                message= _ "Ruuuunnnn!"
            [/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}
        {CLEAR_VARIABLE ogre_escape_msgs}
        [fire_event]
            name=first ogre escapes
        [/fire_event]
    [/event]

    [event]
        name=first ogre escapes

        [message]
            speaker=Gweddry
            message= _ "We have let one escape. Let’s hope not all of them do!"
        [/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]
            array=ogres_to_capture
            [do]
                {VARIABLE ogre_name $this_item.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 be good will! 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=$this_item.x,$this_item.y
                    message=$ogre_capture_msgs[$next_ogre_capture_msg].message
                [/message]
                {KILL x,y=$this_item.x,$this_item.y}
                {VARIABLE this_item.side 1}
                [unstore_unit]
                    variable=this_item
                    x,y=recall,recall
                [/unstore_unit]
            [/do]
        [/foreach]

        {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 100}
        [/endlevel]
    [/event]

    [event]
        name=time over
        [endlevel]
            result=victory
            {NEW_GOLD_CARRYOVER 100}
        [/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= _ "Taking slaves doesn’t feel right, even if it is for a good cause. I refuse to capture any of these ogres."
                [/message]
                [set_achievement]
                    content_for=eastern_invasion
                    id=ei_S07a
                [/set_achievement]
            [/case]
            [case]
                value=1
                [message]
                    speaker=Gweddry
                    message= _ "Taking slaves just doesn’t feel right. I only captured one."
                [/message]
                [message]
                    speaker=Dacyn
                    message= _ "Unfortunate, but at least we have one. Ogres are unskilled fighters but much cheaper to equip than our other soldiers, and thus <i><b>cost much less gold to recall.</b></i>"
                [/message]
            [/case]
            [case]
                value=2
                [message]
                    speaker=Gweddry
                    message= _ "We failed to capture more than two of the ogres."
                [/message]
                [message]
                    speaker=Dacyn
                    message= _ "Unfortunate, but at least we have some. Ogres are unskilled fighters but much cheaper to equip than our other soldiers, and thus <i><b>cost much less gold to recall.</b></i>"
                [/message]
            [/case]
            [case]
                value=3
                [message]
                    speaker=Gweddry
                    message= _ "We captured three ogres! I hope they will prove useful."
                [/message]
                [message]
                    speaker=Dacyn
                    message= _ "Well done. Ogres are unskilled fighters but much cheaper to equip than our other soldiers, and thus <i><b>cost much less gold to recall.</b></i>"
                [/message]
            [/case]
            [else]
                [message]
                    speaker=Gweddry
                    message= _ "We managed to capture many ogres! They will make a good addition to our troops."
                [/message]
                [message]
                    speaker=Dacyn
                    message= _ "Well done. Ogres are unskilled fighters but much cheaper to equip than our other soldiers, and thus <i><b>cost much less gold to recall.</b></i>"
                [/message]
            [/else]
        [/switch]

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

    {FOREIGN_DEFEAT}

    {HERODEATH_GWEDDRY}
    {HERODEATH_DACYN}
    {HERODEATH_OWAEC}
    {HERODEATH_ADDOGIN}
    {HERODEATH_HAHID}
    {HERODEATH_TERRAENT}
    {HERODEATH_GRUG}
    {HERODEATH_DOLBURRAS}
[/scenario]
