Example #1
0
    def setup_graphs(self):
        # Connect the tiles into graph structures that can easily be searched 
        # (for shortest paths and so forth).  Two graphs are currently created: 
        # one with all the land tiles and another with all the sea tiles.  This 
        # makes it possible to find paths for both land and sea units.  More 
        # graphs may be necessary as we add units that move in different ways.

        self.graphs = {
                'land': networkx.Graph(),
                'sea': networkx.Graph()
        }

        # Fill each graphs with the appropriate tiles.

        for tile in self.yield_tiles():
            if tile.is_land: self.graphs['land'].add_node(tile)
            if tile.is_sea: self.graphs['sea'].add_node(tile)

        # Create edges for all the graphs.  Diagonal edges are included.

        half_neighbors = lambda row, col: [
                        (row + 1, col + 0),
                        (row + 1, col + 1),
                        (row + 0, col + 1),
                        (row - 1, col + 1) ]

        for row_1, col_1 in self.yield_indices():
            index_1 = row_1, col_1
            tile_1 = self.tiles[index_1]

            for row_2, col_2 in half_neighbors(row_1, col_1):
                index_2 = row_2, col_2
                tile_2 = self.tiles.get(index_2)

                if tile_2 is None:
                    continue

                weight = vecrec.get_distance(index_1, index_2)

                if tile_1.is_land and tile_2.is_land:
                    self.graphs['land'].add_edge(tile_1, tile_2, weight=weight)

                if tile_1.is_sea and tile_2.is_sea:
                    self.graphs['sea'].add_edge(tile_1, tile_2, weight=weight)
Example #2
0
        def location_ok(tile):
            if not tile.is_land: return False

            # Require that the tile and all it's neighbors are land.

            for neighbor in self.yield_neighbors(tile):
                if not neighbor.is_land: return False

            # Require that there's no other resource within some radius.
            
            min_distance = float('inf')

            for resource_tile in self.resource_tiles:
                distance = vecrec.get_distance(tile.index, resource_tile.index)
                min_distance = min(distance, min_distance)
                if min_distance < resource_spacing: return False

            # If resource looks good, add it to the map.

            return True
Example #3
0
 def a_star_heuristic(a, b):
     return vecrec.get_distance(a.index, b.index)