// old autosave system (superseded by autosaved xmaps)
// (write "load_autosave" into your autoexec.cfg to load)

persistidents 0  // don't save default values

checkinit mapstartalways [numAutoSaves = 0; autoSaveCheck]
checkinit onNewMap [numAutoSaves = 0; autoSaveCheck]
alias autosavemap 0           // Autosavemap system disabled by default
alias timeBetAutoSaves 300000 // Autosave once every 5 minutes.
alias numAutoSaveBackups 3    // Make a map .bak every 3 autosaves (15 minutes) by default
tempalias numAutoSaves 0

const autoSaveCheck [ if $autosavemap [ sleep $timeBetAutoSaves doAutoSave ] ]

const execAutoSave [
  += numAutoSaves 1
  push oldmapbackups $mapbackupsonsave
  if (|| (!= $numAutoSaveBackups $numAutoSaves) (= $numAutoSaveBackups 0)) [ mapbackupsonsave 0 ] [ numAutoSaves = 0 ] // Temporarily turn off map backups to avoid clutter.
  savemap (curmap)
  mapbackupsonsave (pop oldmapbackups)
  echo "\f1The map was automatically saved. \f5Next auto-save in" (divf $timeBetAutoSaves 60000) minutes.
]

const doAutoSave [
  if $autosavemap [
    if (&& (! (strstr $__defaultmaps (curmap))) (strlen (curmap))) [
      if $editing execAutoSave [
        if (= $gamemode (modenum coop)) execAutoSave
      ]
    ]
  ]
  autoSaveCheck
]

showedBAKinfo = 0

// amend settings menu
newmenu [Editing settings]
menuitemcheckbox  "Enable auto save map: " "$autosavemap" [ autosavemap = $arg1 ]
menuitemslider    "Time between auto saves (in minutes): " 2 60 "(div $timeBetAutoSaves 60000)" 1 [ timeBetAutoSaves = (* $arg1 60000) ]
menuitemtextinput "Make a map backup (.BAK) every: " "$numAutoSaveBackups" [ numAutoSaveBackups = $arg1 ] [ if (! $showedBAKinfo) [ echo This setting effects the number of autosavemaps between making a map backup file. "(.BAK)"; showedBAKinfo = 1 ] ] 4
