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