 (defn- debug [& args]
   (.log js/console (clojure.string/join " " (map str args))))
 
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+(defn- positive [n] (if (> 0 n) (- n) n))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; initialization
 
 (defn- window-partial [k]
   (int (* (aget js/document "documentElement" k) 0.95)))

    ;            (gstring/format "spawn-chance:%d" (spawn-chance state))))
    ;        30 30)
  )

(q/defsketch viz
  :title ""

                  (float (/ (:grid-width state) (window-size 0)))))]
             [(:grid-width state) h]))

(defn- scale [state xy]
  (map-indexed #(* %2 (float (/ (window-half-size %1)
                                ((grid-size state) %1)))) xy))

(def bounds-buffer 1)

(defn- in-bounds? [state pos]
  (let [[w h] (apply vector (map #(- % bounds-buffer) (grid-size state)))
        min-bound [(- w) (- h)]
        max-bound [w h]
        pos-k (keep-indexed #(let [mini (min-bound %1)
                                   maxi (max-bound %1)]
                               (when (and (>= %2 mini) (<= %2 maxi)) %2)) pos)]
    (= (count pos) (count pos-k))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; dials

(def dial-period-seconds 2)
(def dial-offset-seconds 0.5)
(def dial-width-frames 1)

(defn- set-dial [state]
  (let [period-frames (* (:frame-rate state) dial-period-seconds)
        frame-in-second (rem (:frame state) (int period-frames))
        offset-frame (int (* (:frame-rate state) dial-offset-seconds))]
    (assoc state :dial
           (if (or (and (>= frame-in-second 0) (< frame-in-second dial-width-frames))
                   (and (>= frame-in-second offset-frame) (< frame-in-second (+ dial-width-frames offset-frame))))
             1
             0))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; poss-fn

  ;(let [period-seconds 1
  ;      rad-per-second (float (/ (/ Math/PI 2) period-seconds))
