#!/usr/local/bin/sbcl --script
(load "snek/src/load")

(setf *random-state* (make-random-state t))

(defvar *file* "shinu.png")
(defvar *size* (+ 500 (random 2000)))

(defmacro rand () `(random 1.0))

(defun rand-rgba (opacity)
  (list (rand) (rand) (rand) opacity))

(defun randomized-linspace (begin end stp)
  (let ((rnd (ceiling (* 0.1 stp))))
    (map 'list
         (lambda (x) (+ x (- (random (* 2 rnd)) rnd)))
         (linspace begin end stp))))

(defun main ()
  (let* ((mid (* *size* .5))
         (repeat (random 10))
         (offset (random 100))
         (grains (random 10))
         (itt    (random 2000))
         (bg     (rand-rgba 1.0))
         (active (rand-rgba 0.6))
         (sand   (sandpaint* *size* :active active :bg bg)))
    (loop for i in (randomized-linspace 100 (- *size* 100) repeat)
          for j from offset to (+ offset repeat) do
      (let ((snk (snek*))
            (va (list 0 0))
            (vb (list 0 0))
            (p1 (list (random 100) i))
            (p2 (list (random (- *size* 100)) i)))

        (loop for k from 1 to itt do
          (let ((v1 (insert-vert (l-on-line k itt p1 p2) into snk))
                (v2 (insert-vert (ladd va (l-on-line k itt p1 p2)) into snk)))

            (setf va (ladd va (l-rand-in-circle (* 0.7 j))))
            (setf vb (ladd vb (l-rand-in-circle (* 0.001 j))))

            (with-snek (snk)
              (with-all-verts (snk v)
                (move-vert v (ladd (l-rand-in-circle 0.1) vb)))
              (join-verts v1 v2))

            (sandpaint-edges sand snk grains)
            (sandpaint-verts sand snk)))))

    (sandpaint-save sand *file*)))


(main)
