aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/scene_water_test.go (7287B)


      1 package main
      2 
      3 import (
      4 	"ao/server/assets"
      5 	"bytes"
      6 	"crypto/sha256"
      7 	"encoding/binary"
      8 	"fmt"
      9 	"math"
     10 	"sort"
     11 	"testing"
     12 )
     13 
     14 func TestSceneWaterDecodesEverySelectedOutdoorPlayfield(t *testing.T) {
     15 	geometry, err := readSceneGeometry(assets.Files)
     16 	if err != nil {
     17 		t.Fatal(err)
     18 	}
     19 	t.Cleanup(func() { geometry.close() })
     20 	grounds, err := readTerrain()
     21 	if err != nil {
     22 		t.Fatal(err)
     23 	}
     24 	identities := make([]int, 0, len(geometry.playfields))
     25 	for identity := range geometry.playfields {
     26 		identities = append(identities, int(identity))
     27 	}
     28 	sort.Ints(identities)
     29 	golden := map[uint32]string{
     30 		540: "8b29477b8001893b0f9deac41fb56d1542f9ab5577049de8b4325f4f793a3e36",
     31 		600: "406dbbe1cf1dc18bb46efa74f586e471f165c15022646fb4253819a4fbd86661",
     32 		705: "7793e806c51b5aaecedb1729907f85434bd0ace715889e42fb95447dc1569bb4",
     33 		800: "2c11d7822547016cdd2f6f063317246d5b90ce5ffb9ee2dd3c4ef6482c0465c7",
     34 		950: "ea56822b3a15f9b95c4d33b07f44d913d70a281005bf426af700e7541cd12130",
     35 	}
     36 	volumes, entries, wetPlayfields := 0, 0, 0
     37 
     38 	for _, identity := range identities {
     39 		water := geometry.water(uint32(identity), grounds[uint32(identity)])
     40 		volumes += len(water.volumes)
     41 		if len(water.volumes) != 0 {
     42 			wetPlayfields++
     43 		}
     44 		digest := sha256.New()
     45 		write := func(value any) {
     46 			if err := binary.Write(digest, binary.LittleEndian, value); err != nil {
     47 				t.Fatal(err)
     48 			}
     49 		}
     50 		for _, volume := range water.volumes {
     51 			write(volume.vertices)
     52 			write(volume.normal)
     53 			write(volume.plane)
     54 			write(volume.kind)
     55 			write(volume.clipOffset)
     56 			write(volume.bounds)
     57 		}
     58 		for _, zone := range water.zones {
     59 			entries += len(zone.volumes)
     60 			write(uint32(len(zone.volumes)))
     61 			for _, index := range zone.volumes {
     62 				write(uint32(index))
     63 			}
     64 		}
     65 
     66 		if expected, sample := golden[uint32(identity)]; sample && fmt.Sprintf("%x", digest.Sum(nil)) != expected {
     67 			t.Fatalf("playfield %d differs from independent water parser: %x", identity, digest.Sum(nil))
     68 		}
     69 	}
     70 	if len(identities) != 188 || volumes != 46140 || entries != 788782 || wetPlayfields != 158 {
     71 		t.Fatal("selected water census changed", len(identities), volumes, entries, wetPlayfields)
     72 	}
     73 	t.Log("188 playfields, 46140 volumes, 788782 ordered zone entries; five complete geometry/list digests")
     74 }
     75 
     76 func sceneWaterFixture() []byte {
     77 	var data bytes.Buffer
     78 	header := [13]uint32{0: 8, 1: 705, 10: 705, 11: 2, 12: 1}
     79 	for _, value := range []any{header, uint32(0), uint32(1), uint32(960), uint32(3), [3][3]float32{{2, 5, 2}, {8, 5, 2}, {2, 5, 8}}, uint32(1), [3]uint16{0, 1, 2}} {
     80 		binary.Write(&data, binary.LittleEndian, value)
     81 	}
     82 	return data.Bytes()
     83 }
     84 
     85 func TestSceneWaterReadsFlagsAndAllowsTheFollowingPlayfieldTail(t *testing.T) {
     86 	ground := &terrain{boundsWidth: 2, boundsHeight: 2, spacing: 10}
     87 	layout := scenePlayfield{cellsX: 1, cellsZ: 1}
     88 
     89 	water, err := readSceneWater(append(sceneWaterFixture(), 1, 2, 3), layout, ground)
     90 
     91 	if err != nil {
     92 		t.Fatal(err)
     93 	}
     94 	if len(water.volumes) != 1 || len(water.zones[0].volumes) != 1 || water.maxHeight != 5 || water.zones[0].maxHeight != 5 {
     95 		t.Fatal("water framing or zone maxima", water)
     96 	}
     97 	if water.volumes[0].kind != 0 || water.volumes[0].clipOffset != 3 || water.volumes[0].normal != [3]float32{0, 1, 0} {
     98 		t.Fatal("authored flags or winding correction", water.volumes[0])
     99 	}
    100 }
    101 
    102 func TestSceneWaterRejectsTruncationAndMalformedPolygons(t *testing.T) {
    103 	ground := &terrain{boundsWidth: 2, boundsHeight: 2, spacing: 10}
    104 	layout := scenePlayfield{cellsX: 1, cellsZ: 1}
    105 	data := sceneWaterFixture()
    106 
    107 	for length := range len(data) {
    108 		if _, err := readSceneWater(data[:length], layout, ground); err == nil {
    109 			t.Fatal("accepted truncated water prefix", length)
    110 		}
    111 	}
    112 	for _, scenario := range []struct {
    113 		name   string
    114 		offset int
    115 		value  uint32
    116 	}{
    117 		{"inner version", 0, 7},
    118 		{"indoor template", 40, 999},
    119 		{"zero zone size", 44, 0},
    120 		{"zone terrain mismatch", 44, 1},
    121 		{"zone count", 48, 2},
    122 		{"objects exceed payload", 52, math.MaxUint32},
    123 		{"polygons exceed payload", 56, math.MaxUint32},
    124 		{"vertices exceed payload", 64, math.MaxUint32},
    125 		{"nonfinite vertex", 68, 0x7f800000},
    126 		{"faces exceed payload", 104, math.MaxUint32},
    127 		{"face vertex out of range", 108, 3},
    128 	} {
    129 		t.Run(scenario.name, func(t *testing.T) {
    130 			changed := bytes.Clone(data)
    131 			binary.LittleEndian.PutUint32(changed[scenario.offset:], scenario.value)
    132 			if _, err := readSceneWater(changed, layout, ground); err == nil {
    133 				t.Fatal("accepted malformed water data")
    134 			}
    135 		})
    136 	}
    137 }
    138 
    139 func TestSceneWaterRequiresStrictInteriorAndPositivePlaneHeight(t *testing.T) {
    140 	water := &sceneWater{}
    141 	water.create([3][3]float32{{2, 5, 2}, {8, 5, 2}, {2, 5, 8}}, 0)
    142 	water.create([3][3]float32{{2, 0, 2}, {8, 0, 2}, {2, 0, 8}}, 0)
    143 
    144 	for _, scenario := range []struct {
    145 		name   string
    146 		origin [3]float32
    147 		height float64
    148 	}{
    149 		{"inside below plane", [3]float32{3, 4, 3}, 5},
    150 		{"at plane", [3]float32{3, 5, 3}, 0},
    151 		{"above plane", [3]float32{3, 6, 3}, 0},
    152 		{"minimum X", [3]float32{2, 4, 3}, 0},
    153 		{"maximum X", [3]float32{8, 4, 3}, 0},
    154 		{"minimum Z", [3]float32{3, 4, 2}, 0},
    155 		{"maximum Z", [3]float32{3, 4, 8}, 0},
    156 		{"diagonal edge", [3]float32{5, 4, 5}, 0},
    157 		{"outside triangle inside bounds", [3]float32{7, 4, 7}, 0},
    158 	} {
    159 		t.Run(scenario.name, func(t *testing.T) {
    160 			if height := water.volumes[0].height(scenario.origin); height != scenario.height {
    161 				t.Fatal("strict water volume admission", height)
    162 			}
    163 		})
    164 	}
    165 	if height := water.volumes[1].height([3]float32{3, -1, 3}); height != 0 {
    166 		t.Fatal("nonpositive water plane must be rejected", height)
    167 	}
    168 }
    169 
    170 func TestSceneWaterUsesFirstMatchAndStopsAtItsClipVeto(t *testing.T) {
    171 	water := &sceneWater{zones: []waterZone{{volumes: []int{0, 1}, maxHeight: 7}}, maxHeight: 7}
    172 	water.create([3][3]float32{{2, 5, 2}, {8, 5, 2}, {2, 5, 8}}, 32)
    173 	water.create([3][3]float32{{2, 7, 2}, {8, 7, 2}, {2, 7, 8}}, 2)
    174 
    175 	accepted := water.surface(0, [3]float32{3, 1, 3}, 5)
    176 	vetoed := water.surface(0, [3]float32{3, 1, 3}, 3)
    177 	next := water.surface(0, [3]float32{3, 5, 3}, 7)
    178 	zoneMiss := water.surface(0, [3]float32{3, 7, 3}, 7)
    179 	playfieldMiss := water.surface(0, [3]float32{3, 8, 3}, 8)
    180 
    181 	if accepted != (waterContact{height: 5, normal: [3]float32{0, 1, 0}}) || next != (waterContact{height: 7, normal: [3]float32{0, 1, 0}, kind: 2}) {
    182 		t.Fatal("water selection must retain creation order", accepted, next)
    183 	}
    184 	if vetoed != (waterContact{height: -100000}) || zoneMiss != (waterContact{height: -100000}) || playfieldMiss != (waterContact{height: -9999}) {
    185 		t.Fatal("water clip veto or miss sentinels", vetoed, zoneMiss, playfieldMiss)
    186 	}
    187 }
    188 
    189 func TestSceneWaterCurvesOddKindsAndRejectsDegenerateVolumes(t *testing.T) {
    190 	water := &sceneWater{}
    191 
    192 	water.create([3][3]float32{{2, 2, 2}, {8, 2, 2}, {2, 6, 8}}, 3)
    193 	water.create([3][3]float32{{2, 2, 2}, {2, 2, 2}, {2, 6, 8}}, 3)
    194 	water.create([3][3]float32{{2, 2, 2}, {3, 2, 3}, {4, 2, 4}}, 0)
    195 	water.create([3][3]float32{{10000, 2, 10000}, {10010, 2, 10000}, {10000, 2, 10010}}, 0)
    196 
    197 	if len(water.volumes) != 19 {
    198 		t.Fatal("ten cubic strips must emit nineteen admitted triangles", len(water.volumes))
    199 	}
    200 	for _, volume := range water.volumes {
    201 		if volume.kind != 2 || volume.clipOffset != 100000 || volume.normal[1] < 0 {
    202 			t.Fatal("odd-kind normalization or default clip offset", volume)
    203 		}
    204 	}
    205 }