summary refs log tree commit diff
path: root/lib/cells.fnl
diff options
context:
space:
mode:
authorequa <equaa@protonmail.com>2021-04-18 09:57:30 -0500
committerequa <equaa@protonmail.com>2021-04-18 09:57:30 -0500
commit0c8a8cf8d861bc4ef3162e45e8b58d2f0173d2f7 (patch)
tree5fe4e826b92173f33abf8a1f2391ccab706db230 /lib/cells.fnl
nanpa wan
commit of the last two days: working cellular automata and rendering,
prototype system, etc
Diffstat (limited to 'lib/cells.fnl')
-rw-r--r--lib/cells.fnl67
1 files changed, 67 insertions, 0 deletions
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}