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

;; name: collide-mesh-h.gc
;; name in dgo: collide-mesh-h
;; dgos: ENGINE, GAME

(declare-type collide-mesh-cache-tri structure)
(declare-type collide-shape-prim-mesh basic)

;; DECOMP BEGINS

(deftype collide-tri-result (structure)
  ((vertex       vector  3 :inline)
   (intersect    vector  :inline)
   (normal       vector  :inline)
   (pat          pat-surface)
   (collide-ptr  basic)
   )
  )


(deftype collide-mesh-tri (structure)
  ((vertex-index  uint8  3)
   (unused        uint8)
   (pat           pat-surface)
   )
  :pack-me
  )


(deftype collide-mesh (basic)
  ((joint-id     int32)
   (num-tris     uint32)
   (num-verts    uint32)
   (vertex-data  (inline-array vector))
   (tris         collide-mesh-tri  1 :inline :offset  32)
   )
  (:methods
    (debug-draw-tris (_type_ process-drawable int) none)
    (overlap-test (_type_ collide-mesh-cache-tri vector) symbol)
    (should-push-away-test (_type_ collide-mesh-cache-tri collide-tri-result vector float) float)
    (sphere-on-platform-test (_type_ collide-mesh-cache-tri collide-tri-result vector float) float)
    (unpack-mesh-to-cache! (_type_ (inline-array collide-mesh-cache-tri) matrix) none)
    (collide-mesh-math-1 (_type_ object object) none)
    (collide-mesh-math-2 (_type_ object object object) none)
    )
  )


(deftype collide-mesh-cache-tri (structure)
  ((vertex  vector          3 :inline)
   (normal  vector          :inline)
   (bbox4w  bounding-box4w  :inline)
   (pat     pat-surface     :overlay-at (-> normal data 3))
   )
  )


(deftype collide-mesh-cache-entry (structure)
  ((mat   matrix                  :inline)
   (tris  collide-mesh-cache-tri  :inline :dynamic)
   )
  )


(deftype collide-mesh-cache (basic)
  ((used-size  uint32)
   (max-size   uint32)
   (id         uint32)
   (data       uint8  48000)
   )
  (:methods
    (populate-for-prim-mesh (_type_ collide-shape-prim-mesh) collide-mesh-cache-entry)
    (is-id? (_type_ int) symbol)
    (next-id! (_type_) uint)
    (allocate! (_type_ int) collide-mesh-cache-entry)
    )
  )

(defmethod next-id! ((this collide-mesh-cache))
  "Reset all used entries in the cache and increment the id.
   If the id is zero, set it to 1"
  ;;     ld v1, 12(a0)
  (let ((v1 (-> this id)))
    ;;     sw r0, 0(a0)
    (set! (-> this used-size) 0)
    ;;     daddiu v0, v1, 1
    (let ((v0 (+ v1 1)))
      ;;     beql v0, r0, L3
      ;;     addiu v0, r0, 1 (only taken if v0 = 0)
      (if (= v0 0)
          (set! v0 (the uint 1))
          )
      ;; L3:
      ;;     sd v0, 12(a0)
      (set! (-> this id) v0)
      v0
      )
    )
  )

(defmethod is-id? ((this collide-mesh-cache) (arg0 int))
  (= (-> this id) arg0)
  )

(kmemopen global "collide-mesh-cache")

(define-perm *collide-mesh-cache* collide-mesh-cache (new 'global 'collide-mesh-cache))

(set! (-> *collide-mesh-cache* id) (the-as uint 1))

(set! (-> *collide-mesh-cache* used-size) (the-as uint 0))

(set! (-> *collide-mesh-cache* max-size) (the-as uint #xbb80))

(kmemclose)
