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