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

          Line data    Source code
       1             : /* Copyright (C) 2025 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_ICMPPOSITION
      19             : #define INCLUDED_ICMPPOSITION
      20             : 
      21             : #include "maths/Fixed.h"
      22             : #include "maths/FixedVector2D.h"
      23             : #include "maths/FixedVector3D.h"
      24             : #include "ps/CStr.h"
      25             : #include "simulation2/helpers/Position.h"
      26             : #include "simulation2/system/Component.h"
      27             : #include "simulation2/system/Entity.h"
      28             : #include "simulation2/system/Interface.h"
      29             : 
      30             : #include <js/Value.h>
      31             : #include <set>
      32             : 
      33             : class CMatrix3D;
      34             : 
      35             : /**
      36             :  * Represents an entity's position in the world (plus its orientation).
      37             :  *
      38             :  * Entity positions are determined by the following:
      39             :  *   - X, Z coordinates (giving left/right and front/back coordinates on the map)
      40             :  *   - Y offset (height; entities always snap to the ground, then are offset by this amount)
      41             :  *   - 'Floating' flag (snap to surface of water instead of going underneath)
      42             :  * As far as the simulation code is concerned, movements are instantaneous.
      43             :  * The only exception is GetInterpolatedTransform, used for rendering, which may
      44             :  * interpolate between the previous and current positions.
      45             :  * (The "Jump" methods circumvent the interpolation, and should be used whenever immediate
      46             :  * movement is needed.)
      47             :  *
      48             :  * Orientations consist of the following:
      49             :  *   - Rotation around upwards 'Y' axis (the common form of rotation)
      50             :  *   - Terrain conformance mode, one of:
      51             :  *     - Upright (upwards axis is always the world Y axis, e.g. for humans)
      52             :  *     - Pitch (rotates backwards and forwards to match the terrain, e.g. for horses)
      53             :  *     - Pitch-Roll (rotates in all directions to match the terrain, e.g. for carts)
      54             :  *     - Roll (rotates sideways to match the terrain)
      55             :  *      NOTE: terrain conformance is currently only a local, visual effect; it doesn't change
      56             :  *       the network synchronized rotation or the data returned by GetRotation
      57             :  *   - Rotation around relative X (pitch), Z (roll) axes (rare; used for special effects)
      58             :  *      NOTE: if XZ rotation is non-zero, it will override the terrain conformance mode
      59             :  *
      60             :  * Entities can also be 'outside the world' (e.g. hidden inside a building), in which
      61             :  * case they have no position. Callers <b>must</b> check the entity is in the world, before
      62             :  * querying its position.
      63             :  */
      64         210 : class ICmpPosition : public IComponent
      65             : {
      66             : public:
      67             :     /**
      68             :      * Set this as a turret of an other entity
      69             :      */
      70             :     virtual void SetTurretParent(entity_id_t parent, const CFixedVector3D& offset) = 0;
      71             : 
      72             :     /**
      73             :      * Get the turret parent of this entity
      74             :      */
      75             :     virtual entity_id_t GetTurretParent() const = 0;
      76             : 
      77             :     /**
      78             :      * Has to be called to update the simulation position of the turret
      79             :      */
      80             :     virtual void UpdateTurretPosition() = 0;
      81             : 
      82             :     /**
      83             :      * Get the list of turrets to read or edit
      84             :      */
      85             :     virtual std::set<entity_id_t>* GetTurrets() = 0;
      86             : 
      87             :     /**
      88             :      * Returns true if the entity currently exists at a defined position in the world.
      89             :      */
      90             :     virtual bool IsInWorld() const = 0;
      91             : 
      92             :     /**
      93             :      * Causes IsInWorld to return false. (Use MoveTo() or JumpTo() to move back into the world.)
      94             :      */
      95             :     virtual void MoveOutOfWorld() = 0;
      96             : 
      97             :     /**
      98             :      * Move smoothly to the given location.
      99             :      */
     100             :     virtual void MoveTo(entity_pos_t x, entity_pos_t z) = 0;
     101             : 
     102             :     /**
     103             :      * Combines MoveTo and TurnTo to avoid an uncessary "AdvertisePositionChange"
     104             :      */
     105             :     virtual void MoveAndTurnTo(entity_pos_t x, entity_pos_t z, entity_angle_t ry) = 0;
     106             : 
     107             :     /**
     108             :      * Move immediately to the given location, with no interpolation.
     109             :      */
     110             :     virtual void JumpTo(entity_pos_t x, entity_pos_t z) = 0;
     111             : 
     112             :     /**
     113             :      * Set the vertical offset above the terrain/water surface.
     114             :      */
     115             :     virtual void SetHeightOffset(entity_pos_t dy) = 0;
     116             : 
     117             :     /**
     118             :      * Returns the current vertical offset above the terrain/water surface.
     119             :      */
     120             :     virtual entity_pos_t GetHeightOffset() const = 0;
     121             : 
     122             :     /**
     123             :      * Set the vertical position above the map zero point
     124             :      */
     125             :     virtual void SetHeightFixed(entity_pos_t y) = 0;
     126             : 
     127             :     /**
     128             :      * Returns the current vertical offset above above the map zero point.
     129             :      */
     130             :     virtual entity_pos_t GetHeightFixed() const = 0;
     131             : 
     132             :     /**
     133             :      * Returns the vertical offset above above the map zero point
     134             :      * the unit would have at the given position.
     135             :      */
     136             :     virtual entity_pos_t GetHeightAtFixed(entity_pos_t x, entity_pos_t z) const = 0;
     137             : 
     138             :     /**
     139             :      * Returns true iff the entity will follow the terrain height (possibly with an offset)
     140             :      */
     141             :     virtual bool IsHeightRelative() const = 0;
     142             : 
     143             :     /**
     144             :      * When set to true, the entity will follow the terrain height (possibly with an offset)
     145             :      * When set to false, it's height won't change automatically
     146             :      */
     147             :     virtual void SetHeightRelative(bool flag) = 0;
     148             : 
     149             :     /**
     150             :      * Returns whether the entity can float on water.
     151             :      */
     152             :     virtual bool CanFloat() const = 0;
     153             : 
     154             :     /**
     155             :      * Set the entity to float on water
     156             :      */
     157             :     virtual void SetFloating(bool flag) = 0;
     158             : 
     159             :     /**
     160             :      * Set the entity to float on water, in a non-network-synchronised visual-only way.
     161             :      * (This is to support the 'floating' flag in actor XMLs.)
     162             :      */
     163             :     virtual void SetActorFloating(bool flag) = 0;
     164             : 
     165             : 
     166             :     /**
     167             :      * Set the entity's anchor type, in a non-network-synchronised visual-only way.
     168             :      * (This is to support the 'anchor' flag in actor XMLs.)
     169             :      */
     170             :      virtual void SetActorAnchor(const CStr& anchor) = 0;
     171             : 
     172             :     /**
     173             :      * Set construction progress of the model, this affects the rendered position of the model.
     174             :      * 0.0 will be fully underground, 1.0 will be fully visible, 0.5 will be half underground.
     175             :      */
     176             :     virtual void SetConstructionProgress(fixed progress) = 0;
     177             : 
     178             :     /**
     179             :      * Returns the current x,y,z position (no interpolation).
     180             :      * Depends on the current terrain heightmap.
     181             :      * Must not be called unless IsInWorld is true.
     182             :      */
     183             :     virtual CFixedVector3D GetPosition() const = 0;
     184             : 
     185             :     /**
     186             :      * Returns the current x,z position (no interpolation).
     187             :      * Must not be called unless IsInWorld is true.
     188             :      */
     189             :     virtual CFixedVector2D GetPosition2D() const = 0;
     190             : 
     191             :     /**
     192             :      * Returns the previous turn's x,y,z position (no interpolation).
     193             :      * Depends on the current terrain heightmap.
     194             :      * Must not be called unless IsInWorld is true.
     195             :      */
     196             :     virtual CFixedVector3D GetPreviousPosition() const = 0;
     197             : 
     198             :     /**
     199             :      * Returns the previous turn's x,z position (no interpolation).
     200             :      * Must not be called unless IsInWorld is true.
     201             :      */
     202             :     virtual CFixedVector2D GetPreviousPosition2D() const = 0;
     203             : 
     204             :     /**
     205             :      * Returns the turn rate in radians per second.
     206             :      */
     207             :     virtual fixed GetTurnRate() const = 0;
     208             : 
     209             :     /**
     210             :      * Rotate smoothly to the given angle around the upwards axis.
     211             :      * @param y clockwise radians from the +Z axis.
     212             :      */
     213             :     virtual void TurnTo(entity_angle_t y) = 0;
     214             : 
     215             :     /**
     216             :      * Rotate immediately to the given angle around the upwards axis.
     217             :      * @param y clockwise radians from the +Z axis.
     218             :      */
     219             :     virtual void SetYRotation(entity_angle_t y) = 0;
     220             : 
     221             :     /**
     222             :      * Rotate immediately to the given angles around the X (pitch) and Z (roll) axes.
     223             :      * @param x radians around the X axis. (TODO: in which direction?)
     224             :      * @param z radians around the Z axis.
     225             :      * @note if either x or z is non-zero, it will override terrain conformance mode
     226             :      */
     227             :     virtual void SetXZRotation(entity_angle_t x, entity_angle_t z) = 0;
     228             : 
     229             :     // NOTE: we separate Y from XZ because most code will only ever change Y;
     230             :     // XZ are typically just used in the editor, and other code should never
     231             :     // worry about them
     232             : 
     233             :     /**
     234             :      * Returns the current rotation (relative to the upwards axis), as Euler
     235             :      * angles with X=pitch, Y=yaw, Z=roll. (TODO: is that the right way round?)
     236             :      */
     237             :     virtual CFixedVector3D GetRotation() const = 0;
     238             : 
     239             :     /**
     240             :      * Returns the distance that the unit will be interpolated over,
     241             :      * i.e. the distance travelled since the start of the turn.
     242             :      */
     243             :     virtual fixed GetDistanceTravelled() const = 0;
     244             : 
     245             :     /**
     246             :      * Get the current interpolated 2D position and orientation, for rendering.
     247             :      * Must not be called unless IsInWorld is true.
     248             :      */
     249             :     virtual void GetInterpolatedPosition2D(float frameOffset, float& x, float& z, float& rotY) const = 0;
     250             : 
     251             :     /**
     252             :      * Get the current interpolated transform matrix, for rendering.
     253             :      * Must not be called unless IsInWorld is true.
     254             :      */
     255             :     virtual CMatrix3D GetInterpolatedTransform(float frameOffset) const = 0;
     256             : 
     257      203792 :     DECLARE_INTERFACE_TYPE(Position)
     258             : };
     259             : 
     260             : #endif // INCLUDED_ICMPPOSITION

Generated by: LCOV version 1.16