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

;; definition of type sprite-distorter-sine-tables
(deftype sprite-distorter-sine-tables (basic)
  ((aspx   float                  :offset-assert   4)
   (aspy   float                  :offset-assert   8)
   (entry  vector     128 :inline :offset-assert  16)
   (ientry qword      9 :inline   :offset-assert 2064)
   (giftag gs-gif-tag :inline     :offset-assert 2208)
   (color  qword      :inline     :offset-assert 2224)
   )
  :method-count-assert 9
  :size-assert         #x8c0
  :flag-assert         #x9000008c0
  )

;; definition for method 3 of type sprite-distorter-sine-tables
(defmethod inspect sprite-distorter-sine-tables ((obj sprite-distorter-sine-tables))
  (format #t "[~8x] ~A~%" obj (-> obj type))
  (format #t "~Taspx: ~f~%" (-> obj aspx))
  (format #t "~Taspy: ~f~%" (-> obj aspy))
  (format #t "~Tentry[128] @ #x~X~%" (-> obj entry))
  (format #t "~Tientry[9] @ #x~X~%" (-> obj ientry))
  (format #t "~Tgiftag: #<qword @ #x~X>~%" (-> obj giftag))
  (format #t "~Tcolor: #<qword @ #x~X>~%" (-> obj color))
  obj
  )

;; definition for symbol *sprite-distorter-sine-tables*, type sprite-distorter-sine-tables
(define *sprite-distorter-sine-tables* (new 'global 'sprite-distorter-sine-tables))

;; definition for function sprite-distorter-generate-tables
;; INFO: Return type mismatch int vs none.
(defun sprite-distorter-generate-tables ()
  (let ((gp-0 *sprite-distorter-sine-tables*))
    (let ((s3-0 0)
          (s5-0 0)
          (s4-0 3)
          (f28-0 (- (-> *math-camera* perspective vector 0 x)))
          (f30-0 (- (-> *math-camera* perspective vector 1 y)))
          )
      (when (or (!= f28-0 (-> gp-0 aspx)) (!= f30-0 (-> gp-0 aspy)))
        (set! (-> gp-0 aspx) f28-0)
        (set! (-> gp-0 aspy) f30-0)
        (while (< s4-0 12)
          (set! (-> gp-0 ientry s5-0 vector4w x) (+ s3-0 352))
          (+! s5-0 1)
          (dotimes (s2-0 s4-0)
            (let ((f26-0 (* 65536.0 (/ (the float s2-0) (the float s4-0)))))
              (set-vector! (-> gp-0 entry s3-0) (* (sin f26-0) f28-0) (* (cos f26-0) f30-0) 0.0 0.0)
              (let ((s3-1 (+ s3-0 1)))
                (set-vector! (-> gp-0 entry s3-1) (* 0.001953125 f28-0 (sin f26-0)) (* 0.00390625 f30-0 (cos f26-0)) 0.0 0.0)
                (set! s3-0 (+ s3-1 1))
                )
              )
            )
          (+! s4-0 1)
          )
        (set-vector! (-> gp-0 entry s3-0) 0.0 f30-0 0.0 0.0)
        (let ((v1-17 (+ s3-0 1)))
          (set-vector! (-> gp-0 entry v1-17) 0.0 (* 0.00390625 f30-0) 0.0 0.0)
          (+ v1-17 1)
          )
        )
      )
    (set! (-> gp-0 giftag tag) (new 'static 'gif-tag64
                                 :nloop #x1
                                 :eop #x1
                                 :pre #x1
                                 :prim (new 'static 'gs-prim :prim (gs-prim-type tri-strip) :tme #x1)
                                 :nreg #xf
                                 )
          )
    (set! (-> gp-0 giftag regs) (new 'static 'gif-tag-regs
                                  :regs0 (gif-reg-id st)
                                  :regs1 (gif-reg-id rgbaq)
                                  :regs2 (gif-reg-id xyzf2)
                                  :regs3 (gif-reg-id st)
                                  :regs4 (gif-reg-id rgbaq)
                                  :regs5 (gif-reg-id xyzf2)
                                  :regs6 (gif-reg-id st)
                                  :regs7 (gif-reg-id rgbaq)
                                  :regs8 (gif-reg-id xyzf2)
                                  :regs9 (gif-reg-id st)
                                  :regs10 (gif-reg-id rgbaq)
                                  :regs11 (gif-reg-id xyzf2)
                                  :regs12 (gif-reg-id st)
                                  :regs13 (gif-reg-id rgbaq)
                                  :regs14 (gif-reg-id xyzf2)
                                  )
          )
    (set! (-> gp-0 color vector4w x) 128)
    (set! (-> gp-0 color vector4w y) 128)
    (set! (-> gp-0 color vector4w z) 128)
    (set! (-> gp-0 color vector4w w) 128)
    )
  0
  (none)
  )

;; definition for symbol sprite-distort-vu1-block, type vu-function
(define sprite-distort-vu1-block (new 'static 'vu-function :length 63 :qlength 32))

;; definition for function sprite-init-distorter
;; INFO: Return type mismatch int vs none.
(defun sprite-init-distorter ((arg0 dma-buffer) (arg1 uint))
  (let* ((v1-0 arg0)
         (a2-0 (the-as object (-> v1-0 base)))
         )
    (set! (-> (the-as dma-packet a2-0) dma) (new 'static 'dma-tag :qwc #x7 :id (dma-tag-id cnt)))
    (set! (-> (the-as dma-packet a2-0) vif0) (new 'static 'vif-tag))
    (set! (-> (the-as dma-packet a2-0) vif1) (new 'static 'vif-tag :imm #x7 :cmd (vif-cmd direct) :msk #x1))
    (set! (-> v1-0 base) (&+ (the-as pointer a2-0) 16))
    )
  (let* ((v1-1 arg0)
         (a2-2 (the-as object (-> v1-1 base)))
         )
    (set! (-> (the-as gs-gif-tag a2-2) tag) (new 'static 'gif-tag64 :nloop #x1 :eop #x1 :nreg #x6))
    (set! (-> (the-as gs-gif-tag a2-2) regs) (new 'static 'gif-tag-regs
                                               :regs0 (gif-reg-id a+d)
                                               :regs1 (gif-reg-id a+d)
                                               :regs2 (gif-reg-id a+d)
                                               :regs3 (gif-reg-id a+d)
                                               :regs4 (gif-reg-id a+d)
                                               :regs5 (gif-reg-id a+d)
                                               :regs6 (gif-reg-id a+d)
                                               :regs7 (gif-reg-id a+d)
                                               :regs8 (gif-reg-id a+d)
                                               :regs9 (gif-reg-id a+d)
                                               :regs10 (gif-reg-id a+d)
                                               :regs11 (gif-reg-id a+d)
                                               :regs12 (gif-reg-id a+d)
                                               :regs13 (gif-reg-id a+d)
                                               :regs14 (gif-reg-id a+d)
                                               :regs15 (gif-reg-id a+d)
                                               )
          )
    (set! (-> v1-1 base) (&+ (the-as pointer a2-2) 16))
    )
  (let* ((v1-2 arg0)
         (a2-4 (-> v1-2 base))
         )
    (set! (-> (the-as (pointer gs-zbuf) a2-4) 0) (new 'static 'gs-zbuf :zbp #x1c0 :psm (gs-psm ct24) :zmsk #x1))
    (set! (-> (the-as (pointer gs-reg64) a2-4) 1) (gs-reg64 zbuf-1))
    (set! (-> (the-as (pointer gs-tex0) a2-4) 2)
          (new 'static 'gs-tex0 :tbw #x8 :tw #x9 :th #x8 :tbp0 (* arg1 32))
          )
    (set! (-> (the-as (pointer gs-reg64) a2-4) 3) (gs-reg64 tex0-1))
    (set! (-> (the-as (pointer gs-tex1) a2-4) 4) (new 'static 'gs-tex1 :mmag #x1 :mmin #x1))
    (set! (-> (the-as (pointer gs-reg64) a2-4) 5) (gs-reg64 tex1-1))
    (set! (-> (the-as (pointer gs-miptbp) a2-4) 6) (new 'static 'gs-miptbp))
    (set! (-> (the-as (pointer gs-reg64) a2-4) 7) (gs-reg64 miptbp1-1))
    (set! (-> (the-as (pointer gs-clamp) a2-4) 8) (new 'static 'gs-clamp
                                                    :wms (gs-tex-wrap-mode region-clamp)
                                                    :wmt (gs-tex-wrap-mode region-clamp)
                                                    :maxu #x1ff
                                                    :maxv (-> *video-parms* screen-masky)
                                                    )
          )
    (set! (-> (the-as (pointer gs-reg64) a2-4) 9) (gs-reg64 clamp-1))
    (set! (-> (the-as (pointer gs-alpha) a2-4) 10) (new 'static 'gs-alpha :b #x1 :d #x1))
    (set! (-> (the-as (pointer gs-reg64) a2-4) 11) (gs-reg64 alpha-1))
    (set! (-> v1-2 base) (&+ a2-4 96))
    )
  (let* ((v1-3 arg0)
         (a1-15 (the-as object (-> v1-3 base)))
         )
    (set! (-> (the-as dma-packet a1-15) dma)
          (new 'static 'dma-tag
            :qwc #x8b
            :id (dma-tag-id ref)
            :addr (the-as uint (-> *sprite-distorter-sine-tables* entry))
            )
          )
    (set! (-> (the-as dma-packet a1-15) vif0) (new 'static 'vif-tag :imm #x404 :cmd (vif-cmd stcycl)))
    (set! (-> (the-as dma-packet a1-15) vif1)
          (new 'static 'vif-tag :imm #x160 :num #x8b :cmd (vif-cmd unpack-v4-32))
          )
    (set! (-> v1-3 base) (&+ (the-as pointer a1-15) 16))
    )
  (dma-buffer-add-vu-function arg0 sprite-distort-vu1-block 1)
  0
  (none)
  )

;; definition for function sprite-draw-distorters
;; INFO: Used lq/sq
;; INFO: Return type mismatch int vs none.
;; ERROR: Inline assembly instruction marked with TODO - [TODO.VCLIP]
;; ERROR: Unsupported inline assembly instruction kind - [cfc2.i v1, Clipping]
(defun sprite-draw-distorters ((arg0 dma-buffer))
  (local-vars
    (v1-14 float)
    (v1-20 float)
    (v1-25 int)
    (v1-57 float)
    (sv-16 sprite-vec-data-2d)
    (sv-32 pointer)
    (sv-48 pointer)
    )
  (rlet ((acc :class vf)
         (Q :class vf)
         (vf0 :class vf)
         (vf1 :class vf)
         (vf10 :class vf)
         (vf2 :class vf)
         (vf3 :class vf)
         (vf4 :class vf)
         (vf5 :class vf)
         (vf6 :class vf)
         (vf8 :class vf)
         (vf9 :class vf)
         )
    (init-vf0-vector)
    (.lvf vf8 (&-> *math-camera* hmge-scale quad))
    (let ((s0-0 (-> arg0 base))
          (s4-0 0)
          (s5-0 0)
          )
      (&+! (-> arg0 base) 16)
      (let* ((s3-0 *sprite-aux-list*)
             (s2-0 (-> s3-0 entry))
             )
        (dotimes (s1-0 s2-0)
          (set! sv-16 (-> s3-0 data s1-0))
          (let ((a0-1 (the-as object (-> arg0 base))))
            (set! sv-32 (&+ (the-as pointer a0-1) 16))
            (set! sv-48 (&+ (the-as pointer a0-1) 32))
            (cond
              ((= (-> sv-16 matrix) 1)
               (.lvf vf3 (&-> *math-camera* sprite-2d vector 0 quad))
               (.lvf vf4 (&-> *math-camera* sprite-2d vector 1 quad))
               (.lvf vf5 (&-> *math-camera* sprite-2d vector 2 quad))
               (.lvf vf6 (&-> *math-camera* sprite-2d vector 3 quad))
               (.lvf vf9 (&-> *math-camera* sprite-2d-hvdf quad))
               (.mov v1-14 vf9)
               )
              (else
                (.lvf vf3 (&-> *math-camera* camera-temp vector 0 quad))
                (.lvf vf4 (&-> *math-camera* camera-temp vector 1 quad))
                (.lvf vf5 (&-> *math-camera* camera-temp vector 2 quad))
                (.lvf vf6 (&-> *math-camera* camera-temp vector 3 quad))
                (.lvf vf9 (&-> *math-camera* hvdf-off quad))
                (.mov v1-20 vf9)
                )
              )
            (.lvf vf1 (&-> sv-16 x-y-z-sx quad))
            (.lvf vf2 (&-> sv-16 color quad))
            (.mul.w.vf acc vf6 vf0)
            (.add.mul.x.vf acc vf3 vf1 acc)
            (.add.mul.y.vf acc vf4 vf1 acc)
            (.add.mul.z.vf vf1 vf5 vf1 acc)
            (.mul.vf vf10 vf1 vf8)
            (.div.vf Q vf0 vf10 :fsf #b11 :ftf #b11)
            (TODO.VCLIP vf10 vf10)
            (b! (zero? (-> sv-16 flag)) cfg-21 :delay (nop!))
            (.wait.vf)
            (.mul.vf vf1 vf1 Q :mask #b111)
            (.mul.vf vf2 vf2 Q :mask #b111)
            (.cfc2.i v1-25 Clipping)
            (b! (logtest? v1-25 63) cfg-21 :delay (.add.vf vf1 vf1 vf9))
            (.max.x.vf vf1 vf1 vf0 :mask #b1000)
            (.svf (&-> (the-as vector a0-1) quad) vf1)
            (.svf (&-> (the-as vector sv-48) quad) vf2)
            (set! (-> (the-as vector a0-1) w) 255.0)
            (set! (-> (the-as (pointer float) sv-32)) (* 0.001953125 (+ -1792.0 (-> (the-as vector a0-1) x))))
            (set! (-> (the-as vector sv-32) y)
                  (* 0.00390625 (+ -2048.0 (the float (-> *video-parms* screen-hy)) (-> (the-as vector a0-1) y)))
                  )
            )
          (set! (-> (the-as vector sv-32) z) 1.0)
          (when (or (< (-> sv-16 flag) 3) (< 11 (-> sv-16 flag)))
            (format 0 "Turns = ~D!!!~%" (-> sv-16 flag))
            (set! (-> (the-as (pointer int32) sv-16) 4) 11)
            )
          (set! (-> (the-as (pointer int32) sv-32) 3) (-> sv-16 flag))
          (let* ((f0-7 (- (-> *math-camera* perspective vector 1 y)))
                 (f2-4 (-> (the-as vector sv-32) y))
                 (f4-0 (+ f2-4 (* (-> (the-as (pointer float) sv-48)) f0-7)))
                 (f3-0 256.0)
                 (f1-7 (-> (the-as (pointer float) sv-48)))
                 )
            (if (< (the float (-> *video-parms* screen-sy)) f4-0)
                (set! f3-0 (/ (- (the float (-> *video-parms* screen-sy)) f2-4) f0-7))
                )
            (if (< 128.0 f3-0)
                (set! f3-0 128.0)
                )
            (when (< f3-0 f1-7)
              (let ((v1-55 (/ f3-0 f1-7)))
                (.mov vf1 v1-55)
                )
              (.mul.x.vf vf2 vf2 vf1)
              (.svf (&-> (the-as vector sv-48) quad) vf2)
              (.mov v1-57 vf2)
              )
            )
          (&+! (-> arg0 base) 48)
          (+! s4-0 1)
          (when (= s4-0 85)
            (let ((v1-62 (-> arg0 base)))
              (set! (-> arg0 base) s0-0)
              (let* ((a0-8 arg0)
                     (a1-3 (the-as object (-> a0-8 base)))
                     )
                (set! (-> (the-as dma-packet a1-3) dma) (new 'static 'dma-tag :id (dma-tag-id cnt) :qwc (* 3 s4-0)))
                (set! (-> (the-as dma-packet a1-3) vif0) (new 'static 'vif-tag))
                (set! (-> (the-as dma-packet a1-3) vif1)
                      (new 'static 'vif-tag :cmd (vif-cmd unpack-v4-32) :imm (shr (shl (+ (* 3 s5-0) 512) 54) 54) :num (* 3 s4-0))
                      )
                (set! (-> a0-8 base) (&+ (the-as pointer a1-3) 16))
                )
              (set! (-> arg0 base) v1-62)
              )
            (set! s4-0 0)
            (+! s5-0 85)
            (when (= s5-0 170)
              (let* ((v1-65 arg0)
                     (a0-9 (the-as object (-> v1-65 base)))
                     )
                (set! (-> (the-as dma-packet a0-9) dma) (new 'static 'dma-tag :qwc #x1 :id (dma-tag-id cnt)))
                (set! (-> (the-as dma-packet a0-9) vif0) (new 'static 'vif-tag))
                (set! (-> (the-as dma-packet a0-9) vif1)
                      (new 'static 'vif-tag :imm #x1ff :num #x1 :cmd (vif-cmd unpack-v4-32))
                      )
                (set! (-> v1-65 base) (&+ (the-as pointer a0-9) 16))
                )
              (let* ((v1-66 arg0)
                     (a0-11 (the-as object (-> v1-66 base)))
                     )
                (set! (-> (the-as vector4w a0-11) x) s5-0)
                (set! (-> (the-as vector4w a0-11) y) 0)
                (set! (-> (the-as vector4w a0-11) z) 0)
                (set! (-> (the-as vector4w a0-11) w) 0)
                (set! (-> v1-66 base) (&+ (the-as pointer a0-11) 16))
                )
              (let* ((v1-67 arg0)
                     (a0-13 (the-as object (-> v1-67 base)))
                     )
                (set! (-> (the-as dma-packet a0-13) dma) (new 'static 'dma-tag :id (dma-tag-id cnt)))
                (set! (-> (the-as dma-packet a0-13) vif0) (new 'static 'vif-tag :cmd (vif-cmd mscalf) :msk #x1))
                (set! (-> (the-as dma-packet a0-13) vif1) (new 'static 'vif-tag :cmd (vif-cmd flush) :msk #x1))
                (set! (-> v1-67 base) (&+ (the-as pointer a0-13) 16))
                )
              (set! s5-0 0)
              )
            (set! s0-0 (-> arg0 base))
            (&+! (-> arg0 base) 16)
            )
          (label cfg-21)
          )
        )
      (cond
        ((zero? s4-0)
         (set! (-> arg0 base) s0-0)
         (nop!)
         0
         )
        (else
          (let ((v1-74 (-> arg0 base)))
            (set! (-> arg0 base) s0-0)
            (let* ((a0-15 arg0)
                   (a1-10 (the-as object (-> a0-15 base)))
                   )
              (set! (-> (the-as dma-packet a1-10) dma) (new 'static 'dma-tag :id (dma-tag-id cnt) :qwc (* 3 s4-0)))
              (set! (-> (the-as dma-packet a1-10) vif0) (new 'static 'vif-tag))
              (set! (-> (the-as dma-packet a1-10) vif1)
                    (new 'static 'vif-tag :cmd (vif-cmd unpack-v4-32) :imm (shr (shl (+ (* 3 s5-0) 512) 54) 54) :num (* 3 s4-0))
                    )
              (set! (-> a0-15 base) (&+ (the-as pointer a1-10) 16))
              )
            (set! (-> arg0 base) v1-74)
            )
          )
        )
      (let ((v1-75 (+ s4-0 s5-0)))
        (when (nonzero? v1-75)
          (let* ((a0-17 arg0)
                 (a1-12 (the-as object (-> a0-17 base)))
                 )
            (set! (-> (the-as dma-packet a1-12) dma) (new 'static 'dma-tag :qwc #x1 :id (dma-tag-id cnt)))
            (set! (-> (the-as dma-packet a1-12) vif0) (new 'static 'vif-tag))
            (set! (-> (the-as dma-packet a1-12) vif1)
                  (new 'static 'vif-tag :imm #x1ff :num #x1 :cmd (vif-cmd unpack-v4-32))
                  )
            (set! (-> a0-17 base) (&+ (the-as pointer a1-12) 16))
            )
          (let* ((a0-18 arg0)
                 (a1-14 (the-as object (-> a0-18 base)))
                 )
            (set! (-> (the-as vector4w a1-14) x) v1-75)
            (set! (-> (the-as vector4w a1-14) y) 0)
            (set! (-> (the-as vector4w a1-14) z) 0)
            (set! (-> (the-as vector4w a1-14) w) 0)
            (set! (-> a0-18 base) (&+ (the-as pointer a1-14) 16))
            )
          (let ((v1-77 (the-as object (-> arg0 base))))
            (set! (-> (the-as dma-packet v1-77) dma) (new 'static 'dma-tag :id (dma-tag-id cnt)))
            (set! (-> (the-as dma-packet v1-77) vif0) (new 'static 'vif-tag :cmd (vif-cmd mscalf) :msk #x1))
            (set! (-> (the-as dma-packet v1-77) vif1) (new 'static 'vif-tag :cmd (vif-cmd flush) :msk #x1))
            (set! (-> arg0 base) (&+ (the-as pointer v1-77) 16))
            )
          )
        )
      )
    0
    (none)
    )
  )
