commit ee9904c70966449034753ffa923db657e598d26c
parent ab9f5ded22666dc07f0b928c5c5d17a8050aaefd
Author: anon <anon@example.com>
Date: Sat, 7 Nov 2026 00:00:43 +0000
Keep asynchronous pursuit moving as targets change
Diffstat:
5 files changed, 82 insertions(+), 21 deletions(-)
diff --git a/DEVELOPERS.md b/DEVELOPERS.md
@@ -76,11 +76,12 @@ it. Do not hold the world lock across a blocking network write.
Navigation searches use one goroutine and a bounded channel per playfield.
Jobs own actor snapshots and pin immutable scene geometry; they never hold the
simulation locks. The simulation accepts a result only while its motion request,
-starting pose, target and collision body still match. A full queue retries on the
-next tick. Mission retirement cancels its worker, and shutdown joins all workers
-before closing their resources. This isolates pathfinding, but other simulation
-and transaction work still shares the world lock; zones do not yet own all their
-mutable state independently.
+starting pose, target identity and collision body still match. Moving targets
+refresh routing after a completed frame, so new positions cannot starve pursuit.
+A full queue retries on the next tick. Mission retirement cancels its worker,
+and shutdown joins all workers before closing their resources. This isolates
+pathfinding, but other simulation and transaction work still shares the world
+lock; zones do not yet own all their mutable state independently.
NPC visibility and activation query a grid of 200-metre X/Z cells keyed by
playfield, then check exact horizontal distance. Movement updates membership,
diff --git a/server/navigation_worker.go b/server/navigation_worker.go
@@ -39,11 +39,12 @@ func (server *server) requestNPCRoute_locked(npc *character, destination [3]floa
return false
}
// Motion replacement (death, zoning, a new command) drops the old job. A
- // changed start, target or collision body invalidates an outstanding result.
+ // changed start, target identity or collision body invalidates a result. A
+ // moving target's last observed position still provides a usable next route.
if job := npc.motion.navigation; job != nil {
if job.actor.position != npc.position || job.actor.playfield != npc.playfield ||
job.actor.fighting != npc.fighting || job.actor.followTarget != npc.followTarget ||
- job.actor.body != npc.body || surfaceDistanceSquared(job.destination, destination) >= 1 {
+ job.actor.body != npc.body {
close(job.cancel)
npc.motion.navigation = nil
} else {
diff --git a/server/navigation_worker_test.go b/server/navigation_worker_test.go
@@ -58,6 +58,7 @@ func TestStalledZoneNavigationDoesNotBlockAnotherZone(t *testing.T) {
if npc.motion.navigation == nil || other.motion.navigation == nil {
t.Fatal("fixture did not queue both zone searches")
}
+ stalledAt := npc.position
select {
case <-other.motion.navigation.done:
case <-time.After(time.Second):
@@ -67,7 +68,7 @@ func TestStalledZoneNavigationDoesNotBlockAnotherZone(t *testing.T) {
before := second.position
world.advancePlayerMovement(second.movement.updatedAt.Add(100 * time.Millisecond))
- if npc.motion.moving || npc.position != [3]float32{15, .011, 15} || npc.motion.navigation == nil {
+ if npc.motion.moving || npc.position != stalledAt || npc.motion.navigation == nil {
t.Fatal("stalled search unexpectedly advanced its actor")
}
if !other.motion.moving || len(other.motion.route) == 0 || second.position == before {
@@ -92,7 +93,11 @@ func TestNavigationDiscardsAResultForAnOldTarget(t *testing.T) {
t.Fatal("navigation worker did not finish")
}
- player.position = [3]float32{15, .011, 43}
+ other := *player
+ other.id, other.position = 2, [3]float32{15, .011, 43}
+ world.characters[other.id] = &other
+ world.clients[other.id] = &worldClient{id: other.id, ready: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)}
+ npc.fighting = other.id
world.advanceNPCs(now.Add(250 * time.Millisecond))
if len(npc.motion.route) != 0 || npc.motion.navigation == nil || npc.motion.navigation == job {
@@ -173,3 +178,43 @@ func TestNavigationMissionRetirementStopsItsWorker(t *testing.T) {
t.Fatal("retired mission's navigation worker did not stop")
}
}
+
+func TestNavigationPursuesATargetMovingBetweenEveryTick(t *testing.T) {
+ for _, mode := range []string{"pursuit", "pet follow"} {
+ t.Run(mode, func(t *testing.T) {
+ world, _, _ := enteredMissionEncounterWorld(t)
+ player, npc := world.characters[1], world.characters[2000003]
+ player.position, npc.position = [3]float32{12.5, .011, 34}, [3]float32{15, .011, 15}
+ npc.fighting = player.id
+ now := time.Now()
+ world.lastNPCUpdate = now
+ advance := world.advanceNPCs
+ if mode == "pet follow" {
+ npc.pet = &ownedPet{owner: player.id}
+ npc.fighting, npc.followTarget, npc.running = 0, player.id, true
+ npc.geometry, npc.movement.updatedAt = world.geometry, now
+ world.petExpiry = []*character{npc}
+ player.pets = map[uint32]*character{npc.id: npc}
+ advance = world.advancePlayerMovement
+ }
+
+ // Finish searches between ticks, but move the target before consuming each
+ // result. Repeated same-time ticks would conceal cancellation starvation.
+ for tick := 1; tick <= 8; tick++ {
+ player.position[0] = 12.5 + float32(tick%2)*2.5
+ advance(now.Add(time.Duration(tick) * 250 * time.Millisecond))
+ if job := npc.motion.navigation; job != nil {
+ select {
+ case <-job.done:
+ case <-time.After(time.Second):
+ t.Fatal("navigation worker did not finish between ticks")
+ }
+ }
+ }
+
+ if surfaceDistanceSquared(npc.position, [3]float32{15, .011, 15}) < 1 {
+ t.Fatal("moving target starved pursuit despite completed route searches", npc.position)
+ }
+ })
+ }
+}
diff --git a/server/npc_pursuit.go b/server/npc_pursuit.go
@@ -34,21 +34,17 @@ func (server *server) advanceNPCPursuit_locked(npc *character, now time.Time, se
}
changed := !npc.motion.moving || npc.motion.destination != destination
+ reconcile := [3]float32{}
if physical {
replan := npc.motion.navigation != nil || len(npc.motion.route) == 0 && !now.Before(npc.motion.routeRetry)
- if len(npc.motion.route) != 0 {
- replan = replan || surfaceDistanceSquared(npc.motion.route[len(npc.motion.route)-1], destination) >= 1
- }
if replan {
- // Observers must stop following the old route while its replacement
- // is pending; the completed job publishes a fresh starting pose.
- if npc.motion.navigation == nil && npc.motion.moving {
- server.stopNPCMotion_locked(npc, updates)
- }
if !server.requestNPCRoute_locked(npc, destination, now) {
+ // Keep the job, but stop observers if the search outlasts a tick.
+ server.pauseNPCMotion_locked(npc, updates)
return true
}
changed = true
+ reconcile = npc.position
}
if len(npc.motion.route) == 0 {
npc.walkSurface(npc.position, seconds)
@@ -66,7 +62,10 @@ func (server *server) advanceNPCPursuit_locked(npc *character, now time.Time, se
npc.motion.destination = destination
npc.motion.moving = true
server.queueCombat_locked(updates, npc, npc, npcRoutePacket(npc, npc.position))
- return true
+ changed = false
+ if !physical {
+ return true
+ }
}
routeLength := len(npc.motion.route)
previous := npc.position
@@ -110,9 +109,13 @@ func (server *server) advanceNPCPursuit_locked(npc *character, now time.Time, se
}
npc.motion.destination = destination
npc.motion.moving = true
+ // Refresh from the completed frame's pose. Consuming a previous search must
+ // advance movement before another target update can request its replacement.
+ if physical && (len(npc.motion.route) == 0 || surfaceDistanceSquared(npc.motion.route[len(npc.motion.route)-1], destination) >= 1) {
+ server.requestNPCRoute_locked(npc, destination, now)
+ }
if changed {
- // A zero reconcile position changes the route without relocating the actor.
- server.queueCombat_locked(updates, npc, npc, npcRoutePacket(npc, [3]float32{}))
+ server.queueCombat_locked(updates, npc, npc, npcRoutePacket(npc, reconcile))
}
return true
}
@@ -122,6 +125,10 @@ func (server *server) stopNPCMotion_locked(npc *character, updates map[*worldCli
close(npc.motion.navigation.cancel)
npc.motion.navigation = nil
}
+ server.pauseNPCMotion_locked(npc, updates)
+}
+
+func (server *server) pauseNPCMotion_locked(npc *character, updates map[*worldClient][][]byte) {
npc.motion.route = nil
if !npc.motion.moving {
return
diff --git a/server/pet_follow.go b/server/pet_follow.go
@@ -145,6 +145,10 @@ func (server *server) advancePetFollowing_locked(now time.Time, updates map[*wor
server.queueCombat_locked(updates, pet, owner, petFollowPacket(pet, owner.id))
}
if pet.pursuitArrived(owner.position) {
+ if pet.motion.navigation != nil {
+ close(pet.motion.navigation.cancel)
+ pet.motion.navigation = nil
+ }
pet.motion.route, pet.motion.moving = nil, false
pet.movement.velocity, pet.movement.speed = [3]float32{}, 0
continue
@@ -152,7 +156,7 @@ func (server *server) advancePetFollowing_locked(now time.Time, updates map[*wor
// Share NPC route planning and native arrival forces, not player follow's 4m offset.
if pet.geometry.physical(pet.playfield) {
- if pet.motion.navigation != nil || surfaceDistanceSquared(pet.motion.destination, owner.position) >= 1 ||
+ if pet.motion.navigation != nil ||
len(pet.motion.route) == 0 && !now.Before(pet.motion.routeRetry) {
if !server.requestNPCRoute_locked(pet, owner.position, now) {
continue
@@ -171,6 +175,9 @@ func (server *server) advancePetFollowing_locked(now time.Time, updates map[*wor
if blocked {
pet.motion.route = nil
pet.motion.routeRetry = now.Add(time.Second)
+ } else if pet.geometry.physical(pet.playfield) &&
+ (len(pet.motion.route) == 0 || surfaceDistanceSquared(pet.motion.route[len(pet.motion.route)-1], owner.position) >= 1) {
+ server.requestNPCRoute_locked(pet, owner.position, now)
}
if pet.position != previous {
server.queueCombat_locked(updates, pet, pet, npcMovementPacket(pet, 0x94))