#textdomain wesnoth-ai

[test]
    id=protect_unit
    name= _ "Protect Unit"
    next_scenario=micro_ai_test

    map_data="{ai/micro_ais/maps/protect_unit.map}"

    {DEFAULT_SCHEDULE}
    turns=unlimited
    victory_when_enemies_defeated=no

    [side]
        side=1
        controller=ai
        suppress_end_turn_confirmation=yes
        name= _ "Langzhar"
        id=Langzhar
        type=Lieutenant
        persistent=no

        team_name=Langzhar
        user_team_name= _ "team_name^Langzhar"
        recruit=Spearman,Bowman

        gold=200
    [/side]

    [side]
        side=2
        controller=ai
        suppress_end_turn_confirmation=yes
        name= _ "Koorzhar"
        id=Koorzhar
        type=Lieutenant
        persistent=no
        defeat_condition=no_units_left

        team_name=Koorzhar
        user_team_name= _ "team_name^Koorzhar"
        recruit=Spearman,Bowman

        gold=175

        [ai]
            [goal]
                [criteria]
                    id=Rossauba
                [/criteria]
                value=100
            [/goal]
        [/ai]

        [ai]
            [engine]
                name="lua"
                code= <<
                    return wesnoth.require("ai/micro_ais/engines/priority_target_engine.lua").init()
                >>
            [/engine]
            [modify_ai]
                side=2
                action=add
                # wmllint: unbalanced-on
                path=stage[main_loop].candidate_action[]
                # wmllint: unbalanced-off
                [candidate_action]
                    engine=lua
                    name=change_attacks_aspect
                    id=change_attacks_aspect
                    max_score=999999
                    evaluation="return (...):change_attacks_aspect('Rossauba')"
                    execution="(...):change_attacks_aspect()"
                [/candidate_action]
            [/modify_ai]
        [/ai]
    [/side]

    [side]  # This side is only here because we need one persistent side for the game to go on
        side=3
        controller=null
        persistent=yes
        save_id=Grnk
        hidden=yes
    [/side]

    # Prestart actions
    [event]
        name=prestart

        {VARIABLE scenario_name protect_unit}

        [micro_ai]
            # Required keys of [micro_ai] tag
            side=1
            ai_type=protect_unit
            action=add

            [unit]
                id=Rossauba
                goal_x,goal_y=1,1
            [/unit]
        [/micro_ai]

        # Put the mage out there
        [unit]
            type=Elder Mage
            id=Rossauba
            name=Rossauba
            side=1
            x,y=20,9
            upkeep=loyal
            overlays=misc/hero-icon.png
        [/unit]

        # Goal signpost for Rossauba
        {PLACE_IMAGE "scenery/signpost.png" 1 1}
        {SET_LABEL 1 1 _"Move Rossauba here"}
    [/event]

    [event]
        name=start

        {STORE_UNIT_VAR id=Koorzhar profile profile}
        [message]
            speaker=Koorzhar
            image="$profile~FL()~RIGHT()"
            message= _ "There's that traitor wizard. Let's get him."
        [/message]
        {CLEAR_VARIABLE profile}
        [message]
            speaker=Langzhar
            message= _ "Men, you know the deal. We must protect Rossauba under all circumstances. Even my survival is not as important."
        [/message]
        [message]
            speaker=Rossauba
            message= _ "That's very kind of you, but ..."
        [/message]
        [message]
            speaker=Langzhar
            message= _ "No buts!  You stay behind the lines and do not engage in battle unless there is no risk to your life, is that understood?  And get to that signpost in the northwest if it is safe."
        [/message]

        [message]
            speaker=narrator
            caption= _ "Question for the Player"
            image=wesnoth-icon.png
            # wmllint: unbalanced-on
            message= _ "In this scenario, the AI playing the humans in the east (Langzhar) is instructed to protect the wizard Rossauba, while moving him safely to the signpost. On the other side, Koorzhar's units (in the west) will primarily attack Rossauba, even if a better target is available. Do you want to play either of the sides or let the AIs battle it out among themselves?

Note: The Protect Unit AI 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." # wmllint: no spellcheck
            # wmllint: unbalanced-off
            [option]
                label= _ "<span font='16'>I'll watch the two AIs fight it out</span>" # wmllint: no spellcheck
            [/option]
            [option]
                label= _ "<span font='16'>I'll play Langzhar's side (to see how Koorzhar's units target Rossauba)</span>"
                [command]
                    [modify_side]
                        side=1
                        controller=human
                    [/modify_side]
                [/command]
            [/option]
            [option]
                label= _ "<span font='16'>I'll play Koorzhar's side (to see how Langzhar's units protect Rossauba)</span>"
                [command]
                    [modify_side]
                        side=2
                        controller=human
                    [/modify_side]
                [/command]
            [/option]
        [/message]

        [objectives]
            side=1
            summary= _ "Protect Rossauba while moving him to the signpost"
            [objective]
                description= _ "Rossauba makes it to the signpost"
                condition=win
            [/objective]
            [objective]
                description= _ "Death of Rossauba"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Langzhar"
                condition=lose
            [/objective]
        [/objectives]
        [objectives]
            side=2
            summary= _ "Get rid of that traitor wizard Rossauba"
            [objective]
                description= _ "Defeat Rossauba"
                condition=win
            [/objective]
            [objective]
                description= _ "Rossauba makes it to the signpost"
                condition=lose
            [/objective]
            [objective]
                description= _ "Death of Koorzhar"
                condition=lose
            [/objective]
        [/objectives]
    [/event]

    # Delay Rossauba by one turn
    [event]
        name=side 1 turn 1 refresh

        {MODIFY_UNIT id=Rossauba moves 0}
    [/event]

    # All the end scenario events involving Rossauba are treated as victories
    # (since the player could play either side)
    # -> only defeat: when side leaders die in human-controlled mode
    # When Rossauba dies:
    [event]
        name=last breath
        [filter]
            id=Rossauba
        [/filter]

        [message]
            speaker=Rossauba
            message= _ "I held out for as long as I could."
        [/message]

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

    # When Rossauba makes it to the signpost:
    [event]
        name=moveto
        [filter]
            id=Rossauba
            x,y=1,1
        [/filter]

        [message]
            speaker=Rossauba
            message= _ "I made it"
        [/message]

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

    [event]
        name=end_scenario
        # So that game goes on to next scenario
        [modify_side]
            side=3
            controller=human
        [/modify_side]

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