1/*	$OpenBSD: main.c,v 1.31 2022/12/04 23:50:45 cheloha 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. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <err.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <signal.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
41#include "robots.h"
42
43void
44usage(void)
45{
46	fprintf(stderr, "usage: %s [-ajrst] [scorefile]\n", getprogname());
47	exit(1);
48}
49
50int
51main(int ac, char *av[])
52{
53	bool		show_only;
54	extern char	Scorefile[PATH_MAX];
55	int		score_wfd;     /* high score writable file descriptor */
56	int		score_err = 0; /* hold errno from score file open */
57	int		ch;
58	int		ret;
59	char		*home;
60#ifdef FANCY
61	char		*sp;
62#endif
63
64	home = getenv("HOME");
65	if (home == NULL || *home == '\0')
66		err(1, "getenv");
67
68	ret = snprintf(Scorefile, sizeof(Scorefile), "%s/%s", home,
69	    ".robots.scores");
70	if (ret < 0 || ret >= PATH_MAX)
71		errc(1, ENAMETOOLONG, "%s/%s", home, ".robots.scores");
72
73	if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) == -1)
74		score_err = errno;
75
76	show_only = FALSE;
77	while ((ch = getopt(ac, av, "srajt")) != -1)
78		switch (ch) {
79		case 's':
80			show_only = TRUE;
81			break;
82		case 'r':
83			Real_time = TRUE;
84			/* Could be a command-line option */
85			tv.tv_sec = 3;
86			break;
87		case 'a':
88			Start_level = 4;
89			break;
90		case 'j':
91			Jump = TRUE;
92			break;
93		case 't':
94			Teleport = TRUE;
95			break;
96		default:
97			usage();
98		}
99	ac -= optind;
100	av += optind;
101
102	if (ac > 1)
103		usage();
104	if (ac == 1) {
105		if (strlcpy(Scorefile, av[0], sizeof(Scorefile)) >=
106		    sizeof(Scorefile))
107			errc(1, ENAMETOOLONG, "%s", av[0]);
108		if (score_wfd >= 0)
109			close(score_wfd);
110		/* This file requires no special privileges. */
111		if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) == -1)
112			score_err = errno;
113#ifdef	FANCY
114		sp = strrchr(Scorefile, '/');
115		if (sp == NULL)
116			sp = Scorefile;
117		if (strcmp(sp, "pattern_roll") == 0)
118			Pattern_roll = TRUE;
119		else if (strcmp(sp, "stand_still") == 0)
120			Stand_still = TRUE;
121		if (Pattern_roll || Stand_still)
122			Teleport = TRUE;
123#endif
124	}
125
126	if (show_only) {
127		show_score();
128		return 0;
129	}
130
131	if (score_wfd < 0) {
132		warnx("%s: %s; no scores will be saved", Scorefile,
133			strerror(score_err));
134		sleep(1);
135	}
136
137	initscr();
138
139	if (pledge("stdio tty", NULL) == -1)
140		err(1, "pledge");
141
142	signal(SIGINT, quit);
143	cbreak();
144	noecho();
145	nonl();
146	if (LINES != Y_SIZE || COLS != X_SIZE) {
147		if (LINES < Y_SIZE || COLS < X_SIZE) {
148			endwin();
149			errx(1, "Need at least a %dx%d screen", Y_SIZE, X_SIZE);
150		}
151		delwin(stdscr);
152		stdscr = newwin(Y_SIZE, X_SIZE, 0, 0);
153	}
154
155	do {
156		init_field();
157		for (Level = Start_level; !Dead; Level++) {
158			make_level();
159			play_level();
160		}
161		if (My_pos.x > X_FIELDSIZE - 16)
162			move(My_pos.y, X_FIELDSIZE - 16);
163		else
164			move(My_pos.y, My_pos.x);
165		printw("AARRrrgghhhh....");
166		refresh();
167		score(score_wfd);
168	} while (another());
169	quit(0);
170}
171
172/*
173 * quit:
174 *	Leave the program elegantly.
175 */
176void
177quit(int dummy)
178{
179	endwin();
180	exit(0);
181}
182
183/*
184 * another:
185 *	See if another game is desired
186 */
187bool
188another(void)
189{
190	int	y;
191
192#ifdef	FANCY
193	if ((Stand_still || Pattern_roll) && !Newscore)
194		return TRUE;
195#endif
196
197	if (query("Another game?")) {
198		if (Full_clear) {
199			for (y = 1; y <= Num_scores; y++) {
200				move(y, 1);
201				clrtoeol();
202			}
203			refresh();
204		}
205		return TRUE;
206	}
207	return FALSE;
208}
209