def generate_terrain(self, seed): scale = 100.0 octaves = 6 persistence = 0.5 lacunarity = 2.0 for x in range(self.grid.width): for y in range(self.grid.height): current_point = Point(x, y) noise = pnoise3(x / scale, y / scale, seed / scale, octaves=octaves, persistence=persistence, lacunarity=lacunarity, repeatx=self.grid.width, repeaty=self.grid.height, base=0) if noise < -0.35: self.grid[current_point] = [Drawable.WATER] elif noise < -0.20: self.grid[current_point] = [Drawable.MUD] elif noise < 0.1: self.grid[current_point] = [Drawable.DIRT] elif noise < 0.45: self.grid[current_point] = [Drawable.GRASS] elif noise < 0.75: self.grid[current_point] = [Drawable.TREE] else: self.grid[current_point] = [Drawable.ROCK]
def find_closest_top_point(self, objective): closest_point = None closest_distance = inf # infinity if self.on_map(objective): return objective for x in range(self.grid.width): current_point = self.offset_point(Point(x, 0)) current_distance = distance(objective, current_point) if current_distance < closest_distance: closest_point = current_point closest_distance = current_distance return closest_point
def find_closest_left_point(self, waypoint): closest_point = None closest_distance = inf # infinity if self.on_map(waypoint): return waypoint for y in range(self.grid.height): current_point = self.offset_point(Point(0, y)) current_distance = distance(waypoint, current_point) if current_distance < closest_distance: closest_point = current_point closest_distance = current_distance return closest_point
def update_mission_map(self, messages_received): """Update game state (mission_map) from agent subprocess messages""" bullets_live = False for message in messages_received: logging.debug("Simulation: Received message: {}".format(message)) if message[MESSAGE_TYPE] == TAKE_TURN: agent = message[FROM] action = message[ACTION] if action == MOVE_TO: location = Point.from_dict(message[LOCATION]) self.mission_map.move_agent(agent, location) elif action == FIRE_AT: location = Point.from_dict(message[LOCATION]) direction = Direction.from_str(message[DIRECTION]) self.mission_map.create_bullet(location, direction) bullets_live = True elif message[MESSAGE_TYPE] == MISSION_COMPLETE: self.mission_complete = True return if bullets_live: while len(self.mission_map.bullets) > 0: self.update_bullets() self.mission_map.move_bullets()
def find_flanking_waypoint(self, objective_location, my_location): left_candidate = objective_location.plus_vector( Direction.WEST.to_scaled_vector(WARBOT_VISION_DISTANCE - 4)) left_candidate_waypoint = Point(left_candidate.x, my_location.y) logging.debug( "left_candidate is: {}, left_candidate_waypoint is: {}".format( left_candidate, left_candidate_waypoint)) right_candidate = objective_location.plus_vector( Direction.EAST.to_scaled_vector(WARBOT_VISION_DISTANCE - 4)) right_candidate_waypoint = Point(right_candidate.x, my_location.y) logging.debug( "right_candidate is: {}, right_candidate_waypoint is: {}".format( right_candidate, right_candidate_waypoint)) left_waypoint_navigable = self.can_enter_route_plan( left_candidate_waypoint) logging.debug( "left_waypoint_navigable is: {}".format(left_waypoint_navigable)) right_waypoint_navigable = self.can_enter_route_plan( right_candidate_waypoint) logging.debug( "right_waypoint_navigable is: {}".format(right_waypoint_navigable)) left_distance = inf # infinity right_distance = inf if left_waypoint_navigable: logging.debug("Calculating left_distance . . .") left_distance = distance(my_location, left_candidate_waypoint) logging.debug("left_distance: {}".format(left_distance)) if right_waypoint_navigable: logging.debug("Calculating right_path and right_distance . . .") right_distance = distance(my_location, right_candidate_waypoint) logging.debug("right_distance: {}".format(right_distance)) if left_distance < right_distance: logging.debug("Selecting left flanking candidate") return left_candidate, left_candidate_waypoint else: logging.debug("Selecting right flanking candidate") return right_candidate, right_candidate_waypoint
def normalize_point(self, point): point_x = point.x if point_x < 0: point_x = 0 elif point_x >= self.grid.width: point_x = self.grid.width - 1 point_y = point.y if point_y < 0: point_y = 0 elif point_y >= self.grid.height: point_y = self.grid.height - 1 # only create a new point if needed if point_x == point.x and point_y == point.y: return point else: # otherwise return the existing point return Point(point_x, point_y)
def find_flanking_path(self, objective_location, my_location): preferred_candidate = Point(my_location.x, objective_location.y) candidate = preferred_candidate logging.debug("candidate is: {}".format(candidate)) navigable = self.can_enter_route_plan(candidate) logging.debug("navigable is: {}".format(navigable)) while not navigable: logging.debug( "candidate is not navigable, selecting alternate point") candidate = self.get_random_location_near_point( preferred_candidate, 3) logging.debug("alternate candidate is: {}".format(candidate)) navigable = self.can_enter_route_plan(candidate) logging.debug("alternate navigable is: {}".format(navigable)) logging.debug("Calculating path . . .") path = find_path(self, my_location, candidate) logging.debug("path is: {}".format(path)) return candidate, path
def scan(self): for x in range(self.grid.width): for y in range(self.grid.height): current_point = Point(x, y) drawables = self.grid[current_point] for drawable in drawables: if drawable is Drawable.OBJECTIVE: self.objective_location = self.offset_point( current_point) elif drawable is Drawable.RALLY_POINT: self.rally_point_location = self.offset_point( current_point) elif drawable.name.startswith(WARBOT_PREFIX): self.warbot_locations[ drawable.name] = self.offset_point(current_point) elif drawable.name.startswith(OPFOR_PREFIX): self.opfor_locations[ drawable.name] = self.offset_point(current_point) elif drawable.name.startswith(CIVILIAN_PREFIX): self.civilian_locations[ drawable.name] = self.offset_point(current_point)
def get_random_lower_location(self): return Point( randint(int(0.25 * self.grid.width), int(0.75 * self.grid.width)), # X randint(int(0.85 * self.grid.height), int(0.95 * self.grid.height))) # Y
def get_random_location(self): return Point(randint(0, self.grid.width - 1), randint(0, self.grid.height - 1))
def get_color(self, mission_map, x, y): drawables = mission_map.grid[Point(x, y)] if len(drawables) >= 1: return drawables[0].color else: return Colors.BLACK, Colors.BLACK
def get_line_position(self, team_leader_position, my_position): return Point(my_position.x, team_leader_position.y)
def update_location_and_visible_map(self, visible_map_around_point): self.location = Point(visible_map_around_point[YOUR_LOCATION][X], visible_map_around_point[YOUR_LOCATION][Y]) self.visible_map = VisibleMap(visible_map_around_point[GRID], visible_map_around_point[MIN_X], visible_map_around_point[MIN_Y])