Add ability to set custom spawn location

This commit is contained in:
2026-10-15 23:53:45 +03:00
parent dc15ec21a4
commit e42e7b46b5
6 changed files with 139 additions and 27 deletions

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,

View File

@@ -47,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:
@@ -69,6 +70,7 @@ Options:
| =--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 |
@@ -107,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;
@@ -133,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

View File

@@ -35,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.
@@ -233,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)))

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

@@ -5,7 +5,7 @@
;;; [--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] [--find-cave]
;;; [--find-hot-spring]
;;; [--find-hot-spring] [--spawn X Z]
;;; [--probe X Z] [--mouse X Y]
;;; [--screenshot FILE.png] [WORLD.vcdbs | serverconfig.json]
;;;
@@ -151,6 +151,11 @@
(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
@@ -239,9 +244,23 @@
(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 ----------------------------------------------
@@ -382,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))))
@@ -531,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))
@@ -636,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
@@ -989,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")
@@ -1089,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)))
@@ -1098,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))
@@ -1216,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])
@@ -1248,6 +1290,12 @@
(+= 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
@@ -1307,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)
@@ -1375,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)]))
@@ -1432,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)))

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)