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 take_turn(self): pos = self.owner.x, self.owner.y dx,dy = 0,0 player_room = self.level.player.get_room() if self.level.is_visible(*pos): if self.level.player.distance(*pos) < 2: self.owner.fighter.attack(game_instance.player) else: dx, dy = self.owner.get_step_towards(*self.level.player.pos) else: dj = self.level.get_djikstra(*self.owner.pos) path = libtcod.dijkstra_path_set(dj, *self.level.player.pos) x,y = libtcod.dijkstra_path_walk(dj) if x is not None: dx = x - self.owner.x dy = y - self.owner.y else: print '!' self.owner.move(dx,dy)
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 RecomputePath(self): self.UpdateNavMapLocation() err = libtcod.dijkstra_path_set(self.currentpath, self.currenttarget.x, self.currenttarget.y) if err == False: print(self.owner.name + ' failed to recompute path') self.currentlyonpath = False self.currentpath = None return err else: self.currentlyonpath = True return True
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 __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 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