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

;; name: bigmap.gc
;; name in dgo: bigmap
;; dgos: GAME

;; DECOMP BEGINS

(defmethod new bigmap ((allocation symbol) (type-to-make type))
  (let ((gp-0 (object-new allocation type-to-make (the-as int (-> type-to-make size)))))
    (set! (-> gp-0 bigmap-image)
          ((method-of-type external-art-buffer new)
           allocation
           external-art-buffer
           0
           (lambda ((arg0 external-art-buffer))
             (let ((a1-3 (logand -64 (+ -591936 (-> *display* frames 0 global-buf real-buffer-end) 63)))
                   (v1-1 (-> arg0 heap))
                   )
               (set! (-> v1-1 base) (the-as pointer a1-3))
               (set! (-> v1-1 current) (-> v1-1 base))
               (set! (-> v1-1 top-base) (&+ (-> v1-1 base) #x90800))
               (set! (-> v1-1 top) (-> v1-1 top-base))
               )
             0
             )
           #f
           )
          )
    (set! (-> gp-0 tpage)
          ((method-of-type external-art-buffer new)
           allocation
           external-art-buffer
           0
           (lambda ((arg0 external-art-buffer))
             (let ((a1-3 (logand -64 (+ -597056 (-> *display* frames 1 global-buf real-buffer-end) 63)))
                   (v1-1 (-> arg0 heap))
                   )
               (set! (-> v1-1 base) (the-as pointer a1-3))
               (set! (-> v1-1 current) (-> v1-1 base))
               (set! (-> v1-1 top-base) (&+ (-> v1-1 base) #x54000))
               (set! (-> v1-1 top) (-> v1-1 top-base))
               )
             0
             )
           #f
           )
          )
    (set! (-> gp-0 tpage2)
          ((method-of-type external-art-buffer new)
           allocation
           external-art-buffer
           0
           (lambda ((arg0 external-art-buffer))
             (let ((a1-3 (logand -64 (+ -941120 (-> *display* frames 1 global-buf real-buffer-end) 63)))
                   (v1-1 (-> arg0 heap))
                   )
               (set! (-> v1-1 base) (the-as pointer a1-3))
               (set! (-> v1-1 current) (-> v1-1 base))
               (set! (-> v1-1 top-base) (&+ (-> v1-1 base) #x54000))
               (set! (-> v1-1 top) (-> v1-1 top-base))
               )
             0
             )
           #f
           )
          )
    (set! (-> gp-0 sprite-tmpl dma-vif dma) (new 'static 'dma-tag :qwc #x6 :id (dma-tag-id cnt)))
    (set! (-> gp-0 sprite-tmpl dma-vif vif0) (new 'static 'vif-tag))
    (set! (-> gp-0 sprite-tmpl dma-vif vif1) (new 'static 'vif-tag :imm #x6 :cmd (vif-cmd direct) :msk #x1))
    (set! (-> gp-0 sprite-tmpl gif0) (the-as uint #x50ab400000008001))
    (set! (-> gp-0 sprite-tmpl gif1) (the-as uint #x53531))
    (set! (-> gp-0 draw-tmpl dma-vif dma) (new 'static 'dma-tag :qwc #xa :id (dma-tag-id cnt)))
    (set! (-> gp-0 draw-tmpl dma-vif vif0) (new 'static 'vif-tag))
    (set! (-> gp-0 draw-tmpl dma-vif vif1) (new 'static 'vif-tag :imm #xa :cmd (vif-cmd direct) :msk #x1))
    (set! (-> gp-0 draw-tmpl gif0) (the-as uint #x90aa400000008001))
    (set! (-> gp-0 draw-tmpl gif1) (the-as uint #x535353531))
    (set! (-> gp-0 adgif-tmpl dma-vif dma) (new 'static 'dma-tag :qwc #x6 :id (dma-tag-id cnt)))
    (set! (-> gp-0 adgif-tmpl dma-vif vif0) (new 'static 'vif-tag))
    (set! (-> gp-0 adgif-tmpl dma-vif vif1) (new 'static 'vif-tag :imm #x6 :cmd (vif-cmd direct) :msk #x1))
    (set! (-> gp-0 adgif-tmpl gif0) (the-as uint #x1000000000008005))
    (set! (-> gp-0 adgif-tmpl gif1) (the-as uint 14))
    (set-vector! (-> gp-0 offset) 0.0 0.0 0.0 0.0)
    (set-vector! (-> gp-0 scroll) 0.0 0.0 0.0 0.0)
    (set-vector! (-> gp-0 pos) 0 0 0 0)
    (set-vector! (-> gp-0 color) 128 128 128 128)
    (set! (-> gp-0 drawing-flag) #f)
    (set! (-> gp-0 loading-flag) #f)
    (set! (-> gp-0 progress-minimap) #f)
    (set! (-> gp-0 progress-minimap2) #f)
    (set! (-> gp-0 auto-save-icon-flag) #f)
    (initialize gp-0)
    gp-0
    )
  )

(define *bigmap-info-array*
  (new 'static 'bigmap-info-array
    :data (new 'static 'inline-array bigmap-info 24
      (new 'static 'bigmap-info :x -2621440.0 :y -4456448.0 :z 16384.0 :w 0.000061035156)
      (new 'static 'bigmap-info :x -2621440.0 :y -4456448.0 :z 16384.0 :w 0.000061035156)
      (new 'static 'bigmap-info :x -2038169.6 :y -2301542.5 :z 27443.2 :w 0.0000364389)
      (new 'static 'bigmap-info :x -354713.6 :y -1128448.0 :z 4505.6 :w 0.00022194601)
      (new 'static 'bigmap-info :x -4205363.0 :y 3437363.2 :z 4628.48 :w 0.00021605365)
      (new 'static 'bigmap-info :x -3381657.5 :y 2019737.6 :z 7618.56 :w 0.0001312584)
      (new 'static 'bigmap-info :x -1399193.6 :y -1023590.4 :z 5406.72 :w 0.00018495502)
      (new 'static 'bigmap-info :x 2215526.5 :y 615055.4 :z 10444.8 :w 0.00009574142)
      (new 'static 'bigmap-info :x 2215526.5 :y 615055.4 :z 10444.8 :w 0.00009574142)
      (new 'static 'bigmap-info :z 16384.0 :w 0.000061035156)
      (new 'static 'bigmap-info :x -1481932.8 :y -918323.2 :z 5857.28 :w 0.00017072771)
      (new 'static 'bigmap-info :x -1481932.8 :y -918323.2 :z 5857.28 :w 0.00017072771)
      (new 'static 'bigmap-info :x -2614886.5 :y -1766195.2 :z 9052.16 :w 0.00011047087)
      (new 'static 'bigmap-info :x -2530508.8 :y -1041203.2 :z 5591.04 :w 0.0001788576)
      (new 'static 'bigmap-info :x -2374451.2 :y -4505.6 :z 4628.48 :w 0.00021605365)
      (new 'static 'bigmap-info :x -2712780.8 :y -2477260.8 :z 6471.68 :w 0.00015451938)
      (new 'static 'bigmap-info :x -1218560.0 :y -3392307.2 :z 5857.28 :w 0.00017072771)
      (new 'static 'bigmap-info :x 16039117.0 :y 16509747.0 :z 3317.76 :w 0.00030140817)
      (new 'static 'bigmap-info :x 15403827.0 :y 16360653.0 :z 5283.84 :w 0.0001892563)
      (new 'static 'bigmap-info :x 15403827.0 :y 16360653.0 :z 5283.84 :w 0.0001892563)
      (new 'static 'bigmap-info :x 15403827.0 :y 16360653.0 :z 5283.84 :w 0.0001892563)
      (new 'static 'bigmap-info :x -1716224.0 :y 1286144.0 :z 3604.48 :w 0.00027743253)
      (new 'static 'bigmap-info :x -1316864.0 :y -2084126.8 :z 4014.08 :w 0.00024912308)
      (new 'static 'bigmap-info :x 5619712.0 :y -2914304.0 :z 9830.4 :w 0.00010172526)
      )
    )
  )

(defmethod set-pos! ((this bigmap) (arg0 vector))
  (rlet ((vf0 :class vf)
         (vf1 :class vf)
         (vf2 :class vf)
         )
    (init-vf0-vector)
    (.lvf vf1 (&-> arg0 quad))
    (.lvf vf2 (&-> this offset quad))
    (.add.z.vf.y vf1 vf0 vf1)
    (.sub.vf vf1 vf1 vf2)
    (.mul.w.vf vf1 vf1 vf2)
    (.ftoi.vf vf1 vf1)
    (.svf (&-> this pos quad) vf1)
    0
    (none)
    )
  )

(defmethod bigmap-method-18 ((this bigmap) (arg0 (pointer int32)))
  (when (or (= (-> this load-index) (bigmap-id sewer-met-hum)) (= (-> this load-index) (bigmap-id sewer-hum-kg)))
    (let ((v1-4 (-> arg0 0)))
      (set! (-> arg0 0) (-> arg0 1))
      (set! (-> arg0 1) (- 832 v1-4))
      )
    )
  (none)
  )

;; og:preserve-this
(defun pc-upload-map-texture ((dma-buf dma-buffer) (image-data pointer) (clut-data pointer) (width int) (height int) (tbp int) (cbp int))
  "Added function in the PC port to create a map texture. The format should be mt8 for the image and ct32 for the clut (16 x 16)"
  (pc-texture-anim-flag start-anim-array dma-buf)
  (pc-texture-anim-flag upload-clut-16-16 dma-buf :qwc 1)
  (let ((upload-record (the texture-anim-pc-upload (-> dma-buf base))))
    (set! (-> upload-record data) clut-data)
    (set! (-> upload-record width) 16)
    (set! (-> upload-record height) 16)
    (set! (-> upload-record dest) cbp)
    (set! (-> upload-record format) (gs-psm ct32))
    (set! (-> upload-record force-to-gpu) 0)
    )
  (&+! (-> dma-buf base) 16)

  (pc-texture-anim-flag upload-generic-vram dma-buf :qwc 1)
  (let ((upload-record (the texture-anim-pc-upload (-> dma-buf base))))
    (set! (-> upload-record data) image-data)
    (set! (-> upload-record width) width)
    (set! (-> upload-record height) height)
    (set! (-> upload-record dest) tbp)
    (set! (-> upload-record format) (gs-psm mt8))
    (set! (-> upload-record force-to-gpu) 1)
    )
  (&+! (-> dma-buf base) 16)
  (pc-texture-anim-flag finish-anim-array dma-buf)
  )

(defmethod texture-upload-dma ((this bigmap) (arg0 dma-buffer) (arg1 (pointer uint32)) (arg2 int) (arg3 int) (arg4 int) (arg5 gs-psm))
  (local-vars (sv-16 int))
  (set! sv-16 arg2)
  (dma-buffer-add-gs-set arg0
                         (bitbltbuf (new 'static 'gs-bitbltbuf :dpsm (the-as int arg5) :dbp sv-16 :dbw (/ arg3 64)))
                         (trxpos (new 'static 'gs-trxpos))
                         (trxreg (new 'static 'gs-trxreg :rrw arg3 :rrh arg4))
                         (trxdir (new 'static 'gs-trxdir))
                         )
  (dma-buffer-add-ref-texture arg0 arg1 arg3 arg4 arg5)
  0
  (none)
  )

;; WARN: Return type mismatch pointer vs object.
(defmethod sprite-dma ((this bigmap) (arg0 dma-buffer) (arg1 int) (arg2 int) (arg3 int) (arg4 int) (arg5 int) (arg6 int))
  (let ((v1-0 (the-as object (-> arg0 base))))
    (let ((t5-0 0)
          (t4-2 (the int (* 416.0 (-> *video-params* relative-x-scale))))
          )
      (set! (-> (the-as (inline-array vector4w) v1-0) 0 quad) (-> this sprite-tmpl dma-vif quad))
      (set! (-> (the-as (inline-array vector4w) v1-0) 1 quad) (-> this sprite-tmpl quad 1))
      (set! (-> (the-as (inline-array vector4w) v1-0) 2 quad) (-> this color quad))
      (set-vector! (-> (the-as (inline-array vector4w) v1-0) 3) (* arg5 16) (* t5-0 16) 0 0)
      (set-vector! (-> (the-as (inline-array vector4w) v1-0) 4) (* arg1 16) (* arg3 16) #xfffff0 0)
      (set-vector! (-> (the-as (inline-array vector4w) v1-0) 5) (* arg6 16) (* t4-2 16) 0 0)
      )
    (set-vector! (-> (the-as (inline-array vector4w) v1-0) 6) (* arg2 16) (* arg4 16) #xfffff0 0)
    )
  (let ((v0-0 (&+ (-> arg0 base) 112)))
    (set! (-> arg0 base) v0-0)
    v0-0
    )
  )

(defmethod bigmap-method-20 ((this bigmap) (arg0 dma-buffer))
  (local-vars (sv-16 uint))
  (let* ((s4-0 (the-as (pointer uint32) (-> this bigmap-image art-group)))
         (f0-1 (* 0.001953125 (the float (- (-> this x1) (-> this x0)))))
         (s3-0 (the int (* 256.0 f0-1)))
         (v1-5 (the int (-> this scroll x)))
         (a0-2 (-> this x0))
         (f1-7 (-> this scroll x))
         (s2-0 (- a0-2 (the int (* (- f1-7 (* (the float (the int (/ f1-7 256.0))) 256.0)) f0-1))))
         (s1-0 (/ v1-5 256))
         (s0-0 (/ (+ v1-5 511) 256))
         )
    (-> s4-0 2)
    (set! sv-16 (* (-> s4-0 3) 256))
    (while (>= s0-0 s1-0)
      ;; og:preserve-this
      ; (texture-upload-dma this arg0 (+ (+ (-> s4-0 0) 16) (the-as uint s4-0)) 0 16 16 (gs-psm ct32))
      ; (dma-buffer-add-gs-set arg0 (texflush 0))
      (let ((v1-18 (+ (-> s4-0 1) (* (the-as uint s1-0) sv-16) (* (the int (-> this scroll y)) 256))))
        ; (texture-upload-dma this arg0 (+ (+ v1-18 16) (the-as uint s4-0)) 8 256 416 (gs-psm mt8))
        (pc-upload-map-texture arg0 (&+ s4-0 v1-18 16) (&+ s4-0 (-> s4-0 0) 16) 256 416 8 0)
        )
      (dma-buffer-add-gs-set arg0
                             (tex0-1 (new 'static 'gs-tex0 :tbp0 #x8 :tbw #x4 :psm #x13 :tw #x8 :th #x9 :cld #x1))
                             ;; og:preserve-this added mmag and mmin for texture filtering
                             (tex1-1 (new 'static 'gs-tex1 :mmag #x1 :mmin #x1))
                             (texflush 0)
                             )
      (sprite-dma this arg0 (+ s2-0 1792) (+ s3-0 1792 s2-0) (-> this y0) (-> this y1) 0 256)
      (+! s2-0 s3-0)
      (+! s1-0 1)
      )
    )
  #f
  )

(defmethod draw-from-minimap ((this bigmap) (arg0 dma-buffer) (arg1 connection-minimap))
  (local-vars (sv-80 vector4w))
  (rlet ((vf0 :class vf)
         (vf1 :class vf)
         (vf2 :class vf)
         )
    (init-vf0-vector)
    (cond
      ((= (-> arg1 position) #t)
       (let ((v1-4 (as-type (handle->process (-> arg1 handle)) process-drawable)))
         (if (and v1-4 (nonzero? (-> v1-4 root)))
             (vector-copy! (-> arg1 last-world-pos) (-> v1-4 root trans))
             )
         )
       )
      ((and (= (logand (the-as int (-> arg1 position)) 7) 4)
            (= (-> (the-as entity-actor (-> arg1 position)) type) entity-actor)
            )
       (let* ((v1-14 (the-as entity-actor (-> arg1 position)))
              (a0-13 (as-type
                       (if v1-14
                           (-> v1-14 extra process)
                           )
                       process-drawable
                       )
                     )
              )
         (if a0-13
             (vector-copy! (-> arg1 last-world-pos) (-> a0-13 root trans))
             (vector-copy! (-> arg1 last-world-pos) (-> (the-as entity-actor (-> arg1 position)) extra trans))
             )
         )
       )
      (else
        (vector-copy! (-> arg1 last-world-pos) (-> arg1 position))
        )
      )
    (let ((f30-0 (-> arg1 class scale))
          (s1-0 (-> arg1 class color))
          )
      (set! sv-80 (new 'stack-no-clear 'vector4w))
      (let ((s0-0 (new-stack-vector0))
            (s2-0 (new-stack-vector0))
            (s3-2 (new-stack-vector0))
            )
        (let ((f26-0 (-> *video-params* relative-x-scale))
              (f28-0 (-> *video-params* relative-x-scale-reciprical))
              )
          (-> arg1 class)
          (.lvf vf1 (&-> arg1 last-world-pos quad))
          (.lvf vf2 (&-> this offset quad))
          (.add.z.vf.y vf1 vf0 vf1)
          (.sub.vf vf1 vf1 vf2)
          (.mul.w.vf vf1 vf1 vf2)
          (.ftoi.vf vf1 vf1)
          (.svf (&-> sv-80 quad) vf1)
          (if (logtest? (-> arg1 class flags) (minimap-flag goal))
              (set! (-> arg1 class icon-xy x) (the-as uint (mod (the int (-> this goal-time)) 6)))
              )
          (bigmap-method-18 this (&-> sv-80 x))
          (cond
            ((get-horizontal-flip-flag *blit-displays-work*)
             (set! f26-0 (- f26-0))
             (set! (-> s0-0 x) (+ (the float (+ (- 2304 (-> sv-80 x)) (-> this x1))) (-> this scroll x)))
             )
            (else
              (set! (-> s0-0 x) (- (the float (+ (-> sv-80 x) 1792 (-> this x0))) (-> this scroll x)))
              )
            )
          (set! (-> s0-0 y) (+ 1840.0 (* (- (the float (-> sv-80 y)) (-> this scroll y)) f28-0)))
          (let ((f0-12 (* 20.0 f26-0 f30-0))
                (f1-8 (* 20.0 f28-0 f30-0))
                )
            (set! (-> s2-0 x) (the float (the int (- (-> s0-0 x) (/ f0-12 2)))))
            (set! (-> s2-0 y) (the float (the int (- (-> s0-0 y) (/ f1-8 2)))))
            (set! (-> s3-2 x) (+ (-> s2-0 x) f0-12))
            (set! (-> s3-2 y) (+ (-> s2-0 y) f1-8))
            )
          )
        ;; og:preserve-this cast
        (let* ((a2-1 (the int (+ (* (the-as uint 320) (-> arg1 class icon-xy x)) 8)))
               (a3-0 (the int (+ (* (the-as uint 320) (-> arg1 class icon-xy y)) 8)))
               ;; og:preserve-this fix icon misalignment
               (v1-55 (+ a2-1 300))
               (a0-35 (+ a3-0 300))
               (a1-6 (the-as object (-> arg0 base)))
               )
          (set! (-> (the-as (inline-array vector4w) a1-6) 0 quad) (-> this sprite-tmpl dma-vif quad))
          (set! (-> (the-as (inline-array vector4w) a1-6) 1 quad) (-> this sprite-tmpl quad 1))
          (set-vector!
            (-> (the-as (inline-array vector4w) a1-6) 2)
            (the-as int (-> s1-0 r))
            (the-as int (-> s1-0 g))
            (the-as int (-> s1-0 b))
            128
            )
          (set-vector! (-> (the-as (inline-array vector4w) a1-6) 3) a2-1 a3-0 0 0)
          (set-vector!
            (-> (the-as (inline-array vector4w) a1-6) 4)
            (the int (* 16.0 (-> s2-0 x)))
            (the int (* 16.0 (-> s2-0 y)))
            #xffffff
            0
            )
          (set-vector! (-> (the-as (inline-array vector4w) a1-6) 5) v1-55 a0-35 0 0)
          (set-vector!
            (-> (the-as (inline-array vector4w) a1-6) 6)
            (the int (* 16.0 (-> s3-2 x)))
            (the int (* 16.0 (-> s3-2 y)))
            #xffffff
            0
            )
          )
        )
      )
    (&+! (-> arg0 base) 112)
    0
    (none)
    )
  )

(defmethod initialize ((this bigmap))
  (set! (-> this bigmap-index) (bigmap-id city))
  (set-pending-file (-> this bigmap-image) (the-as string #f) 0 (process->handle *dproc*) 0.0)
  0
  (none)
  )

(defmethod update ((this bigmap))
  (when (-> this drawing-flag)
    (update! *minimap*)
    (cond
      ((= (-> *blit-displays-work* count-down) 1)
       (set-pending-file
         (-> this bigmap-image)
         "world-map"
         (the-as int (-> this load-index))
         (process->handle *dproc*)
         0.0
         )
       (set-pending-file (-> this tpage) "progress-minimap" 0 (process->handle *dproc*) 0.0)
       (set-pending-file (-> this tpage2) "progress-minimap" 1 (process->handle *dproc*) 0.0)
       (set! (-> this loading-flag) #t)
       )
      (else
        (update (-> this bigmap-image))
        (update (-> this tpage))
        (update (-> this tpage2))
        (when (and (-> this loading-flag)
                   (= (file-status (-> this bigmap-image) "world-map" (the-as int (-> this load-index))) 'active)
                   (= (file-status (-> this tpage) "progress-minimap" 0) 'active)
                   (= (file-status (-> this tpage2) "progress-minimap" 1) 'active)
                   (not (load-in-progress? *level*))
                   )
          (let ((s5-0 (-> *level* loading-level))
                (s4-0 (-> *texture-pool* allocate-func))
                (s3-0 (-> *texture-relocate-later* memcpy))
                (s2-0 loading-level)
                )
            (set! (-> *texture-pool* allocate-func) texture-page-common-boot-allocate)
            (set! (-> *level* loading-level) #f)
            (set! (-> *texture-relocate-later* memcpy) #f)
            (set! loading-level (-> this tpage heap))
            (set! (-> this progress-minimap)
                  (the-as
                    texture-page
                    (link (-> this tpage buf) (-> this tpage load-file data) (-> this tpage len) (-> this tpage heap) 4)
                    )
                  )
            (set! (-> this progress-minimap2)
                  (the-as
                    texture-page
                    (link (-> this tpage2 buf) (-> this tpage2 load-file data) (-> this tpage2 len) (-> this tpage2 heap) 4)
                    )
                  )
            (set! (-> *level* loading-level) s5-0)
            (set! (-> *texture-pool* allocate-func) s4-0)
            (set! (-> *texture-relocate-later* memcpy) s3-0)
            (set! loading-level s2-0)
            )
          (set! (-> this loading-flag) #f)
          )
        )
      )
    )
  0
  (none)
  )

;; WARN: Return type mismatch texture-page vs symbol.
(defmethod loaded? ((this bigmap))
  (the-as symbol (and (-> *bigmap* progress-minimap) (-> *bigmap* progress-minimap2)))
  )

(defmethod draw! ((this bigmap) (arg0 int) (arg1 int) (arg2 int) (arg3 int))
  (local-vars
    (sv-96 (inline-array vector4w))
    (sv-100 texture)
    (sv-104 matrix)
    (sv-112 int)
    (sv-120 float)
    (sv-240 (function bigmap vector none))
    )
  (when (and (= (file-status (-> this bigmap-image) "world-map" (the-as int (-> this load-index))) 'active)
             (not (-> this loading-flag))
             )
    (with-dma-buffer-add-bucket ((s2-0 (-> *display* frames (-> *display* on-screen) global-buf))
                                 ;; og:preserve-this
                                 (bucket-id tex-hud-hud-alpha)
                                 )
      (dma-buffer-add-gs-set s2-0
                             (scissor-1 (new 'static 'gs-scissor
                                          :scax0 (+ arg0 -1792)
                                          :scay0 (+ arg1 -1840)
                                          :scax1 (+ arg2 -1792)
                                          :scay1 (+ arg3 -1840)
                                          )
                                        )
                             (test-1 (new 'static 'gs-test :ate #x1 :afail #x3 :zte #x1 :ztst (gs-ztest always)))
                             (alpha-1 (new 'static 'gs-alpha :b #x1 :d #x1))
                             (clamp-1 (new 'static 'gs-clamp :wms (gs-tex-wrap-mode clamp) :wmt (gs-tex-wrap-mode clamp)))
                             )
      (let ((v1-14 (the-as object (-> this bigmap-image art-group)))
            (f0-0 (-> *video-params* relative-x-scale))
            )
        (set! (-> this scroll x)
              (fmax 0.0 (fmin (-> this scroll x) (the float (+ (-> (the-as (pointer uint32) v1-14) 2) -513))))
              )
        (set! (-> this scroll y)
              (fmax
                0.0
                (fmin (-> this scroll y) (+ (- -1.0 (* 416.0 f0-0)) (the float (-> (the-as (pointer uint32) v1-14) 3))))
                )
              )
        )
      (cond
        ((get-horizontal-flip-flag *blit-displays-work*)
         (set! (-> this x0) (+ arg2 -1792))
         (set! (-> this x1) (+ arg0 -1792))
         )
        (else
          (set! (-> this x0) (+ arg0 -1792))
          (set! (-> this x1) (+ arg2 -1792))
          )
        )
      (set! (-> this y0) arg1)
      (set! (-> this y1) arg3)
      (bigmap-method-20 this s2-0)
      (when (!= (-> this load-index) (bigmap-id none))
        (when (= (-> this y0) 1840)
          (let ((s0-1 (-> s2-0 base))
                (s1-1 (get-texture big-map-icons progress-minimap))
                )
            (when s1-1
              (set! (-> (the-as (pointer uint128) s0-1) 0) (-> this adgif-tmpl dma-vif quad))
              (set! (-> (the-as (pointer uint128) s0-1) 1) (-> this adgif-tmpl quad 1))
              (adgif-shader<-texture-simple! (the-as adgif-shader (&+ s0-1 32)) s1-1)
              (&+! (-> s2-0 base) 112)
              )
            (if (not s1-1)
                (format 0 "ERROR: bigmap: mini-map-icons texture is #f~%")
                )
            )
          (let ((s1-2 (-> *minimap* engine alive-list)))
            (while s1-2
              (let ((a2-11 s1-2))
                (when (logtest? (-> a2-11 class flags) (minimap-flag bigmap bigmap-only))
                  (if (not (and (logtest? (minimap-flag local-only) (-> a2-11 class flags))
                                (not (logtest? (the-as minimap-flag (logand (bigmap-flag ctywide waswide wasall desert) (-> this global-flags)))
                                               (-> a2-11 class flags)
                                               )
                                     )
                                )
                           )
                      (draw-from-minimap this s2-0 a2-11)
                      )
                  )
                )
              (set! s1-2 (-> s1-2 next))
              )
            )
          (let ((s1-3 (new 'stack-no-clear 'vector))
                (f30-0 (-> *video-params* relative-x-scale))
                )
            (vector-z-quaternion! s1-3 (-> *target* control quat))
            (vector-xz-normalize! s1-3 -1.0)
            (set! (-> s1-3 y) 0.0)
            (set! (-> s1-3 w) 0.0)
            (set! sv-96 (the-as (inline-array vector4w) (-> s2-0 base)))
            (set! sv-100 (get-texture map-target-marker progress-minimap))
            (set! sv-104 (new 'stack-no-clear 'matrix))
            (set! sv-112 (the int (* 56.0 f30-0)))
            (set! sv-120 (-> *video-params* relative-x-scale-reciprical))
            (when sv-100
              (let ((s0-2 this))
                (set! sv-240 (method-of-object s0-2 set-pos!))
                (let ((a1-29 (target-pos 0)))
                  (sv-240 s0-2 a1-29)
                  )
                )
              (let ((s0-3 (new 'stack 'vector4w)))
                0.0
                (set! (-> s0-3 quad) (-> this pos quad))
                (bigmap-method-18 this (&-> s0-3 x))
                (let ((f0-15 (cond
                               ((get-horizontal-flip-flag *blit-displays-work*)
                                (set! f30-0 (- f30-0))
                                (+ (the float (+ (- 2304 (-> s0-3 x)) (-> this x1))) (-> this scroll x))
                                )
                               (else
                                 (- (the float (+ (-> s0-3 x) 1792 (-> this x0))) (-> this scroll x))
                                 )
                               )
                             )
                      )
                  (set-vector! (-> sv-104 rvec) (* (-> s1-3 z) f30-0) 0.0 (- (-> s1-3 x)) 0.0)
                  (set-vector! (-> sv-104 uvec) 0.0 1.0 0.0 0.0)
                  (set-vector! (-> sv-104 fvec) (* (-> s1-3 x) f30-0) 0.0 (-> s1-3 z) 1.0)
                  (set-vector!
                    (-> sv-104 trans)
                    f0-15
                    0.0
                    (+ 1840.0 (* (- (the float (-> s0-3 y)) (-> this scroll y)) sv-120))
                    1.0
                    )
                  )
                )
              (let* ((v1-83 (mod (-> *display* real-clock frame-counter) 360))
                     (f0-26 (+ 1.5 (* 0.25 (cos (* 182.04445 (the float v1-83))))))
                     (f0-27 (* 7.0 f0-26))
                     )
                (cond
                  ((or (= (-> this load-index) (bigmap-id sewer-met-hum)) (= (-> this load-index) (bigmap-id sewer-hum-kg)))
                   (set-vector! (-> this corner 0) (- f0-27) 0.0 0.0 1.0)
                   (set-vector! (-> this corner 1) 0.0 0.0 f0-27 1.0)
                   (set-vector! (-> this corner 2) 0.0 0.0 (- f0-27) 1.0)
                   (set-vector! (-> this corner 3) f0-27 0.0 0.0 1.0)
                   )
                  (else
                    (set-vector! (-> this corner 0) 0.0 0.0 (- f0-27) 1.0)
                    (set-vector! (-> this corner 1) f0-27 0.0 0.0 1.0)
                    (set-vector! (-> this corner 2) (- f0-27) 0.0 0.0 1.0)
                    (set-vector! (-> this corner 3) 0.0 0.0 f0-27 1.0)
                    )
                  )
                )
              (vector-matrix*! (-> this corner 0) (-> this corner 0) sv-104)
              (vector-matrix*! (-> this corner 1) (-> this corner 1) sv-104)
              (vector-matrix*! (-> this corner 2) (-> this corner 2) sv-104)
              (vector-matrix*! (-> this corner 3) (-> this corner 3) sv-104)
              (let ((v1-97 (-> this adgif-tmpl dma-vif quad)))
                (set! (-> sv-96 0 quad) v1-97)
                )
              (let ((v1-98 (-> this adgif-tmpl quad 1)))
                (set! (-> sv-96 1 quad) v1-98)
                )
              (adgif-shader<-texture-simple! (the-as adgif-shader (-> sv-96 2)) sv-100)
              (let ((v1-100 (-> this draw-tmpl dma-vif quad)))
                (set! (-> sv-96 7 quad) v1-100)
                )
              (let ((v1-101 (-> this draw-tmpl quad 1)))
                (set! (-> sv-96 8 quad) v1-101)
                )
              (set-vector! (-> sv-96 9) 0 255 255 128)
              (set-vector! (-> sv-96 10) 0 0 0 0)
              (set-vector!
                (-> sv-96 11)
                (the int (* 16.0 (-> this corner 0 x)))
                (the int (* 16.0 (-> this corner 0 z)))
                #xffffff
                0
                )
              (set-vector! (-> sv-96 12) 256 0 0 0)
              (set-vector!
                (-> sv-96 13)
                (the int (* 16.0 (-> this corner 1 x)))
                (the int (* 16.0 (-> this corner 1 z)))
                #xffffff
                0
                )
              (set-vector! (-> sv-96 14) 0 256 0 0)
              (set-vector!
                (-> sv-96 15)
                (the int (* 16.0 (-> this corner 2 x)))
                (the int (* 16.0 (-> this corner 2 z)))
                #xffffff
                0
                )
              (set-vector! (-> sv-96 16) 256 256 0 0)
              (set-vector!
                (-> sv-96 17)
                (the int (* 16.0 (-> this corner 3 x)))
                (the int (* 16.0 (-> this corner 3 z)))
                #xffffff
                0
                )
              (&+! (-> s2-0 base) 288)
              )
            )
          )
        )
      (dma-buffer-add-gs-set s2-0 (scissor-1 (new 'static 'gs-scissor :scax1 #x1ff :scay1 #x19f)))
      )
    (when (= (-> this load-index) (bigmap-id none))
      (let ((s3-1
              (new 'stack 'font-context *font-default-matrix* 0 0 0.0 (font-color default) (font-flags shadow kerning))
              )
            )
        (let ((f30-2 (* 0.0024038462 (the float (- arg3 arg1)))))
          (set-scale! s3-1 f30-2)
          (set-width! s3-1 (the int (* 400.0 f30-2)))
          (let ((a0-100 s3-1))
            (set! (-> a0-100 flags) (font-flags kerning middle large))
            )
          (let ((s5-1 print-game-text))
            (format (clear *temp-string*) "~S" (lookup-text! *common-text* (text-id map-data-unavailable) #f))
            (let* ((f0-63 (s5-1 *temp-string* s3-1 #t 44 (bucket-id hud-draw-hud-alpha)))
                   (v1-142 s3-1)
                   (a0-106 (- 256 (the int (* 200.0 f30-2))))
                   (a1-52 (- 208 (the int (/ f0-63 2))))
                   )
              (set! (-> v1-142 origin x) (the float a0-106))
              (set! (-> v1-142 origin y) (the float a1-52))
              )
            )
          )
        (let ((s5-2 print-game-text))
          (format (clear *temp-string*) "~S" (lookup-text! *common-text* (text-id map-data-unavailable) #f))
          (s5-2 *temp-string* s3-1 #f 44 (bucket-id hud-draw-hud-alpha))
          )
        )
      )
    (+! (-> this goal-time) (* 16.0 (seconds-per-frame)))
    (set-dirty-mask! (-> *level* level-default) 4 #x1a400 0)
    )
  0
  (none)
  )

(defmethod handle-cpad-input ((this bigmap))
  (let ((v1-1 (-> this bigmap-image art-group))
        (s5-0 (-> *cpad-list* cpads 0))
        )
    (when v1-1
      (let ((f30-0 (analog-input (the-as int (-> s5-0 leftx)) 128.0 32.0 110.0 4.0))
            (f0-0 (analog-input (the-as int (-> s5-0 lefty)) 128.0 32.0 110.0 4.0))
            )
        (+! (-> this scroll x) f30-0)
        (+! (-> this scroll y) f0-0)
        )
      )
    )
  0
  (none)
  )

(defmethod set-map-indices! ((this bigmap))
  (let ((s5-0 (level-get-target-inside *level*)))
    (let ((v1-2 (-> *setting-control* user-current bigmap-level)))
      (cond
        (v1-2
          (cond
            ((= v1-2 'city)
             (set! (-> this bigmap-index) (bigmap-id city))
             0
             )
            ((= v1-2 'comb)
             (set! (-> this bigmap-index) (bigmap-id comb))
             )
            ((= v1-2 'desert)
             (set! (-> this bigmap-index) (bigmap-id desert))
             )
            ((= v1-2 'factory)
             (set! (-> this bigmap-index) (bigmap-id factory))
             )
            ((= v1-2 'forest)
             (set! (-> this bigmap-index) (bigmap-id forest))
             )
            ((= v1-2 'metalhead-city)
             (set! (-> this bigmap-index) (bigmap-id mhcity))
             )
            ((= v1-2 'mine)
             (set! (-> this bigmap-index) (bigmap-id mine))
             )
            ((= v1-2 'nest)
             (set! (-> this bigmap-index) (bigmap-id nest))
             )
            ((= v1-2 'nest2)
             (set! (-> this bigmap-index) (bigmap-id nest2))
             )
            ((= v1-2 'none)
             (set! (-> this bigmap-index) (bigmap-id none))
             )
            ((= v1-2 'precursor1)
             (set! (-> this bigmap-index) (bigmap-id precursor1))
             )
            ((= v1-2 'precursor2)
             (set! (-> this bigmap-index) (bigmap-id precursor2))
             )
            ((= v1-2 'rubble)
             (set! (-> this bigmap-index) (bigmap-id rubble))
             )
            ((= v1-2 'sewer-hum-kg)
             (set! (-> this bigmap-index) (bigmap-id sewer-hum-kg))
             )
            ((= v1-2 'sewer-kg-met)
             (set! (-> this bigmap-index) (bigmap-id sewer-kg-met))
             )
            ((= v1-2 'sewer-met-hum)
             (set! (-> this bigmap-index) (bigmap-id sewer-met-hum))
             )
            ((= v1-2 'stadium)
             (set! (-> this bigmap-index) (bigmap-id stadium))
             )
            ((= v1-2 'temple1)
             (set! (-> this bigmap-index) (bigmap-id temple1))
             )
            ((= v1-2 'temple2)
             (set! (-> this bigmap-index) (bigmap-id temple2))
             )
            ((= v1-2 'temple3)
             (set! (-> this bigmap-index) (bigmap-id temple3))
             )
            ((= v1-2 'temple4)
             (set! (-> this bigmap-index) (bigmap-id temple4))
             )
            ((= v1-2 'tower)
             (set! (-> this bigmap-index) (bigmap-id tower))
             )
            ((= v1-2 'volcano)
             (set! (-> this bigmap-index) (bigmap-id volcano))
             )
            ((= v1-2 'wascity)
             (set! (-> this bigmap-index) (bigmap-id wascity))
             )
            )
          )
        (s5-0
          (set! (-> this bigmap-index) (-> s5-0 info bigmap-id))
          )
        )
      )
    (if (and (= (level-status? *level* 'ctywide #f) 'active)
             (not (or (= (-> s5-0 name) 'mhcitya) (= (-> s5-0 name) 'mhcityb)))
             )
        (set! (-> this bigmap-index) (-> ctyport bigmap-id))
        )
    )
  (cond
    ((= (-> this bigmap-index) (bigmap-id temple1))
     (cond
       ((task-node-closed? (game-task-node factory-boss-resolution))
        (set! (-> this load-index) (bigmap-id temple4))
        )
       ((task-node-closed? (game-task-node desert-oasis-defense-resolution))
        (set! (-> this load-index) (bigmap-id temple3))
        )
       ((task-node-closed? (game-task-node volcano-darkeco-resolution))
        (set! (-> this load-index) (bigmap-id temple2))
        )
       (else
         (set! (-> this load-index) (bigmap-id temple1))
         )
       )
     )
    ((= (-> this bigmap-index) (bigmap-id precursor1))
     (if (task-node-closed? (game-task-node comb-wild-ride-resolution))
         (set! (-> this load-index) (bigmap-id precursor2))
         (set! (-> this load-index) (bigmap-id precursor1))
         )
     )
    ((= (-> this bigmap-index) (bigmap-id nest))
     (if (task-node-closed? (game-task-node nest-eggs-gas))
         (set! (-> this load-index) (bigmap-id nest2))
         (set! (-> this load-index) (bigmap-id nest))
         )
     )
    (else
      (set! (-> this load-index) (-> this bigmap-index))
      )
    )
  (none)
  )

;; WARN: Return type mismatch bigmap-flag vs none.
(defmethod enable-drawing ((this bigmap))
  (set-map-indices! this)
  (vector-copy! (-> this offset) (-> *bigmap-info-array* data (-> this load-index)))
  (let ((s4-0 (target-pos 0))
        (s5-0 (-> this offset))
        )
    (cond
      ((= (-> this load-index) (bigmap-id sewer-hum-kg))
       (let ((a0-10 (level-get *level* 'sewa)))
         (if a0-10
             (set! (-> a0-10 info bigmap-id) (bigmap-id sewer-hum-kg))
             )
         )
       (set! (-> this scroll x) (+ -256.0 (* (- (-> s4-0 y) (-> s5-0 y)) (-> s5-0 w))))
       (set! (-> this scroll y) (- 624.0 (* (- (-> s4-0 x) (-> s5-0 x)) (-> s5-0 w))))
       )
      ((= (-> this load-index) (bigmap-id sewer-kg-met))
       (let ((a0-14 (level-get *level* 'sewa)))
         (if a0-14
             (set! (-> a0-14 info bigmap-id) (bigmap-id sewer-kg-met))
             )
         )
       (set! (-> this scroll x) (+ -256.0 (* (- (-> s4-0 x) (-> s5-0 x)) (-> s5-0 w))))
       (set! (-> this scroll y) (+ -208.0 (* (- (-> s4-0 z) (-> s5-0 y)) (-> s5-0 w))))
       )
      ((= (-> this load-index) (bigmap-id sewer-met-hum))
       (let ((a0-18 (level-get *level* 'sewa)))
         (if a0-18
             (set! (-> a0-18 info bigmap-id) (bigmap-id sewer-met-hum))
             )
         )
       (set! (-> this scroll x) (+ -256.0 (* (- (-> s4-0 y) (-> s5-0 y)) (-> s5-0 w))))
       (set! (-> this scroll y) (- 624.0 (* (- (-> s4-0 x) (-> s5-0 x)) (-> s5-0 w))))
       )
      (else
        (set! (-> this scroll x) (+ -256.0 (* (- (-> s4-0 x) (-> s5-0 x)) (-> s5-0 w))))
        (set! (-> this scroll y) (+ -208.0 (* (- (-> s4-0 z) (-> s5-0 y)) (-> s5-0 w))))
        )
      )
    )
  (set! (-> this drawing-flag) #t)
  (set! (-> this loading-flag) #f)
  (set! (-> this global-flags) (bigmap-flag))
  (if (= (level-status? *level* 'ctywide #f) 'active)
      (logior! (-> this global-flags) (bigmap-flag ctywide))
      )
  (if (= (level-status? *level* 'waswide #f) 'active)
      (logior! (-> this global-flags) (bigmap-flag wasall))
      )
  (if (= (level-status? *level* 'wasall #f) 'active)
      (logior! (-> this global-flags) (bigmap-flag waswide))
      )
  (if (= (level-status? *level* 'desert #f) 'active)
      (logior! (-> this global-flags) (bigmap-flag desert))
      )
  (none)
  )

(defmethod disable-drawing ((this bigmap))
  (set-pending-file
    (-> this bigmap-image)
    (the-as string #f)
    (the-as int (-> this bigmap-index))
    (process->handle *dproc*)
    0.0
    )
  (set-pending-file (-> this tpage) (the-as string #f) 0 (process->handle *dproc*) 0.0)
  (set-pending-file (-> this tpage2) (the-as string #f) 0 (process->handle *dproc*) 0.0)
  (let ((v1-12 #f))
    (while (not v1-12)
      (update (-> this bigmap-image))
      (update (-> this tpage))
      (update (-> this tpage2))
      (set! v1-12 (and (= (-> this bigmap-image status) 'inactive)
                       (= (-> this tpage status) 'inactive)
                       (= (-> this tpage2 status) 'inactive)
                       )
            )
      )
    )
  (when (-> this progress-minimap)
    (unload-page *texture-pool* (-> this progress-minimap))
    (set! (-> (&-> *level* level-default texture-page 6) 0) (the-as texture-page 0))
    (set! (-> this progress-minimap) #f)
    )
  (when (-> this progress-minimap2)
    (unload-page *texture-pool* (-> this progress-minimap2))
    (set! (-> (&-> *level* level-default texture-page 3) 0) (the-as texture-page 0))
    (set! (-> this progress-minimap2) #f)
    )
  (set! (-> this drawing-flag) #f)
  (set! (-> this loading-flag) #f)
  0
  )

(kmemopen global "bigmap-struct")

;; og:preserve-this
(define-perm *bigmap* bigmap (new 'global 'bigmap))

(kmemclose)
