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

;; name: race-hud.gc
;; name in dgo: race-hud
;; dgos: STD, STC, LERLCHAL, STB, LPRTRACE

;; DECOMP BEGINS

(defmethod draw ((this hud-race-timer))
  (set-hud-piece-position!
    (the-as hud-sprite (-> this sprites))
    0
    (the int (+ 20.0 (* -100.0 (-> this offset))))
    )
  (format (clear (-> this strings 0 text)) "~2,'0D:" (-> this values 0 current))
  (set-as-offset-from! (the-as hud-sprite (-> this strings 0 pos)) (the-as vector4w (-> this sprites)) 60 5)
  (format (clear (-> this strings 1 text)) "~2,'0D:" (-> this values 1 current))
  (set-as-offset-from!
    (the-as hud-sprite (-> this strings 1 pos))
    (the-as vector4w (-> this strings 0 pos))
    30
    0
    )
  (format (clear (-> this strings 2 text)) "~2,'0D" (-> this values 2 current))
  (set-as-offset-from!
    (the-as hud-sprite (-> this strings 2 pos))
    (the-as vector4w (-> this strings 1 pos))
    28
    0
    )
  (set-as-offset-from! (-> this sprites 1) (the-as vector4w (-> this sprites)) 128 0)
  ((method-of-type hud draw) this)
  0
  (none)
  )

(defmethod update-values ((this hud-race-timer))
  (set! (-> this values 0 target) (/ (the-as int (-> *game-info* race-timer)) (seconds 60)))
  (set! (-> this values 1 target) (/ (mod (the-as int (-> *game-info* race-timer)) (seconds 60)) (seconds 1)))
  ;(set! (-> this values 2 target) (/ (mod (mod (the-as int (-> *game-info* race-timer)) (seconds 60)) (seconds 1)) (/ (seconds 1) 60)))
  ;; og:preserve-this fix wrong hundredths counter...
  (set! (-> this values 2 target) (/ (mod (mod (the-as int (-> *game-info* race-timer)) (seconds 60)) (seconds 1)) (seconds 0.01)))
  (logclear! (-> this flags) (hud-flags disable))
  ((method-of-type hud update-values) this)
  0
  (none)
  )

(defmethod init-callback ((this hud-race-timer))
  (set! (-> this level) (level-get *level* 'ctywide))
  (set! (-> this gui-id)
        (add-process *gui-control* this (gui-channel hud-upper-left) (gui-action hidden) (-> this name) 81920.0 0)
        )
  (logior! (-> this flags) (hud-flags show))
  (set! (-> this sprites 0 tex) (get-texture stadiumb-hud-time-01 ctywide-minimap))
  (alloc-string-if-needed this 0)
  (set! (-> this strings 0 scale) 0.55)
  (set! (-> this strings 0 flags) (font-flags kerning middle large))
  (set! (-> this strings 0 color) (font-color green))
  (alloc-string-if-needed this 1)
  (set! (-> this strings 1 scale) 0.55)
  (set! (-> this strings 1 flags) (font-flags kerning middle large))
  (set! (-> this strings 1 color) (font-color green))
  (alloc-string-if-needed this 2)
  (set! (-> this strings 2 scale) 0.55)
  (set! (-> this strings 2 flags) (font-flags kerning middle large))
  (set! (-> this strings 2 color) (font-color green))
  (set! (-> this sprites 1 tex) (get-texture stadiumb-hud-time-02 ctywide-minimap))
  0
  (none)
  )

(defmethod draw ((this hud-race-lap-counter))
  (set-hud-piece-position!
    (the-as hud-sprite (-> this sprites))
    504
    (the int (+ 20.0 (* -100.0 (-> this offset))))
    )
  (set! (-> this strings 0 scale) 0.55)
  (format (clear (-> this strings 0 text)) "~D/~D" (-> this values 0 current) (-> this values 1 current))
  (set-as-offset-from! (the-as hud-sprite (-> this strings 0 pos)) (the-as vector4w (-> this sprites)) -40 5)
  (set-as-offset-from! (-> this sprites 1) (the-as vector4w (-> this sprites)) -20 0)
  (set-as-offset-from! (-> this sprites 2) (the-as vector4w (-> this sprites 1)) -50 0)
  ((method-of-type hud draw) this)
  0
  (none)
  )

(defmethod update-values ((this hud-race-lap-counter))
  (set! (-> this values 0 target) (-> *game-info* race-current-lap-count))
  (set! (-> this values 1 target) (-> *game-info* race-total-lap-count))
  (logclear! (-> this flags) (hud-flags disable))
  ((method-of-type hud update-values) this)
  0
  (none)
  )

(defmethod init-callback ((this hud-race-lap-counter))
  (set! (-> this level) (level-get *level* 'ctywide))
  (set! (-> this gui-id)
        (add-process *gui-control* this (gui-channel hud-upper-right) (gui-action hidden) (-> this name) 81920.0 0)
        )
  (logior! (-> this flags) (hud-flags show))
  (set! (-> this sprites 0 tex) (get-texture stadiumb-hud-lap-03 ctywide-minimap))
  (alloc-string-if-needed this 0)
  (set! (-> this strings 0 flags) (font-flags kerning middle large))
  (set! (-> this strings 0 color) (font-color green))
  (set! (-> this sprites 1 tex) (get-texture stadiumb-hud-lap-02 ctywide-minimap))
  (set! (-> this sprites 2 tex) (get-texture stadiumb-hud-lap-01 ctywide-minimap))
  0
  (none)
  )

(defmethod draw ((this hud-race-turbo-counter))
  (set-hud-piece-position!
    (the-as hud-sprite (-> this sprites))
    224
    (the int (+ 25.0 (* -100.0 (-> this offset))))
    )
  (set-as-offset-from! (-> this sprites 1) (the-as vector4w (-> this sprites)) 0 0)
  (set-as-offset-from! (-> this sprites 2) (the-as vector4w (-> this sprites 1)) 32 0)
  (set-as-offset-from! (-> this sprites 3) (the-as vector4w (-> this sprites 1)) 32 0)
  (set-as-offset-from! (-> this sprites 4) (the-as vector4w (-> this sprites 2)) 32 0)
  (set-as-offset-from! (-> this sprites 5) (the-as vector4w (-> this sprites 2)) 32 0)
  (let* ((v1-2 (-> this values 0 current))
         (f0-3 (if (> v1-2 0)
                   1.0
                   0.0
                   )
               )
         (f1-2 (if (< 1 v1-2)
                   1.0
                   0.0
                   )
               )
         (f2-1 (if (< 2 v1-2)
                   1.0
                   0.0
                   )
               )
         )
    (set! (-> this sprites 1 scale-x) f0-3)
    (set! (-> this sprites 1 scale-y) f0-3)
    (set! (-> this sprites 3 scale-x) f1-2)
    (set! (-> this sprites 3 scale-y) f1-2)
    (set! (-> this sprites 5 scale-x) f2-1)
    (set! (-> this sprites 5 scale-y) f2-1)
    )
  ((method-of-type hud draw) this)
  0
  (none)
  )

(defmethod update-values ((this hud-race-turbo-counter))
  (set! (-> this values 0 target) (-> *game-info* race-number-turbos))
  (logclear! (-> this flags) (hud-flags disable))
  ((method-of-type hud update-values) this)
  0
  (none)
  )

(defmethod init-callback ((this hud-race-turbo-counter))
  (set! (-> this level) (level-get *level* 'stadium))
  (set! (-> this gui-id)
        (add-process *gui-control* this (gui-channel hud-upper-center) (gui-action hidden) (-> this name) 81920.0 0)
        )
  (logior! (-> this flags) (hud-flags show))
  (set! (-> this sprites 0 tex) (get-texture stadiumb-hud-booster-off-01 stadium-minimap))
  (set! (-> this sprites 1 tex) (get-texture stadiumb-hud-booster-on-01 stadium-minimap))
  (set! (-> this sprites 2 tex) (get-texture stadiumb-hud-booster-off-01 stadium-minimap))
  (set! (-> this sprites 3 tex) (get-texture stadiumb-hud-booster-on-01 stadium-minimap))
  (set! (-> this sprites 4 tex) (get-texture stadiumb-hud-booster-off-01 stadium-minimap))
  (set! (-> this sprites 5 tex) (get-texture stadiumb-hud-booster-on-01 stadium-minimap))
  0
  (none)
  )

(defmethod draw ((this hud-race-position))
  (local-vars (s5-0 int))
  (set-hud-piece-position!
    (the-as hud-sprite (-> this sprites))
    24
    (the int (+ 324.0 (* 100.0 (-> this offset))))
    )
  (set-as-offset-from! (-> this sprites 1) (the-as vector4w (-> this sprites)) 0 0)
  (set-as-offset-from! (-> this sprites 2) (the-as vector4w (-> this sprites)) 0 0)
  (set-as-offset-from! (-> this sprites 3) (the-as vector4w (-> this sprites)) 0 0)
  (set-as-offset-from! (-> this sprites 4) (the-as vector4w (-> this sprites)) 0 0)
  (set-as-offset-from! (-> this sprites 5) (the-as vector4w (-> this sprites)) 0 0)
  (set-as-offset-from! (-> this sprites 6) (the-as vector4w (-> this sprites)) 0 0)
  (set-as-offset-from! (-> this sprites 7) (the-as vector4w (-> this sprites)) 0 0)
  (let ((v1-2 (-> this values 0 current)))
    (dotimes (a0-9 16)
      (set! (-> this sprites a0-9 scale-x) (if (= a0-9 v1-2)
                                               0.8
                                               0.0
                                               )
            )
      (set! (-> this sprites a0-9 scale-y) (if (= a0-9 v1-2)
                                               0.8
                                               0.0
                                               )
            )
      )
    (cond
      ((or (= (-> *common-text* language-id) 4) (= (-> *common-text* language-id) 3))
       (set! s5-0 12)
       )
      ((= (-> *common-text* language-id) 1)
       (set! s5-0 (if (> v1-2 0)
                      14
                      13
                      )
             )
       )
      ((= (-> *common-text* language-id) 6)
       (set! s5-0 15)
       )
      (else
        (set! s5-0 (min 11 (+ v1-2 8)))
        )
      )
    (set-as-offset-from!
      (-> this sprites s5-0)
      (the-as vector4w (-> this sprites v1-2))
      (if (zero? v1-2)
          28
          52
          )
      0
      )
    )
  (set! (-> this sprites s5-0 scale-x) 0.8)
  (set! (-> this sprites s5-0 scale-y) 0.8)
  ((method-of-type hud draw) this)
  0
  (none)
  )

(defmethod update-values ((this hud-race-position))
  (set! (-> this values 0 target) (-> *game-info* race-position))
  (logclear! (-> this flags) (hud-flags disable))
  ((method-of-type hud update-values) this)
  0
  (none)
  )

(defmethod init-callback ((this hud-race-position))
  (set! (-> this level) (level-get *level* 'ctywide))
  (set! (-> this gui-id)
        (add-process *gui-control* this (gui-channel hud-lower-left) (gui-action hidden) (-> this name) 81920.0 0)
        )
  (logior! (-> this flags) (hud-flags show))
  (set! (-> this sprites 0 tex) (get-texture stadiumb-hud-nmbr-01 ctywide-minimap))
  (set! (-> this sprites 1 tex) (get-texture stadiumb-hud-nmbr-02 ctywide-minimap))
  (set! (-> this sprites 2 tex) (get-texture stadiumb-hud-nmbr-03 ctywide-minimap))
  (set! (-> this sprites 3 tex) (get-texture stadiumb-hud-nmbr-04 ctywide-minimap))
  (set! (-> this sprites 4 tex) (get-texture stadiumb-hud-nmbr-05 ctywide-minimap))
  (set! (-> this sprites 5 tex) (get-texture stadiumb-hud-nmbr-06 ctywide-minimap))
  (set! (-> this sprites 6 tex) (get-texture stadiumb-hud-nmbr-07 ctywide-minimap))
  (set! (-> this sprites 7 tex) (get-texture stadiumb-hud-nmbr-08 ctywide-minimap))
  (set! (-> this sprites 8 tex) (get-texture stadiumb-hud-ord-st ctywide-minimap))
  (set! (-> this sprites 9 tex) (get-texture stadiumb-hud-ord-nd ctywide-minimap))
  (set! (-> this sprites 10 tex) (get-texture stadiumb-hud-ord-rd ctywide-minimap))
  (set! (-> this sprites 11 tex) (get-texture stadiumb-hud-ord-th ctywide-minimap))
  (set! (-> this sprites 12 tex) (get-texture stadiumb-hud-ord-o ctywide-minimap))
  (set! (-> this sprites 13 tex) (get-texture stadiumb-hud-ord-er ctywide-minimap))
  (set! (-> this sprites 14 tex) (get-texture stadiumb-hud-ord-e ctywide-minimap))
  (set! (-> this sprites 15 tex) (get-texture stadiumb-hud-ord-korean ctywide-minimap))
  0
  (none)
  )

(defun rminutes ((arg0 int))
  (/ arg0 #x4650)
  )

(defun rseconds ((arg0 int))
  (/ (mod arg0 #x4650) 300)
  )

(defun rmicroseconds ((arg0 int))
  (/ (mod (mod arg0 #x4650) 300) 5)
  )

(defmethod draw ((this hud-race-final-stats))
  (local-vars
    (s0-0 int)
    (sv-112 (function string font-context symbol int bucket-id float))
    (sv-128 (function string font-context symbol int bucket-id float))
    (sv-144 string)
    (sv-160 string)
    )
  (let* ((v1-0 *race-state*)
         (s5-0 (-> v1-0 racer-array (-> v1-0 i-player)))
         )
    30
    (let ((s2-0 (- (-> s5-0 finish-time) (-> v1-0 race-start-time))))
      (/ (the-as int s2-0) #x4650)
      (/ (mod (the-as int s2-0) #x4650) 300)
      (/ (mod (mod (the-as int s2-0) #x4650) 300) 5)
      (new 'static 'font-context)
      (let* ((s4-0 0)
             (s3-0 (+ s4-0 3))
             )
        (let ((s1-0
                (new 'stack 'font-context *font-default-matrix* 0 0 0.0 (font-color default) (font-flags shadow kerning))
                )
              )
          (set! (-> this strings 0 scale) 0.0)
          (set! (-> s1-0 origin x) 45.0)
          (set! (-> s1-0 origin y) 20.0)
          (let ((v1-15 s1-0))
            (set! (-> v1-15 width) (the float 422))
            )
          (let ((v1-16 s1-0))
            (set! (-> v1-16 height) (the float 80))
            )
          (let ((a0-12 s1-0))
            (set! (-> a0-12 color) (font-color red))
            )
          (let ((a0-13 s1-0))
            (set! (-> a0-13 flags) (font-flags kerning middle middle-vert large))
            )
          (let ((v1-19 s1-0))
            (set! (-> v1-19 scale) 1.0)
            )
          (cond
            ((zero? (-> s5-0 finish-count))
             (set! s0-0 80)
             (let ((v1-21 s1-0))
               (set! (-> v1-21 scale) 1.6)
               )
             (when (text-language? german) ;; og:preserve-this text-language patch
               (let ((v1-24 s1-0))
                 (set! (-> v1-24 scale) 1.0)
                 )
               )
             (set! sv-112 print-game-text)
             (let ((a0-20 (lookup-text! *common-text* (text-id race-you-win) #f))
                   (a2-2 #f)
                   (a3-1 44)
                   (t0-1 320)
                   )
               (sv-112 a0-20 s1-0 a2-2 a3-1 (the-as bucket-id t0-1))
               )
             )
            (else
              (set! s0-0 80)
              (let ((v1-27 s1-0))
                (set! (-> v1-27 scale) 1.6)
                )
              (when (text-language? german) ;; og:preserve-this text-language patch
                (let ((v1-30 s1-0))
                  (set! (-> v1-30 scale) 1.0)
                  )
                )
              (set! sv-128 print-game-text)
              (let ((a0-26 (lookup-text! *common-text* (text-id race-you-lose) #f))
                    (a1-5 s1-0)
                    (a2-4 #f)
                    (a3-2 44)
                    (t0-2 320)
                    )
                (sv-128 a0-26 a1-5 a2-4 a3-2 (the-as bucket-id t0-2))
                )
              )
            )
          )
        (set! (-> this strings 1 scale) 0.5)
        (set-hud-piece-position!
          (the-as hud-sprite (-> this strings 1 pos))
          128
          (the int (+ (- 188.0 (the float s0-0)) (* -100.0 (-> this offset))))
          )
        (let ((s1-1 format))
          (set! sv-144 (clear (-> this strings 1 text)))
          (let* ((a0-29 *common-text*)
                 (t9-7 (method-of-object a0-29 lookup-text!))
                 (a1-7 375)
                 (a2-6 #f)
                 (a1-8 (t9-7 a0-29 (the-as text-id a1-7) a2-6))
                 )
            (s1-1 sv-144 a1-8 (the-as none a2-6))
            )
          )
        (set-hud-piece-position!
          (the-as hud-sprite (-> this strings 2 pos))
          384
          (the int (+ (- 188.0 (the float s0-0)) (* -100.0 (-> this offset))))
          )
        (set! (-> this strings 2 scale) 0.5)
        (set! (-> this strings 2 flags) (font-flags kerning right large))
        (print-time (clear (-> this strings 2 text)) (the-as time-frame s2-0))
        (while (and (< 1 (-> s5-0 lap-count)) (< s3-0 (+ (* (-> s5-0 lap-count) 2) 2)))
          (let ((s2-1 (-> s5-0 lap-time-array (+ (- -1 s4-0) (-> s5-0 lap-count)))))
            (set! (-> this strings s3-0 scale) 0.5)
            (set! (-> this strings s3-0 flags) (font-flags kerning large))
            (set! (-> this strings s3-0 color) (font-color white))
            (set-as-offset-from!
              (the-as hud-sprite (&+ (-> this strings 0 pos) (* 48 s3-0)))
              (the-as vector4w (-> this strings 1 pos))
              5
              (+ (* 28 s4-0) 40)
              )
            (let ((s1-3 format)
                  (s0-1 (clear (-> this strings s3-0 text)))
                  )
              (set! sv-160 "~S~D")
              (let ((a2-10 (lookup-text! *common-text* (text-id race-lap) #f))
                    (a3-4 (+ s4-0 1))
                    )
                (s1-3 s0-1 sv-160 a2-10 a3-4)
                )
              )
            (let ((s3-1 (+ s3-0 1)))
              (set! (-> this strings s3-1 scale) 0.5)
              (set! (-> this strings s3-1 flags) (font-flags kerning right large))
              (set! (-> this strings s3-1 color) (font-color white))
              (set-as-offset-from!
                (the-as hud-sprite (&+ (-> this strings 0 pos) (* 48 s3-1)))
                (the-as vector4w (-> this strings 2 pos))
                0
                (+ (* 28 s4-0) 40)
                )
              (print-time (clear (-> this strings s3-1 text)) (the-as time-frame s2-1))
              (+! s4-0 1)
              (set! s3-0 (+ s3-1 1))
              )
            )
          )
        )
      )
    )
  ((method-of-type hud draw) this)
  0
  (none)
  )

(defmethod update-values ((this hud-race-final-stats))
  (logclear! (-> this flags) (hud-flags disable))
  ((method-of-type hud update-values) this)
  0
  (none)
  )

(defmethod init-callback ((this hud-race-final-stats))
  (set! (-> this level) (level-get *level* 'stadium))
  (set! (-> this gui-id)
        (add-process *gui-control* this (gui-channel hud-middle-right) (gui-action hidden) (-> this name) 81920.0 0)
        )
  (logior! (-> this flags) (hud-flags show))
  (dotimes (s5-0 14)
    (alloc-string-if-needed this s5-0)
    )
  (set! (-> this strings 0 flags) (font-flags kerning middle large))
  (set! (-> this strings 0 color) (font-color red))
  (set! (-> this strings 1 flags) (font-flags kerning large))
  (set! (-> this strings 1 color) (font-color white))
  (set! (-> this strings 2 flags) (font-flags kerning large))
  (set! (-> this strings 2 color) (font-color white))
  0
  (none)
  )
