Ticket #2094: MoveOpt.patch

File MoveOpt.patch, 5.0 KB (added by wraitii, 10 years ago)
  • source/simulation2/components/CCmpPosition.cpp

     
    238238
    239239        AdvertisePositionChanges();
    240240    }
     241   
     242    virtual void MoveAndTurnTo(entity_pos_t x, entity_pos_t z, entity_angle_t ry)
     243    {
     244        m_X = x;
     245        m_Z = z;
     246        m_RotY = ry;
     247       
     248        if (!m_InWorld)
     249        {
     250            m_InWorld = true;
     251            m_LastX = m_PrevX = m_X;
     252            m_LastZ = m_PrevZ = m_Z;
     253            m_LastYOffset = m_YOffset;
     254        }
     255       
     256        AdvertisePositionChanges();
     257    }
    241258
    242259    virtual void JumpTo(entity_pos_t x, entity_pos_t z)
    243260    {
  • source/simulation2/components/CCmpUnitMotion.cpp

     
    802802    }
    803803
    804804    if (m_State == STATE_IDLE)
    805     {
    806805        return;
    807     }
    808806
    809807    switch (m_PathState)
    810808    {
     
    870868        // We want to move (at most) maxSpeed*dt units from pos towards the next waypoint
    871869
    872870        fixed timeLeft = dt;
    873 
    874         while (timeLeft > fixed::Zero())
     871        fixed zero = fixed::Zero();
     872       
     873        while (timeLeft > zero)
    875874        {
    876875            // If we ran out of short path, we have to stop
    877876            if (m_ShortPath.m_Waypoints.empty())
     
    880879            CFixedVector2D target(m_ShortPath.m_Waypoints.back().x, m_ShortPath.m_Waypoints.back().z);
    881880            CFixedVector2D offset = target - pos;
    882881
    883             // Face towards the target
    884             if (!offset.IsZero())
    885             {
    886                 entity_angle_t angle = atan2_approx(offset.X, offset.Y);
    887                 cmpPosition->TurnTo(angle);
    888             }
    889 
    890882            // Work out how far we can travel in timeLeft
    891883            fixed maxdist = maxSpeed.Multiply(timeLeft);
    892884
     
    933925
    934926        // Update the Position component after our movement (if we actually moved anywhere)
    935927        if (pos != initialPos)
    936             cmpPosition->MoveTo(pos.X, pos.Y);
     928        {
     929            CFixedVector2D offset = pos - initialPos;
     930           
     931            // Face towards the target
     932            entity_angle_t angle = atan2_approx(offset.X, offset.Y);
     933            cmpPosition->MoveAndTurnTo(pos.X,pos.Y, angle);
    937934
    938         // Calculate the mean speed over this past turn.
    939         m_CurSpeed = cmpPosition->GetDistanceTravelled() / dt;
    940 
     935            // Calculate the mean speed over this past turn.
     936            m_CurSpeed = cmpPosition->GetDistanceTravelled() / dt;
     937        }
     938       
    941939        if (wasObstructed)
    942940        {
    943941            // Oops, we hit something (very likely another unit).
    944942            // Stop, and recompute the whole path.
    945943            // TODO: if the target has UnitMotion and is higher priority,
    946944            // we should wait a little bit.
    947 
     945           
     946            m_CurSpeed = zero;
    948947            RequestLongPath(pos, m_FinalGoal);
    949948            m_PathState = PATHSTATE_WAITING_REQUESTING_LONG;
    950949
     
    11381137
    11391138bool CCmpUnitMotion::PathIsShort(const ICmpPathfinder::Path& path, CFixedVector2D from, entity_pos_t minDistance)
    11401139{
    1141     CFixedVector2D pos = from;
    11421140    entity_pos_t distLeft = minDistance;
    11431141
    11441142    for (ssize_t i = (ssize_t)path.m_Waypoints.size()-1; i >= 0; --i)
    11451143    {
    11461144        // Check if the next path segment is longer than the requested minimum
    11471145        CFixedVector2D waypoint(path.m_Waypoints[i].x, path.m_Waypoints[i].z);
    1148         CFixedVector2D delta = waypoint - pos;
     1146        CFixedVector2D delta = waypoint - from;
    11491147        if (delta.CompareLength(distLeft) > 0)
    11501148            return false;
    11511149
    11521150        // Still short enough - prepare to check the next segment
    11531151        distLeft -= delta.Length();
    1154         pos = waypoint;
     1152        from = waypoint;
    11551153    }
    11561154
    11571155    // Reached the end of the path before exceeding minDistance
  • source/simulation2/components/ICmpPosition.cpp

     
    2525DEFINE_INTERFACE_METHOD_0("IsInWorld", bool, ICmpPosition, IsInWorld)
    2626DEFINE_INTERFACE_METHOD_0("MoveOutOfWorld", void, ICmpPosition, MoveOutOfWorld)
    2727DEFINE_INTERFACE_METHOD_2("MoveTo", void, ICmpPosition, MoveTo, entity_pos_t, entity_pos_t)
     28DEFINE_INTERFACE_METHOD_3("MoveAndTurnTo", void, ICmpPosition, MoveAndTurnTo, entity_pos_t, entity_pos_t, entity_pos_t)
    2829DEFINE_INTERFACE_METHOD_2("JumpTo", void, ICmpPosition, JumpTo, entity_pos_t, entity_pos_t)
    2930DEFINE_INTERFACE_METHOD_1("SetHeightOffset", void, ICmpPosition, SetHeightOffset, entity_pos_t)
    3031DEFINE_INTERFACE_METHOD_0("GetHeightOffset", entity_pos_t, ICmpPosition, GetHeightOffset)
  • source/simulation2/components/ICmpPosition.h

     
    7474    virtual void MoveTo(entity_pos_t x, entity_pos_t z) = 0;
    7575
    7676    /**
     77     * Combines MoveTo and TurnTo to avoid an uncessary "AdvertisePositionChange"
     78     */
     79    virtual void MoveAndTurnTo(entity_pos_t x, entity_pos_t z, entity_angle_t ry) = 0;
     80
     81    /**
    7782     * Move immediately to the given location, with no interpolation.
    7883     */
    7984    virtual void JumpTo(entity_pos_t x, entity_pos_t z) = 0;