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 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
def ReturnNextPointOnPath(self,offset): if self.currentlyonpath == True: if not libtcod.dijkstra_is_empty(self.currentpath): if offset == True: err = libtcod.dijkstra_get(self.currentpath, 1) if not err == None: x,y = libtcod.dijkstra_get(self.currentpath, 0) x = x - self.owner.x y = y - self.owner.y return x,y else: x,y = libtcod.dijkstra_get(self.currentpath, 0) if not x == False: return x,y print('Unable to get next spot in path') return None, None else: print('Path is empty') #self.SetCurrentTarget(CreateRandomWalkableCoords()) self.RecomputePath() return None, None else: print('Not on path') return None, None