home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / main / autosave.c < prev    next >
Text File  |  1998-06-08  |  8KB  |  312 lines

  1. /*
  2.  * $Source: f:/miner/source/main/editor/rcs/autosave.c $
  3.  * $Revision: 2.0 $
  4.  * $Author: john $
  5.  * $Date: 1995/02/27 11:34:53 $
  6.  * 
  7.  * Autosave system: 
  8.  * Saves current mine to disk to prevent loss of work, and support undo.
  9.  * 
  10.  * $Log: autosave.c $
  11.  * Revision 2.0  1995/02/27  11:34:53  john
  12.  * Version 2.0! No anonymous unions, Watcom 10.0, with no need
  13.  * for bitmaps.tbl.
  14.  * 
  15.  * Revision 1.25  1994/11/19  00:04:40  john
  16.  * Changed some shorts to ints.
  17.  * 
  18.  * Revision 1.24  1994/11/17  11:38:59  matt
  19.  * Ripped out code to load old mines
  20.  * 
  21.  * Revision 1.23  1994/07/28  17:00:01  mike
  22.  * fix diagnostic_message erasing.
  23.  * 
  24.  * Revision 1.22  1994/07/21  12:48:28  mike
  25.  * Make time of day a global, fix clock so it doesn't show 10:2 instead of 10:02
  26.  * 
  27.  * Revision 1.21  1994/05/14  17:17:58  matt
  28.  * Got rid of externs in source (non-header) files
  29.  * 
  30.  * Revision 1.20  1994/05/02  18:04:14  yuan
  31.  * Fixed warning.
  32.  * 
  33.  * Revision 1.19  1994/05/02  17:59:04  yuan
  34.  * Changed undo_status into an array rather than malloced pointers.
  35.  * 
  36.  * Revision 1.18  1994/03/16  09:55:48  mike
  37.  * Flashing : in time.
  38.  * 
  39.  * Revision 1.17  1994/02/11  10:27:36  matt
  40.  * Changed 'if !DEMO' to 'ifndef DEMO'
  41.  * 
  42.  * Revision 1.16  1994/02/08  12:43:18  yuan
  43.  * Crippled save game function from demo version
  44.  * 
  45.  * Revision 1.15  1994/02/01  13:27:26  yuan
  46.  * autosave default off.
  47.  * 
  48.  * Revision 1.14  1994/01/05  09:57:37  yuan
  49.  * Fixed calendar/clock problem.
  50.  * 
  51.  * Revision 1.13  1993/12/17  16:09:59  yuan
  52.  * Changed clock font from Red to Black.
  53.  * 
  54.  * Revision 1.12  1993/12/15  13:08:38  yuan
  55.  * Fixed :0x times, so that the 0 shows up.
  56.  * 
  57.  * Revision 1.11  1993/12/15  11:19:52  yuan
  58.  * Added code to display clock in upper right.
  59.  * 
  60.  * Revision 1.10  1993/12/14  21:18:51  yuan
  61.  * Added diagnostic message to display
  62.  * 
  63.  * Revision 1.9  1993/12/14  18:32:59  yuan
  64.  * Added timed autosave code
  65.  * 
  66.  * Revision 1.8  1993/12/13  17:23:25  yuan
  67.  * Fixed bugs with undo.
  68.  * They were caused by badly changed extensions.
  69.  * 
  70.  * Revision 1.7  1993/12/09  16:42:32  yuan
  71.  * Changed extension of temp mines from .mi? -> .mn? 
  72.  * and now to .m? (So it doesn't interfere with .mnu)
  73.  * 
  74.  * Revision 1.6  1993/12/09  16:27:06  yuan
  75.  * Added toggle for autosave
  76.  * 
  77.  * Revision 1.5  1993/11/29  19:46:32  matt
  78.  * Changed includes
  79.  * 
  80.  * Revision 1.4  1993/11/11  15:54:11  yuan
  81.  * Added display message for Undo...
  82.  * Eg. Attach Segment UNDONE.
  83.  * 
  84.  * Revision 1.3  1993/11/09  18:53:11  yuan
  85.  * Autosave/Undo works up to 10 moves.
  86.  * 
  87.  * Revision 1.2  1993/11/08  19:14:03  yuan
  88.  * Added Undo command (not working yet)
  89.  * 
  90.  * Revision 1.1  1993/11/08  16:57:59  yuan
  91.  * Initial revision
  92.  * 
  93.  * 
  94.  */
  95.  
  96.  
  97. #pragma off (unreferenced)
  98. static char rcsid[] = "$Id: autosave.c 2.0 1995/02/27 11:34:53 john Exp $";
  99. #pragma on (unreferenced)
  100.  
  101. #include <stdio.h>
  102. #include <stdlib.h>
  103. #include <stdarg.h>
  104. #include <math.h>
  105. #include <string.h>
  106. #include <process.h>
  107. #include <time.h>
  108.  
  109. #include "error.h"
  110.  
  111. #include "inferno.h"
  112. #include "editor.h"
  113. #include "mono.h"
  114. #include "mem.h"
  115. #include "ui.h"
  116.  
  117. #define AUTOSAVE_PERIOD 5            // Number of minutes for timed autosave
  118.  
  119. int        Autosave_count;
  120. int        Autosave_numfiles;
  121. int        Autosave_total;
  122. int        undo_count;
  123. int        original;
  124.  
  125. int        Timer_save_flag=0;
  126. int        Autosave_flag;
  127. int        save_second=-1;
  128.  
  129. char        undo_status[10][100];
  130.  
  131. void init_autosave(void) {
  132. //    int i;
  133.  
  134.     Autosave_count = 0;
  135.     Autosave_numfiles = 0;
  136.      Autosave_flag = 0;
  137.     undo_count = 0;
  138.     //MALLOC( undo_status, char *, 10 );
  139.     //for (i=0; i<10; i++)
  140.     //    MALLOC( undo_status[i], char, 100 );
  141.     autosave_mine(mine_filename);
  142. }
  143.  
  144. void close_autosave(void) {
  145.     int i;
  146.     char *delname, *ext;
  147.  
  148.     for (i=0;i<Autosave_total;i++) {
  149.  
  150.         MALLOC(delname, char, 128);
  151.  
  152.         strcpy ( delname, mine_filename );
  153.         strupr( delname );
  154.         if ( !strcmp(delname, "*.MIN") ) strcpy(delname, "TEMP.MIN");
  155.  
  156.         ext = strstr(delname, ".MIN");
  157.         sprintf( ext, ".M%d", i );
  158.  
  159.         remove( delname );
  160.         free( delname );
  161.     }
  162.     //for (i=0;i<10;i++) free( undo_status[i] );
  163.     //free( undo_status );
  164.  
  165. }
  166.  
  167. void autosave_mine(char *name) {
  168.     char *savename, *ext;
  169.  
  170.     if (Autosave_flag) {
  171.     
  172.         MALLOC(savename, char, 128);
  173.     
  174.         strcpy ( savename, name );
  175.         strupr( savename );
  176.         if ( !strcmp(savename, "*.MIN") ) strcpy(savename, "TEMP.MIN");
  177.     
  178.         ext = strstr(savename, ".MIN");
  179.         sprintf( ext, ".M%d", Autosave_count );
  180.     
  181.         //mprintf( 0, "Autosave: %s\n", savename );
  182.         med_save_mine( savename );
  183.         Autosave_count++;
  184.         if (undo_count > 0) undo_count--;
  185.         if (Autosave_count > 9) Autosave_count -= 10;
  186.         if (Autosave_numfiles < 10)
  187.             Autosave_numfiles++;
  188.         if (Autosave_total < 10)
  189.             Autosave_total++;
  190.     
  191.         free(savename);
  192.  
  193.     }
  194.  
  195. }
  196.  
  197.  
  198. void print_clock( int seconds, char message[10] ) {
  199.     int w,h,aw;
  200.     char    *p;
  201.  
  202.     //    Make colon flash
  203.     if (seconds & 1)
  204.         if ((p = strchr(message, ':')) != NULL)
  205.             *p = ' ';
  206.  
  207.     gr_set_current_canvas( NULL );
  208.     gr_set_fontcolor( CBLACK, CGREY );
  209.     gr_get_string_size( message, &w, &h, &aw );
  210.     gr_setcolor( CGREY );
  211.     gr_rect( 700, 0, 799, h+1 );
  212.     gr_string( 700, 0, message );
  213.     gr_set_fontcolor( CBLACK, CWHITE );
  214. }
  215.  
  216. static char the_time[14];    // changed from 10, I don't think that was long enough
  217.  
  218. void clock_message( int seconds, char *format, ... ) {
  219.     va_list ap;
  220.  
  221.     va_start(ap, format);
  222.       vsprintf(the_time, format, ap);
  223.     va_end(ap);
  224.  
  225.       print_clock(seconds, the_time);
  226. }
  227.  
  228.  
  229. struct tm Editor_time_of_day;
  230.  
  231. void set_editor_time_of_day()
  232. {
  233.     time_t     ltime;
  234.  
  235.     time( <ime );
  236.     Editor_time_of_day = *localtime( <ime );
  237. }
  238.  
  239. void TimedAutosave(char *name) 
  240. {
  241.     int          month,day,hour,minute,second;
  242.  
  243.     month = (Editor_time_of_day.tm_mon) + 1;
  244.     day =    Editor_time_of_day.tm_mday;
  245.     minute = Editor_time_of_day.tm_min;
  246.     hour = Editor_time_of_day.tm_hour;
  247.     second = Editor_time_of_day.tm_sec;
  248.  
  249.     if (hour > 12) hour-=12;
  250.  
  251.     if (second!=save_second) {
  252.         save_second = second;
  253.         clock_message(second, "%d/%d %d:%02d", month, day, hour, minute);
  254.     }
  255.     
  256.  
  257. #ifndef DEMO
  258.     if (minute%AUTOSAVE_PERIOD != 0)
  259.         Timer_save_flag = 1;
  260.  
  261.     if ((minute%AUTOSAVE_PERIOD == 0) && (Timer_save_flag)) {
  262.         time_t     ltime;
  263.  
  264.         autosave_mine(name);
  265.         Timer_save_flag = 0;
  266.         time( <ime );
  267.        diagnostic_message("Mine Autosaved at %s\n", ctime(<ime));
  268.     }
  269. #endif
  270.  
  271. }
  272.  
  273.  
  274. int undo( void ) {
  275.     Int3();
  276.     return 2;
  277.  
  278. //@@    char *loadname, *ext;
  279. //@@    if (undo_count == 0) original = Autosave_count;
  280. //@@
  281. //@@     if (!Autosave_flag) 
  282. //@@         return 2;
  283. //@@
  284. //@@    if (Autosave_numfiles > 1) {
  285. //@@
  286. //@@        MALLOC(loadname, char, 128);
  287. //@@
  288. //@@        strcpy ( loadname, mine_filename );
  289. //@@        strupr( loadname );
  290. //@@        if ( !strcmp(loadname, "*.MIN") ) strcpy(loadname, "TEMP.MIN");
  291. //@@
  292. //@@        undo_count++;
  293. //@@        Autosave_count = original - undo_count;
  294. //@@        if (Autosave_count < 0) Autosave_count = Autosave_count+10;
  295. //@@        Autosave_numfiles--;
  296. //@@        //mprintf(0, "u=%d  a=%d  o=%d  num=%d\n", undo_count, Autosave_count, original, Autosave_numfiles);
  297. //@@
  298. //@@        ext = strstr(loadname, ".MIN");
  299. //@@        if (Autosave_count == 0) sprintf( ext, ".M9" );
  300. //@@            else sprintf( ext, ".M%d", Autosave_count-1 );
  301. //@@        //mprintf( 0, "Loading: %s\n", loadname );
  302. //@@        med_load_mine( loadname );
  303. //@@
  304. //@@        free(loadname);
  305. //@@        return 0;
  306. //@@     }
  307. //@@     else return 1;
  308. //@@     //diagnostic_message("Can't undo\n");
  309.  
  310. }
  311.  
  312.