#textdomain wesnoth

#define AI_CONTROLLER AFFIX PLAYER_SIDES ALLY_SIDES VARIABLES_WML
    # expects $order_type to be set
    [event]
        name=apply_ai_params
        first_time_only=no

        [set_variables]
            name=new_ai_params

            [insert_tag]
                name=literal
                variable=ai_controller_{AFFIX}.$order_type|.ai
            [/insert_tag]
        [/set_variables]

        [if]
            [variable]
                name=ai_controller_{AFFIX}.$order_type|.type
                equals=behaviour
            [/variable]

            [variable]
                name=ai_controller.side_$ally_side|_current_settings.current_objective
                boolean_equals=yes
            [/variable]

            [then]
                [set_variables]
                    name=new_ai_params
                    mode=append

                    [insert_tag]
                        name=literal
                        variable=ai_controller_{AFFIX}.$ai_controller.side_$ally_side|_current_settings.current_objective|.ai
                    [/insert_tag]
                [/set_variables]
            [/then]
        [/if]

        [if]
            [variable]
                name=ai_controller_{AFFIX}.$order_type|.type
                equals=objective
            [/variable]

            [variable]
                name=ai_controller.side_$ally_side|_current_settings.current_behaviour
                boolean_equals=yes
            [/variable]

            [then]
                [set_variables]
                    name=new_ai_params
                    mode=append

                    [insert_tag]
                        name=literal
                        variable=ai_controller_{AFFIX}.$ai_controller.side_$ally_side|_current_settings.current_behaviour|.ai
                    [/insert_tag]
                [/set_variables]
            [/then]
        [/if]

        [if]
            [and]
                [variable]
                    name=ai_controller_{AFFIX}.$ai_controller.side_$ally_side|_current_settings.current_behaviour|.mode
                    equals=append
                [/variable]

                [or]
                    [variable]
                        name=ai_controller_{AFFIX}.$ai_controller.side_$ally_side|_current_settings.current_behaviour|.mode
                        equals=$empty
                    [/variable]
                [/or]
            [/and]

            [and]
                [variable]
                    name=ai_controller_{AFFIX}.$ai_controller.side_$ally_side|_current_settings.current_objective|.mode
                    equals=append
                [/variable]

                [or]
                    [variable]
                        name=ai_controller_{AFFIX}.$ai_controller.side_$ally_side|_current_settings.current_objective|.mode
                        equals=$empty
                    [/variable]
                [/or]
            [/and]

            [then]
                [set_variables]
                    name=new_ai_params
                    mode=append

                    [insert_tag]
                        name=literal
                        variable=ai_controller.side_$ally_side|_original_settings.ai_settings.ai
                    [/insert_tag]
                [/set_variables]
            [/then]
        [/if]

        [modify_side]
            side=$ally_side

            [insert_tag]
                name=ai
                variable=new_ai_params
            [/insert_tag]
        [/modify_side]

        {CLEAR_VARIABLE new_ai_params}
    [/event]

    [event]
        name=prestart

        [set_variables]
            name=ai_controller_{AFFIX}

            # This block contains all the default settings and options.
            [literal]
                enabled=yes
                player_side={PLAYER_SIDES}
                ally_sides={ALLY_SIDES}
                disabled_for_sides=0

                ################

                [aggression]
                    type=behaviour

                    [ai]
                        aggression=0.9
                        caution=0.1
                    [/ai]

                    [option]
                        message= _ "Be more aggressive"

                        [show_if]
                            [variable]
                                name=ai_controller_{AFFIX}.aggression.disabled
                                not_equals=yes
                            [/variable]
                        [/show_if]

                        [command]
                            {VARIABLE order_type aggression}

                            {MODIFY_AI_ADD_SIMPLE_ASPECT $ally_side aggression set_by_ai_controller "0.9"}
                            {MODIFY_AI_ADD_SIMPLE_ASPECT $ally_side caution set_by_ai_controller "0.1"}

                            {VARIABLE ai_controller.side_$ally_side|_current_settings.current_behaviour aggression}
                            {VARIABLE ai_controller.side_$ally_side|_current_settings.currently_doing_behaviour_description _"aggressive."}
                        [/command]
                    [/option]
                [/aggression]

                ################

                [caution]
                    type=behaviour

                    [ai]
                        aggression=0.1
                        caution=1.0
                        grouping=defensive
                    [/ai]

                    [option]
                        message= _ "Be more defensive"

                        [show_if]
                            [variable]
                                name=ai_controller_{AFFIX}.caution.disabled
                                not_equals=yes
                            [/variable]
                        [/show_if]

                        [command]
                            {VARIABLE order_type caution}

                            {VARIABLE ai_controller.side_$ally_side|_current_settings.current_behaviour caution}
                            {VARIABLE ai_controller.side_$ally_side|_current_settings.currently_doing_behaviour_description _"defensive."}

                            {MODIFY_AI_ADD_SIMPLE_ASPECT $ally_side aggression set_by_ai_controller "0.1"}
                            {MODIFY_AI_ADD_SIMPLE_ASPECT $ally_side caution set_by_ai_controller "1.0"}
                        [/command]
                    [/option]
                [/caution]

                ################

                [focus_on_enemy]
                    type=objective

                    [ai]
                        [target]
                            side=$temp_i
                            value=20
                        [/target]
                    [/ai]

                    [option]
                        message= _ "Focus on defeating..."

                        [show_if]
                            [variable]
                                name=ai_controller_{AFFIX}.focus_on_enemy.disabled
                                not_equals=yes
                            [/variable]
                        [/show_if]

                        [command]
                            [set_variables]
                                name=focus_on_enemy_options_message

                                [literal]
                                    caption= _"Instruct ally"
                                    speaker=narrator
                                    image=wesnoth-icon.png
                                    message= _"Which enemy side to focus on?"
                                [/literal]
                            [/set_variables]

                            [store_side]
                                side=$ally_side
                                variable=stored_ally_side
                            [/store_side]

                            {VARIABLE temp_i 1}

                            [while]
                                [variable]
                                    name=temp_i
                                    less_than=10
                                [/variable]

                                [do]
                                    [store_side]
                                        side=$temp_i
                                        variable=stored_other_side
                                    [/store_side]

                                    [if]
                                        [variable]
                                            name=stored_ally_side.team_name
                                            not_equals=$stored_other_side.team_name
                                        [/variable]

                                        [have_unit]
                                            side=$temp_i
                                        [/have_unit]

                                        [then]
                                            [if]
                                                [have_unit]
                                                    side=$temp_i
                                                    canrecruit=yes

                                                    [filter_vision]
                                                        viewing_side=$side_number
                                                        visible=yes
                                                    [/filter_vision]
                                                [/have_unit]

                                                [then]
                                                    [store_unit]
                                                        [filter]
                                                            side=$temp_i
                                                            canrecruit=yes
                                                        [/filter]

                                                        kill=no
                                                        variable=side_i_leader
                                                    [/store_unit]

                                                    {VARIABLE this_enemy_txt_img_description "&$side_i_leader.image~TC($temp_i|,$side_i_leader.flag_rgb|)=$side_i_leader.name|="+_"(side $temp_i|)"}

                                                    {VARIABLE this_enemy_description _"$side_i_leader.name (side $temp_i|)"}
                                                [/then]

                                                [else]
                                                    {VARIABLE this_enemy_txt_img_description "&misc/blank-hex.png= ="+_"(side $temp_i|)"}
                                                    {VARIABLE this_enemy_description _"side $temp_i"}
                                                [/else]
                                            [/if]

                                            [set_variables]
                                                name=focus_on_enemy_options_message.option
                                                mode=append

                                                [value]
                                                    message=$this_enemy_txt_img_description

                                                    [command]
                                                        {VARIABLE order_type focus_on_enemy}

                                                        {VARIABLE ai_controller.side_$ally_side|_current_settings.focus_on_enemy.side $temp_i}
                                                        {VARIABLE ai_controller.side_$ally_side|_current_settings.current_objective focus_on_enemy}
                                                        {VARIABLE ai_controller.side_$ally_side|_current_settings.currently_doing_objective_description _"focusing on defeating $this_enemy_description|."}

                                                        {MODIFY_AI_ADD_GOAL $ally_side (
                                                            [goal]
                                                                id=set_by_ai_controller
                                                                value=20
                                                                [criteria]
                                                                    side=$temp_i
                                                                [/criteria]
                                                            [/goal]
                                                        )}
                                                    [/command]
                                                [/value]
                                            [/set_variables]
                                        [/then]
                                    [/if]

                                    {VARIABLE_OP temp_i add 1}
                                [/do]
                            [/while]

                            [set_variables]
                                name=focus_on_enemy_options_message.option
                                mode=append

                                [value]
                                    message="<span size='larger'>"+_"Back" + "</span>"

                                    [command]
                                    [/command]
                                [/value]
                            [/set_variables]

                            [insert_tag]
                                name=message
                                variable=focus_on_enemy_options_message
                            [/insert_tag]

                            {CLEAR_VARIABLE stored_other_side,focus_on_enemy_options_message,side_i_leader,this_enemy_description}
                        [/command]
                    [/option]
                [/focus_on_enemy]

                ################

                [protect]
                    type=objective

                    [ai]
                        [target]
                            side=$temp_i
                            value=20
                        [/target]
                    [/ai]

                    [option]
                        message= _ "Focus on protecting..."

                        [show_if]
                            [variable]
                                name=ai_controller_{AFFIX}.protect.disabled
                                not_equals=yes
                            [/variable]
                        [/show_if]

                        [command]
                            [set_variables]
                                name=protect_options_message

                                [literal]
                                    caption= _"Instruct ally"
                                    speaker=narrator
                                    image=wesnoth-icon.png
                                    message= _"Which friendly side to protect?"
                                [/literal]
                            [/set_variables]

                            [store_side]
                                side=$ally_side
                                variable=stored_ally_side
                            [/store_side]

                            {VARIABLE temp_i 1}

                            [while]
                                [variable]
                                    name=temp_i
                                    less_than=10
                                [/variable]

                                [do]
                                    [store_side]
                                        side=$temp_i
                                        variable=stored_other_side
                                    [/store_side]

                                    [if]
                                        [variable]
                                            name=ally_side
                                            not_equals=$temp_i
                                        [/variable]

                                        [variable]
                                            name=stored_ally_side.team_name
                                            equals=$stored_other_side.team_name
                                        [/variable]

                                        [have_unit]
                                            side=$temp_i
                                        [/have_unit]

                                        [then]
                                            [if]
                                                [have_unit]
                                                    side=$temp_i
                                                    canrecruit=yes
                                                [/have_unit]

                                                [then]
                                                    [store_unit]
                                                        [filter]
                                                            side=$temp_i
                                                            canrecruit=yes

                                                            [filter_vision]
                                                                viewing_side=$side_number
                                                                visible=yes
                                                            [/filter_vision]
                                                        [/filter]

                                                        kill=no
                                                        variable=side_i_leader
                                                    [/store_unit]

                                                    {VARIABLE this_ally_txt_img_description "&$side_i_leader.image~TC($temp_i|,$side_i_leader.flag_rgb|)=$side_i_leader.name|="+_"(side $temp_i|)"}
                                                    {VARIABLE this_ally_description _"$side_i_leader.name (side $temp_i|)"}
                                                [/then]

                                                [else]
                                                    {VARIABLE this_ally_txt_img_description "&misc/blank-hex.png= ="+_"(side $temp_i|)"}
                                                    {VARIABLE this_ally_description _"side $temp_i"}
                                                [/else]
                                            [/if]

                                            [set_variables]
                                                name=protect_options_message.option
                                                mode=append

                                                [value]
                                                    message=$this_ally_txt_img_description

                                                    [command]
                                                        {VARIABLE order_type protect}

                                                        {VARIABLE ai_controller.side_$ally_side|_current_settings.protect.side $temp_i}
                                                        {VARIABLE ai_controller.side_$ally_side|_current_settings.current_objective protect}
                                                        {VARIABLE ai_controller.side_$ally_side|_current_settings.currently_doing_objective_description _"focusing on protecting $this_ally_description|."}

                                                        {MODIFY_AI_ADD_GOAL $ally_side (
                                                            [goal]
                                                                id=set_by_ai_controller
                                                                name=protect
                                                                value=20
                                                                [criteria]
                                                                    [filter]
                                                                        side=$temp_i
                                                                    [/filter]
                                                                [/criteria]
                                                            [/goal]
                                                        )}
                                                    [/command]
                                                [/value]
                                            [/set_variables]
                                        [/then]
                                    [/if]

                                    {VARIABLE_OP temp_i add 1}
                                [/do]
                            [/while]

                            [set_variables]
                                name=protect_options_message.option
                                mode=append

                                [value]
                                    message="<span size='larger'>" + _"Back" + "</span>"

                                    [command]
                                    [/command]
                                [/value]
                            [/set_variables]

                            [insert_tag]
                                name=message
                                variable=protect_options_message
                            [/insert_tag]

                            {CLEAR_VARIABLE stored_other_side,side_i_leader,this_ally_description,protect_options_message}
                        [/command]
                    [/option]
                [/protect]

                ################

                [defend_location]
                    type=objective

                    [ai]
                        [protect_location]
                            x=$ai_controller.side_$ally_side|_current_settings.defend_location.x
                            y=$ai_controller.side_$ally_side|_current_settings.defend_location.y
                            radius=6
                            value=20
                        [/protect_location]
                    [/ai]

                    [option]
                        message= _ "Defend a location..."

                        [show_if]
                            [variable]
                                name=ai_controller_{AFFIX}.defend_location.disabled
                                not_equals=yes
                            [/variable]
                        [/show_if]

                        [command]
                            {VARIABLE ai_controller_{AFFIX}.defend_location.picker_active yes}
                            {VARIABLE ai_controller_{AFFIX}.defend_location.original_controller_invokation_x $x1}
                            {VARIABLE ai_controller_{AFFIX}.defend_location.original_controller_invokation_y $y1}
                            {VARIABLE ai_controller_finished yes}

                            [print]
                                text= _ "Right-click to select a location to defend"
                                duration=100
                                {COLOR_WHITE}
                            [/print]

                            [set_menu_item]
                                id=ai_controller_defend_location_picker
                                description= _ "Defend this location"

                                [show_if]
                                    [variable]
                                        name=ai_controller_{AFFIX}.defend_location.picker_active
                                        boolean_equals=yes
                                    [/variable]
                                [/show_if]

                                [command]
                                    {VARIABLE ai_controller.side_$ally_side|_current_settings.defend_location.x $x1}
                                    {VARIABLE ai_controller.side_$ally_side|_current_settings.defend_location.y $y1}
                                    {VARIABLE ai_controller_{AFFIX}.defend_location.picker_active no}

                                    {VARIABLE order_type defend_location}

                                    {VARIABLE ai_controller.side_$ally_side|_current_settings.current_objective defend_location}
                                    {VARIABLE ai_controller.side_$ally_side|_current_settings.currently_doing_objective_description _"focusing on defending the area around $x1|,$y1|."}

                                    {MODIFY_AI_ADD_GOAL $ally_side (
                                        [goal]
                                            id=set_by_ai_controller
                                            name=protect
                                            value=20
                                            [criteria]
                                                x,y=$x1,$y1
                                            [/criteria]
                                        [/goal]
                                    )}

                                    [print]
                                        text=" "	# wmllint: ignore
                                        duration=1
                                    [/print]

                                    [fire_event]
                                        name=menu item ai_controller

                                        [primary_unit]
                                            x=$ai_controller_{AFFIX}.defend_location.original_controller_invokation_x
                                            y=$ai_controller_{AFFIX}.defend_location.original_controller_invokation_y
                                        [/primary_unit]
                                    [/fire_event]
                                [/command]
                            [/set_menu_item]

                            [event]
                                name=select,recruit,recall,side turn,menu item ai_controller

                                [if]
                                    [variable]
                                        name=ai_controller_{AFFIX}.defend_location.picker_active
                                        boolean_equals=yes
                                    [/variable]

                                    [then]
                                        {VARIABLE ai_controller_{AFFIX}.defend_location.picker_active no}

                                        [print]
                                            text= _ "Location selection canceled"
                                            duration=100
                                            {COLOR_WHITE}
                                        [/print]
                                    [/then]
                                [/if]
                            [/event]
                        [/command]
                    [/option]
                [/defend_location]

                ################

                [revoke_objective_orders]
                    [option]
                        message= _ "Clear objective orders"

                        [show_if]
                            [variable]
                                name=ai_controller_{AFFIX}.revoke_objective_orders.disabled
                                not_equals=yes
                            [/variable]
                        [/show_if]

                        [command]
                            {MODIFY_AI_DELETE_GOAL $ally_side set_by_ai_controller}

                            {CLEAR_VARIABLE ai_controller.side_$ally_side|_current_settings.current_objective}
                        [/command]
                    [/option]
                [/revoke_objective_orders]

                ################

                [revoke_behaviour_orders]
                    [option]
                        message= _ "Clear behavior orders"

                        [show_if]
                            [variable]
                                name=ai_controller_{AFFIX}.revoke_behaviour_orders.disabled
                                not_equals=yes
                            [/variable]
                        [/show_if]

                        [command]
                            {MODIFY_AI_DELETE_ASPECT $ally_side aggression set_by_ai_controller}
                            {MODIFY_AI_DELETE_ASPECT $ally_side caution set_by_ai_controller}

                            {CLEAR_VARIABLE ai_controller.side_$ally_side|_current_settings.current_behaviour}
                        [/command]
                    [/option]
                [/revoke_behaviour_orders]

                ################

                [revoke_all_orders]
                    [option]
                        message= _ "Clear all orders"

                        [show_if]
                            [variable]
                                name=ai_controller_{AFFIX}.revoke_orders.disabled
                                not_equals=yes
                            [/variable]
                        [/show_if]

                        [command]
                            {MODIFY_AI_DELETE_GOAL $ally_side set_by_ai_controller}
                            {MODIFY_AI_DELETE_ASPECT $ally_side aggression set_by_ai_controller}
                            {MODIFY_AI_DELETE_ASPECT $ally_side caution set_by_ai_controller}

                            {CLEAR_VARIABLE ai_controller.side_$ally_side|_current_settings}
                        [/command]
                    [/option]
                [/revoke_all_orders]
            [/literal]
        [/set_variables]

        [set_variables]
            name=ai_controller_{AFFIX}
            mode=merge

            [literal]
                {VARIABLES_WML}
            [/literal]
        [/set_variables]

        [set_menu_item]
            id=ai_controller
            description= _ "Instruct ally"

            [show_if]
                [have_unit]
                    side=$ai_controller_{AFFIX}.ally_sides
                    x,y=$x1,$y1

                    [not]
                        side=$ai_controller_{AFFIX}.disabled_for_sides
                    [/not]
                [/have_unit]

                [variable]
                    name=ai_controller_{AFFIX}.enabled
                    equals=yes
                [/variable]

                [variable]
                    name=ai_controller_{AFFIX}.player_side
                    contains=$side_number
                [/variable]

                [variable]
                    name=ai_controller_{AFFIX}.defend_location.picker_active
                    boolean_equals=no
                [/variable]
            [/show_if]

            [command]
                [store_unit]
                    [filter]
                        x,y=$x1,$y1
                    [/filter]

                    kill=no
                    variable=ally_unit
                [/store_unit]

                [store_unit]
                    [filter]
                        side=$ally_unit.side
                        canrecruit=yes
                    [/filter]

                    kill=no
                    variable=ally_leader
                [/store_unit]

                {VARIABLE ally_side $ally_leader.side}

                [if]
                    [variable]
                        name=ai_controller.side_$ally_side|_original_settings.side
                        equals=$empty
                    [/variable]

                    [then]
                        [store_side]
                            side=$ally_side
                            variable=ai_controller.side_$ally_side|_original_settings
                        [/store_side]

                        [set_variables]
                            name=ai_controller.side_$ally_side|_original_settings.ai_settings

                            [value]
                                side=$ally_side

                                [insert_tag]
                                    name=ai
                                    variable=ally_leader.ai
                                [/insert_tag]
                            [/value]
                        [/set_variables]
                    [/then]
                [/if]

                {CLEAR_VARIABLE ai_controller_finished}

                [while]
                    [variable]
                        name=ai_controller_finished
                        not_equals=yes
                    [/variable]

                    [do]
                        [if]
                            [variable]
                                name=ai_controller.side_$ally_side|_current_settings.current_objective
                                equals=$empty
                            [/variable]

                            [then]
                                {VARIABLE ai_controller.side_$ally_side|_current_settings.currently_doing_objective_description _"none set."}
                            [/then]
                        [/if]

                        [if]
                            [variable]
                                name=ai_controller.side_$ally_side|_current_settings.current_behaviour
                                equals=$empty
                            [/variable]

                            [then]
                                {VARIABLE ai_controller.side_$ally_side|_current_settings.currently_doing_behaviour_description _"none set."}
                            [/then]
                        [/if]

                        [if]
                            [variable]
                                name=ai_controller.side_$ally_side|_current_settings.current_objective
                                equals=$empty
                            [/variable]

                            [variable]
                                name=ai_controller.side_$ally_side|_current_settings.current_behaviour
                                equals=$empty
                            [/variable]

                            [then]
                                [set_variables]
                                    name=ai_controller_{AFFIX}.main_menu_message

                                    [value]
                                        speaker=narrator
                                        caption= _ "Instruct ally"
                                        message= _ "$ally_leader.name (side $ally_side|) has not been given any instructions and is acting normally."+"
<span color='black'>-</span>"+"
"+_"Objective: $ai_controller.side_$ally_side|_current_settings.currently_doing_objective_description
Behavior: $ai_controller.side_$ally_side|_current_settings.currently_doing_behaviour_description"+"
<span color='black'>-</span>"
                                        image=wesnoth-icon.png
                                    [/value]
                                [/set_variables]
                            [/then]

                            [else]
                                [set_variables]
                                    name=ai_controller_{AFFIX}.main_menu_message

                                    [value]
                                        speaker=narrator
                                        caption= _ "Instruct ally"
                                        message= _ "$ally_leader.name (side $ally_side|) is carrying out the following instructions:"+"
<span color='black'>-</span>"+"
"+_"Objective: $ai_controller.side_$ally_side|_current_settings.currently_doing_objective_description
Behavior: $ai_controller.side_$ally_side|_current_settings.currently_doing_behaviour_description"+"
<span color='black'>-</span>"
                                        image=wesnoth-icon.png
                                    [/value]
                                [/set_variables]
                            [/else]
                        [/if]

                        [set_variables]
                            name=ai_controller_{AFFIX}.main_menu_message.option

                            [literal]
                                message= _ "Set objective..."

                                [command]
                                    [message]
                                        speaker=narrator
                                        caption= _ "Instruct ally"
                                        message=$ai_controller_{AFFIX}.main_menu_message.message
                                        image=wesnoth-icon.png

                                        [insert_tag]
                                            name=option
                                            variable=ai_controller_{AFFIX}.focus_on_enemy.option
                                        [/insert_tag]

                                        [insert_tag]
                                            name=option
                                            variable=ai_controller_{AFFIX}.protect.option
                                        [/insert_tag]

                                        [insert_tag]
                                            name=option
                                            variable=ai_controller_{AFFIX}.defend_location.option
                                        [/insert_tag]

                                        [insert_tag]
                                            name=option
                                            variable=ai_controller_{AFFIX}.revoke_objective_orders.option
                                        [/insert_tag]

                                        [option]
                                            message="<span size='larger'>" + _"Back" + "</span>"

                                            [command]
                                            [/command]
                                        [/option]
                                    [/message]
                                [/command]
                            [/literal]

                            [literal]
                                message= _ "Set behavior..."

                                [command]
                                    [message]
                                        speaker=narrator
                                        caption= _ "Instruct ally"
                                        message=$ai_controller_{AFFIX}.main_menu_message.message
                                        image=wesnoth-icon.png

                                        [insert_tag]
                                            name=option
                                            variable=ai_controller_{AFFIX}.aggression.option
                                        [/insert_tag]

                                        [insert_tag]
                                            name=option
                                            variable=ai_controller_{AFFIX}.caution.option
                                        [/insert_tag]

                                        [insert_tag]
                                            name=option
                                            variable=ai_controller_{AFFIX}.revoke_behaviour_orders.option
                                        [/insert_tag]

                                        [option]
                                            message="<span size='larger'>" + _"Back" + "</span>"

                                            [command]
                                            [/command]
                                        [/option]
                                    [/message]
                                [/command]
                            [/literal]

                            [insert_tag]
                                name=literal
                                variable=ai_controller_{AFFIX}.revoke_all_orders.option
                            [/insert_tag]

                            [literal]
                                message="<span size='larger'>" + _"Exit" + "</span>"

                                [command]
                                    {VARIABLE ai_controller_finished yes}

#ifndef MULTIPLAYER
                                    [allow_undo]
                                    [/allow_undo]
#endif
                                [/command]
                            [/literal]
                        [/set_variables]

                        [insert_tag]
                            name=message
                            variable=ai_controller_{AFFIX}.main_menu_message
                        [/insert_tag]
                    [/do]
                [/while]

                {CLEAR_VARIABLE ally_unit,ally_leader,ai_controller_{AFFIX}.main_menu_message}
            [/command]
        [/set_menu_item]
    [/event]

    [event]
        name=victory

        {CLEAR_VARIABLE ai_controller,ai_controller_{AFFIX}}
    [/event]
#enddef

#define DISABLE_AI_CONTROLLER AFFIX
    {VARIABLE ai_controller_{AFFIX}.enabled no}
#enddef

#define DISABLE_AI_CONTROLLER_FOR_SIDE AFFIX SIDE
    [if]
        [variable]
            name=ai_controller_{AFFIX}.disabled_for_sides
            equals=0
        [/variable]

        [not]
            [variable]
                name=ai_controller_{AFFIX}.disabled_for_sides
                contains={SIDE}
            [/variable]
        [/not]

        [then]
            {VARIABLE ai_controller_{AFFIX}.disabled_for_sides {SIDE}}
        [/then]

        [else]
            [if]
                [variable]
                    name=ai_controller_{AFFIX}.disabled_for_sides
                    contains={SIDE}
                [/variable]

                [else]
                    {VARIABLE ai_controller_{AFFIX}.disabled_for_sides $ai_controller_{AFFIX}.disabled_for_sides|,{SIDE}}
                [/else]
            [/if]
        [/else]
    [/if]
#enddef

#define ENABLE_AI_CONTROLLER_FOR_SIDE AFFIX SIDE
    [if]
        [variable]
            name=ai_controller_{AFFIX}.disabled_for_sides
            contains={SIDE}
        [/variable]

        [then]
            [set_variables]
                name=disabled_for_sides_array

                [split]
                    list=ai_controller_{AFFIX}.disabled_for_sides
                    key=side
                    separator=,
                [/split]
            [/set_variables]

            {FOREACH disabled_for_sides_array disabled_for_side_i}
                [if]
                    [variable]
                        name=disabled_for_sides_array[$disabled_for_side_i].side
                        equals={SIDE}
                    [/variable]

                    [then]
                        {CLEAR_VARIABLE disabled_for_sides_array[$disabled_for_side_i]}
                    [/then]
                [/if]
            {NEXT disabled_for_side_i}

            [set_variable]
                name=ai_controller_{AFFIX}.disabled_for_sides

                [join]
                    variable=disabled_for_sides_array
                    key=side
                    separator=,
                [/join]
            [/set_variable]

            {CLEAR_VARIABLE disabled_for_sides_array}
        [/then]

        [else]
            {VARIABLE ai_controller_{AFFIX}.ally_sides $ai_controller_{AFFIX}.ally_sides|,{SIDE}}
        [/else]
    [/if]
#enddef

# A stub for scenario 7 of LoW
#define AI_LOCATION AFFIX STRING RADIUS X Y
    {SET_LABEL {X} {Y} ({STRING})}
#enddef

#define AI_CONTROLLER_FUTURE_STRINGS
    # These will be used for the special orders menu and the order for telling
    # the ally leader to move someplace
    future_string_1= _ "Set special orders..."	#wmllint: ignore
    future_string_2= _ "Move leader to..."	#wmllint: ignore
    future_string_3= _ "Right-click to select a location to move leader to"	#wmllint: ignore
    future_string_4= _ "Move leader here"	#wmllint: ignore
    future_string_5= _ "moving leader to $x1|,$y1|."	#wmllint: ignore
    future_string_6= _ "Clear special orders"	#wmllint: ignore
    future_string_7= _ "Special: $ai_controller.side_$ally_side|_current_settings.currently_doing_special_description"	#wmllint: ignore
#enddef
