b08b5fd0f3e8 entry-03-2

moar
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 08 Jul 2012 20:43:22 -0400
parents aacbf1ac74fc
children 53ae2d22f818
branches/tags entry-03-2
files src/caves/core.clj src/caves/world.clj

Changes

--- a/src/caves/core.clj	Sun Jul 08 17:19:39 2012 -0400
+++ b/src/caves/core.clj	Sun Jul 08 20:43:22 2012 -0400
@@ -1,5 +1,5 @@
 (ns caves.core
-  (:use [caves.world :only [random-world]])
+  (:use [caves.world :only [random-world smooth-world]])
   (:require [lanterna.screen :as s]))
 
 
@@ -75,6 +75,7 @@
   (case input
     :enter     (assoc game :uis [(new UI :win)])
     :backspace (assoc game :uis [(new UI :lose)])
+    \s         (assoc game :world (smooth-world (:world game)))
     game))
 
 (defmethod process-input :win [game input]
--- a/src/caves/world.clj	Sun Jul 08 17:19:39 2012 -0400
+++ b/src/caves/world.clj	Sun Jul 08 20:43:22 2012 -0400
@@ -1,8 +1,10 @@
 (ns caves.world)
 
 
+; Constants -------------------------------------------------------------------
 (def world-size [160 50])
 
+; Data structures -------------------------------------------------------------
 (defrecord World [tiles])
 (defrecord Tile [kind glyph color])
 
@@ -14,6 +16,16 @@
 (defn get-tile [tiles x y]
   (get-in tiles [y x] (:bound tiles)))
 
+
+; Debugging -------------------------------------------------------------------
+(defn print-row [row]
+  (println (apply str (map :glyph row))))
+
+(defn print-world [world]
+  (dorun (map print-row (:tiles world))))
+
+
+; World generation ------------------------------------------------------------
 (defn random-tiles []
   (let [[cols rows] world-size]
     (letfn [(random-tile []
@@ -22,5 +34,42 @@
               (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 (fn [[x y]]
+         (get-tile tiles x y))
+       (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 []
-  (new World (random-tiles)))
+  (let [world (new World (random-tiles))
+        world (nth (iterate smooth-world world) 0)]
+    world))
+