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