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

;; definition for symbol *background-work*, type background-work
(define *background-work* (new 'global 'background-work))

;; definition for symbol background-vu0-block, type vu-function
(define background-vu0-block (new 'static 'vu-function :length 59 :qlength 30))

;; definition for function background-upload-vu0
;; ERROR: function was not converted to expressions. Cannot decompile.

;; definition for function init-background
;; INFO: Return type mismatch int vs none.
(defun init-background ()
  (dotimes (v1-0 8)
    (set! (-> *background-work* tfrag-trees v1-0) #f)
    (set! (-> *background-work* trans-tfrag-trees v1-0) #f)
    (set! (-> *background-work* dirt-tfrag-trees v1-0) #f)
    (set! (-> *background-work* ice-tfrag-trees v1-0) #f)
    (set! (-> *background-work* lowres-tfrag-trees v1-0) #f)
    (set! (-> *background-work* lowres-trans-tfrag-trees v1-0) #f)
    )
  (set! (-> *background-work* tfrag-tree-count) 0)
  (set! (-> *background-work* trans-tfrag-tree-count) 0)
  (set! (-> *background-work* dirt-tfrag-tree-count) 0)
  (set! (-> *background-work* ice-tfrag-tree-count) 0)
  (set! (-> *background-work* lowres-tfrag-tree-count) 0)
  (set! (-> *background-work* lowres-trans-tfrag-tree-count) 0)
  (set! (-> *background-work* shrub-tree-count) 0)
  (set! (-> *background-work* tie-tree-count) 0)
  (set! (-> *background-work* wait-to-vu0) (the-as uint 0))
  0
  (none)
  )

;; definition for function upload-vis-bits
;; INFO: Used lq/sq
;; INFO: Return type mismatch int vs none.
(defun upload-vis-bits ((arg0 level) (arg1 level) (arg2 bsp-header))
  (let ((qwc (/ (+ (-> arg2 visible-list-length) 15) 16)))
    (let ((lev-vis-bits (the-as (pointer uint128) (-> arg0 vis-bits)))
          (all-vis (the-as (pointer uint128) (-> arg2 all-visible-list)))
          (spad-vis (the-as (pointer uint128) (+ #x38b0 #x70000000)))
          )
      (b! (not *artist-flip-visible*) cfg-5 :delay (nop!))
      (nop!)
      (nop!)
      (label cfg-2)
      (let ((a3-2 (-> lev-vis-bits 0)))
        (set! lev-vis-bits (&-> lev-vis-bits 1))
        (let ((t0-0 (-> all-vis 0)))
          (set! all-vis (&-> all-vis 1))
          (nop!)
          (nop!)
          (let ((a3-3 (logxor a3-2 (the-as uint t0-0))))
            (+! qwc -1)
            (set! (-> spad-vis 0) a3-3)
            )
          )
        )
      (set! spad-vis (&-> spad-vis 1))
      (b! (> qwc 0) cfg-2 :delay (nop!))
      0
      (b! #t cfg-8 :delay (nop!))
      (nop!)
      (label cfg-5)
      (nop!)
      (nop!)
      (label cfg-6)
      (let ((a1-2 (-> lev-vis-bits 0)))
        (set! lev-vis-bits (&-> lev-vis-bits 1))
        (nop!)
        (+! qwc -1)
        (set! (-> spad-vis 0) a1-2)
        )
      (set! spad-vis (&-> spad-vis 1))
      )
    (b! (> qwc 0) cfg-6 :delay (nop!))
    )
  0
  (label cfg-8)
  (none)
  )

;; definition for function finish-background
;; INFO: Return type mismatch int vs none.
(defun finish-background ()
  (rlet ((vf16 :class vf)
         (vf17 :class vf)
         (vf18 :class vf)
         (vf19 :class vf)
         (vf20 :class vf)
         (vf21 :class vf)
         (vf22 :class vf)
         (vf23 :class vf)
         (vf24 :class vf)
         (vf25 :class vf)
         (vf26 :class vf)
         (vf27 :class vf)
         (vf28 :class vf)
         (vf29 :class vf)
         (vf30 :class vf)
         (vf31 :class vf)
         )
    (background-upload-vu0)
    (set! (-> *instance-shrub-work* paused) (paused?))
    (when (nonzero? (-> *background-work* shrub-tree-count))
      (if *debug-segment*
          (add-frame
            (-> *display* frames (-> *display* on-screen) frame profile-bar 0)
            'draw
            (new 'static 'rgba :r #x40 :b #x40 :a #x80)
            )
          )
      (dotimes (gp-0 (-> *background-work* shrub-tree-count))
        (set! (-> (the-as terrain-context #x70000000) bsp lev-index) (-> *background-work* shrub-levels gp-0 index))
        (flush-cache 0)
        (let ((s5-0 (-> *background-work* shrub-trees gp-0))
              (s4-0 (-> *background-work* shrub-levels gp-0))
              )
          (if (nonzero? (-> s5-0 colors-added))
              (time-of-day-interp-colors
                (-> *instance-shrub-work* colors)
                (the-as uint (-> s5-0 colors-added))
                (-> s4-0 mood)
                )
              )
          (draw-drawable-tree-instance-shrub s5-0 s4-0)
          )
        )
      (if *debug-segment*
          (add-frame
            (-> *display* frames (-> *display* on-screen) frame profile-bar 0)
            'draw
            (new 'static 'rgba :r #x40 :g #x60 :b #x80 :a #x80)
            )
          )
      )
    (let ((gp-1 (the-as level #f)))
      (when (or (nonzero? (-> *background-work* tfrag-tree-count))
                (nonzero? (-> *background-work* trans-tfrag-tree-count))
                (nonzero? (-> *background-work* dirt-tfrag-tree-count))
                (nonzero? (-> *background-work* ice-tfrag-tree-count))
                (nonzero? (-> *background-work* lowres-tfrag-tree-count))
                (nonzero? (-> *background-work* lowres-trans-tfrag-tree-count))
                )
        (let ((v1-48 *math-camera*))
          (.lvf vf16 (&-> v1-48 plane 0 quad))
          (.lvf vf17 (&-> v1-48 plane 1 quad))
          (.lvf vf18 (&-> v1-48 plane 2 quad))
          (.lvf vf19 (&-> v1-48 plane 3 quad))
          (.lvf vf20 (&-> v1-48 shrub-mat vector 0 quad))
          (.lvf vf21 (&-> v1-48 shrub-mat vector 1 quad))
          (.lvf vf22 (&-> v1-48 shrub-mat vector 2 quad))
          (.lvf vf23 (&-> v1-48 shrub-mat vector 3 quad))
          (.lvf vf24 (&-> v1-48 camera-rot vector 0 quad))
          (.lvf vf25 (&-> v1-48 camera-rot vector 1 quad))
          (.lvf vf26 (&-> v1-48 camera-rot vector 2 quad))
          (.lvf vf27 (&-> v1-48 camera-rot vector 3 quad))
          (.lvf vf28 (&-> v1-48 camera-temp vector 0 quad))
          (.lvf vf29 (&-> v1-48 camera-temp vector 1 quad))
          (.lvf vf30 (&-> v1-48 camera-temp vector 2 quad))
          (.lvf vf31 (&-> v1-48 camera-temp vector 3 quad))
          (.lvf vf31 (&-> v1-48 camera-temp vector 3 quad))
          )
        (let* ((v1-52 (max (-> *background-work* tfrag-tree-count) (-> *background-work* trans-tfrag-tree-count)))
               (s4-1 (the-as time-of-day-palette #f))
               (s5-2 (max
                       (max v1-52 (-> *background-work* lowres-tfrag-tree-count))
                       (-> *background-work* lowres-trans-tfrag-tree-count)
                       )
                     )
               )
          (if *debug-segment*
              (add-frame
                (-> *display* frames (-> *display* on-screen) frame profile-bar 0)
                'draw
                (new 'static 'rgba :r #x40 :b #x40 :a #x80)
                )
              )
          (dotimes (s3-0 s5-2)
            (let ((s2-0 (-> *background-work* tfrag-trees s3-0)))
              (when s2-0
                (let ((s1-0 (-> *background-work* tfrag-levels s3-0)))
                  (let ((a2-4 (-> s1-0 bsp))
                        (s0-0 (-> s2-0 time-of-day-pal))
                        )
                    (upload-vis-bits s1-0 gp-1 a2-4)
                    (when (not (or (zero? s0-0) (= s4-1 s0-0)))
                      (flush-cache 0)
                      (time-of-day-interp-colors-scratch
                        (the-as (pointer rgba) (+ 6160 (the-as int (the-as terrain-context #x70000000))))
                        s0-0
                        (-> s1-0 mood)
                        )
                      (set! s4-1 s0-0)
                      )
                    )
                  (set! (-> (the-as terrain-context #x70000000) bsp lev-index) (-> s1-0 index))
                  )
                (set! (-> *tfrag-work* min-dist z) 4095996000.0)
                (draw-drawable-tree-tfrag s2-0)
                (set! (-> *level* level (-> (the-as terrain-context #x70000000) bsp lev-index) closest-object 0)
                      (-> *tfrag-work* min-dist z)
                      )
                )
              )
            (let ((s2-1 (-> *background-work* trans-tfrag-trees s3-0)))
              (when s2-1
                (let ((s1-1 (-> *background-work* trans-tfrag-levels s3-0)))
                  (let ((a2-6 (-> s1-1 bsp))
                        (s0-1 (-> s2-1 time-of-day-pal))
                        )
                    (upload-vis-bits s1-1 gp-1 a2-6)
                    (when (not (or (zero? s0-1) (= s4-1 s0-1)))
                      (flush-cache 0)
                      (time-of-day-interp-colors-scratch (the-as (pointer rgba) (+ 6160 #x70000000)) s0-1 (-> s1-1 mood))
                      (set! s4-1 s0-1)
                      )
                    )
                  (set! (-> (the-as terrain-context #x70000000) bsp lev-index) (-> s1-1 index))
                  )
                (set! (-> *tfrag-work* min-dist z) 4095996000.0)
                (draw-drawable-tree-trans-tfrag s2-1)
                (set! (-> *level* level (-> (the-as terrain-context #x70000000) bsp lev-index) closest-object 3)
                      (-> *tfrag-work* min-dist z)
                      )
                )
              )
            (let ((s2-2 (-> *background-work* dirt-tfrag-trees s3-0)))
              (when s2-2
                (let ((s1-2 (-> *background-work* dirt-tfrag-levels s3-0)))
                  (let ((a2-8 (-> s1-2 bsp))
                        (s0-2 (-> s2-2 time-of-day-pal))
                        )
                    (upload-vis-bits s1-2 gp-1 a2-8)
                    (when (not (or (zero? s0-2) (= s4-1 s0-2)))
                      (flush-cache 0)
                      (time-of-day-interp-colors-scratch (the-as (pointer rgba) (+ 6160 #x70000000)) s0-2 (-> s1-2 mood))
                      (set! s4-1 s0-2)
                      )
                    )
                  (set! (-> (the-as terrain-context #x70000000) bsp lev-index) (-> s1-2 index))
                  )
                (set! (-> *tfrag-work* min-dist z) 4095996000.0)
                (draw-drawable-tree-dirt-tfrag s2-2)
                (set! (-> *level* level (-> (the-as terrain-context #x70000000) bsp lev-index) closest-object 3)
                      (fmin
                        (-> *level* level (-> (the-as terrain-context #x70000000) bsp lev-index) closest-object 3)
                        (-> *tfrag-work* min-dist z)
                        )
                      )
                )
              )
            (let ((s2-3 (-> *background-work* ice-tfrag-trees s3-0)))
              (when s2-3
                (let ((s1-3 (-> *background-work* ice-tfrag-levels s3-0)))
                  (let ((a2-10 (-> s1-3 bsp))
                        (s0-3 (-> s2-3 time-of-day-pal))
                        )
                    (upload-vis-bits s1-3 gp-1 a2-10)
                    (when (not (or (zero? s0-3) (= s4-1 s0-3)))
                      (flush-cache 0)
                      (time-of-day-interp-colors-scratch (the-as (pointer rgba) (+ 6160 #x70000000)) s0-3 (-> s1-3 mood))
                      (set! s4-1 s0-3)
                      )
                    )
                  (set! (-> (the-as terrain-context #x70000000) bsp lev-index) (-> s1-3 index))
                  )
                (set! (-> *tfrag-work* min-dist z) 4095996000.0)
                (draw-drawable-tree-ice-tfrag s2-3)
                (set! (-> *level* level (-> (the-as terrain-context #x70000000) bsp lev-index) closest-object 3)
                      (fmin
                        (-> *level* level (-> (the-as terrain-context #x70000000) bsp lev-index) closest-object 3)
                        (-> *tfrag-work* min-dist z)
                        )
                      )
                )
              )
            (let ((s2-4 (-> *background-work* lowres-tfrag-trees s3-0)))
              (when s2-4
                (let ((s1-4 (-> *background-work* lowres-tfrag-levels s3-0)))
                  (let ((a2-12 (-> s1-4 bsp))
                        (s0-4 (-> s2-4 time-of-day-pal))
                        )
                    (upload-vis-bits s1-4 gp-1 a2-12)
                    (when (not (or (zero? s0-4) (= s4-1 s0-4)))
                      (flush-cache 0)
                      (time-of-day-interp-colors-scratch (the-as (pointer rgba) (+ 6160 #x70000000)) s0-4 (-> s1-4 mood))
                      (set! s4-1 s0-4)
                      )
                    )
                  (set! (-> (the-as terrain-context #x70000000) bsp lev-index) (-> s1-4 index))
                  )
                (draw-drawable-tree-tfrag s2-4)
                )
              )
            (let ((s2-5 (-> *background-work* lowres-trans-tfrag-trees s3-0)))
              (when s2-5
                (let ((s1-5 (-> *background-work* lowres-trans-tfrag-levels s3-0)))
                  (let ((a2-14 (-> s1-5 bsp))
                        (s0-5 (-> s2-5 time-of-day-pal))
                        )
                    (upload-vis-bits s1-5 gp-1 a2-14)
                    (when (not (or (zero? s0-5) (= s4-1 s0-5)))
                      (flush-cache 0)
                      (time-of-day-interp-colors-scratch (the-as (pointer rgba) (+ 6160 #x70000000)) s0-5 (-> s1-5 mood))
                      (set! s4-1 s0-5)
                      )
                    )
                  (set! (-> (the-as terrain-context #x70000000) bsp lev-index) (-> s1-5 index))
                  )
                (set! (-> *tfrag-work* min-dist z) 4095996000.0)
                (draw-drawable-tree-trans-tfrag s2-5)
                (set! (-> *level* level (-> (the-as terrain-context #x70000000) bsp lev-index) closest-object 3)
                      (fmin
                        (-> *level* level (-> (the-as terrain-context #x70000000) bsp lev-index) closest-object 3)
                        (-> *tfrag-work* min-dist z)
                        )
                      )
                )
              )
            )
          )
        (if *debug-segment*
            (add-frame
              (-> *display* frames (-> *display* on-screen) frame profile-bar 0)
              'draw
              (new 'static 'rgba :r #x40 :g #x80 :b #x60 :a #x80)
              )
            )
        )
      (set! (-> *instance-tie-work* paused) (paused?))
      (when (nonzero? (-> *background-work* tie-tree-count))
        (tie-near-make-perspective-matrix (-> *instance-tie-work* tie-near-perspective-matrix))
        (if *debug-segment*
            (add-frame
              (-> *display* frames (-> *display* on-screen) frame profile-bar 0)
              'draw
              (new 'static 'rgba :r #x40 :b #x40 :a #x80)
              )
            )
        (dotimes (s5-3 (-> *background-work* tie-tree-count))
          (let ((s4-2 (-> *background-work* tie-levels s5-3)))
            (let ((a2-18 (-> s4-2 bsp)))
              (when (!= s4-2 gp-1)
                (set! (-> *instance-tie-work* min-dist x) 4095996000.0)
                (upload-vis-bits s4-2 gp-1 a2-18)
                (set! gp-1 s4-2)
                )
              )
            (set! (-> (the-as terrain-context #x70000000) bsp lev-index) (-> s4-2 index))
            (set! (-> (the-as terrain-context #x70000000) bsp mood) (-> s4-2 mood))
            (draw-drawable-tree-instance-tie (-> *background-work* tie-trees s5-3) s4-2)
            )
          (set! (-> *background-work* tie-generic s5-3) (the-as basic (-> *instance-tie-work* first-generic-prototype)))
          )
        (if *debug-segment*
            (add-frame
              (-> *display* frames (-> *display* on-screen) frame profile-bar 0)
              'draw
              (new 'static 'rgba :r #x80 :g #x20 :b #x60 :a #x80)
              )
            )
        (dotimes (gp-2 (-> *background-work* tie-tree-count))
          (when (nonzero? (-> *background-work* tie-generic gp-2))
            (let* ((s5-4 (-> *background-work* tie-levels gp-2 tfrag-tex-foreground-sink-group generic-sink))
                   (s3-1 (-> *display* frames (-> *display* on-screen) frame global-buf))
                   (s4-3 (-> s3-1 base))
                   )
              (generic-tie-execute s5-4 s3-1 (-> *background-work* tie-generic gp-2))
              (let ((a3-0 (-> s3-1 base)))
                (let ((v1-219 (the-as object (-> s3-1 base))))
                  (set! (-> (the-as dma-packet v1-219) dma) (new 'static 'dma-tag :id (dma-tag-id next)))
                  (set! (-> (the-as dma-packet v1-219) vif0) (new 'static 'vif-tag))
                  (set! (-> (the-as dma-packet v1-219) vif1) (new 'static 'vif-tag))
                  (set! (-> s3-1 base) (&+ (the-as pointer v1-219) 16))
                  )
                (dma-bucket-insert-tag
                  (-> *display* frames (-> *display* on-screen) frame bucket-group)
                  (-> s5-4 bucket)
                  s4-3
                  (the-as (pointer dma-tag) a3-0)
                  )
                )
              )
            )
          )
        )
      )
    0
    (none)
    )
  )
