;;-*-Lisp-*-
(in-package goal)
(bundles "ENGINE.CGO" "GAME.CGO")
(require "engine/gfx/hw/display-h.gc")
(require "engine/util/types-h.gc")

;; A "smush-control" generates a damped sinusoidal floating point value.
;;  - on each full period, the amplitude and period are changed (multiplied)
;;  - there is a maximum duration.
;;  - the amplitude is additionally linearly scaled to go to zero over the duration.

;; DECOMP BEGINS

(deftype smush-control (structure)
  ((start-time  time-frame)
   (period      float)
   (duration    float)
   (amp         float)
   (damp-amp    float)
   (damp-period float) ;; set a negative value here to flag as die on next update
   (ticks       float))
  :pack-me
  (:methods
   (set-zero! (_type_) _type_)
   (update! (_type_) float)
   (get-no-update (_type_) float)
   (activate! (_type_ float int int float float) _type_)
   (nonzero-amplitude? (_type_) symbol)
   (die-on-next-update! (_type_) _type_)))

(defmethod nonzero-amplitude? ((this smush-control))
  "Return #t if amp is not zero, #f otherwise"
  (declare (inline))
  (!= (-> this amp) 0.0))

(defmethod set-zero! ((this smush-control))
  (set! (-> this period) 0.0)
  (set! (-> this duration) 0.0)
  (set! (-> this amp) 0.0)
  (set! (-> this damp-amp) 0.0)
  (set! (-> this damp-period) 0.0)
  (set! (-> this ticks) 0.0)
  this)

(defmethod update! ((this smush-control))
  "Run the smush control and return the result. Updates the internal state."
  (cond
    ((nonzero-amplitude? this)
     (let* ((time-since-start (the float (- (-> *display* base-frame-counter) (-> this start-time))))
            ;; use float to int rounding to figure out offset into the current period.
            (time-since-period-start (- time-since-start (* (the float (the int (/ time-since-start (-> this period)))) (-> this period)))))
       ;; we completed a new period!
       (when (>= (- time-since-start (-> this ticks)) (-> this period))
         ;; once per period updates of amp/period
         (set! (-> this amp) (* (-> this amp) (-> this damp-amp)))
         (set! (-> this period) (* (-> this period) (-> this damp-period)))
         ;; store the ticks that we did this on
         (set! (-> this ticks) time-since-start)
         ;; you can set damp-period to a negative number to indicate
         ;; that it should die on the next update. Do that here.
         (if (< (-> this damp-period) 0.0) (set-zero! this)))
       ;; absolute duraction check
       (if (>= time-since-start (-> this duration)) (set-zero! this))
       ;; sine term multiplied by amplitude, and scaled by how much is left to go.
       (* (sin (/ (* DEGREES_PER_ROT time-since-period-start) (-> this period)))
          (* (-> this amp) (/ (- (-> this duration) time-since-start) (-> this duration))))))
    ;; amplitude = 0, die.
    (else 0.0)))

(defmethod get-no-update ((this smush-control))
  "Get the value, but don't update internal state"
  (cond
    ((nonzero-amplitude? this)
     (let* ((time-since-start (the float (- (-> *display* base-frame-counter) (-> this start-time))))
            (time-since-period-start (- time-since-start (* (the float (the int (/ time-since-start (-> this period)))) (-> this period)))))
       (* (sin (/ (* DEGREES_PER_ROT time-since-period-start) (-> this period)))
          (* (-> this amp) (/ (- (-> this duration) time-since-start) (-> this duration))))))
    ;; amplitude = 0, die.
    (else 0.0)))

(defmethod die-on-next-update! ((this smush-control))
  "On the next call to update!, zero everything.
   Calls to get-no-update will still work."
  (if (nonzero-amplitude? this) (set! (-> this damp-period) -1.0))
  this)

(defmethod activate! ((this smush-control) (arg0 float) (arg1 int) (arg2 int) (arg3 float) (arg4 float))
  "Activate the smush! This only activates if the ongoing smush is mostly done."
  (when (>= (fabs (* 0.2 (-> this amp))) (fabs (get-no-update this)))
    (set! (-> this amp) arg0)
    (set! (-> this period) (the float arg1))
    (set! (-> this duration) (the float arg2))
    (set! (-> this damp-amp) arg3)
    (set! (-> this damp-period) arg4)
    (set! (-> this ticks) 0.0)
    (set-time! (-> this start-time)))
  this)
