CorsixTH engine (the C++ part)
Open source implementation of Theme Hospital
Loading...
Searching...
No Matches
th_pathfind.h
Go to the documentation of this file.
1/*
2Copyright (c) 2009 Peter "Corsix" Cawley
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of
5this software and associated documentation files (the "Software"), to deal in
6the Software without restriction, including without limitation the rights to
7use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8of the Software, and to permit persons to whom the Software is furnished to do
9so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22
23#ifndef CORSIX_TH_TH_PATHFIND_H_
24#define CORSIX_TH_TH_PATHFIND_H_
25
26#include "config.h"
27
28#include <cstdint>
29#include <vector>
30
31#include "lua.hpp"
32
33class level_map;
36class pathfinder;
37enum class object_type : uint8_t;
38struct map_tile_flags;
39
41enum class travel_direction {
42 north = 0,
43 east = 1,
44 south = 2,
45 west = 3
46};
47
49struct path_node {
51
56
58 int x;
59
61 int y;
62
64
69
71
75 int guess;
76
78
81 size_t open_idx;
82
84 bool visited;
85
87
91 inline int value() const { return distance + guess; }
92};
93
96 public:
98 virtual ~abstract_pathfinder() = default;
99
101
107 path_node* init(const level_map* pMap, int iStartX, int iStarty);
108
110
117
120 bool passable, path_node* pNeighbour);
121
123
126 virtual int guess_distance(path_node* pNode) = 0;
127
129
137 path_node* pNeighbour, travel_direction direction) = 0;
138
139 protected:
141 const level_map* map;
142};
143
145 public:
148
149 int guess_distance(path_node* pNode) override;
151 travel_direction direction) override;
152
153 bool find_path(const level_map* pMap, int iStartX, int iStartY, int iEndX,
154 int iEndY);
155
158};
159
161 public:
163
164 int guess_distance(path_node* pNode) override;
166 travel_direction direction) override;
167
168 bool find_path_to_hospital(const level_map* pMap, int iStartX, int iStartY);
169};
170
172 public:
175 best_next_node(nullptr),
176 best_distance(0),
177 start_x(0),
178 start_y(0) {}
179
180 int guess_distance(path_node* pNode) override;
182 travel_direction direction) override;
183
184 //| Find a tile for idling.
194 bool find_idle_tile(const level_map* pMap, int iStartX, int iStartY, int iN,
195 int parcelId);
196
201};
202
227
229
244 public:
245 pathfinder();
246 ~pathfinder();
247
248 void set_default_map(const level_map* pMap);
249
250 inline bool find_path(const level_map* pMap, int iStartX, int iStartY,
251 int iEndX, int iEndY) {
253 }
254
255 inline bool find_idle_tile(const level_map* pMap, int iStartX, int iStartY,
256 int iN, int parcelId) {
258 parcelId);
259 }
260
265
273
274 int get_path_length() const;
275 bool get_path_end(int* pX, int* pY) const;
276 void push_result(lua_State* L) const;
277
280
282
286 void allocate_node_cache(int iWidth, int iHeight);
287
291
293
295 std::vector<path_node> nodes;
296
298
303
305
311 std::vector<path_node*> open_heap;
312
317
318 private:
323};
324
325#endif // CORSIX_TH_TH_PATHFIND_H_
Definition th_pathfind.h:95
const level_map * map
Map being searched.
Definition th_pathfind.h:141
void record_neighbour_if_passable(path_node *pNode, map_tile_flags neighbour_flags, bool passable, path_node *pNeighbour)
Definition th_pathfind.cpp:91
virtual int guess_distance(path_node *pNode)=0
Guess distance to the destination for pNode.
virtual ~abstract_pathfinder()=default
virtual bool try_node(path_node *pNode, map_tile_flags flags, path_node *pNeighbour, travel_direction direction)=0
Try the pNeighbour node.
path_node * init(const level_map *pMap, int iStartX, int iStarty)
Initialize the path finder.
Definition th_pathfind.cpp:42
bool search_neighbours(path_node *pNode, map_tile_flags flags, int iWidth)
Expand the pNode to its neighbours.
Definition th_pathfind.cpp:62
pathfinder * parent
Path finder parent object, containing shared data.
Definition th_pathfind.h:140
Definition th_pathfind.h:144
int destination_x
X coordinate of the destination of the path.
Definition th_pathfind.h:156
int destination_y
Y coordinate of the destination of the path.
Definition th_pathfind.h:157
int guess_distance(path_node *pNode) override
Guess distance to the destination for pNode.
Definition th_pathfind.cpp:115
bool try_node(path_node *pNode, map_tile_flags flags, path_node *pNeighbour, travel_direction direction) override
Try the pNeighbour node.
Definition th_pathfind.cpp:121
basic_pathfinder(pathfinder *pf)
Definition th_pathfind.h:146
bool find_path(const level_map *pMap, int iStartX, int iStartY, int iEndX, int iEndY)
Definition th_pathfind.cpp:131
Definition th_pathfind.h:160
hospital_finder(pathfinder *pf)
Definition th_pathfind.h:162
bool find_path_to_hospital(const level_map *pMap, int iStartX, int iStartY)
Definition th_pathfind.cpp:183
int guess_distance(path_node *pNode) override
Guess distance to the destination for pNode.
Definition th_pathfind.cpp:171
bool try_node(path_node *pNode, map_tile_flags flags, path_node *pNeighbour, travel_direction direction) override
Try the pNeighbour node.
Definition th_pathfind.cpp:173
Definition th_pathfind.h:171
double best_distance
Definition th_pathfind.h:198
bool try_node(path_node *pNode, map_tile_flags flags, path_node *pNeighbour, travel_direction direction) override
Try the pNeighbour node.
Definition th_pathfind.cpp:224
int guess_distance(path_node *pNode) override
Guess distance to the destination for pNode.
Definition th_pathfind.cpp:222
int start_x
X coordinate of the start position.
Definition th_pathfind.h:199
idle_tile_finder(pathfinder *pf)
Definition th_pathfind.h:173
path_node * best_next_node
Definition th_pathfind.h:197
bool find_idle_tile(const level_map *pMap, int iStartX, int iStartY, int iN, int parcelId)
Definition th_pathfind.cpp:277
int start_y
Y coordinate of the start position.
Definition th_pathfind.h:200
Definition th_map.h:252
Interface used for depersisting Lua objects.
Definition persist_lua.h:107
Interface used for persisting Lua objects.
Definition persist_lua.h:44
Definition th_pathfind.h:203
int guess_distance(path_node *pNode) override
Guess distance to the destination for pNode.
Definition th_pathfind.cpp:347
int max_distance
Definition th_pathfind.h:223
object_type target
Definition th_pathfind.h:225
bool visit_objects(const level_map *pMap, int iStartX, int iStartY, object_type eTHOB, int iMaxDistance, lua_State *L, int iVisitFunction, bool anyObjectType)
Definition th_pathfind.cpp:421
bool target_any_object_type
Definition th_pathfind.h:224
bool try_node(path_node *pNode, map_tile_flags flags, path_node *pNeighbour, travel_direction direction) override
Try the pNeighbour node.
Definition th_pathfind.cpp:349
int visit_function_index
Definition th_pathfind.h:222
lua_State * L
Definition th_pathfind.h:221
object_visitor(pathfinder *pf)
Definition th_pathfind.h:205
Finds paths through maps.
Definition th_pathfind.h:243
int dirty_node_count
Definition th_pathfind.h:316
int node_cache_height
Definition th_pathfind.h:315
~pathfinder()
Definition th_pathfind.cpp:473
void open_heap_promote(path_node *pNode)
Definition th_pathfind.cpp:560
std::vector< path_node * > open_heap
Heap of not yet evaluated nodes as a 0-based array.
Definition th_pathfind.h:311
std::vector< path_node > nodes
2D array of nodes, one for each map cell
Definition th_pathfind.h:295
path_node ** dirty_node_list
Array of "dirty" nodes which need to be reset before the next path find.
Definition th_pathfind.h:302
int node_cache_width
Definition th_pathfind.h:314
const level_map * default_map
Definition th_pathfind.h:292
void depersist(lua_persist_reader *pReader)
Definition th_pathfind.cpp:641
void push_result(lua_State *L) const
Definition th_pathfind.cpp:533
bool get_path_end(int *pX, int *pY) const
Definition th_pathfind.cpp:514
bool find_idle_tile(const level_map *pMap, int iStartX, int iStartY, int iN, int parcelId)
Definition th_pathfind.h:255
path_node * pop_from_open_heap()
Definition th_pathfind.cpp:581
void persist(lua_persist_writer *pWriter) const
Definition th_pathfind.cpp:627
bool visit_objects(const level_map *pMap, int iStartX, int iStartY, object_type eTHOB, int iMaxDistance, lua_State *L, int iVisitFunction, bool anyObjectType)
Definition th_pathfind.h:266
bool find_path_to_hospital(const level_map *pMap, int iStartX, int iStartY)
Definition th_pathfind.h:261
bool find_path(const level_map *pMap, int iStartX, int iStartY, int iEndX, int iEndY)
Definition th_pathfind.h:250
path_node * destination
Definition th_pathfind.h:313
void set_default_map(const level_map *pMap)
Definition th_pathfind.cpp:475
pathfinder()
Definition th_pathfind.cpp:460
void allocate_node_cache(int iWidth, int iHeight)
Allocate node cache for all tiles of the map.
Definition th_pathfind.cpp:477
int get_path_length() const
Definition th_pathfind.cpp:506
void push_to_open_heap(path_node *pNode)
Definition th_pathfind.cpp:554
Definition th_map.h:118
Definition th_pathfind.h:49
int y
Y-position of this cell (constant)
Definition th_pathfind.h:61
int value() const
Total cost of this node.
Definition th_pathfind.h:91
const path_node * prev
Pointer to the previous node in the path to this cell.
Definition th_pathfind.h:55
bool visited
True if the cell has already been visited (popped from the open heap)
Definition th_pathfind.h:84
int x
X-position of this cell (constant)
Definition th_pathfind.h:58
int distance
Current shortest distance to this cell.
Definition th_pathfind.h:68
int guess
Minimum distance from this cell to the goal.
Definition th_pathfind.h:75
size_t open_idx
Index of this cell in the open heap.
Definition th_pathfind.h:81
object_type
Definition th_map.h:46
travel_direction
Definition th_pathfind.h:41
@ east
Move to the east.
@ south
Move to the south.
@ north
Move to the north.
@ west
Move to the west.