def move_dijkstra(self, target): self.path = libtcod.dijkstra_new(Fov.get_fov_map(), 1.41) libtcod.dijkstra_compute(self.path, self.x, self.y) libtcod.dijkstra_path_set(self.path, target.x, target.y) self.walk_path()
def get_djikstra(self, x,y): if (x,y) not in self.djikstra_cache: print 'new (%s, %s)' % (x,y) dj = libtcod.dijkstra_new(self.fov_map) libtcod.dijkstra_compute(dj, x, y) self.dijkstra_cache[x,y] = dj return dj
def BuildNavMapEmpty(self): for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): libtcod.map_set_properties(self.navmap,x,y,True,True) del self.currentpath print('Finished creating blank NavMap for ' + self.owner.name) self.currentpath = libtcod.dijkstra_new(self.navmap,DIAGONAL_COST) self.UpdateNavMapLocation()
def set_dijkstra_map(self): fov_map = libtcod.map_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT) for y in range(game.WORLDMAP_HEIGHT): for x in range(game.WORLDMAP_WIDTH): libtcod.map_set_properties(fov_map, x, y, True, True) path = libtcod.dijkstra_new(fov_map) libtcod.dijkstra_compute(path, self.originx, self.originy) return path
def set_full_explore_map(map, dijkstra=True): set_map = libtcod.map_new(map.map_width, map.map_height) for py in range(map.map_height): for px in range(map.map_width): libtcod.map_set_properties(set_map, px, py, not map.tile_is_sight_blocked(px, py), not map.tile_is_blocked(px, py)) if dijkstra: path = libtcod.dijkstra_new(set_map) else: path = libtcod.path_new_using_map(set_map) return path
def __join_rooms(self): all_caves = self.__determine_areas() # Build a tunneling map for pathing - use the permanent state check to determine passability. tunnelingmap = libtcod.map_new(self.__width, self.__height) for x in range(1, self.__width - 1): for y in range(1, self.__height - 1): perm_wall = (self.__map[x][y].permanent and self.__map[x][y].blocked) libtcod.map_set_properties(tunnelingmap, x, y, True, not perm_wall) # The tunneling path will let us move from the origin to the center. tunnelingpath = libtcod.dijkstra_new(tunnelingmap, 0.0) # The center needs to be non-permanent, otherwise we can't path to it. center_x, center_y = self.__find_good_center(self.center_pt) for cave in all_caves.keys(): # This comment used to run the joining. The function is still usable! #self.__join_points(all_caves[cave][0]) origin_x = all_caves[cave][0][0] origin_y = all_caves[cave][0][1] libtcod.dijkstra_compute(tunnelingpath, origin_x, origin_y) if not libtcod.dijkstra_path_set(tunnelingpath, center_x, center_y): print "Could not path! Center point permanent:", self.__map[ center_x][center_y].permanent prev_pt = (origin_x, origin_y) while not libtcod.dijkstra_is_empty(tunnelingpath): x, y = libtcod.dijkstra_path_walk(tunnelingpath) next_pt = (x, y) if x is not None: root1 = self.__ds.find(next_pt) root2 = self.__ds.find(prev_pt) if root1 != root2: self.__ds.union(root1, root2) self.__map[next_pt[0]][next_pt[1]].blocked = False self.__map[next_pt[0]][next_pt[1]].block_sight = False self.__map[next_pt[0]][next_pt[1]].debug = True # DEBUG if self.__stop_drawing(prev_pt, next_pt, self.center_pt): print "Done cave", cave break prev_pt = next_pt all_caves = self.__determine_areas() if len(all_caves.keys()) > 1: self.__join_rooms()
def __join_rooms(self): all_caves = self.__determine_areas() # Build a tunneling map for pathing - use the permanent state check to determine passability. tunnelingmap = libtcod.map_new(self.__width, self.__height) for x in range(1, self.__width-1): for y in range(1, self.__height-1): perm_wall = (self.__map[x][y].permanent and self.__map[x][y].blocked) libtcod.map_set_properties(tunnelingmap, x, y, True, not perm_wall) # The tunneling path will let us move from the origin to the center. tunnelingpath = libtcod.dijkstra_new(tunnelingmap, 0.0) # The center needs to be non-permanent, otherwise we can't path to it. center_x, center_y = self.__find_good_center(self.center_pt) for cave in all_caves.keys(): # This comment used to run the joining. The function is still usable! #self.__join_points(all_caves[cave][0]) origin_x = all_caves[cave][0][0] origin_y = all_caves[cave][0][1] libtcod.dijkstra_compute(tunnelingpath, origin_x, origin_y) if not libtcod.dijkstra_path_set(tunnelingpath, center_x, center_y): print "Could not path! Center point permanent:", self.__map[center_x][center_y].permanent prev_pt = (origin_x, origin_y) while not libtcod.dijkstra_is_empty(tunnelingpath): x, y = libtcod.dijkstra_path_walk(tunnelingpath) next_pt = (x, y) if x is not None: root1 = self.__ds.find(next_pt) root2 = self.__ds.find(prev_pt) if root1 != root2: self.__ds.union(root1, root2) self.__map[next_pt[0]][next_pt[1]].blocked = False self.__map[next_pt[0]][next_pt[1]].block_sight = False self.__map[next_pt[0]][next_pt[1]].debug = True # DEBUG if self.__stop_drawing(prev_pt, next_pt, self.center_pt): print "Done cave", cave break prev_pt = next_pt all_caves = self.__determine_areas() if len(all_caves.keys())>1: self.__join_rooms()
def BuildNavMap(self): numberofdoors = 0 for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): scannedtile = Map[x][y] if 'ViewBlocking' in scannedtile.flags: canseethrough = False else: canseethrough = True if scannedtile.name == 'Door': numberofdoors += 1 if "Impassable" in scannedtile.flags and not scannedtile.name in ASSUMED_PASSABLE_TILES: canwalkthrough = False else: canwalkthrough = True libtcod.map_set_properties(self.navmap,x,y,canseethrough,canwalkthrough) del self.currentpath print('Finished Building Navmap:') print('Number of doors found: ' + str(numberofdoors)) self.currentpath = libtcod.dijkstra_new(self.navmap,DIAGONAL_COST) self.UpdateNavMapLocation()
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 findPath( self, start, end ): def point( p ): return ( int( math.floor( p.x ) ), int( math.floor( p.y ) ) ) start = point( start ) end = point( end ) if self.pathEnd != end: self.pathEnd = end self.dijkstra = tcod.dijkstra_new( self.tcodmap, 1.41421356237 ) tcod.dijkstra_compute( self.dijkstra, end[0], end[1] ) if not tcod.dijkstra_path_set( self.dijkstra, start[0], start[1] ): return None ret = [] while not tcod.dijkstra_is_empty( self.dijkstra ): x, y = tcod.dijkstra_path_walk( self.dijkstra ) ret.append( ( x, y ) ) return ret