Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---server code from src / server / zone / objects / tangible / components / ElevatorMenuComponent.cpp
- if (parent == NULL || !parent->isCellObject() || parent != sceneObject->getParent().get())
- return 0;
- CellObject* cell = parent.castTo<CellObject*>();
- float x = creature->getPositionX();
- float y = creature->getPositionY();
- float z = sceneObject->getPositionZ();
- Vector<float>* floors = CollisionManager::getCellFloorCollision(x, y, cell);
- int floorCount = floors->size();
- int i = 0;
- for (; i < floorCount; ++i) {
- float f = floors->get(i);
- if (fabs(z - f) < 1.f)
- break; //Almost certainly the same floor.
- }
- ---client code from client\src\engine\client\library\clientGame\src\shared\controller\RemoteCreatureController.cpp---
- //-- determine our desired speed
- Vector const serverPosition_w = getPosition_w (m_serverCellObject, m_serverTransform_p);
- Vector const clientPosition_w = creatureObject->getPosition_w ();
- Vector2d const serverPosition2d_w(serverPosition_w.x, serverPosition_w.z);
- Vector2d const clientPosition2d_w(clientPosition_w.x, clientPosition_w.z);
- 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.
- float const goalDistance2d = clientPosition2d_w.magnitudeBetween(serverPosition2d_w);
- float const walkThreshold = creatureObject->getMaximumWalkSpeed();
- float const maximumSpeed = creatureObject->getMaximumRunSpeed();
- float const warpTolerance = std::max (creatureObject->getWarpTolerance(), ((maximumSpeed * 3.0f) + m_currentSpeed));
- bool const usesAnimationLocomotion = creatureObject->getClientUsesAnimationLocomotion();
- m_desiredSpeed = 0.0f;
- if (usesAnimationLocomotion)
- {
- //-- stop
- m_desiredSpeed = 0.f;
- }
- else if (((fabs(serverPosition_w.y - clientPosition_w.y) > yThreshold)) && (goalDistance2d < cms_stopThreshold))
- {
- //-- we're moving in y but not in x or z, so we're in an elevator or other conveyance; warp to desired
- warpTransform (m_serverCellObject, m_serverTransform_p);
- }
- else if (goalDistance2d < cms_stopThreshold)
- {
- //-- stop
- m_desiredSpeed = 0.f;
- }
- else if (goalDistance2d < walkThreshold)
- {
- //-- walk
- m_desiredSpeed = goalDistance2d;
- }
- else if (goalDistance2d < maximumSpeed)
- {
- //-- run
- m_desiredSpeed = maximumSpeed;
- }
- else if (goalDistance2d < warpTolerance * 2.f)
- {
- //-- run
- m_desiredSpeed = std::min (goalDistance2d, maximumSpeed * 4.f);
- }
- else
- {
- //-- warp to desired
- warpTransform (m_serverCellObject, m_serverTransform_p);
- }
- //-- if we have not moved at least 1/10 the distance we were supposed to move last frame, we're stuck and should warp
- if (m_desiredSpeed > 0.f && m_distanceToMoveLastFrame > 0.f)
- {
- const Vector & pos_w = creatureObject->getPosition_w ();
- const float distancedMovedLastFrameSquared = pos_w.magnitudeBetweenSquared (m_lastPosition_w);
- if (distancedMovedLastFrameSquared < sqr (m_distanceToMoveLastFrame * 0.1f))
- {
- if (m_stuckWarpTimer > cms_stuckWarpTimeThreshold)
- warpTransform (m_serverCellObject, m_serverTransform_p);
- else
- m_stuckWarpTimer += elapsedTime;
- }
- else
- {
- m_stuckWarpTimer = 0.0f;
- }
- }
- //-- warp if we've got nowhere to go but we're in different cells
- if ( m_desiredSpeed == 0.f
- && m_serverCellObject
- && m_serverCellObject->getCellProperty() != creatureObject->getParentCell()
- )
- {
- warpTransform (m_serverCellObject, m_serverTransform_p);
- }
- //-- accellerate or decelerate (only used for visuals)
- if(ms_disableAcceleration)
- m_currentSpeed = m_desiredSpeed;
- else
- {
- if (m_desiredSpeed>m_currentSpeed)
- {
- const float accelerationScale = std::max (1.0f, std::min (2.0f, (goalDistance2d / warpTolerance) * 2.0f));
- //-- handle acceleration (vf - vi) / t
- const float acceleration = creatureObject->getMaximumAcceleration (m_currentSpeed) * accelerationScale;
- m_currentSpeed += acceleration * elapsedTime;
- if (m_currentSpeed > m_desiredSpeed)
- {
- m_currentSpeed = m_desiredSpeed;
- }
- }
- else if (m_desiredSpeed < m_currentSpeed)
- {
- // we're supposed to be stopped here now so slow down real fast
- // these numbers can be adjusted to move from running in place to moving while standing
- // larger thresholds mean less running in place but more sliding
- if(m_desiredSpeed == 0.0f && goalDistance2d == 0.0f)
- {
- m_currentSpeed *= cms_stopSpeedDecayFactor;
- }
- //-- handle deceleration (vf - vi) / t
- const float deceleration = -creatureObject->getMaximumAcceleration (m_currentSpeed);
- m_currentSpeed += deceleration * elapsedTime;
- if (m_currentSpeed < m_desiredSpeed)
- {
- m_currentSpeed = m_desiredSpeed;
- }
- }
- }
- const bool isPlayer = creatureObject->isPlayer();
- const bool isMountForAnyPlayer = (creatureObject->getRiderDriverCreature() != NULL);
- if(!isMountForAnyPlayer)
- {
- if(m_currentSpeed == 0.0f)
- m_serverSpeed = m_serverSpeed * (isPlayer ? ANIM_SPEED_SERVER_STOP_FACTOR_PLAYER : ANIM_SPEED_SERVER_STOP_FACTOR_NPC);
- else
- 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)));
- if(m_serverSpeed < 0.05f)
- m_serverSpeed = 0.0f;
- }
- else
- {
- m_serverSpeed = m_currentSpeed;
- }
- //-- tell the skeletal system how fast the animation should move
- {
- float animFactor = elapsedTime * cms_animationSpeedFilterFactor;
- animFactor = std::max(0.0f, std::min(1.0f, animFactor));
- m_currentAnimationSpeed += (m_serverSpeed - m_currentAnimationSpeed) * animFactor;
- m_currentAnimationSpeed = std::max( 0.f, std::min( m_currentAnimationSpeed, m_serverSpeed ) );
- SkeletalAppearance2* const appearance = creatureObject->getAppearance() ? creatureObject->getAppearance()->asSkeletalAppearance2() : 0;
- if (appearance)
- {
- float animationSpeed = (usesAnimationLocomotion) ? 0.0f : m_currentAnimationSpeed;
- /*
- DEBUG_REPORT_LOG(true, ("object [%s] goal[%5.3f] anim [%5.3f] server [%5.3f] current [%5.3f] usesAnim [%d]\n",
- creatureObject->getNetworkId().getValueString().c_str(),
- goalDistance2d,
- animationSpeed,
- m_serverSpeed,
- m_currentSpeed,
- usesAnimationLocomotion
- ));
- */
- appearance->setDesiredVelocity (Vector::unitZ * animationSpeed);
- }
- }
- //-- fixup position
- Vector direction_o = creatureObject->rotate_w2o(serverPosition_w - clientPosition_w);
- const float distanceToMoveThisFrame = m_currentSpeed * elapsedTime;
- const float goalDistance = clientPosition_w.magnitudeBetween (serverPosition_w);
- if (distanceToMoveThisFrame <= goalDistance)
- {
- IGNORE_RETURN (direction_o.normalize ());
- direction_o *= m_currentSpeed * elapsedTime;
- }
- //-- record the current creature position to be used to potential warping
- m_lastPosition_w = creatureObject->getPosition_w ();
- m_distanceToMoveLastFrame = direction_o.magnitude ();
- //-- move the creature
- if (!usesAnimationLocomotion)
- {
- creatureObject->move_o (direction_o);
- }
- //-- fixup orientation
- if (!isFaceTracking()
- && !creatureObject->getClientUsesAnimationLocomotion()
- && !isMountForAndDrivenByClientPlayer)
- {
- const Vector facing_w = serverPosition_w + getLocalFrameK_w (m_serverCellObject, m_serverTransform_p) - clientPosition_w;
- const Vector facing_o = creatureObject->rotate_w2o (facing_w);
- const float desiredYaw = facing_o.theta();
- if ( desiredYaw > ms_minimumCreatureRotation
- || desiredYaw < -ms_minimumCreatureRotation
- )
- {
- const float turnRate = creatureObject->getMaximumTurnRate(m_currentSpeed);
- const float maximumYawThisFrame = convertDegreesToRadians (turnRate) * elapsedTime;
- const float yaw = clamp (-maximumYawThisFrame, desiredYaw, maximumYawThisFrame);
- creatureObject->yaw_o(yaw);
- }
- }
- //-- fixup look at yaw
- if (!isFaceTracking()
- && !creatureObject->getClientUsesAnimationLocomotion()
- && !isMountForAndDrivenByClientPlayer
- && !isMountForAnyPlayer
- && creatureObject->getUseLookAtYaw())
- {
- const float currentLookAtYaw = creatureObject->getLookAtYaw();
- float desiredYaw = m_serverLookAtYaw - currentLookAtYaw;
- if((currentLookAtYaw > 2.5f) && (m_serverLookAtYaw < -2.5f)) // transition from PI to -PI
- {
- desiredYaw += 6.28f;
- }
- else if ((currentLookAtYaw < -2.5f) && (m_serverLookAtYaw > 2.5f))
- {
- desiredYaw -= 6.28f;
- }
- if ( desiredYaw > ms_minimumCreatureRotation
- || desiredYaw < -ms_minimumCreatureRotation
- )
- {
- const float turnRate = creatureObject->getMaximumTurnRate(m_currentSpeed);
- const float maximumYawThisFrame = convertDegreesToRadians (turnRate) * elapsedTime;
- const float yaw = clamp (-maximumYawThisFrame, desiredYaw, maximumYawThisFrame);
- float finalYaw = currentLookAtYaw + yaw;
- if(finalYaw > 3.1415f)
- finalYaw -= 6.283f;
- else if(finalYaw < -3.1415f)
- finalYaw += 6.283f;
- if(fabs(finalYaw - m_serverLookAtYaw) < 0.05f)
- finalYaw = m_serverLookAtYaw;
- creatureObject->setLookAtYaw(finalYaw, true);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement