Files
VS-map-previewer/src/settings.sex
alex-eg 28f0e8acac pub-import the modules a unit's interface holds by value
sexc now checks that importers can see every type a pub form holds,
and asks for pub where the type comes from another import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:05:14 +03:00

76 lines
3.1 KiB
Scheme

;;; Where world settings come from: a save, which knows the seed as a
;;; number and the world size as generated, or a server config, which
;;; has the seed as typed and the world config as strings.
(include stdio.h stdlib.h string.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import cs)
(import json)
(import maps)
(import lcg)
(import noise)
(import shape)
(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])))
(fn ends-with ((s (* const char)) (suffix (* const char))) bool
(var n size-t (strlen s))
(var m size-t (strlen suffix))
(return (&& (>= n m) (== (strcmp (+ s (- n m)) suffix) 0))))
(fn from-save ((path (* const char)) (out (* world-source))) bool
(var s (* save) (save/open path))
(if (! s)
(return false))
(for (var i int 0) (< i (. s config-count)) (++ i)
(.set (& (. out settings)) [(. s config-keys) i] [(. s config-values) i]))
;; The sizes the world was generated with, over what the config says.
(= (. out settings seed) (. s seed))
(= (. out settings size-x) (. s size-x))
(= (. 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))
(save/close s)
(return true))
;;; serverconfig.json: WorldConfig.Seed and WorldConfig.WorldConfiguration.
(fn from-server-config ((path (* const char)) (out (* world-source))) bool
(var json (* cJSON) (json-load path))
(if (! json)
(return false))
(var world (* cJSON) (cJSON-GetObjectItem json "WorldConfig"))
(var seed (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem world "Seed")))
(if (! seed)
(do
(fprintf stderr "%s: no WorldConfig.Seed\n" path)
(cJSON-Delete json)
(return false)))
(snprintf (. out seed-text) (sizeof (. out seed-text)) "%s" seed)
(= (. out settings seed) (world-seed seed))
(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)))
(var config (* cJSON) (cJSON-GetObjectItem world "WorldConfiguration"))
(for (var item (* cJSON) (if config (. config child) NULL)) item (= item (. item next))
(var text [char 64])
(if (cJSON-IsString item) (snprintf text 64 "%s" (cJSON-GetStringValue item))
(cJSON-IsNumber item) (snprintf text 64 "%.17g" (cJSON-GetNumberValue item))
(cJSON-IsBool item) (snprintf text 64 "%s" (if (cJSON-IsTrue item) "true" "false"))
(continue))
(.set (& (. out settings)) (. item string) text))
(cJSON-Delete json)
(return true))
;;; A .vcdbs save or a serverconfig.json, by extension.
(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")
(if (ends-with path ".vcdbs")
(return (from-save path out)))
(return (from-server-config path out)))