#textdomain wesnoth-ei

# some examples:
# {MODIFY_AI_DELETE_ASPECT 2,3,4 avoid "*"}
# {MODIFY_SIDE_AI (2,3,4) (grouping=defensive)}
# {MODIFY_SIDE_AI (2,3,4) ({GOAL_SEEK_SIDE 99 1 4})}
# {MODIFY_SIDE_AI (2,3,4) ({GOAL_AVOID_SIDE 99 1 4})}
# {MODIFY_SIDE_AI (2,3,4) ({GOAL_LOCATION 99 (x,y=26,14)})}
# {MODIFY_SIDE_AI (2,3,4) ({GOAL_TERRAIN 15 (terrain=Kh,Ch,Chr,R*)})}
# {MODIFY_SIDE_AI (2,3,4) ({GOAL_LOCATION 99 (
#     x=0,1,2
#     y=0,1,2
# )})}
# {MODIFY_SIDE_AI (2) ([avoid]
#     x=13-32,20-32
#     y=1-6,1-19
# [/avoid])}

#--------------------
# GOALS AND AVOIDS
#--------------------
# don't abandon our leader, even when we'd normally be avoiding an enemy (since leaders don't run away)
#define GOAL_AVOID_SIDE_UNLESS_DEFENDING_LEADER VALUE MY_SIDE AVOID_SIDE AVOID_RADIUS
    {GOAL_LOCATION {VALUE} (
        [not]
            [filter]
                side={AVOID_SIDE}
            [/filter]
            radius={AVOID_RADIUS}
            [not]
                [filter]
                    side={MY_SIDE}
                    canrecruit=yes
                [/filter]
                radius=1
            [/not]
        [/not]
    )}
#enddef

#define GOAL_AVOID_SIDE VALUE SIDE RADIUS
    {GOAL_LOCATION {VALUE} (
        [not]
            [filter]
                side={SIDE}
            [/filter]
            radius={RADIUS}
        [/not]
    )}
#enddef

#define GOAL_SEEK_SIDE VALUE SIDE RADIUS
    {GOAL_LOCATION {VALUE} (
        [filter]
            side={SIDE}
        [/filter]
        radius={RADIUS}
    )}
#enddef

#define GOAL_UNIT VALUE CRITERIA
    [goal]
        name=target_unit
        [criteria]
            {CRITERIA}
        [/criteria]
        value={VALUE}
    [/goal]
#enddef

#define GOAL_LOCATION_ID VALUE ID CRITERIA
    [goal]
        id={ID}
        name=target_location
        [criteria]
            {CRITERIA}
        [/criteria]
        value={VALUE}
    [/goal]
#enddef

#define GOAL_TERRAIN VALUE CRITERIA
    [goal]
        name=target_location
        [criteria]
            {CRITERIA}
        [/criteria]
        value={VALUE}
    [/goal]
#enddef

#define GOAL_LOCATION VALUE CRITERIA
    [goal]
        name=target_location
        [criteria]
            {CRITERIA}
        [/criteria]
        value={VALUE}
    [/goal]
#enddef

#define AVOID_SIDE SIDE RADIUS
    [avoid]
        [filter]
            side={SIDE}
        [/filter]
        radius={RADIUS}
    [/avoid]
#enddef

#--------------------
# MODIFY AI
#--------------------
#define MODIFY_SIDE_AI SIDE MODIFICATION
    [modify_side]
        side={SIDE}
        [ai]
            {MODIFICATION}
        [/ai]
    [/modify_side]
#enddef

#define DISABLE_ATTACKING SIDE
    {MODIFY_AI_DELETE_CANDIDATE_ACTION {SIDE} main_loop spread_poison}
    {MODIFY_AI_DELETE_CANDIDATE_ACTION {SIDE} main_loop high_xp_attack}
    {MODIFY_AI_DELETE_CANDIDATE_ACTION {SIDE} main_loop combat}
#enddef

#define ENABLE_ATTACKING SIDE
    {MODIFY_AI_ADD_CANDIDATE_ACTION {SIDE} main_loop {AI_CA_SPREAD_POISON}}
    {MODIFY_AI_ADD_CANDIDATE_ACTION {SIDE} main_loop {AI_CA_HIGH_XP_ATTACK}}
    {MODIFY_AI_ADD_CANDIDATE_ACTION {SIDE} main_loop {AI_CA_COMBAT}}
#enddef

#--------------------
# OFFENSE/DEFENSE BEHAVIOR
#--------------------
#define RESET_SIDE_AI SIDE GROUPING AGGRESSION CAUTION
    {DISABLE_ATTACKING {SIDE}} # reset any attack-related CA score changes
    {ENABLE_ATTACKING {SIDE}}

    {MODIFY_AI_DELETE_GOAL {SIDE} "*"}
    {MODIFY_SIDE_AI ({SIDE}) (
        [avoid]
            x,y=0,0
        [/avoid]
    )}

    {MODIFY_AI_DELETE_ASPECT {SIDE} grouping "*"}
    {MODIFY_AI_DELETE_ASPECT {SIDE} aggression "*"}
    {MODIFY_AI_DELETE_ASPECT {SIDE} caution "*"}
    {MODIFY_AI_DELETE_ASPECT {SIDE} leader_value "*"}

    {MODIFY_SIDE_AI ({SIDE}) (
        grouping={GROUPING}
        aggression={AGGRESSION}
        caution={CAUTION}
        leader_value=0
    )
    }
#enddef

#define VARY_AI_BY_SCHEDULE SIDE
    {VARY_AI_BY_SCHEDULE_ENEMY {SIDE} 1}
#enddef

#define VARY_AI_BY_SCHEDULE_ENEMY SIDE ENEMY
    {VARY_AI_BY_SCHEDULE_ENEMY_TIME_OF_DAY {SIDE} {ENEMY} morning afternoon}
#enddef

#define VARY_AI_BY_SCHEDULE_ENEMY_NIGHTTIME SIDE ENEMY
    {VARY_AI_BY_SCHEDULE_ENEMY_TIME_OF_DAY {SIDE} {ENEMY} first_watch second_watch}
#enddef

#define VARY_AI_BY_SCHEDULE_WINTER SIDE
    {VARY_AI_BY_SCHEDULE_ENEMY_TIME_OF_DAY {SIDE} 1 midday midday}
#enddef

#define AND_NO_ENEMIES_NEAR_MY_LEADER SIDE
    [not]
        [have_unit]
            side={SIDE}
            canrecruit=yes
            [filter_adjacent]
                is_enemy=yes
            [/filter_adjacent]
        [/have_unit]
    [/not]
#enddef

#define VARY_AI_BY_SCHEDULE_ENEMY_TIME_OF_DAY SIDE ENEMY TIME1 TIME2
    [store_time_of_day]
    [/store_time_of_day]
    [if]
        [variable]
            name=time_of_day.id
            equals={TIME1}
        [/variable]

        {AND_NO_ENEMIES_NEAR_MY_LEADER {SIDE}}
        [then]
            {MODIFY_SIDE_AI ({SIDE}) ({GOAL_AVOID_SIDE_UNLESS_DEFENDING_LEADER 9 {SIDE} {ENEMY} 3})} # avoid staying too close
            {MODIFY_SIDE_AI ({SIDE}) ({GOAL_SEEK_SIDE                          8        {ENEMY} 9})} # but seek staying close enough

            {MODIFY_SIDE_AI ({SIDE}) (aggression=-9)}
            {MODIFY_SIDE_AI ({SIDE}) (caution=9)}
            {MODIFY_SIDE_AI ({SIDE}) (grouping=defensive)}
        [/then]
    [/if]

    [store_time_of_day]
    [/store_time_of_day]
    [if]
        [variable]
            name=time_of_day.id
            equals={TIME2}
        [/variable]

        {AND_NO_ENEMIES_NEAR_MY_LEADER {SIDE}}
        [then]
            {MODIFY_SIDE_AI ({SIDE}) ({GOAL_AVOID_SIDE_UNLESS_DEFENDING_LEADER 9 {SIDE} {ENEMY} 1})} # avoid staying too close
            {MODIFY_SIDE_AI ({SIDE}) ({GOAL_SEEK_SIDE                          8        {ENEMY} 7})} # but seek staying close enough

            {MODIFY_SIDE_AI ({SIDE}) (aggression=-9)}
            {MODIFY_SIDE_AI ({SIDE}) (caution=9)}
            {MODIFY_SIDE_AI ({SIDE}) (grouping=defensive)}
        [/then]
    [/if]

#enddef

#define RETREAT_WHEN_WEAK SIDE COUNT GOALS
    [if]
        [have_unit]
            side={SIDE}
            count={COUNT}
        [/have_unit]

        {AND_NO_ENEMIES_NEAR_MY_LEADER {SIDE}}

        [lua]
            code=<< return wesnoth.current.turn>1 >>
        [/lua]
        # don't open up with retreating; allow at least 1 round of recruitment

        [then]
            {MODIFY_SIDE_AI ({SIDE}) (grouping=defensive)}
            {MODIFY_SIDE_AI ({SIDE}) (aggression=-2)}
            {MODIFY_SIDE_AI ({SIDE}) (caution=2)}
            {MODIFY_SIDE_AI ({SIDE}) ({GOALS})}
        [/then]
    [/if]
#enddef

#--------------------
# SPECIFIC BEHAVIORS
#--------------------
# use inside an [event] tag
#define ZONE_GUARDIAN SIDE GUARD_ID X Y GUARD_X GUARD_Y RADIUS
    [micro_ai]
        side={SIDE}
        ai_type=zone_guardian
        action=add
        [filter]
            id={GUARD_ID}
        [/filter]
        station_x,station_y={X},{Y}
        [filter_location]
            x,y={GUARD_X},{GUARD_Y}
            radius={RADIUS}
        [/filter_location]
    [/micro_ai]
#enddef
# for use inside a [side] tag
#define AI_ZONE_GUARDIAN SIDE GUARD_ID X Y GUARD_X GUARD_Y RADIUS
    [ai]
        [micro_ai]
            side={SIDE}
            ai_type=zone_guardian
            [filter]
                id={GUARD_ID}
            [/filter]
            station_x,station_y={X},{Y}
            [filter_location]
                x,y={GUARD_X},{GUARD_Y}
                radius={RADIUS}
            [/filter_location]
        [/micro_ai]
    [/ai]
#enddef

#define SILENTLY_LIMIT_LEADER_MOVES SIDE MVES
    # don't allow AI leaders to be baited far from their keeps
    # modify per-turn instead of max-moves so that the player can't see it
    {SILENTLY_LIMIT_MOVES {SIDE} canrecruit=yes {MVES}}
#enddef

#define SILENTLY_LIMIT_MOVES SIDE FLT MVES
    [event]
        name=side {SIDE} turn refresh
        first_time_only=no
        [modify_unit]
            [filter]
                side={SIDE}
                {FLT}
            [/filter]
            moves={MVES}
        [/modify_unit]
    [/event]
#enddef

#define LIMIT_RECRUITS_PER_TURN SIDES AMOUNT
    [event]
        name=side turn
        first_time_only=no
        [filter_condition]
            [have_unit]
                side=$side_number

                [and]
                    side={SIDES}
                [/and]
            [/have_unit]
        [/filter_condition]

        {VARIABLE recruits_this_turn 0}

        [event]
            name=turn end

            {CLEAR_VARIABLE recruits_this_turn}
        [/event]
    [/event]

    [event]
        name=recruit
        first_time_only=no
        [filter]
            side={SIDES}
        [/filter]

        {VARIABLE_OP recruits_this_turn add 1}

        [if]
            {VARIABLE_CONDITIONAL recruits_this_turn equals {AMOUNT}}
            [then]
                [store_side]
                    side=$side_number
                [/store_side]

                [disallow_recruit]
                    side=$side_number
                [/disallow_recruit]
                [event]
                    name=turn end

                    [allow_recruit]
                        side=$side.side
                        type=$side.recruit
                    [/allow_recruit]

                    {CLEAR_VARIABLE side}
                [/event]
            [/then]
        [/if]
    [/event]
#enddef
