From 0c8a8cf8d861bc4ef3162e45e8b58d2f0173d2f7 Mon Sep 17 00:00:00 2001 From: equa Date: Sun, 18 Apr 2021 09:57:30 -0500 Subject: nanpa wan commit of the last two days: working cellular automata and rendering, prototype system, etc --- lib/cells.fnl | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 lib/cells.fnl (limited to 'lib/cells.fnl') diff --git a/lib/cells.fnl b/lib/cells.fnl new file mode 100644 index 0000000..442c7c7 --- /dev/null +++ b/lib/cells.fnl @@ -0,0 +1,67 @@ +(local cell (require :lib.cell)) + +(local neighbors [{:x -1 :y -1} + {:x -1 :y 0} + {:x -1 :y 1} + {:x 0 :y -1} + {:x 0 :y 1} + {:x 1 :y -1} + {:x 1 :y 0} + {:x 1 :y 1}]) + +(fn neighbors> [f threshold] + (var x 0) + ;; nnn this could be faster maybe + (each [k v (ipairs neighbors)] + (when (> (cell.aliveness (f v)) threshold) + (set x (+ x 1)))) + x) + +(local life + {cell.init + (fn [self] + (setmetatable {} self)) + cell.birth + (fn [self get] + (if (= (neighbors> get 0) 3) + self + nil)) + cell.update + (fn [self get] + (if (or (= (neighbors> get 0) 3) + (= (neighbors> get 0) 2)) + self + nil)) + cell.aliveness + #1 + cell.color + #[0.4 0.4 0.7] + }) + +(local brain + {cell.init + (fn [self] + (setmetatable {:stage 0} self)) + cell.birth + (fn [self get] + (if (= (neighbors> get 0.8) 2) + (do + (setmetatable {:stage 0} (getmetatable self))) + nil)) + cell.update + (fn [self get] + (if (= self.stage 0) + (setmetatable {:stage 1} (getmetatable self)) + nil)) + cell.aliveness + #(- 1 (* 0.5 $.stage)) + cell.color + #(if (= $.stage 0) + [0.7 0.4 0.3] + (= $.stage 5) + [0.5 0.4 0.3] + (= $.stage 1) + [0.2 0.2 0.3]) + }) + +{: life : brain} -- cgit 1.3.0-6-gf8a5