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_CCMPUNITMOTIONMANAGER 19 : #define INCLUDED_CCMPUNITMOTIONMANAGER 20 : 21 : #include "maths/Fixed.h" 22 : #include "maths/FixedVector2D.h" 23 : #include "simulation2/components/ICmpUnitMotionManager.h" 24 : #include "simulation2/helpers/Grid.h" 25 : #include "simulation2/helpers/Position.h" 26 : #include "simulation2/system/Component.h" 27 : #include "simulation2/system/Entity.h" 28 : #include "simulation2/system/EntityMap.h" 29 : 30 : #include <cstdint> 31 : #include <string> 32 : #include <vector> 33 : 34 : class CCmpUnitMotion; 35 : class ICmpPosition; 36 : 37 7 : class CCmpUnitMotionManager final : public ICmpUnitMotionManager 38 : { 39 : public: 40 : static void ClassInit(CComponentManager& componentManager); 41 : 42 14 : DEFAULT_COMPONENT_ALLOCATOR(UnitMotionManager) 43 : 44 : /** 45 : * Maximum value for pushing pressure. 46 : */ 47 : static constexpr int MAX_PRESSURE = 255; 48 : 49 : // Persisted state for each unit. 50 : struct MotionState 51 : { 52 : MotionState(ICmpPosition* cmpPos, CCmpUnitMotion* cmpMotion); 53 : 54 : // Component references - these must be kept alive for the duration of motion. 55 : // NB: this is generally a super dangerous thing to do, 56 : // but the tight coupling with CCmpUnitMotion makes it workable. 57 : // NB: this assumes that components do _not_ move in memory, 58 : // which is currently a fair assumption but might change in the future. 59 : ICmpPosition* cmpPosition; 60 : CCmpUnitMotion* cmpUnitMotion; 61 : 62 : // Position before units start moving 63 : CFixedVector2D initialPos; 64 : // Transient position during the movement. 65 : CFixedVector2D pos; 66 : 67 : // Accumulated "pushing" from nearby units. 68 : CFixedVector2D push; 69 : 70 : fixed speed; 71 : 72 : fixed initialAngle; 73 : fixed angle; 74 : 75 : // Used for formations - units with the same control group won't push at a distance. 76 : // (this is required because formations may be tight and large units may end up never settling. 77 : entity_id_t controlGroup = INVALID_ENTITY; 78 : 79 : // This is a ad-hoc counter to store under how much pushing 'pressure' an entity is. 80 : // More pressure will slow the unit down and make it harder to push, 81 : // which effectively bogs down groups of colliding units. 82 : uint8_t pushingPressure = 0; 83 : 84 : // Meta-flag -> this entity won't push nor be pushed. 85 : // (used for entities that have their obstruction disabled). 86 : bool ignore = false; 87 : 88 : // If true, the entity needs to be handled during movement. 89 : bool needUpdate = false; 90 : 91 : bool wentStraight = false; 92 : bool wasObstructed = false; 93 : 94 : // Clone of the obstruction manager flag for efficiency 95 : bool isMoving = false; 96 : }; 97 : 98 : // "Template" state, not serialized (cannot be changed mid-game). 99 : 100 : // The maximal distance at which units push each other is the combined unit clearances, multipled by this factor, 101 : // itself pre-multiplied by the circle-square correction factor. 102 : entity_pos_t m_PushingRadiusMultiplier; 103 : // Additive modifiers to the maximum pushing distance for moving units and idle units respectively. 104 : entity_pos_t m_MovingPushExtension; 105 : entity_pos_t m_StaticPushExtension; 106 : // Multiplier for the pushing 'spread'. 107 : // This should be understand as the % of the maximum distance where pushing will be "in full force". 108 : entity_pos_t m_MovingPushingSpread; 109 : entity_pos_t m_StaticPushingSpread; 110 : 111 : // Pushing forces below this value are ignored - this prevents units moving forever by very small increments. 112 : entity_pos_t m_MinimalPushing; 113 : 114 : // Multiplier for pushing pressure strength. 115 : entity_pos_t m_PushingPressureStrength; 116 : // Per-turn reduction in pushing pressure. 117 : entity_pos_t m_PushingPressureDecay; 118 : 119 : // These vectors are reconstructed on deserialization. 120 : 121 : EntityMap<MotionState> m_Units; 122 : EntityMap<MotionState> m_FormationControllers; 123 : 124 : // Turn-local state below, not serialised. 125 : 126 : Grid<std::vector<EntityMap<MotionState>::iterator>> m_MovingUnits; 127 : bool m_ComputingMotion; 128 : 129 137 : static std::string GetSchema() 130 : { 131 274 : return "<a:component type='system'/><empty/>"; 132 : } 133 : 134 : void Init(const CParamNode&) override; 135 : 136 7 : void Deinit() override 137 : { 138 7 : } 139 : 140 : void Serialize(ISerializer& serialize) override; 141 : void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) override; 142 : 143 : void HandleMessage(const CMessage& msg, bool global) override; 144 : 145 : void Register(CCmpUnitMotion* component, entity_id_t ent, bool formationController) override; 146 : void Unregister(entity_id_t ent) override; 147 : 148 14493 : bool ComputingMotion() const override 149 : { 150 14493 : return m_ComputingMotion; 151 : } 152 : 153 729 : bool IsPushingActivated() const override 154 : { 155 540 : return m_PushingRadiusMultiplier != entity_pos_t::Zero(); 156 : } 157 : 158 : private: 159 : void OnDeserialized(); 160 : void ResetSubdivisions(); 161 : void OnTurnStart(); 162 : 163 : void MoveUnits(fixed dt); 164 : void MoveFormations(fixed dt); 165 : void Move(EntityMap<MotionState>& ents, fixed dt); 166 : 167 : void Push(EntityMap<MotionState>::value_type& a, EntityMap<MotionState>::value_type& b, fixed dt); 168 : }; 169 : 170 274 : REGISTER_COMPONENT_TYPE(UnitMotionManager) 171 : 172 : #endif // INCLUDED_CCMPUNITMOTIONMANAGER