def nearest(self, position): if len(self.points) == 0: return None # calc distances to our position distances = {} for p in self.points: distances[p] = calc_distance( p, position ) # if the point we just checked is within the units sight range, return this point immediately # this makes for a speedier algorithm also less exact #if distances[p] < self.sight: # return p sorted_d = sorted( distances.items(), key = lambda x: x[1] ) building_found = closest_thing( position, self.distances.keys() ) if building_found == None: return sorted_d[0][0] sorted_d = list(sorted_d[0:5]) # take first four sorted points for i in range( 0, len(sorted_d) ): sorted_d[i] = list(sorted_d[i]) sorted_d[i][1] = calc_distance( sorted_d[i][0], building_found.position ) + sorted_d[i][1] sorted_d = sorted( distances.items(), key = lambda x: x[1] ) return sorted_d[0][0]
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 building we don't own that we know about bases = [] # list of building we own # 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 not value.defender.is_alive: value.defender = None continue # Defender patrols the base perimeter defender = value.defender if defender != None: if not self.capture(defender): if not self.attack(defender): if not defender.is_moving: try: defender.move( value.perimeter_cycler.next() ) except StopIteration: self.wander(defender) # else building not on our team else: targets.append(key) # 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 target = closest_thing( unit.position, list(merge(targets, enemies)) ) if target == None: self.explore(unit) else: unit.move( self.position_on_circle( unit.sight - 1, target.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 ) )