def defend(self, unit, buildinginfo): if not self.capture(unit): if not self.attack(unit): if not unit.is_moving: try: unit.move( buildinginfo.perimeter_cycler.next() ) except StopIteration: self.wander(unit)
def capture_building(self, b): pos = b.position self.destination = pos for p in self.positions: unit = getattr(self, p) if unit: if unit.position == pos: unit.capture(b) return else: unit.move(pos)
def capture_target(self, unit, building): if unit.is_capturing: return True for friend in self.my_units: if friend != unit: if friend.is_capturing and friend.position == building.position: return False if unit.position == building.position: unit.capture(building) else: unit.move(building.position) return True
def attack_nearby_enemies(self): all_attack = None for p in self.positions: unit = getattr(self, p) if unit: ire = unit.in_range_enemies if ire: all_attack = ire[0] break if all_attack: for p in self.positions: unit = getattr(self, p) if not unit: continue if all_attack in unit.in_range_enemies: unit.shoot(all_attack.position) else: unit.move(all_attack.position)
def capture(self, unit): if unit.is_capturing: return True for building in unit.visible_buildings: if building.team == self.team: continue friend_capturing = False for friend in self.my_units: if friend != unit: if friend.is_capturing and friend.position == building.position: friend_capturing = True if friend_capturing: continue if unit.position == building.position: unit.capture(building) else: unit.move(building.position) return True
def units_in_place(self): for i in xrange(len(self.positions)): p = self.positions[i] unit = getattr(self, p) if unit and not unit.is_capturing: pos = self.destination or self.base off = self.position_offsets[i] d_x, d_y = (pos[0] + off[0], pos[1]+off[1]) d_x = max(min(self.mapsize, d_x), 0) d_y = max(min(self.mapsize, d_y), 0) dest = (d_x, d_y) if dest != unit.position: try: unit.move(dest) except ai_exceptions.IllegalSquareException: unit.move(self.destination) except ai_exceptions.DeadUnitException: print "Tried using a dead unit" raise self.remove_unit(unit)
def wander(self, unit): if not unit.is_moving: unit.move(self.position_on_circle(self.wander_radius, unit.position))
def _spin(self): # run setup if needed if not self.setup_complete: if len(self.drones) > 0: self.map.setup( self.mapsize, self.drones[0].sight ) self.setup_complete = True # print out highlights & debug info if needed self.highlight() # create lists enemies = list(self.visible_enemies) targets = [] # list of buildings we don't own that we know about bases = [] # list of buildings we own # update enemies self.enemies_attacked = {} for enemy in enemies: self.enemies_attacked[enemy] = 0 # update our position tracking if not enemy in self.enemy_predictor: self.enemy_predictor[enemy] = bullseye.Predictor(enemy, self.mapsize) else: self.enemy_predictor[enemy].set_position(enemy.position) # update our map self.map.update(self.my_units) # Check for perimeter distance increase if self.current_turn % 250 == 0: self.perimeter_distance += 1 # Add new buildings we discover for building in self.visible_buildings: if not building in self.buildings: self.buildings[building] = buildinginfo.BuildingInfo(self, building) self.buildings[building].establish_perimeter(self.perimeter_distance) self.map.building(building) # Loop through all known buildings: # value = BuildingInfo instance for the key = building for key, value in self.buildings.iteritems(): if len(value.perimeter) == 0: value.establish_perimeter(self.perimeter_distance) # update perimeter if the distance has changed elif self.perimeter_distance != value.perimeter_distance: value.establish_perimeter(self.perimeter_distance) # our buildings require specific actions if key.team == self.team: # if the building has no defender, request one (preferably closest available unit) if value.defender == None: drone_assigned = closest_thing( value.building.position, self.drones ) # assign drone to defend & remove from drone pool if drone_assigned != None: value.defender = drone_assigned self.drones.remove(drone_assigned) # if we have a defender on this building, make sure its alive else: if value.defender.is_alive: self.defend(value.defender, value) else: value.defender = None continue # else building not on our team else: targets.append(key) # if there is still a defender, have them attempt a recapture! if value.defender != None: if value.defender.is_alive: self.defend(value.defender, value) else: value.defender = None # Loop through our drones for unit in self.drones: # Attempt to attack any enemies in range if not self.attack(unit): # Attempt to capture any building in range if not self.capture(unit): # Either: Explore map or assist other drones if self.current_turn <= self.search_until: self.explore(unit) else: # this area needs a lot of work, target selection is the weakest link right now goto = closest_thing( unit.position, list(targets + enemies) ) if goto == None: self.explore(unit) else: if goto in targets: self.capture_target(unit, goto) else: unit.move( self.position_on_circle( unit.sight - 1, goto.position ) )
def explore(self, unit): point = self.map.nearest(unit.position) if point == None: self.wander(unit) else: unit.move(point)