;;-*-Lisp-*-
(in-package goal)
(bundles "ENGINE.CGO" "GAME.CGO")
(require "engine/ui/progress/progress-draw.gc")
(require "engine/ui/progress/progress-part.gc")

;; DECOMP BEGINS

(deftype progress-global-state (basic)
  ((aspect-ratio-choice symbol)
   (video-mode-choice   symbol)
   (yes-no-choice       symbol)
   (which               int32)
   (starting-state      progress-screen)
   (last-slot-saved     int32)
   (slider-backup       float)
   (language-backup     language-enum)
   (on-off-backup       symbol)
   (center-x-backup     int32)
   (center-y-backup     int32)
   (aspect-ratio-backup symbol)
   (last-slider-sound   time-frame)))

(define *progress-state* (new 'static 'progress-global-state :yes-no-choice #f :which -1 :last-slot-saved -1))

(defun get-game-count ((arg0 int))
  (-> *game-counts* data arg0))

(defun progress-allowed? ()
  (not (or (-> *setting-control* current talking)
           (-> *setting-control* current movie)
           (movie?)
           (handle->process (-> *game-info* pov-camera-handle))
           (handle->process (-> *game-info* other-camera-handle))
           (< (-> *display* base-frame-counter) (-> *game-info* letterbox-time))
           (< (-> *display* base-frame-counter) (-> *game-info* blackout-time))
           (!= (-> *setting-control* current bg-a) 0.0)
           (!= (-> *setting-control* current bg-a-force) 0.0)
           (not (-> *setting-control* current allow-progress))
           (or (and (handle->process (-> *game-info* auto-save-proc))
                    (not (send-event (handle->process (-> *game-info* auto-save-proc)) 'progress-allowed?)))
               (not *target*)
               (= *cheat-mode* 'camera)))))

(defun pause-allowed? ()
  (not (or (< (-> *display* base-frame-counter) (-> *game-info* blackout-time))
           (!= (-> *setting-control* current bg-a) 0.0)
           (!= (-> *setting-control* current bg-a-force) 0.0)
           (not (-> *setting-control* current allow-pause))
           (handle->process (-> *game-info* auto-save-proc))
           (not *target*))))

(defun init-game-options ((this progress))
  "Set the options for all of the menus."
  ;; start off by making them all invalid
  (dotimes (i (progress-screen max))
    (set! (-> *options-remap* i) #f))
  ;; main menu
  (set! (-> *options-remap* (progress-screen settings))
        (case *kernel-boot-message*
          (('demo)
           ;; game demo
           *main-options-demo*)
          (('demo-shared)
           ;; game demo with external launcher
           *main-options-demo-shared*)
          (else
           ;; normal game
           *main-options*)))
  (set! (-> *options-remap* (progress-screen game-settings))
        (cond
          ((!= *kernel-boot-message* 'play) (if (= (scf-get-territory) GAME_TERRITORY_SCEE) *game-options* *game-options-demo*))
          ((and (= (scf-get-territory) GAME_TERRITORY_SCEI)
                (not (and (= *progress-cheat* 'language) (cpad-hold? 0 l2) (cpad-hold? 0 r2))))
           ;; if ntsc-j and we're not using language cheat (and holding l2+r2)
           *game-options-japan*)
          (else *game-options*)))
  (set! (-> *options-remap* (progress-screen graphic-settings))
        (if (and (= (-> *progress-state* starting-state) (progress-screen title))
                 (or (= (scf-get-territory) GAME_TERRITORY_SCEE) (and (= *progress-cheat* 'pal) (cpad-hold? 0 l2) (cpad-hold? 0 r2))))
          ;; (only if we came from title) if PAL or we're using the PAL cheat (and holding l2+r2)
          *graphic-title-options-pal*
          *graphic-options*))
  (set! (-> *options-remap* (progress-screen sound-settings)) *sound-options*)
  (set! (-> *options-remap* (progress-screen memcard-no-space)) *ok-options*)
  (set! (-> *options-remap* (progress-screen memcard-not-inserted)) *ok-options*)
  (set! (-> *options-remap* (progress-screen memcard-not-formatted)) *ok-options*)
  (set! (-> *options-remap* (progress-screen memcard-format)) *yes-no-options*)
  (set! (-> *options-remap* (progress-screen memcard-data-exists)) *yes-no-options*)
  (set! (-> *options-remap* (progress-screen memcard-insert)) *ok-options*)
  (set! (-> *options-remap* (progress-screen load-game)) *load-options*)
  (set! (-> *options-remap* (progress-screen save-game)) *save-options*)
  (set! (-> *options-remap* (progress-screen save-game-title)) *save-options-title*)
  (set! (-> *options-remap* (progress-screen memcard-error-loading)) *ok-options*)
  (set! (-> *options-remap* (progress-screen memcard-error-saving)) *ok-options*)
  (set! (-> *options-remap* (progress-screen memcard-error-formatting)) *ok-options*)
  (set! (-> *options-remap* (progress-screen memcard-error-creating)) *ok-options*)
  (set! (-> *options-remap* (progress-screen memcard-auto-save-error)) *ok-options*)
  (set! (-> *options-remap* (progress-screen memcard-removed)) *ok-options*)
  (set! (-> *options-remap* (progress-screen memcard-no-data)) *yes-no-options*)
  (set! (-> *options-remap* (progress-screen title)) *title*)
  (set! (-> *options-remap* (progress-screen settings-title)) *options*)
  (set! (-> *options-remap* (progress-screen auto-save)) *ok-options*)
  (set! (-> *options-remap* (progress-screen pal-change-to-60hz)) *yes-no-options*)
  (set! (-> *options-remap* (progress-screen pal-now-60hz)) *yes-no-options*)
  (set! (-> *options-remap* (progress-screen no-disc)) *ok-options*)
  (set! (-> *options-remap* (progress-screen bad-disc)) *ok-options*)
  (set! (-> *options-remap* (progress-screen quit)) *yes-no-options*)
  (set! (-> *progress-state* aspect-ratio-choice) (get-aspect-ratio))
  (set! (-> *progress-state* video-mode-choice) (get-video-mode))
  (set! (-> *progress-state* yes-no-choice) #f)
  (set! (-> *game-options* 0 value-to-modify) (&-> *setting-control* default vibration))
  (set! (-> *game-options* 1 value-to-modify) (&-> *setting-control* default play-hints))
  (set! (-> *game-options* 2 value-to-modify) (&-> *setting-control* default language))
  (set! (-> *game-options-japan* 0 value-to-modify) (&-> *setting-control* default vibration))
  (set! (-> *game-options-japan* 1 value-to-modify) (&-> *setting-control* default play-hints))
  (set! (-> *game-options-demo* 0 value-to-modify) (&-> *setting-control* default vibration))
  (set! (-> *game-options-demo* 1 value-to-modify) (&-> *setting-control* default play-hints))
  (set! (-> *graphic-options* 1 value-to-modify) (&-> *progress-state* aspect-ratio-choice))
  (set! (-> *graphic-title-options-pal* 1 value-to-modify) (&-> *progress-state* video-mode-choice))
  (set! (-> *graphic-title-options-pal* 2 value-to-modify) (&-> *progress-state* aspect-ratio-choice))
  (set! (-> *sound-options* 0 value-to-modify) (&-> *setting-control* default sfx-volume))
  (set! (-> *sound-options* 1 value-to-modify) (&-> *setting-control* default music-volume))
  (set! (-> *sound-options* 2 value-to-modify) (&-> *setting-control* default dialog-volume))
  (set! (-> *yes-no-options* 0 value-to-modify) (&-> *progress-state* yes-no-choice))
  (none))

(defun make-current-level-available-to-progress ()
  "exactly what it says on the tin."
  (when (and *target* (-> *level* border?))
    (let* ((cur-lev (-> *target* current-level))
           (lev-idx (+ (-> cur-lev info index) -1)))
      (if (and (>= lev-idx 0)
               (< lev-idx (-> *level-task-data-remap* length))
               (zero? (-> *game-info* level-opened (-> *level-task-data-remap* lev-idx)))
               (or (= *kernel-boot-message* 'play) (= (-> cur-lev nickname) 'mis)))
        (set! (-> *game-info* level-opened (-> *level-task-data-remap* lev-idx)) (the-as uint 1)))))
  0
  (none))

(defun make-levels-with-tasks-available-to-progress ()
  "Open levels that have tasks to do!"
  ;; go through EVERY LEVEL'S TASKS
  (dotimes (i (length *level-task-data*))
    ;; level tasks
    (let ((tasks (-> *level-task-data* i)))
      ;; unless there's no tasks or the level is already open...
      (unless (or (= tasks #f) (= (-> *game-info* level-opened i) 1))
        (cond
          ((!= *kernel-boot-message* 'play)
           (if (= (-> tasks level-name-id) (text-id misty-level-name)) (set! (-> *game-info* level-opened i) (the-as uint 1))))
          (*cheat-mode* (set! (-> *game-info* level-opened i) (the-as uint 1)))
          (else
           (dotimes (ii (-> tasks nb-of-tasks))
             (if (and (zero? (-> *game-info* level-opened ii))
                      (!= ii (-> tasks buzzer-task-index))
                      (task-known? (-> tasks task-info ii task-id)))
               (set! (-> *game-info* level-opened ii) (the-as uint 1)))))))))
  0
  (none))

(defun get-next-task-up ((cur-task-idx int) (lev-idx int))
  "find next available task. skips over unknown tasks and doesn't do anything if none are found"
  (let ((gp-0 cur-task-idx))
    (let ((s4-0 (+ cur-task-idx 1))
          (s3-0 (-> *level-task-data* lev-idx)))
      (while (and (< s4-0 (-> s3-0 nb-of-tasks)) (= gp-0 cur-task-idx))
        (if (or *cheat-mode* (task-known? (-> s3-0 task-info s4-0 task-id))) (set! gp-0 s4-0))
        (+! s4-0 1)))
    gp-0))

(defun get-next-task-down ((cur-task-idx int) (lev-idx int))
  "find previous available task. skips over unknown tasks and doesn't do anything if none are found"
  (let ((gp-0 cur-task-idx))
    (let ((s4-0 (+ cur-task-idx -1))
          (s3-0 (-> *level-task-data* lev-idx)))
      (while (and (>= s4-0 0) (= gp-0 cur-task-idx))
        (if (or *cheat-mode* (task-known? (-> s3-0 task-info s4-0 task-id))) (set! gp-0 s4-0))
        (+! s4-0 -1)))
    gp-0))

(defun get-next-level-up ((lev-idx int))
  (let ((gp-0 lev-idx))
    (let ((s4-0 (+ lev-idx 1)))
      (while (and (< s4-0 (length *level-task-data*)) (= gp-0 lev-idx))
        (if (= (-> *game-info* level-opened s4-0) 1) (set! gp-0 s4-0))
        (+! s4-0 1)))
    gp-0))

(defun get-next-level-down ((lev-idx int))
  (let ((v0-0 lev-idx))
    (let ((v1-0 (+ lev-idx -1)))
      (while (and (>= v1-0 0) (= v0-0 lev-idx))
        (if (= (-> *game-info* level-opened v1-0) 1) (set! v0-0 v1-0))
        (+! v1-0 -1)))
    v0-0))

(defun calculate-completion ((the-progress progress))
  "Updates counters and calculates game completion.
   Cells are tallied 80%
   Buzzers are tallied 10%
   Orbs are tallied 10%"
  (let ((current-cells 0)
        (current-buzzers 0)
        (current-orbs 0)
        (total-cells 0)
        (total-buzzers 0)
        (total-orbs 0))
    (dotimes (s5-0 (length *level-task-data*))
      (let ((s4-0 (-> *level-task-data* s5-0)))
        (when (!= s4-0 #f)
          (when (or (= *kernel-boot-message* 'play) (= (-> s4-0 level-name-id) (text-id misty-level-name)))
            (dotimes (s3-0 (-> s4-0 nb-of-tasks))
              (if (= (get-task-status (-> s4-0 task-info s3-0 task-id)) (task-status invalid)) (1+! current-cells)))
            (set! total-cells (+ total-cells (-> s4-0 nb-of-tasks)))
            (set! current-orbs (+ current-orbs (-> *game-info* money-per-level s5-0)))
            (set! total-orbs (+ total-orbs (-> *game-counts* data s5-0 money-count)))
            (let ((v1-20 (-> s4-0 buzzer-task-index)))
              (when (!= v1-20 -1)
                (set! current-buzzers (+ current-buzzers (buzzer-count *game-info* (-> s4-0 task-info v1-20 task-id))))
                (set! total-buzzers (+ total-buzzers (-> *game-counts* data s5-0 buzzer-count)))))))))
    (when the-progress
      (set! (-> the-progress total-nb-of-power-cells) total-cells)
      (set! (-> the-progress total-nb-of-buzzers) total-buzzers)
      (set! (-> the-progress total-nb-of-orbs) total-orbs))
    (+ (/ (* 80.0 (the float current-cells)) (the float total-cells))
       (/ (* 10.0 (the float current-orbs)) (the float total-orbs))
       (/ (* 10.0 (the float current-buzzers)) (the float total-buzzers)))))

(define *progress-save-info* (new 'global 'mc-slot-info))

(defmacro progress-make-icon (this &key skel &key x &key y &key z &key scale-x &key scale-y)
  `(when (< (-> ,this nb-of-icons) 6)
    (let ((icon-idx (-> ,this nb-of-icons)))
      (set! (-> ,this icons icon-idx) (new 'static 'hud-icon))
      (let ((new-manipy (manipy-spawn (new 'static 'vector :w 1.0) #f ,skel #f :to ,this :stack *scratch-memory-top*)))
        (when new-manipy
          (set! (-> (-> new-manipy) draw dma-add-func) dma-add-process-drawable-hud)
          (set-vector! (-> (-> new-manipy) root trans) 0.0 0.0 0.0 1.0)
          (set-vector! (-> (-> new-manipy) root scale) ,scale-x ,scale-y ,scale-x 1.0)
          (when #f
            (send-event (ppointer->process new-manipy) 'trans-hook #f)))
        (set! (-> ,this icons icon-idx icon) new-manipy)
        (when new-manipy
          (logior! (-> new-manipy 0 mask) (process-mask pause))
          (logclear! (-> new-manipy 0 mask) (process-mask menu progress))
          (set! (-> (-> new-manipy) root trans z) ,z)
          (set! (-> ,this icons icon-idx icon-x) ,x)
          (set! (-> ,this icons icon-idx icon-y) ,y)
          (set! (-> ,this icons icon-idx icon-z) 0)
          (set! (-> ,this icons icon-idx scale-x) ,scale-x)
          (set! (-> ,this icons icon-idx scale-y) ,scale-y))))
    (+! (-> ,this nb-of-icons) 1)))

(defmethod initialize-icons ((this progress))
  (progress-make-icon this :skel *fuelcell-naked-sg* :x 256 :y 77 :z (meters 0.5) :scale-x 0.006 :scale-y 0.006)
  (progress-make-icon this :skel *fuelcell-naked-sg* :x 256 :y 77 :z (meters 0.5) :scale-x 0.006 :scale-y 0.006)
  (progress-make-icon this :skel *fuelcell-naked-sg* :x 256 :y 77 :z (meters 0.5) :scale-x 0.006 :scale-y 0.006)
  (progress-make-icon this :skel *fuelcell-naked-sg* :x 256 :y 77 :z (meters 0.5) :scale-x 0.006 :scale-y 0.006)
  (progress-make-icon this :skel *money-sg* :x -320 :y 253 :z (meters 17) :scale-x 0.013 :scale-y -0.015)
  (progress-make-icon this :skel *money-sg* :x -320 :y 253 :z (meters 0.25) :scale-x 0.008 :scale-y -0.009)
  (send-event (ppointer->process (-> this icons 1 icon)) 'set-frame-num 2.5)
  (send-event (ppointer->process (-> this icons 2 icon)) 'set-frame-num 10.0)
  (send-event (ppointer->process (-> this icons 3 icon)) 'set-frame-num 15.5)
  0
  (none))

(defmethod enter! ((this progress) (screen progress-screen) (option int))
  (when (!= (-> this display-state) screen)
    (set! (-> *progress-state* yes-no-choice) #f)
    (set! (-> this selected-option) #f)
    (set! (-> this option-index) option)
    (set! (-> this last-option-index-change) (-> *display* real-frame-counter))
    (set! (-> this display-state) screen)
    (set! (-> this next-display-state) screen)
    (set-transition-speed! this)
    (case (-> this display-state)
      (((progress-screen memcard-creating)) (auto-save-command 'create-file 0 0 this))
      (((progress-screen memcard-loading))
       (set! (-> *progress-state* last-slot-saved) (-> *progress-state* which))
       (sound-volume-off)
       (auto-save-command 'restore 0 (-> *progress-state* which) this))
      (((progress-screen memcard-saving))
       (set! (-> *progress-state* last-slot-saved) (-> *progress-state* which))
       (auto-save-command 'save 0 (-> *progress-state* which) this))
      (((progress-screen memcard-formatting)) (auto-save-command 'format-card 0 0 this))
      (((progress-screen save-game) (progress-screen load-game))
       (set! (-> this option-index) (max 0 (-> *progress-state* last-slot-saved))))
      (((progress-screen memcard-removed)) (set! (-> *progress-state* last-slot-saved) 0))))
  0
  (none))

(defmethod push! ((this progress))
  (let ((v1-0 (-> this display-state-pos)))
    (cond
      ((< v1-0 5)
       (set! (-> this display-state-stack v1-0) (-> this display-state))
       (set! (-> this option-index-stack v1-0) (-> this option-index))
       (set! (-> this display-state-pos) (+ v1-0 1)))
      (else (format #t "ERROR: Can't push any more states on the display-state-stack.~%"))))
  0
  (none))

(defmethod pop! ((this progress))
  (let ((v1-0 (-> this display-state-pos)))
    (cond
      ((> v1-0 0)
       (let ((a2-0 (+ v1-0 -1)))
         (set! (-> this display-state-pos) a2-0)
         (enter! this (-> this display-state-stack a2-0) (-> this option-index-stack a2-0))))
      (else (set-master-mode 'game))))
  0
  (none))

(defmethod set-transition-progress! ((this progress) (arg0 int))
  (set! (-> this transition-offset) arg0)
  (set! (-> this transition-offset-invert) (- 512 arg0))
  (set! (-> this transition-percentage) (* (1/ 512) (the float arg0)))
  (set! (-> this transition-percentage-invert) (- 1.0 (-> this transition-percentage)))
  0
  (none))

(defmethod set-transition-speed! ((this progress))
  (case (-> this display-state)
    (((progress-screen fuel-cell)
      (progress-screen money)
      (progress-screen buzzer)
      (progress-screen load-game)
      (progress-screen save-game)
      (progress-screen save-game-title))
     (set! (-> this transition-speed) 15.0))
    (else (set! (-> this transition-speed) 45.0)))
  0
  (none))

(defbehavior progress-init-by-other progress ()
  (logclear! (-> self mask) (process-mask menu progress))
  (set! (-> self nb-of-particles) 0)
  (set! (-> self max-nb-of-particles) 40)
  (set! (-> self nb-of-icons) 0)
  (set! (-> self in-out-position) 4096)
  (set! (-> self current-debug-string) 0)
  (set! (-> self current-debug-group) 0)
  (set! (-> self display-level-index) 0)
  (set! (-> self next-level-index) 0)
  (set! (-> self option-index) 0)
  (set! (-> self selected-option) #f)
  (set! (-> self card-info) #f)
  (set! (-> self last-option-index-change) (-> *display* real-frame-counter))
  (set! (-> self display-state-pos) 0)
  (set! (-> self in-transition) #f)
  (set! (-> self force-transition) #f)
  (set! (-> self stat-transition) #f)
  (set! (-> self level-transition) 0)
  (set! (-> self left-side-x-scale) 0.0)
  (set! (-> self left-side-y-scale) 0.0)
  (set! (-> self right-side-x-scale) 0.0)
  (set! (-> self right-side-y-scale) 0.0)
  (dotimes (v1-6 5)
    (set! (-> self display-state-stack v1-6) (progress-screen fuel-cell)))
  (init-game-options self)
  (initialize-icons self)
  (initialize-particles self)
  (set! (-> self particle-state 0) 0)
  (set! (-> self particle-state 1) 0)
  (set! (-> self particle-state 2) 0)
  (set! (-> self particle-state 3) 0)
  (set! (-> self particle-state 4) 0)
  (set! (-> self particle-state 5) 0)
  (set! (-> self particle-state 6) 1)
  (set! (-> self particle-state 7) 1)
  (set! (-> self particle-state 8) 1)
  (set! (-> self particle-state 9) 1)
  (set! (-> self particle-state 10) 1)
  (set! (-> self particle-state 11) 1)
  (set! (-> self particle-state 12) 1)
  (set! (-> self particle-state 13) 1)
  (set! (-> self particle-state 14) 0)
  (set! (-> self particle-state 15) 0)
  (set! (-> self particle-state 16) 3)
  (set! (-> self particle-state 17) 0)
  (set! (-> self particle-state 18) 0)
  (set! (-> self particle-state 19) 0)
  (set! (-> self particle-state 20) 0)
  (set! (-> self particle-state 21) 0)
  (set! (-> self particle-state 22) 0)
  (set! (-> self particle-state 23) 0)
  (set! (-> self particle-state 24) 0)
  (set! (-> self particle-state 25) 0)
  (set! (-> self particle-state 26) 0)
  (set! (-> self particle-state 27) 0)
  (set! (-> self particle-state 28) 0)
  (set! (-> self particle-state 29) 0)
  (set! (-> self particle-state 30) 0)
  (set! (-> self particle-state 31) 0)
  (let ((gp-0 (new 'stack-no-clear 'quaternion)))
    (quaternion-axis-angle! gp-0 0.0 1.0 0.0 16384.0)
    (quaternion*! (-> self icons 0 icon 0 root quat) gp-0 (-> self icons 0 icon 0 root quat))
    (quaternion-axis-angle! gp-0 0.0 1.0 0.0 32768.0)
    (quaternion*! (-> self icons 1 icon 0 root quat) gp-0 (-> self icons 1 icon 0 root quat))
    (quaternion-axis-angle! gp-0 0.0 1.0 0.0 49152.0)
    (quaternion*! (-> self icons 2 icon 0 root quat) gp-0 (-> self icons 2 icon 0 root quat))
    (quaternion-axis-angle! gp-0 0.0 1.0 0.0 0.0)
    (quaternion*! (-> self icons 3 icon 0 root quat) gp-0 (-> self icons 3 icon 0 root quat)))
  (adjust-ratios self (get-aspect-ratio) (get-video-mode))
  (adjust-icons self)
  (set! (-> self event-hook) (-> progress-waiting event))
  (go progress-waiting)
  (none))

(define *progress-stack* (the-as (pointer uint8) (malloc 'global #x3800)))

(defconstant *progress-stack-top* (&-> *progress-stack* #x3800))

(defun activate-progress ((creator process) (screen progress-screen))
  (when *target*
    (cond
      ((not *progress-process*)
       (when (progress-allowed?)
         (hide-hud)
         (make-levels-with-tasks-available-to-progress)
         (disable-level-text-file-loading)
         (set! (-> *progress-state* starting-state) screen)
         (set! *progress-process* (process-spawn progress :to creator :stack *progress-stack-top*))
         (let ((s5-1 *progress-process*))
           (set! (-> s5-1 0 completion-percentage) (calculate-completion (-> s5-1 0)))
           (set! *master-mode* 'progress)
           (let ((s4-1 (-> *target* current-level)))
             (cond
               ((!= *kernel-boot-message* 'play) (set! (-> s5-1 0 display-level-index) 4))
               ((or (= s4-1 #f) (< (length *level-task-data-remap*) (-> s4-1 info index))) (set! (-> s5-1 0 display-level-index) 0) 0)
               (else (set! (-> s5-1 0 display-level-index) (-> *level-task-data-remap* (+ (-> s4-1 info index) -1))))))
           (set! (-> s5-1 0 next-level-index) (-> s5-1 0 display-level-index))
           (set! (-> s5-1 0 display-state) (progress-screen invalid))
           (set-transition-progress! (-> s5-1 0) 512)
           (set! (-> s5-1 0 task-index) (get-next-task-up -1 (-> s5-1 0 display-level-index)))))
       (when *progress-process*
         (enter! (-> *progress-process* 0) screen 0)
         (set! (-> *progress-process* 0 card-info) #f)))
      (else
       (push! (-> *progress-process* 0))
       (set! (-> *progress-process* 0 next-display-state) screen)
       (set! (-> *progress-process* 0 card-info) #f))))
  0
  (none))

(defun deactivate-progress ()
  (when (and *progress-process* (= (-> *progress-process* 0 next-state name) 'progress-gone))
    (apply-settings *setting-control*)
    (dotimes (gp-0 (-> *progress-process* 0 nb-of-particles))
      (kill-and-free-particles (-> *progress-process* 0 particles gp-0 part))
      (set! (-> *progress-process* 0 particles gp-0 part matrix) -1))
    (set! (-> *progress-process* 0 nb-of-particles) 0)
    (deactivate (-> *progress-process* 0))
    (set! *progress-process* (the-as (pointer progress) #f))
    (enable-level-text-file-loading))
  0
  (none))

(defun hide-progress-screen ()
  "shoo!"
  (if *progress-process* (send-event (ppointer->process *progress-process*) 'go-away))
  0
  (none))

(defun hide-progress-icons ()
  (let ((v1-0 6)) (dotimes (a0-0 8) (set! (-> *progress-process* 0 particles v1-0 init-pos x) -320.0) (+! v1-0 1)))
  (set! (-> *progress-process* 0 particles 5 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 14 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 15 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 19 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 20 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 21 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 22 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 23 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 24 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 25 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 26 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 27 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 28 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 29 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 30 init-pos x) -320.0)
  (set! (-> *progress-process* 0 particles 31 init-pos x) -320.0)
  (set! (-> *progress-process* 0 icons 4 icon-x) -320)
  0
  (none))

(defmethod relocate ((this game-count-info) (arg0 int))
  "Load in the game-count-info. This is a bit of a hack."
  (set! *game-counts* this)
  this)

(defmethod relocate ((this progress) (arg0 int))
  (dotimes (v1-0 (-> this nb-of-particles))
    (when (-> this particles v1-0 part)
      (if (nonzero? (-> this particles v1-0 part))
        (set! (-> this particles v1-0 part)
              (the-as sparticle-launch-control (&+ (the-as pointer (-> this particles v1-0 part)) arg0))))))
  (the-as progress ((method-of-type process relocate) this arg0)))

(defmethod adjust-sprites ((this progress))
  (let ((f0-1 (* (1/ METER_LENGTH) (the float (-> this in-out-position)))))
    ;; right position
    (set! (-> this particles 2 init-pos x) (the float (+ (-> this right-x-offset) 409 (the int (* 301.5 f0-1)))))
    ;; left position
    (set! (-> this particles 1 init-pos x) (the float (+ (-> this left-x-offset) 59)))
    (set! (-> this left-side-x-scale)
          (meters (+ (/ (if (= (-> *pc-settings* aspect-custom-x) 16) 5.0 3.5) (-> this sides-x-scale)) (* 10.0 f0-1))))
    (set! (-> this left-side-y-scale) (meters (+ (-> this sides-y-scale) (* 10.0 f0-1))))
    (set! (-> this right-side-x-scale)
          (meters (+ (/ (if (= (-> *pc-settings* aspect-custom-x) 16) 8.5 6.0) (-> this sides-x-scale)) (* 4.0 f0-1))))
    (set! (-> this right-side-y-scale) (meters (+ (-> this sides-y-scale) (* 4.0 f0-1)))))
  (dotimes (s5-0 (-> this nb-of-particles))
    (set! (-> this particles s5-0 pos x) (+ -256.0 (-> this particles s5-0 init-pos x)))
    (set! (-> this particles s5-0 pos y)
          (* 0.5
             (- (* (-> this particles s5-0 init-pos y) (-> *video-parms* relative-y-scale)) (the float (-> *video-parms* screen-sy)))))
    (set! (-> this particles s5-0 pos z) (-> this particles s5-0 init-pos z))
    (if (> (-> this particles s5-0 part matrix) 0)
      (set-vector! (sprite-get-user-hvdf (-> this particles s5-0 part matrix))
                   (the float (+ (the int (-> this particles s5-0 pos x)) 2048))
                   (the float (+ (the int (-> this particles s5-0 pos y)) 2048))
                   (- (-> *math-camera* hvdf-off z) (* 1024.0 (-> this particles s5-0 pos z)))
                   (-> *math-camera* hvdf-off w)))
    (spawn (-> this particles s5-0 part) *null-vector*))
  0
  (none))

(defmethod adjust-icons ((this progress))
  (dotimes (v1-0 (-> this nb-of-icons))
    (when (>= v1-0 4)
      (set-vector! (-> this icons v1-0 icon 0 root scale)
                   (* (-> this icons v1-0 scale-x) (-> *video-parms* relative-x-scale))
                   (* (-> this icons v1-0 scale-y) (-> *video-parms* relative-y-scale))
                   (* (-> this icons v1-0 scale-x) (-> *video-parms* relative-x-scale))
                   1.0)
      (set! (-> this icons v1-0 icon 0 root trans x) (the float (+ (-> this icons v1-0 icon-x) -256)))
      (set! (-> this icons v1-0 icon 0 root trans y)
            (* (-> *video-parms* relative-y-scale)
               (- (* (-> *video-parms* relative-y-scale) (the float (-> this icons v1-0 icon-y)))
                  (the float (-> *video-parms* screen-sy)))))))
  0
  (none))

(defmethod adjust-ratios ((this progress) (aspect symbol) (video-mode symbol))
  (case aspect
    (('aspect4x3)
     (set! (-> this sides-x-scale) 1.0)
     (set! (-> this sides-y-scale) 13.0)
     (set! (-> this left-x-offset) 0)
     (set! (-> this right-x-offset) 0)
     (set! (-> this button-scale) 1.0)
     (set! (-> this slot-scale) 8192.0)
     (set! (-> this small-orb-y-offset) 58)
     (set! (-> this icons 5 scale-x) 0.008)
     (set! (-> this icons 5 scale-y) -0.009)
     (set! (-> this big-orb-y-offset) 243)
     (set! (-> this icons 4 scale-x) 0.013)
     (set! (-> this icons 4 scale-y) -0.015))
    (('aspect16x9)
     (set! (-> this sides-x-scale) 1.2)
     (set! (-> this sides-y-scale) 9.8)
     (set! (-> this left-x-offset) -10)
     (set! (-> this right-x-offset) 17)
     (set! (-> this button-scale) 1.05)
     (set! (-> this slot-scale) 6144.0)
     (set! (-> this small-orb-y-offset) 59)
     (set! (-> this icons 5 scale-x) 0.008)
     (set! (-> this icons 5 scale-y) -0.0098)
     (set! (-> this big-orb-y-offset) 255)
     (set! (-> this icons 4 scale-x) 0.017)
     (set! (-> this icons 4 scale-y) -0.0205)))
  (when (= video-mode 'pal)
    (set! (-> this icons 5 scale-y) (* 1.15 (-> this icons 5 scale-y)))
    (set! (-> this icons 4 scale-x) (* 1.05 (-> this icons 4 scale-x)))
    (set! (-> this icons 4 scale-y) (* (-> this icons 4 scale-y) (the-as float (if (= aspect 'aspect16x9) 1.18 1.15))))
    (+! (-> this big-orb-y-offset) (if (= aspect 'aspect16x9) 3 2)))
  0
  (none))

(defmethod can-go-back? ((this progress))
  (let ((v1-2 (-> *progress-process* 0 display-state))
        (a1-1 (-> *progress-state* starting-state)))
    (and (= (-> this next-state name) 'progress-normal)
         (not (-> this in-transition))
         (not (-> this selected-option))
         (or (= v1-2 (progress-screen fuel-cell))
             (= v1-2 (progress-screen money))
             (= v1-2 (progress-screen buzzer))
             (and (or (= a1-1 (progress-screen fuel-cell))
                      (= a1-1 (progress-screen money))
                      (= a1-1 (progress-screen buzzer))
                      (= a1-1 (progress-screen title))
                      (= a1-1 (progress-screen speedrun-options)))
                  (#if (not PC_PORT)
                    (or (= v1-2 (progress-screen settings))
                        (= v1-2 (progress-screen game-settings))
                        (= v1-2 (progress-screen graphic-settings))
                        (= v1-2 (progress-screen sound-settings))
                        (= v1-2 (progress-screen title))
                        (= v1-2 (progress-screen settings-title)))
                    (or (= v1-2 (progress-screen settings))
                        (= v1-2 (progress-screen game-settings))
                        (= v1-2 (progress-screen graphic-settings))
                        (= v1-2 (progress-screen sound-settings))
                        (= v1-2 (progress-screen title))
                        (= v1-2 (progress-screen settings-title))
                        (= v1-2 (progress-screen camera-options))
                        (= v1-2 (progress-screen accessibility-options))
                        (= v1-2 (progress-screen misc-options))
                        (= v1-2 (progress-screen game-ps2-options))
                        (= v1-2 (progress-screen gfx-ps2-options))
                        (= v1-2 (progress-screen resolution))
                        (= v1-2 (progress-screen aspect-ratio))
                        (= v1-2 (progress-screen secrets))
                        (= v1-2 (progress-screen cheats))
                        (= v1-2 (progress-screen checkpoint-select))
                        (= v1-2 (progress-screen music-player))
                        (= v1-2 (progress-screen flava-player))
                        (= v1-2 (progress-screen monitor))
                        (= v1-2 (progress-screen speedrun-options))
                        (= v1-2 (progress-screen speedrun-il-options))
                        (= v1-2 (progress-screen speedrun-cat-ext-options))
                        (= v1-2 (progress-screen input-options))
                        (= v1-2 (progress-screen select-controller))
                        (= v1-2 (progress-screen controller-binds))
                        (= v1-2 (progress-screen keyboard-binds))
                        (= v1-2 (progress-screen mouse-binds))
                        (= v1-2 (progress-screen controller-options))
                        (= v1-2 (progress-screen mouse-options))
                        (= v1-2 (progress-screen reassign-binds-options)))))))))

(defmethod visible? ((this progress))
  (the-as symbol (and *progress-process* (zero? (-> *progress-process* 0 in-out-position)))))

(defmethod hidden? ((this progress))
  (or (not *progress-process*) (= (-> *progress-process* 0 in-out-position) 4096)))

(defstate progress-waiting (progress)
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (case message
        (('go-away) (go progress-gone))))
  :code
    (behavior ()
      (loop
        (when (hud-hidden?)
          (dotimes (gp-0 (-> self nb-of-particles))
            (if (= (-> self particles gp-0 part matrix) -1) (set! (-> self particles gp-0 part matrix) (sprite-allocate-user-hvdf))))
          (set-setting! 'common-page 'set 0.0 1)
          (suspend)
          (go progress-coming-in))
        (suspend))))

(defstate progress-gone (progress)
  :code
    (behavior ()
      (remove-setting! 'process-mask)
      (apply-settings *setting-control*)
      (logior! (-> self mask) (process-mask sleep))
      (suspend)
      0))

(defmethod set-memcard-screen ((this progress) (arg0 progress-screen))
  (let ((s4-0 (-> this card-info))
        (gp-0 arg0))
    (when s4-0
      (case arg0
        (((progress-screen memcard-no-space) (progress-screen memcard-not-inserted) (progress-screen memcard-not-formatted))
         (cond
           ((zero? (-> s4-0 handle)) (set! gp-0 (progress-screen memcard-not-inserted)))
           ((zero? (-> s4-0 formatted))
            (cond
              ((or (zero? (-> this display-state-pos))
                   (and (!= (-> *progress-state* starting-state) 27) (nonzero? (-> *progress-state* starting-state))))
               (set-master-mode 'game))
              (else (if (!= arg0 (progress-screen memcard-not-formatted)) (set! gp-0 (progress-screen memcard-format))))))
           ((and (zero? (-> s4-0 inited)) (< (-> s4-0 mem-actual) (-> s4-0 mem-required)))
            (set! gp-0 (progress-screen memcard-no-space)))
           ((or (zero? (-> this display-state-pos))
                (and (!= (-> *progress-state* starting-state) 27) (nonzero? (-> *progress-state* starting-state))))
            (set-master-mode 'game))
           (else (set! gp-0 (progress-screen save-game)))))
        (((progress-screen memcard-insert)) (if (= (-> s4-0 inited) 1) (set! gp-0 (progress-screen load-game)))))
      (cond
        ((zero? (-> s4-0 handle))
         (cond
           ((-> *setting-control* current auto-save) (set! gp-0 (progress-screen memcard-removed)))
           (else
            (cond
              ((= arg0 (progress-screen load-game)) (set! gp-0 (progress-screen memcard-insert)))
              ((or (= arg0 (progress-screen memcard-format))
                   (= arg0 (progress-screen memcard-no-space))
                   (= arg0 (progress-screen memcard-not-formatted))
                   (= arg0 (progress-screen save-game))
                   (= arg0 (progress-screen save-game-title))
                   (= arg0 (progress-screen memcard-no-data))
                   (= arg0 (progress-screen memcard-data-exists)))
               (set! gp-0 (progress-screen memcard-not-inserted)))))))
        ((zero? (-> s4-0 formatted))
         (case arg0
           (((progress-screen load-game)) (set! gp-0 (progress-screen memcard-insert)))
           (((progress-screen save-game) (progress-screen save-game-title)) (set! gp-0 (progress-screen memcard-format)))))
        ((zero? (-> s4-0 inited))
         (case arg0
           (((progress-screen save-game) (progress-screen save-game-title))
            (if (>= (-> s4-0 mem-actual) (-> s4-0 mem-required))
              (set! gp-0 (progress-screen memcard-no-data))
              (set! gp-0 (progress-screen memcard-no-space))))
           (((progress-screen load-game)) (set! gp-0 (progress-screen memcard-insert)))))))
    gp-0))

(defmethod respond-memcard ((this progress))
  (let ((s5-0 (-> this card-info)))
    (when (and s5-0 (not (-> this in-transition)))
      (when (or (cpad-pressed? 0 x) (cpad-pressed? 0 circle))
        (cpad-clear! 0 x)
        (cpad-clear! 0 circle)
        (case (-> this display-state)
          (((progress-screen load-game))
           (cond
             ((< (-> this option-index) 4)
              (when (nonzero? (-> s5-0 file (-> this option-index) present))
                (sound-play "start-options")
                (set! (-> *progress-state* which) (-> this option-index))
                (set! (-> this next-display-state) (progress-screen memcard-loading))))
             (else (sound-play "cursor-options") (set! (-> this next-display-state) (progress-screen invalid)))))
          (((progress-screen save-game) (progress-screen save-game-title))
           (cond
             ((< (-> this option-index) 4)
              (sound-play "start-options")
              (set! (-> *progress-state* which) (-> this option-index))
              (if (zero? (-> s5-0 file (-> this option-index) present))
                (set! (-> this next-display-state) (progress-screen memcard-saving))
                (set! (-> this next-display-state) (progress-screen memcard-data-exists))))
             ((and (= (-> this display-state) (progress-screen save-game-title)) (= (-> this option-index) 4))
              (sound-play "starts-options")
              (sound-volume-off)
              (set! (-> *game-info* mode) 'play)
              (initialize! *game-info* 'game (the-as game-save #f) "intro-start")
              (set-master-mode 'game))
             (else (sound-play "cursor-options") (set! (-> this next-display-state) (progress-screen invalid)))))
          (((progress-screen memcard-insert))
           (sound-play "cursor-options")
           (set! (-> this next-display-state) (progress-screen invalid)))
          (((progress-screen memcard-data-exists))
           (cond
             ((-> *progress-state* yes-no-choice)
              (sound-play "start-options")
              (set! (-> this next-display-state) (progress-screen memcard-saving)))
             (else
              (sound-play "cursor-options")
              (if (= (-> this display-state-stack 0) (progress-screen title))
                (set! (-> this next-display-state) (progress-screen save-game-title))
                (set! (-> this next-display-state) (progress-screen save-game))))))
          (((progress-screen memcard-no-data))
           (cond
             ((-> *progress-state* yes-no-choice)
              (sound-play "start-options")
              (set! (-> this next-display-state) (progress-screen memcard-creating)))
             ;; og:preserve-this PAL & NTSC-J patch here
             ((= (-> this display-state-stack 0) (progress-screen title))
              (sound-play "cursor-options")
              (sound-volume-off)
              (set! (-> *game-info* mode) 'play)
              (initialize! *game-info* 'game (the-as game-save #f) "intro-start")
              (set-master-mode 'game))
             (else (sound-play "cursor-options") (set! (-> this next-display-state) (progress-screen invalid)))))
          (((progress-screen memcard-no-space) (progress-screen memcard-not-inserted) (progress-screen memcard-not-formatted))
           (cond
             ((= (-> this display-state-stack 0) (progress-screen title))
              (sound-play "start-options")
              (sound-volume-off)
              (set! (-> *game-info* mode) 'play)
              (initialize! *game-info* 'game (the-as game-save #f) "intro-start")
              (set-master-mode 'game))
             ((nonzero? (-> this display-state-stack 0)) (sound-play "start-options") (set-master-mode 'game))
             (else (sound-play "cursor-options") (set! (-> this next-display-state) (progress-screen invalid)))))
          (((progress-screen memcard-error-loading)
            (progress-screen memcard-error-saving)
            (progress-screen memcard-error-formatting)
            (progress-screen memcard-error-creating)
            (progress-screen memcard-auto-save-error)
            (progress-screen memcard-removed)
            (progress-screen auto-save))
           (sound-play "cursor-options")
           (set! (-> this next-display-state) (progress-screen invalid)))
          (((progress-screen pal-change-to-60hz))
           (cond
             ((-> *progress-state* yes-no-choice)
              (sound-play "start-options")
              (set! (-> *setting-control* default video-mode) (-> *progress-state* video-mode-choice))
              (set! (-> this video-mode-timeout) (-> *display* real-frame-counter))
              (set! (-> this next-display-state) (progress-screen pal-now-60hz)))
             (else
              (sound-play "cursor-options")
              (set! (-> *progress-state* video-mode-choice) 'pal)
              (set! (-> this next-display-state) (progress-screen invalid)))))
          (((progress-screen pal-now-60hz))
           (cond
             ((not (-> *progress-state* yes-no-choice))
              (set! (-> *progress-state* video-mode-choice) 'pal)
              (set! (-> *setting-control* default video-mode) (-> *progress-state* video-mode-choice))
              (sound-play "cursor-options"))
             (else (sound-play "start-options")))
           (set! (-> this next-display-state) (progress-screen invalid)))
          (((progress-screen no-disc) (progress-screen bad-disc))
           (when (is-cd-in?)
             (sound-play "cursor-options")
             (set! (-> this next-display-state) (progress-screen invalid))))
          (((progress-screen quit))
           (cond
             ((-> *progress-state* yes-no-choice)
              (sound-play "start-options")
              (sound-volume-off)
              (set! (-> *game-info* mode) 'play)
              (initialize! *game-info* 'game (the-as game-save #f) "title-start"))
             (else (sound-play "cursor-options") (set! (-> this next-display-state) (progress-screen invalid)))))
          (((progress-screen memcard-format))
           (cond
             ((-> *progress-state* yes-no-choice)
              (sound-play "start-options")
              (set! (-> this next-display-state) (progress-screen memcard-formatting)))
             ;; og:preserve-this NTSC-J patch here
             ((= (-> this display-state-stack 0) (progress-screen title))
              (sound-play "start-options")
              (sound-volume-off)
              (set! (-> *game-info* mode) 'play)
              (initialize! *game-info* 'game (the-as game-save #f) "intro-start")
              (set-master-mode 'game))
             (else (sound-play "cursor-options") (set! (-> this next-display-state) (progress-screen invalid)))))))))
  0
  (none))

(defmethod respond-common ((this progress))
  (mc-get-slot-info 0 *progress-save-info*)
  (set! (-> this card-info) *progress-save-info*)
  (let ((s5-0 (-> *options-remap* (-> this display-state))))
    (when (and s5-0 (not (-> this in-transition)))
      (cond
        ((cpad-hold? 0 up)
         (cond
           ((cpad-pressed? 0 up)
            (when (not (-> this selected-option))
              (if (!= (length s5-0) 1) (sound-play "cursor-up-down"))
              (set! (-> this last-option-index-change) (-> *display* real-frame-counter))
              (if (> (-> this option-index) 0) (+! (-> this option-index) -1) (set! (-> this option-index) (+ (length s5-0) -1)))))
           (else
            (when (-> this selected-option)
              (let ((v1-34 #f))
                (case (-> s5-0 (-> this option-index) option-type)
                  (((game-option-type center-screen))
                   (when (< -48 (-> *setting-control* current screeny))
                     (set! v1-34 #t)
                     (+! (-> *setting-control* default screeny) -1))))
                (when v1-34
                  (when (< (seconds 0.3) (- (-> *display* real-frame-counter) (-> *progress-state* last-slider-sound)))
                    (set! (-> *progress-state* last-slider-sound) (-> *display* real-frame-counter))
                    (sound-play "slider2001"))))))))
        ((cpad-hold? 0 down)
         (cond
           ((cpad-pressed? 0 down)
            (when (not (-> this selected-option))
              (if (!= (length s5-0) 1) (sound-play "cursor-up-down"))
              (set! (-> this last-option-index-change) (-> *display* real-frame-counter))
              (cond
                ((< (-> this option-index) (+ (length s5-0) -1)) (+! (-> this option-index) 1))
                (else (set! (-> this option-index) 0) 0))))
           (else
            (when (-> this selected-option)
              (let ((v1-69 #f))
                (case (-> s5-0 (-> this option-index) option-type)
                  (((game-option-type center-screen))
                   (when (< (-> *setting-control* current screeny) 48)
                     (set! v1-69 #t)
                     (+! (-> *setting-control* default screeny) 1))))
                (when v1-69
                  (when (< (seconds 0.3) (- (-> *display* real-frame-counter) (-> *progress-state* last-slider-sound)))
                    (set! (-> *progress-state* last-slider-sound) (-> *display* real-frame-counter))
                    (sound-play "slider2001"))))))))
        ((cpad-hold? 0 left)
         (cond
           ((cpad-pressed? 0 left)
            (when (or (-> this selected-option) (= (-> s5-0 (-> this option-index) option-type) (game-option-type yes-no)))
              (let ((s4-5 #f))
                (case (-> s5-0 (-> this option-index) option-type)
                  (((game-option-type on-off) (game-option-type yes-no))
                   (when (not (-> (the-as (pointer uint32) (-> s5-0 (-> this option-index) value-to-modify))))
                     (set! s4-5 #t)
                     (if (= (-> s5-0 (-> this option-index) value-to-modify) (&-> *setting-control* current vibration))
                       (cpad-set-buzz! (-> *cpad-list* cpads 0) 1 255 (seconds 0.3))))
                   (set! (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify))) #t))
                  (((game-option-type aspect-ratio))
                   (set! s4-5 (= (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0) 'aspect16x9))
                   (set! (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0) 'aspect4x3))
                  (((game-option-type video-mode))
                   (set! s4-5 (= (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0) 'ntsc))
                   (set! (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0) 'pal))
                  (((game-option-type language))
                   (if (> (the-as int (-> (the-as (pointer uint64) (-> s5-0 (-> this option-index) value-to-modify)))) 0)
                     (+! (-> (the-as (pointer uint64) (-> s5-0 (-> this option-index) value-to-modify))) -1)
                     (set! (-> (the-as (pointer int64) (-> s5-0 (-> this option-index) value-to-modify)))
                           (if (and (= (scf-get-territory) GAME_TERRITORY_SCEA)
                                    (not (and (= *progress-cheat* 'language) (cpad-hold? 0 l2) (cpad-hold? 0 r2))))
                             4
                             5)))
                   (set! (-> this language-transition) #t)
                   (set! (-> this language-direction) #t)
                   (set! s4-5 #t)))
                (if s4-5 (sound-play "cursor-l-r")))))
           (else
            (when (-> this selected-option)
              (let ((v1-157 #f))
                (case (-> s5-0 (-> this option-index) option-type)
                  (((game-option-type slider))
                   (cond
                     ((>= (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify)))
                          (+ 1.0 (-> s5-0 (-> this option-index) param1)))
                      (+! (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify))) -1.0)
                      (set! v1-157 #t))
                     ((< (-> s5-0 (-> this option-index) param1)
                         (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify))))
                      (set! (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify)))
                            (-> s5-0 (-> this option-index) param1))
                      (set! v1-157 #t))))
                  (((game-option-type center-screen))
                   (when (< -96 (-> *setting-control* default screenx))
                     (set! v1-157 #t)
                     (+! (-> *setting-control* default screenx) -1))))
                (when v1-157
                  (let ((f30-0 100.0))
                    (case (-> s5-0 (-> this option-index) name)
                      (((text-id music-volume) (text-id speech-volume))
                       (set! f30-0 (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify))))))
                    (when (< (seconds 0.3) (- (-> *display* real-frame-counter) (-> *progress-state* last-slider-sound)))
                      (set! (-> *progress-state* last-slider-sound) (-> *display* real-frame-counter))
                      (sound-play "slider2001" :vol f30-0)))))))))
        ((cpad-hold? 0 right)
         (cond
           ((cpad-pressed? 0 right)
            (when (or (-> this selected-option) (= (-> s5-0 (-> this option-index) option-type) (game-option-type yes-no)))
              (let ((v1-217 (the-as object #f)))
                (case (-> s5-0 (-> this option-index) option-type)
                  (((game-option-type on-off) (game-option-type yes-no))
                   (set! v1-217 (-> (the-as (pointer uint32) (-> s5-0 (-> this option-index) value-to-modify))))
                   (set! (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0) #f))
                  (((game-option-type aspect-ratio))
                   (set! v1-217 (= (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0) 'aspect4x3))
                   (set! (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0) 'aspect16x9))
                  (((game-option-type video-mode))
                   (set! v1-217 (= (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0) 'pal))
                   (set! (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0) 'ntsc))
                  (((game-option-type language))
                   (let ((v1-243 (if (and (= (scf-get-territory) GAME_TERRITORY_SCEA)
                                          (not (and (= *progress-cheat* 'language) (cpad-hold? 0 l2) (cpad-hold? 0 r2))))
                                   4
                                   5)))
                     (cond
                       ((< (the-as int (-> (the-as (pointer uint64) (-> s5-0 (-> this option-index) value-to-modify)))) v1-243)
                        (+! (-> (the-as (pointer uint64) (-> s5-0 (-> this option-index) value-to-modify))) 1))
                       (else (set! (-> (the-as (pointer int64) (-> s5-0 (-> this option-index) value-to-modify))) 0) 0)))
                   (set! (-> this language-transition) #t)
                   (set! (-> this language-direction) #f)
                   (set! v1-217 #t)))
                (if v1-217 (sound-play "cursor-l-r")))))
           (else
            (when (-> this selected-option)
              (let ((v1-263 #f))
                (case (-> s5-0 (-> this option-index) option-type)
                  (((game-option-type slider))
                   (cond
                     ((>= (+ -1.0 (-> s5-0 (-> this option-index) param2))
                          (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify))))
                      (+! (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify))) 1.0)
                      (set! v1-263 #t))
                     ((< (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify)))
                         (-> s5-0 (-> this option-index) param2))
                      (set! (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify)))
                            (-> s5-0 (-> this option-index) param2))
                      (set! v1-263 #t))))
                  (((game-option-type center-screen))
                   (when (< (-> *setting-control* default screenx) 96)
                     (set! v1-263 #t)
                     (+! (-> *setting-control* default screenx) 1))))
                (when v1-263
                  (let ((f30-1 100.0))
                    (case (-> s5-0 (-> this option-index) name)
                      (((text-id music-volume) (text-id speech-volume))
                       (set! f30-1 (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify))))))
                    (when (< (seconds 0.3) (- (-> *display* real-frame-counter) (-> *progress-state* last-slider-sound)))
                      (set! (-> *progress-state* last-slider-sound) (-> *display* real-frame-counter))
                      (sound-play "slider2001" :vol f30-1)))))))))
        ((or (cpad-pressed? 0 square) (cpad-pressed? 0 triangle))
         (cond
           ((-> this selected-option)
            (case (-> s5-0 (-> this option-index) option-type)
              (((game-option-type slider))
               (set! (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify)))
                     (-> *progress-state* slider-backup)))
              (((game-option-type language))
               (set! (-> (the-as (pointer language-enum) (-> s5-0 (-> this option-index) value-to-modify)))
                     (-> *progress-state* language-backup)))
              (((game-option-type on-off))
               (set! (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0)
                     (-> *progress-state* on-off-backup)))
              (((game-option-type center-screen))
               (set! (-> *setting-control* default screenx) (-> *progress-state* center-x-backup))
               (set! (-> *setting-control* default screeny) (-> *progress-state* center-y-backup)))
              (((game-option-type aspect-ratio) (game-option-type video-mode))
               (set! (-> (the-as (pointer symbol) (-> s5-0 (-> this option-index) value-to-modify)) 0)
                     (-> *progress-state* aspect-ratio-backup))))
            (sound-play "cursor-options")
            (set! (-> this selected-option) #f))
           ((or (can-go-back? this)
                (= (-> this display-state) (progress-screen load-game))
                (= (-> this display-state) (progress-screen save-game))
                (= (-> this display-state) (progress-screen save-game-title)))
            (logclear! (-> *cpad-list* cpads 0 button0-abs 0) (pad-buttons square))
            (logclear! (-> *cpad-list* cpads 0 button0-rel 0) (pad-buttons square))
            (logclear! (-> *cpad-list* cpads 0 button0-abs 0) (pad-buttons triangle))
            (logclear! (-> *cpad-list* cpads 0 button0-rel 0) (pad-buttons triangle))
            (if (= (-> this display-state) (progress-screen settings)) (sound-play "menu-stats") (sound-play "cursor-options"))
            (load-level-text-files (-> *level-task-data* (-> this display-level-index) text-group-index))
            (set! (-> this next-display-state) (progress-screen invalid)))))
        ((or (cpad-pressed? 0 x) (cpad-pressed? 0 circle))
         (cond
           ((not (-> this selected-option))
            (cond
              ((= (-> s5-0 (-> this option-index) option-type) (game-option-type menu))
               (logclear! (-> *cpad-list* cpads 0 button0-abs 0) (pad-buttons x))
               (logclear! (-> *cpad-list* cpads 0 button0-rel 0) (pad-buttons x))
               (logclear! (-> *cpad-list* cpads 0 button0-abs 0) (pad-buttons circle))
               (logclear! (-> *cpad-list* cpads 0 button0-rel 0) (pad-buttons circle))
               (push! this)
               (sound-play "select-option")
               (set! (-> this next-display-state) (the-as progress-screen (-> s5-0 (-> this option-index) param3)))
               (case (-> this next-display-state)
                 (((progress-screen load-game) (progress-screen save-game) (progress-screen save-game-title))
                  (set! (-> this next-display-state) (set-memcard-screen this (-> this next-display-state))))))
              ((= (-> s5-0 (-> this option-index) option-type) (game-option-type button))
               (cond
                 ((= (-> s5-0 (-> this option-index) name) (text-id exit-demo)) (set! *master-exit* 'force) (set-master-mode 'game))
                 ((= (-> s5-0 (-> this option-index) name) (text-id back))
                  (if (= (-> this display-state) (progress-screen settings)) (sound-play "menu-stats") (sound-play "cursor-options"))
                  (load-level-text-files (-> *level-task-data* (-> this display-level-index) text-group-index))
                  (set! (-> this next-display-state) (progress-screen invalid)))))
              ((!= (-> s5-0 (-> this option-index) option-type) (game-option-type yes-no))
               (case (-> s5-0 (-> this option-index) option-type)
                 (((game-option-type slider))
                  (set! (-> *progress-state* slider-backup)
                        (-> (the-as (pointer float) (-> s5-0 (-> this option-index) value-to-modify)))))
                 (((game-option-type language))
                  (set! (-> *progress-state* language-backup)
                        (-> (the-as (pointer language-enum) (-> s5-0 (-> this option-index) value-to-modify)))))
                 (((game-option-type on-off))
                  (set! (-> *progress-state* on-off-backup)
                        (the-as symbol (-> (the-as (pointer uint32) (-> s5-0 (-> this option-index) value-to-modify))))))
                 (((game-option-type center-screen))
                  (set! (-> *progress-state* center-x-backup) (-> *setting-control* default screenx))
                  (set! (-> *progress-state* center-y-backup) (-> *setting-control* default screeny)))
                 (((game-option-type aspect-ratio) (game-option-type video-mode))
                  (set! (-> *progress-state* aspect-ratio-backup)
                        (the-as symbol (-> (the-as (pointer uint32) (-> s5-0 (-> this option-index) value-to-modify)))))))
               (sound-play "select-option")
               (logclear! (-> *cpad-list* cpads 0 button0-abs 0) (pad-buttons x))
               (logclear! (-> *cpad-list* cpads 0 button0-rel 0) (pad-buttons x))
               (logclear! (-> *cpad-list* cpads 0 button0-abs 0) (pad-buttons circle))
               (logclear! (-> *cpad-list* cpads 0 button0-rel 0) (pad-buttons circle))
               (set! (-> this selected-option) #t)
               (when (= (-> s5-0 (-> this option-index) option-type) (game-option-type language))
                 (set! (-> this language-selection) (-> *setting-control* current language))
                 (set! (-> this language-direction) #t)
                 (set! (-> this language-transition) #f)
                 (set! (-> this language-x-offset) 0)
                 0))))
           (else
            (sound-play "start-options")
            (set! (-> this selected-option) #f)
            (case (-> s5-0 (-> this option-index) option-type)
              (((game-option-type aspect-ratio))
               (set! (-> *setting-control* default aspect-ratio)
                     (the-as symbol (-> (the-as (pointer uint32) (-> s5-0 (-> this option-index) value-to-modify))))))
              (((game-option-type video-mode))
               (case (-> (the-as (pointer uint32) (-> s5-0 (-> this option-index) value-to-modify)))
                 (('pal)
                  (set! (-> *setting-control* default video-mode)
                        (the-as symbol (-> (the-as (pointer uint32) (-> s5-0 (-> this option-index) value-to-modify))))))
                 (('ntsc) (push! this) (set! (-> this next-display-state) (progress-screen pal-change-to-60hz)))))
              (((game-option-type language))
               (if (not (-> this language-transition)) (load-level-text-files (-> this display-level-index)))))))))))
  0
  (none))

(defmethod respond-progress ((this progress))
  (when (not (-> this in-transition))
    (cond
      ((cpad-pressed? 0 up)
       (let ((s5-0 (-> this display-level-index)))
         (set! (-> this next-level-index) (get-next-level-down s5-0))
         (when (!= s5-0 (-> this next-level-index))
           (sound-play "cursor-up-down")
           (set! (-> this level-transition) 2))))
      ((cpad-pressed? 0 down)
       (let ((s5-2 (-> this next-level-index)))
         (set! (-> this next-level-index) (get-next-level-up s5-2))
         (when (!= s5-2 (-> this next-level-index))
           (sound-play "cursor-up-down")
           (set! (-> this level-transition) 1))))
      ((cpad-pressed? 0 square)
       (when (nonzero? (-> this display-state))
         (sound-play "select-option")
         (set! (-> this next-display-state) (progress-screen fuel-cell))
         (set! (-> this stat-transition) #t)))
      ((cpad-pressed? 0 x)
       (when (!= (-> this display-state) (progress-screen money))
         (sound-play "select-option")
         (set! (-> this next-display-state) (progress-screen money))
         (set! (-> this stat-transition) #t)))
      ((cpad-pressed? 0 triangle)
       (when (!= (-> this display-state) (progress-screen buzzer))
         (sound-play "select-option")
         (set! (-> this next-display-state) (progress-screen buzzer))
         (set! (-> this stat-transition) #t)))
      ((cpad-pressed? 0 circle)
       (logclear! (-> *cpad-list* cpads 0 button0-abs 0) (pad-buttons circle))
       (logclear! (-> *cpad-list* cpads 0 button0-rel 0) (pad-buttons circle))
       (sound-play "start-options")
       (push! this)
       (set! (-> this next-display-state) (progress-screen settings)))
      ((= (-> this display-state) (progress-screen fuel-cell))
       (cond
         ((cpad-pressed? 0 left)
          (let ((s5-8 (-> this task-index)))
            (set! (-> this task-index) (get-next-task-down (-> this task-index) (-> this display-level-index)))
            (if (!= s5-8 (-> this task-index)) (sound-play "cursor-l-r"))))
         ((cpad-pressed? 0 right)
          (let ((s5-10 (-> this task-index)))
            (set! (-> this task-index) (get-next-task-up (-> this task-index) (-> this display-level-index)))
            (if (!= s5-10 (-> this task-index)) (sound-play "cursor-l-r"))))))))
  0
  (none))

(defstate progress-normal (progress)
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (local-vars (v0-0 object))
      (case message
        (('go-away) (go progress-going-out))
        (('notify)
         (cond
           ((= (-> block param 0) 'done)
            (case (-> self display-state)
              (((progress-screen memcard-saving))
               (cond
                 ((= (-> self display-state-stack 0) (progress-screen title))
                  (let ((gp-1 (-> *setting-control* default auto-save)))
                    (sound-volume-off)
                    (set! (-> *game-info* mode) 'play)
                    (cond
                      ;; Start a new game differently if speedrunning mode is active
                      ((= (-> *pc-settings* speedrunner-mode?) #t) (speedrun-start-full-game-run))
                      ;; start the game normally
                      (else
                       (initialize! *game-info* 'game (the-as game-save #f) "intro-start")
                       (set! (-> *setting-control* default auto-save) gp-1))))
                  (set-master-mode 'game))
                 (else (set! v0-0 -1) (set! (-> self next-display-state) (the-as progress-screen v0-0)) v0-0)))
              (((progress-screen memcard-formatting))
               (set! (-> self force-transition) #t)
               (set! v0-0 15)
               (set! (-> self next-display-state) (the-as progress-screen v0-0))
               v0-0)
              (((progress-screen memcard-creating))
               (cond
                 ((= (-> self display-state-stack 0) (progress-screen title))
                  (set! v0-0 18)
                  (set! (-> self next-display-state) (the-as progress-screen v0-0)))
                 (else (set! v0-0 17) (set! (-> self next-display-state) (the-as progress-screen v0-0))))
               v0-0)))
           ((= (-> block param 0) 'error)
            (format #t "ERROR NOTIFY: ~S ~D~%" (enum->string mc-status-code (-> block param 1)) (-> self display-state))
            (case (-> block param 1)
              ((14) (set! v0-0 7) (set! (-> self next-display-state) (the-as progress-screen v0-0)) v0-0)
              (else
               (case (-> self display-state)
                 (((progress-screen memcard-formatting))
                  (set! v0-0 24)
                  (set! (-> self next-display-state) (the-as progress-screen v0-0))
                  v0-0)
                 (((progress-screen memcard-creating))
                  (set! v0-0 25)
                  (set! (-> self next-display-state) (the-as progress-screen v0-0))
                  v0-0)
                 (((progress-screen memcard-saving))
                  (set! v0-0 21)
                  (set! (-> self next-display-state) (the-as progress-screen v0-0))
                  v0-0)
                 (((progress-screen memcard-loading))
                  (set! v0-0 20)
                  (set! (-> self next-display-state) (the-as progress-screen v0-0))
                  v0-0)))))))))
  :code
    (behavior ()
      (loop
        (when (and (cpad-hold? 0 l1) (cpad-hold? 0 r1) *cheat-mode*)
          (when (and (< (-> self task-index) (-> *level-task-data* (-> self display-level-index) nb-of-tasks))
                     (>= (-> self task-index) 0))
            (let ((gp-0 (-> *level-task-data* (-> self display-level-index) task-info (-> self task-index) task-id)))
              (close-specific-task! gp-0 (task-status need-resolution))
              (send-event *target* 'get-pickup 6 (the float gp-0)))))
        (if (and (= (-> self display-state) (-> self next-display-state))
                 (= (-> self display-level-index) (-> self next-level-index)))
          (seekl! (-> self transition-offset)
                  0
                  (* (the int (* (-> self transition-speed) (-> *display* time-adjust-ratio)))
                     (if (or (-> self stat-transition) (nonzero? (-> self level-transition))) 2 1)))
          (seekl! (-> self transition-offset)
                  512
                  (* (the int (* (-> self transition-speed) (-> *display* time-adjust-ratio)))
                     (if (or (-> self stat-transition) (nonzero? (-> self level-transition))) 2 1))))
        (set-transition-progress! self (-> self transition-offset))
        (set! (-> self in-transition) (or (-> self force-transition) (nonzero? (-> self transition-offset))))
        (when (and (not (handle->process (-> *game-info* auto-save-proc)))
                   (or (-> self force-transition) (-> self in-transition))
                   (>= (-> self transition-offset)
                       (if (and (zero? (-> self level-transition))
                                (nonzero? (-> self next-display-state))
                                (!= (-> self next-display-state) 1)
                                (!= (-> self next-display-state) 2))
                         512
                         256)))
          (if (>= (the-as int (-> self next-display-state)) 0) (enter! self (-> self next-display-state) 0) (pop! self))
          (set! (-> self display-level-index) (-> self next-level-index))
          (when (nonzero? (-> self level-transition))
            (set! (-> self task-index) (get-next-task-up -1 (-> self display-level-index)))
            (case (-> self level-transition)
              ((1) (set! (-> self level-transition) 2))
              ((2) (set! (-> self level-transition) 1))))
          (set! (-> self force-transition) #f))
        (when (zero? (-> self transition-offset))
          (set! (-> self stat-transition) #f)
          (set! (-> self level-transition) 0)
          0)
        (let ((gp-1 #f))
          (let ((v1-62 #f))
            (case (-> self display-state)
              (((progress-screen fuel-cell) (progress-screen money) (progress-screen buzzer))
               (let ((s5-0 (-> self display-level-index)))
                 (when (and (< (mod (-> *display* real-frame-counter) (seconds 0.2)) (seconds 0.1))
                            (zero? (-> *progress-process* 0 in-out-position))
                            (not (-> self in-transition))
                            (zero? (-> self transition-offset)))
                   (set! gp-1 (!= s5-0 (get-next-level-up s5-0)))
                   (set! v1-62 (!= s5-0 (get-next-level-down s5-0)))))))
            (set! (-> self particles 3 init-pos x) (the float (if v1-62 (- 195 (-> *progress-process* 0 left-x-offset)) -320))))
          (set! (-> self particles 4 init-pos x) (the float (if gp-1 (- 195 (-> *progress-process* 0 left-x-offset)) -320))))
        (respond-common self)
        (set! (-> self next-display-state) (set-memcard-screen self (-> self next-display-state)))
        (let ((v1-74 (-> self display-state)))
          (cond
            ((or (= v1-74 (progress-screen fuel-cell)) (or (= v1-74 (progress-screen money)) (= v1-74 (progress-screen buzzer))))
             (respond-progress self))
            ((or (= v1-74 (progress-screen memcard-no-space))
                 (= v1-74 (progress-screen memcard-format))
                 (= v1-74 (progress-screen memcard-data-exists))
                 (= v1-74 (progress-screen memcard-insert))
                 (= v1-74 (progress-screen load-game))
                 (= v1-74 (progress-screen save-game))
                 (= v1-74 (progress-screen save-game-title))
                 (= v1-74 (progress-screen memcard-error-loading))
                 (= v1-74 (progress-screen memcard-error-saving))
                 (= v1-74 (progress-screen memcard-error-formatting))
                 (= v1-74 (progress-screen memcard-error-creating))
                 (= v1-74 (progress-screen memcard-auto-save-error))
                 (= v1-74 (progress-screen memcard-removed))
                 (= v1-74 (progress-screen memcard-no-data))
                 (= v1-74 (progress-screen memcard-not-inserted))
                 (= v1-74 (progress-screen memcard-not-formatted))
                 (= v1-74 (progress-screen auto-save))
                 (= v1-74 (progress-screen pal-change-to-60hz))
                 (= v1-74 (progress-screen pal-now-60hz))
                 (= v1-74 (progress-screen no-disc))
                 (= v1-74 (progress-screen bad-disc))
                 (= v1-74 (progress-screen quit)))
             (respond-memcard self))))
        (suspend)))
  :post
    (behavior ()
      (let* ((a1-0 (-> self display-level-index))
             (gp-0 (-> *level-task-data* a1-0)))
        #t
        (let ((s5-0 #f))
          (case (-> self display-state)
            (((progress-screen fuel-cell)) (set! s5-0 #t) (draw-fuel-cell-screen self a1-0))
            (((progress-screen money)) (set! s5-0 #t) (draw-money-screen self a1-0))
            (((progress-screen buzzer)) (set! s5-0 #t) (draw-buzzer-screen self a1-0))
            (((progress-screen game-settings) (progress-screen settings))
             (hide-progress-icons)
             (draw-options self 115 30 (the-as float 0.82)))
            (((progress-screen graphic-settings)
              (progress-screen sound-settings)
              (progress-screen settings-title)
              (progress-screen title))
             (hide-progress-icons)
             (draw-options self 115 30 (the-as float 0.82)))
            (((progress-screen memcard-removed) (progress-screen memcard-auto-save-error))
             (draw-notice-screen self)
             (draw-options self 192 0 (the-as float 0.82)))
            (((progress-screen memcard-no-data)) (draw-notice-screen self) (draw-options self 165 0 (the-as float 0.82)))
            (((progress-screen memcard-format)) (draw-notice-screen self) (draw-options self 172 0 (the-as float 0.82)))
            (((progress-screen memcard-no-space) (progress-screen memcard-not-inserted) (progress-screen memcard-not-formatted))
             (draw-notice-screen self)
             (draw-options self 195 0 (the-as float 0.82)))
            (((progress-screen memcard-error-loading)
              (progress-screen memcard-error-saving)
              (progress-screen memcard-error-formatting)
              (progress-screen memcard-error-creating)
              (progress-screen memcard-auto-save-error))
             (draw-notice-screen self)
             (draw-options self 190 0 (the-as float 0.82)))
            (((progress-screen pal-change-to-60hz)) (draw-notice-screen self) (draw-options self 190 0 (the-as float 0.82)))
            (((progress-screen pal-now-60hz))
             (when (< (seconds 10) (- (-> *display* real-frame-counter) (-> self video-mode-timeout)))
               (set! (-> *progress-state* video-mode-choice) 'pal)
               (set! (-> *setting-control* default video-mode) (-> *progress-state* video-mode-choice))
               (set! (-> self next-display-state) (progress-screen invalid)))
             (draw-notice-screen self)
             (draw-options self 140 0 (the-as float 0.82)))
            (((progress-screen no-disc) (progress-screen bad-disc))
             (draw-notice-screen self)
             (if (is-cd-in?) (draw-options self 170 0 (the-as float 0.82))))
            (((progress-screen quit)) (draw-notice-screen self) (draw-options self 110 0 (the-as float 0.82)))
            (((progress-screen auto-save)) (draw-notice-screen self) (draw-options self 190 0 (the-as float 0.82)))
            (((progress-screen memcard-insert)) (draw-notice-screen self) (draw-options self 165 0 (the-as float 0.82)))
            (((progress-screen memcard-data-exists)) (draw-notice-screen self) (draw-options self 168 0 (the-as float 0.82)))
            (((progress-screen memcard-loading)
              (progress-screen memcard-saving)
              (progress-screen memcard-formatting)
              (progress-screen memcard-creating))
             (draw-notice-screen self))
            (((progress-screen load-game) (progress-screen save-game))
             (draw-notice-screen self)
             (draw-options self 190 0 (the-as float 0.82)))
            (((progress-screen save-game-title)) (draw-notice-screen self) (draw-options self 169 15 (the-as float 0.6))))
          (when s5-0
            (let* ((v1-98 (cond
                            ((-> self stat-transition) 0)
                            ((= (-> self level-transition) 1) (- (-> self transition-offset)))
                            (else (-> self transition-offset))))
                   (f30-0 (the-as float (if (-> self stat-transition) 1.0 (-> self transition-percentage-invert))))
                   (s5-1 (new 'stack
                              'font-context
                              *font-default-matrix*
                              (- 32 (-> self left-x-offset))
                              (the int (* (+ 42.0 (the float (/ v1-98 2))) f30-0))
                              (the-as float 8325000.0)
                              (font-color progress-blue)
                              (font-flags shadow kerning))))
              (let ((v1-103 s5-1)) (set! (-> v1-103 width) (the float 328)))
              (let ((v1-104 s5-1)) (set! (-> v1-104 height) (the float 45)))
              (set! (-> s5-1 flags) (font-flags shadow kerning middle middle-vert large))
              (print-game-text-scaled (lookup-text! *common-text* (-> gp-0 level-name-id) #f) f30-0 s5-1 (the int (* 128.0 f30-0)))))))
      (case (-> self display-state)
        (((progress-screen fuel-cell) (progress-screen money) (progress-screen buzzer)) (draw-progress self)))
      (adjust-sprites self)
      (adjust-icons self)))

(defstate progress-coming-in (progress)
  :event
    (-> progress-waiting
        event)
  :enter
    (behavior ()
      (sound-group-pause (sound-group sfx music dialog sog3 ambient sog5 sog6 sog7))
      (logclear! (-> *setting-control* default process-mask) (process-mask pause menu))
      (add-setting! 'process-mask 'set 0.0 (process-mask progress))
      (apply-settings *setting-control*)
      (sound-play "select-menu")
      (set-blackout-frames 0)
      (set! *pause-lock* #f))
  :code
    (behavior ()
      (loop
        (seekl! (-> self in-out-position) 0 (the int (* 170.0 (-> *display* time-adjust-ratio))))
        (when (< (-> self in-out-position) 2867)
          (seekl! (-> self transition-offset) 0 (the int (* (-> self transition-speed) (-> *display* time-adjust-ratio))))
          (set-transition-progress! self (-> self transition-offset)))
        (if (zero? (-> self in-out-position)) (go progress-normal))
        (suspend)))
  :post
    (-> progress-normal
        post))

(defstate progress-going-out (progress)
  :enter
    (behavior ()
      (sound-play "menu-close")
      (hide-progress-icons)
      (set! (-> self particles 3 init-pos x) -320.0)
      (set! (-> self particles 4 init-pos x) -320.0)
      (case (-> self display-state)
        (((progress-screen load-game) (progress-screen save-game) (progress-screen save-game-title))
         (set! (-> self transition-speed) 30.0))))
  :code
    (behavior ()
      (loop
        (seekl! (-> self transition-offset) 512 (the int (* (-> self transition-speed) (-> *display* time-adjust-ratio))))
        (set-transition-progress! self (-> self transition-offset))
        (when (< 153 (-> self transition-offset))
          (seekl! (-> self in-out-position) 4096 (the int (* 170.0 (-> *display* time-adjust-ratio))))
          (if (= (-> self in-out-position) 4096) (go progress-gone)))
        (suspend)))
  :post
    (-> progress-normal
        post))

(defstate progress-debug (progress)
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (case message
        (('go-away) (go progress-going-out))))
  :code
    (behavior ()
      (loop
        (cond
          ((cpad-pressed? 0 left) (if (> (-> self current-debug-string) 0) (+! (-> self current-debug-string) -1)))
          ((cpad-pressed? 0 right)
           (if (< (-> self current-debug-string) (+ (-> *common-text* length) -1)) (+! (-> self current-debug-string) 1)))
          ((cpad-pressed? 0 up)
           (when (> (-> self current-debug-group) 0)
             (+! (-> self current-debug-group) -1)
             (set! (-> self current-debug-string) 0)
             0))
          ((cpad-pressed? 0 down)
           (when (< (-> self current-debug-group) (+ (-> *text-group-names* length) -1))
             (+! (-> self current-debug-group) 1)
             (set! (-> self current-debug-string) 0)
             0))
          ((cpad-pressed? 0 l1)
           (if (> (the-as int (-> *setting-control* default language)) 0) (+! (-> *setting-control* default language) -1)))
          ((cpad-pressed? 0 r1)
           (if (< (the-as int (-> *setting-control* default language)) 6) (+! (-> *setting-control* default language) 1)))
          ((cpad-pressed? 0 l2)
           (logclear! (-> *cpad-list* cpads 0 button0-abs 0) (pad-buttons l2))
           (logclear! (-> *cpad-list* cpads 0 button0-rel 0) (pad-buttons l2))
           (go progress-normal)))
        (load-game-text-info (-> *text-group-names* (-> self current-debug-group)) '*common-text* *common-text-heap*)
        (suspend)))
  :post
    (behavior ()
      (let* ((s5-0 (-> *display* frames (-> *display* on-screen) frame global-buf))
             (gp-0 (-> s5-0 base)))
        (let ((s4-0 draw-string-xy))
          (let ((s3-0 format)
                (a0-4 (clear *temp-string*))
                (a1-0 "TEXT DEBUG: LANGUAGE ~S ID 0x~X")
                (v1-4 (-> *setting-control* current language)))
            (s3-0 a0-4
                  a1-0
                  (cond
                    ((= v1-4 (language-enum uk-english)) "uk-english")
                    ((= v1-4 (language-enum japanese)) "japanese")
                    ((= v1-4 (language-enum italian)) "italian")
                    ((= v1-4 (language-enum spanish)) "spanish")
                    ((= v1-4 (language-enum german)) "german")
                    ((= v1-4 (language-enum french)) "french")
                    ((= v1-4 (language-enum english)) "english")
                    (else "*unknown*"))
                  (-> *common-text* data (-> self current-debug-string) id)))
          (s4-0 *temp-string* s5-0 40 40 (font-color default) (font-flags shadow kerning)))
        (let ((a3-4 (-> s5-0 base)))
          (let ((v1-7 (the-as dma-packet (-> s5-0 base))))
            (set! (-> v1-7 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
            (set! (-> v1-7 vif0) (new 'static 'vif-tag))
            (set! (-> v1-7 vif1) (new 'static 'vif-tag))
            (set! (-> s5-0 base) (&+ (the-as pointer v1-7) 16)))
          (dma-bucket-insert-tag (-> *display* frames (-> *display* on-screen) frame bucket-group)
                                 (bucket-id debug)
                                 gp-0
                                 (the-as (pointer dma-tag) a3-4))))
      (let* ((s5-1 (-> *display* frames (-> *display* on-screen) frame global-buf))
             (gp-1 (-> s5-1 base)))
        (let ((s4-1 draw-string-xy))
          (format (clear *temp-string*) "USE LEFT/RIGHT TO SELECT STRING")
          (s4-1 *temp-string* s5-1 40 155 (font-color default) (font-flags shadow kerning)))
        (let ((a3-6 (-> s5-1 base)))
          (let ((v1-16 (the-as dma-packet (-> s5-1 base))))
            (set! (-> v1-16 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
            (set! (-> v1-16 vif0) (new 'static 'vif-tag))
            (set! (-> v1-16 vif1) (new 'static 'vif-tag))
            (set! (-> s5-1 base) (the-as pointer (the-as dma-packet (&+ v1-16 16)))))
          (dma-bucket-insert-tag (-> *display* frames (-> *display* on-screen) frame bucket-group)
                                 (bucket-id debug)
                                 gp-1
                                 (the-as (pointer dma-tag) a3-6))))
      (let* ((s5-2 (-> *display* frames (-> *display* on-screen) frame global-buf))
             (gp-2 (-> s5-2 base)))
        (let ((s4-2 draw-string-xy))
          (format (clear *temp-string*) "USE UP/DOWN TO SELECT GROUP")
          (s4-2 *temp-string* s5-2 40 165 (font-color default) (font-flags shadow kerning)))
        (let ((a3-8 (-> s5-2 base)))
          (let ((v1-25 (the-as dma-packet (-> s5-2 base))))
            (set! (-> v1-25 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
            (set! (-> v1-25 vif0) (new 'static 'vif-tag))
            (set! (-> v1-25 vif1) (new 'static 'vif-tag))
            (set! (-> s5-2 base) (the-as pointer (the-as dma-packet (&+ v1-25 16)))))
          (dma-bucket-insert-tag (-> *display* frames (-> *display* on-screen) frame bucket-group)
                                 (bucket-id debug)
                                 gp-2
                                 (the-as (pointer dma-tag) a3-8))))
      (let* ((s5-3 (-> *display* frames (-> *display* on-screen) frame global-buf))
             (gp-3 (-> s5-3 base)))
        (let ((s4-3 draw-string-xy))
          (format (clear *temp-string*) "USE L1/R1 TO SELECT LANGUAGE")
          (s4-3 *temp-string* s5-3 40 175 (font-color default) (font-flags shadow kerning)))
        (let ((a3-10 (-> s5-3 base)))
          (let ((v1-34 (the-as dma-packet (-> s5-3 base))))
            (set! (-> v1-34 dma) (new 'static 'dma-tag :id (dma-tag-id next)))
            (set! (-> v1-34 vif0) (new 'static 'vif-tag))
            (set! (-> v1-34 vif1) (new 'static 'vif-tag))
            (set! (-> s5-3 base) (the-as pointer (the-as dma-packet (&+ v1-34 16)))))
          (dma-bucket-insert-tag (-> *display* frames (-> *display* on-screen) frame bucket-group)
                                 (bucket-id debug)
                                 gp-3
                                 (the-as (pointer dma-tag) a3-10))))
      (let ((gp-4 (new 'stack 'font-context *font-default-matrix* 32 50 0.0 (font-color default) (font-flags shadow kerning))))
        (let ((v1-42 gp-4)) (set! (-> v1-42 width) (the float 328)))
        (let ((v1-43 gp-4)) (set! (-> v1-43 height) (the float 100)))
        (logior! (-> gp-4 flags) (font-flags shadow kerning large))
        (draw-debug-text-box gp-4)
        (print-game-text (-> *common-text* data (-> self current-debug-string) text) gp-4 #f 128 22))))
