diff --git a/.github/workflows/browser.yml b/.github/workflows/browser.yml index edbd0f3..dc16bc7 100644 --- a/.github/workflows/browser.yml +++ b/.github/workflows/browser.yml @@ -53,7 +53,7 @@ jobs: ctest --test-dir build-web --output-on-failure # The pinned fingerprint of tests/test_determinism.cpp, printed by the wasm build. node build-web/test_determinism.js | tee determinism.txt - grep -q 'fingerprint 011ed2b2f364b21b' determinism.txt + grep -q 'fingerprint 09826a6da618223a' determinism.txt - name: Assemble the site run: | diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6a6691b..8953032 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -62,7 +62,7 @@ jobs: "$RUNNER_TEMP/venv/bin/usd-recorder" replay validate replay.usda "$RUNNER_TEMP/venv/bin/usd-recorder" replay convert replay.usda steading-seed42.usdc # The replay's last state hash is the pinned fingerprint of test_determinism (seed 42, 1000 ticks). - grep -q '1000: "011ed2b2f364b21b"' replay.usda + grep -q '1000: "09826a6da618223a"' replay.usda - name: Upload the replay if: matrix.name == 'gcc-13' uses: actions/upload-artifact@v4 diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f726f0..b5b910a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,10 +52,14 @@ add_library(steading_colony STATIC src/colony/generate.cpp src/colony/realmap.cpp src/colony/systems.cpp - src/colony/view.cpp) + src/colony/view.cpp + third_party/strata_noise/src/noise.cpp) target_include_directories(steading_colony PUBLIC src) # stb_image (PNG only) reads a real map's rasters; private to realmap.cpp. target_include_directories(steading_colony SYSTEM PRIVATE third_party) +# Strata's value noise and fBm (strata/sim/noise.hpp), vendored unchanged so the +# rules stay engine-free; third_party/strata_noise/README.md says from where. +target_include_directories(steading_colony PUBLIC third_party/strata_noise/include) steading_warnings(steading_colony) # Determinism: the rules must give bit-identical results on every compiler, so # never fuse multiply-adds (world generation uses doubles). diff --git a/README.md b/README.md index 4968de8..e03f2b0 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ usd-web replay run.usdc -o site/replay # then serve Settlers, huts, hut plans, the stockpile, trees (sized by what is left on them, white in winter), stone and berries are point sets (a replay from a save uses the colours the slot's content composes to); the terrain (with winter and designated tiles) is a colour grid; wood, stone, food, settlers, housing and winters are stats. Each sample's state -hash is the colony's own `fingerprint()`, so the replay of seed 42 ends on the same `011ed2b2f364b21b` that +hash is the colony's own `fingerprint()`, so the replay of seed 42 ends on the same `09826a6da618223a` that `tests/test_determinism.cpp` pins, and `tests/test_replay.cpp` checks that recording does not change a single fingerprint. diff --git a/docs/THIRD-PARTY.md b/docs/THIRD-PARTY.md index 94d16b9..12213ef 100644 --- a/docs/THIRD-PARTY.md +++ b/docs/THIRD-PARTY.md @@ -8,7 +8,7 @@ third-party notices at that commit. | Component | Licence | How it is used | |---|---|---| -| Strata | MIT | The engine, linked statically. Its own components are listed below. | +| Strata | MIT | The engine, linked statically. Its own components are listed below. Its value noise and fBm (`noise.hpp`, `noise.cpp`) are also vendored in `third_party/strata_noise` from a later Strata commit (that directory's README says which), under the same licence. | | [OpenUSD](https://github.com/PixarAnimationStudios/OpenUSD) v26.08 | Tomorrow Open Source Technology License 1.0 (Apache-2.0 based) | The world, save and mod format. Strata's prebuilt (`openusd-v26.08-*`), shipped as shared libraries and plugins in `lib/` (and `bin/` on Windows). The full licence is `licenses/OpenUSD-LICENSE.txt` in the download. | | [oneTBB](https://github.com/uxlfoundation/oneTBB) | Apache-2.0 | OpenUSD's threading library, shipped beside it. The full licence is `licenses/oneTBB-LICENSE.txt` in the download. | | Microsoft Visual C++ runtime (Windows only) | Microsoft Visual C++ redistributable terms | `msvcp140*.dll`, `vcruntime140*.dll` and friends, copied app-locally from Visual Studio's redistributable folder, because OpenUSD is built against the dynamic C++ runtime. | diff --git a/docs/WEB.md b/docs/WEB.md index cefce14..3a3b3bf 100644 --- a/docs/WEB.md +++ b/docs/WEB.md @@ -8,7 +8,7 @@ drawn by Strata's WebGL 2 backend (Strata `docs/WEB.md`). | | Desktop (`STEADING_WITH_STRATA`) | Browser (`STEADING_WEB`) | |---|---|---| -| Rules (`src/colony`) | the same code | the same code; CI checks the pinned determinism fingerprint `011ed2b2f364b21b` in wasm under Node (ctest) and in Chromium (smoke test) | +| Rules (`src/colony`) | the same code | the same code; CI checks the pinned determinism fingerprint `09826a6da618223a` in wasm under Node (ctest) and in Chromium (smoke test) | | OpenUSD | yes (Strata `modules/usd`, `modules/save`) | **no**: OpenUSD is not built for WebAssembly | | Saves | Strata's save core (checksummed, verified, fallback, migrations) | **none**. A colony lasts until the tab is closed or reloaded, and the page says so | | Content | `content/prefabs.usda` + mods, composed through USD | core content only: the built-in `Stats` defaults (`src/colony/stats.h`) and `coreLooks()` (`src/colony/looks.h`). `tests/test_core_defaults.cpp` (desktop CI) checks that they equal the USD composition with no mods | diff --git a/docs/images/terrain-fractal-before-after.png b/docs/images/terrain-fractal-before-after.png new file mode 100644 index 0000000..7f39446 Binary files /dev/null and b/docs/images/terrain-fractal-before-after.png differ diff --git a/docs/images/terrain-fractal-game.jpg b/docs/images/terrain-fractal-game.jpg new file mode 100644 index 0000000..c2d4d64 Binary files /dev/null and b/docs/images/terrain-fractal-game.jpg differ diff --git a/src/colony/generate.cpp b/src/colony/generate.cpp index 3f916dd..988cf28 100644 --- a/src/colony/generate.cpp +++ b/src/colony/generate.cpp @@ -4,9 +4,55 @@ #include "rng.h" #include "stats.h" +#include "strata/sim/noise.hpp" namespace steading { +namespace { + +// Terrain and forest come from Strata's fractal noise (third_party/strata_noise, +// strata/sim/noise.hpp), so rock outcrops, forest edges and lake shores are +// ragged, as natural edges are, instead of the round blobs that one or two +// octaves of value noise give. noise.hpp's measured table: with octaves down +// to the finest scale that can show, gain sets an edge's fractal dimension; +// gain 0.65 gives about 1.2 (natural coastlines are about 1.1-1.3). +// +// A cell is the finest scale here. Elevation's first layer is 16 cells across: +// rock is cut from five layers (down to one cell), forest from three layers of +// its own (7 cells across down to under two). Water is cut from the first +// three layers of the same elevation (down to four cells): settlers have no +// pathfinding (systems.cpp steps greedily and slides along water), and one- +// or two-cell inlets in a shore trap them, which in a 1,000-seed run with no +// player cost about one colony in twenty. Three layers keep shores irregular +// without the small inlets, and the colonies' survival as it was. +// +// The thresholds make the average share of water, rock and forest (the forest +// mask on grass) over seeds 1-256 the old generator's: about 11%, 9.5% and 34%. +constexpr float kFractalGain = 0.65f; +constexpr int kRockOctaves = 5, kWaterOctaves = 3; + +strata::sim::FbmParams elevationNoise(uint64_t seed, int octaves) { + strata::sim::FbmParams p; + p.octaves = octaves; + p.gain = kFractalGain; + p.frequency = 1.0f / 16.0f; + p.seed = static_cast(mix64(seed ^ 0xE1E7ULL)); + return p; +} + +strata::sim::FbmParams forestNoise(uint64_t seed) { + strata::sim::FbmParams p; + p.octaves = 3; + p.gain = kFractalGain; + p.frequency = 1.0f / 7.0f; + p.seed = static_cast(mix64(seed ^ 0xF0E57ULL)); + return p; +} + +constexpr double kWaterBelow = -0.33, kRockAbove = 0.30, kForestAbove = 0.05; + +} // namespace + // The stockpile, the settlers and the colony-wide state, on a filled grid. `rng` // continues the stream that filled the grid. void settle(Colony& c, const GenParams& p, Rng& rng) { @@ -69,16 +115,19 @@ Colony generate(const GenParams& p) { Grid& g = c.grid; g.init(p.width, p.height); Rng rng{mix64(p.seed)}; + const strata::sim::FbmParams shoreP = elevationNoise(p.seed, kWaterOctaves), + rockP = elevationNoise(p.seed, kRockOctaves), forestP = forestNoise(p.seed); for (int32_t y = 0; y < g.h; ++y) for (int32_t x = 0; x < g.w; ++x) { const size_t i = g.at(x, y); - const double elevation = - 0.65 * valueNoise(p.seed, x / 12.0, y / 12.0) + 0.35 * valueNoise(p.seed + 1, x / 5.0, y / 5.0); - const double forest = valueNoise(p.seed + 7, x / 7.0, y / 7.0); + const float fx = static_cast(x), fy = static_cast(y); + const double shore = strata::sim::fbm(fx, fy, shoreP); + const double elevation = strata::sim::fbm(fx, fy, rockP); + const double forest = strata::sim::fbm(fx, fy, forestP); const int roll = rng.below(1000); - if (elevation < 0.30) { + if (shore < kWaterBelow) { g.terrain[i] = static_cast(Terrain::Water); - } else if (elevation > 0.72) { + } else if (elevation > kRockAbove) { g.terrain[i] = static_cast(Terrain::Rock); if (roll < 300) { g.resType[i] = static_cast(Resource::Stone); @@ -86,7 +135,7 @@ Colony generate(const GenParams& p) { } } else { g.terrain[i] = static_cast(Terrain::Grass); - if (forest > 0.55 && roll < 450) { + if (forest > kForestAbove && roll < 450) { g.resType[i] = static_cast(Resource::Tree); g.resAmount[i] = Rules::kTreeAmount; } else if (roll < 25) { diff --git a/tests/test_determinism.cpp b/tests/test_determinism.cpp index 81d2040..448b8aa 100644 --- a/tests/test_determinism.cpp +++ b/tests/test_determinism.cpp @@ -14,6 +14,7 @@ #include "colony/rng.h" #include "colony/schema.h" #include "colony/systems.h" +#include "strata/sim/noise.hpp" using namespace steading; @@ -206,6 +207,25 @@ int main() { } CHECK_MSG(unit, "valueNoise stays in [0, 1)"); } + SECTION("strata noise: the vendored copy gives Strata's pinned values"); + { + // World generation draws from Strata's fBm (third_party/strata_noise). These are + // the values Strata's own unit test pins (modules/sim/tests/test_sim.cpp at the + // vendored commit), so the copy is checked against the original on every compiler. + CHECK(strata::sim::latticeHash(0, 0, 0u) == 0x944fb554u); + CHECK(strata::sim::latticeHash(-5, 7, 42u) == 0x5818a332u); + strata::sim::FbmParams p; + p.seed = 1234u; + p.octaves = 6; + p.gain = 0.6f; + const float xs[4] = {0.0f, 0.37f, -12.5f, 100.25f}; + const float noise[4] = {0.688756227f, 0.254520804f, -0.0458951704f, 0.0907022059f}; + const float frac[4] = {-0.169958144f, -0.153145447f, -0.296367735f, -0.0396123827f}; + for (int i = 0; i < 4; ++i) { + CHECK(strata::sim::valueNoise(xs[i], xs[i] * 0.5f + 0.3f, 99u) == noise[i]); + CHECK(strata::sim::fbm(xs[i], 1.75f - xs[i], p) == frac[i]); + } + } SECTION("pinned fingerprint: seed 42, 64x64, 30 settlers, 1000 ticks"); { // Pins the rules' exact output on every compiler and platform CI runs. @@ -216,7 +236,7 @@ int main() { for (int t = 0; t < 1000; ++t) tick(c, d, Stats{}); const uint64_t fp = fingerprint(c); std::printf(" fingerprint %016" PRIx64 "\n", fp); - CHECK_MSG(fp == UINT64_C(0x011ed2b2f364b21b), "rules output changed"); + CHECK_MSG(fp == UINT64_C(0x09826a6da618223a), "rules output changed"); } return check::finish(); } diff --git a/tests/test_generate.cpp b/tests/test_generate.cpp index 2661909..419f95a 100644 --- a/tests/test_generate.cpp +++ b/tests/test_generate.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -58,6 +59,36 @@ int main() { CHECK(c.globals.maxSettlers == 50 && c.globals.food == 20 && c.season == Season::Summer); CHECK(c.globals.seed == 42); } + SECTION("the land's shares stay where the two-octave generator had them"); + { + // World generation moved to fractal noise (generate.cpp). The thresholds were + // chosen so that, over many seeds, the shares of water, rock and resources stay + // what the earlier generator gave (seeds 1-256, 64 x 64: water 11.1%, rock 9.5%, + // trees 15.3%, stone 2.8%, berries 1.1%), so a colony's food, wood and stone do not + // move. Averaged over 64 seeds here, with a margin. + double water = 0, rock = 0, trees = 0, stone = 0, berries = 0; + const int seeds = 64; + for (int s = 1; s <= seeds; ++s) { + GenParams p; + p.seed = static_cast(s); + p.settlers = 0; + const Grid& g = generate(p).grid; + const double n = static_cast(g.cells()) * seeds; + for (size_t i = 0; i < g.cells(); ++i) { + if (g.terrain[i] == 1) water += 1 / n; + if (g.terrain[i] == 2) rock += 1 / n; + if (g.resType[i] == 1) trees += 1 / n; + if (g.resType[i] == 2) stone += 1 / n; + if (g.resType[i] == 3) berries += 1 / n; + } + } + std::printf(" water %.3f rock %.3f trees %.3f stone %.3f berries %.4f\n", water, rock, trees, stone, berries); + CHECK(water > 0.09 && water < 0.135); + CHECK(rock > 0.075 && rock < 0.115); + CHECK(trees > 0.135 && trees < 0.17); + CHECK(stone > 0.02 && stone < 0.035); + CHECK(berries > 0.009 && berries < 0.0135); + } SECTION("bad parameters are rejected"); { GenParams p; diff --git a/tests/test_save_fixtures.cpp b/tests/test_save_fixtures.cpp index 90ec331..17992a8 100644 --- a/tests/test_save_fixtures.cpp +++ b/tests/test_save_fixtures.cpp @@ -5,6 +5,7 @@ // slot's lock), checked against what was recorded when it was written, played // on, saved and reloaded exactly. The seed-42 slot also keeps its previous // save, which must take over when the latest save is damaged. +#include #include #include #include @@ -53,6 +54,7 @@ struct Expect { uint64_t seed; size_t settlers; int64_t winters; + uint64_t loaded; // fingerprint() of the colony as loaded, pinned once and never re-pinned }; } // namespace @@ -64,8 +66,8 @@ int main(int argc, char** argv) { // What the game printed when it wrote each fixture (tests/fixtures/README.md). const Expect fixtures[] = { - {"v1-seed42-tick500", {}, 500, 42, 49, 2}, - {"v1-frost-huts-seed7-tick600", {"frost_huts"}, 600, 7, 26, 3}, + {"v1-seed42-tick500", {}, 500, 42, 49, 2, UINT64_C(0x05ffc670c14bab66)}, + {"v1-frost-huts-seed7-tick600", {"frost_huts"}, 600, 7, 26, 3, UINT64_C(0x50ec76feccbfd0e2)}, }; for (const Expect& x : fixtures) { SECTION((std::string(x.name) + ": loads, plays on, saves and reloads exactly").c_str()); @@ -85,6 +87,11 @@ int main(int argc, char** argv) { CHECK(c.globals.seed == static_cast(x.seed)); CHECK_MSG(c.settlers.size() == x.settlers, std::to_string(c.settlers.size()) + " settlers"); CHECK(wintersSurvived(c) == x.winters); + // The whole world (map included) comes from the save, never from the seed: a + // change to world generation must leave every saved colony exactly as saved. + // The pins were recorded before world generation moved to fractal noise. + std::printf(" loaded fingerprint %016" PRIx64 "\n", fingerprint(c)); + CHECK_MSG(fingerprint(c) == x.loaded, std::string(x.name) + ": the colony as loaded changed"); for (int t = 0; t < 100; ++t) s->tick(); const Colony live = s->colony(); auto r = s->save(); diff --git a/third_party/strata_noise/LICENSE b/third_party/strata_noise/LICENSE new file mode 100644 index 0000000..f8d146f --- /dev/null +++ b/third_party/strata_noise/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Daniel McCoy Stephenson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/strata_noise/README.md b/third_party/strata_noise/README.md new file mode 100644 index 0000000..eca5fae --- /dev/null +++ b/third_party/strata_noise/README.md @@ -0,0 +1,15 @@ +# Strata noise (vendored) + +`include/strata/sim/noise.hpp` and `src/noise.cpp` are copied unchanged from +[Strata](https://github.com/Preponderous-Software/strata) `main` at commit +`649f0a52a70937c5629251457ab7a17c04c05351` (Strata PR #103, after v0.7.0 and not yet in a +release), `modules/sim/include/strata/sim/noise.hpp` and `modules/sim/src/noise.cpp`: seeded 2D +value noise and fractal Brownian motion, deterministic on every compiler when built with +`-ffp-contract=off`. MIT licensed, see [LICENSE](LICENSE). + +They are vendored rather than linked from Strata's `strata::sim` module so the rules library +keeps building without Strata (and the browser build, which turns Strata's sim module off, +keeps working), and so the game does not have to pin an unreleased Strata commit. The +determinism test checks the copy against the values Strata's own unit test pins. Update it by +copying the files from a newer Strata commit and bumping this note; once a Strata release +carries `noise.hpp`, linking `strata::sim` instead is an option. diff --git a/third_party/strata_noise/include/strata/sim/noise.hpp b/third_party/strata_noise/include/strata/sim/noise.hpp new file mode 100644 index 0000000..901b615 --- /dev/null +++ b/third_party/strata_noise/include/strata/sim/noise.hpp @@ -0,0 +1,71 @@ +// strata/sim/noise.hpp -- seeded 2D value noise and fractal Brownian motion. +// +// valueNoise() is lattice value noise: an integer hash gives every lattice +// point a value in [-1, 1), and a point between them is the bilinear blend +// of its four corners under smoothstep weights (3t^2 - 2t^3). fbm() sums +// `octaves` layers of it; each layer's frequency is the last one's times +// `lacunarity` and its amplitude the last one's times `gain`. There is no +// global state: the result depends only on the arguments, and every +// operation is + - * / or floor on doubles, so the same seed gives the same +// numbers on every compiler (the module is built with -ffp-contract=off). +// +// Choosing octaves and gain +// ------------------------- +// The edge of a region cut out of noise (land where fbm > threshold, a +// coastline, a patch of grime) is only as rough as the noise has octaves. +// With 1-4 octaves the edges are smooth, round blobs whatever the gain. With +// eight or more, gain sets the roughness. Natural coastlines have a +// box-counting fractal dimension D of about 1.1-1.3 (Britain's west coast is +// about 1.25; Richardson, Mandelbrot 1967); a smooth curve has D = 1. +// +// Measured with this implementation (modules/sim/tools/fbm_dimension.cpp, +// built as strata-sim-fbm-dimension): the zero level set of a disc plus +// 0.6 * fbm, lacunarity 2, on a 2048 x 2048 grid, box sizes 2-64 pixels, +// mean (and range) over seeds 1-8: +// +// octaves gain D (measured) +// 2 0.50 0.98 (0.97-0.99) +// 4 0.50 0.98 (0.97-0.99) +// 4 0.70 0.99 (0.97-1.00) +// 9 0.50 1.01 (1.00-1.03) +// 9 0.60 1.12 (1.08-1.13) +// 9 0.65 1.19 (1.16-1.21) +// 9 0.70 1.25 (1.22-1.30) +// 9 0.75 1.31 (1.27-1.35) +// +// (A smooth closed curve reads slightly under 1 at these box sizes.) +// So for coastline-like edges use octaves >= 8 with gain 0.6-0.7. +// +// Copyright (c) 2026 Daniel McCoy Stephenson. MIT licence; see LICENSE. +#pragma once + +#include + +namespace strata::sim { + +// The 32-bit integer hash behind the noise: a lattice point and a seed to +// a well-mixed word. Exposed for callers that want per-cell randomness on +// the same lattice. +uint32_t latticeHash(int32_t x, int32_t y, uint32_t seed); + +// One octave of value noise in [-1, 1]. The lattice spacing is 1, so +// features are about one unit across; scale the coordinates to change it. +// Lattice cells wrap every 2^32 units; a NaN or infinite coordinate +// reads as 0. +float valueNoise(float x, float y, uint32_t seed); + +struct FbmParams { + int octaves = 8; // number of layers; clamped to [1, 30] + float gain = 0.5f; // amplitude ratio between layers (roughness) + float lacunarity = 2.0f; // frequency ratio between layers + float frequency = 1.0f; // frequency of the first layer + uint32_t seed = 0u; +}; + +// Fractal Brownian motion: the amplitude-weighted sum of `octaves` layers of +// valueNoise, divided by the sum of the amplitudes, so the result stays in +// [-1, 1]. Each layer has its own seed and lattice offset (both derived from +// `seed`), so layers do not line up at the origin. +float fbm(float x, float y, const FbmParams& params); + +} // namespace strata::sim diff --git a/third_party/strata_noise/src/noise.cpp b/third_party/strata_noise/src/noise.cpp new file mode 100644 index 0000000..fb74488 --- /dev/null +++ b/third_party/strata_noise/src/noise.cpp @@ -0,0 +1,87 @@ +// Copyright (c) 2026 Daniel McCoy Stephenson. MIT licence; see LICENSE. +#include "strata/sim/noise.hpp" + +#include + +namespace strata::sim { + +namespace { + +// "lowbias32" (Chris Wellons' hash prospector): a cheap, well-mixed 32-bit +// finaliser. +uint32_t mix32(uint32_t h) { + h ^= h >> 16u; + h *= 0x7feb352du; + h ^= h >> 15u; + h *= 0x846ca68bu; + h ^= h >> 16u; + return h; +} + +// A hash word to [-1, 1): the top 24 bits, so every value is exact. +double toSigned(uint32_t h) { return static_cast(h >> 8u) * (2.0 / 16777216.0) - 1.0; } + +double smooth(double t) { return t * t * (3.0 - 2.0 * t); } + +uint32_t hashU(uint32_t x, uint32_t y, uint32_t seed) { + uint32_t h = mix32(seed ^ 0x9e3779b9u); + h = mix32(h ^ (x * 0x85ebca6bu)); + h = mix32(h ^ (y * 0xc2b2ae35u)); + return h; +} + +// The lattice cell of a coordinate, wrapped to 32 bits (two's complement), +// so the hash is defined for every finite input. +uint32_t cell(double f) { + if (f < -4.0e18 || f > 4.0e18) return f > 0.0 ? 0x7fffffffu : 0x80000000u; + return static_cast(static_cast(static_cast(f))); +} + +double noise(double x, double y, uint32_t seed) { + if (!std::isfinite(x)) x = 0.0; + if (!std::isfinite(y)) y = 0.0; + const double fx = std::floor(x); + const double fy = std::floor(y); + const uint32_t ix = cell(fx); + const uint32_t iy = cell(fy); + const double tx = smooth(x - fx); + const double ty = smooth(y - fy); + const double v00 = toSigned(hashU(ix, iy, seed)); + const double v10 = toSigned(hashU(ix + 1u, iy, seed)); + const double v01 = toSigned(hashU(ix, iy + 1u, seed)); + const double v11 = toSigned(hashU(ix + 1u, iy + 1u, seed)); + const double a = v00 + (v10 - v00) * tx; + const double b = v01 + (v11 - v01) * tx; + return a + (b - a) * ty; +} + +} // namespace + +uint32_t latticeHash(int32_t x, int32_t y, uint32_t seed) { + return hashU(static_cast(x), static_cast(y), seed); +} + +float valueNoise(float x, float y, uint32_t seed) { return static_cast(noise(x, y, seed)); } + +float fbm(float x, float y, const FbmParams& params) { + const int octaves = params.octaves < 1 ? 1 : (params.octaves > 30 ? 30 : params.octaves); + double freq = params.frequency; + double amp = 1.0; + double sum = 0.0; + double norm = 0.0; + for (int o = 0; o < octaves; ++o) { + const uint32_t layer = hashU(static_cast(o), 0x5eedu, params.seed); + // A per-layer offset in [0, 256) on each axis, so the layers' lattices + // do not all meet at the origin. + const double ox = static_cast(layer >> 24u) + static_cast((layer >> 8u) & 0xffffu) / 65536.0; + const uint32_t layer2 = mix32(layer); + const double oy = static_cast(layer2 >> 24u) + static_cast((layer2 >> 8u) & 0xffffu) / 65536.0; + sum += amp * noise(static_cast(x) * freq + ox, static_cast(y) * freq + oy, layer); + norm += amp; + freq *= params.lacunarity; + amp *= params.gain; + } + return norm > 0.0 ? static_cast(sum / norm) : 0.0f; +} + +} // namespace strata::sim diff --git a/web/smoke/smoke.cjs b/web/smoke/smoke.cjs index 2efb03c..593683a 100644 --- a/web/smoke/smoke.cjs +++ b/web/smoke/smoke.cjs @@ -13,7 +13,7 @@ const path = require('path'); const zlib = require('zlib'); const { chromium, devices } = require('playwright'); -const FINGERPRINT = '011ed2b2f364b21b'; // seed 42, 64x64, 30 settlers, 1000 ticks +const FINGERPRINT = '09826a6da618223a'; // seed 42, 64x64, 30 settlers, 1000 ticks const [url, outDir] = process.argv.slice(2); const phone = process.argv.includes('--phone'); if (!url || !outDir) {