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