;;-*-Lisp-*-
(in-package goal)
(bundles "SUN.DGO")
(require "engine/common-obs/plat.gc")
(require "engine/camera/pov-camera.gc")
(require "engine/game/main.gc")
(declare-type seaweed basic)

(define-extern *seaweed* seaweed)

;; DECOMP BEGINS

(deftype water-vol-deadly (process-hidden) ())


(deftype side-to-side-plat (plat)
  ((part-ry float)))


(defskelgroup *side-to-side-plat-sg*
  side-to-side-plat
  side-to-side-plat-lod0-jg
  side-to-side-plat-idle-ja
  ((side-to-side-plat-lod0-mg (meters 20)) (side-to-side-plat-lod1-mg (meters 999999)))
  :bounds (static-spherem 0 0 0 14))

(defpartgroup group-side-to-side-plat
  :id 436
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 8)
  :parts
  ((sp-item 1713 :fade-after (meters 60) :falloff-to (meters 60))
   (sp-item 1714 :fade-after (meters 160) :falloff-to (meters 160))))

(defpart 1713
  :init-specs
  ((:num 4.0)
   (:x (meters -11) (meters 22))
   (:y (meters 1))
   (:rot-x 5)
   (:r 5324.8)
   (:g 4096.0)
   (:b 3686.4)
   (:vel-y (meters 0.0033333334))
   (:fade-b -16.383999)
   (:timer (seconds 0.085))
   (:flags (aux-list))
   (:conerot-x (degrees 140) (degrees 40))
   (:conerot-y (degrees 0) (degrees 360))
   (:conerot-radius (meters 3))
   (:rotate-y (degrees 18))))

(defpart 1714
  :init-specs
  ((:texture (hotdot effects))
   (:num 6.0)
   (:x (meters -11) (meters 22))
   (:y (meters 1.5))
   (:scale-x (meters 2) (meters 1))
   (:scale-y :copy scale-x)
   (:g 0.0 64.0)
   (:b 128.0 128.0)
   (:a 16.0 32.0)
   (:vel-y (meters 0.01))
   (:fade-g -0.42666668)
   (:fade-a -0.32)
   (:accel-y (meters -0.000033333334))
   (:friction 0.96)
   (:timer (seconds 0.5))
   (:flags (bit2 bit3))
   (:conerot-x (degrees 130) (degrees 100.00001))
   (:conerot-y (degrees 0) (degrees 360))
   (:conerot-radius (meters 3))
   (:rotate-y (degrees 18))))

(defmethod get-unlit-skel ((this side-to-side-plat))
  *side-to-side-plat-sg*)

(defmethod baseplat-method-24 ((this side-to-side-plat))
  (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) default-collision-reaction)
    (set! (-> s5-0 no-reaction) (the-as (function collide-shape-moving collide-shape-intersect vector vector none) nothing))
    (alloc-riders s5-0 1)
    (let ((s4-0 (new 'process 'collide-shape-prim-mesh s5-0 (the-as uint 0) (the-as uint 0))))
      (set! (-> s4-0 prim-core collide-as) (collide-kind ground-object))
      (set! (-> s4-0 collide-with) (collide-kind target))
      (set! (-> s4-0 prim-core action) (collide-action solid rider-plat-sticky))
      (set! (-> s4-0 prim-core offense) (collide-offense indestructible))
      (set! (-> s4-0 transform-index) 0)
      (set-vector! (-> s4-0 local-sphere) 0.0 0.0 0.0 57344.0)
      (set-root-prim! s5-0 s4-0))
    (set! (-> s5-0 nav-radius) (* 0.75 (-> s5-0 root-prim local-sphere w)))
    (backup-collide-with-as s5-0)
    (set! (-> this root) s5-0))
  0
  (none))

(defmethod baseplat-method-26 ((this side-to-side-plat))
  (set! (-> this part-ry) (+ 16384.0 (quaternion-y-angle (-> this root quat))))
  (none))

(defmethod baseplat-method-25 ((this side-to-side-plat))
  (let ((v0-0 (create-launch-control (-> *part-group-id-table* 436) this)))
    (set! (-> this part) v0-0)
    (the-as sparticle-launch-group v0-0)))

(defmethod baseplat-method-20 ((this side-to-side-plat))
  (when (nonzero? (-> this part))
    (set! (-> *part-id-table* 1713 init-specs 14 initial-valuef) (-> this part-ry))
    (set! (-> *part-id-table* 1714 init-specs 19 initial-valuef) (-> this part-ry))
    (spawn (-> this part) (-> this root trans)))
  (none))

(deftype sunkencam (pov-camera)
  ((ppointer-override (pointer sunkencam) :overlay-at ppointer)
   (seq               uint64)))


(defskelgroup *sunkencam-sg*
  sunkencam
  sunkencam-lod0-jg
  -1
  ((sunkencam-lod0-mg (meters 999999)))
  :bounds (static-spherem 0 0 0 10))

(defmethod set-stack-size! ((this sunkencam))
  (stack-size-set! (-> this main-thread) 512)
  (none))

(defstate pov-camera-playing (sunkencam)
  :virtual #t
  :exit
    (behavior ()
      (set! (-> *screen-filter* draw?) #f))
  :code
    (behavior ()
      (let ((v1-0 (-> self seq)))
        (cond
          ((zero? v1-0)
           (ja-no-eval :group! sunkencam-start-door-shut-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!)))
           (ja-no-eval :group! sunkencam-start-in-room-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!)))
           (let ((v1-45 *screen-filter*))
             (set! (-> v1-45 draw?) #t)
             (set! (-> v1-45 color) (new 'static 'rgba :g #x20 :b #x40 :a #x50)))
           (set-blackout-frames (seconds 0.1))
           (ja-no-eval :group! sunkencam-middle-in-water-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!)))
           (set-blackout-frames (seconds 0.5))
           (let ((gp-0 2))
             (ja-no-eval :group! sunkencam-end-out-of-water-ja :num! (seek!) :frame-num 0.0)
             (until (ja-done? 0)
               (+! gp-0 -1)
               (if (zero? gp-0) (set! (-> *screen-filter* draw?) #f))
               (suspend)
               (ja :num! (seek!))))
           (set-blackout-frames (seconds 0.1)))
          ((= v1-0 1)
           (ja-no-eval :group! sunkencam-dive-start-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!)))
           (let ((v1-116 *screen-filter*))
             (set! (-> v1-116 draw?) #t)
             (set! (-> v1-116 color) (new 'static 'rgba :g #x20 :b #x40 :a #x50)))
           (set-blackout-frames (seconds 0.1))
           (ja-no-eval :group! sunkencam-dive-middle-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!)))
           (set-blackout-frames (seconds 0.1))
           (let ((gp-1 2))
             (ja-no-eval :group! sunkencam-dive-end-ja :num! (seek!) :frame-num 0.0)
             (until (ja-done? 0)
               (+! gp-1 -1)
               (if (zero? gp-1) (set! (-> *screen-filter* draw?) #f))
               (suspend)
               (ja :num! (seek!))))
           (let ((gp-2 *camera*))
             (send-event gp-2 'teleport)
             (send-event gp-2 'change-state cam-fixed 0)
             (send-event gp-2 'change-state *camera-base-mode* 0))
           (set-time! (-> self state-time))
           (until (time-elapsed? (-> self state-time) (seconds 1))
             (suspend)))
          ((= v1-0 2)
           (ja-no-eval :group! sunkencam-helix-hit-switch-ja :num! (seek!) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek!)))
           (ja-no-eval :group! sunkencam-helix-door-shuts-ja :num! (seek! max 0.67) :frame-num 0.0)
           (until (ja-done? 0)
             (suspend)
             (ja :num! (seek! max 0.67)))
           (pov-camera-play-and-reposition (the-as art-joint-anim sunkencam-helix-show-rising-water-ja)
                                           (new 'static 'vector :x 2449408.0 :y -1822720.0 :z -7299072.0)
                                           0.67))
          ((= v1-0 3)
           (ambient-hint-spawn "gamcam34" (the-as vector #f) *entity-pool* 'camera)
           (pov-camera-play-and-reposition (the-as art-joint-anim sunkencam-square-platform-setup1-rise1-ja)
                                           (new 'static 'vector :x 2707456.0 :y -630784.0 :z -6635520.0)
                                           1.0))
          ((= v1-0 4)
           (ambient-hint-spawn "gamcam35" (the-as vector #f) *entity-pool* 'camera)
           (pov-camera-play-and-reposition (the-as art-joint-anim sunkencam-square-platform-setup1-rise2-ja)
                                           (new 'static 'vector :x 2834432.0 :y -634880.0 :z -6811648.0)
                                           1.3)
           (set-time! (-> self state-time))
           (until (time-elapsed? (-> self state-time) (seconds 0.2))
             (suspend)))
          ((= v1-0 5)
           (ambient-hint-spawn "gamcam37" (the-as vector #f) *entity-pool* 'camera)
           (pov-camera-play-and-reposition (the-as art-joint-anim sunkencam-square-platform-setup2-rise1-ja)
                                           (new 'static 'vector :x 2695168.0 :y -544768.0 :z -7077888.0)
                                           1.0))
          ((= v1-0 6)
           (ambient-hint-spawn "gamcam36" (the-as vector #f) *entity-pool* 'camera)
           (pov-camera-play-and-reposition (the-as art-joint-anim sunkencam-square-platform-setup2-rise2-ja)
                                           (new 'static 'vector :x 2482176.0 :y -516096.0 :z -6922240.0)
                                           1.0))
          (else (suspend) 0)))
      (go-virtual pov-camera-done-playing)))

(deftype seaweed (process-drawable)
  ((anim-speed float))
  (:states
   seaweed-idle))


(defskelgroup *seaweed-sg*
  seaweed
  seaweed-lod0-jg
  -1
  ((seaweed-lod0-mg (meters 999999)))
  :bounds (static-spherem 0 6 0 7))

(defstate seaweed-idle (seaweed)
  :code
    (behavior ()
      (ja-no-eval :num! (seek! max (-> self anim-speed)))
      (while (not (ja-done? 0))
        (suspend)
        (ja-eval))
      (loop
        (ja-no-eval :group! seaweed-idle-ja :num! (seek! max (-> self anim-speed)) :frame-num 0.0)
        (until (ja-done? 0)
          (suspend)
          (ja :num! (seek! max (-> self anim-speed))))))
  :post ja-post)

(defmethod init-from-entity! ((this seaweed) (arg0 entity-actor))
  (set! (-> this root) (new 'process 'trsqv))
  (process-drawable-from-entity! this arg0)
  (initialize-skeleton this *seaweed-sg* '())
  (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)
          (rand-vu-float-range 0.0
                               (the float
                                    (+ (-> (if (> (-> this skel active-channels) 0) (-> this skel root-channel 0 frame-group)) data 0 length) -1)))))
  (set! (-> this anim-speed) (rand-vu-float-range 0.2 0.4))
  (set! *seaweed* this)
  (go seaweed-idle)
  (none))
