wfl 'patrol.fai' def closest_unit(ai*, me) choose( enemy_units, 'unit', -distance_between(me.loc, unit.loc) ); def step_move(me) if( unit_at( desired_path[0] ), if( desired_path.size > 1, move_partial( me.loc, desired_path[1] ), end ), move_partial( me.loc, desired_path[0] ) ) where desired_path = shortest_path( me.loc, me.vars.next_step ); def move_ahead(ai*, me) if( enemy_units, if( distance_between( closest_unit(ai, me).loc, me.loc ) > me.moves-1, move_partial( me.loc, me.vars.next_step ), step_move(me) ), move_partial( me.loc, me.vars.next_step ) ); def patrol_move(ai*, me) if( me.vars.next_step = me.loc, set_unit_var('next_step', me.vars.waypoints[ me.vars.next_step ], me.loc ), move_ahead(ai,me) ); if( me.moves = 0, end, if(attack, attack, patrol_move(self, me) ) ) where attack = if( path_to, if(path_to.size <= me.vars.guard_radius, attack( me.loc, path_to.last, closest_unit(self, me).loc ), 0 ), 0 ) where path_to = if( enemy_units, shortest_path( me.loc, closest_unit(self, me).loc ), [] ) wflend