;;-*-Lisp-*-
(in-package goal)
(bundles "SNO.DGO")
(require "engine/target/logic-target.gc")

;; DECOMP BEGINS

(deftype snowball-info (basic)
  ((entity basic)))

(deftype snowball-bank (basic) ())

(define *SNOWBALL-bank* (new 'static 'snowball-bank))

(defbehavior target-snowball-post target ()
  (let ((gp-0 (the int (-> *display* time-ratio))))
    (set-time-ratios *display* 1.0)
    (countdown (s5-0 gp-0)
      (set! (-> self control unknown-int40) s5-0)
      (flag-setup)
      (build-conversions (-> self control transv))
      (reverse-conversions (-> self control transv))
      (post-flag-setup))
    (set-time-ratios *display* (the float gp-0)))
  (rot->dir-targ! (-> self control))
  (ja-post)
  (joint-points)
  (do-target-shadow)
  (target-powerup-process)
  0
  (none))

(defstate target-snowball-start (target)
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (cond
        ((and (= message 'query) (= (-> block param 0) 'mode)) 'snowball)
        ((let ((v1-3 message)) (= v1-3 'end-mode))
         (go target-jump (-> *TARGET-bank* duck-jump-height-min) (-> *TARGET-bank* duck-jump-height-max) (the-as surface #f)))
        (else (target-generic-event-handler proc argc message block))))
  :exit
    (behavior ()
      (when (!= (-> self next-state name) 'target-snowball)
        (let ((v1-1 (-> self manipy)))
          (when v1-1
            (deactivate (-> v1-1 0))
            (set! (-> self manipy) (the-as (pointer manipy) #f))))
        (logclear! (-> self control root-prim prim-core action) (collide-action snowball))
        (set! (-> self control unknown-surface00) *walk-mods*)
        (set! (-> self control dynam gravity-max) (-> self control unknown-dynamics00 gravity-max))
        (set! (-> self control dynam gravity-length) (-> self control unknown-dynamics00 gravity-length))
        (target-collide-set! 'normal 0.0)
        (set! (-> self control reaction) target-collision-reaction)
        (target-exit)))
  :code
    (behavior ((arg0 handle))
      (target-exit)
      (set! *display-profile* #f)
      (set! *display-entity-errors* #f)
      (if (zero? (-> self snowball)) (set! (-> self snowball) (new 'process 'snowball-info)))
      (set! (-> self snowball entity) #f)
      (let ((a0-2 (handle->process arg0))) (if a0-2 (set! (-> self snowball entity) (-> a0-2 entity))))
      (reset-target-state #t)
      (set! (-> self control transv quad) (the-as uint128 0))
      (set! (-> self control unknown-float01) 0.0)
      (move-by-vector! (-> self control) (new 'static 'vector :y 4096.0 :w 1.0))
      (logior! (-> self control root-prim prim-core action) (collide-action snowball))
      (ja-channel-set! 1)
      (ja :group! eichar-stance-loop-ja :num! min)
      (remove-exit)
      (go target-snowball))
  :post target-post)

(defstate target-snowball (target)
  :event
    (-> target-snowball-start
        event)
  :exit
    (-> target-snowball-start
        exit)
  :code
    (behavior ()
      (loop
        (suspend)))
  :post
    (behavior ()
      (target-snowball-post)))
