Skip to content

Commit

Permalink
calculate estimated arrival time and use it to detect finish move
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyrAhmady committed Apr 23, 2024
1 parent 26abc98 commit 612341a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
36 changes: 19 additions & 17 deletions Server/Components/NPCs/NPC/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ bool NPC::move(Vector3 pos, NPCMoveType moveType)
// Set up everything to start moving in next tick
auto position = getPosition();
float distance = glm::distance(position, pos);
Vector3 newVelocity;

if (!(distance <= 0.0f))
{
newVelocity = (pos - position) / distance;
}

// Determine which speed to use based on moving type
float speed = 0.0f;
Expand All @@ -154,10 +148,6 @@ bool NPC::move(Vector3 pos, NPCMoveType moveType)

footSync_.UpDown = static_cast<uint16_t>(Key::ANALOG_UP);

// Calculate velocity to use on tick
newVelocity *= (speed / 100.0f);
velocity_ = newVelocity;

// Calculate front vector and player's facing angle:
Vector3 front;
if (!(std::fabs(distance) < DBL_EPSILON))
Expand All @@ -169,6 +159,18 @@ bool NPC::move(Vector3 pos, NPCMoveType moveType)
rotation.z = utils::getAngleOfLine(front.x, front.y);
footSync_.Rotation = rotation; // Do this directly, if you use NPC::setRotation it's going to cause recursion

// Calculate velocity to use on tick
velocity_ *= (speed / 100.0f);

if (glm::length(velocity_) != 0.0f)
{
estimatedArrivalTimeNS_ = Time::now().time_since_epoch().count() + (static_cast<long long>(distance / glm::length(velocity_)) * ((npcComponent_->getFootSyncRate() * 10000) + 1000000));
}
else
{
estimatedArrivalTimeNS_ = 0;
}

// Set internal variables
moveSpeed_ = speed;
targetPosition_ = pos;
Expand All @@ -185,6 +187,7 @@ void NPC::stopMove()
targetPosition_ = { 0.0f, 0.0f, 0.0f };
velocity_ = { 0.0f, 0.0f, 0.0f };
moveType_ = NPCMoveType_None;
estimatedArrivalTimeNS_ = 0;

footSync_.Keys &= Key::SPRINT;
footSync_.Keys &= Key::WALK;
Expand Down Expand Up @@ -224,29 +227,28 @@ void NPC::sendFootSync()
void NPC::advance(TimePoint now)
{
auto position = getPosition();
Milliseconds difference = duration_cast<Milliseconds>(now.time_since_epoch()) - duration_cast<Milliseconds>(lastMove_.time_since_epoch());
float remainingDistance = glm::distance(position, targetPosition_);
Vector3 travelled = velocity_ * static_cast<float>(difference.count());

if (glm::length(travelled) >= remainingDistance)
if (estimatedArrivalTimeNS_ <= Time::now().time_since_epoch().count())
{
footSync_.Position = targetPosition_;
stopMove();
npcComponent_->getEventDispatcher_internal().dispatch(&NPCEventHandler::onNPCFinishMove, *this);
}
else
{
Milliseconds difference = duration_cast<Milliseconds>(now.time_since_epoch()) - duration_cast<Milliseconds>(lastMove_.time_since_epoch());
Vector3 travelled = velocity_ * static_cast<float>(difference.count());

position += travelled;
footSync_.Velocity = velocity_;
footSync_.Position = position; // Do this directly, if you use NPC::setPosition it's going to cause recursion
}

lastMove_ = Time::now();
footSync_.Position = position; // Do this directly, if you use NPC::setPosition it's going to cause recursion
}

void NPC::tick(Microseconds elapsed, TimePoint now)
{
static auto footSyncRate = npcComponent_->getCore()->getConfig().getInt("network.on_foot_sync_rate");

// Only process the NPC if it is spawned
if (player_ && (player_->getState() == PlayerState_OnFoot || player_->getState() == PlayerState_Driver || player_->getState() == PlayerState_Passenger || player_->getState() == PlayerState_Spawned))
{
Expand Down
1 change: 1 addition & 0 deletions Server/Components/NPCs/NPC/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class NPC : public INPC, public PoolIDProvider, public NoCopy
// Movements
NPCMoveType moveType_;
TimePoint lastMove_;
long long estimatedArrivalTimeNS_;
TimePoint moveStart_;
float moveSpeed_;
Vector3 targetPosition_;
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/NPCs/npcs_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class NPCComponent final : public INPCComponent, public CoreEventHandler
return eventDispatcher;
}

int getFootSyncRate() const
int getFootSyncRate() const
{
return *footSyncRate;
}
Expand Down

0 comments on commit 612341a

Please sign in to comment.