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

;; name: ladder.gc
;; name in dgo: ladder
;; dgos: HALFPIPE, SED, WWD, VOCA

;; DECOMP BEGINS

(deftype ladder (process-drawable)
  ((root             collide-shape :override)
   (rider-unit       float)
   (rider-time       time-frame)
   (art-height       meters)
   (set-height       meters)
   (meters-per-unit  meters)
   (meters-per-rung  meters)
   (options          ladder-options)
   )
  (:state-methods
    idle
    (active handle)
    )
  (:methods
    (init-collision! (_type_) none)
    (init-skel! (_type_) none)
    (init-params! (_type_) none)
    (ladder-method-25 (_type_ matrix float) matrix)
    (nop (_type_) none)
    )
  )


(defskelgroup skel-ladder ladder ladder-lod0-jg ladder-idle-ja
              ((ladder-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 8 0 16)
              )

(defstate idle (ladder)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('touch 'attack)
       (let ((s2-0 (as-type proc process-focusable)))
         (when s2-0
           (let ((s4-1 (ladder-method-25 self (new 'stack-no-clear 'matrix) 0.0))
                 (s3-0 (ladder-method-25 self (new 'stack-no-clear 'matrix) 1.0))
                 (s1-0 (get-trans (the-as process-focusable s2-0) 0))
                 (s2-1 (new 'stack-no-clear 'vector))
                 )
             (vector-segment-distance-point! s1-0 (-> s4-1 trans) (-> s3-0 trans) s2-1)
             (when (and (time-elapsed? (-> self rider-time) (seconds 0.2))
                        (if (< 0.0 (vector-dot (vector-! (new 'stack-no-clear 'vector) s1-0 s2-1) (-> s4-1 fvec)))
                            (not (logtest? (-> self options) (ladder-options lo0)))
                            (not (logtest? (-> self options) (ladder-options lo1)))
                            )
                        (send-event proc 'ladder (-> block param 0))
                        )
               (set! (-> self meters-per-unit) (vector-vector-distance (-> s4-1 trans) (-> s3-0 trans)))
               (set! (-> self rider-unit) (/ (vector-vector-distance (-> s4-1 trans) s2-1) (-> self meters-per-unit)))
               (set-time! (-> self rider-time))
               (go-virtual active (process->handle proc))
               )
             )
           (the-as ladder-options #f)
           )
         )
       )
      (('options)
       (-> self options)
       )
      )
    )
  :code (behavior ()
    (until #f
      (nop self)
      (suspend)
      )
    #f
    )
  :post ja-post
  )

(defstate active (ladder)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('matrix)
       (let ((v0-0 (the-as object (ladder-method-25 self (the-as matrix (-> block param 0)) (-> self rider-unit)))))
         (set-time! (-> self rider-time))
         v0-0
         )
       )
      (('move)
       (set! (-> self rider-unit)
             (fmax 0.0 (fmin 1.0 (+ (-> self rider-unit) (/ (the-as float (-> block param 0)) (-> self meters-per-unit)))))
             )
       (* (-> self rider-unit) (/ (-> self meters-per-unit) (-> self meters-per-rung)))
       )
      (('pos)
       (if (> argc 0)
           (set! (-> (the-as vector (-> block param 0)) quad) (-> self node-list data 5 bone transform uvec quad))
           )
       (-> self rider-unit)
       )
      (('stance)
       (let* ((f0-8 (/ (/ (-> self meters-per-rung) 2) (-> self meters-per-unit)))
              (f1-12 (* (the float (the int (/ (+ (-> self rider-unit) (/ f0-8 2)) f0-8))) f0-8))
              )
         (+! (-> self rider-unit) (* 0.1 (- f1-12 (-> self rider-unit))))
         (set! (-> self rider-unit) (seek (-> self rider-unit) f1-12 (* 2.0 (seconds-per-frame) f0-8)))
         )
       )
      (('options)
       (-> self options)
       )
      )
    )
  :code (behavior ((arg0 handle))
    (set-time! (-> self rider-time))
    (while (and (as-type (handle->process arg0) process-focusable)
                (not (time-elapsed? (-> self rider-time) (seconds 0.1)))
                )
      (nop self)
      (suspend)
      )
    (go-virtual idle)
    )
  )

(defmethod init-collision! ((this ladder))
  (let ((s5-0 (new 'process 'collide-shape this (collide-list-enum hit-by-player))))
    (set! (-> s5-0 penetrated-by) (penetrate))
    (let ((v1-2 (new 'process 'collide-shape-prim-mesh s5-0 (the-as uint 0) (the-as uint 0))))
      (set! (-> v1-2 prim-core collide-as) (collide-spec obstacle))
      (set! (-> v1-2 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-2 prim-core action) (collide-action solid))
      (set! (-> v1-2 transform-index) 0)
      (set-vector! (-> v1-2 local-sphere) 0.0 10240.0 0.0 12288.0)
      (set! (-> s5-0 total-prims) (the-as uint 1))
      (set! (-> s5-0 root-prim) v1-2)
      )
    (set! (-> s5-0 nav-radius) (* 0.75 (-> s5-0 root-prim local-sphere w)))
    (let ((v1-5 (-> s5-0 root-prim)))
      (set! (-> s5-0 backup-collide-as) (-> v1-5 prim-core collide-as))
      (set! (-> s5-0 backup-collide-with) (-> v1-5 prim-core collide-with))
      )
    (set! (-> this root) s5-0)
    )
  0
  (none)
  )

(defmethod init-skel! ((this ladder))
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-ladder" (the-as (pointer level) #f)))
    (the-as pair 0)
    )
  0
  (none)
  )

(defmethod init-params! ((this ladder))
  (set! (-> this meters-per-rung) 6144.0)
  (set! (-> this art-height) 94208.0)
  (set! (-> this set-height) (res-lump-float (-> this entity) 'height :default 94208.0))
  (set! (-> this options) (res-lump-value (-> this entity) 'options ladder-options :time -1000000000.0))
  0
  (none)
  )

(defmethod ladder-method-25 ((this ladder) (arg0 matrix) (arg1 float))
  (let ((s4-0 (-> this node-list data 4 bone transform))
        (s3-0 (matrix-copy! (new 'stack-no-clear 'matrix) (-> this node-list data 5 bone transform)))
        )
    (vector+!
      (-> s3-0 trans)
      (-> s3-0 trans)
      (vector-normalize! (vector-! (new 'stack-no-clear 'vector) (-> s4-0 trans) (-> s3-0 trans)) 9830.4)
      )
    (matrix-lerp! arg0 s4-0 s3-0 arg1)
    )
  )

(defmethod nop ((this ladder))
  0
  (none)
  )

(defmethod init-from-entity! ((this ladder) (arg0 entity-actor))
  "Set up a newly created process from the entity that created it."
  (init-collision! this)
  (process-drawable-from-entity! this arg0)
  (init-skel! this)
  (init-params! this)
  (if (logtest? (-> this options) (ladder-options nodraw))
      (logior! (-> this draw status) (draw-control-status no-draw-bounds))
      )
  (set! (-> this root scale y) (/ (-> this set-height) (-> this art-height)))
  (set! (-> this draw bounds y) (/ (/ (-> this set-height) 2) (-> this root scale y)))
  (set! (-> this draw bounds w) (+ 2048.0 (/ (-> this set-height) 2)))
  (set! (-> this root root-prim local-sphere y) (/ (/ (-> this set-height) 2) (-> this root scale y)))
  (set! (-> this root root-prim local-sphere w) (+ 2048.0 (/ (-> this set-height) 2)))
  (go (method-of-object this idle))
  )
