server/npcs_test.go (6310B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 _ "embed" 7 "encoding/binary" 8 "encoding/json" 9 "net" 10 "os" 11 "testing" 12 "time" 13 ) 14 15 //go:embed testdata/npc-populations.json 16 var authoredPopulations []byte 17 18 var testLaunchDirectory string 19 20 func TestMain(m *testing.M) { 21 // Child test processes need the launch directory's external assets too. 22 var err error 23 testLaunchDirectory, err = os.Getwd() 24 if err != nil { 25 panic(err) 26 } 27 // Keep persistence tests away from the developer's live user files. 28 directory, err := os.MkdirTemp("", "ao-tests-") 29 if err != nil { 30 panic(err) 31 } 32 if err := os.Chdir(directory); err != nil { 33 panic(err) 34 } 35 code := m.Run() 36 if err := os.Chdir(os.TempDir()); err != nil { 37 panic(err) 38 } 39 if err := os.RemoveAll(directory); err != nil { 40 panic(err) 41 } 42 os.Exit(code) 43 } 44 45 func newTestServer(t *testing.T) *server { 46 t.Helper() 47 t.Chdir(t.TempDir()) 48 world := newServerForTest(t) 49 world.combatRoll = landedCombatRoll 50 return world 51 } 52 53 func readWorldUpdates(t *testing.T, client net.Conn) [][]byte { 54 t.Helper() 55 var updates [][]byte 56 for { 57 packet, err := readPacket(client) 58 if err != nil { 59 t.Fatal(err) 60 } 61 updates = append(updates, packet) 62 if binary.BigEndian.Uint32(packet[20:24]) == 0x5f52412e { 63 return updates 64 } 65 } 66 } 67 68 func TestPopulationsAreSharedAndZoneScoped(t *testing.T) { 69 world := newTestServer(t) 70 // Keep each population in range so the fixture isolates zone membership. 71 for _, npc := range world.characters { 72 npc.position = [3]float32{100, 0, 100} 73 if npc.motion != nil { 74 npc.motion.roaming = false 75 } 76 } 77 var clients []net.Conn 78 zones := []uint32{950, 950, 952, 954, 646} 79 for _, zone := range zones { 80 player := world.newCharacter() 81 player.playfield = zone 82 player.position = [3]float32{100, 0, 100} 83 } 84 startTestService(t, world) 85 for range zones { 86 client, connection := net.Pipe() 87 clients = append(clients, client) 88 defer client.Close() 89 client.SetDeadline(time.Now().Add(5 * time.Second)) 90 go func() { defer connection.Close(); world.serveWorldConnection(connection) }() 91 } 92 var populations []map[uint32][]byte 93 94 for index, client := range clients { 95 cookie := make([]byte, 12) 96 binary.BigEndian.PutUint32(cookie, uint32(index+1)) 97 client.Write(systemPacket(0x1b, cookie)) 98 population := make(map[uint32][]byte) 99 for _, packet := range readWorldUpdates(t, client) { 100 if binary.BigEndian.Uint32(packet[20:24]) == 0x271b3a6b && binary.BigEndian.Uint32(packet[37:41])&0x40001 != 0 { 101 population[binary.BigEndian.Uint32(packet[28:32])] = packet 102 } 103 } 104 populations = append(populations, population) 105 if index == 1 { 106 if _, err := readPacket(clients[0]); err != nil { 107 t.Fatal(err) 108 } 109 } 110 // Finish initialization before the next player broadcasts its arrival. 111 ping := systemPacket(1, nil) 112 binary.BigEndian.PutUint16(ping[2:4], 11) 113 client.Write(ping) 114 if _, err := readPacket(client); err != nil { 115 t.Fatal(err) 116 } 117 } 118 119 if len(populations[0]) != 50 || len(populations[1]) != 50 || len(populations[2]) != 58 || len(populations[3]) != 88 || len(populations[4]) != 1842 { 120 t.Fatalf("population counts: %d, %d, %d, %d, %d", len(populations[0]), len(populations[1]), len(populations[2]), len(populations[3]), len(populations[4])) 121 } 122 var leet []byte 123 for id, packet := range populations[0] { 124 if populations[1][id] == nil || !bytes.Equal(packet[20:], populations[1][id][20:]) { 125 t.Fatalf("NPC %d differs between players", id) 126 } 127 if populations[2][id] != nil || populations[3][id] != nil || populations[4][id] != nil { 128 t.Fatalf("Omni NPC %d leaked into another zone", id) 129 } 130 if string(packet[70:75]) == "Leet\x00" { 131 leet = packet 132 } 133 } 134 for index := 2; index < len(populations); index++ { 135 for _, packet := range populations[index] { 136 if binary.BigEndian.Uint32(packet[41:45]) != zones[index] || binary.BigEndian.Uint32(packet[12:16]) != uint32(index+1) { 137 t.Fatalf("NPC zone or recipient: %x", packet) 138 } 139 } 140 } 141 if leet[75] < 1 || leet[75] > 3 || binary.BigEndian.Uint32(leet[84:88]) != 17655 { 142 t.Fatalf("Leet level and appearance: %x", leet) 143 } 144 } 145 146 func TestEveryAuthoredPopulationIsPreserved(t *testing.T) { 147 var catalog []struct { 148 Playfield uint32 149 Count int 150 } 151 if err := json.Unmarshal(authoredPopulations, &catalog); err != nil { 152 t.Fatal(err) 153 } 154 world := newTestServer(t) 155 total, populated := 0, 0 156 var guard, highLevel []byte 157 158 for _, playfield := range catalog { 159 count := 0 160 for _, npc := range world.characters { 161 if !npc.npc || npc.playfield != playfield.Playfield { 162 continue 163 } 164 count++ 165 packet := characterPacket(npc) 166 group := gamedata.NPCGroups[npc.npcDefinitionID] 167 if playfield.Playfield == 950 && group.TemplateCode == "OTBG" { 168 guard = packet 169 } 170 if playfield.Playfield == 120 && group.TemplateCode == "SRBS" && binary.BigEndian.Uint32(packet[37:41])&0x1000 != 0 { 171 highLevel = packet 172 } 173 } 174 for _, resource := range world.worldResources { 175 if resource.playfield == playfield.Playfield { 176 count++ 177 } 178 } 179 if count != playfield.Count { 180 t.Fatalf("playfield %d: got %d NPCs, disc requests %d", playfield.Playfield, count, playfield.Count) 181 } 182 total += count 183 populated++ 184 } 185 186 if total != 68609 || populated != 251 || len(world.worldResources) != 586 || len(world.characters) != 68023 { 187 t.Fatalf("coverage: %d NPCs in %d playfields", total, populated) 188 } 189 if len(guard) < 195 { 190 t.Fatalf("missing reviewed human NPC: %x", guard) 191 } 192 nameEnd := 70 + int(guard[69]) 193 // The outfitted guide uses humanoid initialization with its original head. 194 headOffset := nameEnd + 39 + int(binary.BigEndian.Uint32(guard[nameEnd+35:nameEnd+39])) 195 if binary.BigEndian.Uint32(guard[37:41]) != 0x402da || binary.BigEndian.Uint32(guard[headOffset:headOffset+4]) != 40687 || 196 !bytes.Equal(guard[headOffset+43:headOffset+61], []byte("hands\x00\x00\x00\x22\x45\x00\x00\x00\x00\x00\x00\x00\x00")) { 197 t.Fatalf("human NPC head and hand material: %x", guard) 198 } 199 if binary.BigEndian.Uint32(guard[nameEnd+29:nameEnd+33]) != 26088 { 200 t.Fatal("human NPC did not receive the original Solitus model") 201 } 202 if len(highLevel) < 91 || binary.BigEndian.Uint32(highLevel[37:41])&0x1000 == 0 || binary.BigEndian.Uint16(highLevel[70+int(highLevel[69]):72+int(highLevel[69])]) != 300 { 203 t.Fatalf("fixed NPC level: %x", highLevel) 204 } 205 }