server/terrain_test.go (4055B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "crypto/sha256" 6 "encoding/binary" 7 "fmt" 8 "math" 9 "testing" 10 "testing/fstest" 11 ) 12 13 func TestTerrainUsesSelectedClientHeightGrids(t *testing.T) { 14 maps, err := readTerrain() 15 if err != nil { 16 t.Fatal(err) 17 } 18 19 for _, expected := range []struct { 20 playfield uint32 21 digest string 22 }{ 23 {551, "f01d4ce7fc3ec37fdfabbd1ef449aac7acdabc7b6fb443df0d9e1dab3e07d0e9"}, 24 {565, "1944dd974fe7621b1726054c3c1ac67977d6a3e947ee2eccbee8d7372e858ad3"}, 25 {585, "7b9e5110e1e3cf985f6b484891120f6049740e2204954ffb601d4fe58810dcc5"}, 26 {670, "126f91ea3512c22f72352525cdc15df4f034a17ce505354e7ef94068f787ceea"}, 27 {716, "d043ce534a88e3e3c3be7297919fea472588a9fb5bd63444f9142b344f3aef69"}, 28 {641, "63b448f5d542bb315a2ab891de42c47911cf7182fe5857dbcee46f0a48d8eafb"}, 29 {706, "027166b25a6385ecda76b3baa68fafe2e7115565818fc8162c06149123da10c7"}, 30 } { 31 if digest := fmt.Sprintf("%x", sha256.Sum256(maps[expected.playfield].heights)); digest != expected.digest { 32 t.Fatal("terrain differs from the selected 14.9.1 archive", expected.playfield, digest) 33 } 34 } 35 if gamedata.TerrainEvidence.ClientVersion != "14.9.1" || gamedata.TerrainEvidence.SourceIndexSHA256 != "46e5dc54af9358cc9bff05f4358e86af630e497bc677f361c133c88073b84d71" { 36 t.Fatal("terrain provenance differs from the selected client", gamedata.TerrainEvidence.ClientVersion, gamedata.TerrainEvidence.SourceIndexSHA256) 37 } 38 } 39 40 func TestOriginalTerrainGrids(t *testing.T) { 41 maps, err := readTerrain() 42 if err != nil { 43 t.Fatal(err) 44 } 45 46 height, inside := maps[950].elevation(59.4206, 83.75195) 47 border, borderInside := maps[950].elevation(239.75, 239.75) 48 _, outside := maps[950].elevation(240, 240) 49 50 if len(maps) != 188 || maps[505].width != 896 || maps[505].height != 1536 || maps[505].boundsWidth != 860 || maps[505].boundsHeight != 1160 || maps[540].width != 224 || maps[950].width != 64 || maps[641] == nil || maps[706] == nil { 51 t.Fatal("terrain dimensions differ from the original ground archives") 52 } 53 if !inside || math.Abs(float64(height-15.765675)) > 0.0001 || !borderInside || border < 0 || outside { 54 t.Fatalf("terrain sampling: height %v, border %v, inside %v/%v, outside %v", height, border, inside, borderInside, outside) 55 } 56 if maps[1000] != nil { 57 t.Fatal("indoor map without a height table received terrain") 58 } 59 } 60 61 func TestTerrainRejectsTruncatedGrid(t *testing.T) { 62 useNPCData(t, testNPCData()) 63 data := append([]byte("AOTERR02"), 1, 0, 0, 0) 64 data = append(data, 182, 3, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 128, 64, 0, 0, 128, 63) 65 useResources(t, fstest.MapFS{"terrain.bin": {Data: data}}) 66 67 _, err := readTerrain() 68 69 if err == nil || err.Error() != "invalid terrain record for playfield 950" { 70 t.Fatalf("truncated height grid: %v", err) 71 } 72 } 73 74 func TestTerrainUsesNativeTrianglePlanesAndEdgeSamples(t *testing.T) { 75 data := append([]byte("AOTERR02"), 1, 0, 0, 0) 76 data = binary.LittleEndian.AppendUint32(data, 950) 77 for range 4 { 78 data = binary.LittleEndian.AppendUint16(data, 3) 79 } 80 data = binary.LittleEndian.AppendUint32(data, math.Float32bits(4)) 81 data = binary.LittleEndian.AppendUint32(data, math.Float32bits(1)) 82 data = append(data, 0, 0, 0, 99, 0, 4, 0, 99, 0, 0, 0, 99, 99, 99, 99, 99) 83 useResources(t, fstest.MapFS{"terrain.bin": {Data: data}}) 84 maps, err := readTerrain() 85 if err != nil { 86 t.Fatal(err) 87 } 88 89 for _, scenario := range []struct{ x, z, height float32 }{ 90 {3, .5, .5}, {.5, 3, .5}, {2, 2, 2}, 91 {4.5, .5, .5}, {7.5, 3.5, .5}, {6, 2, 2}, 92 {8.5, 3.5, 0}, {3.5, 8.5, 0}, {11.75, 11.75, 0}, 93 } { 94 height, inside := maps[950].elevation(scenario.x, scenario.z) 95 if !inside || height != scenario.height { 96 t.Fatal("physical terrain plane or final-edge clamp", scenario, height, inside) 97 } 98 } 99 for _, point := range [][2]float32{{-1, 0}, {0, -1}, {12, 0}, {0, 12}, {13, 2}, {float32(math.NaN()), 0}, {0, float32(math.Inf(1))}} { 100 if height, inside := maps[950].elevation(point[0], point[1]); inside || height != 0 { 101 t.Fatal("outside header domain became valid padded terrain", point, height, inside) 102 } 103 } 104 }