Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "ICmpObstructionManager.h"
21 :
22 : #include "graphics/Color.h"
23 : #include "graphics/Overlay.h"
24 : #include "lib/debug.h"
25 : #include "maths/Fixed.h"
26 : #include "maths/FixedVector2D.h"
27 : #include "maths/MathUtil.h"
28 : #include "ps/Profile.h"
29 : #include "renderer/Scene.h"
30 : #include "simulation2/MessageTypes.h"
31 : #include "simulation2/components/ICmpObstruction.h"
32 : #include "simulation2/components/ICmpPathfinder.h"
33 : #include "simulation2/components/ICmpPosition.h"
34 : #include "simulation2/components/ICmpRangeManager.h"
35 : #include "simulation2/helpers/Geometry.h"
36 : #include "simulation2/helpers/Grid.h"
37 : #include "simulation2/helpers/Pathfinding.h"
38 : #include "simulation2/helpers/Position.h"
39 : #include "simulation2/helpers/Rasterize.h"
40 : #include "simulation2/helpers/Render.h"
41 : #include "simulation2/helpers/Spatial.h"
42 : #include "simulation2/serialization/SerializeTemplates.h"
43 : #include "simulation2/serialization/SerializedTypes.h"
44 : #include "simulation2/system/Component.h"
45 : #include "simulation2/system/Entity.h"
46 : #include "simulation2/system/Message.h"
47 :
48 : #include <algorithm>
49 : #include <cmath>
50 : #include <cstddef>
51 : #include <map>
52 : #include <string>
53 : #include <type_traits>
54 : #include <utility>
55 : #include <vector>
56 :
57 : // Externally, tags are opaque non-zero positive integers.
58 : // Internally, they are tagged (by shape) indexes into shape lists.
59 : // idx must be non-zero.
60 : #define TAG_IS_VALID(tag) ((tag).valid())
61 : #define TAG_IS_UNIT(tag) (((tag).n & 1) == 0)
62 : #define TAG_IS_STATIC(tag) (((tag).n & 1) == 1)
63 : #define UNIT_INDEX_TO_TAG(idx) tag_t(((idx) << 1) | 0)
64 : #define STATIC_INDEX_TO_TAG(idx) tag_t(((idx) << 1) | 1)
65 : #define TAG_TO_INDEX(tag) ((tag).n >> 1)
66 :
67 : namespace
68 : {
69 : /**
70 : * Size of each obstruction subdivision square.
71 : * TODO: find the optimal number instead of blindly guessing.
72 : */
73 : constexpr entity_pos_t OBSTRUCTION_SUBDIVISION_SIZE = entity_pos_t::FromInt(32);
74 :
75 : /**
76 : * Internal representation of axis-aligned circular shapes for moving units
77 : */
78 0 : struct UnitShape
79 : {
80 : entity_id_t entity;
81 : entity_pos_t x, z;
82 : entity_pos_t clearance;
83 : ICmpObstructionManager::flags_t flags;
84 : entity_id_t group; // control group (typically the owner entity, or a formation controller entity) (units ignore collisions with others in the same group)
85 : };
86 :
87 : /**
88 : * Internal representation of arbitrary-rotation static square shapes for buildings
89 : */
90 0 : struct StaticShape
91 : {
92 : entity_id_t entity;
93 : entity_pos_t x, z; // world-space coordinates
94 : CFixedVector2D u, v; // orthogonal unit vectors - axes of local coordinate space
95 : entity_pos_t hw, hh; // half width/height in local coordinate space
96 : ICmpObstructionManager::flags_t flags;
97 : entity_id_t group;
98 : entity_id_t group2;
99 : };
100 : } // anonymous namespace
101 : /**
102 : * Serialization helper template for UnitShape
103 : */
104 : template<>
105 : struct SerializeHelper<UnitShape>
106 : {
107 : template<typename S>
108 1020 : void operator()(S& serialize, const char* /*name*/, Serialize::qualify<S, UnitShape> value) const
109 : {
110 1020 : serialize.NumberU32_Unbounded("entity", value.entity);
111 1020 : serialize.NumberFixed_Unbounded("x", value.x);
112 1020 : serialize.NumberFixed_Unbounded("z", value.z);
113 1020 : serialize.NumberFixed_Unbounded("clearance", value.clearance);
114 1020 : serialize.NumberU8_Unbounded("flags", value.flags);
115 1020 : serialize.NumberU32_Unbounded("group", value.group);
116 1020 : }
117 : };
118 :
119 : /**
120 : * Serialization helper template for StaticShape
121 : */
122 : template<>
123 : struct SerializeHelper<StaticShape>
124 : {
125 : template<typename S>
126 12 : void operator()(S& serialize, const char* /*name*/, Serialize::qualify<S, StaticShape> value) const
127 : {
128 12 : serialize.NumberU32_Unbounded("entity", value.entity);
129 12 : serialize.NumberFixed_Unbounded("x", value.x);
130 12 : serialize.NumberFixed_Unbounded("z", value.z);
131 12 : serialize.NumberFixed_Unbounded("u.x", value.u.X);
132 12 : serialize.NumberFixed_Unbounded("u.y", value.u.Y);
133 12 : serialize.NumberFixed_Unbounded("v.x", value.v.X);
134 12 : serialize.NumberFixed_Unbounded("v.y", value.v.Y);
135 12 : serialize.NumberFixed_Unbounded("hw", value.hw);
136 12 : serialize.NumberFixed_Unbounded("hh", value.hh);
137 12 : serialize.NumberU8_Unbounded("flags", value.flags);
138 12 : serialize.NumberU32_Unbounded("group", value.group);
139 12 : serialize.NumberU32_Unbounded("group2", value.group2);
140 12 : }
141 : };
142 :
143 16 : class CCmpObstructionManager final : public ICmpObstructionManager
144 : {
145 : public:
146 137 : static void ClassInit(CComponentManager& componentManager)
147 : {
148 137 : componentManager.SubscribeToMessageType(MT_RenderSubmit); // for debug overlays
149 : }
150 :
151 32 : DEFAULT_COMPONENT_ALLOCATOR(ObstructionManager)
152 :
153 : bool m_DebugOverlayEnabled;
154 : bool m_DebugOverlayDirty;
155 : std::vector<SOverlayLine> m_DebugOverlayLines;
156 :
157 : SpatialSubdivision m_UnitSubdivision;
158 : SpatialSubdivision m_StaticSubdivision;
159 :
160 : // TODO: using std::map is a bit inefficient; is there a better way to store these?
161 : std::map<std::uint32_t, UnitShape> m_UnitShapes;
162 : std::map<std::uint32_t, StaticShape> m_StaticShapes;
163 : std::uint32_t m_UnitShapeNext; // next allocated id
164 : std::uint32_t m_StaticShapeNext;
165 :
166 : entity_pos_t m_MaxClearance;
167 :
168 : bool m_PassabilityCircular;
169 :
170 : entity_pos_t m_WorldX0;
171 : entity_pos_t m_WorldZ0;
172 : entity_pos_t m_WorldX1;
173 : entity_pos_t m_WorldZ1;
174 :
175 137 : static std::string GetSchema()
176 : {
177 274 : return "<a:component type='system'/><empty/>";
178 : }
179 :
180 16 : void Init(const CParamNode&) override
181 : {
182 16 : m_DebugOverlayEnabled = false;
183 16 : m_DebugOverlayDirty = true;
184 :
185 16 : m_UnitShapeNext = 1;
186 16 : m_StaticShapeNext = 1;
187 :
188 16 : m_UpdateInformations.dirty = true;
189 16 : m_UpdateInformations.globallyDirty = true;
190 :
191 16 : m_PassabilityCircular = false;
192 :
193 16 : m_WorldX0 = m_WorldZ0 = m_WorldX1 = m_WorldZ1 = entity_pos_t::Zero();
194 :
195 : // Initialise with bogus values (these will get replaced when
196 : // SetBounds is called)
197 16 : ResetSubdivisions(entity_pos_t::FromInt(1024), entity_pos_t::FromInt(1024));
198 16 : }
199 :
200 16 : void Deinit() override
201 : {
202 16 : }
203 :
204 : template<typename S>
205 18 : void SerializeCommon(S& serialize)
206 : {
207 18 : Serializer(serialize, "unit subdiv", m_UnitSubdivision);
208 18 : Serializer(serialize, "static subdiv", m_StaticSubdivision);
209 :
210 18 : serialize.NumberFixed_Unbounded("max clearance", m_MaxClearance);
211 :
212 18 : Serializer(serialize, "unit shapes", m_UnitShapes);
213 18 : Serializer(serialize, "static shapes", m_StaticShapes);
214 18 : serialize.NumberU32_Unbounded("unit shape next", m_UnitShapeNext);
215 18 : serialize.NumberU32_Unbounded("static shape next", m_StaticShapeNext);
216 :
217 18 : serialize.Bool("circular", m_PassabilityCircular);
218 :
219 18 : serialize.NumberFixed_Unbounded("world x0", m_WorldX0);
220 18 : serialize.NumberFixed_Unbounded("world z0", m_WorldZ0);
221 18 : serialize.NumberFixed_Unbounded("world x1", m_WorldX1);
222 18 : serialize.NumberFixed_Unbounded("world z1", m_WorldZ1);
223 18 : }
224 :
225 18 : void Serialize(ISerializer& serialize) override
226 : {
227 : // TODO: this could perhaps be optimised by not storing all the obstructions,
228 : // and instead regenerating them from the other entities on Deserialize
229 :
230 18 : SerializeCommon(serialize);
231 18 : }
232 :
233 0 : void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) override
234 : {
235 0 : Init(paramNode);
236 :
237 0 : SerializeCommon(deserialize);
238 :
239 0 : std::int32_t size = ((m_WorldX1-m_WorldX0)/Pathfinding::NAVCELL_SIZE_INT).ToInt_RoundToInfinity();
240 0 : m_UpdateInformations.dirtinessGrid = Grid<std::uint8_t>(size, size);
241 0 : }
242 :
243 0 : void HandleMessage(const CMessage& msg, bool /*global*/) override
244 : {
245 0 : switch (msg.GetType())
246 : {
247 0 : case MT_RenderSubmit:
248 0 : {
249 0 : const CMessageRenderSubmit& msgData = static_cast<const CMessageRenderSubmit&> (msg);
250 0 : RenderSubmit(msgData.collector);
251 0 : break;
252 : }
253 : }
254 0 : }
255 :
256 : // NB: on deserialization, this function is not called after the component is reset.
257 : // So anything that happens here should be safely serialized.
258 12 : void SetBounds(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1) override
259 : {
260 12 : m_WorldX0 = x0;
261 12 : m_WorldZ0 = z0;
262 12 : m_WorldX1 = x1;
263 12 : m_WorldZ1 = z1;
264 24 : MakeDirtyAll();
265 :
266 : // Subdivision system bounds:
267 12 : ENSURE(x0.IsZero() && z0.IsZero()); // don't bother implementing non-zero offsets yet
268 12 : ResetSubdivisions(x1, z1);
269 :
270 12 : std::int32_t size = ((m_WorldX1-m_WorldX0)/Pathfinding::NAVCELL_SIZE_INT).ToInt_RoundToInfinity();
271 24 : m_UpdateInformations.dirtinessGrid = Grid<std::uint8_t>(size, size);
272 :
273 12 : CmpPtr<ICmpPathfinder> cmpPathfinder(GetSystemEntity());
274 12 : if (cmpPathfinder)
275 3 : m_MaxClearance = cmpPathfinder->GetMaximumClearance();
276 12 : }
277 :
278 28 : void ResetSubdivisions(entity_pos_t x1, entity_pos_t z1)
279 : {
280 28 : m_UnitSubdivision.Reset(x1, z1, OBSTRUCTION_SUBDIVISION_SIZE);
281 28 : m_StaticSubdivision.Reset(x1, z1, OBSTRUCTION_SUBDIVISION_SIZE);
282 :
283 28 : for (auto it = m_UnitShapes.begin(); it != m_UnitShapes.end(); ++it)
284 : {
285 0 : CFixedVector2D center(it->second.x, it->second.z);
286 0 : CFixedVector2D halfSize(it->second.clearance, it->second.clearance);
287 0 : m_UnitSubdivision.Add(it->first, center - halfSize, center + halfSize);
288 : }
289 :
290 28 : for (auto it = m_StaticShapes.begin(); it != m_StaticShapes.end(); ++it)
291 : {
292 0 : CFixedVector2D center(it->second.x, it->second.z);
293 0 : CFixedVector2D bbHalfSize = Geometry::GetHalfBoundingBox(it->second.u, it->second.v, CFixedVector2D(it->second.hw, it->second.hh));
294 0 : m_StaticSubdivision.Add(it->first, center - bbHalfSize, center + bbHalfSize);
295 : }
296 28 : }
297 :
298 198 : tag_t AddUnitShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_pos_t clearance, flags_t flags, entity_id_t group) override
299 : {
300 198 : UnitShape shape = { ent, x, z, clearance, flags, group };
301 198 : std::uint32_t id = m_UnitShapeNext++;
302 198 : m_UnitShapes[id] = shape;
303 :
304 198 : m_UnitSubdivision.Add(id, CFixedVector2D(x - clearance, z - clearance), CFixedVector2D(x + clearance, z + clearance));
305 :
306 198 : MakeDirtyUnit(flags, id, shape);
307 :
308 198 : return UNIT_INDEX_TO_TAG(id);
309 : }
310 :
311 18 : tag_t AddStaticShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_angle_t a, entity_pos_t w, entity_pos_t h, flags_t flags, entity_id_t group, entity_id_t group2 /* = INVALID_ENTITY */) override
312 : {
313 18 : fixed s, c;
314 18 : sincos_approx(a, s, c);
315 18 : CFixedVector2D u(c, -s);
316 18 : CFixedVector2D v(s, c);
317 :
318 18 : StaticShape shape = { ent, x, z, u, v, w/2, h/2, flags, group, group2 };
319 18 : std::uint32_t id = m_StaticShapeNext++;
320 18 : m_StaticShapes[id] = shape;
321 :
322 18 : CFixedVector2D center(x, z);
323 18 : CFixedVector2D bbHalfSize = Geometry::GetHalfBoundingBox(u, v, CFixedVector2D(w/2, h/2));
324 18 : m_StaticSubdivision.Add(id, center - bbHalfSize, center + bbHalfSize);
325 :
326 18 : MakeDirtyStatic(flags, id, shape);
327 :
328 18 : return STATIC_INDEX_TO_TAG(id);
329 : }
330 :
331 50100 : ObstructionSquare GetUnitShapeObstruction(entity_pos_t x, entity_pos_t z, entity_pos_t clearance) const override
332 : {
333 50100 : CFixedVector2D u(entity_pos_t::FromInt(1), entity_pos_t::Zero());
334 50100 : CFixedVector2D v(entity_pos_t::Zero(), entity_pos_t::FromInt(1));
335 50100 : ObstructionSquare o = { x, z, u, v, clearance, clearance };
336 50100 : return o;
337 : }
338 :
339 48 : ObstructionSquare GetStaticShapeObstruction(entity_pos_t x, entity_pos_t z, entity_angle_t a, entity_pos_t w, entity_pos_t h) const override
340 : {
341 48 : fixed s, c;
342 48 : sincos_approx(a, s, c);
343 48 : CFixedVector2D u(c, -s);
344 48 : CFixedVector2D v(s, c);
345 :
346 48 : ObstructionSquare o = { x, z, u, v, w/2, h/2 };
347 48 : return o;
348 : }
349 :
350 7716 : void MoveShape(tag_t tag, entity_pos_t x, entity_pos_t z, entity_angle_t a) override
351 : {
352 7716 : ENSURE(TAG_IS_VALID(tag));
353 :
354 7716 : if (TAG_IS_UNIT(tag))
355 : {
356 7710 : UnitShape& shape = m_UnitShapes[TAG_TO_INDEX(tag)];
357 :
358 7710 : MakeDirtyUnit(shape.flags, TAG_TO_INDEX(tag), shape); // dirty the old shape region
359 :
360 7710 : m_UnitSubdivision.Move(TAG_TO_INDEX(tag),
361 : CFixedVector2D(shape.x - shape.clearance, shape.z - shape.clearance),
362 : CFixedVector2D(shape.x + shape.clearance, shape.z + shape.clearance),
363 : CFixedVector2D(x - shape.clearance, z - shape.clearance),
364 : CFixedVector2D(x + shape.clearance, z + shape.clearance));
365 :
366 7710 : shape.x = x;
367 7710 : shape.z = z;
368 :
369 7710 : MakeDirtyUnit(shape.flags, TAG_TO_INDEX(tag), shape); // dirty the new shape region
370 : }
371 : else
372 : {
373 6 : fixed s, c;
374 6 : sincos_approx(a, s, c);
375 6 : CFixedVector2D u(c, -s);
376 6 : CFixedVector2D v(s, c);
377 :
378 6 : StaticShape& shape = m_StaticShapes[TAG_TO_INDEX(tag)];
379 :
380 6 : MakeDirtyStatic(shape.flags, TAG_TO_INDEX(tag), shape); // dirty the old shape region
381 :
382 6 : CFixedVector2D fromBbHalfSize = Geometry::GetHalfBoundingBox(shape.u, shape.v, CFixedVector2D(shape.hw, shape.hh));
383 6 : CFixedVector2D toBbHalfSize = Geometry::GetHalfBoundingBox(u, v, CFixedVector2D(shape.hw, shape.hh));
384 6 : m_StaticSubdivision.Move(TAG_TO_INDEX(tag),
385 6 : CFixedVector2D(shape.x, shape.z) - fromBbHalfSize,
386 6 : CFixedVector2D(shape.x, shape.z) + fromBbHalfSize,
387 6 : CFixedVector2D(x, z) - toBbHalfSize,
388 6 : CFixedVector2D(x, z) + toBbHalfSize);
389 :
390 6 : shape.x = x;
391 6 : shape.z = z;
392 6 : shape.u = u;
393 6 : shape.v = v;
394 :
395 6 : MakeDirtyStatic(shape.flags, TAG_TO_INDEX(tag), shape); // dirty the new shape region
396 : }
397 7716 : }
398 :
399 8118 : void SetUnitMovingFlag(tag_t tag, bool moving) override
400 : {
401 8118 : ENSURE(TAG_IS_VALID(tag) && TAG_IS_UNIT(tag));
402 :
403 8118 : if (TAG_IS_UNIT(tag))
404 : {
405 8118 : UnitShape& shape = m_UnitShapes[TAG_TO_INDEX(tag)];
406 8118 : if (moving)
407 7026 : shape.flags |= FLAG_MOVING;
408 : else
409 1092 : shape.flags &= (flags_t)~FLAG_MOVING;
410 :
411 16236 : MakeDirtyDebug();
412 : }
413 8118 : }
414 :
415 0 : void SetUnitControlGroup(tag_t tag, entity_id_t group) override
416 : {
417 0 : ENSURE(TAG_IS_VALID(tag) && TAG_IS_UNIT(tag));
418 :
419 0 : if (TAG_IS_UNIT(tag))
420 : {
421 0 : UnitShape& shape = m_UnitShapes[TAG_TO_INDEX(tag)];
422 0 : shape.group = group;
423 : }
424 0 : }
425 :
426 2 : void SetStaticControlGroup(tag_t tag, entity_id_t group, entity_id_t group2) override
427 : {
428 2 : ENSURE(TAG_IS_VALID(tag) && TAG_IS_STATIC(tag));
429 :
430 2 : if (TAG_IS_STATIC(tag))
431 : {
432 2 : StaticShape& shape = m_StaticShapes[TAG_TO_INDEX(tag)];
433 2 : shape.group = group;
434 2 : shape.group2 = group2;
435 : }
436 2 : }
437 :
438 30 : void RemoveShape(tag_t tag) override
439 : {
440 30 : ENSURE(TAG_IS_VALID(tag));
441 :
442 30 : if (TAG_IS_UNIT(tag))
443 : {
444 30 : UnitShape& shape = m_UnitShapes[TAG_TO_INDEX(tag)];
445 30 : m_UnitSubdivision.Remove(TAG_TO_INDEX(tag),
446 : CFixedVector2D(shape.x - shape.clearance, shape.z - shape.clearance),
447 : CFixedVector2D(shape.x + shape.clearance, shape.z + shape.clearance));
448 :
449 30 : MakeDirtyUnit(shape.flags, TAG_TO_INDEX(tag), shape);
450 :
451 30 : m_UnitShapes.erase(TAG_TO_INDEX(tag));
452 : }
453 : else
454 : {
455 0 : StaticShape& shape = m_StaticShapes[TAG_TO_INDEX(tag)];
456 :
457 0 : CFixedVector2D center(shape.x, shape.z);
458 0 : CFixedVector2D bbHalfSize = Geometry::GetHalfBoundingBox(shape.u, shape.v, CFixedVector2D(shape.hw, shape.hh));
459 0 : m_StaticSubdivision.Remove(TAG_TO_INDEX(tag), center - bbHalfSize, center + bbHalfSize);
460 :
461 0 : MakeDirtyStatic(shape.flags, TAG_TO_INDEX(tag), shape);
462 :
463 0 : m_StaticShapes.erase(TAG_TO_INDEX(tag));
464 : }
465 30 : }
466 :
467 8 : ObstructionSquare GetObstruction(tag_t tag) const override
468 : {
469 8 : ENSURE(TAG_IS_VALID(tag));
470 :
471 8 : if (TAG_IS_UNIT(tag))
472 : {
473 4 : const UnitShape& shape = m_UnitShapes.at(TAG_TO_INDEX(tag));
474 4 : CFixedVector2D u(entity_pos_t::FromInt(1), entity_pos_t::Zero());
475 4 : CFixedVector2D v(entity_pos_t::Zero(), entity_pos_t::FromInt(1));
476 4 : ObstructionSquare o = { shape.x, shape.z, u, v, shape.clearance, shape.clearance };
477 4 : return o;
478 : }
479 : else
480 : {
481 4 : const StaticShape& shape = m_StaticShapes.at(TAG_TO_INDEX(tag));
482 4 : ObstructionSquare o = { shape.x, shape.z, shape.u, shape.v, shape.hw, shape.hh };
483 4 : return o;
484 : }
485 : }
486 :
487 : fixed DistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const override;
488 : fixed MaxDistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const override;
489 : fixed DistanceToTarget(entity_id_t ent, entity_id_t target) const override;
490 : fixed MaxDistanceToTarget(entity_id_t ent, entity_id_t target) const override;
491 : fixed DistanceBetweenShapes(const ObstructionSquare& source, const ObstructionSquare& target) const override;
492 : fixed MaxDistanceBetweenShapes(const ObstructionSquare& source, const ObstructionSquare& target) const override;
493 :
494 : bool IsInPointRange(entity_id_t ent, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const override;
495 : bool IsInTargetRange(entity_id_t ent, entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const override;
496 : bool IsInTargetParabolicRange(entity_id_t ent, entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange, entity_pos_t yOrigin, bool opposite) const override;
497 : bool IsPointInPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange) const override;
498 : bool AreShapesInRange(const ObstructionSquare& source, const ObstructionSquare& target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const override;
499 :
500 : bool TestLine(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, bool relaxClearanceForUnits = false) const override;
501 : bool TestUnitLine(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, bool relaxClearanceForUnits) const override;
502 : bool TestStaticShape(const IObstructionTestFilter& filter, entity_pos_t x, entity_pos_t z, entity_pos_t a, entity_pos_t w, entity_pos_t h, std::vector<entity_id_t>* out) const override;
503 : bool TestUnitShape(const IObstructionTestFilter& filter, entity_pos_t x, entity_pos_t z, entity_pos_t r, std::vector<entity_id_t>* out) const override;
504 :
505 : void Rasterize(Grid<NavcellData>& grid, const std::vector<PathfinderPassability>& passClasses, bool fullUpdate) override;
506 : void GetObstructionsInRange(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, std::vector<ObstructionSquare>& squares) const override;
507 : void GetUnitObstructionsInRange(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, std::vector<ObstructionSquare>& squares) const override;
508 : void GetStaticObstructionsInRange(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, std::vector<ObstructionSquare>& squares) const override;
509 : void GetUnitsOnObstruction(const ObstructionSquare& square, std::vector<entity_id_t>& out, const IObstructionTestFilter& filter, bool strict = false) const override;
510 : void GetStaticObstructionsOnObstruction(const ObstructionSquare& square, std::vector<entity_id_t>& out, const IObstructionTestFilter& filter) const override;
511 :
512 3 : void SetPassabilityCircular(bool enabled) override
513 : {
514 3 : m_PassabilityCircular = enabled;
515 6 : MakeDirtyAll();
516 :
517 3 : CMessageObstructionMapShapeChanged msg;
518 3 : GetSimContext().GetComponentManager().BroadcastMessage(msg);
519 3 : }
520 :
521 9 : bool GetPassabilityCircular() const override
522 : {
523 9 : return m_PassabilityCircular;
524 : }
525 :
526 0 : void SetDebugOverlay(bool enabled) override
527 : {
528 0 : m_DebugOverlayEnabled = enabled;
529 0 : m_DebugOverlayDirty = true;
530 0 : if (!enabled)
531 0 : m_DebugOverlayLines.clear();
532 0 : }
533 :
534 : void RenderSubmit(SceneCollector& collector);
535 :
536 189 : void UpdateInformations(GridUpdateInformation& informations) override
537 : {
538 189 : if (!m_UpdateInformations.dirtinessGrid.blank())
539 186 : informations.MergeAndClear(m_UpdateInformations);
540 189 : }
541 :
542 : private:
543 : // Dynamic updates for the long-range pathfinder
544 : GridUpdateInformation m_UpdateInformations;
545 : // These vectors might contain shapes that were deleted
546 : std::vector<std::uint32_t> m_DirtyStaticShapes;
547 : std::vector<std::uint32_t> m_DirtyUnitShapes;
548 :
549 : /**
550 : * Mark all previous Rasterize()d grids as dirty, and the debug display.
551 : * Call this when the world bounds have changed.
552 : */
553 15 : void MakeDirtyAll()
554 : {
555 15 : m_UpdateInformations.dirty = true;
556 15 : m_UpdateInformations.globallyDirty = true;
557 15 : m_UpdateInformations.dirtinessGrid.reset();
558 :
559 15 : m_DebugOverlayDirty = true;
560 : }
561 :
562 : /**
563 : * Mark the debug display as dirty.
564 : * Call this when nothing has changed except a unit's 'moving' flag.
565 : */
566 8118 : void MakeDirtyDebug()
567 : {
568 8118 : m_DebugOverlayDirty = true;
569 8118 : }
570 :
571 0 : inline void MarkDirtinessGrid(const entity_pos_t& x, const entity_pos_t& z, const entity_pos_t& r)
572 : {
573 0 : MarkDirtinessGrid(x, z, CFixedVector2D(r, r));
574 : }
575 :
576 0 : inline void MarkDirtinessGrid(const entity_pos_t& x, const entity_pos_t& z, const CFixedVector2D& hbox)
577 : {
578 0 : if (m_UpdateInformations.dirtinessGrid.m_W == 0)
579 0 : return;
580 :
581 0 : std::uint16_t j0, j1, i0, i1;
582 0 : Pathfinding::NearestNavcell(x - hbox.X, z - hbox.Y, i0, j0, m_UpdateInformations.dirtinessGrid.m_W, m_UpdateInformations.dirtinessGrid.m_H);
583 0 : Pathfinding::NearestNavcell(x + hbox.X, z + hbox.Y, i1, j1, m_UpdateInformations.dirtinessGrid.m_W, m_UpdateInformations.dirtinessGrid.m_H);
584 :
585 0 : for (int j = j0; j < j1; ++j)
586 0 : for (int i = i0; i < i1; ++i)
587 0 : m_UpdateInformations.dirtinessGrid.set(i, j, 1);
588 : }
589 :
590 : /**
591 : * Mark all previous Rasterize()d grids as dirty, if they depend on this shape.
592 : * Call this when a static shape has changed.
593 : */
594 30 : void MakeDirtyStatic(flags_t flags, std::uint32_t index, const StaticShape& shape)
595 : {
596 30 : m_DebugOverlayDirty = true;
597 :
598 : // Early exit to speed up the map initialisation.
599 30 : if (m_UpdateInformations.globallyDirty)
600 : return;
601 :
602 18 : if (flags & (FLAG_BLOCK_PATHFINDING | FLAG_BLOCK_FOUNDATION))
603 : {
604 0 : m_UpdateInformations.dirty = true;
605 :
606 0 : if (std::find(m_DirtyStaticShapes.begin(), m_DirtyStaticShapes.end(), index) == m_DirtyStaticShapes.end())
607 0 : m_DirtyStaticShapes.push_back(index);
608 :
609 : // All shapes overlapping the updated part of the grid should be dirtied too.
610 : // We are going to invalidate the region of the grid corresponding to the modified shape plus its clearance,
611 : // and we need to get the shapes whose clearance can overlap this area. So we need to extend the search area
612 : // by two times the maximum clearance.
613 :
614 0 : CFixedVector2D center(shape.x, shape.z);
615 0 : CFixedVector2D hbox = Geometry::GetHalfBoundingBox(shape.u, shape.v, CFixedVector2D(shape.hw, shape.hh));
616 0 : CFixedVector2D expand(m_MaxClearance, m_MaxClearance);
617 :
618 0 : std::vector<std::uint32_t> staticsNear;
619 0 : m_StaticSubdivision.GetInRange(staticsNear, center - hbox - expand*2, center + hbox + expand*2);
620 0 : for (std::uint32_t& staticId : staticsNear)
621 0 : if (std::find(m_DirtyStaticShapes.begin(), m_DirtyStaticShapes.end(), staticId) == m_DirtyStaticShapes.end())
622 0 : m_DirtyStaticShapes.push_back(staticId);
623 :
624 0 : std::vector<std::uint32_t> unitsNear;
625 0 : m_UnitSubdivision.GetInRange(unitsNear, center - hbox - expand*2, center + hbox + expand*2);
626 0 : for (std::uint32_t& unitId : unitsNear)
627 0 : if (std::find(m_DirtyUnitShapes.begin(), m_DirtyUnitShapes.end(), unitId) == m_DirtyUnitShapes.end())
628 0 : m_DirtyUnitShapes.push_back(unitId);
629 :
630 0 : MarkDirtinessGrid(shape.x, shape.z, hbox + expand);
631 0 : }
632 : }
633 :
634 : /**
635 : * Mark all previous Rasterize()d grids as dirty, if they depend on this shape.
636 : * Call this when a unit shape has changed.
637 : */
638 15648 : void MakeDirtyUnit(flags_t flags, std::uint32_t index, const UnitShape& shape)
639 : {
640 15648 : m_DebugOverlayDirty = true;
641 :
642 : // Early exit to speed up the map initialisation.
643 15648 : if (m_UpdateInformations.globallyDirty)
644 : return;
645 :
646 15630 : if (flags & (FLAG_BLOCK_PATHFINDING | FLAG_BLOCK_FOUNDATION))
647 : {
648 0 : m_UpdateInformations.dirty = true;
649 :
650 0 : if (std::find(m_DirtyUnitShapes.begin(), m_DirtyUnitShapes.end(), index) == m_DirtyUnitShapes.end())
651 0 : m_DirtyUnitShapes.push_back(index);
652 :
653 : // All shapes overlapping the updated part of the grid should be dirtied too.
654 : // We are going to invalidate the region of the grid corresponding to the modified shape plus its clearance,
655 : // and we need to get the shapes whose clearance can overlap this area. So we need to extend the search area
656 : // by two times the maximum clearance.
657 :
658 0 : CFixedVector2D center(shape.x, shape.z);
659 :
660 0 : std::vector<std::uint32_t> staticsNear;
661 0 : m_StaticSubdivision.GetNear(staticsNear, center, shape.clearance + m_MaxClearance*2);
662 0 : for (std::uint32_t& staticId : staticsNear)
663 0 : if (std::find(m_DirtyStaticShapes.begin(), m_DirtyStaticShapes.end(), staticId) == m_DirtyStaticShapes.end())
664 0 : m_DirtyStaticShapes.push_back(staticId);
665 :
666 0 : std::vector<std::uint32_t> unitsNear;
667 0 : m_UnitSubdivision.GetNear(unitsNear, center, shape.clearance + m_MaxClearance*2);
668 0 : for (std::uint32_t& unitId : unitsNear)
669 0 : if (std::find(m_DirtyUnitShapes.begin(), m_DirtyUnitShapes.end(), unitId) == m_DirtyUnitShapes.end())
670 0 : m_DirtyUnitShapes.push_back(unitId);
671 :
672 0 : MarkDirtinessGrid(shape.x, shape.z, shape.clearance + m_MaxClearance);
673 0 : }
674 : }
675 :
676 : /**
677 : * Return whether the given point is within the world bounds by at least r
678 : */
679 29320 : inline bool IsInWorld(entity_pos_t x, entity_pos_t z, entity_pos_t r) const
680 : {
681 29320 : return (m_WorldX0+r <= x && x <= m_WorldX1-r && m_WorldZ0+r <= z && z <= m_WorldZ1-r);
682 : }
683 :
684 : /**
685 : * Return whether the given point is within the world bounds
686 : */
687 64 : inline bool IsInWorld(const CFixedVector2D& p) const
688 : {
689 32 : return (m_WorldX0 <= p.X && p.X <= m_WorldX1 && m_WorldZ0 <= p.Y && p.Y <= m_WorldZ1);
690 : }
691 :
692 : void RasterizeHelper(Grid<NavcellData>& grid, ICmpObstructionManager::flags_t requireMask, bool fullUpdate, pass_class_t appliedMask, entity_pos_t clearance = fixed::Zero()) const;
693 : };
694 :
695 274 : REGISTER_COMPONENT_TYPE(ObstructionManager)
696 :
697 : /**
698 : * DistanceTo function family, all end up in calculating a vector length, DistanceBetweenShapes or
699 : * MaxDistanceBetweenShapes. The MaxFoo family calculates the opposite edge opposite edge distance.
700 : * When the distance is undefined we return -1.
701 : */
702 3588 : fixed CCmpObstructionManager::DistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const
703 : {
704 3588 : ObstructionSquare obstruction;
705 3588 : CmpPtr<ICmpObstruction> cmpObstruction(GetSimContext(), ent);
706 3588 : if (cmpObstruction && cmpObstruction->GetObstructionSquare(obstruction))
707 : {
708 3588 : ObstructionSquare point;
709 3588 : point.x = px;
710 3588 : point.z = pz;
711 3588 : return DistanceBetweenShapes(obstruction, point);
712 : }
713 :
714 0 : CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), ent);
715 0 : if (!cmpPosition || !cmpPosition->IsInWorld())
716 0 : return fixed::FromInt(-1);
717 :
718 0 : return (CFixedVector2D(cmpPosition->GetPosition2D().X, cmpPosition->GetPosition2D().Y) - CFixedVector2D(px, pz)).Length();
719 : }
720 :
721 0 : fixed CCmpObstructionManager::MaxDistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const
722 : {
723 0 : ObstructionSquare obstruction;
724 0 : CmpPtr<ICmpObstruction> cmpObstruction(GetSimContext(), ent);
725 0 : if (!cmpObstruction || !cmpObstruction->GetObstructionSquare(obstruction))
726 : {
727 0 : ObstructionSquare point;
728 0 : point.x = px;
729 0 : point.z = pz;
730 0 : return MaxDistanceBetweenShapes(obstruction, point);
731 : }
732 :
733 0 : CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), ent);
734 0 : if (!cmpPosition || !cmpPosition->IsInWorld())
735 0 : return fixed::FromInt(-1);
736 :
737 0 : return (CFixedVector2D(cmpPosition->GetPosition2D().X, cmpPosition->GetPosition2D().Y) - CFixedVector2D(px, pz)).Length();
738 : }
739 :
740 10706 : fixed CCmpObstructionManager::DistanceToTarget(entity_id_t ent, entity_id_t target) const
741 : {
742 10706 : ObstructionSquare obstruction;
743 10706 : CmpPtr<ICmpObstruction> cmpObstruction(GetSimContext(), ent);
744 10706 : if (!cmpObstruction || !cmpObstruction->GetObstructionSquare(obstruction))
745 : {
746 0 : CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), ent);
747 0 : if (!cmpPosition || !cmpPosition->IsInWorld())
748 0 : return fixed::FromInt(-1);
749 0 : return DistanceToPoint(target, cmpPosition->GetPosition2D().X, cmpPosition->GetPosition2D().Y);
750 : }
751 :
752 10706 : ObstructionSquare target_obstruction;
753 10706 : CmpPtr<ICmpObstruction> cmpObstructionTarget(GetSimContext(), target);
754 10706 : if (!cmpObstructionTarget || !cmpObstructionTarget->GetObstructionSquare(target_obstruction))
755 : {
756 0 : CmpPtr<ICmpPosition> cmpPositionTarget(GetSimContext(), target);
757 0 : if (!cmpPositionTarget || !cmpPositionTarget->IsInWorld())
758 0 : return fixed::FromInt(-1);
759 0 : return DistanceToPoint(ent, cmpPositionTarget->GetPosition2D().X, cmpPositionTarget->GetPosition2D().Y);
760 : }
761 :
762 10706 : return DistanceBetweenShapes(obstruction, target_obstruction);
763 : }
764 :
765 11 : fixed CCmpObstructionManager::MaxDistanceToTarget(entity_id_t ent, entity_id_t target) const
766 : {
767 11 : ObstructionSquare obstruction;
768 11 : CmpPtr<ICmpObstruction> cmpObstruction(GetSimContext(), ent);
769 11 : if (!cmpObstruction || !cmpObstruction->GetObstructionSquare(obstruction))
770 : {
771 0 : CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), ent);
772 0 : if (!cmpPosition || !cmpPosition->IsInWorld())
773 0 : return fixed::FromInt(-1);
774 0 : return MaxDistanceToPoint(target, cmpPosition->GetPosition2D().X, cmpPosition->GetPosition2D().Y);
775 : }
776 :
777 11 : ObstructionSquare target_obstruction;
778 11 : CmpPtr<ICmpObstruction> cmpObstructionTarget(GetSimContext(), target);
779 11 : if (!cmpObstructionTarget || !cmpObstructionTarget->GetObstructionSquare(target_obstruction))
780 : {
781 0 : CmpPtr<ICmpPosition> cmpPositionTarget(GetSimContext(), target);
782 0 : if (!cmpPositionTarget || !cmpPositionTarget->IsInWorld())
783 0 : return fixed::FromInt(-1);
784 0 : return MaxDistanceToPoint(ent, cmpPositionTarget->GetPosition2D().X, cmpPositionTarget->GetPosition2D().Y);
785 : }
786 :
787 11 : return MaxDistanceBetweenShapes(obstruction, target_obstruction);
788 : }
789 :
790 25334 : fixed CCmpObstructionManager::DistanceBetweenShapes(const ObstructionSquare& source, const ObstructionSquare& target) const
791 : {
792 : // Sphere-sphere collision.
793 25334 : if (source.hh == fixed::Zero() && target.hh == fixed::Zero())
794 0 : return (CFixedVector2D(target.x, target.z) - CFixedVector2D(source.x, source.z)).Length() - source.hw - target.hw;
795 :
796 : // Square to square.
797 25334 : if (source.hh != fixed::Zero() && target.hh != fixed::Zero())
798 21324 : return Geometry::DistanceSquareToSquare(
799 42648 : CFixedVector2D(target.x, target.z) - CFixedVector2D(source.x, source.z),
800 42648 : source.u, source.v, CFixedVector2D(source.hw, source.hh),
801 21324 : target.u, target.v, CFixedVector2D(target.hw, target.hh));
802 :
803 : // To cover both remaining cases, shape a is the square one, shape b is the circular one.
804 4010 : const ObstructionSquare& a = source.hh == fixed::Zero() ? target : source;
805 4010 : const ObstructionSquare& b = source.hh == fixed::Zero() ? source : target;
806 4010 : return Geometry::DistanceToSquare(
807 8020 : CFixedVector2D(b.x, b.z) - CFixedVector2D(a.x, a.z),
808 4010 : a.u, a.v, CFixedVector2D(a.hw, a.hh), true) - b.hw;
809 : }
810 :
811 11 : fixed CCmpObstructionManager::MaxDistanceBetweenShapes(const ObstructionSquare& source, const ObstructionSquare& target) const
812 : {
813 : // Sphere-sphere collision.
814 11 : if (source.hh == fixed::Zero() && target.hh == fixed::Zero())
815 0 : return (CFixedVector2D(target.x, target.z) - CFixedVector2D(source.x, source.z)).Length() + source.hw + target.hw;
816 :
817 : // Square to square.
818 11 : if (source.hh != fixed::Zero() && target.hh != fixed::Zero())
819 9 : return Geometry::MaxDistanceSquareToSquare(
820 18 : CFixedVector2D(target.x, target.z) - CFixedVector2D(source.x, source.z),
821 18 : source.u, source.v, CFixedVector2D(source.hw, source.hh),
822 9 : target.u, target.v, CFixedVector2D(target.hw, target.hh));
823 :
824 : // To cover both remaining cases, shape a is the square one, shape b is the circular one.
825 2 : const ObstructionSquare& a = source.hh == fixed::Zero() ? target : source;
826 2 : const ObstructionSquare& b = source.hh == fixed::Zero() ? source : target;
827 2 : return Geometry::MaxDistanceToSquare(
828 4 : CFixedVector2D(b.x, b.z) - CFixedVector2D(a.x, a.z),
829 2 : a.u, a.v, CFixedVector2D(a.hw, a.hh), true) + b.hw;
830 : }
831 :
832 : /**
833 : * IsInRange function family depending on the DistanceTo family.
834 : *
835 : * In range if the edge to edge distance is inferior to maxRange
836 : * and if the opposite edge to opposite edge distance is greater than minRange when the opposite bool is true
837 : * or when the opposite bool is false the edge to edge distance is more than minRange.
838 : *
839 : * Using the opposite egde for minRange means that a unit is in range of a building if it is farther than
840 : * clearance-buildingsize, which is generally going to be negative (and thus this returns true).
841 : * NB: from a game POV, this means units can easily fire on buildings, which is good,
842 : * but it also means that buildings can easily fire on units. Buildings are usually meant
843 : * to fire from the edge, not the opposite edge, so this looks odd. For this reason one can choose
844 : * to set the opposite bool false and use the edge to egde distance.
845 : *
846 : * We don't use squares because the are likely to overflow.
847 : * TODO Avoid the overflows and use squares instead.
848 : * We use a 0.0001 margin to avoid rounding errors.
849 : */
850 3588 : bool CCmpObstructionManager::IsInPointRange(entity_id_t ent, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const
851 : {
852 3588 : fixed dist = DistanceToPoint(ent, px, pz);
853 3588 : return maxRange != NEVER_IN_RANGE && dist != fixed::FromInt(-1) &&
854 7176 : (dist <= (maxRange + fixed::FromFloat(0.0001f)) || maxRange == ALWAYS_IN_RANGE) &&
855 0 : (opposite ? MaxDistanceToPoint(ent, px, pz) : dist) >= minRange - fixed::FromFloat(0.0001f);
856 : }
857 :
858 10696 : bool CCmpObstructionManager::IsInTargetRange(entity_id_t ent, entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const
859 : {
860 10696 : fixed dist = DistanceToTarget(ent, target);
861 10696 : return maxRange != NEVER_IN_RANGE && dist != fixed::FromInt(-1) &&
862 21392 : (dist <= (maxRange + fixed::FromFloat(0.0001f)) || maxRange == ALWAYS_IN_RANGE) &&
863 3345 : (opposite ? MaxDistanceToTarget(ent, target) : dist) >= minRange - fixed::FromFloat(0.0001f);
864 : }
865 :
866 0 : bool CCmpObstructionManager::IsInTargetParabolicRange(entity_id_t ent, entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange, entity_pos_t yOrigin, bool opposite) const
867 : {
868 0 : CmpPtr<ICmpRangeManager> cmpRangeManager(GetSystemEntity());
869 0 : return IsInTargetRange(ent, target, minRange, cmpRangeManager->GetEffectiveParabolicRange(ent, target, maxRange, yOrigin), opposite);
870 : }
871 :
872 0 : bool CCmpObstructionManager::IsPointInPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange) const
873 : {
874 0 : entity_pos_t distance = (CFixedVector2D(x, z) - CFixedVector2D(px, pz)).Length();
875 0 : return maxRange != NEVER_IN_RANGE && (distance <= (maxRange + fixed::FromFloat(0.0001f)) || maxRange == ALWAYS_IN_RANGE) &&
876 0 : distance >= minRange - fixed::FromFloat(0.0001f);
877 : }
878 :
879 4761 : bool CCmpObstructionManager::AreShapesInRange(const ObstructionSquare& source, const ObstructionSquare& target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const
880 : {
881 4761 : fixed dist = DistanceBetweenShapes(source, target);
882 4761 : return maxRange != NEVER_IN_RANGE && dist != fixed::FromInt(-1) &&
883 9522 : (dist <= (maxRange + fixed::FromFloat(0.0001f)) || maxRange == ALWAYS_IN_RANGE) &&
884 3636 : (opposite ? MaxDistanceBetweenShapes(source, target) : dist) >= minRange - fixed::FromFloat(0.0001f);
885 : }
886 :
887 11349 : bool CCmpObstructionManager::TestLine(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, bool relaxClearanceForUnits) const
888 : {
889 : // Check that both end points are within the world (which means the whole line must be)
890 22698 : if (!IsInWorld(x0, z0, r) || !IsInWorld(x1, z1, r))
891 : return true;
892 :
893 14058 : CFixedVector2D posMin (std::min(x0, x1) - r, std::min(z0, z1) - r);
894 29712 : CFixedVector2D posMax (std::max(x0, x1) + r, std::max(z0, z1) + r);
895 :
896 : // actual radius used for unit-unit collisions. If relaxClearanceForUnits, will be smaller to allow more overlap.
897 11349 : entity_pos_t unitUnitRadius = r;
898 11349 : if (relaxClearanceForUnits)
899 11349 : unitUnitRadius -= entity_pos_t::FromInt(1)/2;
900 :
901 11349 : std::vector<entity_id_t> unitShapes;
902 11349 : m_UnitSubdivision.GetInRange(unitShapes, posMin, posMax);
903 42171 : for (const entity_id_t& shape : unitShapes)
904 : {
905 30822 : std::map<std::uint32_t, UnitShape>::const_iterator it = m_UnitShapes.find(shape);
906 30822 : ENSURE(it != m_UnitShapes.end());
907 :
908 30822 : if (!filter.TestShape(UNIT_INDEX_TO_TAG(it->first), it->second.flags, it->second.group, INVALID_ENTITY))
909 29418 : continue;
910 :
911 1404 : CFixedVector2D center(it->second.x, it->second.z);
912 1404 : CFixedVector2D halfSize(it->second.clearance + unitUnitRadius, it->second.clearance + unitUnitRadius);
913 1404 : if (Geometry::TestRayAASquare(CFixedVector2D(x0, z0) - center, CFixedVector2D(x1, z1) - center, halfSize))
914 0 : return true;
915 : }
916 :
917 11349 : std::vector<entity_id_t> staticShapes;
918 11349 : m_StaticSubdivision.GetInRange(staticShapes, posMin, posMax);
919 11415 : for (const entity_id_t& shape : staticShapes)
920 : {
921 66 : std::map<std::uint32_t, StaticShape>::const_iterator it = m_StaticShapes.find(shape);
922 66 : ENSURE(it != m_StaticShapes.end());
923 :
924 66 : if (!filter.TestShape(STATIC_INDEX_TO_TAG(it->first), it->second.flags, it->second.group, it->second.group2))
925 66 : continue;
926 :
927 0 : CFixedVector2D center(it->second.x, it->second.z);
928 0 : CFixedVector2D halfSize(it->second.hw + r, it->second.hh + r);
929 0 : if (Geometry::TestRaySquare(CFixedVector2D(x0, z0) - center, CFixedVector2D(x1, z1) - center, it->second.u, it->second.v, halfSize))
930 0 : return true;
931 : }
932 :
933 : return false;
934 11349 : }
935 :
936 3303 : bool CCmpObstructionManager::TestUnitLine(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, bool relaxClearanceForUnits) const
937 : {
938 : // Check that both end points are within the world (which means the whole line must be)
939 6606 : if (!IsInWorld(x0, z0, r) || !IsInWorld(x1, z1, r))
940 : return true;
941 :
942 3303 : CFixedVector2D posMin (std::min(x0, x1) - r, std::min(z0, z1) - r);
943 9909 : CFixedVector2D posMax (std::max(x0, x1) + r, std::max(z0, z1) + r);
944 :
945 : // actual radius used for unit-unit collisions. If relaxClearanceForUnits, will be smaller to allow more overlap.
946 3303 : entity_pos_t unitUnitRadius = r;
947 3303 : if (relaxClearanceForUnits)
948 3303 : unitUnitRadius -= entity_pos_t::FromInt(1)/2;
949 :
950 3303 : std::vector<entity_id_t> unitShapes;
951 3303 : m_UnitSubdivision.GetInRange(unitShapes, posMin, posMax);
952 13881 : for (const entity_id_t& shape : unitShapes)
953 : {
954 10578 : std::map<std::uint32_t, UnitShape>::const_iterator it = m_UnitShapes.find(shape);
955 10578 : ENSURE(it != m_UnitShapes.end());
956 :
957 10578 : if (!filter.TestShape(UNIT_INDEX_TO_TAG(it->first), it->second.flags, it->second.group, INVALID_ENTITY))
958 10251 : continue;
959 :
960 327 : CFixedVector2D center(it->second.x, it->second.z);
961 327 : CFixedVector2D halfSize(it->second.clearance + unitUnitRadius, it->second.clearance + unitUnitRadius);
962 327 : if (Geometry::TestRayAASquare(CFixedVector2D(x0, z0) - center, CFixedVector2D(x1, z1) - center, halfSize))
963 0 : return true;
964 : }
965 : return false;
966 3303 : }
967 :
968 :
969 16 : bool CCmpObstructionManager::TestStaticShape(const IObstructionTestFilter& filter,
970 : entity_pos_t x, entity_pos_t z, entity_pos_t a, entity_pos_t w, entity_pos_t h,
971 : std::vector<entity_id_t>* out) const
972 : {
973 16 : PROFILE("TestStaticShape");
974 :
975 16 : if (out)
976 16 : out->clear();
977 :
978 16 : fixed s, c;
979 16 : sincos_approx(a, s, c);
980 16 : CFixedVector2D u(c, -s);
981 16 : CFixedVector2D v(s, c);
982 16 : CFixedVector2D center(x, z);
983 16 : CFixedVector2D halfSize(w/2, h/2);
984 16 : CFixedVector2D corner1 = u.Multiply(halfSize.X) + v.Multiply(halfSize.Y);
985 16 : CFixedVector2D corner2 = u.Multiply(halfSize.X) - v.Multiply(halfSize.Y);
986 :
987 : // Check that all corners are within the world (which means the whole shape must be)
988 48 : if (!IsInWorld(center + corner1) || !IsInWorld(center + corner2) ||
989 32 : !IsInWorld(center - corner1) || !IsInWorld(center - corner2))
990 : {
991 0 : if (out)
992 0 : out->push_back(INVALID_ENTITY); // no entity ID, so just push an arbitrary marker
993 : else
994 : return true;
995 : }
996 :
997 16 : fixed bbHalfWidth = std::max(corner1.X.Absolute(), corner2.X.Absolute());
998 16 : fixed bbHalfHeight = std::max(corner1.Y.Absolute(), corner2.Y.Absolute());
999 16 : CFixedVector2D posMin(x - bbHalfWidth, z - bbHalfHeight);
1000 16 : CFixedVector2D posMax(x + bbHalfWidth, z + bbHalfHeight);
1001 :
1002 16 : std::vector<entity_id_t> unitShapes;
1003 16 : m_UnitSubdivision.GetInRange(unitShapes, posMin, posMax);
1004 48 : for (entity_id_t& shape : unitShapes)
1005 : {
1006 32 : std::map<std::uint32_t, UnitShape>::const_iterator it = m_UnitShapes.find(shape);
1007 32 : ENSURE(it != m_UnitShapes.end());
1008 :
1009 32 : if (!filter.TestShape(UNIT_INDEX_TO_TAG(it->first), it->second.flags, it->second.group, INVALID_ENTITY))
1010 10 : continue;
1011 :
1012 22 : CFixedVector2D center1(it->second.x, it->second.z);
1013 :
1014 25 : if (Geometry::PointIsInSquare(center1 - center, u, v, CFixedVector2D(halfSize.X + it->second.clearance, halfSize.Y + it->second.clearance)))
1015 : {
1016 16 : if (out)
1017 16 : out->push_back(it->second.entity);
1018 : else
1019 0 : return true;
1020 : }
1021 : }
1022 :
1023 16 : std::vector<entity_id_t> staticShapes;
1024 16 : m_StaticSubdivision.GetInRange(staticShapes, posMin, posMax);
1025 34 : for (entity_id_t& shape : staticShapes)
1026 : {
1027 18 : std::map<std::uint32_t, StaticShape>::const_iterator it = m_StaticShapes.find(shape);
1028 18 : ENSURE(it != m_StaticShapes.end());
1029 :
1030 18 : if (!filter.TestShape(STATIC_INDEX_TO_TAG(it->first), it->second.flags, it->second.group, it->second.group2))
1031 11 : continue;
1032 :
1033 7 : CFixedVector2D center1(it->second.x, it->second.z);
1034 7 : CFixedVector2D halfSize1(it->second.hw, it->second.hh);
1035 7 : if (Geometry::TestSquareSquare(center, u, v, halfSize, center1, it->second.u, it->second.v, halfSize1))
1036 : {
1037 4 : if (out)
1038 4 : out->push_back(it->second.entity);
1039 : else
1040 0 : return true;
1041 : }
1042 : }
1043 :
1044 16 : if (out)
1045 16 : return !out->empty(); // collided if the list isn't empty
1046 : else
1047 : return false; // didn't collide, if we got this far
1048 16 : }
1049 :
1050 16 : bool CCmpObstructionManager::TestUnitShape(const IObstructionTestFilter& filter,
1051 : entity_pos_t x, entity_pos_t z, entity_pos_t clearance,
1052 : std::vector<entity_id_t>* out) const
1053 : {
1054 16 : PROFILE("TestUnitShape");
1055 :
1056 : // Check that the shape is within the world
1057 16 : if (!IsInWorld(x, z, clearance))
1058 : {
1059 0 : if (out)
1060 0 : out->push_back(INVALID_ENTITY); // no entity ID, so just push an arbitrary marker
1061 : else
1062 : return true;
1063 : }
1064 :
1065 16 : CFixedVector2D center(x, z);
1066 16 : CFixedVector2D posMin(x - clearance, z - clearance);
1067 16 : CFixedVector2D posMax(x + clearance, z + clearance);
1068 :
1069 16 : std::vector<entity_id_t> unitShapes;
1070 16 : m_UnitSubdivision.GetInRange(unitShapes, posMin, posMax);
1071 48 : for (const entity_id_t& shape : unitShapes)
1072 : {
1073 32 : std::map<std::uint32_t, UnitShape>::const_iterator it = m_UnitShapes.find(shape);
1074 32 : ENSURE(it != m_UnitShapes.end());
1075 :
1076 32 : if (!filter.TestShape(UNIT_INDEX_TO_TAG(it->first), it->second.flags, it->second.group, INVALID_ENTITY))
1077 10 : continue;
1078 :
1079 22 : entity_pos_t c1 = it->second.clearance;
1080 :
1081 40 : if (!(
1082 22 : it->second.x + c1 < x - clearance ||
1083 19 : it->second.x - c1 > x + clearance ||
1084 19 : it->second.z + c1 < z - clearance ||
1085 18 : it->second.z - c1 > z + clearance))
1086 : {
1087 16 : if (out)
1088 16 : out->push_back(it->second.entity);
1089 : else
1090 16 : return true;
1091 : }
1092 : }
1093 :
1094 16 : std::vector<entity_id_t> staticShapes;
1095 16 : m_StaticSubdivision.GetInRange(staticShapes, posMin, posMax);
1096 34 : for (const entity_id_t& shape : staticShapes)
1097 : {
1098 18 : std::map<std::uint32_t, StaticShape>::const_iterator it = m_StaticShapes.find(shape);
1099 18 : ENSURE(it != m_StaticShapes.end());
1100 :
1101 18 : if (!filter.TestShape(STATIC_INDEX_TO_TAG(it->first), it->second.flags, it->second.group, it->second.group2))
1102 11 : continue;
1103 :
1104 7 : CFixedVector2D center1(it->second.x, it->second.z);
1105 8 : if (Geometry::PointIsInSquare(center1 - center, it->second.u, it->second.v, CFixedVector2D(it->second.hw + clearance, it->second.hh + clearance)))
1106 : {
1107 4 : if (out)
1108 4 : out->push_back(it->second.entity);
1109 : else
1110 0 : return true;
1111 : }
1112 : }
1113 :
1114 16 : if (out)
1115 16 : return !out->empty(); // collided if the list isn't empty
1116 : else
1117 : return false; // didn't collide, if we got this far
1118 16 : }
1119 :
1120 6 : void CCmpObstructionManager::Rasterize(Grid<NavcellData>& grid, const std::vector<PathfinderPassability>& passClasses, bool fullUpdate)
1121 : {
1122 6 : PROFILE3("Rasterize Obstructions");
1123 :
1124 : // Cells are only marked as blocked if the whole cell is strictly inside the shape.
1125 : // (That ensures the shape's geometric border is always reachable.)
1126 :
1127 : // Pass classes will get shapes rasterized on them depending on their Obstruction value.
1128 : // Classes with another value than "pathfinding" should not use Clearance.
1129 :
1130 6 : std::map<entity_pos_t, std::uint16_t> pathfindingMasks;
1131 6 : std::uint16_t foundationMask = 0;
1132 60 : for (const PathfinderPassability& passability : passClasses)
1133 : {
1134 54 : switch (passability.m_Obstructions)
1135 : {
1136 24 : case PathfinderPassability::PATHFINDING:
1137 24 : {
1138 24 : std::map<entity_pos_t, std::uint16_t>::iterator it = pathfindingMasks.find(passability.m_Clearance);
1139 24 : if (it == pathfindingMasks.end())
1140 18 : pathfindingMasks[passability.m_Clearance] = passability.m_Mask;
1141 : else
1142 6 : it->second |= passability.m_Mask;
1143 : break;
1144 : }
1145 12 : case PathfinderPassability::FOUNDATION:
1146 12 : foundationMask |= passability.m_Mask;
1147 12 : break;
1148 18 : default:
1149 18 : continue;
1150 18 : }
1151 : }
1152 :
1153 : // FLAG_BLOCK_PATHFINDING and FLAG_BLOCK_FOUNDATION are the only flags taken into account by MakeDirty* functions,
1154 : // so they should be the only ones rasterized using with the help of m_Dirty*Shapes vectors.
1155 :
1156 24 : for (auto& maskPair : pathfindingMasks)
1157 18 : RasterizeHelper(grid, FLAG_BLOCK_PATHFINDING, fullUpdate, maskPair.second, maskPair.first);
1158 :
1159 6 : RasterizeHelper(grid, FLAG_BLOCK_FOUNDATION, fullUpdate, foundationMask);
1160 :
1161 6 : m_DirtyStaticShapes.clear();
1162 6 : m_DirtyUnitShapes.clear();
1163 6 : }
1164 :
1165 24 : void CCmpObstructionManager::RasterizeHelper(Grid<NavcellData>& grid, ICmpObstructionManager::flags_t requireMask, bool fullUpdate, pass_class_t appliedMask, entity_pos_t clearance) const
1166 : {
1167 24 : for (auto& pair : m_StaticShapes)
1168 : {
1169 0 : const StaticShape& shape = pair.second;
1170 0 : if (!(shape.flags & requireMask))
1171 0 : continue;
1172 :
1173 0 : if (!fullUpdate && std::find(m_DirtyStaticShapes.begin(), m_DirtyStaticShapes.end(), pair.first) == m_DirtyStaticShapes.end())
1174 0 : continue;
1175 :
1176 : // TODO: it might be nice to rasterize with rounded corners for large 'expand' values.
1177 0 : ObstructionSquare square = { shape.x, shape.z, shape.u, shape.v, shape.hw, shape.hh };
1178 0 : SimRasterize::Spans spans;
1179 0 : SimRasterize::RasterizeRectWithClearance(spans, square, clearance, Pathfinding::NAVCELL_SIZE);
1180 0 : for (SimRasterize::Span& span : spans)
1181 : {
1182 0 : std::int16_t j = Clamp(span.j, static_cast<std::int16_t>(0),
1183 0 : static_cast<std::int16_t>(grid.m_H-1));
1184 0 : std::int16_t i0 = std::max(span.i0, static_cast<std::int16_t>(0));
1185 0 : std::int16_t i1 = std::min(span.i1, static_cast<std::int16_t>(grid.m_W));
1186 :
1187 0 : for (std::int16_t i = i0; i < i1; ++i)
1188 0 : grid.set(i, j, grid.get(i, j) | appliedMask);
1189 : }
1190 0 : }
1191 :
1192 24 : for (auto& pair : m_UnitShapes)
1193 : {
1194 0 : if (!(pair.second.flags & requireMask))
1195 0 : continue;
1196 :
1197 0 : if (!fullUpdate && std::find(m_DirtyUnitShapes.begin(), m_DirtyUnitShapes.end(), pair.first) == m_DirtyUnitShapes.end())
1198 0 : continue;
1199 :
1200 0 : CFixedVector2D center(pair.second.x, pair.second.z);
1201 0 : entity_pos_t r = pair.second.clearance + clearance;
1202 :
1203 0 : std::uint16_t i0, j0, i1, j1;
1204 0 : Pathfinding::NearestNavcell(center.X - r, center.Y - r, i0, j0, grid.m_W, grid.m_H);
1205 0 : Pathfinding::NearestNavcell(center.X + r, center.Y + r, i1, j1, grid.m_W, grid.m_H);
1206 0 : for (std::uint16_t j = j0+1; j < j1; ++j)
1207 0 : for (std::uint16_t i = i0+1; i < i1; ++i)
1208 0 : grid.set(i, j, grid.get(i, j) | appliedMask);
1209 : }
1210 24 : }
1211 :
1212 0 : void CCmpObstructionManager::GetObstructionsInRange(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, std::vector<ObstructionSquare>& squares) const
1213 : {
1214 0 : GetUnitObstructionsInRange(filter, x0, z0, x1, z1, squares);
1215 0 : GetStaticObstructionsInRange(filter, x0, z0, x1, z1, squares);
1216 0 : }
1217 :
1218 0 : void CCmpObstructionManager::GetUnitObstructionsInRange(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, std::vector<ObstructionSquare>& squares) const
1219 : {
1220 0 : PROFILE("GetObstructionsInRange");
1221 :
1222 0 : ENSURE(x0 <= x1 && z0 <= z1);
1223 :
1224 0 : std::vector<entity_id_t> unitShapes;
1225 0 : m_UnitSubdivision.GetInRange(unitShapes, CFixedVector2D(x0, z0), CFixedVector2D(x1, z1));
1226 0 : for (entity_id_t& unitShape : unitShapes)
1227 : {
1228 0 : std::map<std::uint32_t, UnitShape>::const_iterator it = m_UnitShapes.find(unitShape);
1229 0 : ENSURE(it != m_UnitShapes.end());
1230 :
1231 0 : if (!filter.TestShape(UNIT_INDEX_TO_TAG(it->first), it->second.flags, it->second.group, INVALID_ENTITY))
1232 0 : continue;
1233 :
1234 0 : entity_pos_t c = it->second.clearance;
1235 :
1236 : // Skip this object if it's completely outside the requested range
1237 0 : if (it->second.x + c < x0 || it->second.x - c > x1 || it->second.z + c < z0 || it->second.z - c > z1)
1238 0 : continue;
1239 :
1240 0 : CFixedVector2D u(entity_pos_t::FromInt(1), entity_pos_t::Zero());
1241 0 : CFixedVector2D v(entity_pos_t::Zero(), entity_pos_t::FromInt(1));
1242 0 : squares.emplace_back(ObstructionSquare{ it->second.x, it->second.z, u, v, c, c });
1243 : }
1244 0 : }
1245 :
1246 0 : void CCmpObstructionManager::GetStaticObstructionsInRange(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, std::vector<ObstructionSquare>& squares) const
1247 : {
1248 0 : PROFILE("GetObstructionsInRange");
1249 :
1250 0 : ENSURE(x0 <= x1 && z0 <= z1);
1251 :
1252 0 : std::vector<entity_id_t> staticShapes;
1253 0 : m_StaticSubdivision.GetInRange(staticShapes, CFixedVector2D(x0, z0), CFixedVector2D(x1, z1));
1254 0 : for (entity_id_t& staticShape : staticShapes)
1255 : {
1256 0 : std::map<std::uint32_t, StaticShape>::const_iterator it = m_StaticShapes.find(staticShape);
1257 0 : ENSURE(it != m_StaticShapes.end());
1258 :
1259 0 : if (!filter.TestShape(STATIC_INDEX_TO_TAG(it->first), it->second.flags, it->second.group, it->second.group2))
1260 0 : continue;
1261 :
1262 0 : entity_pos_t r = it->second.hw + it->second.hh; // overestimate the max dist of an edge from the center
1263 :
1264 : // Skip this object if its overestimated bounding box is completely outside the requested range
1265 0 : if (it->second.x + r < x0 || it->second.x - r > x1 || it->second.z + r < z0 || it->second.z - r > z1)
1266 0 : continue;
1267 :
1268 : // TODO: maybe we should use Geometry::GetHalfBoundingBox to be more precise?
1269 :
1270 0 : squares.emplace_back(ObstructionSquare{ it->second.x, it->second.z, it->second.u, it->second.v, it->second.hw, it->second.hh });
1271 : }
1272 0 : }
1273 :
1274 0 : void CCmpObstructionManager::GetUnitsOnObstruction(const ObstructionSquare& square, std::vector<entity_id_t>& out, const IObstructionTestFilter& filter, bool strict) const
1275 : {
1276 0 : PROFILE("GetUnitsOnObstruction");
1277 :
1278 : // In order to avoid getting units on impassable cells, we want to find all
1279 : // units subject to the RasterizeRectWithClearance of the building's shape with the
1280 : // unit's clearance covers the navcell the unit is on.
1281 :
1282 0 : std::vector<entity_id_t> unitShapes;
1283 0 : CFixedVector2D center(square.x, square.z);
1284 0 : CFixedVector2D expandedBox =
1285 0 : Geometry::GetHalfBoundingBox(square.u, square.v, CFixedVector2D(square.hw, square.hh)) +
1286 0 : CFixedVector2D(m_MaxClearance, m_MaxClearance);
1287 0 : m_UnitSubdivision.GetInRange(unitShapes, center - expandedBox, center + expandedBox);
1288 :
1289 0 : std::map<entity_pos_t, SimRasterize::Spans> rasterizedRects;
1290 :
1291 0 : for (const std::uint32_t& unitShape : unitShapes)
1292 : {
1293 0 : std::map<std::uint32_t, UnitShape>::const_iterator it = m_UnitShapes.find(unitShape);
1294 0 : ENSURE(it != m_UnitShapes.end());
1295 :
1296 0 : const UnitShape& shape = it->second;
1297 :
1298 0 : if (!filter.TestShape(UNIT_INDEX_TO_TAG(unitShape), shape.flags, shape.group, INVALID_ENTITY))
1299 0 : continue;
1300 :
1301 0 : if (rasterizedRects.find(shape.clearance) == rasterizedRects.end())
1302 : {
1303 : // The rasterization is an approximation of the real shapes.
1304 : // Depending on your use, you may want to be more or less strict on the rasterization,
1305 : // ie this may either return some units that aren't actually on the shape (if strict is set)
1306 : // or this may not return some units that are on the shape (if strict is not set).
1307 : // Foundations need to be non-strict, as otherwise it sometimes detects the builder units
1308 : // as being on the shape, so it orders them away.
1309 0 : SimRasterize::Spans& newSpans = rasterizedRects[shape.clearance];
1310 0 : if (strict)
1311 0 : SimRasterize::RasterizeRectWithClearance(newSpans, square, shape.clearance, Pathfinding::NAVCELL_SIZE);
1312 : else
1313 0 : SimRasterize::RasterizeRectWithClearance(newSpans, square, shape.clearance-Pathfinding::CLEARANCE_EXTENSION_RADIUS, Pathfinding::NAVCELL_SIZE);
1314 : }
1315 :
1316 0 : SimRasterize::Spans& spans = rasterizedRects[shape.clearance];
1317 :
1318 : // Check whether the unit's center is on a navcell that's in
1319 : // any of the spans
1320 :
1321 0 : std::uint16_t i = (shape.x / Pathfinding::NAVCELL_SIZE).ToInt_RoundToNegInfinity();
1322 0 : std::uint16_t j = (shape.z / Pathfinding::NAVCELL_SIZE).ToInt_RoundToNegInfinity();
1323 :
1324 0 : for (const SimRasterize::Span& span : spans)
1325 : {
1326 0 : if (j == span.j && span.i0 <= i && i < span.i1)
1327 : {
1328 0 : out.push_back(shape.entity);
1329 : break;
1330 : }
1331 : }
1332 : }
1333 0 : }
1334 :
1335 0 : void CCmpObstructionManager::GetStaticObstructionsOnObstruction(const ObstructionSquare& square, std::vector<entity_id_t>& out, const IObstructionTestFilter& filter) const
1336 : {
1337 0 : PROFILE("GetStaticObstructionsOnObstruction");
1338 :
1339 0 : std::vector<entity_id_t> staticShapes;
1340 0 : CFixedVector2D center(square.x, square.z);
1341 0 : CFixedVector2D expandedBox = Geometry::GetHalfBoundingBox(square.u, square.v, CFixedVector2D(square.hw, square.hh));
1342 0 : m_StaticSubdivision.GetInRange(staticShapes, center - expandedBox, center + expandedBox);
1343 :
1344 0 : for (const std::uint32_t& staticShape : staticShapes)
1345 : {
1346 0 : std::map<std::uint32_t, StaticShape>::const_iterator it = m_StaticShapes.find(staticShape);
1347 0 : ENSURE(it != m_StaticShapes.end());
1348 :
1349 0 : const StaticShape& shape = it->second;
1350 :
1351 0 : if (!filter.TestShape(STATIC_INDEX_TO_TAG(staticShape), shape.flags, shape.group, shape.group2))
1352 0 : continue;
1353 :
1354 0 : if (Geometry::TestSquareSquare(
1355 : center,
1356 : square.u,
1357 : square.v,
1358 0 : CFixedVector2D(square.hw, square.hh),
1359 0 : CFixedVector2D(shape.x, shape.z),
1360 0 : shape.u,
1361 0 : shape.v,
1362 0 : CFixedVector2D(shape.hw, shape.hh)))
1363 : {
1364 0 : out.push_back(shape.entity);
1365 : }
1366 : }
1367 0 : }
1368 :
1369 0 : void CCmpObstructionManager::RenderSubmit(SceneCollector& collector)
1370 : {
1371 0 : if (!m_DebugOverlayEnabled)
1372 0 : return;
1373 :
1374 0 : CColor defaultColor(0, 0, 1, 1);
1375 0 : CColor movingColor(1, 0, 1, 1);
1376 0 : CColor boundsColor(1, 1, 0, 1);
1377 :
1378 : // If the shapes have changed, then regenerate all the overlays
1379 0 : if (m_DebugOverlayDirty)
1380 : {
1381 0 : m_DebugOverlayLines.clear();
1382 :
1383 0 : m_DebugOverlayLines.push_back(SOverlayLine());
1384 0 : m_DebugOverlayLines.back().m_Color = boundsColor;
1385 0 : SimRender::ConstructSquareOnGround(GetSimContext(),
1386 0 : (m_WorldX0+m_WorldX1).ToFloat()/2.f, (m_WorldZ0+m_WorldZ1).ToFloat()/2.f,
1387 0 : (m_WorldX1-m_WorldX0).ToFloat(), (m_WorldZ1-m_WorldZ0).ToFloat(),
1388 0 : 0, m_DebugOverlayLines.back(), true);
1389 :
1390 0 : for (std::map<std::uint32_t, UnitShape>::iterator it = m_UnitShapes.begin(); it != m_UnitShapes.end(); ++it)
1391 : {
1392 0 : m_DebugOverlayLines.push_back(SOverlayLine());
1393 0 : m_DebugOverlayLines.back().m_Color = ((it->second.flags & FLAG_MOVING) ? movingColor : defaultColor);
1394 0 : SimRender::ConstructSquareOnGround(GetSimContext(), it->second.x.ToFloat(), it->second.z.ToFloat(), it->second.clearance.ToFloat(), it->second.clearance.ToFloat(), 0, m_DebugOverlayLines.back(), true);
1395 : }
1396 :
1397 0 : for (std::map<std::uint32_t, StaticShape>::iterator it = m_StaticShapes.begin(); it != m_StaticShapes.end(); ++it)
1398 : {
1399 0 : m_DebugOverlayLines.push_back(SOverlayLine());
1400 0 : m_DebugOverlayLines.back().m_Color = defaultColor;
1401 0 : float a = atan2f(it->second.v.X.ToFloat(), it->second.v.Y.ToFloat());
1402 0 : SimRender::ConstructSquareOnGround(GetSimContext(), it->second.x.ToFloat(), it->second.z.ToFloat(), it->second.hw.ToFloat()*2, it->second.hh.ToFloat()*2, a, m_DebugOverlayLines.back(), true);
1403 : }
1404 :
1405 0 : m_DebugOverlayDirty = false;
1406 : }
1407 :
1408 0 : for (size_t i = 0; i < m_DebugOverlayLines.size(); ++i)
1409 0 : collector.Submit(&m_DebugOverlayLines[i]);
1410 : }
|