Skip to content

Commit 612341a

Browse files
committed
calculate estimated arrival time and use it to detect finish move
1 parent 26abc98 commit 612341a

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

Server/Components/NPCs/NPC/npc.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,6 @@ bool NPC::move(Vector3 pos, NPCMoveType moveType)
128128
// Set up everything to start moving in next tick
129129
auto position = getPosition();
130130
float distance = glm::distance(position, pos);
131-
Vector3 newVelocity;
132-
133-
if (!(distance <= 0.0f))
134-
{
135-
newVelocity = (pos - position) / distance;
136-
}
137131

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

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

157-
// Calculate velocity to use on tick
158-
newVelocity *= (speed / 100.0f);
159-
velocity_ = newVelocity;
160-
161151
// Calculate front vector and player's facing angle:
162152
Vector3 front;
163153
if (!(std::fabs(distance) < DBL_EPSILON))
@@ -169,6 +159,18 @@ bool NPC::move(Vector3 pos, NPCMoveType moveType)
169159
rotation.z = utils::getAngleOfLine(front.x, front.y);
170160
footSync_.Rotation = rotation; // Do this directly, if you use NPC::setRotation it's going to cause recursion
171161

162+
// Calculate velocity to use on tick
163+
velocity_ *= (speed / 100.0f);
164+
165+
if (glm::length(velocity_) != 0.0f)
166+
{
167+
estimatedArrivalTimeNS_ = Time::now().time_since_epoch().count() + (static_cast<long long>(distance / glm::length(velocity_)) * ((npcComponent_->getFootSyncRate() * 10000) + 1000000));
168+
}
169+
else
170+
{
171+
estimatedArrivalTimeNS_ = 0;
172+
}
173+
172174
// Set internal variables
173175
moveSpeed_ = speed;
174176
targetPosition_ = pos;
@@ -185,6 +187,7 @@ void NPC::stopMove()
185187
targetPosition_ = { 0.0f, 0.0f, 0.0f };
186188
velocity_ = { 0.0f, 0.0f, 0.0f };
187189
moveType_ = NPCMoveType_None;
190+
estimatedArrivalTimeNS_ = 0;
188191

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

231-
if (glm::length(travelled) >= remainingDistance)
231+
if (estimatedArrivalTimeNS_ <= Time::now().time_since_epoch().count())
232232
{
233+
footSync_.Position = targetPosition_;
233234
stopMove();
234235
npcComponent_->getEventDispatcher_internal().dispatch(&NPCEventHandler::onNPCFinishMove, *this);
235236
}
236237
else
237238
{
239+
Milliseconds difference = duration_cast<Milliseconds>(now.time_since_epoch()) - duration_cast<Milliseconds>(lastMove_.time_since_epoch());
240+
Vector3 travelled = velocity_ * static_cast<float>(difference.count());
241+
238242
position += travelled;
239243
footSync_.Velocity = velocity_;
244+
footSync_.Position = position; // Do this directly, if you use NPC::setPosition it's going to cause recursion
240245
}
241246

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

246250
void NPC::tick(Microseconds elapsed, TimePoint now)
247251
{
248-
static auto footSyncRate = npcComponent_->getCore()->getConfig().getInt("network.on_foot_sync_rate");
249-
250252
// Only process the NPC if it is spawned
251253
if (player_ && (player_->getState() == PlayerState_OnFoot || player_->getState() == PlayerState_Driver || player_->getState() == PlayerState_Passenger || player_->getState() == PlayerState_Spawned))
252254
{

Server/Components/NPCs/NPC/npc.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class NPC : public INPC, public PoolIDProvider, public NoCopy
6666
// Movements
6767
NPCMoveType moveType_;
6868
TimePoint lastMove_;
69+
long long estimatedArrivalTimeNS_;
6970
TimePoint moveStart_;
7071
float moveSpeed_;
7172
Vector3 targetPosition_;

Server/Components/NPCs/npcs_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class NPCComponent final : public INPCComponent, public CoreEventHandler
7979
return eventDispatcher;
8080
}
8181

82-
int getFootSyncRate() const
82+
int getFootSyncRate() const
8383
{
8484
return *footSyncRate;
8585
}

0 commit comments

Comments
 (0)