Index: ps/trunk/source/simulation2/components/CCmpUnitMotion.cpp =================================================================== --- ps/trunk/source/simulation2/components/CCmpUnitMotion.cpp +++ ps/trunk/source/simulation2/components/CCmpUnitMotion.cpp @@ -563,24 +563,40 @@ void MoveFailed() { - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - CMessageMotionChanged msg(true); GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); } void MoveSucceeded() { - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - CMessageMotionChanged msg(false); GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); } + /** + * Update other components on our speed. + * This doesn't use messages for efficiency. + * This should only be called when speed changes. + */ + void UpdateMovementState(entity_pos_t speed) + { + CmpPtr cmpObstruction(GetEntityHandle()); + // Moved last turn, didn't this turn. + if (speed == fixed::Zero() && m_CurSpeed > fixed::Zero()) + { + if (cmpObstruction) + cmpObstruction->SetMovingFlag(false); + } + // Moved this turn, didn't last turn + else if (speed > fixed::Zero() && m_CurSpeed == fixed::Zero()) + { + if (cmpObstruction) + cmpObstruction->SetMovingFlag(true); + } + + m_CurSpeed = speed; + } + bool MoveToPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t minRange, entity_pos_t maxRange, entity_id_t target); /** @@ -681,11 +697,6 @@ void CCmpUnitMotion::PathResult(u32 ticket, const WaypointPath& path) { - // reset our state for sanity. - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - // Ignore obsolete path requests if (ticket != m_ExpectedPathTicket) return; @@ -718,9 +729,6 @@ m_LongPath.m_Waypoints.emplace_back(Waypoint{ m_FinalGoal.x, m_FinalGoal.z }); m_PathState = PATHSTATE_FOLLOWING; - - if (cmpObstruction) - cmpObstruction->SetMovingFlag(true); } else if (m_PathState == PATHSTATE_WAITING_REQUESTING_SHORT || m_PathState == PATHSTATE_FOLLOWING_REQUESTING_SHORT) { @@ -767,9 +775,6 @@ m_Tries = 0; m_PathState = PATHSTATE_FOLLOWING; - - if (cmpObstruction) - cmpObstruction->SetMovingFlag(true); } else LOGWARNING("unexpected PathResult (%u %d %d)", GetEntityId(), m_State, m_PathState); @@ -784,6 +789,9 @@ m_State = STATE_IDLE; MoveSucceeded(); m_CurSpeed = fixed::Zero(); + CmpPtr cmpObstruction(GetEntityHandle()); + if (cmpObstruction) + cmpObstruction->SetMovingFlag(false); return; } @@ -898,7 +906,7 @@ // Update our speed over this turn so that the visual actor shows the correct animation. if (pos == initialPos) - m_CurSpeed = fixed::Zero(); + UpdateMovementState(fixed::Zero()); else { // Update the Position component after our movement (if we actually moved anywhere) @@ -909,7 +917,7 @@ cmpPosition->MoveAndTurnTo(pos.X,pos.Y, angle); // Calculate the mean speed over this past turn. - m_CurSpeed = cmpPosition->GetDistanceTravelled() / dt; + UpdateMovementState(offset.Length() / dt); } if (wasObstructed) @@ -991,10 +999,6 @@ CmpPtr cmpUnitMotion(GetSimContext(), m_MoveRequest.m_Entity); if (cmpUnitMotion && !cmpUnitMotion->IsMoving()) { - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - CMessageMotionChanged msg(false); GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); } @@ -1240,10 +1244,6 @@ // reset our state for sanity. m_ExpectedPathTicket = 0; - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - m_PathState = PATHSTATE_NONE; #if DISABLE_PATHFINDER