]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - robots/main.c
don't use char values for functions that can return -1; chars are not always
[bsdgames-darwin.git] / robots / main.c
1 /* $NetBSD: main.c,v 1.31 2009/08/05 19:34:09 christos 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.31 2009/08/05 19:34:09 christos 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 argc, char **argv)
62 {
63 const char *word;
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 int maximum = 0;
68 int ch, i;
69
70 score_wfd = open(Scorefile, O_RDWR);
71 if (score_wfd < 0)
72 score_err = errno;
73 else if (score_wfd < 3)
74 exit(1);
75
76 /* Revoke setgid privileges */
77 setgid(getgid());
78
79 show_only = false;
80 Num_games = 1;
81
82 while ((ch = getopt(argc, argv, "Aajnrst")) != -1) {
83 switch (ch) {
84 case 'A':
85 Auto_bot = true;
86 break;
87 case 'a':
88 Start_level = 4;
89 break;
90 case 'j':
91 Jump = true;
92 break;
93 case 'n':
94 Num_games++;
95 break;
96 case 'r':
97 Real_time = true;
98 break;
99 case 's':
100 show_only = true;
101 break;
102 case 't':
103 Teleport = true;
104 break;
105 default:
106 errx(1,
107 "Usage: robots [-Aajnrst] [maximum] [scorefile]");
108 break;
109 }
110 }
111
112 for (i = optind; i < argc; i++) {
113 word = argv[i];
114 if (isdigit((unsigned char)word[0])) {
115 maximum = atoi(word);
116 } else {
117 Scorefile = word;
118 Max_per_uid = maximum;
119 if (score_wfd >= 0)
120 close(score_wfd);
121 score_wfd = open(Scorefile, O_RDWR);
122 if (score_wfd < 0)
123 score_err = errno;
124 #ifdef FANCY
125 word = strrchr(Scorefile, '/');
126 if (word == NULL)
127 word = Scorefile;
128 if (strcmp(word, "pattern_roll") == 0)
129 Pattern_roll = true;
130 else if (strcmp(word, "stand_still") == 0)
131 Stand_still = true;
132 if (Pattern_roll || Stand_still)
133 Teleport = true;
134 #endif
135 }
136 }
137
138 if (show_only) {
139 show_score();
140 exit(0);
141 /* NOTREACHED */
142 }
143
144 if (score_wfd < 0) {
145 errno = score_err;
146 warn("%s", Scorefile);
147 warnx("High scores will not be recorded!");
148 sleep(2);
149 }
150
151 if (!initscr())
152 errx(0, "couldn't initialize screen");
153 signal(SIGINT, quit);
154 cbreak();
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 srandom(time(NULL));
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(int dummy __unused)
203 {
204 endwin();
205 exit(0);
206 /* NOTREACHED */
207 }
208
209 /*
210 * another:
211 * See if another game is desired
212 */
213 bool
214 another(void)
215 {
216 int y;
217
218 #ifdef FANCY
219 if ((Stand_still || Pattern_roll) && !Newscore)
220 return true;
221 #endif
222
223 if (query("Another game?")) {
224 if (Full_clear) {
225 for (y = 1; y <= Num_scores; y++) {
226 move(y, 1);
227 clrtoeol();
228 }
229 refresh();
230 }
231 return true;
232 }
233 return false;
234 }