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

;; name: sewer-move-turret.gc
;; name in dgo: sewer-move-turret
;; dgos: SEA

(declare-type sew-move-turret process-drawable)
(declare-type sew-move-turret-shot projectile)
(define-extern spawn-sew-move-turret-projectile (function sew-move-turret vector vector float vector (pointer sew-move-turret-shot)))

;; DECOMP BEGINS

(deftype sew-move-turret (process-drawable)
  ((sound-id      sound-id)
   (use-doppler?  symbol)
   )
  (:state-methods
    idle
    active
    )
  (:methods
    (sew-move-turret-method-22 (_type_ int) none)
    )
  )


(defskelgroup skel-sew-move-turret sew-move-turret sew-move-turret-lod0-jg sew-move-turret-idle-ja
              ((sew-move-turret-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 20)
              )

(defstate idle (sew-move-turret)
  :virtual #t
  :trans (behavior ()
    (let ((f30-0 (vector-vector-distance (-> self root trans) (target-pos 0)))
          (gp-1 (new 'stack-no-clear 'vector))
          )
      (vector-! gp-1 (target-pos 0) (-> self root trans))
      (let ((f0-1 (vector-dot gp-1 (-> self node-list data 5 bone transform fvec)))
            (f1-2 (fabs (vector-dot gp-1 (-> self node-list data 5 bone transform rvec))))
            )
        (if (and (< 4096.0 f0-1) (>= 81920.0 f1-2) (< f30-0 450560.0))
            (go-virtual active)
            )
        )
      )
    )
  :code sleep-code
  :post ja-post
  )

(defstate active (sew-move-turret)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('use-doppler?)
       (-> self use-doppler?)
       )
      )
    )
  :enter (behavior ()
    (set-time! (-> self state-time))
    (set! (-> self use-doppler?) #f)
    )
  :trans (behavior ()
    (let ((f30-0 (vector-vector-distance (-> self root trans) (target-pos 0)))
          (gp-1 (new 'stack-no-clear 'vector))
          )
      (vector-! gp-1 (target-pos 0) (-> self root trans))
      (let ((f0-1 (vector-dot gp-1 (-> self node-list data 5 bone transform fvec)))
            (f1-2 (fabs (vector-dot gp-1 (-> self node-list data 5 bone transform rvec))))
            )
        (when (or (>= 0.0 f0-1) (< 81920.0 f1-2) (>= f30-0 450560.0))
          (if (nonzero? (-> self sound-id))
              (sound-stop (-> self sound-id))
              )
          (go-virtual idle)
          )
        )
      )
    (logior! (-> self skel status) (joint-control-status sync-math))
    )
  :code (behavior ()
    (until #f
      (sound-play "turret-move-lp" :id (-> self sound-id))
      (let ((f30-0 -10.0))
        (until (ja-done? 0)
          (when (time-elapsed? (-> self state-time) (seconds 0.05))
            (let ((f28-0 (ja-frame-num 0)))
              (let ((v1-6 #f))
                (if (or (and (< f30-0 23.0) (>= f28-0 23.0)) (and (< f30-0 67.0) (>= f28-0 67.0)))
                    (set! v1-6 #t)
                    )
                (set! (-> self use-doppler?) v1-6)
                )
              (set-time! (-> self state-time))
              (sound-play "turret-gun-fire")
              (sew-move-turret-method-22 self 5)
              (set! f30-0 f28-0)
              )
            )
          (suspend)
          (ja :num! (seek! max 2.0))
          )
        )
      (ja-no-eval :group! sew-move-turret-move-shoot-ja :num! (seek!) :frame-num 0.0)
      )
    #f
    )
  :post transform-post
  )

(defmethod sew-move-turret-method-22 ((this sew-move-turret) (arg0 int))
  (let ((s5-0 (new 'stack-no-clear 'vector)))
    (vector-copy! s5-0 (-> this node-list data arg0 bone transform fvec))
    (let ((s4-0 (vector<-cspace! (new 'stack-no-clear 'vector) (-> this node-list data arg0))))
      (let ((f0-0 (ja-frame-num 0)))
        (cond
          ((< f0-0 45.0)
           (let ((f1-1 1228.8))
             (cond
               ((< f0-0 5.0)
                (set! f1-1 (* (/ f0-0 5) f1-1))
                )
               ((< 40.0 f0-0)
                (set! f1-1 (* 0.2 (- 45.0 f0-0) f1-1))
                )
               )
             (vector+float*! s4-0 s4-0 (-> this node-list data arg0 bone transform rvec) f1-1)
             )
           )
          (else
            (let ((f1-2 -1228.8))
              (cond
                ((< f0-0 50.0)
                 (set! f1-2 (* 0.2 (+ -45.0 f0-0) f1-2))
                 )
                ((< 85.0 f0-0)
                 (set! f1-2 (* 0.2 (- 90.0 f0-0) f1-2))
                 )
                )
              (vector+float*! s4-0 s4-0 (-> this node-list data arg0 bone transform rvec) f1-2)
              )
            )
          )
        )
      (vector-normalize! s5-0 40960.0)
      (spawn-sew-move-turret-projectile
        this
        s4-0
        (vector+! (new 'stack-no-clear 'vector) s4-0 s5-0)
        122880.0
        (the-as vector #f)
        )
      )
    )
  0
  (none)
  )

(defmethod init-from-entity! ((this sew-move-turret) (arg0 entity-actor))
  "Set up a newly created process from the entity that created it."
  (let ((s4-0 (new 'process 'collide-shape this (collide-list-enum usually-hit-by-player))))
    (let ((s3-0 (new 'process 'collide-shape-prim-group s4-0 (the-as uint 2) 0)))
      (set! (-> s4-0 total-prims) (the-as uint 3))
      (set! (-> s3-0 prim-core collide-as) (collide-spec obstacle))
      (set! (-> s3-0 prim-core collide-with) (collide-spec jak player-list))
      (set! (-> s3-0 prim-core action) (collide-action solid no-standon))
      (set! (-> s3-0 transform-index) 3)
      (set-vector! (-> s3-0 local-sphere) 0.0 0.0 0.0 12288.0)
      (set! (-> s4-0 root-prim) s3-0)
      )
    (let ((v1-9 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 0) (the-as uint 1))))
      (set! (-> v1-9 prim-core collide-as) (collide-spec obstacle))
      (set! (-> v1-9 prim-core collide-with) (collide-spec jak player-list))
      (set! (-> v1-9 prim-core action) (collide-action solid no-standon))
      (set! (-> v1-9 transform-index) 4)
      (set-vector! (-> v1-9 local-sphere) 0.0 0.0 0.0 12288.0)
      )
    (let ((v1-11 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 1) (the-as uint 1))))
      (set! (-> v1-11 prim-core collide-as) (collide-spec obstacle))
      (set! (-> v1-11 prim-core collide-with) (collide-spec jak player-list))
      (set! (-> v1-11 prim-core action) (collide-action solid no-standon))
      (set! (-> v1-11 transform-index) 3)
      (set-vector! (-> v1-11 local-sphere) 0.0 0.0 0.0 12288.0)
      )
    (set! (-> s4-0 nav-radius) (* 0.75 (-> s4-0 root-prim local-sphere w)))
    (let ((v1-14 (-> s4-0 root-prim)))
      (set! (-> s4-0 backup-collide-as) (-> v1-14 prim-core collide-as))
      (set! (-> s4-0 backup-collide-with) (-> v1-14 prim-core collide-with))
      )
    (set! (-> s4-0 penetrated-by) (penetrate))
    (set! (-> this root) s4-0)
    )
  (process-drawable-from-entity! this arg0)
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-sew-move-turret" (the-as (pointer level) #f)))
    (the-as pair 0)
    )
  (logclear! (-> this mask) (process-mask actor-pause))
  (let ((v1-23 (-> this skel root-channel 0)))
    (set! (-> v1-23 frame-group) (the-as art-joint-anim (-> this draw art-group data 3)))
    )
  (set! (-> this sound-id) (new-sound-id))
  (transform-post)
  (go (method-of-object this idle))
  )

(defmethod deactivate ((this sew-move-turret))
  "Make a process dead, clean it up, remove it from the active pool, and return to dead pool."
  (if (nonzero? (-> this sound-id))
      (sound-stop (-> this sound-id))
      )
  (call-parent-method this)
  (none)
  )

(deftype sew-move-turret-shot (guard-shot)
  ((doppler-sound  sound-id)
   (use-doppler?   symbol)
   )
  )


(defun sew-turret-shot-move ((arg0 sew-move-turret-shot))
  (projectile-move-fill-line-sphere arg0)
  (let ((s5-0 (-> arg0 root)))
    (let ((s4-0 (new 'stack-no-clear 'vector)))
      (vector-! s4-0 (-> arg0 tail-pos) (-> s5-0 trans))
      (let ((f0-0 (vector-length s4-0)))
        (when (< 16384.0 f0-0)
          (vector-normalize! s4-0 16384.0)
          (vector+! (-> arg0 tail-pos) (-> s5-0 trans) s4-0)
          )
        )
      )
    (when (logtest? (-> s5-0 status) (collide-status touch-surface))
      (if (logtest? (-> arg0 root status) (collide-status touch-actor))
          (set! (-> arg0 hit-actor?) #t)
          )
      (go (method-of-object arg0 impact))
      )
    )
  0
  (none)
  )

(defpart 4940
  :init-specs ((:texture (hotdot level-default-sprite))
    (:num 1.0)
    (:scale-x (meters 0.9))
    (:scale-y :copy scale-x)
    (:r 128.0)
    (:g 32.0)
    (:b 0.0)
    (:a 255.0)
    (:scalevel-x (meters -0.013333334))
    (:scalevel-y :copy scalevel-x)
    (:timer (seconds 0.085))
    (:flags (sp-cpuinfo-flag-2 sp-cpuinfo-flag-3 sp-cpuinfo-flag-14))
    )
  )

(defpart 4941
  :init-specs ((:texture (hotdot level-default-sprite))
    (:num 1.0)
    (:x (meters 0.3))
    (:scale-x (meters 0.9))
    (:scale-y :copy scale-x)
    (:r 128.0)
    (:g 32.0)
    (:b 0.0)
    (:a 255.0)
    (:scalevel-x (meters -0.013333334))
    (:scalevel-y :copy scalevel-x)
    (:timer (seconds 0.085))
    (:flags (sp-cpuinfo-flag-2 sp-cpuinfo-flag-3 sp-cpuinfo-flag-14))
    )
  )

(defpart 4942
  :init-specs ((:texture (gun-enemy-muzzleflash level-default-sprite))
    (:birth-func 'birth-func-set-quat)
    (:num 1.0)
    (:scale-x (meters 2) (meters 1))
    (:scale-y (meters 1) (meters 0.5))
    (:r 128.0)
    (:g 64.0 128.0)
    (:b 32.0)
    (:a 128.0)
    (:timer (seconds 0.035))
    (:flags (sp-cpuinfo-flag-3))
    )
  )

(defpart 4943
  :init-specs ((:texture (glow level-default-sprite))
    (:num 1.0)
    (:scale-x (meters 0.5) (meters 1.5))
    (:rot-z (degrees 0) (degrees 360))
    (:scale-y :copy scale-x)
    (:r 128.0)
    (:g 128.0)
    (:b 0.0)
    (:a 255.0)
    (:timer (seconds 0.017))
    (:flags (sp-cpuinfo-flag-3))
    )
  )

(defpartgroup group-sew-move-turret-hit-object
  :id 1506
  :duration (seconds 1)
  :flags (sp0)
  :bounds (static-bspherem 0 0 0 8)
  :parts ((sp-item 4944 :flags (sp6) :period (seconds 2) :length (seconds 0.017))
    (sp-item 4945 :flags (sp6) :period (seconds 2) :length (seconds 0.017))
    )
  )

(defpartgroup group-sew-move-turret-hit
  :id 1507
  :duration (seconds 1)
  :flags (sp0)
  :bounds (static-bspherem 0 0 0 8)
  :parts ((sp-item 4944 :flags (sp6) :period (seconds 2) :length (seconds 0.017))
    (sp-item 4945 :flags (sp6) :period (seconds 2) :length (seconds 0.017))
    (sp-item 4946 :period (seconds 2) :length (seconds 0.017))
    )
  )

(defpart 4946
  :init-specs ((:texture (bigpuff level-default-sprite))
    (:num 1.0)
    (:scale-x (meters 0.75) (meters 0.5))
    (:rot-z (degrees 0) (degrees 3600))
    (:scale-y :copy scale-x)
    (:r 255.0)
    (:g 128.0)
    (:b 0.0)
    (:a 16.0 48.0)
    (:vel-y (meters 0.013333334) (meters 0.013333334))
    (:scalevel-x (meters 0.0016666667) (meters 0.006666667))
    (:rotvel-z (degrees -0.2) (degrees 0.4))
    (:scalevel-y :copy scalevel-x)
    (:fade-r -0.30476192)
    (:fade-g 0.15238096)
    (:fade-b 0.30476192)
    (:fade-a -0.15238096)
    (:accel-y (meters -0.00016666666) (meters -0.00016666666))
    (:friction 0.9)
    (:timer (seconds 1.4))
    (:flags (sp-cpuinfo-flag-2 sp-cpuinfo-flag-3))
    (:conerot-x (degrees 0) (degrees 3600))
    (:conerot-y (degrees 0) (degrees 3600))
    )
  )

(defpart 4944
  :init-specs ((:texture (glow level-default-sprite))
    (:num 1.0)
    (:scale-x (meters 3))
    (:rot-z (degrees 0) (degrees 360))
    (:scale-y :copy scale-x)
    (:r 255.0)
    (:g 255.0)
    (:b 255.0)
    (:a 128.0)
    (:scalevel-x (meters -0.026666667))
    (:rotvel-z (degrees 0.3))
    (:scalevel-y :copy scalevel-x)
    (:fade-g -3.2)
    (:fade-b -6.375)
    (:fade-a -2.4)
    (:timer (seconds 0.135))
    (:flags (sp-cpuinfo-flag-0 sp-cpuinfo-flag-2 sp-cpuinfo-flag-3))
    )
  )

(defmethod projectile-method-25 ((this sew-move-turret-shot))
  (let* ((a1-0 (-> this root trans))
         (v1-1 (-> this tail-pos))
         (a0-2 (vector-! (new 'stack-no-clear 'vector) a1-0 v1-1))
         )
    (vector-length a0-2)
    (let ((gp-1 (vector+*! (new 'stack-no-clear 'vector) v1-1 a0-2 0.8)))
      (launch-particles (-> *part-id-table* 4940) gp-1)
      (launch-particles (-> *part-id-table* 4941) gp-1)
      )
    )
  0
  (none)
  )

(defmethod projectile-method-24 ((this sew-move-turret-shot))
  (draw-beam (-> *part-id-table* 4942) (-> this tail-pos) (-> this starting-dir) #f)
  (let* ((v1-1 (vector-normalize-copy! (new 'stack-no-clear 'vector) (-> this starting-dir) 2048.0))
         (gp-1 (vector+! (new 'stack-no-clear 'vector) (-> this tail-pos) v1-1))
         )
    (set-sewh-turret-flash!)
    (launch-particles (-> *part-id-table* 4943) gp-1)
    )
  0
  (none)
  )

(defmethod projectile-method-26 ((this sew-move-turret-shot))
  (let* ((s4-0 (-> this root))
         (v1-1 (vector-normalize! (vector-! (new 'stack-no-clear 'vector) (-> this tail-pos) (-> s4-0 trans)) 2048.0))
         (gp-0 (new 'stack-no-clear 'vector))
         )
    (vector-copy! gp-0 (-> s4-0 trans))
    (vector+! gp-0 gp-0 v1-1)
    (cond
      ((-> this hit-actor?)
       (sound-play "bullet-hit-jak")
       (cond
         ((logtest? (-> *part-group-id-table* 1506 flags) (sp-group-flag sp13))
          (vector-copy! (-> *launch-matrix* trans) gp-0)
          (part-tracker-spawn part-tracker-subsampler :to *entity-pool* :group (-> *part-group-id-table* 1506))
          )
         (else
           (vector-copy! (-> *launch-matrix* trans) gp-0)
           (part-tracker-spawn part-tracker :to *entity-pool* :group (-> *part-group-id-table* 1506))
           )
         )
       )
      ((begin (sound-play "bullet-ricco") (logtest? (-> *part-group-id-table* 1507 flags) (sp-group-flag sp13)))
       (vector-copy! (-> *launch-matrix* trans) gp-0)
       (part-tracker-spawn part-tracker-subsampler :to *entity-pool* :group (-> *part-group-id-table* 1507))
       )
      (else
        (vector-copy! (-> *launch-matrix* trans) gp-0)
        (part-tracker-spawn part-tracker :to *entity-pool* :group (-> *part-group-id-table* 1507))
        )
      )
    )
  0
  (none)
  )

;; WARN: Return type mismatch (pointer process) vs (pointer sew-move-turret-shot).
(defun spawn-sew-move-turret-projectile ((arg0 sew-move-turret) (arg1 vector) (arg2 vector) (arg3 float) (arg4 vector))
  (let ((gp-0 (new 'stack-no-clear 'projectile-init-by-other-params)))
    (let ((v1-1 (vector-! (new 'stack-no-clear 'vector) arg2 arg1)))
      (set! (-> gp-0 ent) (-> arg0 entity))
      (set! (-> gp-0 charge) 1.0)
      (set! (-> gp-0 options) (projectile-options))
      (logclear! (-> gp-0 options) (projectile-options po14 po15 po16))
      (set! (-> gp-0 notify-handle) (process->handle arg0))
      (set! (-> gp-0 owner-handle) (the-as handle #f))
      (set! (-> gp-0 target-handle) (the-as handle #f))
      (set! (-> gp-0 target-pos quad) (the-as uint128 0))
      (set! (-> gp-0 ignore-handle) (process->handle arg0))
      (let* ((a0-13 *game-info*)
             (a2-12 (+ (-> a0-13 attack-id) 1))
             )
        (set! (-> a0-13 attack-id) a2-12)
        (set! (-> gp-0 attack-id) a2-12)
        )
      (set! (-> gp-0 timeout) (seconds 4))
      (if arg4
          (vector-copy! (-> gp-0 pos) arg4)
          (vector-copy! (-> gp-0 pos) arg1)
          )
      (vector-normalize-copy! (-> gp-0 vel) v1-1 40960.0)
      )
    (the-as (pointer sew-move-turret-shot) (spawn-projectile sew-move-turret-shot gp-0 arg0 *default-dead-pool*))
    )
  )

;; WARN: Return type mismatch (function sew-move-turret-shot none) vs none.
(defmethod init-proj-settings! ((this sew-move-turret-shot))
  (call-parent-method this)
  (set! (-> this doppler-sound) (new-sound-id))
  (set! (-> this use-doppler?) (the-as symbol (send-event (ppointer->process (-> this parent)) 'use-doppler?)))
  (set! (-> this max-speed) 163840.0)
  (set! (-> this timeout) (the-as time-frame (the int (* 300.0 (/ 819200.0 (-> this max-speed))))))
  (set! (-> this move) sew-turret-shot-move)
  (none)
  )

;; WARN: Return type mismatch object vs none.
(defmethod play-impact-sound ((this sew-move-turret-shot) (arg0 projectile-options))
  (cond
    ((and (= arg0 3) (-> this use-doppler?))
     (let ((s5-2 (vector-! (new 'stack-no-clear 'vector) (-> this root trans) (target-pos 0))))
       0.0
       (let ((s4-1 (< 0.0 (vector-dot s5-2 (-> this root transv))))
             (f0-2 (vector-normalize-ret-len! s5-2 1.0))
             )
         (if (or (and s4-1 (< f0-2 40960.0)) (and (not s4-1) (< f0-2 102400.0)))
             (sound-play-by-name
               (static-sound-name "bullet-by-loop")
               (-> this doppler-sound)
               1024
               (the int
                    (* 1524.0 (fmax -0.08 (fmin 1.0 (* 0.5 (doppler-pitch-shift (-> this root trans) (-> this root transv))))))
                    )
               0
               (sound-group)
               (-> this root trans)
               )
             (sound-stop (-> this doppler-sound))
             )
         )
       )
     )
    (else
      (call-parent-method this arg0)
      )
    )
  (none)
  )

(defmethod deactivate ((this sew-move-turret-shot))
  "Make a process dead, clean it up, remove it from the active pool, and return to dead pool."
  (sound-stop (-> this doppler-sound))
  (call-parent-method this)
  (none)
  )
