1
1
forked from 0ad/0ad

Remove the now useless UnitMotion planning, and cleanup of CCmpUnitMotion.cpp (unused variables, whitespace). Fixes #3790.

This should have a noticeable impact on performance (in the good way!)

Thanks mimo for noticing something was off with the planning system!

This was SVN commit r17866.
This commit is contained in:
Nicolas Auvray 2016-03-12 13:44:51 +00:00
parent 27b4df0978
commit a4a1bcab94

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games. /* Copyright (C) 2016 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -109,29 +109,6 @@ static const fixed CHECK_TARGET_MOVEMENT_MIN_COS = fixed::FromInt(866)/1000;
static const CColor OVERLAY_COLOR_LONG_PATH(1, 1, 1, 1); static const CColor OVERLAY_COLOR_LONG_PATH(1, 1, 1, 1);
static const CColor OVERLAY_COLOR_SHORT_PATH(1, 0, 0, 1); static const CColor OVERLAY_COLOR_SHORT_PATH(1, 0, 0, 1);
struct SUnitMotionPlanning
{
WaypointPath nextStepShortPath; // if !nextStepClean, store a short path for the next step here
u32 expectedPathTicket;
bool nextStepClean; // is there any obstruction between the next two long waypoints?
SUnitMotionPlanning() : expectedPathTicket(0), nextStepClean(true) {}
};
/**
* Serialization helper template for SUnitMotionPlanning
*/
struct SerializeUnitMotionPlanning
{
template<typename S>
void operator()(S& serialize, const char* UNUSED(name), SUnitMotionPlanning& value)
{
SerializeVector<SerializeWaypoint>()(serialize, "next step short path", value.nextStepShortPath.m_Waypoints);
serialize.NumberU32_Unbounded("expected path ticket", value.expectedPathTicket);
serialize.Bool("next step clean", value.nextStepClean);
}
};
class CCmpUnitMotion : public ICmpUnitMotion class CCmpUnitMotion : public ICmpUnitMotion
{ {
public: public:
@ -264,7 +241,6 @@ public:
// Motion planning // Motion planning
u8 m_Tries; // how many tries we've done to get to our current Final Goal. u8 m_Tries; // how many tries we've done to get to our current Final Goal.
SUnitMotionPlanning m_Planning;
PathGoal m_FinalGoal; PathGoal m_FinalGoal;
@ -336,7 +312,6 @@ public:
m_ExpectedPathTicket = 0; m_ExpectedPathTicket = 0;
m_Tries = 0; m_Tries = 0;
m_Planning = SUnitMotionPlanning();
m_TargetEntity = INVALID_ENTITY; m_TargetEntity = INVALID_ENTITY;
@ -377,8 +352,6 @@ public:
SerializeVector<SerializeWaypoint>()(serialize, "long path", m_LongPath.m_Waypoints); SerializeVector<SerializeWaypoint>()(serialize, "long path", m_LongPath.m_Waypoints);
SerializeVector<SerializeWaypoint>()(serialize, "short path", m_ShortPath.m_Waypoints); SerializeVector<SerializeWaypoint>()(serialize, "short path", m_ShortPath.m_Waypoints);
SerializeUnitMotionPlanning()(serialize, "planning", m_Planning);
SerializeGoal()(serialize, "goal", m_FinalGoal); SerializeGoal()(serialize, "goal", m_FinalGoal);
} }
@ -633,13 +606,6 @@ private:
*/ */
void Move(fixed dt); void Move(fixed dt);
/**
* Analyze our current path, and check if we expect to be obstructed soon
* If yes, try to anticipate.
* TODO: remove this and use a more general "pushing" manager.
*/
void PlanNextStep(const CFixedVector2D& pos, const CFixedVector2D& currentOffset);
/** /**
* Decide whether to approximate the given range from a square target as a circle, * Decide whether to approximate the given range from a square target as a circle,
* rather than as a square. * rather than as a square.
@ -697,7 +663,7 @@ private:
* noTarget is true only when used inside tryGoingStraightToTargetEntity, * noTarget is true only when used inside tryGoingStraightToTargetEntity,
* in which case we do not want the target obstruction otherwise it would always fail * in which case we do not want the target obstruction otherwise it would always fail
*/ */
ControlGroupMovementObstructionFilter GetObstructionFilter(bool forceAvoidMovingUnits = false, bool noTarget = false) const; ControlGroupMovementObstructionFilter GetObstructionFilter(bool noTarget = false) const;
/** /**
* Start moving to the given goal, from our current position 'from'. * Start moving to the given goal, from our current position 'from'.
@ -735,16 +701,6 @@ void CCmpUnitMotion::PathResult(u32 ticket, const WaypointPath& path)
m_Moving = false; m_Moving = false;
if (ticket == m_Planning.expectedPathTicket)
{
// If no path was found, better cancel the planning
if (path.m_Waypoints.empty())
m_Planning = SUnitMotionPlanning();
m_Planning.nextStepShortPath = path;
return;
}
// Ignore obsolete path requests // Ignore obsolete path requests
if (ticket != m_ExpectedPathTicket) if (ticket != m_ExpectedPathTicket)
return; return;
@ -842,10 +798,8 @@ void CCmpUnitMotion::PathResult(u32 ticket, const WaypointPath& path)
m_Moving = true; m_Moving = true;
} }
else else
{
LOGWARNING("unexpected PathResult (%u %d %d)", GetEntityId(), m_State, m_PathState); LOGWARNING("unexpected PathResult (%u %d %d)", GetEntityId(), m_State, m_PathState);
} }
}
void CCmpUnitMotion::Move(fixed dt) void CCmpUnitMotion::Move(fixed dt)
{ {
@ -976,19 +930,13 @@ void CCmpUnitMotion::Move(fixed dt)
target = pos + offset; target = pos + offset;
if (cmpPathfinder->CheckMovement(GetObstructionFilter(), pos.X, pos.Y, target.X, target.Y, m_Clearance, m_PassClass)) if (cmpPathfinder->CheckMovement(GetObstructionFilter(), pos.X, pos.Y, target.X, target.Y, m_Clearance, m_PassClass))
{
PlanNextStep(pos, offset);
pos = target; pos = target;
break;
}
else else
{ wasObstructed = true; // Error - path was obstructed
// Error - path was obstructed
wasObstructed = true;
break; break;
} }
} }
}
// Update the Position component after our movement (if we actually moved anywhere) // Update the Position component after our movement (if we actually moved anywhere)
if (pos != initialPos) if (pos != initialPos)
@ -1032,7 +980,7 @@ void CCmpUnitMotion::Move(fixed dt)
square.z = m_LongPath.m_Waypoints.back().z; square.z = m_LongPath.m_Waypoints.back().z;
std::vector<entity_id_t> unitOnGoal; std::vector<entity_id_t> unitOnGoal;
// don't ignore moving units as those might be units like us, ie not really moving. // don't ignore moving units as those might be units like us, ie not really moving.
cmpObstructionManager->GetUnitsOnObstruction(square, unitOnGoal, GetObstructionFilter(false, false), true); cmpObstructionManager->GetUnitsOnObstruction(square, unitOnGoal, GetObstructionFilter(), true);
if (!unitOnGoal.empty()) if (!unitOnGoal.empty())
m_LongPath.m_Waypoints.pop_back(); m_LongPath.m_Waypoints.pop_back();
} }
@ -1119,41 +1067,6 @@ void CCmpUnitMotion::Move(fixed dt)
} }
} }
void CCmpUnitMotion::PlanNextStep(const CFixedVector2D& pos, const CFixedVector2D& currentOffset)
{
if (m_LongPath.m_Waypoints.empty())
return;
CmpPtr<ICmpPathfinder> cmpPathfinder(GetSystemEntity());
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSystemEntity());
if (!cmpPathfinder || !cmpObstructionManager)
return;
m_Planning = SUnitMotionPlanning();
// see 2 turns in advance, otherwise this would start to lag in MP
CFixedVector2D futurePos = pos + currentOffset*2;
// Don't actually use CheckMovement since we want to check against units only, we assume the rest is taken care of.
if (!cmpObstructionManager->TestLine(GetObstructionFilter(true, false), pos.X, pos.Y, futurePos.X, futurePos.Y, m_Clearance, true))
return;
// we will run into a static unit obstruction. Try to shortpath around it.
PathGoal goal;
if (m_LongPath.m_Waypoints.size() > 1 || m_FinalGoal.DistanceToPoint(pos) > LONG_PATH_MIN_DIST)
goal = { PathGoal::POINT, m_LongPath.m_Waypoints.back().x, m_LongPath.m_Waypoints.back().z };
else
{
UpdateFinalGoal();
goal = m_FinalGoal;
m_LongPath.m_Waypoints.clear();
CFixedVector2D target = goal.NearestPointOnGoal(pos);
m_LongPath.m_Waypoints.emplace_back(Waypoint{ target.X, target.Y });
}
RequestShortPath(pos, goal, false);
m_PathState = PATHSTATE_FOLLOWING_REQUESTING_SHORT;
}
bool CCmpUnitMotion::ComputeTargetPosition(CFixedVector2D& out) bool CCmpUnitMotion::ComputeTargetPosition(CFixedVector2D& out)
{ {
if (m_TargetEntity == INVALID_ENTITY) if (m_TargetEntity == INVALID_ENTITY)
@ -1230,7 +1143,7 @@ bool CCmpUnitMotion::TryGoingStraightToTargetEntity(const CFixedVector2D& from)
CFixedVector2D goalPos = goal.NearestPointOnGoal(from); CFixedVector2D goalPos = goal.NearestPointOnGoal(from);
// Check if there's any collisions on that route // Check if there's any collisions on that route
if (!cmpPathfinder->CheckMovement(GetObstructionFilter(false, true), from.X, from.Y, goalPos.X, goalPos.Y, m_Clearance, m_PassClass)) if (!cmpPathfinder->CheckMovement(GetObstructionFilter(true), from.X, from.Y, goalPos.X, goalPos.Y, m_Clearance, m_PassClass))
return false; return false;
// That route is okay, so update our path // That route is okay, so update our path
@ -1365,10 +1278,10 @@ void CCmpUnitMotion::FaceTowardsPointFromPos(const CFixedVector2D& pos, entity_p
} }
} }
ControlGroupMovementObstructionFilter CCmpUnitMotion::GetObstructionFilter(bool forceAvoidMovingUnits, bool noTarget) const ControlGroupMovementObstructionFilter CCmpUnitMotion::GetObstructionFilter(bool noTarget) const
{ {
entity_id_t group = noTarget ? m_TargetEntity : GetGroup(); entity_id_t group = noTarget ? m_TargetEntity : GetGroup();
return ControlGroupMovementObstructionFilter(forceAvoidMovingUnits || ShouldAvoidMovingUnits(), group); return ControlGroupMovementObstructionFilter(ShouldAvoidMovingUnits(), group);
} }
@ -1559,7 +1472,7 @@ bool CCmpUnitMotion::IsInPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSystemEntity()); CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSystemEntity());
ICmpObstructionManager::ObstructionSquare obstruction; ICmpObstructionManager::ObstructionSquare obstruction;
//TODO if (cmpObstructionManager) //TODO if (cmpObstructionManager)
// hasObstruction = cmpObstructionManager->FindMostImportantObstruction(GetObstructionFilter(true), x, z, m_Radius, obstruction); // hasObstruction = cmpObstructionManager->FindMostImportantObstruction(GetObstructionFilter(), x, z, m_Radius, obstruction);
if (minRange.IsZero() && maxRange.IsZero() && hasObstruction) if (minRange.IsZero() && maxRange.IsZero() && hasObstruction)
{ {