home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Issue 2 / Freelog_HS_3_Setp_Oct_Nov_2000_CD2.mdx / Arcade / Orbit / src / save.c < prev    next >
C/C++ Source or Header  |  1999-08-23  |  3KB  |  195 lines

  1. /*
  2.  
  3. ORBIT, a freeware space combat simulator
  4. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. */
  21.  
  22. #include "orbit.h"
  23.  
  24. /*
  25.  *  Routines to save and load missions
  26.  */
  27.  
  28. ReadSaves()
  29. /*
  30.  *  Write the save file
  31.  */
  32. {
  33.     FILE *fd;
  34.  
  35.     nsaves = 0;
  36.  
  37.     /* Open the save file */
  38.     if (NULL == (fd = fopen ("saves.txt", "rt")))
  39.     {
  40.         /* No saves */
  41.         return;
  42.     }
  43.  
  44.     /* Read through the file */
  45.     while (2 == (fscanf (fd, "%s %d", save[nsaves].fn,
  46.             &save[nsaves].time)))
  47.     {
  48.         nsaves++;
  49.  
  50.         /* Too many? */
  51.         if (nsaves >= NSAVES)
  52.         {
  53.             nsaves = NSAVES;
  54.             fclose (fd);
  55.             return;
  56.         }
  57.     }
  58.  
  59.     /* That's it! */
  60.     fclose (fd);
  61.     return;
  62. }
  63.  
  64. WriteSaves()
  65. /*
  66.  *  Write the save file
  67.  */
  68. {
  69.     FILE *fd;
  70.     int s;
  71.  
  72.     /* Open the file */
  73.     if (NULL == (fd = fopen ("saves.txt", "wt")))
  74.     {
  75.         Mprint ("Can't open save file!");
  76.         return;
  77.     }
  78.  
  79.     for (s=0; s<nsaves; s++)
  80.     {
  81.         fprintf (fd, "%s %d\n", save[s].fn, save[s].time);
  82.     }
  83.  
  84.     /* All done */
  85.     fclose (fd);
  86.  
  87.     return;
  88. }
  89.  
  90. AddSave (fn)
  91. char *fn;
  92. /*
  93.  *  Add a mission to the list of saves
  94.  */
  95. {
  96.     int s, sm;
  97.  
  98.     /* Read the file */
  99.     ReadSaves();
  100.  
  101.     /* See if we can find one with same mission name */
  102.     sm = -1;
  103.     for (s=0; s<nsaves; s++)
  104.     {
  105.         if (!strcmp (fn, save[s].fn))
  106.         {
  107.             /* Found one */
  108.             sm = s;
  109.             break;
  110.         }
  111.     }
  112.  
  113.     /* Didn't find one */
  114.     if (sm < 0)
  115.     {
  116.         if (nsaves < NSAVES) nsaves++;
  117.         sm = nsaves - 1;
  118.     }
  119.  
  120.     /* Make room */
  121.     for (s=sm; s>0; s--)
  122.     {
  123.         strcpy (save[s].fn, save[s-1].fn);
  124.         save[s].time = save[s-1].time;
  125.     }
  126.  
  127.     /* Plop this one in */
  128.     strcpy (save[0].fn, fn);
  129.     save[0].time = time(NULL);
  130.  
  131.     /* Write out the file */
  132.     WriteSaves();
  133. }
  134.  
  135. LoadGame()
  136. /*
  137.  *  Sparky wants to load a game
  138.  */
  139. {
  140.     int s;
  141.  
  142.     char buf[4096], buf2[32];
  143.  
  144.     /* Read the saves file */
  145.     ReadSaves();
  146.  
  147.     /* Don't bother if no saves */
  148.     if (nsaves == 0)
  149.     {
  150.         Mprint ("No saved games!");
  151.         return;
  152.     }
  153.  
  154.     /* Pause the game */
  155.     paused = 1;
  156.  
  157.     /* Set the game state */
  158.     state = STATE_LOADGAME;
  159.  
  160.     /* Construct the selection list */
  161.     strcpy (buf, "Select mission to load:\\\\");
  162.  
  163.     for (s=0; s<nsaves; s++)
  164.     {
  165.         sprintf (buf2, "%d) ", s);
  166.         strcat (buf, buf2);
  167.         strcat (buf, save[s].fn);
  168.         strcat (buf, " ");
  169.         sprintf (buf2, "%s", ctime((const time_t *)&save[s].time));
  170.         strcat (buf, buf2);
  171.         strcat (buf, "\\");
  172.     }
  173.  
  174.     Mprint (buf);
  175. }
  176.  
  177. LoadGameByKey (k)
  178. int k;
  179. /*
  180.  *  Load game depending on the key Sparky pressed
  181.  */
  182. {
  183.     if ( (k < 0) || (k >= nsaves) ) return;
  184.  
  185.     /* Set state back to normal */
  186.     state = STATE_NORMAL;
  187.  
  188.     /* Read the mission */
  189.     ReadMission (save[k].fn);
  190.  
  191.     /* That's it! */
  192.     return;
  193. }
  194.  
  195.