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

;; name: squid-extras.gc
;; name in dgo: squid-extras
;; dgos: PALBOSS

;; DECOMP BEGINS

(defstate idle (squid-driver)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('matrix)
       (let ((v0-0 (the-as object (-> block param 0))))
         (let* ((a2-1 (-> self node-list data 3 bone transform))
                (v1-3 (-> a2-1 quad 0))
                (a0-2 (-> a2-1 quad 1))
                (a1-1 (-> a2-1 quad 2))
                (a2-2 (-> a2-1 trans quad))
                )
           (set! (-> (the-as matrix v0-0) quad 0) v1-3)
           (set! (-> (the-as matrix v0-0) quad 1) a0-2)
           (set! (-> (the-as matrix v0-0) quad 2) a1-1)
           (set! (-> (the-as matrix v0-0) trans quad) a2-2)
           )
         v0-0
         )
       )
      )
    )
  :trans (behavior ()
    (ja :num! (loop!))
    (ja-post)
    )
  :code sleep-code
  )

(defstate idle (squid-baron)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (rlet ((vf0 :class vf))
      (init-vf0-vector)
      (case message
        (('color)
         (.svf (&-> (-> self draw color-mult) quad) vf0)
         (set! (-> self draw color-mult w) 1.0)
         (let ((v0-0 (-> self draw color-emissive)))
           (set! (-> v0-0 quad) (-> (the-as rgbaf (-> block param 0)) quad))
           v0-0
           )
         )
        )
      )
    )
  :trans (behavior ()
    (clone-anim-once (ppointer->handle (-> self parent)) #f "")
    )
  :code sleep-code
  )

(defstate test (squid-tentacle)
  :virtual #t
  :event squid-tentacle-handler
  :enter (behavior ()
    (joint-setup self)
    )
  :trans (behavior ()
    (ja :num! (loop!))
    (squid-tentacle-method-23 self)
    (dotimes (v1-6 11)
      )
    )
  :code sleep-code
  :post (behavior ()
    (transform-post)
    (do-push-aways (-> self root))
    )
  )

(defstate idle (squid-tentacle)
  :virtual #t
  :event squid-tentacle-handler
  :trans (behavior ()
    (ja :num! (loop!))
    (ja-post)
    (go-virtual test)
    )
  :code sleep-code
  :post (behavior ()
    (transform-post)
    (do-push-aways (-> self root))
    )
  )

(defstate idle (squid-collision)
  :virtual #t
  :code sleep-code
  )

(defstate impact (squid-grenade)
  :virtual #t
  :enter (behavior ()
    (sound-play "sqd-missile-hit")
    (let ((t9-2 (-> (method-of-type projectile impact) enter)))
      (if t9-2
          (t9-2)
          )
      )
    (let ((gp-1 (new 'stack-no-clear 'explosion-init-params)))
      (set! (-> gp-1 spawn-point quad) (-> self root trans quad))
      (quaternion-copy! (-> gp-1 spawn-quat) (-> self root quat))
      (set! (-> gp-1 radius) 16384.0)
      (set! (-> gp-1 group) group-squid-grenade-explode)
      (set! (-> gp-1 collide-with)
            (collide-spec backgnd jak crate civilian enemy obstacle vehicle-sphere hit-by-others-list player-list pusher)
            )
      (set! (-> gp-1 penetrate-using) (penetrate explode))
      (explosion-spawn (the-as process-drawable *default-pool*) explosion gp-1)
      )
    )
  )

(defstate moving (squid-grenade)
  :virtual #t
  :exit (behavior ()
    (sound-stop (-> self fly-sound))
    (let ((t9-1 (-> (method-of-type projectile moving) exit)))
      (if t9-1
          (t9-1)
          )
      )
    )
  )

(defun squid-grenade-move ((arg0 squid-grenade))
  (local-vars (at-0 int))
  (with-pp
    (rlet ((vf0 :class vf)
           (vf1 :class vf)
           (vf2 :class vf)
           )
      (init-vf0-vector)
      (let ((f28-0 (/ (the float (- (current-time) (-> arg0 traj-timer))) (-> arg0 traj-duration)))
            (s5-0 (new 'stack-no-clear 'vector))
            )
        (let ((f30-0 (compute-time-until-apex (-> arg0 traj))))
          (if (>= f28-0 1.0)
              (go (method-of-object arg0 impact))
              )
          (vector-! s5-0 (-> arg0 traj-dest) (-> arg0 traj initial-position))
          (vector-normalize!
            s5-0
            (* (lerp-scale 1.0 0.0 f28-0 (* 1.5 f30-0) 1.0) (lerp-scale 0.0 20480.0 f28-0 0.0 0.06))
            )
          (let ((f28-1 (* f28-0 f28-0)))
            (let ((s4-1 (new 'stack-no-clear 'vector)))
              (compute-trans-at-time (-> arg0 traj) f28-1 s4-1)
              (vector+! s5-0 s5-0 s4-1)
              )
            (let ((s3-1 (new 'stack-no-clear 'matrix)))
              (vector-normalize! (compute-transv-at-time (-> arg0 traj) f28-1 (-> s3-1 vector 2)) 1.0)
              (vector-normalize! (vector-cross! (-> s3-1 vector 0) (new 'static 'vector :y 1.0) (-> s3-1 vector 2)) 1.0)
              (vector-cross! (-> s3-1 vector 1) (-> s3-1 vector 2) (-> s3-1 vector 0))
              (when (>= f30-0 f28-1)
                (let ((s4-3 (new 'stack-no-clear 'vector)))
                  (let ((f26-1 (/ 4.0 f30-0)))
                    (if (< 1.5 (-> arg0 charge-level))
                        (vector-float*! s4-3 (-> s3-1 vector 0) (* 8192.0 (+ -1.0 (cos (* 65536.0 f28-1 f26-1)))))
                        (vector-float*! s4-3 (-> s3-1 vector 0) (* 8192.0 (- 1.0 (cos (* 65536.0 f28-1 f26-1)))))
                        )
                    (vector+float*! s4-3 s4-3 (-> s3-1 vector 1) (* -8192.0 (sin (* 65536.0 f28-1 f26-1))))
                    )
                  (vector+float*! s5-0 s5-0 s4-3 (lerp-scale 1.0 0.0 f28-1 0.0 f30-0))
                  )
                )
              )
            )
          )
        (vector-! (-> arg0 root transv) s5-0 (-> arg0 root trans))
        )
      (let ((v1-33 (-> arg0 root transv)))
        (.lvf vf1 (&-> (-> arg0 root transv) quad))
        (let ((f0-28 (-> pp clock frames-per-second)))
          (.mov at-0 f0-28)
          )
        (.mov vf2 at-0)
        (.mov.vf.w vf1 vf0)
        (.mul.x.vf.xyz vf1 vf1 vf2)
        (.svf (&-> v1-33 quad) vf1)
        )
      (let* ((s5-1 (quaternion->matrix (new 'stack-no-clear 'matrix) (-> arg0 root quat)))
             (a2-6 (vector-normalize-copy! (new 'stack-no-clear 'vector) (-> arg0 root transv) 1.0))
             (a1-16 (quaternion-from-two-vectors! (new 'stack-no-clear 'quaternion) (-> s5-1 vector 2) a2-6))
             )
        (quaternion-normalize! (quaternion*! (-> arg0 root quat) a1-16 (-> arg0 root quat)))
        )
      (projectile-move-fill-line-sphere arg0)
      (if (logtest? (-> arg0 root status) (collide-status touch-surface))
          (go (method-of-object arg0 impact))
          )
      (sound-play "sqd-missl-trail" :id (-> arg0 fly-sound) :position (-> arg0 root trans))
      0
      (none)
      )
    )
  )

(defmethod play-impact-sound! ((this squid-grenade))
  "Plays impact sound"
  (ja-post)
  0
  (none)
  )

(defmethod init-proj-collision! ((this squid-grenade))
  "Init the [[projectile]]'s [[collide-shape]]"
  (let ((s5-0 (new 'process 'collide-shape-moving this (collide-list-enum hit-by-player))))
    (set! (-> s5-0 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s5-0 reaction)
          (the-as (function control-info collide-query vector vector collide-status) cshape-reaction-just-move)
          )
    (set! (-> s5-0 no-reaction)
          (the-as (function collide-shape-moving collide-query vector vector object) nothing)
          )
    (set! (-> s5-0 penetrate-using)
          (penetrate
            generic-attack
            lunge
            flop
            punch
            spin
            roll
            uppercut
            bonk
            tube
            vehicle
            flut-attack
            board
            mech-punch
            dark-punch
            dark-giant
            )
          )
    (let ((v1-7 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-7 prim-core collide-as) (collide-spec projectile))
      (set! (-> v1-7 prim-core collide-with)
            (collide-spec backgnd jak bot crate obstacle hit-by-others-list player-list)
            )
      (set! (-> v1-7 prim-core action) (collide-action solid deadly))
      (set-vector! (-> v1-7 local-sphere) 0.0 0.0 0.0 2048.0)
      (set! (-> s5-0 total-prims) (the-as uint 1))
      (set! (-> s5-0 root-prim) v1-7)
      )
    (set! (-> s5-0 nav-radius) (* 0.75 (-> s5-0 root-prim local-sphere w)))
    (let ((v1-10 (-> s5-0 root-prim)))
      (set! (-> s5-0 backup-collide-as) (-> v1-10 prim-core collide-as))
      (set! (-> s5-0 backup-collide-with) (-> v1-10 prim-core collide-with))
      )
    (set! (-> s5-0 max-iteration-count) (the-as uint 1))
    (set! (-> s5-0 event-self) 'touched)
    (set! (-> this root) s5-0)
    )
  (set! (-> this root pat-ignore-mask)
        (new 'static 'pat-surface :noentity #x1 :nojak #x1 :probe #x1 :noproj #x1 :noendlessfall #x1)
        )
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-squid-grenade" (the-as (pointer uint32) #f)))
    (the-as pair 0)
    )
  (set! (-> this draw light-index) (the-as uint 10))
  (when (-> this draw shadow)
    (set! (-> this draw shadow-ctrl)
          (new 'process 'shadow-control -204800.0 4096.0 4096000.0 (shadow-flags) 819200.0)
          )
    (let ((v1-25 (-> this draw shadow-ctrl)))
      (logclear! (-> v1-25 settings flags) (shadow-flags disable-draw))
      )
    0
    )
  0
  (none)
  )

(defmethod init-proj-settings! ((this squid-grenade))
  "Init relevant settings for the [[projectile]] such as gravity, speed, timeout, etc"
  (set! (-> this part) (create-launch-control group-squid-grenade-trail this))
  (set! (-> this attack-mode) 'squid-grenade)
  (set! (-> this max-speed) 655360.0)
  (set! (-> this move) squid-grenade-move)
  (set! (-> this timeout) (seconds 5))
  (set! (-> this stop-speed) 4.096)
  (set! (-> this fly-sound) (new-sound-id))
  (set-time! (-> this traj-timer))
  (vector+! (-> this traj-dest) (-> this root trans) (-> this root transv))
  (set! (-> this traj-duration)
        (fmax 600.0 (* 0.0018310547 (vector-vector-xz-distance (-> this root trans) (-> this traj-dest))))
        )
  (+! (-> this traj-duration) (* 75.0 (rand-vu)))
  (let ((f0-8 32768.0))
    (if (< (-> this root trans y) (-> this traj-dest y))
        (+! f0-8 (- (-> this traj-dest y) (-> this root trans y)))
        )
    (setup-from-to-duration-and-height! (-> this traj) (-> this root trans) (-> this traj-dest) 1.0 f0-8)
    )
  (none)
  )

(defbehavior squid-whirlwind-move squid-whirlwind ()
  (vector+float*!
    (-> self center)
    (-> self center)
    (-> self center-vel)
    (* 819.2 (-> self clock time-adjust-ratio))
    )
  (let ((gp-0 (new 'stack-no-clear 'vector)))
    (let ((f30-0 (lerp-scale 12288.0 32768.0 (the float (- (current-time) (-> self state-time))) 0.0 900.0))
          (f28-0 (lerp-scale 1024.0 4096.0 (the float (- (current-time) (-> self state-time))) 0.0 900.0))
          )
      (vector-! (-> self root transv) (-> self root trans) (-> self center))
      (set-vector! gp-0 (- (-> self root transv z)) 0.0 (-> self root transv x) 1.0)
      (vector-normalize! gp-0 (* 2252.8 (-> self clock time-adjust-ratio)))
      (vector+! gp-0 gp-0 (-> self root trans))
      (vector-! gp-0 gp-0 (-> self center))
      (set! (-> gp-0 y) 0.0)
      (let ((f0-14 (vector-length gp-0)))
        (cond
          ((< f0-14 (- f30-0 f28-0))
           (vector-float*! gp-0 gp-0 (/ (- f30-0 f28-0) f0-14))
           )
          ((< (+ f30-0 f28-0) f0-14)
           (vector-float*! gp-0 gp-0 (/ (+ f30-0 f28-0) f0-14))
           )
          )
        )
      )
    (vector+! gp-0 gp-0 (-> self center))
    (vector-! (-> self root transv) gp-0 (-> self root trans))
    )
  (set! (-> self root transv y) 0.0)
  (let ((s5-0 (new 'stack-no-clear 'vector))
        (gp-1 (new 'stack-no-clear 'vector))
        (f30-1 (-> self root trans y))
        )
    (when (-> self current-nav-poly)
      (vector+! (-> self root trans) (-> self root trans) (-> self root transv))
      (set! (-> self current-nav-poly)
            (cloest-point-on-mesh (-> self nav) (-> self root trans) (-> self root trans) (-> self current-nav-poly))
            )
      (let ((s3-0 (-> self nav))
            (a1-14 (-> self current-nav-poly))
            (s4-0 gp-1)
            )
        (let ((v1-41 (-> self root trans)))
          (project-point-onto-plane-of-poly-local
            (-> s3-0 state mesh)
            a1-14
            s4-0
            s5-0
            (vector-! (new 'stack-no-clear 'vector) v1-41 (-> s3-0 state mesh bounds))
            )
          )
        (vector+! s4-0 s4-0 (-> s3-0 state mesh bounds))
        )
      0
      (set! (-> self root trans y) (lerp f30-1 (+ 4096.0 (-> gp-1 y)) 0.1))
      )
    )
  (ja-post)
  0
  (none)
  )

(defstate idle (squid-whirlwind)
  :virtual #t
  :enter (behavior ()
    (set-time! (-> self state-time))
    (set! (-> self duration) (the-as time-frame (+ (the int (* 150.0 (rand-vu))) 1050)))
    )
  :trans (behavior ()
    (squid-whirlwind-move)
    (ja :num! (loop!))
    (sound-play "squid-whirlwind" :id (-> self whirl-sound) :position (-> self root trans))
    (set! (-> self whirl-sound-playing) #t)
    (let ((a1-2 (new 'stack-no-clear 'vector)))
      (set! (-> a1-2 quad) (-> self root trans quad))
      (+! (-> a1-2 y) 2048.0)
      (spawn (-> self part) a1-2)
      )
    (when (or (time-elapsed? (-> self state-time) (-> self duration))
              (let ((f0-3 (vector-vector-xz-distance-squared (target-pos 0) (-> self root trans)))
                    (f1-1 12288.0)
                    )
                (< f0-3 (* f1-1 f1-1))
                )
              )
      (let ((gp-1 (new 'stack-no-clear 'explosion-init-params)))
        (set! (-> gp-1 spawn-point quad) (-> self root trans quad))
        (quaternion-copy! (-> gp-1 spawn-quat) (-> self root quat))
        (set! (-> gp-1 radius) 16384.0)
        (set! (-> gp-1 group) group-squid-whirlwind-explode)
        (set! (-> gp-1 collide-with)
              (collide-spec backgnd jak crate civilian enemy obstacle vehicle-sphere hit-by-others-list player-list pusher)
              )
        (set! (-> gp-1 penetrate-using) (penetrate explode))
        (explosion-spawn (the-as process-drawable *default-pool*) explosion gp-1)
        )
      (sound-play "sqd-whirl-xplod")
      (deactivate self)
      )
    )
  :code sleep-code
  )

(defmethod deactivate ((this squid-whirlwind))
  "Make a process dead, clean it up, remove it from the active pool, and return to dead pool."
  (if (-> this whirl-sound-playing)
      (sound-stop (-> this whirl-sound))
      )
  ((method-of-type process-drawable deactivate) this)
  (none)
  )

(defbehavior scene-tentacle-spawn squid-tentacle ((arg0 process-drawable) (arg1 vector) (arg2 cspace))
  (let ((gp-0 0))
    (case (+ (-> arg2 joint number) 1)
      ((7)
       (set! gp-0 0)
       )
      ((8)
       (set! gp-0 1)
       )
      ((9)
       (set! gp-0 2)
       )
      ((10)
       (set! gp-0 3)
       )
      ((11)
       (set! gp-0 4)
       )
      ((12)
       (set! gp-0 5)
       )
      )
    (let ((s4-0 (process-spawn squid-tentacle (-> arg0 root trans) gp-0 :to arg0)))
      (send-event (ppointer->process s4-0) 'set-stretch-vel 0.76)
      (send-event (ppointer->process s4-0) 'set-fade 128)
      )
    )
  0
  (none)
  )

(defmethod draw ((this hud-squid))
  (set-hud-piece-position! (-> this sprites 1) (the int (+ 462.0 (* 130.0 (-> this offset)))) 350)
  (set-as-offset-from! (-> this sprites 0) (-> this sprites 1 pos) -32 0)
  (set-as-offset-from! (-> this sprites 2) (-> this sprites 1 pos) -96 0)
  (set-as-offset-from! (-> this sprites 5) (-> this sprites 1 pos) -62 14)
  (set-as-offset-from! (-> this sprites 6) (-> this sprites 1 pos) -32 14)
  (let ((s5-0 (-> this values 0 current))
        (f28-0 (* 0.01 (the float (-> this values 1 current))))
        (f30-0 (* 0.01 (the float (-> this values 2 current))))
        )
    (set-as-offset-from! (-> this sprites 3) (-> this sprites 1 pos) -92 15)
    (cond
      ((zero? s5-0)
       (set! (-> this sprites 3 color x) 0)
       (set! (-> this sprites 3 color y) 255)
       (set! f28-0 (+ 2.0 f28-0))
       )
      ((= s5-0 1)
       (set! (-> this sprites 3 color y) 255)
       (set! (-> this sprites 3 color x) 255)
       (set! f28-0 (+ 1.0 f28-0))
       )
      (else
        (set! (-> this sprites 3 color x) 255)
        (set! (-> this sprites 3 color y) 0)
        0
        )
      )
    (set! (-> this sprites 3 scale-x) (* -7.25 f28-0))
    (set-as-offset-from! (-> this sprites 4) (-> this sprites 1 pos) -84 4)
    (cond
      ((< f30-0 0.5)
       (set! (-> this sprites 4 color x) 255)
       (set! (-> this sprites 4 color y) (the int (lerp 0.0 255.0 (* 2.0 f30-0))))
       )
      (else
        (set! (-> this sprites 4 color x) (the int (lerp 255.0 0.0 (* 2.0 (+ -0.5 f30-0)))))
        (set! (-> this sprites 4 color y) 255)
        )
      )
    (set! (-> this sprites 4 scale-x) (* -18.25 f30-0))
    )
  ((method-of-type hud draw) this)
  0
  (none)
  )

(defmethod init-callback ((this hud-squid))
  (set! (-> this gui-id)
        (add-process *gui-control* this (gui-channel hud-middle-right) (gui-action hidden) (-> this name) 81920.0 0)
        )
  (set! (-> this values 0 target) 0)
  (set! (-> this values 1 target) 100)
  (logior! (-> this flags) (hud-flags show))
  (set! (-> this sprites 0 tex) (get-texture hud-bmech-health-01 level-default-minimap))
  (set! (-> this sprites 0 flags) (the-as uint 4))
  (set! (-> this sprites 1 tex) (get-texture hud-bmech-health-02 level-default-minimap))
  (set! (-> this sprites 1 flags) (the-as uint 4))
  (set! (-> this sprites 2 tex)
        (lookup-texture-by-name "hud-baronsymbol-01" (the-as string #f) (the-as (pointer texture-page) #f))
        )
  (set! (-> this sprites 2 flags) (the-as uint 4))
  (set! (-> this sprites 5 tex) (get-texture hud-bmech-health-03 level-default-minimap))
  (set! (-> this sprites 5 scale-x) 0.5)
  (set! (-> this sprites 5 flags) (the-as uint 4))
  (set! (-> this sprites 6 tex) (get-texture hud-bmech-health-03 level-default-minimap))
  (set! (-> this sprites 6 scale-x) 0.5)
  (set! (-> this sprites 6 flags) (the-as uint 4))
  (set! (-> this sprites 3 tex) (get-texture hud-bmech-health-04 level-default-minimap))
  (set! (-> this sprites 3 scale-y) 3.25)
  (set! (-> this sprites 3 color z) 0)
  (set! (-> this sprites 3 flags) (the-as uint 4))
  (set! (-> this sprites 4 tex) (get-texture hud-bmech-health-04 level-default-minimap))
  (set! (-> this sprites 4 scale-y) 1.5)
  (set! (-> this sprites 4 color z) 0)
  (set! (-> this sprites 4 flags) (the-as uint 4))
  (alloc-string-if-needed this 0)
  (set! (-> this strings 0 flags) (font-flags kerning large))
  (set! (-> this strings 0 scale) 0.5)
  0
  (none)
  )
