Home | History | Annotate | Line # | Download | only in robots
main.c revision 1.16
      1 /*	$NetBSD: main.c,v 1.16 2000/05/08 07:56:05 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 5/31/93";
     45 #else
     46 __RCSID("$NetBSD: main.c,v 1.16 2000/05/08 07:56:05 mycroft Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 # include	"robots.h"
     51 
     52 int main __P((int, char **));
     53 
     54 int
     55 main(ac, av)
     56 	int	ac;
     57 	char	**av;
     58 {
     59 	const char	*sp;
     60 	bool	bad_arg;
     61 	bool	show_only;
     62 	extern const char	*Scorefile;
     63 	extern int	Max_per_uid;
     64 	int		score_wfd; /* high score writable file descriptor */
     65 	int		score_err = 0; /* hold errno from score file open */
     66 
     67 	score_wfd = open(Scorefile, O_RDWR);
     68 	if (score_wfd < 0)
     69 		score_err = errno;
     70 	else if (score_wfd < 3)
     71 		exit(1);
     72 
     73 	/* Revoke setgid privileges */
     74 	setgid(getgid());
     75 
     76 	show_only = FALSE;
     77 	Num_games = 1;
     78 	if (ac > 1) {
     79 		bad_arg = FALSE;
     80 		for (++av; ac > 1 && *av[0]; av++, ac--)
     81 			if (av[0][0] != '-')
     82 				if (isdigit(av[0][0]))
     83 					Max_per_uid = atoi(av[0]);
     84 				else {
     85 					Scorefile = av[0];
     86 					if (score_wfd >= 0)
     87 						close(score_wfd);
     88 					score_wfd = open(Scorefile, O_RDWR);
     89 					if (score_wfd < 0)
     90 						score_err = errno;
     91 # ifdef	FANCY
     92 					sp = strrchr(Scorefile, '/');
     93 					if (sp == NULL)
     94 						sp = Scorefile;
     95 					if (strcmp(sp, "pattern_roll") == 0)
     96 						Pattern_roll = TRUE;
     97 					else if (strcmp(sp, "stand_still") == 0)
     98 						Stand_still = TRUE;
     99 					if (Pattern_roll || Stand_still)
    100 						Teleport = TRUE;
    101 # endif
    102 				}
    103 			else
    104 				for (sp = &av[0][1]; *sp; sp++)
    105 					switch (*sp) {
    106 					  case 'A':
    107 						Auto_bot = TRUE;
    108 						break;
    109 					  case 's':
    110 						show_only = TRUE;
    111 						break;
    112 					  case 'r':
    113 						Real_time = TRUE;
    114 						break;
    115 					  case 'a':
    116 						Start_level = 4;
    117 						break;
    118 					  case 'n':
    119 						Num_games++;
    120 						break;
    121 					  case 'j':
    122 						Jump = TRUE;
    123 						break;
    124 					  case 't':
    125 						Teleport = TRUE;
    126 						break;
    127 
    128 					  default:
    129 						fprintf(stderr, "robots: unknown option: %c\n", *sp);
    130 						bad_arg = TRUE;
    131 						break;
    132 					}
    133 		if (bad_arg) {
    134 			exit(1);
    135 			/* NOTREACHED */
    136 		}
    137 	}
    138 
    139 	if (show_only) {
    140 		show_score();
    141 		exit(0);
    142 		/* NOTREACHED */
    143 	}
    144 
    145 	if (score_wfd < 0) {
    146 		errno = score_err;
    147 		warn("%s", Scorefile);
    148 		warnx("High scores will not be recorded!");
    149 		sleep(2);
    150 	}
    151 
    152 	initscr();
    153 	signal(SIGINT, quit);
    154 	crmode();
    155 	noecho();
    156 	nonl();
    157 	if (LINES != Y_SIZE || COLS != X_SIZE) {
    158 		if (LINES < Y_SIZE || COLS < X_SIZE) {
    159 			endwin();
    160 			printf("Need at least a %dx%d screen\n",
    161 			    Y_SIZE, X_SIZE);
    162 			exit(1);
    163 		}
    164 		delwin(stdscr);
    165 		stdscr = newwin(Y_SIZE, X_SIZE, 0, 0);
    166 	}
    167 
    168 	srand(getpid());
    169 	if (Real_time)
    170 		signal(SIGALRM, move_robots);
    171 	do {
    172 		while (Num_games--) {
    173 			init_field();
    174 			for (Level = Start_level; !Dead; Level++) {
    175 				make_level();
    176 				play_level();
    177 				if (Auto_bot)
    178 					sleep(1);
    179 			}
    180 			move(My_pos.y, My_pos.x);
    181 			printw("AARRrrgghhhh....");
    182 			refresh();
    183 			if (Auto_bot)
    184 				sleep(1);
    185 			score(score_wfd);
    186 			if (Auto_bot)
    187 				sleep(1);
    188 			refresh();
    189 		}
    190 		Num_games = 1;
    191 	} while (!Auto_bot && another());
    192 	quit(0);
    193 	/* NOTREACHED */
    194 	return(0);
    195 }
    196 
    197 /*
    198  * quit:
    199  *	Leave the program elegantly.
    200  */
    201 void
    202 quit(dummy)
    203 	int dummy __attribute__((__unused__));
    204 {
    205 	endwin();
    206 	exit(0);
    207 	/* NOTREACHED */
    208 }
    209 
    210 /*
    211  * another:
    212  *	See if another game is desired
    213  */
    214 bool
    215 another()
    216 {
    217 	int	y;
    218 
    219 #ifdef	FANCY
    220 	if ((Stand_still || Pattern_roll) && !Newscore)
    221 		return TRUE;
    222 #endif
    223 
    224 	if (query("Another game?")) {
    225 		if (Full_clear) {
    226 			for (y = 1; y <= Num_scores; y++) {
    227 				move(y, 1);
    228 				clrtoeol();
    229 			}
    230 			refresh();
    231 		}
    232 		return TRUE;
    233 	}
    234 	return FALSE;
    235 }
    236