def connect_rooms(cls, taken_spaces_map, exits): path_map = tcod.map_new(len(taken_spaces_map), len(taken_spaces_map[0])) for x in range(path_map.width): for y in range(path_map.height): if (x, y) not in exits: tcod.map_set_properties(path_map, x, y, False, taken_spaces_map[x][y] != 1) else: tcod.map_set_properties(path_map, x, y, False, True) connected_exits = list() for exit in exits: if exit not in connected_exits: path = tcod.dijkstra_new(path_map, 0) destination = None tries = 5 while destination in [exit, None ] + connected_exits and tries > 0: destination = random.choice(exits) tries -= 1 tcod.dijkstra_compute(path, exit[0], exit[1]) if tcod.dijkstra_path_set(path, destination[0], destination[1]): keep_destination = False for index in range(tcod.dijkstra_size(path)): point = tcod.dijkstra_get(path, index) if taken_spaces_map[point[0]][point[1]] == 3: keep_destination = True break taken_spaces_map[point[0]][point[1]] = 3 connected_exits.append(exit) if not keep_destination: connected_exits.append(destination) for exit in set(exits) - set(connected_exits): taken_spaces_map[exit[0]][exit[1]] = 4
def test_dijkstra(map_): path = libtcodpy.dijkstra_new(map_) libtcodpy.dijkstra_compute(path, *POINT_A) assert not libtcodpy.dijkstra_path_set(path, *POINT_C) assert libtcodpy.dijkstra_get_distance(path, *POINT_C) == -1 assert libtcodpy.dijkstra_path_set(path, *POINT_B) assert libtcodpy.dijkstra_size(path) assert not libtcodpy.dijkstra_is_empty(path) libtcodpy.dijkstra_reverse(path) for i in range(libtcodpy.dijkstra_size(path)): x, y = libtcodpy.dijkstra_get(path, i) while (x, y) != (None, None): x, y = libtcodpy.dijkstra_path_walk(path) libtcodpy.dijkstra_delete(path)
def route_between(self, x1, y1, x2, y2, tile=Tiles.CORRIDOR_FLOOR, avoid=[], weights=[], overwrite=False, avoid_rooms=False): dijk, dijk_dist = self.create_dijkstra_map(x1, y1, default_weight=1, avoid=avoid, weights=weights, avoid_rooms=avoid_rooms) tcod.dijkstra_path_set(dijk, x2, y2) for i in range(tcod.dijkstra_size(dijk)): x, y = tcod.dijkstra_get(dijk, i) if overwrite: self.grid[x, y] = tile else: replacement_tile = self.grid[x, y] if self.grid[x, y] in BLOCKING_TILES: replacement_tile = self.grid[x, y] = wall_to_floor.get( self.grid[x, y], tile) elif self.grid[x, y] == Tiles.EMPTY: replacement_tile = tile elif self.grid[x, y] == Tiles.POTENTIAL_CORRIDOR_FLOOR: replacement_tile = Tiles.CORRIDOR_FLOOR self.grid[x, y] = replacement_tile return dijk_dist