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

(import "goal_src/jak1/import/snow-bumper-ag.gc")

;; definition of type snow-bumper
(deftype snow-bumper (process-drawable)
  ((bumper-id               int32                     :offset-assert 176)
   (base-shove-ry           float                     :offset-assert 180)
   (max-shove-diff-ry       float                     :offset-assert 184)
   (part2                   sparticle-launch-control  :offset-assert 188)
   (last-shoved-player-time time-frame                :offset-assert 192)
   )
  :heap-base #x60
  :method-count-assert 22
  :size-assert         #xc8
  :flag-assert         #x16006000c8
  (:methods
    (dummy-20 (_type_) none 20)
    (shove-player (_type_ process-drawable) none 21)
    )
  (:states
    snow-bumper-active-close-idle
    snow-bumper-active-far-idle
    snow-bumper-deactivate
    snow-bumper-inactive-idle
    snow-bumper-spawn-fuel-cell
    )
  )

;; definition for method 3 of type snow-bumper
(defmethod inspect snow-bumper ((obj snow-bumper))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tbumper-id: ~D~%" (-> obj bumper-id))
  (format #t "~T~Tbase-shove-ry: ~f~%" (-> obj base-shove-ry))
  (format #t "~T~Tmax-shove-diff-ry: ~f~%" (-> obj max-shove-diff-ry))
  (format #t "~T~Tpart2: ~A~%" (-> obj part2))
  (format #t "~T~Tlast-shoved-player-time: ~D~%" (-> obj last-shoved-player-time))
  obj
  )

;; failed to figure out what this is:
(defskelgroup *snow-bumper-sg* snow-bumper snow-bumper-lod0-jg -1
              ((snow-bumper-lod0-mg (meters 20)) (snow-bumper-lod1-mg (meters 999999)))
              :bounds (static-spherem 0 2.2 0 3.4)
              )

;; failed to figure out what this is:
(defpartgroup group-snow-bumper-idle
  :id 519
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 8)
  :parts ((sp-item 1893 :fade-after (meters 90) :falloff-to (meters 90) :period 25 :length 10))
  )

;; failed to figure out what this is:
(defpart 1893
  :init-specs ((sp-tex spt-texture (new 'static 'texture-id :index #xf :page #x2))
    (sp-flt spt-num 15.0)
    (sp-flt spt-y (meters 1.5))
    (sp-flt spt-z (meters 0))
    (sp-flt spt-scale-x (meters 1.5))
    (sp-flt spt-scale-y (meters 6))
    (sp-flt spt-r 0.0)
    (sp-flt spt-g 64.0)
    (sp-rnd-flt spt-b 64.0 32.0 1.0)
    (sp-rnd-flt spt-a 32.0 64.0 1.0)
    (sp-flt spt-vel-y (meters 0.013333334))
    (sp-flt spt-scalevel-x (meters 0.0038461538))
    (sp-flt spt-scalevel-y (meters -0.03076923))
    (sp-flt spt-fade-a 0.0)
    (sp-int spt-timer 25)
    (sp-cpuinfo-flags bit2 bit3 bit14)
    (sp-flt spt-conerot-x (degrees 90.0))
    (sp-rnd-flt spt-conerot-y (degrees 0.0) (degrees 360.0) 1.0)
    (sp-flt spt-conerot-radius (meters 5))
    (sp-rnd-flt spt-rotate-y (degrees 0.0) (degrees 360.0) 1.0)
    )
  )

;; failed to figure out what this is:
(defpartgroup group-snow-bumper-shove
  :id 520
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 8)
  :parts ((sp-item 1895))
  )

;; failed to figure out what this is:
(defpart 1895
  :init-specs ((sp-tex spt-texture (new 'static 'texture-id :index #xf :page #x2))
    (sp-flt spt-num 16.0)
    (sp-flt spt-x (meters 5.5))
    (sp-flt spt-y (meters 1.5))
    (sp-flt spt-z (meters 0))
    (sp-flt spt-scale-x (meters 2))
    (sp-flt spt-scale-y (meters 6))
    (sp-rnd-flt spt-r 64.0 32.0 1.0)
    (sp-flt spt-g 64.0)
    (sp-flt spt-b 0.0)
    (sp-flt spt-a 64.0)
    (sp-flt spt-vel-x (meters 0.033333335))
    (sp-flt spt-scalevel-x (meters 0.008333334))
    (sp-flt spt-scalevel-y (meters 0.2))
    (sp-flt spt-fade-a 0.0)
    (sp-int spt-timer 25)
    (sp-cpuinfo-flags bit2 bit3 bit14)
    (sp-rnd-flt spt-rotate-y (degrees 0.0) (degrees 45.0) 1.0)
    )
  )

;; definition for method 21 of type snow-bumper
;; INFO: Used lq/sq
;; INFO: Return type mismatch time-frame vs none.
(defmethod shove-player snow-bumper ((obj snow-bumper) (arg0 process-drawable))
  (let ((s5-0 (new 'stack-no-clear 'vector)))
    (vector-! s5-0 (-> arg0 root trans) (-> obj root trans))
    (set! (-> s5-0 y) 0.0)
    (vector-normalize! s5-0 1.0)
    (let* ((f0-3 (atan (-> s5-0 x) (-> s5-0 z)))
           (f30-0 (-> obj base-shove-ry))
           (f28-0 (-> obj max-shove-diff-ry))
           (f0-4 (deg- f0-3 f30-0))
           )
      (when (< f28-0 (fabs f0-4))
        (let ((f30-1 (if (>= f0-4 0.0)
                         (+ f30-0 f28-0)
                         (- f30-0 f28-0)
                         )
                     )
              )
          (set-vector! s5-0 (sin f30-1) 0.0 (cos f30-1) 1.0)
          )
        )
      )
    (let ((f0-12 (+ -16384.0 (atan (-> s5-0 x) (-> s5-0 z)))))
      (set! (-> *part-id-table* 1895 init-specs 17 initial-valuef) (+ -4096.0 f0-12))
      )
    (spawn (-> obj part2) (-> obj root trans))
    (let ((s3-1 (new 'stack-no-clear 'vector)))
      (vector-normalize-copy! s3-1 s5-0 32768.0)
      (vector+! s3-1 s3-1 (-> obj root trans))
      (vector-! s5-0 s3-1 (-> arg0 root trans))
      )
    (let ((f30-3 (vector-xz-length s5-0)))
      (vector-normalize! s5-0 1.0)
      (if (send-event *target* 'shove #f (static-attack-info ((vector s5-0) (shove-up (meters 1)) (shove-back f30-3))))
          (set! (-> obj last-shoved-player-time) (-> *display* base-frame-counter))
          )
      )
    )
  (none)
  )

;; failed to figure out what this is:
(defstate snow-bumper-active-far-idle (snow-bumper)
  :trans (behavior ()
    (if (and *target* (>= 135895450000.0 (vector-vector-xz-distance-squared (-> self root trans) (target-pos 0))))
        (go snow-bumper-active-close-idle)
        )
    0
    (none)
    )
  :code (behavior ()
    (transform-post)
    (suspend)
    (loop
      (logior! (-> self mask) (process-mask sleep-code))
      (suspend)
      )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate snow-bumper-active-close-idle (snow-bumper)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (case arg2
      (('touch 'attack 'bonk)
       (when (= (-> arg0 type) target)
         (let ((gp-0 (-> arg3 param 0)))
           (when (and (>= 6144.0 (vector-vector-xz-distance (-> self root trans) (target-pos 0)))
                      ((method-of-type touching-shapes-entry prims-touching?)
                       (the-as touching-shapes-entry gp-0)
                       (the-as collide-shape-moving (-> self root))
                       (the-as uint 1)
                       )
                      )
             (close-specific-task! (game-task snow-bumpers) (task-status need-hint))
             (go snow-bumper-deactivate)
             )
           )
         )
       )
      )
    )
  :trans (behavior ()
    (cond
      (*target*
        (let* ((gp-0 (target-pos 0))
               (f30-0 (vector-vector-xz-distance (-> self root trans) gp-0))
               (f28-0 (- (-> gp-0 y) (-> self root trans y)))
               )
          (if (< 368640.0 f30-0)
              (go snow-bumper-active-far-idle)
              )
          (if (and (>= (- (-> *display* base-frame-counter) (-> self last-shoved-player-time)) (seconds 0.5))
                   (>= 20480.0 f30-0)
                   (>= f28-0 -4096.0)
                   (>= 11059.2 f28-0)
                   )
              (shove-player self *target*)
              )
          )
        )
      (else
        (go snow-bumper-active-far-idle)
        )
      )
    (spawn (-> self part) (-> self root trans))
    (update! (-> self sound))
    (none)
    )
  :code (behavior ()
    (transform-post)
    (suspend)
    (loop
      (logior! (-> self mask) (process-mask sleep-code))
      (suspend)
      )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate snow-bumper-deactivate (snow-bumper)
  :exit (behavior ()
    (stop! (-> self sound))
    (logior! (-> self mask) (process-mask actor-pause))
    (none)
    )
  :trans (the-as (function none :behavior snow-bumper) rider-trans)
  :code (behavior ()
    (local-vars (sv-16 symbol))
    (logclear! (-> self mask) (process-mask actor-pause))
    (sound-play "bumper-pwr-dwn")
    (ja-no-eval :group! snow-bumper-button-ja :num! (seek! max 0.05) :frame-num 0.0)
    (until (ja-done? 0)
      (spawn (-> self part) (-> self root trans))
      (update! (-> self sound))
      (suspend)
      (ja :num! (seek! max 0.05))
      )
    (stop! (-> self sound))
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (until (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 0.5))
      (suspend)
      )
    (ja-no-eval :group! snow-bumper-collapse-ja :num! (seek! max 0.02) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek! max 0.02))
      )
    (process-entity-status! self (entity-perm-status complete) #t)
    (set! sv-16 (the-as symbol #f))
    (apply-all (-> self link) actor-link-subtask-complete-hook (& sv-16))
    (when sv-16
      (save-reminder (get-task-control (game-task snow-bumpers)) (-> self bumper-id) 0)
      (go snow-bumper-spawn-fuel-cell)
      )
    (go snow-bumper-inactive-idle)
    (none)
    )
  :post (the-as (function none :behavior snow-bumper) rider-post)
  )

;; failed to figure out what this is:
(defstate snow-bumper-spawn-fuel-cell (snow-bumper)
  :code (behavior ()
    (ja-channel-set! 1)
    (ja :group! snow-bumper-collapse-ja :num! max)
    (transform-post)
    (if (not (task-complete? *game-info* (-> self entity extra perm task)))
        (birth-pickup-at-point
          (-> self root trans)
          (pickup-type fuel-cell)
          (the float (-> self entity extra perm task))
          #t
          self
          (the-as fact-info #f)
          )
        )
    (until (not (-> self child))
      (suspend)
      )
    (go snow-bumper-inactive-idle)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate snow-bumper-inactive-idle (snow-bumper)
  :code (behavior ()
    (set! (-> self root nav-radius) 6963.2)
    (ja-channel-set! 1)
    (ja :group! snow-bumper-collapse-ja :num! max)
    (transform-post)
    (suspend)
    (logior! (-> self mask) (process-mask sleep-code))
    (suspend)
    0
    (none)
    )
  )

;; definition for method 10 of type snow-bumper
(defmethod deactivate snow-bumper ((obj snow-bumper))
  (if (nonzero? (-> obj part2))
      (kill-and-free-particles (-> obj part2))
      )
  ((method-of-type process-drawable deactivate) obj)
  (none)
  )

;; definition for method 7 of type snow-bumper
;; INFO: Return type mismatch process-drawable vs snow-bumper.
(defmethod relocate snow-bumper ((obj snow-bumper) (arg0 int))
  (if (nonzero? (-> obj part2))
      (&+! (-> obj part2) arg0)
      )
  (the-as
    snow-bumper
    ((the-as (function process-drawable int process-drawable) (find-parent-method snow-bumper 7)) obj arg0)
    )
  )

;; definition for method 11 of type snow-bumper
;; INFO: Used lq/sq
;; INFO: Return type mismatch object vs none.
(defmethod init-from-entity! snow-bumper ((obj snow-bumper) (arg0 entity-actor))
  (local-vars (sv-16 res-tag))
  (set! (-> obj last-shoved-player-time) 0)
  (let ((s4-0 (new 'process 'collide-shape-moving obj (collide-list-enum hit-by-player))))
    (set! (-> s4-0 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s4-0 reaction) default-collision-reaction)
    (set! (-> s4-0 no-reaction)
          (the-as (function collide-shape-moving collide-shape-intersect vector vector none) nothing)
          )
    (let ((s3-0 (new 'process 'collide-shape-prim-group s4-0 (the-as uint 2) 0)))
      (set! (-> s3-0 prim-core collide-as) (collide-kind ground-object))
      (set! (-> s3-0 collide-with) (collide-kind target))
      (set! (-> s3-0 prim-core action) (collide-action solid ca-1))
      (set-vector! (-> s3-0 local-sphere) 0.0 9011.2 0.0 13926.4)
      (set-root-prim! s4-0 s3-0)
      (let ((s2-0 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 0) (the-as uint 1))))
        (set! (-> s2-0 prim-core collide-as) (collide-kind ground-object))
        (set! (-> s2-0 collide-with) (collide-kind target))
        (set! (-> s2-0 prim-core action) (collide-action solid ca-1))
        (set! (-> s2-0 prim-core offense) (collide-offense indestructible))
        (set! (-> s2-0 transform-index) 6)
        (set-vector! (-> s2-0 local-sphere) 0.0 0.0 0.0 4915.2)
        (append-prim s3-0 s2-0)
        )
      (let ((s2-1 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 1) (the-as uint 2))))
        (set! (-> s2-1 prim-core collide-as) (collide-kind wall-object))
        (set! (-> s2-1 collide-with) (collide-kind target))
        (set! (-> s2-1 prim-core action) (collide-action solid))
        (set! (-> s2-1 prim-core offense) (collide-offense indestructible))
        (set! (-> s2-1 transform-index) 3)
        (set-vector! (-> s2-1 local-sphere) 0.0 8192.0 0.0 11878.4)
        (append-prim s3-0 s2-1)
        )
      )
    (set! (-> s4-0 nav-radius) 20480.0)
    (backup-collide-with-as s4-0)
    (set! (-> obj root) s4-0)
    )
  (process-drawable-from-entity! obj arg0)
  (initialize-skeleton obj *snow-bumper-sg* '())
  (set! (-> obj part) (create-launch-control (-> *part-group-id-table* 519) obj))
  (set! (-> obj part2) (create-launch-control (-> *part-group-id-table* 520) obj))
  (nav-mesh-connect obj (-> obj root) (the-as nav-control #f))
  (set! (-> obj link) (new 'process 'actor-link-info obj))
  (set! (-> obj bumper-id) (+ (actor-count-before (-> obj link)) 1))
  (set! (-> obj sound)
        (new 'process 'ambient-sound (static-sound-spec "snow-bumper" :fo-max 40) (-> obj root trans))
        )
  (ja-channel-set! 1)
  (let ((s5-1 (-> obj skel root-channel 0)))
    (joint-control-channel-group-eval!
      s5-1
      (the-as art-joint-anim (-> obj draw art-group data 3))
      num-func-identity
      )
    (set! (-> s5-1 frame-num) 0.0)
    )
  (transform-post)
  (set! (-> obj base-shove-ry) 0.0)
  (set! (-> obj max-shove-diff-ry) 32768.0)
  (set! sv-16 (new 'static 'res-tag))
  (let ((v1-52 (res-lump-data (-> obj entity) 'rotmin (pointer float) :tag-ptr (& sv-16))))
    (when v1-52
      (set! (-> obj base-shove-ry) (-> v1-52 0))
      (set! (-> obj max-shove-diff-ry) (-> v1-52 1))
      )
    )
  (cond
    ((and (-> obj entity) (logtest? (-> obj entity extra perm status) (entity-perm-status complete)))
     (if (and (= (get-reminder (get-task-control (game-task snow-bumpers)) 0) (-> obj bumper-id))
              (not (task-complete? *game-info* (-> obj entity extra perm task)))
              )
         (go snow-bumper-spawn-fuel-cell)
         (go snow-bumper-inactive-idle)
         )
     )
    (else
      (go snow-bumper-active-far-idle)
      )
    )
  (none)
  )
