;;-*-Lisp-*-
(in-package goal)

;; name: idle-control.gc
;; name in dgo: idle-control
;; dgos: GAME

;; +++idle-control-cmd
(defenum idle-control-cmd
  :type uint8
  (reset 0)
  (play 1)
  (push 2)
  )
;; ---idle-control-cmd


;; DECOMP BEGINS

(deftype idle-control-frame (structure)
  ((command  idle-control-cmd)
   (anim     uint32)
   (param0   int32)
   (param1   int32)
   (param2   pair)
   )
  )


(deftype idle-control (structure)
  ((anim           (inline-array idle-control-frame))
   (anim-speed     float)
   (current-index  int32)
   (counter        int32)
   (target         int32)
   )
  (:methods
    (init! (_type_ (inline-array idle-control-frame)) none)
    (play-idle-frames! (_type_ process-drawable) none :behavior process-drawable)
    )
  )


;; WARN: Return type mismatch idle-control vs none.
(defmethod init! ((this idle-control) (arg0 (inline-array idle-control-frame)))
  "Initialize this [[idle-control]]."
  (set! (-> this anim) arg0)
  (set! (-> this anim-speed) 0.0)
  (set! (-> this current-index) 0)
  (set! (-> this counter) 0)
  (set! (-> this target) 0)
  (none)
  )

;; WARN: Function (method 10 idle-control) has a return type of none, but the expression builder found a return statement.
(defmethod play-idle-frames! ((this idle-control) (arg0 process-drawable))
  "Set the process pointer to the given [[process-drawable]] and run the idle frames for it."
  (when (nonzero? (-> this anim))
    ;; og:preserve-this
    (protect (PP)
      ;; (set! self arg0)
      (set! PP arg0)
      (loop
        (let ((s4-0 (-> this anim (-> this current-index))))
          (case (-> s4-0 command)
            (((idle-control-cmd play))
             (if (< (-> s4-0 anim) 0)
                 (return #f)
                 )
             (when (zero? (-> this target))
               (set! (-> this target) (rand-vu-int-range (-> s4-0 param0) (-> s4-0 param1)))
               (set! (-> this anim-speed)
                     (rand-vu-float-range
                       (command-get-float (-> s4-0 param2 car) 0.0)
                       (command-get-float (-> (the-as pair (-> s4-0 param2 cdr)) car) 0.0)
                       )
                     )
               (ja :group! (-> (the-as process-drawable self) draw art-group data (-> s4-0 anim)) :num! min)
               (return #f)
               )
             (ja :group! (-> (the-as process-drawable self) draw art-group data (-> s4-0 anim))
                 :num! (seek! max (-> this anim-speed))
                 )
             (cond
               ((ja-done? 0)
                (+! (-> this counter) 1)
                (cond
                  ((>= (-> this counter) (-> this target))
                   (+! (-> this current-index) 1)
                   (set! (-> this counter) 0)
                   (set! (-> this target) 0)
                   0
                   )
                  (else
                    (ja :num-func num-func-identity :frame-num 0.0)
                    (return #f)
                    )
                  )
                )
               (else
                 (return #f)
                 )
               )
             )
            (((idle-control-cmd push))
             (ja-channel-push! 1 (the-as time-frame (-> s4-0 param0)))
             (+! (-> this current-index) 1)
             (set! (-> this counter) 0)
             (set! (-> this target) 0)
             0
             )
            (((idle-control-cmd reset))
             (set! (-> this current-index) 0)
             (set! (-> this counter) 0)
             (set! (-> this target) 0)
             0
             )
            )
          )
        )
      ;; og:preserve-this
      ;; (set! self s5-0)
      )
    )
  0
  (none)
  )
