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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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)