ScummVM API documentation
pathfinding.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef MACS2_PATHFINDING_H
23 #define MACS2_PATHFINDING_H
24 
25 #include "common/array.h"
26 #include "common/rect.h"
27 #include "graphics/managed_surface.h"
28 #include "macs2/macs2_constants.h"
29 
30 namespace Macs2 {
31 
32 // Area override table at scene+0x4EA8 (indexed by pathfinding value 0xC8..0xEF)
33 // Set by opcode 0x4D, read by getAreaAtPoint (1008:101d)
34 static constexpr uint16 AREA_OVERRIDE_MIN = 200;
35 static constexpr uint16 AREA_OVERRIDE_MAX = 239;
36 static constexpr uint16 AREA_OVERRIDE_COUNT = AREA_OVERRIDE_MAX - AREA_OVERRIDE_MIN + 1;
37 
39  uint8 _index = 0;
40  Common::Point _position;
41  Common::Array<uint8> _adjacentPoints;
42 };
43 
45  bool _active = false;
46  uint16 _index = 0;
47  uint16 _overrideValue = 0;
48 };
49 
50 struct PathRoute {
52  int16 startIndex = 0;
53  Common::Point firstWaypoint;
54  bool found = false;
55 };
56 
57 class Pathfinding {
58 public:
62  uint16 _areaOverrides[AREA_OVERRIDE_COUNT] = {0};
63  uint16 _numPoints = 0;
64 
65  void createMap(int width, int height);
66  void clearWalkOverrides();
67  void clearAreaOverrides();
68 
69  // Walkability threshold 0xC8 uses signed 16-bit comparison in the binary (JL/JGE).
70  // Values with (int16)value < 0xC8 are walkable heights; e.g. -2 (0xFFFE) is walkable.
71  static inline bool isWalkabilityBlocking(uint16 value) {
72  return (int16)value >= 0xC8;
73  }
74  static inline bool isWalkabilityWalkable(uint16 value) {
75  return (int16)value < 0xC8;
76  }
77 
78  uint16 walkabilityAt(int16 y, int16 x) const;
79  uint16 walkabilityAt(const Common::Point &p) const;
80  uint16 areaAt(uint16 x, uint16 y) const;
81 
82  bool getWalkOverride(uint16 index, uint16 &result) const;
83  void setWalkOverride(uint16 index, uint16 overrideValue);
84  void removeWalkOverride(uint16 index);
85 
86  bool isLineWalkable(int16 y1, int16 x1, int16 y2, int16 x2) const;
87  void snapToWalkable(int16 *pTargetY, int16 *pTargetX, int16 charY, int16 charX) const;
88 
89  int nodeCount() const { return (int)_numPoints; }
90  int euclideanDistance(const Common::Point &a, const Common::Point &b) const;
91  int walkableDistance(int nodeA, int nodeB) const;
92 
93  PathRoute calculateRoute(const Common::Point &from, const Common::Point &to);
94 
95 private:
96  int _visitedStack[17] {};
97  int _visitedCount = 0;
98  uint16 areaOverrideAt(uint16 index) const;
99 
100  int computeMinCostToReachable(int nodeIndex, int prevNode, const bool *reachable, int nodeCount, const Common::Point &finalDest);
101  bool canNodeConnectSourceToTarget(uint16 nodeIndex, const Common::Point &charPos, const Common::Point &target, const bool *reachable, int nodeCount) const;
102  void floodFillConnectedNodes(int nodeIndex, bool *visited, int nodeCount) const;
103 };
104 
105 } // namespace Macs2
106 
107 #endif
Definition: managed_surface.h:51
Definition: pathfinding.h:44
Definition: rect.h:144
Definition: pathfinding.h:57
Definition: actionbar.h:31
Definition: pathfinding.h:50
Definition: pathfinding.h:38