server/roaming_test.go (14322B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 "encoding/binary" 7 "math" 8 "net" 9 "sync" 10 "testing" 11 "time" 12 ) 13 14 func TestRoamingIsSharedBoundedAndZoneScoped(t *testing.T) { 15 data := testNPCData() 16 data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point" 17 data.Playfields[0].Districts[0].Groups = append(data.Playfields[0].Districts[0].Groups, gamedata.NPCGroup{ 18 TemplateCode: "GUARD", Name: "Guard", MonsterData: 26092, Appearance: 789, Levels: [2]uint16{1, 1}, 19 AppearanceStatus: "placeholder_creature", PlacementStatus: "authored_point", Positions: [][3]float32{{54, 14, 56}}, 20 }) 21 data.Playfields = append(data.Playfields, gamedata.NPCPlayfield{ID: 952, Districts: []gamedata.NPCDistrict{{Groups: []gamedata.NPCGroup{{ 22 TemplateCode: "LEET", Name: "Leet", MonsterData: 17655, Appearance: 229, Levels: [2]uint16{1, 1}, 23 AppearanceStatus: "placeholder_creature", PlacementStatus: "authored_point", Positions: [][3]float32{{100, 14, 100}}, 24 }}}}}) 25 useNPCData(t, data) 26 world := newTestServer(t) 27 world.newCharacter() 28 world.newCharacter() 29 actors := world.characters 30 shutdown := startTestService(t, world) 31 var clients []net.Conn 32 for _, id := range []uint32{1, 2} { 33 client, connection := net.Pipe() 34 clients = append(clients, client) 35 defer client.Close() 36 client.SetDeadline(time.Now().Add(5 * time.Second)) 37 go func() { defer connection.Close(); world.serveWorldConnection(connection) }() 38 cookie := make([]byte, 12) 39 binary.BigEndian.PutUint32(cookie, id) 40 client.Write(systemPacket(0x1b, cookie)) 41 readWorldUpdates(t, client) 42 if id == 2 { 43 readPacket(clients[0]) 44 title, err := readPacket(clients[0]) 45 if err != nil || binary.BigEndian.Uint32(title[20:24]) != 0x734e5a7b { 46 t.Fatalf("observer did not receive the second player's title: %x %v", title, err) 47 } 48 } 49 // Drain readiness and its combat foundations to the first observer as well. 50 var peer sync.WaitGroup 51 if id == 2 { 52 peer.Add(1) 53 go func() { 54 defer peer.Done() 55 for range 17 { 56 if _, err := readPacket(clients[0]); err != nil { 57 t.Error(err) 58 return 59 } 60 } 61 }() 62 } 63 client.Write(n3Packet(0x570c2039, 50000, id, nil)) 64 if _, err := readPacket(client); err != nil { 65 t.Fatal(err) 66 } 67 peer.Wait() 68 // Finish connection initialization before expecting peer broadcasts. 69 ping := systemPacket(1, nil) 70 binary.BigEndian.PutUint16(ping[2:4], 11) 71 client.Write(ping) 72 if _, err := readPacket(client); err != nil { 73 t.Fatal(err) 74 } 75 } 76 now := time.Now() 77 78 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { state.advanceNPCs(now); return nil })) 79 packets := make([][]byte, 2) 80 errors := make([]error, 2) 81 var readers sync.WaitGroup 82 for index, client := range clients { 83 readers.Add(1) 84 go func() { 85 defer readers.Done() 86 packets[index], errors[index] = readPacket(client) 87 }() 88 } 89 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { state.advanceNPCs(now.Add(8 * time.Second)); return nil })) 90 readers.Wait() 91 for _, err := range errors { 92 if err != nil { 93 t.Fatal(err) 94 } 95 } 96 if err := shutdown(); err != nil { 97 t.Fatal(err) 98 } 99 100 first, second := packets[0], packets[1] 101 if binary.BigEndian.Uint32(first[20:24]) != 0x260f3671 || binary.BigEndian.Uint32(first[28:32]) != 1000000 { 102 t.Fatalf("creature movement: %x", first) 103 } 104 if !bytes.Equal(first[20:], second[20:]) || binary.BigEndian.Uint32(first[12:16]) != 1 || binary.BigEndian.Uint32(second[12:16]) != 2 { 105 t.Fatal("players received different NPC motion") 106 } 107 x := math.Float32frombits(binary.BigEndian.Uint32(first[49:53])) - 52 108 z := math.Float32frombits(binary.BigEndian.Uint32(first[57:61])) - 56 109 if x != 0 || z != 0 || first[48] != 22 || binary.BigEndian.Uint32(first[61:65]) != 2 { 110 t.Fatalf("walk did not start at the current position: %x", first) 111 } 112 if actors[1000001].position != [3]float32{54, 14, 56} || actors[1000002].position != [3]float32{100, 14, 100} { 113 t.Fatal("human or unoccupied-zone NPC moved") 114 } 115 if math.Hypot(float64(actors[1000000].motion.destination[0]-52), float64(actors[1000000].motion.destination[2]-56)) > 4.001 { 116 t.Fatal("destination escaped the home radius") 117 } 118 } 119 120 func TestRoamingDoesNotResetPositionDuringWalk(t *testing.T) { 121 data := testNPCData() 122 data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point" 123 useNPCData(t, data) 124 world := newTestServer(t) 125 player := world.newCharacter() 126 npc := world.characters[1000000] 127 shutdown := startTestService(t, world) 128 client, connection := net.Pipe() 129 defer client.Close() 130 client.SetDeadline(time.Now().Add(5 * time.Second)) 131 go func() { defer connection.Close(); world.serveWorldConnection(connection) }() 132 cookie := make([]byte, 12) 133 binary.BigEndian.PutUint32(cookie, player.id) 134 client.Write(systemPacket(0x1b, cookie)) 135 readWorldUpdates(t, client) 136 // Finish native loading before expecting movement broadcasts. 137 client.Write(n3Packet(0x570c2039, 50000, player.id, nil)) 138 if _, err := readPacket(client); err != nil { 139 t.Fatal(err) 140 } 141 // A reply after initialization also synchronizes the live fixture state. 142 ping := systemPacket(1, nil) 143 binary.BigEndian.PutUint16(ping[2:4], 11) 144 client.Write(ping) 145 if _, err := readPacket(client); err != nil { 146 t.Fatal(err) 147 } 148 now := time.Now() 149 var packets [][]byte 150 done := make(chan struct{}) 151 go func() { 152 defer close(done) 153 for { 154 packet, err := readPacket(client) 155 if err != nil || binary.BigEndian.Uint16(packet[2:4]) == 11 { 156 return 157 } 158 packets = append(packets, packet) 159 } 160 }() 161 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 162 state.lastNPCUpdate = now 163 state.characters[1000000].motion.destination = [3]float32{53.5, 14, 56} 164 state.characters[1000000].motion.moving = true 165 state.characters[1000000].motion.nextMove = now 166 for tick := 1; tick <= 4; tick++ { 167 state.advanceNPCs(now.Add(time.Duration(tick) * 250 * time.Millisecond)) 168 } 169 return nil 170 })) 171 client.Write(ping) 172 <-done 173 if err := shutdown(); err != nil { 174 t.Fatal(err) 175 } 176 177 if len(packets) != 2 || binary.BigEndian.Uint32(packets[0][20:24]) != 0x260f3671 || binary.BigEndian.Uint32(packets[0][61:65]) != 0 || binary.BigEndian.Uint32(packets[1][36:40]) != 22 { 178 t.Fatalf("received position resets during the walk: %x", packets) 179 } 180 if npc.position[0] <= 52.5 || npc.position[0] >= 52.55 || npc.position[1] != 14 || npc.position[2] != 56 { 181 t.Fatalf("walking speed did not match the original client: %v", npc.position) 182 } 183 } 184 185 func TestJoiningPlayerReceivesOngoingNPCMovement(t *testing.T) { 186 data := testNPCData() 187 data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point" 188 useNPCData(t, data) 189 world := newTestServer(t) 190 world.characters[1000000].position = [3]float32{53, 14, 56} 191 world.characters[1000000].motion.destination = [3]float32{54.5, 14, 56} 192 world.characters[1000000].motion.moving = true 193 player := world.newCharacter() 194 shutdown := startTestService(t, world) 195 client, connection := net.Pipe() 196 defer client.Close() 197 client.SetDeadline(time.Now().Add(5 * time.Second)) 198 go func() { defer connection.Close(); world.serveWorldConnection(connection) }() 199 cookie := make([]byte, 12) 200 binary.BigEndian.PutUint32(cookie, player.id) 201 202 client.Write(systemPacket(0x1b, cookie)) 203 packets := readWorldUpdates(t, client) 204 // Finish native loading before expecting movement broadcasts. 205 client.Write(n3Packet(0x570c2039, 50000, player.id, nil)) 206 if _, err := readPacket(client); err != nil { 207 t.Fatal(err) 208 } 209 210 if err := shutdown(); err != nil { 211 t.Fatal(err) 212 } 213 var movement []byte 214 for _, packet := range packets { 215 if binary.BigEndian.Uint32(packet[20:24]) == 0x260f3671 && binary.BigEndian.Uint32(packet[28:32]) == 1000000 { 216 movement = packet 217 } 218 } 219 if movement == nil || movement[48] != 22 || math.Float32frombits(binary.BigEndian.Uint32(movement[49:53])) != 53 || math.Float32frombits(binary.BigEndian.Uint32(movement[77:81])) != 54.5 { 220 t.Fatalf("joining player did not receive ongoing movement: %x", movement) 221 } 222 } 223 224 func TestRoamingStopsWithinNativeWaypointRadiusAndPauses(t *testing.T) { 225 data := testNPCData() 226 data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point" 227 useNPCData(t, data) 228 world := newTestServer(t) 229 player := world.newCharacter() 230 npc := world.characters[1000000] 231 shutdown := startTestService(t, world) 232 client, connection := net.Pipe() 233 defer client.Close() 234 client.SetDeadline(time.Now().Add(5 * time.Second)) 235 go func() { defer connection.Close(); world.serveWorldConnection(connection) }() 236 cookie := make([]byte, 12) 237 binary.BigEndian.PutUint32(cookie, player.id) 238 client.Write(systemPacket(0x1b, cookie)) 239 readWorldUpdates(t, client) 240 // Finish native loading before expecting movement broadcasts. 241 client.Write(n3Packet(0x570c2039, 50000, player.id, nil)) 242 if _, err := readPacket(client); err != nil { 243 t.Fatal(err) 244 } 245 now := time.Now() 246 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 247 state.characters[1000000].motion.destination = [3]float32{52.125, 14, 56} 248 state.characters[1000000].motion.moving = true 249 state.characters[1000000].motion.nextMove = now 250 state.advanceNPCs(now) 251 state.advanceNPCs(now.Add(250 * time.Millisecond)) 252 return nil 253 })) 254 packet, err := readPacket(client) 255 if err != nil { 256 t.Fatal(err) 257 } 258 walk, err := readPacket(client) 259 if err != nil { 260 t.Fatal(err) 261 } 262 if err := shutdown(); err != nil { 263 t.Fatal(err) 264 } 265 266 if binary.BigEndian.Uint32(packet[20:24]) != 0x260f3671 || binary.BigEndian.Uint32(packet[61:65]) != 0 || binary.BigEndian.Uint32(walk[36:40]) != 22 || npc.position != [3]float32{52, 14, 56} || npc.motion.moving { 267 t.Fatal("NPC did not stop within the native waypoint radius") 268 } 269 if npc.motion.nextMove.Before(now.Add(3 * time.Second)) { 270 t.Fatal("NPC did not pause between walks") 271 } 272 } 273 274 func TestFixedCreatureDoesNotRoam(t *testing.T) { 275 useNPCData(t, testNPCData()) 276 277 world := newTestServer(t) 278 279 if world.characters[1000000].motion != nil { 280 t.Fatal("fixed creature received roaming behavior") 281 } 282 } 283 284 func TestRoamingPausesWithoutPlayers(t *testing.T) { 285 data := testNPCData() 286 data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point" 287 useNPCData(t, data) 288 world := newTestServer(t) 289 now := time.Now() 290 world.characters[1000000].motion.moving = true 291 world.characters[1000000].motion.nextMove = now 292 world.characters[1000000].motion.destination = [3]float32{55, 14, 56} 293 294 world.advanceNPCs(now.Add(time.Hour)) 295 296 if world.characters[1000000].position != [3]float32{52, 14, 56} { 297 t.Fatal("NPC moved without an observer") 298 } 299 } 300 301 func TestRoamingFollowsTerrainWithAuthoredOffset(t *testing.T) { 302 data := testNPCData() 303 data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point" 304 useNPCData(t, data) 305 world := newTestServer(t) 306 world.terrain[950] = &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 128, scale: 1, heights: []byte{0, 64, 128, 0, 64, 128, 0, 64, 128}} 307 player := world.newCharacter() 308 npc := world.characters[1000000] 309 shutdown := startTestService(t, world) 310 client, connection := net.Pipe() 311 defer client.Close() 312 client.SetDeadline(time.Now().Add(5 * time.Second)) 313 go func() { defer connection.Close(); world.serveWorldConnection(connection) }() 314 cookie := make([]byte, 12) 315 binary.BigEndian.PutUint32(cookie, player.id) 316 client.Write(systemPacket(0x1b, cookie)) 317 readWorldUpdates(t, client) 318 // Finish native loading before expecting movement broadcasts. 319 client.Write(n3Packet(0x570c2039, 50000, player.id, nil)) 320 if _, err := readPacket(client); err != nil { 321 t.Fatal(err) 322 } 323 now := time.Now() 324 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 325 state.characters[1000000].motion.destination = [3]float32{53.25, 14, 56} 326 state.characters[1000000].motion.moving = true 327 state.characters[1000000].motion.nextMove = now 328 state.advanceNPCs(now) 329 state.advanceNPCs(now.Add(250 * time.Millisecond)) 330 state.advanceNPCs(now.Add(500 * time.Millisecond)) 331 return nil 332 })) 333 packet, err := readPacket(client) 334 if err != nil { 335 t.Fatal(err) 336 } 337 walk, err := readPacket(client) 338 if err != nil { 339 t.Fatal(err) 340 } 341 if err := shutdown(); err != nil { 342 t.Fatal(err) 343 } 344 345 if binary.BigEndian.Uint32(walk[36:40]) != 22 || npc.position[0] <= 52 || npc.position[0] >= 53.25 || math.Abs(float64(npc.position[1]-14-(npc.position[0]-52)*.5)) > .0001 || math.Float32frombits(binary.BigEndian.Uint32(packet[53:57])) != npc.position[1] { 346 t.Fatalf("NPC did not follow the slope: %v", npc.position) 347 } 348 } 349 350 func TestRoamingStopsAtTerrainBoundary(t *testing.T) { 351 data := testNPCData() 352 data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point" 353 useNPCData(t, data) 354 world := newTestServer(t) 355 world.terrain[950] = &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 32, scale: 1, heights: []byte{0, 16, 32, 0, 16, 32, 0, 16, 32}} 356 player := world.newCharacter() 357 npc := world.characters[1000000] 358 shutdown := startTestService(t, world) 359 client, connection := net.Pipe() 360 defer client.Close() 361 client.SetDeadline(time.Now().Add(5 * time.Second)) 362 go func() { defer connection.Close(); world.serveWorldConnection(connection) }() 363 cookie := make([]byte, 12) 364 binary.BigEndian.PutUint32(cookie, player.id) 365 client.Write(systemPacket(0x1b, cookie)) 366 readWorldUpdates(t, client) 367 // Finish native loading before expecting movement broadcasts. 368 client.Write(n3Packet(0x570c2039, 50000, player.id, nil)) 369 if _, err := readPacket(client); err != nil { 370 t.Fatal(err) 371 } 372 now := time.Now() 373 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 374 state.characters[1000000].position = [3]float32{64, 20, 56} 375 state.characters[1000000].motion.destination = [3]float32{64.25, 14, 56} 376 state.characters[1000000].motion.moving = true 377 state.characters[1000000].motion.nextMove = now 378 state.advanceNPCs(now) 379 return nil 380 })) 381 packet, err := readPacket(client) 382 if err != nil { 383 t.Fatal(err) 384 } 385 walk, err := readPacket(client) 386 if err != nil { 387 t.Fatal(err) 388 } 389 if err := shutdown(); err != nil { 390 t.Fatal(err) 391 } 392 393 if binary.BigEndian.Uint32(packet[20:24]) != 0x260f3671 || binary.BigEndian.Uint32(packet[61:65]) != 0 || binary.BigEndian.Uint32(walk[36:40]) != 22 || npc.position != [3]float32{64, 20, 56} || npc.motion.moving { 394 t.Fatal("NPC walked beyond the terrain grid") 395 } 396 }