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

;; name: cty-guard-projectile.gc
;; name in dgo: cty-guard-projectile
;; dgos: CWI

;; DECOMP BEGINS

(deftype vehicle-grenade (projectile-bounce)
  ((blast-radius  float)
   )
  (:methods
    (vehicle-grenade-method-44 (_type_) none)
    )
  )


(defskelgroup skel-vehicle-grenade gun gun-grenade-lod0-jg gun-grenade-idle-ja
              ((gun-grenade-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 2)
              :texture-level 10
              )

(defmethod play-impact-sound ((this vehicle-grenade) (arg0 projectile-options))
  (let ((v1-0 arg0))
    (cond
      ((zero? v1-0)
       (sound-play "grenade-fire")
       )
      ((= v1-0 (projectile-options po0))
       (sound-play "grenade-explode")
       )
      )
    )
  0
  (none)
  )

(defmethod setup-collision! ((this vehicle-grenade))
  (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) projectile-bounce-reaction)
    (set! (-> s5-0 no-reaction)
          (the-as (function collide-shape-moving collide-query vector vector object) nothing)
          )
    (set! (-> s5-0 penetrate-using) (penetrate explode))
    (let ((v1-7 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-7 prim-core action) (collide-action solid))
      (set-vector! (-> v1-7 local-sphere) 0.0 0.0 0.0 819.2)
      (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 2))
    (set! (-> s5-0 event-self) 'touched)
    (set! (-> this root) s5-0)
    )
  (set-collide-with!
    (-> this root)
    (collide-spec
      backgnd
      jak
      crate
      civilian
      enemy
      obstacle
      vehicle-sphere
      hit-by-others-list
      player-list
      pusher
      shield
      )
    )
  (set-collide-as! (-> this root) (collide-spec enemy))
  (set! (-> this root pat-ignore-mask)
        (new 'static 'pat-surface :noentity #x1 :nojak #x1 :probe #x1 :noproj #x1 :noendlessfall #x1 :board #x1)
        )
  (none)
  )

(defmethod projectile-method-40 ((this vehicle-grenade))
  256
  )

(defmethod init-proj-settings! ((this vehicle-grenade))
  (ctywide-entity-hack)
  (set! (-> this attack-mode) 'eco-dark)
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-vehicle-grenade" (the-as (pointer level) #f)))
    (the-as pair 0)
    )
  (let ((t9-3 (method-of-type projectile-bounce init-proj-settings!)))
    (t9-3 this)
    )
  (set! (-> this part) (create-launch-control group-guard-grenade this))
  (set! (-> this blast-radius) 16384.0)
  (set! (-> this max-speed) 90112.0)
  (set! (-> this timeout) (seconds 4))
  (let ((v1-11 (new 'stack-no-clear 'vector)))
    (set! (-> v1-11 x) 2.0)
    (set! (-> v1-11 y) 2.0)
    (set! (-> v1-11 z) 2.0)
    (set! (-> v1-11 w) 1.0)
    (vector-copy! (-> this root scale) v1-11)
    )
  0
  (none)
  )

(defmethod projectile-method-25 ((this vehicle-grenade))
  (spawn (-> this part) (-> this root trans))
  (ja-post)
  0
  (none)
  )

(defmethod projectile-bounce-method-42 ((this vehicle-grenade))
  (spawn (-> this part) (-> this root trans))
  0
  (none)
  )

;; WARN: Return type mismatch sound-id vs none.
(defmethod projectile-method-39 ((this vehicle-grenade))
  (let* ((a2-0 (-> this root))
         (v1-0 (-> a2-0 status))
         )
    (if (logtest? v1-0 (collide-status touch-surface))
        (vector-float*! (-> a2-0 transv) (-> a2-0 transv) 0.2)
        )
    (when (and (logtest? v1-0 (collide-status impact-surface))
               (time-elapsed? (-> this played-bounce-time) (seconds 0.3))
               )
      (set-time! (-> this played-bounce-time))
      (sound-play "grenade-bounce")
      )
    )
  (none)
  )

(defstate impact (vehicle-grenade)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('touched)
       (if (send-event
             proc
             'attack
             (-> block param 0)
             (static-attack-info :mask (vehicle-impulse-factor) ((id (-> self attack-id))
                                                                 (damage 2.0)
                                                                 (vehicle-damage-factor 1.0)
                                                                 (vehicle-impulse-factor 1.0)
                                                                 (mode 'explode)
                                                                 )
                                 )
             )
           #t
           )
       )
      )
    )
  :code (behavior ()
    (let ((gp-0 (new 'stack-no-clear 'explosion-init-params)))
      (vector-copy! (-> gp-0 spawn-point) (-> self root trans))
      (quaternion-identity! (-> gp-0 spawn-quat))
      (set! (-> gp-0 radius) (-> self blast-radius))
      (set! (-> gp-0 scale) 1.0)
      (set! (-> gp-0 group) group-gun-dark-shot-hit)
      (set! (-> gp-0 collide-with)
            (collide-spec backgnd jak crate civilian enemy obstacle vehicle-sphere hit-by-others-list player-list pusher)
            )
      (set! (-> gp-0 damage) 6.0)
      (set! (-> gp-0 damage-scale) 1.0)
      (set! (-> gp-0 vehicle-damage-factor) 1.0)
      (set! (-> gp-0 vehicle-impulse-factor) 1.0)
      (set! (-> gp-0 ignore-proc) (process->handle #f))
      (explosion-spawn gp-0 self)
      )
    (logior! (-> self draw status) (draw-control-status no-draw))
    (let ((v1-17 (-> self root root-prim)))
      (set! (-> v1-17 prim-core collide-as) (collide-spec))
      (set! (-> v1-17 prim-core collide-with) (collide-spec))
      )
    0
    (while (-> self child)
      (suspend)
      )
    (deactivate self)
    )
  )

;; WARN: Return type mismatch object vs none.
(defmethod vehicle-grenade-method-44 ((this vehicle-grenade))
  (when *traffic-manager*
    (let ((v1-1 (new 'stack-no-clear 'traffic-danger-info)))
      (set! (-> v1-1 sphere quad) (-> this root trans quad))
      (set! (-> v1-1 sphere r) (-> this blast-radius))
      (vector-copy! (-> v1-1 velocity) (-> this root transv))
      (set! (-> v1-1 notify-radius) 122880.0)
      (set! (-> v1-1 danger-level) 2.0)
      (set! (-> v1-1 decay-rate) 0.0)
      (set! (-> v1-1 flags) (the-as uint 1))
      (set! (-> v1-1 danger-type) (the-as uint 6))
      (set! (-> v1-1 handle) (the-as handle #f))
      (send-event *traffic-manager* 'add-danger-sphere v1-1)
      )
    )
  (none)
  )

(defstate sitting (vehicle-grenade)
  :virtual #t
  :enter (behavior ()
    (set-time! (-> self state-time))
    (vehicle-grenade-method-44 self)
    )
  :code (behavior ()
    (until #f
      (when (time-elapsed? (-> self state-time) (seconds 0.5))
        (set-time! (-> self state-time))
        (vehicle-grenade-method-44 self)
        )
      (suspend)
      )
    #f
    (when *traffic-manager*
      (let ((v1-10 (new 'stack-no-clear 'traffic-danger-info)))
        (set! (-> v1-10 sphere quad) (-> self root trans quad))
        (set! (-> v1-10 sphere r) (the-as float (* (the int (-> self blast-radius)) 2)))
        (vector-copy! (-> v1-10 velocity) (-> self root transv))
        (set! (-> v1-10 notify-radius) 122880.0)
        (set! (-> v1-10 danger-level) 2.0)
        (set! (-> v1-10 decay-rate) 0.0)
        (set! (-> v1-10 flags) (the-as uint 1))
        (set! (-> v1-10 danger-type) (the-as uint 6))
        (set! (-> v1-10 handle) (the-as handle #f))
        (send-event *traffic-manager* 'add-danger-sphere v1-10)
        )
      )
    )
  )

(deftype vehicle-grenade-deflect (vehicle-grenade)
  ((has-been-deflected  symbol)
   (has-hit-ground      symbol)
   )
  )


;; WARN: Return type mismatch symbol vs none.
(defmethod init-proj-settings! ((this vehicle-grenade-deflect))
  (set! (-> this has-been-deflected) #f)
  (let ((t9-0 (method-of-type vehicle-grenade init-proj-settings!)))
    (t9-0 this)
    )
  (set! (-> this has-hit-ground) #f)
  (none)
  )

(defstate moving (vehicle-grenade-deflect)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type vehicle-grenade moving) enter)))
      (if t9-0
          (t9-0)
          )
      )
    )
  :exit (behavior ()
    (let ((t9-0 (-> (method-of-type vehicle-grenade moving) exit)))
      (if t9-0
          (t9-0)
          )
      )
    )
  :trans (behavior ()
    (let ((t9-0 (-> (method-of-type vehicle-grenade moving) trans)))
      (if t9-0
          (t9-0)
          )
      )
    )
  :code (behavior ()
    (let ((t9-0 (-> (method-of-type vehicle-grenade moving) code)))
      (if t9-0
          ((the-as (function none) t9-0))
          )
      )
    )
  :post (behavior ()
    (when (logtest? (-> self root status) (collide-status on-ground))
      (set! (-> self has-hit-ground) #t)
      (if (= (-> self charge-level) 0.0)
          (go-virtual impact)
          )
      )
    (let ((t9-1 (-> (method-of-type vehicle-grenade moving) post)))
      (if t9-1
          ((the-as (function none) t9-1))
          )
      )
    )
  )

(defmethod proj-event-handler ((this vehicle-grenade-deflect) (arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
  (case arg2
    (('attack)
     (format #t "attack")
     (when (and (!= (-> this owner-handle) arg0)
                (not (-> this has-been-deflected))
                (logtest? (get-penetrate-using-from-attack-event (the-as process-drawable arg0) arg3) (penetrate spin))
                (-> this next-state)
                (let ((v1-7 (-> this next-state name)))
                  (or (= v1-7 'moving) (= v1-7 'sitting))
                  )
                )
       (let ((s4-2
               (vector-!
                 (new 'stack-no-clear 'vector)
                 (get-trans (the-as process-focusable (as-type (handle->process (-> this owner-handle)) process-focusable)) 0)
                 (-> this root trans)
                 )
               )
             (s3-1 (new 'stack-no-clear 'vector))
             )
         (set! (-> s3-1 quad) (the-as uint128 0))
         (set! (-> s4-2 y) 0.0)
         (let ((f28-0 1274.3112)
               (f30-0 245760.0)
               )
           (vector-normalize! s4-2 1.0)
           (when (-> this has-hit-ground)
             (set! f28-0 3276.8)
             (set! f30-0 204800.0)
             )
           (let ((a0-16 (as-type arg0 process-focusable)))
             0.0
             (let ((f26-0 -0.3)
                   (v1-22 a0-16)
                   )
               (when (= (-> a0-16 type) target)
                 (if (-> this has-hit-ground)
                     (set! f26-0 0.7)
                     )
                 (vector-copy! s3-1 (-> (the-as target v1-22) control to-target-pt-xz))
                 (vector-normalize! s3-1 1.0)
                 (set! (-> s3-1 y) 0.0)
                 (if (< (vector-dot s3-1 s4-2) f26-0)
                     (vector-copy! s4-2 s3-1)
                     )
                 )
               )
             )
           (set! (-> s4-2 y) (tan f28-0))
           (vector-normalize! s4-2 1.0)
           (let ((v1-29 (-> s4-2 quad)))
             (set! (-> this root transv quad) v1-29)
             )
           (vector-normalize! (-> this root transv) f30-0)
           )
         )
       (set! (-> this has-been-deflected) #t)
       (set! (-> this owner-handle) (process->handle arg0))
       (set! (-> this ignore-handle) (-> this owner-handle))
       (if (and (-> this next-state) (= (-> this next-state name) 'sitting))
           (go (method-of-object this moving))
           )
       )
     )
    (('touched 'touch)
     (if (or (not (-> this has-been-deflected)) (!= (process->handle arg0) (-> this owner-handle)))
         ((method-of-type projectile proj-event-handler) this arg0 arg1 arg2 arg3)
         )
     )
    (else
      ((method-of-type projectile proj-event-handler) this arg0 arg1 arg2 arg3)
      )
    )
  )

(set! (-> *lightning-spec-id-table* 19) (new 'static 'lightning-spec
                                          :name "lightning-trail-crimson-guard"
                                          :flags (lightning-spec-flags lsf1)
                                          :rand-func #x2
                                          :start-color (new 'static 'rgba :r #x80 :g #x80 :b #x80 :a #x80)
                                          :fade-to-color (new 'static 'rgba :r #xbf :b #x8f :a #x5)
                                          :fade-start-factor 0.2
                                          :fade-time 120.0
                                          :texture (new 'static 'texture-id :index #x3f :page #x4)
                                          :reduction 0.6
                                          :num-points 16
                                          :box-size 2048.0
                                          :merge-factor 0.5
                                          :merge-count 2
                                          :radius 1638.4
                                          :duration -1.0
                                          :sound #f
                                          )
      )

(set! (-> *lightning-spec-id-table* 20) (new 'static 'lightning-spec
                                          :name "lightning-fractal-crimson-guard"
                                          :flags (lightning-spec-flags lsf1)
                                          :start-color (new 'static 'rgba :r #x80 :g #x80 :b #x80 :a #x80)
                                          :fade-to-color (new 'static 'rgba :r #xbf :b #x8f :a #x5)
                                          :fade-start-factor 0.2
                                          :fade-time 120.0
                                          :texture (new 'static 'texture-id :index #x3f :page #x4)
                                          :reduction 0.6
                                          :num-points 16
                                          :box-size 2048.0
                                          :merge-factor 0.5
                                          :merge-count 2
                                          :radius 1638.4
                                          :duration -1.0
                                          :sound #f
                                          )
      )

(defpart 1163
  :init-specs ((:texture (glow level-default-sprite))
    (:num 1.0)
    (:scale-x (meters 0.8) (meters 0.2))
    (:rot-x (degrees 2.25))
    (:rot-z (degrees -17) (degrees 360))
    (:scale-y :copy scale-x)
    (:r 32.0 96.0)
    (:g :copy r)
    (:b 255.0)
    (:a 32.0 16.0)
    (:omega (degrees 2265.75))
    (:timer (seconds 0.017))
    (:flags (sp-cpuinfo-flag-2 sp-cpuinfo-flag-3 glow))
    (:userdata 819.2)
    )
  )

(defpart 1164
  :init-specs ((:texture (glow level-default-sprite))
    (:num 1.0)
    (:scale-x (meters 2.5) (meters 1))
    (:rot-x (degrees 2.25))
    (:rot-z (degrees -17))
    (:scale-y :copy scale-x)
    (:r 0.0 64.0)
    (:g :copy r)
    (:b 255.0)
    (:a 16.0 16.0)
    (:omega (degrees 2265.75))
    (:timer (seconds 0.017))
    (:flags (sp-cpuinfo-flag-2 sp-cpuinfo-flag-3 glow))
    (:userdata 819.2)
    )
  )

(defpartgroup part-crimson-lightning-glow
  :id 254
  :duration (seconds 0.017)
  :linger-duration (seconds 0.035)
  :bounds (static-bspherem 0 0 0 4)
  :parts ((sp-item 1164 :fade-after (meters 100) :flags (sp6)) (sp-item 1163 :fade-after (meters 100) :flags (sp6)))
  )

(deftype guard-lazer-shot (projectile)
  ()
  )


(defstate dissipate (guard-lazer-shot)
  :virtual #t
  :enter (behavior ()
    (go-virtual impact)
    )
  )

(defmethod made-impact? ((this guard-lazer-shot))
  (let ((gp-0 (-> this root))
        (s5-0 (new 'stack-no-clear 'collide-query))
        )
    (let ((v1-0 s5-0))
      (set! (-> v1-0 radius) (-> gp-0 root-prim prim-core world-sphere w))
      (set! (-> v1-0 collide-with) (-> gp-0 root-prim prim-core collide-with))
      (set! (-> v1-0 ignore-process0) this)
      (set! (-> v1-0 ignore-process1) (ppointer->process (-> this parent)))
      (set! (-> v1-0 ignore-pat)
            (new 'static 'pat-surface :noentity #x1 :nojak #x1 :probe #x1 :noendlessfall #x1 :board #x1)
            )
      (set! (-> v1-0 action-mask) (collide-action solid))
      )
    (let ((a0-2 (the-as process-focusable (handle->process (-> this notify-handle)))))
      (when a0-2
        (let* ((s4-1 (vector-! (new 'stack-no-clear 'vector) (-> gp-0 trans) (get-trans a0-2 3)))
               (f0-2 (- (vector-length s4-1)))
               )
          (fill-and-try-snap-to-surface gp-0 s4-1 f0-2 0.0 -3072.0 s5-0)
          )
        )
      )
    )
  )

(defun guard-lazer-shot-move ((arg0 guard-lazer-shot))
  (projectile-move-fill-line-sphere arg0)
  (if (logtest? (-> arg0 root status) (collide-status touch-surface))
      (go (method-of-object arg0 impact))
      )
  0
  (none)
  )

(defmethod setup-collision! ((this guard-lazer-shot))
  (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) cshape-reaction-default)
    (set! (-> s5-0 no-reaction)
          (the-as (function collide-shape-moving collide-query vector vector object) nothing)
          )
    (set! (-> s5-0 penetrate-using) (penetrate enemy-yellow-shot))
    (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
              civilian
              enemy
              obstacle
              vehicle-sphere
              hit-by-others-list
              player-list
              pusher
              shield
              )
            )
      (set! (-> v1-7 prim-core action) (collide-action solid))
      (set-vector! (-> v1-7 local-sphere) 0.0 0.0 0.0 1228.8)
      (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 :board #x1)
        )
  0
  (none)
  )

(defmethod init-proj-settings! ((this guard-lazer-shot))
  (set! (-> this attack-mode) 'shock)
  (set! (-> this max-speed) 131072.0)
  (set! (-> this timeout) (seconds 0.125))
  (set! (-> this move) guard-lazer-shot-move)
  (set! (-> this damage) 3.0)
  (set! (-> this root dynam gravity y) 0.0)
  (set! (-> this root dynam gravity-length) 0.0)
  (set! (-> this root dynam gravity-max) 0.0)
  (set! (-> this vehicle-damage-factor) 0.5)
  (logior! (-> this options) (projectile-options po13))
  0
  (none)
  )

(defmethod deal-damage! ((this guard-lazer-shot) (arg0 process) (arg1 event-message-block))
  (if (logtest? (process-mask kg-robot) (-> arg0 mask))
      (set! (-> this damage) 5.0)
      )
  (call-parent-method this arg0 arg1)
  )
