Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/browser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion docs/THIRD-PARTY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
2 changes: 1 addition & 1 deletion docs/WEB.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Binary file added docs/images/terrain-fractal-before-after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/terrain-fractal-game.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 55 additions & 6 deletions src/colony/generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(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<uint32_t>(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) {
Expand Down Expand Up @@ -69,24 +115,27 @@ 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<float>(x), fy = static_cast<float>(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<int32_t>(Terrain::Water);
} else if (elevation > 0.72) {
} else if (elevation > kRockAbove) {
g.terrain[i] = static_cast<int32_t>(Terrain::Rock);
if (roll < 300) {
g.resType[i] = static_cast<int32_t>(Resource::Stone);
g.resAmount[i] = Rules::kStoneAmount;
}
} else {
g.terrain[i] = static_cast<int32_t>(Terrain::Grass);
if (forest > 0.55 && roll < 450) {
if (forest > kForestAbove && roll < 450) {
g.resType[i] = static_cast<int32_t>(Resource::Tree);
g.resAmount[i] = Rules::kTreeAmount;
} else if (roll < 25) {
Expand Down
22 changes: 21 additions & 1 deletion tests/test_determinism.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "colony/rng.h"
#include "colony/schema.h"
#include "colony/systems.h"
#include "strata/sim/noise.hpp"

using namespace steading;

Expand Down Expand Up @@ -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.
Expand All @@ -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();
}
31 changes: 31 additions & 0 deletions tests/test_generate.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstdio>
#include <set>
#include <stdexcept>

Expand Down Expand Up @@ -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<uint64_t>(s);
p.settlers = 0;
const Grid& g = generate(p).grid;
const double n = static_cast<double>(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;
Expand Down
11 changes: 9 additions & 2 deletions tests/test_save_fixtures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cinttypes>
#include <cstdio>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand All @@ -85,6 +87,11 @@ int main(int argc, char** argv) {
CHECK(c.globals.seed == static_cast<int64_t>(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();
Expand Down
21 changes: 21 additions & 0 deletions third_party/strata_noise/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions third_party/strata_noise/README.md
Original file line number Diff line number Diff line change
@@ -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.
Loading
Loading