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

;; name: gun-states.gc
;; name in dgo: gun-states
;; dgos: GAME

;; DECOMP BEGINS

(defstate target-gun-stance (target)
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('gun)
       (let ((gp-0 (-> block param 0))
             (v0-0 (target-standard-event-handler proc argc message block))
             )
         (when v0-0
           (cond
             ((= gp-0 26)
              (if (logtest? (game-secrets gun-upgrade-red-1) (-> *game-info* secrets))
                  (set! (-> self control unknown-word04) (the-as uint jakb-gun-red-fire-fast-ja))
                  (set! (-> self control unknown-word04) (the-as uint jakb-gun-red-fire-ja))
                  )
              )
             ((= gp-0 28)
              (if (logtest? (game-secrets gun-upgrade-red-3) (-> *game-info* secrets))
                  (set! (-> self control unknown-word04) (the-as uint jakb-gun-red-fire-fast-ja))
                  (set! (-> self control unknown-word04) (the-as uint jakb-gun-red-fire-ja))
                  )
              )
             ((= gp-0 27)
              (set! (-> self control unknown-word04) (the-as uint jakb-gun-red-fire-2-ja))
              )
             ((or (= gp-0 35) (= gp-0 27) (= gp-0 36) (= gp-0 37))
              (set! (-> self control unknown-word04) (the-as uint jakb-gun-dark-fire-ja))
              )
             ((or (= gp-0 29) (= gp-0 30))
              (set! (-> self control unknown-word04) (the-as uint jakb-gun-yellow-fire-low-ja))
              )
             ((= gp-0 31)
              (set! (-> self control unknown-word04) (the-as uint jakb-gun-yellow-fire-3-ja))
              )
             ((or (= gp-0 32) (= gp-0 34))
              (set! (-> self control unknown-word04) (the-as uint jakb-gun-blue-fire-single-ja))
              )
             ((= gp-0 33)
              (set! (-> self control unknown-word04) (the-as uint jakb-gun-blue-fire-2-ja))
              )
             )
           )
         v0-0
         )
       )
      (('wade)
       #f
       )
      (else
        (target-standard-event-handler proc argc message block)
        )
      )
    )
  :enter (behavior ()
    (logior! (-> self target-flags) (target-flags lleg-still rleg-still))
    (set! (-> self control mod-surface) *gun-walk-mods*)
    (set! (-> self control unknown-word04) (the-as uint #f))
    (set-time! (-> self state-time))
    )
  :exit (behavior ()
    (set! (-> self control bend-target) 0.0)
    (logclear! (-> self target-flags) (target-flags lleg-still rleg-still))
    (target-state-hook-exit)
    (target-gun-exit)
    )
  :trans (behavior ()
    ((-> self state-hook))
    (when (= (-> self control ground-pat material) (pat-material ice))
      (logclear! (-> self target-flags) (target-flags lleg-still rleg-still))
      (set! (-> self control bend-target) 0.0)
      (remove-exit)
      (go target-ice-stance)
      )
    (when (or (move-legs?) (< 8192.0 (fabs (-> self gun gun-roty-rel))))
      (logclear! (-> self target-flags) (target-flags lleg-still rleg-still))
      (set! (-> self control bend-target) 0.0)
      (remove-exit)
      (go target-gun-walk)
      )
    (if (want-to-powerjak?)
        (go target-powerjak-get-on)
        )
    (when (and (cpad-hold? (-> self control cpad number) l1) (can-duck?))
      (logclear! (-> self target-flags) (target-flags lleg-still rleg-still))
      (set! (-> self control bend-target) 0.0)
      (remove-exit)
      (go target-duck-stance #f)
      )
    ;; og:preserve-this - High FPS Fix
    (if (and (recently-pressed? x)
             (can-jump? #f)
             )
        (target-jump-go)
        )
    ;; og:preserve-this - High FPS Fix
    (if (and (recently-pressed? circle)
             (can-feet? #t)
             )
        (go target-attack)
        )
    ;; og:preserve-this - High FPS Fix
    (if (and (recently-pressed? square)
             (can-hands? #t)
             )
        (go target-running-attack)
        )
    (when (and (and (focus-test? self light) (nonzero? (-> self lightjak)))
               (and (cpad-pressed? (-> self control cpad number) triangle)
                    (logtest? (game-feature lightjak-regen) (-> self game features))
                    (and (< (-> self fact health) (-> self fact health-max)) (zero? (-> self lightjak latch-out-time)))
                    )
               )
      (set! (-> self lightjak lightjak-before-powerjak) #t)
      (go target-lightjak-regen 0)
      )
    (slide-down-test)
    (fall-test target-falling (-> *TARGET-bank* fall-height))
    )
  :code (behavior ()
    (local-vars (v1-344 object))
    (let ((gp-0 (-> self skel top-anim frame-targ)))
      (cond
        ((or (= gp-0 jakb-gun-red-takeout-ja)
             (= gp-0 jakb-gun-yellow-takeout-ja)
             (= gp-0 jakb-gun-blue-takeout-ja)
             (= gp-0 jakb-gun-dark-takeout-ja)
             )
         (ja-channel-push! 1 (seconds 0.1))
         (ja-no-eval :group! gp-0 :num! (seek!) :frame-num 0.0)
         (until (ja-done? 0)
           (suspend)
           (ja :num! (seek!))
           )
         )
        ((or (= gp-0 jakb-gun-red-fire-2-ja) (= gp-0 jakb-gun-dark-fire-ja) (= gp-0 jakb-gun-blue-fire-2-ja))
         (set! (-> self control unknown-word04) (the-as uint gp-0))
         )
        )
      )
    (let ((s5-0 (-> self game gun-type)))
      (b! #t cfg-143 :delay (nop!))
      (label cfg-19)
      (let* ((v1-47 (gun->eco (-> self gun gun-type)))
             (gp-1 (cond
                     ((= v1-47 (pickup-type eco-blue))
                      jakb-gun-stance-blue-ja
                      )
                     ((= v1-47 (pickup-type eco-yellow))
                      jakb-gun-stance-yellow-ja
                      )
                     ((= v1-47 (pickup-type eco-dark))
                      jakb-gun-stance-dark-ja
                      )
                     (else
                       jakb-gun-stance-ja
                       )
                     )
                   )
             )
        (cond
          ((and (ja-group? gp-1) (= s5-0 (-> self gun gun-type)))
           )
          ((let ((v1-67 (ja-group)))
             (and (and v1-67 (or (= v1-67 jakb-gun-stance-ja) (= v1-67 jakb-gun-stance-dark-ja)))
                  (or (= gp-1 jakb-gun-stance-ja) (= gp-1 jakb-gun-stance-dark-ja))
                  )
             )
           (ja-channel-push! 1 (seconds 0.1))
           (ja-no-eval :group! jakb-gun-transformation-twirl-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!))
             )
           (ja-channel-push! 1 (seconds 0.1))
           (set! (-> self skel root-channel 0 frame-group) (the-as art-joint-anim gp-1))
           )
          ((let ((v1-106 (ja-group)))
             (and (and v1-106 (or (= v1-106 jakb-gun-stance-ja) (= v1-106 jakb-gun-stance-dark-ja)))
                  (or (= gp-1 jakb-gun-stance-yellow-ja) (= gp-1 jakb-gun-stance-blue-ja))
                  )
             )
           (ja-channel-push! 1 (seconds 0.1))
           (ja-no-eval :group! jakb-gun-front-to-side-hop-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!))
             )
           (ja-channel-push! 1 (seconds 0.1))
           (set! (-> self skel root-channel 0 frame-group) (the-as art-joint-anim gp-1))
           )
          ((let ((v1-146 (ja-group)))
             (and (and v1-146 (or (= v1-146 jakb-gun-stance-yellow-ja) (= v1-146 jakb-gun-stance-blue-ja)))
                  (or (= gp-1 jakb-gun-stance-ja) (= gp-1 jakb-gun-stance-dark-ja))
                  )
             )
           (ja-channel-push! 1 (seconds 0.1))
           (ja-no-eval :group! jakb-gun-side-to-front-hop-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!))
             )
           (ja-channel-push! 1 (seconds 0.1))
           (set! (-> self skel root-channel 0 frame-group) (the-as art-joint-anim gp-1))
           )
          ((let ((v1-186 (ja-group)))
             (and (and v1-186 (or (= v1-186 jakb-gun-stance-yellow-ja) (= v1-186 jakb-gun-stance-blue-ja)))
                  (or (= gp-1 jakb-gun-stance-blue-ja) (= gp-1 jakb-gun-stance-yellow-ja))
                  (!= (gun->eco s5-0) (gun->eco (-> self gun gun-type)))
                  )
             )
           (ja-channel-push! 1 (seconds 0.1))
           (ja-no-eval :group! jakb-gun-blue-to-yellow-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!))
             )
           (ja-channel-push! 1 (seconds 0.1))
           (set! (-> self skel root-channel 0 frame-group) (the-as art-joint-anim gp-1))
           )
          ((and (!= s5-0 (-> self gun gun-type)) (= (gun->eco s5-0) (gun->eco (-> self gun gun-type))))
           (ja-channel-push! 1 (seconds 0.1))
           (cond
             ((= (-> self skel top-anim frame-targ) jakb-gun-side-to-side-hop-1-ja)
              (ja-no-eval :group! jakb-gun-side-to-side-hop-1-ja :num! (seek!) :frame-num 0.0)
              (until (ja-done? 0)
                (suspend)
                (ja :num! (seek!))
                )
              )
             ((= (-> self skel top-anim frame-targ) jakb-gun-side-to-side-hop-2-ja)
              (ja-no-eval :group! jakb-gun-side-to-side-hop-2-ja :num! (seek!) :frame-num 0.0)
              (until (ja-done? 0)
                (suspend)
                (ja :num! (seek!))
                )
              )
             ((= (gun->eco s5-0) (pickup-type eco-yellow))
              )
             (else
               (ja-no-eval :group! jakb-gun-transformation-twirl-ja :num! (seek!) :frame-num 0.0)
               (until (ja-done? 0)
                 (suspend)
                 (ja :num! (seek!))
                 )
               )
             )
           (ja-channel-push! 1 (seconds 0.1))
           (set! (-> self skel root-channel 0 frame-group) (the-as art-joint-anim gp-1))
           )
          (else
            (ja-channel-push! 1 (seconds 0.1))
            (set! (-> self skel root-channel 0 frame-group) (the-as art-joint-anim gp-1))
            )
          )
        )
      (set! s5-0 (-> self gun gun-type))
      (suspend)
      (ja :num! (loop!))
      (when (can-play-stance-amibent?)
        (set-time! (-> self ambient-time))
        (target-gun-end-mode #t)
        )
      (cond
        ((not (-> self control unknown-spool-anim00))
         )
        (else
          (b!
            (not (or (= (-> self control unknown-spool-anim00) jakb-gun-dark-fire-ja)
                     (= (-> self control unknown-spool-anim00) jakb-gun-red-fire-2-ja)
                     (= (-> self control unknown-spool-anim00) jakb-gun-blue-fire-2-ja)
                     )
                 )
            cfg-140
            :delay (nop!)
            )
          (ja-channel-push! 1 (seconds 0.1))
          (ja :group! (-> self control unknown-spool-anim00))
          (suspend)
          (b! #t cfg-131 :delay (nop!))
          (label cfg-130)
          (suspend)
          (label cfg-131)
          (let ((v1-343 (get-channel (-> self skel top-anim) 0)))
            (b! (not v1-343) cfg-138 :likely-delay (set! v1-344 v1-343))
            (let ((a1-37 (= (-> v1-343 frame-group) (-> self control unknown-spool-anim00))))
              (b! (not a1-37) cfg-138 :likely-delay (set! v1-344 a1-37))
              )
            (let ((a0-110 (-> self skel root-channel 0)))
              (set! (-> a0-110 num-func) num-func-identity)
              (let ((f0-57 (-> v1-343 frame-num)))
                (set! (-> a0-110 frame-num) f0-57)
                (let ((v1-345 f0-57))
                  (b! (not (the int v1-345)) cfg-138 :likely-delay (set! v1-344 v1-345))
                  )
                )
              )
            )
          (set! v1-344 (= s5-0 (-> self game gun-type)))
          (label cfg-138)
          (b! v1-344 cfg-130 :delay (nop!))
          (set! (-> self control unknown-word04) (the-as uint #f))
          (b! #t cfg-143 :delay (nop!))
          (label cfg-140)
          (ja-channel-push! 1 0)
          (ja-no-eval :group! (-> self control unknown-spool-anim00) :num! (seek!) :frame-num 0.0)
          (until (ja-done? 0)
            (suspend)
            (ja :num! (seek!))
            )
          (set! (-> self control unknown-word04) (the-as uint #f))
          )
        )
      )
    (label cfg-143)
    (b! (using-gun? self) cfg-19 :delay (nop!))
    (let ((gp-2 (-> self skel top-anim frame-targ)))
      (cond
        ((or (= gp-2 jakb-gun-red-takeout-ja) (= gp-2 jakb-gun-yellow-takeout-ja) (= gp-2 jakb-gun-dark-takeout-ja))
         (ja-channel-push! 1 (seconds 0.1))
         (ja-no-eval :group! gp-2 :num! (seek! 0.0) :frame-num max)
         (until (ja-done? 0)
           (suspend)
           (ja :num! (seek! 0.0))
           )
         )
        ((= gp-2 jakb-gun-blue-takeout-ja)
         (ja-channel-push! 1 (seconds 0.1))
         (ja-no-eval :group! gp-2 :num! (seek! 0.0) :frame-num (ja-aframe -34.0 0))
         (until (ja-done? 0)
           (suspend)
           (ja :num! (seek! 0.0))
           )
         )
        )
      )
    (go target-stance)
    )
  :post target-gun-post
  )

(defstate target-gun-walk (target)
  :event target-standard-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (set! (-> self control mod-surface) *gun-walk-mods*)
    (case (gun->eco (-> self gun gun-type))
      (((pickup-type eco-yellow) (pickup-type eco-blue))
       (set! (-> self control unknown-word04) (the-as uint 0))
       0
       )
      (else
        (set! (-> self control unknown-word04) (the-as uint 1))
        )
      )
    )
  :exit (behavior ()
    (target-effect-exit)
    (target-state-hook-exit)
    (target-gun-exit)
    )
  :trans (behavior ()
    (cond
      ((zero? (-> self control unknown-spool-anim00))
       (case (gun->eco (-> self gun gun-type))
         (((pickup-type eco-red) (pickup-type eco-dark))
          (remove-exit)
          (go target-gun-walk)
          )
         )
       )
      (else
        (case (gun->eco (-> self gun gun-type))
          (((pickup-type eco-yellow) (pickup-type eco-blue))
           (remove-exit)
           (go target-gun-walk)
           )
          )
        )
      )
    ((-> self state-hook))
    (if (not (using-gun? self))
        (go target-walk)
        )
    (if (logtest? (water-flag wading) (-> self water flags))
        (go target-wade-walk)
        )
    (when (= (-> self control ground-pat material) (pat-material ice))
      (target-effect-exit)
      (remove-exit)
      (go target-ice-walk)
      )
    (when (not (or (move-legs?) (< 182.04445 (fabs (-> self gun gun-roty-rel)))))
      (target-effect-exit)
      (remove-exit)
      (go target-gun-stance)
      )
    (if (want-to-powerjak?)
        (go target-powerjak-get-on)
        )
    ;; og:preserve-this - High FPS Fix
    (if (and (recently-pressed? l1)
             (and (!= (-> *cpad-list* cpads (-> self control cpad number) stick0-speed) 0.0) (can-roll?))
             )
        (go target-roll)
        )
    (when (and (cpad-hold? (-> self control cpad number) l1) (can-duck?))
      (target-effect-exit)
      (remove-exit)
      (go target-duck-walk #f)
      )
    ;; og:preserve-this - High FPS Fix
    (if (and (recently-pressed? x)
             (can-jump? #f)
             )
        (target-jump-go)
        )
    ;; og:preserve-this - High FPS Fix
    (if (and (recently-pressed? circle)
             (can-feet? #t)
             )
        (go target-attack)
        )
    ;; og:preserve-this - High FPS Fix
    (if (and (recently-pressed? square)
             (can-hands? #t)
             )
        (go target-running-attack)
        )
    (if (wall-hide?)
        (go target-hide)
        )
    (slide-down-test)
    (fall-test target-falling (-> *TARGET-bank* fall-height))
    )
  :code (behavior ()
    (target-walk-anim -300)
    )
  :post target-gun-post
  )
