ScummVM API documentation
shape.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 ALCACHOFA_SHAPE_H
23 #define ALCACHOFA_SHAPE_H
24 
25 #include "common/stream.h"
26 #include "common/array.h"
27 #include "common/stack.h"
28 #include "common/rect.h"
29 #include "common/span.h"
30 #include "common/util.h"
31 #include "math/vector2d.h"
32 
33 #include "alcachofa/common.h"
34 
35 namespace Alcachofa {
36 
37 struct EdgeDistances {
39 
40  float _edgeLength;
41  float _onEdge;
42  float _toEdge;
43 };
44 
45 struct Polygon {
46  uint _index;
48 
49  bool contains(Common::Point query) const;
50  bool intersectsEdge(uint startPointI, Common::Point a, Common::Point b) const;
51  EdgeDistances edgeDistances(uint startPointI, Common::Point query) const;
52  Common::Point closestPointTo(Common::Point query, float &distanceSqr) const;
53  inline Common::Point closestPointTo(Common::Point query) const {
54  float dummy;
55  return closestPointTo(query, dummy);
56  }
57  Common::Point midPoint() const;
58 };
59 
61  Common::Span<const uint8> _pointDepths;
62  int8 _order;
63 
65 
66  float depthAt(Common::Point query) const;
67  uint findSharedPoints(const PathFindingPolygon &other, Common::Span<SharedPoint> sharedPoints) const;
68 };
69 
71  Common::Span<const Color> _pointColors;
72 
73  Color colorAt(Common::Point query) const;
74 };
75 
76 template<class TShape, typename TPolygon>
78  using difference_type = uint;
79  using value_type = TPolygon;
81 
82  inline value_type operator*() const {
83  return _shape.at(_index);
84  }
85 
86  inline my_type &operator++() {
87  assert(_index < _shape.polygonCount());
88  _index++;
89  return *this;
90  }
91 
92  inline my_type operator++(int) {
93  assert(_index < _shape.polygonCount());
94  auto tmp = *this;
95  ++*this;
96  return tmp;
97  }
98 
99  inline bool operator==(const my_type &it) const {
100  return &this->_shape == &it._shape && this->_index == it._index;
101  }
102 
103  inline bool operator!=(const my_type &it) const {
104  return &this->_shape != &it._shape || this->_index != it._index;
105  }
106 
107 private:
108  friend typename Common::remove_const_t<TShape>;
109  PolygonIterator(const TShape &shape, uint index = 0)
110  : _shape(shape)
111  , _index(index) {
112  }
113 
114  const TShape &_shape;
115  uint _index;
116 };
117 
118 class Shape {
119 public:
121 
122  Shape();
123  Shape(Common::ReadStream &stream);
124 
125  inline Common::Point firstPoint() const { return _points.empty() ? Common::Point() : _points[0]; }
126  inline uint polygonCount() const { return _polygons.size(); }
127  inline bool empty() const { return polygonCount() == 0; }
128  inline iterator begin() const { return { *this, 0 }; }
129  inline iterator end() const { return { *this, polygonCount() }; }
130 
131  Polygon at(uint index) const;
132  int32 polygonContaining(Common::Point query) const;
133  bool contains(Common::Point query) const;
134  Common::Point closestPointTo(Common::Point query, int32 &polygonI) const;
135  inline Common::Point closestPointTo(Common::Point query) const {
136  int32 dummy;
137  return closestPointTo(query, dummy);
138  }
139  void setAsRectangle(const Common::Rect &rect);
140 
141 protected:
142  uint addPolygon(uint maxCount);
143 
145  Common::Array<PolygonRange> _polygons;
147 };
148 
159 class PathFindingShape final : public Shape {
160 public:
162  static constexpr const uint kPointsPerPolygon = 4;
163 
166 
167  inline iterator begin() const { return { *this, 0 }; }
168  inline iterator end() const { return { *this, polygonCount() }; }
169 
170  PathFindingPolygon at(uint index) const;
171  int8 orderAt(Common::Point query) const;
172  float depthAt(Common::Point query) const;
173  bool findPath(
174  Common::Point from,
175  Common::Point to,
176  Common::Stack<Common::Point> &path) const;
177  int32 edgeTarget(uint polygonI, uint pointI) const;
178  bool findEvadeTarget(
179  Common::Point centerTarget,
180  float depthScale,
181  float minDistSqr,
182  Common::Point &evadeTarget) const;
183 
184 private:
186 
187  void setupLinks();
188  void setupLinkPoint(
189  const PathFindingPolygon &outer,
190  const PathFindingPolygon &inner,
192  void setupLinkEdge(
193  const PathFindingPolygon &outer,
194  const PathFindingPolygon &inner,
195  LinkIndex outerP, LinkIndex innerP);
196  void initializeFloydWarshall();
197  void calculateFloydWarshall();
198  bool canGoStraightThrough(
199  Common::Point from,
200  Common::Point to,
201  int32 fromContaining, int32 toContaining) const;
202  void floydWarshallPath(
203  Common::Point from,
204  Common::Point to,
205  int32 fromContaining, int32 toContaining,
206  Common::Stack<Common::Point> &path) const;
207 
208  Common::Array<uint8> _pointDepths;
209  Common::Array<int8> _polygonOrders;
210 
216  Common::Array<Common::Point> _linkPoints;
222  struct LinkPolygonIndices {
223  LinkPolygonIndices();
224  LinkIndex _points[kPointsPerPolygon];
225  };
232  Common::Array<int32> _targetQuads;
233  Common::Array<uint> _distanceMatrix;
234  Common::Array<int32> _previousTarget;
235 };
236 
238 class FloorColorShape final : public Shape {
239 public:
241  static constexpr const uint kPointsPerPolygon = 4;
242 
243  FloorColorShape();
245 
246  inline iterator begin() const { return { *this, 0 }; }
247  inline iterator end() const { return { *this, polygonCount() }; }
248 
249  FloorColorPolygon at(uint index) const;
250  OptionalColor colorAt(Common::Point query) const;
251 
252 private:
253  Common::Array<Color> _pointColors;
254 };
255 
256 }
257 
258 #endif // ALCACHOFA_SHAPE_H
Definition: util.h:167
Definition: alcachofa.h:45
Definition: common.h:69
Definition: shape.h:77
Definition: shape.h:45
Definition: array.h:52
Path finding is based on the Shape class with the invariant that every polygon is a convex polygon wi...
Definition: shape.h:159
Definition: rect.h:524
Definition: shape.h:238
Definition: shape.h:70
Definition: rect.h:144
Definition: shape.h:118
Definition: stream.h:385
Definition: shape.h:60
Definition: shape.h:37