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 : #ifndef INCLUDED_ICMPRANGEMANAGER
19 : #define INCLUDED_ICMPRANGEMANAGER
20 :
21 : #include "maths/FixedVector2D.h"
22 : #include "maths/FixedVector3D.h"
23 : #include "simulation2/helpers/Player.h"
24 : #include "simulation2/helpers/Position.h"
25 : #include "simulation2/system/Component.h"
26 : #include "simulation2/system/Entity.h"
27 : #include "simulation2/system/Interface.h"
28 :
29 : #include <cstddef>
30 : #include <js/Value.h>
31 : #include <string>
32 : #include <vector>
33 :
34 : class FastSpatialSubdivision;
35 :
36 : /**
37 : * Value assigned to a range we will always be in (caused by out of world or "too high" in parabolic ranges).
38 : * TODO Add this for minRanges too.
39 : */
40 : const entity_pos_t ALWAYS_IN_RANGE = entity_pos_t::FromInt(-1);
41 :
42 : /**
43 : * Value assigned to a range we will never be in (caused by out of world or "too high" in parabolic ranges).
44 : * TODO Add this to range queries too.
45 : */
46 : const entity_pos_t NEVER_IN_RANGE = entity_pos_t::FromInt(-2);
47 :
48 : /**
49 : * Since GetVisibility queries are run by the range manager
50 : * other code using these must include ICmpRangeManager.h anyways,
51 : * so define this enum here (Ideally, it'd be in its own header file,
52 : * but adding header file does incur its own compilation time increase).
53 : */
54 : enum class LosVisibility : std::uint8_t
55 : {
56 : HIDDEN = 0,
57 : FOGGED = 1,
58 : VISIBLE = 2
59 : };
60 :
61 : /**
62 : * The same principle applies to CLosQuerier, but to avoid recompiling TUs (a fortiori headers)
63 : * dependent on RangeManager but not CLosQuerier when CLosQuerier changes,
64 : * we define it in another file. Code using LOS will then explicitly include the LOS header
65 : * which makes sense anyways.
66 : */
67 : class CLosQuerier;
68 :
69 : /**
70 : * Provides efficient range-based queries of the game world,
71 : * and also LOS-based effects (fog of war).
72 : *
73 : * (These are somewhat distinct concepts but they share a lot of the implementation,
74 : * so for efficiency they're combined into this class.)
75 : *
76 : * Possible use cases:
77 : * - combat units need to detect targetable enemies entering LOS, so they can choose
78 : * to auto-attack.
79 : * - auras let a unit have some effect on all units (or those of the same player, or of enemies)
80 : * within a certain range.
81 : * - capturable animals need to detect when a player-owned unit is nearby and no units of other
82 : * players are in range.
83 : * - scenario triggers may want to detect when units enter a given area.
84 : * - units gathering from a resource that is exhausted need to find a new resource of the
85 : * same type, near the old one and reachable.
86 : * - projectile weapons with splash damage need to find all units within some distance
87 : * of the target point.
88 : * - ...
89 : *
90 : * In most cases the users are event-based and want notifications when something
91 : * has entered or left the range, and the query can be set up once and rarely changed.
92 : * These queries have to be fast. Entities are approximated as points or circles
93 : * (queries can be set up to ignore sizes because LOS currently ignores it, and mismatches are problematic).
94 : *
95 : * Current design:
96 : *
97 : * This class handles just the most common parts of range queries:
98 : * distance, target interface, and player ownership.
99 : * The caller can then apply any more complex filtering that it needs.
100 : *
101 : * There are two types of query:
102 : * Passive queries are performed by ExecuteQuery and immediately return the matching entities.
103 : * Active queries are set up by CreateActiveQuery, and then a CMessageRangeUpdate message will be
104 : * sent to the entity once per turn if anybody has entered or left the range since the last RangeUpdate.
105 : * Queries can be disabled, in which case no message will be sent.
106 : */
107 : class ICmpRangeManager : public IComponent
108 : {
109 : public:
110 : /**
111 : * External identifiers for active queries.
112 : */
113 : typedef std::uint32_t tag_t;
114 :
115 : /**
116 : * Access the spatial subdivision kept by the range manager.
117 : * @return pointer to spatial subdivision structure.
118 : */
119 : virtual FastSpatialSubdivision* GetSubdivision() = 0;
120 :
121 : /**
122 : * Set the bounds of the world.
123 : * Entities should not be outside the bounds (else efficiency will suffer).
124 : * @param x0,z0,x1,z1 Coordinates of the corners of the world
125 : */
126 : virtual void SetBounds(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1) = 0;
127 :
128 : /**
129 : * Execute a passive query.
130 : * @param source the entity around which the range will be computed.
131 : * @param minRange non-negative minimum distance in metres (inclusive).
132 : * @param maxRange non-negative maximum distance in metres (inclusive); or -1.0 to ignore distance.
133 : * @param owners list of player IDs that matching entities may have; -1 matches entities with no owner.
134 : * @param requiredInterface if non-zero, an interface ID that matching entities must implement.
135 : * @param accountForSize if true, compensate for source/target entity sizes.
136 : * @return list of entities matching the query, ordered by increasing distance from the source entity.
137 : */
138 : virtual std::vector<entity_id_t> ExecuteQuery(entity_id_t source, entity_pos_t minRange, entity_pos_t maxRange,
139 : const std::vector<int>& owners, int requiredInterface, bool accountForSize) = 0;
140 :
141 : /**
142 : * Execute a passive query.
143 : * @param pos the position around which the range will be computed.
144 : * @param minRange non-negative minimum distance in metres (inclusive).
145 : * @param maxRange non-negative maximum distance in metres (inclusive); or -1.0 to ignore distance.
146 : * @param owners list of player IDs that matching entities may have; -1 matches entities with no owner.
147 : * @param requiredInterface if non-zero, an interface ID that matching entities must implement.
148 : * @param accountForSize if true, compensate for source/target entity sizes.
149 : * @return list of entities matching the query, ordered by increasing distance from the source entity.
150 : */
151 : virtual std::vector<entity_id_t> ExecuteQueryAroundPos(const CFixedVector2D& pos, entity_pos_t minRange, entity_pos_t maxRange,
152 : const std::vector<int>& owners, int requiredInterface, bool accountForSize) = 0;
153 :
154 : /**
155 : * Construct an active query. The query will be disabled by default.
156 : * @param source the entity around which the range will be computed.
157 : * @param minRange non-negative minimum distance in metres (inclusive).
158 : * @param maxRange non-negative maximum distance in metres (inclusive); or -1.0 to ignore distance.
159 : * @param owners list of player IDs that matching entities may have; -1 matches entities with no owner.
160 : * @param requiredInterface if non-zero, an interface ID that matching entities must implement.
161 : * @param flags if a entity in range has one of the flags set it will show up.
162 : * @param accountForSize if true, compensate for source/target entity sizes.
163 : * @param preferMirages if true, mirage entities are included (bypassing interface checks)
164 : * and HIDDEN entities are filtered out (needed for targeting fogged enemies).
165 : * When false (default), mirages are excluded entirely and real entities
166 : * are returned regardless of visibility (even if hidden).
167 : * @return unique non-zero identifier of query.
168 : */
169 : virtual tag_t CreateActiveQuery(entity_id_t source,
170 : entity_pos_t minRange, entity_pos_t maxRange,
171 : const std::vector<int>& owners, int requiredInterface, std::uint8_t flags,
172 : bool accountForSize, bool preferMirages = false) = 0;
173 :
174 : /**
175 : * Construct an active query of a parabolic form around the unit.
176 : * The query will be disabled by default.
177 : * @param source the entity around which the range will be computed.
178 : * @param minRange non-negative minimum horizontal distance in metres (inclusive). MinRange doesn't do parabolic checks.
179 : * @param maxRange non-negative maximum distance in metres (inclusive) for units on the same elevation;
180 : * or -1.0 to ignore distance.
181 : * For units on a different height positions, a physical correct paraboloid with height=maxRange/2 above the unit is used to query them
182 : * @param baseRange non-negative base detection range in metres (inclusive) for simple 2D circle checks.
183 : * Units within this horizontal distance are always considered in range regardless of height.
184 : * Set to 0 to disable (original parabolic-only behavior).
185 : * @param yOrigin extra bonus so the source can be placed higher and shoot further
186 : * @param owners list of player IDs that matching entities may have; -1 matches entities with no owner.
187 : * @param requiredInterface if non-zero, an interface ID that matching entities must implement.
188 : * @param flags if a entity in range has one of the flags set it will show up.
189 : * @param preferMirages if true, mirage entities are included (bypassing interface checks)
190 : * and HIDDEN entities are filtered out (needed for targeting fogged enemies).
191 : * When false (default), mirages are excluded entirely and real entities
192 : * are returned regardless of visibility (even if hidden).
193 : * NB: this one has no accountForSize parameter (assumed true), because we currently can only have 7 arguments for JS functions.
194 : * @return unique non-zero identifier of query.
195 : */
196 : virtual tag_t CreateActiveParabolicQuery(entity_id_t source,
197 : entity_pos_t minRange, entity_pos_t maxRange, entity_pos_t baseRange, entity_pos_t yOrigin,
198 : const std::vector<int>& owners, int requiredInterface, std::uint8_t flags,
199 : bool preferMirages = false) = 0;
200 :
201 : /**
202 : * Get the effective range in a parablic range query.
203 : * @param source The entity id at the origin of the query.
204 : * @param target A target entity id.
205 : * @param range The distance to compare terrain height with.
206 : * @param yOrigin Height the source gains over the target by default.
207 : * @return a fixed number representing the effective range correcting parabolicly for the height difference. Returns -1 when the target is too high compared to the source to be in range.
208 : */
209 : virtual entity_pos_t GetEffectiveParabolicRange(entity_id_t source, entity_id_t target, entity_pos_t range, entity_pos_t yOrigin) const = 0;
210 :
211 : /**
212 : * Get the max height (relative to the source) a parabolic projectile can reach
213 : * at a given horizontal distance.
214 : * @param source the entity at the origin.
215 : * @param range the maximum parabolic range on flat terrain.
216 : * @param yOrigin height bonus for the source.
217 : * @param horizDistance the horizontal distance to check.
218 : * @return the maximum reachable height difference (target height - source height),
219 : * or a very negative value if the horizontal distance exceeds the range.
220 : */
221 : virtual entity_pos_t GetMaxReachableParabolicHeight(entity_pos_t range, entity_pos_t yOrigin, entity_pos_t horizDistance) const = 0;
222 :
223 : /**
224 : * Get the average elevation over 8 points on distance range around the entity
225 : * @param id the entity id to look around
226 : * @param range the distance to compare terrain height with
227 : * @return a fixed number representing the average difference. It's positive when the entity is on average higher than the terrain surrounding it.
228 : */
229 : virtual entity_pos_t GetElevationAdaptedRange(const CFixedVector3D& pos, const CFixedVector3D& rot, entity_pos_t range, entity_pos_t yOrigin, entity_pos_t angle) const = 0;
230 :
231 : /**
232 : * Destroy a query and clean up resources. This must be called when an entity no longer needs its
233 : * query (e.g. when the entity is destroyed).
234 : * @param tag identifier of query.
235 : */
236 : virtual void DestroyActiveQuery(tag_t tag) = 0;
237 :
238 : /**
239 : * Re-enable the processing of a query.
240 : * @param tag identifier of query.
241 : */
242 : virtual void EnableActiveQuery(tag_t tag) = 0;
243 :
244 : /**
245 : * Disable the processing of a query (no RangeUpdate messages will be sent).
246 : * @param tag identifier of query.
247 : */
248 : virtual void DisableActiveQuery(tag_t tag) = 0;
249 :
250 : /**
251 : * Check if the processing of a query is enabled.
252 : * @param tag identifier of a query.
253 : */
254 : virtual bool IsActiveQueryEnabled(tag_t tag) const = 0;
255 :
256 : /**
257 : * Immediately execute a query, and re-enable it if disabled.
258 : * The next RangeUpdate message will say who has entered/left since this call,
259 : * so you won't miss any notifications.
260 : * @param tag identifier of query.
261 : * @return list of entities matching the query, ordered by increasing distance from the source entity.
262 : */
263 : virtual std::vector<entity_id_t> ResetActiveQuery(tag_t tag) = 0;
264 :
265 : /**
266 : * Returns a list of all entities for a specific player.
267 : * (This is on this interface because it shares a lot of the implementation.
268 : * Maybe it should be extended to be more like ExecuteQuery without
269 : * the range parameter.)
270 : */
271 : virtual std::vector<entity_id_t> GetEntitiesByPlayer(player_id_t player) const = 0;
272 :
273 : /**
274 : * Returns a list of all entities of all players except gaia.
275 : */
276 : virtual std::vector<entity_id_t> GetNonGaiaEntities() const = 0;
277 :
278 : /**
279 : * Returns a list of all entities owned by a player or gaia.
280 : */
281 : virtual std::vector<entity_id_t> GetGaiaAndNonGaiaEntities() const = 0;
282 :
283 : /**
284 : * Toggle the rendering of debug info.
285 : */
286 : virtual void SetDebugOverlay(bool enabled) = 0;
287 :
288 : /**
289 : * Returns the mask for the specified identifier.
290 : */
291 : virtual std::uint8_t GetEntityFlagMask(const std::string& identifier) const = 0;
292 :
293 : /**
294 : * Set the flag specified by the identifier to the supplied value for the entity
295 : * @param ent the entity whose flags will be modified.
296 : * @param identifier the flag to be modified.
297 : * @param value to which the flag will be set.
298 : */
299 : virtual void SetEntityFlag(entity_id_t ent, const std::string& identifier, bool value) = 0;
300 :
301 :
302 : //////////////////////////////////////////////////////////////////
303 : //// LOS interface below this line ////
304 : //////////////////////////////////////////////////////////////////
305 :
306 : /**
307 : * Returns a CLosQuerier for checking whether vertex positions are visible to the given player
308 : * (or other players it shares LOS with).
309 : */
310 : virtual CLosQuerier GetLosQuerier(player_id_t player) const = 0;
311 :
312 : /**
313 : * Toggle the scripted Visibility component activation for entity ent.
314 : */
315 : virtual void ActivateScriptedVisibility(entity_id_t ent, bool status) = 0;
316 :
317 : /**
318 : * Returns the visibility status of the given entity, with respect to the given player.
319 : * Returns LosVisibility::HIDDEN if the entity doesn't exist or is not in the world.
320 : * This respects the GetLosRevealWholeMap flag.
321 : */
322 : virtual LosVisibility GetLosVisibility(CEntityHandle ent, player_id_t player) const = 0;
323 : virtual LosVisibility GetLosVisibility(entity_id_t ent, player_id_t player) const = 0;
324 :
325 : /**
326 : * Returns the visibility status of the given position, with respect to the given player.
327 : * This respects the GetLosRevealWholeMap flag.
328 : */
329 : virtual LosVisibility GetLosVisibilityPosition(entity_pos_t x, entity_pos_t z, player_id_t player) const = 0;
330 :
331 : /**
332 : * Request the update of the visibility cache of ent at next turn.
333 : * Typically used for fogging.
334 : */
335 : virtual void RequestVisibilityUpdate(entity_id_t ent) = 0;
336 :
337 :
338 : /**
339 : * GetLosVisibility wrapped for script calls.
340 : * Returns "hidden", "fogged" or "visible".
341 : */
342 : std::string GetLosVisibility_wrapper(entity_id_t ent, player_id_t player) const;
343 :
344 : /**
345 : * GetLosVisibilityPosition wrapped for script calls.
346 : * Returns "hidden", "fogged" or "visible".
347 : */
348 : std::string GetLosVisibilityPosition_wrapper(entity_pos_t x, entity_pos_t z, player_id_t player) const;
349 :
350 : /**
351 : * Explore the map (but leave it in the FoW) for player p
352 : */
353 : virtual void ExploreMap(player_id_t p) = 0;
354 :
355 : /**
356 : * Explore the tiles inside each player's territory.
357 : * This is done only at the beginning of the game.
358 : */
359 : virtual void ExploreTerritories() = 0;
360 :
361 : /**
362 : * Explore a circular area of the map for the given player.
363 : * This marks the tiles as explored (fogged) but not necessarily visible.
364 : */
365 : virtual void ExploreCircle(player_id_t p, entity_pos_t x, entity_pos_t z, fixed radius) = 0;
366 :
367 : /**
368 : * Reveal the shore for specified player p.
369 : * This works like for entities: if RevealShore is called multiple times with enabled, it
370 : * will be necessary to call it the same number of times with !enabled to make the shore
371 : * fall back into the FoW.
372 : */
373 : virtual void RevealShore(player_id_t p, bool enable) = 0;
374 :
375 : /**
376 : * Set whether the whole map should be made visible to the given player.
377 : */
378 : virtual void SetLosRevealWholeMap(player_id_t player, bool enabled) = 0;
379 :
380 : /**
381 : * Set whether the whole map should be made visible to all players.
382 : */
383 : virtual void SetLosRevealWholeMapForAll(bool enabled) = 0;
384 :
385 : /**
386 : * Returns whether the whole map has been made visible to the given player.
387 : */
388 : virtual bool GetLosRevealWholeMap(player_id_t player) const = 0;
389 :
390 : /**
391 : * Returns whether the whole map has been made visible to all players.
392 : */
393 : virtual bool GetLosRevealWholeMapForAll() const = 0;
394 :
395 : /**
396 : * Set the LOS to be restricted to a circular map.
397 : */
398 : virtual void SetLosCircular(bool enabled) = 0;
399 :
400 : /**
401 : * Returns whether the LOS is restricted to a circular map.
402 : */
403 : virtual bool GetLosCircular() const = 0;
404 :
405 : /**
406 : * Sets shared LOS data for player to the given list of players.
407 : */
408 : virtual void SetSharedLos(player_id_t player, const std::vector<player_id_t>& players) = 0;
409 :
410 : /**
411 : * Returns shared LOS mask for player.
412 : */
413 : virtual std::uint32_t GetSharedLosMask(player_id_t player) const = 0;
414 :
415 : /**
416 : * Get percent map explored statistics for specified player.
417 : */
418 : virtual std::uint8_t GetPercentMapExplored(player_id_t player) const = 0;
419 :
420 : /**
421 : * Get percent map explored statistics for specified set of players.
422 : * Note: this function computes statistics from scratch and should not be called too often.
423 : */
424 : virtual std::uint8_t GetUnionPercentMapExplored(const std::vector<player_id_t>& players) const = 0;
425 :
426 : /**
427 : * @return The number of LOS vertices.
428 : */
429 : virtual size_t GetVerticesPerSide() const = 0;
430 :
431 : /**
432 : * Perform some internal consistency checks for testing/debugging.
433 : */
434 : virtual void Verify() = 0;
435 :
436 13 : DECLARE_INTERFACE_TYPE(RangeManager)
437 : };
438 :
439 : #endif // INCLUDED_ICMPRANGEMANAGER
|