[comp.sources.d] A fix and enhancement for 'robots'

eric@snark.UUCP (Eric S. Raymond) (10/07/88)

This is a patch for the 'robots' game. It adds the following features:

1. Support for System V file locking on the score file (so you don't get
   bogus 'bad file number' messages at game end).

2. A new option 'beepbad' that rejects suicidal moves. This helps against
   typographical errors.

I also have Oleg Kiselev's already-posted fix to good.c to prevent core dumps
on some out-of-bounds array checks, available by email to interested parted.

Is anyone still maintaining this creature?

*** main.c-orig	Wed Sep 21 18:24:29 1988
--- main.c	Thu Oct  6 22:25:29 1988
***************
*** 21,27 ****
  
  bool	dead = FALSE;
  bool	last_stand;
! bool	show_highscore = TRUE;
  bool	running, adjacent, first_move, bad_move, waiting;
  bool	moveable_heaps = TRUE;
  
--- 21,28 ----
  
  bool	dead = FALSE;
  bool	last_stand;
! bool	show_highscore = FALSE;
! bool	beep_badmoves = TRUE;
  bool	running, adjacent, first_move, bad_move, waiting;
  bool	moveable_heaps = TRUE;
  
***************
*** 88,94 ****
  	seed = time((time_t *)0)+getuid();
  	(void) signal(SIGQUIT,interrupt);
  	(void) signal(SIGINT,interrupt);
! 	if( initscr() == ERR) {
  		fprintf(stderr, "Curses won't initialise - seek a guru\n");
  		quit(FALSE);
  	}
--- 89,95 ----
  	seed = time((time_t *)0)+getuid();
  	(void) signal(SIGQUIT,interrupt);
  	(void) signal(SIGINT,interrupt);
! 	if( (int)initscr() == ERR) {
  		fprintf(stderr, "Curses won't initialise - seek a guru\n");
  		quit(FALSE);
  	}
***************
*** 325,331 ****
--- 326,361 ----
  	return close(fd);
  }
  # else
+ #include <unistd.h>
  
+ # ifdef F_LOCK
+ lk_open(file, mode)	/* open and lock a file using the lockf(3) sys call */
+ char	*file;
+ int	mode;
+ {
+ 	int	fd;
+ 
+ 	if( (fd = open(file, mode)) < 0)
+ 		return -1;
+ 	if(lockf(fd, F_LOCK, 0) < 0)
+ 	{
+ 		(void) close(fd);
+ 		return -1;
+ 	}
+ 	return fd;
+ }
+ 
+ lk_close( fd, file)
+ int	fd;
+ char	*file;
+ {
+ # ifdef lint
+ 	file = file;	/* now will you shut up lint???? */
+ # endif
+ 	return close(fd);
+ }
+ # else
+ 
  # define	LOCKTIME	(60)	/* 1 minute */
  # include	<sys/stat.h>
  
***************
*** 372,376 ****
  		perror(lfile);
  	return close(fd);
  }
! # endif
  
--- 402,407 ----
  		perror(lfile);
  	return close(fd);
  }
! # endif /* !LOCKF */
  
+ # endif /* !BSD42 */

*** opt.c-orig	Wed Sep 21 18:54:34 1988
--- opt.c	Thu Oct  6 22:28:00 1988
***************
*** 20,25 ****
--- 20,29 ----
  	"showhscore",	OPT_SHOW_HSCORE,
  # define	OPT_NOSHOW_HSCORE 5
  	"noshowhscore", OPT_NOSHOW_HSCORE,
+ # define	OPT_BEEP_BAD 6
+ 	"beepbad", OPT_BEEP_BAD,
+ # define	OPT_NOBEEP_BAD 7
+ 	"nobeepbad", OPT_NOBEEP_BAD,
  	0,		0
  };
  
***************
*** 72,77 ****
--- 76,87 ----
  				break;
  			case	OPT_NOSHOW_HSCORE:
  				show_highscore = FALSE;
+ 				break;
+ 			case	OPT_BEEP_BAD:
+ 				beep_badmoves = TRUE;
+ 				break;
+ 			case	OPT_NOBEEP_BAD:
+ 				beep_badmoves = FALSE;
  				break;
  		}
  	}

*** robots.6-orig	Thu Oct  6 22:28:26 1988
--- robots.6	Thu Oct  6 22:34:50 1988
***************
*** 90,95 ****
--- 90,99 ----
  This option shows the highscore table after every game. The
  negated version will only print out the highscore only if you appear
  on it.
+ .IP beepbad
+ If this option is on, the program will emit a beep instead of executing
+ moves that would inevitably get you killed (it can't check this for teleports
+ of course). Useful if you mis-type a lot.
  .SH NOTE
  The first time the `t' command is used on a new level
  you are guaranteed not to land next to a robot, a count of
***************
*** 111,113 ****
--- 115,120 ----
  Allan Black, Strathclyde University, Glasgow.
  .br
  Enhancements by Graeme Lunt & Julian Onions, Nottingham University.
+ .br
+ Further enhancements (including beepbad and System V file-locking support)
+ by Eric S. Raymond (eric@snark.UUCP).

*** robots.h-orig	Wed Sep 21 19:05:52 1988
--- robots.h	Thu Oct  6 22:25:35 1988
***************
*** 65,70 ****
--- 65,71 ----
  
  extern	bool	moveable_heaps;
  extern	bool	show_highscore;
+ extern	bool	beep_badmoves;
  extern	bool	last_stand;
  extern	bool	bad_move;
  extern	bool	running;

*** score.c-orig	Fri Jul 10 02:46:09 1987
--- score.c	Thu Oct  6 22:08:45 1988
***************
*** 87,93 ****
  	sfile = (struct scorefile *)(malloc(FILE_SIZE));
  	if( sfile == NULL)
  	{
! 		fprintf( stderr, "Out of memmory so no scoring");
  		return FALSE;
  	}
  	eof = &sfile[NUMSCORES];
--- 87,93 ----
  	sfile = (struct scorefile *)(malloc(FILE_SIZE));
  	if( sfile == NULL)
  	{
! 		fprintf( stderr, "Out of memory so no scoring");
  		return FALSE;
  	}
  	eof = &sfile[NUMSCORES];

*** user.c-orig	Wed Sep 21 18:54:51 1988
--- user.c	Thu Oct  6 22:25:39 1988
***************
*** 197,202 ****
--- 197,207 ----
  			}
  		}
  	}
+ 	if (beep_badmoves && !isgood(new_y, new_x))
+ 	{
+ 	    bad_move = TRUE;
+ 	    return;
+ 	}
  	move(new_y,new_x);
  	switch(inch()) {
  	case SCRAP:

-- 
      Eric S. Raymond                     (the mad mastermind of TMN-Netnews)
      UUCP: ...!{uunet,att,rutgers}!snark!eric = eric@snark.UUCP
      Post: 22 S. Warren Avenue, Malvern, PA 19355      Phone: (215)-296-5718