summaryrefslogtreecommitdiff
path: root/src/http/static/viz/2/viz/ghost.cljs
blob: d468479c9f61f162c8534ebada012f0b1ee78613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(ns viz.ghost
  (:require [quil.core :as q]
            [quil.middleware :as m]
            [viz.forest :as forest]
            [viz.grid   :as grid]
            clojure.set))

(defn new-ghost []
  {:active-node-ids #{}
   :color 0xFF000000
   })

(defn add-active-node [ghost id]
  (update-in ghost [:active-node-ids] conj id))

(defn rm-active-node [ghost id]
  (update-in ghost [:active-node-ids] disj id))

(defn- gen-new-poss [forest poss-fn id]
  "generates new positions branching from the given node"
  (let [pos (:pos (forest/get-node forest id))
        adj-poss (forest/empty-adjacent-points forest pos)
        new-poss (poss-fn pos adj-poss)]
    new-poss))

(defn- spawn-children [forest poss-fn id]
  (reduce (fn [[forest new-ids] pos]
            (let [[forest new-id] (forest/spawn-child forest id pos)]
              [forest (conj new-ids new-id)]))
          [forest #{}]
          (gen-new-poss forest poss-fn id)))

(defn- spawn-children-multi [forest poss-fn ids]
  (reduce (fn [[forest new-ids] id]
            (let [[forest this-new-ids] (spawn-children forest poss-fn id)]
              [forest (clojure.set/union new-ids this-new-ids)]))
          [forest #{}]
          ids))

(defn incr [ghost forest poss-fn]
  (let [[forest new-ids] (spawn-children-multi forest poss-fn (:active-node-ids ghost))]
    [(assoc ghost :active-node-ids new-ids)
     (reduce (fn [forest id]
               (forest/update-node-meta forest id
                  (fn [m] (assoc m :color (:color ghost)))))
             forest new-ids)]))

(defn- eg-poss-fn [pos adj-poss]
  (take 2 (random-sample 0.6 adj-poss)))