Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.03 KB | None | 0 0
  1. ---server code from src / server / zone / objects / tangible / components / ElevatorMenuComponent.cpp
  2. if (parent == NULL || !parent->isCellObject() || parent != sceneObject->getParent().get())
  3. return 0;
  4.  
  5. CellObject* cell = parent.castTo<CellObject*>();
  6.  
  7. float x = creature->getPositionX();
  8. float y = creature->getPositionY();
  9. float z = sceneObject->getPositionZ();
  10.  
  11. Vector<float>* floors = CollisionManager::getCellFloorCollision(x, y, cell);
  12. int floorCount = floors->size();
  13.  
  14. int i = 0;
  15. for (; i < floorCount; ++i) {
  16. float f = floors->get(i);
  17.  
  18. if (fabs(z - f) < 1.f)
  19. break; //Almost certainly the same floor.
  20. }
  21.  
  22. ---client code from client\src\engine\client\library\clientGame\src\shared\controller\RemoteCreatureController.cpp---
  23. //-- determine our desired speed
  24. Vector const serverPosition_w = getPosition_w (m_serverCellObject, m_serverTransform_p);
  25. Vector const clientPosition_w = creatureObject->getPosition_w ();
  26. Vector2d const serverPosition2d_w(serverPosition_w.x, serverPosition_w.z);
  27. Vector2d const clientPosition2d_w(clientPosition_w.x, clientPosition_w.z);
  28. float const yThreshold = 4.5f; // This value is based on the height of an average player. If we are in a tightly compact building (i.e. medic center) we'll still warp properly.
  29. float const goalDistance2d = clientPosition2d_w.magnitudeBetween(serverPosition2d_w);
  30. float const walkThreshold = creatureObject->getMaximumWalkSpeed();
  31. float const maximumSpeed = creatureObject->getMaximumRunSpeed();
  32. float const warpTolerance = std::max (creatureObject->getWarpTolerance(), ((maximumSpeed * 3.0f) + m_currentSpeed));
  33. bool const usesAnimationLocomotion = creatureObject->getClientUsesAnimationLocomotion();
  34.  
  35. m_desiredSpeed = 0.0f;
  36.  
  37. if (usesAnimationLocomotion)
  38. {
  39. //-- stop
  40. m_desiredSpeed = 0.f;
  41.  
  42. }
  43. else if (((fabs(serverPosition_w.y - clientPosition_w.y) > yThreshold)) && (goalDistance2d < cms_stopThreshold))
  44. {
  45. //-- we're moving in y but not in x or z, so we're in an elevator or other conveyance; warp to desired
  46. warpTransform (m_serverCellObject, m_serverTransform_p);
  47. }
  48. else if (goalDistance2d < cms_stopThreshold)
  49. {
  50. //-- stop
  51. m_desiredSpeed = 0.f;
  52.  
  53. }
  54. else if (goalDistance2d < walkThreshold)
  55. {
  56. //-- walk
  57. m_desiredSpeed = goalDistance2d;
  58. }
  59. else if (goalDistance2d < maximumSpeed)
  60. {
  61. //-- run
  62. m_desiredSpeed = maximumSpeed;
  63. }
  64. else if (goalDistance2d < warpTolerance * 2.f)
  65. {
  66. //-- run
  67. m_desiredSpeed = std::min (goalDistance2d, maximumSpeed * 4.f);
  68. }
  69. else
  70. {
  71. //-- warp to desired
  72. warpTransform (m_serverCellObject, m_serverTransform_p);
  73.  
  74. }
  75.  
  76. //-- if we have not moved at least 1/10 the distance we were supposed to move last frame, we're stuck and should warp
  77. if (m_desiredSpeed > 0.f && m_distanceToMoveLastFrame > 0.f)
  78. {
  79. const Vector & pos_w = creatureObject->getPosition_w ();
  80. const float distancedMovedLastFrameSquared = pos_w.magnitudeBetweenSquared (m_lastPosition_w);
  81. if (distancedMovedLastFrameSquared < sqr (m_distanceToMoveLastFrame * 0.1f))
  82. {
  83. if (m_stuckWarpTimer > cms_stuckWarpTimeThreshold)
  84. warpTransform (m_serverCellObject, m_serverTransform_p);
  85. else
  86. m_stuckWarpTimer += elapsedTime;
  87. }
  88. else
  89. {
  90. m_stuckWarpTimer = 0.0f;
  91. }
  92. }
  93.  
  94. //-- warp if we've got nowhere to go but we're in different cells
  95. if ( m_desiredSpeed == 0.f
  96. && m_serverCellObject
  97. && m_serverCellObject->getCellProperty() != creatureObject->getParentCell()
  98. )
  99. {
  100. warpTransform (m_serverCellObject, m_serverTransform_p);
  101. }
  102.  
  103. //-- accellerate or decelerate (only used for visuals)
  104. if(ms_disableAcceleration)
  105. m_currentSpeed = m_desiredSpeed;
  106. else
  107. {
  108. if (m_desiredSpeed>m_currentSpeed)
  109. {
  110. const float accelerationScale = std::max (1.0f, std::min (2.0f, (goalDistance2d / warpTolerance) * 2.0f));
  111.  
  112. //-- handle acceleration (vf - vi) / t
  113. const float acceleration = creatureObject->getMaximumAcceleration (m_currentSpeed) * accelerationScale;
  114.  
  115. m_currentSpeed += acceleration * elapsedTime;
  116.  
  117. if (m_currentSpeed > m_desiredSpeed)
  118. {
  119. m_currentSpeed = m_desiredSpeed;
  120. }
  121. }
  122. else if (m_desiredSpeed < m_currentSpeed)
  123. {
  124. // we're supposed to be stopped here now so slow down real fast
  125. // these numbers can be adjusted to move from running in place to moving while standing
  126. // larger thresholds mean less running in place but more sliding
  127. if(m_desiredSpeed == 0.0f && goalDistance2d == 0.0f)
  128. {
  129. m_currentSpeed *= cms_stopSpeedDecayFactor;
  130. }
  131.  
  132. //-- handle deceleration (vf - vi) / t
  133. const float deceleration = -creatureObject->getMaximumAcceleration (m_currentSpeed);
  134.  
  135. m_currentSpeed += deceleration * elapsedTime;
  136.  
  137. if (m_currentSpeed < m_desiredSpeed)
  138. {
  139. m_currentSpeed = m_desiredSpeed;
  140. }
  141. }
  142.  
  143. }
  144.  
  145. const bool isPlayer = creatureObject->isPlayer();
  146. const bool isMountForAnyPlayer = (creatureObject->getRiderDriverCreature() != NULL);
  147. if(!isMountForAnyPlayer)
  148. {
  149. if(m_currentSpeed == 0.0f)
  150. m_serverSpeed = m_serverSpeed * (isPlayer ? ANIM_SPEED_SERVER_STOP_FACTOR_PLAYER : ANIM_SPEED_SERVER_STOP_FACTOR_NPC);
  151. else
  152. m_serverSpeed = (m_serverSpeed * (isPlayer ? ANIM_SPEED_SERVER_WEIGHT_FACTOR_PLAYER : ANIM_SPEED_SERVER_WEIGHT_FACTOR_NPC)) + (m_currentSpeed * (1.0f - (isPlayer ? ANIM_SPEED_SERVER_WEIGHT_FACTOR_PLAYER : ANIM_SPEED_SERVER_WEIGHT_FACTOR_NPC)));
  153. if(m_serverSpeed < 0.05f)
  154. m_serverSpeed = 0.0f;
  155. }
  156. else
  157. {
  158. m_serverSpeed = m_currentSpeed;
  159. }
  160.  
  161.  
  162. //-- tell the skeletal system how fast the animation should move
  163. {
  164. float animFactor = elapsedTime * cms_animationSpeedFilterFactor;
  165. animFactor = std::max(0.0f, std::min(1.0f, animFactor));
  166. m_currentAnimationSpeed += (m_serverSpeed - m_currentAnimationSpeed) * animFactor;
  167.  
  168. m_currentAnimationSpeed = std::max( 0.f, std::min( m_currentAnimationSpeed, m_serverSpeed ) );
  169.  
  170. SkeletalAppearance2* const appearance = creatureObject->getAppearance() ? creatureObject->getAppearance()->asSkeletalAppearance2() : 0;
  171.  
  172. if (appearance)
  173. {
  174. float animationSpeed = (usesAnimationLocomotion) ? 0.0f : m_currentAnimationSpeed;
  175.  
  176. /*
  177. DEBUG_REPORT_LOG(true, ("object [%s] goal[%5.3f] anim [%5.3f] server [%5.3f] current [%5.3f] usesAnim [%d]\n",
  178. creatureObject->getNetworkId().getValueString().c_str(),
  179. goalDistance2d,
  180. animationSpeed,
  181. m_serverSpeed,
  182. m_currentSpeed,
  183. usesAnimationLocomotion
  184. ));
  185. */
  186.  
  187. appearance->setDesiredVelocity (Vector::unitZ * animationSpeed);
  188. }
  189. }
  190.  
  191. //-- fixup position
  192. Vector direction_o = creatureObject->rotate_w2o(serverPosition_w - clientPosition_w);
  193. const float distanceToMoveThisFrame = m_currentSpeed * elapsedTime;
  194. const float goalDistance = clientPosition_w.magnitudeBetween (serverPosition_w);
  195. if (distanceToMoveThisFrame <= goalDistance)
  196. {
  197. IGNORE_RETURN (direction_o.normalize ());
  198. direction_o *= m_currentSpeed * elapsedTime;
  199. }
  200.  
  201. //-- record the current creature position to be used to potential warping
  202. m_lastPosition_w = creatureObject->getPosition_w ();
  203. m_distanceToMoveLastFrame = direction_o.magnitude ();
  204.  
  205. //-- move the creature
  206. if (!usesAnimationLocomotion)
  207. {
  208. creatureObject->move_o (direction_o);
  209. }
  210.  
  211. //-- fixup orientation
  212. if (!isFaceTracking()
  213. && !creatureObject->getClientUsesAnimationLocomotion()
  214. && !isMountForAndDrivenByClientPlayer)
  215. {
  216. const Vector facing_w = serverPosition_w + getLocalFrameK_w (m_serverCellObject, m_serverTransform_p) - clientPosition_w;
  217. const Vector facing_o = creatureObject->rotate_w2o (facing_w);
  218.  
  219. const float desiredYaw = facing_o.theta();
  220. if ( desiredYaw > ms_minimumCreatureRotation
  221. || desiredYaw < -ms_minimumCreatureRotation
  222. )
  223. {
  224. const float turnRate = creatureObject->getMaximumTurnRate(m_currentSpeed);
  225. const float maximumYawThisFrame = convertDegreesToRadians (turnRate) * elapsedTime;
  226. const float yaw = clamp (-maximumYawThisFrame, desiredYaw, maximumYawThisFrame);
  227. creatureObject->yaw_o(yaw);
  228. }
  229. }
  230.  
  231. //-- fixup look at yaw
  232. if (!isFaceTracking()
  233. && !creatureObject->getClientUsesAnimationLocomotion()
  234. && !isMountForAndDrivenByClientPlayer
  235. && !isMountForAnyPlayer
  236. && creatureObject->getUseLookAtYaw())
  237. {
  238. const float currentLookAtYaw = creatureObject->getLookAtYaw();
  239. float desiredYaw = m_serverLookAtYaw - currentLookAtYaw;
  240. if((currentLookAtYaw > 2.5f) && (m_serverLookAtYaw < -2.5f)) // transition from PI to -PI
  241. {
  242. desiredYaw += 6.28f;
  243. }
  244. else if ((currentLookAtYaw < -2.5f) && (m_serverLookAtYaw > 2.5f))
  245. {
  246. desiredYaw -= 6.28f;
  247. }
  248.  
  249. if ( desiredYaw > ms_minimumCreatureRotation
  250. || desiredYaw < -ms_minimumCreatureRotation
  251. )
  252. {
  253. const float turnRate = creatureObject->getMaximumTurnRate(m_currentSpeed);
  254. const float maximumYawThisFrame = convertDegreesToRadians (turnRate) * elapsedTime;
  255. const float yaw = clamp (-maximumYawThisFrame, desiredYaw, maximumYawThisFrame);
  256. float finalYaw = currentLookAtYaw + yaw;
  257. if(finalYaw > 3.1415f)
  258. finalYaw -= 6.283f;
  259. else if(finalYaw < -3.1415f)
  260. finalYaw += 6.283f;
  261. if(fabs(finalYaw - m_serverLookAtYaw) < 0.05f)
  262. finalYaw = m_serverLookAtYaw;
  263. creatureObject->setLookAtYaw(finalYaw, true);
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement