def set_path(self): # there is no path, or the target has moved if self.path is None or (self.target_x, self.target_y) != libtcod.path_get_destination(self.path): self.path = libtcod.path_new_using_map(self.owner.map.libtcod_map, 1) # create the path; if failed, set path to None if not libtcod.path_compute(self.path, self.owner.x, self.owner.y, self.target_x, self.target_y): self.path = None
def test_astar(map_): astar = libtcodpy.path_new_using_map(map_) assert not libtcodpy.path_compute(astar, *POINTS_AC) assert libtcodpy.path_size(astar) == 0 assert libtcodpy.path_compute(astar, *POINTS_AB) assert libtcodpy.path_get_origin(astar) == POINT_A assert libtcodpy.path_get_destination(astar) == POINT_B libtcodpy.path_reverse(astar) assert libtcodpy.path_get_origin(astar) == POINT_B assert libtcodpy.path_get_destination(astar) == POINT_A assert libtcodpy.path_size(astar) != 0 assert libtcodpy.path_size(astar) > 0 assert not libtcodpy.path_is_empty(astar) for i in range(libtcodpy.path_size(astar)): x, y = libtcodpy.path_get(astar, i) while (x, y) != (None, None): x, y = libtcodpy.path_walk(astar, False) libtcodpy.path_delete(astar)
def create_path(self, gamemap_instance): """Creates the initial path_map, and then on subsequent calls uses that path_map to play.""" mymap = gamemap_instance.level if not self.is_pathmap_created: #Create the path map self.path_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT) for x in range(1, MAP_WIDTH): for y in range(1, MAP_HEIGHT): libtcod.map_set_properties(self.path_map, x, y, not mymap[x][y].block_sight, not mymap[x][y].blocked) self.is_pathmap_created = True # now use the path map to create the path from the explorer's current position to another spot: self.path = libtcod.path_new_using_map(self.path_map) random_destination_x, random_destination_y = choose_random_unblocked_spot(mymap) libtcod.path_compute(self.path, self.owner.x, self.owner.y, random_destination_x, random_destination_y) originx, originy = libtcod.path_get_origin(self.path) destx, desty = libtcod.path_get_destination(self.path)
def render(mouse): libtcod.console_clear(0) libtcod.console_clear(unit_panel.console) main_map.draw(0, cam) #unit_panel rendering #unit rendering libtcod.console_set_alignment(0, libtcod.CENTER) libtcod.console_set_default_background(0, libtcod.black) for u in main_map.units: u.draw(0,cam) #Draw name function if (u.x == mouse.cx + cam.x and u.y == mouse.cy + cam.y) or u == main_map.selected_unit: libtcod.console_print(0, u.x - cam.x, u.y - cam.y -1, u.name) #Draw the destination if moving x,y = libtcod.path_get_destination(u.path) if not libtcod.path_is_empty(u.path): libtcod.console_set_char(0, x - cam.x, y - cam.y, libtcod.CHAR_ARROW_S) unit_panel.draw(main_map)