#textdomain wesnoth-ai

[test]
    id=patrols
    name= _ "Patrols"
    next_scenario=micro_ai_test

    map_data="{multiplayer/maps/2p_Sullas_Ruins.map}"

    {DEFAULT_SCHEDULE}
    turns=unlimited
    victory_when_enemies_defeated=no

    [side]
        side=1
        controller=human
        suppress_end_turn_confirmation=yes
        id=Gertburt
        name= _ "Gertburt"
        unrenamable=yes
        type=Outlaw
        x,y=13,8

        persistent=no

        team_name=bandits
        user_team_name= _ "Bandits"
        recruit=Ruffian,Footpad,Thug,Poacher

        gold=200
    [/side]

    # Patrol AI
    [side]
        side=2
        controller=ai
        no_leader=yes
        persistent=no

        team_name=Konrad
        user_team_name= _ "team_name^Konrad"

        gold=0
        income=-2
    [/side]

    # Urudin's side
    # This is taken almost literally from 'Ka'lian under Attack' in 'Legend of Wesmere'
    [side]
        side=3
        controller=ai
        no_leader=yes
        persistent=no
        team_name=Urudin
        # wmllint: local spellings Urudin
        user_team_name= _ "team_name^Urudin"
        gold=0
        recruit=""
        [ai]
            [engine]
                name=lua
                code= <<
                    return wesnoth.require("ai/micro_ais/engines/urudin_engine.lua").init(ai)
                >>
            [/engine]
            [stage]
                id=leader_retreat
                engine=lua
                name=leader_retreat
                #retreat on > half HP lost  or turn>=3
                code="(...):retreat()"
            [/stage]
            [stage]
                name=ai_default_rca::candidate_action_evaluation_loop
                id=simple_main_loop
                {AI_CA_COMBAT}
                {AI_CA_SIMPLE_MOVE_TO_TARGETS}
            [/stage]
        [/ai]
    [/side]

    # Put all the patrol units and labels/items out there
    [event]
        name=prestart

        {VARIABLE scenario_name patrols}

        [unit]
            type=Spearman
            side=2
            id=Konrad
            name= _ "Konrad"
            x,y=5,7
            random_traits=no
        [/unit]
        [unit]
            type=Longbowman
            side=2
            id=patrol1
            random_traits=no
            x,y=14,12
        [/unit]
        [unit]
            type=Swordsman
            side=2
            id=patrol2
            random_traits=no
            x,y=17,12
        [/unit]

        [unit]
            type="Orcish Slayer"
            id=Urudin
            name= _ "Urudin"
            side=3
            x,y=22,4
        [/unit]

        [micro_ai]
            side=2
            ai_type=patrol
            action=add

            [filter]
                id=Konrad
            [/filter]
            ca_score=300000
            waypoint_x=9,24,25
            waypoint_y=21,23,15
            one_time_only=yes
            attack=Gertburt
        [/micro_ai]

        [micro_ai]
            side=2
            ai_type=patrol
            action=add

            [filter]
                id=patrol1
            [/filter]
            ca_score=299999
            waypoint_x=14,22,22,14,14
            waypoint_y=12,12,18,19,12
            out_and_back=yes
        [/micro_ai]

        [micro_ai]
            side=2
            ai_type=patrol
            action=add

            [filter]
                id=patrol2
            [/filter]
            ca_score=299998
            waypoint_x=14,14,22,22
            waypoint_y=12,19,18,12
            attack=xxxx # don't attack anybody
        [/micro_ai]

        [unit]
            type=Javelineer
            side=2
            id=guard1
            random_traits=no
            x,y=25,27
        [/unit]

        [micro_ai]
            side=2
            ai_type=zone_guardian
            action=add

            [filter]
                id=guard1
            [/filter]
            ca_score=299997
            [filter_location]
                x,y=10-26,26-28
            [/filter_location]
        [/micro_ai]

        {PLACE_IMAGE "scenery/signpost.png" 11 4}
        {SET_LABEL 11 4 _"End Scenario"}

        {PLACE_IMAGE "scenery/signpost.png" 9 21}
        {SET_LABEL 9 21 _"Konrad Waypoint 1"}
        {PLACE_IMAGE "scenery/signpost.png" 24 23}
        {SET_LABEL 24 23 _"Konrad Waypoint 2"}
        {PLACE_IMAGE "scenery/signpost.png" 25 15}
        {SET_LABEL 25 15 _"Konrad Final Waypoint"}

        {PLACE_IMAGE "scenery/signpost.png" 14 12}
        {SET_LABEL 14 12 _"Patrol Waypoint 1"}
        {PLACE_IMAGE "scenery/signpost.png" 14 19}
        {SET_LABEL 14 19 _"Patrol Waypoint 2"}
        {PLACE_IMAGE "scenery/signpost.png" 22 18}
        {SET_LABEL 22 18 _"Patrol Waypoint 3"}
        {PLACE_IMAGE "scenery/signpost.png" 22 12}
        {SET_LABEL 22 12 _"Patrol Waypoint 4"}

        {PLACE_IMAGE "scenery/signpost.png" 33 8}
        {SET_LABEL 33 8 _"Urudin retreats here"}
    [/event]

    [event]
        name=start

        {STORE_UNIT_VAR id=Konrad profile profile}
        # wmllint: unbalanced-on
        [message]
            speaker=Konrad
            image=$profile~FL()~RIGHT()
            message= _ "Hello!  I'm a Konrad impostor. We are going to demonstrate the Patrol AI to you in this scenario.

I am heading for the keep east of the central mountain via a couple waypoints in the south. I will stay there once I get there. By contrast, those two fellas in the center are perpetually circling the mountain, one of them always in the same direction, the other changing directions after every lap.

All of this is implemented by use of the Patrol [micro_ai] tag."
        [/message]
        # wmllint: unbalanced-off
        {CLEAR_VARIABLE profile}
        [message]
            speaker=guard1
            message= _ "By contrast, I am a zone guardian patrolling, in a way, the southernmost part of the map.  This AI is implemented via the Guardian [micro_ai] tag.  It is here mostly to demonstrate how to set up different Micro AIs for the same side.  For more details on different types of guardian AIs, there is a separate test scenario specializing on those."}
        [/message]
        [message]
            speaker=Urudin
            message= _ "And I am Urudin. I will attack my enemies for a few turns, but will retreat toward the right edge of the map if my hitpoints are below half of maximum or by Turn 5, whatever happens first.

This is an AI separate from the Patrols of Side 2."
        [/message]

        # wmllint: unbalanced-on
        [message]
            speaker=narrator
            image=wesnoth-icon.png
            caption= _ "Notes"
            message= _ "You, as the player, are in charge of Gertburt's bandits in this scenario. You can either simply watch the patrols move around, or you can move units into their way. The three patrol units are instructed to behave differently when facing enemy units:

Konrad only attacks Gertburt, or any enemy unit that blocks his final waypoint.

The Swordsman never attacks at all.

The Longbowman attacks any enemy unit he ends up next to at the end of his move.

They all have in common, however, that getting to their next waypoint takes priority over attacking. They will thus prefer to move around enemies rather than straight for them. Also, if a waypoint is occupied by a unit they are not instructed to attack, they will (eventually) abandon that waypoint once they get close enough and move on to the next one.

The Patrol AI controlling all Side 2 units is coded as a Micro AI. A Micro AI can be added and adapted to the need of a scenario easily using only WML and the [micro_ai] tag. Check out the <span color='#00A000'>Micro AI wiki page</span> at https://wiki.wesnoth.org/Micro_AIs for more information."
        [/message]
        # wmllint: unbalanced-off

        [objectives]
            summary= _ "Watch the patrols, attack them etc."
            [objective]
                description= _ "Defeat all enemy units"
                condition=win
            [/objective]
            [objective]
                description= _ "Move Gertburt to the signpost"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Gertburt"
                condition=lose
            [/objective]
        [/objectives]
    [/event]

    # When Konrad gets to the end of his route, display a message
    [event]
        name=moveto
        first_time_only=no
        [filter]
            id=Konrad
            x,y=25,15
        [/filter]

        [message]
            speaker=Konrad
            message= _ "Well, that was fun!  I'll just hang out here now and watch those two guys walk and walk and ..."
        [/message]
    [/event]

    # The events finishing the scenario
    [event]
        name=die
        first_time_only=no

        [if]
            [not]
                [have_unit]
                    side=2,3
                [/have_unit]
            [/not]
            [then]
                [kill]
                    id=$unit.id
                [/kill]

                [fire_event]
                    name=end_scenario
                [/fire_event]
            [/then]
        [/if]
    [/event]

    [event]
        name=moveto
        [filter]
            id=Gertburt
            x,y=11,4
        [/filter]

        [fire_event]
            name=end_scenario
        [/fire_event]
    [/event]

    [event]
        name=end_scenario

        [message]
            speaker=Gertburt
            message= _ "Let's go home, chaps."
        [/message]

        [endlevel]
            result=victory
            bonus=no
            carryover_percentage=0
            carryover_report=no
            linger_mode=no
        [/endlevel]
    [/event]
[/test]
