More work on drainage and rendering.

This commit is contained in:
Simon Brooke 2024-04-06 23:37:55 +01:00
parent c739dd7094
commit 716eb9442e
3 changed files with 74 additions and 12 deletions

View file

@ -0,0 +1,14 @@
<!DOCTYPE html PUBLIC ""
"">
<html><head><meta charset="UTF-8" /><title>mw-engine.render documentation</title><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="css/highlight.css" /><script type="text/javascript" src="js/highlight.min.js"></script><script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/page_effects.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><div id="header"><h2>Generated by <a href="https://github.com/weavejester/codox">Codox</a></h2><h1><a href="index.html"><span class="project-title"><span class="project-name">Mw-engine</span> <span class="project-version">0.3.0-SNAPSHOT</span></span></a></h1></div><div class="sidebar primary"><h3 class="no-link"><span class="inner">Project</span></h3><ul class="index-link"><li class="depth-1"><a href="index.html"><div class="inner">Index</div></a></li></ul><h3 class="no-link"><span class="inner">Topics</span></h3><ul><li class="depth-1"><a href="intro.html"><div class="inner"><span>Introduction to mw-engine</span></div></a></li></ul><h3 class="no-link"><span class="inner">Namespaces</span></h3><ul><li class="depth-1"><div class="no-link"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>mw-engine</span></div></div></li><li class="depth-2 branch"><a href="mw-engine.core.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>core</span></div></a></li><li class="depth-2 branch"><a href="mw-engine.display.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>display</span></div></a></li><li class="depth-2 branch"><a href="mw-engine.drainage.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>drainage</span></div></a></li><li class="depth-2 branch"><a href="mw-engine.flow.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>flow</span></div></a></li><li class="depth-2 branch"><a href="mw-engine.heightmap.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>heightmap</span></div></a></li><li class="depth-2 branch"><a href="mw-engine.natural-rules.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>natural-rules</span></div></a></li><li class="depth-2 branch current"><a href="mw-engine.render.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>render</span></div></a></li><li class="depth-2 branch"><a href="mw-engine.utils.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>utils</span></div></a></li><li class="depth-2"><a href="mw-engine.world.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>world</span></div></a></li></ul></div><div class="sidebar secondary"><h3><a href="#top"><span class="inner">Public Vars</span></a></h3><ul><li class="depth-1"><a href="mw-engine.render.html#var-*state-images-relative-path*"><div class="inner"><span>*state-images-relative-path*</span></div></a></li><li class="depth-1"><a href="mw-engine.render.html#var-format-css-class"><div class="inner"><span>format-css-class</span></div></a></li><li class="depth-1"><a href="mw-engine.render.html#var-format-image-path"><div class="inner"><span>format-image-path</span></div></a></li><li class="depth-1"><a href="mw-engine.render.html#var-format-mouseover"><div class="inner"><span>format-mouseover</span></div></a></li><li class="depth-1"><a href="mw-engine.render.html#var-render-cell"><div class="inner"><span>render-cell</span></div></a></li><li class="depth-1"><a href="mw-engine.render.html#var-render-world-page"><div class="inner"><span>render-world-page</span></div></a></li><li class="depth-1"><a href="mw-engine.render.html#var-render-world-row"><div class="inner"><span>render-world-row</span></div></a></li><li class="depth-1"><a href="mw-engine.render.html#var-render-world-table"><div class="inner"><span>render-world-table</span></div></a></li><li class="depth-1"><a href="mw-engine.render.html#var-world-.3Ehtml-file"><div class="inner"><span>world-&gt;html-file</span></div></a></li></ul></div><div class="namespace-docs" id="content"><h1 class="anchor" id="top">mw-engine.render</h1><div class="doc"><div class="markdown"><p>Render a world as HTML.</p>
<p>Adapted (simplified) from mw-ui.render-world; this is for visualisation, not interaction.</p>
</div></div><div class="public anchor" id="var-*state-images-relative-path*"><h3>*state-images-relative-path*</h3><h4 class="dynamic">dynamic</h4><div class="usage"></div><div class="doc"><div class="markdown"><p><strong>TODO</strong>: write docs</p>
</div></div><div class="src-link"><a href="https://github.com/simon-brooke/mw-engine/blob/master/src/cljc/mw_engine/render.clj#L32">view source</a></div></div><div class="public anchor" id="var-format-css-class"><h3>format-css-class</h3><div class="usage"><code>(format-css-class statekey)</code></div><div class="doc"><div class="markdown"><p>Format this statekey, assumed to be a keyword indicating a state in the world, into a CSS class</p>
</div></div><div class="src-link"><a href="https://github.com/simon-brooke/mw-engine/blob/master/src/cljc/mw_engine/render.clj#L34">view source</a></div></div><div class="public anchor" id="var-format-image-path"><h3>format-image-path</h3><div class="usage"><code>(format-image-path statekey)</code></div><div class="doc"><div class="markdown"><p>Render this statekey, assumed to be a keyword indicating a state in the world, into a path which should recover the corresponding image file.</p>
</div></div><div class="src-link"><a href="https://github.com/simon-brooke/mw-engine/blob/master/src/cljc/mw_engine/render.clj#L40">view source</a></div></div><div class="public anchor" id="var-format-mouseover"><h3>format-mouseover</h3><div class="usage"><code>(format-mouseover cell)</code></div><div class="doc"><div class="markdown"><p><strong>TODO</strong>: write docs</p>
</div></div><div class="src-link"><a href="https://github.com/simon-brooke/mw-engine/blob/master/src/cljc/mw_engine/render.clj#L46">view source</a></div></div><div class="public anchor" id="var-render-cell"><h3>render-cell</h3><div class="usage"><code>(render-cell cell)</code></div><div class="doc"><div class="markdown"><p>Render this world cell as a Hiccup table cell.</p>
</div></div><div class="src-link"><a href="https://github.com/simon-brooke/mw-engine/blob/master/src/cljc/mw_engine/render.clj#L49">view source</a></div></div><div class="public anchor" id="var-render-world-page"><h3>render-world-page</h3><div class="usage"><code>(render-world-page world)</code><code>(render-world-page world state-images-relative-path)</code></div><div class="doc"><div class="markdown"><p><strong>TODO</strong>: write docs</p>
</div></div><div class="src-link"><a href="https://github.com/simon-brooke/mw-engine/blob/master/src/cljc/mw_engine/render.clj#L77">view source</a></div></div><div class="public anchor" id="var-render-world-row"><h3>render-world-row</h3><div class="usage"><code>(render-world-row row)</code></div><div class="doc"><div class="markdown"><p>Render this world row as a Hiccup table row.</p>
</div></div><div class="src-link"><a href="https://github.com/simon-brooke/mw-engine/blob/master/src/cljc/mw_engine/render.clj#L58">view source</a></div></div><div class="public anchor" id="var-render-world-table"><h3>render-world-table</h3><div class="usage"><code>(render-world-table world)</code><code>(render-world-table world state-images-relative-path)</code></div><div class="doc"><div class="markdown"><p>Render this <code>world</code> as a complete HTML table in a DIV. If <code>state-images-relative-path</code> is passed, use that to override the default path.</p>
</div></div><div class="src-link"><a href="https://github.com/simon-brooke/mw-engine/blob/master/src/cljc/mw_engine/render.clj#L63">view source</a></div></div><div class="public anchor" id="var-world-.3Ehtml-file"><h3>world-&gt;html-file</h3><div class="usage"><code>(world-&gt;html-file world output-path)</code><code>(world-&gt;html-file world output-path state-images-relative-path)</code></div><div class="doc"><div class="markdown"><p><strong>TODO</strong>: write docs</p>
</div></div><div class="src-link"><a href="https://github.com/simon-brooke/mw-engine/blob/master/src/cljc/mw_engine/render.clj#L93">view source</a></div></div></div></body></html>

View file

@ -3,7 +3,9 @@
from a heightmap."
:author "Simon Brooke"}
mw-engine.drainage
(:require [mw-engine.core :refer [run-world]]
(:require [clojure.string :refer [replace]]
[hiccup2.core :refer [html]]
[mw-engine.core :refer [run-world]]
[mw-engine.heightmap :refer [apply-heightmap]]
[mw-engine.utils :refer [get-int-or-zero get-least-cell get-neighbours
get-neighbours-with-property-value
@ -221,11 +223,15 @@
:else (throw (ex-info "Invalid cell?"
{:cell cell}))))
(defn identify-lake
[world cell]
(or (is-lake? world cell) cell))
(defn find-lakes
"Identify cells in this `world` which are lakes."
[world]
(info "find-lakes started.")
(let [w' (map-world world is-lake?)]
(let [w' (map-world world identify-lake)]
(info "find-lakes completed.")
w'))
@ -235,4 +241,48 @@
[hmap]
(find-lakes (flow-world-nr (rain-world (flood-hollows (apply-heightmap hmap))))))
;; (run-drainage "resources/heightmaps/20x20/crucible.png")
(defn visualise-drainage
[world html-file]
(let [mxf (apply max (map :flow (flatten world)))
scf (/ 128 mxf)
mxa (apply max (map :altitude (flatten world)))
sca (/ 128 mxa)]
(spit
html-file
(replace
(str
(html [:html
[:head
[:title "Drainage visualisation"]
[:style "table, table tr td {
padding: 0.5em;
margin: 0.2em;
width: 2em;
height: 2em;
border-collapse: collapse;
border: none;}"]]
[:body
(into [:table]
(map
#(into [:tr]
(map
(fn [c]
(let [g (- 255 (int (* sca (:altitude c))))]
[:td {:style (if (> (:altitude c) 1)
(let [blue (int (* scf (or (:flow c) 0)))
o (- g blue)]
(format "background-color: rgb(%d, %d, %d)"
o
o
(+ g blue)))
"background-color: cornflower-blue")
:title (format "state %s, x %d, y %d, rainfall %d, flow %d"
(:state c) (:x c) (:y c) (:rainfall c) (:flow c))}
(or (:rainfall c) "&nbsp;")]))
%))
world))]]))
"&amp;"
"&"))))
(visualise-drainage (run-drainage "resources/heightmaps/20x20/crucible.png") "test.html")

View file

@ -5,7 +5,7 @@
interaction."
;; TODO: but possibly it would be better if there is to be a newer version of
;; mw-ui, to base it on this.
(:require [hiccup2.core :refer [html]])
(:require [hiccup2.page :refer [xhtml]])
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -50,24 +50,21 @@
"Render this world cell as a Hiccup table cell."
[cell]
(let [state (:state cell)]
[:td {:class (format-css-class state) :title (format-mouseover cell)}
[:td {:class (format-css-class state) :title (format-mouseover cell)}
[:img {:alt (:state cell) :src (format-image-path state)}]]))
(defn render-world-row
"Render this world row as a Hiccup table row."
[row]
(apply vector (cons :tr (map render-cell row))))
(into [:tr] (map render-cell row)))
(defn render-world-table
"Render this `world` as a complete HTML table in a DIV. If
`state-images-relative-path` is passed, use that to override the default path."
([world]
[:div {:class "world"}
(apply vector
(cons :table
(map render-world-row world)))
(into [:table] (map render-world-row world))
[:p
(str "Generation " (:generation (first (flatten world))))]])
([world state-images-relative-path]
@ -92,7 +89,8 @@
(defn world->html-file
([world output-path]
(spit output-path (str (html (render-world-page world)))))
(spit output-path (str (xhtml (render-world-page world)))))
([world output-path state-images-relative-path]
(binding [*state-images-relative-path* state-images-relative-path]
(world->html-file world output-path))))
(world->html-file world output-path))))