server/scene_queries_test.go (8076B)
1 package main 2 3 import ( 4 "math" 5 "testing" 6 ) 7 8 func TestSceneClippingUsesBoundaryInterpolationAndHighMargin(t *testing.T) { 9 layout := scenePlayfield{sizeX: 10, sizeZ: 10} 10 11 for _, scenario := range []struct { 12 name string 13 origin, end [3]float32 14 first, last [3]float32 15 accepted bool 16 }{ 17 {"inside", [3]float32{0, 3, 0}, [3]float32{9, 4, 9}, [3]float32{0, 3, 0}, [3]float32{9, 4, 9}, true}, 18 {"high X", [3]float32{5, 0, 5}, [3]float32{15, 10, 5}, [3]float32{5, 0, 5}, [3]float32{9.9, 5, 5}, true}, 19 {"low Z", [3]float32{5, 0, -5}, [3]float32{5, 10, 5}, [3]float32{5, 5, 0}, [3]float32{5, 10, 5}, true}, 20 {"both sides", [3]float32{-5, 0, 5}, [3]float32{15, 20, 5}, [3]float32{0, 5, 5}, [3]float32{9.9, 15, 5}, true}, 21 {"shared outside side", [3]float32{11, 0, 3}, [3]float32{12, 5, 9}, [3]float32{11, 0, 3}, [3]float32{12, 5, 9}, false}, 22 } { 23 t.Run(scenario.name, func(t *testing.T) { 24 origin, end := scenario.origin, scenario.end 25 accepted := layout.clip(&origin, &end) 26 if accepted != scenario.accepted || origin != scenario.first || end != scenario.last { 27 t.Fatal("native clipped segment", origin, end, accepted) 28 } 29 }) 30 } 31 } 32 33 func TestSceneVetoRevertsBeforeClampingAndKeepsValidZero(t *testing.T) { 34 layout := scenePlayfield{sizeX: 10, sizeZ: 20} 35 36 for _, scenario := range []struct { 37 name string 38 position [3]float32 39 previous *[3]float32 40 veto bool 41 result [3]float32 42 }{ 43 {"valid zero", [3]float32{}, nil, false, [3]float32{}}, 44 {"revert whole pose", [3]float32{11, 10, 15}, &[3]float32{4, 5, 6}, true, [3]float32{4, 5, 6}}, 45 {"clamp reverted zero edges", [3]float32{11, 10, 15}, &[3]float32{0, 5, 0}, true, [3]float32{.1, 5, .1}}, 46 {"final clamp", [3]float32{-1, 2000, 21}, nil, true, [3]float32{.1, 1999.9, 19.9}}, 47 {"lower height", [3]float32{5, -1, 5}, nil, true, [3]float32{5, .01, 5}}, 48 } { 49 t.Run(scenario.name, func(t *testing.T) { 50 position := scenario.position 51 veto := layout.veto(&position, scenario.previous) 52 if veto != scenario.veto || position != scenario.result { 53 t.Fatal("native outdoor bounds veto", position, veto) 54 } 55 }) 56 } 57 } 58 59 func sceneSlopeFixture(offset float32) *sceneSurface { 60 return &sceneSurface{primary: &surfaceTree{ 61 vertices: [][3]float32{{0, offset, 0}, {0, offset + 8, 8}, {8, offset, 0}}, 62 groups: []surfaceGroup{{bounds: [6]float32{0, offset, 0, 8, offset + 8, 8}, triangles: [][3]uint32{{0, 1, 2}}}}, 63 nodes: []surfaceNode{{groups: []uint32{0}}}, 64 }} 65 } 66 67 func TestSceneLineChoosesNearestSurfaceAndTerrainOnEqualHits(t *testing.T) { 68 ground := &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 5, scale: 1, heights: make([]byte, 9)} 69 for _, scenario := range []struct { 70 name string 71 offset, height float32 72 normal [3]float32 73 }{ 74 {"static nearer", -1, 1, [3]float32{0, .70710677, -.70710677}}, 75 {"terrain wins tie", -2, 0, [3]float32{0, 1, 0}}, 76 {"terrain nearer", -3, 0, [3]float32{0, 1, 0}}, 77 } { 78 t.Run(scenario.name, func(t *testing.T) { 79 geometry := &sceneGeometry{ 80 playfields: map[uint32]scenePlayfield{705: {cellsX: 1, cellsZ: 1, cellWidth: 10, cellDepth: 10, sizeX: 10, sizeZ: 10}}, 81 surfaces: sceneResourcesForTest(map[uint32]*sceneSurface{705 << 16: sceneSlopeFixture(scenario.offset)}), 82 } 83 84 contact := geometry.line(705, ground, [3]float32{1, 5, 2}, [3]float32{1, -5, 2}, true) 85 86 if !contact.hit || math.Abs(float64(contact.point[1]-scenario.height)) > .000001 || contact.normal != scenario.normal { 87 t.Fatal("combined terrain/static segment selection", contact) 88 } 89 }) 90 } 91 } 92 93 func TestSceneLineRetainsTerrainDisplacementAfterClipping(t *testing.T) { 94 geometry := &sceneGeometry{ 95 playfields: map[uint32]scenePlayfield{705: {cellsX: 1, cellsZ: 1, cellWidth: 10, cellDepth: 10, sizeX: 10, sizeZ: 10}}, 96 surfaces: sceneResourcesForTest(map[uint32]*sceneSurface{705 << 16: nil}), 97 } 98 ground := &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 5, scale: 1, 99 heights: []byte{2, 2, 2, 2, 2, 2, 2, 2, 2}} 100 101 clipped := geometry.line(705, ground, [3]float32{-5, 10, 2}, [3]float32{5, 0, 2}, true) 102 unclipped := geometry.line(705, ground, [3]float32{-5, 10, 2}, [3]float32{5, 0, 2}, false) 103 outside := geometry.line(705, ground, [3]float32{-5, 10, 2}, [3]float32{-1, 0, 2}, true) 104 105 if !clipped.hit || clipped.point != [3]float32{3, 2, 2} || clipped.fraction != .3 || !unclipped.hit || unclipped.fraction != .8 || outside.hit { 106 t.Fatal("clipped terrain must keep the original displacement and distance gate", clipped, unclipped, outside) 107 } 108 } 109 110 func TestSceneFloorSelectsTerrainStaticAndLiquidInNativeOrder(t *testing.T) { 111 ground := &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 5, scale: 1, 112 heights: []byte{2, 2, 2, 2, 2, 2, 2, 2, 2}} 113 for _, scenario := range []struct { 114 name string 115 originY, offset, waterY float32 116 height float32 117 normal [3]float32 118 }{ 119 {"static above terrain", 5, 1, 0, 3, [3]float32{0, .70710677, -.70710677}}, 120 {"terrain wins tie", 5, 0, 0, 2, [3]float32{0, 1, 0}}, 121 {"below terrain skips static", 1, 1, 0, 2, [3]float32{0, 1, 0}}, 122 {"below static keeps terrain", 2.5, 1, 0, 2, [3]float32{0, 1, 0}}, 123 {"liquid lifts selected floor", 5, 1, 5, 3.8, [3]float32{0, 1, 0}}, 124 {"liquid clearance keeps static normal", 5, 1, 4, 3, [3]float32{0, .70710677, -.70710677}}, 125 } { 126 t.Run(scenario.name, func(t *testing.T) { 127 water := &sceneWater{zones: []waterZone{{maxHeight: -1}}} 128 if scenario.waterY > 0 { 129 water.create([3][3]float32{{0, scenario.waterY, 0}, {8, scenario.waterY, 0}, {0, scenario.waterY, 8}}, 2) 130 water.zones[0] = waterZone{volumes: []int{0}, maxHeight: scenario.waterY} 131 water.maxHeight = scenario.waterY 132 } 133 geometry := &sceneGeometry{ 134 playfields: map[uint32]scenePlayfield{705: {cellsX: 1, cellsZ: 1, cellWidth: 10, cellDepth: 10, sizeX: 10, sizeZ: 10}}, 135 surfaces: sceneResourcesForTest(map[uint32]*sceneSurface{705 << 16: sceneSlopeFixture(scenario.offset)}), 136 waters: sceneResourcesForTest(map[uint32]*sceneWater{705: water}), 137 } 138 point, normal := [3]float32{99, 99, 99}, [3]float32{99, 99, 99} 139 140 medium := geometry.floor(705, ground, [3]float32{1, scenario.originY, 2}, &point, &normal) 141 142 if point[0] != 1 || point[2] != 2 || math.Abs(float64(point[1]-scenario.height)) > .000001 || normal != scenario.normal { 143 t.Fatal("combined terrain/static/liquid floor", point, normal) 144 } 145 if scenario.waterY > 0 && medium != (waterContact{height: scenario.waterY, normal: [3]float32{0, 1, 0}, kind: 2}) { 146 t.Fatal("liquid medium must retain raw water height even without floor replacement", medium) 147 } 148 }) 149 } 150 } 151 152 func TestSceneFloorPreservesOutputsOutsideCellsAndTerrainNormalOnInvalidSamples(t *testing.T) { 153 geometry := &sceneGeometry{ 154 playfields: map[uint32]scenePlayfield{705: {cellsX: 1, cellsZ: 1, cellWidth: 10, cellDepth: 10, sizeX: 10, sizeZ: 10}}, 155 surfaces: sceneResourcesForTest(map[uint32]*sceneSurface{705 << 16: nil}), 156 waters: sceneResourcesForTest(map[uint32]*sceneWater{705: {zones: []waterZone{{maxHeight: -1}}}}), 157 } 158 ground := &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 5, scale: 1, heights: make([]byte, 9)} 159 point, normal := [3]float32{1, 2, 3}, [3]float32{4, 5, 6} 160 161 missing := geometry.floor(999, ground, [3]float32{}, &point, &normal) 162 outside := geometry.floor(705, ground, [3]float32{10, 2, 2}, &point, &normal) 163 unchangedPoint, unchangedNormal := point, normal 164 boundary := geometry.floor(705, ground, [3]float32{-.1, 2, 2}, &point, &normal) 165 166 if missing != (waterContact{}) || outside != (waterContact{}) || unchangedPoint != [3]float32{1, 2, 3} || unchangedNormal != [3]float32{4, 5, 6} { 167 t.Fatal("invalid cell queries must retain caller outputs", unchangedPoint, unchangedNormal) 168 } 169 if point != [3]float32{-.1, 0, 2} || normal != [3]float32{4, 5, 6} || boundary.height != -100000 { 170 t.Fatal("cell truncation can admit a point outside terrain; numeric height zero leaves normal", point, normal, boundary) 171 } 172 }