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

;; name: rigid-body-queue.gc
;; name in dgo: rigid-body-queue
;; dgos: GAME, COMMON

;; DECOMP BEGINS

(defmethod rigid-body-queue-method-9 ((this rigid-body-queue))
  (set! (-> this count) 0)
  (dotimes (v1-0 128)
    (set! (-> this array v1-0) (the-as handle #f))
    )
  0
  (none)
  )

(defmethod validate ((this rigid-body-queue))
  (let ((gp-0 0))
    (dotimes (v1-0 (-> this count))
      (let ((a1-2 (-> this array v1-0))
            (a2-0 (+ v1-0 1))
            )
        (while (< a2-0 (-> this count))
          (if (= a1-2 (-> this array a2-0))
              (+! gp-0 1)
              )
          (+! a2-0 1)
          )
        )
      )
    (if (> gp-0 0)
        (format 0 "rigid-body-queue::validate: duplicate count ~d~%" gp-0)
        )
    (zero? gp-0)
    )
  )

(defmethod rigid-body-queue-method-10 ((this rigid-body-queue))
  (local-vars (s4-0 process))
  (with-pp
    (let ((f0-0 (seconds-per-frame))
          (v1-1 (-> this count))
          )
      (b! #t cfg-9 :delay (nop!))
      (label cfg-1)
      (+! v1-1 -1)
      (let ((a0-4 (handle->process (-> this array v1-1))))
        (cond
          (a0-4
            (let ((a0-6 (-> (the-as rigid-body-object a0-4) rbody)))
              (set! (-> a0-6 state time-remaining) f0-0)
              (set! (-> a0-6 state blocked-by) #f)
              (set! (-> a0-6 state step-count) 0)
              (logclear! (-> a0-6 state flags) (rigid-body-flag blocker))
              )
            )
          (else
            )
          )
        )
      (label cfg-9)
      (b! (nonzero? v1-1) cfg-1 :delay (nop!))
      )
    (let ((s5-0 0))
      (b! #t cfg-35 :delay (nop!))
      (until (= (-> this array s5-0) (process->handle s4-0))
        (label cfg-11)
        (set! s4-0 (handle->process (-> this array s5-0)))
        (b! (not s4-0) cfg-34 :delay (empty-form))
        (let ((s3-0 (-> (the-as rigid-body-object s4-0) rbody)))
          (b!
            (not (and (logtest? (-> s3-0 state flags) (rigid-body-flag enable-physics))
                      (and (< 0.001 (-> s3-0 state time-remaining)) (< (-> s3-0 state step-count) 4))
                      )
                 )
            cfg-34
            :delay (empty-form)
            )
          (rigid-body-object-method-51 (the-as rigid-body-object s4-0))
          (+! (-> s3-0 state step-count) 1)
          (let ((a2-2 (-> s3-0 state blocked-by)))
            (b! (not a2-2) cfg-34 :delay (empty-form))
            (b! (not #t) cfg-34 :delay (empty-form))
            (logior! (-> a2-2 rbody state flags) (rigid-body-flag blocker))
            (rigid-body-queue-method-13 this s5-0 a2-2)
            )
          )
        )
      (label cfg-34)
      (+! s5-0 1)
      (label cfg-35)
      (b! (< s5-0 (-> this count)) cfg-11)
      )
    (let ((s5-1 0))
      (b! #t cfg-48 :delay (nop!))
      (label cfg-37)
      (let ((a0-20 (handle->process (-> this array s5-1))))
        (when (and a0-20
                   (logtest? (-> (the-as rigid-body-object a0-20) rbody state flags) (rigid-body-flag enable-physics))
                   )
          (let ((s4-1 pp))
            (set! pp (the-as rigid-body-object a0-20))
            (rigid-body-object-method-52 (the-as rigid-body-object a0-20))
            (set! pp s4-1)
            )
          )
        )
      (+! s5-1 1)
      (label cfg-48)
      (b! (< s5-1 (-> this count)) cfg-37)
      )
    0
    (none)
    )
  )

(defmethod rigid-body-queue-method-11 ((this rigid-body-queue) (arg0 rigid-body-object))
  (let ((v1-0 -1))
    (let ((a2-0 0))
      (b! #t cfg-9 :delay (nop!))
      (label cfg-1)
      (b! (handle->process (-> this array a2-0)) cfg-8 :delay (empty-form))
      (set! v1-0 a2-0)
      (b! #t cfg-11 :delay (nop!))
      (label cfg-8)
      (+! a2-0 1)
      (label cfg-9)
      (b! (< a2-0 (-> this count)) cfg-1)
      )
    (label cfg-11)
    (b! (= v1-0 -1) cfg-18 :delay (nop!))
    (set! (-> this array v1-0) (process->handle arg0))
    )
  (b! #t cfg-25 :delay (nop!))
  (label cfg-18)
  (b! (>= (-> this count) 128) cfg-25 :delay #f)
  (set! (-> this array (-> this count)) (process->handle arg0))
  (+! (-> this count) 1)
  (label cfg-25)
  0
  0
  (none)
  )

(defmethod rigid-body-queue-method-12 ((this rigid-body-queue) (arg0 int) (arg1 int))
  (when (< arg0 arg1)
    (let ((v1-1 arg1)
          (a3-0 (+ arg1 -1))
          (a2-3 (-> this array arg1))
          )
      (while (>= a3-0 arg0)
        (set! (-> this array v1-1) (-> this array a3-0))
        (+! a3-0 -1)
        (+! v1-1 -1)
        )
      (set! (-> this array arg0) a2-3)
      )
    )
  0
  (none)
  )

(defmethod rigid-body-queue-method-13 ((this rigid-body-queue) (arg0 int) (arg1 rigid-body-object))
  (let ((v1-2 (process->handle arg1))
        (a2-4 (+ arg0 1))
        )
    (b! #t cfg-9 :delay (nop!))
    (label cfg-6)
    (b! (!= (-> this array a2-4) v1-2) cfg-8 :delay (empty-form))
    (rigid-body-queue-method-12 this arg0 a2-4)
    (b! #t cfg-11 :delay (nop!))
    (label cfg-8)
    (+! a2-4 1)
    (label cfg-9)
    (b! (< a2-4 (-> this count)) cfg-6)
    )
  (label cfg-11)
  0
  0
  (none)
  )

(defmethod rigid-body-queue-method-14 ((this rigid-body-queue) (arg0 int))
  (let ((v1-0 arg0)
        (a1-1 (+ arg0 1))
        )
    (while (< a1-1 (-> this count))
      (set! (-> this array v1-0) (-> this array a1-1))
      (+! a1-1 1)
      (+! v1-0 1)
      )
    )
  (+! (-> this count) -1)
  0
  (none)
  )

(defmethod rigid-body-queue-method-15 ((this rigid-body-queue) (arg0 rigid-body-object))
  (let ((v1-2 (process->handle arg0))
        (a1-4 0)
        )
    (b! #t cfg-9 :delay (nop!))
    (label cfg-6)
    (b! (!= (-> this array a1-4) v1-2) cfg-8 :delay (empty-form))
    (rigid-body-queue-method-14 this a1-4)
    (b! #t cfg-11 :delay (nop!))
    (label cfg-8)
    (+! a1-4 1)
    (label cfg-9)
    (b! (< a1-4 (-> this count)) cfg-6)
    )
  (label cfg-11)
  0
  0
  (none)
  )

(deftype rigid-body-queue-manager (process)
  ((queue  rigid-body-queue)
   )
  (:state-methods
    idle
    )
  )


(defstate idle (rigid-body-queue-manager)
  :virtual #t
  :exit (behavior ()
    (set! (-> self queue count) 0)
    0
    )
  :code sleep-code
  :post (behavior ()
    (local-vars (a0-3 int) (a0-5 int))
    (let* ((v1-1 (-> *perf-stats* data 17))
           (a0-0 (-> v1-1 ctrl))
           )
      (+! (-> v1-1 count) 1)
      (b! (zero? a0-0) cfg-2 :delay (nop!))
      (.mtc0 Perf 0)
      (.sync.l)
      (.sync.p)
      (.mtpc pcr0 0)
      (.mtpc pcr1 0)
      (.sync.l)
      (.sync.p)
      (.mtc0 Perf a0-0)
      )
    (.sync.l)
    (.sync.p)
    (label cfg-2)
    0
    (rigid-body-queue-method-10 (-> self queue))
    (let ((v1-6 (-> *perf-stats* data 17)))
      (b! (zero? (-> v1-6 ctrl)) cfg-4 :delay (nop!))
      (.mtc0 Perf 0)
      (.sync.l)
      (.sync.p)
      (.mfpc a0-3 pcr0)
      (+! (-> v1-6 accum0) a0-3)
      (.mfpc a0-5 pcr1)
      (+! (-> v1-6 accum1) a0-5)
      )
    (label cfg-4)
    0
    )
  )

(defbehavior rigid-body-queue-manager-init-by-other rigid-body-queue-manager ((arg0 rigid-body-queue))
  (stack-size-set! (-> self main-thread) 128)
  (set! (-> self queue) arg0)
  (rigid-body-queue-method-9 (-> self queue))
  (go-virtual idle)
  )

(defun rigid-body-queue-manager-spawn ((arg0 rigid-body-queue) (arg1 process-tree))
  (let ((gp-0 (the-as process #f)))
    (let ((v1-1 (process-spawn rigid-body-queue-manager arg0 :to arg1)))
      (if v1-1
          (set! gp-0 (-> v1-1 0))
          )
      )
    gp-0
    )
  )
