server/npc_navigation_test.go (14830B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 "encoding/binary" 7 "fmt" 8 "math" 9 "slices" 10 "testing" 11 "time" 12 ) 13 14 func TestMissionNPCWalksAroundTheTongueAfterDescending(t *testing.T) { 15 for _, returning := range []bool{false, true} { 16 t.Run(fmt.Sprintf("returning=%t", returning), func(t *testing.T) { 17 world, _, _ := enteredMissionEncounterWorld(t) 18 player, npc := world.characters[1], world.characters[2000003] 19 player.position = [3]float32{15, .011, 15} 20 npc.fighting = player.id 21 world.fighting[npc.id] = npc 22 start := time.Now() 23 world.lastNPCUpdate = start 24 if _, err := world.db.Exec("CREATE TABLE navigation_writes (count INTEGER); INSERT INTO navigation_writes VALUES (0); CREATE TRIGGER count_navigation_writes AFTER UPDATE OF position_x,position_y,position_z ON characters WHEN NEW.id=2000003 BEGIN UPDATE navigation_writes SET count=count+1; END"); err != nil { 25 t.Fatal(err) 26 } 27 28 for tick := 1; tick <= 48; tick++ { 29 advanceNavigationForTest(t, world, start.Add(time.Duration(tick) * 250 * time.Millisecond), world.advanceNPCs) 30 } 31 lower := npc.position 32 player.position = [3]float32{15, .011, 28} 33 if returning { 34 player.health = 0 35 } 36 for tick := 49; tick <= 144; tick++ { 37 advanceNavigationForTest(t, world, start.Add(time.Duration(tick) * 250 * time.Millisecond), world.advanceNPCs) 38 } 39 40 var writes int 41 if err := world.db.QueryRow("SELECT count FROM navigation_writes").Scan(&writes); err != nil { 42 t.Fatal(err) 43 } 44 if lower[2] >= 17 || npc.position[2] < 26 || npc.position[1] > .1 || 45 world.geometry.mission(npc.playfield).roomAt(npc.position) == nil || writes != 0 { 46 t.Fatal("indoor actor failed to walk back around scenery without position writes", lower, npc.position, writes) 47 } 48 if returning && (npc.motion.returning || npc.motion.moving || math.Hypot(float64(npc.position[0]-npc.motion.home[0]), float64(npc.position[2]-npc.motion.home[2])) >= 1) { 49 t.Fatal("obstacle route did not finish the return home", npc.position, npc.motion) 50 } 51 }) 52 } 53 } 54 55 func TestMissionNPCDetoursIntoTheSidePassage(t *testing.T) { 56 for _, monster := range []uint32{17655, 30354} { 57 for _, returning := range []bool{false, true} { 58 t.Run(fmt.Sprintf("%d/returning=%t", monster, returning), func(t *testing.T) { 59 world, _, _ := enteredMissionEncounterWorld(t) 60 player, npc := world.characters[1], world.characters[2000003] 61 npc.monsterData, npc.body = monster, playerCollisionBody{} 62 if monster == 30354 { 63 npc.level, npc.combatProfile = 4, gamedata.NPCCombatProfiles[30354][4] 64 } 65 npc.position = [3]float32{15, .011, 15} 66 player.position = [3]float32{12.5, .011, 34} 67 if returning { 68 npc.motion.home, npc.motion.returning = player.position, true 69 } else { 70 npc.fighting = player.id 71 world.fighting[npc.id] = npc 72 } 73 start := time.Now() 74 world.lastNPCUpdate = start 75 deadline := start.Add(time.Second) 76 npc.nextAttack = deadline 77 78 for tick := 1; tick <= 96; tick++ { 79 advanceNavigationForTest(t, world, start.Add(time.Duration(tick) * 250 * time.Millisecond), world.advanceNPCs) 80 } 81 82 if surfaceDistanceSquared(npc.position, player.position) >= 4 || npc.position[2] < 32 || npc.nextAttack != deadline { 83 t.Fatal("NPC followed the obstructed straight line instead of a walkable detour", npc.position) 84 } 85 if returning && (npc.motion.returning || npc.motion.moving || math.Hypot(float64(npc.position[0]-npc.motion.home[0]), float64(npc.position[2]-npc.motion.home[2])) >= 1) { 86 t.Fatal("detour did not reach the retained home's native arrival radius", npc.position, npc.motion) 87 } 88 }) 89 } 90 } 91 } 92 93 func TestMissionNPCDetourDoesNotCutAcrossTheSidePassageWall(t *testing.T) { 94 world, _, _ := enteredMissionEncounterWorld(t) 95 npc := world.characters[2000003] 96 npc.position = [3]float32{15, .011, 15} 97 npc.motion.home = [3]float32{12.5, .011, 34} 98 npc.motion.returning = true 99 start := time.Now() 100 world.lastNPCUpdate = start 101 102 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 103 104 if len(npc.motion.route) < 2 || npc.motion.route[0][0] < 15 { 105 t.Fatal("detour requires sliding along the passage wall", npc.motion.route) 106 } 107 } 108 109 func TestMissionNPCRouteFollowsAChangingTarget(t *testing.T) { 110 world, _, _ := enteredMissionEncounterWorld(t) 111 player, npc := world.characters[1], world.characters[2000003] 112 npc.position = [3]float32{15, .011, 15} 113 player.position = [3]float32{12.5, .011, 34} 114 npc.fighting = player.id 115 world.fighting[npc.id] = npc 116 start := time.Now() 117 world.lastNPCUpdate = start 118 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 119 120 for tick := 2; tick <= 9; tick++ { 121 player.position[0] += .25 122 advanceNavigationForTest(t, world, start.Add(time.Duration(tick) * 250 * time.Millisecond), world.advanceNPCs) 123 } 124 for tick := 10; tick <= 96; tick++ { 125 advanceNavigationForTest(t, world, start.Add(time.Duration(tick) * 250 * time.Millisecond), world.advanceNPCs) 126 } 127 128 if surfaceDistanceSquared(npc.position, player.position) >= 4 || npc.motion.moving || npc.fighting != player.id { 129 t.Fatal("cached route ignored a target moving in small increments", npc.position, npc.motion) 130 } 131 } 132 133 func TestMissionNPCLongRoutePublishesBoundedWaypointsToBothObservers(t *testing.T) { 134 world, _, first := enteredMissionEncounterWorld(t) 135 player, npc := world.characters[1], world.characters[2000003] 136 player.position = [3]float32{15, .011, 43} 137 npc.fighting = player.id 138 world.fighting[npc.id] = npc 139 npc.motion.destination, npc.motion.moving = player.position, true 140 for point := 1; point <= 40; point++ { 141 npc.motion.route = append(npc.motion.route, [3]float32{15, .011, 28 + float32(point)*.375}) 142 } 143 second := &combatTestConn{} 144 world.characters[2] = &character{id: 2, playfield: 65536, position: player.position} 145 world.clients[2] = &worldClient{id: 2, conn: second, target: true, ready: true, npcs: map[uint32]bool{npc.id: true}} 146 start := time.Now() 147 world.lastNPCUpdate = start 148 149 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 150 151 var routes [2][]byte 152 for index, output := range []*combatTestConn{first, second} { 153 for _, packet := range output.packets { 154 if binary.BigEndian.Uint32(packet[16:20]) == 0x260f3671 && binary.BigEndian.Uint32(packet[24:28]) == npc.id { 155 routes[index] = packet 156 } 157 } 158 } 159 if len(routes[0]) != 420 || len(routes[1]) != 420 || binary.BigEndian.Uint16(routes[0][6:8]) != 418 || !bytes.Equal(routes[0][16:], routes[1][16:]) || binary.BigEndian.Uint32(routes[0][54:58]) != 30 || len(npc.motion.route) <= 29 { 160 t.Fatal("native route exceeded its 30-point capacity or missed an observer", len(routes[0]), len(routes[1]), len(npc.motion.route)) 161 } 162 } 163 164 func TestMissionNPCDoesNotCrossDisconnectedRooms(t *testing.T) { 165 world, _, _ := enteredMissionEncounterWorld(t) 166 player, npc := world.characters[1], world.characters[2000003] 167 player.position = [3]float32{15, .011, 43} 168 scene := world.geometry.mission(npc.playfield) 169 scene.links = nil 170 npc.fighting = player.id 171 world.fighting[npc.id] = npc 172 start := time.Now() 173 world.lastNPCUpdate = start 174 before := npc.position 175 176 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 177 retry := npc.motion.routeRetry 178 advanceNavigationForTest(t, world, start.Add(500 * time.Millisecond), world.advanceNPCs) 179 180 if surfaceDistanceSquared(npc.position, before) >= .01 || npc.motion.moving || len(npc.motion.route) != 0 || !npc.motion.routeRetry.Equal(retry) || !retry.After(start.Add(time.Second)) { 181 t.Fatal("unreachable room caused movement or a search on every tick", npc.position, npc.motion) 182 } 183 } 184 185 func TestMissionNPCRootClearsPendingDetour(t *testing.T) { 186 world, _, _ := enteredMissionEncounterWorld(t) 187 player, npc := world.characters[1], world.characters[2000003] 188 npc.position = [3]float32{15, .011, 15} 189 player.position = [3]float32{12.5, .011, 34} 190 npc.fighting = player.id 191 world.fighting[npc.id] = npc 192 start := time.Now() 193 world.lastNPCUpdate = start 194 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 195 before := npc.position 196 if len(npc.motion.route) == 0 { 197 t.Fatal("fixture did not start an obstacle route") 198 } 199 npc.activeNanos = []activeNano{{program: 42110, caster: player.id, expiresAt: start.Add(time.Minute)}} 200 201 advanceNavigationForTest(t, world, start.Add(500 * time.Millisecond), world.advanceNPCs) 202 203 if npc.position != before || npc.motion.moving || npc.running || len(npc.motion.route) != 0 || npc.fighting != player.id { 204 t.Fatal("root retained an active waypoint route", npc.position, npc.motion) 205 } 206 } 207 208 func TestMissionNPCColdLoadResumesReturnWhenObserved(t *testing.T) { 209 world, _, _ := enteredMissionEncounterWorld(t) 210 npc := world.characters[2000003] 211 npc.position = [3]float32{15, .011, 15} 212 npc.motion.home = [3]float32{12.5, .011, 34} 213 npc.motion.returning = true 214 start := time.Now() 215 world.lastNPCUpdate = start 216 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 217 if len(npc.motion.route) == 0 { 218 t.Fatal("fixture did not start an obstacle return") 219 } 220 if err := world.flushWorld(); err != nil { 221 t.Fatal(err) 222 } 223 224 cold := reloadMissionEntranceWorld(t) 225 restored := cold.characters[npc.id] 226 cached := len(restored.motion.route) 227 cold.lastNPCUpdate = start 228 before := restored.position 229 advanceNavigationForTest(t, cold, start.Add(250 * time.Millisecond), cold.advanceNPCs) 230 if restored.position != before || len(restored.motion.route) != 0 { 231 t.Fatal("unobserved cold NPC advanced its return") 232 } 233 cold.clients[1] = &worldClient{id: 1, ready: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)} 234 for tick := 1; tick <= 96; tick++ { 235 advanceNavigationForTest(t, cold, start.Add(time.Duration(tick) * 250 * time.Millisecond), cold.advanceNPCs) 236 } 237 238 if cached != 0 || restored.motion.returning || restored.motion.moving || len(restored.motion.route) != 0 || 239 math.Hypot(float64(restored.position[0]-restored.motion.home[0]), float64(restored.position[2]-restored.motion.home[2])) >= 1 || restored.health != npc.health || restored.fighting != 0 { 240 t.Fatal("cold return restored transient waypoints, lost its home or healed", restored.position, restored.motion) 241 } 242 } 243 244 func TestMissionNPCNewObserverReceivesTheRemainingDetourAfterSpawn(t *testing.T) { 245 world, _, _ := enteredMissionEncounterWorld(t) 246 player, npc := world.characters[1], world.characters[2000003] 247 npc.position = [3]float32{15, .011, 15} 248 player.position = [3]float32{12.5, .011, 34} 249 npc.fighting = player.id 250 world.fighting[npc.id] = npc 251 start := time.Now() 252 world.lastNPCUpdate = start 253 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 254 observer := &combatTestConn{} 255 world.characters[2] = &character{id: 2, playfield: 65536, position: player.position} 256 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: make(map[uint32]bool)} 257 258 advanceNavigationForTest(t, world, start.Add(500 * time.Millisecond), world.advanceNPCs) 259 260 var keys []uint32 261 var route []byte 262 for _, packet := range observer.packets { 263 if binary.BigEndian.Uint32(packet[24:28]) != npc.id { 264 continue 265 } 266 key := binary.BigEndian.Uint32(packet[16:20]) 267 keys = append(keys, key) 268 if key == 0x260f3671 { 269 route = packet 270 } 271 } 272 if !slices.Equal(keys, []uint32{0x271b3a6b, 0x1d3c0f1c, 0x260f3671, 0x28494070}) || 273 binary.BigEndian.Uint32(route[54:58]) != 5 || len(npc.motion.route) != 4 { 274 t.Fatal("new observer received a stale straight route or incorrect spawn order", keys, len(npc.motion.route)) 275 } 276 } 277 278 func TestMissionNPCDeathDiscardsItsPendingDetour(t *testing.T) { 279 world, client, _ := enteredMissionEncounterWorld(t) 280 player, npc := world.characters[1], world.characters[2000003] 281 npc.position = [3]float32{15, .011, 15} 282 player.position = [3]float32{12.5, .011, 34} 283 npc.fighting = player.id 284 world.fighting[npc.id] = npc 285 start := time.Now() 286 world.lastNPCUpdate = start 287 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 288 if len(npc.motion.route) == 0 { 289 t.Fatal("fixture did not start an obstacle route") 290 } 291 player.position, npc.health = npc.position, 1 292 world.combatRoll = func(uint32) uint32 { return 0 } 293 294 if err := world.handleCombat(client, attackPacket(player.id, npc.id)[20:]); err != nil { 295 t.Fatal(err) 296 } 297 world.advanceCombat(player.nextAttack) 298 position := npc.position 299 advanceNavigationForTest(t, world, start.Add(time.Minute), world.advanceNPCs) 300 301 if npc.health != 0 || npc.corpse == nil || npc.position != position || npc.motion.moving || npc.motion.returning || len(npc.motion.route) != 0 { 302 t.Fatal("dead NPC retained its obstacle route or moved its corpse", npc.position, npc.motion) 303 } 304 } 305 306 func TestMissionNPCFailedDeathCancelsItsDetourAndReturnsAlive(t *testing.T) { 307 world, client, output := enteredMissionEncounterWorld(t) 308 player, npc := world.characters[1], world.characters[2000003] 309 npc.position = [3]float32{15, .011, 15} 310 player.position = [3]float32{12.5, .011, 34} 311 npc.fighting = player.id 312 world.fighting[npc.id] = npc 313 start := time.Now() 314 world.lastNPCUpdate = start 315 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 316 player.position, npc.health = npc.position, 1 317 world.combatRoll = func(uint32) uint32 { return 0 } 318 if err := world.handleCombat(client, attackPacket(player.id, npc.id)[20:]); err != nil { 319 t.Fatal(err) 320 } 321 if len(npc.motion.route) == 0 { 322 t.Fatal("fixture did not retain its pending obstacle route") 323 } 324 if _, err := world.db.Exec("CREATE TRIGGER reject_navigation_death BEFORE UPDATE OF health ON characters WHEN NEW.id=2000003 AND NEW.health=0 BEGIN SELECT RAISE(FAIL,'blocked death'); END"); err != nil { 325 t.Fatal(err) 326 } 327 328 world.advanceCombat(player.nextAttack) 329 cancelled := !npc.motion.moving && len(npc.motion.route) == 0 && npc.motion.returning && npc.fighting == 0 330 for tick := 2; tick <= 96; tick++ { 331 advanceNavigationForTest(t, world, start.Add(time.Duration(tick) * 250 * time.Millisecond), world.advanceNPCs) 332 } 333 334 if !cancelled || npc.health != 1 || npc.corpse != nil || npc.motion.moving || npc.motion.returning || 335 len(npc.motion.route) != 0 || math.Hypot(float64(npc.position[0]-npc.motion.home[0]), float64(npc.position[2]-npc.motion.home[2])) >= 1 { 336 t.Fatal("failed death did not cancel the old detour and return a living NPC", npc.health, npc.position, npc.motion, npc.movement.velocity, npc.movement.normal, npc.movement.falling) 337 } 338 for _, packet := range output.packets { 339 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[24:28]) == npc.id && binary.BigEndian.Uint32(packet[29:33]) == 99 { 340 t.Fatal("failed death published a corpse") 341 } 342 } 343 }