def find_closest_foe(self): """"Finds enemy robot closest to location of given robot and returns its location. Breaks ties by lowest HP. Assumes there are enemies remaining""" closest_robots = [] if self.total_foes: closest_robot = self.total_foes[0] # take any robot to start out for rob in self.total_foes: current_dist = rg.wdist(self.robot.location, rob.location) if current_dist < rg.wdist(self.robot.location, closest_robot.location): closest_robot = rob # make list of closest robots (may include more than one) for rob in self.total_foes: current_dist = rg.wdist(self.robot.location, rob.location) if current_dist == rg.wdist(self.robot.location, closest_robot.location): closest_robots.append(rob) # break ties with lowest HP if more than one closest robot if len(closest_robots) > 1: lowest_HP_rob = closest_robots[0] for rob in closest_robots: if rob.hp < lowest_HP_rob.hp: lowest_HP_rob = rob return lowest_HP_rob.location else: # only one robot, pick him return closest_robots[0].location
def closest_bro(self,game): closest_dist = 1000 for loc, bot in game['robots'].iteritems(): if self.location != loc and bot.player_id == self.player_id and rg.wdist(self.location, loc) < closest_dist: closest = bot closest_dist = rg.wdist(self.location,loc) return closest, closest_dist
def GetClosestEnemy(self): for loc, bot in self.game.get('robots').items(): if bot.player_id != self.player_id: if rg.wdist(loc, self.location) <= rg.wdist(self.location, self.closestEnemy): self.closestEnemy = loc #print "Enemy = %d, %d" %self.closestEnemy return self.closestEnemy
def act(self, game): """Called in every turn, for every instance of Robot :param game: a dictionary of all robots on the field mapped {location: robot} :return: any of the following: ['move', (x, y)] ['attack', (x, y)] ['guard'] ['suicide'] """ # check if any robot is right next to us for (x, y) in rg.locs_around(self.location): if (x, y) in game['robots']: # if it is an enemy attack it, otherwise just guard if game['robots'][(x, y)].player_id != self.player_id: return ['attack', (x, y)] else: return ['guard'] # move towards the closest enemy robot min_distance = 200 # for loc, robot in list(game['robots'].items()): # --use this with python3.x for loc, robot in game['robots'].iteritems(): # --use this with python2.x if robot.player_id != self.player_id and rg.wdist(self.location, loc) < min_distance: min_distance, target_loc = rg.wdist(self.location, loc), loc # check if we can move towards the nearest robot # or someone else will move to that spot next_move_loc = rg.toward(self.location, target_loc) if self.is_loc_available(game, next_move_loc): return ['move', next_move_loc] return ['guard']
def GetClosestFriendly(self): for loc, bot in self.game.get('robots').items(): if bot.player_id == self.player_id: if rg.wdist(loc, self.location) <= rg.wdist(self.location, self.closestFriend): self.closestFriend = loc #print "Friend = %d" %self.closestFriend return self.closestFriend
def safe(): around = {} for loc in rg.locs_around(curr, filter_out=('invalid','obstacle','spawn')): if loc not in self.movements: if loc not in game_bots: danger = (len(nearby(True)) * 5) + len(nearby(True, 2)) + rg.wdist(curr, nearest_spawn()) + rg.wdist(curr, rg.CENTER_POINT) if loc in rg.settings.spawn_coords: danger += 100 * (10 - til_spawn) if rg.wdist(loc, nearest_spawn()) == 1: if nearest_spawn() in game_bots: if game_bots[nearest_spawn()].player_id == self.player_id: danger += 1000 else: danger -= 100 if moveable(loc) == False: danger += 2500 around[loc] = danger output = sorted(around, key=lambda a : around[a]) if len(output) == 0: return None duplicated = {} supposed_best = output[0] duplicated[supposed_best] = len(nearby_of_location(supposed_best, False)) for out in around.keys(): if supposed_best != out: if around[supposed_best] == around[out]: duplicated[out] = len(nearby_of_location(out, False)) return sorted(duplicated, key=lambda p : duplicated[p])[0] # Move to a less crowded area
def act(self, game): num_enemies = self.num_enemies(game) if num_enemies * 9 > self.hp: return ['suicide'] min_distance = float("inf") move_to = self.get_center(game) for location, bot in game.get('robots').items(): if bot.get('player_id') != self.player_id: if rg.dist(location, self.location) <= 1: return ['attack', location] if bot.get('player_id') == self.player_id: if rg.wdist(location, self.location) < min_distance: min_distance = rg.wdist(location, self.location) move_to = location if min_distance < 2: move_to = self.get_center(game) if self.location == self.get_center(game): return ['guard'] if self.num_frieds(game) > 1: return ['guard'] return ['move', rg.toward(self.location, move_to)]
def calculate_proposals_for_loc(self,src): panic=False;aggressive=True if self.robots[src]['hp']>=30 else False;proposals=ProposedMoveCollection();safer_neighbours=self.find_safer_neighbours(src);nearby_enemies=self.find_neighbours(src=src,player_id=self.enemy_id);max_damage_to_me=10*len(nearby_enemies);here_is_suicide=is_spawn(src)and self.is_spawn_imminent();i_will_be_killed=self.robots[src]['hp']<=max_damage_to_me if nearby_enemies and not safer_neighbours and(i_will_be_killed or here_is_suicide):return proposals.add_move(SCORE['suicide'],'suicide',src) if is_spawn(src)and self.is_spawn_imminent(within=1):panic=True if len(nearby_enemies)>=2:panic=True overwhelmed_enemies=[(x,self.count_neighbours(src=x,player_id=self.enemy_id),self.robots[x]['hp'])for x in nearby_enemies if self.count_neighbours(src=x,player_id=self.player_id)>1] for e in overwhelmed_enemies:score=SCORE['attack_overwhelmed_enemy']+e[1];proposals.add_move(score,'attack',src,e[0]) for e in nearby_enemies:score=SCORE['attack_normal_enemy']+50-self.robots[e]['hp'];proposals.add_move(score,'attack',src,e) possibles=self.ring_search(src,inclusive=True);possibles=self.filter_locs(possibles,filter_id=self.enemy_id);src_ally_neighbours=self.count_neighbours(src=src,player_id=self.player_id);src_enemy_neighbours=self.count_neighbours(src=src,player_id=self.enemy_id) for dst in possibles: if dst in self.enemy_next_moves:score=SCORE['preemptive_strike']+self.enemy_next_moves[dst];proposals.add_move(score,'attack',src,dst) base_move_score=0 if dst in self.enemy_next_moves:base_move_score-=20*self.enemy_next_moves[dst] if aggressive and src in self.ally_assignments: src_target_distance=rg.wdist(src,self.ally_assignments[src]);dst_target_distance=rg.wdist(dst,self.ally_assignments[src]) if dst in self.best_attack_spots:base_move_score+=SCORE['move_to_best_attack_spot'] base_move_score+=100*(src_target_distance-dst_target_distance) dst_enemy_neighbours=self.count_neighbours(src=dst,player_id=self.enemy_id) if src==dst:base_move_score+=10 if is_spawn(src)and self.is_spawn_imminent(within=1):base_move_score+=SCORE['move_to_safer_location'] if is_spawn(dst):base_move_score-=10 if is_spawn(dst)and self.is_spawn_imminent():base_move_score-=SCORE['suicide'] if panic and src!=dst:base_move_score+=SCORE['panic'] if dst_enemy_neighbours<src_enemy_neighbours:base_move_score+=SCORE['move_to_safer_location']+dst_enemy_neighbours-src_enemy_neighbours action='guard'if dst==src else'move';proposals.add_move(base_move_score,action,src,dst) return proposals
def nearestEnemy(self, game): currentTarget = rg.CENTER_POINT for loc, bot in game.get('robots').items(): if bot.player_id != self.player_id: if (rg.wdist(self.location, loc) <= rg.wdist(self.location, currentTarget)): currentTarget = loc return currentTarget
def act(self, game): num_enemies = self.num_enemies(game) if num_enemies * 9 > self.hp: return ['suicide'] min_distance = float("inf") move_to = rg.CENTER_POINT for location, bot in game.get('robots').items(): if bot.get('player_id') != self.player_id: if rg.dist(location, self.location) <= 1: return ['attack', location] if bot.get('player_id') == self.player_id: if rg.wdist(location, self.location) < min_distance: min_distance = rg.wdist(location, self.location) move_to = location if min_distance < 2: move_to = rg.CENTER_POINT if self.location == rg.CENTER_POINT: return ['guard'] if self.num_frieds(game) > 1: return ['guard'] return ['move', rg.toward(self.location, move_to)]
def act(self, game): print print "Turn: " +str( game.turn) + "\tBot: " + str(self.robot_id) # find closest friend & enemy closestFriend = (1000, 1000) closestEnemy = (1000, 1000) for loc, bot in game.get('robots').items(): if bot.player_id != self.player_id: if rg.wdist(loc, self.location) <= rg.wdist(closestEnemy, self.location): closestEnemy = loc else: if rg.wdist(loc, self.location) <= rg.wdist(closestFriend, self.location) and self.robot_id != bot.robot_id: closestFriend = loc for loc, bot in game['robots'].iteritems(): # if there are enemies around, attack them if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1: return ['attack', loc] else: # if there are friends around, move to them if rg.dist(loc, self.location) > 2: return ['move', rg.toward(self.location, closestFriend)] # move towards enemy return ['move', rg.toward(self.location, closestEnemy)]
def check_friend(current_close_friend, new_friend): if not current_close_friend: return new_friend if rg.wdist(self.location, new_friend.location) < rg.wdist( self.location, current_close_friend.location): return new_friend return current_close_friend
def act(self, game): center = rg.CENTER_POINT enemy_bots = [ b for b in game.robots.values() if b.player_id != self.player_id ] target = min(enemy_bots, key=lambda bot: rg.wdist(bot.location, center)) adjacent_bots = [] for enemy in enemy_bots: if rg.wdist(enemy.location, self.location) == 1: adjacent_bots.append(enemy) if len(adjacent_bots) > self.hp / 9: return ['suicide'] elif adjacent_bots: return ['attack', min(adjacent_bots, key=lambda b: b.hp).location] adj = rg.locs_around(target.location, filter_out=('invalid', 'obstacle')) closest_locs = min(rg.wdist(loc, self.location) for loc in adj) dests = [ loc for loc in adj if rg.wdist(loc, self.location) == closest_locs ] dest = random.choice(dests) if rg.wdist(target.location, self.location) == 1: return ['attack', target.location] else: return ['move', rg.toward(self.location, dest)]
def closest_enemy(player_id, location, game): closest_dist = 1000 for loc, bot in game['robots'].iteritems(): if bot.player_id != player_id and rg.wdist(location, loc) < closest_dist: closest = bot closest_dist = rg.wdist(location,loc) return closest
def act(self, game): if self.robot_id in self.mayday: del self.mayday[self.robot_id] if self.hp < 10: return self.kill_self(game) enemies = [] friends = [] for robot in game.robots.itervalues(): if hasattr(robot, "robot_id"): friends.append(robot) else: enemies.append(robot) targets = [] for enemy in enemies: targets.append((enemy, rg.wdist(self.location, enemy.location))) target, distance = min(targets, key=lambda (a, b): b) if distance == 1: self.mayday[self.robot_id] = target.location return ["attack", target.location] if self.mayday: targets = [(t, rg.wdist(self.location, t)) for t in self.mayday.values()] target, _ = min(targets, key=lambda (a, b): b) if rg.wdist(self.location, target) == 1: return ["attack", target] return ["move", rg.toward(self.location, target)] return ["move", rg.toward(self.location, target.location)]
def TurtleMode(self,game): #for loc,bot in game.robots.items(): # if bot.player_id == self.player_id: if self.hp <= 15: if rg.wdist(self.location, GetClosestEnemy(self)) == 1: if rg.wdist(self.location, GetClosestFriendly(self)) > 1: print "Bot at %d %d entered turtle mode" %self.location return True
def hunt(self, robot): if rg.wdist(self.me.location, robot.location) <= 1: return Move(['attack', robot.location], "Hunter %s" % str(robot.location)) for enemyRobotLocation in _gameView.locationsOfEnemyRobots: if rg.wdist(self.me.location, enemyRobotLocation) <= 1: return Move(['attack', enemyRobotLocation], "Hunter [found other enemy] %s" % str(enemyRobotLocation)) walker = Walker(self.me) return Move(walker.goTowards(robot.location), "Hunter %s [needed to walk]" % str(robot.location))
def process_attack_state(self, game): # Try parry from predicted explosion dangers = {x.location: x for x in self.neighbours_exploding()} if len(dangers) > 0: res = self.try_parry(game, spawn_points_ok=True) return res if res else ['guard'] # Try escape from spawn reset res = self.escape_spawn_trap_move(game) if res: return res # Combat-mode targets = self.intel.enemies() targets = [x for x in targets if rg.wdist(self.location, x.location) == 1] targets.sort(lambda x, y: cmp(x.hp, y.hp)) # 1 block away targets (in combat) if len(targets) > 0: # try parry if may die or getting double-team'd and can't kill # opponent if ((len(targets) == 1 and self.hp <= 10 and targets[0].hp > 9) or (len(targets) > 1 and any(x for x in targets if x.hp > 15))): res = self.try_parry(game, spawn_points_ok=False) if res: return res # explode or attack decision if self.should_explode_over_attack(): return ['suicide'] else: return ['attack', targets[0].location] # TODO: team up attacks # 2 block away targets targets = self.intel.enemies() targets = [x for x in targets if rg.wdist(self.location, x.location) == 2] targets.sort(lambda x, y: cmp(x.hp, y.hp)) for x in targets: # Can hit predicted loc 1? (towards center) predicted_loc = rg.toward(x.location, rg.CENTER_POINT) if rg.wdist(self.location, predicted_loc) == 1: return ['attack', predicted_loc] # Try predicted loc 2 (towards robot) predicted_loc = self.towards(x.location)[0] return ['attack', predicted_loc] # for later, if want to coordinate attacks from afar # targets.sort(lambda x, y: cmp(rg.wdist(self.location,x.location), # rg.wdist(self.location,y.location))) return ['guard']
def closest_robot(location, robot_set, exclude_location): closest = None closestDistance = 300 for bot in robot_set: if rg.wdist(location, bot.location) < closestDistance and ((not exclude_location) or (bot.location != location)): closest = bot closestDistance = rg.wdist(location, bot.location) return closest
def runAwayFrom(self, enemy): valid_locs = rg.locs_around(self.location, filter_out=('invalid', 'obstacle', 'spawn')) farthest = self.location farthest_dist = rg.wdist(farthest, enemy.location) for loc in valid_locs: dist = rg.wdist(loc, enemy.location) if dist > farthest_dist: farthest = loc farthest_dist = dist return farthest
def findNearestEnemy(self, game): enemies = self.getEnemies(game) closest = enemies[0] closestDist = rg.wdist(closest.location, self.location) for enemy in enemies[1:]: dist = rg.wdist(enemy.location, self.location) if dist < closestDist: closest = enemy closestDist = dist return closest
def nearbyMembers(self, location, radius = 1, borderonly=False): robots = [] for bot in self.bots: if bot.location is not location: if borderonly: if rg.wdist(bot.location, location) == radius: robots.append(bot) else: if rg.wdist(bot.location, location) <= radius: robots.append(bot) return robots
def nearbyEnemies(self, location, radius=1, borderonly=False): robots = [] for loc, bot in self.all_bots.iteritems(): if bot.player_id != self.playerid(): if borderonly: if rg.wdist(loc, location) == radius: robots.append(bot) else: if rg.wdist(loc, location) <= radius: robots.append(bot) return robots
def nearbyAllies(self, location, radius=1, borderonly=False): robots = [] for loc, bot in self.all_bots.iteritems(): if bot.player_id is self.player_id: if bot.location is not location: if borderonly: if rg.wdist(loc, location) == radius: robots.append(loc) else: if rg.wdist(loc, location) <= radius: robots.append(loc) return robots
def _flee(self, robot): ''' Bravely run away! If there is a nice chunk of empty space to move to close-by, run there. If not, run to the protection of a nearby friendly ''' # Find the closest point that's the farthest from enemies # distance_cost is how much we subtract from a tile's enemy farness to factor in that we'd rather be lazy # and not move if we don't have to. distance_cost = FLEE_DISTANCE_COST tile_scores = dict() for loc in self.play_area_locations: distance_to_tile = rg.wdist(robot.location, loc) closest_enemy = self._closest_robot(self.enemy_id, loc) distance_to_closest_enemy = 0 if closest_enemy == None: # No enemies left - move to center since that'll be far from new enemy spawns return ['move', rg.toward(robot.location, rg.CENTER_POINT)] else: distance_to_closest_enemy = rg.wdist(loc, closest_enemy.location) tile_score = int(distance_to_closest_enemy - (distance_to_tile * distance_cost)) tile_scores[loc] = tile_score # How many of the top locations we will try pathing to incase the best option is unreachable flee_attempts = 5 for flee_location in sorted(tile_scores, key=lambda a: tile_scores[a], reverse=True): if (robot.location == flee_location): # Don't bother pathing since our current location is the best return ['guard'] path = self._cooperative_shortest_path(self.flee_graph, robot.location, flee_location) if path == None: flee_attempts -= 1 if flee_attempts <= 0: break; else: continue else: self._commit_path_to_future_locations(path) return ['move', path[1]] closestEnemy = self._closest_robot(self.enemy_id, robot.location) if closestEnemy != None: if rg.wdist(robot.location, closestEnemy.location) <= 1: return ['attack', closestEnemy.location] # We can't path to safety but we're not next to an enemy so stay where we are return ['guard']
def propose(self, loc, action): if action[0]=="move": if self.is_empty(action[1]) and rg.wdist(loc,action[1])==1: return 1 else: return -1 #invalid if action[0]=="attack": if self.is_enemy(action[1]) and rg.wdist(loc,action[1])==1: return 1 else: return -1 #invalid if action[0]=="guard": return 1 if action[0]=="suicide": return 1
def ring_search(self,src,wdist=1,inclusive=False): result=[] try: for x in range(src[0]-wdist,src[0]+wdist+1): for y in range(src[1]-wdist,src[1]+wdist+1): xy=x,y if'obstacle'in rg.loc_types(xy):continue if'invalid'in rg.loc_types(xy):continue if inclusive: if rg.wdist(src,xy)<=wdist:result.append(xy) if not inclusive: if rg.wdist(src,xy)==wdist:result.append(xy) except TypeError,e:raise Exception('Typeerror %s, src = %s and wdist = %s'%(e,src,wdist)) return set(result)
def distance_from_spawn(square): #canonise the square canonical_x = square[0] canonical_y = square[1] if(canonical_x > 9): canonical_x = 18-canonical_x if(canonical_y > 9): canonical_y = 18-canonical_y if(canonical_x > canonical_y): canonical_square = (canonical_y, canonical_x) else: canonical_square = (canonical_x, canonical_y) distance = 10 for loc in canonical_spawn_locs: if rg.wdist(loc, canonical_square) < distance: distance = rg.wdist(loc,canonical_square) return distance
def distance_from_spawn(square): #canonise the square canonical_x = square[0] canonical_y = square[1] if (canonical_x > 9): canonical_x = 18 - canonical_x if (canonical_y > 9): canonical_y = 18 - canonical_y if (canonical_x > canonical_y): canonical_square = (canonical_y, canonical_x) else: canonical_square = (canonical_x, canonical_y) distance = 10 for loc in canonical_spawn_locs: if rg.wdist(loc, canonical_square) < distance: distance = rg.wdist(loc, canonical_square) return distance
def infront(l1, l2): if rg.wdist(l1, l2) == 2: if diag(l1, l2): return False else: return True return False
def zprzodu(l1, l2): if rg.wdist(l1, l2) == 2: if abs(l1[0] - l2[0]) == 1: return False else: return True return False
def act(self, game): us, them = process_game(game, self.player_id) x = int(sum([x[0] for x in us]) / len(us)) y = int(sum([y[1] for y in us]) / len(us)) if self.location == (x, y): return ['guard'] enemies_around = list() friends_around = list() for each in them: if rg.dist(self.location, each) == 1: enemies_around.append(each) for each in us: if rg.dist(self.location, each) == 1: friends_around.append(each) if len(friends_around) >= 3: return ['guard'] distance_border = min([ rg.wdist(place, self.location) for place in rg.settings.spawn_coords ]) if len(friends_around) >= 2 and distance_border > 3: return ['guard'] if len(enemies_around) >= 2: return ['suicide'] if self.hp < 8 and enemies_around: return ['suicide'] for each in them: if rg.dist(self.location, each) == 1: if test_location(each): return ['attack', each] if test_location((x, y)): return ['move', rg.toward(self.location, (x, y))] return ['guard']
def num_enemies(self, game): enemies = 0 for location, bot in game.get('robots').items(): if bot.get('player_id') != self.player_id: if 1 == rg.wdist(self.location, location): enemies += 1 return enemies
def score_aggressiveness(loc1): self._aggressiveness = 0 for loc2 in enemies: if rg.wdist(loc1, loc2) <= 2: self._aggressiveness = money_time return self._aggressiveness return self._aggressiveness
def get_enemies_that_could_move_next_to(self, loc, game): enemies = [] for bot in game.get('robots').values(): if bot.player_id != self.player_id: if rg.wdist(bot.location, loc) == 2: enemies.append(bot) return enemies
def act(self, game): self.game = game max_angry = 10 angry = 8 is_angry = random.choice([True]*angry+[False]*(max_angry-angry)) local_enemies = self.local_enemies(game) empty_spaces = [e for e in self.local_spaces() if e not in local_enemies.keys()] if local_enemies: if self.hp < 10 and len(local_enemies) > 1: return ['suicide'] if is_angry: return ['attack', random.choice(local_enemies.keys())] if empty_spaces: return ['move', random.choice(empty_spaces)] return['guard'] enemies = self.enemies(game) for enemy in enemies: displacement = (self.location, enemy.location) if rg.wdist(*displacement) == 2: attack_loc = rg.toward(*displacement) if is_angry: return ['move', attack_loc] if self.in_square(attack_loc) != 'friend': return ['attack', attack_loc] return ['move', rg.toward(self.location, random.choice(self.enemies(game)).location)]
def act(self, game): # TODO: # Swarm around the sub-leader bot # attack in group, increase radius of search to 2-3 tiles away #print " robot {} has moves ".format(self.robot_id), self.move_randomly(game) #print " robot {} has {} enemy around".format(self.robot_id, len(self.enemy_around(game))) if self.move_randomly(game): return self.move_randomly(game) else: enemies_1 = self.enemy_around(game, 1) enemies_2 = self.enemy_around(game) if len(enemies_1) > 0: if self.hp <= 15: return ['suicide'] #attack the weakest enemy weakest_enemy = self.find_weakest(enemies_1) return ['attack', weakest_enemy.location] elif len(enemies_2) > 0: #no enemy around, extend to 2nd radius weakest_enemy = self.find_weakest(enemies_2) if rg.wdist(self.location, weakest_enemy.location) == 2: return ['attack', rg.toward(self.location, weakest_enemy.location)] else: return ['move', rg.toward(self.location, weakest_enemy.location)] return ['guard'] #else: #next_move = rg.toward(self.location, rg.CENTER_POINT) #if next_move in game.robots #else: # return ['move', next_move] return ['guard']
def enemys_near(self, query_loc): enemys = [] for loc, bot in self.game.get('robots').items(): if bot.get('player_id') != self.player_id: if rg.wdist(loc, query_loc) <= 1: enemys.append([loc, bot]) return enemys
def num_frieds(self, game): friends = 0 for location, bot in game.get('robots').items(): if bot.get('player_id') == self.player_id: if 1 == rg.wdist(self.location, location): friends += 1 return friends
def score_coordination(loc1): self._coordination = 0 for loc2 in friends: if rg.wdist(loc1, loc2) <= 1: self._coordination = living_space return self._coordination return self._coordination
def spawn(self, game, current_return): if current_return != ['empty']: return current_return #If bot is in spawn, determine how to move out. if rg.loc_types(self.location) == 'spawn': possible_locations = rg.locs_around(self.location, filter_out=('invalid', 'obstacle', 'spawn')) enemy_locs = [] for locs in possible_locations: if locs in game.robots and game.robots[ locs].player_id != self.player_id: enemy_locs.append(locs) for enemy in enemy_locs: if enemy.hp < 9: return ['attack', enemy] elif enemy in possible_locations: possible_locations.remove(enemy) best_option = [100, (0, 0)] for locs in possible_locations: current = [rg.wdist(locs, rg.CENTER_POINT), locs] if current[0] <= best_option[0]: best_option = current return ['move', best_option[1]] return current_return
def act(self, game): # if we're in the center, stay put if self.location == rg.CENTER_POINT: return ['guard'] num_enemies_near = sum([ pt in self.enemy_bot_locations(game) for pt in rg.locs_around(self.location)]) if num_enemies_near > 1 and self.hp < 20: return ['suicide'] # if there are enemies around, attack them for loc, bot in game.robots.items(): if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1: return ['attack', loc] # move toward the center dist_to_center = rg.wdist(self.location, rg.CENTER_POINT) if dist_to_center > 5: next_location = rg.toward(self.location, rg.CENTER_POINT) else: next_location = self.random_location() if next_location in game.robots: return ['guard'] return ['move', next_location]
def act(self, game): bestDistance = 999 bestLoc = (0, 0) otherPlaces = rg.locs_around(self.location, filter_out=('invalid', 'obstacle')) for loc, bot in game['robots'].iteritems(): if bot.player_id != self.player_id: if rg.wdist(loc, self.location) < bestDistance: bestDistance = rg.dist(loc, self.location) bestLoc = loc if game['robots'][self.location].hp <= 10: away = tuple( map(lambda x, y: x - y, rg.toward(self.location, bestLoc), self.location)) move = tuple(map(lambda x, y: x - y, self.location, away)) goodPlaces = rg.locs_around(self.location, filter_out=('invalid', 'obstacle', 'spawn')) if move in goodPlaces: return ['move', move] for loc in rg.locs_around(self.location, filter_out=('invalid', 'obstacle')): if loc in game['robots']: if game['robots'][loc].player_id != self.player_id: return ['suicide'] else: return ['guard'] for loc, bot in game['robots'].iteritems(): if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1: if loc in otherPlaces: return ['attack', loc] if rg.dist(bestLoc, self.location) <= enemyDistance: if rg.toward(self.location, bestLoc) in otherPlaces: return ['move', rg.toward(self.location, bestLoc)] # otherwise clump up for loc, bot in game['robots'].iteritems(): if bot.player_id == self.player_id: if rg.wdist(loc, self.location) <= allyDistance: if loc in otherPlaces: return ['move', rg.toward(self.location, loc)] if self.location == rg.CENTER_POINT: return ['guard'] if rg.toward(self.location, rg.CENTER_POINT) in otherPlaces: return ['move', rg.toward(self.location, rg.CENTER_POINT)]
def find_opponents(loc1): #next to the bot self._opponents = [] for loc2, bot in robots.iteritems(): if rg.wdist(loc1, loc2) <= 1: if robots[base_bot]['player_id'] != robots[loc2][ 'player_id']: self._opponents.append(loc2) return self._opponents
def act(self, game): # find closest enemy CLOSESTE = (1000, 1000) for loc, bot in game.get('robots').items(): if bot.player_id != self.player_id: if rg.wdist(loc, self.location) <= rg.wdist( CLOSESTE, self.location): CLOSESTE = loc # Suicide if an enemy is close for loc, bot in game['robots'].iteritems(): if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1: return ['suicide'] # move towards enemy return ['move', rg.toward(self.location, CLOSESTE)]
def act(self, game): # find closest enemy CLOSESTE = (1000, 1000) for loc, bot in game.get('robots').items(): if bot.player_id != self.player_id: if rg.wdist(loc, self.location) <= rg.wdist( CLOSESTE, self.location): CLOSESTE = loc # if there are enemies around, attack them for loc, bot in game['robots'].iteritems(): if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1: return ['attack', loc] # move towards enemy return ['move', rg.toward(self.location, CLOSESTE)]
def ring_search(self, src, wdist=1, inclusive=False): """Give me all locations that are within wdist of src excluding src""" result = [] try: for x in range(src[0] - wdist, src[0] + wdist + 1): for y in range(src[1] - wdist, src[1] + wdist + 1): xy = (x, y) if 'obstacle' in rg.loc_types(xy): continue if 'invalid' in rg.loc_types(xy): continue if inclusive: if rg.wdist(src, xy) <= wdist: result.append(xy) if not inclusive: if rg.wdist(src, xy) == wdist: result.append(xy) except TypeError, e: raise Exception("Typeerror %s, src = %s and wdist = %s" % (e, src, wdist))
def find_closest(self, locs, center=None): if not locs: return None if not center: center = self.location locs.sort(key=lambda x: rg.wdist(center, x)) return locs[0]
def around(self, bot=None, distance=1, include_spawn=False): bot = bot or self valid_locations = self.ALL_LOCATIONS if include_spawn else self.VALID_LOCATIONS if distance == 1: return [ l for l in (self.north(bot), self.east(bot), self.south(bot), self.west(bot)) if l in valid_locations ] return filter(lambda l: rg.wdist(bot.location, l) <= distance, valid_locations)
def get_center(bot): centers = [(9, 4), (14, 9), (9, 14), (4, 9)] # centers = [(9, 4), (9, 14)] min_distance = sys.maxint center = centers[0] for c in centers: dist = rg.wdist(bot.location, c) if dist < min_distance: min_distance = dist center = c return center
def safe(this_robot, loc, game): turns_left = 10 - game.turn % 10 if (turns_left == 10): turns_left = 0 if (turns_left <= 2 and spawn(loc)): return 0 for bot in one_robots: if (loc == bot.location and bot.player_id != this_robot.player_id): return 0 for bot in two_robots: if bot.player_id != this_robot.player_id: if rg.wdist(loc, bot.location) == 1: return 0 return 1
def act(self, game): # if we're in the center, stay put if self.location == rg.CENTER_POINT: return ['guard'] # if there are enemies around, attack them for loc, bot in game.robots.iteritems(): if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1: return ['attack', loc] self_to_center = rg.wdist(self.location, rg.CENTER_POINT) # try to approach the center for dst in rg.locs_around(self.location): dst_to_center = rg.wdist(dst, rg.CENTER_POINT) if dst_to_center < self_to_center: return ['move', dst] # it doesnt matter return ['guard']
def find_closest_enemy(loc): self._ref_enemy_hp = 999 self._closest_enemy_hp = 0 self._ref_dist = 999 self._enemy_dist = 0 for loc2 in them: self._enemy_dist = rg.wdist(loc, loc2) if self._enemy_dist <= self._ref_dist: if robots[loc2]['hp'] < self._ref_enemy_hp: self._closest_enemy_hp = robots[loc2]['hp'] self._closest_enemy = loc2 return self._closest_enemy
def act(self, game): # If we're on a spawn square, get out if rg.loc_types(self.loc) != 'normal': return ['move', rg.toward(self.location, rg.CENTER_POINT)] d1_us = {} d1_them = {} d2_us = {} d2_them = {} for loc, bot in game.robots.iteritems(): if rg.wdist(self.loc, loc) == 1: if bot.robot_id: d1_us[loc] = bot else: d1_them[loc] = bot elif rg.wdist(self.loc, loc) == 2: if bot.robot_id: d2_us[loc] = bot else: d2_them[loc] = bot if d1_them: return ['attack', d1_them.keys()[0]] elif d2_them: return ['attack', rg.toward(self.loc, d2_them.keys()[0])] # move: # - don't move into a square an ally could move into # - move towards an ally # - move towards an enemy # suicide condition #allyNeighbours = 0 # work out #enemyNeightbours = 0 #if self.hp < 10 and allyNeighbours == 0 and (enemyNeightbours > 1 or enemyNeightbours == 1 and their hp > 10) # return ['suicide'] # Default action #return ['guard'] return ['move', rg.toward(self.location, rg.CENTER_POINT)]
def ring_search(self, src, wdist=1): """Give me all locations that are within wdist of src excluding src""" result = [] for x in range(src[0] - wdist, src[0] + wdist + 1): for y in range(src[1] - wdist, src[1] + wdist + 1): xy = (x, y) if rg.loc_types(xy) in ['obstacle', 'invalid']: continue if rg.wdist(src, xy) <= wdist: result.append(xy) result.remove(src) return result