;;-*-Lisp-*-
(in-package goal)
(bundles "SNO.DGO")
(require "engine/common-obs/generic-obs.gc")
(require "engine/game/task/task-control.gc")

;; DECOMP BEGINS

(deftype snow-bumper (process-drawable)
  ((bumper-id               int32)
   (base-shove-ry           float)
   (max-shove-diff-ry       float)
   (part2                   sparticle-launch-control)
   (last-shoved-player-time time-frame))
  (:methods
   (snow-bumper-method-20 (_type_) none)
   (shove-player (_type_ process-drawable) none))
  (:states
   snow-bumper-active-close-idle
   snow-bumper-active-far-idle
   snow-bumper-deactivate
   snow-bumper-inactive-idle
   snow-bumper-spawn-fuel-cell))


(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))

(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 (seconds 0.085) :length (seconds 0.035))))

(defpart 1893
  :init-specs
  ((:texture (hotdot effects))
   (:num 15.0)
   (:y (meters 1.5))
   (:z (meters 0))
   (:scale-x (meters 1.5))
   (:scale-y (meters 6))
   (:r 0.0)
   (:g 64.0)
   (:b 64.0 32.0)
   (:a 32.0 64.0)
   (:vel-y (meters 0.013333334))
   (:scalevel-x (meters 0.0038461538))
   (:scalevel-y (meters -0.03076923))
   (:fade-a 0.0)
   (:timer (seconds 0.085))
   (:flags (bit2 bit3 bit14))
   (:conerot-x (degrees 90))
   (:conerot-y (degrees 0) (degrees 360))
   (:conerot-radius (meters 5))
   (:rotate-y (degrees 0) (degrees 360))))

(defpartgroup group-snow-bumper-shove
  :id 520
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 8)
  :parts ((sp-item 1895)))

(defpart 1895
  :init-specs
  ((:texture (hotdot effects))
   (:num 16.0)
   (:x (meters 5.5))
   (:y (meters 1.5))
   (:z (meters 0))
   (:scale-x (meters 2))
   (:scale-y (meters 6))
   (:r 64.0 32.0)
   (:g 64.0)
   (:b 0.0)
   (:a 64.0)
   (:vel-x (meters 0.033333335))
   (:scalevel-x (meters 0.008333334))
   (:scalevel-y (meters 0.2))
   (:fade-a 0.0)
   (:timer (seconds 0.085))
   (:flags (bit2 bit3 bit14))
   (:rotate-y (degrees 0) (degrees 45))))

(defmethod shove-player ((this snow-bumper) (arg0 process-drawable))
  (let ((s5-0 (new 'stack-no-clear 'vector)))
    (vector-! s5-0 (-> arg0 root trans) (-> this 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 (-> this base-shove-ry))
           (f28-0 (-> this 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 (-> this part2) (-> this root trans))
    (let ((s3-1 (new 'stack-no-clear 'vector)))
      (vector-normalize-copy! s3-1 s5-0 32768.0)
      (vector+! s3-1 s3-1 (-> this 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-time! (-> this last-shoved-player-time)))))
  (none))

(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)
  :code
    (behavior ()
      (transform-post)
      (suspend)
      (loop
        (logior! (-> self mask) (process-mask sleep-code))
        (suspend))))

(defstate snow-bumper-active-close-idle (snow-bumper)
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (case message
        (('touch 'attack 'bonk)
         (when (= (-> proc type) target)
           (let ((gp-0 (-> block 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 (time-elapsed? (-> 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)))
  :code
    (behavior ()
      (transform-post)
      (suspend)
      (loop
        (logior! (-> self mask) (process-mask sleep-code))
        (suspend))))

(defstate snow-bumper-deactivate (snow-bumper)
  :exit
    (behavior ()
      (stop! (-> self sound))
      (logior! (-> self mask) (process-mask actor-pause)))
  :trans 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-time! (-> self state-time))
      (until (time-elapsed? (-> 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))
  :post rider-post)

(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)))

(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))

(defmethod deactivate ((this snow-bumper))
  (if (nonzero? (-> this part2)) (kill-and-free-particles (-> this part2)))
  ((method-of-type process-drawable deactivate) this)
  (none))

(defmethod relocate ((this snow-bumper) (offset int))
  (if (nonzero? (-> this part2)) (&+! (-> this part2) offset))
  (call-parent-method this offset))

(defmethod init-from-entity! ((this snow-bumper) (arg0 entity-actor))
  (local-vars (sv-16 res-tag))
  (set! (-> this last-shoved-player-time) 0)
  (let ((s4-0 (new 'process 'collide-shape-moving this (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 rider-plat-sticky))
      (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 rider-plat-sticky))
        (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! (-> this root) s4-0))
  (process-drawable-from-entity! this arg0)
  (initialize-skeleton this *snow-bumper-sg* '())
  (set! (-> this part) (create-launch-control (-> *part-group-id-table* 519) this))
  (set! (-> this part2) (create-launch-control (-> *part-group-id-table* 520) this))
  (nav-mesh-connect this (-> this root) (the-as nav-control #f))
  (set! (-> this link) (new 'process 'actor-link-info this))
  (set! (-> this bumper-id) (+ (actor-count-before (-> this link)) 1))
  (set! (-> this sound) (new 'process 'ambient-sound (static-sound-spec "snow-bumper" :fo-max 40) (-> this root trans)))
  (ja-channel-set! 1)
  (let ((s5-1 (-> this skel root-channel 0)))
    (joint-control-channel-group-eval! s5-1 (the-as art-joint-anim (-> this draw art-group data 3)) num-func-identity)
    (set! (-> s5-1 frame-num) 0.0))
  (transform-post)
  (set! (-> this base-shove-ry) 0.0)
  (set! (-> this max-shove-diff-ry) 32768.0)
  (set! sv-16 (new 'static 'res-tag))
  (let ((v1-52 (res-lump-data (-> this entity) 'rotmin (pointer float) :tag-ptr (& sv-16))))
    (when v1-52
      (set! (-> this base-shove-ry) (-> v1-52 0))
      (set! (-> this max-shove-diff-ry) (-> v1-52 1))))
  (cond
    ((and (-> this entity) (logtest? (-> this entity extra perm status) (entity-perm-status complete)))
     (if (and (= (get-reminder (get-task-control (game-task snow-bumpers)) 0) (-> this bumper-id))
              (not (task-complete? *game-info* (-> this entity extra perm task))))
       (go snow-bumper-spawn-fuel-cell)
       (go snow-bumper-inactive-idle)))
    (else (go snow-bumper-active-far-idle)))
  (none))
