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

(import "goal_src/jak1/import/plat-button-ag.gc")

;; definition of type plat-button
(deftype plat-button (process-drawable)
  ((root-override           collide-shape-moving         :offset        112)
   (go-back-if-lost-player? symbol                       :offset-assert 176)
   (grab-player?            symbol                       :offset-assert 180)
   (should-grab-player?     symbol                       :offset-assert 184)
   (path-pos                float                        :offset-assert 188)
   (bidirectional?          symbol                       :offset-assert 192)
   (allow-auto-kill         symbol                       :offset-assert 196)
   (sound-id                sound-id                     :offset-assert 200)
   (trans-off               vector               :inline :offset-assert 208)
   (spawn-pos               vector               :inline :offset-assert 224)
   )
  :heap-base #x80
  :method-count-assert 33
  :size-assert         #xf0
  :flag-assert         #x21008000f0
  (:methods
    (plat-button-at-end () _type_ :state 20)
    (plat-button-idle () _type_ :state 21)
    (plat-button-pressed () _type_ :state 22)
    (plat-button-move-downward () _type_ :state 23)
    (plat-button-move-upward () _type_ :state 24)
    (plat-button-teleport-to-other-end () _type_ :state 25)
    (can-activate? (_type_) symbol 26)
    (TODO-RENAME-27 (_type_) none 27)
    (TODO-RENAME-28 (_type_) collide-shape-moving 28)
    (can-target-move? (_type_) none 29)
    (should-teleport? (_type_) symbol 30)
    (TODO-RENAME-31 (_type_) none 31)
    (TODO-RENAME-32 (_type_) none 32)
    )
  )

;; definition for method 3 of type plat-button
(defmethod inspect plat-button ((obj plat-button))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tgo-back-if-lost-player?: ~A~%" (-> obj go-back-if-lost-player?))
  (format #t "~T~Tgrab-player?: ~A~%" (-> obj grab-player?))
  (format #t "~T~Tshould-grab-player?: ~A~%" (-> obj should-grab-player?))
  (format #t "~T~Tpath-pos: ~f~%" (-> obj path-pos))
  (format #t "~T~Tbidirectional?: ~A~%" (-> obj bidirectional?))
  (format #t "~T~Tallow-auto-kill: ~A~%" (-> obj allow-auto-kill))
  (format #t "~T~Tsound-id: ~D~%" (-> obj sound-id))
  (format #t "~T~Ttrans-off: #<vector @ #x~X>~%" (-> obj trans-off))
  (format #t "~T~Tspawn-pos: #<vector @ #x~X>~%" (-> obj spawn-pos))
  obj
  )

;; failed to figure out what this is:
(defskelgroup *plat-button-sg* plat-button plat-button-geo-jg plat-button-pressed-ja
              ((plat-button-geo-mg (meters 999999)))
              :bounds (static-spherem 0 -1 0 6.6)
              )

;; definition for method 30 of type plat-button
(defmethod should-teleport? plat-button ((obj plat-button))
  #f
  )

;; definition for method 26 of type plat-button
(defmethod can-activate? plat-button ((obj plat-button))
  (or (= (-> obj path-pos) 0.0) (and (-> obj bidirectional?) (= (-> obj path-pos) 1.0)))
  )

;; failed to figure out what this is:
(defstate plat-button-idle (plat-button)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (case arg2
      (('touch)
       (when (can-activate? self)
         (if (and ((method-of-type touching-shapes-entry prims-touching?)
                   (the-as touching-shapes-entry (-> arg3 param 0))
                   (-> self root-override)
                   (the-as uint 1)
                   )
                  (or (not (-> self should-grab-player?)) (let ((v0-3 (process-grab? *target*)))
                                                            (set! (-> self grab-player?) v0-3)
                                                            v0-3
                                                            )
                      )
                  )
             (go-virtual plat-button-pressed)
             )
         )
       )
      )
    )
  :code (behavior ()
    (let ((gp-0 (can-activate? self)))
      (if gp-0
          (ja-no-eval :num! (seek! 0.0))
          (ja-no-eval :num! (seek!))
          )
      (loop
        (if (should-teleport? self)
            (go-virtual plat-button-teleport-to-other-end)
            )
        (let ((s5-0 (can-activate? self)))
          (when (!= s5-0 gp-0)
            (set! gp-0 s5-0)
            (if s5-0
                (ja-no-eval :num! (seek! 0.0))
                (ja-no-eval :num! (seek!))
                )
            )
          (when (not (ja-done? 0))
            (rider-trans)
            (if s5-0
                (ja :num! (seek! 0.0))
                (ja :num! (seek!))
                )
            (rider-post)
            )
          )
        (suspend)
        )
      )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate plat-button-teleport-to-other-end (plat-button)
  :virtual #t
  :code (behavior ()
    (let ((f0-0 1.0))
      (if (>= (-> self path-pos) 0.5)
          (set! f0-0 0.0)
          )
      (set! (-> self path-pos) f0-0)
      (let ((gp-0 (new 'stack-no-clear 'vector)))
        (eval-path-curve! (-> self path) gp-0 f0-0 'interp)
        (vector+! gp-0 gp-0 (-> self trans-off))
        (move-to-point! (-> self root-override) gp-0)
        )
      )
    (ja-post)
    (go-virtual plat-button-idle)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate plat-button-pressed (plat-button)
  :virtual #t
  :trans (the-as (function none :behavior plat-button) rider-trans)
  :code (behavior ()
    (ja-no-eval :num! (seek!))
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (set! (-> self go-back-if-lost-player?) #t)
    (process-entity-status! self (entity-perm-status complete) #t)
    (if (= (-> self path-pos) 0.0)
        (go-virtual plat-button-move-downward)
        (go-virtual plat-button-move-upward)
        )
    (none)
    )
  :post (the-as (function none :behavior plat-button) rider-post)
  )

;; definition for function plat-button-camera-on
;; INFO: Return type mismatch int vs none.
(defbehavior plat-button-camera-on plat-button ()
  (let ((v1-1 (res-lump-struct (-> self entity) 'camera-name structure)))
    (if v1-1
        (send-event *camera* 'change-to-entity-by-name v1-1)
        )
    )
  0
  (none)
  )

;; definition for function plat-button-camera-off
;; INFO: Return type mismatch int vs none.
(defbehavior plat-button-camera-off plat-button ()
  (send-event *camera* 'clear-entity)
  0
  (none)
  )

;; failed to figure out what this is:
(defstate plat-button-move-downward (plat-button)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (the-as object (when (or (= arg2 'touch) (= arg2 'attack))
                     (set! (-> self state-time) (-> *display* base-frame-counter))
                     #f
                     )
            )
    )
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (process-entity-status! self (entity-perm-status bit-3) #t)
    (plat-button-camera-on)
    (set-setting! 'allow-look-around #f 0.0 0)
    (none)
    )
  :exit (behavior ()
    (plat-button-camera-off)
    (remove-setting! 'allow-look-around)
    (none)
    )
  :trans (behavior ()
    (if (= (-> self path-pos) 1.0)
        (go-virtual plat-button-at-end)
        )
    (rider-trans)
    (when (-> self go-back-if-lost-player?)
      (when (or (not *target*)
                (and (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 4))
                     (not (and (logtest? (-> *target* control unknown-surface00 flags) (surface-flags jump))
                               (zero? (logand (-> *target* control status) (cshape-moving-flags onsurf)))
                               )
                          )
                     )
                )
        (set! (-> self go-back-if-lost-player?) #f)
        (go-virtual plat-button-move-upward)
        )
      )
    (let ((f0-4 (seek-with-smooth (-> self path-pos) 1.0 (* 0.1 (-> *display* seconds-per-frame)) 0.25 0.001)))
      (set! (-> self path-pos) f0-4)
      (let ((gp-0 (new 'stack-no-clear 'vector)))
        (eval-path-curve! (-> self path) gp-0 f0-4 'interp)
        (vector+! gp-0 gp-0 (-> self trans-off))
        (move-to-point! (-> self root-override) gp-0)
        )
      )
    (sound-play "elev-loop" :id (-> self sound-id))
    (let ((gp-1 (the-as sound-rpc-set-param (get-sound-buffer-entry))))
      (set! (-> gp-1 command) (sound-command set-param))
      (set! (-> gp-1 id) (-> self sound-id))
      (let ((a1-6 (-> self root-override trans)))
        (let ((s5-0 self))
          (when (= a1-6 #t)
            (if (and s5-0 (type-type? (-> s5-0 type) process-drawable) (nonzero? (-> s5-0 root-override)))
                (set! a1-6 (-> s5-0 root-override trans))
                (set! a1-6 (the-as vector #f))
                )
            )
          )
        (sound-trans-convert (-> gp-1 parms trans) a1-6)
        )
      (set! (-> gp-1 parms mask) (sound-mask trans))
      (-> gp-1 id)
      )
    (if (and (-> self grab-player?) (< 0.2 (-> self path-pos)))
        (set! (-> self grab-player?) (not (process-release? *target*)))
        )
    (none)
    )
  :code (the-as (function none :behavior plat-button) anim-loop)
  :post (the-as (function none :behavior plat-button) rider-post)
  )

;; failed to figure out what this is:
(defstate plat-button-move-upward (plat-button)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (the-as object (when (or (= arg2 'touch) (= arg2 'attack))
                     (set! (-> self state-time) (-> *display* base-frame-counter))
                     #f
                     )
            )
    )
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (process-entity-status! self (entity-perm-status bit-3) #t)
    (plat-button-camera-on)
    (set-setting! 'allow-look-around #f 0.0 0)
    (none)
    )
  :exit (behavior ()
    (plat-button-camera-off)
    (remove-setting! 'allow-look-around)
    (none)
    )
  :trans (behavior ()
    (if (= (-> self path-pos) 0.0)
        (go-virtual plat-button-at-end)
        )
    (rider-trans)
    (when (-> self go-back-if-lost-player?)
      (when (or (not *target*)
                (and (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 4))
                     (not (and (logtest? (-> *target* control unknown-surface00 flags) (surface-flags jump))
                               (zero? (logand (-> *target* control status) (cshape-moving-flags onsurf)))
                               )
                          )
                     )
                )
        (set! (-> self go-back-if-lost-player?) #f)
        (go-virtual plat-button-move-downward)
        )
      )
    (let ((f0-4 (seek-with-smooth (-> self path-pos) 0.0 (* 0.1 (-> *display* seconds-per-frame)) 0.25 0.001)))
      (set! (-> self path-pos) f0-4)
      (let ((gp-0 (new 'stack-no-clear 'vector)))
        (eval-path-curve! (-> self path) gp-0 f0-4 'interp)
        (vector+! gp-0 gp-0 (-> self trans-off))
        (move-to-point! (-> self root-override) gp-0)
        )
      )
    (sound-play "elev-loop" :id (-> self sound-id))
    (let ((gp-1 (the-as sound-rpc-set-param (get-sound-buffer-entry))))
      (set! (-> gp-1 command) (sound-command set-param))
      (set! (-> gp-1 id) (-> self sound-id))
      (let ((a1-6 (-> self root-override trans)))
        (let ((s5-0 self))
          (when (= a1-6 #t)
            (if (and s5-0 (type-type? (-> s5-0 type) process-drawable) (nonzero? (-> s5-0 root-override)))
                (set! a1-6 (-> s5-0 root-override trans))
                (set! a1-6 (the-as vector #f))
                )
            )
          )
        (sound-trans-convert (-> gp-1 parms trans) a1-6)
        )
      (set! (-> gp-1 parms mask) (sound-mask trans))
      (-> gp-1 id)
      )
    (if (and (-> self grab-player?) (< (-> self path-pos) 0.8))
        (set! (-> self grab-player?) (not (process-release? *target*)))
        )
    (none)
    )
  :code (the-as (function none :behavior plat-button) anim-loop)
  :post (the-as (function none :behavior plat-button) rider-post)
  )

;; failed to figure out what this is:
(defstate plat-button-at-end (plat-button)
  :virtual #t
  :code (behavior ()
    (if (-> self allow-auto-kill)
        (process-entity-status! self (entity-perm-status bit-3) #f)
        )
    (sound-stop (-> self sound-id))
    (sound-play "elev-land")
    (loop
      (if (or (not *target*)
              (< 268435460.0 (vector-vector-xz-distance-squared (-> self root-override trans) (target-pos 0)))
              )
          (go-virtual plat-button-idle)
          )
      (suspend)
      )
    (none)
    )
  )

;; definition for method 28 of type plat-button
(defmethod TODO-RENAME-28 plat-button ((obj plat-button))
  (let ((s5-0 (new 'process 'collide-shape-moving obj (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-group s5-0 (the-as uint 2) 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 ca-1))
      (set! (-> s4-0 transform-index) 0)
      (set-vector! (-> s4-0 local-sphere) 0.0 -4096.0 0.0 27033.6)
      (set-root-prim! s5-0 s4-0)
      (let ((s3-0 (new 'process 'collide-shape-prim-mesh s5-0 (the-as uint 0) (the-as uint 1))))
        (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 ca-1))
        (set! (-> s3-0 prim-core offense) (collide-offense indestructible))
        (set! (-> s3-0 transform-index) 4)
        (set-vector! (-> s3-0 local-sphere) 0.0 -3072.0 0.0 7372.8)
        (append-prim s4-0 s3-0)
        )
      (let ((s3-1 (new 'process 'collide-shape-prim-mesh s5-0 (the-as uint 1) (the-as uint 2))))
        (set! (-> s3-1 prim-core collide-as) (collide-kind ground-object))
        (set! (-> s3-1 collide-with) (collide-kind target))
        (set! (-> s3-1 prim-core action) (collide-action solid ca-1))
        (set! (-> s3-1 prim-core offense) (collide-offense indestructible))
        (set! (-> s3-1 transform-index) 3)
        (set-vector! (-> s3-1 local-sphere) 0.0 -4096.0 0.0 27033.6)
        (append-prim s4-0 s3-1)
        )
      )
    (set! (-> s5-0 nav-radius) (* 0.75 (-> s5-0 root-prim local-sphere w)))
    (backup-collide-with-as s5-0)
    (set! (-> obj root-override) s5-0)
    s5-0
    )
  )

;; definition for method 29 of type plat-button
;; INFO: Return type mismatch int vs none.
(defmethod can-target-move? plat-button ((obj plat-button))
  0
  (none)
  )

;; definition for method 27 of type plat-button
;; INFO: Return type mismatch symbol vs none.
(defmethod TODO-RENAME-27 plat-button ((obj plat-button))
  (ja-channel-set! 1)
  (cond
    ((can-activate? obj)
     (let ((s5-0 (-> obj skel root-channel 0)))
       (joint-control-channel-group-eval!
         s5-0
         (the-as art-joint-anim (-> obj draw art-group data 2))
         num-func-identity
         )
       (set! (-> s5-0 frame-num) 0.0)
       )
     )
    (else
      (let ((s5-1 (-> obj skel root-channel 0)))
        (joint-control-channel-group-eval!
          s5-1
          (the-as art-joint-anim (-> obj draw art-group data 2))
          num-func-identity
          )
        (set! (-> s5-1 frame-num)
              (the float (+ (-> (the-as art-joint-anim (-> obj draw art-group data 2)) data 0 length) -1))
              )
        )
      )
    )
  (ja-post)
  (update-transforms! (-> obj root-override))
  (none)
  )

;; definition for method 31 of type plat-button
;; INFO: Return type mismatch int vs none.
(defmethod TODO-RENAME-31 plat-button ((obj plat-button))
  (initialize-skeleton obj *plat-button-sg* '())
  0
  (none)
  )

;; definition for method 32 of type plat-button
;; INFO: Return type mismatch int vs none.
(defmethod TODO-RENAME-32 plat-button ((obj plat-button))
  (go (method-of-object obj plat-button-idle))
  0
  (none)
  )

;; definition for method 11 of type plat-button
;; INFO: Used lq/sq
(defmethod init-from-entity! plat-button ((obj plat-button) (arg0 entity-actor))
  (set! (-> obj go-back-if-lost-player?) #f)
  (set! (-> obj grab-player?) #f)
  (set! (-> obj should-grab-player?) #f)
  (set! (-> obj trans-off quad) (-> (the-as vector ((method-of-type res-lump get-property-struct)
                                                    arg0
                                                    'trans-offset
                                                    'interp
                                                    -1000000000.0
                                                    *null-vector*
                                                    (the-as (pointer res-tag) #f)
                                                    *res-static-buf*
                                                    )
                                            )
                                    quad
                                    )
        )
  (set! (-> obj bidirectional?) (nonzero? (res-lump-value arg0 'bidirectional uint128)))
  (TODO-RENAME-28 obj)
  (process-drawable-from-entity! obj arg0)
  (logclear! (-> obj mask) (process-mask actor-pause))
  (TODO-RENAME-31 obj)
  (logior! (-> obj skel status) (janim-status inited))
  (set! (-> obj spawn-pos quad) (-> obj root-override trans quad))
  (set! (-> obj path) (new 'process 'curve-control obj 'path -1000000000.0))
  (logior! (-> obj path flags) (path-control-flag display draw-line draw-point draw-text))
  (set! (-> obj path-pos) 0.0)
  (let ((s5-1 (-> obj root-override trans)))
    (eval-path-curve! (-> obj path) s5-1 (-> obj path-pos) 'interp)
    (vector+! s5-1 s5-1 (-> obj trans-off))
    )
  (set! (-> obj sound-id) (new-sound-id))
  (set! (-> obj allow-auto-kill) #t)
  (can-target-move? obj)
  (TODO-RENAME-27 obj)
  (TODO-RENAME-32 obj)
  (none)
  )
