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

;; name: blocking-plane.gc
;; name in dgo: blocking-plane
;; dgos: GAME, COMMON

;; DECOMP BEGINS

(deftype blocking-plane (process-drawable)
  ((root  collide-shape :override)
   )
  (:state-methods
    idle
    )
  (:methods
    (init! (_type_ (inline-array vector) float) none)
    )
  )


(defskelgroup skel-blocking-plane blocking-plane blocking-plane-lod0-jg blocking-plane-idle-ja
              ((blocking-plane-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 100.1)
              :texture-level 6
              )

(defstate idle (blocking-plane)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('on)
       (cond
         ((nonzero? (-> self root))
          (let ((prim (-> self root root-prim)))
            (set! (-> prim prim-core collide-as) (-> self root backup-collide-as))
            (let ((v0-0 (the-as int (-> self root backup-collide-with))))
              (set! (-> prim prim-core collide-with) (the-as collide-spec v0-0))
              v0-0
              )
            )
          )
         (else
           (let ((proc-child-ptr-0 (-> self child)))
             (while proc-child-ptr-0
               (let ((proc-child-0 (ppointer->process proc-child-ptr-0)))
                 (set! proc-child-ptr-0 (-> proc-child-ptr-0 0 brother))
                 (if (type? proc-child-0 blocking-plane)
                     (send-event proc-child-0 'on)
                     )
                 )
               )
             )
           (the-as int #f)
           )
         )
       )
      (('off)
       (cond
         ((nonzero? (-> self root))
          (let ((v1-13 (-> self root root-prim)))
            (set! (-> v1-13 prim-core collide-as) (collide-spec))
            (set! (-> v1-13 prim-core collide-with) (collide-spec))
            )
          0
          )
         (else
           (let ((proc-child-ptr-1 (-> self child)))
             (while proc-child-ptr-1
               (let ((proc-child-1 (ppointer->process proc-child-ptr-1)))
                 (set! proc-child-ptr-1 (-> proc-child-ptr-1 0 brother))
                 (if (type? proc-child-1 blocking-plane)
                     (send-event proc-child-1 'off)
                     )
                 )
               )
             )
           (the-as int #f)
           )
         )
       )
      )
    )
  :code sleep-code
  )

(defmethod init! ((this blocking-plane) (vec-pair (inline-array vector)) (height float))
  "TODO - but sets up the plane given 2 vectors and a height"
  (let ((s3-0 (-> vec-pair 0))
        (s4-0 (-> vec-pair 1))
        )
    0.0
    (* 0.5 (vector-vector-distance s3-0 s4-0))
    (let ((s2-0 (new 'process 'collide-shape this (collide-list-enum usually-hit-by-player))))
      (let ((v1-3 (new 'process 'collide-shape-prim-mesh s2-0 (the-as uint 0) (the-as uint 0))))
        (set! (-> v1-3 prim-core collide-as) (collide-spec blocking-plane))
        (set! (-> v1-3 prim-core collide-with) (collide-spec jak player-list))
        (set! (-> v1-3 prim-core action) (collide-action solid))
        (set! (-> v1-3 transform-index) 3)
        (set! (-> s2-0 total-prims) (the-as uint 1))
        (set! (-> s2-0 root-prim) v1-3)
        )
      (set! (-> s2-0 nav-radius) (* 0.75 (-> s2-0 root-prim local-sphere w)))
      (let ((v1-6 (-> s2-0 root-prim)))
        (set! (-> s2-0 backup-collide-as) (-> v1-6 prim-core collide-as))
        (set! (-> s2-0 backup-collide-with) (-> v1-6 prim-core collide-with))
        )
      (set! (-> this root) s2-0)
      )
    (let ((s1-0 (new 'stack-no-clear 'matrix))
          (s2-1 (-> this root))
          )
      (vector+! (-> s2-1 trans) s3-0 s4-0)
      (vector-float*! (-> s2-1 trans) (-> s2-1 trans) 0.5)
      (+! (-> s2-1 trans y) (/ height 2))
      (vector-! (the-as vector (-> s1-0 vector)) s4-0 s3-0)
      (let ((f30-1 (vector-normalize-ret-len! (the-as vector (-> s1-0 vector)) 1.0)))
        (set! (-> s2-1 scale x) (/ f30-1 METER_LENGTH))
        (set! (-> s2-1 scale y) (/ height METER_LENGTH))
        (set! (-> s2-1 scale z) 0.0)
        (set! (-> s1-0 vector 1 quad) (-> (new 'static 'vector :y 1.0 :w 1.0) quad))
        (vector-cross! (-> s1-0 vector 2) (the-as vector (-> s1-0 vector)) (-> s1-0 vector 1))
        (vector-normalize! (-> s1-0 vector 2) 1.0)
        (matrix->quaternion (-> s2-1 quat) s1-0)
        (let ((v1-20 (-> this root root-prim local-sphere)))
          (set! (-> v1-20 x) 0.0)
          (set! (-> v1-20 y) (* 0.00024414062 (/ height 2)))
          (set! (-> v1-20 z) 0.0)
          (let ((f0-17 0.5)
                (f1-7 (* f30-1 f30-1))
                (f2-2 height)
                )
            (set! (-> v1-20 w) (* f0-17 (sqrtf (+ f1-7 (* f2-2 f2-2)))))
            )
          )
        )
      )
    )
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-blocking-plane" (the-as (pointer uint32) #f)))
    (the-as pair 0)
    )
  (logior! (-> this draw status) (draw-control-status no-draw-bounds))
  (transform-post)
  (none)
  )

;; WARN: Return type mismatch object vs none.
(defbehavior blocking-plane-init-by-other blocking-plane ((vec-pair (inline-array vector)) (height float))
  "Calls [[blocking-plane::21]] with the given args"
  (if (not vec-pair)
      (deactivate self)
      )
  (init! self vec-pair height)
  (go-virtual idle)
  (none)
  )

;; WARN: Return type mismatch object vs none.
(defmethod init-from-entity! ((this blocking-plane) (arg0 entity-actor))
  "Typically the method that does the initial setup on the process, potentially using the [[entity-actor]] provided as part of that.
   This commonly includes things such as:
   - stack size
   - collision information
   - loading the skeleton group / bones
   - sounds"
  (let ((s5-0 (new 'process 'path-control this 'path 0.0 (the-as entity #f) #f))
        (f30-0 (res-lump-float (-> this entity) 'height :default 122880.0))
        )
    (set! (-> this path) s5-0)
    (if (or (not s5-0) (< (-> s5-0 curve num-cverts) 2))
        (go process-drawable-art-error "bad path")
        )
    (logior! (-> s5-0 flags) (path-control-flag display draw-line draw-point draw-text))
    (let ((s4-0 (+ (-> s5-0 curve num-cverts) -1))
          (s3-0 (new 'stack-no-clear 'inline-array 'vector 2))
          )
      (dotimes (v1-12 2)
        (set! (-> s3-0 v1-12 quad) (the-as uint128 0))
        )
      (dotimes (s2-0 s4-0)
        (get-point-in-path! s5-0 (-> s3-0 0) (the float s2-0) 'interp)
        (get-point-in-path! s5-0 (-> s3-0 1) (the float (+ s2-0 1)) 'interp)
        (process-spawn blocking-plane s3-0 f30-0 :to this)
        )
      )
    )
  (go (method-of-object this idle))
  (none)
  )

(defbehavior blocking-plane-spawn process ((arg0 curve-control) (arg1 (inline-array vector)) (arg2 float))
  (cond
    ((and arg1 (or (not arg0) (logtest? (-> arg0 flags) (path-control-flag not-found))))
     (process-spawn blocking-plane arg1 arg2 :to self)
     )
    (else
      (let ((s4-1 (the int (get-num-segments arg0)))
            (s3-0 0)
            (s2-0 (new 'stack-no-clear 'inline-array 'vector 2))
            )
        (dotimes (v1-10 2)
          (set! (-> s2-0 v1-10 quad) (the-as uint128 0))
          )
        (while (< s3-0 s4-1)
          (get-point-in-path! arg0 (-> s2-0 0) (the float s3-0) 'interp)
          (get-point-in-path! arg0 (-> s2-0 1) (the float (+ s3-0 1)) 'interp)
          (process-spawn blocking-plane s2-0 arg2 :to self)
          (+! s3-0 2)
          )
        )
      )
    )
  0
  (none)
  )

(defbehavior blocking-plane-destroy blocking-plane ()
  (let ((child-proc-ptr (-> self child)))
    (while child-proc-ptr
      (let ((child-proc (ppointer->process child-proc-ptr)))
        (set! child-proc-ptr (-> child-proc-ptr 0 brother))
        (if (type? child-proc blocking-plane)
            (deactivate child-proc)
            )
        )
      )
    )
  0
  (none)
  )
