xref: /openbsd/games/robots/main.c (revision f2dfb0a4)
1 /*	$OpenBSD: main.c,v 1.5 1997/02/05 18:28:35 kstailey 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[] = "$NetBSD: main.c,v 1.5 1995/04/22 10:08:54 cgd Exp $";
48 #endif
49 #endif /* not lint */
50 
51 # include	"robots.h"
52 # include	<signal.h>
53 # include	<ctype.h>
54 
55 main(ac, av)
56 int	ac;
57 char	**av;
58 {
59 	register char	*sp;
60 	register bool	bad_arg;
61 	register bool	show_only;
62 	extern char	*Scorefile;
63 	extern int	Max_per_uid;
64 	int		score_wfd; /* high score writable file descriptor */
65 	void quit();
66 
67 	if ((score_wfd = open(Scorefile, 2)) < 0) {
68 		perror(Scorefile);
69 		exit(1);
70 	}
71 
72 	/* revoke */
73 	setegid(getgid());
74 	setgid(getgid());
75 
76 	show_only = FALSE;
77 	if (ac > 1) {
78 		bad_arg = FALSE;
79 		for (++av; ac > 1 && *av[0]; av++, ac--)
80 			if (av[0][0] != '-')
81 				if (isdigit(av[0][0]))
82 					Max_per_uid = atoi(av[0]);
83 				else {
84 					Scorefile = av[0];
85 # ifdef	FANCY
86 					sp = strrchr(Scorefile, '/');
87 					if (sp == NULL)
88 						sp = Scorefile;
89 					close(score_wfd);
90 				/* This file is in the current directory  */
91 				/* and requires no special privileges: */
92 					if ((score_wfd =
93 					     open(Scorefile, 2)) < 0) {
94 						perror(Scorefile);
95 						exit(1);
96 					}
97 
98 					if (strcmp(sp, "pattern_roll") == 0)
99 						Pattern_roll = TRUE;
100 					else if (strcmp(sp, "stand_still") == 0)
101 						Stand_still = TRUE;
102 					if (Pattern_roll || Stand_still)
103 						Teleport = TRUE;
104 # endif
105 				}
106 			else
107 				for (sp = &av[0][1]; *sp; sp++)
108 					switch (*sp) {
109 					  case 's':
110 						show_only = TRUE;
111 						break;
112 					  case 'r':
113 						Real_time = TRUE;
114 						break;
115 					  case 'a':
116 						Start_level = 4;
117 						break;
118 					  case 'j':
119 						Jump = TRUE;
120 						break;
121 					  case 't':
122 						Teleport = TRUE;
123 						break;
124 					  default:
125 						fprintf(stderr, "robots: uknown option: %c\n", *sp);
126 						bad_arg = TRUE;
127 						break;
128 					}
129 		if (bad_arg) {
130 			exit(1);
131 			/* NOTREACHED */
132 		}
133 	}
134 
135 	if (show_only) {
136 		show_score();
137 		exit(0);
138 		/* NOTREACHED */
139 	}
140 
141 	initscr();
142 	signal(SIGINT, quit);
143 	crmode();
144 	noecho();
145 	nonl();
146 	if (LINES != Y_SIZE || COLS != X_SIZE) {
147 		if (LINES < Y_SIZE || COLS < X_SIZE) {
148 			endwin();
149 			printf("Need at least a %dx%d screen\n",
150 			    Y_SIZE, X_SIZE);
151 			exit(1);
152 		}
153 		delwin(stdscr);
154 		stdscr = newwin(Y_SIZE, X_SIZE, 0, 0);
155 	}
156 
157 	srand(getpid());
158 	if (Real_time)
159 		signal(SIGALRM, move_robots);
160 	do {
161 		init_field();
162 		for (Level = Start_level; !Dead; Level++) {
163 			make_level();
164 			play_level();
165 		}
166 		move(My_pos.y, My_pos.x);
167 		printw("AARRrrgghhhh....");
168 		refresh();
169 		score(score_wfd);
170 	} while (another());
171 	quit();
172 }
173 
174 void
175 __cputchar(ch)
176 	int ch;
177 {
178 	(void)putchar(ch);
179 }
180 
181 /*
182  * quit:
183  *	Leave the program elegantly.
184  */
185 void
186 quit()
187 {
188 	endwin();
189 	exit(0);
190 	/* NOTREACHED */
191 }
192 
193 /*
194  * another:
195  *	See if another game is desired
196  */
197 another()
198 {
199 	register int	y;
200 
201 #ifdef	FANCY
202 	if ((Stand_still || Pattern_roll) && !Newscore)
203 		return TRUE;
204 #endif
205 
206 	if (query("Another game?")) {
207 		if (Full_clear) {
208 			for (y = 1; y <= Num_scores; y++) {
209 				move(y, 1);
210 				clrtoeol();
211 			}
212 			refresh();
213 		}
214 		return TRUE;
215 	}
216 	return FALSE;
217 }
218