[comp.sources.games.bugs] Wanderer2 save/restore error-recovery enhancements

john@sysint.UUCP (John Abbott) (11/09/88)

The following patches modify the save/restore feature of wanderer so that
error recovery is more graceful.  Instead of kicking the user out of the
game (especially annoying if you are trying to save!), it displays an
error message and lets the user continue.

Bugs:

I really should have added the system error string (e.g. Access Denied).

*** /tmp/,RCSt1a11797	Fri Nov  4 11:24:51 1988
--- wand_head.h	Thu Nov  3 15:49:49 1988
***************
*** 115,122
  extern  int wscreen(int ,int );
  
  /* SAVE.C */
! extern  void save_game(int ,int *,int *,int ,struct mon_rec *,struct mon_rec *);
! extern  void restore_game(int *,int *,int *,int *,struct mon_rec *,struct mon_rec **);
  
  /* SCORES.C */
  extern  int savescore(char *,int ,int ,char *);

--- 115,122 -----
  extern  int wscreen(int ,int );
  
  /* SAVE.C */
! extern  int save_game(int ,int *,int *,int ,struct mon_rec *,struct mon_rec *);
! extern  int restore_game(int *,int *,int *,int *,struct mon_rec *,struct mon_rec **);
  
  /* SCORES.C */
  extern  int savescore(char *,int ,int ,char *);
***************
*** 156,163
  extern  int wscreen();
  
  /* SAVE.C */
! extern  void save_game();
! extern  void restore_game();
  
  /* SCORES.C */
  extern  int savescore();

--- 156,163 -----
  extern  int wscreen();
  
  /* SAVE.C */
! extern  int save_game();
! extern  int restore_game();
  
  /* SCORES.C */
  extern  int savescore();


*** /tmp/,RCSt1a11803	Fri Nov  4 11:25:08 1988
--- game.c	Fri Nov  4 10:30:06 1988
***************
*** 340,347
  	zz.z_xdirection	= xdirection;
  	zz.z_ydirection	= ydirection;
  
! 	save_game(*num, score, bell, maxmoves, &start_of_list, tail_of_list);
! 	/* NOTREACHED */
  	}
  if(ch == 'R')    	/* restore game */
  	{

--- 340,356 -----
  	zz.z_xdirection	= xdirection;
  	zz.z_ydirection	= ydirection;
  
! 	if (save_game(*num, score, bell, maxmoves, &start_of_list, tail_of_list))
! 		exit(0);
! 	/* must have had an error saving game */
! 	move(LINES - 2, 0);
! 	addstr("Type any character to continue");
! 	ch = getch();
! 	move(LINES - 2, 0);
! 	clrtoeol();
! 	move(LINES - 1, 0);
! 	clrtoeol();		/* clear error message */
! 	continue;
  	}
  if(ch == 'R')    	/* restore game */
  	{
***************
*** 347,353
  	{
  	extern	struct	save_vars	zz;
  
! 	restore_game(num, score, bell, &maxmoves, &start_of_list, &tail_of_list);
  
  	/* recover important local variables */
  	x		= zz.z_x;

--- 356,372 -----
  	{
  	extern	struct	save_vars	zz;
  
! 	if (!restore_game(num, score, bell, &maxmoves, &start_of_list, &tail_of_list))
! 		{
! 			move(LINES - 2, 0);
! 			addstr("Type any character to continue");
! 			ch = getch();
! 			move(LINES - 2, 0);
! 			clrtoeol();
! 			move(LINES - 1, 0);
! 			clrtoeol();		/* clear error message */
! 			continue;
! 		}
  
  	/* recover important local variables */
  	x		= zz.z_x;


*** /tmp/,RCSt1a11808	Fri Nov  4 11:25:18 1988
--- save.c	Thu Nov  3 15:52:24 1988
***************
*** 2,7
  
  extern char screen[NOOFROWS][ROWLEN+1];
  extern int saved_game;
  
  struct	saved_game	{
  	short	num;

--- 2,8 -----
  
  extern char screen[NOOFROWS][ROWLEN+1];
  extern int saved_game;
+ extern int debug_disp;
  
  struct	saved_game	{
  	short	num;
***************
*** 13,19
  
  struct	save_vars	zz;
  
! void
  save_game(num, score, bell, maxmoves, start_of_list, tail_of_list)
  int	num, *score, *bell, maxmoves;
  struct	mon_rec	*start_of_list, *tail_of_list;

--- 14,20 -----
  
  struct	save_vars	zz;
  
! int
  save_game(num, score, bell, maxmoves, start_of_list, tail_of_list)
  int	num, *score, *bell, maxmoves;
  struct	mon_rec	*start_of_list, *tail_of_list;
***************
*** 24,31
  	extern	char	*getenv();
  	struct	mon_rec	*mp;
  
- 	clear();
- 	refresh();
  	echo();
  	CBOFF;
  	endwin();

--- 25,30 -----
  	extern	char	*getenv();
  	struct	mon_rec	*mp;
  
  	echo();
  	move(LINES - 2, 0);
  	CBOFF;
***************
*** 27,32
  	clear();
  	refresh();
  	echo();
  	CBOFF;
  	endwin();
  

--- 26,32 -----
  	struct	mon_rec	*mp;
  
  	echo();
+ 	move(LINES - 2, 0);
  	CBOFF;
  
  	if ((char *)NULL == (fp = getenv("SAVENAME"))) {
***************
*** 28,34
  	refresh();
  	echo();
  	CBOFF;
- 	endwin();
  
  	if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  		printf("Saving game.... Filename ? ");

--- 28,33 -----
  	echo();
  	move(LINES - 2, 0);
  	CBOFF;
  
  	if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  		addstr("Saving game.... Filename ? ");
***************
*** 31,38
  	endwin();
  
  	if ((char *)NULL == (fp = getenv("SAVENAME"))) {
! 		printf("Saving game.... Filename ? ");
! 		fflush(stdout);
  		fp = fname;
  		gets(fp);
  	}

--- 30,37 -----
  	CBOFF;
  
  	if ((char *)NULL == (fp = getenv("SAVENAME"))) {
! 		addstr("Saving game.... Filename ? ");
! 		refresh();
  		fp = fname;
  		getstr(fp);
  	}
***************
*** 34,40
  		printf("Saving game.... Filename ? ");
  		fflush(stdout);
  		fp = fname;
! 		gets(fp);
  	}
  	if ((FILE *)NULL == (fo = fopen(fp, W_BIN))) {
  		printf("Open error on '%s'n", fp);

--- 33,39 -----
  		addstr("Saving game.... Filename ? ");
  		refresh();
  		fp = fname;
! 		getstr(fp);
  	}
  	move(LINES-2,0);
  	clrtoeol();
***************
*** 36,41
  		fp = fname;
  		gets(fp);
  	}
  	if ((FILE *)NULL == (fo = fopen(fp, W_BIN))) {
  		printf("Open error on '%s'n", fp);
  		exit(1);

--- 35,44 -----
  		fp = fname;
  		getstr(fp);
  	}
+ 	move(LINES-2,0);
+ 	clrtoeol();
+ 	move(LINES-1,0);
+ 
  	if ((FILE *)NULL == (fo = fopen(fp, W_BIN))) {
  		printw("Error opening '%s'", fp);
  		refresh();
***************
*** 37,44
  		gets(fp);
  	}
  	if ((FILE *)NULL == (fo = fopen(fp, W_BIN))) {
! 		printf("Open error on '%s'n", fp);
! 		exit(1);
  	}
  
  	s.num = num;

--- 40,48 -----
  	move(LINES-1,0);
  
  	if ((FILE *)NULL == (fo = fopen(fp, W_BIN))) {
! 		printw("Error opening '%s'", fp);
! 		refresh();
! 		return 0;
  	}
  
  	s.num = num;
***************
*** 57,63
  	     (1 != fwrite((char *)screen, sizeof(screen), 1, fo)) ||
  	     (1 != fwrite((char *)&zz, sizeof(zz), 1, fo)) )
  	{
! 		printf("Write error on '%s'n", fname);
  		fclose(fo);
  		unlink(fname);
  		exit(1);

--- 61,68 -----
  	     (1 != fwrite((char *)screen, sizeof(screen), 1, fo)) ||
  	     (1 != fwrite((char *)&zz, sizeof(zz), 1, fo)) )
  	{
! 		printw("Error writing '%s'", fname);
! 		refresh();
  		fclose(fo);
  		unlink(fname);
  		return 0;
***************
*** 60,66
  		printf("Write error on '%s'n", fname);
  		fclose(fo);
  		unlink(fname);
! 		exit(1);
  	}
  
  	mp = start_of_list;

--- 65,71 -----
  		refresh();
  		fclose(fo);
  		unlink(fname);
! 		return 0;
  	}
  
  	mp = start_of_list;
***************
*** 68,74
  		/* save them monsters	*/
  		mp = mp->next;
  		if (1 != fwrite((char *)mp, sizeof(struct mon_rec), 1, fo)) {
! 			printf("Write error on '%s'n", fname);
  			fclose(fo);
  			unlink(fname);
  			exit(1);

--- 73,80 -----
  		/* save them monsters	*/
  		mp = mp->next;
  		if (1 != fwrite((char *)mp, sizeof(struct mon_rec), 1, fo)) {
! 			printw("Error writing '%s'", fname);
! 			refresh();
  			fclose(fo);
  			unlink(fname);
  			return 0;
***************
*** 71,77
  			printf("Write error on '%s'n", fname);
  			fclose(fo);
  			unlink(fname);
! 			exit(1);
  		}
  	}
  

--- 77,83 -----
  			refresh();
  			fclose(fo);
  			unlink(fname);
! 			return 0;
  		}
  	}
  
***************
*** 77,83
  
  	fclose(fo);
  
! 	exit(0);
  }
  
  void

--- 83,93 -----
  
  	fclose(fo);
  
! 	clear();
! 	refresh();
! 	endwin();
! 
! 	return 1;
  }
  
  int
***************
*** 80,86
  	exit(0);
  }
  
! void
  restore_game(num, score, bell, maxmoves, start_of_list, tail_of_list)
  int	*num, *score, *bell, *maxmoves;
  struct	mon_rec	*start_of_list, **tail_of_list;

--- 90,96 -----
  	return 1;
  }
  
! int
  restore_game(num, score, bell, maxmoves, start_of_list, tail_of_list)
  int	*num, *score, *bell, *maxmoves;
  struct	mon_rec	*start_of_list, **tail_of_list;
***************
*** 93,99
  	extern	char	*getenv();
  
  	if ((char *)NULL == (fp = getenv("SAVENAME"))) {
! 		move((LINES-1),0);
  		addstr("Restore Filename ? ");
  		refresh();
  		echo(); CBOFF;

--- 103,109 -----
  	extern	char	*getenv();
  
  	if ((char *)NULL == (fp = getenv("SAVENAME"))) {
! 		move(LINES-2,0);
  		addstr("Restore Filename ? ");
  		refresh();
  		echo(); CBOFF;
***************
*** 101,108
  		getstr(fp);
  		CBON; noecho();
  	}
! 	clear();
! 	refresh();
  
  	if ((FILE *)NULL == (fi = fopen(fp, R_BIN))) {
  		endwin();

--- 111,119 -----
  		getstr(fp);
  		CBON; noecho();
  	}
! 	move(LINES-2,0);
! 	clrtoeol();
! 	move(LINES-1,0);
  
  	if ((FILE *)NULL == (fi = fopen(fp, R_BIN))) {
  		printw("Error opening '%s'", fp);
***************
*** 105,113
  	refresh();
  
  	if ((FILE *)NULL == (fi = fopen(fp, R_BIN))) {
! 		endwin();
! 		printf("Open error on '%s'n", fp);
! 		exit(1);
  	}
  	if ( (1 != fread((char *)&s, sizeof(s), 1, fi)) ||
  	     (1 != fread((char *)screen, sizeof(screen), 1, fi)) ||

--- 116,124 -----
  	move(LINES-1,0);
  
  	if ((FILE *)NULL == (fi = fopen(fp, R_BIN))) {
! 		printw("Error opening '%s'", fp);
! 		refresh();
! 		return 0;
  	}
  	if ( (1 != fread((char *)&s, sizeof(s), 1, fi)) ||
  	     (1 != fread((char *)screen, sizeof(screen), 1, fi)) ||
***************
*** 112,119
  	if ( (1 != fread((char *)&s, sizeof(s), 1, fi)) ||
  	     (1 != fread((char *)screen, sizeof(screen), 1, fi)) ||
  	     (1 != fread((char *)&zz, sizeof(zz), 1, fi)) ) {
! 		endwin();
! 		printf("Read error on '%s'n", fp);
  		fclose(fi);
  		exit(1);
  	}

--- 123,129 -----
  	if ( (1 != fread((char *)&s, sizeof(s), 1, fi)) ||
  	     (1 != fread((char *)screen, sizeof(screen), 1, fi)) ||
  	     (1 != fread((char *)&zz, sizeof(zz), 1, fi)) ) {
! 		printw("Error reading '%s'", fp);
  		fclose(fi);
  		refresh();
  		return 0;
***************
*** 115,121
  		endwin();
  		printf("Read error on '%s'n", fp);
  		fclose(fi);
! 		exit(1);
  	}
  
  	*num = s.num;

--- 125,132 -----
  	     (1 != fread((char *)&zz, sizeof(zz), 1, fi)) ) {
  		printw("Error reading '%s'", fp);
  		fclose(fi);
! 		refresh();
! 		return 0;
  	}
  	clear();
  	refresh();
***************
*** 117,122
  		fclose(fi);
  		exit(1);
  	}
  
  	*num = s.num;
  	*score = s.score;

--- 128,135 -----
  		refresh();
  		return 0;
  	}
+ 	clear();
+ 	refresh();
  
  	*num = s.num;
  	*score = s.score;
***************
*** 170,173
  	fclose(fi);
  	unlink(fp);
  	saved_game = 1;
  }

--- 183,187 -----
  	fclose(fi);
  	unlink(fp);
  	saved_game = 1;
+ 	return 1;
  }
-- 
      /\    John Abbott                       Systems Interface Inc.
     /  \   VOICE: (613) 230-4103             560 Rochester St, 2nd Floor
    /----\  FAX:   (613) 230-6928             Ottawa, Ontario, CANADA  K1S 5K2
\__/      \ UUCP:  uunet!mnetor!dciem!nrcaer!cognos!sysint!john