wfl 'lurker_moves.fai'
# run_file('ai/micro_ais/engines/lurker_moves.fai') #

def is_swamp(map, xx, yy)
    # Tests whether terrain at xx,yy is swamp_water #
    map(
        filter( map.terrain, (x=xx) and (y=yy)),
        self.id
    )[0] = 'swamp_water_reed';

def reachable_swamp(unit_loc,map)
    # get all reachable swamp locs for unit at unit_loc #
    # exclude own location #
    filter(
        unit_moves( unit_loc ),
        (is_swamp( map, x, y )) and (self != unit_loc)
    );

def random(array)
    # Picks a random elements of 'array' #
    array[(1d size(array) - 1)];

def reachable_enemies_next_to_swamp(unit_loc,attacks,map)
    # Returns all the attacks for enemies that the unit at 'unit_loc' #
    # can reach and that are next to a swamp hex #
    filter(
        map( attacks.attacks, self),
        (move_from = unit_loc) and (is_swamp( map, attack_from.x, attack_from.y))
    );

def weakest_defender(attacks)
    # Get the enemy with the lowest HP #
    choose( attacks, -unit_at(defender).hitpoints
    );

# Attack if possible, otherwise random move #
if( size(possible_attacks) != 0,
    weakest_defender(possible_attacks),
    if( size(swamp_in_reach) != 0,
        move(me.loc,random(swamp_in_reach)),
        end)
)

where possible_attacks = reachable_enemies_next_to_swamp( me.loc, my_attacks,map)

where swamp_in_reach = reachable_swamp(me.loc,map)

wflend
