Skip to content

world: save the terrain lab's world as a bundle, with exports for other software (GB4) - #1518

Merged
transfix merged 1 commit into
masterfrom
feat/gb4-lab-save
Nov 25, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/gb4-lab-save

Conversation

@transfix

Copy link
Copy Markdown
Owner

GB4 of ARIADNE_WORLDS section 18.8: the terrain lab saves its world. Natively it writes a directory or a zip; in the browser it downloads a zip. A save can also include the terrain as a mesh and a 16-bit height map for other software. The saved world loads again as the same world, with the same hash.

What changes

  • Save in world_controls: a "Save as" field (world.save.file), a "With terrain mesh and height map" checkbox (world.save.exports), a Save button and a status line (world.save.status).
    • The button sends world.request: save, through the same request key as Regenerate and Stop. A save is not a rebuild.
    • The default name is the node's save_as prop, else its dataset's name (world). Natively that makes a directory beside the document; a name ending in .zip makes one zip. In a browser the save is always a .zip download.
  • Both hosts save the world shown: cvcGL's generated node and the headless GeneratedWorldRuntime both call the new save_generated_world. It saves the node's dataset (the bundle files GB2 already puts in the store) and writes where it went, or error: ..., to world.save.status.
  • data_save_files (data.h), and (data-save-files NAME TARGET [EXPORTS]) for programs:
    • It saves any dataset (a loaded city too) as its files, each at its path: a directory, or a zip for .zip and always in a browser.
    • It returns where it wrote, the file count and the bytes. It throws, saying why, for no dataset, a dataset with no files yet, a document at a URL, or a failed write.
    • A world saved this way loads again as data: { x: { uri: <it>, as: city_bundle } }.
  • Files for other software (world::city_bundle_exports), under export/:
    • terrain.glb: the terrain as a mesh, one mesh per surface its cells use, through the same glb writer as the bundle's other items. A terrain over 1025 samples a side is resampled over the same extent.
    • heightmap.png: 16-bit grey at full size, the north row at the top. height = min_height_m + value / 65535 * (max_height_m - min_height_m).
    • heightmap.json: the size, bounds, spacing, height range, water level and the mesh's size.
    • city_bundle_hash now leaves export/ out, so a bundle with or without its exports has one hash. write_city_bundle writes a name with a / into its subdirectory and refuses one that climbs out.
  • archive::pack_zip and archive::deflate (archive.h):
    • Zip entries are deflated when that is smaller, else stored. Names are UTF-8, and every entry carries a fixed 1980 date, so the same files make the same bytes.
    • Deflate is stb_image_write's compressor (the one its PNGs use), reached through detail::stb_zlib_compress in stb_io.cpp. No new dependency.
  • encode_png writes grey u16 at 16 bits (Sub filter, big-endian samples). Before, it converted grey u16 to RGBA u8. Every other format is unchanged.

Tests

  • ariadne_data_test:
    • Archive.PackZipRoundTripsDeflatedAndStoredEntries: unpack gives the same files in the same order; the bytes are the same every time; the deflate stream inflates; unsafe and duplicate paths throw.
    • ImageEncode.AGrey16PngKeepsItsSamples: the header says 16-bit grey; the IDAT inflates to exactly the expected filtered rows; an 8-bit read gives each sample's high byte.
  • world_bundle_v2_test, CityBundleV2.AWorldsExportsAreItsTerrainForOtherSoftware:
    • the JSON fields match the terrain, and the height map's samples sit at the right rows;
    • the glb reads back with two triangles a cell, one mesh per surface;
    • a mesh cap resamples the mesh but not the height map;
    • the hash ignores export/, and a directory with export/ loads as the same bundle.
  • ariadne_world_dataset_test:
    • WorldDataset.ASavedWorldLoadsAgainAsTheSameWorld: a generated world saved as a directory, a zip, and a directory with exports loads back each time with the same manifest hash, the same hash over its files, the same terrain and the same collide items; bad saves throw.
    • WorldDataset.AGeneratedWorldNodeSavesOnRequest: a node's save request writes a zip, then a directory with export/; the status says saved ...; the world isn't rebuilt; a file in the way gives error: ....

Locally (MSVC Release, full build): the new tests pass (ariadne_data_test Archive/ImageEncode 9 of 9, world_bundle_v2_test 10 of 10, ariadne_world_dataset_test WorldDataset 9 of 9), and the broad filter (world, ariadne, data, nav, bundle and cvcGL ariadne tests) passes 2,144 of 2,144, cvcgl_ariadne_data among them.

Stacked on #1508 (GB3 part 2): until it merges, this PR's diff also shows its commit. I'll rebase onto master once it squash-merges.

Not in this PR

  • The save runs on the frame thread, at once. A large world (thousands of metres, tens of MB) holds the frame while it deflates and writes.
  • Zips are classic zip, with no zip64: up to 65535 entries and 4 GiB.
  • GB5 (clients: a world's bytes by content address, and the seed-and-hash shortcut) is next.

… software

world_controls' Save sends world.request: save; both generated_world hosts
save the world shown (its dataset) with the new data_save_files: a directory,
or a zip for a .zip name and always in a browser (a download). With exports it
adds export/terrain.glb, a 16-bit export/heightmap.png and heightmap.json
(world::city_bundle_exports); the content hash leaves export/ out, so a saved
world loads back with the hash it was saved with.

Also: archive::pack_zip and archive::deflate (stb_image_write's compressor),
encode_png writes grey u16 at 16 bits, write_city_bundle writes subdirectories,
and (data-save-files NAME TARGET [EXPORTS]) for programs.
@transfix
transfix enabled auto-merge (squash) November 25, 2026 00:11
@transfix
transfix merged commit 66d1ed1 into master Nov 25, 2026
29 of 30 checks passed
@transfix
transfix deleted the feat/gb4-lab-save branch November 25, 2026 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant