about summary refs log tree commit diff stats
path: root/fire.lsp
blob: 1ab8205121b4f1432278c3b8bf6f561db102c38f (plain) (blame)
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
50
51
52
53
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .g
#!/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)