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_ICMPUNITMOTION 19 : #define INCLUDED_ICMPUNITMOTION 20 : 21 : #include "maths/Fixed.h" 22 : #include "maths/FixedVector2D.h" 23 : #include "simulation2/helpers/Pathfinding.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 <string> 31 : 32 : /** 33 : * Motion interface for entities with complex movement capabilities. 34 : * (Simpler motion is handled by ICmpMotion instead.) 35 : * 36 : * It should eventually support different movement speeds, moving to areas 37 : * instead of points, moving as part of a group, moving as part of a formation, 38 : * etc. 39 : */ 40 0 : class ICmpUnitMotion : public IComponent 41 : { 42 : public: 43 : 44 : /** 45 : * Attempt to walk into range of a to a given point, or as close as possible. 46 : * The range is measured from the center of the unit. 47 : * If cannot move anywhere at all, or if there is some other error, then returns false. 48 : * Otherwise, returns true. 49 : * If maxRange is negative, then the maximum range is treated as infinity. 50 : */ 51 : virtual bool MoveToPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t minRange, entity_pos_t maxRange) = 0; 52 : 53 : /** 54 : * Attempt to walk into range of a given target entity, or as close as possible. 55 : * The range is measured between approximately the edges of the unit and the target, so that 56 : * maxRange=0 is not unreachably close to the target. 57 : * If the unit cannot move anywhere at all, or if there is some other error, then returns false. 58 : * Otherwise, returns true. 59 : * If maxRange is negative, then the maximum range is treated as infinity. 60 : */ 61 : virtual bool MoveToTargetRange(entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange) = 0; 62 : 63 : /** 64 : * Join a formation, and move towards a given offset relative to the formation controller entity. 65 : * The unit will remain 'in formation' fromthe perspective of UnitMotion 66 : * until SetMemberOfFormation(INVALID_ENTITY) is passed. 67 : */ 68 : virtual void MoveToFormationOffset(entity_id_t controller, entity_pos_t x, entity_pos_t z) = 0; 69 : 70 : /** 71 : * Set/unset the unit as a formation member. 72 : * @param controller - if INVALID_ENTITY, the unit is no longer a formation member. Otherwise it is and this is the controller. 73 : */ 74 : virtual void SetMemberOfFormation(entity_id_t controller) = 0; 75 : 76 : /** 77 : * Check if the target is reachable. 78 : * Don't take this as absolute gospel since there are things that the pathfinder may not detect, such as 79 : * entity obstructions in the way, but in general it should return satisfactory results. 80 : * The interface is similar to MoveToTargetRange but the move is not attempted. 81 : * @return true if the target is assumed reachable, false otherwise. 82 : */ 83 : virtual bool IsTargetRangeReachable(entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange) = 0; 84 : 85 : /** 86 : * Returns true if we are possibly at our destination. 87 : * Since the concept of being at destination is dependent on why the move was requested, 88 : * UnitMotion can only ever hint about this, hence the conditional tone. 89 : */ 90 : virtual bool PossiblyAtDestination() const = 0; 91 : 92 : /** 93 : * Turn to look towards the given point. 94 : */ 95 : virtual void FaceTowardsPoint(entity_pos_t x, entity_pos_t z) = 0; 96 : 97 : /** 98 : * Stop moving immediately. 99 : */ 100 : virtual void StopMoving() = 0; 101 : 102 : /** 103 : * Get the speed at the end of the current turn. 104 : */ 105 : virtual fixed GetCurrentSpeed() const = 0; 106 : 107 : /** 108 : * Set the speed. 109 : */ 110 : virtual void SetCurrentSpeed(const fixed& speed) = 0; 111 : 112 : /** 113 : * Get the current formation offset if this unit is moving as a formation member. 114 : * @returns std::nullopt if the unit is not in formation movement mode, 115 : * otherwise returns the formation offset. 116 : */ 117 : virtual std::optional<CFixedVector2D> GetFormationOffset() const = 0; 118 : 119 : /** 120 : * @returns true if the unit has a destination. 121 : */ 122 : virtual bool IsMoveRequested() const = 0; 123 : 124 : /** 125 : * @returns true if the unit is moving orderly in it's Formation. 126 : */ 127 : virtual bool IsMovingAsFormation() const = 0; 128 : 129 : /** 130 : * Get the unit template walk speed after modifications. 131 : */ 132 : virtual fixed GetWalkSpeed() const = 0; 133 : 134 : /** 135 : * Get the unit template running (i.e. max) speed after modifications. 136 : */ 137 : virtual fixed GetRunMultiplier() const = 0; 138 : 139 : /** 140 : * Returns the ratio of GetSpeed() / GetWalkSpeed(). 141 : */ 142 : virtual fixed GetSpeedMultiplier() const = 0; 143 : 144 : /** 145 : * Set the current movement speed. 146 : * @param speed A multiplier of GetWalkSpeed(). 147 : */ 148 : virtual void SetSpeedMultiplier(fixed multiplier) = 0; 149 : 150 : /** 151 : * Get the speed at which the unit intends to move. 152 : * (regardless of whether the unit is moving or not right now). 153 : */ 154 : virtual fixed GetSpeed() const = 0; 155 : 156 : /** 157 : * @return the estimated position of the unit in @param dt seconds, 158 : * following current paths. This is allowed to 'look into the future'. 159 : */ 160 : virtual CFixedVector2D EstimateFuturePosition(const fixed dt) const = 0; 161 : 162 : /** 163 : * Get the current acceleration. 164 : */ 165 : virtual fixed GetAcceleration() const = 0; 166 : 167 : /** 168 : * Set the current acceleration. 169 : * @param acceleration The acceleration. 170 : */ 171 : virtual void SetAcceleration(fixed acceleration) = 0; 172 : 173 : /** 174 : * Set whether the unit will turn to face the target point after finishing moving. 175 : */ 176 : virtual void SetFacePointAfterMove(bool facePointAfterMove) = 0; 177 : 178 : virtual bool GetFacePointAfterMove() const = 0; 179 : 180 : /** 181 : * Get the unit's passability class. 182 : */ 183 : virtual pass_class_t GetPassabilityClass() const = 0; 184 : 185 : /** 186 : * Get the passability class name (as defined in pathfinder.xml) 187 : */ 188 : virtual std::string GetPassabilityClassName() const = 0; 189 : 190 : /** 191 : * Sets the passability class name (as defined in pathfinder.xml) 192 : */ 193 : virtual void SetPassabilityClassName(const std::string& passClassName) = 0; 194 : 195 : /** 196 : * Get the unit clearance (used by the Obstruction component) 197 : */ 198 : virtual entity_pos_t GetUnitClearance() const = 0; 199 : 200 : /** 201 : * Toggle the rendering of debug info. 202 : */ 203 : virtual void SetDebugOverlay(bool enabled) = 0; 204 : 205 16560 : DECLARE_INTERFACE_TYPE(UnitMotion) 206 : }; 207 : 208 : #endif // INCLUDED_ICMPUNITMOTION