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