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

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

;; NOTE - for cam-update
(declare-type instance-tie-work structure)
(declare-type prototype-tie-work structure)
(define-extern *instance-tie-work* instance-tie-work)
(define-extern *prototype-tie-work* prototype-tie-work)

(define-extern tie-scissor-make-perspective-matrix (function matrix matrix none))

;; DECOMP BEGINS

(deftype tie-fragment-debug (structure)
  ((num-tris     uint16)
   (num-dverts   uint16)
   (debug-lines  (array vector-array))
   )
  )


(deftype tie-fragment (drawable)
  ((gif-ref        (inline-array adgif-shader)  :overlay-at id)
   (point-ref      uint32                       :offset   8)
   (color-index    uint16                       :offset  12)
   (base-colors    uint8                        :offset  14)
   (tex-count      uint16                       :offset  32)
   (gif-count      uint16                       :offset  34)
   (vertex-count   uint16                       :offset  36)
   (color-count    uint16                       :offset  38)
   (dp-ref         uint32                       :offset  40)
   (dp-qwc         uint32                       :offset  44)
   (generic-ref    uint32                       :offset  48)
   (generic-count  uint16                       :offset  52)
   (normal-count   uint16                       :offset  54)
   (normal-ref     uint32                       :offset  56)
   (debug          tie-fragment-debug           :offset  60)
   )
  )


(deftype instance-tie (instance)
  ((color-indices  uint32                :offset   8)
   (bucket-ptr     prototype-bucket-tie  :offset  12)
   (max-scale      uint16                :overlay-at (-> origin data 3))
   (rmin-scale     uint16                :overlay-at (-> origin data 11))
   )
  )


(deftype drawable-inline-array-instance-tie (drawable-inline-array)
  ((data  instance-tie  1 :inline)
   (pad   uint32)
   )
  )

(deftype drawable-tree-instance-tie (drawable-tree)
  ((prototypes  proxy-prototype-array-tie  :offset   8)
   )
  )


(deftype prototype-tie (drawable-inline-array)
  ((data  tie-fragment  1 :inline)
   (pad   uint32)
   )
  )

(deftype tie-matrix (structure)
  ((mat           matrix  :inline)
   (morph         qword   :inline)
   (fog           qword   :inline)
   (envmap-flag   uint32  :overlay-at (-> fog data 0))
   (guard-flag    uint32  :overlay-at (-> fog data 1))
   (vertex-alpha  float   :overlay-at (-> fog data 2))
   (fog-value     float   :overlay-at (-> fog data 3))
   (fixed-alpha   float   :overlay-at (-> morph data 1))
   )
  )


(deftype instance-tie-work (structure)
  ((wind-const                      vector      :inline)
   (hmge-d                          vector      :inline)
   (hvdf-offset                     vector      :inline)
   (wind-force                      vector      :inline)
   (constant                        vector      :inline)
   (far-morph                       vector      :inline)
   (dist-test                       vector      :inline)
   (min-dist                        vector      :inline)
   (guard-plane                     plane       4 :inline)
   (upload-color-0                  dma-packet  :inline)
   (upload-color-1                  dma-packet  :inline)
   (upload-color-2                  dma-packet  :inline)
   (upload-color-ret                dma-packet  :inline)
   (upload-color-temp               dma-packet  :inline)
   (generic-color-0                 dma-packet  :inline)
   (generic-color-1                 dma-packet  :inline)
   (generic-color-end               dma-packet  :inline)
   (envmap-color-0                  dma-packet  :inline)
   (envmap-color-1                  dma-packet  :inline)
   (tie-scissor-perspective-matrix  matrix      :inline)
   (tod-env-color                   vector      :inline)
   (morph-temp                      vector      :inline)
   (fog-temp                        vector      :inline)
   (fade-temp                       float)
   (wind-vectors                    uint32)
   (test-id                         uint32)
   (test-id2                        uint32)
   (dma-buffer                      basic)
   (to-spr                          uint32)
   (from-spr                        uint32)
   (wind-work                       uint32)
   (cur-vis-bits                    uint32)
   (end-vis-bits                    uint32)
   (refl-fade-fac                   float)
   (refl-fade-end                   float)
   (flags                           uint32)
   (vanish-flag                     uint32)
   (translucent-flag                uint32)
   (wait-from-spr                   uint32)
   (wait-to-spr                     uint32)
   (use-etie                        symbol)
   (buffer-start                    uint32)
   (buffer-end                      uint32)
   (tfrag-dists                     uint32)
   (alpha-dists                     uint32)
   (water-dists                     uint32)
   )
  )


(deftype instance-tie-dma (structure)
  ((banka  instance-tie       32 :inline)
   (bankb  instance-tie       32 :inline)
   (outa   uint128            256)
   (outb   uint128            256)
   (work   instance-tie-work  :dynamic)
   )
  )


(deftype prototype-tie-work (structure)
  ((upload-flushe               dma-packet  :inline)
   (upload-palette              dma-packet  :inline)
   (upload-model-0              dma-packet  :inline)
   (upload-model-1              dma-packet  :inline)
   (upload-model-2              dma-packet  :inline)
   (upload-model-3              dma-packet  :inline)
   (upload-model-near-0         dma-packet  :inline)
   (upload-model-near-1         dma-packet  :inline)
   (upload-model-near-2         dma-packet  :inline)
   (upload-model-near-3         dma-packet  :inline)
   (upload-model-near-4         dma-packet  :inline)
   (envmap-palette              dma-packet  :inline)
   (envmap-shader               dma-packet  :inline)
   (upload-envmap-0             dma-packet  :inline)
   (upload-envmap-1             dma-packet  :inline)
   (upload-envmap-2             dma-packet  :inline)
   (upload-envmap-3             dma-packet  :inline)
   (upload-envmap-4             dma-packet  :inline)
   (upload-envmap-scissor-4     dma-packet  :inline)
   (generic-palette             dma-packet  :inline)
   (generic-model-0             dma-packet  :inline)
   (generic-model-1             dma-packet  :inline)
   (generic-model-2             dma-packet  :inline)
   (model-next                  dma-packet  :inline)
   (clamp                       uint64)
   (prototype-array             basic)
   (wait-from-spr               uint32)
   (wait-to-spr                 uint32)
   (mood                        mood-context)
   (last                        uint32      16 :offset 416)
   (next                        uint32      16)
   (count                       uint16      16)
   (tie-last                    uint32         :overlay-at (-> last 0))
   (tie-next                    uint32         :overlay-at (-> next 0))
   (tie-count                   uint16         :overlay-at (-> count 0))
   (trans-last                  uint32         :overlay-at (-> last 1))
   (trans-next                  uint32         :overlay-at (-> next 1))
   (trans-count                 uint16         :overlay-at (-> count 1))
   (water-last                  uint32         :overlay-at (-> last 2))
   (water-next                  uint32         :overlay-at (-> next 2))
   (water-count                 uint16         :overlay-at (-> count 2))
   (scissor-last                uint32         :overlay-at (-> last 3))
   (scissor-next                uint32         :overlay-at (-> next 3))
   (scissor-count               uint16         :overlay-at (-> count 3))
   (scissor-trans-last          uint32         :overlay-at (-> last 4))
   (scissor-trans-next          uint32         :overlay-at (-> next 4))
   (scissor-trans-count         uint16         :overlay-at (-> count 4))
   (scissor-water-last          uint32         :overlay-at (-> last 5))
   (scissor-water-next          uint32         :overlay-at (-> next 5))
   (scissor-water-count         uint16         :overlay-at (-> count 5))
   (envmap-last                 uint32         :overlay-at (-> last 6))
   (envmap-next                 uint32         :overlay-at (-> next 6))
   (envmap-count                uint16         :overlay-at (-> count 6))
   (envmap-trans-last           uint32         :overlay-at (-> last 7))
   (envmap-trans-next           uint32         :overlay-at (-> next 7))
   (envmap-trans-count          uint16         :overlay-at (-> count 7))
   (envmap-water-last           uint32         :overlay-at (-> last 8))
   (envmap-water-next           uint32         :overlay-at (-> next 8))
   (envmap-water-count          uint16         :overlay-at (-> count 8))
   (envmap-scissor-last         uint32         :overlay-at (-> last 9))
   (envmap-scissor-next         uint32         :overlay-at (-> next 9))
   (envmap-scissor-count        uint16         :overlay-at (-> count 9))
   (envmap-scissor-trans-last   uint32         :overlay-at (-> last 10))
   (envmap-scissor-trans-next   uint32         :overlay-at (-> next 10))
   (envmap-scissor-trans-count  uint16         :overlay-at (-> count 10))
   (envmap-scissor-water-last   uint32         :overlay-at (-> last 11))
   (envmap-scissor-water-next   uint32         :overlay-at (-> next 11))
   (envmap-scissor-water-count  uint16         :overlay-at (-> count 11))
   (generic-last                uint32         :overlay-at (-> last 12))
   (generic-next                uint32         :overlay-at (-> next 12))
   (generic-count               uint16         :overlay-at (-> count 12))
   (generic-trans-last          uint32         :overlay-at (-> last 13))
   (generic-trans-next          uint32         :overlay-at (-> next 13))
   (generic-trans-count         uint16         :overlay-at (-> count 13))
   (generic-water-last          uint32         :overlay-at (-> last 14))
   (generic-water-next          uint32         :overlay-at (-> next 14))
   (generic-water-count         uint16         :overlay-at (-> count 14))
   (vanish-last                 uint32         :overlay-at (-> last 15))
   (vanish-next                 uint32         :overlay-at (-> next 15))
   (vanish-count                uint16         :overlay-at (-> count 15))
   )
  )


(deftype prototype-tie-dma (structure)
  ((colora                rgba     256)
   (colorb                rgba     256)
   (outa                  uint128  256)
   (outb                  uint128  256)
   (geometry              uint32   4)
   (next                  uint32   12 :offset 10256)
   (count                 uint16   12 :offset 10304)
   (counts                uint32   4  :offset 10328)
   (palette-ptr           uint32      :overlay-at (-> counts 2))
   (model-ptr             uint32      :overlay-at (-> counts 3))
   (ret-ptr               uint32      :offset 10344)
   (length                uint32      :offset 10348)
   (flags                 uint32      :offset 10352)
   (dma-buffer            basic       :offset 10356)
   (this-frag-count       uint32      :offset 10360)
   (frag-count            uint8    4  :offset 10364)
   (from-spr              uint32      :offset 10368)
   (to-spr                uint32      :offset 10372)
   (spr-out               uint32      :offset 10376)
   (this-count            uint32      :offset 10380)
   (scissor-geometry      uint32      :overlay-at (-> geometry 0))
   (near-geometry         uint32      :overlay-at (-> geometry 1))
   (mid-geometry          uint32      :overlay-at (-> geometry 2))
   (far-geometry          uint32      :overlay-at (-> geometry 3))
   (scissor-frag-count    uint8       :overlay-at (-> frag-count 0))
   (near-frag-count       uint8       :overlay-at (-> frag-count 1))
   (mid-frag-count        uint8       :overlay-at (-> frag-count 2))
   (far-frag-count        uint8       :overlay-at (-> frag-count 3))
   (tie-scissor-next      uint32      :overlay-at (-> next 0))
   (tie-near-next         uint32      :overlay-at (-> next 1))
   (tie-mid-next          uint32      :overlay-at (-> next 2))
   (tie-far-next          uint32      :overlay-at (-> next 3))
   (trans-scissor-next    uint32   4  :overlay-at tie-scissor-next)
   (trans-near-next       uint32      :overlay-at tie-near-next)
   (trans-mid-next        uint32      :overlay-at tie-mid-next)
   (trans-far-next        uint32      :overlay-at tie-far-next)
   (water-scissor-next    uint32   4  :overlay-at tie-scissor-next)
   (water-near-next       uint32      :overlay-at trans-near-next)
   (water-mid-next        uint32      :overlay-at trans-mid-next)
   (water-far-next        uint32      :overlay-at trans-far-next)
   (envmap-scissor-next   uint32   4  :overlay-at (-> next 4))
   (envmap-near-next      uint32      :overlay-at (-> envmap-scissor-next 1))
   (envmap-mid-next       uint32      :overlay-at (-> envmap-scissor-next 2))
   (envmap-far-next       uint32      :overlay-at (-> envmap-scissor-next 3))
   (generic-near-next     uint32      :overlay-at (-> next 8))
   (generic-mid-next      uint32      :overlay-at (-> next 9))
   (generic-far-next      uint32      :overlay-at (-> next 10))
   (vanish-next           uint32      :overlay-at (-> next 11))
   (tie-count             uint16      :overlay-at (-> count 0))
   (tie-scissor-count     uint16      :overlay-at tie-count)
   (tie-near-count        uint16      :overlay-at (-> count 1))
   (tie-mid-count         uint16      :overlay-at (-> count 2))
   (tie-far-count         uint16      :overlay-at (-> count 3))
   (trans-count           uint16      :overlay-at tie-scissor-count)
   (trans-scissor-count   uint16      :overlay-at trans-count)
   (trans-near-count      uint16      :overlay-at tie-near-count)
   (trans-mid-count       uint16      :overlay-at tie-mid-count)
   (trans-far-count       uint16      :overlay-at tie-far-count)
   (water-count           uint16      :overlay-at trans-scissor-count)
   (water-scissor-count   uint16      :overlay-at water-count)
   (water-near-count      uint16      :overlay-at trans-near-count)
   (water-mid-count       uint16      :overlay-at trans-mid-count)
   (water-far-count       uint16      :overlay-at trans-far-count)
   (envmap-count          uint16      :overlay-at (-> count 4))
   (envmap-scissor-count  uint16      :overlay-at envmap-count)
   (envmap-near-count     uint16      :overlay-at (-> count 5))
   (envmap-mid-count      uint16      :overlay-at (-> count 6))
   (envmap-far-count      uint16      :overlay-at (-> count 7))
   (generic-count         uint16      :overlay-at (-> count 8))
   (generic-near-count    uint16      :overlay-at generic-count)
   (generic-mid-count     uint16      :overlay-at (-> count 9))
   (generic-far-count     uint16      :overlay-at (-> count 10))
   (vanish-count          uint16      :overlay-at (-> count 11))
   (next-clear            uint32   3  :overlay-at tie-scissor-next)
   (count-clear           uint16   3  :overlay-at water-scissor-count)
   )
  )


(define *instance-tie-work-copy* (the-as instance-tie-work #f))
(define-extern draw-drawable-tree-instance-tie (function drawable-tree-instance-tie level none))