;;-*-Lisp-*-
(in-package goal)
(bundles "GAME.CGO")
(require "engine/common-obs/collectables.gc")
(require "engine/common-obs/baseplat.gc")

;; DECOMP BEGINS

(deftype orb-cache-top (baseplat)
  ((active-distance   float)
   (inactive-distance float)
   (money-list        handle 60)
   (money-pos-list    float 60)
   (money-pos-actual  float 60)
   (platform-pos      float)
   (root-pos          float)
   (money             int32)
   (activated         symbol))
  (:methods
   (pos-logic (_type_ symbol) symbol)
   (calculate-pos (_type_ symbol) none))
  (:states (orb-cache-top-activate symbol)
   (orb-cache-top-complete symbol)
   (orb-cache-top-idle symbol)))

(defskelgroup *orb-cache-top-sg*
  orb-cache-top
  orb-cache-top-lod0-jg
  orb-cache-top-idle-ja
  ((orb-cache-top-lod0-mg (meters 20)) (orb-cache-top-lod1-mg (meters 999999)))
  :bounds (static-spherem 0 0 0 4))

(defstate orb-cache-top-idle (orb-cache-top)
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (case message
        (('eco-blue)
         (process-entity-status! self (entity-perm-status complete) #t)
         (dotimes (gp-0 5)
           (spawn-projectile-blue *target*))
         (increment-success-for-hint (text-id sidekick-hint-orb-cache-top))
         (go orb-cache-top-activate #f))
        (else (plat-event proc argc message block))))
  :trans
    (behavior ()
      (if (and (and *target* (>= 20480.0 (vector-vector-distance (-> self root trans) (-> *target* control trans))))
               (not (send-event *target* 'query 'powerup (pickup-type eco-blue))))
        (level-hint-spawn (text-id sidekick-hint-orb-cache-top) "sksp0009" (the-as entity #f) *entity-pool* (game-task none))))
  :code
    (behavior ((arg0 symbol))
      (if (and (not arg0) (-> self child)) (sound-play "close-orb-cash"))
      (dotimes (gp-1 (-> self money))
        (let ((a0-4 (handle->process (-> self money-list gp-1)))) (if a0-4 (deactivate a0-4))))
      (process-entity-status! self (entity-perm-status complete) #f)
      (ja :group! orb-cache-top-idle-ja :num! (identity (ja-aframe 0.0 0)))
      (transform-post)
      (anim-loop)))

(defmethod baseplat-method-22 ((this orb-cache-top))
  (if (< 4096.0 (- (-> this basetrans y) (-> this root-pos)))
    (activate! (-> this smush) -1.0 60 150 1.0 1.0)
    (activate! (-> this smush) -0.5 60 150 1.0 1.0))
  (set! (-> this bouncing) #t)
  (logclear! (-> this mask) (process-mask sleep))
  0
  (none))

(defmethod calculate-pos ((this orb-cache-top) (arg0 symbol))
  (let ((f0-0 0.0))
    (when arg0
      (set! f0-0 (+ 10240.0 (* 6144.0 (the float (+ (-> this money) -1)))))
      (if (< f0-0 2048.0) (set! f0-0 2048.0)))
    (set! (-> this platform-pos) (+ (-> this root-pos) f0-0))
    (let ((f0-2 (+ -6144.0 f0-0)))
      (dotimes (v1-5 (-> this money))
        (set! (-> this money-pos-list v1-5) (+ (-> this root-pos) f0-2))
        (set! f0-2 (+ -6144.0 f0-2)))))
  0
  (none))

(defmethod pos-logic ((this orb-cache-top) (arg0 symbol))
  (dotimes (s4-0 (-> this money))
    (when (not (handle->process (-> this money-list s4-0)))
      (dotimes (v1-6 (-> this money))
        (when (< s4-0 v1-6)
          (set! (-> this money-list (+ v1-6 -1)) (-> this money-list v1-6))
          (set! (-> this money-pos-actual (+ v1-6 -1)) (-> this money-pos-actual v1-6))))
      (+! (-> this money) -1)
      (calculate-pos this arg0)
      (+! s4-0 -1)
      (let ((v1-15 (-> this entity extra perm)))
        (logior! (-> v1-15 status) (entity-perm-status user-set-from-cstage))
        (+! (-> v1-15 user-int16 0) 1))))
  (let ((s4-1 (new 'stack-no-clear 'vector))
        (s5-1 #t))
    (let ((s3-0 #f))
      (let ((f28-0 (- (-> this basetrans y) (-> this root-pos)))
            (f30-0 (- (-> this platform-pos) (-> this root-pos))))
        (when (zero? (-> this money))
          (set! f30-0 2048.0)
          (set! s3-0 #t))
        (when (and (< f30-0 15155.2)
                   (and *target* (>= 16384.0 (vector-vector-distance (-> this root trans) (-> *target* control trans))))
                   (< (-> (target-pos 0) y) (-> this basetrans y)))
          (set! f30-0 (if (< 14131.2 f28-0) 15155.2 f28-0))
          (set! s3-0 #f))
        (seek! (-> this basetrans y) (+ (-> this root-pos) f30-0) (* 40960.0 (seconds-per-frame)))
        (if (not (= (-> this basetrans y) (+ (-> this root-pos) f30-0))) (set! s5-1 #f)))
      (dotimes (s2-0 (-> this money))
        (set! (-> s4-1 quad) (-> (the-as process-drawable (handle->process (-> this money-list s2-0))) root trans quad))
        (seek! (-> this money-pos-actual s2-0) (-> this money-pos-list s2-0) (* 40960.0 (seconds-per-frame)))
        (if (not (= (-> this money-pos-actual s2-0) (-> this money-pos-list s2-0))) (set! s5-1 #f))
        (if (>= (-> this money-pos-actual s2-0) (+ -8192.0 (-> this root-pos)))
          (set! (-> s4-1 y) (-> this money-pos-actual s2-0))
          (set! (-> s4-1 y) (+ -8192.0 (-> this root-pos))))
        (send-event (handle->process (-> this money-list s2-0)) 'trans s4-1))
      (set! s3-0 (and s5-1 s3-0))
      (if s3-0 (go orb-cache-top-complete #f)))
    s5-1))

(defstate orb-cache-top-activate (orb-cache-top)
  :event plat-event
  :exit
    (behavior ()
      (process-entity-status! self (entity-perm-status bit-3) #f))
  :trans plat-trans
  :code
    (behavior ((arg0 symbol))
      (process-entity-status! self (entity-perm-status bit-3) #t)
      (calculate-pos self arg0)
      (if arg0 (set! (-> self basetrans y) (-> self platform-pos)) (sound-play "open-orb-cash"))
      (dotimes (s5-1 (-> self money))
        (let ((s4-1 (new 'stack-no-clear 'vector)))
          (set! (-> s4-1 quad) (-> self basetrans quad))
          (set! (-> s4-1 y) (-> self money-pos-list s5-1))
          (set! (-> self money-pos-actual s5-1) (-> s4-1 y))
          (set! (-> self money-list s5-1)
                (ppointer->handle (process-spawn money :init money-init-by-other-no-bob s4-1 *null-vector* 5 1.0 (-> self entity) :to self)))))
      (loop
        (calculate-pos self #t)
        (while (not (or (not *target*)
                        (< (-> self inactive-distance) (vector-vector-xz-distance (-> self root trans) (-> *target* control trans)))))
          (pos-logic self #t)
          (suspend))
        (calculate-pos self #f)
        (while (and (not (and (and *target*
                                   (>= (-> self active-distance) (vector-vector-xz-distance (-> self root trans) (-> *target* control trans))))
                              (let ((a1-11 (new 'stack-no-clear 'event-message-block)))
                                (set! (-> a1-11 from) self)
                                (set! (-> a1-11 num-params) 2)
                                (set! (-> a1-11 message) 'query)
                                (set! (-> a1-11 param 0) (the-as uint 'powerup))
                                (set! (-> a1-11 param 1) (the-as uint 3))
                                (or (send-event-function *target* a1-11) (-> self activated)))))
                    (not (pos-logic self #f)))
          (suspend))
        (if (not (and (and *target*
                           (>= (-> self active-distance) (vector-vector-xz-distance (-> self root trans) (-> *target* control trans))))
                      (let ((a1-14 (new 'stack-no-clear 'event-message-block)))
                        (set! (-> a1-14 from) self)
                        (set! (-> a1-14 num-params) 2)
                        (set! (-> a1-14 message) 'query)
                        (set! (-> a1-14 param 0) (the-as uint 'powerup))
                        (set! (-> a1-14 param 1) (the-as uint 3))
                        (or (send-event-function *target* a1-14) (-> self activated)))))
          (go orb-cache-top-idle arg0))))
  :post plat-post)

(defstate orb-cache-top-complete (orb-cache-top)
  :event plat-event
  :trans plat-trans
  :code
    (behavior ((arg0 symbol))
      (if (not arg0) (sound-play "close-orb-cash"))
      (ja :group! orb-cache-top-idle-ja :num! (identity (ja-aframe 0.0 0)))
      (new 'stack-no-clear 'vector)
      (set! (-> self basetrans y) (+ 2048.0 (-> self root-pos)))
      (anim-loop))
  :post plat-post)

(defmethod init-from-entity! ((this orb-cache-top) (arg0 entity-actor))
  (let ((a0-1 (-> this entity)))
    (if (when a0-1
          (let ((a0-2 (-> a0-1 extra perm task))) (if a0-2 (= a0-2 (game-task none)))))
      (set! (-> this entity extra perm task) (game-task complete))))
  (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))
    (alloc-riders s4-0 1)
    (let ((s3-0 (new 'process 'collide-shape-prim-mesh s4-0 (the-as uint 0) (the-as uint 0))))
      (set! (-> s3-0 prim-core collide-as) (collide-kind ground-object blue-eco-suck))
      (set! (-> s3-0 collide-with) (collide-kind target))
      (set! (-> s3-0 prim-core action) (collide-action solid rider-plat-sticky))
      (set! (-> s3-0 prim-core offense) (collide-offense indestructible))
      (set! (-> s3-0 transform-index) 0)
      (set-vector! (-> s3-0 local-sphere) 0.0 0.0 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)
  (logclear! (-> this mask) (process-mask actor-pause))
  (initialize-skeleton this *orb-cache-top-sg* '())
  (logior! (-> this skel status) (janim-status inited))
  (update-transforms! (-> this root))
  (baseplat-method-21 this)
  (set! (-> this money) (res-lump-value (-> this entity) 'orb-cache-count int :default (the-as uint128 20)))
  (set! (-> this active-distance) 61440.0)
  (set! (-> this inactive-distance) 245760.0)
  (set! (-> this root-pos) (-> this basetrans y))
  (set! (-> this platform-pos) (-> this root-pos))
  (set! (-> this activated)
        (the-as symbol (and (-> this entity) (logtest? (-> this entity extra perm status) (entity-perm-status complete)))))
  (let ((v1-39 (-> this entity extra perm)))
    (logior! (-> v1-39 status) (entity-perm-status user-set-from-cstage))
    (set! (-> this money) (- (-> this money) (-> v1-39 user-int16 0))))
  (dotimes (v1-42 (-> this money))
    (set! (-> this money-list v1-42) (the-as handle #f)))
  (cond
    ((zero? (-> this money)) (go orb-cache-top-complete #t))
    ((and (-> this entity) (logtest? (-> this entity extra perm status) (entity-perm-status complete)))
     (go orb-cache-top-activate #t))
    (else (go orb-cache-top-idle #f)))
  (none))
