98.2% of stored columns match exactly; the rest is the dry seabed raise and surface caves, both later passes that the strata never see. GenTerra thresholds the new terrain noise through the old tanh curve's inverse, and matching that quirk is what moved it from 87%. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
556 lines
24 KiB
Scheme
556 lines
24 KiB
Scheme
;;; The map viewer window: SDL3 for the window, events and rendering,
|
|
;;; Nuklear for the panels drawn over the map, tiles for the map.
|
|
;;;
|
|
;;; vs-map-viewer [--assets DIR] [--zoom BLOCKS-PER-POINT] [--layer N]
|
|
;;; [--screenshot FILE.png] [WORLD.vcdbs | serverconfig.json]
|
|
;;;
|
|
;;; --screenshot draws until no tile is pending, saves the window and
|
|
;;; quits, which is how the viewer is checked without looking at it.
|
|
|
|
(include stdio.h stdlib.h string.h math.h "nk.h" "stb_image_write.h")
|
|
(import cs)
|
|
(import json)
|
|
(import maps)
|
|
(import lcg)
|
|
(import noise)
|
|
(import shape)
|
|
(import worldgen)
|
|
(import fastnoise)
|
|
(import rivers)
|
|
(import terra)
|
|
(import settings)
|
|
(import tiles)
|
|
|
|
(define WINDOW-WIDTH 1280)
|
|
(define WINDOW-HEIGHT 800)
|
|
(define FONT-SIZE 13.0)
|
|
(define REGION 512)
|
|
;; Blocks per window point, either way. Zoomed out further, a window
|
|
;; shows more regions than the tile cache holds.
|
|
(define MIN-ZOOM 0.0625)
|
|
(define MAX-ZOOM 32.0)
|
|
|
|
(var layer-names [(* const char) 8]
|
|
#("Landform" "Climate" "Geologic province" "Upheaval" "Ocean" "Rock strata thickness"
|
|
"Rivers" "Terrain height"))
|
|
(var climate-names [(* const char) 5] #("temperate" "hot" "warm" "cool" "icy"))
|
|
|
|
;;; What the region under the cursor holds, generated on the main
|
|
;;; thread when the cursor moves to another region.
|
|
(record hover ((valid bool) (rx int) (rz int) (maps region-maps)
|
|
(plate (* river-plate)) (segments (* int))
|
|
(terra (* terra)) (cx int) (cz int) (has-chunk bool) (heights [u16 1024])))
|
|
|
|
(record app
|
|
((window (* SDL-Window)) (renderer (* SDL-Renderer)) (ctx (* struct nk-context))
|
|
(assets-dir [char 1024]) (world-path [char 1024]) (rivers-path [char 1024])
|
|
(rivers river-config) (sampler river-sampler)
|
|
(source world-source) (edit world-source) (g (* worldgen)) (tiles (* tiles))
|
|
(center-x double) (center-z double) (zoom double) (layer int) (stratum int)
|
|
(mouse-x float) (mouse-y float) (dragging bool) (ui-hovered bool)
|
|
(hover hover) (pending int) (status [char 256]) (screenshot (* const char))))
|
|
|
|
(fn fail ((what (* const char))) int
|
|
(SDL-Log "%s: %s" what (SDL-GetError))
|
|
(return 1))
|
|
|
|
;;; Bakes Nuklear's default font at the display scale, then shrinks the
|
|
;;; reported height back, so text is crisp on a Retina display and still
|
|
;;; laid out in window units.
|
|
(fn setup-font ((ctx (* struct nk-context)) (scale float)) void
|
|
(var config (struct nk-font-config) (nk-font-config 0))
|
|
(var atlas (* struct nk-font-atlas) (nk-sdl-font-stash-begin ctx))
|
|
(var font (* struct nk-font)
|
|
(nk-font-atlas-add-default atlas (* FONT-SIZE scale) (& config)))
|
|
(nk-sdl-font-stash-end ctx)
|
|
(/= (. font handle height) scale)
|
|
(nk-style-set-font ctx (& (. font handle))))
|
|
|
|
;;; World ↔ window -------------------------------------------------
|
|
|
|
(fn window-size ((a (* app)) (w (* int)) (h (* int))) void
|
|
(SDL-GetWindowSize (. a window) w h))
|
|
|
|
(fn to-world-x ((a (* app)) (sx float) (w int)) double
|
|
(return (+ (. a center-x) (* (- sx (/ w 2.0)) (. a zoom)))))
|
|
|
|
(fn to-world-z ((a (* app)) (sy float) (h int)) double
|
|
(return (+ (. a center-z) (* (- sy (/ h 2.0)) (. a zoom)))))
|
|
|
|
(fn to-screen ((a (* app)) (world double) (center double) (extent int)) float
|
|
(return (cast (+ (/ (- world center) (. a zoom)) (/ extent 2.0)) float)))
|
|
|
|
(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)))
|
|
|
|
;;; Applying settings ----------------------------------------------
|
|
|
|
(fn release-hover ((a (* app))) void
|
|
(if (. a hover valid)
|
|
(region-maps/free (& (. a hover maps))))
|
|
(= (. a hover valid) false)
|
|
(if (. a hover plate)
|
|
(river-plate/free (. a hover plate)))
|
|
(= (. a hover plate) NULL)
|
|
(if (. a hover terra)
|
|
(terra/free (. a hover terra)))
|
|
(= (. a hover terra) NULL)
|
|
(= (. a hover has-chunk) false))
|
|
|
|
;;; Makes SOURCE the world shown: a fresh generator for the panels,
|
|
;;; and every tile regenerated.
|
|
(fn apply ((a (* app)) (source world-source)) bool
|
|
(var g (* worldgen) (worldgen/make (. source settings) (. a assets-dir)))
|
|
(if (! g)
|
|
(do
|
|
(snprintf (. a status) (sizeof (. a status)) "cannot load worldgen assets from %s"
|
|
(. a assets-dir))
|
|
(return false)))
|
|
(if (. a g)
|
|
(worldgen/free (. a g)))
|
|
(= (. a g) g)
|
|
(= (. a source) source)
|
|
(= (. a edit) source)
|
|
(release-hover a)
|
|
(if (>= (. a stratum) (. g strata count))
|
|
(= (. a stratum) 0))
|
|
(= (. a sampler) (river-sampler/make (& (. a rivers)) (. source settings size-y)))
|
|
(.set-settings (. a tiles) (. source settings) (. a rivers))
|
|
(snprintf (. a status) (sizeof (. a status)) "seed %d" (. source settings seed))
|
|
(return true))
|
|
|
|
;;; ModConfig/rivers.json, or the mod's defaults for an empty path.
|
|
(fn load-rivers ((a (* app))) void
|
|
(var config river-config (river-config/defaults))
|
|
(if (&& (* (. a rivers-path)) (! (river-config/load (. a rivers-path) (& config))))
|
|
(do
|
|
(snprintf (. a status) (sizeof (. a status)) "cannot load %s" (. a rivers-path))
|
|
(return)))
|
|
(= (. a rivers) config)
|
|
(apply a (. a source)))
|
|
|
|
(fn load-world ((a (* app))) void
|
|
(var source world-source)
|
|
(if (world-source/load (. a world-path) (& source))
|
|
(do
|
|
(apply a source)
|
|
(go-to-spawn a))
|
|
(snprintf (. a status) (sizeof (. a status)) "cannot load %s" (. a world-path))))
|
|
|
|
;;; Map ------------------------------------------------------------
|
|
|
|
(record wanted ((rx int) (rz int) (distance double)))
|
|
|
|
(fn by-distance ((pa (* const void)) (pb (* const void))) int
|
|
(var wa (* const wanted) pa)
|
|
(var wb (* const wanted) pb)
|
|
(var da double (. wa distance))
|
|
(var db double (. wb distance))
|
|
(return (- (> da db) (< da db))))
|
|
|
|
;;; River samples per region side: about one per point on screen,
|
|
;;; at most one per 8 blocks.
|
|
(fn river-resolution ((zoom double)) int
|
|
(return (if (<= zoom 8) 64 (if (<= zoom 16) 32 16))))
|
|
|
|
;;; Height samples per region side: one per block close up, down to
|
|
;;; one per chunk for an overview, where a column costs ~11 us.
|
|
(fn height-resolution ((zoom double)) int
|
|
(if (< zoom 1) (return 512))
|
|
(if (< zoom 2) (return 256))
|
|
(if (< zoom 4) (return 128))
|
|
(if (< zoom 8) (return 64))
|
|
(if (< zoom 16) (return 32))
|
|
(return 16))
|
|
|
|
;;; Requests the visible tiles, nearest the centre first, and draws
|
|
;;; those that are ready.
|
|
(fn draw-map ((a (* app))) void
|
|
(var w int)
|
|
(var h int)
|
|
(window-size a (& w) (& h))
|
|
(var s (* world-settings) (& (. a source settings)))
|
|
(var x0 double (to-world-x a 0 w))
|
|
(var z0 double (to-world-z a 0 h))
|
|
(var x1 double (to-world-x a w w))
|
|
(var z1 double (to-world-z a h h))
|
|
(var rx0 int (cast (fmax 0 (floor (/ x0 REGION))) int))
|
|
(var rz0 int (cast (fmax 0 (floor (/ z0 REGION))) int))
|
|
(var rx1 int (cast (fmin (- (/ (. s size-x) REGION) 1) (floor (/ x1 REGION))) int))
|
|
(var rz1 int (cast (fmin (- (/ (. s size-z) REGION) 1) (floor (/ z1 REGION))) int))
|
|
(var count int (* (fmax 0 (+ (- rx1 rx0) 1)) (fmax 0 (+ (- rz1 rz0) 1))))
|
|
(var list (* wanted) (malloc (* (+ count 1) (sizeof wanted))))
|
|
(var n int 0)
|
|
(for (var rz int rz0) (<= rz rz1) (++ rz)
|
|
(for (var rx int rx0) (<= rx rx1) (++ rx)
|
|
(var dx double (- (* (+ rx 0.5) REGION) (. a center-x)))
|
|
(var dz double (- (* (+ rz 0.5) REGION) (. a center-z)))
|
|
(= [list n] #(wanted : rx rz (+ (* dx dx) (* dz dz))))
|
|
(++ n)))
|
|
(qsort list n (sizeof wanted) by-distance)
|
|
|
|
(.begin-frame (. a tiles))
|
|
(for (var i int 0) (< i n) (++ i)
|
|
(var k tile-key)
|
|
(= (. k map) (. a layer))
|
|
(= (. k stratum) (. a stratum))
|
|
(if (== (. a layer) LAYER-RIVERS) (= (. k stratum) (river-resolution (. a zoom))))
|
|
(if (== (. a layer) LAYER-HEIGHT) (= (. k stratum) (height-resolution (. a zoom))))
|
|
(= (. k rx) (. [list i] rx))
|
|
(= (. k rz) (. [list i] rz))
|
|
(var tex (* SDL-Texture) (.request (. a tiles) (. a renderer) k))
|
|
(if (! tex)
|
|
(continue))
|
|
(var sx0 float (roundf (to-screen a (* (. k rx) REGION) (. a center-x) w)))
|
|
(var sz0 float (roundf (to-screen a (* (. k rz) REGION) (. a center-z) h)))
|
|
(var sx1 float (roundf (to-screen a (* (+ (. k rx) 1) REGION) (. a center-x) w)))
|
|
(var sz1 float (roundf (to-screen a (* (+ (. k rz) 1) REGION) (. a center-z) h)))
|
|
(var dst SDL-FRect #(SDL-FRect : sx0 sz0 (- sx1 sx0) (- sz1 sz0)))
|
|
(SDL-RenderTexture (. a renderer) tex NULL (& dst)))
|
|
(= (. a pending) (.end-frame (. a tiles)))
|
|
(free list))
|
|
|
|
;;; Spawn and the story locations, as small squares with their names.
|
|
(fn draw-markers ((a (* app))) void
|
|
(var w int)
|
|
(var h int)
|
|
(window-size a (& w) (& h))
|
|
(var r (* SDL-Renderer) (. a renderer))
|
|
(var g (* worldgen) (. a g))
|
|
(SDL-SetRenderDrawColor r 255 255 255 255)
|
|
(for (var i int -1) (< i (. g story-count)) (++ i)
|
|
(var x double (/ (. a source settings size-x) 2.0))
|
|
(var z double (/ (. a source settings size-z) 2.0))
|
|
(var name (* const char) "spawn")
|
|
(if (>= i 0)
|
|
(do
|
|
(= x (. [(. g stories) i] x))
|
|
(= z (. [(. g stories) i] z))
|
|
(= name (. [(. g stories) i] code))))
|
|
(var sx float (to-screen a x (. a center-x) w))
|
|
(var sy float (to-screen a z (. a center-z) h))
|
|
(var box SDL-FRect #(SDL-FRect : (- sx 3) (- sy 3) 7 7))
|
|
(SDL-RenderRect r (& box))
|
|
(SDL-RenderDebugText r (+ sx 6) (- sy 4) name)))
|
|
|
|
;;; Panels ---------------------------------------------------------
|
|
|
|
(fn property-float ((ctx (* struct nk-context)) (name (* const char)) (v (* float))
|
|
(lo float) (hi float) (step float)) void
|
|
(nk-property-float ctx name lo v hi step (/ step 4)))
|
|
|
|
(fn draw-settings ((a (* app))) void
|
|
(var ctx (* struct nk-context) (. a ctx))
|
|
(var e (* world-settings) (& (. a edit settings)))
|
|
(if (nk-begin ctx "World" (nk-rect 10 10 290 580)
|
|
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE
|
|
NK-WINDOW-SCALABLE))
|
|
(do
|
|
(nk-layout-row-dynamic ctx 22 1)
|
|
(nk-label ctx "World or server config file" NK-TEXT-LEFT)
|
|
(nk-layout-row-template-begin ctx 24)
|
|
(nk-layout-row-template-push-dynamic ctx)
|
|
(nk-layout-row-template-push-static ctx 50)
|
|
(nk-layout-row-template-end ctx)
|
|
(nk-edit-string-zero-terminated ctx NK-EDIT-FIELD (. a world-path)
|
|
(sizeof (. a world-path)) nk-filter-default)
|
|
(if (nk-button-label ctx "Load")
|
|
(load-world a))
|
|
(nk-layout-row-dynamic ctx 22 1)
|
|
(nk-label ctx "Rivers config (empty: defaults)" NK-TEXT-LEFT)
|
|
(nk-layout-row-template-begin ctx 24)
|
|
(nk-layout-row-template-push-dynamic ctx)
|
|
(nk-layout-row-template-push-static ctx 50)
|
|
(nk-layout-row-template-end ctx)
|
|
(nk-edit-string-zero-terminated ctx NK-EDIT-FIELD (. a rivers-path)
|
|
(sizeof (. a rivers-path)) nk-filter-default)
|
|
(if (nk-button-label ctx "Load")
|
|
(load-rivers a))
|
|
|
|
(nk-layout-row-dynamic ctx 8 1)
|
|
(nk-spacing ctx 1)
|
|
(nk-layout-row-template-begin ctx 24)
|
|
(nk-layout-row-template-push-static ctx 40)
|
|
(nk-layout-row-template-push-dynamic ctx)
|
|
(nk-layout-row-template-end ctx)
|
|
(nk-label ctx "Seed" NK-TEXT-LEFT)
|
|
(nk-edit-string-zero-terminated ctx NK-EDIT-FIELD (. a edit seed-text)
|
|
(sizeof (. a edit seed-text)) nk-filter-default)
|
|
(nk-layout-row-dynamic ctx 22 1)
|
|
(nk-property-int ctx "World width" 512 (& (. e size-x)) 8192000 512 512)
|
|
(nk-property-int ctx "World length" 512 (& (. e size-z)) 8192000 512 512)
|
|
(nk-property-int ctx "World height" 256 (& (. e size-y)) 1024 64 64)
|
|
(property-float ctx "Landcover" (& (. e landcover)) 0 1 0.05)
|
|
(property-float ctx "Ocean scale" (& (. e oceanscale)) 0.1 10 0.1)
|
|
(property-float ctx "Landform scale" (& (. e landform-scale)) 0.1 4 0.1)
|
|
(property-float ctx "Upheaval commonness" (& (. e upheaval-commonness)) 0 1 0.05)
|
|
(property-float ctx "Geologic activity" (& (. e geologic-activity)) 0.01 1 0.05)
|
|
(nk-property-int ctx "Polar-equator distance" 1000 (& (. e polar-equator-distance))
|
|
1000000 1000 100)
|
|
(property-float ctx "Global temperature" (& (. e global-temperature)) 0 5 0.1)
|
|
(property-float ctx "Global precipitation" (& (. e global-precipitation)) 0 5 0.1)
|
|
(property-float ctx "Story distance scale" (& (. e story-dist-scaling)) 0.05 2 0.05)
|
|
(var current int 0)
|
|
(for (var i int 0) (< i 5) (++ i)
|
|
(if (== (strcmp (. e starting-climate) [climate-names i]) 0)
|
|
(= current i)))
|
|
(nk-layout-row-dynamic ctx 22 2)
|
|
(nk-label ctx "Starting climate" NK-TEXT-LEFT)
|
|
(var chosen int (nk-combo ctx climate-names 5 current 20 (nk-vec2 140 120)))
|
|
(snprintf (. e starting-climate) 16 "%s" [climate-names chosen])
|
|
|
|
(nk-layout-row-dynamic ctx 26 2)
|
|
(if (nk-button-label ctx "Apply")
|
|
(do
|
|
(var source world-source (. a edit))
|
|
(= (. source settings seed) (world-seed (. source seed-text)))
|
|
(apply a source)))
|
|
(if (nk-button-label ctx "Go to spawn")
|
|
(go-to-spawn a))))
|
|
(nk-end ctx))
|
|
|
|
(fn draw-layers ((a (* app))) void
|
|
(var ctx (* struct nk-context) (. a ctx))
|
|
(var w int)
|
|
(var h int)
|
|
(window-size a (& w) (& h))
|
|
(if (nk-begin ctx "Layer" (nk-rect (- w 250) 10 240 260)
|
|
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE))
|
|
(do
|
|
(nk-layout-row-dynamic ctx 20 1)
|
|
(for (var i int 0) (< i 8) (++ i)
|
|
(if (nk-option-label ctx [layer-names i] (== (. a layer) i))
|
|
(= (. a layer) i)))
|
|
(if (== (. a layer) MAP-STRATA)
|
|
(do
|
|
(var g (* worldgen) (. a g))
|
|
(nk-layout-row-dynamic ctx 22 1)
|
|
(= (. a stratum)
|
|
(nk-combo ctx (cast (. g strata codes) (* * const char)) (. g strata count)
|
|
(. a stratum) 20 (nk-vec2 200 300)))))))
|
|
(nk-end ctx))
|
|
|
|
;;; A map's value at a block of its region.
|
|
(fn sample ((m (* const int-map)) (rx int) (rz int) (bx int) (bz int)) int
|
|
(var inner int (- (. m size) (. m pad-tl) (. m pad-br)))
|
|
(var px int (+ (. m pad-tl) (/ (* (- bx (* rx REGION)) inner) REGION)))
|
|
(var pz int (+ (. m pad-tl) (/ (* (- bz (* rz REGION)) inner) REGION)))
|
|
(return [(. m data) (+ (* pz (. m size)) px)]))
|
|
|
|
;;; The terrain height at block (x, z), its chunk computed once.
|
|
(fn hover-height ((a (* app)) (x int) (z int)) int
|
|
(if (! (. a hover terra))
|
|
(= (. a hover terra) (terra/make (. a g) (. a rivers))))
|
|
(var cx int (/ x 32))
|
|
(var cz int (/ z 32))
|
|
(if (|| (! (. a hover has-chunk)) (!= (. a hover cx) cx) (!= (. a hover cz) cz))
|
|
(do
|
|
(terra/chunk-heights (. a hover terra) cx cz 1 (. a hover heights))
|
|
(= (. a hover cx) cx)
|
|
(= (. a hover cz) cz)
|
|
(= (. a hover has-chunk) true)))
|
|
(return [(. a hover heights) (+ (* (% z 32) 32) (% x 32))]))
|
|
|
|
;;; Rivers' sample at block (x, z), building the plate it needs.
|
|
(fn hover-river ((a (* app)) (x int) (z int)) river-sample
|
|
(var per-plate int (river-plate/chunks (& (. a rivers))))
|
|
(var cx int (/ x 32))
|
|
(var cz int (/ z 32))
|
|
(var p (* river-plate) (. a hover plate))
|
|
(if (|| (! p) (!= (. p px) (/ cx per-plate)) (!= (. p pz) (/ cz per-plate)))
|
|
(do
|
|
(if p (river-plate/free p))
|
|
(= p (river-plate/make (. a rivers) (& (. a g oceans)) (. a source settings size-y)
|
|
(/ cx per-plate) (/ cz per-plate)))
|
|
(= (. a hover plate) p)
|
|
(= (. a hover segments) (realloc (. a hover segments)
|
|
(* (+ (. p segment-count) 1) (sizeof int))))))
|
|
(var count int (river-plate/chunk-segments p cx cz (. a hover segments)))
|
|
(return (river-sampler/sample (& (. a sampler)) p (. a hover segments) count x z)))
|
|
|
|
(fn draw-info ((a (* app))) void
|
|
(var ctx (* struct nk-context) (. a ctx))
|
|
(var w int)
|
|
(var h int)
|
|
(window-size a (& w) (& h))
|
|
(var s (* world-settings) (& (. a source settings)))
|
|
(var bx int (cast (floor (to-world-x a (. a mouse-x) w)) int))
|
|
(var bz int (cast (floor (to-world-z a (. a mouse-y) h)) int))
|
|
(var inside bool (&& (>= bx 0) (>= bz 0) (< bx (. s size-x)) (< bz (. s size-z))))
|
|
(var rx int (/ bx REGION))
|
|
(var rz int (/ bz REGION))
|
|
(if (&& inside (|| (! (. a hover valid)) (!= (. a hover rx) rx) (!= (. a hover rz) rz)))
|
|
(do
|
|
(release-hover a)
|
|
(worldgen/region (. a g) rx rz (& (. a hover maps)))
|
|
(= (. a hover valid) true)
|
|
(= (. a hover rx) rx)
|
|
(= (. a hover rz) rz)))
|
|
(if (nk-begin ctx "Info" (nk-rect 10 (- h 270) 290 260)
|
|
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE))
|
|
(do
|
|
(nk-layout-row-dynamic ctx 18 1)
|
|
(nk-labelf ctx NK-TEXT-LEFT "x %d z %d (from centre %d, %d)" bx bz
|
|
(- bx (/ (. s size-x) 2)) (- bz (/ (. s size-z) 2)))
|
|
(nk-labelf ctx NK-TEXT-LEFT "region %d, %d zoom %.3g blocks/pt" rx rz (. a zoom))
|
|
(if (&& inside (. a hover valid))
|
|
(do
|
|
(var g (* worldgen) (. a g))
|
|
(var m (* region-maps) (& (. a hover maps)))
|
|
(var lf int (sample (& (. m landform)) rx rz bx bz))
|
|
(nk-labelf ctx NK-TEXT-LEFT "landform %s" (. [(. g landform-table all) lf] code))
|
|
(var pv int (sample (& (. m geoprov)) rx rz bx bz))
|
|
(nk-labelf ctx NK-TEXT-LEFT "province %s" [(. g provinces codes) pv])
|
|
(var c int (sample (& (. m climate)) rx rz bx bz))
|
|
(nk-labelf ctx NK-TEXT-LEFT "climate %d C at sea level, rain %d, activity %d"
|
|
(scaled-temperature (& (>> c 16) 255)) (& (>> c 8) 255) (& c 255))
|
|
(nk-labelf ctx NK-TEXT-LEFT "ocean %d upheaval %d"
|
|
(sample (& (. m ocean)) rx rz bx bz)
|
|
(sample (& (. m upheaval)) rx rz bx bz))
|
|
(var r river-sample (hover-river a bx bz))
|
|
(nk-labelf ctx NK-TEXT-LEFT "river distance %.0f, bank depth %.3f"
|
|
(. r distance) (. r bank))
|
|
(nk-labelf ctx NK-TEXT-LEFT "terrain height %d (sea level %d)"
|
|
(hover-height a bx bz) (. a hover terra sea-level))))
|
|
(nk-labelf ctx NK-TEXT-LEFT "%d tiles pending" (. a pending))
|
|
(nk-label ctx (. a status) NK-TEXT-LEFT)))
|
|
(nk-end ctx))
|
|
|
|
;;; Events ---------------------------------------------------------
|
|
|
|
;;; Zooms by FACTOR keeping the block under the cursor where it is.
|
|
(fn zoom-at ((a (* app)) (factor double)) void
|
|
(var w int)
|
|
(var h int)
|
|
(window-size a (& w) (& h))
|
|
(var wx double (to-world-x a (. a mouse-x) w))
|
|
(var wz double (to-world-z a (. a mouse-y) h))
|
|
(= (. a zoom) (fmin MAX-ZOOM (fmax MIN-ZOOM (* (. a zoom) factor))))
|
|
(= (. a center-x) (- wx (* (- (. a mouse-x) (/ w 2.0)) (. a zoom))))
|
|
(= (. a center-z) (- wz (* (- (. a mouse-y) (/ h 2.0)) (. a zoom)))))
|
|
|
|
(fn handle-event ((a (* app)) (event (* SDL-Event))) bool
|
|
(var type u32 (. event type))
|
|
(if (== type SDL-EVENT-QUIT)
|
|
(return false))
|
|
(SDL-ConvertEventToRenderCoordinates (. a renderer) event)
|
|
(if (== type SDL-EVENT-MOUSE-MOTION)
|
|
(do
|
|
(if (. a dragging)
|
|
(do
|
|
(-= (. a center-x) (* (. event motion xrel) (. a zoom)))
|
|
(-= (. a center-z) (* (. event motion yrel) (. a zoom)))))
|
|
(= (. a mouse-x) (. event motion x))
|
|
(= (. a mouse-y) (. event motion y)))
|
|
(&& (== type SDL-EVENT-MOUSE-BUTTON-DOWN) (! (. a ui-hovered))
|
|
(== (. event button button) SDL-BUTTON-LEFT))
|
|
(= (. a dragging) true)
|
|
(== type SDL-EVENT-MOUSE-BUTTON-UP)
|
|
(= (. a dragging) 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)
|
|
(return true))
|
|
|
|
(fn save-screenshot ((a (* app))) bool
|
|
(var shot (* SDL-Surface) (SDL-RenderReadPixels (. a renderer) NULL))
|
|
(if (! shot)
|
|
(return false))
|
|
(var rgba (* SDL-Surface) (SDL-ConvertSurface shot SDL-PIXELFORMAT-ABGR8888))
|
|
(SDL-DestroySurface shot)
|
|
(if (! rgba)
|
|
(return false))
|
|
(var ok bool (!= (stbi-write-png (. a screenshot) (. rgba w) (. rgba h) 4 (. rgba pixels)
|
|
(. rgba pitch)) 0))
|
|
(SDL-DestroySurface rgba)
|
|
(return ok))
|
|
|
|
;;; Main -----------------------------------------------------------
|
|
|
|
(pub fn main ((argc int) (argv [* char])) int
|
|
(var a (* app) (calloc 1 (sizeof app)))
|
|
(snprintf (. a assets-dir) (sizeof (. a assets-dir)) "assets")
|
|
(snprintf (. a world-path) (sizeof (. a world-path)) "serverconfig.json")
|
|
(for (var i int 1) (< i argc) (++ i)
|
|
(var more bool (< (+ i 1) argc))
|
|
(if (&& (== (strcmp [argv i] "--assets") 0) more)
|
|
(do
|
|
(++ i)
|
|
(snprintf (. a assets-dir) (sizeof (. a assets-dir)) "%s" [argv i]))
|
|
(&& (== (strcmp [argv i] "--zoom") 0) more)
|
|
(do (++ i) (= (. a zoom) (atof [argv i])))
|
|
(&& (== (strcmp [argv i] "--layer") 0) more)
|
|
(do (++ i) (= (. a layer) (atoi [argv i])))
|
|
(&& (== (strcmp [argv i] "--screenshot") 0) more)
|
|
(do (++ i) (= (. a screenshot) [argv i]))
|
|
(snprintf (. a world-path) (sizeof (. a world-path)) "%s" [argv i])))
|
|
|
|
(if (! (SDL-Init SDL-INIT-VIDEO))
|
|
(return (fail "SDL_Init")))
|
|
(if (! (SDL-CreateWindowAndRenderer "vs-map-viewer" WINDOW-WIDTH WINDOW-HEIGHT
|
|
(| SDL-WINDOW-RESIZABLE SDL-WINDOW-HIGH-PIXEL-DENSITY)
|
|
(& (. a window)) (& (. a renderer))))
|
|
(return (fail "SDL_CreateWindowAndRenderer")))
|
|
(SDL-SetRenderVSync (. a renderer) 1)
|
|
(var scale float (SDL-GetWindowDisplayScale (. a window)))
|
|
(SDL-SetRenderScale (. a renderer) scale scale)
|
|
(= (. a ctx) (nk-sdl-init (. a window) (. a renderer) (nk-sdl-allocator)))
|
|
(setup-font (. a ctx) scale)
|
|
|
|
(noise-init)
|
|
(var source world-source)
|
|
(if (! (world-source/load (. a world-path) (& source)))
|
|
(do
|
|
(= (. source settings) (world-settings/defaults))
|
|
(snprintf (. source seed-text) (sizeof (. source seed-text)) "0")))
|
|
(var workers int (- (SDL-GetNumLogicalCPUCores) 1))
|
|
(= (. a rivers) (river-config/defaults))
|
|
(= (. a tiles) (tiles/start (. source settings) (. a rivers) (. a assets-dir) workers))
|
|
(if (<= (. a zoom) 0)
|
|
(= (. a zoom) 4))
|
|
(if (! (apply a source))
|
|
(do
|
|
(fprintf stderr "%s\n" (. a status))
|
|
(return 1)))
|
|
(go-to-spawn a)
|
|
|
|
(var running bool true)
|
|
(while running
|
|
;; Nothing left to generate: sleep until something happens.
|
|
(if (&& (== (. a pending) 0) (! (. a dragging)) (! (. a screenshot)))
|
|
(SDL-WaitEventTimeout NULL 500))
|
|
(var event SDL-Event)
|
|
(nk-input-begin (. a ctx))
|
|
(while (SDL-PollEvent (& event))
|
|
(if (! (handle-event a (& event)))
|
|
(= running false)))
|
|
(nk-sdl-update-TextInput (. a ctx))
|
|
(nk-input-end (. a ctx))
|
|
|
|
(draw-settings a)
|
|
(draw-layers a)
|
|
(draw-info a)
|
|
(= (. a ui-hovered) (|| (nk-window-is-any-hovered (. a ctx)) (nk-item-is-any-active (. a ctx))))
|
|
|
|
(SDL-SetRenderDrawColor (. a renderer) 24 32 40 255)
|
|
(SDL-RenderClear (. a renderer))
|
|
(draw-map a)
|
|
(draw-markers a)
|
|
(nk-sdl-render (. a ctx) NK-ANTI-ALIASING-ON)
|
|
(if (&& (. a screenshot) (== (. a pending) 0))
|
|
(do
|
|
(if (! (save-screenshot a))
|
|
(fprintf stderr "%s: %s\n" (. a screenshot) (SDL-GetError)))
|
|
(= running false)))
|
|
(SDL-RenderPresent (. a renderer)))
|
|
|
|
(.stop (. a tiles))
|
|
(release-hover a)
|
|
(worldgen/free (. a g))
|
|
(nk-sdl-shutdown (. a ctx))
|
|
(SDL-DestroyRenderer (. a renderer))
|
|
(SDL-DestroyWindow (. a window))
|
|
(SDL-Quit)
|
|
(return 0))
|