@@ -133,6 +133,68 @@ void Editor::update() {
133133 }
134134}
135135
136+ int Editor::autosave (const char * /* desc*/ ) {
137+ if (autosaveDisabled || !_lastActiveViewport)
138+ return 0 ;
139+
140+ Fred_mission_save save;
141+ save.set_always_save_display_names (_lastActiveViewport->Always_save_display_names );
142+ save.set_view_pos (_lastActiveViewport->view_pos );
143+ save.set_view_orient (_lastActiveViewport->view_orient );
144+ save.set_fred_alt_names (Fred_alt_names);
145+ save.set_fred_callsigns (Fred_callsigns);
146+
147+ // autosave_mission_file() needs a mutable buffer because it reads but doesn't write through it
148+ char backup_name_buf[] = MISSION_BACKUP_NAME ;
149+ if (save.autosave_mission_file (backup_name_buf)) {
150+ undoCount = undoAvailable = 0 ;
151+ return -1 ;
152+ }
153+
154+ undoCount++;
155+ checkUndo ();
156+ return 0 ;
157+ }
158+
159+ int Editor::checkUndo () {
160+ undoAvailable = 0 ;
161+ if (undoCount == 0 )
162+ return 0 ;
163+
164+ // Undo is available when Backup.002 exists (Backup.001 is the current, .002 is what we load)
165+ CFileLocation loc = cf_find_file_location (" Backup.002" , CF_TYPE_MISSIONS );
166+ if (loc.found ) {
167+ undoAvailable = 1 ;
168+ return 1 ;
169+ }
170+ return 0 ;
171+ }
172+
173+ bool Editor::autoload () {
174+ if (!undoAvailable || !_lastActiveViewport)
175+ return false ;
176+
177+ // Load the previous state from Backup.002
178+ if (!loadMission (" Backup.002" , MPF_FAST_RELOAD ))
179+ return false ;
180+
181+ // Delete Backup.001 (the state we just replaced)
182+ cf_delete (" Backup.001" , CF_TYPE_MISSIONS );
183+
184+ // Rotate backups back one slot: .003->.002, .004->.003, etc, .009->.008
185+ char old_name[256 ], new_name[256 ];
186+ for (int i = 1 ; i < MISSION_BACKUP_DEPTH ; i++) {
187+ sprintf (old_name, " Backup.%.3d" , i + 1 );
188+ sprintf (new_name, " Backup.%.3d" , i);
189+ cf_rename (old_name, new_name, CF_TYPE_MISSIONS );
190+ }
191+
192+ if (undoCount > 0 )
193+ undoCount--;
194+ checkUndo ();
195+ return true ;
196+ }
197+
136198void Editor::maybeUseAutosave (std::string& filepath)
137199{
138200 // first, just grab the info of this mission
@@ -431,6 +493,11 @@ bool Editor::loadMission(const std::string& mission_name, int flags) {
431493 scripting::hooks::FredOnMissionLoad->run ();
432494 }
433495
496+ if (!(flags & MPF_FAST_RELOAD )) {
497+ undoCount = undoAvailable = 0 ;
498+ autosave (" nothing" );
499+ }
500+
434501 return true ;
435502}
436503void Editor::clean_up_selections () {
@@ -824,6 +891,8 @@ void Editor::createNewMission() {
824891 clearMission ();
825892 create_player (&vmd_zero_vector, &vmd_identity_matrix);
826893 stars_post_level_init ();
894+ undoCount = undoAvailable = 0 ;
895+ autosave (" nothing" );
827896}
828897void Editor::hideMarkedObjects () {
829898 object* ptr;
0 commit comments