server/npc_index_test.go (3651B)
1 package main 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 ) 8 9 func TestNPCSpatialInterestPreservesRadiusAndZoneBoundaries(t *testing.T) { 10 world, _, now := newNPCPursuitWorld(t) 11 player := world.characters[1] 12 player.position = [3]float32{} 13 world.clients[1].npcs = make(map[uint32]bool) 14 template := *world.characters[1000000] 15 world.removeNPC(template.id) 16 for i, position := range [][3]float32{{-180, 1000, 0}, {180, 0, 0}, {180.01, 0, 0}, {150, 0, 150}, {0, 0, -200}, {0, 0, -200.01}} { 17 npc := template 18 npc.id, npc.position, npc.motion, npc.fighting = uint32(1000000+i), position, nil, 0 19 world.addNPC(&npc) 20 } 21 otherZone := template 22 otherZone.id, otherZone.playfield, otherZone.position = 2000000, 635, [3]float32{} 23 world.addNPC(&otherZone) 24 25 world.advanceNPCs(now) 26 27 if visible := world.clients[1].npcs; len(visible) != 2 || !visible[1000000] || !visible[1000001] { 28 t.Fatal("spatial interest changed the 180m horizontal radius", visible) 29 } 30 if active := world.simulatedNPCs; len(active) != 4 || active[1000000] == nil || active[1000001] == nil || active[1000002] == nil || active[1000004] == nil { 31 t.Fatal("spatial activation changed the 200m radius or included another zone", active) 32 } 33 } 34 35 func TestNPCSpatialInterestFollowsMotionAcrossCells(t *testing.T) { 36 world, _, now := newNPCPursuitWorld(t) 37 world.characters[1].position = [3]float32{401, 0, 0} 38 world.clients[1].npcs = make(map[uint32]bool) 39 npc := world.characters[1000000] 40 npc.position = [3]float32{199, 0, 0} 41 npc.npcActiveUntil = now.Add(time.Minute) 42 npc.fighting = 0 43 npc.motion = &npcMotion{home: [3]float32{300, 0, 0}, returning: true} 44 world.simulatedNPCs = map[uint32]*character{npc.id: npc} 45 world.advanceNPCs(now) 46 47 for tick := 1; tick <= 32; tick++ { 48 world.advanceNPCs(now.Add(time.Duration(tick) * 250 * time.Millisecond)) 49 } 50 51 if npc.position[0] <= 221 || !world.clients[1].npcs[npc.id] { 52 t.Fatal("moving NPC was lost in its old spatial cell", npc.position, world.clients[1].npcs) 53 } 54 if len(world.npcCells) != 1 || len(world.npcCells[npcCell{950, 1, 0}]) != 1 { 55 t.Fatal("motion retained stale cell membership", world.npcCells) 56 } 57 } 58 59 func TestNPCSpatialIndexUpdatesSpawnsAndRemovals(t *testing.T) { 60 world, _, now := newNPCPursuitWorld(t) 61 world.advanceNPCs(now) 62 npc := *world.characters[1000000] 63 npc.id, npc.position, npc.motion, npc.fighting = 1000001, [3]float32{20, 0, 0}, nil, 0 64 65 world.removeNPC(1000000) 66 world.addNPC(&npc) 67 world.advanceNPCs(now.Add(250 * time.Millisecond)) 68 69 if visible := world.clients[1].npcs; len(visible) != 1 || !visible[npc.id] { 70 t.Fatal("spawn/removal did not update spatial interest", visible) 71 } 72 if len(world.npcLocations) != 1 || len(world.npcCells[npcCell{950, 0, 0}]) != 1 { 73 t.Fatal("removed NPC retained spatial membership") 74 } 75 } 76 77 func BenchmarkNPCSpatialProximity(b *testing.B) { 78 for _, population := range []int{1000, 100000} { 79 b.Run(fmt.Sprint(population), func(b *testing.B) { 80 world := &server{characters: make(map[uint32]*character)} 81 world.addNPC(&character{id: 1, npc: true, playfield: 950, position: [3]float32{1, 0, 0}}) 82 for id := 2; id <= population; id++ { 83 world.addNPC(&character{id: uint32(id), npc: true, playfield: 950, position: [3]float32{2000 + float32(id), 0, 0}}) 84 } 85 world.nearbyNPCs(950, [3]float32{}) 86 b.ResetTimer() 87 for b.Loop() { 88 candidates, count := 0, 0 89 for _, npc := range world.nearbyNPCs(950, [3]float32{}) { 90 candidates++ 91 if surfaceDistanceSquared(npc.position, [3]float32{}) <= 200*200 { 92 count++ 93 } 94 } 95 if candidates != 1 || count != 1 { 96 b.Fatal("query visited distant population", candidates, count) 97 } 98 } 99 }) 100 } 101 }