Compare commits

...

7 Commits

Author SHA1 Message Date
e42e7b46b5 Add ability to set custom spawn location 2026-10-15 23:53:45 +03:00
dc15ec21a4 add cJSON as submodule 2026-10-11 18:52:21 +03:00
40defbd367 place hot springs as GenHotSprings does, and find the nearest
Carving records hot spring candidates where the game does: deep, wide
tunnel steps that pass the water check where geologic activity is
over 128. A candidate becomes a hot spring unless the surface over
twice its radius, after caves, is rough, below sea level + 1 or high
in the mountains, or a story location keeps hot springs away; carved
chunks are kept so their neighbours' heights come cheap. The Caves
overlay paints them as discs, and Find nearest looks for one.

Against the save, 173 of the 180 stored candidates match in centre and
radius; five differ only in the radius's last float bits, from a
rounding not yet found. Of the 102 hot springs placed, 84 hold the
save's sludgy gravel, and 94.8% of that gravel lies in one. Which edge
blocks are gravel or mat the game rolls each time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 18:24:10 +03:00
82774445af show the caves on the map, and find the nearest entrance
A Caves overlay colours each column by how far below the surface its
topmost cave starts, and marks where a cave opens at the surface; its
legend gives each band's share of the view. Find nearest entrance
carves the chunks it scans and looks at every column. The cursor label
and the probe list a column's caves, air or lava, top down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 15:34:26 +03:00
aeb6d3be22 carve caves as GenCaves does, and check them block by block
A cave's course depends only on the seed and the chunk it starts in,
so each source chunk's tunnels are traced once, in its own
coordinates, as the ellipsoids they carve. A chunk is carved as the
game carves it: its eleven by eleven sources in order, each ellipsoid
skipped where the terrain's water or earlier lava is near, lava and
basalt below y 12 by geologic activity, the height map lowered where a
cave opens. The terrain now keeps whether each block is solid, which
makes the water exact; story locations keep caves away as theirs do.

vsmv verify compares every stored block below our surface: 96.1% of
the 18.5 million carved blocks agree, and of the rest all but 0.28%
are what later passes put in caves or made of air themselves, the
vugs and the block layers above all. Carving lifts the columns
matching the stored height by 71,379.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 15:34:00 +03:00
2c16f8caef read the save's chunk blocks, and check the rock below the surface
A chunk's solid blocks are a palette of block ids, raw or zstd, and a
zstd frame of bit planes; a layer of a lone 0 is all air. With them,
vsmv verify compares the rock of every stored block below the surface
with the strata laid against our own heights, which predate the dry
seabed raise: 99.91% of 1.38 billion blocks match. Lava basalt near the
bottom, the granite of cave walls and structures, and columns whose
height differs explain all but 0.0011%, nearly all of that in one area
at the explored edge whose layer boundaries are not explained yet.

What the save has instead of stratum rock is tallied too, as the
ground truth the deposits phase can be checked against.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 11:59:26 +03:00
53950a465c show silver, gold and peridot by their ore maps
The pick reads them from ore inside quartz and olivine deposits, which
are not placed yet, so for them the Minerals list shows the ore map
instead, marked as such: the overlay, Find nearest and the cursor work
on its value in sevenths, and the probe lists them on a line of their
own. Their maps already match the save.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 11:59:20 +03:00
19 changed files with 2234 additions and 162 deletions

6
.gitignore vendored
View File

@@ -1,11 +1,9 @@
/build/
# Inputs that stay local: the game's assets, the Rivers release, the
# test world and its server config, and the cJSON checkout the build
# compiles from.
# Inputs that stay local: the game's assets, the Rivers release, and
# the test world and its server config.
/assets/
/serverconfig.json
/cJSON/
*.zip
*.vcdbs
*.vcdbs-*

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "cJSON"]
path = cJSON
url = https://github.com/DaveGamble/cJSON.git

View File

@@ -4,10 +4,11 @@
SEXC ?= $(HOME)/dev/sex/sexc
CFLAGS ?= -O2 -g
CFLAGS += -std=c11 -Wall -ffp-contract=off -fwrapv
CPPFLAGS += -Isrc -Ithird_party/nuklear -Ithird_party/stb -IcJSON $(shell pkg-config --cflags sdl3)
CPPFLAGS += -Isrc -Ithird_party/nuklear -Ithird_party/stb -IcJSON $(shell pkg-config --cflags sdl3 libzstd)
# Sex writes C with only the parentheses precedence needs.
SEX_CFLAGS = -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wno-shift-op-parentheses
SDL_LIBS := $(shell pkg-config --libs sdl3)
ZSTD_LIBS := $(shell pkg-config --libs libzstd)
B = build
@@ -30,15 +31,15 @@ $(B)/cJSON.o: cJSON/cJSON.c cJSON/cJSON.h | $(B)
$(B)/stb.o: src/stb.c third_party/stb/stb_image_write.h | $(B)
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
VSMV_OBJ = vsmv cs pb json maps save img dump lcg noise shape worldgen fastnoise rivers terra strata ores settings verify cJSON stb
VSMV_OBJ = vsmv cs pb json maps save img dump lcg noise shape worldgen fastnoise rivers terra strata ores caves settings verify cJSON stb
$(B)/vsmv: $(VSMV_OBJ:%=$(B)/%.o)
$(CC) $^ -o $@ -lsqlite3
$(CC) $^ -o $@ -lsqlite3 $(ZSTD_LIBS)
VIEWER_OBJ = viewer tiles search hover settings worldgen fastnoise rivers terra strata ores shape noise lcg maps save pb json cs nk cJSON stb
VIEWER_OBJ = viewer tiles search hover settings worldgen fastnoise rivers terra strata ores caves shape noise lcg maps save pb json cs nk cJSON stb
$(B)/vs-map-viewer: $(VIEWER_OBJ:%=$(B)/%.o)
$(CC) $^ -o $@ $(SDL_LIBS) -lsqlite3
$(CC) $^ -o $@ $(SDL_LIBS) -lsqlite3 $(ZSTD_LIBS)
$(B):
mkdir -p $@

183
Plan.org
View File

@@ -227,9 +227,14 @@ Modules. Each is a Sex module; the =vsmv= CLI and the GUI share them.
- =ui=: Nuklear + SDL3 SDL_Renderer. Tiles are SDL textures under the
Nuklear panels.
Coordinates are shown like the in-game HUD, relative to the map centre
=((MapSizeX+32)/2, (MapSizeZ+32)/2)=, with absolute coordinates available
too. The viewer opens at spawn.
Coordinates are shown as the game shows them, relative to the world's
spawn (=DefaultSpawnPosition=, which =GuiDialogWorldMap= subtracts):
=SaveGame.DefaultSpawn= when the world has one (game data field 40, a
=PlayerSpawnPos= {1: x, 2: y, 3: z, 7: remaining uses}; this save's is
1023719, 142, 12553), or =serverconfig.json='s =DefaultSpawn=, else
the map's middle. A spawn of one's own can be set over it, by clicking
the map after /Set spawn/ or with =--spawn X Z=. Absolute
coordinates are shown too. The viewer opens at the spawn.
* Phases
Each phase ends with a check against the save. A mismatch means stop,
@@ -292,6 +297,25 @@ What the save turned out to be:
| 7 | =WorldGenTerrainHeightMap= |
| 13 | =TopRockIdMap= (block ids) |
- *=chunk=*: one 32 x 32 x 32 chunk per row, keyed by
=x | z << 27 | y << 54= in chunks. All of this save's chunks are
from 1.22.7 (field 12).
| Field | Contents |
|-------+----------|
| 1 | solid blocks, see below |
| 8 | block entities |
| 9 | mod data, e.g. ="flowVectors"= |
| 12 | game version that generated it |
The block layer is an int32 n, then the palette of block ids: raw in
-n bytes if n is negative, zstd-compressed in n bytes if not. Then
one zstd frame of bit planes, 4096 bytes each, plane k holding bit k
of every block's palette index, block =(y * 32 + z) * 32 + x= at bit
i % 8 of byte i / 8. A layer that is just n = 0 holds no blocks: all
air. 53,855 of the 114,140 chunks are like that. Water is not in this
layer; fields 2 and 3 are the likely fluid and light layers, not read
yet.
- *Assets.* The six worldgen JSONs (and all 910 asset JSONs) parse
after a relaxing pass: comments, unquoted keys and trailing commas.
There are 48 landforms, 6 provinces and 16 strata.
@@ -469,9 +493,8 @@ Validation:
- *Result.* 11,675,804 / 11,675,818 columns match, 99.9999%. The 14
others likely carry structures placed later. A further 12,118
columns have no rock on top.
- *Deeper layers* go through the same code but aren't checked against
chunk blocks. Decoding those blobs is the remaining step, if it is
ever needed.
- *Deeper layers.* Checked against the chunk blocks since; see
"Chunk blocks".
Viewer:
- *Rock layer.* Modes: surface, depth below the surface, or absolute
@@ -626,10 +649,15 @@ Viewer:
- *Minerals overlay.* Tiles keep =64 + category= per sample and share
the rock palette, so they cost one more palette range, not a new
tile kind.
- *Minerals panel.* Picks the mineral from a list of the readable ones.
Its legend gives each category's share of the view. Find nearest
takes a least category (decent by default) and runs as search mode
4.
- *Minerals panel.* Picks the mineral from a list: the readable ones,
then silver, gold and peridot as "(ore map)". Its legend gives each
category's share of the view. Find nearest takes a least category
(decent by default) and runs as search mode 4.
- *Ore map only.* For the three the pick can't read, the overlay,
Find nearest and the cursor use the ore map's value in sevenths
(=ores/map-category=). Those tiles need neither heights nor strata.
The legend says it isn't a reading, and the Probe lists their ore
maps on a line of their own.
- *Cursor and Info.* A second label line gives the reading under the
cursor while the Minerals overlay is on.
- *Probe.* Lists the pick's full reading for the column, highest first,
@@ -649,7 +677,130 @@ Viewer:
- Go-to-coordinates, PNG export of the current view.
- Later: gravel-beach layer; deposits, see below.
** Next: deposits (planned, after the Sex fixes)
** Chunk blocks (done)
=save/chunk-blocks= decodes a stored chunk's solid blocks (format under
the =chunk= table above), with zstd from the system.
=vsmv verify= now compares the rock of every stored block from y = 1
up to the lower of our height and the stored one. The strata are laid
against our height, since the game laid them before the dry seabed
raise changed the stored height. Laying them against the stored height
put a whole raised area (around z 7440-7900) one stratum off.
Result: 1,375,064,177 of 1,376,283,630 stratum rock blocks match
(99.9114%). The rest:
- 26,042 in columns whose height differs from ours;
- 1,075,584 basalt at y 18 or below: deep cave lava (y 11 and below)
turns the rock around it to basalt;
- 103,165 granite, the default rock, in pockets with air,
=stalagsection-granite=, =sand-granite= and =cobblestone-granite=.
Cave walls and underground structures write it after the strata.
Some of it later turned to =ore-quartz-granite=, so it predates the
deposits.
- 14,662 others (0.0011%), nearly all in one area near the explored
edge (x 1024100-1024700, z 7340-7630). There the layer boundaries sit
a few blocks off ours: peridotite, slate and sandstone, all ordinary
strata. Not explained yet. The province map cache is keyed so that
regions can't collide (=regionMapSize= is 8000 here), so that isn't
it. The check passes under 0.01% unexplained.
Where the save has no stratum rock (247 million blocks), it tallies what
is there instead: crackedrock 123 million, ore 66 million, soil 20
million, air 18 million, glacier ice 9 million, gravel, sand,
saltpeter, and 373 thousand other rock (halite, marble, obsidian,
travertine). That is what the deposits, caves and block layers wrote,
and what the deposits phase can be checked against.
** Caves (done)
=src/caves.sex= ports =GenCaves= (terrain pass, after the strata).
- *Starts.* =chunkRand= (world seed), positioned at the chunk, starts
a cave with chance 0.37: a position, size and shape from its draws,
then =caveRand= reseeded from it. Tunnels run 108 to 144 steps, so
they reach 5 chunks; they branch (horizontally, and at level 0 into
vertical shafts that end in a fan of tunnels), widen, flatten, go
tall, and near y 12 open lava caverns. =FastSin=/=FastCos= are the
game's 4096-entry tables.
- *Traced once.* A tunnel's course depends only on the seed and its
chunk. It is traced once per source chunk, as the ordered list of
=SetBlocks= ellipsoids, in the source chunk's coordinates. The game
works in the target chunk's coordinates; the double sums are exact
at that size, so the two differ by whole chunks only. World
coordinates would round.
- *Carved per chunk.* A target chunk replays its 11 x 11 sources in
=GenPartial='s order (x outer, z inner):
- Each ellipsoid that reaches the chunk is skipped if any liquid is
in it or one size around it: the water the terrain pass left in
every open block below sea level, or lava an earlier ellipsoid put.
- Below y 12 a carved block becomes basalt above
=geologic activity * 16 / 128= and lava at or below it. At y 11,
=basaltNoise= over 0.65 makes basalt.
- A carve at the column's height lowers it, which later carving
reads through =CaveHeightDistort=.
- Story locations skip caves for chunks whose centre lies in their
"caves" radius, or in the village's footprint (195 x 225; the
story schematics aren't in the assets read here).
- *Terrain solidity.* =terra/chunk-solidity= keeps every block's solid
or open from the height scan, so the water check is exact, overhang
pockets below sea level included.
- *Hot springs* (=GenHotSprings=, terrain features pass).
- Candidates: a tunnel step at -5 < y < 16 with radii over 4 and 2
flags its ellipsoid. Carving it, if it passes the water check, the
region's geologic activity is over 128, and its centre is in the
chunk, records it in the chunk's "hotspringlocations": keyed by
its whole centre, a later one at the same key taking its place.
- Placement: over a disc of twice the radius, the surface after caves
of the 3 x 3 chunks around (carved on demand, and kept, 512 chunks
a carver). Rejected when the surface varies by 4 or more, dips
below sea level + 1, or is above 88% of the world height, and
where a story location's "hotsprings" radius covers the chunk.
The game also rejects water standing at the surface; above sea
level that can only be a pond, which comes later, or a neighbour's.
- The disc sits at the average surface, rounded half to even:
boiling water in the middle (=xzdist= under 0.1), sludgy gravel a
base 3 deep and walls beside liquid, bacterial mats by distance.
The game jitters each column's distance with its unseeded random
generator, so the edges of those rings vary from run to run.
Validation (=vsmv verify=, every stored chunk, blocks below our
terrain height):
- 17,794,389 of our 18,521,333 carved blocks agree with the save
(96.08%): air or lava where the save has air (lava is a fluid, so its
solid block is air), basalt where it has basalt, or basalt a deposit
later made obsidian, ore or cracked rock. Of the rest, 667,823 hold
what later passes put in caves (stalactites, gravel, ice).
- Air the save has where we leave rock: 656,515 inside generated
structures (vugs above all), 292,212 at or above the save's surface
(the block layers take away top blocks, glaciers especially),
26,749 otherwise.
- Rock the save has where we carve air: 28,562 inside structures,
25,388 otherwise. The 52,137 unexplained are 0.28% of what we carve;
the check fails over 0.5%.
- The terrain's own pockets: 334,438 air blocks, 99.4% matching.
- Hot spring candidates against the 44 map chunks that store some: 173
of 180 the same, centre and radius. Five differ in the radius's last
bit or two, four of those from one tunnel, so some float step
somewhere rounds differently; not found yet. Two differ more.
- Hot springs: 102 placed, 84 with the save's sludgy gravel in them;
18,973 of the save's 20,024 sludgy gravel blocks lie in one (from 2
below the disc to 4 above, base and walls).
- Terrain height after carving: 11,552,740 of 11,687,936 columns match
the stored height, against 11,481,361 before. The caves make up most
of the "stored lower on land" columns of the height check.
Viewer:
- *Caves overlay.* Per column: none, an entrance (a cave lowered the
surface), or the depth of the topmost cave below the surface in
bands: 10, 25, 50, 100, deeper. Tiles carve every chunk they sample
whole, so a view takes some 30 s to fill on all cores.
- *Caves panel.* The legend with each band's share of the view, and
Find nearest entrance (search mode 5, every column of each chunk it
carves) or hot spring (mode 6).
- *Hot springs* are painted over the caves as discs, from the chunks of
the tile and one ring around it, whose discs can reach in.
- *Cursor label and Probe.* The column's cave runs, air or lava, top
down, and whether a cave opens there.
** Next: deposits (planned)
Placing the deposits themselves, not just the pick's reading of them.
Soil deposits come first: they sit at the surface, so they don't need
the 3D rock column, and the steps up to the disc are the first half of
@@ -683,19 +834,17 @@ showing them means placing them.
gravel. Fertility and grass cover come from =GenBlockLayers= and
=blocklayers.json=, which are not ported. Without them, patches show
where the game puts them on sand, gravel or rich soil.
- *Checking.* The save stores surface blocks only in chunk blobs, which
aren't decoded yet. Until they are, walk to a few patches in game.
- *Checking.* Against the chunk blocks: clay, peat and compost soil
where the save has them, at the top of the column.
*Exact ore deposits.* The same replay, then the disc, sea-level,
anywhere and child generators placing into the strata column. That
places real ore blocks and makes the pick readings exact: the pick's
column holds the ore of its own chunk's deposits. It also reads silver
and gold (children of quartz) and peridot (a child of olivine), whose
host is their parent's ore. Checking needs the chunk blocks decoded.
host is their parent's ore. Checking: against the save's ore blocks,
which the strata check already counts (66 million).
*Meanwhile, cheap:* the gold and silver ore maps already match the
save. They could go in the Minerals list as "ore map only", marked as
not a pick reading.
* Risks
- *Bit-exactness* of the noise and the Rivers trigonometry. The

View File

@@ -6,38 +6,24 @@ it as a zoomable top-down map: terrain, rivers, oceans, climate,
landforms, geologic provinces, and rock strata at any depth. It is
mostly for finding rock: where chalk, bauxite or limestone lie, at the
surface or 20 blocks down, and which one is nearest. It also shows
what the prospecting pick would read for an ore, anywhere on the map.
what the prospecting pick would read for an ore, anywhere on the map,
and where the caves run.
[[file:doc/screenshot.png]]
It covers Vintage Story 1.22.7 with the Rivers 5.0.3 mod. The
generator is a port of the game's own, written in [[https://git.kotobank.ch/alex-eg/sex][Sex]]. It is checked
against a real save, and matches it:
| What | Matches |
|-------------------------------------------+-----------------------------------------|
| Region maps: landform, climate, province, ocean, upheaval, strata | every pixel of 118 regions |
| Story structure locations | all 6 |
| Rivers' distance to the nearest river | 100% of 6.5 million columns |
| Terrain height | 98.2% of 11.7 million columns |
| Rock at the surface | all but 14 of 11.7 million columns |
| Ore maps, 31 minerals | every pixel of 118 regions |
| Prospecting pick readings | 38 of 42 in the game's category; see Limits |
All but 0.02% of the other heights come from two passes that run after
the strata are laid: caves that open the surface, and the raise of dry
seabeds. Neither changes which rock is where.
=Plan.org= has the details.
generator is a port of the game's own, written in [[https://git.kotobank.ch/alex-eg/sex][Sex]].
** Building
You need:
- the Sex compiler, expected at =~/dev/sex/sexc= (or set =SEXC=);
- the Sex compiler;
- a C compiler;
- SDL3 (=brew install sdl3=);
- SDL3;
- SQLite3;
- a checkout of [[https://github.com/DaveGamble/cJSON][cJSON]] in =./cJSON=.
- zstd;
Nuklear and stb_image_write are vendored in =third_party/=.
сJSON is a submodule, so use =git submodule update --init= before building.
#+begin_src sh
make
@@ -61,8 +47,9 @@ WORLD is where the seed and settings come from:
real size;
- or a =serverconfig.json=: the seed as typed and the world config.
Without WORLD it opens =./serverconfig.json=. It starts at spawn, in the
middle of the map.
Without WORLD it opens =./serverconfig.json=. It starts at the world's
spawn: the one the save or config names, else the middle of the map,
as in the game.
Options:
@@ -71,16 +58,19 @@ Options:
| =--assets DIR= | where the game's =assets= are; default =./assets= |
| =--zoom Z= | blocks per screen point, 1/16 to 32; default 4 |
| =--base LAYER= | the base layer: =ocean=, =rivers= or =height=; default =height= |
| =--overlay NAME= | turn on an overlay: =landform=, =climate=, =province=, =upheaval=, =strata=, =rock=, =mineral=; repeatable |
| =--overlay NAME= | turn on an overlay: =landform=, =climate=, =province=, =upheaval=, =strata=, =rock=, =mineral=, =caves=; repeatable |
| =--opacity F= | overlay opacity, 0 to 1; default 0.5 |
| =--rock-depth N= / =--rock-y Y= | rocks at N blocks under the surface, or at height Y |
| =--rock-any= | the one highlighted rock at any depth |
| =--highlight ROCK= | highlight a rock, e.g. =chalk=; repeatable |
| =--find= | find the nearest highlighted rock on start |
| =--mineral CODE= | show a mineral's readings, e.g. =nativecopper= |
| =--min-reading NAME= | Find nearest's least reading for minerals, e.g. =high= or =very-poor=; default =decent= |
| =--min-reading NAME= | Find nearest's least reading for minerals, e.g. =high= or =very-poor=, or an ore map band, e.g. =86-100%=; default =decent= |
| =--find-mineral= | find the nearest such reading on start |
| =--find-cave= | find the nearest cave entrance on start |
| =--find-hot-spring= | find the nearest hot spring on start |
| =--probe X Z= | probe the column at block X, Z on start |
| =--spawn X Z= | count coordinates from block X, Z instead of the world's spawn |
| =--screenshot FILE.png= | wait until everything is drawn, save the window, quit |
| =--mouse X Y= | place the cursor, for screenshots |
@@ -104,7 +94,8 @@ The map fills the middle of the window, between two panels.
under it, as the Rocks section is set (at the surface, at a depth, or
at a height). At any depth, it gives the Y of the rock's topmost block
and how far down that is, or says the column has none. With the
Minerals overlay on, a second line gives the mineral's reading.
Minerals overlay on, another line gives the mineral's reading; with
the Caves overlay on, another gives the column's caves, top down.
The further out you zoom, the coarser the terrain is sampled: every
block when zoomed in close, one in 32 when far out. Only what is on screen is computed;
@@ -118,9 +109,13 @@ when nothing is left to compute, the viewer sleeps.
- Change any setting (seed, sizes, landcover, scales, climate...)
and press /Apply/ to generate that world instead. The seed can be
any text, as typed into the game.
- /Go to spawn/ goes back to the centre of the map.
- /Go to spawn/ goes back to the spawn.
- /Set spawn/, then a click on the map, sets the spawn that
coordinates count from there, as the game counts them from the
world's; Escape or the button again gives up. It starts as the
world's spawn; /World's spawn/ goes back to it.
- *Info*: what is under the cursor:
- coordinates, absolute and from the map centre, and the region;
- coordinates, absolute and from the spawn, and the region;
- landform and geologic province;
- climate: temperature at sea level, rain, geologic activity;
- ocean and upheaval values;
@@ -131,7 +126,9 @@ when nothing is left to compute, the viewer sleeps.
- *Probe*: the last column probed: its surface height and its strata,
top down, with the Y range and thickness of each. Below them, what
the prospecting pick reads there: every mineral it finds, highest
first, with its parts per thousand, then the miniscule amounts.
first, with its parts per thousand, then the miniscule amounts. Then
its caves, top down: air or lava, with the Y range of each, and
whether a cave opens at the surface there.
- /Go to/ centres it.
- /Clear pins/ removes the pins.
- /Close/ hides it.
@@ -142,7 +139,8 @@ when nothing is left to compute, the viewer sleeps.
- *Overlays*: any of Landform, Climate, Geologic province, Upheaval,
Rock strata thickness (pick the stratum below it), Rock and
Minerals. Drawn over the base at the /Overlay opacity/.
- /Story locations/ marks spawn and the story structures.
- /Story locations/ marks the spawn, the world's spawn if the one set
differs, and the story structures.
- /Rock under cursor/ turns the cursor label on and off.
- *Rocks*: finding rock.
- Choose where to look: at the /Surface/, at a /Depth below
@@ -170,6 +168,20 @@ when nothing is left to compute, the viewer sleeps.
are.
- /Find nearest/ looks for the nearest column that reads at least
/Find at least/, as Rocks' /Find nearest/ does.
- Silver, gold and peridot are listed as /(ore map)/. For them the
overlay shows the mineral's ore map, how common it is there, in
bands of sevenths, and Find nearest looks for a band.
- *Caves*: the caves the game carves.
- The Caves overlay colours each column by how far below the
surface its topmost cave starts, and marks entrances, where a cave
opens at the surface. Hot springs show over it as discs. The legend
gives each band's share of the view.
- /Nearest entrance/ and /Nearest hot spring/ look for the nearest
cave entrance or hot spring around the centre of the view.
- A hot spring's place and size come from the seed; which blocks at
its edge are gravel or bacterial mat the game rolls each time.
- Caves are carved a chunk at a time, every block of it, so the
overlay is slow to fill when zoomed out.
** vsmv
The command-line side, mostly for checking the generator against a
@@ -183,25 +195,29 @@ save:
| =vsmv dump SAVE.vcdbs OUT= | PNGs of the maps the save stored |
| =vsmv verify SAVE.vcdbs DIR OUT= | regenerates everything the save stored and compares it; diff images go to OUT |
=verify= takes a few minutes, most of it on the terrain heights of
every stored chunk. It also compares the ore maps, and the prospecting
pick readings players took in the save, if there are any.
=verify= takes about eight minutes, most of it on the terrain heights
of every stored chunk and on the caves. It also compares the ore maps,
the prospecting pick readings players took in the save, if there are
any, and every stored block below the surface: its rock, and whether
it is a cave.
** Limits
- Only the /realistic/ world climate is supported, not /patchy/.
- Strata below the surface use the same code as the surface rock,
which is checked, but they are not checked against the save's blocks.
- Rivers' settings are not stored in a save. Without a =rivers.json=,
the mod's defaults are assumed.
- Other mods that change worldgen are not modelled.
- Caves leave out what later passes put in them: stalactites, gravel,
and the structures some of them hold. Of the blocks the
save has where caves are carved, 3.6% are such things.
- Prospecting readings come out a little higher than the game's: in
the readings checked, the game's was up to 12.5% lower, which can
move one across a category edge. The game's column already holds the ore of the
deposits generated in its chunk, and those deposits are not
generated here.
- Silver, gold and peridot are left out of Minerals. The game reads
them from ore inside other deposits (quartz and olivine),
which needs the deposits themselves.
- The pick can't be reproduced for silver, gold and peridot. The game
reads them from ore inside other deposits (quartz and olivine),
which needs the deposits themselves. Minerals shows their ore maps
instead.
** Notes
- =Plan.org= is the design and how each part was checked.

1
cJSON Submodule

Submodule cJSON added at 6d9f2443ab

734
src/caves.sex Normal file
View File

@@ -0,0 +1,734 @@
;;; Caves as GenCaves carves them, during the terrain pass right after
;;; the strata.
;;;
;;; A chunk starts a cave with chance 0.37: a tunnel that wanders,
;;; widens and branches for up to about 140 steps, so it reaches up to
;;; five chunks away. Each step carves an ellipsoid. Its course depends
;;; only on the seed and the chunk, so it is traced once per source
;;; chunk, in that chunk's own coordinates, as the list of ellipsoids
;;; it carves.
;;;
;;; Carving them is per target chunk, as the game does it: the eleven
;;; by eleven source chunks around it in order, each ellipsoid skipped
;;; when there is liquid in or around it, water from the terrain or
;;; lava an earlier one left. Below y 12 a carved block becomes lava or
;;; basalt, by the region's geologic activity. Where a cave reaches the
;;; surface it lowers the height map, which later carving reads.
;;;
;;; Positions are kept relative to the source chunk. The game keeps them
;;; relative to the target chunk; at that size both sums are exact, so
;;; the two differ by whole chunks only, where world coordinates would
;;; round.
(include stdlib.h string.h math.h)
(pub import maps)
(import lcg)
(import noise)
(pub import worldgen)
(pub import terra)
;;; How many chunks a cave reaches, and the states of a carved column.
(pub define CAVE-RANGE 5)
(pub define CAVE-UNTOUCHED 0)
(pub define CAVE-AIR 1)
(pub define CAVE-LAVA 2)
(pub define CAVE-BASALT 3)
(define CHUNK 32)
(pub define SIN-COUNT 4096)
(pub define TRACE-SLOTS 4096)
;;; One SetBlocks call: the ellipsoid's centre in its source chunk's
;;; coordinates, its radii, and whether it may make a hot spring.
(record carve ((x double) (y double) (z double) (hr float) (vr float) (hot bool)))
;;; The carves of one source chunk's cave, in order.
(pub record trace ((sx int) (sz int) (valid bool) (carves (* carve)) (count int) (cap int)))
;;; A place a hot spring may go, as SetBlocks records it in its chunk's
;;; "hotspringlocations": the centre within the chunk, and the radius.
(pub record spring-candidate ((x int) (y int) (z int) (radius double)))
(pub define MAX-CANDIDATES 16)
;;; A carved chunk column, kept: its heights after carving and its
;;; candidates, in the order they were first recorded.
(pub record carved
((cx int) (cz int) (valid bool) (heights [int 1024]) (count int)
(candidates [spring-candidate MAX-CANDIDATES])))
(pub define CARVED-SLOTS 512)
(pub record caves
((g (* worldgen)) (seed i32) (size-y int) (sea-level int)
(height-noise (* simplex-noise)) (basalt-noise (* simplex-noise))
;; GameMath.FastSin and FastCos
(sin [float SIN-COUNT]) (cos [float SIN-COUNT]) (rad-to-index float)
(traces [trace TRACE-SLOTS])
;; the climate map of the region last asked about
(climate int-map) (climate-rx int) (climate-rz int)
;; the trace being made, and its random numbers
(cave-rand lcg) (current (* trace))
(carved [carved CARVED-SLOTS])
;; room to carve a neighbour in, for hot springs
(scratch-states (* u8)) (scratch-solid (* u32))))
(define PI-F 3.14159274)
(pub fn caves/make ((g (* worldgen))) (* caves)
(var c (* caves) (calloc 1 (sizeof caves)))
(= (. c g) g)
(= (. c seed) (. g settings seed))
(= (. c size-y) (. g settings size-y))
(= (. c sea-level) (cast (* 0.4313725490196078 (. g settings size-y)) int))
;; Seed + 12, as a C# int
(var seed i32 (cast (+ (cast (. c seed) u32) 12) i32))
(= (. c height-noise) (simplex-noise/octaves 3 (/ (cast 1 float) (cast 20 float)) (cast 0.9 float) seed))
(= (. c basalt-noise) (simplex-noise/octaves 2 (/ (cast 1 float) (cast 3.5 float)) (cast 0.9 float) seed))
(var rad-full float (cast (* M-PI 2.0) float))
(= (. c rad-to-index) (/ (cast SIN-COUNT float) rad-full))
(for (var i int 0) (< i SIN-COUNT) (++ i)
(var a float (* (/ (+ (cast i float) (cast 0.5 float)) (cast SIN-COUNT float)) rad-full))
(= [(. c sin) i] (cast (sin (cast a double)) float))
(= [(. c cos) i] (cast (cos (cast a double)) float)))
(return c))
(pub fn caves/free ((c (* caves))) void
(for (var i int 0) (< i TRACE-SLOTS) (++ i)
(free (. [(. c traces) i] carves)))
(free (. c climate data))
(free (. c scratch-states))
(free (. c scratch-solid))
(free (. c height-noise))
(free (. c basalt-noise))
(free c))
(fn fast-sin ((c (* const caves)) (rad float)) float
(return [(. c sin) (& (cast (* rad (. c rad-to-index)) int) (- SIN-COUNT 1))]))
(fn fast-cos ((c (* const caves)) (rad float)) float
(return [(. c cos) (& (cast (* rad (. c rad-to-index)) int) (- SIN-COUNT 1))]))
(fn clampf ((v float) (lo float) (hi float)) float
(if (< v lo) (return lo))
(if (> v hi) (return hi))
(return v))
(fn clampd ((v double) (lo double) (hi double)) double
(if (< v lo) (return lo))
(if (> v hi) (return hi))
(return v))
;;; Tracing ----------------------------------------------------------
(fn add-carve ((c (* caves)) (x double) (y double) (z double) (hr float) (vr float) (hot bool)) void
(var t (* trace) (. c current))
(if (>= (. t count) (. t cap))
(do
(= (. t cap) (if (> (. t cap) 0) (* (. t cap) 2) 256))
(= (. t carves) (realloc (. t carves) (* (. t cap) (sizeof carve))))))
(= [(. t carves) (. t count)] #(carve : x y z hr vr hot))
(++ (. t count)))
;; declared ahead: a tunnel branches into shafts, a shaft into tunnels
(fn carve-tunnel ((c (* caves)) (pos-x double) (pos-y double) (pos-z double) (hor-angle float)
(vert-angle float) (horizontal-size float) (vertical-size float)
(current-iteration int) (max-iterations int) (branch-level int)
(extra-branchy bool) (curviness float) (large-near-lava bool)) void)
;;; CarveShaft: a vertical branch that ends in a few horizontal ones.
(fn carve-shaft ((c (* caves)) (pos-x double) (pos-y double) (pos-z double) (hor-angle float)
(vert-angle float) (horizontal-size float) (vertical-size float)
(cave-current-iteration int) (max-iterations int) (branch-level int)) void
(var r (* lcg) (& (. c cave-rand)))
(var vert-angle-change float 0)
(var current-iteration int 0)
(while (< current-iteration max-iterations)
(++ current-iteration)
(var rel-pos float (/ (cast current-iteration float) (cast max-iterations float)))
(var hor-radius float (* horizontal-size (- (cast 1 float) (* rel-pos (cast 0.33 float)))))
(var vert-radius float (* hor-radius vertical-size))
(var advance-hor float (fast-cos c vert-angle))
(var advance-ver float (fast-sin c vert-angle))
(if (< vert-radius (cast 1 float))
(*= vert-angle (cast 0.1 float)))
(+= pos-x (* (fast-cos c hor-angle) advance-hor))
(+= pos-y (clampf advance-ver (- vert-radius) vert-radius))
(+= pos-z (* (fast-sin c hor-angle) advance-hor))
(+= vert-angle (* (cast 0.1 float) vert-angle-change))
(var a float (.next-float r))
(var b float (.next-float r))
(var d float (.next-float r))
(= vert-angle-change (+ (* (cast 0.9 float) vert-angle-change)
(/ (* (- a b) d) (cast 3 float))))
;; the horizontal branches at the bottom
(if (< (- max-iterations current-iteration) 10)
(do
(var num int (+ 3 (.next-int r 4)))
(for (var i int 0) (< i num) (++ i)
(var h float (* (.next-float r) (* (cast 2 float) (cast PI-F float))))
(var v float (* (- (.next-float r) (cast 0.5 float)) (cast 0.25 float)))
(carve-tunnel c pos-x pos-y pos-z h v (+ horizontal-size (cast 1 float)) vertical-size
cave-current-iteration max-iterations 1 false (cast 0.1 float) false))
(return)))
(if (&& (== (.next-int r 5) 0) (>= hor-radius (cast 2 float)))
(continue))
(add-carve c pos-x pos-y pos-z hor-radius vert-radius false)))
;;; CarveTunnel.
(fn carve-tunnel ((c (* caves)) (pos-x double) (pos-y double) (pos-z double) (hor-angle float)
(vert-angle float) (horizontal-size float) (vertical-size float)
(current-iteration int) (max-iterations int) (branch-level int)
(extra-branchy bool) (curviness float) (large-near-lava bool)) void
(var r (* lcg) (& (. c cave-rand)))
(var sea int (. c sea-level))
(var pi float (cast PI-F float))
(var hor-angle-change float 0)
(var vert-angle-change float 0)
(var hor-radius-gain float 0)
(var hor-radius-loss float 0)
(var hor-radius-gain-accum float 0)
(var hor-radius-loss-accum float 0)
(var ver-height-gain float 0)
(var ver-height-loss float 0)
(var ver-height-gain-accum float 0)
(var ver-height-loss-accum float 0)
(var size-change-speed-accum float (cast 0.15 float))
(var size-change-speed-gain float 0)
(var branch-rand int (* (+ branch-level 1) (if extra-branchy 12 25)))
(while (< current-iteration max-iterations)
(++ current-iteration)
(var rel-pos float (/ (cast current-iteration float) (cast max-iterations float)))
(var s float (fast-sin c (* rel-pos pi)))
(var hor-radius float (+ (+ (cast 1.5 float) (* s horizontal-size)) hor-radius-gain-accum))
(= hor-radius (fminf hor-radius (fmaxf (cast 1 float) (- hor-radius hor-radius-loss-accum))))
(var vert-radius float (+ (+ (cast 1.5 float)
(* s (+ vertical-size (/ hor-radius-loss-accum (cast 4 float)))))
ver-height-gain-accum))
(= vert-radius (fminf vert-radius (fmaxf (cast 0.6 float) (- vert-radius ver-height-loss-accum))))
(var advance-hor float (fast-cos c vert-angle))
(var advance-ver float (fast-sin c vert-angle))
;; caves get bigger near y 12
(if large-near-lava
(do
(var near float (- (cast 1 float) (/ (cast (fabs (- pos-y 12.0)) float) (cast 10 float))))
(var factor float (+ (cast 1 float) (fmaxf (cast 0 float) near)))
(*= hor-radius factor)
(*= vert-radius factor)))
(if (< vert-radius (cast 1 float))
(*= vert-angle (cast 0.1 float)))
(+= pos-x (* (fast-cos c hor-angle) advance-hor))
(+= pos-y (clampf advance-ver (- vert-radius) vert-radius))
(+= pos-z (* (fast-sin c hor-angle) advance-hor))
(*= vert-angle (cast 0.8 float))
(var rrnd int (.next-int r 800000))
(if (== (/ rrnd 10000) 0)
(do
(var a float (.next-float r))
(var b float (.next-float r))
(= size-change-speed-gain (/ (* a b) (cast 2 float)))))
;; one draw picks among the rare changes: the game subtracts each
;; one's share in turn, which is these running totals
(var rnd int (% rrnd 10000))
(if (<= rnd 30)
(= hor-angle (* (.next-float r) (* (cast 2 float) pi)))
(<= rnd 106)
(+= hor-angle (- (* (.next-float r) pi) (/ pi (cast 2 float))))
(<= rnd 166)
(do
(var a float (.next-float r))
(var b float (.next-float r))
(= hor-radius-gain (* (* a b) (cast 3.5 float))))
(<= rnd 226)
(do
(var a float (.next-float r))
(var b float (.next-float r))
(= hor-radius-loss (* (* a b) (cast 10 float))))
(<= rnd 276)
(if (< pos-y (- sea 10))
(do
(var a float (.next-float r))
(var b float (.next-float r))
(= ver-height-loss (* (* a b) (cast 12 float)))
(var e float (.next-float r))
(var f float (.next-float r))
(= hor-radius-gain (fmaxf hor-radius-gain (* (* e f) (cast 3 float))))))
(<= rnd 285)
(if (< pos-y (- sea 20))
(do
(var a float (.next-float r))
(var b float (.next-float r))
(= hor-radius-gain (+ (cast 1 float) (* (* a b) (cast 5 float))))))
(<= rnd 294)
(do
(var a float (.next-float r))
(var b float (.next-float r))
(= ver-height-gain (+ (cast 2 float) (* (* a b) (cast 7 float)))))
(<= rnd 394)
(if (< pos-y 19)
(do
(var a float (.next-float r))
(var b float (.next-float r))
(= ver-height-gain (+ (cast 2 float) (* (* a b) (cast 5 float))))
(var e float (.next-float r))
(var f float (.next-float r))
(= hor-radius-gain (+ (cast 4 float) (* (* e f) (cast 9 float)))))))
;; deep and wide enough for a hot spring, if the carving gets there
(var hot bool (&& (> pos-y -5) (< pos-y 16) (> hor-radius (cast 4 float))
(> vert-radius (cast 2 float))))
(= size-change-speed-accum (fmaxf (cast 0.1 float) (+ size-change-speed-accum
(* size-change-speed-gain (cast 0.05 float)))))
(-= size-change-speed-gain (cast 0.02 float))
(= hor-radius-gain-accum (fmaxf (cast 0 float) (+ hor-radius-gain-accum (* hor-radius-gain size-change-speed-accum))))
(-= hor-radius-gain (cast 0.45 float))
(= hor-radius-loss-accum (fmaxf (cast 0 float) (+ hor-radius-loss-accum (* hor-radius-loss size-change-speed-accum))))
(-= hor-radius-loss (cast 0.4 float))
(= ver-height-gain-accum (fmaxf (cast 0 float) (+ ver-height-gain-accum (* ver-height-gain size-change-speed-accum))))
(-= ver-height-gain (cast 0.45 float))
(= ver-height-loss-accum (fmaxf (cast 0 float) (+ ver-height-loss-accum (* ver-height-loss size-change-speed-accum))))
(-= ver-height-loss (cast 0.4 float))
(+= hor-angle (* curviness hor-angle-change))
(+= vert-angle (* curviness vert-angle-change))
(var m1 float (.next-float-minus-to-plus-one r))
(var f1 float (.next-float r))
(= vert-angle-change (+ (* (cast 0.9 float) vert-angle-change) (* (* m1 f1) (cast 3 float))))
(var m2 float (.next-float-minus-to-plus-one r))
(var f2 float (.next-float r))
(= hor-angle-change (+ (* (cast 0.9 float) hor-angle-change) (* m2 f2)))
(if (== (% rrnd 140) 0)
(*= hor-angle-change (* (.next-float r) (cast 6 float))))
;; a horizontal branch, less likely above sea level
(var depth int (- (cast pos-y int) (- sea 20)))
(var brand int (+ branch-rand (* 2 (if (> depth 0) depth 0))))
(if (&& (< branch-level 3) (|| (> vert-radius (cast 1 float)) (> hor-radius (cast 1 float)))
(== (.next-int r brand) 0))
(do
(var a float (.next-float r))
(var b float (.next-float r))
(var h float (+ (+ hor-angle (- (+ a b) (cast 1 float))) pi))
(var e float (.next-float r))
(var f float (.next-float r))
(var v float (+ vert-angle (* (- e (cast 0.5 float)) (- f (cast 0.5 float)))))
(var k float (.next-float r))
(var shorter int (cast (* (* (cast k double) 0.5) max-iterations) int))
(carve-tunnel c pos-x (+ pos-y (/ ver-height-gain-accum (cast 2 float))) pos-z h v
horizontal-size (+ vertical-size ver-height-gain-accum) current-iteration
(- max-iterations shorter) (+ branch-level 1) false (cast 0.1 float) false)))
;; a vertical one
(if (&& (< branch-level 1) (> hor-radius (cast 3 float)) (> pos-y 60) (== (.next-int r 60) 0))
(do
(var a float (.next-float r))
(var b float (.next-float r))
(var h float (+ (+ hor-angle (- (+ a b) (cast 1 float))) pi))
(var e float (.next-float r))
(var v float (+ (- (- (/ pi (cast 2 float))) (cast 0.1 float)) (* (cast 0.2 float) e)))
(var k float (.next-float r))
(var shorter int (cast (* (* (cast k double) 0.5) max-iterations) int))
(var l float (.next-float r))
(var longer int (cast (* (/ pos-y 5) (cast (+ (cast 0.5 float) (* (cast 0.5 float) l)) double)) int))
(carve-shaft c pos-x (+ pos-y (/ ver-height-gain-accum (cast 2 float))) pos-z h v
(fminf (cast 3.5 float) (- hor-radius (cast 1 float)))
(+ vertical-size ver-height-gain-accum) current-iteration
(+ (- max-iterations shorter) longer) branch-level)
(++ branch-level)))
(if (&& (>= hor-radius (cast 2 float)) (== (% rrnd 5) 0))
(continue))
(add-carve c pos-x (+ pos-y (/ ver-height-gain-accum (cast 2 float))) pos-z hor-radius
(+ vert-radius ver-height-gain-accum) hot)))
;;; GeneratePartial's cave for source chunk (sx, sz), into T.
(fn trace-chunk ((c (* caves)) (t (* trace)) (sx int) (sz int)) void
(= (. t sx) sx)
(= (. t sz) sz)
(= (. t count) 0)
(= (. t valid) true)
(= (. c current) t)
(var r lcg (lcg/make (. c seed)))
(.init-position (& r) sx sz)
(if (! (< (.next-int (& r) 100) (* 0.37 100)))
(return))
(var rnd int (.next-int (& r) (* CHUNK CHUNK (- (. c size-y) 20))))
(var pos-x int (% rnd CHUNK))
(/= rnd CHUNK)
(var pos-z int (% rnd CHUNK))
(/= rnd CHUNK)
(var pos-y int (+ rnd 8))
(var pi float (cast PI-F float))
(var hor-angle float (* (.next-float (& r)) (* (cast 2 float) pi)))
(var vert-angle float (* (- (.next-float (& r)) (cast 0.5 float)) (cast 0.25 float)))
(var a float (.next-float (& r)))
(var b float (.next-float (& r)))
(var horizontal-size float (+ (* a (cast 2 float)) b))
(var vertical-size float (+ (cast 0.75 float) (* (.next-float (& r)) (cast 0.4 float))))
(= rnd (.next-int (& r) (* 100 50 1000 100)))
(if (< (% rnd 100) 4)
(do
(var e float (.next-float (& r)))
(var f float (.next-float (& r)))
(var g float (.next-float (& r)))
(= horizontal-size (+ (+ (* e (cast 2 float)) f) g))
(= vertical-size (+ (cast 0.25 float) (* (.next-float (& r)) (cast 0.2 float)))))
(== (% rnd 100) 4)
(do
(= horizontal-size (+ (cast 0.75 float) (.next-float (& r))))
(var e float (.next-float (& r)))
(var f float (.next-float (& r)))
(= vertical-size (+ (* e (cast 2 float)) f))))
(/= rnd 100)
(var extra-branchy bool (&& (< pos-y (/ (. c sea-level) 2)) (== (% rnd 50) 0)))
(/= rnd 50)
(var rnd1000 int (% rnd 1000))
(/= rnd 1000)
(var large-near-lava bool (< (% rnd1000 10) 3))
(var curviness float (cast 0.1 float))
(if (== rnd 0) (= curviness (cast 0.035 float))
(< rnd1000 30) (= curviness (cast 0.5 float)))
(var max-iterations int (- (* CAVE-RANGE CHUNK) (/ CHUNK 2)))
(-= max-iterations (.next-int (& r) (/ max-iterations 4)))
(.reseed (& (. c cave-rand)) (.next-int (& r) 10000000))
(.init-position (& (. c cave-rand)) sx sz)
(carve-tunnel c pos-x pos-y pos-z hor-angle vert-angle horizontal-size vertical-size 0
max-iterations 0 extra-branchy curviness large-near-lava))
;;; Source chunk (sx, sz)'s trace, traced once and kept.
(fn trace-of ((c (* caves)) (sx int) (sz int)) (* trace)
(var h u32 (+ (* (cast sx u32) 73856093) (* (cast sz u32) 19349663)))
(var t (* trace) (& [(. c traces) (% h TRACE-SLOTS)]))
(if (! (&& (. t valid) (== (. t sx) sx) (== (. t sz) sz)))
(trace-chunk c t sx sz))
(return t))
;;; Carving ----------------------------------------------------------
;;; getGeologicActivity: the climate map's low byte at the chunk.
(fn geologic-activity ((c (* caves)) (x int) (z int)) int
(var rx int (/ x 512))
(var rz int (/ z 512))
(if (|| (! (. c climate data)) (!= (. c climate-rx) rx) (!= (. c climate-rz) rz))
(do
(free (. c climate data))
(worldgen/map (. c g) MAP-CLIMATE 0 rx rz (& (. c climate)))
(= (. c climate-rx) rx)
(= (. c climate-rz) rz)))
(var m (* int-map) (& (. c climate)))
(var lx int (% (/ x CHUNK) 16))
(var lz int (% (/ z CHUNK) 16))
(return (& [(. m data) (+ (* (+ lz (. m pad-tl)) (. m size)) lx (. m pad-tl))] 255)))
;;; A target chunk column while it is carved.
(record column
((tx int) (tz int) (states (* u8)) (heights (* int)) (water-top (* int)) (distort [u8 1024])
(solid (* const u32)) (words int)
(candidates [spring-candidate MAX-CANDIDATES]) (count int)))
;;; The terrain pass put water in every block below sea level that it
;;; left open; with the terrain's solidity at hand that is exact, else
;;; it is the column above its surface.
(fn liquid-at ((c (* const caves)) (k (* const column)) (lx int) (y int) (lz int)) bool
(var i int (+ (* lz CHUNK) lx))
(if (< y (. c sea-level))
(if (. k solid)
(if (== (& (>> [(. k solid) (+ (* i (. k words)) (/ y 32))] (% y 32)) 1) 0)
(return true))
(> y [(. k water-top) i])
(return true)))
(return (== [(. k states) (+ (* (+ (* y CHUNK) lz) CHUNK) lx)] CAVE-LAVA)))
;;; SetBlocks, at centre (x, y, z) in the target chunk's coordinates.
(fn set-blocks ((c (* caves)) (k (* column)) (hor-radius float) (vert-radius float)
(x double) (y double) (z double) (hot bool)) void
(var top int (- (. c size-y) 1))
;; one size more, to see whether it would run into liquid
(+= hor-radius (cast 1 float))
(+= vert-radius (cast 2 float))
(var min-x int (cast (clampd (- x hor-radius) 0 (- CHUNK 1)) int))
(var max-x int (cast (clampd (+ (+ x hor-radius) 1) 0 (- CHUNK 1)) int))
(var min-y int (cast (clampd (- y (* vert-radius (cast 0.7 float))) 1 top) int))
(var max-y int (cast (clampd (+ (+ y vert-radius) 1) 1 top) int))
(var min-z int (cast (clampd (- z hor-radius) 0 (- CHUNK 1)) int))
(var max-z int (cast (clampd (+ (+ z hor-radius) 1) 0 (- CHUNK 1)) int))
(var h-sq double (* hor-radius hor-radius))
(var v-sq double (* vert-radius vert-radius))
(var distort-strength double (clampd (/ vert-radius 4.0) 0 0.1))
(for (var lx int min-x) (<= lx max-x) (++ lx)
(var xd double (/ (* (- lx x) (- lx x)) h-sq))
(for (var lz int min-z) (<= lz max-z) (++ lz)
(var zd double (/ (* (- lz z) (- lz z)) h-sq))
(var height-rnd double (* (- [(. k distort) (+ (* lz CHUNK) lx)] 127) distort-strength))
(for (var ly int min-y) (<= ly (+ max-y 10)) (++ ly)
(var dy double (- ly y))
(var off double 0)
(if (> dy 0) (= off (* height-rnd height-rnd)))
(var yd double (/ (* dy dy) (+ v-sq off)))
(if (|| (> (+ xd yd zd) 1.0) (> ly top))
(continue))
(if (liquid-at c k lx ly lz)
(return)))))
(-= hor-radius (cast 1 float))
(-= vert-radius (cast 2 float))
(= min-x (cast (clampd (- x hor-radius) 0 (- CHUNK 1)) int))
(= max-x (cast (clampd (+ (+ x hor-radius) 1) 0 (- CHUNK 1)) int))
(= min-z (cast (clampd (- z hor-radius) 0 (- CHUNK 1)) int))
(= max-z (cast (clampd (+ (+ z hor-radius) 1) 0 (- CHUNK 1)) int))
(= min-y (cast (clampd (- y (* vert-radius (cast 0.7 float))) 1 top) int))
(= max-y (cast (clampd (+ (+ y vert-radius) 1) 1 top) int))
(= h-sq (* hor-radius hor-radius))
(= v-sq (* vert-radius vert-radius))
(var geo int (geologic-activity c (+ (* (. k tx) CHUNK) (cast x int)) (+ (* (. k tz) CHUNK) (cast z int))))
;; a hot spring candidate, keyed by its whole centre as the game's
;; dictionary is: a later one at the same key takes its place there
(if (&& hot (> geo 128) (>= x 0) (< x CHUNK) (>= z 0) (< z CHUNK))
(do
(var cand spring-candidate #(spring-candidate : (cast x int) (cast y int) (cast z int) hor-radius))
(var i int 0)
(while (&& (< i (. k count))
(! (&& (== (. [(. k candidates) i] x) (. cand x)) (== (. [(. k candidates) i] y) (. cand y))
(== (. [(. k candidates) i] z) (. cand z)))))
(++ i))
(if (< i MAX-CANDIDATES)
(do
(= [(. k candidates) i] cand)
(if (== i (. k count)) (++ (. k count)))))))
(var lava-top int (/ (* geo 16) 128))
(for (var lx int min-x) (<= lx max-x) (++ lx)
(var xd double (/ (* (- lx x) (- lx x)) h-sq))
(for (var lz int min-z) (<= lz max-z) (++ lz)
(var zd double (/ (* (- lz z) (- lz z)) h-sq))
(var i int (+ (* lz CHUNK) lx))
(var height-rnd double (* (- [(. k distort) i] 127) distort-strength))
(var surface int [(. k heights) i])
(for (var ly int (+ max-y 10)) (>= ly min-y) (-- ly)
(var dy double (- ly y))
(var off double 0)
(if (> dy 0)
(do
(var near double (/ (abs (- ly surface)) 10.0))
(= off (* height-rnd height-rnd (fmin 1 near)))))
(var yd double (/ (* dy dy) (+ v-sq off)))
(if (|| (> ly top) (> (+ xd yd zd) 1.0))
(continue))
(if (== [(. k heights) i] ly)
(= [(. k heights) i] (- ly 1)))
(var b (* u8) (& [(. k states) (+ (* (+ (* ly CHUNK) lz) CHUNK) lx)]))
(if (== ly 11)
(if (> (.noise-2d (. c basalt-noise) (+ (* (. k tx) CHUNK) lx) (+ (* (. k tz) CHUNK) lz)) 0.65)
(do
(= (* b) CAVE-BASALT)
(if (< [(. k heights) i] 11) (= [(. k heights) i] 11)))
(> ly lava-top) (= (* b) CAVE-BASALT)
(= (* b) CAVE-LAVA))
(< ly 12)
(if (> ly lava-top) (= (* b) CAVE-BASALT) (= (* b) CAVE-LAVA))
(= (* b) CAVE-AIR))))))
;;; Carves chunk column (tx, tz): STATES, 32 x 32 x size-y by
;;; (y * 32 + z) * 32 + x, gets each block's CAVE- state; HEIGHTS, the
;;; terrain heights by z * 32 + x, are lowered where caves reach the
;;; surface. SOLID, terra/chunk-solidity's bits with WORDS per column,
;;; tells where the terrain left water; without it, NULL, water is
;;; taken to be the columns above their surface.
(fn slot-of ((c (* caves)) (cx int) (cz int)) (* carved)
(var h u32 (+ (* (cast cx u32) 73856093) (* (cast cz u32) 19349663)))
(return (& [(. c carved) (% h CARVED-SLOTS)])))
;;; Keeps a carved chunk's heights and candidates.
(fn keep-carved ((c (* caves)) (cx int) (cz int) (heights (* const int))
(candidates (* const spring-candidate)) (count int)) void
(var e (* carved) (slot-of c cx cz))
(= (. e cx) cx)
(= (. e cz) cz)
(= (. e valid) true)
(memcpy (. e heights) heights (sizeof (. e heights)))
(= (. e count) count)
(if (> count 0)
(memcpy (. e candidates) candidates (* count (sizeof spring-candidate)))))
(pub fn caves/carve-column ((c (* caves)) (tx int) (tz int) (heights (* int)) (solid (* const u32))
(words int) (states (* u8))) void
(memset states 0 (* CHUNK CHUNK (. c size-y)))
(if (worldgen/caves-blocked (. c g) (+ (* tx CHUNK) 16) (+ (* tz CHUNK) 16))
(do
(keep-carved c tx tz heights NULL 0)
(return)))
(var k column)
(= (. k count) 0)
(= (. k tx) tx)
(= (. k tz) tz)
(= (. k states) states)
(= (. k heights) heights)
(= (. k solid) solid)
(= (. k words) words)
;; the terrain pass left water in every column below sea level above
;; its surface
(var water-top [int 1024])
(memcpy water-top heights (sizeof water-top))
(= (. k water-top) water-top)
;; GenCaves.OnMapChunkGen: CaveHeightDistort
(for (var lz int 0) (< lz CHUNK) (++ lz)
(for (var lx int 0) (< lx CHUNK) (++ lx)
(var v double (- (.noise-2d (. c height-noise) (+ (* tx CHUNK) lx) (+ (* tz CHUNK) lz)) 0.5))
(if (> v 0)
(= v (fmax 0 (- v 0.07)))
(= v (fmin 0 (+ v 0.07))))
(= [(. k distort) (+ (* lz CHUNK) lx)] (cast (cast (+ (* 128 v) 127) int) u8))))
(for (var dx int (- CAVE-RANGE)) (<= dx CAVE-RANGE) (++ dx)
(for (var dz int (- CAVE-RANGE)) (<= dz CAVE-RANGE) (++ dz)
(var t (* trace) (trace-of c (+ tx dx) (+ tz dz)))
(for (var i int 0) (< i (. t count)) (++ i)
(var cv (* carve) (& [(. t carves) i]))
(var x double (+ (. cv x) (* dx CHUNK)))
(var z double (+ (. cv z) (* dz CHUNK)))
;; only what reaches this chunk
(var reach float (* (. cv hr) (cast 2 float)))
(if (|| (<= x (- reach)) (>= x (+ CHUNK reach)) (<= z (- reach)) (>= z (+ CHUNK reach)))
(continue))
(set-blocks c (& k) (. cv hr) (. cv vr) x (. cv y) z (. cv hot)))))
(keep-carved c tx tz heights (. k candidates) (. k count)))
;;; The hot spring candidates of the chunk column last carved at (cx,
;;; cz), into OUT; how many, or -1 if it isn't kept.
(pub fn caves/candidates ((c (* caves)) (cx int) (cz int) (out (* spring-candidate)) (max int)) int
(var e (* carved) (slot-of c cx cz))
(if (! (&& (. e valid) (== (. e cx) cx) (== (. e cz) cz)))
(return -1))
(var n int (if (< (. e count) max) (. e count) max))
(if (> n 0)
(memcpy out (. e candidates) (* n (sizeof spring-candidate))))
(return n))
;;; Columns ----------------------------------------------------------
;;; How a column's caves show on the map: none; an entrance, where a
;;; cave reached the surface; or how deep below the surface the topmost
;;; one starts.
(pub define CAVE-NONE 0)
(pub define CAVE-ENTRANCE 1)
;;; Not a column's own: what the Caves overlay paints over a hot spring.
(pub define CAVE-HOT-SPRING 7)
(pub define CAVE-CATEGORIES 8)
(var depth-bands [int 4] #(10 25 50 100))
;;; The category of column (lx, lz) of a carved chunk column: SURFACE is
;;; its terrain height before carving, CARVED after.
(pub fn caves/category ((c (* const caves)) (states (* const u8)) (lx int) (lz int) (surface int)
(carved int)) int
(if (< carved surface)
(return CAVE-ENTRANCE))
(for (var y int surface) (>= y 1) (-- y)
(var s u8 [states (+ (* (+ (* y CHUNK) lz) CHUNK) lx)])
(if (|| (== s CAVE-AIR) (== s CAVE-LAVA))
(do
(var depth int (- surface y))
(for (var b int 0) (< b 4) (++ b)
(if (<= depth [depth-bands b])
(return (+ 2 b))))
(return 6))))
(return CAVE-NONE))
;;; A column's caves as runs of air or lava, top down: run i is from
;;; TOPS[i] down to BOTTOMS[i], lava when LAVA[i]. Returns how many, up
;;; to MAX.
(pub fn caves/runs ((c (* const caves)) (states (* const u8)) (lx int) (lz int) (surface int)
(tops (* int)) (bottoms (* int)) (lava (* bool)) (max int)) int
(var n int 0)
(var prev u8 CAVE-UNTOUCHED)
(for (var y int surface) (>= y 1) (-- y)
(var s u8 [states (+ (* (+ (* y CHUNK) lz) CHUNK) lx)])
(if (== s CAVE-BASALT) (= s CAVE-UNTOUCHED))
(if (&& (!= s CAVE-UNTOUCHED) (== s prev) (> n 0))
(= [bottoms (- n 1)] y)
(&& (!= s CAVE-UNTOUCHED) (< n max))
(do
(= [tops n] y)
(= [bottoms n] y)
(= [lava n] (== s CAVE-LAVA))
(++ n)))
(= prev s))
(return n))
;;; Hot springs ------------------------------------------------------
;;; A hot spring GenHotSprings makes: a disc of RADIUS around (x, z) at
;;; height y. Its middle, out to about a third of the radius, is boiling
;;; water, the rest sludgy gravel and bacterial mats; where exactly the
;;; edge of each falls the game rolls with a random generator of its
;;; own.
(pub record hot-spring ((x int) (y int) (z int) (radius double)))
;;; Chunk column (cx, cz) carved, from what is kept or carved now.
(fn carved-of ((c (* caves)) (tr (* terra)) (cx int) (cz int)) (* carved)
(var e (* carved) (slot-of c cx cz))
(if (&& (. e valid) (== (. e cx) cx) (== (. e cz) cz))
(return e))
(var words int (terra/solid-words tr))
(if (! (. c scratch-states))
(do
(= (. c scratch-states) (malloc (* CHUNK CHUNK (. c size-y))))
(= (. c scratch-solid) (malloc (* 1024 words (sizeof u32))))))
(var heights16 [u16 1024])
(var heights [int 1024])
(terra/chunk-solidity tr cx cz heights16 (. c scratch-solid))
(for (var i int 0) (< i 1024) (++ i)
(= [heights i] [heights16 i]))
(caves/carve-column c cx cz heights (. c scratch-solid) words (. c scratch-states))
(return (slot-of c cx cz)))
(fn surface-at ((c (* caves)) (tr (* terra)) (x int) (z int)) int
(var e (* carved) (carved-of c tr (/ x CHUNK) (/ z CHUNK)))
(return [(. e heights) (+ (* (% z CHUNK) CHUNK) (% x CHUNK))]))
;;; GenHotSprings for chunk column (cx, cz): its candidates that become
;;; hot springs, into OUT; how many, up to MAX. A candidate is kept
;;; unless the ground over twice its radius is rough (4 or more blocks
;;; between highest and lowest), anywhere below sea level + 1, or high
;;; in the mountains. The game also leaves out one with water standing
;;; at the surface; above sea level that can only be a pond, which comes
;;; later, or a neighbour's, which it does not see here.
(pub fn caves/hot-springs ((c (* caves)) (tr (* terra)) (cx int) (cz int) (out (* hot-spring)) (max int))
int
(if (worldgen/springs-blocked (. c g) (+ (* cx CHUNK) 16) (+ (* cz CHUNK) 16))
(return 0))
(var candidates [spring-candidate MAX-CANDIDATES])
(var e (* carved) (carved-of c tr cx cz))
(var count int (. e count))
(memcpy candidates (. e candidates) (* count (sizeof spring-candidate)))
(var n int 0)
(var base-x int (* cx CHUNK))
(var base-z int (* cz CHUNK))
(for (var k int 0) (&& (< k count) (< n max)) (++ k)
(var cand (* spring-candidate) (& [candidates k]))
(var radius double (* 2 (. cand radius)))
(var min-x int (cast (clampd (- (. cand x) radius) (- CHUNK) (- (* 2 CHUNK) 1)) int))
(var max-x int (cast (clampd (+ (+ (. cand x) radius) 1) (- CHUNK) (- (* 2 CHUNK) 1)) int))
(var min-z int (cast (clampd (- (. cand z) radius) (- CHUNK) (- (* 2 CHUNK) 1)) int))
(var max-z int (cast (clampd (+ (+ (. cand z) radius) 1) (- CHUNK) (- (* 2 CHUNK) 1)) int))
(var r-sq double (* radius radius))
(var lowest int 99999)
(var highest int 0)
(var checks int 0)
(var sum i64 0)
(for (var lx int min-x) (<= lx max-x) (++ lx)
(var xd double (/ (* (- lx (. cand x)) (- lx (. cand x))) r-sq))
(for (var lz int min-z) (<= lz max-z) (++ lz)
(var zd double (/ (* (- lz (. cand z)) (- lz (. cand z))) r-sq))
(if (>= (+ xd zd) 1)
(continue))
(var surface int (surface-at c tr (+ base-x lx) (+ base-z lz)))
(if (< surface lowest) (= lowest surface))
(if (> surface highest) (= highest surface))
(++ checks)
(+= sum surface)))
(if (== checks 0)
(continue))
;; Math.Round rounds halves to even, as rint does
(var average int (cast (rint (/ (cast sum double) checks)) int))
(if (|| (>= (- highest lowest) 4) (< lowest (+ (. c sea-level) 1))
(> lowest (* (cast (. c size-y) float) (cast 0.88 float))))
(continue))
(= [out n] #(hot-spring : (+ base-x (. cand x)) average (+ base-z (. cand z)) radius))
(++ n))
(return n))

View File

@@ -15,9 +15,14 @@
(import terra)
(import strata)
(pub import ores)
(import caves)
;;; MINERAL is the mineral whose reading is wanted, or -1.
(pub record hover-query ((x int) (z int) (mode int) (value int) (mineral int)))
;;; The most cave runs a column's answer lists.
(pub define MAX-CAVE-RUNS 4)
;;; MINERAL is the mineral whose reading is wanted, or -1; CAVES whether
;;; the caves are.
(pub record hover-query ((x int) (z int) (mode int) (value int) (mineral int) (caves bool)))
(pub record hover-result
((valid bool) (query hover-query)
@@ -29,8 +34,11 @@
(rock int) (rock-y int) (surface-rock int)
(landform int) (province int) (climate int) (ocean int) (upheaval int)
(river-distance double) (bank double)
;; the pick's reading of the query's mineral
(mineral mineral-reading)))
;; the pick's reading of the query's mineral, and its ore map's value
(mineral mineral-reading) (mineral-map double)
;; the column's caves, top down, when asked for
(cave-entrance bool) (cave-runs int) (cave-tops [int MAX-CAVE-RUNS])
(cave-bottoms [int MAX-CAVE-RUNS]) (cave-lava [bool MAX-CAVE-RUNS])))
(pub record hover-worker
((lock pthread-mutex-t) (wake pthread-cond-t) (thread pthread-t) (quit bool)
@@ -50,7 +58,11 @@
;; the ore map last asked for: its mineral and region
(ore-map int-map) (ore-mineral int) (ore-rx int) (ore-rz int)
(plate (* river-plate)) (segments (* int)) (sampler river-sampler)
(column (* u8))))
(column (* u8))
;; the chunk last carved for caves: its heights before and after, and
;; its blocks' states
(caves (* caves)) (cave-cx int) (cave-cz int) (has-cave-chunk bool) (cave-states (* u8))
(cave-before [int 1024]) (cave-after [int 1024]) (cave-solid (* u32))))
(var region-kinds [int 5] #(0 2 1 4 3))
@@ -61,6 +73,9 @@
(if (. k plate) (river-plate/free (. k plate)))
(free (. k ore-map data))
(if (. k minerals) (ores/free (. k minerals)))
(if (. k caves) (caves/free (. k caves)))
(free (. k cave-states))
(free (. k cave-solid))
(if (. k rocks) (strata/free (. k rocks)))
(if (. k tr) (terra/free (. k tr)))
(if (. k g) (worldgen/free (. k g)))
@@ -110,6 +125,27 @@
(= (. r sea-level) (. (. k tr) sea-level))
(strata/column (. k rocks) (/ x 32) (/ z 32) (% x 32) (% z 32) h (. k column))
(= (. r surface-rock) [(. k column) h])
(if (&& (. q caves) (. k caves))
(do
(var cx int (/ x 32))
(var cz int (/ z 32))
(if (|| (! (. k has-cave-chunk)) (!= (. k cave-cx) cx) (!= (. k cave-cz) cz))
(do
(var heights [u16 1024])
(terra/chunk-solidity (. k tr) cx cz heights (. k cave-solid))
(for (var i int 0) (< i 1024) (++ i)
(= [(. k cave-before) i] [heights i])
(= [(. k cave-after) i] [heights i]))
(caves/carve-column (. k caves) cx cz (. k cave-after) (. k cave-solid)
(terra/solid-words (. k tr)) (. k cave-states))
(= (. k cave-cx) cx)
(= (. k cave-cz) cz)
(= (. k has-cave-chunk) true)))
(var i int (+ (* (% z 32) 32) (% x 32)))
(= (. r cave-entrance) (< [(. k cave-after) i] [(. k cave-before) i]))
(= (. r cave-runs) (caves/runs (. k caves) (. k cave-states) (% x 32) (% z 32)
[(. k cave-before) i] (. r cave-tops) (. r cave-bottoms)
(. r cave-lava) MAX-CAVE-RUNS))))
(if (&& (. k minerals) (>= (. q mineral) 0) (< (. q mineral) (. (. k minerals) count)))
(do
(if (|| (! (. k ore-map data)) (!= (. k ore-mineral) (. q mineral)) (!= (. k ore-rx) rx)
@@ -120,7 +156,8 @@
(= (. k ore-mineral) (. q mineral))
(= (. k ore-rx) rx)
(= (. k ore-rz) rz)))
(= (. r mineral) (ores/reading (. k minerals) (. q mineral) (& (. k ore-map)) x z (. k column) h))))
(= (. r mineral) (ores/reading (. k minerals) (. q mineral) (& (. k ore-map)) x z (. k column) h))
(= (. r mineral-map) (ores/map-factor (& (. k ore-map)) x z))))
(if (== (. q mode) 3)
(do
(= (. r rock) -3)
@@ -168,6 +205,9 @@
(= (. k tr) (terra/make (. k g) (. w rivers)))
(= (. k rocks) (strata/make (. k g) (. w assets-dir)))
(= (. k minerals) (ores/make settings (. w assets-dir) (. k rocks)))
(= (. k caves) (caves/make (. k g)))
(= (. k cave-states) (malloc (* 1024 (. settings size-y))))
(= (. k cave-solid) (malloc (* 1024 (terra/solid-words (. k tr)) (sizeof u32))))
(= (. k sampler) (river-sampler/make (& (. w rivers)) (. settings size-y)))
(= (. k column) (malloc (+ (. settings size-y) 1)))))
(= (. k generation) generation)))
@@ -206,6 +246,7 @@
(pthread-mutex-lock (& (. w lock)))
(if (|| (!= (. w query x) (. q x)) (!= (. w query z) (. q z)) (!= (. w query mode) (. q mode))
(!= (. w query value) (. q value)) (!= (. w query mineral) (. q mineral))
(!= (. w query caves) (. q caves))
(! (. w result valid)))
(do
(= (. w query) q)

View File

@@ -62,6 +62,12 @@
(pub fn next-float ((self (* lcg))) float
(return (/ (cast (.next-bits self) float) (cast INT32_MAX float))))
;; NextFloatMinusToPlusOne: -1 to 1, most likely near 0, the
;; difference of two numbers below 46341 that one draw holds.
(pub fn next-float-minus-to-plus-one ((self (* lcg))) float
(var r int (.next-bits self))
(return (/ (cast (- (% r 46341) (/ r 46341)) float) (cast 46340 float))))
(pub fn next-double ((self (* lcg))) double
(var r u32 (cast (>> (cast (. self current) i64) 24) u32))
(.advance self)

View File

@@ -17,7 +17,8 @@
;;; where the game may already have put ore in some of it. And a
;;; child deposit (silver, gold, and peridot, whose reading comes from
;;; the peridot inside olivine) is hosted by its parent's ore, so it
;;; can't be read at all without the deposits.
;;; can't be read at all without the deposits. For those, only the
;;; ore map is shown.
(include stdio.h stdlib.h string.h math.h dirent.h)
(import cs)
@@ -463,8 +464,9 @@
;;; Readings ---------------------------------------------------------
;;; IntDataMap2D.GetUnpaddedColorLerped, the low byte only.
(fn ore-factor ((map (* const int-map)) (x int) (z int)) double
;;; The ore map's value at column (x, z), 0 to 1: the low byte of
;;; IntDataMap2D.GetUnpaddedColorLerped over 255.
(pub fn ores/map-factor ((map (* const int-map)) (x int) (z int)) double
(var inner int (- (. map size) (. map pad-tl) (. map pad-br)))
(var px float (* (/ (cast (% x 512) float) (cast 512 float)) (cast inner float)))
(var pz float (* (/ (cast (% z 512) float) (cast 512 float)) (cast inner float)))
@@ -515,7 +517,7 @@
(if (&& (< rock 32) (!= (& (. m bearing) (<< (cast 1 u32) rock)) 0))
(++ q)))
(var rock-factor double (/ (cast q double) height))
(= (. r total) (* (ore-factor map x z) rock-factor))
(= (. r total) (* (ores/map-factor map x z) rock-factor))
(var blocks int (* height 1024))
(= (. r ppt) (* (/ (* (. r total) (. m abs-avg)) blocks) 1000))
(return r))
@@ -530,3 +532,21 @@
(var k double (* total (cast 7.5 float)))
(if (> k 5) (= k 5))
(return (+ 2 (cast k int))))
;;; A band of the ore map's value, for a mineral the pick can't read:
;;; 0 where it is 0, then 1 to 7 by sevenths.
(pub fn ores/map-category ((factor double)) int
(if (<= factor 0)
(return 0))
(var k int (cast (* factor 7) int))
(if (> k 6) (= k 6))
(return (+ 1 k)))
;;; What the Minerals overlay shows for mineral I at a column, 0 to
;;; READING-CATEGORIES - 1: the pick's reading where it can read the
;;; mineral, else the ore map's band.
(pub fn ores/category-at ((o (* const ores)) (i int) (map (* const int-map)) (x int) (z int)
(column (* const u8)) (height int)) int
(if (. [(. o minerals) i] readable)
(return (ores/category (. (ores/reading o i map x z column height) total))))
(return (ores/map-category (ores/map-factor map x z))))

View File

@@ -7,7 +7,7 @@
;;; data itself, matched against the map sizes in TerraGenConfig.
(pub include sqlite3.h)
(include stdio.h stdlib.h string.h)
(include stdio.h stdlib.h string.h zstd.h)
(import pb)
(import maps)
@@ -15,6 +15,7 @@
(pub define REGION-SIZE 512)
(pub define CHUNK-SIZE 32)
(pub define CHUNK-AREA 1024)
(pub define CHUNK-VOLUME 32768)
(pub define MAX-STORIES 32)
(pub record chunk-maps
@@ -34,7 +35,10 @@
(block-codes (* * char)) (block-count int)
(stories [story-location MAX-STORIES]) (story-count int)
;; every player's stored pick readings
(pick-readings (* pick-reading)) (pick-reading-count int)))
(pick-readings (* pick-reading)) (pick-reading-count int)
;; SaveGame.DefaultSpawn, when the world has one: where the game counts
;; its coordinates from
(has-spawn bool) (spawn-x i32) (spawn-y i32) (spawn-z i32)))
;;; Map regions and map chunks share one key: x in the low 27 bits,
;;; z above them.
@@ -232,6 +236,17 @@
(== f 3) (= (. s size-z) (.int32 (& r)))
(== f 7) (= (. s seed) (.int32 (& r)))
(== f 31) (read-tree-attribute s (.bytes (& r)))
;; DefaultSpawn, a PlayerSpawnPos: {1: x, 2: y, 3: z, ...}
(== f 40)
(do
(var spawn pb (.bytes (& r)))
(var g u32)
(= (. s has-spawn) true)
(while (> (= g (.field (& spawn) (& wire))) 0)
(if (== g 1) (= (. s spawn-x) (.int32 (& spawn)))
(== g 2) (= (. s spawn-y) (.int32 (& spawn)))
(== g 3) (= (. s spawn-z) (.int32 (& spawn)))
(.skip (& spawn) wire))))
(== f 11)
(do
(var entry pb (.bytes (& r)))
@@ -382,6 +397,38 @@
(sqlite3-finalize stmt)
(return n))
;;; ServerMapRegion field 11, GeneratedStructures: one {1: code, 3: box
;;; {1: x1, 2: y1, 3: z1, 4: x2, 5: y2, 6: z2}} per structure the
;;; region's chunks placed. Fills up to MAX boxes, six ints each, and
;;; returns how many.
(pub fn save/structure-boxes ((s (* const save)) (rx i32) (rz i32) (boxes (* i32)) (max int)) int
(var stmt (* sqlite3-stmt) NULL)
(var r pb (query-blob s "select data from mapregion where position = ?1"
(position rx rz) (& stmt)))
(var n int 0)
(var wire int)
(var f u32)
(while (&& (! (. r err)) (> (= f (.field (& r) (& wire))) 0))
(if (|| (!= f 11) (>= n max))
(do (.skip (& r) wire) (continue)))
(var entry pb (.bytes (& r)))
(var g u32)
(while (> (= g (.field (& entry) (& wire))) 0)
(if (== g 3)
(do
(var box pb (.bytes (& entry)))
(var b (* i32) (+ boxes (* n 6)))
(memset b 0 (* 6 (sizeof i32)))
(var k u32)
(while (> (= k (.field (& box) (& wire))) 0)
(if (&& (>= k 1) (<= k 6))
(= [b (- k 1)] (.int32 (& box)))
(.skip (& box) wire)))
(++ n))
(.skip (& entry) wire))))
(sqlite3-finalize stmt)
(return n))
;;; Map chunks -------------------------------------------------------
;;; A repeated ushort field arrives one value per field, or packed.
@@ -443,6 +490,146 @@
(sqlite3-finalize stmt)
(return found))
;;; Chunks ------------------------------------------------------------
;;; A zstd frame, whole, into a fresh buffer; NULL if it isn't one.
(fn unzstd ((p (* const u8)) (len size-t) (out-len (* size-t))) (* u8)
(var size u64 (ZSTD-getFrameContentSize p len))
(if (>= size ZSTD-CONTENTSIZE-ERROR)
(return NULL))
(var out (* u8) (malloc (+ size 1)))
(var got size-t (ZSTD-decompress out size p len))
(if (ZSTD-isError got)
(do (free out) (return NULL)))
(= (* out-len) got)
(return out))
;;; A chunk's block layer: an int32, then the palette of block ids,
;;; raw in -n bytes if it is negative and zstd in n if not; then a zstd
;;; frame of bit planes, 4096 bytes each, plane k holding bit k of each
;;; block's palette index, block i at bit i % 8 of byte i / 8. A layer
;;; of 0 alone holds no blocks: all air.
(fn read-blocks ((p (* const u8)) (len size-t) (out (* i32))) bool
(if (< len 4)
(return false))
(var n i32 (cast (c-bit-or [p 0] (<< [p 1] 8) (<< [p 2] 16) (<< (cast [p 3] u32) 24)) i32))
(if (&& (== n 0) (== len 4))
(do
(memset out 0 (* CHUNK-VOLUME (sizeof i32)))
(return true)))
(var palette (* u8) NULL)
(var palette-len size-t 0)
(var rest (* const u8) NULL)
(if (< n 0)
(do
(= palette-len (cast (- n) size-t))
(if (> (+ 4 palette-len) len) (return false))
(= palette (malloc palette-len))
(memcpy palette (+ p 4) palette-len)
(= rest (+ p 4 palette-len)))
(do
(if (> (+ 4 (cast n size-t)) len) (return false))
(= palette (unzstd (+ p 4) (cast n size-t) (& palette-len)))
(= rest (+ p 4 n))))
(var planes-len size-t 0)
(var planes (* u8) (if palette (unzstd rest (cast (- (+ p len) rest) size-t) (& planes-len)) NULL))
(var count int (cast (/ palette-len 4) int))
(var bits int (cast (/ planes-len 4096) int))
(var ok bool (&& palette planes (> count 0)))
(if ok
(do
(var index (* i32) (calloc CHUNK-VOLUME (sizeof i32)))
(for (var k int 0) (< k bits) (++ k)
(var plane (* const u8) (+ planes (* k 4096)))
(for (var i int 0) (< i CHUNK-VOLUME) (++ i)
(if (& (>> [plane (/ i 8)] (% i 8)) 1)
(c-bit-or= [index i] (<< 1 k)))))
(for (var i int 0) (< i CHUNK-VOLUME) (++ i)
(var k int [index i])
(var id i32 -1)
(if (< k count)
(memcpy (& id) (+ palette (* k 4)) 4))
(= [out i] id))
(free index)))
(free palette)
(free planes)
(return ok))
;;; The blocks of stored chunk (cx, cy, cz), by (y * 32 + z) * 32 + x,
;;; as block ids (see save/block-code). False when the save has no such
;;; chunk or its blocks don't read.
(pub fn save/chunk-blocks ((s (* const save)) (cx i32) (cy i32) (cz i32) (out (* i32))) bool
(var stmt (* sqlite3-stmt) NULL)
(var key i64 (c-bit-or (position cx cz) (<< (cast cy i64) 54)))
(var r pb (query-blob s "select data from chunk where position = ?1" key (& stmt)))
(var ok bool false)
(if (! (. r err))
(do
(var wire int)
(var f u32)
(while (> (= f (.field (& r) (& wire))) 0)
(if (== f 1)
(do
(var blocks pb (.bytes (& r)))
(= ok (read-blocks (. blocks p) (.len blocks) out)))
(.skip (& r) wire)))))
(sqlite3-finalize stmt)
(return ok))
;;; A hot spring candidate GenCaves stored in a map chunk's
;;; "hotspringlocations": centre within the chunk, and radius.
(pub record saved-spring ((x i32) (y i32) (z i32) (radius double)))
;;; The map chunk's "hotspringlocations" (mod data, field 1): a
;;; Dictionary<Vec3i, HotSpringGenData> of {1: {1: x, 2: y, 3: z},
;;; 2: {1: radius}} entries. Fills up to MAX and returns how many.
(pub fn save/hot-spring-locations ((s (* const save)) (cx i32) (cz i32) (out (* saved-spring)) (max int))
int
(var stmt (* sqlite3-stmt) NULL)
(var r pb (query-blob s "select data from mapchunk where position = ?1" (position cx cz) (& stmt)))
(var n int 0)
(var wire int)
(var f u32)
(while (&& (! (. r err)) (> (= f (.field (& r) (& wire))) 0))
(if (!= f 1)
(do (.skip (& r) wire) (continue)))
(var entry pb (.bytes (& r)))
(var key pb #(pb : NULL NULL false))
(var value pb #(pb : NULL NULL false))
(var g u32)
(while (> (= g (.field (& entry) (& wire))) 0)
(if (== g 1) (= key (.bytes (& entry)))
(== g 2) (= value (.bytes (& entry)))
(.skip (& entry) wire)))
(if (! (.equals key "hotspringlocations"))
(continue))
(while (&& (< n max) (> (.field (& value) (& wire)) 0))
(var item pb (.bytes (& value)))
(var spring saved-spring #(saved-spring : 0 0 0 0))
(var h u32)
(while (> (= h (.field (& item) (& wire))) 0)
(if (== h 1)
(do
(var pos pb (.bytes (& item)))
(var k u32)
(while (> (= k (.field (& pos) (& wire))) 0)
(if (== k 1) (= (. spring x) (.int32 (& pos)))
(== k 2) (= (. spring y) (.int32 (& pos)))
(== k 3) (= (. spring z) (.int32 (& pos)))
(.skip (& pos) wire))))
(== h 2)
(do
(var data pb (.bytes (& item)))
(var k u32)
(while (> (= k (.field (& data) (& wire))) 0)
(if (== k 1) (= (. spring radius) (read-double (& data)))
(.skip (& data) wire))))
(.skip (& item) wire)))
(= [out n] spring)
(++ n)))
(sqlite3-finalize stmt)
(return n))
;;; The stored extent of a table, in its own units (regions or chunks):
;;; inclusive bounds and the row count.
(pub record extent ((x0 i32) (z0 i32) (x1 i32) (z1 i32) (count i64)))

View File

@@ -1,6 +1,7 @@
;;; Finds the block nearest a point where one of a set of rocks lies, at
;;; the surface, at a depth below it, at a Y or at any depth; or where
;;; the prospecting pick reads a mineral at least so high.
;;; the surface, at a depth below it, at a Y or at any depth; where the
;;; prospecting pick reads a mineral at least so high; where a cave
;;; opens at the surface; or a hot spring.
;;;
;;; Regions are taken in order of their distance from the point and
;;; sampled every 16 blocks by several threads at once, each with its
@@ -17,11 +18,16 @@
(import terra)
(import strata)
(import ores)
(import caves)
(pub define SEARCH-RADIUS-REGIONS 24)
;;; The mode that looks for a mineral: the value is the mineral, the
;;; mask the least reading category.
(pub define SEARCH-MINERAL 4)
;;; The mode that looks for a cave entrance.
(pub define SEARCH-CAVE-ENTRANCE 5)
;;; The mode that looks for a hot spring.
(pub define SEARCH-HOT-SPRING 6)
(define SEARCH-STEP 16)
(pub define MAX-SEARCH-THREADS 16)
@@ -44,10 +50,11 @@
(return (- (> a b) (< a b))))
;;; A thread's generators, and the searched mineral's ore map of the
;;; region it last scanned in.
;;; region it last scanned in; for caves, a chunk's terrain and states.
(record scanner
((tr (* terra)) (rocks (* strata)) (minerals (* ores)) (column (* u8))
(ore-map int-map) (ore-rx int) (ore-rz int)))
(ore-map int-map) (ore-rx int) (ore-rz int)
(caves (* caves)) (solid (* u32)) (states (* u8))))
(fn region-distance ((s (* const search)) (rx int) (rz int)) double
(var x0 double (* rx 512.0))
@@ -72,7 +79,8 @@
;;; Whether column (x, z), with the strata in the scanner's column,
;;; holds what is searched for: a rock where the mode looks, at any
;;; depth (mode 3) anywhere from y = 1 to the surface; or a reading of
;;; the mineral of at least the mask's category.
;;; the mineral, or its ore map's band where the pick can't read it, of
;;; at least the mask's category.
(fn column-matches ((s (* const search)) (sc (* scanner)) (x int) (z int) (height int)) bool
(var column (* const u8) (. sc column))
(if (== (. s mode) SEARCH-MINERAL)
@@ -85,9 +93,9 @@
(ores/map (. sc minerals) (. s value) rx rz (& (. sc ore-map)))
(= (. sc ore-rx) rx)
(= (. sc ore-rz) rz)))
(var r mineral-reading (ores/reading (. sc minerals) (. s value) (& (. sc ore-map)) x z
column height))
(return (>= (ores/category (. r total)) (cast (. s mask) int)))))
(return (>= (ores/category-at (. sc minerals) (. s value) (& (. sc ore-map)) x z column
height)
(cast (. s mask) int)))))
(if (!= (. s mode) 3)
(return (matches s (rock-at column height (. s mode) (. s value)))))
(for (var y int 1) (<= y height) (++ y)
@@ -111,6 +119,27 @@
;;; Every STEP-th column of chunk (cx, cz) against the mask.
(fn scan-chunk ((s (* search)) (sc (* scanner)) (cx int) (cz int) (step int)) void
(var heights [u16 1024])
;; caves are carved a chunk at a time, so every column is looked at:
;; an entrance can be a block wide
(if (== (. s mode) SEARCH-HOT-SPRING)
(do
(var springs [hot-spring MAX-CANDIDATES])
(var n int (caves/hot-springs (. sc caves) (. sc tr) cx cz springs MAX-CANDIDATES))
(for (var i int 0) (< i n) (++ i)
(offer s (. [springs i] x) (. [springs i] z)))
(return)))
(if (== (. s mode) SEARCH-CAVE-ENTRANCE)
(do
(var carved [int 1024])
(terra/chunk-solidity (. sc tr) cx cz heights (. sc solid))
(for (var i int 0) (< i 1024) (++ i)
(= [carved i] [heights i]))
(caves/carve-column (. sc caves) cx cz carved (. sc solid) (terra/solid-words (. sc tr))
(. sc states))
(for (var i int 0) (< i 1024) (++ i)
(if (< [carved i] [heights i])
(offer s (+ (* cx 32) (% i 32)) (+ (* cz 32) (/ i 32)))))
(return)))
(terra/chunk-heights (. sc tr) cx cz step heights)
(for (var lz int 0) (< lz 32) (+= lz step)
(for (var lx int 0) (< lx 32) (+= lx step)
@@ -131,7 +160,13 @@
(if (&& (. sc rocks) (== (. s mode) SEARCH-MINERAL))
(= (. sc minerals) (ores/make (. s settings) (. s assets-dir) (. sc rocks))))
(= (. sc column) (malloc (+ (. s settings size-y) 1)))
(var ready bool (&& (. sc tr) (. sc rocks) (|| (!= (. s mode) SEARCH-MINERAL) (. sc minerals))))
(if (&& g (. sc tr) (|| (== (. s mode) SEARCH-CAVE-ENTRANCE) (== (. s mode) SEARCH-HOT-SPRING)))
(do
(= (. sc caves) (caves/make g))
(= (. sc solid) (malloc (* 1024 (terra/solid-words (. sc tr)) (sizeof u32))))
(= (. sc states) (malloc (* 1024 (. s settings size-y))))))
(var ready bool (&& (. sc tr) (. sc rocks) (|| (!= (. s mode) SEARCH-MINERAL) (. sc minerals))
(|| (< (. s mode) SEARCH-CAVE-ENTRANCE) (. sc caves))))
(while ready
(pthread-mutex-lock (& (. s lock)))
(var i int (. s next))
@@ -171,6 +206,9 @@
(scan-chunk s (& sc) cx cz 1)))))
(free (. sc column))
(free (. sc ore-map data))
(if (. sc caves) (caves/free (. sc caves)))
(free (. sc solid))
(free (. sc states))
(if (. sc minerals) (ores/free (. sc minerals)))
(if (. sc rocks) (strata/free (. sc rocks)))
(if (. sc tr) (terra/free (. sc tr)))

View File

@@ -8,8 +8,10 @@
(import save)
(pub import worldgen)
;;; Settings plus the seed as the user would type it.
(pub record world-source ((settings world-settings) (seed-text [char 128])))
;;; Settings plus the seed as the user would type it, and the world's
;;; spawn if it names one; the game's default is the map's middle.
(pub record world-source
((settings world-settings) (seed-text [char 128]) (has-spawn bool) (spawn-x int) (spawn-z int)))
(fn ends-with ((s (* const char)) (suffix (* const char))) bool
(var n size-t (strlen s))
@@ -28,6 +30,9 @@
(= (. out settings size-y) (. s size-y))
(= (. out settings size-z) (. s size-z))
(snprintf (. out seed-text) (sizeof (. out seed-text)) "%d" (. s seed))
(= (. out has-spawn) (. s has-spawn))
(= (. out spawn-x) (. s spawn-x))
(= (. out spawn-z) (. s spawn-z))
(save/close s)
(return true))
@@ -48,6 +53,14 @@
(var map-size-y (* cJSON) (cJSON-GetObjectItem world "MapSizeY"))
(if (cJSON-IsNumber map-size-y)
(= (. out settings size-y) (cast (cJSON-GetNumberValue map-size-y) int)))
;; DefaultSpawn: a PlayerSpawnPos, null unless set
(var spawn (* cJSON) (cJSON-GetObjectItem json "DefaultSpawn"))
(if (&& (cJSON-IsObject spawn) (cJSON-IsNumber (cJSON-GetObjectItem spawn "x"))
(cJSON-IsNumber (cJSON-GetObjectItem spawn "z")))
(do
(= (. out has-spawn) true)
(= (. out spawn-x) (cast (cJSON-GetNumberValue (cJSON-GetObjectItem spawn "x")) int))
(= (. out spawn-z) (cast (cJSON-GetNumberValue (cJSON-GetObjectItem spawn "z")) int))))
(var config (* cJSON) (cJSON-GetObjectItem world "WorldConfiguration"))
(for (var item (* cJSON) (if config (. config child) NULL)) item (= item (. item next))
(var text [char 64])
@@ -63,6 +76,7 @@
(pub fn world-source/load ((path (* const char)) (out (* world-source))) bool
(= (. out settings) (world-settings/defaults))
(snprintf (. out seed-text) (sizeof (. out seed-text)) "0")
(= (. out has-spawn) false)
(if (ends-with path ".vcdbs")
(return (from-save path out)))
(return (from-server-config path out)))

View File

@@ -645,8 +645,11 @@
;;; A height is the top solid block, 0 if there is none.
;;; Heights of the columns of chunk (cx, cz) from local (lx0, lz0) to
;;; (lx1, lz1) inclusive, every STEP-th, into HEIGHTS[lz * 32 + lx].
;;; SOLID-BITS, unless NULL, gets every column's solid blocks below the top
;;; too, overhangs and pockets showing as gaps: bit y % 32 of word
;;; (lz * 32 + lx) * WORDS + y / 32, set where solid; it starts zeroed.
(fn heights-in ((t (* terra)) (cx int) (cz int) (lx0 int) (lz0 int) (lx1 int) (lz1 int)
(step int) (heights (* u16))) void
(step int) (heights (* u16)) (solid-bits (* u32)) (words int)) void
(use-region t (/ cx 16) (/ cz 16))
(var per-plate int (river-plate/chunks (& (. t config))))
(var plate (* river-plate) (use-plate t (/ cx per-plate) (/ cz per-plate)))
@@ -778,7 +781,11 @@
(! (|| (<= y (- baseline bank-blocks))
(>= (cast y float) (+ baseline (* bank-blocks (. c top-factor)))))))
(= solid false))
(if solid (= height y)))
(if solid
(do
(= height y)
(if solid-bits
(c-bit-or= [solid-bits (+ (* (+ (* lz 32) lx) words) (/ y 32))] (<< (cast 1 u32) (% y 32)))))))
(= [heights (+ (* lz 32) lx)] (cast height u16))))
(free w))
@@ -786,7 +793,19 @@
;;; multiples of STEP, into HEIGHTS[lz * 32 + lx]; the rest are left
;;; alone.
(pub fn terra/chunk-heights ((t (* terra)) (cx int) (cz int) (step int) (heights (* u16))) void
(heights-in t cx cz 0 0 31 31 step heights))
(heights-in t cx cz 0 0 31 31 step heights NULL 0))
;;; Words per column of terra/chunk-solidity's bits.
(pub fn terra/solid-words ((t (* const terra))) int
(return (/ (+ (. t size-y) 31) 32)))
;;; Every block of chunk (cx, cz): its heights as terra/chunk-heights,
;;; and SOLID, 1024 * terra/solid-words words, bit y % 32 of word
;;; (lz * 32 + lx) * words + y / 32 set where the terrain is solid.
(pub fn terra/chunk-solidity ((t (* terra)) (cx int) (cz int) (heights (* u16)) (solid (* u32))) void
(var words int (terra/solid-words t))
(memset solid 0 (* 1024 words (sizeof u32)))
(heights-in t cx cz 0 0 31 31 1 heights solid words))
;;; The height of the one column at block (x, z): 1/1024th of a chunk's
;;; work, for a cursor or a probe.
@@ -794,5 +813,5 @@
(var heights [u16 1024])
(var lx int (% x 32))
(var lz int (% z 32))
(heights-in t (/ x 32) (/ z 32) lx lz lx lz 1 heights)
(heights-in t (/ x 32) (/ z 32) lx lz lx lz 1 heights NULL 0)
(return [heights (+ (* lz 32) lx)]))

View File

@@ -21,6 +21,7 @@
(import terra)
(import strata)
(import ores)
(import caves)
(pub define MAX-TILES 65536)
;;; Not worldgen maps: Rivers' distance to the nearest river, the
@@ -31,6 +32,7 @@
(pub define LAYER-HEIGHT 7)
(pub define LAYER-ROCK 8)
(pub define LAYER-MINERAL 9)
(pub define LAYER-CAVES 10)
(pub define ROCK-AT-SURFACE 0)
(pub define ROCK-AT-DEPTH 1)
(pub define ROCK-AT-Y 2)
@@ -38,6 +40,8 @@
(pub define ROCK-ANY-DEPTH 3)
;;; Mineral tile values: this plus the reading's category.
(pub define MINERAL-VALUE-BASE 64)
;;; Cave tile values: this plus the column's cave category.
(pub define CAVE-VALUE-BASE 80)
;;; Rock tile values besides rock indices.
(pub define VALUE-WATER 253)
(pub define VALUE-AIR 254)
@@ -303,7 +307,8 @@
(return out))
;;; The mineral tile: per sample, MINERAL-VALUE-BASE plus the category
;;; of the pick's reading there.
;;; of the pick's reading there, or of the ore map where the pick can't
;;; read the mineral; that needs neither heights nor strata.
(fn render-minerals ((tr (* terra)) (rocks (* strata)) (o (* ores)) (k tile-key) (size (* int)))
(* u8)
(var res int (. k stratum))
@@ -313,6 +318,7 @@
(var out (* u8) (malloc (* res res)))
(var heights [u16 1024])
(var column (* u8) (malloc (+ (. tr size-y) 1)))
(var readable bool (. [(. o minerals) (. k param)] readable))
(var per-chunk int (/ 32 step))
(if (< per-chunk 1) (= per-chunk 1))
(for (var cz int 0) (< cz 16) (++ cz)
@@ -321,22 +327,85 @@
(continue))
(var chunk-x int (+ (* (. k rx) 16) cx))
(var chunk-z int (+ (* (. k rz) 16) cz))
(terra/chunk-heights tr chunk-x chunk-z (if (> step 32) 32 step) heights)
(if readable
(terra/chunk-heights tr chunk-x chunk-z (if (> step 32) 32 step) heights))
(for (var dz int 0) (< dz per-chunk) (++ dz)
(for (var dx int 0) (< dx per-chunk) (++ dx)
(var lx int (* dx step))
(var lz int (* dz step))
(var h int [heights (+ (* lz 32) lx)])
(strata/column rocks chunk-x chunk-z lx lz h column)
(var r mineral-reading (ores/reading o (. k param) (& map) (+ (* chunk-x 32) lx)
(var h int 0)
(if readable
(do
(= h [heights (+ (* lz 32) lx)])
(strata/column rocks chunk-x chunk-z lx lz h column)))
(var c int (ores/category-at o (. k param) (& map) (+ (* chunk-x 32) lx)
(+ (* chunk-z 32) lz) column h))
(= [out (+ (* (+ (/ (* cz 32) step) dz) res) (/ (* cx 32) step) dx)]
(cast (+ MINERAL-VALUE-BASE (ores/category (. r total))) u8))))))
(cast (+ MINERAL-VALUE-BASE c) u8))))))
(free column)
(free (. map data))
(= (* size) res)
(return out))
;;; The cave tile: per sample, CAVE-VALUE-BASE plus its column's cave
;;; category, or CAVE-HOT-SPRING inside a hot spring. Caves are carved chunk by chunk, and a chunk needs every
;;; block of its terrain, so this costs a whole chunk per sample when
;;; zoomed out.
(fn render-caves ((tr (* terra)) (c (* caves)) (k tile-key) (size (* int))) (* u8)
(var res int (. k stratum))
(var step int (/ 512 res))
(var out (* u8) (malloc (* res res)))
(var heights [u16 1024])
(var carved [int 1024])
(var words int (terra/solid-words tr))
(var solid (* u32) (malloc (* 1024 words (sizeof u32))))
(var states (* u8) (malloc (* 1024 (. tr size-y))))
(var per-chunk int (/ 32 step))
(if (< per-chunk 1) (= per-chunk 1))
(for (var cz int 0) (< cz 16) (++ cz)
(for (var cx int 0) (< cx 16) (++ cx)
(if (|| (!= (% (* cx 32) step) 0) (!= (% (* cz 32) step) 0))
(continue))
(var chunk-x int (+ (* (. k rx) 16) cx))
(var chunk-z int (+ (* (. k rz) 16) cz))
(terra/chunk-solidity tr chunk-x chunk-z heights solid)
(for (var i int 0) (< i 1024) (++ i)
(= [carved i] [heights i]))
(caves/carve-column c chunk-x chunk-z carved solid words states)
(for (var dz int 0) (< dz per-chunk) (++ dz)
(for (var dx int 0) (< dx per-chunk) (++ dx)
(var lx int (* dx step))
(var lz int (* dz step))
(var i int (+ (* lz 32) lx))
(var cat int (caves/category c states lx lz [heights i] [carved i]))
(= [out (+ (* (+ (/ (* cz 32) step) dz) res) (/ (* cx 32) step) dx)]
(cast (+ CAVE-VALUE-BASE cat) u8))))))
;; hot springs over it, from the chunks of the region and around it,
;; whose discs may reach in
(var springs [hot-spring MAX-CANDIDATES])
(for (var cz int -1) (<= cz 16) (++ cz)
(for (var cx int -1) (<= cx 16) (++ cx)
(var chunk-x int (+ (* (. k rx) 16) cx))
(var chunk-z int (+ (* (. k rz) 16) cz))
(if (|| (< chunk-x 0) (< chunk-z 0) (>= (* chunk-x 32) (. (. c g) settings size-x))
(>= (* chunk-z 32) (. (. c g) settings size-z)))
(continue))
(var n int (caves/hot-springs c tr chunk-x chunk-z springs MAX-CANDIDATES))
(for (var i int 0) (< i n) (++ i)
(var sp (* hot-spring) (& [springs i]))
(for (var iz int 0) (< iz res) (++ iz)
(var dz double (- (+ (* (. k rz) 512) (* iz step)) (. sp z)))
(if (> (fabs dz) (. sp radius))
(continue))
(for (var ix int 0) (< ix res) (++ ix)
(var dx double (- (+ (* (. k rx) 512) (* ix step)) (. sp x)))
(if (< (+ (* dx dx) (* dz dz)) (* (. sp radius) (. sp radius)))
(= [out (+ (* iz res) ix)] (cast (+ CAVE-VALUE-BASE CAVE-HOT-SPRING) u8))))))))
(free solid)
(free states)
(= (* size) res)
(return out))
;;; Workers -------------------------------------------------------
(fn worker-main ((arg (* void))) (* void)
@@ -348,6 +417,7 @@
(var tr (* terra) NULL)
(var rocks (* strata) NULL)
(var minerals (* ores) NULL)
(var cave-gen (* caves) NULL)
(pthread-mutex-lock (& (. t lock)))
(while (! (. t quit))
(if (>= (. t queue-pos) (. t queue-len))
@@ -372,6 +442,8 @@
(= tr NULL)
(if minerals (ores/free minerals))
(= minerals NULL)
(if cave-gen (caves/free cave-gen))
(= cave-gen NULL)
(if rocks (strata/free rocks))
(= rocks NULL)
(if g (worldgen/free g))
@@ -380,6 +452,7 @@
(if g (= tr (terra/make g rivers)))
(if g (= rocks (strata/make g (. t assets-dir))))
(if rocks (= minerals (ores/make settings (. t assets-dir) rocks)))
(if g (= cave-gen (caves/make g)))
(= (. cache sampler) (river-sampler/make (& rivers) (. settings size-y)))
(= g-generation generation)))
(var size int 0)
@@ -393,6 +466,8 @@
(= values (render-rocks tr rocks key (& size)))
(&& tr rocks minerals (== (. key map) LAYER-MINERAL))
(= values (render-minerals tr rocks minerals key (& size)))
(&& tr cave-gen (== (. key map) LAYER-CAVES))
(= values (render-caves tr cave-gen key (& size)))
g
(= px (render g key (& size))))
@@ -410,6 +485,7 @@
(= (. tl state) TILE-QUEUED)))))
(pthread-mutex-unlock (& (. t lock)))
(if minerals (ores/free minerals))
(if cave-gen (caves/free cave-gen))
(if rocks (strata/free rocks))
(if tr (terra/free tr))
(if g (worldgen/free g))

View File

@@ -11,6 +11,7 @@
(import terra)
(import strata)
(import ores)
(import caves)
(import img)
(define MAP-KINDS 6)
@@ -165,6 +166,432 @@
(.release (& diff))
(return (if (|| (!= (. t matched) (. t compared)) (> unknown 0)) 1 0)))
;;; What a block of the save is, for the strata check.
(define BLOCK-OTHER -1)
;;; a rock- block whose rock is not a stratum, e.g. a deposit's halite
(define BLOCK-OTHER-ROCK -2)
;;; Kinds of blocks the save has where the strata put rock, by the code
;;; up to its first hyphen, e.g. "ore" or "crackedrock".
(define MAX-KINDS 64)
(record kind-count ((name [char 24]) (count i64)))
(fn count-kind ((kinds (* kind-count)) (n (* int)) (code (* const char))) void
(var len int (cast (strcspn code "-") int))
(for (var i int 0) (< i (* n)) (++ i)
(if (&& (== (cast (strlen (. [kinds i] name)) int) len)
(== (strncmp (. [kinds i] name) code len) 0))
(do (++ (. [kinds i] count)) (return))))
(if (>= (* n) MAX-KINDS)
(return))
(snprintf (. [kinds (* n)] name) 24 "%.*s" len code)
(= (. [kinds (* n)] count) 1)
(++ (* n)))
(fn by-count ((pa (* const void)) (pb (* const void))) int
(var a (* const kind-count) pa)
(var b (* const kind-count) pb)
(return (- (< (. a count) (. b count)) (> (. a count) (. b count)))))
;;; Lava in caves sits at y 11 and below and turns the rock around it
;;; to basalt, a few blocks further up.
(define LAVA-BASALT-TOP 18)
;;; Compares the rock below the surface with the save's blocks, chunk
;;; by chunk: every block from y = 1 up to the lower of our height and
;;; the stored one. The strata are laid against our height, as the game
;;; laid them against its height before the dry seabed raise.
;;;
;;; Where the save has a stratum rock, it should be ours. Three things
;;; written after the strata explain where it isn't: the column's height
;;; differs from ours, so its strata sit differently; basalt around cave
;;; lava near the bottom; and the default rock, which cave walls and
;;; underground structures are built of. Where the save has anything
;;; else, ore, caves, deposits, it is counted by kind.
(pub fn verify-strata ((s (* save)) (g (* worldgen)) (config river-config) (rocks (* strata))
(dir (* const char))) int
(var t (* terra) (terra/make g config))
(var heights [u16 1024])
(var basalt int -1)
(for (var r int 0) (< r (. rocks rock-count)) (++ r)
(if (== (strcmp [(. rocks rock-codes) r] "rock-basalt") 0)
(= basalt r)))
(var e extent (save/extent s "mapchunk"))
(var w int (* (+ (- (. e x1) (. e x0)) 1) 32))
(var h int (* (+ (- (. e z1) (. e z0)) 1) 32))
(var diff image (image/make w h))
(var t0 clock-t (clock))
;; block id -> strata rock index, or a BLOCK- kind
(var rock-of (* int) (malloc (* (+ (. s block-count) 1) (sizeof int))))
(for (var id int 0) (< id (. s block-count)) (++ id)
(var code (* const char) (save/block-code s id))
(= [rock-of id] BLOCK-OTHER)
(if (== (strncmp code "rock-" 5) 0)
(do
(= [rock-of id] BLOCK-OTHER-ROCK)
(for (var r int 0) (< r (. rocks rock-count)) (++ r)
(if (== (strcmp code [(. rocks rock-codes) r]) 0)
(= [rock-of id] r))))))
(var size-y int (. s size-y))
(var columns (* u8) (malloc (* CHUNK-AREA (+ size-y 1))))
(var blocks (* i32) (malloc (* CHUNK-VOLUME (sizeof i32))))
;; ours x theirs, for strata rocks that differ
(var confusion (* i64) (calloc (* MAX-ROCKS MAX-ROCKS) (sizeof i64)))
(var kinds [kind-count MAX-KINDS])
(var kind-n int 0)
(var compared i64 0)
(var matched i64 0)
(var other-rock i64 0)
(var other i64 0)
(var chunks i64 0)
(var unreadable i64 0)
(var other-height i64 0)
(var lava i64 0)
(var default-rock i64 0)
(var m chunk-maps)
(for (var cz int (. e z0)) (<= cz (. e z1)) (++ cz)
(for (var cx int (. e x0)) (<= cx (. e x1)) (++ cx)
(if (! (save/chunk s cx cz (& m)))
(continue))
(var top int 0)
(terra/chunk-heights t cx cz 1 heights)
(for (var i int 0) (< i CHUNK-AREA) (++ i)
(var hi int [heights i])
(if (> hi top) (= top hi))
(strata/column rocks cx cz (% i 32) (/ i 32) hi (+ columns (* i (+ size-y 1)))))
(var bad [bool 1024])
(memset bad 0 (sizeof bad))
(for (var cy int 0) (&& (<= (* cy 32) top) (< (* cy 32) size-y)) (++ cy)
(if (! (save/chunk-blocks s cx cy cz blocks))
(do
(++ unreadable)
(continue)))
(++ chunks)
(for (var i int 0) (< i CHUNK-VOLUME) (++ i)
(var y int (+ (* cy 32) (/ i 1024)))
(var col int (% i 1024))
(if (|| (< y 1) (> y [(. m terrain-height) col]) (> y [heights col]))
(continue))
(var id i32 [blocks i])
(var theirs int (if (&& (>= id 0) (< id (. s block-count))) [rock-of id] BLOCK-OTHER))
(var ours int [columns (+ (* col (+ size-y 1)) y)])
(if (>= theirs 0)
(do
(++ compared)
(if (== theirs ours)
(++ matched)
(!= [heights col] [(. m terrain-height) col])
(++ other-height)
(&& (== theirs basalt) (<= y LAVA-BASALT-TOP))
(++ lava)
(== theirs (. rocks granite))
(++ default-rock)
(do
(= [bad col] true)
(if (< ours MAX-ROCKS)
(++ [confusion (+ (* ours MAX-ROCKS) theirs)])))))
(== theirs BLOCK-OTHER-ROCK)
(do
(++ other-rock)
(count-kind kinds (& kind-n) (save/block-code s id)))
(do
(++ other)
(count-kind kinds (& kind-n) (if (>= id 0) (save/block-code s id) "?"))))))
(for (var i int 0) (< i CHUNK-AREA) (++ i)
(var colour u32 (if [bad i] 0xFF2020 (grey 96)))
(.set (& diff) (+ (* (- cx (. e x0)) 32) (% i 32)) (+ (* (- cz (. e z0)) 32) (/ i 32))
colour))))
(var unexplained i64 (- compared matched other-height lava default-rock))
(printf "strata %lld/%lld rock blocks below the surface match (%.4f%%), %lld chunks, %.1f s\n"
matched compared (if (> compared 0) (/ (* 100.0 matched) compared) 0.0) chunks
(/ (cast (- (clock) t0) double) CLOCKS-PER-SEC))
(printf " explained: %lld where the height differs, %lld lava basalt, %lld default rock; %lld others (%.4f%%):\n"
other-height lava default-rock unexplained
(if (> compared 0) (/ (* 100.0 unexplained) compared) 0.0))
(if (> unreadable 0)
(printf " %lld chunks missing or unreadable\n" unreadable))
;; the commonest confusions
(for (var shown int 0) (< shown 8) (++ shown)
(var best int -1)
(for (var k int 0) (< k (* MAX-ROCKS MAX-ROCKS)) (++ k)
(if (&& (> [confusion k] 0) (|| (< best 0) (> [confusion k] [confusion best])))
(= best k)))
(if (< best 0)
(break))
(printf " ours %-16s save %-16s %lld\n" [(. rocks rock-codes) (/ best MAX-ROCKS)]
[(. rocks rock-codes) (% best MAX-ROCKS)] [confusion best])
(= [confusion best] 0))
(qsort kinds kind-n (sizeof kind-count) by-count)
(printf " where the save has no stratum rock: %lld blocks, %lld of them other rock;" (+ other other-rock)
other-rock)
(for (var k int 0) (&& (< k kind-n) (< k 12)) (++ k)
(printf " %s %lld" (. [kinds k] name) (. [kinds k] count)))
(printf "\n")
(var path [char 1024])
(snprintf path (sizeof path) "%s/diff-strata.png" dir)
(.write (& diff) path)
(.release (& diff))
(free columns)
(free blocks)
(free confusion)
(free rock-of)
(terra/free t)
(return (if (< (* unexplained 10000) compared) 0 1)))
;;; The save's block, as the cave check sees it. Obsidian and anything
;;; else of basalt, ore or cracked rock, is what the deposits made of it.
(define SAVED-AIR 0)
(define SAVED-BASALT 1)
(define SAVED-ROCK 2)
(define SAVED-OTHER 3)
(define SAVED-FROM-BASALT 4)
(define SAVED-KINDS 5)
;;; Ours: the CAVE- states, and one more for the gaps the terrain itself
;;; leaves below its surface.
(define TERRAIN-AIR 4)
(define OUR-KINDS 5)
(var cave-state-names [(* const char) 5] #("rock" "air" "lava" "basalt" "terrain air"))
(var saved-names [(* const char) 5] #("air" "basalt" "rock" "other" "deposit in basalt"))
(define MAX-STRUCTURES 8192)
;;; Whether block (x, y, z) is inside one of N structure boxes.
(fn in-structure ((boxes (* const i32)) (n int) (x int) (y int) (z int)) bool
(for (var i int 0) (< i n) (++ i)
(var b (* const i32) (+ boxes (* i 6)))
(if (&& (>= x [b 0]) (<= x [b 3]) (>= y [b 1]) (<= y [b 4]) (>= z [b 2]) (<= z [b 5]))
(return true)))
(return false))
;;; Carves every stored chunk column and compares its blocks below our
;;; terrain height with the save's: where we carve air or lava the save
;;; should have air (lava is a fluid, so its solid block is air), where
;;; we put basalt it should have basalt, and where we leave rock it
;;; should not have air. Later passes put things in caves too, so air
;;; next to "other" is not wrong by itself. Also compares the terrain
;;; heights after carving with the stored ones.
(pub fn verify-caves ((s (* save)) (g (* worldgen)) (config river-config) (dir (* const char))) int
(var e extent (save/extent s "mapchunk"))
(var w int (* (+ (- (. e x1) (. e x0)) 1) 32))
(var h int (* (+ (- (. e z1) (. e z0)) 1) 32))
(var diff image (image/make w h))
(var t0 clock-t (clock))
(var t (* terra) (terra/make g config))
(var c (* caves) (caves/make g))
(var size-y int (. s size-y))
;; hot springs: the stored candidates against ours, and ours placed
;; against the sludgy gravel the save has
(var sludgy int -1)
(var gravel (* i32) NULL)
(var gravel-count int 0)
(var gravel-cap int 0)
(var springs (* hot-spring) NULL)
(var spring-count int 0)
(var spring-cap int 0)
(var cand-same i64 0)
(var cand-ours-only i64 0)
(var cand-saved-only i64 0)
(var saved (* u8) (malloc (+ (. s block-count) 1)))
(for (var id int 0) (< id (. s block-count)) (++ id)
(var code (* const char) (save/block-code s id))
(= [saved id] SAVED-OTHER)
(var len size-t (strlen code))
(if (== (strcmp code "sludgygravel") 0) (= sludgy id))
(if (== id 0) (= [saved id] SAVED-AIR)
(== (strcmp code "rock-basalt") 0) (= [saved id] SAVED-BASALT)
(|| (== (strcmp code "rock-obsidian") 0)
(&& (> len 7) (== (strcmp (+ code (- len 7)) "-basalt") 0)))
(= [saved id] SAVED-FROM-BASALT)
(|| (== (strncmp code "rock-" 5) 0) (== (strncmp code "crackedrock-" 12) 0)
(== (strncmp code "ore-" 4) 0))
(= [saved id] SAVED-ROCK)))
(var states (* u8) (malloc (* 1024 size-y)))
(var blocks (* i32) (malloc (* CHUNK-VOLUME (sizeof i32))))
(var tally [i64 25])
(var words int (terra/solid-words t))
(var solid (* u32) (malloc (* 1024 words (sizeof u32))))
(memset tally 0 (sizeof tally))
;; the structure boxes of the regions around the column
(var boxes (* i32) (malloc (* MAX-STRUCTURES 6 (sizeof i32))))
(var box-count int 0)
(var boxes-rx int -1)
(var boxes-rz int -1)
(var missed-in i64 0)
(var missed-out i64 0)
(var over-in i64 0)
(var over-out i64 0)
(var missed-surface i64 0)
(var heights [u16 1024])
(var before [int 1024])
(var after [int 1024])
(var columns i64 0)
(var height-before i64 0)
(var height-after i64 0)
(var m chunk-maps)
(for (var cz int (. e z0)) (<= cz (. e z1)) (++ cz)
(for (var cx int (. e x0)) (<= cx (. e x1)) (++ cx)
(if (! (save/chunk s cx cz (& m)))
(continue))
(terra/chunk-solidity t cx cz heights solid)
;; the region's structures; boxes reach into neighbours only a
;; little, so the region itself is enough for a tally
(if (|| (!= boxes-rx (/ cx 16)) (!= boxes-rz (/ cz 16)))
(do
(= boxes-rx (/ cx 16))
(= boxes-rz (/ cz 16))
(= box-count (save/structure-boxes s boxes-rx boxes-rz boxes MAX-STRUCTURES))))
(var top int 0)
(for (var i int 0) (< i 1024) (++ i)
(= [before i] [heights i])
(= [after i] [heights i])
(if (> [heights i] top) (= top [heights i])))
(caves/carve-column c cx cz after solid words states)
;; the candidates, each against the stored one at its key
(var ours [spring-candidate MAX-CANDIDATES])
(var mine int (caves/candidates c cx cz ours MAX-CANDIDATES))
(var theirs-c [saved-spring MAX-CANDIDATES])
(var stored int (save/hot-spring-locations s cx cz theirs-c MAX-CANDIDATES))
(for (var a int 0) (< a mine) (++ a)
(var hit bool false)
(for (var b int 0) (< b stored) (++ b)
(if (&& (== (. [ours a] x) (. [theirs-c b] x)) (== (. [ours a] y) (. [theirs-c b] y))
(== (. [ours a] z) (. [theirs-c b] z)) (== (. [ours a] radius) (. [theirs-c b] radius)))
(= hit true)))
(if hit (++ cand-same) (++ cand-ours-only)))
(for (var b int 0) (< b stored) (++ b)
(var hit bool false)
(for (var a int 0) (< a mine) (++ a)
(if (&& (== (. [ours a] x) (. [theirs-c b] x)) (== (. [ours a] y) (. [theirs-c b] y))
(== (. [ours a] z) (. [theirs-c b] z)) (== (. [ours a] radius) (. [theirs-c b] radius)))
(= hit true)))
(if (! hit) (++ cand-saved-only)))
;; and which of ours become hot springs
(if (> mine 0)
(do
(var placed [hot-spring MAX-CANDIDATES])
(var n int (caves/hot-springs c t cx cz placed MAX-CANDIDATES))
(for (var a int 0) (< a n) (++ a)
(if (>= spring-count spring-cap)
(do
(= spring-cap (+ (* spring-cap 2) 16))
(= springs (realloc springs (* spring-cap (sizeof hot-spring))))))
(= [springs spring-count] [placed a])
(++ spring-count))))
(for (var i int 0) (< i 1024) (++ i)
(++ columns)
(if (== [before i] [(. m terrain-height) i]) (++ height-before))
(if (== [after i] [(. m terrain-height) i]) (++ height-after)))
(var over [bool 1024])
(var missed [bool 1024])
(memset over 0 (sizeof over))
(memset missed 0 (sizeof missed))
(for (var cy int 0) (&& (<= (* cy 32) top) (< (* cy 32) size-y)) (++ cy)
(if (! (save/chunk-blocks s cx cy cz blocks))
(continue))
(for (var i int 0) (< i CHUNK-VOLUME) (++ i)
(var y int (+ (* cy 32) (/ i 1024)))
(var col int (% i 1024))
(if (&& (== [blocks i] sludgy) (>= sludgy 0))
(do
(if (>= (+ gravel-count 3) gravel-cap)
(do
(= gravel-cap (+ (* gravel-cap 2) 768))
(= gravel (realloc gravel (* gravel-cap (sizeof i32))))))
(= [gravel gravel-count] (+ (* cx 32) (% col 32)))
(= [gravel (+ gravel-count 1)] y)
(= [gravel (+ gravel-count 2)] (+ (* cz 32) (/ col 32)))
(+= gravel-count 3)))
(if (|| (< y 1) (> y [before col]))
(continue))
(var id i32 [blocks i])
(var theirs int (if (&& (>= id 0) (< id (. s block-count))) [saved id] SAVED-OTHER))
(var ours int [states (+ (* y 1024) col)])
(if (&& (== ours CAVE-UNTOUCHED)
(== (& (>> [solid (+ (* col words) (/ y 32))] (% y 32)) 1) 0))
(= ours TERRAIN-AIR))
(++ [tally (+ (* ours SAVED-KINDS) theirs)])
(var bx int (+ (* cx 32) (% col 32)))
(var bz int (+ (* cz 32) (/ col 32)))
(if (&& (== ours CAVE-AIR) (== theirs SAVED-ROCK))
(if (in-structure boxes box-count bx y bz)
(++ over-in)
(do (++ over-out) (= [over col] true))))
;; air the save has and we don't: structures, the save's own
;; surface block (the block layers take some away, glaciers
;; above all) and anything above it, or ours to explain
(if (&& (== ours CAVE-UNTOUCHED) (== theirs SAVED-AIR))
(if (in-structure boxes box-count bx y bz)
(++ missed-in)
(>= y [(. m terrain-height) col])
(++ missed-surface)
(do (++ missed-out) (= [missed col] true))))))
(for (var i int 0) (< i 1024) (++ i)
(var colour u32 (grey 96))
(if [missed i] (= colour 0x2020FF))
(if [over i] (= colour 0xFF2020))
(.set (& diff) (+ (* (- cx (. e x0)) 32) (% i 32)) (+ (* (- cz (. e z0)) 32) (/ i 32)) colour))))
(printf "caves ours by the save's, below our terrain height, %.1f s:\n"
(/ (cast (- (clock) t0) double) CLOCKS-PER-SEC))
(for (var o int 0) (< o OUR-KINDS) (++ o)
(printf " %-11s" [cave-state-names o])
(for (var k int 0) (< k SAVED-KINDS) (++ k)
(printf " %s %lld" [saved-names k] [tally (+ (* o SAVED-KINDS) k)]))
(printf "\n"))
(var carved i64 (+ [tally (+ (* CAVE-AIR SAVED-KINDS) SAVED-AIR)]
[tally (+ (* CAVE-LAVA SAVED-KINDS) SAVED-AIR)]
[tally (+ (* CAVE-BASALT SAVED-KINDS) SAVED-BASALT)]
[tally (+ (* CAVE-BASALT SAVED-KINDS) SAVED-FROM-BASALT)]))
(var ours-carved i64 0)
(for (var k int 0) (< k SAVED-KINDS) (++ k)
(+= ours-carved (+ [tally (+ (* CAVE-AIR SAVED-KINDS) k)] [tally (+ (* CAVE-LAVA SAVED-KINDS) k)]
[tally (+ (* CAVE-BASALT SAVED-KINDS) k)])))
(printf " %lld of our %lld carved blocks agree (%.2f%%)\n" carved ours-carved
(if (> ours-carved 0) (/ (* 100.0 carved) ours-carved) 0.0))
(printf " air the save has where we leave rock: %lld in generated structures, %lld at or above the save's surface, %lld otherwise\n"
missed-in missed-surface missed-out)
(printf " rock the save has where we carve air: %lld in generated structures, %lld otherwise\n"
over-in over-out)
(printf " terrain height: %lld/%lld columns before carving, %lld after\n" height-before columns
height-after)
;; each sludgy gravel block, in one of our hot springs at its height?
(var gravel-in int 0)
(var found (* bool) (calloc (+ spring-count 1) (sizeof bool)))
(for (var g3 int 0) (< g3 gravel-count) (+= g3 3)
(var gx int [gravel g3])
(var gy int [gravel (+ g3 1)])
(var gz int [gravel (+ g3 2)])
(var in bool false)
(for (var k int 0) (< k spring-count) (++ k)
(var sp (* hot-spring) (& [springs k]))
(var dx double (- gx (. sp x)))
(var dz double (- gz (. sp z)))
;; the base goes 2 below, the walls up to the surface + 1
(if (&& (>= gy (- (. sp y) 2)) (<= gy (+ (. sp y) 4))
(< (+ (* dx dx) (* dz dz)) (* (+ (. sp radius) 1) (+ (. sp radius) 1))))
(do (= in true) (= [found k] true))))
(if in (++ gravel-in)))
(var springs-found int 0)
(for (var k int 0) (< k spring-count) (++ k)
(if [found k] (++ springs-found)))
(printf "hot springs candidates: %lld as stored, %lld only ours, %lld only stored\n" cand-same cand-ours-only
cand-saved-only)
(printf " %d placed, %d of them with the save's sludgy gravel; %d of %d sludgy gravel blocks in one\n"
spring-count springs-found gravel-in (/ gravel-count 3))
(free found)
(free gravel)
(free springs)
(var path [char 1024])
(snprintf path (sizeof path) "%s/diff-caves.png" dir)
(.write (& diff) path)
(.release (& diff))
(free states)
(free blocks)
(free saved)
(free boxes)
(free solid)
(caves/free c)
(terra/free t)
(return (if (< (* (+ missed-out over-out) 200) ours-carved) 0 1)))
;;; Recomputes the prospecting pick readings players took in the save
;;; and compares each mineral's TotalFactor: per reading, every
;;; mineral the save has or this reads above nothing.

View File

@@ -4,7 +4,8 @@
;;; vs-map-viewer [--assets DIR] [--zoom BLOCKS-PER-POINT]
;;; [--base ocean|rivers|height] [--overlay NAME]... [--opacity F]
;;; [--rock-depth N | --rock-y Y | --rock-any] [--highlight ROCK]... [--find]
;;; [--mineral CODE] [--min-reading NAME] [--find-mineral]
;;; [--mineral CODE] [--min-reading NAME] [--find-mineral] [--find-cave]
;;; [--find-hot-spring] [--spawn X Z]
;;; [--probe X Z] [--mouse X Y]
;;; [--screenshot FILE.png] [WORLD.vcdbs | serverconfig.json]
;;;
@@ -20,6 +21,7 @@
(import terra)
(import strata)
(import ores)
(import caves)
(import search)
(import hover)
(import settings)
@@ -42,16 +44,17 @@
(var base-layers [int 3] #(4 6 7))
(var base-names [(* const char) 3] #("ocean" "rivers" "height"))
(var base-labels [(* const char) 3] #("Ocean" "Rivers" "Terrain height"))
(var overlay-layers [int 7] #(0 1 2 3 5 8 9))
(var overlay-names [(* const char) 7]
#("landform" "climate" "province" "upheaval" "strata" "rock" "mineral"))
(var overlay-layers [int 8] #(0 1 2 3 5 8 9 10))
(var overlay-names [(* const char) 8]
#("landform" "climate" "province" "upheaval" "strata" "rock" "mineral" "caves"))
;;; The Rock and Minerals overlays' bits in `overlays'.
(define ROCK-OVERLAY-BIT 32)
(define MINERAL-OVERLAY-BIT 64)
(define OVERLAY-COUNT 7)
(var overlay-labels [(* const char) 7]
(define CAVES-OVERLAY-BIT 128)
(define OVERLAY-COUNT 8)
(var overlay-labels [(* const char) 8]
#("Landform" "Climate" "Geologic province" "Upheaval" "Rock strata thickness" "Rock"
"Minerals"))
"Minerals" "Caves"))
(var rock-mode-names [(* const char) 4] #("Surface" "Depth below surface" "At Y" "Any depth"))
;;; Rock colours, roughly as they look in game; others get a stable one.
@@ -70,27 +73,55 @@
#("none" "miniscule" "very poor" "poor" "decent" "high" "very high" "ultra high"))
(var reading-colours [u32 8]
#(0x000000 0x5A6E7A 0x3B6FB6 0x2BA3A3 0x7BC043 0xF3C623 0xF37735 0xD11141))
;;; The same numbering for a mineral the pick can't read, whose ore map
;;; is shown instead: its value in sevenths (ores/map-category).
(var map-band-names [(* const char) 8]
#("none" "0-14%" "14-29%" "29-43%" "43-57%" "57-71%" "71-86%" "86-100%"))
;;; Find nearest's least reading, unless set: decent.
(define DEFAULT-MIN-READING 4)
;;; The category NAME names, with a hyphen for a space ("very-high"),
;;; from miniscule up; the default for a name it doesn't know.
;;; The category NAME names, a reading with a hyphen for a space
;;; ("very-high") or an ore map band ("43-57%"), from the lowest up; the
;;; default for a name it doesn't know.
(fn reading-named ((name (* const char))) int
(for (var c int 1) (< c 8) (++ c)
(var hyphenated [char 16])
(snprintf hyphenated (sizeof hyphenated) "%s" [reading-names c])
(for (var p (* char) hyphenated) (* p) (++ p)
(if (== (* p) #\space) (= (* p) #\-)))
(if (== (strcmp hyphenated name) 0)
(if (|| (== (strcmp hyphenated name) 0) (== (strcmp [map-band-names c] name) 0))
(return c)))
(return DEFAULT-MIN-READING))
;;; A mineral's name in the panel's list.
(record item-label ((text [char 40])))
;;; The cave categories, caves/category's numbering, and their colours
;;; on the Caves overlay: entrances stand out, then the deeper the
;;; topmost cave, the darker.
(var cave-names [(* const char) 8]
#("none" "entrance" "10 deep or less" "11-25 deep" "26-50 deep" "51-100 deep" "deeper"
"hot spring"))
(var cave-colours [u32 8]
#(0x000000 0x00E5FF 0xFFD54F 0xFF9800 0xE64A19 0x8E24AA 0x303F9F 0xFF4081))
;;; What Find nearest is looking for, for which panel shows its status.
(define FIND-ROCK 0)
(define FIND-MINERAL 1)
(define FIND-CAVE 2)
(define FIND-HOT-SPRING 3)
(define MAX-PROBE-CAVES 16)
;;; The strata of the column clicked last: top down, as (rock, top y,
;;; bottom y) runs; and the pick's readings there, highest first.
;;; bottom y) runs; the pick's readings there, highest first; and the
;;; ore maps of the minerals the pick can't read.
(record probe ((valid bool) (x int) (z int) (height int) (count int)
(rocks [int 64]) (tops [int 64]) (bottoms [int 64])
(reading-count int) (reading-minerals [int MAX-MINERALS])
(readings [mineral-reading MAX-MINERALS])))
(readings [mineral-reading MAX-MINERALS])
(map-count int) (map-minerals [int MAX-MINERALS]) (map-factors [double MAX-MINERALS])
(cave-entrance bool) (cave-count int) (cave-tops [int MAX-PROBE-CAVES])
(cave-bottoms [int MAX-PROBE-CAVES]) (cave-lava [bool MAX-PROBE-CAVES])))
(record app
((window (* SDL-Window)) (renderer (* SDL-Renderer)) (ctx (* struct nk-context))
@@ -107,13 +138,24 @@
(coverage [i64 256]) (coverage-total i64)
(pins [int 64]) (pin-count int)
(search (* search)) (searching bool) (search-status [char 160]) (find-requested bool)
;; minerals: the one shown, the readable ones for the panel's list,
;; its share of the view per category, and what Find nearest wants
;; minerals: the one shown, the panel's list of them, its share of
;; the view per category, and what Find nearest wants
(minerals (* ores)) (mineral int) (mineral-code (* const char))
(mineral-labels [item-label MAX-MINERALS])
(mineral-items [* const char MAX-MINERALS]) (mineral-ids [int MAX-MINERALS])
(mineral-item-count int)
(mineral-coverage [i64 256]) (mineral-coverage-total i64)
(min-reading int) (find-mineral-requested bool) (searching-mineral bool)
(min-reading int) (find-mineral-requested bool)
;; caves: their share of the view per category, the probe's carver
(cave-coverage [i64 256]) (cave-coverage-total i64) (find-cave-requested bool)
(find-spring-requested bool)
(probe-caves (* caves))
(search-kind int)
;; where coordinates count from: the world's spawn unless set, and one
;; given on the command line
(spawn-x int) (spawn-z int) (spawn-arg bool) (arg-spawn-x int) (arg-spawn-z int)
;; the next click on the map sets the spawn instead of probing
(picking-spawn bool)
(show-stories bool) (show-cursor-rock bool)
;; what is under the cursor, from the hover worker; and a terrain
;; generator of the main thread's own, for probes
@@ -202,16 +244,34 @@
(var r SDL-FRect (map-rect a))
(return (&& (>= x (. r x)) (< x (+ (. r x) (. r w))) (>= y (. r y)) (< y (+ (. r y) (. r h))))))
;;; The world's own spawn: the one its save or server config names,
;;; else the map's middle, the game's default.
(fn world-spawn ((a (* app)) (x (* int)) (z (* int))) void
(if (. a source has-spawn)
(do
(= (* x) (. a source spawn-x))
(= (* z) (. a source spawn-z)))
(do
(= (* x) (/ (. a source settings size-x) 2))
(= (* z) (/ (. a source settings size-z) 2)))))
(fn reset-spawn ((a (* app))) void
(world-spawn a (& (. a spawn-x)) (& (. a spawn-z))))
(fn go-to-spawn ((a (* app))) void
(= (. a center-x) (/ (. a source settings size-x) 2.0))
(= (. a center-z) (/ (. a source settings size-z) 2.0)))
(= (. a center-x) (+ (. a spawn-x) 0.5))
(= (. a center-z) (+ (. a spawn-z) 0.5)))
;;; Applying settings ----------------------------------------------
(fn release-probe-terra ((a (* app))) void
(if (. a probe-terra)
(terra/free (. a probe-terra)))
(= (. a probe-terra) NULL))
(= (. a probe-terra) NULL)
;; it carves with the generator of the world being let go
(if (. a probe-caves)
(caves/free (. a probe-caves)))
(= (. a probe-caves) NULL))
;;; The colour of rock index I.
(fn rock-colour ((a (* app)) (i int)) u32
@@ -241,6 +301,9 @@
(= [p MINERAL-VALUE-BASE] 0)
(for (var c int 1) (< c READING-CATEGORIES) (++ c)
(= [p (+ MINERAL-VALUE-BASE c)] (c-bit-or 0xFF000000 [reading-colours c])))
(= [p CAVE-VALUE-BASE] 0)
(for (var c int 1) (< c CAVE-CATEGORIES) (++ c)
(= [p (+ CAVE-VALUE-BASE c)] (c-bit-or 0xFF000000 [cave-colours c])))
(.set-palette (. a tiles) p))
;;; The highlighted rock when it is the only one, else -1.
@@ -271,16 +334,24 @@
(if (. a minerals)
(ores/free (. a minerals)))
(= (. a minerals) o)
;; the panel's list: nothing, then every mineral the pick can read
;; the panel's list: nothing, then every mineral the pick can read,
;; then the ones shown by their ore map
(= [(. a mineral-items) 0] "(none)")
(= [(. a mineral-ids) 0] -1)
(= (. a mineral-item-count) 1)
(for (var pass int 0) (< pass 2) (++ pass)
(for (var i int 0) (< i (. o count)) (++ i)
(if (. [(. o minerals) i] readable)
(do
(= [(. a mineral-items) (. a mineral-item-count)] (. [(. o minerals) i] code))
(= [(. a mineral-ids) (. a mineral-item-count)] i)
(++ (. a mineral-item-count)))))
(var m (* mineral) (& [(. o minerals) i]))
(if (!= (. m readable) (== pass 0))
(continue))
(var n int (. a mineral-item-count))
(var label (* char) (. [(. a mineral-labels) n] text))
(if (. m readable)
(snprintf label 40 "%s" (. m code))
(snprintf label 40 "%s (ore map)" (. m code)))
(= [(. a mineral-items) n] label)
(= [(. a mineral-ids) n] i)
(++ (. a mineral-item-count))))
(= (. a mineral-code) (if (>= (. a mineral) 0) (. [(. o minerals) (. a mineral)] code) NULL)))
;;; Makes SOURCE the world shown: a fresh generator for the panels,
@@ -330,6 +401,7 @@
(if (world-source/load (. a world-path) (& source))
(do
(apply a source)
(reset-spawn a)
(go-to-spawn a))
(snprintf (. a status) (sizeof (. a status)) "cannot load %s" (. a world-path))))
@@ -377,7 +449,9 @@
(== layer LAYER-MINERAL)
(do
(= (. k stratum) (height-resolution (. a zoom)))
(= (. k param) (. a mineral))))
(= (. k param) (. a mineral)))
(== layer LAYER-CAVES)
(= (. k stratum) (height-resolution (. a zoom))))
(return k))
;;; Adds a value tile's samples that fall inside the visible world
@@ -425,10 +499,12 @@
(= (. a coverage-total) 0)
(memset (. a mineral-coverage) 0 (sizeof (. a mineral-coverage)))
(= (. a mineral-coverage-total) 0)
(memset (. a cave-coverage) 0 (sizeof (. a cave-coverage)))
(= (. a cave-coverage-total) 0)
(.begin-frame (. a tiles))
;; layer by layer, each nearest the centre first: the base fills in
;; before the overlays start
(var layers [int 9])
(var layers [int 10])
(var layer-count int 0)
(= [layers 0] (. a base))
(= layer-count 1)
@@ -451,6 +527,9 @@
x0 z0 x1 z1)
(&& values (== [layers l] LAYER-MINERAL))
(count-visible (. a mineral-coverage) (& (. a mineral-coverage-total)) values size
(. k rx) (. k rz) x0 z0 x1 z1)
(&& values (== [layers l] LAYER-CAVES))
(count-visible (. a cave-coverage) (& (. a cave-coverage-total)) values size
(. k rx) (. k rz) x0 z0 x1 z1))
(SDL-SetTextureBlendMode tex SDL-BLENDMODE-BLEND)
(SDL-SetTextureAlphaMod tex (if (== l 0) 255 alpha))
@@ -472,12 +551,23 @@
(var g (* worldgen) (. a g))
(SDL-SetRenderDrawColor r 255 255 255 255)
(var stories int (. g story-count))
(if (! (. a show-stories)) (= stories -1))
(for (var i int -1) (< i stories) (++ i)
(var x double (/ (. a source settings size-x) 2.0))
(var z double (/ (. a source settings size-z) 2.0))
(if (! (. a show-stories)) (= stories -2))
;; the spawn, the world's own if the one set differs, then the stories
(var wx int)
(var wz int)
(world-spawn a (& wx) (& wz))
(for (var i int -2) (< i stories) (++ i)
(var x double (+ (. a spawn-x) 0.5))
(var z double (+ (. a spawn-z) 0.5))
(var name (* const char) "spawn")
(if (>= i 0)
(if (== i -1)
(do
(if (&& (== wx (. a spawn-x)) (== wz (. a spawn-z)))
(continue))
(= x (+ wx 0.5))
(= z (+ wz 0.5))
(= name "world spawn"))
(>= i 0)
(do
(= x (. [(. g stories) i] x))
(= z (. [(. g stories) i] z))
@@ -577,6 +667,19 @@
(apply a source)))
(if (nk-button-label ctx "Go to spawn")
(go-to-spawn a))
;; the spawn coordinates count from, by default the world's: set
;; by a click on the map
(nk-layout-row-dynamic ctx 26 2)
(if (nk-button-label ctx (if (. a picking-spawn) "Click the map" "Set spawn"))
(= (. a picking-spawn) (! (. a picking-spawn))))
(var wx int)
(var wz int)
(world-spawn a (& wx) (& wz))
(var moved bool (|| (!= wx (. a spawn-x)) (!= wz (. a spawn-z))))
(if (! moved) (nk-widget-disable-begin ctx))
(if (nk-button-label ctx "World's spawn")
(reset-spawn a))
(if (! moved) (nk-widget-disable-end ctx))
(nk-tree-pop ctx))))
;;; The rock layer's mode, its depth or Y, and the legend: clicking rocks
@@ -631,7 +734,7 @@
(nk-layout-row-dynamic ctx 24 2)
(if (nk-button-label ctx "Show all")
(do (= (. a highlight) 0) (update-palette a)))
(if (&& (. a searching) (! (. a searching-mineral)))
(if (&& (. a searching) (== (. a search-kind) FIND-ROCK))
(if (nk-button-label ctx "Cancel")
(do
(search/stop (. a search))
@@ -639,7 +742,7 @@
(snprintf (. a search-status) (sizeof (. a search-status)) "cancelled")))
(if (nk-button-label ctx "Find nearest")
(= (. a find-requested) true)))))
(if (&& (* (. a search-status)) (! (. a searching-mineral)))
(if (&& (* (. a search-status)) (== (. a search-kind) FIND-ROCK))
(do
(nk-layout-row-dynamic ctx 34 1)
(nk-label-wrap ctx (. a search-status)))))
@@ -694,8 +797,25 @@
(draw-rock-controls a)
(nk-tree-pop ctx))))
;;; The names of the mineral shown's categories: the pick's readings, or
;;; its ore map's bands where the pick can't read it.
(fn category-names ((a (* app))) (* * const char)
(if (&& (>= (. a mineral) 0) (! (. [(. (. a minerals) minerals) (. a mineral)] readable)))
(return map-band-names))
(return reading-names))
;;; What the cursor label and Info say of the mineral shown at the
;;; cursor, into TEXT.
(fn mineral-at-cursor ((a (* app)) (r (* const hover-result)) (text (* char)) (size size-t)) void
(var m (* mineral) (& [(. (. a minerals) minerals) (. r query mineral)]))
(if (. m readable)
(snprintf text size "%s %s, %.2f ppt" (. m code)
[reading-names (ores/category (. r mineral total))] (. r mineral ppt))
(snprintf text size "%s ore map %.0f%%" (. m code) (* 100 (. r mineral-map)))))
;;; The mineral shown, the legend of the pick's readings with each one's
;;; share of the view, and Find nearest for a reading at least so high.
;;; A mineral the pick can't read is shown by its ore map, in bands.
(fn draw-mineral-controls ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(var current int 0)
@@ -708,12 +828,18 @@
(if (!= chosen current)
(do
(= (. a mineral) [(. a mineral-ids) chosen])
(= (. a mineral-code) (if (>= (. a mineral) 0) [(. a mineral-items) chosen] NULL))))
(= (. a mineral-code)
(if (>= (. a mineral) 0) (. [(. (. a minerals) minerals) (. a mineral)] code) NULL))))
(if (< (. a mineral) 0)
(do
(nk-layout-row-dynamic ctx 34 1)
(nk-label-wrap ctx "Choose a mineral to see what the prospecting pick reads")
(return)))
(var names (* * const char) (category-names a))
(if (! (. [(. (. a minerals) minerals) (. a mineral)] readable))
(do
(nk-layout-row-dynamic ctx 50 1)
(nk-label-wrap ctx "The pick reads this from ore inside other deposits; this is its ore map, how common it is here")))
(nk-layout-row-dynamic ctx 8 1)
(nk-spacing ctx 1)
(var shown bool (&& (> (. a mineral-coverage-total) 0)
@@ -727,18 +853,18 @@
(nk-button-color ctx (nk-rgb (cast (& (>> colour 16) 255) int) (cast (& (>> colour 8) 255) int)
(cast (& colour 255) int)))
(if shown
(nk-labelf ctx NK-TEXT-LEFT "%-13s %5.1f%%" [reading-names c]
(nk-labelf ctx NK-TEXT-LEFT "%-13s %5.1f%%" [names c]
(/ (* 100.0 [(. a mineral-coverage) (+ MINERAL-VALUE-BASE c)])
(. a mineral-coverage-total)))
(nk-label ctx [reading-names c] NK-TEXT-LEFT)))
(nk-label ctx [names c] NK-TEXT-LEFT)))
(nk-layout-row-dynamic ctx 22 2)
(nk-label ctx "Find at least" NK-TEXT-LEFT)
;; from miniscule up
(= (. a min-reading)
(+ 1 (nk-combo ctx (+ reading-names 1) (- READING-CATEGORIES 1) (- (. a min-reading) 1) 20
(+ 1 (nk-combo ctx (+ names 1) (- READING-CATEGORIES 1) (- (. a min-reading) 1) 20
(nk-vec2 140 200))))
(nk-layout-row-dynamic ctx 24 1)
(if (&& (. a searching) (. a searching-mineral))
(if (&& (. a searching) (== (. a search-kind) FIND-MINERAL))
(if (nk-button-label ctx "Cancel")
(do
(search/stop (. a search))
@@ -746,11 +872,56 @@
(snprintf (. a search-status) (sizeof (. a search-status)) "cancelled")))
(if (nk-button-label ctx "Find nearest")
(= (. a find-mineral-requested) true)))
(if (&& (* (. a search-status)) (. a searching-mineral))
(if (&& (* (. a search-status)) (== (. a search-kind) FIND-MINERAL))
(do
(nk-layout-row-dynamic ctx 34 1)
(nk-label-wrap ctx (. a search-status)))))
;;; The legend of the Caves overlay, with each category's share of the
;;; view, and Find nearest for a cave entrance.
(fn draw-cave-controls ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(var shown bool (&& (> (. a cave-coverage-total) 0) (!= (& (. a overlays) CAVES-OVERLAY-BIT) 0)))
(nk-layout-row-dynamic ctx 18 1)
(nk-label ctx "The topmost cave below the surface" NK-TEXT-LEFT)
(for (var c int 1) (< c CAVE-CATEGORIES) (++ c)
(nk-layout-row-template-begin ctx 18)
(nk-layout-row-template-push-static ctx 18)
(nk-layout-row-template-push-dynamic ctx)
(nk-layout-row-template-end ctx)
(var colour u32 [cave-colours c])
(nk-button-color ctx (nk-rgb (cast (& (>> colour 16) 255) int) (cast (& (>> colour 8) 255) int)
(cast (& colour 255) int)))
(if shown
(nk-labelf ctx NK-TEXT-LEFT "%-16s %5.1f%%" [cave-names c]
(/ (* 100.0 [(. a cave-coverage) (+ CAVE-VALUE-BASE c)]) (. a cave-coverage-total)))
(nk-label ctx [cave-names c] NK-TEXT-LEFT)))
(nk-layout-row-dynamic ctx 24 2)
(var finding bool (&& (. a searching) (|| (== (. a search-kind) FIND-CAVE)
(== (. a search-kind) FIND-HOT-SPRING))))
(if finding
(if (nk-button-label ctx "Cancel")
(do
(search/stop (. a search))
(= (. a searching) false)
(snprintf (. a search-status) (sizeof (. a search-status)) "cancelled")))
(do
(if (nk-button-label ctx "Nearest entrance")
(= (. a find-cave-requested) true))
(if (nk-button-label ctx "Nearest hot spring")
(= (. a find-spring-requested) true))))
(if (&& (* (. a search-status)) (|| (== (. a search-kind) FIND-CAVE) (== (. a search-kind) FIND-HOT-SPRING)))
(do
(nk-layout-row-dynamic ctx 34 1)
(nk-label-wrap ctx (. a search-status)))))
(fn draw-caves ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(if (nk-tree-push ctx NK-TREE-TAB "Caves" NK-MAXIMIZED)
(do
(draw-cave-controls a)
(nk-tree-pop ctx))))
(fn draw-minerals ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(if (nk-tree-push ctx NK-TREE-TAB "Minerals" NK-MAXIMIZED)
@@ -807,16 +978,42 @@
(= [(. p tops) (. p count)] y)
(= [(. p bottoms) (. p count)] y)
(++ (. p count)))))
;; the caves through the column
(if (! (. a probe-caves))
(= (. a probe-caves) (caves/make (. a g))))
(var words int (terra/solid-words (. a probe-terra)))
(var solid (* u32) (malloc (* 1024 words (sizeof u32))))
(var states (* u8) (malloc (* 1024 (. a source settings size-y))))
(var heights [u16 1024])
(var carved [int 1024])
(terra/chunk-solidity (. a probe-terra) (/ x 32) (/ z 32) heights solid)
(for (var i int 0) (< i 1024) (++ i)
(= [carved i] [heights i]))
(caves/carve-column (. a probe-caves) (/ x 32) (/ z 32) carved solid words states)
(var ci int (+ (* (% z 32) 32) (% x 32)))
(= (. p cave-entrance) (< [carved ci] [heights ci]))
(= (. p cave-count) (caves/runs (. a probe-caves) states (% x 32) (% z 32) [heights ci]
(. p cave-tops) (. p cave-bottoms) (. p cave-lava) MAX-PROBE-CAVES))
(free solid)
(free states)
;; what the pick reads here: every mineral it can, highest first
(var o (* ores) (. a minerals))
(= (. p reading-count) 0)
(= (. p map-count) 0)
(for (var i int 0) (< i (. o count)) (++ i)
(if (! (. [(. o minerals) i] readable))
(continue))
(var m int-map)
(ores/map o i (/ x 512) (/ z 512) (& m))
(var r mineral-reading (ores/reading o i (& m) x z (. a column) h))
(var factor double (ores/map-factor (& m) x z))
(free (. m data))
(if (! (. [(. o minerals) i] readable))
(do
(if (> factor 0)
(do
(= [(. p map-minerals) (. p map-count)] i)
(= [(. p map-factors) (. p map-count)] factor)
(++ (. p map-count))))
(continue)))
(if (== (ores/category (. r total)) 0)
(continue))
(var n int (. p reading-count))
@@ -836,10 +1033,8 @@
(if (nk-tree-push ctx NK-TREE-TAB "Probe" NK-MAXIMIZED)
(do
(nk-layout-row-dynamic ctx 18 1)
(var s (* world-settings) (& (. a source settings)))
(nk-labelf ctx NK-TEXT-LEFT "x %d z %d" (. p x) (. p z))
(nk-labelf ctx NK-TEXT-LEFT "from centre %d, %d" (- (. p x) (/ (. s size-x) 2))
(- (. p z) (/ (. s size-z) 2)))
(nk-labelf ctx NK-TEXT-LEFT "from spawn %d, %d" (- (. p x) (. a spawn-x)) (- (. p z) (. a spawn-z)))
(nk-labelf ctx NK-TEXT-LEFT "surface at y %d" (. p height))
(nk-layout-row-dynamic ctx 22 3)
(if (nk-button-label ctx "Go to")
@@ -882,6 +1077,28 @@
(nk-label-wrap ctx text))
(== shown 0)
(nk-label ctx "nothing" NK-TEXT-LEFT))
(if (> (. p map-count) 0)
(do
(var maps [char 300])
(var used int (snprintf maps (sizeof maps) "ore maps, not read by the pick:"))
(for (var i int 0) (&& (< i (. p map-count)) (< used (cast (sizeof maps) int))) (++ i)
(+= used (snprintf (+ maps used) (- (sizeof maps) used) "%s %s %.0f%%"
(if (> i 0) "," "")
(. [(. (. a minerals) minerals) [(. p map-minerals) i]] code)
(* 100 [(. p map-factors) i]))))
(nk-layout-row-dynamic ctx 34 1)
(nk-label-wrap ctx maps)))
(nk-layout-row-dynamic ctx 6 1)
(nk-spacing ctx 1)
(nk-layout-row-dynamic ctx 18 1)
(nk-label ctx (if (. p cave-entrance) "Caves: an entrance here" "Caves") NK-TEXT-LEFT)
(if (== (. p cave-count) 0)
(nk-label ctx "none" NK-TEXT-LEFT))
(for (var i int 0) (< i (. p cave-count)) (++ i)
(var top int [(. p cave-tops) i])
(var bottom int [(. p cave-bottoms) i])
(nk-labelf ctx NK-TEXT-LEFT "%3d-%-3d %-14s %3d" top bottom
(if [(. p cave-lava) i] "lava" "air") (+ (- top bottom) 1)))
(nk-tree-pop ctx))))
;;; The block under the cursor; false when the cursor is not on the
@@ -902,8 +1119,9 @@
(do
(var value int (rock-query-value a))
(var mineral int (if (!= (& (. a overlays) MINERAL-OVERLAY-BIT) 0) (. a mineral) -1))
(var caves bool (!= (& (. a overlays) CAVES-OVERLAY-BIT) 0))
(hover-worker/ask (. a hover-worker)
#(hover-query : bx bz (. a rock-mode) value mineral))))
#(hover-query : bx bz (. a rock-mode) value mineral caves))))
(= (. a hover) (hover-worker/result (. a hover-worker))))
(fn rock-name ((a (* app)) (rock int)) (* const char)
@@ -913,7 +1131,6 @@
(fn draw-info ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(var s (* world-settings) (& (. a source settings)))
(var bx int)
(var bz int)
(var inside bool (cursor-block a (& bx) (& bz)))
@@ -922,7 +1139,7 @@
(do
(nk-layout-row-dynamic ctx 18 1)
(nk-labelf ctx NK-TEXT-LEFT "x %d z %d" bx bz)
(nk-labelf ctx NK-TEXT-LEFT "from centre %d, %d" (- bx (/ (. s size-x) 2)) (- bz (/ (. s size-z) 2)))
(nk-labelf ctx NK-TEXT-LEFT "from spawn %d, %d" (- bx (. a spawn-x)) (- bz (. a spawn-z)))
(nk-labelf ctx NK-TEXT-LEFT "region %d, %d zoom %.3g blocks/pt" (/ bx REGION) (/ bz REGION)
(. a zoom))
(if (&& inside (. r valid))
@@ -939,9 +1156,10 @@
(nk-labelf ctx NK-TEXT-LEFT "terrain height %d (sea level %d)" (. r height) (. r sea-level))
(nk-labelf ctx NK-TEXT-LEFT "surface rock %s" (rock-name a (. r surface-rock)))
(if (>= (. r query mineral) 0)
(nk-labelf ctx NK-TEXT-LEFT "%s %s, %.2f ppt"
(. [(. (. a minerals) minerals) (. r query mineral)] code)
[reading-names (ores/category (. r mineral total))] (. r mineral ppt)))))
(do
(var text [char 96])
(mineral-at-cursor a r text (sizeof text))
(nk-label ctx text NK-TEXT-LEFT)))))
(nk-labelf ctx NK-TEXT-LEFT "%d tiles pending" (. a pending))
(nk-label ctx (. a status) NK-TEXT-LEFT)
(nk-tree-pop ctx))))
@@ -957,7 +1175,7 @@
(. a rock-mode) value (. a highlight) (. a center-x) (. a center-z)
(- (SDL-GetNumLogicalCPUCores) 1))
(= (. a searching) true)
(= (. a searching-mineral) false)))
(= (. a search-kind) FIND-ROCK)))
(if (. a find-mineral-requested)
(do
(= (. a find-mineral-requested) false)
@@ -967,7 +1185,23 @@
SEARCH-MINERAL (. a mineral) (cast (. a min-reading) u32)
(. a center-x) (. a center-z) (- (SDL-GetNumLogicalCPUCores) 1))
(= (. a searching) true)
(= (. a searching-mineral) true)))))
(= (. a search-kind) FIND-MINERAL)))))
(if (. a find-cave-requested)
(do
(= (. a find-cave-requested) false)
(search/start (. a search) (. a source settings) (. a rivers) (. a assets-dir)
SEARCH-CAVE-ENTRANCE 0 0 (. a center-x) (. a center-z)
(- (SDL-GetNumLogicalCPUCores) 1))
(= (. a searching) true)
(= (. a search-kind) FIND-CAVE)))
(if (. a find-spring-requested)
(do
(= (. a find-spring-requested) false)
(search/start (. a search) (. a source settings) (. a rivers) (. a assets-dir)
SEARCH-HOT-SPRING 0 0 (. a center-x) (. a center-z)
(- (SDL-GetNumLogicalCPUCores) 1))
(= (. a searching) true)
(= (. a search-kind) FIND-HOT-SPRING)))
(if (! (. a searching))
(return))
(var s (* search) (. a search))
@@ -1009,7 +1243,8 @@
(do
(draw-layers a)
(draw-rocks a)
(draw-minerals a)))
(draw-minerals a)
(draw-caves a)))
(nk-end ctx))
;;; A label by the cursor naming the rock under it, as the Rocks
@@ -1022,7 +1257,8 @@
(fn draw-cursor-rock ((a (* app))) void
(var bx int)
(var bz int)
(if (|| (! (. a show-cursor-rock)) (. a dragging) (! (cursor-block a (& bx) (& bz))))
(if (|| (&& (! (. a show-cursor-rock)) (! (. a picking-spawn))) (. a dragging)
(! (cursor-block a (& bx) (& bz))))
(return))
(var r (* hover-result) (& (. a hover)))
(var text [char 64])
@@ -1039,10 +1275,29 @@
(var reading [char 96])
(= [reading 0] 0)
(if (&& (. r valid) (>= (. r query mineral) 0))
(snprintf reading (sizeof reading) "%s %s, %.2f ppt"
(. [(. (. a minerals) minerals) (. r query mineral)] code)
[reading-names (ores/category (. r mineral total))] (. r mineral ppt)))
(var height float (if [reading 0] 50 30))
(mineral-at-cursor a r reading (sizeof reading)))
;; and the caves through the column, top down
(var caves [char 96])
(= [caves 0] 0)
(if (&& (. r valid) (. r query caves))
(if (. r cave-entrance)
(snprintf caves (sizeof caves) "cave entrance")
(== (. r cave-runs) 0)
(snprintf caves (sizeof caves) "no caves")
(do
(var used int (snprintf caves (sizeof caves) "caves y"))
(for (var i int 0) (&& (< i (. r cave-runs)) (< i 3) (< used 80)) (++ i)
(+= used (snprintf (+ caves used) (- (sizeof caves) used) "%s %d-%d%s" (if (> i 0) "," "")
[(. r cave-tops) i] [(. r cave-bottoms) i]
(if [(. r cave-lava) i] " lava" "")))))))
;; or, while the next click sets the spawn, only that
(if (. a picking-spawn)
(do
(snprintf text (sizeof text) "click to set the spawn here")
(= [reading 0] 0)
(= [caves 0] 0)))
(var lines int (+ 1 (if [reading 0] 1 0) (if [caves 0] 1 0)))
(var height float (+ 12 (* 18 lines)))
;; beside the cursor, not under it, so it never takes the map's input
(var ctx (* struct nk-context) (. a ctx))
(var bounds (struct nk-rect) (nk-rect (+ (. a mouse-x) 16) (+ (. a mouse-y) 16) 260 height))
@@ -1052,7 +1307,9 @@
(nk-layout-row-dynamic ctx 18 1)
(nk-label ctx text NK-TEXT-LEFT)
(if [reading 0]
(nk-label ctx reading NK-TEXT-LEFT))))
(nk-label ctx reading NK-TEXT-LEFT))
(if [caves 0]
(nk-label ctx caves NK-TEXT-LEFT))))
(nk-end ctx))
;;; Events ---------------------------------------------------------
@@ -1098,11 +1355,21 @@
(var bx int (cast (floor (to-world-x a (. event button x))) int))
(var bz int (cast (floor (to-world-z a (. event button y))) int))
(var pin int (pin-at a (. event button x) (. event button y)))
(if (>= pin 0)
(var on-world bool (&& (>= bx 0) (>= bz 0) (< bx (. a source settings size-x))
(< bz (. a source settings size-z))))
(if (&& (. a picking-spawn) on-world)
(do
(= (. a spawn-x) bx)
(= (. a spawn-z) bz)
(= (. a picking-spawn) false))
(>= pin 0)
(probe-at a [(. a pins) (* pin 2)] [(. a pins) (+ (* pin 2) 1)])
(&& (>= bx 0) (>= bz 0) (< bx (. a source settings size-x)) (< bz (. a source settings size-z)))
on-world
(probe-at a bx bz))))
(= (. a dragging) false))
;; Escape gives up setting the spawn
(&& (== type SDL-EVENT-KEY-DOWN) (== (. event key key) SDLK-ESCAPE))
(= (. a picking-spawn) false)
(&& (== type SDL-EVENT-MOUSE-WHEEL) (! (. a ui-hovered)))
(zoom-at a (pow 1.25 (- (. event wheel y)))))
(nk-sdl-handle-event (. a ctx) event)
@@ -1166,6 +1433,12 @@
(= (. a mouse-x) (cast (atof [argv (+ i 1)]) float))
(= (. a mouse-y) (cast (atof [argv (+ i 2)]) float))
(+= i 2))
(&& (== (strcmp [argv i] "--spawn") 0) (< (+ i 2) argc))
(do
(= (. a spawn-arg) true)
(= (. a arg-spawn-x) (atoi [argv (+ i 1)]))
(= (. a arg-spawn-z) (atoi [argv (+ i 2)]))
(+= i 2))
(&& (== (strcmp [argv i] "--probe") 0) (< (+ i 2) argc))
(do
(= (. a probe-x) (atoi [argv (+ i 1)]))
@@ -1179,6 +1452,10 @@
(do (++ i) (= (. a min-reading) (reading-named [argv i])))
(== (strcmp [argv i] "--find-mineral") 0)
(= (. a find-mineral-requested) true)
(== (strcmp [argv i] "--find-cave") 0)
(= (. a find-cave-requested) true)
(== (strcmp [argv i] "--find-hot-spring") 0)
(= (. a find-spring-requested) true)
(&& (== (strcmp [argv i] "--highlight") 0) more)
(do
(++ i)
@@ -1219,6 +1496,11 @@
(do
(fprintf stderr "%s\n" (. a status))
(return 1)))
(reset-spawn a)
(if (. a spawn-arg)
(do
(= (. a spawn-x) (. a arg-spawn-x))
(= (. a spawn-z) (. a arg-spawn-z))))
(go-to-spawn a)
(if (|| (. a probe-x) (. a probe-z))
(probe-at a (. a probe-x) (. a probe-z)))
@@ -1260,7 +1542,8 @@
(SDL-SetRenderClipRect (. a renderer) NULL)
(nk-sdl-render (. a ctx) NK-ANTI-ALIASING-ON)
(if (&& (. a screenshot) (== (. a pending) 0) (! (. a searching)) (! (. a find-requested))
(! (. a find-mineral-requested)))
(! (. a find-mineral-requested)) (! (. a find-cave-requested))
(! (. a find-spring-requested)))
(do
(if (! (save-screenshot a))
(fprintf stderr "%s: %s\n" (. a screenshot) (SDL-GetError)))

View File

@@ -28,6 +28,9 @@
(return 1))
(printf "seed %d\n" (. s seed))
(printf "size %d x %d x %d\n" (. s size-x) (. s size-y) (. s size-z))
(if (. s has-spawn)
(printf "spawn %d %d %d\n" (. s spawn-x) (. s spawn-y) (. s spawn-z))
(printf "spawn the map's middle\n"))
(printf "block ids %d\n" (. s block-count))
(printf "world config, %d values:\n" (. s config-count))
(for (var i int 0) (< i (. s config-count)) (++ i)
@@ -88,6 +91,8 @@
(var failed int (+ (verify-stories s g) (verify-regions s g out)
(verify-ores s minerals out)
(verify-readings s g (river-config/defaults) rocks minerals)
(verify-strata s g (river-config/defaults) rocks out)
(verify-caves s g (river-config/defaults) out)
(verify-rivers s g (river-config/defaults) out)
(verify-heights s g (river-config/defaults) rocks out)))
(save/close s)

View File

@@ -767,7 +767,18 @@
;;; forces on the region maps around it.
(pub record story
((code [char 64]) (x int) (z int) (dir-x int) (landform-radius int)
(landform int) (forces-climate bool) (climate int)))
(landform int) (forces-climate bool) (climate int)
;; skipGenerationCategories' "caves" and "hotsprings": none within
;; this radius, or in the schematic's footprint when it is 0; -1 when
;; not skipped
(cave-skip int) (spring-skip int) (size-x int) (size-z int)))
;;; The footprints of the story schematics that skip caves by footprint
;;; rather than by radius. The schematics are not among the assets this
;;; reads, so their sizes are written here, as the 1.22.7 save records
;;; the village's.
(record story-size ((code (* const char)) (x int) (z int)))
(var story-sizes [story-size 1] #(#(story-size : "village" 195 225)))
(fn json-int ((o (* cJSON)) (key (* const char)) (fallback int)) int
(return (cast (json-number o key fallback) int)))
@@ -826,6 +837,16 @@
(= (. st z) (+ base-z (* dist-z dir-z)))
(= (. st dir-x) dir-x)
(= (. st landform-radius) (json-int c "landformRadius" 0))
(var skips (* cJSON) (cJSON-GetObjectItem c "skipGenerationCategories"))
(var skip (* cJSON) (cJSON-GetObjectItem skips "caves"))
(= (. st cave-skip) (if (cJSON-IsNumber skip) (cast (cJSON-GetNumberValue skip) int) -1))
(var spring-skip (* cJSON) (cJSON-GetObjectItem skips "hotsprings"))
(= (. st spring-skip) (if (cJSON-IsNumber spring-skip) (cast (cJSON-GetNumberValue spring-skip) int) -1))
(for (var k int 0) (< k 1) (++ k)
(if (== (strcmp (. [story-sizes k] code) (. st code)) 0)
(do
(= (. st size-x) (. [story-sizes k] x))
(= (. st size-z) (. [story-sizes k] z)))))
(var landform (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem c "requireLandform")))
(= (. st landform) (if landform (landform-table/find landforms landform) -1))
(var force-temp (* cJSON) (cJSON-GetObjectItem c "forceTemperature"))
@@ -1086,6 +1107,39 @@
(for (var i int 0) (< i (. g strata count)) (++ i)
(worldgen/map g MAP-STRATA i rx rz (& [(. out strata) i]))))
;;; GenStoryStructures.GetBlockingStructureAt: whether a story location
;;; keeps a category out of block column (x, z), by the radius SKIP
;;; reads off it.
(fn story-blocks ((g (* const worldgen)) (x int) (z int) (springs bool)) bool
(for (var i int 0) (< i (. g story-count)) (++ i)
(var st (* const story) (& [(. g stories) i]))
(var r int (if springs (. st spring-skip) (. st cave-skip)))
(if (< r 0)
(continue))
(if (== r 0)
(do
;; the schematic's cuboid, X2 = X1 + SizeX
(var x1 int (- (. st x) (/ (. st size-x) 2)))
(var z1 int (- (. st z) (/ (. st size-z) 2)))
(if (&& (>= x x1) (<= x (+ x1 (. st size-x))) (>= z z1) (<= z (+ z1 (. st size-z))))
(return true))
(continue)))
;; BlockPos.HorDistanceSqTo: in doubles, returned as a float
(var dx double (- x (. st x)))
(var dz double (- z (. st z)))
(var d float (cast (+ (* dx dx) (* dz dz)) float))
(if (< d (cast (* r r) float))
(return true)))
(return false))
;;; For the "caves" category.
(pub fn worldgen/caves-blocked ((g (* const worldgen)) (x int) (z int)) bool
(return (story-blocks g x z false)))
;;; For the "hotsprings" category.
(pub fn worldgen/springs-blocked ((g (* const worldgen)) (x int) (z int)) bool
(return (story-blocks g x z true)))
(pub fn worldgen/free ((g (* worldgen))) void
(for (var i int 0) (< i (. g landform-table count)) (++ i)
(free (. [(. g landform-table all) i] y-thresholds)))