libtcod
Loading...
Searching...
No Matches
path.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2025, Jice and the libtcod contributors.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
34#pragma once
35#ifndef TCOD_PATH_HPP_
36#define TCOD_PATH_HPP_
37
38#include <utility>
39
40#include "fov.hpp"
41#include "path.h"
42
45
46
49class TCODLIB_API ITCODPathCallback {
50 public:
51 virtual ~ITCODPathCallback() = default;
52 virtual float getWalkCost(int xFrom, int yFrom, int xTo, int yTo, void* userData) const = 0;
53};
54
55// clang-format off
72class TCODLIB_API TCODPath {
73public :
120 TCODPath(const TCODMap *map, float diagonalCost=1.41f);
174 TCODPath(int width, int height, const ITCODPathCallback *listener, void *userData, float diagonalCost=1.41f);
175
176 TCODPath(const TCODPath&) = delete;
177 TCODPath& operator=(const TCODPath&) = delete;
178 TCODPath(TCODPath&& rhs) noexcept {
179 std::swap(data, rhs.data);
180 std::swap(cppData, rhs.cppData);
181 }
182 TCODPath& operator=(TCODPath&& rhs) noexcept {
183 std::swap(data, rhs.data);
184 std::swap(cppData, rhs.cppData);
185 return *this;
186 }
230 virtual ~TCODPath();
231
259 bool compute(int ox, int oy, int dx, int dy);
260
294 void reverse();
295
296
321 void getOrigin(int *x,int *y) const;
322 void getDestination(int *x,int *y) const;
323
342 int size() const;
343
395 void get(int index, int *x, int *y) const;
396
416 bool isEmpty() const;
417
492 bool walk(int *x, int *y, bool recalculateWhenNeeded);
493
494protected :
495 friend float TCOD_path_func(int xFrom, int yFrom, int xTo,int yTo, void *data);
496 TCOD_path_t data{};
497 struct WrapperData {
498 void *userData{};
499 const ITCODPathCallback *listener{};
500 } cppData;
501};
502
503//Dijkstra kit
504class TCODLIB_API TCODDijkstra {
505 public:
506 TCODDijkstra (TCODMap *map, float diagonalCost=1.41f);
507 TCODDijkstra (int width, int height, const ITCODPathCallback *listener, void *userData, float diagonalCost=1.41f);
508
509 TCODDijkstra(const TCODDijkstra&) = delete;
510 TCODDijkstra& operator=(const TCODDijkstra&) = delete;
511 TCODDijkstra(TCODDijkstra&& rhs) noexcept {
512 std::swap(data, rhs.data);
513 std::swap(cppData, rhs.cppData);
514 }
515 TCODDijkstra& operator=(TCODDijkstra&& rhs) noexcept {
516 std::swap(data, rhs.data);
517 std::swap(cppData, rhs.cppData);
518 return *this;
519 }
520
521 ~TCODDijkstra (void);
534 void compute (int rootX, int rootY);
535
566 bool setPath (int toX, int toY);
567
580 float getDistance (int x, int y);
581 bool walk (int *x, int *y);
582 bool isEmpty() const;
583 void reverse();
584 int size() const;
585 void get(int index, int *x, int *y) const;
586 private:
587 TCOD_dijkstra_t data{};
588 struct WrapperData {
589 void *userData{};
590 const ITCODPathCallback *listener{};
591 } cppData;
592};
593
594#endif // TCOD_PATH_HPP_
Callback class for defining pathfinding graph edges.
Definition path.hpp:49
void compute(int rootX, int rootY)
In case of Dijkstra, this works in a slightly different way. In order to be able to compute a path,...
bool setPath(int toX, int toY)
After the map is analyzed and all the distances from the root node are known, an unlimited number of ...
float getDistance(int x, int y)
You can get the distance of any set of coordinates from the root node: Note that if the coordinates x...
Definition fov.hpp:56
Definition path.hpp:72
void get(int index, int *x, int *y) const
You can get the coordinates of each point along the path :void TCODPath::get(int index,...
bool compute(int ox, int oy, int dx, int dy)
Once you created a TCODPath object, you can compute the path between two points:
virtual ~TCODPath()
To release the resources used by a path, destroy it with :TCODPath::~TCODPath() TCODDijkstra::~TCODDi...
void reverse()
Once you computed a path, you can exchange origin and destination :void TCODPath::reverse() void TCOD...
int size() const
You can get the number of steps needed to reach destination :int TCODPath::size() const int TCODDijks...
bool walk(int *x, int *y, bool recalculateWhenNeeded)
You can walk the path and go to the next step with : Note that walking the path consume one step (and...
void getOrigin(int *x, int *y) const
You can read the current origin and destination cells with getOrigin/getDestination....
TCODPath(int width, int height, const ITCODPathCallback *listener, void *userData, float diagonalCost=1.41f)
Since the walkable status of a cell may depend on a lot of parameters (the creature type,...
TCODPath(const TCODMap *map, float diagonalCost=1.41f)
First, you have to allocate a path using a map from the Field of view module.
bool isEmpty() const
If you want a creature to follow the path, a more convenient way is to walk the path : You know when ...
Field-of-view module.
Libtcod A* and Dijkstra pathfinders.
Definition path.hpp:497