;;-*-Lisp-*-
(in-package goal)
(bundles "SUN.DGO")
(require "engine/common-obs/generic-obs.gc")
(require "engine/util/sync-info.gc")

;; DECOMP BEGINS

(deftype whirlpool (process-drawable)
  ((root             collide-shape :override)
   (spin-ry          float)
   (spin-speed-idle  float)
   (spin-speed-delta float)
   (sync             sync-info-paused :inline))
  (:methods
   (whirlpool-method-20 (_type_ float) cshape-moving-flags))
  (:states
   whirlpool-idle))


(defskelgroup *whirlpool-sg*
  whirlpool
  whirlpool-lod0-jg
  whirlpool-idle-ja
  ((whirlpool-lod0-mg (meters 999999)))
  :bounds (static-spherem 0 0.6 0 3))

(defpartgroup group-whirlpool-swirl
  :id 447
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 8)
  :parts
  ((sp-item 1739 :fade-after (meters 60) :falloff-to (meters 60) :binding 1737)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1737 :flags (bit1 start-dead launch-asap) :binding 1738)
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))
   (sp-item 1738 :flags (bit1 start-dead))))

(defpart 1739
  :init-specs
  ((:texture (lakedrop effects))
   (:num 1.0)
   (:y (meters 6.6))
   (:scale-x (meters 0.2))
   (:scale-y :copy scale-x)
   (:g 0.0)
   (:a 128.0)
   (:accel-y (meters -0.00016666666))
   (:timer (seconds 1.7))
   (:flags (bit2 bit3))))

(defpart 1737
  :init-specs
  ((:texture (bigpuff effects))
   (:num 1.0)
   (:x (meters 0))
   (:y (meters 0))
   (:z (meters 4) (meters 1))
   (:scale-x (meters 3.2))
   (:scale-y (meters 1))
   (:r 150.0)
   (:g 150.0)
   (:b 255.0)
   (:a 0.0)
   (:omega 0.0 65536.0)
   (:vel-x (meters 0.053333335))
   (:vel-y (meters 0))
   (:vel-z (meters -0.01))
   (:scalevel-x (meters -0.004888889))
   (:fade-a 0.42666668)
   (:timer (seconds 1.5))
   (:flags (bit2 bit3 bit7))
   (:next-time (seconds 0.125) (seconds 0.12))
   (:next-launcher 1740)))

(defpart 1740
  :init-specs ((:fade-a 0.0)))

(defpart 1738
  :init-specs
  ((:texture (lakedrop effects))
   (:num 1.0)
   (:scale-x (meters 0.3) (meters 0.1))
   (:scale-y :copy scale-x)
   (:r 255.0)
   (:g 255.0)
   (:b 255.0)
   (:a 32.0 32.0)
   (:fade-a -1.8285716)
   (:timer (seconds 0.1))
   (:flags (bit2 bit3))))

(defmethod whirlpool-method-20 ((this whirlpool) (arg0 float))
  (let* ((gp-0 (target-pos 0))
         (f28-0 (vector-vector-xz-distance (-> this root trans) gp-0)))
    (when (< f28-0 40960.0)
      (let* ((f0-2 (* 0.000024414063 (- 40960.0 f28-0)))
             (f26-0 (* f0-2 f0-2))
             (f0-7 (atan (- (-> gp-0 x) (-> this root trans x)) (- (-> gp-0 z) (-> this root trans z))))
             (f30-0 (* (/ f26-0 2) arg0 (seconds-per-frame)))
             (f24-0 (+ f0-7 f30-0))
             (f28-1 (- f28-0 (fmin f28-0 (* 0.16874999 f26-0 (fabs arg0) (seconds-per-frame)))))
             (s4-1 (new 'stack-no-clear 'vector)))
        (set-vector! s4-1 (* (sin f24-0) f28-1) 0.0 (* (cos f24-0) f28-1) 1.0)
        (vector+! s4-1 s4-1 (-> this root trans))
        (set! (-> s4-1 x) (* (- (-> s4-1 x) (-> gp-0 x)) (-> *display* frames-per-second)))
        (set! (-> s4-1 y) 0.0)
        (set! (-> s4-1 z) (* (- (-> s4-1 z) (-> gp-0 z)) (-> *display* frames-per-second)))
        (let ((gp-1 (-> *target* control)))
          (send-event (-> gp-1 process) 'rotate-y-angle f30-0)
          (fill-cache-for-shape! gp-1 8192.0 (-> gp-1 root-prim collide-with))
          (let ((s3-0 (-> gp-1 status))) (integrate-and-collide! gp-1 s4-1) (set! (-> gp-1 status) s3-0) s3-0))))))

(defstate whirlpool-idle (whirlpool)
  :trans
    (behavior ()
      (let ((f30-0 (+ (* (get-current-phase-with-mirror (-> self sync)) (-> self spin-speed-delta)) (-> self spin-speed-idle))))
        (when (>= (fabs f30-0) 45511.11)
          (let ((a1-0 (new 'stack-no-clear 'vector)))
            (set! (-> a1-0 quad) (-> self root trans quad))
            (+! (-> a1-0 y) -8192.0)
            (spawn (-> self part) a1-0)))
        (+! (-> self spin-ry) (* f30-0 (seconds-per-frame)))
        (quaternion-axis-angle! (-> self root quat) 0.0 1.0 0.0 (-> self spin-ry))
        (if (and *target* (logtest? (-> *target* water flags) (water-flags wt09))) (whirlpool-method-20 self f30-0))
        (update! (-> self sound))
        (let ((f0-9 (fmin 1.0 (* 0.000007608261 (fmax 0.0 (+ -28580.979 (fabs f30-0)))))))
          (update-vol! (-> self sound) (the int (* 100.0 f0-9))))))
  :code anim-loop
  :post ja-post)

(defmethod deactivate ((this whirlpool))
  (if (nonzero? (-> this sound)) (stop! (-> this sound)))
  ((method-of-type process-drawable deactivate) this)
  (none))

(defmethod init-from-entity! ((this whirlpool) (arg0 entity-actor))
  (local-vars (sv-16 res-tag))
  (set! (-> this spin-ry) (rand-vu-float-range 0.0 65536.0))
  (let ((s4-0 (new 'process 'collide-shape this (collide-list-enum hit-by-player))))
    (let ((s3-0 (new 'process 'collide-shape-prim-sphere s4-0 (the-as uint 0))))
      (set! (-> s3-0 prim-core collide-as) (collide-kind wall-object))
      (set! (-> s3-0 collide-with) (collide-kind target))
      (set! (-> s3-0 prim-core action) (collide-action solid))
      (set! (-> s3-0 prim-core offense) (collide-offense indestructible))
      (set-vector! (-> s3-0 local-sphere) 0.0 -819.2 0.0 11468.8)
      (set-root-prim! s4-0 s3-0))
    (set! (-> s4-0 nav-radius) (* 0.75 (-> s4-0 root-prim local-sphere w)))
    (backup-collide-with-as s4-0)
    (set! (-> this root) s4-0))
  (process-drawable-from-entity! this arg0)
  (initialize-skeleton this *whirlpool-sg* '())
  (load-params! (-> this sync) this (the-as uint 1500) 0.0 0.15 0.15)
  (let ((f30-0 32768.0)
        (f28-0 145635.56))
    (set! sv-16 (new 'static 'res-tag))
    (let ((v1-17 (res-lump-data arg0 'speed (pointer float) :tag-ptr (& sv-16))))
      (when v1-17
        (set! f30-0 (-> v1-17 0))
        (set! f28-0 (-> v1-17 1))))
    (set! (-> this spin-speed-idle) f30-0)
    (set! (-> this spin-speed-delta) (- f28-0 f30-0)))
  (set! (-> this part) (create-launch-control (-> *part-group-id-table* 447) this))
  (set! (-> this sound) (new 'process 'ambient-sound (static-sound-spec "whirlpool" :fo-max 55) (-> 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 2)) num-func-identity)
    (set! (-> s5-1 frame-num) 0.0))
  (ja-post)
  (update-transforms! (-> this root))
  (go whirlpool-idle)
  (none))
