ScummVM API documentation
route_finder_jps.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 #include "common/std/queue.h"
23 #include "common/std/vector.h"
24 #include "common/std/algorithm.h"
25 #include "common/std/functional.h"
26 #include "common/std/xutility.h"
27 
28 // Not all platforms define INFINITY
29 #ifndef INFINITY
30 #define INFINITY ((float)(1e+300 * 1e+300)) // This must overflow
31 #endif
32 
33 namespace AGS3 {
34 
35 // TODO: this could be cleaned up/simplified ...
36 
37 // further optimizations possible:
38 // - forward refinement should use binary search
39 
40 class Navigation {
41 public:
42  Navigation();
43 
44  void Resize(int width, int height);
45 
46  enum NavResult {
47  // unreachable
48  NAV_UNREACHABLE,
49  // straight line exists
50  NAV_STRAIGHT,
51  // path used
52  NAV_PATH
53  };
54 
55  // ncpath = navpoint-compressed path
56  // opath = path composed of individual grid elements
57  NavResult NavigateRefined(int sx, int sy, int ex, int ey, std::vector<int> &opath,
58  std::vector<int> &ncpath);
59 
60  NavResult Navigate(int sx, int sy, int ex, int ey, std::vector<int> &opath);
61 
62  bool TraceLine(int srcx, int srcy, int targx, int targy, int &lastValidX, int &lastValidY) const;
63  bool TraceLine(int srcx, int srcy, int targx, int targy, std::vector<int> *rpath = nullptr) const;
64 
65  inline void SetMapRow(int y, const unsigned char *row) {
66  map[y] = row;
67  }
68 
69  static int PackSquare(int x, int y);
70  static void UnpackSquare(int sq, int &x, int &y);
71 
72 private:
73  // priority queue entry
74  struct Entry {
75  float cost;
76  int index;
77 
78  inline Entry() {}
79 
80  inline Entry(float ncost, int nindex)
81  : cost(ncost)
82  , index(nindex) {
83  }
84 
85  inline bool operator <(const Entry &b) const {
86  return cost < b.cost;
87  }
88 
89  inline bool operator >(const Entry &b) const {
90  return cost > b.cost;
91  }
92  };
93 
94  int mapWidth;
95  int mapHeight;
97 
98  typedef unsigned short tFrameId;
99  typedef int tPrev;
100 
101  struct NodeInfo {
102  // quantized min distance from origin
103  unsigned short dist;
104  // frame id (counter to detect new search)
105  tFrameId frameId;
106  // previous node index (packed, relative to current node)
107  tPrev prev;
108 
109  inline NodeInfo()
110  : dist(0)
111  , frameId(0)
112  , prev(-1) {
113  }
114  };
115 
116  static const float DIST_SCALE_PACK;
117  static const float DIST_SCALE_UNPACK;
118 
119  std::vector<NodeInfo> mapNodes;
120  tFrameId frameId;
121 
123 
124  // temporary buffers:
125  mutable std::vector<int> fpath;
126  std::vector<int> ncpathIndex;
127  std::vector<int> rayPath, orayPath;
128 
129  // temps for routing towards unreachable areas
130  int cnode;
131  int closest;
132 
133  // orthogonal only (this should correspond to what AGS is doing)
134  bool nodiag;
135 
136  bool navLock;
137 
138  void IncFrameId();
139 
140  // outside map test
141  inline bool Outside(int x, int y) const;
142  // stronger inside test
143  bool Passable(int x, int y) const;
144  // plain access, unchecked
145  inline bool Walkable(int x, int y) const;
146 
147  void AddPruned(int *buf, int &bcount, int x, int y) const;
148  bool HasForcedNeighbor(int x, int y, int dx, int dy) const;
149  int FindJump(int x, int y, int dx, int dy, int ex, int ey);
150  int FindOrthoJump(int x, int y, int dx, int dy, int ex, int ey);
151 
152  // neighbor reachable (nodiag only)
153  bool Reachable(int x0, int y0, int x1, int y1) const;
154 
155  static inline int sign(int n) {
156  return n < 0 ? -1 : (n > 0 ? 1 : 0);
157  }
158 
159  static inline int iabs(int n) {
160  return n < 0 ? -n : n;
161  }
162 
163  static inline int iclamp(int v, int min, int max) {
164  return v < min ? min : (v > max ? max : v);
165  }
166 
167  static inline int ClosestDist(int dx, int dy) {
168  return dx * dx + dy * dy;
169  // Manhattan?
170  //return iabs(dx) + iabs(dy);
171  }
172 };
173 
174 inline int Navigation::PackSquare(int x, int y) {
175  return (y << 16) + x;
176 }
177 
178 inline void Navigation::UnpackSquare(int sq, int &x, int &y) {
179  y = sq >> 16;
180  x = sq & ((1 << 16) - 1);
181 }
182 
183 inline bool Navigation::Outside(int x, int y) const {
184  return
185  (unsigned)x >= (unsigned)mapWidth ||
186  (unsigned)y >= (unsigned)mapHeight;
187 }
188 
189 inline bool Navigation::Walkable(int x, int y) const {
190  // invert condition because of AGS
191  return map[y][x] != 0;
192 }
193 
194 } // namespace AGS3
Definition: queue.h:49
Definition: route_finder_jps.h:40
Definition: map.h:40
Definition: ags.h:40