merge ws/build
This commit is contained in:
commit
9ae847d2a3
22 files changed
+6770
-1123
No files matched your search
@@ -46,12 +46,13 @@ print(F.detect(webp), #F.formats()) -- "webp", the format list
|
||||
## Formats
|
||||
|
||||
<!-- formats:begin -->
|
||||
174 formats. Generated from the format contracts by `tools/gen_registry.py`; [docs/FORMATS.md](docs/FORMATS.md)
|
||||
176 formats. Generated from the format contracts by `tools/gen_registry.py`; [docs/FORMATS.md](docs/FORMATS.md)
|
||||
adds each format's capabilities.
|
||||
|
||||
| Category | Format | Extensions | Read | Write |
|
||||
|---|---|---|---|---|
|
||||
| Images | ani | .ani | yes | yes |
|
||||
| Images | art | .art | yes | yes |
|
||||
| Images | blp | .blp | yes | yes |
|
||||
| Images | bmp | .bmp .dib | yes | yes |
|
||||
| Images | dds | .dds | yes | yes |
|
||||
@@ -112,6 +113,7 @@ adds each format's capabilities.
|
||||
| 3D models | 3mf | .3mf | yes | yes |
|
||||
| 3D models | b3d | .b3d | yes | - |
|
||||
| 3D models | bsp | .bsp | yes | - |
|
||||
| 3D models | buildmap | .map | yes | yes |
|
||||
| 3D models | dae | .dae .zae | yes | yes |
|
||||
| 3D models | fbx | .fbx | yes | yes |
|
||||
| 3D models | gltf | .gltf .glb | yes | yes |
|
||||
|
||||
+1187
File diff suppressed because it is too large.
Load diff
+538
-535
File diff suppressed because it is too large.
Load diff
Binary file not shown.
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"Core_1": [[8, "src/Rosetta/init.luau"], [852, "src/Rosetta/Registry.luau"], [2961, "src/Rosetta/Types.luau"], [4044, "src/Rosetta/codecs/audio/Adpcm/init.luau"], [4798, "src/Rosetta/codecs/image/Yuv.luau"], [5066, "src/Rosetta/codecs/model/Rbx/init.luau"], [5303, "src/Rosetta/codecs/video/Video/init.luau"], [5860, "src/Rosetta/containers/AudioWrap.luau"], [6363, "src/Rosetta/containers/Ogg/Mux.luau"], [6558, "src/Rosetta/containers/Ogg/Page.luau"], [6727, "src/Rosetta/core/Bytes.luau"], [7254, "src/Rosetta/core/Crc32.luau"], [7366, "src/Rosetta/core/Err.luau"], [7448, "src/Rosetta/core/Input.luau"], [7524, "src/Rosetta/host/init.luau"]],
|
||||
"Core_1": [[8, "src/Rosetta/init.luau"], [852, "src/Rosetta/Registry.luau"], [2985, "src/Rosetta/Types.luau"], [4068, "src/Rosetta/codecs/audio/Adpcm/init.luau"], [4822, "src/Rosetta/codecs/image/Yuv.luau"], [5090, "src/Rosetta/codecs/model/Rbx/init.luau"], [5327, "src/Rosetta/codecs/video/Video/init.luau"], [5884, "src/Rosetta/containers/AudioWrap.luau"], [6387, "src/Rosetta/containers/Ogg/Mux.luau"], [6582, "src/Rosetta/containers/Ogg/Page.luau"], [6751, "src/Rosetta/core/Bytes.luau"], [7278, "src/Rosetta/core/Crc32.luau"], [7390, "src/Rosetta/core/Err.luau"], [7472, "src/Rosetta/core/Input.luau"], [7548, "src/Rosetta/host/init.luau"]],
|
||||
"Core_2": [[8, "src/Rosetta/ops/ImageOps/init.luau"], [652, "src/Rosetta/ops/ImageOps/Kernels.luau"]],
|
||||
"Mpeg2": [[8, "src/Rosetta/codecs/video/Dsp.luau"], [731, "src/Rosetta/codecs/video/Mpeg1/init.luau"], [743, "src/Rosetta/codecs/video/Mpeg2/init.luau"], [1110, "src/Rosetta/codecs/video/Mpeg2/Decoder.luau"], [2617, "src/Rosetta/codecs/video/Mpeg2/Tables.luau"], [2756, "src/Rosetta/codecs/video/Mpeg2/Vlc.luau"]],
|
||||
"H263": [[8, "src/Rosetta/codecs/video/H263/init.luau"], [164, "src/Rosetta/codecs/video/H263/Decoder.luau"], [1022, "src/Rosetta/codecs/video/H263/Tables.luau"]],
|
||||
@@ -44,12 +44,12 @@
|
||||
"data": [[8, "src/Rosetta/data/cbor/init.luau"], [107, "src/Rosetta/data/cbor/Codec.luau"], [676, "src/Rosetta/data/csv/init.luau"], [988, "src/Rosetta/data/ini/init.luau"], [1234, "src/Rosetta/data/json/init.luau"], [1326, "src/Rosetta/data/msgpack/init.luau"], [1415, "src/Rosetta/data/msgpack/Codec.luau"], [1857, "src/Rosetta/data/nbt/init.luau"], [1916, "src/Rosetta/data/plist/init.luau"], [2222, "src/Rosetta/data/plist/Binary.luau"], [2489, "src/Rosetta/data/snbt/init.luau"], [2552, "src/Rosetta/data/toml/init.luau"], [2619, "src/Rosetta/data/toml/Decode.luau"], [3247, "src/Rosetta/data/toml/Encode.luau"], [3456, "src/Rosetta/data/xml/init.luau"], [3512, "src/Rosetta/data/yaml/init.luau"], [3585, "src/Rosetta/data/yaml/Emit.luau"], [3875, "src/Rosetta/data/yaml/Parse.luau"]],
|
||||
"font": [[8, "src/Rosetta/font/bdf/init.luau"], [297, "src/Rosetta/font/bmfont/init.luau"], [724, "src/Rosetta/font/bmfont/Parse.luau"], [1177, "src/Rosetta/font/eot/init.luau"], [1409, "src/Rosetta/font/fon/init.luau"], [1692, "src/Rosetta/font/hershey/init.luau"], [1887, "src/Rosetta/font/pcf/init.luau"], [2167, "src/Rosetta/font/psf/init.luau"], [2487, "src/Rosetta/font/ttf/init.luau"], [2538, "src/Rosetta/font/type1/init.luau"], [2897, "src/Rosetta/font/type1/Charstring.luau"], [3175, "src/Rosetta/font/woff/init.luau"], [3307, "src/Rosetta/font/woff2/init.luau"], [3364, "src/Rosetta/font/woff2/Rebuild.luau"]],
|
||||
"host": [[8, "src/Rosetta/host/Love.luau"], [151, "src/Rosetta/host/LuaJIT.luau"], [201, "src/Rosetta/host/Luau.luau"], [224, "src/Rosetta/host/Roblox.luau"]],
|
||||
"image_1": [[8, "src/Rosetta/image/ani/init.luau"], [188, "src/Rosetta/image/blp/init.luau"], [446, "src/Rosetta/image/bmp/init.luau"], [861, "src/Rosetta/image/dds/init.luau"], [1344, "src/Rosetta/image/exr/init.luau"], [2104, "src/Rosetta/image/exr/Codec.luau"], [2215, "src/Rosetta/image/exr/Piz.luau"], [2572, "src/Rosetta/image/gif/init.luau"], [3088, "src/Rosetta/image/gif/Decode.luau"], [3151, "src/Rosetta/image/hdr/init.luau"], [3380, "src/Rosetta/image/hdr/Codec.luau"], [3591, "src/Rosetta/image/icns/init.luau"], [3922, "src/Rosetta/image/ico/init.luau"], [4199, "src/Rosetta/image/ico/Dib.luau"], [4294, "src/Rosetta/image/ilbm/init.luau"], [4613, "src/Rosetta/image/jpeg/init.luau"], [4749, "src/Rosetta/image/ktx/init.luau"], [5409, "src/Rosetta/image/pcx/init.luau"], [5673, "src/Rosetta/image/pnm/init.luau"], [6051, "src/Rosetta/image/pnm/Codec.luau"], [6328, "src/Rosetta/image/qoi/init.luau"], [6450, "src/Rosetta/image/qoi/Codec.luau"], [6600, "src/Rosetta/image/sgi/init.luau"], [6885, "src/Rosetta/image/sunras/init.luau"], [7131, "src/Rosetta/image/tga/init.luau"], [7401, "src/Rosetta/image/tga/Codec.luau"]],
|
||||
"image_2": [[8, "src/Rosetta/image/tiff/init.luau"], [835, "src/Rosetta/image/tiff/Pixels.luau"], [1225, "src/Rosetta/image/tim/init.luau"], [1467, "src/Rosetta/image/vtf/init.luau"], [1702, "src/Rosetta/image/webp/init.luau"], [2312, "src/Rosetta/image/webp/Alpha.luau"], [2388, "src/Rosetta/image/webp/Anim.luau"], [2543, "src/Rosetta/image/xbm/init.luau"], [2704, "src/Rosetta/image/xpm/init.luau"], [2972, "src/Rosetta/image/xpm/Names.luau"]],
|
||||
"image_1": [[8, "src/Rosetta/image/ani/init.luau"], [188, "src/Rosetta/image/art/init.luau"], [676, "src/Rosetta/image/blp/init.luau"], [934, "src/Rosetta/image/bmp/init.luau"], [1349, "src/Rosetta/image/dds/init.luau"], [1832, "src/Rosetta/image/exr/init.luau"], [2592, "src/Rosetta/image/exr/Codec.luau"], [2703, "src/Rosetta/image/exr/Piz.luau"], [3060, "src/Rosetta/image/gif/init.luau"], [3576, "src/Rosetta/image/gif/Decode.luau"], [3639, "src/Rosetta/image/hdr/init.luau"], [3868, "src/Rosetta/image/hdr/Codec.luau"], [4079, "src/Rosetta/image/icns/init.luau"], [4410, "src/Rosetta/image/ico/init.luau"], [4687, "src/Rosetta/image/ico/Dib.luau"], [4782, "src/Rosetta/image/ilbm/init.luau"], [5101, "src/Rosetta/image/jpeg/init.luau"], [5237, "src/Rosetta/image/ktx/init.luau"], [5897, "src/Rosetta/image/pcx/init.luau"], [6161, "src/Rosetta/image/pnm/init.luau"], [6539, "src/Rosetta/image/pnm/Codec.luau"], [6816, "src/Rosetta/image/qoi/init.luau"], [6938, "src/Rosetta/image/qoi/Codec.luau"], [7088, "src/Rosetta/image/sgi/init.luau"], [7373, "src/Rosetta/image/sunras/init.luau"], [7619, "src/Rosetta/image/tga/init.luau"]],
|
||||
"image_2": [[8, "src/Rosetta/image/tga/Codec.luau"], [160, "src/Rosetta/image/tiff/init.luau"], [987, "src/Rosetta/image/tiff/Pixels.luau"], [1377, "src/Rosetta/image/tim/init.luau"], [1619, "src/Rosetta/image/vtf/init.luau"], [1854, "src/Rosetta/image/webp/init.luau"], [2464, "src/Rosetta/image/webp/Alpha.luau"], [2540, "src/Rosetta/image/webp/Anim.luau"], [2695, "src/Rosetta/image/xbm/init.luau"], [2856, "src/Rosetta/image/xpm/init.luau"], [3124, "src/Rosetta/image/xpm/Names.luau"]],
|
||||
"layered": [[8, "src/Rosetta/layered/aseprite/init.luau"], [746, "src/Rosetta/layered/ora/init.luau"], [1033, "src/Rosetta/layered/psd/init.luau"], [1768, "src/Rosetta/layered/psd/Channels.luau"]],
|
||||
"model_1": [[8, "src/Rosetta/model/3ds/init.luau"], [91, "src/Rosetta/model/3ds/Build.luau"], [738, "src/Rosetta/model/3ds/Read.luau"], [1234, "src/Rosetta/model/3ds/Write.luau"], [2039, "src/Rosetta/model/3mf/init.luau"], [2121, "src/Rosetta/model/3mf/Read.luau"], [2709, "src/Rosetta/model/3mf/Write.luau"], [3114, "src/Rosetta/model/b3d/init.luau"], [3169, "src/Rosetta/model/b3d/Read.luau"], [3740, "src/Rosetta/model/bsp/init.luau"], [3885, "src/Rosetta/model/bsp/Brush.luau"], [4163, "src/Rosetta/model/bsp/Quake3.luau"], [4418, "src/Rosetta/model/dae/init.luau"], [4607, "src/Rosetta/model/dae/Math.luau"], [4654, "src/Rosetta/model/dae/Read.luau"], [6504, "src/Rosetta/model/dae/Write.luau"], [6873, "src/Rosetta/model/fbx/init.luau"], [7021, "src/Rosetta/model/fbx/Ascii.luau"], [7291, "src/Rosetta/model/fbx/Binary.luau"]],
|
||||
"model_2": [[8, "src/Rosetta/model/fbx/Read.luau"], [1370, "src/Rosetta/model/fbx/Write.luau"], [2355, "src/Rosetta/model/fbx/Xform.luau"], [2396, "src/Rosetta/model/gltf/init.luau"], [2633, "src/Rosetta/model/gltf/Pack.luau"], [2702, "src/Rosetta/model/gltf/Read.luau"], [3433, "src/Rosetta/model/gltf/Write.luau"], [4113, "src/Rosetta/model/iqm/init.luau"], [4189, "src/Rosetta/model/iqm/Read.luau"], [4544, "src/Rosetta/model/iqm/Write.luau"], [5231, "src/Rosetta/model/ldraw/init.luau"], [5309, "src/Rosetta/model/ldraw/Colors.luau"], [5627, "src/Rosetta/model/ldraw/Read.luau"], [6204, "src/Rosetta/model/lwo/init.luau"], [6280, "src/Rosetta/model/lwo/Build.luau"], [6742, "src/Rosetta/model/lwo/Parse.luau"]],
|
||||
"model_3": [[8, "src/Rosetta/model/mca/init.luau"], [350, "src/Rosetta/model/mca/Chunk.luau"], [483, "src/Rosetta/model/mca/Lz4Block.luau"], [553, "src/Rosetta/model/md2/init.luau"], [752, "src/Rosetta/model/md3/init.luau"], [980, "src/Rosetta/model/md5/init.luau"], [1061, "src/Rosetta/model/md5/Read.luau"], [1494, "src/Rosetta/model/mdl/init.luau"], [1576, "src/Rosetta/model/mdl/HalfLife.luau"], [1904, "src/Rosetta/model/mdl/Quake.luau"], [2122, "src/Rosetta/model/ms3d/init.luau"], [2194, "src/Rosetta/model/ms3d/Read.luau"], [2640, "src/Rosetta/model/ms3d/Write.luau"], [3109, "src/Rosetta/model/obj/init.luau"], [3851, "src/Rosetta/model/obj/Mtl.luau"], [4138, "src/Rosetta/model/off/init.luau"], [4600, "src/Rosetta/model/ply/init.luau"], [5705, "src/Rosetta/model/ply/Binary.luau"], [5906, "src/Rosetta/model/pmx/init.luau"], [5975, "src/Rosetta/model/pmx/Build.luau"], [6271, "src/Rosetta/model/pmx/Pmd.luau"], [6606, "src/Rosetta/model/pmx/Read.luau"], [7010, "src/Rosetta/model/qb/init.luau"]],
|
||||
"model_1": [[8, "src/Rosetta/model/3ds/init.luau"], [91, "src/Rosetta/model/3ds/Build.luau"], [738, "src/Rosetta/model/3ds/Read.luau"], [1234, "src/Rosetta/model/3ds/Write.luau"], [2039, "src/Rosetta/model/3mf/init.luau"], [2121, "src/Rosetta/model/3mf/Read.luau"], [2709, "src/Rosetta/model/3mf/Write.luau"], [3114, "src/Rosetta/model/b3d/init.luau"], [3169, "src/Rosetta/model/b3d/Read.luau"], [3740, "src/Rosetta/model/bsp/init.luau"], [3885, "src/Rosetta/model/bsp/Brush.luau"], [4163, "src/Rosetta/model/bsp/Quake3.luau"], [4418, "src/Rosetta/model/buildmap/init.luau"], [4690, "src/Rosetta/model/buildmap/Geometry.luau"], [5093, "src/Rosetta/model/dae/init.luau"], [5282, "src/Rosetta/model/dae/Math.luau"], [5329, "src/Rosetta/model/dae/Read.luau"], [7179, "src/Rosetta/model/dae/Write.luau"], [7548, "src/Rosetta/model/fbx/init.luau"]],
|
||||
"model_2": [[8, "src/Rosetta/model/fbx/Ascii.luau"], [278, "src/Rosetta/model/fbx/Binary.luau"], [733, "src/Rosetta/model/fbx/Read.luau"], [2095, "src/Rosetta/model/fbx/Write.luau"], [3080, "src/Rosetta/model/fbx/Xform.luau"], [3121, "src/Rosetta/model/gltf/init.luau"], [3358, "src/Rosetta/model/gltf/Pack.luau"], [3427, "src/Rosetta/model/gltf/Read.luau"], [4158, "src/Rosetta/model/gltf/Write.luau"], [4838, "src/Rosetta/model/iqm/init.luau"], [4914, "src/Rosetta/model/iqm/Read.luau"], [5269, "src/Rosetta/model/iqm/Write.luau"], [5956, "src/Rosetta/model/ldraw/init.luau"], [6034, "src/Rosetta/model/ldraw/Colors.luau"], [6352, "src/Rosetta/model/ldraw/Read.luau"], [6929, "src/Rosetta/model/lwo/init.luau"], [7005, "src/Rosetta/model/lwo/Build.luau"]],
|
||||
"model_3": [[8, "src/Rosetta/model/lwo/Parse.luau"], [551, "src/Rosetta/model/mca/init.luau"], [893, "src/Rosetta/model/mca/Chunk.luau"], [1026, "src/Rosetta/model/mca/Lz4Block.luau"], [1096, "src/Rosetta/model/md2/init.luau"], [1295, "src/Rosetta/model/md3/init.luau"], [1523, "src/Rosetta/model/md5/init.luau"], [1604, "src/Rosetta/model/md5/Read.luau"], [2037, "src/Rosetta/model/mdl/init.luau"], [2119, "src/Rosetta/model/mdl/HalfLife.luau"], [2447, "src/Rosetta/model/mdl/Quake.luau"], [2665, "src/Rosetta/model/ms3d/init.luau"], [2737, "src/Rosetta/model/ms3d/Read.luau"], [3183, "src/Rosetta/model/ms3d/Write.luau"], [3652, "src/Rosetta/model/obj/init.luau"], [4394, "src/Rosetta/model/obj/Mtl.luau"], [4681, "src/Rosetta/model/off/init.luau"], [5143, "src/Rosetta/model/ply/init.luau"], [6248, "src/Rosetta/model/ply/Binary.luau"], [6449, "src/Rosetta/model/pmx/init.luau"], [6518, "src/Rosetta/model/pmx/Build.luau"], [6814, "src/Rosetta/model/pmx/Pmd.luau"], [7149, "src/Rosetta/model/pmx/Read.luau"], [7553, "src/Rosetta/model/qb/init.luau"]],
|
||||
"model_4": [[8, "src/Rosetta/model/rbxm/init.luau"], [600, "src/Rosetta/model/rbxmx/init.luau"], [1253, "src/Rosetta/model/robloxmesh/init.luau"], [1912, "src/Rosetta/model/robloxmesh/Parse.luau"], [2369, "src/Rosetta/model/robloxmesh/Write.luau"], [2478, "src/Rosetta/model/schematic/init.luau"], [2799, "src/Rosetta/model/schematic/Common.luau"], [2919, "src/Rosetta/model/schematic/Litematic.luau"], [3130, "src/Rosetta/model/schematic/Mcedit.luau"], [3374, "src/Rosetta/model/schematic/Sponge.luau"], [3503, "src/Rosetta/model/smd/init.luau"], [3570, "src/Rosetta/model/smd/Read.luau"], [3839, "src/Rosetta/model/smd/Write.luau"], [4082, "src/Rosetta/model/stl/init.luau"], [4532, "src/Rosetta/model/stl/Decode.luau"], [4606, "src/Rosetta/model/usda/init.luau"], [4815, "src/Rosetta/model/usda/Build.luau"], [6093, "src/Rosetta/model/usda/Parse.luau"], [6891, "src/Rosetta/model/usda/Write.luau"]],
|
||||
"model_5": [[8, "src/Rosetta/model/vox/init.luau"], [894, "src/Rosetta/model/vox/Grid.luau"], [951, "src/Rosetta/model/vrml/init.luau"], [1017, "src/Rosetta/model/x/init.luau"], [1147, "src/Rosetta/model/x/Build.luau"], [1710, "src/Rosetta/model/x/Parse.luau"], [2007, "src/Rosetta/model/x3d/init.luau"]],
|
||||
"music": [[8, "src/Rosetta/music/669/init.luau"], [214, "src/Rosetta/music/ahx/init.luau"], [265, "src/Rosetta/music/far/init.luau"], [494, "src/Rosetta/music/hmp/init.luau"], [758, "src/Rosetta/music/hvl/init.luau"], [809, "src/Rosetta/music/it/init.luau"], [1227, "src/Rosetta/music/it/Itsex.luau"], [1343, "src/Rosetta/music/med/init.luau"], [1990, "src/Rosetta/music/midi/init.luau"], [2133, "src/Rosetta/music/midi/Smf.luau"], [2544, "src/Rosetta/music/mod/init.luau"], [2897, "src/Rosetta/music/mtm/init.luau"], [3119, "src/Rosetta/music/mus/init.luau"], [3276, "src/Rosetta/music/okt/init.luau"], [3557, "src/Rosetta/music/ptm/init.luau"], [3779, "src/Rosetta/music/s3m/init.luau"], [4015, "src/Rosetta/music/sf2/init.luau"], [4058, "src/Rosetta/music/stm/init.luau"], [4289, "src/Rosetta/music/ult/init.luau"], [4593, "src/Rosetta/music/umx/init.luau"], [4809, "src/Rosetta/music/xm/init.luau"], [5370, "src/Rosetta/music/xmi/init.luau"]],
|
||||
|
||||
+1235
-45
File diff suppressed because it is too large.
Load diff
+1187
File diff suppressed because it is too large.
Load diff
+538
-535
File diff suppressed because it is too large.
Load diff
@@ -6,6 +6,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| Category | Format | Extensions | Read | Write | Capabilities | Test |
|
||||
|---|---|---|---|---|---|---|
|
||||
| image | ani | .ani | yes | yes | alpha: true, animation: true, frames: true | `tests/image/ani.luau` |
|
||||
| image | art | .art | yes | yes | alpha: true, palette: true, frames: true, depths: 8 | `tests/image/art.luau` |
|
||||
| image | blp | .blp | yes | yes | alpha: true, variants: "blp1", "blp2", compressions: "jpeg", "palette", "dxt1", "dxt3", "dxt5", "bgra" | `tests/image/blp.luau` |
|
||||
| image | bmp | .bmp .dib | yes | yes | alpha: true, depths: 1, 2, 4, 8, 16, 24, 32, rle: true | `tests/image/bmp.luau` |
|
||||
| image | dds | .dds | yes | yes | alpha: true, float: true, mipmaps: true, cubemap: true, compressed: "bc1", "bc2", "bc3", "bc4", "bc5", "bc6h", "bc7" | `tests/image/dds.luau` |
|
||||
@@ -66,6 +67,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| model | 3mf | .3mf | yes | yes | materials: true, vertexColors: true, textures: true, units: true | `tests/model/3mf.luau` |
|
||||
| model | b3d | .b3d | yes | - | materials: true, textures: true, skins: true, animation: true, colors: true | `tests/model/b3d.luau` |
|
||||
| model | bsp | .bsp | yes | - | variants: "quake", "halflife", "quake2", "quake3", lightmaps: "quake3" | `tests/model/bsp.luau` |
|
||||
| model | buildmap | .map | yes | yes | sprites: true, slopes: true | `tests/model/buildmap.luau` |
|
||||
| model | dae | .dae .zae | yes | yes | materials: true, polygons: true, skins: true, morphTargets: true, animation: true, cameras: true, lights: true, zae: true | `tests/model/dae.luau` |
|
||||
| model | fbx | .fbx | yes | yes | binary: true, ascii: true, materials: true, animation: true, skins: true, morphTargets: true, cameras: true, lights: true, polygons: true | `tests/model/fbx.luau` |
|
||||
| model | gltf | .gltf .glb | yes | yes | binary: true, materials: true, animation: true, skins: true, morphTargets: true, cameras: true, lights: true, draco: true | `tests/model/gltf.luau` |
|
||||
|
||||
+19
-1
@@ -1,7 +1,7 @@
|
||||
# Images
|
||||
|
||||
Every image format (PNG / APNG, JPEG, WebP, GIF, BMP, TGA, QOI, TIFF, ICO / CUR, PNM / PAM / PFM, Radiance HDR,
|
||||
OpenEXR, the [game textures](#game-textures) DDS / KTX / VTF / BLP / TIM, and the [older formats](#formats) PCX, SGI,
|
||||
OpenEXR, the [game textures](#game-textures) DDS / KTX / VTF / BLP / TIM / ART, and the [older formats](#formats) PCX, SGI,
|
||||
Sun Raster, ILBM, XPM, XBM, ICNS, ANI) loads as one `Image`: a width, a height, a pixel format and one tightly packed `buffer`. Animations (GIF, APNG, animated
|
||||
WebP) and multi-image files (TIFF pages, ICO entries) add a list of `Frame`s. Editing goes through `F.ops.image`.
|
||||
|
||||
@@ -214,6 +214,23 @@ local small = assert(F.image.load(vtfBytes, { mip = 1 })) -- or face = n (cub
|
||||
assert(F.detect(tim) == "tim")
|
||||
```
|
||||
|
||||
Build engine ART files (`TILES000.ART` ..., Duke Nukem 3D, Shadow Warrior) hold many tiles: each is one entry of
|
||||
`frames` (in tile order, empty tiles 0 x 0), with its tile number in `meta.tile` and its picanm animation and centre
|
||||
offsets in `meta.picanm`; the top-level image is the first non-empty tile. Colours come from the game's
|
||||
`PALETTE.DAT`, passed as `palette` or found next to the file through `resolve` (without it the tiles decode as a grey
|
||||
ramp). Index 255 is transparent. `shade` / `pal` apply a PALETTE.DAT shade table / a LOOKUP.DAT palookup, `native`
|
||||
keeps the palette indices (gray8), and ART writes back from either form:
|
||||
|
||||
```luau
|
||||
local tiles = assert(F.image.load(artBytes, { palette = paletteBytes }))
|
||||
local second = tiles.frames[2].image
|
||||
assert(second.meta.tile == 1 and second.width == 8)
|
||||
assert(second.meta.picanm.type == "forward" and second.meta.picanm.frames == 2)
|
||||
local raw = assert(F.image.load(artBytes, { format = "art", native = true }))
|
||||
assert(raw.frames[1].image.format == "gray8")
|
||||
assert(buffer.tostring(assert(F.save(raw, nil, { format = "art" }))) == buffer.tostring(artBytes))
|
||||
```
|
||||
|
||||
Doom's graphics come from WAD lumps rather than files: `F.format("wad").image(wad, name)` (see
|
||||
[archive.md](archive.md#game-archives)).
|
||||
|
||||
@@ -233,6 +250,7 @@ Read and write unless noted; see [FORMATS.md](../FORMATS.md) for depths and deta
|
||||
| TIM | | yes | PlayStation; 4 / 8-bit CLUT, 15 / 24-bit |
|
||||
| BLP | | yes | Blizzard BLP1 / BLP2 (DXT through the BCn codec); writes BLP2 BGRA |
|
||||
| VTF | frames | yes | Valve; read only; mips / faces / slices by option |
|
||||
| ART | tiles as frames | index 255 | Build engine tiles; palette from PALETTE.DAT (`palette` / `resolve`), picanm in `meta.picanm` |
|
||||
| DDS, KTX / KTX2 | faces / layers / slices | yes | GPU textures: BC1..BC7, ETC / EAC, every uncompressed layout; mips in `meta.mipmaps` |
|
||||
| OpenEXR | parts (`opts.part`) | yes | half / float / uint, NONE / RLE / ZIPS / ZIP / PIZ, tiles; writes NONE / ZIPS / ZIP |
|
||||
| PCX, SGI, Sun Raster, IFF ILBM / PBM, XPM, XBM | | yes (XBM 1-bit) | old paint / workstation formats; ILBM reads HAM and EHB |
|
||||
|
||||
@@ -507,6 +507,29 @@ local again = assert(F.save(barney, nil, { format = "smd" })) -- SMD writes ba
|
||||
assert(F.model.load(again).skins[1])
|
||||
```
|
||||
|
||||
## Build engine maps
|
||||
|
||||
Duke Nukem 3D / Shadow Warrior `.map` files (versions 7-9; the text Quake `.map` is a different format, told apart by
|
||||
content) load as one mesh: floors and ceilings triangulated with their slopes, walls with upper and lower parts at red
|
||||
walls (and masked walls), a primitive per surface kind and tile (`extras.build = { kind, tile }`); materials and
|
||||
textures are named `tile<n>`. Sprites become nodes under the map with their tile in `extras.build.tile` (the raw
|
||||
sprite in `extras.build.sprite`), and the player start is the node `start`. Coordinates turn z up (Build's y and z
|
||||
point down), z is divided by 16 so all axes share Build's xy unit, and `scale` multiplies everything. `tiles` (tile
|
||||
number to Image, e.g. from an ART file) gives the texture sizes for the UVs and attaches the images.
|
||||
|
||||
The raw sector, wall and sprite tables stay in `meta.build`, and saving writes them back:
|
||||
|
||||
```luau
|
||||
local map = assert(F.model.load(buildmapBytes, { scale = 1 / 512 }))
|
||||
local level = map.meta.build
|
||||
assert(level.version == 7 and #level.sectors == 3)
|
||||
assert(map.nodes[2].name == "start" and map.nodes[3].extras.build.tile == level.sprites[1].picnum)
|
||||
local again = assert(F.save(map, nil, { format = "buildmap" }))
|
||||
assert(buffer.tostring(again) == buffer.tostring(buildmapBytes))
|
||||
local Build = F.format("buildmap") -- the floor height under a point, slopes included
|
||||
assert(Build.slopeZ(level, 0, true, 100, 100) == level.sectors[1].floorz)
|
||||
```
|
||||
|
||||
## Blitz3D (B3D)
|
||||
|
||||
`.b3d` files (Blitz3D, also written by many old game tools) load read-only. The node tree keeps its names and TRS;
|
||||
@@ -745,6 +768,7 @@ assert(back.meshes[1].primitives[1].indices.count == 3)
|
||||
| X3D (XML / classic) | yes | yes | the VRML reader plus the X3D triangle / quad / line sets; no binary `.x3db` |
|
||||
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
|
||||
| MD2, MD3, MDL, MD5, BSP | yes | - | game models and maps: see "Game models" above |
|
||||
| Build engine MAP (v7-9) | yes | yes | sloped sectors, walls, sprites as nodes, raw tables in `meta.build`; see "Build engine maps" above |
|
||||
| SMD | yes | yes | Valve studiomdl data |
|
||||
| Roblox `.mesh` (robloxmesh) | yes | yes | see [roblox.md](roblox.md) |
|
||||
| Roblox `.rbxm` / `.rbxl`, `.rbxmx` / `.rbxlx` | as an `RbxTree` | yes | instance trees with typed properties: see [roblox.md](roblox.md) |
|
||||
|
||||
@@ -26,6 +26,18 @@ local list: { Entry } = {
|
||||
return require("@rosetta/image/ani")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "art",
|
||||
category = "image",
|
||||
path = "image/art",
|
||||
extensions = { "art" },
|
||||
mime = { "image/x-build-art" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Pixels", "codecs/image/Quantize" },
|
||||
caps = { decode = true, encode = true, alpha = true, palette = true, frames = true, depths = { 8 } },
|
||||
load = function()
|
||||
return require("@rosetta/image/art")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "blp",
|
||||
category = "image",
|
||||
@@ -746,6 +758,18 @@ local list: { Entry } = {
|
||||
return require("@rosetta/model/bsp")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "buildmap",
|
||||
category = "model",
|
||||
path = "model/buildmap",
|
||||
extensions = { "map" },
|
||||
mime = { "application/x-build-map" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Polygon" },
|
||||
caps = { decode = true, encode = true, sprites = true, slopes = true },
|
||||
load = function()
|
||||
return require("@rosetta/model/buildmap")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "dae",
|
||||
category = "model",
|
||||
|
||||
@@ -0,0 +1,486 @@
|
||||
--!strict
|
||||
-- image/art: Build engine ART tile files (TILES000.ART ...), from Ken Silverman's BUILDINF.TXT and the eduke32 wiki
|
||||
-- ("ART format"); no code taken. Supports: version 1 with the optional "BUILDART" prefix, every tile as a frame (tile
|
||||
-- order, empty tiles 0 x 0), picanm animation / centre fields, PALETTE.DAT (6-bit VGA, shade tables, translucency
|
||||
-- table) and LOOKUP.DAT palookups through opts / resolve; writes ART from gray8 indices or rgba8 mapped to the palette.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Pixels = require("@rosetta/codecs/image/Pixels")
|
||||
local Quantize = require("@rosetta/codecs/image/Quantize")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Art = {
|
||||
name = "art",
|
||||
category = "image",
|
||||
extensions = { "art" },
|
||||
mime = { "image/x-build-art" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Pixels", "codecs/image/Quantize" },
|
||||
caps = { decode = true, encode = true, alpha = true, palette = true, frames = true, depths = { 8 } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type PaletteSource = buffer | string | { { number } } -- PALETTE.DAT bytes, or 8-bit { r, g, b[, a] } entries
|
||||
export type DecodeOptions = {
|
||||
palette: PaletteSource?, -- default opts.resolve("PALETTE.DAT"); without one, a grey ramp (meta.source.palette = false)
|
||||
lookup: (buffer | string)?, -- LOOKUP.DAT bytes; default opts.resolve("LOOKUP.DAT")
|
||||
shade: number?, -- shade table of PALETTE.DAT applied to the indices (0 = none, default)
|
||||
pal: number?, -- LOOKUP.DAT palookup applied before the shade (0 = none, default)
|
||||
native: boolean?, -- gray8 palette indices instead of rgba8
|
||||
resolve: ((uri: string) -> buffer?)?,
|
||||
maxPixels: number?, -- all tiles together, default 2^26
|
||||
}
|
||||
export type Picanm = {
|
||||
frames: number, -- bits 0-5
|
||||
type: "none" | "oscillate" | "forward" | "backward", -- bits 6-7
|
||||
xoffset: number, -- bits 8-15, signed
|
||||
yoffset: number, -- bits 16-23, signed
|
||||
speed: number, -- bits 24-27: a frame lasts 2^speed ticks of 120 Hz
|
||||
extra: number, -- bits 28-31
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
palette: PaletteSource?, -- for rgba8 frames; default img.meta.palette
|
||||
tileStart: number?, -- default img.meta.source.tileStart, else the first frame's meta.tile, else 0
|
||||
numtiles: number?, -- the legacy header field; default img.meta.source.numtiles, else 0
|
||||
buildart: boolean?, -- write the "BUILDART" prefix (eduke32); default img.meta.source.buildart
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local TYPES = { "none", "oscillate", "forward", "backward" }
|
||||
local TYPE_INDEX = { none = 0, oscillate = 1, forward = 2, backward = 3 }
|
||||
local TRANSPARENT = 255
|
||||
local readu8, readu16, readu32, readi16 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi16
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
local function header(b: buffer, o: number, limit: number): (number?, number?, number?, number?, boolean?)
|
||||
local p = o
|
||||
local prefixed = false
|
||||
if limit - p >= 8 and buffer.readstring(b, p, 8) == "BUILDART" then
|
||||
p += 8
|
||||
prefixed = true
|
||||
end
|
||||
if limit - p < 16 or readu32(b, p) ~= 1 then
|
||||
return nil, nil, nil, nil, nil
|
||||
end
|
||||
local first, last = readu32(b, p + 8), readu32(b, p + 12)
|
||||
if last < first or last - first >= 65536 then
|
||||
return nil, nil, nil, nil, nil
|
||||
end
|
||||
return p + 16, first, last, readu32(b, p + 4), prefixed
|
||||
end
|
||||
|
||||
function Art.sniff(r: Bytes.Reader): number
|
||||
local at, first, last = header(r.buf, r.base, r.limit)
|
||||
if not at or not first or not last then
|
||||
return 0
|
||||
end
|
||||
local n = last - first + 1
|
||||
local p = at :: number
|
||||
if r.limit - p < n * 8 then
|
||||
return 0
|
||||
end
|
||||
local total = 0
|
||||
for i = 0, n - 1 do
|
||||
local w, h = readi16(r.buf, p + i * 2), readi16(r.buf, p + n * 2 + i * 2)
|
||||
if w < 0 or h < 0 then
|
||||
return 0
|
||||
end
|
||||
total += w * h
|
||||
end
|
||||
local size = p + n * 8 + total - r.base
|
||||
local have = r.limit - r.base
|
||||
return if size == have then 0.9 elseif size < have and have - size < 4096 then 0.5 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function bytesOf(v: any): buffer?
|
||||
if type(v) == "buffer" then
|
||||
return v
|
||||
elseif type(v) == "string" then
|
||||
return buffer.fromstring(v)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function fetch(opts: DecodeOptions, key: string, file: string): buffer?
|
||||
local v = bytesOf((opts :: any)[key])
|
||||
if v then
|
||||
return v
|
||||
end
|
||||
local resolve = opts.resolve
|
||||
if type(resolve) == "function" then
|
||||
for _, name in { file, string.lower(file) } do
|
||||
local ok, res = pcall(resolve, name)
|
||||
local b = if ok then bytesOf(res) else nil
|
||||
if b then
|
||||
return b
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- palette as 256 u32 rgba words; PALETTE.DAT stores 6-bit components, scaled << 2 as the engine and arttool do
|
||||
local function makePalette(src: any, dat: buffer?): buffer
|
||||
if type(src) == "table" then
|
||||
local tri = buffer.create(768)
|
||||
for i = 0, 255 do
|
||||
local c = src[i + 1]
|
||||
if type(c) == "table" then
|
||||
for k = 1, 3 do
|
||||
buffer.writeu8(tri, i * 3 + k - 1, math.clamp(math.floor(tonumber(c[k]) or 0), 0, 255))
|
||||
end
|
||||
end
|
||||
end
|
||||
return Pixels.palette(tri, 0, 256)
|
||||
end
|
||||
if dat and buffer.len(dat) >= 768 then
|
||||
local tri = buffer.create(768)
|
||||
for i = 0, 767 do
|
||||
buffer.writeu8(tri, i, bit32.band(readu8(dat, i), 63) * 4)
|
||||
end
|
||||
return Pixels.palette(tri, 0, 256)
|
||||
end
|
||||
local tri = buffer.create(768)
|
||||
for i = 0, 767 do
|
||||
buffer.writeu8(tri, i, i // 3)
|
||||
end
|
||||
return Pixels.palette(tri, 0, 256)
|
||||
end
|
||||
|
||||
local function paletteList(pal: buffer): { { number } }
|
||||
local list = table.create(256) :: { { number } }
|
||||
for i = 0, 255 do
|
||||
local v = readu32(pal, i * 4)
|
||||
list[i + 1] = { bit32.band(v, 255), bit32.band(bit32.rshift(v, 8), 255), bit32.band(bit32.rshift(v, 16), 255), 255 }
|
||||
end
|
||||
return list
|
||||
end
|
||||
|
||||
-- PALETTE.DAT: 768 palette bytes, u16 numshades, numshades x 256 shade table, 65536-byte translucency table
|
||||
local function shadeTables(dat: buffer): (number, buffer?, buffer?)
|
||||
local n = buffer.len(dat)
|
||||
if n < 770 then
|
||||
return 0, nil, nil
|
||||
end
|
||||
local count = readu16(dat, 768)
|
||||
if count == 0 or 770 + count * 256 > n then
|
||||
return 0, nil, nil
|
||||
end
|
||||
local tables = buffer.create(count * 256)
|
||||
buffer.copy(tables, 0, dat, 770, count * 256)
|
||||
local tr: buffer? = nil
|
||||
local at = 770 + count * 256
|
||||
if at + 65536 <= n then
|
||||
local t = buffer.create(65536)
|
||||
buffer.copy(t, 0, dat, at, 65536)
|
||||
tr = t
|
||||
end
|
||||
return count, tables, tr
|
||||
end
|
||||
|
||||
-- LOOKUP.DAT: u8 count, count x (u8 palnum, 256-byte remap), then up to 5 alternate 768-byte palettes (Duke 3D)
|
||||
local function lookups(dat: buffer): ({ [number]: buffer }, { buffer })
|
||||
local n = buffer.len(dat)
|
||||
local out: { [number]: buffer } = {}
|
||||
local alts: { buffer } = {}
|
||||
if n < 1 then
|
||||
return out, alts
|
||||
end
|
||||
local count = readu8(dat, 0)
|
||||
local p = 1
|
||||
for _ = 1, count do
|
||||
if p + 257 > n then
|
||||
return out, alts
|
||||
end
|
||||
local t = buffer.create(256)
|
||||
buffer.copy(t, 0, dat, p + 1, 256)
|
||||
out[readu8(dat, p)] = t
|
||||
p += 257
|
||||
end
|
||||
while p + 768 <= n and #alts < 5 do
|
||||
local a = buffer.create(768)
|
||||
buffer.copy(a, 0, dat, p, 768)
|
||||
table.insert(alts, a)
|
||||
p += 768
|
||||
end
|
||||
return out, alts
|
||||
end
|
||||
|
||||
local function picanm(v: number): Picanm
|
||||
local function s8(x: number): number
|
||||
return if x >= 128 then x - 256 else x
|
||||
end
|
||||
return {
|
||||
frames = bit32.band(v, 63),
|
||||
type = TYPES[bit32.band(bit32.rshift(v, 6), 3) + 1] :: any,
|
||||
xoffset = s8(bit32.band(bit32.rshift(v, 8), 255)),
|
||||
yoffset = s8(bit32.band(bit32.rshift(v, 16), 255)),
|
||||
speed = bit32.band(bit32.rshift(v, 24), 15),
|
||||
extra = bit32.rshift(v, 28),
|
||||
}
|
||||
end
|
||||
|
||||
function Art.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
||||
local o: DecodeOptions = opts or {}
|
||||
local b, base, limit = r.buf, r.base, r.limit
|
||||
local at, first, last, numtiles, prefixed = header(b, base, limit)
|
||||
if not at or not first or not last then
|
||||
if limit - base < 16 then
|
||||
return nil, Err.new(Err.TRUNCATED, "short header", base)
|
||||
end
|
||||
return nil, Err.new(Err.CORRUPT, "not an ART file (version 1, first <= last tile)", base)
|
||||
end
|
||||
local n = last - first + 1
|
||||
local p = at :: number
|
||||
if limit - p < n * 8 then
|
||||
return nil, Err.new(Err.TRUNCATED, "tile tables", p)
|
||||
end
|
||||
local maxPixels = o.maxPixels or 2 ^ 26
|
||||
local sizes = table.create(n * 2) :: { number }
|
||||
local total = 0
|
||||
for i = 0, n - 1 do
|
||||
local w, h = readi16(b, p + i * 2), readi16(b, p + n * 2 + i * 2)
|
||||
if w < 0 or h < 0 then
|
||||
return nil, Err.new(Err.CORRUPT, string.format("tile %d has a negative size", first + i), p + i * 2)
|
||||
end
|
||||
sizes[i * 2 + 1], sizes[i * 2 + 2] = w, h
|
||||
total += w * h
|
||||
end
|
||||
if total > maxPixels then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d pixels is over the %d pixel limit", total, maxPixels))
|
||||
end
|
||||
local datStart = p + n * 8
|
||||
local palDat = fetch(o, "palette", "PALETTE.DAT")
|
||||
local pal = makePalette(if type(o.palette) == "table" then o.palette else nil, palDat)
|
||||
local hasPalette = type(o.palette) == "table" or (palDat ~= nil and buffer.len(palDat) >= 768)
|
||||
local remap: buffer? = nil
|
||||
local numShades, shades, translucency = 0, nil :: buffer?, nil :: buffer?
|
||||
if palDat then
|
||||
numShades, shades, translucency = shadeTables(palDat)
|
||||
end
|
||||
local lookDat = fetch(o, "lookup", "LOOKUP.DAT")
|
||||
local looks, alts = {} :: { [number]: buffer }, {} :: { buffer }
|
||||
if lookDat then
|
||||
looks, alts = lookups(lookDat)
|
||||
end
|
||||
local palNum = math.floor(tonumber(o.pal) or 0)
|
||||
if palNum ~= 0 then
|
||||
local t = looks[palNum]
|
||||
if not t then
|
||||
return nil, Err.new(Err.ARG, string.format("palookup %d is not in LOOKUP.DAT", palNum))
|
||||
end
|
||||
remap = t
|
||||
end
|
||||
local shade = math.floor(tonumber(o.shade) or 0)
|
||||
local shadeAt = -1
|
||||
if shade ~= 0 then
|
||||
if not shades or shade < 0 or shade >= numShades then
|
||||
return nil, Err.new(Err.ARG, string.format("shade %d is not in PALETTE.DAT", shade))
|
||||
end
|
||||
shadeAt = shade * 256
|
||||
end
|
||||
local native = o.native == true
|
||||
local frames = table.create(n) :: { Types.Frame }
|
||||
local truncated = false
|
||||
local q = datStart
|
||||
local main: Types.Image? = nil
|
||||
for i = 0, n - 1 do
|
||||
local w, h = sizes[i * 2 + 1], sizes[i * 2 + 2]
|
||||
local img = Types.Image.new(w, h, if native then "gray8" else "rgba8")
|
||||
local px = img.pixels
|
||||
local avail = math.clamp(limit - q, 0, w * h)
|
||||
if avail < w * h then
|
||||
truncated = true
|
||||
end
|
||||
-- pixels are stored column by column
|
||||
for x = 0, w - 1 do
|
||||
local col = q + x * h
|
||||
for y = 0, h - 1 do
|
||||
local k = x * h + y
|
||||
local idx = if k < avail then readu8(b, col + y) else TRANSPARENT
|
||||
local out = idx
|
||||
if idx ~= TRANSPARENT then
|
||||
if remap then
|
||||
out = readu8(remap, out)
|
||||
end
|
||||
if shades and shadeAt >= 0 then
|
||||
out = readu8(shades, shadeAt + out)
|
||||
end
|
||||
end
|
||||
if native then
|
||||
buffer.writeu8(px, y * w + x, out)
|
||||
elseif idx == TRANSPARENT then
|
||||
buffer.writeu32(px, (y * w + x) * 4, 0)
|
||||
else
|
||||
buffer.writeu32(px, (y * w + x) * 4, readu32(pal, out * 4))
|
||||
end
|
||||
end
|
||||
end
|
||||
q += w * h
|
||||
local anm = readu32(b, p + n * 4 + i * 4)
|
||||
img.meta.tile = first + i
|
||||
img.meta.picanm = picanm(anm)
|
||||
img.meta.source = { format = "art", picanm = anm }
|
||||
table.insert(frames, { image = img, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } :: Types.Frame)
|
||||
if not main and w > 0 then
|
||||
main = img
|
||||
end
|
||||
end
|
||||
local top = main or Types.Image.new(0, 0, if native then "gray8" else "rgba8")
|
||||
local img = Types.Image.new(top.width, top.height, top.format)
|
||||
buffer.copy(img.pixels, 0, top.pixels)
|
||||
img.frames = frames
|
||||
img.meta.palette = paletteList(pal)
|
||||
img.meta.tile = top.meta.tile
|
||||
img.meta.picanm = top.meta.picanm
|
||||
if shades then
|
||||
img.meta.shades = { count = numShades, data = shades }
|
||||
end
|
||||
if translucency then
|
||||
img.meta.translucency = translucency
|
||||
end
|
||||
if next(looks) then
|
||||
img.meta.lookups = looks
|
||||
end
|
||||
if #alts > 0 then
|
||||
img.meta.altPalettes = alts
|
||||
end
|
||||
if truncated then
|
||||
img.meta.truncated = true
|
||||
end
|
||||
img.meta.source = {
|
||||
format = "art",
|
||||
bitDepth = 8,
|
||||
colorType = "palette",
|
||||
version = 1,
|
||||
numtiles = numtiles,
|
||||
tileStart = first,
|
||||
tileEnd = last,
|
||||
buildart = prefixed,
|
||||
palette = hasPalette,
|
||||
trailing = math.max(0, limit - q),
|
||||
}
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function picanmWord(m: any, fallback: number?): number
|
||||
if type(m) ~= "table" then
|
||||
return fallback or 0
|
||||
end
|
||||
local function u8(v: any): number
|
||||
return math.floor(tonumber(v) or 0) % 256
|
||||
end
|
||||
local t = TYPE_INDEX[m.type] or 0
|
||||
return bit32.bor(
|
||||
bit32.band(math.floor(tonumber(m.frames) or 0), 63),
|
||||
bit32.lshift(t, 6),
|
||||
bit32.lshift(u8(m.xoffset), 8),
|
||||
bit32.lshift(u8(m.yoffset), 16),
|
||||
bit32.lshift(bit32.band(math.floor(tonumber(m.speed) or 0), 15), 24),
|
||||
bit32.lshift(bit32.band(math.floor(tonumber(m.extra) or 0), 15), 28)
|
||||
)
|
||||
end
|
||||
|
||||
function Art.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if type(img) ~= "table" or type(img.pixels) ~= "buffer" then
|
||||
return nil, Err.new(Err.ARG, "needs an Image")
|
||||
end
|
||||
local o: EncodeOptions = opts or {}
|
||||
local list: { Types.Image } = {}
|
||||
if type(img.frames) == "table" and #img.frames > 0 then
|
||||
for _, f in img.frames do
|
||||
if type(f) ~= "table" or type(f.image) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "bad frame")
|
||||
end
|
||||
table.insert(list, f.image)
|
||||
end
|
||||
else
|
||||
list[1] = img
|
||||
end
|
||||
local meta: any = img.meta or {}
|
||||
local src: any = meta.source or {}
|
||||
local palSrc: any = o.palette or meta.palette
|
||||
local palList: { { number } }? = nil
|
||||
if palSrc ~= nil then
|
||||
if type(palSrc) == "table" then
|
||||
palList = palSrc
|
||||
else
|
||||
local dat = bytesOf(palSrc)
|
||||
if not dat or buffer.len(dat) < 768 then
|
||||
return nil, Err.new(Err.ARG, "palette must be PALETTE.DAT bytes or a list of colours")
|
||||
end
|
||||
palList = paletteList(makePalette(nil, dat))
|
||||
end
|
||||
end
|
||||
local first = math.floor(tonumber(o.tileStart) or tonumber(src.tileStart) or tonumber(list[1].meta and list[1].meta.tile) or 0)
|
||||
if first < 0 or first + #list - 1 > 0xFFFFFFFF then
|
||||
return nil, Err.new(Err.ARG, "bad tileStart")
|
||||
end
|
||||
local n = #list
|
||||
local total = 0
|
||||
for i, t in list do
|
||||
local w, h = t.width, t.height
|
||||
if type(w) ~= "number" or type(h) ~= "number" or w < 0 or h < 0 or w > 32767 or h > 32767 or type(t.pixels) ~= "buffer" then
|
||||
return nil, Err.new(Err.ARG, string.format("frame %d: size must be 0..32767", i))
|
||||
end
|
||||
if w * h > 0 then
|
||||
if t.format == "rgba8" then
|
||||
if not palList then
|
||||
return nil, Err.new(Err.ARG, "rgba8 tiles need a palette (opts.palette or meta.palette)")
|
||||
end
|
||||
elseif t.format ~= "gray8" then
|
||||
return nil, Err.new(Err.ARG, string.format("frame %d: needs rgba8 or gray8 (indices)", i))
|
||||
end
|
||||
if buffer.len(t.pixels) < w * h * (if t.format == "rgba8" then 4 else 1) then
|
||||
return nil, Err.new(Err.ARG, string.format("frame %d: short pixel buffer", i))
|
||||
end
|
||||
end
|
||||
total += w * h
|
||||
end
|
||||
local prefixed = if o.buildart ~= nil then o.buildart == true else src.buildart == true
|
||||
local out = Bytes.writer((if prefixed then 8 else 0) + 16 + n * 8 + total)
|
||||
if prefixed then
|
||||
out:string("BUILDART")
|
||||
end
|
||||
out:u32le(1)
|
||||
out:u32le(math.floor(tonumber(o.numtiles) or tonumber(src.numtiles) or 0) % 2 ^ 32)
|
||||
out:u32le(first)
|
||||
out:u32le(first + n - 1)
|
||||
for _, t in list do
|
||||
out:u16le(t.width)
|
||||
end
|
||||
for _, t in list do
|
||||
out:u16le(t.height)
|
||||
end
|
||||
for _, t in list do
|
||||
local m: any = t.meta or {}
|
||||
out:u32le(picanmWord(m.picanm, m.source and tonumber(m.source.picanm)))
|
||||
end
|
||||
for _, t in list do
|
||||
local w, h = t.width, t.height
|
||||
if w * h > 0 then
|
||||
local idx: buffer
|
||||
if t.format == "rgba8" then
|
||||
idx = Quantize.map(t.pixels, w, h, palList :: { { number } }, { transparentIndex = TRANSPARENT })
|
||||
else
|
||||
idx = t.pixels
|
||||
end
|
||||
for x = 0, w - 1 do
|
||||
for y = 0, h - 1 do
|
||||
out:u8(readu8(idx, y * w + x))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return out:finish(), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
Art.picanm = picanm
|
||||
Art.picanmWord = picanmWord
|
||||
|
||||
return Art
|
||||
@@ -0,0 +1,401 @@
|
||||
--!strict
|
||||
-- model/buildmap/Geometry: Build map tables to a Scene. Slopes after BUILDINF.TXT (heinum: z rises heinum / 4096 per
|
||||
-- unit across the first wall, z units 16 to an xy unit); walls split at red walls into upper / lower parts and clipped
|
||||
-- where they cross, as Ken Silverman's map2stl does; texture coordinates approximate the engine's (xrepeat * 8 pixels
|
||||
-- across a wall, 128 z units per pixel at yrepeat 8, 16 units per floor pixel). New code.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Polygon = require("@rosetta/codecs/model/Polygon")
|
||||
|
||||
local Geometry = {}
|
||||
|
||||
local Scene = Types.Scene
|
||||
|
||||
export type Sector = { [string]: number }
|
||||
export type Wall = { [string]: number }
|
||||
export type Map = {
|
||||
version: number,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
ang: number, -- 2048 = a full turn, 0 = +x, 512 = +y
|
||||
cursectnum: number,
|
||||
sectors: { Sector }, -- raw fields, indices 0-based as in the file
|
||||
walls: { Wall },
|
||||
sprites: { { [string]: number } },
|
||||
trailing: buffer?, -- bytes after the sprites (kept for the writer)
|
||||
}
|
||||
export type Options = {
|
||||
scale: number?, -- world units per Build xy unit, default 1 (z is divided by 16 first)
|
||||
tiles: { [number]: any }?, -- tile number -> Image or { width, height }: texture sizes for the UVs, images attached
|
||||
skyWalls: boolean?, -- keep upper / lower walls between two parallaxed ceilings / floors; default false
|
||||
}
|
||||
|
||||
local PARALLAX, SLOPED, SWAP_XY, SMOOSH, FLIP_X, FLIP_Y, RELATIVE = 1, 2, 4, 8, 16, 32, 64
|
||||
local W_BOTTOM_SWAP, W_ALIGN_BOTTOM, W_FLIP_X, W_MASKED, W_FLIP_Y = 2, 4, 8, 16, 256
|
||||
local YAX_BIT = 1024
|
||||
local band = bit32.band
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ queries
|
||||
function Geometry.slopeZ(map: Map, sector: number, floor: boolean, x: number, y: number): number
|
||||
local s = map.sectors[sector + 1]
|
||||
if not s then
|
||||
return 0
|
||||
end
|
||||
local z = if floor then s.floorz else s.ceilingz
|
||||
local stat = if floor then s.floorstat else s.ceilingstat
|
||||
local heinum = if floor then s.floorheinum else s.ceilingheinum
|
||||
if band(stat, SLOPED) == 0 or heinum == 0 then
|
||||
return z
|
||||
end
|
||||
local w0 = map.walls[s.wallptr + 1]
|
||||
local w1 = w0 and map.walls[w0.point2 + 1]
|
||||
if not w1 then
|
||||
return z
|
||||
end
|
||||
local dx, dy = w1.x - w0.x, w1.y - w0.y
|
||||
local len = math.sqrt(dx * dx + dy * dy)
|
||||
if len == 0 then
|
||||
return z
|
||||
end
|
||||
return z + heinum * (dx * (y - w0.y) - dy * (x - w0.x)) / (len * 256)
|
||||
end
|
||||
|
||||
-- the sector's wall loops (0-based wall indices, following point2); loops that leave the sector or do not close are dropped
|
||||
function Geometry.loops(map: Map, sector: number): { { number } }
|
||||
local s = map.sectors[sector + 1]
|
||||
local out: { { number } } = {}
|
||||
if not s then
|
||||
return out
|
||||
end
|
||||
local first, last = s.wallptr, s.wallptr + s.wallnum - 1
|
||||
local seen: { [number]: boolean } = {}
|
||||
for start = first, last do
|
||||
if not seen[start] then
|
||||
local loop: { number } = {}
|
||||
local w = start
|
||||
local ok = false
|
||||
for _ = 0, s.wallnum do
|
||||
if w < first or w > last or seen[w] then
|
||||
break
|
||||
end
|
||||
local wall = map.walls[w + 1]
|
||||
if not wall then
|
||||
break
|
||||
end
|
||||
seen[w] = true
|
||||
table.insert(loop, w)
|
||||
w = wall.point2
|
||||
if w == start then
|
||||
ok = true
|
||||
break
|
||||
end
|
||||
end
|
||||
if ok and #loop >= 3 then
|
||||
table.insert(out, loop)
|
||||
end
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- v9 room over room (eduke32 YAX): bunch numbers in the x panning of a ceiling / floor flagged 1024; walls flagged
|
||||
-- 1024 / 2048 keep the linked wall in lotag (ceiling) / extra (floor)
|
||||
function Geometry.yax(map: Map)
|
||||
for _, s in map.sectors do
|
||||
if band(s.ceilingstat, YAX_BIT) ~= 0 then
|
||||
s.ceilingbunch = s.ceilingxpanning
|
||||
end
|
||||
if band(s.floorstat, YAX_BIT) ~= 0 then
|
||||
s.floorbunch = s.floorxpanning
|
||||
end
|
||||
end
|
||||
for _, w in map.walls do
|
||||
if band(w.cstat, YAX_BIT) ~= 0 then
|
||||
w.yaxceilingwall = w.lotag
|
||||
end
|
||||
if band(w.cstat, YAX_BIT * 2) ~= 0 then
|
||||
w.yaxfloorwall = w.extra
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ scene
|
||||
type Batch = { kind: string, tile: number, data: { number }, n: number } -- data: x y z nx ny nz u v per vertex
|
||||
|
||||
function Geometry.scene(map: Map, opts: Options): Types.Scene
|
||||
local scale = tonumber(opts.scale) or 1
|
||||
local tiles = if type(opts.tiles) == "table" then opts.tiles else nil
|
||||
local skyWalls = opts.skyWalls == true
|
||||
local walls, sectors = map.walls, map.sectors
|
||||
local batches: { [string]: Batch } = {}
|
||||
local order: { Batch } = {}
|
||||
local function tileSize(tile: number): (number, number)
|
||||
local t = tiles and tiles[tile]
|
||||
local w, h = if type(t) == "table" then tonumber(t.width) else nil, if type(t) == "table" then tonumber(t.height) else nil
|
||||
return if w and w > 0 then w else 64, if h and h > 0 then h else 64
|
||||
end
|
||||
local function batch(kind: string, tile: number): Batch
|
||||
local key = kind .. ":" .. tile
|
||||
local b = batches[key]
|
||||
if not b then
|
||||
b = { kind = kind, tile = tile, data = {}, n = 0 }
|
||||
batches[key] = b
|
||||
table.insert(order, b)
|
||||
end
|
||||
return b
|
||||
end
|
||||
local function emit(b: Batch, v: { number })
|
||||
local d = b.data
|
||||
local k = #d
|
||||
for i = 1, 8 do
|
||||
d[k + i] = v[i]
|
||||
end
|
||||
b.n += 1
|
||||
end
|
||||
local function world(x: number, y: number, z: number): (number, number, number)
|
||||
return x * scale, -y * scale, -z / 16 * scale
|
||||
end
|
||||
local function tri(b: Batch, a: { number }, c: { number }, d: { number })
|
||||
-- a / c / d: Build x, y, z, u, v
|
||||
local ax, ay, az = world(a[1], a[2], a[3])
|
||||
local cx, cy, cz = world(c[1], c[2], c[3])
|
||||
local dx, dy, dz = world(d[1], d[2], d[3])
|
||||
local ux, uy, uz = cx - ax, cy - ay, cz - az
|
||||
local vx, vy, vz = dx - ax, dy - ay, dz - az
|
||||
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
||||
local l = math.sqrt(nx * nx + ny * ny + nz * nz)
|
||||
if l == 0 then
|
||||
return
|
||||
end
|
||||
nx, ny, nz = nx / l, ny / l, nz / l
|
||||
emit(b, { ax, ay, az, nx, ny, nz, a[4], a[5] })
|
||||
emit(b, { cx, cy, cz, nx, ny, nz, c[4], c[5] })
|
||||
emit(b, { dx, dy, dz, nx, ny, nz, d[4], d[5] })
|
||||
end
|
||||
|
||||
-- floors and ceilings
|
||||
local secLoops: { { { number } } } = {}
|
||||
for si, s in sectors do
|
||||
local ls = Geometry.loops(map, si - 1)
|
||||
secLoops[si] = ls
|
||||
local flat: { { number } } = {}
|
||||
for li, loop in ls do
|
||||
local f: { number } = {}
|
||||
for _, w in loop do
|
||||
table.insert(f, walls[w + 1].x)
|
||||
table.insert(f, -walls[w + 1].y)
|
||||
end
|
||||
flat[li] = f
|
||||
end
|
||||
local tris = Polygon.triangulate(flat)
|
||||
for pass = 1, 2 do
|
||||
local isFloor = pass == 1
|
||||
local stat = if isFloor then s.floorstat else s.ceilingstat
|
||||
if not (map.version == 9 and band(stat, YAX_BIT) ~= 0) then
|
||||
local tile = if isFloor then s.floorpicnum else s.ceilingpicnum
|
||||
local tw, th = tileSize(tile)
|
||||
local texel = if band(stat, SMOOSH) ~= 0 then 8 else 16
|
||||
local xpan = (if isFloor then s.floorxpanning else s.ceilingxpanning) * tw / 256
|
||||
local ypan = (if isFloor then s.floorypanning else s.ceilingypanning) * th / 256
|
||||
local ox, oy, ex, ey = 0, 0, 1, 0
|
||||
if band(stat, RELATIVE) ~= 0 then
|
||||
local w0 = walls[s.wallptr + 1]
|
||||
local w1 = w0 and walls[w0.point2 + 1]
|
||||
if w1 then
|
||||
local dx, dy = w1.x - w0.x, w1.y - w0.y
|
||||
local len = math.sqrt(dx * dx + dy * dy)
|
||||
if len > 0 then
|
||||
ox, oy, ex, ey = w0.x, w0.y, dx / len, dy / len
|
||||
end
|
||||
end
|
||||
end
|
||||
local b = batch(if isFloor then "floor" else "ceiling", tile)
|
||||
local function corner(li: number, vi: number): { number }
|
||||
local w = walls[ls[li][vi] + 1]
|
||||
local x, y = w.x, w.y
|
||||
local lx, ly = (x - ox) * ex + (y - oy) * ey, (y - oy) * ex - (x - ox) * ey
|
||||
if band(stat, SWAP_XY) ~= 0 then
|
||||
lx, ly = ly, lx
|
||||
end
|
||||
if band(stat, FLIP_X) ~= 0 then
|
||||
lx = -lx
|
||||
end
|
||||
if band(stat, FLIP_Y) ~= 0 then
|
||||
ly = -ly
|
||||
end
|
||||
return { x, y, Geometry.slopeZ(map, si - 1, isFloor, x, y), (lx / texel + xpan) / tw, (ly / texel + ypan) / th }
|
||||
end
|
||||
for k = 1, #tris, 6 do
|
||||
local a, c, d = corner(tris[k], tris[k + 1]), corner(tris[k + 2], tris[k + 3]), corner(tris[k + 4], tris[k + 5])
|
||||
if isFloor then
|
||||
tri(b, a, c, d)
|
||||
else
|
||||
tri(b, a, d, c)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- walls: (x1, y1) -> (x2, y2) with the sector on the right; top / bottom z at both ends, z down
|
||||
local function part(kind: string, tile: number, w: Wall, x1: number, y1: number, x2: number, y2: number, t1: number, t2: number, b1: number, b2: number, anchor: number, fromBottom: boolean)
|
||||
local h1, h2 = b1 - t1, b2 - t2
|
||||
if h1 <= 0 and h2 <= 0 then
|
||||
return
|
||||
end
|
||||
local tw, th = tileSize(tile)
|
||||
local flipX, flipY = band(w.cstat, W_FLIP_X) ~= 0, band(w.cstat, W_FLIP_Y) ~= 0
|
||||
local span = w.xrepeat * 8
|
||||
local function v(t: number, z: number): { number }
|
||||
local u = (w.xpanning + (if flipX then 1 - t else t) * span) / tw
|
||||
local px = (z - anchor) * w.yrepeat / 1024 + w.ypanning * th / 256 + (if fromBottom then th else 0)
|
||||
local vv = px / th
|
||||
return { x1 + (x2 - x1) * t, y1 + (y2 - y1) * t, z, u, if flipY then -vv else vv }
|
||||
end
|
||||
local b = batch(kind, tile)
|
||||
if h1 > 0 and h2 > 0 then
|
||||
local p1b, p2b, p2t, p1t = v(0, b1), v(1, b2), v(1, t2), v(0, t1)
|
||||
tri(b, p1b, p2b, p2t)
|
||||
tri(b, p1b, p2t, p1t)
|
||||
elseif h1 > 0 then
|
||||
local f = h1 / (h1 - h2)
|
||||
tri(b, v(0, b1), v(f, t1 + (t2 - t1) * f), v(0, t1))
|
||||
else
|
||||
local f = h1 / (h1 - h2)
|
||||
tri(b, v(f, t1 + (t2 - t1) * f), v(1, b2), v(1, t2))
|
||||
end
|
||||
end
|
||||
for si, s in sectors do
|
||||
local me = si - 1
|
||||
for _, loop in secLoops[si] do
|
||||
for _, wi in loop do
|
||||
local w = walls[wi + 1]
|
||||
local w2 = walls[w.point2 + 1]
|
||||
local x1, y1, x2, y2 = w.x, w.y, w2.x, w2.y
|
||||
local c1, c2 = Geometry.slopeZ(map, me, false, x1, y1), Geometry.slopeZ(map, me, false, x2, y2)
|
||||
local f1, f2 = Geometry.slopeZ(map, me, true, x1, y1), Geometry.slopeZ(map, me, true, x2, y2)
|
||||
local ns = w.nextsector
|
||||
local other = if ns >= 0 then sectors[ns + 1] else nil
|
||||
if not other then
|
||||
local fromBottom = band(w.cstat, W_ALIGN_BOTTOM) ~= 0
|
||||
part("wall", w.picnum, w, x1, y1, x2, y2, c1, c2, f1, f2, if fromBottom then s.floorz else s.ceilingz, fromBottom)
|
||||
else
|
||||
local oc1, oc2 = Geometry.slopeZ(map, ns, false, x1, y1), Geometry.slopeZ(map, ns, false, x2, y2)
|
||||
local of1, of2 = Geometry.slopeZ(map, ns, true, x1, y1), Geometry.slopeZ(map, ns, true, x2, y2)
|
||||
local aligned = band(w.cstat, W_ALIGN_BOTTOM) ~= 0
|
||||
if skyWalls or band(s.ceilingstat, PARALLAX) == 0 or band(other.ceilingstat, PARALLAX) == 0 then
|
||||
part("wall", w.picnum, w, x1, y1, x2, y2, c1, c2, oc1, oc2, if aligned then s.ceilingz else other.ceilingz, not aligned)
|
||||
end
|
||||
if skyWalls or band(s.floorstat, PARALLAX) == 0 or band(other.floorstat, PARALLAX) == 0 then
|
||||
local lw = w
|
||||
if band(w.cstat, W_BOTTOM_SWAP) ~= 0 and walls[w.nextwall + 1] then
|
||||
lw = walls[w.nextwall + 1]
|
||||
end
|
||||
part("wall", lw.picnum, lw, x1, y1, x2, y2, of1, of2, f1, f2, if aligned then s.ceilingz else other.floorz, false)
|
||||
end
|
||||
if band(w.cstat, W_MASKED) ~= 0 then
|
||||
local fromBottom = aligned
|
||||
part("masked", w.overpicnum, w, x1, y1, x2, y2, math.max(c1, oc1), math.max(c2, oc2), math.min(f1, of1), math.min(f2, of2),
|
||||
if fromBottom then math.min(s.floorz, other.floorz) else math.max(s.ceilingz, other.ceilingz), fromBottom)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the mesh: a primitive per (kind, tile), a material per tile (masked walls their own, alpha-masked)
|
||||
local scene = Scene.new()
|
||||
scene.meta.up = "z"
|
||||
local mesh: Types.Mesh = { name = "map", primitives = {} }
|
||||
local texIndex: { [number]: number } = {}
|
||||
local matIndex: { [string]: number } = {}
|
||||
for _, b in order do
|
||||
local n = b.n
|
||||
if n > 0 then
|
||||
local pos, nrm, uv, idx = buffer.create(n * 12), buffer.create(n * 12), buffer.create(n * 8), buffer.create(n * 4)
|
||||
local d = b.data
|
||||
for i = 0, n - 1 do
|
||||
local k = i * 8
|
||||
buffer.writef32(pos, i * 12, d[k + 1])
|
||||
buffer.writef32(pos, i * 12 + 4, d[k + 2])
|
||||
buffer.writef32(pos, i * 12 + 8, d[k + 3])
|
||||
buffer.writef32(nrm, i * 12, d[k + 4])
|
||||
buffer.writef32(nrm, i * 12 + 4, d[k + 5])
|
||||
buffer.writef32(nrm, i * 12 + 8, d[k + 6])
|
||||
buffer.writef32(uv, i * 8, d[k + 7])
|
||||
buffer.writef32(uv, i * 8 + 4, d[k + 8])
|
||||
buffer.writeu32(idx, i * 4, i)
|
||||
end
|
||||
local ti = texIndex[b.tile]
|
||||
if not ti then
|
||||
local t = tiles and tiles[b.tile]
|
||||
table.insert(scene.textures, {
|
||||
name = "tile" .. b.tile,
|
||||
uri = "tile" .. b.tile,
|
||||
image = if type(t) == "table" and t.kind == "image" then t else nil,
|
||||
extras = { build = { tile = b.tile } },
|
||||
})
|
||||
ti = #scene.textures
|
||||
texIndex[b.tile] = ti
|
||||
end
|
||||
local masked = b.kind == "masked"
|
||||
local mkey = (if masked then "m" else "s") .. b.tile
|
||||
local mi = matIndex[mkey]
|
||||
if not mi then
|
||||
local mat = Scene.material("tile" .. b.tile)
|
||||
mat.metallic = 0
|
||||
mat.baseColorTexture = { texture = ti }
|
||||
if masked then
|
||||
mat.alphaMode = "mask"
|
||||
mat.doubleSided = true
|
||||
end
|
||||
mat.extras = { build = { tile = b.tile } }
|
||||
table.insert(scene.materials, mat)
|
||||
mi = #scene.materials
|
||||
matIndex[mkey] = mi
|
||||
end
|
||||
table.insert(mesh.primitives, {
|
||||
attributes = {
|
||||
POSITION = Scene.accessor(pos, "f32", 3, n),
|
||||
NORMAL = Scene.accessor(nrm, "f32", 3, n),
|
||||
TEXCOORD_0 = Scene.accessor(uv, "f32", 2, n),
|
||||
},
|
||||
indices = Scene.accessor(idx, "u32", 1, n),
|
||||
mode = "triangles" :: any,
|
||||
material = mi,
|
||||
extras = { build = { kind = b.kind, tile = b.tile } },
|
||||
})
|
||||
end
|
||||
end
|
||||
table.insert(scene.meshes, mesh)
|
||||
local root: Types.Node = { name = "map", children = {}, mesh = 1 }
|
||||
scene.nodes[1] = root
|
||||
scene.roots[1] = 1
|
||||
local function yaw(ang: number): { number }
|
||||
local a = -ang * math.pi / 2048
|
||||
return { 0, 0, math.sin(a), math.cos(a) }
|
||||
end
|
||||
local sx, sy, sz = world(map.x, map.y, map.z)
|
||||
table.insert(scene.nodes, {
|
||||
name = "start",
|
||||
children = {},
|
||||
translation = { sx, sy, sz },
|
||||
rotation = yaw(map.ang),
|
||||
extras = { build = { start = true, sector = map.cursectnum } },
|
||||
})
|
||||
table.insert(root.children, #scene.nodes)
|
||||
for i, sp in map.sprites do
|
||||
local px, py, pz = world(sp.x, sp.y, sp.z)
|
||||
table.insert(scene.nodes, {
|
||||
name = "sprite " .. (i - 1),
|
||||
children = {},
|
||||
translation = { px, py, pz },
|
||||
rotation = yaw(sp.ang),
|
||||
extras = { build = { tile = sp.picnum, sprite = sp } },
|
||||
})
|
||||
table.insert(root.children, #scene.nodes)
|
||||
end
|
||||
return scene
|
||||
end
|
||||
|
||||
return Geometry
|
||||
@@ -0,0 +1,270 @@
|
||||
--!strict
|
||||
-- model/buildmap: Build engine maps (.map versions 7-9: Duke Nukem 3D, Shadow Warrior, eduke32), from Ken Silverman's
|
||||
-- BUILDINF.TXT and the eduke32 wiki ("MAP format"); no code taken. Supports: sectors, walls, sprites as raw tables
|
||||
-- (meta.build), a Scene (sloped floors / ceilings, walls with upper / lower parts at red walls, masked walls, sprites
|
||||
-- as nodes), v9 room-over-room fields, trailing bytes; writes the raw tables back. Not supported: v5 / v6, Blood's
|
||||
-- encrypted BLM maps, the eduke32 text format (v10).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Geometry = require("@rosetta/model/buildmap/Geometry")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local BuildMap = {
|
||||
name = "buildmap",
|
||||
category = "model",
|
||||
extensions = { "map" },
|
||||
mime = { "application/x-build-map" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Polygon" },
|
||||
caps = { decode = true, encode = true, sprites = true, slopes = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type Sector = Geometry.Sector
|
||||
export type Wall = Geometry.Wall
|
||||
export type Sprite = { [string]: number }
|
||||
export type Map = Geometry.Map
|
||||
export type DecodeOptions = Geometry.Options & {
|
||||
maxBytes: number?, -- default 2^28
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
version: number?, -- 7, 8 or 9; default map.version
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
-- field name, size (1 / -1 = u8 / i8, 2 / -2 = u16 / i16, -4 = i32), BUILDINF.TXT order
|
||||
local SECTOR = {
|
||||
{ "wallptr", -2 }, { "wallnum", -2 }, { "ceilingz", -4 }, { "floorz", -4 }, { "ceilingstat", -2 }, { "floorstat", -2 },
|
||||
{ "ceilingpicnum", -2 }, { "ceilingheinum", -2 }, { "ceilingshade", -1 }, { "ceilingpal", 1 }, { "ceilingxpanning", 1 },
|
||||
{ "ceilingypanning", 1 }, { "floorpicnum", -2 }, { "floorheinum", -2 }, { "floorshade", -1 }, { "floorpal", 1 },
|
||||
{ "floorxpanning", 1 }, { "floorypanning", 1 }, { "visibility", 1 }, { "filler", 1 }, { "lotag", -2 }, { "hitag", -2 },
|
||||
{ "extra", -2 },
|
||||
}
|
||||
local WALL = {
|
||||
{ "x", -4 }, { "y", -4 }, { "point2", -2 }, { "nextwall", -2 }, { "nextsector", -2 }, { "cstat", -2 }, { "picnum", -2 },
|
||||
{ "overpicnum", -2 }, { "shade", -1 }, { "pal", 1 }, { "xrepeat", 1 }, { "yrepeat", 1 }, { "xpanning", 1 },
|
||||
{ "ypanning", 1 }, { "lotag", -2 }, { "hitag", -2 }, { "extra", -2 },
|
||||
}
|
||||
local SPRITE = {
|
||||
{ "x", -4 }, { "y", -4 }, { "z", -4 }, { "cstat", -2 }, { "picnum", -2 }, { "shade", -1 }, { "pal", 1 }, { "clipdist", 1 },
|
||||
{ "filler", 1 }, { "xrepeat", 1 }, { "yrepeat", 1 }, { "xoffset", -1 }, { "yoffset", -1 }, { "sectnum", -2 },
|
||||
{ "statnum", -2 }, { "ang", -2 }, { "owner", -2 }, { "xvel", -2 }, { "yvel", -2 }, { "zvel", -2 }, { "lotag", -2 },
|
||||
{ "hitag", -2 }, { "extra", -2 },
|
||||
}
|
||||
local SECTOR_SIZE, WALL_SIZE, SPRITE_SIZE = 40, 32, 44
|
||||
local HEADER = 22 -- version, posx, posy, posz, ang, cursectnum, then u16 numsectors at 20
|
||||
local readu16 = buffer.readu16
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
local function layout(b: buffer, o: number, limit: number): (number?, number?, number?, number?)
|
||||
if limit - o < HEADER then
|
||||
return nil, nil, nil, nil
|
||||
end
|
||||
local ns = readu16(b, o + 20)
|
||||
local p = o + HEADER + ns * SECTOR_SIZE
|
||||
if p + 2 > limit then
|
||||
return nil, nil, nil, nil
|
||||
end
|
||||
local nw = readu16(b, p)
|
||||
p += 2 + nw * WALL_SIZE
|
||||
if p + 2 > limit then
|
||||
return nil, nil, nil, nil
|
||||
end
|
||||
local nsp = readu16(b, p)
|
||||
p += 2 + nsp * SPRITE_SIZE
|
||||
return ns, nw, nsp, p
|
||||
end
|
||||
|
||||
function BuildMap.sniff(r: Bytes.Reader): number
|
||||
local b, o, limit = r.buf, r.base, r.limit
|
||||
if limit - o < HEADER then
|
||||
return 0
|
||||
end
|
||||
local v = buffer.readu32(b, o)
|
||||
if v ~= 7 and v ~= 8 and v ~= 9 then
|
||||
return 0
|
||||
end
|
||||
local ns, _, _, stop = layout(b, o, limit)
|
||||
if not ns or not stop or stop > limit then
|
||||
return 0
|
||||
end
|
||||
return if stop == limit then 0.9 else 0.5
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function readRecord(b: buffer, p: number, fields: { any }): { [string]: number }
|
||||
local rec: { [string]: number } = {}
|
||||
for _, f in fields do
|
||||
local size = f[2]
|
||||
local v
|
||||
if size == 1 then
|
||||
v = buffer.readu8(b, p)
|
||||
elseif size == -1 then
|
||||
v = buffer.readi8(b, p)
|
||||
elseif size == 2 then
|
||||
v = readu16(b, p)
|
||||
elseif size == -2 then
|
||||
v = buffer.readi16(b, p)
|
||||
else
|
||||
v = buffer.readi32(b, p)
|
||||
end
|
||||
rec[f[1]] = v
|
||||
p += math.abs(size)
|
||||
end
|
||||
return rec
|
||||
end
|
||||
|
||||
function BuildMap.parse(r: Bytes.Reader, opts: DecodeOptions?): (Map?, Err.Err?)
|
||||
local b, o, limit = r.buf, r.base, r.limit
|
||||
if limit - o < 4 then
|
||||
return nil, Err.new(Err.TRUNCATED, "short header", o)
|
||||
end
|
||||
if buffer.readstring(b, o, 4) == "BLM\26" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "Blood maps (BLM, encrypted) are not supported", o)
|
||||
end
|
||||
if buffer.readstring(b, o, 4) == "--ED" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "eduke32 text maps (version 10) are not supported", o)
|
||||
end
|
||||
local version = buffer.readu32(b, o)
|
||||
if version == 5 or version == 6 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, string.format("map version %d (only 7-9)", version), o)
|
||||
elseif version < 7 or version > 9 then
|
||||
return nil, Err.new(Err.CORRUPT, string.format("not a Build map (version %d)", version), o)
|
||||
end
|
||||
local maxBytes = opts and opts.maxBytes or 2 ^ 28
|
||||
if limit - o > maxBytes then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d bytes is over the %d byte limit", limit - o, maxBytes))
|
||||
end
|
||||
local ns, nw, nsp, stop = layout(b, o, limit)
|
||||
if not ns or not nw or not nsp or not stop or stop > limit then
|
||||
return nil, Err.new(Err.TRUNCATED, "sector / wall / sprite tables end early", o)
|
||||
end
|
||||
local map: Map = {
|
||||
version = version,
|
||||
x = buffer.readi32(b, o + 4),
|
||||
y = buffer.readi32(b, o + 8),
|
||||
z = buffer.readi32(b, o + 12),
|
||||
ang = buffer.readi16(b, o + 16),
|
||||
cursectnum = buffer.readi16(b, o + 18),
|
||||
sectors = table.create(ns) :: { Sector },
|
||||
walls = table.create(nw) :: { Wall },
|
||||
sprites = table.create(nsp) :: { Sprite },
|
||||
}
|
||||
local p = o + HEADER
|
||||
for i = 1, ns do
|
||||
local s = readRecord(b, p, SECTOR) :: any
|
||||
if s.wallptr < 0 or s.wallnum < 0 or s.wallptr + s.wallnum > nw then
|
||||
return nil, Err.new(Err.CORRUPT, string.format("sector %d: walls %d + %d out of range", i - 1, s.wallptr, s.wallnum), p)
|
||||
end
|
||||
map.sectors[i] = s
|
||||
p += SECTOR_SIZE
|
||||
end
|
||||
p += 2
|
||||
for i = 1, nw do
|
||||
map.walls[i] = readRecord(b, p, WALL) :: any
|
||||
p += WALL_SIZE
|
||||
end
|
||||
p += 2
|
||||
for i = 1, nsp do
|
||||
map.sprites[i] = readRecord(b, p, SPRITE)
|
||||
p += SPRITE_SIZE
|
||||
end
|
||||
if stop < limit then
|
||||
local t = buffer.create(limit - stop)
|
||||
buffer.copy(t, 0, b, stop, limit - stop)
|
||||
map.trailing = t
|
||||
end
|
||||
if version == 9 then
|
||||
Geometry.yax(map)
|
||||
end
|
||||
return map, nil
|
||||
end
|
||||
|
||||
function BuildMap.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local map, err = BuildMap.parse(r, opts)
|
||||
if not map then
|
||||
return nil, err
|
||||
end
|
||||
return BuildMap.scene(map, opts), nil
|
||||
end
|
||||
|
||||
function BuildMap.scene(map: Map, opts: Geometry.Options?): Types.Scene
|
||||
local scene = Geometry.scene(map, opts or {})
|
||||
scene.meta.build = map
|
||||
scene.meta.source = {
|
||||
format = "buildmap",
|
||||
version = map.version,
|
||||
sectors = #map.sectors,
|
||||
walls = #map.walls,
|
||||
sprites = #map.sprites,
|
||||
}
|
||||
return scene
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function writeRecord(out: Bytes.Writer, rec: { [string]: any }, fields: { any }): string?
|
||||
for _, f in fields do
|
||||
local v = rec[f[1]]
|
||||
if type(v) ~= "number" or v ~= v then
|
||||
return f[1]
|
||||
end
|
||||
local size = math.abs(f[2])
|
||||
local n = math.floor(v)
|
||||
if size == 1 then
|
||||
out:u8(n % 256)
|
||||
elseif size == 2 then
|
||||
out:u16le(n % 65536)
|
||||
else
|
||||
out:u32le(n % 4294967296)
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function BuildMap.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local map = if type(value) == "table" and type(value.meta) == "table" and value.meta.build then value.meta.build else value
|
||||
if type(map) ~= "table" or type(map.sectors) ~= "table" or type(map.walls) ~= "table" or type(map.sprites) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "needs a Scene from model/buildmap (meta.build) or a map table")
|
||||
end
|
||||
local version = math.floor(tonumber(opts and opts.version) or tonumber(map.version) or 7)
|
||||
if version < 7 or version > 9 then
|
||||
return nil, Err.new(Err.ARG, "version must be 7, 8 or 9")
|
||||
end
|
||||
local ns, nw, nsp = #map.sectors, #map.walls, #map.sprites
|
||||
if ns > 65535 or nw > 65535 or nsp > 65535 then
|
||||
return nil, Err.new(Err.ARG, "at most 65535 sectors / walls / sprites")
|
||||
end
|
||||
local trailing = if type(map.trailing) == "buffer" then map.trailing else nil
|
||||
local out = Bytes.writer(HEADER + 6 + ns * SECTOR_SIZE + nw * WALL_SIZE + nsp * SPRITE_SIZE + (if trailing then buffer.len(trailing) else 0))
|
||||
out:u32le(version)
|
||||
for _, k in { "x", "y", "z" } do
|
||||
out:u32le(math.floor(tonumber(map[k]) or 0) % 4294967296)
|
||||
end
|
||||
out:u16le(math.floor(tonumber(map.ang) or 0) % 65536)
|
||||
out:u16le(math.floor(tonumber(map.cursectnum) or 0) % 65536)
|
||||
for _, part in { { map.sectors, SECTOR, "sector" }, { map.walls, WALL, "wall" }, { map.sprites, SPRITE, "sprite" } } do
|
||||
local list = part[1] :: { any }
|
||||
out:u16le(#list)
|
||||
for i, rec in list do
|
||||
if type(rec) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, string.format("%s %d is not a table", part[3] :: string, i - 1))
|
||||
end
|
||||
local bad = writeRecord(out, rec, part[2] :: { any })
|
||||
if bad then
|
||||
return nil, Err.new(Err.ARG, string.format("%s %d: field %s is not a number", part[3] :: string, i - 1, bad))
|
||||
end
|
||||
end
|
||||
end
|
||||
if trailing then
|
||||
out:bytes(trailing)
|
||||
end
|
||||
return out:finish(), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
BuildMap.slopeZ = Geometry.slopeZ
|
||||
BuildMap.loops = Geometry.loops
|
||||
|
||||
return BuildMap
|
||||
@@ -0,0 +1,133 @@
|
||||
-- image/art tests against arttool (eduke32 tools) and an independent Python reading (tools/fixtures/art.py): indices,
|
||||
-- picanm fields, colours through PALETTE.DAT as arttool exports them, shade / palookup remaps, write round trips.
|
||||
local T = require("@tests/T")
|
||||
local Art = require("@rosetta/image/art")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Fx = require("@tests/gen/art")
|
||||
|
||||
local t = T.suite("art")
|
||||
t:eq(Art.name, "art", "name")
|
||||
t:check(Art.caps.decode and Art.caps.encode, "caps")
|
||||
|
||||
local data = buffer.fromstring(Fx.art)
|
||||
t:eq(Art.sniff(Bytes.reader(data)), 0.9, "sniff")
|
||||
t:eq(Art.sniff(Bytes.reader(buffer.fromstring("GIF89a........................"))), 0, "sniff other")
|
||||
|
||||
local TYPES = { [0] = "none", "oscillate", "forward", "backward" }
|
||||
local function s8(v: number): number
|
||||
return if v >= 128 then v - 256 else v
|
||||
end
|
||||
|
||||
local nat = assert(Art.decode(Bytes.reader(data), { native = true }))
|
||||
local img = assert(Art.decode(Bytes.reader(data), { palette = Fx.palette }))
|
||||
t:eq(#(nat.frames :: any), Fx.last - Fx.start + 1, "one frame per tile")
|
||||
t:eq(img.meta.source.tileStart, Fx.start, "tileStart")
|
||||
t:eq(img.meta.source.tileEnd, Fx.last, "tileEnd")
|
||||
t:check(img.meta.source.palette, "palette used")
|
||||
t:eq(img.meta.shades.count, 32, "shade tables")
|
||||
t:check(img.meta.translucency and buffer.len(img.meta.translucency) == 65536, "translucency table")
|
||||
for i, c in Fx.tiles do
|
||||
local fn = (nat.frames :: any)[i].image
|
||||
local fr = (img.frames :: any)[i].image
|
||||
local label = "tile " .. c.tile
|
||||
t:eq(fn.meta.tile, c.tile, label .. ": number")
|
||||
t:eq(fn.width, c.w, label .. ": width")
|
||||
t:eq(fn.height, c.h, label .. ": height")
|
||||
local p = fn.meta.picanm
|
||||
t:eq(p.frames, c.animFrames, label .. ": anim frames")
|
||||
t:eq(p.type, TYPES[c.animType], label .. ": anim type")
|
||||
t:eq(p.speed, c.animSpeed, label .. ": anim speed")
|
||||
t:eq(p.xoffset, s8(c.xofs), label .. ": x offset")
|
||||
t:eq(p.yoffset, s8(c.yofs), label .. ": y offset")
|
||||
t:eq(Art.picanmWord(p), c.picanm, label .. ": picanm word")
|
||||
if c.idx then
|
||||
t:eq(buffer.tostring(fn.pixels), c.idx, label .. ": indices")
|
||||
local bad = 0
|
||||
for k = 0, c.w * c.h - 1 do
|
||||
local idx = string.byte(c.idx, k + 1)
|
||||
local px = fr.pixels
|
||||
if idx == 255 then
|
||||
if buffer.readu32(px, k * 4) ~= 0 then
|
||||
bad += 1
|
||||
end
|
||||
else
|
||||
for ch = 0, 2 do
|
||||
if buffer.readu8(px, k * 4 + ch) ~= string.byte(c.rgb, k * 3 + ch + 1) then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
if buffer.readu8(px, k * 4 + 3) ~= 255 then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, label .. ": colours as arttool exports them")
|
||||
end
|
||||
end
|
||||
t:eq(img.width, Fx.tiles[1].w, "top-level image = first non-empty tile")
|
||||
|
||||
-- shade and palookup remaps (index 255 stays transparent)
|
||||
do
|
||||
local resolve = function(name: string): buffer?
|
||||
if name == "PALETTE.DAT" then
|
||||
return buffer.fromstring(Fx.palette)
|
||||
elseif name == "LOOKUP.DAT" then
|
||||
return buffer.fromstring(Fx.lookup)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
local sh = assert(Art.decode(Bytes.reader(data), { resolve = resolve, native = true, shade = Fx.shade.n, pal = Fx.remap.pal }))
|
||||
t:check(sh.meta.lookups[1] and sh.meta.lookups[5], "palookups 1 and 5")
|
||||
t:eq(#sh.meta.altPalettes, 5, "alternate palettes")
|
||||
local c = Fx.tiles[1]
|
||||
local fr = (sh.frames :: any)[1].image
|
||||
local bad = 0
|
||||
for k = 0, c.w * c.h - 1 do
|
||||
local idx = string.byte(c.idx, k + 1)
|
||||
local want = if idx == 255 then 255 else string.byte(Fx.shade.table, string.byte(Fx.remap.table, idx + 1) + 1)
|
||||
if buffer.readu8(fr.pixels, k) ~= want then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, "palookup then shade")
|
||||
local _, e = Art.decode(Bytes.reader(data), { resolve = resolve, pal = 9 })
|
||||
t:check(e and e.code == Err.ARG, "missing palookup")
|
||||
local grey = assert(Art.decode(Bytes.reader(data)))
|
||||
t:eq(grey.meta.source.palette, false, "no palette: grey ramp")
|
||||
end
|
||||
|
||||
-- write: native round trip is byte exact; rgba8 maps back through the palette
|
||||
do
|
||||
local again = assert(Art.encode(nat))
|
||||
t:eq(buffer.tostring(again), Fx.art, "native round trip")
|
||||
local again2 = assert(Art.encode(img))
|
||||
t:eq(buffer.tostring(again2), Fx.art, "rgba8 round trip through the palette")
|
||||
local pref = assert(Art.encode(nat, { buildart = true }))
|
||||
t:eq(buffer.readstring(pref, 0, 8), "BUILDART", "BUILDART prefix")
|
||||
local back = assert(Art.decode(Bytes.reader(pref), { native = true }))
|
||||
t:check(back.meta.source.buildart, "prefix read")
|
||||
t:eq(buffer.tostring(assert(Art.encode(back, { buildart = false }))), Fx.art, "prefix round trip")
|
||||
-- a single image becomes one tile
|
||||
local one = assert(Art.encode((img.frames :: any)[4].image, { palette = img.meta.palette, tileStart = 100 }))
|
||||
local d1 = assert(Art.decode(Bytes.reader(one), { native = true }))
|
||||
t:eq(d1.meta.source.tileStart, 100, "single tile number")
|
||||
t:eq(buffer.tostring(d1.pixels), Fx.tiles[4].idx, "single tile indices")
|
||||
local _, e = Art.encode((img.frames :: any)[1].image)
|
||||
t:check(e and e.code == Err.ARG, "rgba8 without a palette")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = Art.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.art, 1, 10))))
|
||||
t:check(e and e.code == Err.TRUNCATED, "short header")
|
||||
local _, e2 = Art.decode(Bytes.reader(data), { maxPixels = 10 })
|
||||
t:check(e2 and e2.code == Err.LIMIT, "limit")
|
||||
local cut = assert(Art.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.art, 1, #Fx.art - 100)))))
|
||||
t:check(cut.meta.truncated, "truncated pixels flagged")
|
||||
end
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Art.decode(Bytes.reader(b), { palette = Fx.palette })
|
||||
end, { Fx.art }, { mutations = 300 })
|
||||
t:done()
|
||||
@@ -0,0 +1,203 @@
|
||||
-- model/buildmap tests against an independent Python reading and Ken Silverman's map2stl (tools/fixtures/buildmap.py):
|
||||
-- raw tables, loops, slope heights, floor / wall areas and centroids of the Scene, sprites, v8 / v9, write round trips.
|
||||
local T = require("@tests/T")
|
||||
local BuildMap = require("@rosetta/model/buildmap")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Fx = require("@tests/gen/buildmap")
|
||||
|
||||
local t = T.suite("buildmap")
|
||||
t:eq(BuildMap.name, "buildmap", "name")
|
||||
t:check(BuildMap.caps.decode and BuildMap.caps.encode, "caps")
|
||||
|
||||
local function near(a: number, b: number, tol: number): boolean
|
||||
return math.abs(a - b) <= tol * math.max(1, math.abs(b))
|
||||
end
|
||||
|
||||
-- area and area-weighted centroid of the triangles of the given primitive kinds, in map2stl's frame (x / 512, y / 512,
|
||||
-- z / 8192, z down)
|
||||
local function stats(scene: Types.Scene, kinds: { [string]: boolean }): (number, { number })
|
||||
local area, cx, cy, cz = 0, 0, 0, 0
|
||||
for _, prim in scene.meshes[1].primitives do
|
||||
if kinds[prim.extras.build.kind] then
|
||||
local get = assert(Types.Scene.reader(prim.attributes.POSITION))
|
||||
for i = 0, prim.attributes.POSITION.count - 1, 3 do
|
||||
local p: { { number } } = {}
|
||||
for k = 0, 2 do
|
||||
p[k + 1] = { get(i + k, 0), -get(i + k, 1), -get(i + k, 2) }
|
||||
end
|
||||
local ux, uy, uz = p[2][1] - p[1][1], p[2][2] - p[1][2], p[2][3] - p[1][3]
|
||||
local vx, vy, vz = p[3][1] - p[1][1], p[3][2] - p[1][2], p[3][3] - p[1][3]
|
||||
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
||||
local a = 0.5 * math.sqrt(nx * nx + ny * ny + nz * nz)
|
||||
area += a
|
||||
cx += a * (p[1][1] + p[2][1] + p[3][1]) / 3
|
||||
cy += a * (p[1][2] + p[2][2] + p[3][2]) / 3
|
||||
cz += a * (p[1][3] + p[2][3] + p[3][3]) / 3
|
||||
end
|
||||
end
|
||||
end
|
||||
return area, if area > 0 then { cx / area, cy / area, cz / area } else { 0, 0, 0 }
|
||||
end
|
||||
|
||||
local SECTOR_FIELDS = { "wallptr", "wallnum", "ceilingz", "floorz", "ceilingstat", "floorstat", "ceilingpicnum", "ceilingheinum", "ceilingshade", "ceilingpal", "ceilingxpanning", "ceilingypanning", "floorpicnum", "floorheinum", "floorshade", "floorpal", "floorxpanning", "floorypanning", "visibility", "filler", "lotag", "hitag", "extra" }
|
||||
local WALL_FIELDS = { "x", "y", "point2", "nextwall", "nextsector", "cstat", "picnum", "overpicnum", "shade", "pal", "xrepeat", "yrepeat", "xpanning", "ypanning", "lotag", "hitag", "extra" }
|
||||
local SPRITE_FIELDS = { "x", "y", "z", "cstat", "picnum", "shade", "pal", "clipdist", "filler", "xrepeat", "yrepeat", "xoffset", "yoffset", "sectnum", "statnum", "ang", "owner", "xvel", "yvel", "zvel", "lotag", "hitag", "extra" }
|
||||
|
||||
for _, m in Fx.maps do
|
||||
local data = buffer.fromstring(m.data)
|
||||
local label = m.name
|
||||
t:check(BuildMap.sniff(Bytes.reader(data)) >= 0.5, label .. ": sniff")
|
||||
local scene, err = BuildMap.decode(Bytes.reader(data), { scale = 1 / 512, skyWalls = true })
|
||||
t:check(scene, label .. ": " .. tostring(err))
|
||||
local s = scene :: Types.Scene
|
||||
local map = s.meta.build
|
||||
t:eq(map.version, m.version, label .. ": version")
|
||||
t:eq(#map.sectors, m.counts.sectors, label .. ": sectors")
|
||||
t:eq(#map.walls, m.counts.walls, label .. ": walls")
|
||||
t:eq(#map.sprites, m.counts.sprites, label .. ": sprites")
|
||||
for _, part in { { map.sectors, SECTOR_FIELDS, "sector" }, { map.walls, WALL_FIELDS, "wall" }, { map.sprites, SPRITE_FIELDS, "sprite" } } do
|
||||
for _, f in part[2] :: { string } do
|
||||
local sum = 0
|
||||
for _, rec in part[1] :: { any } do
|
||||
sum += rec[f]
|
||||
end
|
||||
t:eq(sum, m.sums[part[3]][f], label .. ": " .. part[3] .. "." .. f .. " sum")
|
||||
end
|
||||
end
|
||||
for si, want in m.loops do
|
||||
local got = BuildMap.loops(map, si - 1)
|
||||
t:eq(#got, #want, label .. ": sector " .. (si - 1) .. " loops")
|
||||
for li, l in want do
|
||||
t:eq(table.concat(got[li], ","), table.concat(l, ","), label .. ": sector " .. (si - 1) .. " loop " .. li)
|
||||
end
|
||||
end
|
||||
for _, z in m.z do
|
||||
t:check(near(BuildMap.slopeZ(map, z.sector, true, z.x, z.y), z.floor, 1e-9), label .. ": floor z")
|
||||
t:check(near(BuildMap.slopeZ(map, z.sector, false, z.x, z.y), z.ceiling, 1e-9), label .. ": ceiling z")
|
||||
end
|
||||
if m.stl then
|
||||
local fa, fc = stats(s, { floor = true, ceiling = true })
|
||||
local wa, wc = stats(s, { wall = true })
|
||||
t:check(near(fa, m.stl.floorArea, 1e-4), string.format("%s: floor area %.5f vs map2stl %.5f", label, fa, m.stl.floorArea))
|
||||
t:check(near(wa, m.stl.wallArea, 1e-4), string.format("%s: wall area %.5f vs map2stl %.5f", label, wa, m.stl.wallArea))
|
||||
for k = 1, 3 do
|
||||
t:check(near(fc[k], m.stl.floorCentroid[k], 1e-4), label .. ": floor centroid " .. k)
|
||||
t:check(near(wc[k], m.stl.wallCentroid[k], 1e-4), label .. ": wall centroid " .. k)
|
||||
end
|
||||
end
|
||||
-- sprites: nodes with their tile, in world units
|
||||
local sprites = 0
|
||||
for _, node in s.nodes do
|
||||
local b = node.extras and node.extras.build
|
||||
if b and b.sprite then
|
||||
sprites += 1
|
||||
t:eq(b.tile, b.sprite.picnum, label .. ": sprite tile")
|
||||
local tr = node.translation :: { number }
|
||||
t:check(near(tr[1], b.sprite.x / 512, 1e-9) and near(tr[2], -b.sprite.y / 512, 1e-9) and near(tr[3], -b.sprite.z / 8192, 1e-9), label .. ": sprite position")
|
||||
end
|
||||
end
|
||||
t:eq(sprites, m.counts.sprites, label .. ": sprite nodes")
|
||||
-- write back
|
||||
local again = assert(BuildMap.encode(s))
|
||||
t:eq(buffer.tostring(again), m.data, label .. ": round trip")
|
||||
t:eq(buffer.tostring(assert(BuildMap.encode(map))), m.data, label .. ": round trip from the map table")
|
||||
end
|
||||
|
||||
-- details
|
||||
do
|
||||
local rooms = assert(BuildMap.decode(Bytes.reader(buffer.fromstring(Fx.maps[1].data))))
|
||||
local kinds: { [string]: number } = {}
|
||||
for _, prim in rooms.meshes[1].primitives do
|
||||
local k = prim.extras.build.kind
|
||||
kinds[k] = (kinds[k] or 0) + prim.attributes.POSITION.count
|
||||
t:check(rooms.materials[prim.material :: number].baseColorTexture ~= nil, "material texture")
|
||||
end
|
||||
t:check(kinds.floor and kinds.ceiling and kinds.wall and kinds.masked, "floor, ceiling, wall and masked parts")
|
||||
t:eq(rooms.meta.up, "z", "z up")
|
||||
-- facing: floors up, ceilings down, the hall's north wall (Build y = 0) towards the hall (world -y)
|
||||
local facing = 0
|
||||
for _, prim in rooms.meshes[1].primitives do
|
||||
local kind = prim.extras.build.kind
|
||||
local pos = assert(Types.Scene.reader(prim.attributes.POSITION))
|
||||
local nrm = assert(Types.Scene.reader(prim.attributes.NORMAL))
|
||||
for i = 0, prim.attributes.POSITION.count - 1 do
|
||||
if kind == "floor" and nrm(i, 2) <= 0 or kind == "ceiling" and nrm(i, 2) >= 0 then
|
||||
facing += 1
|
||||
elseif kind == "wall" and pos(i, 1) == 0 and math.abs(nrm(i, 0)) < 1e-6 and math.abs(nrm(i, 1) + 1) > 1e-6 then
|
||||
facing += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(facing, 0, "faces point into the sectors")
|
||||
t:eq(rooms.nodes[2].name, "start", "start node")
|
||||
-- tile sizes and images from opts.tiles
|
||||
local img = Types.Image.new(32, 16)
|
||||
local withTiles = assert(BuildMap.decode(Bytes.reader(buffer.fromstring(Fx.maps[1].data)), { tiles = { [77] = img } }))
|
||||
local found = false
|
||||
for _, tex in withTiles.textures do
|
||||
if tex.extras and tex.extras.build.tile == 77 then
|
||||
found = tex.image == img
|
||||
end
|
||||
end
|
||||
t:check(found, "tile image attached")
|
||||
-- parallax: walls between two sky ceilings left out unless skyWalls
|
||||
local skyData = buffer.fromstring(Fx.maps[2].data)
|
||||
local a = assert(BuildMap.decode(Bytes.reader(skyData)))
|
||||
local b = assert(BuildMap.decode(Bytes.reader(skyData), { skyWalls = true }))
|
||||
local function wallVerts(sc: Types.Scene): number
|
||||
local n = 0
|
||||
for _, prim in sc.meshes[1].primitives do
|
||||
if prim.extras.build.kind == "wall" then
|
||||
n += prim.attributes.POSITION.count
|
||||
end
|
||||
end
|
||||
return n
|
||||
end
|
||||
t:check(wallVerts(a) < wallVerts(b), "sky walls dropped")
|
||||
-- v9: room over room fields; linked floors / ceilings not drawn
|
||||
local yax = assert(BuildMap.decode(Bytes.reader(buffer.fromstring(Fx.maps[3].data))))
|
||||
local ym = yax.meta.build
|
||||
t:eq(ym.sectors[1].ceilingbunch, 3, "yax ceiling bunch")
|
||||
t:eq(ym.sectors[2].floorbunch, 3, "yax floor bunch")
|
||||
t:eq(ym.walls[1].yaxceilingwall, 4, "yax ceiling wall")
|
||||
t:eq(ym.walls[6].yaxfloorwall, 0, "yax floor wall")
|
||||
local flats = 0
|
||||
for _, prim in yax.meshes[1].primitives do
|
||||
if prim.extras.build.kind == "floor" or prim.extras.build.kind == "ceiling" then
|
||||
flats += prim.attributes.POSITION.count
|
||||
end
|
||||
end
|
||||
t:eq(flats, 12, "yax-linked ceiling / floor skipped (two squares of two triangles left)")
|
||||
t:check(Fx.maps[4].name == "trailing" and buffer.len(assert(BuildMap.parse(Bytes.reader(buffer.fromstring(Fx.maps[4].data)))).trailing :: buffer) == 12, "trailing bytes kept")
|
||||
-- version override
|
||||
local v8 = assert(BuildMap.encode(rooms, { version = 8 }))
|
||||
t:eq(buffer.readu32(v8, 0), 8, "version override")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local function code(s: string): string?
|
||||
local _, e = BuildMap.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("BLM\26" .. string.rep("\0", 40)), Err.UNSUPPORTED, "Blood map")
|
||||
t:eq(code("\6\0\0\0" .. string.rep("\0", 40)), Err.UNSUPPORTED, "version 6")
|
||||
t:eq(code("// Game: Quake\n{\n\"classname\" \"worldspawn\"\n}\n"), Err.CORRUPT, "Quake map")
|
||||
t:eq(BuildMap.sniff(Bytes.reader(buffer.fromstring("// Game: Quake\n{\n\"classname\" \"worldspawn\"\n}\n"))), 0, "sniff Quake map")
|
||||
t:eq(code(string.sub(Fx.maps[1].data, 1, 100)), Err.TRUNCATED, "truncated")
|
||||
local _, e = BuildMap.encode({ sectors = {}, walls = { { x = 1 } }, sprites = {} })
|
||||
t:check(e and e.code == Err.ARG, "encode: missing field")
|
||||
local _, e2 = BuildMap.encode(42)
|
||||
t:check(e2 and e2.code == Err.ARG, "encode: not a map")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, m in Fx.maps do
|
||||
table.insert(samples, m.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return BuildMap.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 200 })
|
||||
t:done()
|
||||
@@ -1902,3 +1902,33 @@ do
|
||||
jitTune(false)
|
||||
end
|
||||
end
|
||||
do
|
||||
-- Build engine (ws-build): an ART of 64 random 128 x 128 tiles to rgba8 through a palette (MB/s of rgba8 output),
|
||||
-- and the two-room map of tools/fixtures/buildmap.py to a Scene (MB/s of map bytes)
|
||||
local Art = require("@rosetta/image/art")
|
||||
local BuildMap = require("@rosetta/model/buildmap")
|
||||
local AF = require("@tests/gen/art")
|
||||
local BF = require("@tests/gen/buildmap")
|
||||
local rnd = 12345
|
||||
local frames: { Types.Frame } = {}
|
||||
for i = 1, 64 do
|
||||
local img = Types.Image.new(128, 128, "gray8")
|
||||
for k = 0, 128 * 128 - 1 do
|
||||
rnd = (rnd * 1103515245 + 12345) % 2147483648
|
||||
buffer.writeu8(img.pixels, k, rnd % 256)
|
||||
end
|
||||
img.meta.tile = i - 1
|
||||
frames[i] = { image = img, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }
|
||||
end
|
||||
local top = Types.Image.new(1, 1, "gray8")
|
||||
top.frames = frames
|
||||
local art = assert(Art.encode(top))
|
||||
local pal = buffer.fromstring(AF.palette)
|
||||
bench("art decode 64 x 128x128 -> rgba8", 3, 64 * 128 * 128 * 4, function()
|
||||
Art.decode(Bytes.reader(art), { palette = pal })
|
||||
end)
|
||||
local map = buffer.fromstring(BF.maps[1].data)
|
||||
bench("buildmap rooms -> Scene", 20, buffer.len(map), function()
|
||||
BuildMap.decode(Bytes.reader(map))
|
||||
end)
|
||||
end
|
||||
@@ -0,0 +1,106 @@
|
||||
# Build engine reference tools for the art / buildmap fixtures: arttool (Jonathon Fowler, Artistic License 2.0) and
|
||||
# map2stl (Ken Silverman) compiled from the nix eduke32 source (nixpkgs#eduke32.src, never committed) with g++ and a
|
||||
# small compat.h stub written here: stdio and a few B* aliases, a NULL-safe Bfclose, and a DO_FREE_AND_NULL that leaks
|
||||
# (arttool's Bstring copy constructor frees an uninitialised pointer). Binaries are cached in tests/gen/.buildtools/.
|
||||
import os, subprocess
|
||||
|
||||
STUB = r"""
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#define Bassert assert
|
||||
#define Bfclose(f) ((f) ? fclose(f) : 0)
|
||||
#define Bfileno fileno
|
||||
#define Bfopen fopen
|
||||
#define Bfprintf fprintf
|
||||
#define Bfputc fputc
|
||||
#define Bfread fread
|
||||
#define Bfseek fseek
|
||||
#define Bfstat fstat
|
||||
#define Bftell ftell
|
||||
#define Bfwrite fwrite
|
||||
#define Bmalloc malloc
|
||||
#define Bprintf printf
|
||||
#define Brealloc realloc
|
||||
#define Brewind rewind
|
||||
#define Bstat stat
|
||||
#define Bstrcat strcat
|
||||
#define Bstrcmp strcmp
|
||||
#define Bstrdup strdup
|
||||
#define Bstrlen strlen
|
||||
#define BFILE FILE
|
||||
#define DO_FREE_AND_NULL(x) do { (x) = NULL; } while (0)
|
||||
#define EDUKE32_PREDICT_FALSE(x) (x)
|
||||
#define ATTRIBUTE(x)
|
||||
"""
|
||||
|
||||
|
||||
def tools(ctx):
|
||||
"""{"arttool": path, "map2stl": path}"""
|
||||
out = os.path.join(ctx.gen, ".buildtools")
|
||||
paths = {n: os.path.join(out, n) for n in ("arttool", "map2stl")}
|
||||
if all(os.path.exists(p) for p in paths.values()):
|
||||
return paths
|
||||
os.makedirs(out, exist_ok=True)
|
||||
src = subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "nixpkgs#eduke32.src"], capture_output=True,
|
||||
check=True).stdout.decode().strip().split("\n")[-1]
|
||||
stub = os.path.join(out, "stub")
|
||||
os.makedirs(stub, exist_ok=True)
|
||||
with open(os.path.join(stub, "compat.h"), "w") as f:
|
||||
f.write(STUB)
|
||||
for name, path in paths.items():
|
||||
ctx.nix("gcc", ["g++", "-O1", "-w", "-funsigned-char", "-I" + stub, os.path.join(src, "source/tools/src/%s.cpp" % name),
|
||||
"-o", path])
|
||||
return paths
|
||||
|
||||
|
||||
def pcx_write(w, h, idx, pal):
|
||||
"""8-bit RLE PCX (version 5) of row-major indices with a 256 x RGB palette, as arttool reads it"""
|
||||
bpl = w + (w & 1)
|
||||
hd = bytearray(128)
|
||||
hd[0:4] = bytes([10, 5, 1, 8])
|
||||
hd[8:10] = (w - 1).to_bytes(2, "little")
|
||||
hd[10:12] = (h - 1).to_bytes(2, "little")
|
||||
hd[65] = 1
|
||||
hd[66:68] = bpl.to_bytes(2, "little")
|
||||
hd[68] = 1
|
||||
out = bytearray(hd)
|
||||
for y in range(h):
|
||||
row = list(idx[y * w:(y + 1) * w]) + [0] * (bpl - w)
|
||||
i = 0
|
||||
while i < bpl:
|
||||
v, n = row[i], 1
|
||||
while i + n < bpl and n < 63 and row[i + n] == v:
|
||||
n += 1
|
||||
if n > 1 or v >= 0xC0:
|
||||
out.append(0xC0 | n)
|
||||
out.append(v)
|
||||
i += n
|
||||
out.append(12)
|
||||
out += bytes(pal)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def pcx_read(data):
|
||||
"""(w, h, row-major indices, 768-byte palette) of an 8-bit RLE PCX"""
|
||||
w = int.from_bytes(data[8:10], "little") - int.from_bytes(data[4:6], "little") + 1
|
||||
h = int.from_bytes(data[10:12], "little") - int.from_bytes(data[6:8], "little") + 1
|
||||
bpl = int.from_bytes(data[66:68], "little")
|
||||
raw, p = bytearray(), 128
|
||||
while len(raw) < bpl * h:
|
||||
c = data[p]
|
||||
p += 1
|
||||
if c >= 0xC0:
|
||||
raw += bytes([data[p]]) * (c & 63)
|
||||
p += 1
|
||||
else:
|
||||
raw.append(c)
|
||||
idx = bytearray()
|
||||
for y in range(h):
|
||||
idx += raw[y * bpl:y * bpl + w]
|
||||
return w, h, bytes(idx), data[-768:]
|
||||
@@ -0,0 +1,117 @@
|
||||
# Build ART fixtures (tools/fixtures.py). No Duke 3D data (proprietary): a random PALETTE.DAT (256 distinct 6-bit
|
||||
# colours, 32 shade tables, a translucency table) and LOOKUP.DAT (palookups 1 and 5, five alternate palettes) are
|
||||
# made here. References, from the nix eduke32 source (tools/fixtures/_build_ref.py):
|
||||
# - arttool create / addtile builds TILES000.ART (tiles 10..15, two of them empty) from 8-bit PCX files written
|
||||
# here, with centre offsets and animation fields; `arttool info` gives the per-tile fields;
|
||||
# - arttool exporttile writes each tile back as PCX through palette.dat (<< 2 scaling): its RGB is the reference;
|
||||
# - an independent Python reading of the ART bytes (BUILDINF.TXT layout) gives each tile's column-major indices.
|
||||
# tests/gen/art.luau { art, palette, lookup, start, last, tiles = { { tile, w, h, idx, rgb, xofs, yofs, animType,
|
||||
# animFrames, animSpeed, picanm } }, shade = { n, table }, remap = { pal, table } }
|
||||
import os, shutil, struct, subprocess, tempfile, importlib.util
|
||||
|
||||
_spec = importlib.util.spec_from_file_location("_build_ref", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_build_ref.py"))
|
||||
B = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(B)
|
||||
|
||||
INPUTS = ["tools/fixtures/_build_ref.py"]
|
||||
|
||||
|
||||
def palette_dat(rng):
|
||||
cols, seen = [], set()
|
||||
while len(cols) < 256:
|
||||
c = (rng.randrange(64), rng.randrange(64), rng.randrange(64))
|
||||
if c not in seen:
|
||||
seen.add(c)
|
||||
cols.append(c)
|
||||
pal = bytes(v for c in cols for v in c)
|
||||
shades = rng.randbytes(32 * 256)
|
||||
return pal + struct.pack("<H", 32) + shades + rng.randbytes(65536)
|
||||
|
||||
|
||||
def lookup_dat(rng):
|
||||
out = bytearray([2])
|
||||
for n in (1, 5):
|
||||
out.append(n)
|
||||
out += rng.randbytes(256)
|
||||
for _ in range(5):
|
||||
out += bytes(rng.randrange(64) for _ in range(768))
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def parse_art(data):
|
||||
"""independent ART reading: (first, last, [(w, h, picanm, column-major bytes)])"""
|
||||
ver, _, first, last = struct.unpack_from("<4I", data, 0)
|
||||
assert ver == 1
|
||||
n = last - first + 1
|
||||
xs = struct.unpack_from("<%dh" % n, data, 16)
|
||||
ys = struct.unpack_from("<%dh" % n, data, 16 + 2 * n)
|
||||
anm = struct.unpack_from("<%dI" % n, data, 16 + 4 * n)
|
||||
p, tiles = 16 + 8 * n, []
|
||||
for i in range(n):
|
||||
size = xs[i] * ys[i]
|
||||
tiles.append((xs[i], ys[i], anm[i], data[p:p + size]))
|
||||
p += size
|
||||
return first, last, tiles
|
||||
|
||||
|
||||
SPECS = [ # tile, w, h, addtile options
|
||||
(10, 16, 8, ["-x", "3", "-y", "-2"]),
|
||||
(11, 1, 5, ["-ann", "3", "-ant", "2", "-ans", "4"]),
|
||||
(13, 33, 17, ["-x", "-7", "-ann", "63", "-ant", "1", "-ans", "15"]),
|
||||
(15, 64, 64, ["-y", "100", "-ant", "3"]),
|
||||
]
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(1997)
|
||||
tools = B.tools(ctx)
|
||||
pal_dat = palette_dat(rng)
|
||||
pal8 = bytes(v << 2 for v in pal_dat[:768])
|
||||
lookup = lookup_dat(rng)
|
||||
tmp = tempfile.mkdtemp(prefix="rosetta-art-")
|
||||
try:
|
||||
run = lambda *a: subprocess.run([tools["arttool"]] + list(a), cwd=tmp, capture_output=True, check=True).stdout.decode()
|
||||
run("create", "-o", "10", "-n", "6")
|
||||
made = {}
|
||||
for tile, w, h, extra in SPECS:
|
||||
idx = bytes(rng.randrange(256) if rng.random() < 0.85 else 255 for _ in range(w * h))
|
||||
made[tile] = idx
|
||||
with open(os.path.join(tmp, "t%d.pcx" % tile), "wb") as f:
|
||||
f.write(B.pcx_write(w, h, idx, pal8))
|
||||
run("addtile", *extra, str(tile), "t%d.pcx" % tile)
|
||||
art = open(os.path.join(tmp, "tiles000.art"), "rb").read()
|
||||
with open(os.path.join(tmp, "palette.dat"), "wb") as f:
|
||||
f.write(pal_dat)
|
||||
info = {}
|
||||
for line in run("info").split("\n"):
|
||||
line = line.strip()
|
||||
if line.startswith("Tile "):
|
||||
head, rest = line.split(": ", 1)
|
||||
t = int(head.split()[1])
|
||||
size, rest = rest.split(" ", 1)
|
||||
fields = dict(kv.split(": ") for kv in rest.split(", "))
|
||||
info[t] = (size, {k: int(v) for k, v in fields.items()})
|
||||
first, last, parsed = parse_art(art)
|
||||
tiles = []
|
||||
for i, (w, h, anm, cols) in enumerate(parsed):
|
||||
t = first + i
|
||||
size, f = info[t]
|
||||
assert size == "%dx%d" % (w, h)
|
||||
e = {"tile": t, "w": w, "h": h, "picanm": anm, "xofs": f["Xofs"], "yofs": f["Yofs"], "animType": f["AnimType"],
|
||||
"animFrames": f["AnimFrames"], "animSpeed": f["AnimSpeed"]}
|
||||
if w * h > 0:
|
||||
idx = bytes(cols[x * h + y] for y in range(h) for x in range(w))
|
||||
assert idx == made[t]
|
||||
run("exporttile", str(t))
|
||||
pw, ph, pidx, ppal = B.pcx_read(open(os.path.join(tmp, "tile%04d.pcx" % t), "rb").read())
|
||||
assert (pw, ph, pidx) == (w, h, idx)
|
||||
e["idx"] = idx
|
||||
e["rgb"] = bytes(ppal[v * 3 + k] for v in pidx for k in range(3))
|
||||
tiles.append(e)
|
||||
finally:
|
||||
shutil.rmtree(tmp)
|
||||
ctx.emit("art", {
|
||||
"art": art, "palette": pal_dat, "lookup": lookup, "start": first, "last": last, "tiles": tiles,
|
||||
"shade": {"n": 7, "table": pal_dat[770 + 7 * 256:770 + 8 * 256]},
|
||||
"remap": {"pal": 5, "table": lookup[1 + 257 + 1:1 + 257 + 257]},
|
||||
})
|
||||
@@ -0,0 +1,223 @@
|
||||
# Build .map fixtures (tools/fixtures.py). No Duke 3D / Shadow Warrior maps (proprietary): maps are written here from
|
||||
# BUILDINF.TXT (v7 layout; v8 / v9 share it), red walls linked by matching reversed wall segments. References:
|
||||
# - an independent Python reading of each map: field sums per table, every sector's loops, and the floor / ceiling
|
||||
# z of every sector at each of its wall points by the BUILDINF slope formula;
|
||||
# - Ken Silverman's map2stl (eduke32 source, tools/fixtures/_build_ref.py) turns the v7 bytes of each map into an
|
||||
# STL: total area and area-weighted centroid of its floor / ceiling triangles and of its wall triangles.
|
||||
# tests/gen/buildmap.luau { maps = { { name, data, version, counts = { sectors, walls, sprites }, sums = { sector =
|
||||
# { field = sum }, wall, sprite }, loops = { { { wall } } }, z = { { sector, x, y, floor,
|
||||
# ceiling } }, stl = { floorArea, wallArea, floorCentroid = { x, y, z }, wallCentroid } } } }
|
||||
import os, struct, subprocess, tempfile, shutil, importlib.util
|
||||
|
||||
_spec = importlib.util.spec_from_file_location("_build_ref", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_build_ref.py"))
|
||||
B = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(B)
|
||||
|
||||
INPUTS = ["tools/fixtures/_build_ref.py"]
|
||||
|
||||
SECTOR = [("wallptr", "h"), ("wallnum", "h"), ("ceilingz", "i"), ("floorz", "i"), ("ceilingstat", "h"), ("floorstat", "h"),
|
||||
("ceilingpicnum", "h"), ("ceilingheinum", "h"), ("ceilingshade", "b"), ("ceilingpal", "B"), ("ceilingxpanning", "B"),
|
||||
("ceilingypanning", "B"), ("floorpicnum", "h"), ("floorheinum", "h"), ("floorshade", "b"), ("floorpal", "B"),
|
||||
("floorxpanning", "B"), ("floorypanning", "B"), ("visibility", "B"), ("filler", "B"), ("lotag", "h"), ("hitag", "h"),
|
||||
("extra", "h")]
|
||||
WALL = [("x", "i"), ("y", "i"), ("point2", "h"), ("nextwall", "h"), ("nextsector", "h"), ("cstat", "h"), ("picnum", "h"),
|
||||
("overpicnum", "h"), ("shade", "b"), ("pal", "B"), ("xrepeat", "B"), ("yrepeat", "B"), ("xpanning", "B"),
|
||||
("ypanning", "B"), ("lotag", "h"), ("hitag", "h"), ("extra", "h")]
|
||||
SPRITE = [("x", "i"), ("y", "i"), ("z", "i"), ("cstat", "h"), ("picnum", "h"), ("shade", "b"), ("pal", "B"), ("clipdist", "B"),
|
||||
("filler", "B"), ("xrepeat", "B"), ("yrepeat", "B"), ("xoffset", "b"), ("yoffset", "b"), ("sectnum", "h"),
|
||||
("statnum", "h"), ("ang", "h"), ("owner", "h"), ("xvel", "h"), ("yvel", "h"), ("zvel", "h"), ("lotag", "h"),
|
||||
("hitag", "h"), ("extra", "h")]
|
||||
FMT = {name: "<" + "".join(t for _, t in fields) for name, fields in (("sector", SECTOR), ("wall", WALL), ("sprite", SPRITE))}
|
||||
assert [struct.calcsize(FMT[k]) for k in ("sector", "wall", "sprite")] == [40, 32, 44]
|
||||
|
||||
|
||||
class MapBuilder:
|
||||
def __init__(self, rng):
|
||||
self.rng = rng
|
||||
self.sectors, self.walls, self.sprites = [], [], []
|
||||
|
||||
def sector(self, loops, **f):
|
||||
"""loops: outer loop first (positive shoelace in Build's y-down coordinates), then holes (negative)"""
|
||||
start = len(self.walls)
|
||||
for li, loop in enumerate(loops):
|
||||
area = sum(loop[i][0] * loop[(i + 1) % len(loop)][1] - loop[(i + 1) % len(loop)][0] * loop[i][1] for i in range(len(loop)))
|
||||
assert (area > 0) == (li == 0), "loop orientation"
|
||||
base = len(self.walls)
|
||||
for i, (x, y) in enumerate(loop):
|
||||
self.walls.append({"x": x, "y": y, "point2": base + (i + 1) % len(loop), "nextwall": -1, "nextsector": -1,
|
||||
"cstat": 0, "picnum": self.rng.randrange(1, 40), "overpicnum": 0, "shade": self.rng.randrange(-8, 20),
|
||||
"pal": 0, "xrepeat": 8, "yrepeat": 8, "xpanning": self.rng.randrange(256),
|
||||
"ypanning": self.rng.randrange(256), "lotag": 0, "hitag": 0, "extra": -1})
|
||||
s = {k: 0 for k, _ in SECTOR}
|
||||
s.update({"wallptr": start, "wallnum": len(self.walls) - start, "extra": -1, "ceilingpicnum": self.rng.randrange(1, 40),
|
||||
"floorpicnum": self.rng.randrange(1, 40), "visibility": self.rng.randrange(256)})
|
||||
s.update(f)
|
||||
self.sectors.append(s)
|
||||
return len(self.sectors) - 1
|
||||
|
||||
def link(self):
|
||||
"""red walls: a wall a -> b of one sector and b -> a of another"""
|
||||
seg = {}
|
||||
for si, s in enumerate(self.sectors):
|
||||
for w in range(s["wallptr"], s["wallptr"] + s["wallnum"]):
|
||||
a, b = self.walls[w], self.walls[self.walls[w]["point2"]]
|
||||
seg[(a["x"], a["y"], b["x"], b["y"])] = (si, w)
|
||||
for si, s in enumerate(self.sectors):
|
||||
for w in range(s["wallptr"], s["wallptr"] + s["wallnum"]):
|
||||
a, b = self.walls[w], self.walls[self.walls[w]["point2"]]
|
||||
o = seg.get((b["x"], b["y"], a["x"], a["y"]))
|
||||
if o:
|
||||
a["nextsector"], a["nextwall"] = o
|
||||
|
||||
def sprite(self, **f):
|
||||
sp = {k: 0 for k, _ in SPRITE}
|
||||
sp.update({"xrepeat": 64, "yrepeat": 64, "clipdist": 32, "owner": -1, "picnum": self.rng.randrange(1000, 2000),
|
||||
"ang": self.rng.randrange(2048), "lotag": self.rng.randrange(100)})
|
||||
sp.update(f)
|
||||
self.sprites.append(sp)
|
||||
|
||||
def write(self, version, pos=(512, 512, -4096), ang=512, cursect=0, trailing=b""):
|
||||
out = bytearray(struct.pack("<iiiihh", version, *pos, ang, cursect))
|
||||
for name, rows, fields in (("sector", self.sectors, SECTOR), ("wall", self.walls, WALL), ("sprite", self.sprites, SPRITE)):
|
||||
out += struct.pack("<H", len(rows))
|
||||
for r in rows:
|
||||
out += struct.pack(FMT[name], *[r[k] for k, _ in fields])
|
||||
return bytes(out) + trailing
|
||||
|
||||
|
||||
def parse(data):
|
||||
"""independent reading: (version, {"sector": [...], "wall": [...], "sprite": [...]})"""
|
||||
version = struct.unpack_from("<i", data, 0)[0]
|
||||
p, tables = 20, {}
|
||||
for name, fields in (("sector", SECTOR), ("wall", WALL), ("sprite", SPRITE)):
|
||||
n = struct.unpack_from("<H", data, p)[0]
|
||||
p += 2
|
||||
size = struct.calcsize(FMT[name])
|
||||
tables[name] = [dict(zip([k for k, _ in fields], struct.unpack_from(FMT[name], data, p + i * size))) for i in range(n)]
|
||||
p += n * size
|
||||
return version, tables
|
||||
|
||||
|
||||
def slope_z(t, si, floor, x, y):
|
||||
s = t["sector"][si]
|
||||
pre = "floor" if floor else "ceiling"
|
||||
z = s[pre + "z"]
|
||||
if not s[pre + "stat"] & 2 or s[pre + "heinum"] == 0:
|
||||
return z
|
||||
w0 = t["wall"][s["wallptr"]]
|
||||
w1 = t["wall"][w0["point2"]]
|
||||
dx, dy = w1["x"] - w0["x"], w1["y"] - w0["y"]
|
||||
ln = (dx * dx + dy * dy) ** 0.5
|
||||
return z + s[pre + "heinum"] * (dx * (y - w0["y"]) - dy * (x - w0["x"])) / (ln * 256)
|
||||
|
||||
|
||||
def loops_of(t, si):
|
||||
s, out, seen = t["sector"][si], [], set()
|
||||
for start in range(s["wallptr"], s["wallptr"] + s["wallnum"]):
|
||||
if start in seen:
|
||||
continue
|
||||
loop, w = [], start
|
||||
while w not in seen:
|
||||
seen.add(w)
|
||||
loop.append(w)
|
||||
w = t["wall"][w]["point2"]
|
||||
out.append(loop)
|
||||
return out
|
||||
|
||||
|
||||
def stl_stats(stl):
|
||||
n = struct.unpack_from("<I", stl, 80)[0]
|
||||
acc = {"floor": [0.0, 0.0, 0.0, 0.0], "wall": [0.0, 0.0, 0.0, 0.0]}
|
||||
for i in range(n):
|
||||
v = struct.unpack_from("<12f", stl, 84 + i * 50)
|
||||
nz, a, b, c = v[2], v[3:6], v[6:9], v[9:12]
|
||||
ux, uy, uz = b[0] - a[0], b[1] - a[1], b[2] - a[2]
|
||||
vx, vy, vz = c[0] - a[0], c[1] - a[1], c[2] - a[2]
|
||||
cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
||||
area = 0.5 * (cx * cx + cy * cy + cz * cz) ** 0.5
|
||||
k = acc["wall" if abs(nz) < 1e-6 else "floor"]
|
||||
k[0] += area
|
||||
for d in range(3):
|
||||
k[d + 1] += area * (a[d] + b[d] + c[d]) / 3
|
||||
res = {}
|
||||
for k, (area, sx, sy, sz) in acc.items():
|
||||
res[k + "Area"] = area
|
||||
res[k + "Centroid"] = [sx / area, sy / area, sz / area] if area > 0 else [0.0, 0.0, 0.0]
|
||||
return res
|
||||
|
||||
|
||||
def rooms(rng):
|
||||
m = MapBuilder(rng)
|
||||
# a hall with a sloped platform sector inside (the hall's hole) and a red-wall opening to a sloped east room
|
||||
m.sector([[(0, 0), (4096, 0), (4096, 1024), (4096, 3072), (4096, 4096), (0, 4096)],
|
||||
[(1024, 1024), (1024, 2048), (2048, 2048), (2048, 1024)]], floorz=0, ceilingz=-32768)
|
||||
m.sector([[(4096, 1024), (6144, 1024), (6144, 3072), (4096, 3072)]], floorz=-4096, ceilingz=-24576,
|
||||
floorstat=2, floorheinum=-1024, ceilingstat=2 | 64, ceilingheinum=512, floorxpanning=17)
|
||||
m.sector([[(1024, 1024), (2048, 1024), (2048, 2048), (1024, 2048)]], floorz=-8192, ceilingz=-28672,
|
||||
floorstat=2 | 4 | 16, floorheinum=4096, ceilingstat=8 | 32)
|
||||
m.link()
|
||||
for w in m.walls:
|
||||
if (w["x"], w["y"]) == (4096, 1024) and w["nextsector"] == 1:
|
||||
w["cstat"] |= 16 | 4
|
||||
w["overpicnum"] = 77
|
||||
if w["nextsector"] == 2:
|
||||
w["cstat"] |= 2 | 8
|
||||
m.walls[0]["cstat"] |= 256
|
||||
m.sprite(x=300, y=400, z=0, sectnum=0, cstat=1)
|
||||
m.sprite(x=5000, y=2000, z=-4096, sectnum=1, cstat=16, ang=0)
|
||||
m.sprite(x=1500, y=1500, z=-8192, sectnum=2, cstat=32 | 4, ang=1024)
|
||||
return m
|
||||
|
||||
|
||||
def sky(rng):
|
||||
m = MapBuilder(rng)
|
||||
m.sector([[(0, 0), (2048, 0), (2048, 2048), (0, 2048)]], floorz=0, ceilingz=-16384, ceilingstat=1)
|
||||
m.sector([[(2048, 0), (4096, 0), (4096, 2048), (2048, 2048)]], floorz=1024, ceilingz=-20480, ceilingstat=1)
|
||||
m.link()
|
||||
m.sprite(x=1000, y=1000, z=0, sectnum=0)
|
||||
return m
|
||||
|
||||
|
||||
def yax(rng):
|
||||
m = MapBuilder(rng)
|
||||
m.sector([[(0, 0), (1024, 0), (1024, 1024), (0, 1024)]], floorz=0, ceilingz=-8192, ceilingstat=1024, ceilingxpanning=3)
|
||||
m.sector([[(0, 0), (1024, 0), (1024, 1024), (0, 1024)]], floorz=-8192, ceilingz=-16384, floorstat=1024, floorxpanning=3)
|
||||
m.walls[0]["cstat"] |= 1024
|
||||
m.walls[0]["lotag"] = 4
|
||||
m.walls[5]["cstat"] |= 2048
|
||||
m.walls[5]["extra"] = 0
|
||||
return m
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(1996)
|
||||
tools = B.tools(ctx)
|
||||
specs = [("rooms", rooms(rng), 7, b""), ("sky", sky(rng), 8, b""), ("yax", yax(rng), 9, b""),
|
||||
("trailing", rooms(rng), 7, b"EXTRA DATA\x00\x01")]
|
||||
maps = []
|
||||
tmp = tempfile.mkdtemp(prefix="rosetta-buildmap-")
|
||||
try:
|
||||
for name, m, version, trailing in specs:
|
||||
data = m.write(version, trailing=trailing)
|
||||
v, t = parse(data)
|
||||
assert v == version
|
||||
e = {"name": name, "data": data, "version": version,
|
||||
"counts": {"sectors": len(t["sector"]), "walls": len(t["wall"]), "sprites": len(t["sprite"])},
|
||||
"sums": {k: {f: sum(r[f] for r in rows) for f, _ in fields} for k, rows, fields in
|
||||
(("sector", t["sector"], SECTOR), ("wall", t["wall"], WALL), ("sprite", t["sprite"], SPRITE))},
|
||||
"loops": [loops_of(t, si) for si in range(len(t["sector"]))], "z": []}
|
||||
for si, s in enumerate(t["sector"]):
|
||||
for w in range(s["wallptr"], s["wallptr"] + s["wallnum"]):
|
||||
x, y = t["wall"][w]["x"], t["wall"][w]["y"]
|
||||
e["z"].append({"sector": si, "x": x, "y": y, "floor": float(slope_z(t, si, True, x, y)),
|
||||
"ceiling": float(slope_z(t, si, False, x, y))})
|
||||
if name != "yax":
|
||||
src, dst = os.path.join(tmp, name + ".map"), os.path.join(tmp, name + ".stl")
|
||||
with open(src, "wb") as f:
|
||||
f.write(m.write(7))
|
||||
subprocess.run([tools["map2stl"], src, dst], capture_output=True, check=True)
|
||||
e["stl"] = stl_stats(open(dst, "rb").read())
|
||||
maps.append(e)
|
||||
finally:
|
||||
shutil.rmtree(tmp)
|
||||
ctx.emit("buildmap", {"maps": maps})
|
||||
@@ -0,0 +1,38 @@
|
||||
# Docs inputs for the Build engine sections of the image / scene pages (tools/fixtures.py; tools/docs_check.py explains
|
||||
# the inputs): an ART file of three tiles and a random PALETTE.DAT written here (BUILDINF.TXT layout), and the two-room
|
||||
# map of tools/fixtures/buildmap.py.
|
||||
# tests/gen/docs_build.luau { files = { [name] = bytes }, vars = { artBytes = "TILES000.ART", ... } }
|
||||
import importlib.util, os, struct
|
||||
|
||||
INPUTS = ["tools/docs_check.py", "tools/fixtures/buildmap.py"]
|
||||
|
||||
|
||||
def mod(ctx, path, name):
|
||||
spec = importlib.util.spec_from_file_location(name, os.path.join(ctx.root, path))
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
return m
|
||||
|
||||
|
||||
def art(rng, first, tiles):
|
||||
"""tiles: [(w, h, picanm)]; column-major random indices"""
|
||||
out = bytearray(struct.pack("<4I", 1, 0, first, first + len(tiles) - 1))
|
||||
out += b"".join(struct.pack("<h", w) for w, _, _ in tiles)
|
||||
out += b"".join(struct.pack("<h", h) for _, h, _ in tiles)
|
||||
out += b"".join(struct.pack("<I", a) for _, _, a in tiles)
|
||||
for w, h, _ in tiles:
|
||||
out += bytes(rng.randrange(255) for _ in range(w * h))
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(5)
|
||||
check = mod(ctx, "tools/docs_check.py", "docs_check")
|
||||
bm = mod(ctx, "tools/fixtures/buildmap.py", "fx_buildmap")
|
||||
pal = bytes(rng.randrange(64) for _ in range(768)) + struct.pack("<H", 1) + bytes(range(256))
|
||||
files = {
|
||||
"TILES000.ART": art(rng, 0, [(16, 16, 0), (8, 4, 2 | (2 << 6) | (3 << 24)), (0, 0, 0)]),
|
||||
"PALETTE.DAT": pal,
|
||||
"E1L1.MAP": bm.rooms(rng).write(7),
|
||||
}
|
||||
check.emit_inputs(ctx, files, {"artBytes": "TILES000.ART", "paletteBytes": "PALETTE.DAT", "buildmapBytes": "E1L1.MAP"})
|
||||
Reference in new issue
Block a user