def hunt(self, limit): lowest = 999999 weakest = 0 for index in range(1,7): num_hills = len(self.game_state.enemy_hills[index]) if num_hills > 0: num_ants = len(self.game_state.enemy_ants[index]) if num_ants < lowest: lowest = num_ants weakest = index if weakest != 0: #self.logger.debug("weak hill %d, %s", weakest, self.game_state.enemy_hills[weakest]) for loc in self.game_state.enemy_hills[weakest]: hill_loc = loc #self.logger.debug("ant_dist %s", ant_dist) for ant_loc in self.game_state.movable.copy(): path = self.pathfinder.a_star(ant_loc, hill_loc, 200) if path: #self.logger.debug("adding path to path list %s", path) self.add_path(path, target(HILL_VALUE, hill_loc)) # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: #self.logger.debug("Out of time... Hunt... break count") break
def get_targets(self): targets = [] id_name_regex = 'id\:\s*([0-9]+)\s*or\s*"([^"]+)"' proc = os.popen(self.cfg.android_cmd + ' list target') try: line = proc.readline() while len(line) > 0: line = line.replace("\n", "") id_matches = re.search(id_name_regex, line) if id_matches: line = proc.readline() tg = target(self.cfg) tg.Id = int(id_matches.group(1)) tg.str_id = id_matches.group(2) target.str_id = id_matches.group(2) name = '' value = '' while len(line) > 0: line = line.replace("\n", "") if len(line) == line.count("-"): # ignore -------- after target ABIs line = proc.readline() continue matches = re.search(id_name_regex, line) if matches: id_matches = matches if len(name) > 0: tg.set_value(name, value) targets.append(tg) break matches = re.search("\s*([^\:]+)\:(.*)", line) if matches: if len(name) > 0: tg.set_value(name, value) name = matches.group(1) value = matches.group(2) else: value += line.replace("\t", "") line = proc.readline() else: line = proc.readline() if len(name) > 0: tg.set_value(name, value) if len(tg.name) > 0: targets.append(tg) except error as err: proc.close() raise err proc.close() return targets
def find_food(self, limit): #self.logger.debug("Find Food") ant_dist = [] # Check the distance for each ant for ant_loc in self.game_state.movable: # Start with a really high number for comparisions lowest = 999999 low_ant = (0,0) low_food = (0,0) for food_loc in self.game_state.food: if self.ants.time_remaining() < limit: self.logger.debug("Out of time... Food... break count") return dist = self.ants.distance(ant_loc, food_loc) # If shorter distance then before record it if dist < lowest and dist != 0: lowest = dist low_ant = ant_loc low_food = food_loc # If a lower distance then the starting value is found then append it if lowest != 999999: ant_dist.append((lowest, low_ant, low_food)) self.game_state.food.discard(low_food) ant_dist.sort() #self.logger.debug("ant_dist %s", ant_dist) for dist, ant_loc, food_loc in ant_dist: # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: self.logger.debug("Out of time... Food... break count") return path = self.pathfinder.a_star(ant_loc, food_loc, FOOD_MOVE_LIMIT) if path: #self.logger.debug("adding path to path list %s", path) self.add_path(path, target(FOOD_VALUE, food_loc))
def hunt(self, limit): #self.logger.debug("Hunt") lowest = 999999 weakest = 0 for index in range(1,7): num_hills = len(self.game_state.enemy_hills[index]) if num_hills > 0: num_ants = len(self.game_state.enemy_ants[index]) if num_ants < lowest: lowest = num_ants weakest = index if weakest != 0: #self.logger.debug("weak hill %d, %s", weakest, self.game_state.enemy_hills[weakest]) for loc in self.game_state.enemy_hills[weakest]: hill_loc = loc #self.logger.debug("ant_dist %s", ant_dist) attack_num = int(len(self.game_state.movable) * ATTACK_RATIO) count = 0 for ant_loc in self.game_state.movable.copy(): # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: self.logger.debug("Out of time... Hunt... break count") return count = count + 1 if count > attack_num: self.logger.debug("Attack limit... Hunt... break count") return; path = self.pathfinder.a_star(ant_loc, hill_loc, ATTACK_MOVE_LIMIT) if path: #self.logger.debug("adding path to path list %s", path) self.add_path(path, target(HILL_VALUE, hill_loc))
def explore(self, limit): #self.logger.debug("Explore") desired = [] size = self.game_state.size half = int(size / 2) for y in range(self.game_state.bucket_rows): # check if we still have time left to calculate more orders for x in range(self.game_state.bucket_cols): if self.ants.time_remaining() < limit: self.logger.debug("Out of time... Explore... break count") return loc = ((y * size) + half, (x * size) + half) #if not self.game_state.bucket_map[x][y].my_ants: # for offset_y in range(size - 1): # for offset_x in range(size - 1): # loc = ((y * size) + offset_y, (x * size) + offset_x) # passable = self.ants.passable(loc) # if passable: # break # if passable: # break dist = 0 for hill_loc in self.game_state.my_hills: dist = dist + self.ants.distance(hill_loc, loc) dist = dist + (len(self.game_state.bucket_map[x][y].enemy_ants) * ENEMY_ANT_SCORE) desired.append((dist, loc)) desired.sort() #self.logger.debug("Desired %s", desired) desired_density = int(self.game_state.num_my_ants * DENSE_RATIO) + 1 ant_dist = [] for ant_loc in self.game_state.movable: row, col = ant_loc col = int(col / self.game_state.size) row = int(row / self.game_state.size) if len(self.game_state.bucket_map[col][row].my_ants) > desired_density: lowest = 999999 low_ant = (0,0) low_food = (0,0) save_dist = 0 # Check the distance for each ant for dist, land_loc in desired: # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: self.logger.debug("Out of time... Explore... break count") return test = self.ants.distance(ant_loc, land_loc) # If shorter distance then before record it if test < lowest and test != 0: lowest = test save_dist = dist low_ant = ant_loc low_land = land_loc # If a lower distance then the starting value is found then append it if lowest != 999999: ant_dist.append((lowest, low_ant, low_land)) desired.remove(( save_dist, low_land )) #self.logger.debug("ant_dist %s", ant_dist) for dist, ant_loc, land_loc in ant_dist: # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: self.logger.debug("Out of time... Explore... break count") return path = self.pathfinder.a_star(ant_loc, land_loc, EXPLORE_MOVE_LIMIT) if path: #self.logger.debug("adding path to path list %s", path) self.add_path(path, target(LAND_VALUE, land_loc))
def hunt(self, limit): #self.logger.debug("----------------- Hunt Phase -----------------") lowest = 999999 weakest = 0 for index in range(1,7): num_hills = len(self.game_state.enemy_hills[index]) if num_hills > 0: num_ants = len(self.game_state.enemy_ants[index]) if num_ants < lowest: lowest = num_ants weakest = index #self.logger.debug("weakest enemy %d", weakest) if weakest != 0: attack_loc = (-1,-1) if self.game_state.enemy_hills[weakest]: for loc in self.game_state.enemy_hills[weakest]: attack_loc = loc else: size = self.game_state.size half = int(size / 2) highest = 0 for y in range(self.game_state.bucket_rows): # check if we still have time left to calculate more orders for x in range(self.game_state.bucket_cols): if self.ants.time_remaining() < limit: #self.logger.debug("Lowest bucket... Hunt... break count") return if self.game_state.bucket_map[x][y].num_enemy_ants != 0: ant_ratio = self.game_state.bucket_map[x][y].num_my_ants / self.game_state.bucket_map[x][y].num_enemy_ants if highest < ant_ratio: highest = ant_ratio attack_loc = self.game_state.bucket_map[x][y].enemy_ants[0] if attack_loc == (-1,-1): #self.logger.debug("No one to attack... Hunt... break count") return ant_dist = [] for ant_loc in self.game_state.movable: # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: #self.logger.debug("Out of time... Hunt... break count") return dist = self.ants.distance(ant_loc, attack_loc) ant_dist.append((dist, ant_loc)) ant_dist.sort() attack_num = int(self.game_state.num_my_ants * ATTACK_RATIO) #self.logger.debug("Attack ratio %s location %s", attack_num, attack_loc) count = 0 for dist, ant_loc in ant_dist: # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: #self.logger.debug("Distance... Hunt... break count") return count = count + 1 if count > attack_num: #self.logger.debug("Attack limit... Hunt... break count") return; path = self.pathfinder.a_star(ant_loc, attack_loc, ATTACK_MOVE_LIMIT) if path: #self.logger.debug("adding path to path list %s", path) self.add_path(path, target(HILL_VALUE, attack_loc))
def explore(self, limit): desired = [] size = self.game_state.size half = int(size / 2) for y in range(self.game_state.bucket_rows): for x in range(self.game_state.bucket_cols): if not self.game_state.bucket_map[x][y].my_ants: for offset_y in range(size - 1): for offset_x in range(size - 1): loc = ((y * size) + offset_y, (x * size) + offset_x) passable = self.ants.passable(loc) if passable: break if passable: break dist = 0 for hill_loc in self.game_state.my_hills: dist = dist + self.ants.distance(hill_loc, loc) desired.append((dist, loc)) # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: #self.logger.debug("Out of time... Explore... break count") break desired.sort() #self.logger.debug("Desired %s", desired) ant_dist = [] for ant_loc in self.game_state.movable: row, col = ant_loc col = int(col / self.game_state.size) row = int(row / self.game_state.size) if len(self.game_state.bucket_map[col][row].my_ants) > 1: lowest = 999999 low_ant = (0,0) low_food = (0,0) save_dist = 0 # Check the distance for each ant for dist, land_loc in desired: test = self.ants.distance(ant_loc, land_loc) #ant_dist.append((test, ant_loc, land_loc)) #test = len(self.pathfinder.a_star(ant_loc, land_loc, 50)) # If shorter distance then before record it if test < lowest and test != 0: lowest = test save_dist = dist low_ant = ant_loc low_land = land_loc # If a lower distance then the starting value is found then append it if lowest != 999999: ant_dist.append((lowest, low_ant, low_land)) desired.remove(( save_dist, low_land )) # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: #self.logger.debug("Out of time... Explore... break count") break #self.logger.debug("ant_dist %s", ant_dist) for dist, ant_loc, land_loc in ant_dist: path = self.pathfinder.a_star(ant_loc, land_loc, 50) if path: #self.logger.debug("adding path to path list %s", path) self.add_path(path, target(LAND_VALUE, land_loc)) # check if we still have time left to calculate more orders if self.ants.time_remaining() < limit: #self.logger.debug("Out of time... Explore... break count") break