;;-*-Lisp-*-
(in-package goal)

(import "goal_src/jak1/import/power-left-ag.gc")
(import "goal_src/jak1/import/power-right-ag.gc")
(import "goal_src/jak1/import/powercellalt-ag.gc")

;; definition of type fin-door
(deftype fin-door (process-hidden)
  ()
  :method-count-assert 15
  :size-assert         #x70
  :flag-assert         #xf00000070
  )

;; definition for method 3 of type fin-door
(defmethod inspect fin-door ((obj fin-door))
  (format #t "[~8x] ~A~%" obj (-> obj type))
  (format #t "~Tname: ~A~%" (-> obj name))
  (format #t "~Tmask: ~D~%" (-> obj mask))
  (format #t "~Tparent: #x~X~%" (-> obj parent))
  (format #t "~Tbrother: #x~X~%" (-> obj brother))
  (format #t "~Tchild: #x~X~%" (-> obj child))
  (format #t "~Tppointer: #x~X~%" (-> obj ppointer))
  (format #t "~Tself: ~A~%" (-> obj self))
  (format #t "~Tpool: ~A~%" (-> obj pool))
  (format #t "~Tstatus: ~A~%" (-> obj status))
  (format #t "~Tpid: ~D~%" (-> obj pid))
  (format #t "~Tmain-thread: ~A~%" (-> obj main-thread))
  (format #t "~Ttop-thread: ~A~%" (-> obj top-thread))
  (format #t "~Tentity: ~A~%" (-> obj entity))
  (format #t "~Tstate: ~A~%" (-> obj state))
  (format #t "~Ttrans-hook: ~A~%" (-> obj trans-hook))
  (format #t "~Tpost-hook: ~A~%" (-> obj post-hook))
  (format #t "~Tevent-hook: ~A~%" (-> obj event-hook))
  (format #t "~Tallocated-length: ~D~%" (-> obj allocated-length))
  (format #t "~Tnext-state: ~A~%" (-> obj next-state))
  (format #t "~Theap-base: #x~X~%" (-> obj heap-base))
  (format #t "~Theap-top: #x~X~%" (-> obj heap-top))
  (format #t "~Theap-cur: #x~X~%" (-> obj heap-cur))
  (format #t "~Tstack-frame-top: ~A~%" (-> obj stack-frame-top))
  (format #t "~Theap: #<kheap @ #x~X>~%" (&-> obj heap-base))
  (format #t "~Tconnection-list: ~`'connectable`P~%" (-> obj connection-list))
  (format #t "~Tstack[0] @ #x~X~%" (-> obj stack))
  obj
  )

;; definition of type final-door
(deftype final-door (process-drawable)
  ()
  :heap-base #x40
  :method-count-assert 23
  :size-assert         #xb0
  :flag-assert         #x17004000b0
  (:methods
    (idle () _type_ :state 20)
    (dummy-21 (_type_) none 21)
    (open (symbol) _type_ :state 22)
    )
  )

;; definition for method 3 of type final-door
(defmethod inspect final-door ((obj final-door))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  obj
  )

;; failed to figure out what this is:
(defskelgroup *power-left-sg* power-left power-left-lod0-jg power-left-idle-ja
              ((power-left-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 11 0 12)
              )

;; failed to figure out what this is:
(defskelgroup *power-right-sg* power-right power-right-lod0-jg power-right-idle-ja
              ((power-right-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 11 0 12)
              )

;; failed to figure out what this is:
(defskelgroup *powercellalt-sg* powercellalt powercellalt-lod0-jg powercellalt-idle-ja
              ((powercellalt-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 2)
              )

;; failed to figure out what this is:
(defstate idle (final-door)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (case arg2
      (('open)
       (go-virtual open #f)
       )
      )
    )
  :code (behavior ()
    (transform-post)
    (logior! (-> self mask) (process-mask sleep))
    (suspend)
    0
    (none)
    )
  )

;; failed to figure out what this is:
(defstate open (final-door)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (let ((v1-0 arg2))
      (the-as object (when (= v1-0 'brightness)
                       (let ((f0-0 (the-as float (-> arg3 param 0)))
                             (f1-0 (the-as float (-> arg3 param 1)))
                             )
                         (set-vector! (-> self draw color-mult) f0-0 f0-0 f0-0 f0-0)
                         (let ((v0-0 (-> self draw color-emissive)))
                           (set! (-> v0-0 x) f1-0)
                           (set! (-> v0-0 y) f1-0)
                           (set! (-> v0-0 z) f1-0)
                           (set! (-> v0-0 w) f1-0)
                           v0-0
                           )
                         )
                       )
              )
      )
    )
  :code (behavior ((arg0 symbol))
    (case (-> self type)
      ((power-left)
       (ja-no-eval :group! (-> self draw art-group data 2) :num! (seek! max 0.353) :frame-num 0.0)
       (until (ja-done? 0)
         (transform-post)
         (suspend)
         (ja :num! (seek! max 0.353))
         )
       )
      (else
        (ja-no-eval :group! (-> self draw art-group data 2) :num! (seek! max 0.353) :frame-num 0.0)
        (until (ja-done? 0)
          (transform-post)
          (suspend)
          (ja :num! (seek! max 0.353))
          )
        )
      )
    (transform-post)
    (logior! (-> self mask) (process-mask sleep))
    (suspend)
    0
    (none)
    )
  )

;; definition for method 11 of type final-door
;; INFO: Return type mismatch object vs none.
(defmethod init-from-entity! final-door ((obj final-door) (arg0 entity-actor))
  (let ((s4-0 (new 'process 'collide-shape-moving obj (collide-list-enum hit-by-others))))
    (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-mesh s4-0 (the-as uint 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! (-> s3-0 transform-index) 3)
      (set-vector! (-> s3-0 local-sphere) 0.0 45056.0 0.0 49152.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! (-> obj root) s4-0)
    )
  (process-drawable-from-entity! obj arg0)
  (dummy-21 obj)
  (go (method-of-object obj idle))
  (none)
  )

;; definition of type power-left
(deftype power-left (final-door)
  ()
  :heap-base #x40
  :method-count-assert 23
  :size-assert         #xb0
  :flag-assert         #x17004000b0
  )

;; definition for method 3 of type power-left
(defmethod inspect power-left ((obj power-left))
  (let ((t9-0 (method-of-type final-door inspect)))
    (t9-0 obj)
    )
  obj
  )

;; definition of type power-right
(deftype power-right (final-door)
  ()
  :heap-base #x40
  :method-count-assert 23
  :size-assert         #xb0
  :flag-assert         #x17004000b0
  )

;; definition for method 3 of type power-right
(defmethod inspect power-right ((obj power-right))
  (let ((t9-0 (method-of-type final-door inspect)))
    (t9-0 obj)
    )
  obj
  )

;; failed to figure out what this is:
(defstate idle (power-left)
  :virtual #t
  :code (behavior ()
    (ja-post)
    (loop
      (if (and (and *target* (>= 61440.0 (vector-vector-distance (-> self root trans) (-> *target* control trans))))
               (and (zero? (logand (-> *target* state-flags) (state-flags grabbed)))
                    (and (>= (-> *game-info* fuel) 100.0)
                         (not (and (-> self entity) (logtest? (-> self entity extra perm status) (entity-perm-status complete))))
                         (= (get-task-status (game-task finalboss-movies)) (task-status need-reward-speech))
                         (send-event
                           *target*
                           'change-mode
                           'final-door
                           (search-process-tree *active-pool* (lambda ((arg0 final-door)) (= (-> arg0 type) power-right)))
                           (search-process-tree *active-pool* (lambda ((arg0 final-door)) (= (-> arg0 type) power-left)))
                           )
                         )
                    )
               )
          (process-entity-status! self (entity-perm-status complete) #t)
          )
      (suspend)
      )
    (none)
    )
  )

;; definition for method 21 of type power-left
;; INFO: Return type mismatch int vs none.
(defmethod dummy-21 power-left ((obj power-left))
  (initialize-skeleton obj *power-left-sg* '())
  0
  (none)
  )

;; definition for method 21 of type power-right
;; INFO: Return type mismatch int vs none.
(defmethod dummy-21 power-right ((obj power-right))
  (initialize-skeleton obj *power-right-sg* '())
  0
  (none)
  )

;; definition of type powercellalt
(deftype powercellalt (process-drawable)
  ((root-override collide-shape-moving         :offset        112)
   (jump-pos      vector               :inline :offset-assert 176)
   (base          vector               :inline :offset-assert 192)
   (index         int32                        :offset-assert 208)
   )
  :heap-base #x70
  :method-count-assert 23
  :size-assert         #xd4
  :flag-assert         #x17007000d4
  (:methods
    (dummy-20 () none 20)
    (jump () _type_ :state 21)
    (idle () _type_ :state 22)
    )
  )

;; definition for method 3 of type powercellalt
(defmethod inspect powercellalt ((obj powercellalt))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tjump-pos: ~`vector`P~%" (-> obj jump-pos))
  (format #t "~T~Tbase: ~`vector`P~%" (-> obj base))
  (format #t "~T~Tindex: ~D~%" (-> obj index))
  obj
  )

;; failed to figure out what this is:
(defstate jump (powercellalt)
  :virtual #t
  :code (behavior ()
    (sound-play "cell-prize")
    (let ((gp-1 (new 'stack 'trajectory)))
      (set! (-> self base y) (-> self jump-pos y))
      (setup-from-to-duration! gp-1 (-> self root-override trans) (-> self jump-pos) 300.0 -2.2755556)
      (set! (-> self state-time) (-> *display* base-frame-counter))
      (until (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 1))
        (let ((f0-2 (the float (- (-> *display* base-frame-counter) (-> self state-time)))))
          (eval-position! gp-1 f0-2 (-> self root-override trans))
          )
        (transform-post)
        (spawn (-> self part) (the-as vector (-> self root-override root-prim prim-core)))
        (suspend)
        (if (nonzero? (-> self skel))
            (ja :num! (loop! 0.5))
            )
        )
      )
    (set! (-> self root-override trans quad) (-> self jump-pos quad))
    (set! (-> self base quad) (-> self root-override trans quad))
    (transform-post)
    (sound-play "land-pcmetal" :pitch 2)
    (process-spawn
      part-tracker
      :init part-tracker-init
      (-> *part-group-id-table* 4)
      -1
      #f
      #f
      #f
      (-> self root-override root-prim prim-core)
      :to *entity-pool*
      )
    (go-virtual idle)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate idle (powercellalt)
  :virtual #t
  :code (behavior ()
    (loop
      (vector<-cspace!
        (-> self root-override trans)
        (-> (the-as process-drawable (-> self parent 0)) node-list data (-> self index))
        )
      (transform-post)
      (suspend)
      )
    (none)
    )
  )

;; definition for function powercellalt-init-by-other
;; INFO: Used lq/sq
;; INFO: Return type mismatch object vs none.
(defbehavior powercellalt-init-by-other powercellalt ((arg0 entity-actor) (arg1 vector) (arg2 vector) (arg3 int))
  (set! (-> self entity) arg0)
  (let ((s3-0 (new 'process 'collide-shape-moving self (collide-list-enum hit-by-player))))
    (set! (-> s3-0 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s3-0 reaction) default-collision-reaction)
    (set! (-> s3-0 no-reaction)
          (the-as (function collide-shape-moving collide-shape-intersect vector vector none) nothing)
          )
    (let ((s2-0 (new 'process 'collide-shape-prim-sphere s3-0 (the-as uint 0))))
      (set! (-> s2-0 prim-core collide-as) (collide-kind powerup))
      (set! (-> s2-0 collide-with) (collide-kind target))
      (set-vector! (-> s2-0 local-sphere) 0.0 0.0 0.0 4915.2)
      (set-root-prim! s3-0 s2-0)
      )
    (set! (-> s3-0 nav-radius) (* 0.75 (-> s3-0 root-prim local-sphere w)))
    (backup-collide-with-as s3-0)
    (set! (-> self root-override) s3-0)
    )
  (set! (-> self root-override trans quad) (-> arg1 quad))
  (set! (-> self jump-pos quad) (-> arg2 quad))
  (set-vector! (-> self root-override scale) 0.5 0.5 0.5 1.0)
  (set! (-> self index) arg3)
  (initialize-skeleton self *powercellalt-sg* '())
  (set! (-> self part) (create-launch-control (-> *part-group-id-table* 63) self))
  enter-state
  (-> self root-override trans)
  (-> self jump-pos)
  (go-virtual jump)
  (none)
  )

;; failed to figure out what this is:
(defstate target-final-door (target)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    ((-> target-grab event) arg0 arg1 arg2 arg3)
    )
  :enter (behavior ((arg0 basic) (arg1 handle))
    (send-event *camera* 'change-to-entity-by-name "camera-403")
    (set! (-> self control unknown-surface00) *empty-mods*)
    (logior! (-> self state-flags) (state-flags invulnerable grabbed))
    (set-setting! 'allow-progress #f 0.0 0)
    (none)
    )
  :exit (behavior ()
    (send-event *camera* 'change-state *camera-base-mode* (seconds 0.2))
    (logclear! (-> self state-flags) (state-flags invulnerable grabbed))
    (target-exit)
    (remove-setting! 'allow-progress)
    (none)
    )
  :trans (behavior ()
    (set-letterbox-frames (seconds 0.017))
    (none)
    )
  :code (behavior ((arg0 basic) (arg1 handle))
    (local-vars (sv-144 process) (sv-160 vector) (sv-176 process) (sv-192 vector))
    (let ((a0-2 (get-task-control (game-task finalboss-movies))))
      (save-reminder a0-2 (logior (get-reminder a0-2 0) 2) 0)
      )
    (move-to-point! (-> self control) (new 'static 'vector :x 11368946.0 :y 2215900.2 :z -19405602.0 :w 1.0))
    (set-quaternion! (-> self control) (the-as quaternion (new 'static 'vector :y -0.8472 :w 0.5312)))
    (rot->dir-targ! (-> self control))
    (transform-post)
    (ja-channel-push! 1 (seconds 0.2))
    (ja-no-eval :group! (-> self draw art-group data 88) :num! (seek! (ja-aframe 18.0 0)) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek! (ja-aframe 18.0 0)))
      )
    (let ((s4-3 (-> (handle->process (the-as handle arg0)) entity))
          (s3-1 (vector<-cspace! (new 'stack-no-clear 'vector) (-> self node-list data 7)))
          )
      (dotimes (s2-0 50)
        (when (handle->process (the-as handle arg0))
          (let ((s1-0 (handle->process (the-as handle arg0)))
                (s0-0 (+ s2-0 4))
                )
            (set! sv-160
                  (vector<-cspace! (new 'stack-no-clear 'vector) (-> (the-as process-drawable s1-0) node-list data s0-0))
                  )
            (set! sv-144 (get-process *default-dead-pool* powercellalt #x4000))
            (when sv-144
              (let ((t9-16 (method-of-type powercellalt activate)))
                (t9-16 (the-as powercellalt sv-144) s1-0 'powercellalt (the-as pointer #x70004000))
                )
              (run-now-in-process sv-144 powercellalt-init-by-other s4-3 s3-1 sv-160 s0-0)
              (-> sv-144 ppointer)
              )
            )
          )
        (let ((s1-1 (-> *display* base-frame-counter)))
          (until (>= (- (-> *display* base-frame-counter) s1-1) (seconds 0.1))
            (suspend)
            )
          )
        (when (handle->process arg1)
          (let ((s1-2 (handle->process arg1))
                (s0-1 (+ s2-0 4))
                )
            (set! sv-192
                  (vector<-cspace! (new 'stack-no-clear 'vector) (-> (the-as process-drawable s1-2) node-list data s0-1))
                  )
            (set! sv-176 (get-process *default-dead-pool* powercellalt #x4000))
            (when sv-176
              (let ((t9-20 (method-of-type powercellalt activate)))
                (t9-20 (the-as powercellalt sv-176) s1-2 'powercellalt (the-as pointer #x70004000))
                )
              (run-now-in-process sv-176 powercellalt-init-by-other s4-3 s3-1 sv-192 s0-1)
              (-> sv-176 ppointer)
              )
            )
          )
        (let ((s1-3 (-> *display* base-frame-counter)))
          (until (>= (- (-> *display* base-frame-counter) s1-3) (seconds 0.1))
            (suspend)
            )
          )
        )
      )
    (let ((gp-1 (-> *display* base-frame-counter)))
      (until (>= (- (-> *display* base-frame-counter) gp-1) (seconds 2))
        (suspend)
        )
      )
    (let ((v0-22 (entity-by-name "sage-finalboss-1"))
          (a1-26 (new 'stack-no-clear 'event-message-block))
          )
      (set! (-> a1-26 from) self)
      (set! (-> a1-26 num-params) 0)
      (set! (-> a1-26 message) 'play-anim)
      (let ((v1-70 v0-22))
        (send-event-function
          (if v1-70
              (-> v1-70 extra process)
              )
          a1-26
          )
        )
      )
    (go target-stance)
    (none)
    )
  :post target-no-stick-post
  )
