server/scene_geometry_test.go (5543B)
1 package main 2 3 import ( 4 "ao/server/assets" 5 "io" 6 "math" 7 "sync" 8 "sync/atomic" 9 "testing" 10 "time" 11 ) 12 13 func TestSceneCellLineRejectsHitsOutsideTheVisitedCell(t *testing.T) { 14 wall := func(x float32) *sceneSurface { 15 return &sceneSurface{primary: &surfaceTree{ 16 vertices: [][3]float32{{x, 0, 0}, {x, 0, 2}, {x, 2, 0}}, 17 groups: []surfaceGroup{{bounds: [6]float32{x, 0, 0, x, 2, 2}, triangles: [][3]uint32{{0, 1, 2}}}}, 18 nodes: []surfaceNode{{groups: []uint32{0}}}, 19 }} 20 } 21 geometry := &sceneGeometry{ 22 playfields: map[uint32]scenePlayfield{705: {cellsX: 3, cellsZ: 1, cellWidth: 1, cellDepth: 1, sizeX: 3, sizeZ: 1}}, 23 surfaces: sceneResourcesForTest(map[uint32]*sceneSurface{705 << 16: wall(1.5), 705<<16 | 1: wall(1.75), 705<<16 | 2: nil}), 24 } 25 26 contact := geometry.cellLine(705, [3]float32{.5, .5, .5}, [3]float32{2.5, .5, .5}) 27 outside := geometry.cellLine(705, [3]float32{-1.5, .5, .5}, [3]float32{2.5, .5, .5}) 28 unknown := geometry.cellLine(9999, [3]float32{.5, .5, .5}, [3]float32{2.5, .5, .5}) 29 30 if !contact.hit || contact.point != [3]float32{1.75, .5, .5} { 31 t.Fatal("out-of-cell hit must not prevent the next cell query", contact) 32 } 33 if outside.hit || unknown.hit { 34 t.Fatal("unclipped cell traversal must stop outside the grid", outside, unknown) 35 } 36 } 37 38 func TestSceneSphereUsesOnlyItsCenterCell(t *testing.T) { 39 geometry := &sceneGeometry{ 40 playfields: map[uint32]scenePlayfield{705: {cellsX: 2, cellsZ: 1, cellWidth: 1, cellDepth: 1, sizeX: 2, sizeZ: 1}}, 41 surfaces: sceneResourcesForTest(map[uint32]*sceneSurface{ 42 705 << 16: nil, 43 705<<16 | 1: {primary: &surfaceTree{ 44 vertices: [][3]float32{{1.1, 0, 0}, {1.1, 0, 2}, {1.1, 2, 0}}, 45 groups: []surfaceGroup{{bounds: [6]float32{1.1, 0, 0, 1.1, 2, 2}, triangles: [][3]uint32{{0, 1, 2}}}}, 46 nodes: []surfaceNode{{groups: []uint32{0}}}, 47 }}, 48 }), 49 } 50 51 neighbor := geometry.sphere(705, [3]float32{.99, .5, .5}, .5) 52 center := geometry.sphere(705, [3]float32{1.01, .5, .5}, .5) 53 54 if neighbor.hit || !center.hit || center.normal != [3]float32{-1, 0, 0} { 55 t.Fatal("native sphere lookup must not expand into neighboring cells", neighbor, center) 56 } 57 } 58 59 func TestSceneGeometrySharesShippedCellAcrossConcurrentQueries(t *testing.T) { 60 geometry, err := readSceneGeometry(assets.Files) 61 if err != nil { 62 t.Fatal(err) 63 } 64 t.Cleanup(func() { geometry.close() }) 65 grounds, err := readTerrain() 66 if err != nil { 67 t.Fatal(err) 68 } 69 var workers sync.WaitGroup 70 type result struct { 71 correct bool 72 surface *sceneSurface 73 water *sceneWater 74 } 75 results := make(chan result, 16) 76 77 for range 16 { 78 workers.Add(1) 79 go func() { 80 defer workers.Done() 81 sphere := geometry.sphere(705, [3]float32{971.72, 16.66075, 519.66}, .25075) 82 line := geometry.cellLine(705, [3]float32{970, 17.5, 519.8}, [3]float32{975, 17.5, 519.8}) 83 var point, normal [3]float32 84 geometry.floor(705, grounds[705], [3]float32{971.72, 17, 519.66}, &point, &normal) 85 results <- result{sphere.hit && line.hit && math.Abs(float64(line.point[0]-971.9442138671875)) < .0001 && math.Abs(float64(point[1]-16.4)) < .0001 && normal == [3]float32{0, 1, 0}, 86 geometry.surface(46203264), geometry.water(705, grounds[705])} 87 }() 88 } 89 workers.Wait() 90 close(results) 91 92 surface, water := geometry.surface(46203264), geometry.water(705, grounds[705]) 93 for query := range results { 94 if !query.correct || query.surface != surface || query.water != water { 95 t.Fatal("concurrent archive-backed queries did not share immutable geometry") 96 } 97 } 98 } 99 100 // A slow archive read must hold only callers of that particular resource. 101 type gatedSceneArchive struct { 102 file interface { 103 io.ReaderAt 104 io.Closer 105 } 106 offset int64 107 started chan struct{} 108 release <-chan struct{} 109 reads atomic.Int32 110 } 111 112 func (archive *gatedSceneArchive) ReadAt(data []byte, offset int64) (int, error) { 113 if offset == archive.offset { 114 archive.reads.Add(1) 115 archive.started <- struct{}{} 116 <-archive.release 117 } 118 return archive.file.ReadAt(data, offset) 119 } 120 121 func (archive *gatedSceneArchive) Close() error { return archive.file.Close() } 122 123 func TestSceneGeometrySlowSurfaceLoadDoesNotBlockUnrelatedModel(t *testing.T) { 124 geometry, err := readSceneGeometry(assets.Files) 125 if err != nil { 126 t.Fatal(err) 127 } 128 t.Cleanup(func() { geometry.close() }) 129 release := make(chan struct{}) 130 archive := &gatedSceneArchive{file: geometry.database.file, 131 offset: int64(geometry.database.positions[[2]uint32{1000013, 46203264}]), 132 started: make(chan struct{}, 16), release: release} 133 geometry.database.file = archive 134 unblock := sync.OnceFunc(func() { close(release) }) 135 var workers sync.WaitGroup 136 t.Cleanup(func() { unblock(); workers.Wait() }) 137 contacts := make(chan bool, 16) 138 for range 16 { 139 workers.Go(func() { 140 contacts <- geometry.sphere(705, [3]float32{971.72, 16.66075, 519.66}, .25075).hit 141 }) 142 } 143 select { 144 case <-archive.started: 145 case <-time.After(time.Second): 146 t.Fatal("surface decoding did not reach the archive") 147 } 148 model := make(chan collisionSphere, 1) 149 workers.Go(func() { model <- geometry.model(5907) }) 150 select { 151 case sphere := <-model: 152 if sphere.radius != .25075000524520874 { 153 t.Fatal("unrelated model lost its authored radius", sphere) 154 } 155 case <-time.After(time.Second): 156 t.Fatal("one slow resource blocked unrelated geometry") 157 } 158 unblock() 159 workers.Wait() 160 161 for range 16 { 162 if !<-contacts { 163 t.Fatal("surface readers did not receive the decoded wall") 164 } 165 } 166 if archive.reads.Load() != 1 { 167 t.Fatal("concurrent queries decoded the same resource more than once", archive.reads.Load()) 168 } 169 }