LCOV - code coverage report
Current view: top level - source/simulation2/components - ICmpObstructionManager.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 52 65 80.0 %
Date: 2026-12-02 01:21:50 Functions: 8 10 80.0 %

          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_ICMPOBSTRUCTIONMANAGER
      19             : #define INCLUDED_ICMPOBSTRUCTIONMANAGER
      20             : 
      21             : #include "lib/debug.h"
      22             : #include "maths/Fixed.h"
      23             : #include "maths/FixedVector2D.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 <js/Value.h>
      30             : #include <vector>
      31             : 
      32             : class IObstructionTestFilter;
      33             : class PathfinderPassability;
      34             : struct GridUpdateInformation;
      35             : template<typename T> class Grid;
      36             : 
      37             : using NavcellData = std::uint16_t;
      38             : 
      39             : /**
      40             :  * Obstruction manager: provides efficient spatial queries over objects in the world.
      41             :  *
      42             :  * The class deals with two types of shape:
      43             :  * "static" shapes, typically representing buildings, which are rectangles with a given
      44             :  * width and height and angle;
      45             :  * and "unit" shapes, representing units that can move around the world, which have a
      46             :  * radius and no rotation. (Units sometimes act as axis-aligned squares, sometimes
      47             :  * as approximately circles, due to the algorithm used by the short pathfinder.)
      48             :  *
      49             :  * Other classes (particularly ICmpObstruction) register shapes with this interface
      50             :  * and keep them updated.
      51             :  *
      52             :  * The @c Test functions provide exact collision tests.
      53             :  * The edge of a shape counts as 'inside' the shape, for the purpose of collisions.
      54             :  * The functions accept an IObstructionTestFilter argument, which can restrict the
      55             :  * set of shapes that are counted as collisions.
      56             :  *
      57             :  * Units can be marked as either moving or stationary, which simply determines whether
      58             :  * certain filters include or exclude them.
      59             :  *
      60             :  * The @c Rasterize function approximates the current set of shapes onto a 2D grid,
      61             :  * for use with tile-based pathfinding.
      62             :  */
      63             : class ICmpObstructionManager : public IComponent
      64             : {
      65             : public:
      66             :     /**
      67             :      * Standard representation for all types of shapes, for use with geometry processing code.
      68             :      */
      69       54219 :     struct ObstructionSquare
      70             :     {
      71             :         entity_pos_t x, z; // position of center
      72             :         CFixedVector2D u, v; // 'horizontal' and 'vertical' orthogonal unit vectors, representing orientation
      73             :         entity_pos_t hw, hh; // half width, half height of square
      74             :     };
      75             : 
      76             :     /**
      77             :      * External identifiers for shapes.
      78             :      * (This is a struct rather than a raw std::uint32_t for type-safety.)
      79             :      */
      80             :     struct tag_t
      81             :     {
      82       14010 :         tag_t() : n(0) {}
      83       41566 :         explicit tag_t(std::uint32_t n) : n(n) {}
      84       16236 :         bool valid() const { return n != 0; }
      85             : 
      86             :         std::uint32_t n;
      87             :     };
      88             : 
      89             :     /**
      90             :      * Boolean flags affecting the obstruction behaviour of a shape.
      91             :      */
      92             :     enum EFlags
      93             :     {
      94             :         FLAG_BLOCK_MOVEMENT           = (1 << 0), // prevents units moving through this shape
      95             :         FLAG_BLOCK_FOUNDATION         = (1 << 1), // prevents foundations being placed on this shape
      96             :         FLAG_BLOCK_CONSTRUCTION       = (1 << 2), // prevents buildings being constructed on this shape
      97             :         FLAG_BLOCK_PATHFINDING        = (1 << 3), // prevents the tile pathfinder choosing paths through this shape
      98             :         FLAG_MOVING                   = (1 << 4), // reserved for unitMotion - see usage there.
      99             :         FLAG_DELETE_UPON_CONSTRUCTION = (1 << 5)  // this entity is deleted when construction of a building placed on top of this entity starts
     100             :     };
     101             : 
     102             :     /**
     103             :      * Bitmask of EFlag values.
     104             :      */
     105             :     typedef std::uint8_t flags_t;
     106             : 
     107             :     /**
     108             :      * Set the bounds of the world.
     109             :      * Any point outside the bounds is considered obstructed.
     110             :      * @param x0,z0,x1,z1 Coordinates of the corners of the world
     111             :      */
     112             :     virtual void SetBounds(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1) = 0;
     113             : 
     114             :     /**
     115             :      * Register a static shape.
     116             :      *
     117             :      * @param ent entity ID associated with this shape (or INVALID_ENTITY if none)
     118             :      * @param x,z coordinates of center, in world space
     119             :      * @param a angle of rotation (clockwise from +Z direction)
     120             :      * @param w width (size along X axis)
     121             :      * @param h height (size along Z axis)
     122             :      * @param flags a set of EFlags values
     123             :      * @param group primary control group of the shape. Must be a valid control group ID.
     124             :      * @param group2 Optional; secondary control group of the shape. Defaults to INVALID_ENTITY.
     125             :      * @return a valid tag for manipulating the shape
     126             :      * @see StaticShape
     127             :      */
     128             :     virtual tag_t AddStaticShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_angle_t a,
     129             :         entity_pos_t w, entity_pos_t h, flags_t flags, entity_id_t group, entity_id_t group2 = INVALID_ENTITY) = 0;
     130             : 
     131             :     /**
     132             :      * Register a unit shape.
     133             :      *
     134             :      * @param ent entity ID associated with this shape (or INVALID_ENTITY if none)
     135             :      * @param x,z coordinates of center, in world space
     136             :      * @param clearance pathfinding clearance of the unit (works as a radius)
     137             :      * @param flags a set of EFlags values
     138             :      * @param group control group (typically the owner entity, or a formation controller entity
     139             :      *  - units ignore collisions with others in the same group)
     140             :      * @return a valid tag for manipulating the shape
     141             :      * @see UnitShape
     142             :      */
     143             :     virtual tag_t AddUnitShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_pos_t clearance,
     144             :         flags_t flags, entity_id_t group) = 0;
     145             : 
     146             :     /**
     147             :      * Adjust the position and angle of an existing shape.
     148             :      * @param tag tag of shape (must be valid)
     149             :      * @param x X coordinate of center, in world space
     150             :      * @param z Z coordinate of center, in world space
     151             :      * @param a angle of rotation (clockwise from +Z direction); ignored for unit shapes
     152             :      */
     153             :     virtual void MoveShape(tag_t tag, entity_pos_t x, entity_pos_t z, entity_angle_t a) = 0;
     154             : 
     155             :     /**
     156             :      * Set whether a unit shape is moving or stationary.
     157             :      * @param tag tag of shape (must be valid and a unit shape)
     158             :      * @param moving whether the unit is currently moving through the world or is stationary
     159             :      */
     160             :     virtual void SetUnitMovingFlag(tag_t tag, bool moving) = 0;
     161             : 
     162             :     /**
     163             :      * Set the control group of a unit shape.
     164             :      * @param tag tag of shape (must be valid and a unit shape)
     165             :      * @param group control group entity ID
     166             :      */
     167             :     virtual void SetUnitControlGroup(tag_t tag, entity_id_t group) = 0;
     168             : 
     169             :     /**
     170             :      * Sets the control group of a static shape.
     171             :      * @param tag Tag of the shape to set the control group for. Must be a valid and static shape tag.
     172             :      * @param group Control group entity ID.
     173             :      */
     174             :     virtual void SetStaticControlGroup(tag_t tag, entity_id_t group, entity_id_t group2) = 0;
     175             : 
     176             :     /**
     177             :      * Remove an existing shape. The tag will be made invalid and must not be used after this.
     178             :      * @param tag tag of shape (must be valid)
     179             :      */
     180             :     virtual void RemoveShape(tag_t tag) = 0;
     181             : 
     182             :     /**
     183             :      * Returns the distance from the obstruction to the point (px, pz), or -1 if the entity is out of the world.
     184             :      */
     185             :     virtual fixed DistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const = 0;
     186             : 
     187             :     /**
     188             :      * Calculate the largest straight line distance between the entity and the point.
     189             :      */
     190             :     virtual fixed MaxDistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const = 0;
     191             : 
     192             :     /**
     193             :      * Calculate the shortest distance between the entity and the target.
     194             :      */
     195             :     virtual fixed DistanceToTarget(entity_id_t ent, entity_id_t target) const = 0;
     196             : 
     197             :     /**
     198             :      * Calculate the largest straight line distance between the entity and the target.
     199             :      */
     200             :     virtual fixed MaxDistanceToTarget(entity_id_t ent, entity_id_t target) const = 0;
     201             : 
     202             :     /**
     203             :      * Calculate the shortest straight line distance between the source and the target
     204             :      */
     205             :     virtual fixed DistanceBetweenShapes(const ObstructionSquare& source, const ObstructionSquare& target) const = 0;
     206             : 
     207             :     /**
     208             :      * Calculate the largest straight line distance between the source and the target
     209             :      */
     210             :     virtual fixed MaxDistanceBetweenShapes(const ObstructionSquare& source, const ObstructionSquare& target) const = 0;
     211             : 
     212             :     /**
     213             :      * Check if the given entity is in range of the other point given those parameters.
     214             :      * @param maxRange - Can be a nonnegative decimal, ALWAYS_IN_RANGE or NEVER_IN_RANGE.
     215             :      */
     216             :     virtual 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 = 0;
     217             : 
     218             :     /**
     219             :      * Check if the given entity is in range of the target given those parameters.
     220             :      * @param maxRange - Can be a nonnegative decimal, ALWAYS_IN_RANGE or NEVER_IN_RANGE.
     221             :      */
     222             :     virtual bool IsInTargetRange(entity_id_t ent, entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const = 0;
     223             : 
     224             :     /**
     225             :      * Check if the given entity is in parabolic range of the target given those parameters.
     226             :      * @param maxRange - Can be a nonnegative decimal, ALWAYS_IN_RANGE or NEVER_IN_RANGE.
     227             :      */
     228             :     virtual 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 = 0;
     229             : 
     230             :     /**
     231             :      * Check if the given point is in range of the other point given those parameters.
     232             :      * @param maxRange - Can be a nonnegative decimal, ALWAYS_IN_RANGE or NEVER_IN_RANGE.
     233             :      */
     234             :     virtual 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 = 0;
     235             : 
     236             :     /**
     237             :      * Check if the given shape is in range of the target shape given those parameters.
     238             :      * @param maxRange - Can be a nonnegative decimal, ALWAYS_IN_RANGE or NEVER_IN_RANGE.
     239             :      */
     240             :     virtual bool AreShapesInRange(const ObstructionSquare& source, const ObstructionSquare& target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const = 0;
     241             : 
     242             :     /**
     243             :      * Collision test a flat-ended thick line against the current set of shapes.
     244             :      * The line caps extend by @p r beyond the end points.
     245             :      * Only intersections going from outside to inside a shape are counted.
     246             :      * @param filter filter to restrict the shapes that are counted
     247             :      * @param x0 X coordinate of line's first point
     248             :      * @param z0 Z coordinate of line's first point
     249             :      * @param x1 X coordinate of line's second point
     250             :      * @param z1 Z coordinate of line's second point
     251             :      * @param r radius (half width) of line
     252             :      * @param relaxClearanceForUnits whether unit-unit collisions should be more permissive.
     253             :      * @return true if there is a collision
     254             :      */
     255             :     virtual 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) const = 0;
     256             : 
     257             :     virtual 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 = 0;
     258             : 
     259             :     /**
     260             :      * Collision test a static square shape against the current set of shapes.
     261             :      * @param filter filter to restrict the shapes that are being tested against
     262             :      * @param x X coordinate of center
     263             :      * @param z Z coordinate of center
     264             :      * @param a angle of rotation (clockwise from +Z direction)
     265             :      * @param w width (size along X axis)
     266             :      * @param h height (size along Z axis)
     267             :      * @param out if non-NULL, all colliding shapes' entities will be added to this list
     268             :      * @return true if there is a collision
     269             :      */
     270             :     virtual bool TestStaticShape(const IObstructionTestFilter& filter,
     271             :         entity_pos_t x, entity_pos_t z, entity_pos_t a, entity_pos_t w, entity_pos_t h,
     272             :         std::vector<entity_id_t>* out) const = 0;
     273             : 
     274             :     /**
     275             :      * Collision test a unit shape against the current set of registered shapes, and optionally writes a list of the colliding
     276             :      * shapes' entities to an output list.
     277             :      *
     278             :      * @param filter filter to restrict the shapes that are being tested against
     279             :      * @param x X coordinate of shape's center
     280             :      * @param z Z coordinate of shape's center
     281             :      * @param clearance clearance of the shape's unit
     282             :      * @param out if non-NULL, all colliding shapes' entities will be added to this list
     283             :      *
     284             :      * @return true if there is a collision
     285             :      */
     286             :     virtual bool TestUnitShape(const IObstructionTestFilter& filter,
     287             :         entity_pos_t x, entity_pos_t z, entity_pos_t clearance,
     288             :         std::vector<entity_id_t>* out) const = 0;
     289             : 
     290             :     /**
     291             :      * Convert the current set of shapes onto a navcell grid, for all passability classes contained in @p passClasses.
     292             :      * If @p fullUpdate is false, the function will only go through dirty shapes.
     293             :      * Shapes are expanded by the @p passClasses clearances, by ORing their masks onto the @p grid.
     294             :      */
     295             :     virtual void Rasterize(Grid<NavcellData>& grid, const std::vector<PathfinderPassability>& passClasses, bool fullUpdate) = 0;
     296             : 
     297             :     /**
     298             :      * Gets dirtiness information and resets it afterwards. Then it's the role of CCmpPathfinder
     299             :      * to pass the information to other components if needed. (AIs, etc.)
     300             :      * The return value is false if an update is unnecessary.
     301             :      */
     302             :     virtual void UpdateInformations(GridUpdateInformation& informations) = 0;
     303             : 
     304             :     /**
     305             :      * Find all the obstructions that are inside (or partially inside) the given range.
     306             :      * @param filter filter to restrict the shapes that are counted
     307             :      * @param x0 X coordinate of left edge of range
     308             :      * @param z0 Z coordinate of bottom edge of range
     309             :      * @param x1 X coordinate of right edge of range
     310             :      * @param z1 Z coordinate of top edge of range
     311             :      * @param squares output list of obstructions
     312             :      */
     313             :     virtual 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 = 0;
     314             :     virtual 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 = 0;
     315             :     virtual 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 = 0;
     316             :     virtual void GetStaticObstructionsOnObstruction(const ObstructionSquare& square, std::vector<entity_id_t>& out, const IObstructionTestFilter& filter) const = 0;
     317             : 
     318             :     /**
     319             :      * Returns the entity IDs of all unit shapes that intersect the given
     320             :      * obstruction square, filtering out using the given filter.
     321             :      * @param square the Obstruction squre we want to compare with.
     322             :      * @param out output list of obstructions
     323             :      * @param filter filter for the obstructing units
     324             :      * @param strict whether to be strict in the check or more permissive (ie rasterize more or less). Default false.
     325             :      */
     326             :     virtual void GetUnitsOnObstruction(const ObstructionSquare& square, std::vector<entity_id_t>& out, const IObstructionTestFilter& filter, bool strict = false) const = 0;
     327             : 
     328             :     /**
     329             :      * Get the obstruction square representing the given shape.
     330             :      * @param tag tag of shape (must be valid)
     331             :      */
     332             :     virtual ObstructionSquare GetObstruction(tag_t tag) const = 0;
     333             : 
     334             :     virtual ObstructionSquare GetUnitShapeObstruction(entity_pos_t x, entity_pos_t z, entity_pos_t clearance) const = 0;
     335             : 
     336             :     virtual ObstructionSquare GetStaticShapeObstruction(entity_pos_t x, entity_pos_t z, entity_angle_t a, entity_pos_t w, entity_pos_t h) const = 0;
     337             : 
     338             :     /**
     339             :      * Set the passability to be restricted to a circular map.
     340             :      */
     341             :     virtual void SetPassabilityCircular(bool enabled) = 0;
     342             : 
     343             :     virtual bool GetPassabilityCircular() const = 0;
     344             : 
     345             :     /**
     346             :      * Toggle the rendering of debug info.
     347             :      */
     348             :     virtual void SetDebugOverlay(bool enabled) = 0;
     349             : 
     350      107763 :     DECLARE_INTERFACE_TYPE(ObstructionManager)
     351             : };
     352             : 
     353             : /**
     354             :  * Interface for ICmpObstructionManager @c Test functions to filter out unwanted shapes.
     355             :  */
     356       14663 : class IObstructionTestFilter
     357             : {
     358             : public:
     359             :     typedef ICmpObstructionManager::tag_t tag_t;
     360             :     typedef ICmpObstructionManager::flags_t flags_t;
     361             : 
     362       14659 :     virtual ~IObstructionTestFilter() {}
     363             : 
     364             :     /**
     365             :      * Return true if the shape with the specified parameters should be tested for collisions.
     366             :      * This is called for all shapes that would collide, and also for some that wouldn't.
     367             :      *
     368             :      * @param tag tag of shape being tested
     369             :      * @param flags set of EFlags for the shape
     370             :      * @param group the control group of the shape (typically the shape's unit, or the unit's formation controller, or 0)
     371             :      * @param group2 an optional secondary control group of the shape, or INVALID_ENTITY if none specified. Currently
     372             :      *               exists only for static shapes.
     373             :      */
     374             :     virtual bool TestShape(tag_t tag, flags_t flags, entity_id_t group, entity_id_t group2) const = 0;
     375             : };
     376             : 
     377             : /**
     378             :  * Obstruction test filter that will test against all shapes.
     379             :  */
     380           3 : class NullObstructionFilter : public IObstructionTestFilter
     381             : {
     382             : public:
     383          30 :     virtual bool TestShape(tag_t, flags_t /*flags*/, entity_id_t /*group*/, entity_id_t /*group2*/) const
     384             :     {
     385          30 :         return true;
     386             :     }
     387             : };
     388             : 
     389             : /**
     390             :  * Obstruction test filter that will test only against stationary (i.e. non-moving) shapes.
     391             :  */
     392           1 : class StationaryOnlyObstructionFilter : public IObstructionTestFilter
     393             : {
     394             : public:
     395           6 :     virtual bool TestShape(tag_t, flags_t flags, entity_id_t /*group*/, entity_id_t /*group2*/) const
     396             :     {
     397           6 :         return !(flags & ICmpObstructionManager::FLAG_MOVING);
     398             :     }
     399             : };
     400             : 
     401             : /**
     402             :  * Obstruction test filter that reject shapes in a given control group,
     403             :  * and rejects shapes that don't block unit movement, and optionally rejects moving shapes.
     404             :  */
     405         867 : class ControlGroupMovementObstructionFilter : public IObstructionTestFilter
     406             : {
     407             :     bool m_AvoidMoving;
     408             :     entity_id_t m_Group;
     409             : 
     410             : public:
     411         867 :     ControlGroupMovementObstructionFilter(bool avoidMoving, entity_id_t group) :
     412         867 :         m_AvoidMoving(avoidMoving), m_Group(group)
     413             :     {}
     414             : 
     415        3084 :     virtual bool TestShape(tag_t, flags_t flags, entity_id_t group, entity_id_t group2) const
     416             :     {
     417        3084 :         if (group == m_Group || (group2 != INVALID_ENTITY && group2 == m_Group))
     418             :             return false;
     419             : 
     420        2217 :         if (!(flags & ICmpObstructionManager::FLAG_BLOCK_MOVEMENT))
     421             :             return false;
     422             : 
     423        2211 :         if ((flags & ICmpObstructionManager::FLAG_MOVING) && !m_AvoidMoving)
     424             :             return false;
     425             : 
     426             :         return true;
     427             :     }
     428             : };
     429             : 
     430             : /**
     431             :  * Obstruction test filter that will test only against shapes that:
     432             :  *     - are part of neither one of the specified control groups
     433             :  *     - AND, depending on the value of the 'exclude' argument:
     434             :  *       - have at least one of the specified flags set.
     435             :  *       - OR have none of the specified flags set.
     436             :  *
     437             :  * The first (primary) control group to reject shapes from must be specified and valid. The secondary
     438             :  * control group to reject entities from may be set to INVALID_ENTITY to not use it.
     439             :  *
     440             :  * This filter is useful to e.g. allow foundations within the same control group to be placed and
     441             :  * constructed arbitrarily close together (e.g. for wall pieces that need to link up tightly).
     442             :  */
     443           1 : class SkipControlGroupsRequireFlagObstructionFilter : public IObstructionTestFilter
     444             : {
     445             :     bool m_Exclude;
     446             :     entity_id_t m_Group;
     447             :     entity_id_t m_Group2;
     448             :     flags_t m_Mask;
     449             : 
     450             : public:
     451           0 :     SkipControlGroupsRequireFlagObstructionFilter(bool exclude, entity_id_t group1, entity_id_t group2, flags_t mask) :
     452           0 :         m_Exclude(exclude), m_Group(group1), m_Group2(group2), m_Mask(mask)
     453             :     {
     454           0 :         Init();
     455           0 :     }
     456             : 
     457           6 :     SkipControlGroupsRequireFlagObstructionFilter(entity_id_t group1, entity_id_t group2, flags_t mask) :
     458           6 :         m_Exclude(false), m_Group(group1), m_Group2(group2), m_Mask(mask)
     459             :     {
     460           6 :         Init();
     461           6 :     }
     462             : 
     463          40 :     virtual bool TestShape(tag_t, flags_t flags, entity_id_t group, entity_id_t group2) const
     464             :     {
     465             :         // Don't test shapes that share one or more of our control groups.
     466          40 :         if (group == m_Group || group == m_Group2 || (group2 != INVALID_ENTITY &&
     467           2 :                 (group2 == m_Group || group2 == m_Group2)))
     468             :             return false;
     469             : 
     470             :         // If m_Exclude is true, don't test against shapes that have any of the
     471             :         // obstruction flags specified in m_Mask.
     472          18 :         if (m_Exclude)
     473           0 :             return (flags & m_Mask) == 0;
     474             : 
     475             :         // Otherwise, only include shapes that match at least one flag in m_Mask.
     476          18 :         return (flags & m_Mask) != 0;
     477             :     }
     478             : private:
     479           6 :     void Init()
     480             :     {
     481             :         // the primary control group to filter out must be valid
     482           6 :         ENSURE(m_Group != INVALID_ENTITY);
     483             : 
     484             :         // for simplicity, if m_Group2 is INVALID_ENTITY (i.e. not used), then set it equal to m_Group
     485             :         // so that we have fewer special cases to consider in TestShape().
     486           6 :         if (m_Group2 == INVALID_ENTITY)
     487           3 :             m_Group2 = m_Group;
     488           6 :     }
     489             : };
     490             : 
     491             : /**
     492             :  * Obstruction test filter that will test only against shapes that:
     493             :  *     - are part of both of the specified control groups
     494             :  *     - AND have at least one of the specified flags set.
     495             :  *
     496             :  * The first (primary) control group to include shapes from must be specified and valid.
     497             :  *
     498             :  * This filter is useful for preventing entities with identical control groups
     499             :  * from colliding e.g. building a new wall segment on top of an existing wall)
     500             :  *
     501             :  * @todo This filter needs test cases.
     502             :  */
     503           0 : class SkipTagRequireControlGroupsAndFlagObstructionFilter : public IObstructionTestFilter
     504             : {
     505             :     tag_t m_Tag;
     506             :     entity_id_t m_Group;
     507             :     entity_id_t m_Group2;
     508             :     flags_t m_Mask;
     509             : 
     510             : public:
     511           0 :     SkipTagRequireControlGroupsAndFlagObstructionFilter(tag_t tag, entity_id_t group1, entity_id_t group2, flags_t mask) :
     512           0 :         m_Tag(tag), m_Group(group1), m_Group2(group2), m_Mask(mask)
     513             :     {
     514           0 :         ENSURE(m_Group != INVALID_ENTITY);
     515           0 :     }
     516             : 
     517           0 :     virtual bool TestShape(tag_t tag, flags_t flags, entity_id_t group, entity_id_t group2) const
     518             :     {
     519             :         // To be included in testing, a shape must not have the specified tag, and must
     520             :         // match at least one of the flags in m_Mask, as well as both control groups.
     521           0 :         return (tag.n != m_Tag.n && (flags & m_Mask) != 0 && ((group == m_Group
     522           0 :             && group2 == m_Group2) || (group2 == m_Group && group == m_Group2)));
     523             :     }
     524             : };
     525             : 
     526             : /**
     527             :  * Obstruction test filter that will test only against shapes that do not have the specified tag set.
     528             :  */
     529           2 : class SkipTagObstructionFilter : public IObstructionTestFilter
     530             : {
     531             :     tag_t m_Tag;
     532             : public:
     533           2 :     SkipTagObstructionFilter(tag_t tag) : m_Tag(tag)
     534             :     {
     535             :     }
     536             : 
     537           6 :     virtual bool TestShape(tag_t tag, flags_t, entity_id_t /*group*/, entity_id_t /*group2*/) const
     538             :     {
     539           6 :         return tag.n != m_Tag.n;
     540             :     }
     541             : };
     542             : 
     543             : /**
     544             :  * Similar to ControlGroupMovementObstructionFilter, but also ignoring a specific tag. See D3482 for why this exists.
     545             :  */
     546       13785 : class SkipTagAndControlGroupObstructionFilter : public IObstructionTestFilter
     547             : {
     548             :     entity_id_t m_Group;
     549             :     tag_t m_Tag;
     550             :     bool m_AvoidMoving;
     551             : 
     552             : public:
     553       13785 :     SkipTagAndControlGroupObstructionFilter(tag_t tag, bool avoidMoving,  entity_id_t group) :
     554       13785 :     m_Tag(tag), m_Group(group), m_AvoidMoving(avoidMoving)
     555             :     {}
     556             : 
     557       38382 :     virtual bool TestShape(tag_t tag, flags_t flags, entity_id_t group, entity_id_t group2) const
     558             :     {
     559       38382 :         if (tag.n == m_Tag.n)
     560             :             return false;
     561             : 
     562       34467 :         if (group == m_Group || (group2 != INVALID_ENTITY && group2 == m_Group))
     563             :             return false;
     564             : 
     565       20682 :         if ((flags & ICmpObstructionManager::FLAG_MOVING) && !m_AvoidMoving)
     566             :             return false;
     567             : 
     568        1452 :         if (!(flags & ICmpObstructionManager::FLAG_BLOCK_MOVEMENT))
     569          42 :             return false;
     570             : 
     571             :         return true;
     572             :     }
     573             : };
     574             : 
     575             : 
     576             : /**
     577             :  * Obstruction test filter that will test only against shapes that:
     578             :  *    - do not have the specified tag
     579             :  *    - AND have at least one of the specified flags set.
     580             :  */
     581           1 : class SkipTagRequireFlagsObstructionFilter : public IObstructionTestFilter
     582             : {
     583             :     tag_t m_Tag;
     584             :     flags_t m_Mask;
     585             : public:
     586           3 :     SkipTagRequireFlagsObstructionFilter(tag_t tag, flags_t mask) : m_Tag(tag), m_Mask(mask)
     587             :     {
     588             :     }
     589             : 
     590          18 :     virtual bool TestShape(tag_t tag, flags_t flags, entity_id_t /*group*/, entity_id_t /*group2*/) const
     591             :     {
     592          18 :         return (tag.n != m_Tag.n && (flags & m_Mask) != 0);
     593             :     }
     594             : };
     595             : 
     596             : #endif // INCLUDED_ICMPOBSTRUCTIONMANAGER

Generated by: LCOV version 1.16