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