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 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