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

;; name: game-save.gc
;; name in dgo: game-save
;; dgos: ENGINE, GAME

;; NOTE - for default-menu
(declare-type auto-save process)
(define-extern auto-save-command (function symbol int int process-tree symbol (pointer auto-save)))

(defenum game-save-elt
  :type uint16
  (task-deaths 402)
  (task-node-list 306)
  (task-list 300)
  (real-time 102)
  (aspect-ratio 509)
  (play-hints 507)
  (continue-deaths 401)
  (node-close-time 419)
  (continue-time 408)
  (dialog-volume 502)
  (vibration 506)
  (real-frame-time 105)
  (task-complete-time 409)
  (total-game-time 108)
  (frame-time 104)
  (in-level-time 414)
  (sfx-volume 500)
  (life 201)
  (gun-ammo 216)
  (task-start-time 415)
  (auto-save-count 413)
  (eco-pill-dark-total 212)
  (game-time 103)
  (screenx 504)
  (death-movie-tick 207)
  (buzzer-total 205)
  (bigmap-data 308)
  (stereo-mode 513)
  (task-pickup-time 407)
  (name 100)
  (level-open-list 305)
  (disk-tester 600)
  (camera-stick-dir 510)
  (continue 200)
  (deaths-per-level 411)
  (karma 210)
  (scores 221)
  (session-time 106)
  (bg-time 107)
  (screeny 505)
  (total-trys 421)
  (music-volume 501)
  (node-death-count 416)
  (gem 218)
  (base-time 101)
  (node-name 420)
  (eco-pill-dark 211)
  (skill-total 209)
  (bigmap-offsets 309)
  (death-pos 412)
  (talker-state 307)
  (features 214)
  (game-start-time 403)
  (hit-time 406)
  (subtitle 511)
  (total-deaths 400)
  (death-time 405)
  (video-mode 508)
  (fuel-cell 206)
  (node-gem-count 417)
  (skill 208)
  (subtitle-language 512)
  (secrets 220)
  (shield 213)
  (gun-type 215)
  (gem-total 219)
  (node-skill-count 418)
  (money-per-level 204)
  (money-total 203)
  (money 202)
  (enter-level-time 410)
  (purchase-secrets 222)
  (perm-list 301)
  (language 503)
  )

;; DECOMP BEGINS

(defun-debug game-save-elt->string ((arg0 game-save-elt))
  (enum->string game-save-elt arg0)
  )

(deftype game-save-tag (structure)
  ((user-object  object  2 :offset   0)
   (user-uint64  uint64    :overlay-at (-> user-object 0))
   (user-float0  float     :overlay-at user-uint64)
   (user-float   float   2 :overlay-at user-float0)
   (user-int32   int32   2 :overlay-at user-float0)
   (user-uint32  uint32  2 :overlay-at user-float0)
   (user-int16   int16   4 :overlay-at user-uint64)
   (user-uint16  uint16  4 :overlay-at user-uint64)
   (user-int8    int8    8 :overlay-at user-uint64)
   (user-int80   int8      :overlay-at user-uint64)
   (user-int81   int8      :overlay-at (-> user-int8 1))
   (user-uint8   uint8   8 :overlay-at user-int80)
   (elt-count    int32)
   (elt-size     uint16)
   (elt-type     game-save-elt)
   )
  )


(deftype game-save (basic)
  ((version                int32)
   (allocated-length       int32)
   (length                 int32)
   (info-int32             int32          16)
   (info-int8              int8           64 :overlay-at (-> info-int32 0))
   (level-index            int32             :overlay-at (-> info-int32 0))
   (gem-count              float             :overlay-at (-> info-int32 1))
   (skill-count            float             :overlay-at (-> info-int32 2))
   (completion-percentage  float             :overlay-at (-> info-int32 3))
   (minute                 uint8             :overlay-at (-> info-int32 5))
   (hour                   uint8             :overlay-at (-> info-int8 21))
   (week                   uint8             :overlay-at (-> info-int8 22))
   (day                    uint8             :overlay-at (-> info-int8 23))
   (month                  uint8             :overlay-at (-> info-int32 6))
   (year                   uint8             :overlay-at (-> info-int8 25))
   (new-game               int32             :overlay-at (-> info-int32 7))
   (game-time              time-frame        :overlay-at (-> info-int32 8))
   (secrets                uint32            :overlay-at (-> info-int32 10))
   (features               uint32            :overlay-at (-> info-int32 11))
   (tag                    game-save-tag  :inline :dynamic)
   )
  (:methods
    (new (symbol type int) _type_)
    (save-to-file (_type_ string) _type_)
    (load-from-file (_type_ string) _type_)
    (debug-inspect (_type_ symbol) _type_)
    )
  )


;; WARN: Return type mismatch uint vs int.
(defmethod asize-of ((this game-save))
  (the-as int (+ (-> game-save size) (-> this allocated-length)))
  )

(defmethod new game-save ((allocation symbol) (type-to-make type) (arg0 int))
  (let ((v0-0 (object-new allocation type-to-make (the-as int (+ (-> type-to-make size) arg0)))))
    (set! (-> v0-0 version) 3)
    (set! (-> v0-0 allocated-length) arg0)
    v0-0
    )
  )

(defmethod debug-inspect ((this game-save) (arg0 symbol))
  (local-vars (sv-16 int) (sv-32 string) (sv-48 string))
  (format #t "[~8x] ~A~%" this (-> this type))
  (format #t "~Tversion: ~D~%" (-> this version))
  (format #t "~Tallocated-length: ~D~%" (-> this allocated-length))
  (format #t "~Tlength: ~D~%" (-> this length))
  (format #t "~Tlevel-index: ~D~%" (-> this level-index))
  (format #t "~Tgem-count: ~f~%" (-> this gem-count))
  (format #t "~Tskill-count: ~f~%" (-> this skill-count))
  (format #t "~Tcompletion-percentage: ~f~%" (-> this completion-percentage))
  (format
    #t
    "~Tsave-time: ~x:~x ~x/~x/~x~%"
    (-> this hour)
    (-> this minute)
    (-> this day)
    (-> this month)
    (-> this year)
    )
  (format #t "~Tgame-time: ~E~%" (-> this game-time))
  (format #t "~Tsecrets: #x~x~%" (-> this secrets))
  (format #t "~Tfeatures: #x~x~%" (-> this features))
  (format #t "~Ttag[]: @ #x~X~%" (-> this tag))
  (let ((s4-0 (the-as object (-> this tag)))
        (s3-0 0)
        )
    (while (< (the-as int s4-0) (the-as int (&-> this tag 0 user-int8 (-> this length))))
      (let ((s2-0 format)
            (s1-0 #t)
            (s0-0 "~T  [~3D] ~-32S [~3D/~3D]  ~12D ~8f    ")
            )
        (set! sv-16 s3-0)
        (let ((a3-2 (game-save-elt->string (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-type)))
              (t0-1 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))
              (t1-1 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-size))
              (t2-1 (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64))
              (t3-0 (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
              )
          (s2-0 s1-0 s0-0 sv-16 a3-2 t0-1 t1-1 t2-1 t3-0)
          )
        )
      (let ((v1-0 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-type)))
        (if (or (= v1-0 (game-save-elt name)) (= v1-0 (game-save-elt continue)))
            (format #t "= \"~G\"~%" (-> (the-as (inline-array game-save-tag) s4-0) 1))
            (format #t "~%")
            )
        )
      (when arg0
        (case (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-type)
          (((game-save-elt node-death-count) (game-save-elt node-skill-count) (game-save-elt node-gem-count))
           (dotimes (s2-1 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))
             (let ((s1-1 format)
                   (s0-1 #t)
                   )
               (set! sv-32 "                 ~-32S: ~D~%")
               (let ((a2-15 (game-task-node->string (the-as game-task-node s2-1)))
                     (a3-3 (-> (the-as (inline-array game-save-tag) s4-0) 1 user-uint16 s2-1))
                     )
                 (s1-1 s0-1 sv-32 a2-15 a3-3)
                 )
               )
             )
           )
          (((game-save-elt node-close-time))
           (dotimes (s2-2 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))
             (let ((s1-2 format)
                   (s0-2 #t)
                   )
               (set! sv-48 "                 ~-32S: ~D~%")
               (let ((a2-16 (game-task-node->string (the-as game-task-node s2-2)))
                     (a3-4
                       (-> (the-as (pointer uint64) (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* s2-2 8))))
                       )
                     )
                 (s1-2 s0-2 sv-48 a2-16 a3-4)
                 )
               )
             )
           )
          (((game-save-elt enter-level-time))
           (dotimes (s2-3 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))
             (when (< s2-3 (-> *task-level* length))
               (let ((a2-17 (task-level->string s2-3)))
                 (if a2-17
                     (format
                       #t
                       "                 ~-32S: ~D~%"
                       a2-17
                       (-> (the-as (pointer uint64) (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* s2-3 8))))
                       )
                     )
                 )
               )
             )
           )
          (((game-save-elt in-level-time))
           (dotimes (s2-4 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))
             (when (< s2-4 (-> *task-level* length))
               (let ((a2-18 (task-level->string s2-4)))
                 (if a2-18
                     (format
                       #t
                       "                 ~-32S: ~D~%"
                       a2-18
                       (-> (the-as (pointer uint64) (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* s2-4 8))))
                       )
                     )
                 )
               )
             )
           )
          (((game-save-elt task-complete-time) (game-save-elt task-start-time))
           (dotimes (s2-5 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))
             (let ((a2-19 (game-task->string (the-as game-task s2-5))))
               (if a2-19
                   (format
                     #t
                     "                 ~-32S: ~D~%"
                     a2-19
                     (-> (the-as (pointer uint64) (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* s2-5 8))))
                     )
                   )
               )
             )
           )
          (((game-save-elt disk-tester))
           0
           )
          )
        )
      (set! s4-0 (&+
                   (the-as pointer s4-0)
                   (logand -16 (+ (* (the-as int (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-size))
                                     (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count)
                                     )
                                  31
                                  )
                           )
                   )
            )
      (+! s3-0 1)
      )
    )
  this
  )

(defmethod inspect game-save ((this game-save))
  (debug-inspect this #f)
  )

(defmethod save-game ((this game-info) (arg0 game-save) (arg1 string))
  (with-pp
    (dotimes (s4-0 (-> *level* length))
      (let ((a1-1 (-> *level* level s4-0)))
        (if (= (-> a1-1 status) 'active)
            (copy-perms-from-level! this a1-1)
            )
        )
      )
    (set! (-> arg0 length) 0)
    (set! (-> arg0 version) 3)
    (let ((s4-1 (get-continue-by-name this (-> this current-continue name))))
      (when (not s4-1)
        (format
          0
          "ERROR: SAVE: attempting to save continue ~A which is not in level-info~%"
          (-> this current-continue name)
          )
        (set! s4-1 (get-continue-by-name this "title-start"))
        )
      (let ((v1-19 (-> *task-manager-engine* alive-list next0)))
        *task-manager-engine*
        (let ((s2-0 (-> v1-19 next0)))
          (while (!= v1-19 (-> *task-manager-engine* alive-list-end))
            (let ((a1-5 (new 'stack-no-clear 'event-message-block)))
              (set! (-> a1-5 from) (process->ppointer pp))
              (set! (-> a1-5 num-params) 0)
              (set! (-> a1-5 message) 'fail-continue)
              (let ((a1-6 (send-event-function (the-as process-tree (-> (the-as connection v1-19) param1)) a1-5)))
                (when a1-6
                  (let ((a0-13 (get-continue-by-name this (the-as string a1-6))))
                    (if a0-13
                        (set! s4-1 a0-13)
                        )
                    )
                  )
                )
              )
            (set! v1-19 s2-0)
            *task-manager-engine*
            (set! s2-0 (-> s2-0 next0))
            )
          )
        )
      (set! (-> arg0 level-index) (the-as int (-> (lookup-level-info (-> s4-1 level)) task-level)))
      (set! (-> arg0 gem-count) (-> this gem))
      (set! (-> arg0 skill-count) (-> this skill))
      (set! (-> arg0 completion-percentage) (calculate-percentage this))
      (set! (-> arg0 game-time) (+ -300000 (-> *display* total-game-clock frame-counter)))
      (set! (-> arg0 secrets) (the-as uint (-> this purchase-secrets)))
      (set! (-> arg0 features) (the-as uint (-> this features)))
      (when (string= (-> s4-1 name) "title-start")
        (set! (-> arg0 new-game) 1)
        (set! (-> arg0 level-index) 0)
        (set! (-> arg0 gem-count) 0.0)
        (set! (-> arg0 skill-count) 0.0)
        (set! (-> arg0 completion-percentage) 0.0)
        (set! (-> arg0 game-time) 0)
        (set! (-> arg0 features) (the-as uint 0))
        (set! (-> arg0 secrets) (the-as uint 0))
        (when (logtest? (-> *game-info* secrets) (game-secrets hero-mode))
          (set! (-> arg0 new-game) 2)
          (set! (-> arg0 secrets) (the-as uint #x8000))
          )
        )
      (let ((s2-1 (new 'stack 'scf-time)))
        (scf-get-time s2-1)
        (when (zero? (-> s2-1 stat))
          (set! (-> arg0 minute) (-> s2-1 minute))
          (set! (-> arg0 hour) (-> s2-1 hour))
          (set! (-> arg0 day) (-> s2-1 day))
          (set! (-> arg0 week) (-> s2-1 week))
          (set! (-> arg0 month) (-> s2-1 month))
          (set! (-> arg0 year) (-> s2-1 year))
          )
        )
      (let ((s2-2 (the-as object (-> arg0 tag))))
        (let ((s1-0 (-> (the-as (inline-array game-save-tag) s2-2) 0)))
          (set! (-> s1-0 elt-type) (game-save-elt name))
          (set! (-> s1-0 elt-count) (+ (length arg1) 1))
          (set! (-> s1-0 elt-size) (the-as uint 1))
          )
        (copy-charp<-charp (the-as (pointer uint8) (-> (the-as (inline-array game-save-tag) s2-2) 1)) (-> arg1 data))
        (let ((v1-59
                (the-as
                  (inline-array game-save-tag)
                  (+ (the-as int s2-2)
                     (the-as
                       int
                       (&+ (logand -16 (&+ (the-as pointer (-> (the-as (inline-array game-save-tag) s2-2) 0 elt-count)) 15)) 16)
                       )
                     )
                  )
                )
              )
          (let ((a0-31 (-> v1-59 0)))
            (set! (-> a0-31 elt-type) (game-save-elt base-time))
            (set! (-> a0-31 elt-count) 2)
            (set! (-> a0-31 elt-size) (the-as uint 8))
            )
          (let ((s3-1 (-> v1-59 1)))
            (save! (-> *display* base-clock) (the-as (pointer uint64) s3-1))
            (let ((v1-63 (the-as object (&+ s3-1 16))))
              (let ((a0-33 (-> (the-as (inline-array game-save-tag) v1-63) 0)))
                (set! (-> a0-33 elt-type) (game-save-elt game-time))
                (set! (-> a0-33 elt-count) 2)
                (set! (-> a0-33 elt-size) (the-as uint 8))
                )
              (let ((s3-2 (&+ (the-as game-save-tag v1-63) 16)))
                (save! (-> *display* game-clock) (the-as (pointer uint64) s3-2))
                (let ((v1-67 (the-as object (&+ s3-2 16))))
                  (let ((a0-35 (-> (the-as (inline-array game-save-tag) v1-67) 0)))
                    (set! (-> a0-35 elt-type) (game-save-elt total-game-time))
                    (set! (-> a0-35 elt-count) 2)
                    (set! (-> a0-35 elt-size) (the-as uint 8))
                    )
                  (let ((s3-3 (&+ (the-as game-save-tag v1-67) 16)))
                    (save! (-> *display* total-game-clock) (the-as (pointer uint64) s3-3))
                    (let ((v1-71 (the-as object (&+ s3-3 16))))
                      (let ((a0-37 (-> (the-as (inline-array game-save-tag) v1-71) 0)))
                        (set! (-> a0-37 elt-type) (game-save-elt real-time))
                        (set! (-> a0-37 elt-count) 2)
                        (set! (-> a0-37 elt-size) (the-as uint 8))
                        )
                      (let ((s3-4 (&+ (the-as game-save-tag v1-71) 16)))
                        (save! (-> *display* real-clock) (the-as (pointer uint64) s3-4))
                        (let ((v1-75 (the-as object (&+ s3-4 16))))
                          (let ((a0-39 (-> (the-as (inline-array game-save-tag) v1-75) 0)))
                            (set! (-> a0-39 elt-type) (game-save-elt frame-time))
                            (set! (-> a0-39 elt-count) 2)
                            (set! (-> a0-39 elt-size) (the-as uint 8))
                            )
                          (let ((s3-5 (&+ (the-as game-save-tag v1-75) 16)))
                            (save! (-> *display* frame-clock) (the-as (pointer uint64) s3-5))
                            (let ((v1-79 (the-as object (&+ s3-5 16))))
                              (let ((a0-41 (-> (the-as (inline-array game-save-tag) v1-79) 0)))
                                (set! (-> a0-41 elt-type) (game-save-elt real-frame-time))
                                (set! (-> a0-41 elt-count) 2)
                                (set! (-> a0-41 elt-size) (the-as uint 8))
                                )
                              (let ((s3-6 (&+ (the-as game-save-tag v1-79) 16)))
                                (save! (-> *display* real-frame-clock) (the-as (pointer uint64) s3-6))
                                (let ((v1-83 (the-as object (&+ s3-6 16))))
                                  (let ((a0-43 (-> (the-as (inline-array game-save-tag) v1-83) 0)))
                                    (set! (-> a0-43 elt-type) (game-save-elt session-time))
                                    (set! (-> a0-43 elt-count) 2)
                                    (set! (-> a0-43 elt-size) (the-as uint 8))
                                    )
                                  (let ((s3-7 (&+ (the-as game-save-tag v1-83) 16)))
                                    (save! (-> *display* session-clock) (the-as (pointer uint64) s3-7))
                                    (let ((v1-87 (the-as object (&+ s3-7 16))))
                                      (let ((a0-45 (-> (the-as (inline-array game-save-tag) v1-87) 0)))
                                        (set! (-> a0-45 elt-type) (game-save-elt bg-time))
                                        (set! (-> a0-45 elt-count) 2)
                                        (set! (-> a0-45 elt-size) (the-as uint 8))
                                        )
                                      (let ((s3-8 (&+ (the-as game-save-tag v1-87) 16)))
                                        (save! (-> *display* bg-clock) (the-as (pointer uint64) s3-8))
                                        (let ((s3-9 (the-as object (&+ s3-8 16))))
                                          (let ((s4-2 (-> s4-1 name)))
                                            (let ((s2-3 (-> (the-as (inline-array game-save-tag) s3-9) 0)))
                                              (set! (-> s2-3 elt-type) (game-save-elt continue))
                                              (set! (-> s2-3 elt-count) (+ (length s4-2) 1))
                                              (set! (-> s2-3 elt-size) (the-as uint 1))
                                              )
                                            (copy-charp<-charp (the-as (pointer uint8) (&+ (the-as game-save-tag s3-9) 16)) (-> s4-2 data))
                                            )
                                          (let ((v1-99 (the-as
                                                         object
                                                         (&+ (the-as pointer s3-9) (+ (logand -16 (+ (-> (the-as game-save-tag s3-9) elt-count) 15)) 16))
                                                         )
                                                       )
                                                )
                                            (let ((a0-52 (-> (the-as (inline-array game-save-tag) v1-99) 0)))
                                              (set! (-> a0-52 elt-type) (game-save-elt life))
                                              (set! (-> a0-52 elt-count) 0)
                                              (set! (-> a0-52 user-float0) (-> this life))
                                              )
                                            (let ((v1-100 (the-as (inline-array game-save-tag) (&+ (the-as pointer v1-99) 16))))
                                              (let ((a0-53 (-> v1-100 0)))
                                                (set! (-> a0-53 elt-type) (game-save-elt buzzer-total))
                                                (set! (-> a0-53 elt-count) 0)
                                                (set! (-> a0-53 user-float0) (-> this buzzer-total))
                                                )
                                              (let ((v1-101 (the-as object (-> v1-100 1))))
                                                (let ((a0-54 (-> (the-as (inline-array game-save-tag) v1-101) 0)))
                                                  (set! (-> a0-54 elt-type) (game-save-elt fuel-cell))
                                                  (set! (-> a0-54 elt-count) 0)
                                                  (set! (-> a0-54 user-float0) (-> this fuel))
                                                  )
                                                (let ((v1-102 (the-as object (&+ (the-as game-save-tag v1-101) 16))))
                                                  (let ((a0-55 (-> (the-as (inline-array game-save-tag) v1-102) 0)))
                                                    (set! (-> a0-55 elt-type) (game-save-elt death-movie-tick))
                                                    (set! (-> a0-55 elt-count) 0)
                                                    (set! (-> a0-55 user-uint64) (the-as uint (-> this death-movie-tick)))
                                                    )
                                                  (let ((v1-103 (the-as object (&+ (the-as game-save-tag v1-102) 16))))
                                                    (let ((a0-56 (-> (the-as (inline-array game-save-tag) v1-103) 0)))
                                                      (set! (-> a0-56 elt-type) (game-save-elt money))
                                                      (set! (-> a0-56 elt-count) 0)
                                                      (set! (-> a0-56 user-float0) (-> this money))
                                                      )
                                                    (let ((v1-104 (the-as (inline-array game-save-tag) (&+ (the-as game-save-tag v1-103) 16))))
                                                      (let ((a0-57 (-> v1-104 0)))
                                                        (set! (-> a0-57 elt-type) (game-save-elt money-total))
                                                        (set! (-> a0-57 elt-count) 0)
                                                        (set! (-> a0-57 user-float0) (-> this money-total))
                                                        )
                                                      (let ((v1-106 (the-as object (-> (the-as (inline-array game-save-tag) (-> v1-104 1)) 2))))
                                                        (let ((a0-58 (-> (the-as (inline-array game-save-tag) v1-106) 0)))
                                                          (set! (-> a0-58 elt-type) (game-save-elt skill))
                                                          (set! (-> a0-58 elt-count) 0)
                                                          (set! (-> a0-58 user-float0) (-> this skill))
                                                          )
                                                        (let ((v1-107 (the-as object (&+ (the-as game-save-tag v1-106) 16))))
                                                          (let ((a0-59 (-> (the-as (inline-array game-save-tag) v1-107) 0)))
                                                            (set! (-> a0-59 elt-type) (game-save-elt skill-total))
                                                            (set! (-> a0-59 elt-count) 0)
                                                            (set! (-> a0-59 user-float0) (-> this skill-total))
                                                            )
                                                          (let ((v1-108 (the-as object (&+ (the-as game-save-tag v1-107) 16))))
                                                            (let ((a0-60 (-> (the-as (inline-array game-save-tag) v1-108) 0)))
                                                              (set! (-> a0-60 elt-type) (game-save-elt gem))
                                                              (set! (-> a0-60 elt-count) 0)
                                                              (set! (-> a0-60 user-float0) (-> this gem))
                                                              )
                                                            (let ((v1-109 (the-as object (&+ (the-as game-save-tag v1-108) 16))))
                                                              (let ((a0-61 (-> (the-as (inline-array game-save-tag) v1-109) 0)))
                                                                (set! (-> a0-61 elt-type) (game-save-elt gem-total))
                                                                (set! (-> a0-61 elt-count) 0)
                                                                (set! (-> a0-61 user-float0) (-> this gem-total))
                                                                )
                                                              (let ((v1-110 (the-as object (&+ (the-as game-save-tag v1-109) 16))))
                                                                (let ((a0-62 (-> (the-as (inline-array game-save-tag) v1-110) 0)))
                                                                  (set! (-> a0-62 elt-type) (game-save-elt karma))
                                                                  (set! (-> a0-62 elt-count) 0)
                                                                  (set! (-> a0-62 user-float0) (-> this karma))
                                                                  )
                                                                (let ((v1-111 (the-as object (&+ (the-as game-save-tag v1-110) 16))))
                                                                  (let ((a0-63 (-> (the-as (inline-array game-save-tag) v1-111) 0)))
                                                                    (set! (-> a0-63 elt-type) (game-save-elt eco-pill-dark))
                                                                    (set! (-> a0-63 elt-count) 0)
                                                                    (set! (-> a0-63 user-float0) (-> this eco-pill-dark))
                                                                    )
                                                                  (let ((v1-112 (the-as object (&+ (the-as game-save-tag v1-111) 16))))
                                                                    (let ((a0-64 (-> (the-as (inline-array game-save-tag) v1-112) 0)))
                                                                      (set! (-> a0-64 elt-type) (game-save-elt eco-pill-dark-total))
                                                                      (set! (-> a0-64 elt-count) 0)
                                                                      (set! (-> a0-64 user-float0) (-> this eco-pill-dark-total))
                                                                      )
                                                                    (let ((v1-113 (the-as object (&+ (the-as game-save-tag v1-112) 16))))
                                                                      (let ((a0-65 (-> (the-as (inline-array game-save-tag) v1-113) 0)))
                                                                        (set! (-> a0-65 elt-type) (game-save-elt shield))
                                                                        (set! (-> a0-65 elt-count) 0)
                                                                        (set! (-> a0-65 user-float0) (-> this shield))
                                                                        )
                                                                      (let ((v1-114 (the-as object (&+ (the-as game-save-tag v1-113) 16))))
                                                                        (let ((a0-66 (-> (the-as (inline-array game-save-tag) v1-114) 0)))
                                                                          (set! (-> a0-66 elt-type) (game-save-elt features))
                                                                          (set! (-> a0-66 elt-count) 0)
                                                                          (set! (-> a0-66 user-uint64) (the-as uint (-> this features)))
                                                                          )
                                                                        (let ((v1-115 (the-as object (&+ (the-as game-save-tag v1-114) 16))))
                                                                          (let ((a0-67 (-> (the-as (inline-array game-save-tag) v1-115) 0)))
                                                                            (set! (-> a0-67 elt-type) (game-save-elt secrets))
                                                                            (set! (-> a0-67 elt-count) 0)
                                                                            (set! (-> a0-67 user-uint64) (the-as uint (-> this secrets)))
                                                                            )
                                                                          (let ((v1-116 (the-as object (&+ (the-as game-save-tag v1-115) 16))))
                                                                            (let ((a0-68 (-> (the-as (inline-array game-save-tag) v1-116) 0)))
                                                                              (set! (-> a0-68 elt-type) (game-save-elt purchase-secrets))
                                                                              (set! (-> a0-68 elt-count) 0)
                                                                              (set! (-> a0-68 user-uint64) (the-as uint (-> this purchase-secrets)))
                                                                              )
                                                                            (let ((v1-117 (the-as object (&+ (the-as game-save-tag v1-116) 16))))
                                                                              (let ((a0-69 (-> (the-as (inline-array game-save-tag) v1-117) 0)))
                                                                                (set! (-> a0-69 elt-type) (game-save-elt gun-type))
                                                                                (set! (-> a0-69 elt-count) 0)
                                                                                (set! (-> a0-69 user-uint64) (the-as uint (-> this gun-type)))
                                                                                )
                                                                              (let ((v1-118 (the-as object (&+ (the-as game-save-tag v1-117) 16))))
                                                                                (let ((a0-70 (-> (the-as (inline-array game-save-tag) v1-118) 0)))
                                                                                  (set! (-> a0-70 elt-type) (game-save-elt gun-ammo))
                                                                                  (set! (-> a0-70 elt-count) 4)
                                                                                  (set! (-> a0-70 elt-size) (the-as uint 4))
                                                                                  )
                                                                                (let ((v1-119 (&+ (the-as game-save-tag v1-118) 16)))
                                                                                  (dotimes (a0-71 4)
                                                                                    (set! (-> v1-119 user-object a0-71) (-> this gun-ammo a0-71))
                                                                                    )
                                                                                  (let ((v1-120 (the-as object (&+ v1-119 16))))
                                                                                    (let ((a0-74 (-> (the-as (inline-array game-save-tag) v1-120) 0)))
                                                                                      (set! (-> a0-74 elt-type) (game-save-elt level-open-list))
                                                                                      (set! (-> a0-74 elt-count) 32)
                                                                                      (set! (-> a0-74 elt-size) (the-as uint 1))
                                                                                      )
                                                                                    (let ((v1-121 (the-as object (&+ (the-as game-save-tag v1-120) 16))))
                                                                                      (dotimes (a0-75 32)
                                                                                        (set! (-> (the-as (pointer uint8) (&+ (the-as pointer v1-121) a0-75))) (-> this level-opened a0-75))
                                                                                        )
                                                                                      (let ((v1-122 (the-as object (-> (the-as (inline-array game-save-tag) v1-121) 2)))
                                                                                            (s3-10 (-> this sub-task-list length))
                                                                                            )
                                                                                        (let ((a0-79 (-> (the-as (inline-array game-save-tag) v1-122) 0)))
                                                                                          (set! (-> a0-79 elt-type) (game-save-elt node-name))
                                                                                          (set! (-> a0-79 elt-count) s3-10)
                                                                                          (set! (-> a0-79 elt-size) (the-as uint 64))
                                                                                          )
                                                                                        (let ((s4-3 (the-as object (&+ (the-as game-save-tag v1-122) 16))))
                                                                                          (dotimes (s2-4 s3-10)
                                                                                            (copyn-charp<-string (the-as (pointer uint8) s4-3) (-> this sub-task-list s2-4 name) 64)
                                                                                            (set! s4-3 (&+ (the-as pointer s4-3) 64))
                                                                                            )
                                                                                          (let ((s3-11 (-> this perm-list length)))
                                                                                            (let ((v1-129 (-> (the-as (inline-array game-save-tag) s4-3) 0)))
                                                                                              (set! (-> v1-129 elt-type) (game-save-elt perm-list))
                                                                                              (set! (-> v1-129 elt-count) s3-11)
                                                                                              (set! (-> v1-129 elt-size) (the-as uint 16))
                                                                                              )
                                                                                            (let ((s4-4 (the-as object (&+ (the-as pointer s4-3) 16))))
                                                                                              (dotimes (s2-5 s3-11)
                                                                                                (mem-copy! (&+ (the-as pointer s4-4) (* s2-5 16)) (the-as pointer (-> this perm-list data s2-5)) 16)
                                                                                                )
                                                                                              (let ((v1-137 (the-as object (+ (the-as int s4-4) (logand -16 (+ (* s3-11 16) 15)))))
                                                                                                    (s4-5 (-> this task-perm-list length))
                                                                                                    )
                                                                                                (let ((a0-88 (-> (the-as (inline-array game-save-tag) v1-137) 0)))
                                                                                                  (set! (-> a0-88 elt-type) (game-save-elt task-list))
                                                                                                  (set! (-> a0-88 elt-count) s4-5)
                                                                                                  (set! (-> a0-88 elt-size) (the-as uint 16))
                                                                                                  )
                                                                                                (let ((s3-12 (+ (the-as int v1-137) 16)))
                                                                                                  (dotimes (s2-6 s4-5)
                                                                                                    (mem-copy! (the-as pointer (+ s3-12 (* s2-6 16))) (the-as pointer (-> this task-perm-list data s2-6)) 16)
                                                                                                    )
                                                                                                  (let ((a0-92 (the-as object (+ s3-12 (logand -16 (+ (* s4-5 16) 15)))))
                                                                                                        (v1-147 (-> this unknown-pad6 allocated-length))
                                                                                                        )
                                                                                                    (let ((a1-88 (-> (the-as (inline-array game-save-tag) a0-92) 0)))
                                                                                                      (set! (-> a1-88 elt-type) (game-save-elt talker-state))
                                                                                                      (set! (-> a1-88 elt-count) v1-147)
                                                                                                      (set! (-> a1-88 elt-size) (the-as uint 2))
                                                                                                      )
                                                                                                    (let ((a0-93 (+ (the-as int a0-92) 16)))
                                                                                                      (dotimes (a1-89 v1-147)
                                                                                                        (set! (-> (the-as (pointer uint16) (+ a0-93 (* a1-89 2))) 0) (-> this unknown-pad6 a1-89))
                                                                                                        )
                                                                                                      (let ((a0-94 (the-as object (+ a0-93 (logand -16 (+ (* v1-147 2) 15)))))
                                                                                                            (v1-153 (-> this game-score allocated-length))
                                                                                                            )
                                                                                                        (let ((a1-93 (-> (the-as (inline-array game-save-tag) a0-94) 0)))
                                                                                                          (set! (-> a1-93 elt-type) (game-save-elt scores))
                                                                                                          (set! (-> a1-93 elt-count) v1-153)
                                                                                                          (set! (-> a1-93 elt-size) (the-as uint 4))
                                                                                                          )
                                                                                                        (let ((a0-95 (+ (the-as int a0-94) 16)))
                                                                                                          (dotimes (a1-94 v1-153)
                                                                                                            (set! (-> (the-as (pointer float) (+ a0-95 (* a1-94 4))) 0) (-> this game-score a1-94))
                                                                                                            )
                                                                                                          (let ((s4-6 (the-as object (+ a0-95 (logand -16 (+ (* v1-153 4) 15))))))
                                                                                                            (compress-all *bigmap*)
                                                                                                            (let ((v1-161 (-> *bigmap* compressed-next-index)))
                                                                                                              (let ((a0-97 (-> (the-as (inline-array game-save-tag) s4-6) 0)))
                                                                                                                (set! (-> a0-97 elt-type) (game-save-elt bigmap-data))
                                                                                                                (set! (-> a0-97 elt-count) (the-as int v1-161))
                                                                                                                (set! (-> a0-97 elt-size) (the-as uint 1))
                                                                                                                )
                                                                                                              (let ((a0-98 (+ (the-as int s4-6) 16)))
                                                                                                                (dotimes (a1-100 (the-as int v1-161))
                                                                                                                  (set! (-> (the-as (pointer uint8) (+ a0-98 a1-100)) 0)
                                                                                                                        (-> (the-as game-save-tag (+ (-> *bigmap* compressed-data) a1-100)) user-uint8 0)
                                                                                                                        )
                                                                                                                  )
                                                                                                                (let ((a0-99 (the-as object (+ a0-98 (logand -16 (+ v1-161 15)))))
                                                                                                                      (v1-165 20)
                                                                                                                      )
                                                                                                                  (let ((a1-104 (-> (the-as (inline-array game-save-tag) a0-99) 0)))
                                                                                                                    (set! (-> a1-104 elt-type) (game-save-elt bigmap-offsets))
                                                                                                                    (set! (-> a1-104 elt-count) v1-165)
                                                                                                                    (set! (-> a1-104 elt-size) (the-as uint 4))
                                                                                                                    )
                                                                                                                  (let ((a0-100 (+ (the-as int a0-99) 16)))
                                                                                                                    (dotimes (a1-105 v1-165)
                                                                                                                      (if (-> *bigmap* compressed-masks a1-105)
                                                                                                                          (set! (-> (the-as (pointer uint32) (+ a0-100 (* a1-105 4))) 0)
                                                                                                                                (the-as uint (&- (-> *bigmap* compressed-masks a1-105) (the-as uint (-> *bigmap* compressed-data))))
                                                                                                                                )
                                                                                                                          (set! (-> (the-as (pointer int32) (+ a0-100 (* a1-105 4))) 0) -1)
                                                                                                                          )
                                                                                                                      )
                                                                                                                    (let ((v1-169 (the-as object (+ a0-100 (logand -16 (+ (* v1-165 4) 15))))))
                                                                                                                      (let ((a0-102 (-> (the-as (inline-array game-save-tag) v1-169) 0)))
                                                                                                                        (set! (-> a0-102 elt-type) (game-save-elt auto-save-count))
                                                                                                                        (set! (-> a0-102 elt-count) 0)
                                                                                                                        (set! (-> a0-102 user-uint64) (the-as uint (-> this auto-save-count)))
                                                                                                                        )
                                                                                                                      (let ((v1-170 (the-as object (+ (the-as int v1-169) 16))))
                                                                                                                        (let ((a0-103 (-> (the-as (inline-array game-save-tag) v1-170) 0)))
                                                                                                                          (set! (-> a0-103 elt-type) (game-save-elt total-deaths))
                                                                                                                          (set! (-> a0-103 elt-count) 0)
                                                                                                                          (set! (-> a0-103 user-uint64) (the-as uint (-> this total-deaths)))
                                                                                                                          )
                                                                                                                        (let ((v1-171 (the-as object (+ (the-as int v1-170) 16))))
                                                                                                                          (let ((a0-104 (-> (the-as (inline-array game-save-tag) v1-171) 0)))
                                                                                                                            (set! (-> a0-104 elt-type) (game-save-elt total-trys))
                                                                                                                            (set! (-> a0-104 elt-count) 0)
                                                                                                                            (set! (-> a0-104 user-uint64) (the-as uint (-> this total-trys)))
                                                                                                                            )
                                                                                                                          (let ((v1-172 (the-as object (+ (the-as int v1-171) 16))))
                                                                                                                            (let ((a0-105 (-> (the-as (inline-array game-save-tag) v1-172) 0)))
                                                                                                                              (set! (-> a0-105 elt-type) (game-save-elt continue-deaths))
                                                                                                                              (set! (-> a0-105 elt-count) 0)
                                                                                                                              (set! (-> a0-105 user-uint64) (the-as uint (-> this continue-deaths)))
                                                                                                                              )
                                                                                                                            (let ((v1-173 (the-as object (+ (the-as int v1-172) 16))))
                                                                                                                              (let ((a0-106 (-> (the-as (inline-array game-save-tag) v1-173) 0)))
                                                                                                                                (set! (-> a0-106 elt-type) (game-save-elt task-deaths))
                                                                                                                                (set! (-> a0-106 elt-count) 0)
                                                                                                                                (set! (-> a0-106 user-uint64) (the-as uint (-> this task-deaths)))
                                                                                                                                )
                                                                                                                              (let ((v1-174 (the-as object (+ (the-as int v1-173) 16))))
                                                                                                                                (let ((a0-107 (-> (the-as (inline-array game-save-tag) v1-174) 0)))
                                                                                                                                  (set! (-> a0-107 elt-type) (game-save-elt game-start-time))
                                                                                                                                  (set! (-> a0-107 elt-count) 0)
                                                                                                                                  (set! (-> a0-107 user-uint64) (the-as uint (-> this game-start-time)))
                                                                                                                                  )
                                                                                                                                (let ((v1-175 (the-as object (+ (the-as int v1-174) 16))))
                                                                                                                                  (let ((a0-108 (-> (the-as (inline-array game-save-tag) v1-175) 0)))
                                                                                                                                    (set! (-> a0-108 elt-type) (game-save-elt continue-time))
                                                                                                                                    (set! (-> a0-108 elt-count) 0)
                                                                                                                                    (set! (-> a0-108 user-uint64) (the-as uint (-> this continue-time)))
                                                                                                                                    )
                                                                                                                                  (let ((v1-176 (the-as object (+ (the-as int v1-175) 16))))
                                                                                                                                    (let ((a0-109 (-> (the-as (inline-array game-save-tag) v1-176) 0)))
                                                                                                                                      (set! (-> a0-109 elt-type) (game-save-elt death-time))
                                                                                                                                      (set! (-> a0-109 elt-count) 0)
                                                                                                                                      (set! (-> a0-109 user-uint64) (the-as uint (-> this death-time)))
                                                                                                                                      )
                                                                                                                                    (let ((v1-177 (the-as object (+ (the-as int v1-176) 16))))
                                                                                                                                      (let ((a0-110 (-> (the-as (inline-array game-save-tag) v1-177) 0)))
                                                                                                                                        (set! (-> a0-110 elt-type) (game-save-elt hit-time))
                                                                                                                                        (set! (-> a0-110 elt-count) 0)
                                                                                                                                        (set! (-> a0-110 user-uint64) (the-as uint (-> this hit-time)))
                                                                                                                                        )
                                                                                                                                      (let ((v1-178 (the-as object (+ (the-as int v1-177) 16))))
                                                                                                                                        (let ((a0-111 (-> (the-as (inline-array game-save-tag) v1-178) 0)))
                                                                                                                                          (set! (-> a0-111 elt-type) (game-save-elt task-pickup-time))
                                                                                                                                          (set! (-> a0-111 elt-count) 0)
                                                                                                                                          (set! (-> a0-111 user-uint64) (the-as uint (-> this task-pickup-time)))
                                                                                                                                          )
                                                                                                                                        (let ((v1-179 (the-as object (+ (the-as int v1-178) 16))))
                                                                                                                                          (let ((a0-112 (-> (the-as (inline-array game-save-tag) v1-179) 0)))
                                                                                                                                            (set! (-> a0-112 elt-type) (game-save-elt task-complete-time))
                                                                                                                                            (set! (-> a0-112 elt-count) 110)
                                                                                                                                            (set! (-> a0-112 elt-size) (the-as uint 8))
                                                                                                                                            )
                                                                                                                                          (let ((v1-180 (+ (the-as int v1-179) 16)))
                                                                                                                                            (dotimes (a0-113 110)
                                                                                                                                              (set! (-> (the-as (pointer time-frame) (+ v1-180 (* a0-113 8))) 0) (-> this unknown-array1 a0-113))
                                                                                                                                              )
                                                                                                                                            (let ((v1-181 (the-as object (+ v1-180 880))))
                                                                                                                                              (let ((a0-116 (-> (the-as (inline-array game-save-tag) v1-181) 0)))
                                                                                                                                                (set! (-> a0-116 elt-type) (game-save-elt task-start-time))
                                                                                                                                                (set! (-> a0-116 elt-count) 110)
                                                                                                                                                (set! (-> a0-116 elt-size) (the-as uint 8))
                                                                                                                                                )
                                                                                                                                              (let ((v1-182 (+ (the-as int v1-181) 16)))
                                                                                                                                                (dotimes (a0-117 110)
                                                                                                                                                  (set! (-> (the-as (pointer time-frame) (+ v1-182 (* a0-117 8))) 0) (-> this task-close-times a0-117))
                                                                                                                                                  )
                                                                                                                                                (let ((v1-183 (the-as object (+ v1-182 880))))
                                                                                                                                                  (let ((a0-120 (-> (the-as (inline-array game-save-tag) v1-183) 0)))
                                                                                                                                                    (set! (-> a0-120 elt-type) (game-save-elt enter-level-time))
                                                                                                                                                    (set! (-> a0-120 elt-count) 32)
                                                                                                                                                    (set! (-> a0-120 elt-size) (the-as uint 8))
                                                                                                                                                    )
                                                                                                                                                  (let ((v1-184 (+ (the-as int v1-183) 16)))
                                                                                                                                                    (dotimes (a0-121 32)
                                                                                                                                                      (set! (-> (the-as (pointer time-frame) (+ v1-184 (* a0-121 8))) 0) (-> this task-enter-times a0-121))
                                                                                                                                                      )
                                                                                                                                                    (let ((v1-185 (the-as object (+ v1-184 256))))
                                                                                                                                                      (let ((a0-124 (-> (the-as (inline-array game-save-tag) v1-185) 0)))
                                                                                                                                                        (set! (-> a0-124 elt-type) (game-save-elt in-level-time))
                                                                                                                                                        (set! (-> a0-124 elt-count) 32)
                                                                                                                                                        (set! (-> a0-124 elt-size) (the-as uint 8))
                                                                                                                                                        )
                                                                                                                                                      (let ((v1-186 (+ (the-as int v1-185) 16)))
                                                                                                                                                        (dotimes (a0-125 32)
                                                                                                                                                          (set! (-> (the-as (pointer time-frame) (+ v1-186 (* a0-125 8))) 0) (-> this task-in-times a0-125))
                                                                                                                                                          )
                                                                                                                                                        (let ((a0-128 (the-as object (+ v1-186 256)))
                                                                                                                                                              (v1-188 (-> this sub-task-list length))
                                                                                                                                                              )
                                                                                                                                                          (let ((a1-153 (-> (the-as (inline-array game-save-tag) a0-128) 0)))
                                                                                                                                                            (set! (-> a1-153 elt-type) (game-save-elt node-death-count))
                                                                                                                                                            (set! (-> a1-153 elt-count) v1-188)
                                                                                                                                                            (set! (-> a1-153 elt-size) (the-as uint 2))
                                                                                                                                                            )
                                                                                                                                                          (let ((a0-129 (+ (the-as int a0-128) 16)))
                                                                                                                                                            (dotimes (a1-154 v1-188)
                                                                                                                                                              (set! (-> (the-as (pointer uint16) (+ a0-129 (* a1-154 2))) 0) (-> this sub-task-list a1-154 death-count))
                                                                                                                                                              )
                                                                                                                                                            (let ((a0-130 (the-as object (+ a0-129 (logand -16 (+ (* v1-188 2) 15))))))
                                                                                                                                                              (let ((a1-159 (-> (the-as (inline-array game-save-tag) a0-130) 0)))
                                                                                                                                                                (set! (-> a1-159 elt-type) (game-save-elt node-gem-count))
                                                                                                                                                                (set! (-> a1-159 elt-count) v1-188)
                                                                                                                                                                (set! (-> a1-159 elt-size) (the-as uint 2))
                                                                                                                                                                )
                                                                                                                                                              (let ((a0-131 (+ (the-as int a0-130) 16)))
                                                                                                                                                                (dotimes (a1-160 v1-188)
                                                                                                                                                                  (set! (-> (the-as (pointer uint16) (+ a0-131 (* a1-160 2))) 0) (-> this sub-task-list a1-160 gem-count))
                                                                                                                                                                  )
                                                                                                                                                                (let ((a0-132 (the-as object (+ a0-131 (logand -16 (+ (* v1-188 2) 15))))))
                                                                                                                                                                  (let ((a1-165 (-> (the-as (inline-array game-save-tag) a0-132) 0)))
                                                                                                                                                                    (set! (-> a1-165 elt-type) (game-save-elt node-skill-count))
                                                                                                                                                                    (set! (-> a1-165 elt-count) v1-188)
                                                                                                                                                                    (set! (-> a1-165 elt-size) (the-as uint 2))
                                                                                                                                                                    )
                                                                                                                                                                  (let ((a0-133 (+ (the-as int a0-132) 16)))
                                                                                                                                                                    (dotimes (a1-166 v1-188)
                                                                                                                                                                      (set! (-> (the-as (pointer uint16) (+ a0-133 (* a1-166 2))) 0) (-> this sub-task-list a1-166 skill-count))
                                                                                                                                                                      )
                                                                                                                                                                    (let ((a0-134 (the-as object (+ a0-133 (logand -16 (+ (* v1-188 2) 15))))))
                                                                                                                                                                      (let ((a1-171 (-> (the-as (inline-array game-save-tag) a0-134) 0)))
                                                                                                                                                                        (set! (-> a1-171 elt-type) (game-save-elt node-close-time))
                                                                                                                                                                        (set! (-> a1-171 elt-count) v1-188)
                                                                                                                                                                        (set! (-> a1-171 elt-size) (the-as uint 8))
                                                                                                                                                                        )
                                                                                                                                                                      (let ((a0-135 (+ (the-as int a0-134) 16)))
                                                                                                                                                                        (dotimes (a1-172 v1-188)
                                                                                                                                                                          (set! (-> (the-as (pointer time-frame) (+ a0-135 (* a1-172 8))) 0) (-> this sub-task-list a1-172 close-time))
                                                                                                                                                                          )
                                                                                                                                                                        (let ((a0-136 (the-as object (+ a0-135 (logand -16 (+ (* v1-188 8) 15)))))
                                                                                                                                                                              (v1-194 (-> this sub-task-list length))
                                                                                                                                                                              )
                                                                                                                                                                          (let ((a1-176 (-> (the-as (inline-array game-save-tag) a0-136) 0)))
                                                                                                                                                                            (set! (-> a1-176 elt-type) (game-save-elt task-node-list))
                                                                                                                                                                            (set! (-> a1-176 elt-count) v1-194)
                                                                                                                                                                            (set! (-> a1-176 elt-size) (the-as uint 1))
                                                                                                                                                                            )
                                                                                                                                                                          (let ((a0-137 (+ (the-as int a0-136) 16)))
                                                                                                                                                                            (dotimes (a1-177 v1-194)
                                                                                                                                                                              (set! (-> (the-as (pointer int8) (+ a0-137 a1-177)) 0)
                                                                                                                                                                                    (if (logtest? (-> this sub-task-list a1-177 flags) (game-task-node-flag closed))
                                                                                                                                                                                        1
                                                                                                                                                                                        0
                                                                                                                                                                                        )
                                                                                                                                                                                    )
                                                                                                                                                                              )
                                                                                                                                                                            (let ((v1-197 (the-as object (+ a0-137 (logand -16 (+ v1-194 15))))))
                                                                                                                                                                              (let ((a0-139 (-> (the-as (inline-array game-save-tag) v1-197) 0)))
                                                                                                                                                                                (set! (-> a0-139 elt-type) (game-save-elt sfx-volume))
                                                                                                                                                                                (set! (-> a0-139 elt-count) 0)
                                                                                                                                                                                (set! (-> a0-139 user-float0) (-> *setting-control* user-default sfx-volume))
                                                                                                                                                                                )
                                                                                                                                                                              (let ((v1-198 (the-as object (+ (the-as int v1-197) 16))))
                                                                                                                                                                                (let ((a0-140 (-> (the-as (inline-array game-save-tag) v1-198) 0)))
                                                                                                                                                                                  (set! (-> a0-140 elt-type) (game-save-elt music-volume))
                                                                                                                                                                                  (set! (-> a0-140 elt-count) 0)
                                                                                                                                                                                  (set! (-> a0-140 user-float0) (-> *setting-control* user-default music-volume))
                                                                                                                                                                                  )
                                                                                                                                                                                (let ((v1-199 (the-as object (+ (the-as int v1-198) 16))))
                                                                                                                                                                                  (let ((a0-141 (-> (the-as (inline-array game-save-tag) v1-199) 0)))
                                                                                                                                                                                    (set! (-> a0-141 elt-type) (game-save-elt dialog-volume))
                                                                                                                                                                                    (set! (-> a0-141 elt-count) 0)
                                                                                                                                                                                    (set! (-> a0-141 user-float0) (-> *setting-control* user-default dialog-volume))
                                                                                                                                                                                    )
                                                                                                                                                                                  (let ((v1-200 (the-as object (+ (the-as int v1-199) 16))))
                                                                                                                                                                                    (let ((a0-142 (-> (the-as (inline-array game-save-tag) v1-200) 0)))
                                                                                                                                                                                      (set! (-> a0-142 elt-type) (game-save-elt language))
                                                                                                                                                                                      (set! (-> a0-142 elt-count) 0)
                                                                                                                                                                                      (set! (-> a0-142 user-uint64) (the-as uint (-> *setting-control* user-default language)))
                                                                                                                                                                                      )
                                                                                                                                                                                    (let ((v1-201 (the-as object (+ (the-as int v1-200) 16))))
                                                                                                                                                                                      (let ((a0-143 (-> (the-as (inline-array game-save-tag) v1-201) 0)))
                                                                                                                                                                                        (set! (-> a0-143 elt-type) (game-save-elt subtitle-language))
                                                                                                                                                                                        (set! (-> a0-143 elt-count) 0)
                                                                                                                                                                                        (set! (-> a0-143 user-uint64) (the-as uint (-> *setting-control* user-default subtitle-language)))
                                                                                                                                                                                        )
                                                                                                                                                                                      (let ((v1-202 (the-as object (+ (the-as int v1-201) 16))))
                                                                                                                                                                                        (let ((a0-144 (-> (the-as (inline-array game-save-tag) v1-202) 0)))
                                                                                                                                                                                          (set! (-> a0-144 elt-type) (game-save-elt stereo-mode))
                                                                                                                                                                                          (set! (-> a0-144 elt-count) 0)
                                                                                                                                                                                          (set! (-> a0-144 user-uint64) (the-as uint (-> *setting-control* user-default stereo-mode)))
                                                                                                                                                                                          )
                                                                                                                                                                                        (let ((v1-203 (the-as object (+ (the-as int v1-202) 16))))
                                                                                                                                                                                          (let ((a0-145 (-> (the-as (inline-array game-save-tag) v1-203) 0)))
                                                                                                                                                                                            (set! (-> a0-145 elt-type) (game-save-elt screenx))
                                                                                                                                                                                            (set! (-> a0-145 elt-count) 0)
                                                                                                                                                                                            (set! (-> a0-145 user-float0) (the float (-> *setting-control* user-default display-dx)))
                                                                                                                                                                                            )
                                                                                                                                                                                          (let ((v1-204 (the-as object (+ (the-as int v1-203) 16))))
                                                                                                                                                                                            (let ((a0-146 (-> (the-as (inline-array game-save-tag) v1-204) 0)))
                                                                                                                                                                                              (set! (-> a0-146 elt-type) (game-save-elt screeny))
                                                                                                                                                                                              (set! (-> a0-146 elt-count) 0)
                                                                                                                                                                                              (set! (-> a0-146 user-float0) (the float (-> *setting-control* user-default display-dy)))
                                                                                                                                                                                              )
                                                                                                                                                                                            (let ((v1-205 (the-as object (+ (the-as int v1-204) 16))))
                                                                                                                                                                                              (let ((a0-147 (-> (the-as (inline-array game-save-tag) v1-205) 0)))
                                                                                                                                                                                                (set! (-> a0-147 elt-type) (game-save-elt vibration))
                                                                                                                                                                                                (set! (-> a0-147 elt-count) 0)
                                                                                                                                                                                                (set! (-> a0-147 user-uint64) (the-as uint (if (-> *setting-control* user-default vibration)
                                                                                                                                                                                                                                               1
                                                                                                                                                                                                                                               0
                                                                                                                                                                                                                                               )
                                                                                                                                                                                                                                      )
                                                                                                                                                                                                      )
                                                                                                                                                                                                )
                                                                                                                                                                                              (let ((v1-206 (the-as object (+ (the-as int v1-205) 16))))
                                                                                                                                                                                                (let ((a0-148 (-> (the-as (inline-array game-save-tag) v1-206) 0)))
                                                                                                                                                                                                  (set! (-> a0-148 elt-type) (game-save-elt subtitle))
                                                                                                                                                                                                  (set! (-> a0-148 elt-count) 0)
                                                                                                                                                                                                  (set! (-> a0-148 user-uint64) (the-as uint (if (-> *setting-control* user-default subtitle)
                                                                                                                                                                                                                                                 1
                                                                                                                                                                                                                                                 0
                                                                                                                                                                                                                                                 )
                                                                                                                                                                                                                                        )
                                                                                                                                                                                                        )
                                                                                                                                                                                                  )
                                                                                                                                                                                                (let ((v1-207 (the-as object (+ (the-as int v1-206) 16))))
                                                                                                                                                                                                  (let ((a0-149 (-> (the-as (inline-array game-save-tag) v1-207) 0)))
                                                                                                                                                                                                    (set! (-> a0-149 elt-type) (game-save-elt camera-stick-dir))
                                                                                                                                                                                                    (set! (-> a0-149 elt-count) 0)
                                                                                                                                                                                                    (set! (-> a0-149 user-uint64) (the-as uint (if (-> *setting-control* user-default camera-stick-dir)
                                                                                                                                                                                                                                                   1
                                                                                                                                                                                                                                                   0
                                                                                                                                                                                                                                                   )
                                                                                                                                                                                                                                          )
                                                                                                                                                                                                          )
                                                                                                                                                                                                    )
                                                                                                                                                                                                  (let ((v1-208 (the-as object (+ (the-as int v1-207) 16))))
                                                                                                                                                                                                    (let ((a0-150 (-> (the-as (inline-array game-save-tag) v1-208) 0)))
                                                                                                                                                                                                      (set! (-> a0-150 elt-type) (game-save-elt play-hints))
                                                                                                                                                                                                      (set! (-> a0-150 elt-count) 0)
                                                                                                                                                                                                      (set! (-> a0-150 user-uint64) (the-as uint (if (-> *setting-control* user-default play-hints)
                                                                                                                                                                                                                                                     1
                                                                                                                                                                                                                                                     0
                                                                                                                                                                                                                                                     )
                                                                                                                                                                                                                                            )
                                                                                                                                                                                                            )
                                                                                                                                                                                                      )
                                                                                                                                                                                                    (let ((v1-209 (the-as object (+ (the-as int v1-208) 16))))
                                                                                                                                                                                                      (let ((a0-151 (-> (the-as (inline-array game-save-tag) v1-209) 0)))
                                                                                                                                                                                                        (set! (-> a0-151 elt-type) (game-save-elt video-mode))
                                                                                                                                                                                                        (set! (-> a0-151 elt-count) 0)
                                                                                                                                                                                                        (let ((a1-220 (-> *setting-control* user-default video-mode)))
                                                                                                                                                                                                          (set! (-> a0-151 user-uint64) (the-as uint (cond
                                                                                                                                                                                                                                                       ((= a1-220 'ntsc)
                                                                                                                                                                                                                                                        1
                                                                                                                                                                                                                                                        )
                                                                                                                                                                                                                                                       ((= a1-220 'pal)
                                                                                                                                                                                                                                                        2
                                                                                                                                                                                                                                                        )
                                                                                                                                                                                                                                                       (else
                                                                                                                                                                                                                                                         0
                                                                                                                                                                                                                                                         )
                                                                                                                                                                                                                                                       )
                                                                                                                                                                                                                                                )
                                                                                                                                                                                                                )
                                                                                                                                                                                                          )
                                                                                                                                                                                                        )
                                                                                                                                                                                                      (let ((v1-210 (the-as object (+ (the-as int v1-209) 16))))
                                                                                                                                                                                                        (let ((a0-152 (-> (the-as (inline-array game-save-tag) v1-210) 0)))
                                                                                                                                                                                                          (set! (-> a0-152 elt-type) (game-save-elt aspect-ratio))
                                                                                                                                                                                                          (set! (-> a0-152 elt-count) 0)
                                                                                                                                                                                                          (let ((a1-224 (-> *setting-control* user-default aspect-ratio)))
                                                                                                                                                                                                            (set! (-> a0-152 user-uint64) (the-as uint (cond
                                                                                                                                                                                                                                                         ((= a1-224 'aspect4x3)
                                                                                                                                                                                                                                                          1
                                                                                                                                                                                                                                                          )
                                                                                                                                                                                                                                                         ((= a1-224 'aspect16x9)
                                                                                                                                                                                                                                                          2
                                                                                                                                                                                                                                                          )
                                                                                                                                                                                                                                                         (else
                                                                                                                                                                                                                                                           0
                                                                                                                                                                                                                                                           )
                                                                                                                                                                                                                                                         )
                                                                                                                                                                                                                                                  )
                                                                                                                                                                                                                  )
                                                                                                                                                                                                            )
                                                                                                                                                                                                          )
                                                                                                                                                                                                        (set! (-> arg0 length) (- (+ (the-as int v1-210) 16) (the-as int (the-as pointer (-> arg0 tag)))))
                                                                                                                                                                                                        )
                                                                                                                                                                                                      )
                                                                                                                                                                                                    )
                                                                                                                                                                                                  )
                                                                                                                                                                                                )
                                                                                                                                                                                              )
                                                                                                                                                                                            )
                                                                                                                                                                                          )
                                                                                                                                                                                        )
                                                                                                                                                                                      )
                                                                                                                                                                                    )
                                                                                                                                                                                  )
                                                                                                                                                                                )
                                                                                                                                                                              )
                                                                                                                                                                            )
                                                                                                                                                                          )
                                                                                                                                                                        )
                                                                                                                                                                      )
                                                                                                                                                                    )
                                                                                                                                                                  )
                                                                                                                                                                )
                                                                                                                                                              )
                                                                                                                                                            )
                                                                                                                                                          )
                                                                                                                                                        )
                                                                                                                                                      )
                                                                                                                                                    )
                                                                                                                                                  )
                                                                                                                                                )
                                                                                                                                              )
                                                                                                                                            )
                                                                                                                                          )
                                                                                                                                        )
                                                                                                                                      )
                                                                                                                                    )
                                                                                                                                  )
                                                                                                                                )
                                                                                                                              )
                                                                                                                            )
                                                                                                                          )
                                                                                                                        )
                                                                                                                      )
                                                                                                                    )
                                                                                                                  )
                                                                                                                )
                                                                                                              )
                                                                                                            )
                                                                                                          )
                                                                                                        )
                                                                                                      )
                                                                                                    )
                                                                                                  )
                                                                                                )
                                                                                              )
                                                                                            )
                                                                                          )
                                                                                        )
                                                                                      )
                                                                                    )
                                                                                  )
                                                                                )
                                                                              )
                                                                            )
                                                                          )
                                                                        )
                                                                      )
                                                                    )
                                                                  )
                                                                )
                                                              )
                                                            )
                                                          )
                                                        )
                                                      )
                                                    )
                                                  )
                                                )
                                              )
                                            )
                                          )
                                        )
                                      )
                                    )
                                  )
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    (if (< (-> arg0 allocated-length) (-> arg0 length))
        (format
          0
          "ERROR: SAVEGAME: fatal error, save is using ~D of ~D bytes."
          (-> arg0 length)
          (-> arg0 allocated-length)
          )
        )
    arg0
    )
  )

(defmethod load-game ((this game-info) (arg0 game-save))
  (let ((v1-0 (the-as object (-> arg0 tag))))
    (while (< (the-as int v1-0) (the-as int (&-> arg0 tag 0 user-int8 (-> arg0 length))))
      (case (-> (the-as (inline-array game-save-tag) v1-0) 0 elt-type)
        ;; og:preserve-this Moved saving these settings to pc-settings
        ;; (((game-save-elt sfx-volume))
        ;;  (set! (-> *setting-control* user-default sfx-volume)
        ;;        (-> (the-as (inline-array game-save-tag) v1-0) 0 user-float0)
        ;;        )
        ;;  )
        ;; (((game-save-elt music-volume))
        ;;  (set! (-> *setting-control* user-default music-volume)
        ;;        (-> (the-as (inline-array game-save-tag) v1-0) 0 user-float0)
        ;;        )
        ;;  )
        ;; (((game-save-elt dialog-volume))
        ;;  (set! (-> *setting-control* user-default dialog-volume)
        ;;        (-> (the-as (inline-array game-save-tag) v1-0) 0 user-float0)
        ;;        )
        ;;  )
        (((game-save-elt language))
         (set! (-> *setting-control* user-default language)
               (the-as language-enum (-> (the-as (inline-array game-save-tag) v1-0) 0 user-uint64))
               )
         )
        (((game-save-elt subtitle-language))
         (set! (-> *setting-control* user-default subtitle-language)
               (the-as language-enum (-> (the-as (inline-array game-save-tag) v1-0) 0 user-uint64))
               )
         )
        (((game-save-elt stereo-mode))
         (set! (-> *setting-control* user-default stereo-mode)
               (the-as int (-> (the-as (inline-array game-save-tag) v1-0) 0 user-uint64))
               )
         )
        ;; (((game-save-elt vibration))
        ;;  (set! (-> *setting-control* user-default vibration)
        ;;        (= (-> (the-as (inline-array game-save-tag) v1-0) 0 user-uint64) 1)
        ;;        )
        ;;  )
        ;; (((game-save-elt subtitle))
        ;;  (set! (-> *setting-control* user-default subtitle)
        ;;        (= (-> (the-as (inline-array game-save-tag) v1-0) 0 user-uint64) 1)
        ;;        )
        ;;  )
        (((game-save-elt camera-stick-dir))
         (set! (-> *setting-control* user-default camera-stick-dir)
               (= (-> (the-as (inline-array game-save-tag) v1-0) 0 user-uint64) 1)
               )
         )
        (((game-save-elt play-hints))
         (set! (-> *setting-control* user-default play-hints)
               (= (-> (the-as (inline-array game-save-tag) v1-0) 0 user-uint64) 1)
               )
         )
        (((game-save-elt screenx))
         (set! (-> *setting-control* user-default display-dx)
               (the int (-> (the-as (inline-array game-save-tag) v1-0) 0 user-float0))
               )
         )
        (((game-save-elt screeny))
         (set! (-> *setting-control* user-default display-dy)
               (the int (-> (the-as (inline-array game-save-tag) v1-0) 0 user-float0))
               )
         )
        (else (format 0 "PC PORT: Skipping setting from game save, its stored in the pc-settings now"))
        )
      (set! v1-0 (&+
                   (the-as pointer v1-0)
                   (logand -16 (+ (* (the-as int (-> (the-as (inline-array game-save-tag) v1-0) 0 elt-size))
                                     (-> (the-as (inline-array game-save-tag) v1-0) 0 elt-count)
                                     )
                                  31
                                  )
                           )
                   )
            )
      )
    )
  (when (nonzero? (-> arg0 new-game))
    (case (-> arg0 new-game)
      ((2)
       (logior! (-> *game-info* secrets) (game-secrets hero-mode))
       (logior! (-> *game-info* purchase-secrets) (game-secrets hero-mode))
       )
      )
    (set-continue! this "game-start" #f)
    (set! arg0 arg0)
    (goto cfg-230)
    )
  (let ((s4-0 (the-as object (-> arg0 tag)))
        (s3-0 (new 'stack-no-clear 'array 'uint16 512))
        )
    (dotimes (v1-14 512)
      (set! (-> s3-0 v1-14) (the-as uint -1))
      )
    (while (< (the-as int s4-0) (the-as int (&-> arg0 tag 0 user-int8 (-> arg0 length))))
      (case (-> (the-as game-save-tag s4-0) elt-type)
        (((game-save-elt node-name))
         (dotimes (s2-0 (-> (the-as game-save-tag s4-0) elt-count))
           (dotimes (s1-0 (-> this sub-task-list length))
             (let ((v1-20 (-> this sub-task-list s1-0)))
               (when (string-charp= (-> v1-20 name) (the-as (pointer uint8) (+ (+ (* s2-0 64) 16) (the-as int s4-0))))
                 (set! (-> s3-0 s2-0) (the-as uint s1-0))
                 (goto cfg-45)
                 )
               )
             )
           (label cfg-45)
           )
         )
        (((game-save-elt base-time))
         )
        (((game-save-elt game-time))
         (load! (-> *display* game-clock) (the-as (pointer uint64) (-> (the-as (inline-array game-save-tag) s4-0) 1)))
         (set! (-> *game-info* kiosk-timeout) (the-as uint (-> *display* game-clock frame-counter)))
         )
        (((game-save-elt total-game-time))
         (load!
           (-> *display* total-game-clock)
           (the-as (pointer uint64) (-> (the-as (inline-array game-save-tag) s4-0) 1))
           )
         (set! (-> *game-info* kiosk-timeout) (the-as uint (-> *display* game-clock frame-counter)))
         )
        (((game-save-elt continue))
         (format (clear *temp-string*) "~G" (-> (the-as (inline-array game-save-tag) s4-0) 1))
         (set-continue! this *temp-string* #f)
         )
        (((game-save-elt life))
         (set! (-> this life) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt buzzer-total))
         (set! (-> this buzzer-total) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt fuel-cell))
         (set! (-> this fuel) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt death-movie-tick))
         (set! (-> this death-movie-tick) (the-as int (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt skill))
         (set! (-> this skill) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt skill-total))
         (set! (-> this skill-total) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt gem))
         (set! (-> this gem) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt gem-total))
         (set! (-> this gem-total) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt karma))
         (set! (-> this karma) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt eco-pill-dark))
         (set! (-> this eco-pill-dark) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt eco-pill-dark-total))
         (set! (-> this eco-pill-dark-total) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt shield))
         (set! (-> this shield) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt features))
         (set! (-> this features) (the-as game-feature (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt secrets))
         (set! (-> this secrets) (the-as game-secrets (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt purchase-secrets))
         (set! (-> this purchase-secrets)
               (the-as game-secrets (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64))
               )
         )
        (((game-save-elt gun-type))
         (set! (-> this gun-type) (the-as pickup-type (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt gun-ammo))
         (let ((v1-67 (min 4 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))))
           (dotimes (a0-89 v1-67)
             (set! (-> this gun-ammo a0-89)
                   (the-as float (-> (the-as (inline-array game-save-tag) s4-0) 1 user-object a0-89))
                   )
             )
           )
         )
        (((game-save-elt money))
         (set! (-> this money) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt money-total))
         (set! (-> this money-total) (-> (the-as (inline-array game-save-tag) s4-0) 0 user-float0))
         )
        (((game-save-elt level-open-list))
         (let ((v1-73 (min 32 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))))
           (dotimes (a0-97 v1-73)
             (set! (-> this level-opened a0-97)
                   (-> (the-as (pointer uint8) (&+ (the-as pointer (-> (the-as (inline-array game-save-tag) s4-0) 1)) a0-97)))
                   )
             )
           )
         )
        (((game-save-elt perm-list))
         (let ((s2-3 (min (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count) (-> this perm-list allocated-length)))
               )
           (set! (-> this perm-list length) s2-3)
           (dotimes (s1-1 s2-3)
             (mem-copy!
               (the-as pointer (-> this perm-list data s1-1))
               (the-as pointer (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* s1-1 16)))
               16
               )
             )
           )
         )
        (((game-save-elt task-list))
         (let ((s2-5
                 (min (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count) (-> this task-perm-list allocated-length))
                 )
               )
           (set! (-> this task-perm-list length) s2-5)
           (dotimes (s1-2 s2-5)
             (mem-copy!
               (the-as pointer (-> this task-perm-list data s1-2))
               (the-as pointer (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* s1-2 16)))
               16
               )
             )
           )
         )
        (((game-save-elt talker-state))
         (let ((v1-95 (-> this unknown-pad6 allocated-length))
               (a0-108 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))
               )
           (dotimes (a1-57 v1-95)
             (cond
               ((>= a1-57 a0-108)
                (set! (-> this unknown-pad6 a1-57) (the-as uint 0))
                0
                )
               (else
                 (set! (-> this unknown-pad6 a1-57) (-> (the-as (inline-array game-save-tag) s4-0) 1 user-uint16 a1-57))
                 )
               )
             )
           )
         )
        (((game-save-elt scores))
         (let ((v1-99 (-> this game-score allocated-length))
               (a0-111 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))
               )
           (dotimes (a1-58 v1-99)
             (if (>= a1-58 a0-111)
                 (set! (-> this game-score a1-58) 0.0)
                 (set! (-> this game-score a1-58)
                       (the-as float (-> (the-as (inline-array game-save-tag) s4-0) 1 user-object a1-58))
                       )
                 )
             )
           )
         )
        (((game-save-elt bigmap-data))
         (initialize *bigmap*)
         (let ((v1-104 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count)))
           (set! (-> *bigmap* compressed-next-index) (the-as uint v1-104))
           (dotimes (a0-116 v1-104)
             (set! (-> (the-as (pointer uint8) (+ (-> *bigmap* compressed-data) a0-116)) 0)
                   (-> (the-as (pointer uint8) (&+ (the-as pointer (-> (the-as (inline-array game-save-tag) s4-0) 1)) a0-116)))
                   )
             )
           )
         )
        (((game-save-elt bigmap-offsets))
         (let ((a1-62 20)
               (v1-107 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))
               (a0-120 (-> *bigmap* compressed-data))
               )
           (cond
             ((= a1-62 v1-107)
              (dotimes (a1-63 v1-107)
                (let ((a2-27 (-> (the-as (inline-array game-save-tag) s4-0) 1 user-object a1-63)))
                  (if (< (the-as int a2-27) 0)
                      (set! (-> *bigmap* compressed-masks a1-63) (the-as (pointer int8) #f))
                      (set! (-> *bigmap* compressed-masks a1-63) (the-as (pointer int8) (+ a0-120 (the-as uint a2-27))))
                      )
                  )
                )
              )
             (else
               (initialize *bigmap*)
               )
             )
           )
         )
        (((game-save-elt auto-save-count))
         (set! (-> this auto-save-count) (the-as int (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt total-deaths))
         (set! (-> this total-deaths) (the-as int (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt total-trys))
         (set! (-> this total-trys) (the-as int (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt continue-deaths))
         (set! (-> this continue-deaths) (the-as int (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt task-deaths))
         (set! (-> this task-deaths) (the-as int (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt game-start-time))
         (set! (-> this game-start-time)
               (the-as time-frame (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64))
               )
         )
        (((game-save-elt continue-time))
         (set! (-> this continue-time)
               (the-as time-frame (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64))
               )
         )
        (((game-save-elt death-time))
         (set! (-> this death-time) (the-as time-frame (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt hit-time))
         (set! (-> this hit-time) (the-as time-frame (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64)))
         )
        (((game-save-elt task-pickup-time))
         (set! (-> this task-pickup-time)
               (the-as time-frame (-> (the-as (inline-array game-save-tag) s4-0) 0 user-uint64))
               )
         )
        (((game-save-elt enter-level-time))
         (let ((v1-123 (min 32 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))))
           (dotimes (a0-145 v1-123)
             (set! (-> this task-enter-times a0-145)
                   (the-as
                     time-frame
                     (-> (the-as (pointer uint64) (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* a0-145 8)))
                         )
                     )
                   )
             )
           )
         )
        (((game-save-elt in-level-time))
         (let ((v1-127 (min 32 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))))
           (dotimes (a0-149 v1-127)
             (set! (-> this task-in-times a0-149)
                   (the-as
                     time-frame
                     (-> (the-as (pointer uint64) (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* a0-149 8)))
                         )
                     )
                   )
             )
           )
         )
        (((game-save-elt task-complete-time))
         (let ((v1-131 (min 32 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))))
           (dotimes (a0-153 v1-131)
             (set! (-> this unknown-array1 a0-153)
                   (the-as
                     time-frame
                     (-> (the-as (pointer uint64) (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* a0-153 8)))
                         )
                     )
                   )
             )
           )
         )
        (((game-save-elt task-start-time))
         (let ((v1-135 (min 32 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count))))
           (dotimes (a0-157 v1-135)
             (set! (-> this task-close-times a0-157)
                   (the-as
                     time-frame
                     (-> (the-as (pointer uint64) (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* a0-157 8)))
                         )
                     )
                   )
             )
           )
         )
        (((game-save-elt task-node-list))
         (let ((s2-6 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count)))
           (dotimes (s1-3 s2-6)
             (if (and (nonzero? (-> (the-as (pointer uint8) (&+ (the-as pointer (-> (the-as (inline-array game-save-tag) s4-0) 1)) s1-3)))
                                )
                      (>= (-> (the-as (pointer int16) (&+ s3-0 (* s1-3 2)))) 0)
                      )
                 (close! (-> this sub-task-list (-> (the-as (pointer int16) (&+ s3-0 (* s1-3 2))))) 'event)
                 )
             )
           )
         )
        (((game-save-elt node-death-count))
         (let ((v1-155 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count)))
           (dotimes (a0-164 v1-155)
             (if (>= (-> (the-as (pointer int16) (&+ s3-0 (* a0-164 2)))) 0)
                 (set! (-> this sub-task-list (-> (the-as (pointer int16) (&+ s3-0 (* a0-164 2)))) death-count)
                       (-> (the-as (inline-array game-save-tag) s4-0) 1 user-uint16 a0-164)
                       )
                 )
             )
           )
         )
        (((game-save-elt node-gem-count))
         (let ((v1-158 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count)))
           (dotimes (a0-167 v1-158)
             (if (>= (-> (the-as (pointer int16) (&+ s3-0 (* a0-167 2)))) 0)
                 (set! (-> this sub-task-list (-> (the-as (pointer int16) (&+ s3-0 (* a0-167 2)))) gem-count)
                       (-> (the-as (inline-array game-save-tag) s4-0) 1 user-uint16 a0-167)
                       )
                 )
             )
           )
         )
        (((game-save-elt node-skill-count))
         (let ((v1-161 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count)))
           (dotimes (a0-170 v1-161)
             (if (>= (-> (the-as (pointer int16) (&+ s3-0 (* a0-170 2)))) 0)
                 (set! (-> this sub-task-list (-> (the-as (pointer int16) (&+ s3-0 (* a0-170 2)))) skill-count)
                       (-> (the-as (inline-array game-save-tag) s4-0) 1 user-uint16 a0-170)
                       )
                 )
             )
           )
         )
        (((game-save-elt node-close-time))
         (let ((v1-165 (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count)))
           (dotimes (a0-172 v1-165)
             (if (>= (-> (the-as (pointer int16) (&+ s3-0 (* a0-172 2)))) 0)
                 (set! (-> this sub-task-list (-> (the-as (pointer int16) (&+ s3-0 (* a0-172 2)))) close-time)
                       (the-as
                         time-frame
                         (-> (the-as (pointer uint64) (+ (the-as uint (-> (the-as (inline-array game-save-tag) s4-0) 1)) (* a0-172 8)))
                             )
                         )
                       )
                 )
             )
           )
         )
        )
      (set! s4-0 (&+
                   (the-as pointer s4-0)
                   (logand -16 (+ (* (the-as int (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-size))
                                     (-> (the-as (inline-array game-save-tag) s4-0) 0 elt-count)
                                     )
                                  31
                                  )
                           )
                   )
            )
      )
    )
  (dotimes (s4-1 (-> *level* length))
    (let ((a1-106 (-> *level* level s4-1)))
      (if (= (-> a1-106 status) 'active)
          (copy-perms-to-level! this a1-106)
          )
      )
    )
  (label cfg-230)
  arg0
  )

(defmethod save-to-file ((this game-save) (arg0 string))
  (let ((s5-0 (new 'stack 'file-stream arg0 'write)))
    (file-stream-write s5-0 (&-> this type) (+ (-> this type size) (-> this length)))
    (file-stream-close s5-0)
    )
  this
  )

(defmethod load-from-file ((this game-save) (arg0 string))
  (let ((s5-0 (new 'stack 'file-stream arg0 'read)))
    (let ((s3-0 (file-stream-length s5-0))
          (s4-0 (-> this allocated-length))
          )
      (cond
        ((>= (asize-of this) s3-0)
         (cond
           ((= (file-stream-read s5-0 (&-> this type) s3-0) s3-0)
            (set! (-> this type) game-save)
            )
           (else
             (format 0 "ERROR: SAVEGAME: save file ~A did not read correctly.~%" s5-0)
             (set! (-> this length) 0)
             0
             )
           )
         )
        (else
          (format 0 "ERROR: SAVEGAME: save file ~A is too big~%" s5-0)
          )
        )
      (set! (-> this allocated-length) s4-0)
      )
    (when (!= (-> this version) 3)
      (format
        0
        "ERROR: SAVEGAME: save file ~A was version ~d, but only ~d is supported.~%"
        s5-0
        (-> this version)
        3
        )
      (set! (-> this length) 0)
      0
      )
    (file-stream-close s5-0)
    )
  this
  )

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

(deftype auto-save (process)
  ((card        int32)
   (slot        int32)
   (which       int32)
   (buffer      kheap)
   (mode        symbol)
   (result      mc-status-code)
   (save        game-save)
   (info        mc-slot-info  :inline)
   (notify      handle)
   (force       symbol)
   (state-time  time-frame)
   (icon        hud-sprite    :inline :offset 480)
   )
  (:state-methods
    get-heap
    get-card
    format-card
    unformat-card
    create-file
    save
    restore
    (error mc-status-code)
    done
    )
  )


(defbehavior auto-save-post auto-save ()
  (when (and (= *cheat-mode* 'debug) (cpad-hold? 0 l3))
    (let ((gp-0
            (new 'stack 'font-context *font-default-matrix* 32 320 0.0 (font-color default) (font-flags shadow kerning))
            )
          )
      (let ((v1-5 gp-0))
        (set! (-> v1-5 width) (the float 440))
        )
      (let ((v1-6 gp-0))
        (set! (-> v1-6 height) (the float 80))
        )
      (set! (-> gp-0 flags) (font-flags shadow kerning))
      (format (clear *temp-string*) "~S / ~S ~D~%" (-> self mode) (-> self state name) (-> self which))
      (print-game-text *temp-string* gp-0 #f 44 (bucket-id progress))
      )
    )
  (when (and (= (-> self mode) 'auto-save) (not (and (-> self next-state) (= (-> self next-state name) 'done))))
    (let ((gp-1
            (new 'stack 'font-context *font-default-matrix* 20 80 0.0 (font-color default) (font-flags shadow kerning))
            )
          )
      (let ((v1-17 gp-1))
        (set! (-> v1-17 scale) 0.8)
        )
      (let ((v1-18 gp-1))
        (set! (-> v1-18 width) (the float 432))
        )
      (let ((v1-19 gp-1))
        (set! (-> v1-19 height) (the float 20))
        )
      (set! (-> gp-1 flags) (font-flags shadow kerning middle middle-vert large))
      (when (and (>= 1 (-> *game-info* auto-save-count)) (-> self next-state) (= (-> self next-state name) 'save))
        (print-game-text
          (lookup-text! *common-text* (text-id progress-memcard-saving-data) #f)
          gp-1
          #f
          44
          (bucket-id progress)
          )
        (set! (-> gp-1 origin x) 20.0)
        (set! (-> gp-1 origin y) 130.0)
        (let ((v1-30 gp-1))
          (set! (-> v1-30 scale) 0.7)
          )
        (let ((v1-31 gp-1))
          (set! (-> v1-31 height) (the float 200))
          )
        (let ((s5-2 print-game-text))
          (format (clear *temp-string*) (lookup-text! *common-text* (text-id progress-memcard-dont-remove) #f) 1)
          (s5-2 *temp-string* gp-1 #f 44 (bucket-id progress))
          )
        )
      )
    (when (< (mod (-> *display* real-clock frame-counter) 300) 270)
      (with-dma-buffer-add-bucket ((s5-3 (-> *display* frames (-> *display* on-screen) global-buf))
                                   (bucket-id progress)
                                   )
        (draw (-> self icon) s5-3 (-> self level))
        )
      )
    )
  )

auto-save-post

(defbehavior auto-save-init-by-other auto-save ((arg0 symbol) (arg1 process) (arg2 int) (arg3 int) (arg4 symbol))
  (when (handle->process (-> *game-info* auto-save-proc))
    (send-event arg1 'notify 'error (mc-status-code no-process))
    (return #f)
    )
  (set! (-> *game-info* auto-save-proc) (process->handle self))
  (set! (-> *game-info* auto-save-status) (mc-status-code ok))
  (stack-size-set! (-> self main-thread) 512)
  (logclear! (-> self mask) (process-mask freeze pause menu progress))
  (set! (-> self card) arg2)
  (set! (-> self which) arg3)
  (set! (-> self buffer) #f)
  (set! (-> self mode) arg0)
  (set! (-> self result) (mc-status-code ok))
  (set! (-> self save) #f)
  (set! (-> self notify) (process->handle arg1))
  (set! (-> self force) arg4)
  (cond
    ((= arg0 'auto-save)
     (if (not (-> *setting-control* user-current auto-save))
         (go-virtual error (mc-status-code no-auto-save))
         )
     (when (and (zero? (-> self card)) (-> *setting-control* user-current auto-save))
       (set! (-> self card) (-> *game-info* auto-save-card))
       (set! (-> self which) (-> *game-info* auto-save-which))
       )
     (set-setting! 'allow-pause #f 0.0 0)
     (apply-settings *setting-control*)
     )
    ((= arg0 'error)
     (set! (-> *setting-control* user-default auto-save) #f)
     (go-virtual error (mc-status-code no-card))
     )
    )
  (set! (-> *setting-control* user-default auto-save) #f)
  (let ((v1-51 (-> self icon color2)))
    (set! (-> v1-51 0) 128)
    (set! (-> v1-51 1) 128)
    (set! (-> v1-51 2) 128)
    (set! (-> v1-51 3) 128)
    )
  (set! (-> self icon pos x) 440)
  (set! (-> self icon pos y) 200)
  (set! (-> self icon pos z) #xffffff)
  (set! (-> self icon pos w) 0)
  (set! (-> self icon scale-x) 2.0)
  (set! (-> self icon scale-y) 2.0)
  (set! (-> self icon angle) 0.0)
  (set! (-> self icon flags) (the-as uint 0))
  (set! (-> self icon tex) (get-texture checkpoint level-default-minimap))
  (go-virtual get-heap)
  )

(defstate get-heap (auto-save)
  :virtual #t
  :code (behavior ()
    (case (-> self mode)
      (('auto-save)
       (when (zero? (-> *game-info* auto-save-count))
         (set! (-> self event-hook) (-> (method-of-object self error) event))
         (set! (-> self post-hook) #f)
         (set! (-> self state-time) (-> *display* real-clock frame-counter))
         (while (< (- (-> *display* real-clock frame-counter) (-> self state-time)) (seconds 0.2))
           (if (not (progress-allowed?))
               (set! (-> self state-time) (-> *display* real-clock frame-counter))
               )
           (suspend)
           )
         ;; og:preserve-this Disable the auto-save prompt in speedrunner mode, we get it
         (when (not (-> *pc-settings* speedrunner-mode?))
          (activate-progress *dproc* 'icon-info))
         (while *progress-process*
           (suspend)
           )
         (set! (-> self event-hook) #f)
         )
       (+! (-> *game-info* auto-save-count) 1)
       )
      )
    (while (or (< (-> *display* base-clock frame-counter) (-> *game-info* blackout-time))
               (!= (-> *setting-control* user-current bg-a) 0.0)
               (!= (-> *setting-control* user-current bg-a-force) 0.0)
               )
      (suspend)
      )
    (set! (-> self state-time) (-> *display* real-clock frame-counter))
    (let ((a0-9 (reserve-alloc *art-control*)))
      (while (not a0-9)
        (if (>= (- (-> *display* real-clock frame-counter) (-> self state-time)) (seconds 60))
            (go-virtual error (mc-status-code no-memory))
            )
        (suspend)
        (set! a0-9 (reserve-alloc *art-control*))
        )
      (set! (-> self buffer) a0-9)
      )
    (format #t "got buffer #x~X~%" (-> self buffer base))
    (go-virtual get-card)
    )
  :post auto-save-post
  )

(defstate get-card (auto-save)
  :virtual #t
  :code (behavior ()
    (label cfg-0)
    (mc-get-slot-info (-> self slot) (-> self info))
    (when (zero? (-> self info known))
      (suspend)
      (goto cfg-0)
      )
    (cond
      ((zero? (-> self info handle))
       (go-virtual error (mc-status-code no-card))
       )
      ((zero? (-> self card))
       (set! (-> self card) (-> self info handle))
       )
      ((!= (-> self info handle) (-> self card))
       (go-virtual error (mc-status-code bad-handle))
       )
      )
    (case (-> self mode)
      (('save 'auto-save)
       (if (-> self force)
           (go-virtual format-card)
           (go-virtual save)
           )
       )
      (('save-last)
       (set! (-> self which) (-> self info last-file))
       (if (= (-> self which) -1)
           (go-virtual error (mc-status-code no-last))
           (go-virtual save)
           )
       )
      (('restore)
       (go-virtual restore)
       )
      (('format-card)
       (go-virtual format-card)
       )
      (('unformat-card)
       (go-virtual unformat-card)
       )
      (('create-file)
       (go-virtual create-file)
       )
      (else
        (go-virtual done)
        )
      )
    )
  :post auto-save-post
  )

(defstate format-card (auto-save)
  :virtual #t
  :code (behavior ()
    (b! (nonzero? (-> self info formatted)) cfg-11 :delay (empty-form))
    (label cfg-1)
    (set! (-> self result) (mc-format (-> self card)))
    (when (!= (-> self result) (mc-status-code ok))
      (suspend)
      (goto cfg-1)
      )
    (until #f
      (set! (-> self result) (mc-check-result))
      (let ((v1-4 (-> self result)))
        (b! (nonzero? v1-4) cfg-5 :delay (nop!))
        (b! #t cfg-10 :delay (nop!))
        (label cfg-5)
        (b! (= v1-4 (mc-status-code format-failed)) cfg-1 :delay (nop!))
        (nop!)
        (if (= v1-4 (mc-status-code ok))
            (goto cfg-11)
            (go-virtual error (-> self result))
            )
        )
      (label cfg-10)
      (suspend)
      )
    #f
    (label cfg-11)
    (case (-> self mode)
      (('create-file 'save 'save-last 'auto-save 'restore)
       (label cfg-21)
       (mc-get-slot-info (-> self slot) (-> self info))
       (when (zero? (-> self info known))
         (suspend)
         (goto cfg-21)
         )
       (go-virtual create-file)
       )
      )
    (go-virtual done)
    )
  :post auto-save-post
  )

(defstate unformat-card (auto-save)
  :virtual #t
  :code (behavior ()
    (when (nonzero? (-> self info formatted))
      (label cfg-1)
      (set! (-> self result) (mc-unformat (-> self card)))
      (when (!= (-> self result) (mc-status-code ok))
        (suspend)
        (goto cfg-1)
        )
      (until #f
        (set! (-> self result) (mc-check-result))
        (case (-> self result)
          (((mc-status-code busy))
           )
          (((mc-status-code ok))
           (goto cfg-10)
           )
          (else
            (go-virtual error (-> self result))
            )
          )
        (suspend)
        )
      #f
      )
    (label cfg-10)
    (go-virtual done)
    )
  :post auto-save-post
  )

(defstate create-file (auto-save)
  :virtual #t
  :code (behavior ()
    (cond
      ((zero? (-> self info formatted))
       (go-virtual error (mc-status-code no-format))
       )
      ((zero? (-> self info inited))
       (if (< (-> self info mem-actual) (-> self info mem-required))
           (go-virtual error (mc-status-code no-space))
           )
       (let ((v1-12 (-> self buffer)))
         (set! (-> v1-12 current) (-> v1-12 base))
         )
       (label cfg-6)
       (set! (-> self result) (mc-create-file (-> self card) (the-as uint (-> self buffer base))))
       (when (!= (-> self result) (mc-status-code ok))
         (suspend)
         (goto cfg-6)
         )
       (until #f
         (set! (-> self result) (mc-check-result))
         (case (-> self result)
           (((mc-status-code busy))
            )
           (((mc-status-code ok))
            (goto cfg-15)
            )
           (else
             (go-virtual error (-> self result))
             )
           )
         (suspend)
         )
       #f
       )
      )
    (label cfg-15)
    (case (-> self mode)
      (('restore)
       (go-virtual restore)
       )
      (('save 'save-last 'auto-save)
       (label cfg-23)
       (mc-get-slot-info (-> self slot) (-> self info))
       (when (zero? (-> self info known))
         (suspend)
         (goto cfg-23)
         )
       (go-virtual save)
       )
      )
    (go-virtual done)
    )
  :post auto-save-post
  )

(defstate save (auto-save)
  :virtual #t
  :code (behavior ()
    (cond
      ((zero? (-> self info formatted))
       (go-virtual error (mc-status-code no-format))
       )
      ((zero? (-> self info inited))
       (go-virtual error (mc-status-code no-file))
       )
      )
    (let ((v1-10 (-> self buffer)))
      (set! (-> v1-10 current) (-> v1-10 base))
      )
    (let ((gp-0 (the-as object loading-level)))
      (set! loading-level (-> self buffer))
      (set! (-> self save) (new 'loading-level 'game-save #x20000))
      (save-game *game-info* (-> self save) "save")
      (set! loading-level (the-as kheap gp-0))
      0
      (label cfg-5)
      (set! (-> self result)
            (mc-save (-> self card) (-> self which) (&-> (-> self save) type) (the-as int (-> self save info-int32)))
            )
      (when (!= (-> self result) (mc-status-code ok))
        (suspend)
        (goto cfg-5)
        )
      (until #f
        (set! (-> self result) (mc-check-result))
        (let ((v1-20 (-> self result)))
          (set! gp-0 (cond
                       ((= v1-20 (mc-status-code busy))
                        #f
                        )
                       ((= v1-20 (mc-status-code ok))
                        (goto cfg-18)
                        gp-0
                        )
                       ((= v1-20 (mc-status-code write-error))
                        (suspend)
                        gp-0
                        )
                       (else
                         (case (-> self mode)
                           (('auto-save)
                            (seekl! (-> *game-info* auto-save-count) 0 1)
                            )
                           )
                         (go-virtual error (-> self result))
                         )
                       )
                )
          )
        (suspend)
        )
      )
    #f
    (label cfg-18)
    (go-virtual done)
    )
  :post auto-save-post
  )

(defstate restore (auto-save)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('progress-allowed?)
       #t
       )
      (('done)
       (go-virtual done)
       )
      )
    )
  :code (behavior ()
    (local-vars (gp-0 object))
    (cond
      ((zero? (-> self info formatted))
       (go-virtual error (mc-status-code no-format))
       )
      ((zero? (-> self info inited))
       (go-virtual error (mc-status-code no-file))
       )
      )
    (let ((v1-10 (-> self buffer)))
      (set! (-> v1-10 current) (-> v1-10 base))
      )
    (if (zero? (-> self info file (-> self which) present))
        (go-virtual error (mc-status-code no-save))
        )
    (label cfg-6)
    (set! (-> self result) (mc-load (-> self card) (-> self which) (-> self buffer base)))
    (when (!= (-> self result) (mc-status-code ok))
      (suspend)
      (goto cfg-6)
      )
    (until #f
      (set! (-> self result) (mc-check-result))
      (let ((v1-22 (-> self result)))
        (set! gp-0 (cond
                     ((= v1-22 (mc-status-code busy))
                      #f
                      )
                     ((= v1-22 (mc-status-code ok))
                      (goto cfg-19)
                      gp-0
                      )
                     ((= v1-22 (mc-status-code read-error))
                      (suspend)
                      gp-0
                      )
                     ((= v1-22 (mc-status-code new-game))
                      (go-virtual error (mc-status-code no-save))
                      )
                     (else
                       (go-virtual error (-> self result))
                       )
                     )
              )
        )
      (suspend)
      )
    #f
    (label cfg-19)
    (set! (-> self save) (the-as game-save (&+ (-> self buffer base) 4)))
    (let ((v1-34 (-> self save)))
      (set! (-> v1-34 type) game-save)
      (if (!= (-> v1-34 version) 3)
          (go-virtual error (mc-status-code bad-version))
          )
      )
    (set-setting! 'music-volume 'abs 0.0 0)
    (set-setting! 'sfx-volume 'abs 0.0 0)
    (set! (-> *game-info* mode) 'play)
    (initialize! *game-info* 'game (-> self save) (the-as string #f))
    (set-master-mode 'game)
    (add-setting! 'process-mask 'set 0.0 (process-mask progress))
    (apply-settings *setting-control*)
    (sleep-code)
    (go-virtual done)
    )
  :post auto-save-post
  )

(defstate error (auto-save)
  :virtual #t
  :event (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
    (case message
      (('progress-allowed?)
       #t
       )
      (('die)
       (deactivate self)
       )
      )
    )
  :code (behavior ((arg0 mc-status-code))
    (if (-> self buffer)
        (reserve-free *art-control* (-> self buffer))
        )
    (set! (-> self result) arg0)
    (let ((s5-0 *auto-save-info*))
      (mem-copy! (the-as pointer s5-0) (the-as pointer (-> self info)) 300)
      (send-event (handle->process (-> self notify)) 'notify 'error (-> self result) s5-0)
      )
    (let ((t9-3 format)
          (a0-8 #t)
          (a1-3 "SAVE ERROR: ~A~%")
          (v1-13 (-> self result))
          )
      (t9-3 a0-8 a1-3 (cond
                        ((= v1-13 (mc-status-code bad-version))
                         "bad-version"
                         )
                        ((= v1-13 (mc-status-code no-save))
                         "no-save"
                         )
                        ((= v1-13 (mc-status-code no-last))
                         "no-last"
                         )
                        ((= v1-13 (mc-status-code no-space))
                         "no-space"
                         )
                        ((= v1-13 (mc-status-code internal-error))
                         "internal-error"
                         )
                        ((= v1-13 (mc-status-code no-memory))
                         "no-memory"
                         )
                        ((= v1-13 (mc-status-code bad-handle))
                         "bad-handle"
                         )
                        ((= v1-13 (mc-status-code busy))
                         "busy"
                         )
                        ((= v1-13 (mc-status-code write-error))
                         "write-error"
                         )
                        ((= v1-13 (mc-status-code read-error))
                         "read-error"
                         )
                        ((= v1-13 (mc-status-code no-card))
                         "no-card"
                         )
                        ((= v1-13 (mc-status-code no-format))
                         "no-format"
                         )
                        ((= v1-13 (mc-status-code ok))
                         "ok"
                         )
                        ((= v1-13 (mc-status-code no-process))
                         "no-process"
                         )
                        ((= v1-13 (mc-status-code no-auto-save))
                         "no-auto-save"
                         )
                        ((= v1-13 (mc-status-code no-file))
                         "no-file"
                         )
                        ((= v1-13 (mc-status-code format-failed))
                         "format-failed"
                         )
                        ((= v1-13 (mc-status-code new-game))
                         "new-game"
                         )
                        (else
                          "*unknown*"
                          )
                        )
            )
      )
    (if (= (-> self result) (mc-status-code no-auto-save))
        (return #f)
        )
    (case (-> self mode)
      (('auto-save 'error)
       (set! (-> self state-time) (-> *display* real-clock frame-counter))
       (set! (-> *game-info* auto-save-status) arg0)
       (while (< (- (-> *display* real-clock frame-counter) (-> self state-time)) (seconds 0.2))
         (if (not (progress-allowed?))
             (set! (-> self state-time) (-> *display* real-clock frame-counter))
             )
         (suspend)
         )
       (if (= arg0 (mc-status-code no-card))
           (activate-progress *dproc* 'card-removed)
           (activate-progress *dproc* 'error-auto-saving)
           )
       )
      )
    )
  :post auto-save-post
  )

(defstate done (auto-save)
  :virtual #t
  :code (behavior ()
    (if (and (-> self buffer)
             (or (-> *art-control* reserve-buffer) (nonzero? (-> *art-control* dma-reserve-buffer-count)))
             )
        (reserve-free *art-control* (-> self buffer))
        )
    (set! (-> *game-info* auto-save-status) (mc-status-code ok))
    (case (-> self mode)
      (('save 'save-last 'auto-save 'restore)
       (set! (-> *setting-control* user-default auto-save) #t)
       (set! (-> *game-info* auto-save-card) (-> self card))
       (set! (-> *game-info* auto-save-which) (-> self which))
       )
      )
    (case (-> self mode)
      (('auto-save)
       )
      (else
        (set! (-> *game-info* auto-save-proc) (the-as handle #f))
        )
      )
    (let ((gp-0 *auto-save-info*))
      (mem-copy! (the-as pointer gp-0) (the-as pointer (-> self info)) 300)
      (send-event (handle->process (-> self notify)) 'notify 'done (mc-status-code ok) gp-0)
      )
    )
  :post auto-save-post
  )

;; WARN: Return type mismatch (pointer process) vs (pointer auto-save).
(defun auto-save-command ((arg0 symbol) (arg1 int) (arg2 int) (arg3 process-tree) (arg4 symbol))
  (format #t "AUTO SAVE COMMAND ~S~%" arg0)
  (process-spawn auto-save arg0 arg3 arg1 arg2 arg4 :to *target-pool* :stack *kernel-dram-stack*)
  )

(defun auto-save-check ()
  (when (and (-> *setting-control* user-current auto-save) (not (handle->process (-> *game-info* auto-save-proc))))
    (mc-get-slot-info 0 *auto-save-info*)
    (if (and (nonzero? (-> *auto-save-info* known))
             (or (zero? (-> *auto-save-info* handle)) (!= (-> *auto-save-info* handle) (-> *game-info* auto-save-card)))
             )
        (auto-save-command 'error 0 0 *default-pool* #f)
        )
    )
  0
  )

(defun auto-save-user ()
  (case *kernel-boot-message*
    (('play 'preview)
     (#when PC_PORT
        (pc-settings-save))
     (auto-save-command 'auto-save 0 0 *default-pool* #f)
     )
    )
  )
