Ticket #2094: MoveOpt.patch
File MoveOpt.patch, 5.0 KB (added by , 10 years ago) |
---|
-
source/simulation2/components/CCmpPosition.cpp
238 238 239 239 AdvertisePositionChanges(); 240 240 } 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 } 241 258 242 259 virtual void JumpTo(entity_pos_t x, entity_pos_t z) 243 260 { -
source/simulation2/components/CCmpUnitMotion.cpp
802 802 } 803 803 804 804 if (m_State == STATE_IDLE) 805 {806 805 return; 807 }808 806 809 807 switch (m_PathState) 810 808 { … … 870 868 // We want to move (at most) maxSpeed*dt units from pos towards the next waypoint 871 869 872 870 fixed timeLeft = dt; 873 874 while (timeLeft > fixed::Zero()) 871 fixed zero = fixed::Zero(); 872 873 while (timeLeft > zero) 875 874 { 876 875 // If we ran out of short path, we have to stop 877 876 if (m_ShortPath.m_Waypoints.empty()) … … 880 879 CFixedVector2D target(m_ShortPath.m_Waypoints.back().x, m_ShortPath.m_Waypoints.back().z); 881 880 CFixedVector2D offset = target - pos; 882 881 883 // Face towards the target884 if (!offset.IsZero())885 {886 entity_angle_t angle = atan2_approx(offset.X, offset.Y);887 cmpPosition->TurnTo(angle);888 }889 890 882 // Work out how far we can travel in timeLeft 891 883 fixed maxdist = maxSpeed.Multiply(timeLeft); 892 884 … … 933 925 934 926 // Update the Position component after our movement (if we actually moved anywhere) 935 927 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); 937 934 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 941 939 if (wasObstructed) 942 940 { 943 941 // Oops, we hit something (very likely another unit). 944 942 // Stop, and recompute the whole path. 945 943 // TODO: if the target has UnitMotion and is higher priority, 946 944 // we should wait a little bit. 947 945 946 m_CurSpeed = zero; 948 947 RequestLongPath(pos, m_FinalGoal); 949 948 m_PathState = PATHSTATE_WAITING_REQUESTING_LONG; 950 949 … … 1138 1137 1139 1138 bool CCmpUnitMotion::PathIsShort(const ICmpPathfinder::Path& path, CFixedVector2D from, entity_pos_t minDistance) 1140 1139 { 1141 CFixedVector2D pos = from;1142 1140 entity_pos_t distLeft = minDistance; 1143 1141 1144 1142 for (ssize_t i = (ssize_t)path.m_Waypoints.size()-1; i >= 0; --i) 1145 1143 { 1146 1144 // Check if the next path segment is longer than the requested minimum 1147 1145 CFixedVector2D waypoint(path.m_Waypoints[i].x, path.m_Waypoints[i].z); 1148 CFixedVector2D delta = waypoint - pos;1146 CFixedVector2D delta = waypoint - from; 1149 1147 if (delta.CompareLength(distLeft) > 0) 1150 1148 return false; 1151 1149 1152 1150 // Still short enough - prepare to check the next segment 1153 1151 distLeft -= delta.Length(); 1154 pos= waypoint;1152 from = waypoint; 1155 1153 } 1156 1154 1157 1155 // Reached the end of the path before exceeding minDistance -
source/simulation2/components/ICmpPosition.cpp
25 25 DEFINE_INTERFACE_METHOD_0("IsInWorld", bool, ICmpPosition, IsInWorld) 26 26 DEFINE_INTERFACE_METHOD_0("MoveOutOfWorld", void, ICmpPosition, MoveOutOfWorld) 27 27 DEFINE_INTERFACE_METHOD_2("MoveTo", void, ICmpPosition, MoveTo, entity_pos_t, entity_pos_t) 28 DEFINE_INTERFACE_METHOD_3("MoveAndTurnTo", void, ICmpPosition, MoveAndTurnTo, entity_pos_t, entity_pos_t, entity_pos_t) 28 29 DEFINE_INTERFACE_METHOD_2("JumpTo", void, ICmpPosition, JumpTo, entity_pos_t, entity_pos_t) 29 30 DEFINE_INTERFACE_METHOD_1("SetHeightOffset", void, ICmpPosition, SetHeightOffset, entity_pos_t) 30 31 DEFINE_INTERFACE_METHOD_0("GetHeightOffset", entity_pos_t, ICmpPosition, GetHeightOffset) -
source/simulation2/components/ICmpPosition.h
74 74 virtual void MoveTo(entity_pos_t x, entity_pos_t z) = 0; 75 75 76 76 /** 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 /** 77 82 * Move immediately to the given location, with no interpolation. 78 83 */ 79 84 virtual void JumpTo(entity_pos_t x, entity_pos_t z) = 0;