mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-17 05:44:08 -07:00
This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
181 lines
4.9 KiB
C++
181 lines
4.9 KiB
C++
/* Copyright (C) 2021 Wildfire Games.
|
|
* This file is part of 0 A.D.
|
|
*
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef INCLUDED_CCMPUNITMOTIONMANAGER
|
|
#define INCLUDED_CCMPUNITMOTIONMANAGER
|
|
|
|
#include "simulation2/system/Component.h"
|
|
#include "ICmpUnitMotionManager.h"
|
|
|
|
#include "simulation2/MessageTypes.h"
|
|
#include "simulation2/components/ICmpTerrain.h"
|
|
#include "simulation2/helpers/Grid.h"
|
|
#include "simulation2/system/EntityMap.h"
|
|
|
|
class CCmpUnitMotion;
|
|
|
|
class CCmpUnitMotionManager : public ICmpUnitMotionManager
|
|
{
|
|
public:
|
|
static void ClassInit(CComponentManager& componentManager)
|
|
{
|
|
componentManager.SubscribeToMessageType(MT_TerrainChanged);
|
|
componentManager.SubscribeToMessageType(MT_TurnStart);
|
|
componentManager.SubscribeToMessageType(MT_Update_Final);
|
|
componentManager.SubscribeToMessageType(MT_Update_MotionUnit);
|
|
componentManager.SubscribeToMessageType(MT_Update_MotionFormation);
|
|
}
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(UnitMotionManager)
|
|
|
|
// Persisted state for each unit.
|
|
struct MotionState
|
|
{
|
|
MotionState(CmpPtr<ICmpPosition> cmpPos, CCmpUnitMotion* cmpMotion);
|
|
|
|
// Component references - these must be kept alive for the duration of motion.
|
|
// NB: this is generally not something one should do, but because of the tight coupling here it's doable.
|
|
CmpPtr<ICmpPosition> cmpPosition;
|
|
CCmpUnitMotion* cmpUnitMotion;
|
|
|
|
// Position before units start moving
|
|
CFixedVector2D initialPos;
|
|
// Transient position during the movement.
|
|
CFixedVector2D pos;
|
|
|
|
// Accumulated "pushing" from nearby units.
|
|
CFixedVector2D push;
|
|
|
|
fixed initialAngle;
|
|
fixed angle;
|
|
|
|
// Used for formations - units with the same control group won't push at a distance.
|
|
// (this is required because formations may be tight and large units may end up never settling.
|
|
entity_id_t controlGroup = INVALID_ENTITY;
|
|
|
|
// Meta-flag -> this entity won't push nor be pushed.
|
|
// (used for entities that have their obstruction disabled).
|
|
bool ignore = false;
|
|
|
|
// If true, the entity needs to be handled during movement.
|
|
bool needUpdate = false;
|
|
|
|
bool wentStraight = false;
|
|
bool wasObstructed = false;
|
|
|
|
// Clone of the obstruction manager flag for efficiency
|
|
bool isMoving = false;
|
|
};
|
|
|
|
EntityMap<MotionState> m_Units;
|
|
EntityMap<MotionState> m_FormationControllers;
|
|
|
|
// The vectors are cleared each frame.
|
|
Grid<std::vector<EntityMap<MotionState>::iterator>> m_MovingUnits;
|
|
|
|
bool m_ComputingMotion;
|
|
|
|
static std::string GetSchema()
|
|
{
|
|
return "<a:component type='system'/><empty/>";
|
|
}
|
|
|
|
virtual void Init(const CParamNode& UNUSED(paramNode))
|
|
{
|
|
}
|
|
|
|
virtual void Deinit()
|
|
{
|
|
}
|
|
|
|
virtual void Serialize(ISerializer& UNUSED(serialize))
|
|
{
|
|
}
|
|
|
|
virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize))
|
|
{
|
|
Init(paramNode);
|
|
ResetSubdivisions();
|
|
}
|
|
|
|
virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
|
|
{
|
|
switch (msg.GetType())
|
|
{
|
|
case MT_TerrainChanged:
|
|
{
|
|
CmpPtr<ICmpTerrain> cmpTerrain(GetSystemEntity());
|
|
if (cmpTerrain->GetVerticesPerSide() != m_MovingUnits.width())
|
|
ResetSubdivisions();
|
|
break;
|
|
}
|
|
case MT_TurnStart:
|
|
{
|
|
OnTurnStart();
|
|
break;
|
|
}
|
|
case MT_Update_MotionFormation:
|
|
{
|
|
fixed dt = static_cast<const CMessageUpdate_MotionFormation&>(msg).turnLength;
|
|
m_ComputingMotion = true;
|
|
MoveFormations(dt);
|
|
m_ComputingMotion = false;
|
|
break;
|
|
}
|
|
case MT_Update_MotionUnit:
|
|
{
|
|
fixed dt = static_cast<const CMessageUpdate_MotionUnit&>(msg).turnLength;
|
|
m_ComputingMotion = true;
|
|
MoveUnits(dt);
|
|
m_ComputingMotion = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
virtual void Register(CCmpUnitMotion* component, entity_id_t ent, bool formationController);
|
|
virtual void Unregister(entity_id_t ent);
|
|
|
|
virtual bool ComputingMotion() const
|
|
{
|
|
return m_ComputingMotion;
|
|
}
|
|
|
|
private:
|
|
void ResetSubdivisions();
|
|
void OnTurnStart();
|
|
|
|
void MoveUnits(fixed dt);
|
|
void MoveFormations(fixed dt);
|
|
void Move(EntityMap<MotionState>& ents, fixed dt);
|
|
|
|
void Push(EntityMap<MotionState>::value_type& a, EntityMap<MotionState>::value_type& b, fixed dt);
|
|
};
|
|
|
|
void CCmpUnitMotionManager::ResetSubdivisions()
|
|
{
|
|
CmpPtr<ICmpTerrain> cmpTerrain(GetSystemEntity());
|
|
if (!cmpTerrain)
|
|
return;
|
|
|
|
size_t size = cmpTerrain->GetVerticesPerSide() - 1;
|
|
m_MovingUnits.resize(size * TERRAIN_TILE_SIZE / 20 + 1, size * TERRAIN_TILE_SIZE / 20 + 1);
|
|
}
|
|
|
|
REGISTER_COMPONENT_TYPE(UnitMotionManager)
|
|
|
|
#endif // INCLUDED_CCMPUNITMOTIONMANAGER
|