server/scene_surface_test.go (7852B)
1 package main 2 3 import ( 4 "ao/server/assets" 5 "bytes" 6 "compress/zlib" 7 "crypto/sha256" 8 "encoding/binary" 9 "fmt" 10 "io" 11 "math" 12 "slices" 13 "testing" 14 ) 15 16 func TestSceneSurfaceReadsShippedWallAndPortal(t *testing.T) { 17 database, err := readGameDatabase(assets.Files) 18 if err != nil { 19 t.Fatal(err) 20 } 21 t.Cleanup(func() { database.file.Close() }) 22 23 payload, version, err := database.read(1000013, 46203264) 24 if err != nil { 25 t.Fatal(err) 26 } 27 surface, err := decodeSceneSurface(payload, version) 28 if err != nil { 29 t.Fatal(err) 30 } 31 portalPayload, portalVersion, err := database.read(1000013, 46203265) 32 if err != nil { 33 t.Fatal(err) 34 } 35 portalSurface, err := decodeSceneSurface(portalPayload, portalVersion) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 if surface.primary == nil || surface.invisible != nil || len(surface.primary.vertices) != 176 || len(surface.primary.groups) != 21 { 41 t.Fatal("shipped wall lost invisible-to-primary fallback or geometry") 42 } 43 if len(surface.primary.groups[4].triangles) != 12 || surface.primary.groups[4].triangles[0] != [3]uint32{144, 145, 146} { 44 t.Fatal("shipped building bounds or triangle order changed", surface.primary.groups[4]) 45 } 46 for axis, expected := range [6]float32{971.94812, 16.41435, 519.74072, 972.45020, 21.42010, 521.42419} { 47 if math.Abs(float64(surface.primary.groups[4].bounds[axis]-expected)) > .0001 { 48 t.Fatal("shipped wall authored bounds changed", surface.primary.groups[4].bounds) 49 } 50 } 51 if portalSurface.primary == nil || portalSurface.invisible != nil { 52 t.Fatal("portal tail displaced ordinary static geometry") 53 } 54 t.Logf("wall payload SHA256 %x; nodes %d; first vertex %v; first wall triangle %v", sha256.Sum256(payload), len(surface.primary.nodes), surface.primary.vertices[0], surface.primary.groups[4].triangles[0]) 55 } 56 57 func TestSceneSurfaceRejectsBrokenFramingAndReferences(t *testing.T) { 58 database, err := readGameDatabase(assets.Files) 59 if err != nil { 60 t.Fatal(err) 61 } 62 t.Cleanup(func() { database.file.Close() }) 63 payload, _, err := database.read(1000013, 46203264) 64 if err != nil { 65 t.Fatal(err) 66 } 67 compressed, err := zlib.NewReader(bytes.NewReader(payload[4:])) 68 if err != nil { 69 t.Fatal(err) 70 } 71 data, err := io.ReadAll(compressed) 72 compressed.Close() 73 if err != nil { 74 t.Fatal(err) 75 } 76 if len(data) != 3022 { 77 t.Fatal("shipped malformed-input fixture changed", len(data)) 78 } 79 80 for _, scenario := range []struct { 81 name string 82 change func([]byte) []byte 83 }{ 84 {"truncated header", func(data []byte) []byte { return data[:3] }}, 85 {"wrong signature", func(data []byte) []byte { data[8] = 'X'; return data }}, 86 {"wrong dimension", func(data []byte) []byte { binary.LittleEndian.PutUint32(data[27:31], 4); return data }}, 87 {"invalid node count", func(data []byte) []byte { binary.LittleEndian.PutUint32(data[23:27], 0); return data }}, 88 {"invalid vertex reference", func(data []byte) []byte { data[1131] = 255; return data }}, 89 {"wrong tree length", func(data []byte) []byte { binary.LittleEndian.PutUint32(data[4:8], 3009); return data }}, 90 {"trailing data", func(data []byte) []byte { return append(data, 0) }}, 91 {"truncated portal count", func(data []byte) []byte { data[3018] = 1; return data }}, 92 } { 93 t.Run(scenario.name, func(t *testing.T) { 94 changed := scenario.change(append([]byte(nil), data...)) 95 var output bytes.Buffer 96 writer := zlib.NewWriter(&output) 97 if _, err := writer.Write(changed); err != nil { 98 t.Fatal(err) 99 } 100 if err := writer.Close(); err != nil { 101 t.Fatal(err) 102 } 103 encoded := binary.LittleEndian.AppendUint32(nil, uint32(len(changed))) 104 encoded = append(encoded, output.Bytes()...) 105 106 _, err := decodeSceneSurface(encoded, 4) 107 108 if err == nil { 109 t.Fatal("broken scene surface was accepted") 110 } 111 }) 112 } 113 } 114 115 func TestSceneSurfaceCatalogDecodesAllOutdoorCells(t *testing.T) { 116 database, err := readGameDatabase(assets.Files) 117 if err != nil { 118 t.Fatal(err) 119 } 120 t.Cleanup(func() { database.file.Close() }) 121 playfields, err := database.outdoorPlayfields() 122 if err != nil { 123 t.Fatal(err) 124 } 125 cells, trees, vertices, triangles, nodes := 0, 0, 0, 0, 0 126 maxVertices, maxGroups := 0, 0 127 var identities []uint32 128 for identity := range database.positions { 129 if identity[0] == 1000013 { 130 identities = append(identities, identity[1]) 131 } 132 } 133 slices.Sort(identities) 134 coordinates := sha256.New() 135 write := func(value any) { 136 if err := binary.Write(coordinates, binary.LittleEndian, value); err != nil { 137 t.Fatal(err) 138 } 139 } 140 141 for _, identity := range identities { 142 playfield, exists := playfields[identity>>16] 143 if !exists { 144 continue 145 } 146 if int(identity&65535) >= playfield.cellsX*playfield.cellsZ { 147 t.Fatal("surface identity exceeds playfield cell count", identity) 148 } 149 payload, version, err := database.read(1000013, identity) 150 if err != nil { 151 t.Fatal(err) 152 } 153 surface, err := decodeSceneSurface(payload, version) 154 if err != nil { 155 t.Fatal("shipped surface", identity, err) 156 } 157 cells++ 158 write(identity) 159 for _, tree := range []*surfaceTree{surface.primary, surface.invisible} { 160 if tree == nil { 161 write(uint32(0)) 162 continue 163 } 164 write(uint32(len(tree.vertices))) 165 write(tree.vertices) 166 trees++ 167 vertices += len(tree.vertices) 168 nodes += len(tree.nodes) 169 maxVertices = max(maxVertices, len(tree.vertices)) 170 maxGroups = max(maxGroups, len(tree.groups)) 171 for _, group := range tree.groups { 172 triangles += len(group.triangles) 173 } 174 } 175 } 176 177 if len(playfields) != 188 || cells != 154023 { 178 t.Fatal("selected outdoor resource coverage changed", len(playfields), cells) 179 } 180 if trees != 150620 || vertices != 9419782 || triangles != 15017490 || nodes != 1651254 || maxVertices != 3477 || maxGroups != 351 { 181 t.Fatal("selected scene geometry census changed", trees, vertices, triangles, nodes, maxVertices, maxGroups) 182 } 183 if digest := fmt.Sprintf("%x", coordinates.Sum(nil)); digest != "22d3412759c061e57a624ce84145118969a5552f704fe7f97b103448d030b257" { 184 t.Fatal("decoded vertices differ from the independent selected-archive parser", digest) 185 } 186 t.Log(fmt.Sprintf("cells=%d trees=%d vertices=%d triangles=%d nodes=%d maxVertices=%d maxGroups=%d", cells, trees, vertices, triangles, nodes, maxVertices, maxGroups)) 187 t.Logf("ordered decoded-coordinate SHA256 %x", coordinates.Sum(nil)) 188 } 189 190 func TestSceneSurfaceRejectsInvalidCompression(t *testing.T) { 191 for _, scenario := range []struct { 192 name string 193 payload []byte 194 version uint32 195 }{ 196 {"old format", make([]byte, 4), 3}, 197 {"short header", make([]byte, 3), 4}, 198 {"size limit", binary.LittleEndian.AppendUint32(nil, 4000001), 4}, 199 {"bad zlib stream", []byte{0, 0, 0, 0, 1, 2, 3}, 4}, 200 } { 201 t.Run(scenario.name, func(t *testing.T) { 202 _, err := decodeSceneSurface(scenario.payload, scenario.version) 203 204 if err == nil { 205 t.Fatal("invalid compressed scene surface was accepted") 206 } 207 }) 208 } 209 } 210 211 func TestScenePlayfieldUsesNativeCellSelection(t *testing.T) { 212 database, err := readGameDatabase(assets.Files) 213 if err != nil { 214 t.Fatal(err) 215 } 216 t.Cleanup(func() { database.file.Close() }) 217 playfields, err := database.outdoorPlayfields() 218 if err != nil { 219 t.Fatal(err) 220 } 221 222 if playfields[705] != (scenePlayfield{30, 30, 40, 40, 1200, 1200}) || playfields[950] != (scenePlayfield{6, 6, 40, 40, 240, 240}) { 223 t.Fatal("outdoor collision dimensions differ from native", playfields[705], playfields[950]) 224 } 225 for _, scenario := range []struct { 226 position [3]float32 227 cell uint32 228 }{ 229 {[3]float32{961.625, 16.41, 510.75}, 384}, 230 {[3]float32{971.701416, 16.41, 519.643555}, 384}, 231 {[3]float32{1000, 20, 520}, 415}, 232 {[3]float32{-1, 0, 80}, 60}, 233 {[3]float32{-40, 0, 80}, 0}, 234 {[3]float32{1200, 0, 80}, 0}, 235 {[3]float32{80, 0, 1200}, 0}, 236 } { 237 if cell := playfields[705].cellID(scenario.position); cell != scenario.cell { 238 t.Fatal("native cell truncation/fallback", scenario, cell) 239 } 240 } 241 }