Skip to content

Instantly share code, notes, and snippets.

Created July 16, 2011 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1086896 to your computer and use it in GitHub Desktop.
Save anonymous/1086896 to your computer and use it in GitHub Desktop.
(ns robo-replacement.map-maker)
;;note: deprecating record usage for maps because they are more flexible
;;various record types for what can exist on the map
;; (defrecord Road [])
;; (defrecord Building [health burned database])
;; (defrecord Firefighter [health water database])
(defn make-road
[] {:type "road"})
(defn make-building
[health burned database]
{:type "building" :health health :burned burned :database database})
(defn make-firefighter
[health water database]
{:type "firefighter" :health health :water water :database database})
;;creates base layout 2d vector array full of Road records
(defn generate-world-vec
[y x] (vec (repeat y (vec (repeat x (make-road))))))
;;populates a given road-only-world (generated by generate-world-vec)
;;with Buildings with probability prob [0-1.0] or .5 by default
(defn add-buildings
([world]
(add-buildings world 0.5 (count world) (count (world 0)) (count (world 0)) (count world)))
([world prob]
(add-buildings world prob (count world) (count (world 0)) (count (world 0)) (count world)))
([world prob maxy maxx cury curx]
(if (and (zero? curx) (zero? cury))
world
(if (zero? curx)
(recur (update-in world [cury curx]
(fn [arg]
(println (str "y:" cury ",x:" curx " -- arg type: " (type arg)))
(if (> (rand) prob) (make-building 1.0 0.0 {}) arg)))
prob maxy maxx (dec cury) maxx)
(recur (update-in world [curx cury]
(fn [arg]
(println (str "y:" cury ",x:" curx " -- arg type: " (type arg)))
(if (> (rand) prob) (make-building 1.0 0.0 {}) arg)))
prob maxy maxx cury (dec curx))))))
;;prings the string vectors generated by pretty-print-world
(defn pretty-print-world-vec
[pretty-print-world]
;; (if (first pretty-print-world))
;; (println (first pretty-print-world))
(if (not (empty? pretty-print-world))
(recur (rest pretty-print-world))))
;;returns the string representation of a given record in the world vector
(defn world-obj-to-str
[world-obj]
(println "Checking type: " (:type world-obj))
(cond
;; (instance? Building rec-to-check) "B"
;; (instance? Road rec-to-check) "r"
;; (instance? Firefighter rec-to-check) "F")
(= "building" (:type world-obj)) "B"
(= "road" (:type world-obj)) "r"
(= "firefighter" (:type world-obj)) "F"))
;;handles trying to nicely print the existing world
(defn pretty-print-world
([world]
(println world)
(pretty-print-world [] [] (rest world) (count (world 0)) (count world)
(count (world 0)) (count world)))
([world-vec-str world-temp world maxx maxy curx cury]
(println "i:" (- (+ maxx maxy) (+ curx cury)))
(pretty-print-world-vec world-vec-str)
(if (and (zero? curx) (zero? cury))
world-vec-str
(let [add-string (str (world-obj-to-str (get-in world [cury curx])))
world-temp-update (conj world-temp add-string)]
(println "@y:" cury "@x:" curx " = " (get-in world [cury curx]))
(println "@y:" cury "@x:" curx " = " (world-obj-to-str (get-in world [cury curx])))
(if (zero? curx)
(recur (conj world-vec-str world-temp-update) [] world maxx maxy maxx (dec cury))
(recur world-vec-str world-temp-update world maxx maxy (dec curx) cury))))))
;;entry point function for creating a world
(defn create-world-vec
([] (create-world-vec 10 10 0.5 true))
([y x prob debg]
(pretty-print-world (generate-world-vec y x))
(if (not debg)
(add-buildings (generate-world-vec y x) prob)
(pretty-print-world (add-buildings (generate-world-vec y x) prob)))))
;;tester1
;;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;;this doesn't work at all
;;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;;should produce a vector like
;;[["r" "r" "r"] ["r" "r" "r"] ["r" "r" "r"]]
(defn tester1 [] (pretty-print-world (generate-world-vec 3 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment