Created
May 9, 2020 04:38
-
-
Save markx/cd7990aeaef3c6087ed5116d4006f703 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def TILES | |
{:floor {:type :floor | |
:blocking false | |
:opaque false | |
:glyph "."} | |
:wall {:type :wall | |
:blocking true | |
:opaque true | |
:glyph "#"} | |
:bound {:type :bound | |
:blocking true | |
:opaque true | |
:glyph "X"}}) | |
(defn tiles-indexed [world] | |
(vec | |
(for [[row tile-row] (map-indexed list world)] | |
(vec (map-indexed | |
(fn [col tile] | |
(assoc tile :pos [col row])) | |
tile-row))))) | |
(defn generate-map [width height] | |
(letfn [(random-tile [] | |
(TILES (rand-nth [:floor :wall]))) | |
(random-row [] | |
(vec (repeatedly width random-tile)))] | |
(vec (tiles-indexed (repeatedly height random-row))))) | |
(defn- distance [[x1 y1] [x2 y2]] | |
(Math/sqrt (+ | |
(Math/pow (- x1 x2) 2) | |
(Math/pow (- y1 y2) 2)))) | |
(defn- is-around [a b] | |
(< (distance (:pos a) (:pos b)) 2)) | |
(defn- around-blocks [t tiles] | |
(filter #(is-around t %) tiles)) | |
(defn smooth-tile [t tiles] | |
(let [block (around-blocks t tiles) | |
walls (filter #(= :wall (:type %)) block)] | |
(cond | |
(>= (count walls) 5) (into t (TILES :wall)) | |
:else (into t (TILES :floor))))) | |
(defn smooth-world [world] | |
(let [tiles (flatten world)] | |
(for [row world] | |
(vec (map #(smooth-tile % tiles) row))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment