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

;; name: transport.gc
;; name in dgo: transport
;; dgos: CWI

(define-extern ctywide-entity-hack (function none))

;; DECOMP BEGINS

(defskelgroup skel-vehicle-turret vehicle-turret vehicle-turret-lod0-jg vehicle-turret-idle-ja
              ((vehicle-turret-lod0-mg (meters 20))
               (vehicle-turret-lod1-mg (meters 40))
               (vehicle-turret-lod2-mg (meters 999999))
               )
              :bounds (static-spherem 0 0 0 2)
              )

(define *transport-turret-state-info* (new 'static 'traffic-guard-type-settings
                                        :acquire-delay (seconds 1.5)
                                        :shot-delay (seconds 0.25)
                                        :burst-delay (seconds 3)
                                        :shot-count 2
                                        :rand-shot-count 1
                                        )
        )

(deftype vehicle-turret (process-focusable)
  ((turret-jm  joint-mod)
   (turret     turret-control  :inline)
   (target     handle)
   )
  (:state-methods
    idle
    )
  (:methods
    (vehicle-turret-method-28 (_type_) none)
    (vehicle-turret-method-29 (_type_) none)
    (vehicle-turret-method-30 (_type_) none)
    )
  )


(defmethod vehicle-turret-method-29 ((this vehicle-turret))
  (let* ((s4-0 (ppointer->process (-> this parent)))
         (s5-0 (if (type? s4-0 process-focusable)
                   s4-0
                   )
               )
         )
    (when s5-0
      (let ((a2-0 (matrix<-transformq!
                    (new 'stack-no-clear 'matrix)
                    (the-as transformq (-> (the-as process-drawable s5-0) root trans))
                    )
                  )
            )
        (vector-matrix*! (-> this root trans) (new 'static 'vector :y 8601.6 :z 12288.0 :w 1.0) a2-0)
        )
      (quaternion-copy! (-> this root quat) (-> (the-as process-drawable s5-0) root quat))
      )
    )
  0
  (none)
  )

(defstate idle (vehicle-turret)
  :virtual #t
  :trans (behavior ()
    (update-joint-mod (-> self turret) (the-as joint-mod-rotate-local (-> self turret-jm)))
    (vehicle-turret-method-29 self)
    (when (and (nonzero? (-> self target)) (handle->process (-> self target)))
      (let* ((s5-0 (handle->process (-> self target)))
             (gp-0 (if (type? s5-0 process-focusable)
                       s5-0
                       )
                   )
             (s5-1 (new 'stack-no-clear 'vector))
             )
        (set! (-> s5-1 quad) (-> (get-trans (the-as process-focusable gp-0) 3) quad))
        (turret-control-method-11 (-> self turret) self s5-1 (-> (the-as process-focusable gp-0) root transv))
        )
      )
    )
  :code sleep-code
  :post transform-post
  )

(defmethod vehicle-turret-method-28 ((this vehicle-turret))
  (let ((s5-0 (new 'process 'collide-shape-moving this (collide-list-enum usually-hit-by-player))))
    (set! (-> s5-0 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s5-0 reaction) cshape-reaction-default)
    (set! (-> s5-0 no-reaction)
          (the-as (function collide-shape-moving collide-query vector vector object) nothing)
          )
    (set! (-> s5-0 penetrated-by) (penetrate))
    (let ((v1-6 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-6 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-6 prim-core collide-with) (collide-spec jak player-list))
      (set! (-> v1-6 prim-core action) (collide-action solid))
      (set! (-> v1-6 transform-index) 3)
      (set-vector! (-> v1-6 local-sphere) 0.0 0.0 0.0 4096.0)
      (set! (-> s5-0 total-prims) (the-as uint 1))
      (set! (-> s5-0 root-prim) v1-6)
      )
    (set! (-> s5-0 nav-radius) (* 0.75 (-> s5-0 root-prim local-sphere w)))
    (let ((v1-9 (-> s5-0 root-prim)))
      (set! (-> s5-0 backup-collide-as) (-> v1-9 prim-core collide-as))
      (set! (-> s5-0 backup-collide-with) (-> v1-9 prim-core collide-with))
      )
    (set! (-> this root) s5-0)
    )
  0
  (none)
  )

(defmethod relocate ((this vehicle-turret) (arg0 int))
  (if (nonzero? (-> this turret-jm))
      (&+! (-> this turret-jm) arg0)
      )
  (call-parent-method this arg0)
  )

(defmethod vehicle-turret-method-30 ((this vehicle-turret))
  (set! (-> this turret-jm)
        (the-as joint-mod (new 'process 'joint-mod-rotate-local this (-> this turret info joint-index) #t))
        )
  0
  (none)
  )

;; WARN: Return type mismatch object vs none.
(defbehavior vehicle-turret-init-by-other vehicle-turret ((arg0 turret-control-info))
  (vehicle-turret-method-28 self)
  (set-info (-> self turret) arg0)
  (logior! (-> self turret flags) (turret-flag targetting-laser no-rot-y-clamp))
  (vehicle-turret-method-29 self)
  (vector-identity! (-> self root scale))
  (vector-float*! (-> self root scale) (-> self root scale) 2.0)
  (ctywide-entity-hack)
  (initialize-skeleton
    self
    (the-as skeleton-group (art-group-get-by-name *level* "skel-vehicle-turret" (the-as (pointer uint32) #f)))
    (the-as pair 0)
    )
  (vehicle-turret-method-30 self)
  (set! (-> self turret guard-settings) *transport-turret-state-info*)
  (go-virtual idle)
  (none)
  )

(define *transport-turret-control-info* (new 'static 'turret-control-info
                                          :joint-index 3
                                          :barrel-count 1
                                          :shot-speed 819200.0
                                          :attack-range 102400.0
                                          :rot-min (new 'static 'array float 2 -16384.0 -2184533.2)
                                          :rot-max (new 'static 'array float 2 0.0 2184533.2)
                                          :local-pos (new 'static 'vector :y -1433.6 :z 4096.0 :w 1.0)
                                          :local-dir (new 'static 'vector :z 1.0 :w 1.0)
                                          :barrel-array (new 'static 'inline-array turret-barrel-info 4
                                            (new 'static 'turret-barrel-info
                                              :local-pos (new 'static 'vector :y -1433.6 :z 4096.0 :w 1.0)
                                              :local-dir (new 'static 'vector :z 1.0 :w 1.0)
                                              )
                                            (new 'static 'turret-barrel-info)
                                            (new 'static 'turret-barrel-info)
                                            (new 'static 'turret-barrel-info)
                                            )
                                          )
        )

(deftype transport-params (structure)
  ((spawn-pos  vector      :inline)
   (quat       quaternion  :inline)
   (nav-mesh   nav-mesh)
   (max-guard  uint32)
   (max-time   float)
   (turret?    symbol)
   (speeches?  symbol)
   )
  )


(deftype transport (process-focusable)
  ((y-dest                 float)
   (last-guard-spawn-time  time-frame)
   (nav-mesh               nav-mesh)
   (spawn-side             uint32)
   (spawn?                 symbol)
   (leave-time             time-frame)
   (max-guard              uint32)
   (count-guard            uint32)
   (max-time               float)
   (ambient-sound-id       sound-id)
   (turret                 handle)
   )
  (:state-methods
    come-down
    idle
    leave
    die-fast
    )
  (:methods
    (transport-method-31 (_type_) none)
    (transport-method-32 (_type_) none)
    (transport-method-33 (_type_) none)
    (transport-method-34 (_type_ process) none)
    (transport-method-35 (_type_) none)
    )
  )


(defskelgroup skel-transport transport transport-lod0-jg transport-idle-ja
              ((transport-lod0-mg (meters 20)) (transport-lod1-mg (meters 40)) (transport-lod2-mg (meters 999999)))
              :bounds (static-spherem 0 5 0 17)
              )

(defbehavior transport-event-handler transport ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
  (case arg2
    (('die-fast)
     (go-virtual die-fast)
     )
    (('leave)
     (if (not (and (-> self next-state) (let ((v1-6 (-> self next-state name)))
                                          (or (= v1-6 'leave) (= v1-6 'die-fast))
                                          )
                   )
              )
         (go-virtual leave)
         )
     )
    (('guards-left)
     (- (-> self max-guard) (-> self count-guard))
     )
    (('stop-spawning)
     (set! (-> self spawn?) #f)
     #f
     )
    )
  )

(defmethod run-logic? ((this transport))
  #t
  )

(defmethod transport-method-35 ((this transport))
  (let ((f30-0 (lerp-scale 0.0 2.0 (fabs (-> this root transv y)) 0.0 122880.0))
        (f0-4 (lerp-scale 0.0 1.0 (- (-> this root trans y) (-> this y-dest)) 143360.0 20480.0))
        (a0-3 (static-sound-spec "transport" :volume 0.0 :mask (pitch reg0)))
        )
    (set! (-> a0-3 volume) (the int (* 1024.0 f0-4)))
    (set! (-> a0-3 pitch-mod) (the int (* 1524.0 f30-0)))
    (sound-play-by-spec a0-3 (-> this ambient-sound-id) (-> this root trans))
    )
  0
  (none)
  )

(defmethod deactivate ((this transport))
  (sound-stop (-> this ambient-sound-id))
  (call-parent-method this)
  (none)
  )

(defstate come-down (transport)
  :virtual #t
  :event transport-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (set-time! (-> self last-guard-spawn-time))
    )
  :code (behavior ()
    (ja-channel-push! 1 0)
    (ja-no-eval :group! transport-idle-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (sleep-code)
    )
  :post (behavior ()
    (set! (-> self root transv y) (* 2.0 (- (-> self y-dest) (-> self root trans y))))
    (set! (-> self root transv y) (fmax -122880.0 (fmin 122880.0 (-> self root transv y))))
    (+! (-> self root trans y) (* (-> self root transv y) (seconds-per-frame)))
    (if (< (fabs (- (-> self root trans y) (-> self y-dest))) 409.6)
        (go-virtual idle)
        )
    (transport-method-35 self)
    (transform-post)
    )
  )

(defstate idle (transport)
  :virtual #t
  :event transport-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (set! (-> self root transv y) 0.0)
    (set! (-> self last-guard-spawn-time) 0)
    0
    )
  :exit (behavior ()
    (transport-method-34 self (the-as process #f))
    )
  :code (behavior ()
    (ja-channel-push! 1 0)
    (sound-play "tran-door-open")
    (ja-no-eval :group! transport-hatch-open-ja :num! (seek! max 0.5) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek! max 0.5))
      )
    (sleep-code)
    )
  :post (behavior ()
    (local-vars (gp-0 symbol))
    (if (-> self spawn?)
        (transport-method-33 self)
        )
    (if (and (zero? (-> self leave-time))
             (begin
               (let ((gp-1 #t)
                     (a1-0 (new 'stack-no-clear 'event-message-block))
                     )
                 (set! (-> a1-0 from) (process->ppointer self))
                 (set! (-> a1-0 num-params) 1)
                 (set! (-> a1-0 message) 'get-object-reserve-count)
                 (set! (-> a1-0 param 0) (the-as uint 6))
                 (let ((v1-9 (the-as object (send-event-function *traffic-manager* a1-0))))
                   (cmove-#f-nonzero gp-0 v1-9 gp-1)
                   )
                 )
               gp-0
               )
             )
        (set! (-> self leave-time) (+ (current-time) (seconds 3)))
        )
    (if (and (nonzero? (-> self leave-time)) (< (-> self leave-time) (current-time)))
        (go-virtual leave)
        )
    (when (and (zero? (-> self leave-time))
               (nonzero? (-> self max-guard))
               (>= (-> self count-guard) (-> self max-guard))
               )
      (set! (-> self leave-time) (+ (current-time) (seconds 3)))
      (send-event self 'stop-spawning)
      )
    (transport-method-34 self *target*)
    (transport-method-35 self)
    (transform-post)
    )
  )

(defstate die-fast (transport)
  :virtual #t
  :code (behavior ()
    (sound-stop (-> self ambient-sound-id))
    (cleanup-for-death self)
    )
  )

(defstate leave (transport)
  :virtual #t
  :event transport-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (set! (-> self root transv y) 0.0)
    )
  :code (behavior ()
    (ja-channel-push! 1 0)
    (sound-play "tran-thrust")
    (sound-play "tran-door-close")
    (ja-no-eval :group! transport-hatch-close-ja :num! (seek! max 0.2) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek! max 0.2))
      )
    (sleep-code)
    )
  :post (behavior ()
    (transport-method-35 self)
    (+! (-> self root transv y) (* 40960.0 (seconds-per-frame)))
    (set! (-> self root transv y) (fmax -122880.0 (fmin 122880.0 (-> self root transv y))))
    (+! (-> self root trans y) (* (-> self root transv y) (seconds-per-frame)))
    (if (and (not (logtest? (-> self draw status) (draw-control-status on-screen)))
             (< 163840.0 (- (-> self root trans y) (-> self y-dest)))
             )
        (go-virtual die-fast)
        )
    (transform-post)
    )
  )

(defmethod transport-method-33 ((this transport))
  (when (>= (- (current-time) (-> this last-guard-spawn-time)) 0)
    (let ((s5-0 (new 'stack 'traffic-object-spawn-params)))
      (set! (-> s5-0 object-type) (traffic-type crimson-guard-1))
      (set! (-> s5-0 behavior) (the-as uint 6))
      (set! (-> s5-0 id) (the-as uint 0))
      (set! (-> s5-0 nav-mesh) (-> this nav-mesh))
      (set! (-> s5-0 nav-branch) #f)
      (set! (-> s5-0 proc) #f)
      (set! (-> s5-0 handle) (process->handle this))
      (set! (-> s5-0 user-data) (-> this spawn-side))
      (set! (-> s5-0 flags) (traffic-spawn-flags))
      (set! (-> s5-0 guard-type) (the-as uint 7))
      (vector-reset! (-> s5-0 velocity))
      (set! (-> s5-0 position quad) (-> this root trans quad))
      (+! (-> s5-0 position y) 8192.0)
      (quaternion-rotate-local-y! (-> s5-0 rotation) (-> this root quat) 32768.0)
      (send-event *traffic-manager* 'activate-object s5-0)
      (when (-> s5-0 proc)
        (set! (-> this spawn-side) (- 1 (the-as int (-> this spawn-side))))
        (if (logtest? (-> this draw status) (draw-control-status on-screen))
            (set! (-> this last-guard-spawn-time) (+ (current-time) (seconds 1)))
            (set! (-> this last-guard-spawn-time) (+ (current-time) (seconds 2)))
            )
        (+! (-> this count-guard) 1)
        )
      (when (not (-> s5-0 proc))
        (if (logtest? (-> this draw status) (draw-control-status on-screen))
            (set! (-> this last-guard-spawn-time) (+ (current-time) (seconds 0.1)))
            (set! (-> this last-guard-spawn-time) (+ (current-time) (seconds 1)))
            )
        )
      )
    )
  (none)
  )

(defmethod transport-method-34 ((this transport) (arg0 process))
  (let ((v1-1 (handle->process (-> this turret))))
    (if v1-1
        (set! (-> (the-as vehicle-turret v1-1) target) (process->handle arg0))
        )
    )
  0
  (none)
  )

(defmethod transport-method-31 ((this transport))
  (let ((s5-0 (new 'process 'collide-shape-moving this (collide-list-enum usually-hit-by-player))))
    (set! (-> s5-0 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s5-0 reaction) cshape-reaction-default)
    (set! (-> s5-0 no-reaction)
          (the-as (function collide-shape-moving collide-query vector vector object) nothing)
          )
    (set! (-> s5-0 penetrated-by) (penetrate))
    (let ((s4-0 (new 'process 'collide-shape-prim-group s5-0 (the-as uint 2) 0)))
      (set! (-> s5-0 total-prims) (the-as uint 3))
      (set! (-> s4-0 prim-core collide-as) (collide-spec obstacle))
      (set! (-> s4-0 prim-core collide-with) (collide-spec jak player-list))
      (set! (-> s4-0 prim-core action) (collide-action solid))
      (set-vector! (-> s4-0 local-sphere) 0.0 20480.0 0.0 69632.0)
      (set! (-> s5-0 root-prim) s4-0)
      )
    (let ((v1-12 (new 'process 'collide-shape-prim-mesh s5-0 (the-as uint 0) (the-as uint 0))))
      (set! (-> v1-12 prim-core collide-as) (collide-spec obstacle))
      (set! (-> v1-12 prim-core collide-with) (collide-spec jak player-list))
      (set! (-> v1-12 prim-core action) (collide-action solid))
      (set! (-> v1-12 transform-index) 4)
      (set-vector! (-> v1-12 local-sphere) 0.0 0.0 0.0 40960.0)
      )
    (let ((v1-14 (new 'process 'collide-shape-prim-mesh s5-0 (the-as uint 1) (the-as uint 0))))
      (set! (-> v1-14 prim-core collide-as) (collide-spec obstacle))
      (set! (-> v1-14 prim-core collide-with) (collide-spec jak player-list))
      (set! (-> v1-14 prim-core action) (collide-action solid))
      (set! (-> v1-14 transform-index) 3)
      (set-vector! (-> v1-14 local-sphere) 0.0 20480.0 0.0 69632.0)
      )
    (set! (-> s5-0 nav-radius) (* 0.75 (-> s5-0 root-prim local-sphere w)))
    (let ((v1-17 (-> s5-0 root-prim)))
      (set! (-> s5-0 backup-collide-as) (-> v1-17 prim-core collide-as))
      (set! (-> s5-0 backup-collide-with) (-> v1-17 prim-core collide-with))
      )
    (set! (-> this root) s5-0)
    )
  0
  (none)
  )

(defmethod transport-method-32 ((this transport))
  (set! (-> this spawn-side) (the-as uint 0))
  0
  (none)
  )

;; WARN: Return type mismatch object vs none.
(defbehavior transport-init-by-other transport ((arg0 transport-params))
  (transport-method-31 self)
  (set! (-> self root trans quad) (-> arg0 spawn-pos quad))
  (quaternion-copy! (-> self root quat) (-> arg0 quat))
  (vector-identity! (-> self root scale))
  (set! (-> self max-guard) (-> arg0 max-guard))
  (set! (-> self count-guard) (the-as uint 0))
  (set! (-> self nav-mesh) (-> arg0 nav-mesh))
  (set! (-> self max-time) 0.0)
  (initialize-skeleton
    self
    (the-as skeleton-group (art-group-get-by-name *level* "skel-transport" (the-as (pointer uint32) #f)))
    (the-as pair 0)
    )
  (transport-method-32 self)
  (set! (-> self y-dest) (-> self root trans y))
  (+! (-> self root trans y) 163840.0)
  (set! (-> self spawn?) #t)
  (set! (-> self leave-time) 0)
  (if (-> arg0 turret?)
      (set! (-> self turret)
            (ppointer->handle (process-spawn vehicle-turret *transport-turret-control-info* :to self))
            )
      (set! (-> self turret) (the-as handle #f))
      )
  (set! (-> self ambient-sound-id) (new-sound-id))
  (go-virtual come-down)
  (none)
  )

;; WARN: Return type mismatch object vs none.
(defmethod init-from-entity! ((this transport) (arg0 entity-actor))
  "Typically the method that does the initial setup on the process, potentially using the [[entity-actor]] provided as part of that.
This commonly includes things such as:
- stack size
- collision information
- loading the skeleton group / bones
- sounds"
  (transport-method-31 this)
  (process-drawable-from-entity! this arg0)
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-transport" (the-as (pointer uint32) #f)))
    (the-as pair 0)
    )
  (transport-method-32 this)
  (set! (-> this spawn?) #t)
  (go (method-of-object this come-down))
  (none)
  )
