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

;; name: actor-hash.gc
;; name in dgo: actor-hash
;; dgos: ENGINE, GAME

;; DECOMP BEGINS

(kmemopen global "actor-hash")

(define *actor-hash* (new 'global 'spatial-hash 4096 256))

(kmemclose)

(deftype actor-cshape-ptr (structure)
  ((cshape  collide-shape)
   )
  )


(deftype actor-hash-bucket (structure)
  ((length      int16)
   (max-length  int16)
   (data        (inline-array actor-cshape-ptr))
   )
  :allow-misaligned
  (:methods
    (add-actor-cshape (_type_ collide-shape) none)
    )
  )


(defmethod add-actor-cshape ((this actor-hash-bucket) (arg0 collide-shape))
  (let ((v1-0 (-> this length)))
    (when (< v1-0 (-> this max-length))
      (set! (-> this data v1-0 cshape) arg0)
      (set! (-> this length) (+ v1-0 1))
      )
    )
  0
  (none)
  )

(deftype actor-hash-buckets (structure)
  ((hash  spatial-hash)
   (list  engine)
   (data  actor-hash-bucket  4 :inline)
   (tpos  vector             :inline)
   )
  (:methods
    (hash-actors (_type_) none)
    )
  )


(defmethod hash-actors ((this actor-hash-buckets))
  (local-vars (sv-16 hash-object-info) (sv-32 collide-prim-core) (sv-48 collide-shape) (sv-64 hash-object-info))
  (set! (-> this hash) *actor-hash*)
  (set! (-> this list) *collide-hit-by-others-list*)
  (clear-objects! (-> this hash))
  (cond
    ((>= 256 (-> this list length))
     (let ((v1-5 (-> this list alive-list next0)))
       (-> this list)
       (let ((s5-0 (-> v1-5 next0)))
         (while (!= v1-5 (-> this list alive-list-end))
           (let* ((s4-0 (the-as collide-shape (-> (the-as connection v1-5) param1)))
                  (s3-0 (-> this hash))
                  (s1-0 (-> s4-0 root-prim prim-core))
                  (s0-0 s4-0)
                  (s2-0 (-> s3-0 object-count))
                  )
             (set! s2-0
                   (cond
                     ((< s2-0 (-> s3-0 max-object-count))
                      (let ((a0-7 (-> s3-0 sphere-array s2-0)))
                        (set! sv-16 (-> s3-0 object-array s2-0))
                        (mem-copy! (the-as pointer a0-7) (the-as pointer s1-0) 16)
                        )
                      (set! (-> sv-16 object) s0-0)
                      (dotimes (v1-12 3)
                        (set! (-> s3-0 box-min v1-12)
                              (fmin (-> s3-0 box-min v1-12) (- (-> s1-0 world-sphere data v1-12) (-> s1-0 world-sphere w)))
                              )
                        (set! (-> s3-0 box-max v1-12)
                              (fmax (-> s3-0 box-max v1-12) (+ (-> s1-0 world-sphere data v1-12) (-> s1-0 world-sphere w)))
                              )
                        )
                      (+! (-> s3-0 object-count) 1)
                      s2-0
                      )
                     (else
                       -1
                       )
                     )
                   )
             (set! (-> s4-0 actor-hash-index) s2-0)
             )
           (set! v1-5 s5-0)
           (-> this list)
           (set! s5-0 (-> s5-0 next0))
           )
         )
       )
     )
    (else
      (set! (-> this tpos quad) (-> (target-pos 0) quad))
      (dotimes (v1-21 4)
        (set! (-> this data v1-21 length) 0)
        )
      (let ((v1-25 (-> this list alive-list next0)))
        (-> this list)
        (let ((s5-2 (-> v1-25 next0)))
          (while (!= v1-25 (-> this list alive-list-end))
            (let* ((s4-1 (the-as collide-shape (-> (the-as connection v1-25) param1)))
                   (f0-4 (vector-vector-distance-squared (-> this tpos) (-> s4-1 trans)))
                   (f1-4 102400.0)
                   )
              (cond
                ((< f0-4 (* f1-4 f1-4))
                 ((method-of-type actor-hash-bucket add-actor-cshape) (the-as actor-hash-bucket (-> this data)) s4-1)
                 )
                ((let ((f1-7 204800.0))
                   (< f0-4 (* f1-7 f1-7))
                   )
                 (add-actor-cshape (-> this data 1) s4-1)
                 )
                ((let ((f1-10 307200.0))
                   (< f0-4 (* f1-10 f1-10))
                   )
                 (add-actor-cshape (-> this data 2) s4-1)
                 )
                (else
                  (add-actor-cshape (-> this data 3) s4-1)
                  )
                )
              )
            (set! v1-25 s5-2)
            (-> this list)
            (set! s5-2 (-> s5-2 next0))
            )
          )
        )
      (dotimes (s5-3 4)
        (let ((s4-2 (-> this data s5-3)))
          (countdown (s3-1 (-> s4-2 length))
            (let ((s2-1 (-> s4-2 data s3-1 cshape))
                  (s1-1 (-> this hash))
                  )
              (set! sv-32 (-> s2-1 root-prim prim-core))
              (set! sv-48 s2-1)
              (let ((s0-1 (-> s1-1 object-count)))
                (set! s0-1
                      (cond
                        ((< s0-1 (-> s1-1 max-object-count))
                         (let ((a0-40 (-> s1-1 sphere-array s0-1)))
                           (set! sv-64 (-> s1-1 object-array s0-1))
                           (mem-copy! (the-as pointer a0-40) (the-as pointer sv-32) 16)
                           )
                         (set! (-> sv-64 object) sv-48)
                         (dotimes (v1-53 3)
                           (set! (-> s1-1 box-min v1-53)
                                 (fmin (-> s1-1 box-min v1-53) (- (-> sv-32 world-sphere data v1-53) (-> sv-32 world-sphere w)))
                                 )
                           (set! (-> s1-1 box-max v1-53)
                                 (fmax (-> s1-1 box-max v1-53) (+ (-> sv-32 world-sphere data v1-53) (-> sv-32 world-sphere w)))
                                 )
                           )
                         (+! (-> s1-1 object-count) 1)
                         s0-1
                         )
                        (else
                          -1
                          )
                        )
                      )
                (set! (-> s2-1 actor-hash-index) s0-1)
                )
              )
            )
          )
        )
      )
    )
  (update-from-spheres (-> this hash))
  0
  (none)
  )

(define *actor-hash-buckets*
  (new 'static 'actor-hash-buckets
    :data (new 'static 'inline-array actor-hash-bucket 4
      (new 'static 'actor-hash-bucket :max-length #x100 :data (new 'static 'inline-array actor-cshape-ptr 256))
      (new 'static 'actor-hash-bucket :max-length #x100 :data (new 'static 'inline-array actor-cshape-ptr 256))
      (new 'static 'actor-hash-bucket :max-length #x100 :data (new 'static 'inline-array actor-cshape-ptr 256))
      (new 'static 'actor-hash-bucket :max-length #x100 :data (new 'static 'inline-array actor-cshape-ptr 256))
      )
    )
  )

(defun update-actor-hash ()
  (reset! (-> *perf-stats* data (perf-stat-bucket actor-hash)))
  (hash-actors *actor-hash-buckets*)
  (read! (-> *perf-stats* data (perf-stat-bucket actor-hash)))
  (none)
  )
