def one_bot_visibility(instance, bot): position = bot.position cells = [] w = visibility.Wave((88, 50), lambda x, y: instance.level.blockHeights[x][y] > 1, lambda x, y: cells.append((x, y))) w.compute(position) instance.bots[bot.name]["visibility"] = set([get_node_index(instance, Vector2(x, y)) for x, y in cells]) return cells
def weight_camp_locations_by_sight(instance, close_nodes): #Calculate the weight of all squares close to the enemy base relying on how many of the exit squares can be shot. enemy_base = get_enemy_base(instance) for node_index in close_nodes: node_position = regressions2.get_node_vector(instance, node_index) cells = [] w = visibility.Wave((88, 50), lambda x, y: instance.level.blockHeights[x][y] > 1, lambda x, y: cells.append((x, y))) w.compute(node_position) for x, y in cells: cell_position = Vector2(x, y) cell_node_index = regressions2.get_node_index( instance, cell_position) if node_position.distance( cell_position) < instance.level.firingDistance: #Edges don't work with our functions, and are unlikely to be actual optimum. #TODO fully debug rather than hack. if not (node_position.x < 1.0 or node_position.x > 87.0 or node_position.y < 1.0 or node_position.y > 47.0): camp_value = instance.graph.node[cell_node_index][ "camp_target"] / (cell_position.distance(enemy_base) + 3) instance.graph.node[node_index][ "camp_location"] += camp_value
def one_bot_visibility(instance, bot): position = bot.position cells = [] w = visibility.Wave((88, 50), lambda x, y: instance.level.blockHeights[x][y] > 1, lambda x, y: cells.append((x, y))) w.compute(position) return cells
def weight_camp_locations_by_choke_exposure(instance): for node in instance.choke_dict.keys(): enemy_base_square = regressions2.get_node_vector(instance, node) cells = [] w = visibility.Wave((88, 50), lambda x, y: instance.level.blockHeights[x][y] > 1, lambda x, y: cells.append((x, y))) w.compute(enemy_base_square) for x, y in cells: cell_position = Vector2(x, y) cell_node_index = regressions2.get_node_index( instance, cell_position) if cell_position.distance( enemy_base_square) < instance.level.firingDistance + 3: instance.graph.node[cell_node_index]["camp_location"] *= .8
def calculate_control_main_route2(instance): #Set ambush dictionary. for node_index in instance.graph.nodes(): instance.graph.node[node_index]["ambush"] = 0.0 start, finish = instance.level.botSpawnAreas[instance.game.enemyTeam.name] instance.graph.add_node("enemy_base", position = (start.x, start.y), weight = 0.0) instance.graph.node["enemy_base"]["ambush"] = 0.0 for i, j in itertools.product(range(int(start.x), int(finish.x)), range(int(start.y), int(finish.y))): instance.graph.add_edge("enemy_base", instance.terrain[j][i], weight = 1.0) our_flag_node = get_node_index(instance, instance.game.team.flag.position) enemy_score_node = get_node_index(instance, instance.game.enemyTeam.flagScoreLocation) enemy_flag_node = None vb2f = nx.shortest_path(instance.graph, source="enemy_base", target=our_flag_node) vf2s = nx.shortest_path(instance.graph, source=our_flag_node, target=enemy_score_node) path = vb2f + vf2s edgesinpath=zip(path[0:],path[1:]) for vt, vf in edgesinpath[:-1]: if "position" not in instance.graph.node[vf]: continue position = Vector2(*instance.graph.node[vf]["position"]) if "position" not in instance.graph.node[vt]: continue next_position = Vector2(*instance.graph.node[vt]["position"]) if position == next_position: continue orientation = (next_position - position).normalized() def visible(p): delta = (p-position) l = delta.length() if l < instance.level.firingDistance: return True else: return False cells = [] w = visibility.Wave((88, 50), lambda x, y: instance.level.blockHeights[x][y] > 1, lambda x, y: cells.append((x,y))) w.compute(position) for x, y in [c for c in cells if visible(Vector2(c[0]+0.5, c[1]+0.5))]: instance.graph.node[get_node_index(instance, Vector2(x,y))]["ambush"] += 2.0
def weight_camp_locations_by_base_exposure(instance): #Adjust the weight based on what squares can be seen from the enemy base. start, finish = instance.level.botSpawnAreas[instance.game.enemyTeam.name] for i, j in itertools.product(range(int(start.x), int(finish.x)), range(int(start.y), int(finish.y))): enemy_base_square = Vector2(i, j) cells = [] w = visibility.Wave((88, 50), lambda x, y: instance.level.blockHeights[x][y] > 1, lambda x, y: cells.append((x, y))) w.compute(enemy_base_square) for x, y in cells: cell_position = Vector2(x, y) cell_node_index = regressions2.get_node_index( instance, cell_position) if cell_position.distance( enemy_base_square) < instance.level.firingDistance + 3: instance.graph.node[cell_node_index]["camp_location"] *= .8 instance.graph.node["enemy_base"]["camp_location"] = 0.0
def one_bot_sees(instance, bot, simulated_bot = None): nodes = [] #finds visible squares to one bot, or a dictionary with keys "direction" and "position" if simulated_bot is specified. if not simulated_bot: cells = one_bot_visibility(instance, bot) for cell in cells: x = cell[0] y = cell[1] #Deals with problem of 1 height blocks being visible but not in path graph. if instance.level.blockHeights[x][y] == 1.0: continue cell_position = Vector2(x, y) if can_bot_see(instance, bot, cell_position): node_index = get_node_index(instance, cell_position) nodes.append(node_index) #This is for when we are looking at visibility points along a path as opposed to for a specific bot. The lack of DRY is bad, #but not backbreaking. #TODO refactor when free time allows. else: position = simulated_bot["position"] direction = simulated_bot["direction"] cells = [] w = visibility.Wave((88, 50), lambda x, y: instance.level.blockHeights[x][y] > 1, lambda x, y: cells.append((x, y))) w.compute(position) for cell in cells: x = cell[0] y = cell[1] #Deals with problem of 1 height blocks being visible but not in path graph. if instance.level.blockHeights[x][y] == 1.0: continue cell_position = Vector2(x, y) position_to_bot_vector = cell_position - position angle = get_angle(direction, position_to_bot_vector) bot_fov = instance.level.fieldOfViewAngles[bot.state] if not bot_fov: bot_fov = 1.57 if abs(angle) < bot_fov/2.0: node_index = get_node_index(instance, cell_position) nodes.append(node_index) return nodes
def visibleSquares(instance, viewing_point): visible_cell_list = [] w = visibility.Wave((88, 50), lambda x, y: instance.level.blockHeights[x][y] > 1, lambda x, y: visible_cell_list.append((x,y))) w.compute(viewing_point) return visible_cell_list