about summary refs log tree commit diff stats
path: root/fire.lsp
diff options
context:
space:
mode:
authorDarren Bane <darren.bane@gmail.com>2020-05-11 01:15:06 +0100
committerDarren Bane <darren.bane@gmail.com>2020-05-11 01:15:06 +0100
commitc066ab2a55a069802009568a051673b3505503d4 (patch)
tree3cf0613bb334461c1f39f55a78a4ff79d3fb971f /fire.lsp
parent5eba13b70414e1a40ef2418978082c6e4ac37c19 (diff)
downloadlsp-c066ab2a55a069802009568a051673b3505503d4.tar.gz
Lots of unfinished stuff
Diffstat (limited to 'fire.lsp')
-rwxr-xr-xfire.lsp91
1 files changed, 91 insertions, 0 deletions
diff --git a/fire.lsp b/fire.lsp
new file mode 100755
index 0000000..1ab8205
--- /dev/null
+++ b/fire.lsp
@@ -0,0 +1,91 @@
+#!/usr/bin/env uxlisp -shell
+
+(require "virtty")
+
+(defconstant +width+ 50)
+(defconstant +height+ 25)
+(defconstant +max-y+ (- +height+ 1))
+(defglobal *buf* (create-vector (* +width+ +height+)))
+(defconstant +out+ (standard-output))
+(defconstant +max-fade+ 50)
+(defglobal *avg-frames* 0)
+(defglobal *avg-time* 0.0)
+
+(defconstant +esc+ (convert 27 <character>))
+(defun ctrl (&rest args)
+  (format +out+ "~C[~A" +esc+ args))
+
+(defun home ()
+  (ctrl "H"))
+
+(defun init ()
+  (for ((i 0 (+ i 1)))
+    ((> i +width+))
+    (setf (elt *buf* i) #xff))
+  (tycls))
+
+(defmacro inc (x)
+  `(setq ,x (+ ,x 1)))
+(defmacro dec (x)
+  `(setq ,x (- ,x 1)))
+(defmacro foreach (lim &rest body)
+  (let ((i (gensym)))
+    `(for ((,i 0 (+ ,i 1)))
+       ((> ,i ,lim))
+       ,@body)))
+(defun render ()
+  (let ((t0 (get-internal-real-time))
+         (i -1))
+
+    (foreach +max-y+
+      (for ((x 0 (+ x 1)))
+        ((> x +width+))
+        (let ((v (elt *buf* (inc i)))
+               (j (+ i +width+)))
+
+          (if (and (> x 0) (< x (- +width+ 1)))
+            (inc j (- 1 (random 3))))
+
+          (setf (elt *buf* j)
+            (if (> v 0)
+              (- v (random (min +max-fade+ (+ v 1))))
+              v)))))
+
+    (setq i (+ i width 1))
+    (let ((prev-g nil))
+      (home)
+
+      (for ((y 0 (+ y 1)))
+        ((> y +height+))
+        (for ((x 0 (+ x 1)))
+          ((> x +width+))
+          (let* ((g (elt *buf* (dec i)))
+                  (r (if (> g 0) #xff 0))
+                  (b (if (= g #xff) #xff 0)))
+
+            (if (= g prev-g)
+              (print +out+ " ")
+              (ctrl "48;2;" r ";" (setq prev-g g) ";" b "m "))))
+
+        (print +out+ #\newline)))
+
+    (setq *avg-time* (+ *avg-time* (- (get-internal-real-time) t0)))
+    (inc *avg-frames*)))
+
+(defun restore ()
+  (ctrl "0m")
+  (tycls)
+  (home))
+
+(defun print (arg1 &rest args)
+  (if (null arg)
+    (format (standard-output) "~A~%" arg1)
+    (format arg1 "~A~%" (car args))))
+(defun main ()
+  (init)
+  (for ((i 0 (+ i 1)))
+    ((> i 50))
+    (render))
+  (restore)
+  (print (/ (* (internal-time-units-per-second) *avg-frames*) *avg-time*)))
+(main)