home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff216.lzh / Wanderer / src / save.c < prev    next >
C/C++ Source or Header  |  1989-06-02  |  4KB  |  192 lines

  1. #include "wand_head.h"
  2. #include <errno.h>
  3.  
  4. extern char screen[NOOFROWS][ROWLEN+1];
  5. extern int saved_game;
  6.  
  7. extern void crypt_file();
  8. struct    saved_game    {
  9.     short    num;
  10.     short    score;
  11.     short    bell;
  12.     short    maxmoves;
  13.     short    num_monsters;
  14. };
  15.  
  16. struct    save_vars    zz;
  17.  
  18. /* save game and exit - if trouble occurs, return to game */
  19. void
  20. save_game(num, score, bell, maxmoves, start_of_list, tail_of_list)
  21. int    num, *score, *bell, maxmoves;
  22. struct    mon_rec    *start_of_list, *tail_of_list;
  23. {
  24.     char    fname[128], *fp, buffer[40];
  25.     FILE    *fo;
  26.     struct    saved_game    s;
  27.     extern    char    *getenv();
  28.     struct    mon_rec    *mp;
  29.  
  30.     clear();
  31.  
  32.     if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  33.         addstr("Saving game.... Filename ? ");
  34.         refresh();
  35.         fp = fname;
  36.         echo(); CBOFF;
  37.         gets(fp);
  38.         CBON; noecho();
  39.     }
  40.     if ((FILE *)NULL == (fo = fopen(fp, W_BIN))) {
  41.         addstr("\nUnable to save game, press any key.");
  42.         refresh();
  43.         getch();
  44.         return;
  45.     }
  46.  
  47.     s.num = num;
  48.     s.score = *score;
  49.     s.bell = *bell;
  50.     s.maxmoves = maxmoves;
  51.     s.num_monsters = 0;
  52.  
  53.     mp = start_of_list;        /* first entry is dummy    */
  54.     while (mp != tail_of_list) {
  55.         mp = mp->next;
  56.         s.num_monsters++;    /* count them monsters    */
  57.     }
  58.  
  59.     if ( (1 != fwrite((char *)&s, sizeof(s), 1, fo)) ||
  60.          (1 != fwrite((char *)screen, sizeof(screen), 1, fo)) ||
  61.          (1 != fwrite((char *)&zz, sizeof(zz), 1, fo)) )
  62.     {
  63.         goto file_error;
  64.     }
  65.  
  66.     mp = start_of_list;
  67.     while (mp != tail_of_list) {
  68.         /* save them monsters    */
  69.         mp = mp->next;
  70.         if (1 != fwrite((char *)mp, sizeof(struct mon_rec), 1, fo)) {
  71. file_error:
  72.             sprintf(buffer, "Write error on '%s', press any key.", fname);
  73.             addstr(buffer);
  74.             refresh();
  75.             fclose(fo);
  76.             unlink(fname);
  77.             getch();
  78.             return;
  79.         }
  80.     }
  81.  
  82.     fclose(fo);
  83. #ifndef NO_ENCRYPTION
  84.     crypt_file(fp,0);   /* encrpyt the saved game */
  85. #endif
  86.     echo();
  87.     CBOFF;
  88.     endwin();
  89.     exit(0);
  90. }
  91.  
  92. void
  93. restore_game(num, score, bell, maxmoves, start_of_list, tail_of_list)
  94. int    *num, *score, *bell, *maxmoves;
  95. struct    mon_rec    *start_of_list, **tail_of_list;
  96. {
  97.     FILE    *fi;
  98.     struct    saved_game    s;
  99.     struct    mon_rec    *mp, *tmp, tmp_monst;
  100.     char    fname[128], *fp, buffer[80];
  101.     FILE    *fo;
  102.     extern    char    *getenv();
  103.  
  104.     if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  105.         move((LINES-1),0);
  106.         addstr("Restore Filename ? ");
  107.         refresh();
  108.         echo(); CBOFF;
  109.         fp = fname;
  110.         gets(fp);
  111.         CBON; noecho();
  112.     }
  113.     clear();
  114.     refresh();
  115. #ifndef NO_ENCRYPTION
  116.      crypt_file(fp,1);   /*decrypt it*/
  117. #endif
  118.     if ((FILE *)NULL == (fi = fopen(fp, R_BIN))) {
  119.         sprintf(buffer, "Open error on '%s'\n", fp);
  120.         addstr(buffer);
  121.         goto cant_restore;
  122.     }
  123.     if ( (1 != fread((char *)&s, sizeof(s), 1, fi)) ||
  124.          (1 != fread((char *)screen, sizeof(screen), 1, fi)) ||
  125.          (1 != fread((char *)&zz, sizeof(zz), 1, fi)) ) {
  126.         sprintf(buffer, "Read error on '%s'\n", fp);
  127.         addstr(buffer);
  128.         fclose(fi);
  129.         goto cant_restore;
  130.     }
  131.  
  132.     *num = s.num;
  133.     *score = s.score;
  134.     *bell = s.bell;
  135.     *maxmoves = s.maxmoves;
  136.  
  137.     /* free any monsters already on chain, to start clean */
  138.     mp = start_of_list->next;
  139.     while ((mp != NULL) && (mp != start_of_list)) {
  140.         /* free them monsters    */
  141.         tmp = mp;
  142.         mp = mp->next;
  143.         free(tmp);
  144.     }
  145.  
  146.     /* re-initialize the monster list    */
  147.     /* *start_of_list = {0,0,0,0,0,NULL,NULL}; */
  148.     start_of_list->x = 0;
  149.     start_of_list->y = 0;
  150.     start_of_list->mx = 0;
  151.     start_of_list->my = 0;
  152.     start_of_list->under = 0;
  153.     start_of_list->next = (struct mon_rec *)NULL;
  154.     start_of_list->prev = (struct mon_rec *)NULL;
  155.  
  156.     *tail_of_list = start_of_list;
  157.  
  158.     while (s.num_monsters--) {
  159.         /* use make_monster to allocate the monster structures    */
  160.         /* to get all the linking right without even trying    */
  161.         if ((struct mon_rec *)NULL == (mp = make_monster(0, 0))) {
  162.             sprintf(buffer,"Monster alloc error on '%s'\n", fp);
  163.             addstr(buffer);
  164.                         addstr("Try again - it might work.\nBut then,pigs might fly...\n");
  165.             fclose(fi);
  166.             goto cant_restore;
  167.         }
  168.         if (1 != fread((char *)&tmp_monst, sizeof(struct mon_rec), 1, fi)) {
  169.             sprintf(buffer,"Monster read error on '%s'\n", fp);
  170.             addstr(buffer);
  171.             fclose(fi);
  172. cant_restore:
  173.             addstr("Cannot restore game --- sorry.\n");
  174.             addstr("Press any key...");
  175.             refresh();
  176.             getch();
  177.             endwin();
  178.             exit(1);
  179.         }
  180.         /* copy info without trashing links    */
  181.         mp->x     = tmp_monst.x;
  182.         mp->y     = tmp_monst.y;
  183.         mp->mx    = tmp_monst.mx;
  184.         mp->my    = tmp_monst.my;
  185.         mp->under = tmp_monst.under;
  186.     }
  187.  
  188.     fclose(fi);
  189.     unlink(fp);
  190.     saved_game = 1;
  191. }
  192.