src/caves/world/generation.clj @ 1a3a4f8d5d85
Refactor the world code into separate files. This pulls out the world generation code into its own file. It was getting a bit crowded in there.
| author | Steve Losh <steve@stevelosh.com> |
|---|---|
| date | Wed, 01 Aug 2012 21:13:37 -0400 |
| parents | (none) |
| children | f15b8173a124 |
(ns caves.world.generation (:use [caves.world.core :only [tiles get-tile-from-tiles random-coordinate world-size ->World]])) (defn random-tiles [] (let [[cols rows] world-size] (letfn [(random-tile [] (tiles (rand-nth [:floor :wall]))) (random-row [] (vec (repeatedly cols random-tile)))] (vec (repeatedly rows random-row))))) (defn get-smoothed-tile [block] (let [tile-counts (frequencies (map :kind block)) floor-threshold 5 floor-count (get tile-counts :floor 0) result (if (>= floor-count floor-threshold) :floor :wall)] (tiles result))) (defn block-coords [x y] (for [dx [-1 0 1] dy [-1 0 1]] [(+ x dx) (+ y dy)])) (defn get-block [tiles x y] (map (partial get-tile-from-tiles tiles) (block-coords x y))) (defn get-smoothed-row [tiles y] (mapv (fn [x] (get-smoothed-tile (get-block tiles x y))) (range (count (first tiles))))) (defn get-smoothed-tiles [tiles] (mapv (fn [y] (get-smoothed-row tiles y)) (range (count tiles)))) (defn smooth-world [{:keys [tiles] :as world}] (assoc world :tiles (get-smoothed-tiles tiles))) (defn random-world [] (let [world (->World (random-tiles) {}) world (nth (iterate smooth-world world) 3)] world))