diff --git a/resources/public/docs/mw-engine/uberdoc.html b/resources/public/docs/mw-engine/uberdoc.html index 18e674e..c175a03 100644 --- a/resources/public/docs/mw-engine/uberdoc.html +++ b/resources/public/docs/mw-engine/uberdoc.html @@ -3032,8 +3032,8 @@ net.brehaut.ClojureTools = (function (SH) {
org.clojure/clojure | 1.5.1 | |
org.clojure/math.combinatorics | 0.0.7 | |
org.clojure/tools.trace | 0.7.8 | |
net.mikera/imagez | 0.3.1 | |
fivetonine/collage | 0.2.1 |
Functions to transform a world and run rules.
(ns mw-engine.core - (:require [mw-engine.world :as world] - mw-engine.utils))
Every rule is a function of two arguments, a cell and a world. If the rule + (:use mw-engine.utils) + (:require [mw-engine.world :as world]))
Every rule is a function of two arguments, a cell and a world. If the rule fires, it returns a new cell, which should have the same values for :x and :y as the old cell. Anything else can be modified.
@@ -3053,13 +3053,14 @@ Seeworld.clj
.
Each time the world is transformed (see transform-world
, for each cell,
rules are applied in turn until one matches. Once one rule has matched no
further rules can be applied.
Apply a single rule to a cell. What this is about is that I want to be able, +
Apply a single rule
to a cell
. What this is about is that I want to be able,
for debugging purposes, to tag a cell with the rule text of the rule which
fired (and especially so when an exception is thrown. So a rule may be either
an ifn, or a list (ifn source-text). This function deals with despatching
- on those two possibilities.
world
is also passed in in order to be able
+ to access neighbours.
(defn apply-rule - ([cell world rule] + ([world cell rule] (cond (ifn? rule) (apply-rule cell world rule nil) (seq? rule) (let [[afn src] rule] (apply-rule cell world afn src)))) @@ -3067,35 +3068,30 @@ further rules can be applied. (let [result (apply rule (list cell world))] (cond (and result source) (merge result {:rule source}) - true result))))
Derive a cell from this cell of this world by applying these rules.
+ true result))))Derive a cell from this cell
of this world
by applying these rules
.
(defn- apply-rules - [cell world rules] + [world cell rules] (cond (empty? rules) cell - true (let [result (apply-rule cell world (first rules))] + true (let [result (apply-rule world cell (first rules))] (cond result result - true (apply-rules cell world (rest rules))))))
Derive a cell from this cell of this world by applying these rules. If an - exception is thrown, cache its message on the cell and set state to error
+ true (apply-rules world cell (rest rules))))))Derive a cell from this cell
of this world
by applying these rules
. If an
+ exception is thrown, cache its message on the cell and set it's state to error
(defn- transform-cell - [cell world rules] + [world cell rules] (try (merge - (apply-rules cell world rules) + (apply-rules world cell rules) {:generation (+ (or (:generation cell) 0) 1)}) (catch Exception e (merge cell {:error (format "%s at generation %d when in state %s" (.getMessage e) (:generation cell) - (:state cell))}))))
Return a row derived from this row of this world by applying these rules to each cell.
-(defn- transform-world-row - [row world rules] - (apply vector (map #(transform-cell % world rules) row)))
Return a world derived from this world by applying these rules to each cell.
+ (:state cell)) + :state :error}))))Return a world derived from this world
by applying these rules
to each cell.
(defn transform-world [world rules] - (apply vector - (map - #(transform-world-row % world rules) - world)))
Consider this single argument as a map of :world
and :rules
; apply the rules
+ (map-world world transform-cell (list rules)))
Consider this single argument as a map of :world
and :rules
; apply the rules
to transform the world, and return a map of the new, transformed :world
and
these :rules
. As a side effect, print the world.
(defn- transform-world-state @@ -3139,11 +3135,14 @@ ignored). Darker shades are higher. (cond (< n 0) (- 0 n) true n))
Set the gradient
property of this cell
of this world
to the difference in
altitude between its highest and lowest neghbours.
(defn tag-gradient - [cell world] - (let [heights (map '(:altitude %) (get-neighbours world cell)) - highest (apply max heights) - lowest (apply min heights)] - #(merge cell {:gradient (- highest lowest)})))
Set the gradient
property of each cell in this world
to the difference in
+ [world cell]
+ (let [heights (remove nil? (map #(:altitude %) (get-neighbours world cell)))
+ highest (cond (empty? heights) 0 ;; shouldn't happen
+ true (apply max heights))
+ lowest (cond (empty? heights) 0 ;; shouldn't
+ true (apply min heights))
+ gradient (- highest lowest)]
+ (merge cell {:gradient gradient})))
Set the gradient
property of each cell in this world
to the difference in
altitude between its highest and lowest neghbours.
(defn tag-gradients [world] @@ -3169,19 +3168,7 @@ ignored). Darker shades are higher. (mod (.getRGB heightmap (get-int cell :x) - (get-int cell :y)) 256))))})))
Set the altitude of each cell in this sequence from the corresponding pixel - of this heightmap. - If the heightmap you supply is smaller than the world, this will break.
- -row
a row in a world, as discussed in world.clj, q.v. Alternatively, a
- sequence of maps;heightmap
an (ideally) greyscale image, whose x and y dimensions should
- exceed those of the world of which the cell
forms part.(defn- apply-heightmap-row - [row heightmap] - (apply vector (map #(transform-altitude % heightmap) row)))
Apply the image file loaded from this path to this world, and return a world whose + (get-int cell :y)) 256))))})))
Apply the image file loaded from this path to this world, and return a world whose altitudes are modified (added to) by the altitudes in the heightmap. It is assumed that the heightmap is at least as large in x and y dimensions as the world.
@@ -3194,14 +3181,14 @@ a world the size of the heightmap will be created. ([world imagepath] ;; bizarrely, the collage load-util is working for me, but the imagez version isn't. (let [heightmap (filter-image (grayscale)(load-image imagepath))] - (map-world + (map-world (map-world world transform-altitude (list heightmap)) tag-gradient))) ([imagepath] (let [heightmap (filter-image (grayscale)(load-image imagepath)) world (make-world (.getWidth heightmap) (.getHeight heightmap))] - (map-world - (map-world world transform-altitude (list heightmap)) + (map-world + (map-world world transform-altitude (list heightmap)) tag-gradient))))A set of MicroWorld rules describing a simplified natural ecosystem.
Since the completion of the rule language this is more or less obsolete - @@ -3359,7 +3346,7 @@ important.
(defn in-bounds [world x y] (and (>= x 0)(>= y 0)(< y (count world))(< x (count (first world)))))
Apply this function
to each cell in this world
to produce a new world.
- the arguments to the function will be the cell, the world, and any
+ the arguments to the function will be the world, the cell, and any
additional-args
supplied
(defn map-world ([world function] diff --git a/resources/public/docs/mw-ui/uberdoc.html b/resources/public/docs/mw-ui/uberdoc.html index ca9d459..c684566 100644 --- a/resources/public/docs/mw-ui/uberdoc.html +++ b/resources/public/docs/mw-ui/uberdoc.html @@ -3146,7 +3146,7 @@ net.brehaut.ClojureTools = (function (SH) { [:img {:alt (:state cell) :src (format-image-path state)}]]]))
Render this world row as a Hiccup table row.
(defn render-world-row [row] - (apply vector (cons :tr (map render-cell row))))
Render the world implied by the session as a complete HTML page.
+ (apply vector (cons :tr (map render-cell row))))Render the world implied by the current session as a complete HTML table in a DIV.
(defn render-world-table [] (let [world (or (session/get :world) @@ -3169,7 +3169,8 @@ net.brehaut.ClojureTools = (function (SH) { (apply vector (cons :table (map render-world-row w2))) - [:p (str "Generation " generation)]]))
(defn render-inspector [cell table] [:table {:class "music-ruled"} diff --git a/resources/public/rulesets/settlement.txt b/resources/public/rulesets/settlement.txt index 42d348a..259d9d4 100644 --- a/resources/public/rulesets/settlement.txt +++ b/resources/public/rulesets/settlement.txt @@ -34,8 +34,9 @@ if state is pasture and fertility is more than 10 and altitude is less than 100 if state is ploughland then state should be crop -;; after the crop is harvested, the land is allowed to lie fallow -if state is crop then state should be grassland +;; after the crop is harvested, the land is allowed to lie fallow. But cropping +;; depletes fertility. +if state is crop then state should be grassland and fertility should be fertility - 1 ;; if there's reliable food available, nomads build permanent settlements if state is in camp or abandoned and some neighbours are crop then state should be house @@ -125,4 +126,6 @@ if state is new and altitude is less than 10 then state should be water if state is new and altitude is more than 200 then state should be snow ;; otherwise, we have grassland. -if state is new then state should be grassland \ No newline at end of file +if state is new then state should be grassland + + \ No newline at end of file diff --git a/src/mw_ui/render_world.clj b/src/mw_ui/render_world.clj index b5b8faf..1e5ca9e 100644 --- a/src/mw_ui/render_world.clj +++ b/src/mw_ui/render_world.clj @@ -37,7 +37,7 @@ (apply vector (cons :tr (map render-cell row)))) (defn render-world-table - "Render the world implied by the session as a complete HTML page." + "Render the world implied by the current session as a complete HTML table in a DIV." [] (let [world (or (session/get :world) (engine/transform-world @@ -57,11 +57,11 @@ (session/put! :world w2) (session/put! :generation generation) [:div {:class "world"} - (apply vector (cons :table (map render-world-row w2))) - [:p (str "Generation " generation)]])) + [:p + (str "Generation " generation)]])) (defn render-inspector [cell table]