Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/components/CCmpUnitMotion.cpp
Show First 20 Lines • Show All 645 Lines • ▼ Show 20 Lines | private: | ||||
* Update goal position if moving target | * Update goal position if moving target | ||||
*/ | */ | ||||
void UpdateFinalGoal(); | void UpdateFinalGoal(); | ||||
/** | /** | ||||
* Returns whether we are close enough to the target to assume it's a good enough | * Returns whether we are close enough to the target to assume it's a good enough | ||||
* position to stop. | * position to stop. | ||||
*/ | */ | ||||
bool ShouldConsiderOurselvesAtDestination(const CFixedVector2D& from); | bool CloseEnoughFromDestinationToStop(const CFixedVector2D& from) const; | ||||
/** | /** | ||||
* Returns whether the length of the given path, plus the distance from | * Returns whether the length of the given path, plus the distance from | ||||
* 'from' to the first waypoints, it shorter than minDistance. | * 'from' to the first waypoints, it shorter than minDistance. | ||||
*/ | */ | ||||
bool PathIsShort(const WaypointPath& path, const CFixedVector2D& from, entity_pos_t minDistance) const; | bool PathIsShort(const WaypointPath& path, const CFixedVector2D& from, entity_pos_t minDistance) const; | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | if (m_ShortPath.m_Waypoints.empty()) | ||||
GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); | GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); | ||||
CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle()); | CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle()); | ||||
if (!cmpPosition || !cmpPosition->IsInWorld()) | if (!cmpPosition || !cmpPosition->IsInWorld()) | ||||
return; | return; | ||||
CFixedVector2D pos = cmpPosition->GetPosition2D(); | CFixedVector2D pos = cmpPosition->GetPosition2D(); | ||||
if (ShouldConsiderOurselvesAtDestination(pos)) | if (CloseEnoughFromDestinationToStop(pos)) | ||||
{ | |||||
StopMoving(); | |||||
MoveSucceeded(); | |||||
if (m_FacePointAfterMove) | |||||
FaceTowardsPointFromPos(pos, m_FinalGoal.x, m_FinalGoal.z); | |||||
return; | return; | ||||
} | |||||
UpdateFinalGoal(); | UpdateFinalGoal(); | ||||
RequestLongPath(pos, m_FinalGoal); | RequestLongPath(pos, m_FinalGoal); | ||||
m_PathState = PATHSTATE_WAITING_REQUESTING_LONG; | m_PathState = PATHSTATE_WAITING_REQUESTING_LONG; | ||||
return; | return; | ||||
} | } | ||||
// else we could, so reset our number of tries. | // else we could, so reset our number of tries. | ||||
▲ Show 20 Lines • Show All 162 Lines • ▼ Show 20 Lines | case PATHSTATE_FOLLOWING_REQUESTING_LONG: | ||||
} | } | ||||
if (wasObstructed) | if (wasObstructed) | ||||
{ | { | ||||
// Oops, we hit something (very likely another unit). | // Oops, we hit something (very likely another unit). | ||||
// This is when we might easily get stuck wrongly. | // This is when we might easily get stuck wrongly. | ||||
// check if we've arrived. | // check if we've arrived. | ||||
if (ShouldConsiderOurselvesAtDestination(pos)) | if (CloseEnoughFromDestinationToStop(pos)) | ||||
{ | |||||
StopMoving(); | |||||
MoveSucceeded(); | |||||
if (m_FacePointAfterMove) | |||||
FaceTowardsPointFromPos(pos, m_FinalGoal.x, m_FinalGoal.z); | |||||
return; | return; | ||||
} | |||||
// If we still have long waypoints, try and compute a short path | // If we still have long waypoints, try and compute a short path | ||||
// This will get us around units, amongst others. | // This will get us around units, amongst others. | ||||
// However in some cases a long waypoint will be in located in the obstruction of | // However in some cases a long waypoint will be in located in the obstruction of | ||||
// an idle unit. In that case, we need to scrap that waypoint or we might never be able to reach it. | // an idle unit. In that case, we need to scrap that waypoint or we might never be able to reach it. | ||||
// I am not sure why this happens but the following code seems to work. | // I am not sure why this happens but the following code seems to work. | ||||
if (!m_LongPath.m_Waypoints.empty()) | if (!m_LongPath.m_Waypoints.empty()) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 242 Lines • ▼ Show 20 Lines | if (IsFormationMember()) | ||||
return; | return; | ||||
CFixedVector2D targetPos; | CFixedVector2D targetPos; | ||||
if (!ComputeTargetPosition(targetPos)) | if (!ComputeTargetPosition(targetPos)) | ||||
return; | return; | ||||
m_FinalGoal.x = targetPos.X; | m_FinalGoal.x = targetPos.X; | ||||
m_FinalGoal.z = targetPos.Y; | m_FinalGoal.z = targetPos.Y; | ||||
} | } | ||||
bool CCmpUnitMotion::ShouldConsiderOurselvesAtDestination(const CFixedVector2D& from) | bool CCmpUnitMotion::CloseEnoughFromDestinationToStop(const CFixedVector2D& from) const | ||||
{ | { | ||||
if (m_TargetEntity != INVALID_ENTITY || m_FinalGoal.DistanceToPoint(from) > SHORT_PATH_GOAL_RADIUS) | if (m_TargetEntity != INVALID_ENTITY || m_FinalGoal.DistanceToPoint(from) > SHORT_PATH_GOAL_RADIUS) | ||||
return false; | return false; | ||||
StopMoving(); | |||||
MoveSucceeded(); | |||||
if (m_FacePointAfterMove) | |||||
FaceTowardsPointFromPos(from, m_FinalGoal.x, m_FinalGoal.z); | |||||
return true; | return true; | ||||
} | } | ||||
bool CCmpUnitMotion::PathIsShort(const WaypointPath& path, const CFixedVector2D& from, entity_pos_t minDistance) const | bool CCmpUnitMotion::PathIsShort(const WaypointPath& path, const CFixedVector2D& from, entity_pos_t minDistance) const | ||||
{ | { | ||||
CFixedVector2D prev = from; | CFixedVector2D prev = from; | ||||
entity_pos_t distLeft = minDistance; | entity_pos_t distLeft = minDistance; | ||||
▲ Show 20 Lines • Show All 578 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator