def __init__(self, bzrc): self.bzrc = bzrc self.constants = self.bzrc.get_constants() self.commands = [] mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.numoftanks = len(mytanks) self.timeuntilshot = [0]*self.numoftanks self.ismoving = [True]*self.numoftanks # For PD Control self.old_angle = [0]*self.numoftanks self.old_speed = [0]*self.numoftanks # For visibility graph self.final_goals = [0]*self.numoftanks self.graph = VisibilityGraph(self.bzrc) self.fields = [] self.goal_paths = [] for i in xrange(self.numoftanks): self.fields.append(PotentialFields(self.bzrc)) self.goal_paths.append(Path()) obstacles = self.bzrc.get_obstacles() for flag in flags: if flag.color == self.constants['team']: self.base = {'x':flag.x, 'y':flag.y}
class Agent(object): """Class handles all command and control logic for a teams tanks.""" def __init__(self, bzrc): self.bzrc = bzrc self.constants = self.bzrc.get_constants() self.commands = [] mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.numoftanks = len(mytanks) self.timeuntilshot = [0]*self.numoftanks self.ismoving = [True]*self.numoftanks # For PD Control self.old_angle = [0]*self.numoftanks self.old_speed = [0]*self.numoftanks # For visibility graph self.final_goals = [0]*self.numoftanks self.graph = VisibilityGraph(self.bzrc) self.fields = [] self.goal_paths = [] for i in xrange(self.numoftanks): self.fields.append(PotentialFields(self.bzrc)) self.goal_paths.append(Path()) obstacles = self.bzrc.get_obstacles() for flag in flags: if flag.color == self.constants['team']: self.base = {'x':flag.x, 'y':flag.y} def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots self.enemies = [tank for tank in othertanks if tank.color != self.constants['team']] self.commands = [] for tank in mytanks: self.create_command(tank, time_diff) #self.attack_enemies(tank) results = self.bzrc.do_commands(self.commands) def attack_enemies(self, tank): """Find the closest enemy and chase it, shooting as you go.""" best_enemy = None best_dist = 2 * float(self.constants['worldsize']) for enemy in self.enemies: if enemy.status != 'alive': continue dist = math.sqrt((enemy.x - tank.x)**2 + (enemy.y - tank.y)**2) if dist < best_dist: best_dist = dist best_enemy = enemy if best_enemy is None: command = Command(tank.index, 0, 0, False) self.commands.append(command) else: self.move_to_position(tank, best_enemy.x, best_enemy.y) def move_to_position(self, tank, target_x, target_y): """Set command to move to given coordinates.""" target_angle = math.atan2(target_y - tank.y, target_x - tank.x) relative_angle = self.normalize_angle(target_angle - tank.angle) command = Command(tank.index, 1, 2 * relative_angle, True) self.commands.append(command) def create_command(self, tank, time_diff): """Set command to move to given coordinates.""" speed = 1 angvel = 0 shoot = True kp = 1 kd = -.07 goal_threshold = 15.0 if time_diff <= 0: return # assign goal to tank if tank.flag == '-': closest_goal = self.get_closest_goal(tank) if closest_goal is None: self.attack_enemies(tank) return goal_x, goal_y = closest_goal else: goal_x, goal_y = self.base['x'], self.base['y'] # see if we need a new visibility graph and path new_goal = (goal_x, goal_y) if self.final_goals[tank.index] != new_goal: self.final_goals[tank.index] = new_goal start = (tank.x, tank.y) end = self.final_goals[tank.index] self.graph.createTankGraph(start, end) #print 1 search_alg = search.AStar(self.graph) #print 2 new_path = search_alg.run(start, end) #print 3 self.graph.displayGraph() self.goal_paths[tank.index].set_path(new_path) #print 4 # get potential field at this location for the calculated goal goal = self.goal_paths[tank.index].get_next(tank.x, tank.y, goal_threshold) field = self.fields[tank.index] field.set_goal(goal[0], goal[1]) x, y = field.calculate_potential(tank.x, tank.y) # PD Controller - angle target_angle = math.atan2(y, x) angle_remaining = self.angle_remaining(tank.angle, target_angle) angvel = self.pd_angvel(tank, target_angle, time_diff) if angvel > 1: angvel = 1 elif angvel < -1: angvel = -1 # PD Controller - speed if abs(angle_remaining) <= math.pi / 6: speed = self.pd_speed(tank, x, y, time_diff) else: speed = 1 - abs(angle_remaining / math.pi) if speed > 1: speed = 1.0 elif speed < -1: speed = -1.0 command = Command(tank.index, speed, angvel, shoot) self.commands.append(command) self.old_angle[tank.index] = tank.angle def pd_angvel(self, tank, target_angle, time_diff): """PD Controller for the angular velocity of the tank.""" kp = 1.0 kd = -0.2 angle_remaining = self.angle_remaining(tank.angle, target_angle) differential = self.angle_remaining(self.old_angle[tank.index], tank.angle) / time_diff angvel = ( kp * angle_remaining ) + ( kd * differential ) return angvel def pd_speed(self, tank, target_x, target_y, time_diff): """PD Controller for the speed of the tank.""" kp = 1.0 kd = -0.2 x_remaining = target_x - tank.x y_remaining = target_y - tank.y distance_remaining = math.sqrt( ( x_remaining ) ** 2 + ( y_remaining ) ** 2 ) current_speed = math.sqrt( tank.vx ** 2 + tank.vy ** 2 ) differential = current_speed - self.old_speed[tank.index] / time_diff speed = ( kp * distance_remaining ) + ( kd * differential ) return speed def angle_remaining(self, tank_angle, target_angle): """Find the angle remaining (in radians) between the tank and the target.""" tank_angle = self.normalize_angle(tank_angle) target_angle = self.normalize_angle(target_angle) # If the angles are on the same hemisphere, target - tank. if tank_angle * target_angle > 0: return target_angle - tank_angle # Otherwise they are on opposite hemispheres. positive_angle = max(tank_angle, target_angle) negative_angle = min(tank_angle, target_angle) tank_positive = True if tank_angle < 0: tank_positive = False # Compare the angles to turn right and left. right_angle = -1 * ( positive_angle - negative_angle ) left_angle = 2 * math.pi - positive_angle + negative_angle if not tank_positive: temp_angle = -1 * right_angle right_angle = -1 * left_angle left_angle = temp_angle # Pick the smallest angle. if abs(right_angle) <= abs(left_angle): return right_angle return left_angle def normalize_angle(self, angle): """Make any angle be between +/- pi.""" angle -= 2 * math.pi * int (angle / (2 * math.pi)) if angle <= -math.pi: angle += 2 * math.pi elif angle > math.pi: angle -= 2 * math.pi return angle def cmp_flags(self, a, b): a_dist = math.sqrt(a.x ** 2 + a.y ** 2) b_dist = math.sqrt(b.x ** 2 + b.y ** 2) if a_dist < b_dist: return -1 elif a_dist == b_dist: return 0 return 1 def get_closest_goal(self, tank): flag_list = copy.deepcopy(self.flags) for flag in flag_list: flag.x -= tank.x flag.y -= tank.y flag_list.sort(self.cmp_flags) for flag in flag_list: flag.x += tank.x flag.y += tank.y if flag.color != self.constants['team'] and flag.poss_color != self.constants['team']: return (flag.x, flag.y) elif flag.color == self.constants['team'] and (flag.x != self.base['x'] or flag.y != self.base['y']): return (flag.x, flag.y)