server/mission_scene_test.go (18908B)
1 package main 2 3 import ( 4 "ao/server/assets" 5 "bytes" 6 "crypto/sha256" 7 "encoding/binary" 8 "fmt" 9 "math" 10 "os" 11 "slices" 12 "sync" 13 "testing" 14 ) 15 16 func missionGeometryFixture(t *testing.T) *sceneGeometry { 17 t.Helper() 18 database, err := readGameDatabase(assets.Files) 19 if err != nil { 20 t.Fatal(err) 21 } 22 geometry := newSceneGeometry(database) 23 t.Cleanup(func() { geometry.close() }) 24 return geometry 25 } 26 27 func TestMissionSceneSamplesDungeonTrianglesAndRotatesGridNormals(t *testing.T) { 28 geometry := missionGeometryFixture(t) 29 ground := &dungeonTilemap{width: 4, depth: 4, spacing: 2, scale: 1, 30 tiles: bytes.Repeat([]byte{1}, 16), floors: make([]byte, 16), ceilings: bytes.Repeat([]byte{32}, 16), tilesetHeights: []float32{0, 20}} 31 ground.floors[0], ground.floors[1], ground.floors[4], ground.floors[5] = 1, 3, 2, 7 32 surface := geometry.surface(331 << 16) 33 for _, scenario := range []struct { 34 name string 35 rotation byte 36 origin [3]float32 37 height float32 38 normal [3]float32 39 }{ 40 {"lower triangle", 0, [3]float32{2.5, 10, 3.5}, 3, [3]float32{-.9128709, .36514837, -.18257418}}, 41 {"upper triangle", 0, [3]float32{3.5, 10, 2.5}, 3.5, [3]float32{-.4082483, .4082483, -.8164966}}, 42 {"rotated lower triangle", 1, [3]float32{3.5, 10, 5.5}, 3, [3]float32{-.18257418, .36514837, .9128709}}, 43 } { 44 t.Run(scenario.name, func(t *testing.T) { 45 scene := &missionScene{ground: ground, rooms: []placedRoom{{position: [3]float32{4, 0, 4}, rotation: scenario.rotation, surface: surface, 46 template: &dungeonRoom{bounds: [4]uint16{0, 0, 4, 4}, position: [3]float32{10000, 0, 10000}, rotation: 1}}}} 47 var point, normal [3]float32 48 49 scene.floor(scenario.origin, &point, &normal) 50 51 if point != [3]float32{scenario.origin[0], scenario.height, scenario.origin[2]} { 52 t.Fatal("dungeon fixed-diagonal floor height", point) 53 } 54 for axis := range normal { 55 if math.Abs(float64(normal[axis]-scenario.normal[axis])) > .000001 { 56 t.Fatal("placed grid slope normal", normal) 57 } 58 } 59 }) 60 } 61 } 62 63 func TestMissionSceneKeepsNativeStaticFloorBoundAndUnknownRoomOutputs(t *testing.T) { 64 scene := &missionScene{ground: &dungeonTilemap{width: 4, depth: 4, spacing: 2, scale: 1, 65 tiles: bytes.Repeat([]byte{1}, 16), floors: make([]byte, 16), ceilings: bytes.Repeat([]byte{32}, 16), tilesetHeights: []float32{0, 20}}, 66 rooms: []placedRoom{{position: [3]float32{4, 0, 4}, template: &dungeonRoom{bounds: [4]uint16{0, 0, 4, 4}, position: [3]float32{4, 0, 4}}, surface: sceneSlopeFixture(0)}}} 67 var point, normal [3]float32 68 69 scene.floor([3]float32{2, 2.75, 2}, &point, &normal) 70 if point != [3]float32{2, 2, 2} || math.Abs(float64(normal[2]+.70710677)) > .000001 || math.Abs(float64(normal[1]-.70710677)) > .000001 { 71 t.Fatal("nearby mesh floor must beat the base tile floor", point, normal) 72 } 73 scene.floor([3]float32{2, 3.01, 2}, &point, &normal) 74 if point != [3]float32{2, .001, 2} || normal != [3]float32{0, 1, 0} { 75 t.Fatal("mesh floor beyond the native one-unit probe must miss", point, normal) 76 } 77 scene.rooms[0].surface = &sceneSurface{} 78 scene.floor([3]float32{2, 2.75, 2}, &point, &normal) 79 if point != [3]float32{2, .001, 2} || normal != [3]float32{0, 1, 0} { 80 t.Fatal("empty mesh must retain the tile floor", point, normal) 81 } 82 normal = [3]float32{7, 8, 9} 83 scene.floor([3]float32{50, 1, 50}, &point, &normal) 84 if point != [3]float32{50, 0, 50} || normal != [3]float32{7, 8, 9} { 85 t.Fatal("missing room resets only point height", point, normal) 86 } 87 } 88 89 func TestMissionSceneQueriesOriginalWallsAndFloor(t *testing.T) { 90 geometry := missionGeometryFixture(t) 91 scene, err := geometry.missionScene(missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10, 92 rooms: []missionRoom{{template: 0, x: 1, z: 3, rotation: 2}, {template: 37, x: 1, z: 1}}}) 93 if err != nil { 94 t.Fatal(err) 95 } 96 97 wall := scene.line([3]float32{15, 1, 16}, [3]float32{20, 1, 16}) 98 floor := scene.line([3]float32{15, 1, 35}, [3]float32{15, -1, 35}) 99 outside := scene.line([3]float32{0, 1, 0}, [3]float32{5, 1, 0}) 100 sphere := scene.sphere([3]float32{17.1, .8, 16}, .25) 101 102 if !wall.hit || math.Abs(float64(wall.point[0]-17.601997)) > .00001 || wall.normal != [3]float32{-1, 0, 0} { 103 t.Fatal("placed original entrance wall", wall) 104 } 105 if !floor.hit || math.Abs(float64(floor.point[1]-.001)) > .000001 || floor.normal != [3]float32{0, 1, 0} || outside.hit { 106 t.Fatal("native dungeon floor fallback or outside endpoints", floor, outside) 107 } 108 if !sphere.hit || math.Abs(float64(sphere.point[0]-17.255186)) > .00001 || math.Abs(float64(sphere.normal[0]+.7071066)) > .000001 || math.Abs(float64(sphere.normal[1]-.707107)) > .000001 { 109 t.Fatal("original mesh sphere contact must rotate its point and normal", sphere) 110 } 111 } 112 113 func TestDungeonLibrariesMatchIndependentOriginalRoomDecoding(t *testing.T) { 114 geometry := missionGeometryFixture(t) 115 for _, scenario := range []struct { 116 identity uint32 117 rooms int 118 hash string 119 }{ 120 {331, 42, "915a805373ab7e9c0eef6b498c329f1d342b2f549cbe08d6259f439b329b6c9c"}, 121 {341, 103, "ffe8298c354033050e29a4291b670faa2543cdfba3533770ca11b6fe84645ec2"}, 122 {346, 72, "2b543b43607515188b1d3f7c96c8375885e4f7af11d3c156430d423aaeb43500"}, 123 {351, 81, "5cf78291e89470ac6d50a2b650195741967e0c16f01a85b37c55ace020872311"}, 124 } { 125 t.Run(fmt.Sprint(scenario.identity), func(t *testing.T) { 126 library, err := geometry.dungeonLibrary(scenario.identity) 127 128 if err != nil { 129 t.Fatal(err) 130 } 131 if len(library.rooms) != scenario.rooms { 132 t.Fatal("original room count", len(library.rooms)) 133 } 134 var fields bytes.Buffer 135 for _, room := range library.rooms { 136 binary.Write(&fields, binary.LittleEndian, room.bounds) 137 binary.Write(&fields, binary.LittleEndian, room.position) 138 fields.WriteByte(room.rotation) 139 binary.Write(&fields, binary.LittleEndian, room.minHeight) 140 binary.Write(&fields, binary.LittleEndian, room.water) 141 binary.Write(&fields, binary.LittleEndian, uint16(len(room.doors))) 142 binary.Write(&fields, binary.LittleEndian, room.doors) 143 } 144 if fmt.Sprintf("%x", sha256.Sum256(fields.Bytes())) != scenario.hash { 145 t.Fatal("room bounds, template pose, floor baseline, water or door bindings differ") 146 } 147 }) 148 } 149 } 150 151 func TestMissionScenesLoadEveryDryOriginalRoomMesh(t *testing.T) { 152 geometry := missionGeometryFixture(t) 153 for _, identity := range []uint32{331, 341, 346, 351} { 154 library, err := geometry.dungeonLibrary(identity) 155 if err != nil { 156 t.Fatal(err) 157 } 158 for index, room := range library.rooms { 159 if room.water { 160 continue 161 } 162 t.Run(fmt.Sprintf("%d/%d", identity, index), func(t *testing.T) { 163 layout := missionLayout{template: identity, width: 32, depth: 32, floorHeight: 10, 164 rooms: []missionRoom{{template: uint16(index), x: 1, z: 1}}} 165 166 scene, err := geometry.missionScene(layout) 167 168 if err != nil { 169 t.Fatal(err) 170 } 171 if scene.rooms[0].surface.primary == nil || scene.rooms[0].position[1] != room.position[1] { 172 t.Fatal("original mesh or authored room elevation was lost") 173 } 174 }) 175 } 176 } 177 } 178 179 func TestMissionScenePlacesTheNativeTwoRoomLayout(t *testing.T) { 180 geometry := missionGeometryFixture(t) 181 layout := missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10, 182 rooms: []missionRoom{{template: 0, x: 1, z: 3, rotation: 2}, {template: 37, x: 1, z: 1}}} 183 184 scene, err := geometry.missionScene(layout) 185 186 if err != nil { 187 t.Fatal(err) 188 } 189 if scene.rooms[0].position != [3]float32{14, 0, 20} || scene.rooms[1].position != [3]float32{20, 0, 40} { 190 t.Fatal("native generated-room anchors", scene.rooms[0].position, scene.rooms[1].position) 191 } 192 if scene.rooms[0].template.position != [3]float32{24, 0, 368} || scene.rooms[1].template.position != [3]float32{212, 0, 178} { 193 t.Fatal("template-world surface origins must survive placement") 194 } 195 if scene.rooms[0].surface != geometry.surface(331<<16) || scene.rooms[1].surface != geometry.surface(331<<16|37) { 196 t.Fatal("generated rooms must share original immutable surface trees") 197 } 198 } 199 200 func TestMissionScenePlacesEveryQuarterTurnAndNormalizesFloors(t *testing.T) { 201 geometry := missionGeometryFixture(t) 202 for _, scenario := range []struct { 203 rotation byte 204 position [3]float32 205 point [3]float32 206 }{ 207 {0, [3]float32{16, 0, 20}, [3]float32{17, 2, 23}}, 208 {1, [3]float32{20, 0, 24}, [3]float32{23, 2, 23}}, 209 {2, [3]float32{14, 0, 20}, [3]float32{13, 2, 17}}, 210 {3, [3]float32{20, 0, 26}, [3]float32{17, 2, 27}}, 211 } { 212 t.Run(fmt.Sprint(scenario.rotation), func(t *testing.T) { 213 layout := missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10, 214 rooms: []missionRoom{{template: 0, x: 1, z: 3, rotation: scenario.rotation, floor: -2}, {template: 37, x: 1, z: 1, floor: 1}}} 215 216 scene, err := geometry.missionScene(layout) 217 218 if err != nil { 219 t.Fatal(err) 220 } 221 if scene.rooms[0].position != scenario.position || scene.rooms[1].position != [3]float32{20, 30, 40} { 222 t.Fatal("quarter-turn extent or normalized signed floor", scene.rooms[0].position, scene.rooms[1].position) 223 } 224 if point := scene.rooms[0].globalPoint([3]float32{25, 2, 371}); point != scenario.point { 225 t.Fatal("template to global point", point) 226 } 227 if point := scene.rooms[0].templatePoint(scenario.point); point != [3]float32{25, 2, 371} { 228 t.Fatal("global to template point", point) 229 } 230 }) 231 } 232 } 233 234 func TestMissionSceneFindsWalkableCellsAndVerticalBounds(t *testing.T) { 235 geometry := missionGeometryFixture(t) 236 scene, err := geometry.missionScene(missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10, 237 rooms: []missionRoom{{template: 0, x: 1, z: 3, rotation: 2}, {template: 37, x: 1, z: 1}}}) 238 if err != nil { 239 t.Fatal(err) 240 } 241 for _, scenario := range []struct { 242 name string 243 point [3]float32 244 room int 245 }{ 246 {"entrance", [3]float32{15, 1, 16}, 0}, 247 {"chamber", [3]float32{15, 1, 35}, 1}, 248 {"empty corner", [3]float32{10.5, 1, 10.5}, -1}, 249 {"outside building", [3]float32{0, 1, 0}, -1}, 250 {"below floor", [3]float32{15, -.501, 35}, -1}, 251 {"lower vertical boundary", [3]float32{15, -.5, 35}, 1}, 252 {"above chamber ceiling", [3]float32{15, 5, 35}, -1}, 253 {"upper vertical boundary", [3]float32{15, 4.5, 35}, 1}, 254 } { 255 t.Run(scenario.name, func(t *testing.T) { 256 room := scene.roomAt(scenario.point) 257 258 if scenario.room == -1 { 259 if room != nil { 260 t.Fatal("nonwalkable or vertically excluded point admitted", scenario.point) 261 } 262 } else if room != &scene.rooms[scenario.room] { 263 t.Fatal("wrong containing room", scenario.point) 264 } 265 }) 266 } 267 } 268 269 func TestMissionSceneRejectsUnavailableOrIncompleteRoomAssignments(t *testing.T) { 270 for _, scenario := range []struct { 271 name string 272 change func(*missionLayout, *sceneGeometry) 273 }{ 274 {"missing library", func(layout *missionLayout, geometry *sceneGeometry) { layout.template = 65535 }}, 275 {"public map", func(layout *missionLayout, geometry *sceneGeometry) { layout.template = 705 }}, 276 {"surface namespace overflow", func(layout *missionLayout, geometry *sceneGeometry) { layout.template = 65536 }}, 277 {"unknown room", func(layout *missionLayout, geometry *sceneGeometry) { layout.rooms[0].template = 42 }}, 278 {"outside width", func(layout *missionLayout, geometry *sceneGeometry) { layout.width = 1 }}, 279 {"outside depth", func(layout *missionLayout, geometry *sceneGeometry) { layout.depth = 3 }}, 280 {"unsupported water", func(layout *missionLayout, geometry *sceneGeometry) { 281 layout.template = 341 282 layout.rooms[0].template = 21 283 }}, 284 {"missing surface", func(layout *missionLayout, geometry *sceneGeometry) { 285 geometry.surfaces.values.Store(uint32(331<<16), func() *sceneSurface { return nil }) 286 }}, 287 } { 288 t.Run(scenario.name, func(t *testing.T) { 289 geometry := missionGeometryFixture(t) 290 layout := missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10, rooms: []missionRoom{{x: 1, z: 3}}} 291 scenario.change(&layout, geometry) 292 293 scene, err := geometry.missionScene(layout) 294 295 if err == nil || scene != nil { 296 t.Fatal("invalid assignment produced a usable private scene", err) 297 } 298 }) 299 } 300 } 301 302 func TestMissionSceneUsesBothTemplateAndPlacedOrientation(t *testing.T) { 303 room := placedRoom{position: [3]float32{14, 30, 20}, rotation: 3, 304 template: &dungeonRoom{position: [3]float32{100, 2, 200}, rotation: 1}} 305 306 global := room.globalPoint([3]float32{103, 4, 205}) 307 template := room.templatePoint([3]float32{11, 32, 15}) 308 309 if global != [3]float32{11, 32, 15} || template != [3]float32{103, 4, 205} { 310 t.Fatal("relative template rotation or vertical translation", global, template) 311 } 312 } 313 314 func TestDungeonLibraryRejectsMalformedRoomRecords(t *testing.T) { 315 geometry := missionGeometryFixture(t) 316 original, _, err := geometry.database.read(1000001, 331) 317 if err != nil { 318 t.Fatal(err) 319 } 320 ground, _, err := geometry.database.read(1000009, 330) 321 if err != nil { 322 t.Fatal(err) 323 } 324 for _, scenario := range []struct { 325 name string 326 change func([]byte) []byte 327 }{ 328 {"short header", func(data []byte) []byte { return data[:51] }}, 329 {"version", func(data []byte) []byte { data[0] = 9; return data }}, 330 {"identity", func(data []byte) []byte { data[4]++; return data }}, 331 {"public map", func(data []byte) []byte { binary.LittleEndian.PutUint32(data[40:44], 331); return data }}, 332 {"room count", func(data []byte) []byte { binary.LittleEndian.PutUint32(data[48:52], math.MaxUint32); return data }}, 333 {"door count", func(data []byte) []byte { binary.LittleEndian.PutUint16(data[74:76], math.MaxUint16); return data }}, 334 {"lightmap length", func(data []byte) []byte { binary.LittleEndian.PutUint32(data[116:120], 3); return data }}, 335 {"truncated lightmap", func(data []byte) []byte { binary.LittleEndian.PutUint32(data[116:120], math.MaxUint32); return data }}, 336 {"water list", func(data []byte) []byte { binary.LittleEndian.PutUint32(data[653:657], 1008); return data }}, 337 {"camera list", func(data []byte) []byte { binary.LittleEndian.PutUint32(data[657:661], math.MaxUint32); return data }}, 338 {"bounds", func(data []byte) []byte { clear(data[58:60]); return data }}, 339 {"position", func(data []byte) []byte { 340 binary.LittleEndian.PutUint32(data[62:66], math.Float32bits(float32(math.NaN()))) 341 return data 342 }}, 343 } { 344 t.Run(scenario.name, func(t *testing.T) { 345 file, err := os.CreateTemp(t.TempDir(), "rooms") 346 if err != nil { 347 t.Fatal(err) 348 } 349 t.Cleanup(func() { file.Close() }) 350 database := &gameDatabase{file: file, positions: make(map[[2]uint32]uint32)} 351 for _, record := range []struct { 352 identity [2]uint32 353 data []byte 354 }{{[2]uint32{1000001, 331}, scenario.change(bytes.Clone(original))}, {[2]uint32{1000009, 330}, ground}} { 355 var header [34]byte 356 binary.LittleEndian.PutUint32(header[10:14], record.identity[0]) 357 binary.LittleEndian.PutUint32(header[14:18], record.identity[1]) 358 binary.LittleEndian.PutUint32(header[18:22], uint32(len(record.data)+12)) 359 database.positions[record.identity] = uint32(database.size) 360 if _, err := file.Write(header[:]); err != nil { 361 t.Fatal(err) 362 } 363 if _, err := file.Write(record.data); err != nil { 364 t.Fatal(err) 365 } 366 database.size += int64(34 + len(record.data)) 367 } 368 broken := &sceneGeometry{database: database} 369 370 library, err := broken.dungeonLibrary(331) 371 372 if err == nil || library != nil { 373 t.Fatal("malformed archive produced usable room geometry", err) 374 } 375 }) 376 } 377 } 378 379 func TestMissionScenesShareMeshesWithoutSharingPlacement(t *testing.T) { 380 geometry := missionGeometryFixture(t) 381 var workers sync.WaitGroup 382 results := make(chan bool, 8) 383 384 for level := range int8(8) { 385 workers.Add(1) 386 go func() { 387 defer workers.Done() 388 scene, err := geometry.missionScene(missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10, 389 rooms: []missionRoom{{x: 1, z: 3, rotation: 2}, {template: 37, x: 1, z: 1, floor: level}}}) 390 if err != nil { 391 results <- false 392 return 393 } 394 var point, normal [3]float32 395 scene.floor([3]float32{15, float32(level)*10 + 1, 35}, &point, &normal) 396 wall := scene.line([3]float32{15, 1, 16}, [3]float32{20, 1, 16}) 397 results <- point == [3]float32{15, max(.001, float32(level)*10), 35} && normal == [3]float32{0, 1, 0} && wall.hit && scene.rooms[1].position[1] == float32(level)*10 && 398 scene.rooms[0].surface == geometry.surface(331<<16) && scene.rooms[1].surface == geometry.surface(331<<16|37) 399 }() 400 } 401 workers.Wait() 402 close(results) 403 404 for passed := range results { 405 if !passed { 406 t.Fatal("concurrent scene changed another scene's floor placement") 407 } 408 } 409 } 410 411 func TestMissionSceneSelectsStackedRoomsByHeight(t *testing.T) { 412 geometry := missionGeometryFixture(t) 413 scene, err := geometry.missionScene(missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10, 414 rooms: []missionRoom{{template: 37, x: 1, z: 1}, {template: 37, x: 1, z: 1, floor: 1}}}) 415 if err != nil { 416 t.Fatal(err) 417 } 418 var point, normal [3]float32 419 420 lower := scene.roomAt([3]float32{15, 1, 35}) 421 upper := scene.roomAt([3]float32{15, 11, 35}) 422 between := scene.roomAt([3]float32{15, 6, 35}) 423 scene.floor([3]float32{15, 11, 35}, &point, &normal) 424 425 if lower != &scene.rooms[0] || upper != &scene.rooms[1] || between != nil { 426 t.Fatal("overlapping footprints must use the cell's vertical room bounds") 427 } 428 if point != [3]float32{15, 10, 35} || normal != [3]float32{0, 1, 0} { 429 t.Fatal("upper room floor must preserve its placed elevation", point, normal) 430 } 431 if scene.rooms[0].surface != scene.rooms[1].surface { 432 t.Fatal("stacked copies must share the original mesh") 433 } 434 } 435 436 func TestMissionSceneAppliesNativeUnavailableHeightNormalization(t *testing.T) { 437 geometry := missionGeometryFixture(t) 438 439 scene, err := geometry.missionScene(missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10, 440 rooms: []missionRoom{{x: 1, z: 3, rotation: 2}, {template: 37, x: 1, z: 1}}}) 441 if err != nil { 442 t.Fatal(err) 443 } 444 wall := scene.roomAt([3]float32{11.9, .8, 20}) 445 room := scene.roomAt([3]float32{15, 1, 16}) 446 library, err := geometry.dungeonLibrary(331) 447 448 if err != nil { 449 t.Fatal(err) 450 } 451 if !slices.Equal(scene.ground.tilesetHeights, []float32{0, 0, 6, 4, 5.9}) { 452 t.Fatal("native generated-world heights", scene.ground.tilesetHeights) 453 } 454 if wall != nil || room != &scene.rooms[0] { 455 t.Fatal("unavailable wall height must not admit an ordinary standing character") 456 } 457 if library.ground.tilesetHeights[0] != 6553.5 || library.ground.tilesetHeights[1] != 6553.5 { 458 t.Fatal("scene normalization must not change the archived tilemap decoder") 459 } 460 } 461 462 func TestMissionScenePreservesOrdinaryHeightsAtEveryTilesetIndex(t *testing.T) { 463 geometry := missionGeometryFixture(t) 464 465 scene, err := geometry.missionScene(missionLayout{template: 341, width: 8, depth: 8, floorHeight: 10, rooms: []missionRoom{{x: 1, z: 1}}}) 466 467 if err != nil { 468 t.Fatal(err) 469 } 470 if !slices.Equal(scene.ground.tilesetHeights, []float32{4, 4, 8, 16}) { 471 t.Fatal("ordinary heights, including indices zero and one, must survive normalization", scene.ground.tilesetHeights) 472 } 473 }