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 create_dijkstra_map(self, x1, y1, default_weight=1, avoid=[], weights=[], avoid_rooms=False): walkable = np.zeros(self.grid.shape, dtype=np.int8) walkable[np.where(self.grid != Tiles.EMPTY)] = default_weight avoid.append(Tiles.IMPENETRABLE) mask = np.isin(self.grid, avoid) walkable[mask] = 0 for tile, weight in weights: walkable[np.where(self.grid == tile)] = weight if avoid_rooms: for room in self.rooms: if isinstance(room, prefabRoom): room_slice = walkable[room.x:room.x + room.width, room.y:room.y + room.height] room_slice[np.where(room.mask == 1)] = 0 dijk = tcod.dijkstra_new(walkable, 0) tcod.dijkstra_compute(dijk, x1, y1) dijk_dist = np.zeros(walkable.shape, dtype=np.int8) for (x, y), value in np.ndenumerate(dijk_dist): dijk_dist[x, y] = tcod.dijkstra_get_distance(dijk, x, y) #dijk_dist[np.where(dijk_dist == -1)] = 0 return dijk, dijk_dist
def test_dijkstra_callback(map_, path_callback): path = libtcodpy.dijkstra_new_using_function( libtcodpy.map_get_width(map_), libtcodpy.map_get_height(map_), path_callback, ) libtcodpy.dijkstra_compute(path, *POINT_A) libtcodpy.dijkstra_delete(path)
def generate_dijkstra_player_map(game_map, player): walkable = game_map.current_level.make_walkable_array() dijk = tcod.dijkstra_new(walkable) tcod.dijkstra_compute(dijk, player.x, player.y) dijk_dist = np.zeros(game_map.current_level.walkable.shape, dtype=np.int8) for y in range(game_map.current_level.height): for x in range(game_map.current_level.width): dijk_dist[x, y] = tcod.dijkstra_get_distance(dijk, x, y) dijk_dist[np.where(dijk_dist == -1)] = 0 game_map.current_level.dijkstra_player = dijk_dist
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 generate_dijkstra_flee_map(game_map, player): #if not game_map.current_level.dijkstra_player: generate_dijkstra_player_map(game_map, player) dijk_dist = np.copy(game_map.current_level.dijkstra_player) #print(np.amax(dijk_dist)) max_distance = np.where(dijk_dist == np.amax(dijk_dist)) #print(max_distance) dijk_dist[np.where(dijk_dist != 0)] *= -1.2 updated_dijk = tcod.dijkstra_new(dijk_dist) tcod.dijkstra_compute(updated_dijk, max_distance[0][0], max_distance[1][0]) flee_dist = np.zeros( (game_map.current_level.width, game_map.current_level.height), dtype=np.float32) for y in range(game_map.current_level.height): for x in range(game_map.current_level.width): flee_dist[x, y] = tcod.dijkstra_get_distance(updated_dijk, x, y) flee_dist[np.where(flee_dist == -1)] = 0 game_map.current_level.dijkstra_flee = flee_dist