;;-*-Lisp-*-
(in-package goal)
(bundles "MIS.DGO")
(require "engine/game/task/task-control.gc")
(require "engine/common-obs/baseplat.gc")
(declare-type silostep process-drawable)

;; DECOMP BEGINS

(deftype silostep (process-drawable)
  ((anim-limit  float)
   (cam-tracker handle))
  (:states
   silostep-camera
   silostep-idle
   (silostep-rise symbol)))


(defskelgroup *silostep-sg*
  silostep
  silostep-lod0-jg
  silostep-idle-ja
  ((silostep-lod0-mg (meters 999999)))
  :bounds (static-spherem 0 2 0 8))

(defstate silostep-idle (silostep)
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (case message
        (('trigger) (go silostep-camera))
        (('trigger-rise) (go silostep-rise #f))))
  :code
    (behavior ()
      (ja :group! silostep-idle-ja :num! min)
      (transform-post)
      (loop
        (suspend)))
  :post ja-post)

(defbehavior misty-camera-view silostep ()
  (set! (-> self cam-tracker)
        (ppointer->handle (process-spawn camera-tracker
                                         :init
                                         camera-tracker-init
                                         (lambda :behavior camera-tracker ()
                                           (while (not (process-grab? *target*))
                                             (suspend))
                                           (camera-change-to "camera-160" 150 #f)
                                           (suspend-for (seconds 3))
                                           (while (not (process-release? (handle->process (-> self grab-target))))
                                             (suspend))
                                           (camera-change-to (the-as string 'base) 150 #f)
                                           (none))
                                         :to
                                         self)))
  (none))

(defstate silostep-camera (silostep)
  :code
    (behavior ()
      (misty-camera-view)
      (let* ((gp-0 (get-task-control (game-task misty-warehouse)))
             (v1-1 (get-reminder gp-0 0)))
        (save-reminder gp-0 (logior v1-1 2) 0))
      (set-time! (-> self state-time))
      (suspend-for (seconds 1))
      (sound-play "arena-steps")
      (send-to-all-after (-> self link) 'trigger-rise)
      (go silostep-rise #f))
  :post ja-post)

(defstate silostep-rise (silostep)
  :code
    (behavior ((arg0 symbol))
      (process-entity-status! self (entity-perm-status complete) #t)
      (when (not arg0)
        (ja-no-eval :group! silostep-idle-ja :num! (seek! (-> self anim-limit)) :frame-num 0.0)
        (until (ja-done? 0)
          (rider-trans)
          (rider-post)
          (suspend)
          (ja :num! (seek! (-> self anim-limit)))))
      (ja :group! silostep-idle-ja :num! (identity (-> self anim-limit)))
      (rider-post)
      (loop
        (ja-post)
        (suspend)))
  :post #f)

(defmethod init-from-entity! ((this silostep) (arg0 entity-actor))
  (logior! (-> this mask) (process-mask movie-subject))
  (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))
      (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! (-> s3-0 transform-index) 4)
      (set-vector! (-> s3-0 local-sphere) 0.0 4096.0 0.0 18432.0)
      (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 *silostep-sg* '())
  (set! (-> this anim-limit)
        (* (res-lump-float arg0 'distance :default 1.0)
           (the float (+ (-> (the-as art-joint-anim (-> this draw art-group data 2)) data 0 length) -1))))
  (set! (-> this link) (new 'process 'actor-link-info this))
  (if (and (-> this entity) (logtest? (-> this entity extra perm status) (entity-perm-status complete)))
    (go silostep-rise #t)
    (go silostep-idle))
  (none))

(defskelgroup *rounddoor-sg*
  rounddoor
  rounddoor-lod0-jg
  rounddoor-idle-ja
  ((rounddoor-lod0-mg (meters 20)) (rounddoor-lod1-mg (meters 999999)))
  :bounds (static-spherem 0 0 0 8))

(deftype rounddoor (eco-door) ())


(defmethod eco-door-method-24 ((this rounddoor))
  (let ((s5-0 (new 'process 'collide-shape this (collide-list-enum hit-by-others))))
    (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))
      (set! (-> s4-0 prim-core offense) (collide-offense indestructible))
      (set! (-> s4-0 transform-index) 0)
      (set-vector! (-> s4-0 local-sphere) -12288.0 0.0 0.0 40960.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 eco-door-method-25 ((this rounddoor))
  (initialize-skeleton this *rounddoor-sg* '())
  (set! (-> this open-distance) 69632.0)
  (set! (-> this close-distance) 81920.0)
  (set! (-> this open-sound) (static-sound-name "arenadoor-open"))
  (set! (-> this close-sound) (static-sound-name "arenadoor-close"))
  (set! (-> this speed) 1.5)
  (set! (-> this auto-close) #t)
  (set! (-> this one-way) #t)
  (vector-x-quaternion! (-> this out-dir) (-> this root quat))
  (set! (-> this out-dir w) (- 8192.0 (vector-dot (-> this out-dir) (-> this root trans))))
  (update-transforms! (-> this root))
  0
  (none))
