Index: source/simulation2/components/CCmpUnitMotion.cpp =================================================================== --- source/simulation2/components/CCmpUnitMotion.cpp +++ source/simulation2/components/CCmpUnitMotion.cpp @@ -568,24 +568,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); /** @@ -686,11 +702,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; @@ -723,9 +734,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) { @@ -772,9 +780,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); @@ -789,6 +794,9 @@ m_State = STATE_IDLE; MoveSucceeded(); m_CurSpeed = fixed::Zero(); + CmpPtr cmpObstruction(GetEntityHandle()); + if (cmpObstruction) + cmpObstruction->SetMovingFlag(false); return; } @@ -903,7 +911,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) @@ -914,7 +922,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) @@ -996,10 +1004,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); } @@ -1244,10 +1248,6 @@ // reset our state for sanity. m_ExpectedPathTicket = 0; - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - m_PathState = PATHSTATE_NONE; #if DISABLE_PATHFINDER