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

;; name: sewer-obs2.gc
;; name in dgo: sewer-obs2
;; dgos: SEA

;; DECOMP BEGINS

(deftype sew-laser-beam (process-drawable)
  ((sync      sync-linear  :inline)
   (sound-id  sound-id)
   )
  (:state-methods
    idle
    )
  )


(defskelgroup skel-sew-laser-beam sew-laser-beam sew-laser-beam-lod0-jg sew-laser-beam-idle-ja
              ((sew-laser-beam-lod0-mg (meters 20)) (sew-laser-beam-lod1-mg (meters 999999)))
              :bounds (static-spherem 0 0 5.75 12)
              :shadow sew-laser-beam-shadow-mg
              :shadow-joint-index 4
              )

(defun fire-sew-laser-beam! ((arg0 vector) (arg1 vector) (arg2 sew-laser-beam))
  (let ((s4-0 (new 'stack-no-clear 'collide-query))
        (f30-0 47104.0)
        )
    (vector-copy! (-> s4-0 start-pos) arg0)
    (vector-normalize-copy! (-> s4-0 move-dist) arg1 f30-0)
    (let ((v1-2 s4-0))
      (set! (-> v1-2 radius) 40.96)
      (set! (-> v1-2 collide-with) (collide-spec backgnd jak enemy obstacle hit-by-others-list player-list))
      (set! (-> v1-2 ignore-process0) arg2)
      (set! (-> v1-2 ignore-process1) #f)
      (set! (-> v1-2 ignore-pat)
            (new 'static 'pat-surface :noentity #x1 :nojak #x1 :probe #x1 :noendlessfall #x1 :board #x1)
            )
      (set! (-> v1-2 action-mask) (collide-action solid))
      )
    (let ((f0-1 (fill-and-probe-using-line-sphere *collide-cache* s4-0)))
      (if (>= f0-1 0.0)
          (vector-float*! (-> s4-0 move-dist) (-> s4-0 move-dist) f0-1)
          (set! (-> s4-0 best-other-tri collide-ptr) #f)
          )
      )
    (when (and (-> s4-0 best-other-tri collide-ptr)
               (as-type (-> s4-0 best-other-tri collide-ptr) collide-shape-prim-sphere)
               )
      (let ((s2-1 (new 'stack-no-clear 'projectile-init-by-other-params)))
        (set! (-> s2-1 ent) (-> arg2 entity))
        (set! (-> s2-1 charge) 1.0)
        (set! (-> s2-1 options) (projectile-options))
        (logclear! (-> s2-1 options) (projectile-options po14 po15 po16))
        (set! (-> s2-1 notify-handle) (the-as handle #f))
        (set! (-> s2-1 owner-handle) (the-as handle #f))
        (set! (-> s2-1 target-handle) (the-as handle #f))
        (set! (-> s2-1 target-pos quad) (the-as uint128 0))
        (set! (-> s2-1 ignore-handle) (process->handle arg2))
        (let* ((v1-19 *game-info*)
               (a0-17 (+ (-> v1-19 attack-id) 1))
               )
          (set! (-> v1-19 attack-id) a0-17)
          (set! (-> s2-1 attack-id) a0-17)
          )
        (set! (-> s2-1 timeout) (seconds 4))
        (vector-copy! (-> s2-1 pos) (-> s4-0 start-pos))
        (vector-normalize-copy! (-> s2-1 vel) (-> s4-0 move-dist) 491520.0)
        (sound-play "laser-hit-jak")
        (spawn-projectile sew-laser-shot s2-1 arg2 *default-dead-pool*)
        )
      (vector-normalize! (-> s4-0 move-dist) (+ 2048.0 (vector-length (-> s4-0 move-dist))))
      )
    (set! (-> *part-id-table* 4935 init-specs 4 initial-valuef) f30-0)
    (let ((a2-5 (new 'stack-no-clear 'vector)))
      (vector-float*! a2-5 arg1 f30-0)
      (draw-beam (-> *part-id-table* 4935) arg0 a2-5 #t)
      )
    )
  (none)
  )

(defstate idle (sew-laser-beam)
  :virtual #t
  :trans (behavior ()
    (sound-play "laser-beam-loop" :id (-> self sound-id))
    (let ((gp-0 (vector<-cspace! (new 'stack-no-clear 'vector) (joint-node sew-laser-beam-lod0-jg laser))))
      (new 'stack-no-clear 'vector)
      (let ((s5-0 (new 'stack-no-clear 'vector)))
        (vector-z-quaternion! s5-0 (-> self root quat))
        (vector-normalize! s5-0 1.0)
        (fire-sew-laser-beam! gp-0 s5-0 self)
        )
      )
    )
  :code (behavior ()
    (until #f
      (let ((f0-2 (* (get-norm! (-> self sync) 0) (the float (ja-num-frames 0))))
            (a0-2 (-> self skel root-channel 0))
            )
        (set! (-> a0-2 param 0) f0-2)
        (joint-control-channel-group-eval! a0-2 (the-as art-joint-anim #f) num-func-loop-set!)
        )
      (suspend)
      )
    #f
    )
  :post ja-post
  )

(define *sew-laser-beam-shadow-control*
  (new 'static 'shadow-control :settings (new 'static 'shadow-settings
                                           :center (new 'static 'vector :w (the-as float #x9a))
                                           :shadow-dir (new 'static 'vector :y -1.0 :w 614400.0)
                                           :bot-plane (new 'static 'plane :y 1.0 :w 163840.0)
                                           :top-plane (new 'static 'plane :y 1.0 :w -163840.0)
                                           :shadow-type 1
                                           )
                               )
  )

(defmethod init-from-entity! ((this sew-laser-beam) (arg0 entity-actor))
  "Set up a newly created process from the entity that created it."
  (set! (-> this root) (new 'process 'trsqv))
  (process-drawable-from-entity! this arg0)
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-sew-laser-beam" (the-as (pointer level) #f)))
    (the-as pair 0)
    )
  (let ((a1-5 (new 'stack-no-clear 'sync-info-params)))
    (let ((v1-4 0))
      (if #f
          (set! v1-4 (logior v1-4 1))
          )
      (set! (-> a1-5 sync-type) 'sync-linear)
      (set! (-> a1-5 sync-flags) (the-as sync-flags v1-4))
      )
    (set! (-> a1-5 entity) arg0)
    (set! (-> a1-5 period) (the-as uint 2700))
    (set! (-> a1-5 percent) 0.0)
    (initialize! (-> this sync) a1-5)
    )
  (set! (-> this sound-id) (new-sound-id))
  (set! (-> this draw shadow-ctrl) *sew-laser-beam-shadow-control*)
  (set-setting! 'spotlight-color #f 0.0 (the-as uint #x80ffc000))
  (go (method-of-object this idle))
  )

(defmethod deactivate ((this sew-laser-beam))
  "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-m-gate (process-drawable)
  ((actor-group        (pointer actor-group))
   (actor-group-count  int32)
   )
  (:state-methods
    idle
    open
    raised
    )
  )


(defskelgroup skel-sew-m-gate sew-m-gate sew-m-gate-lod0-jg sew-m-gate-idle-ja
              ((sew-m-gate-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 -0.5 0 6.5)
              )

(defstate idle (sew-m-gate)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('trigger)
       (go-virtual open)
       )
      )
    )
  :enter (behavior ()
    (set-time! (-> self state-time))
    )
  :trans (behavior ()
    (local-vars (v1-6 symbol))
    (when (time-elapsed? (-> self state-time) (seconds 0.1))
      (when (> (-> self actor-group-count) 0)
        (dotimes (v1-5 (-> self actor-group 0 length))
          (let ((a1-1 (-> self actor-group 0 data v1-5 actor)))
            (when (or (not a1-1) (not (logtest? (-> a1-1 extra perm status) (entity-perm-status subtask-complete))))
              (set! v1-6 #f)
              (goto cfg-13)
              )
            )
          )
        )
      (set! v1-6 #t)
      (label cfg-13)
      (if v1-6
          (go-virtual open)
          )
      )
    )
  :code sleep-code
  :post #f
  )

(defstate open (sew-m-gate)
  :virtual #t
  :code (behavior ()
    (process-entity-status! self (entity-perm-status subtask-complete) #t)
    (suspend-for (seconds 0.5)
      )
    (set-setting! 'mode-name 'cam-fixed 0.0 0)
    (set-setting! 'interp-time 'abs 0.0 0)
    (set-setting! 'entity-name "camera-353" 0.0 0)
    (process-grab? *target* #f)
    (suspend-for (seconds 2)
      )
    (sound-play "gate-open")
    (ja-no-eval :group! sew-m-gate-gate-open-ja :num! (seek! max 0.1) :frame-num 0.0)
    (until (ja-done? 0)
      (transform-post)
      (suspend)
      (ja :num! (seek! max 0.1))
      )
    (remove-setting! 'mode-name)
    (remove-setting! 'interp-time)
    (remove-setting! 'entity-name)
    (suspend-for (seconds 0.5)
      )
    (process-release? *target*)
    (go-virtual raised)
    )
  :post #f
  )

(defstate raised (sew-m-gate)
  :virtual #t
  :enter (behavior ()
    (ja :group! sew-m-gate-gate-open-ja :num! max)
    (transform-post)
    )
  :code sleep-code
  :post #f
  )

(defmethod init-from-entity! ((this sew-m-gate) (arg0 entity-actor))
  "Set up a newly created process from the entity that created it."
  (local-vars (sv-16 res-tag))
  (let ((s4-0 (new 'process 'collide-shape this (collide-list-enum usually-hit-by-player))))
    (let ((v1-2 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 0) (the-as uint 0))))
      (set! (-> v1-2 prim-core collide-as) (collide-spec obstacle))
      (set! (-> v1-2 prim-core collide-with) (collide-spec jak hit-by-others-list player-list))
      (set! (-> v1-2 prim-core action) (collide-action solid))
      (set! (-> v1-2 transform-index) 3)
      (set-vector! (-> v1-2 local-sphere) 0.0 -2048.0 0.0 26624.0)
      (set! (-> s4-0 total-prims) (the-as uint 1))
      (set! (-> s4-0 root-prim) v1-2)
      )
    (set! (-> s4-0 nav-radius) (* 0.75 (-> s4-0 root-prim local-sphere w)))
    (let ((v1-5 (-> s4-0 root-prim)))
      (set! (-> s4-0 backup-collide-as) (-> v1-5 prim-core collide-as))
      (set! (-> s4-0 backup-collide-with) (-> v1-5 prim-core collide-with))
      )
    (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-m-gate" (the-as (pointer level) #f)))
    (the-as pair 0)
    )
  (logclear! (-> this mask) (process-mask actor-pause))
  (set! sv-16 (new 'static 'res-tag))
  (let ((v1-13 (res-lump-data (-> this entity) 'actor-groups pointer :tag-ptr (& sv-16))))
    (cond
      ((and v1-13 (nonzero? (-> sv-16 elt-count)))
       (set! (-> this actor-group) (the-as (pointer actor-group) v1-13))
       (set! (-> this actor-group-count) (the-as int (-> sv-16 elt-count)))
       )
      (else
        (set! (-> this actor-group) (the-as (pointer actor-group) #f))
        (set! (-> this actor-group-count) 0)
        0
        )
      )
    )
  (let ((v1-21 (-> this skel root-channel 0)))
    (set! (-> v1-21 frame-group) (the-as art-joint-anim (-> this draw art-group data 2)))
    )
  (transform-post)
  (if (logtest? (-> this entity extra perm status) (entity-perm-status subtask-complete))
      (go (method-of-object this raised))
      (go (method-of-object this idle))
      )
  )

(deftype sew-pipe (process-drawable)
  ((actor-group        (pointer actor-group))
   (actor-group-count  int32)
   )
  (:state-methods
    idle
    lower
    down
    )
  )


(defskelgroup skel-sew-pipe sew-pipe sew-pipe-lod0-jg sew-pipe-idle-ja
              ((sew-pipe-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 -7.5 0 27)
              )

(defstate idle (sew-pipe)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('trigger)
       (go-virtual lower)
       )
      )
    )
  :enter (behavior ()
    (set-time! (-> self state-time))
    )
  :trans (behavior ()
    (local-vars (v1-6 symbol))
    (when (time-elapsed? (-> self state-time) (seconds 0.1))
      (when (> (-> self actor-group-count) 0)
        (dotimes (v1-5 (-> self actor-group 0 length))
          (let ((a1-1 (-> self actor-group 0 data v1-5 actor)))
            (when (or (not a1-1) (not (logtest? (-> a1-1 extra perm status) (entity-perm-status subtask-complete))))
              (set! v1-6 #f)
              (goto cfg-13)
              )
            )
          )
        )
      (set! v1-6 #t)
      (label cfg-13)
      (if v1-6
          (go-virtual lower)
          )
      )
    )
  :code sleep-code
  :post ja-post
  )

(defstate lower (sew-pipe)
  :virtual #t
  :code (behavior ()
    (process-entity-status! self (entity-perm-status subtask-complete) #t)
    (suspend-for (seconds 1)
      )
    (set-setting! 'mode-name 'cam-fixed 0.0 0)
    (set-setting! 'interp-time 'abs 0.0 0)
    (set-setting! 'entity-name "camera-352" 0.0 0)
    (process-grab? *target* #f)
    (suspend-for (seconds 1)
      )
    (sound-play "pipe-lower")
    (set-time! (-> self state-time))
    (ja-no-eval :group! sew-pipe-down-ja :num! (seek! max 0.1) :frame-num 0.0)
    (until (ja-done? 0)
      (if (time-elapsed? (-> self state-time) (seconds 1.557))
          (sound-play "pipe-lower-hit")
          )
      (transform-post)
      (suspend)
      (ja :num! (seek! max 0.1))
      )
    (remove-setting! 'mode-name)
    (remove-setting! 'interp-time)
    (remove-setting! 'entity-name)
    (suspend-for (seconds 0.5)
      )
    (process-release? *target*)
    (go-virtual down)
    )
  :post #f
  )

(defstate down (sew-pipe)
  :virtual #t
  :enter (behavior ()
    (ja :group! sew-pipe-down-ja :num! max)
    (transform-post)
    )
  :code sleep-code
  :post #f
  )

(defmethod init-from-entity! ((this sew-pipe) (arg0 entity-actor))
  "Set up a newly created process from the entity that created it."
  (local-vars (sv-16 res-tag))
  (let ((s4-0 (new 'process 'collide-shape this (collide-list-enum usually-hit-by-player))))
    (let ((v1-2 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 0) (the-as uint 0))))
      (set! (-> v1-2 prim-core collide-as) (collide-spec obstacle))
      (set! (-> v1-2 prim-core collide-with) (collide-spec jak hit-by-others-list player-list))
      (set! (-> v1-2 prim-core action) (collide-action solid))
      (set! (-> v1-2 transform-index) 3)
      (set-vector! (-> v1-2 local-sphere) 0.0 -30720.0 0.0 110592.0)
      (set! (-> s4-0 total-prims) (the-as uint 1))
      (set! (-> s4-0 root-prim) v1-2)
      )
    (set! (-> s4-0 nav-radius) (* 0.75 (-> s4-0 root-prim local-sphere w)))
    (let ((v1-5 (-> s4-0 root-prim)))
      (set! (-> s4-0 backup-collide-as) (-> v1-5 prim-core collide-as))
      (set! (-> s4-0 backup-collide-with) (-> v1-5 prim-core collide-with))
      )
    (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-pipe" (the-as (pointer level) #f)))
    (the-as pair 0)
    )
  (logclear! (-> this mask) (process-mask actor-pause))
  (set! sv-16 (new 'static 'res-tag))
  (let ((v1-13 (res-lump-data (-> this entity) 'actor-groups pointer :tag-ptr (& sv-16))))
    (cond
      ((and v1-13 (nonzero? (-> sv-16 elt-count)))
       (set! (-> this actor-group) (the-as (pointer actor-group) v1-13))
       (set! (-> this actor-group-count) (the-as int (-> sv-16 elt-count)))
       )
      (else
        (set! (-> this actor-group) (the-as (pointer actor-group) #f))
        (set! (-> this actor-group-count) 0)
        0
        )
      )
    )
  (if (logtest? (-> this entity extra perm status) (entity-perm-status subtask-complete))
      (go (method-of-object this down))
      (go (method-of-object this idle))
      )
  )

(deftype sew-grate-plat (process-drawable)
  ((test-pos  vector  :inline)
   (closed-x  float)
   (opened-x  float)
   )
  (:state-methods
    closed
    open
    opened
    close
    )
  )


(defskelgroup skel-sew-grate-plat sew-grate-plat sew-grate-plat-lod0-jg sew-grate-plat-idle-ja
              ((sew-grate-plat-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 10)
              )

(defstate closed (sew-grate-plat)
  :virtual #t
  :trans (behavior ()
    (let ((gp-0 (-> self test-pos))
          (a0-1 (target-pos 0))
          )
      (if (and (< 0.0 (- (-> a0-1 y) (-> gp-0 y))) (< (vector-vector-xz-distance a0-1 gp-0) 61440.0))
          (go-virtual open)
          )
      )
    )
  :code sleep-code
  :post ja-post
  )

(defstate open (sew-grate-plat)
  :virtual #t
  :enter (behavior ()
    (sound-play "sew-grate-slide")
    )
  :trans (behavior ()
    (seek! (-> self root trans x) (-> self opened-x) (* 327680.0 (seconds-per-frame)))
    (if (= (-> self root trans x) (-> self opened-x))
        (go-virtual opened)
        )
    )
  :code sleep-code
  :post transform-post
  )

(defstate opened (sew-grate-plat)
  :virtual #t
  :trans (behavior ()
    (let ((gp-0 (-> self test-pos))
          (a0-1 (target-pos 0))
          )
      (if (not (and (< 0.0 (- (-> a0-1 y) (-> gp-0 y))) (< (vector-vector-xz-distance a0-1 gp-0) 61440.0)))
          (go-virtual close)
          )
      )
    )
  :code sleep-code
  :post ja-post
  )

(defstate close (sew-grate-plat)
  :virtual #t
  :enter (behavior ()
    (sound-play "sew-grate-slide" :pitch -0.25)
    )
  :trans (behavior ()
    (seek! (-> self root trans x) (-> self closed-x) (* 327680.0 (seconds-per-frame)))
    (if (= (-> self root trans x) (-> self closed-x))
        (go-virtual closed)
        )
    )
  :code sleep-code
  :post transform-post
  )

(defmethod init-from-entity! ((this sew-grate-plat) (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 ((v1-2 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 0) (the-as uint 0))))
      (set! (-> v1-2 prim-core collide-as) (collide-spec obstacle))
      (set! (-> v1-2 prim-core collide-with) (collide-spec jak hit-by-others-list player-list))
      (set! (-> v1-2 prim-core action) (collide-action solid))
      (set! (-> v1-2 transform-index) 3)
      (set-vector! (-> v1-2 local-sphere) 0.0 0.0 0.0 40960.0)
      (set! (-> s4-0 total-prims) (the-as uint 1))
      (set! (-> s4-0 root-prim) v1-2)
      )
    (set! (-> s4-0 nav-radius) (* 0.75 (-> s4-0 root-prim local-sphere w)))
    (let ((v1-5 (-> s4-0 root-prim)))
      (set! (-> s4-0 backup-collide-as) (-> v1-5 prim-core collide-as))
      (set! (-> s4-0 backup-collide-with) (-> v1-5 prim-core collide-with))
      )
    (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-grate-plat" (the-as (pointer level) #f)))
    (the-as pair 0)
    )
  (vector-copy! (-> this test-pos) (-> this root trans))
  (+! (-> this test-pos x) -40960.0)
  (set! (-> this closed-x) (-> this root trans x))
  (set! (-> this opened-x) (+ -32768.0 (-> this root trans x)))
  (go (method-of-object this closed))
  )
