xref: /netbsd/games/robots/main.c (revision bf9ec67e)
1 /*	$NetBSD: main.c,v 1.18 2001/12/06 12:17:57 blymn 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.18 2001/12/06 12:17:57 blymn Exp $");
47 #endif
48 #endif /* not lint */
49 
50 # include	"robots.h"
51 
52 int main __P((int, char **));
53 
54 extern const char	*Scorefile;
55 extern int	Max_per_uid;
56 
57 int
58 main(ac, av)
59 	int	ac;
60 	char	**av;
61 {
62 	const char	*sp;
63 	bool	bad_arg;
64 	bool	show_only;
65 	int		score_wfd; /* high score writable file descriptor */
66 	int		score_err = 0; /* hold errno from score file open */
67 
68 	score_wfd = open(Scorefile, O_RDWR);
69 	if (score_wfd < 0)
70 		score_err = errno;
71 	else if (score_wfd < 3)
72 		exit(1);
73 
74 	/* Revoke setgid privileges */
75 	setgid(getgid());
76 
77 	show_only = FALSE;
78 	Num_games = 1;
79 	if (ac > 1) {
80 		bad_arg = FALSE;
81 		for (++av; ac > 1 && *av[0]; av++, ac--)
82 			if (av[0][0] != '-')
83 				if (isdigit(av[0][0]))
84 					Max_per_uid = atoi(av[0]);
85 				else {
86 					Scorefile = av[0];
87 					if (score_wfd >= 0)
88 						close(score_wfd);
89 					score_wfd = open(Scorefile, O_RDWR);
90 					if (score_wfd < 0)
91 						score_err = errno;
92 # ifdef	FANCY
93 					sp = strrchr(Scorefile, '/');
94 					if (sp == NULL)
95 						sp = Scorefile;
96 					if (strcmp(sp, "pattern_roll") == 0)
97 						Pattern_roll = TRUE;
98 					else if (strcmp(sp, "stand_still") == 0)
99 						Stand_still = TRUE;
100 					if (Pattern_roll || Stand_still)
101 						Teleport = TRUE;
102 # endif
103 				}
104 			else
105 				for (sp = &av[0][1]; *sp; sp++)
106 					switch (*sp) {
107 					  case 'A':
108 						Auto_bot = TRUE;
109 						break;
110 					  case 's':
111 						show_only = TRUE;
112 						break;
113 					  case 'r':
114 						Real_time = TRUE;
115 						break;
116 					  case 'a':
117 						Start_level = 4;
118 						break;
119 					  case 'n':
120 						Num_games++;
121 						break;
122 					  case 'j':
123 						Jump = TRUE;
124 						break;
125 					  case 't':
126 						Teleport = TRUE;
127 						break;
128 
129 					  default:
130 						fprintf(stderr, "robots: unknown option: %c\n", *sp);
131 						bad_arg = TRUE;
132 						break;
133 					}
134 		if (bad_arg) {
135 			exit(1);
136 			/* NOTREACHED */
137 		}
138 	}
139 
140 	if (show_only) {
141 		show_score();
142 		exit(0);
143 		/* NOTREACHED */
144 	}
145 
146 	if (score_wfd < 0) {
147 		errno = score_err;
148 		warn("%s", Scorefile);
149 		warnx("High scores will not be recorded!");
150 		sleep(2);
151 	}
152 
153 	initscr();
154 	signal(SIGINT, quit);
155 	cbreak();
156 	noecho();
157 	nonl();
158 	if (LINES != Y_SIZE || COLS != X_SIZE) {
159 		if (LINES < Y_SIZE || COLS < X_SIZE) {
160 			endwin();
161 			printf("Need at least a %dx%d screen\n",
162 			    Y_SIZE, X_SIZE);
163 			exit(1);
164 		}
165 		delwin(stdscr);
166 		stdscr = newwin(Y_SIZE, X_SIZE, 0, 0);
167 	}
168 
169 	srand(getpid());
170 	if (Real_time)
171 		signal(SIGALRM, move_robots);
172 	do {
173 		while (Num_games--) {
174 			init_field();
175 			for (Level = Start_level; !Dead; Level++) {
176 				make_level();
177 				play_level();
178 				if (Auto_bot)
179 					sleep(1);
180 			}
181 			move(My_pos.y, My_pos.x);
182 			printw("AARRrrgghhhh....");
183 			refresh();
184 			if (Auto_bot)
185 				sleep(1);
186 			score(score_wfd);
187 			if (Auto_bot)
188 				sleep(1);
189 			refresh();
190 		}
191 		Num_games = 1;
192 	} while (!Auto_bot && another());
193 	quit(0);
194 	/* NOTREACHED */
195 	return(0);
196 }
197 
198 /*
199  * quit:
200  *	Leave the program elegantly.
201  */
202 void
203 quit(dummy)
204 	int dummy __attribute__((__unused__));
205 {
206 	endwin();
207 	exit(0);
208 	/* NOTREACHED */
209 }
210 
211 /*
212  * another:
213  *	See if another game is desired
214  */
215 bool
216 another()
217 {
218 	int	y;
219 
220 #ifdef	FANCY
221 	if ((Stand_still || Pattern_roll) && !Newscore)
222 		return TRUE;
223 #endif
224 
225 	if (query("Another game?")) {
226 		if (Full_clear) {
227 			for (y = 1; y <= Num_scores; y++) {
228 				move(y, 1);
229 				clrtoeol();
230 			}
231 			refresh();
232 		}
233 		return TRUE;
234 	}
235 	return FALSE;
236 }
237