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