xref: /openbsd/games/robots/main.c (revision 5a38ef86)
1 /*	$OpenBSD: main.c,v 1.30 2021/10/23 11:22:49 mestre 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 
43 void
44 usage(void)
45 {
46 	fprintf(stderr, "usage: %s [-ajrst] [scorefile]\n", getprogname());
47 	exit(1);
48 }
49 
50 int
51 main(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 		case '?':
97 		default:
98 			usage();
99 		}
100 	ac -= optind;
101 	av += optind;
102 
103 	if (ac > 1)
104 		usage();
105 	if (ac == 1) {
106 		if (strlcpy(Scorefile, av[0], sizeof(Scorefile)) >=
107 		    sizeof(Scorefile))
108 			errc(1, ENAMETOOLONG, "%s", av[0]);
109 		if (score_wfd >= 0)
110 			close(score_wfd);
111 		/* This file requires no special privileges. */
112 		if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) == -1)
113 			score_err = errno;
114 #ifdef	FANCY
115 		sp = strrchr(Scorefile, '/');
116 		if (sp == NULL)
117 			sp = Scorefile;
118 		if (strcmp(sp, "pattern_roll") == 0)
119 			Pattern_roll = TRUE;
120 		else if (strcmp(sp, "stand_still") == 0)
121 			Stand_still = TRUE;
122 		if (Pattern_roll || Stand_still)
123 			Teleport = TRUE;
124 #endif
125 	}
126 
127 	if (show_only) {
128 		show_score();
129 		return 0;
130 	}
131 
132 	if (score_wfd < 0) {
133 		warnx("%s: %s; no scores will be saved", Scorefile,
134 			strerror(score_err));
135 		sleep(1);
136 	}
137 
138 	initscr();
139 
140 	if (pledge("stdio tty", NULL) == -1)
141 		err(1, "pledge");
142 
143 	signal(SIGINT, quit);
144 	cbreak();
145 	noecho();
146 	nonl();
147 	if (LINES != Y_SIZE || COLS != X_SIZE) {
148 		if (LINES < Y_SIZE || COLS < X_SIZE) {
149 			endwin();
150 			errx(1, "Need at least a %dx%d screen", Y_SIZE, X_SIZE);
151 		}
152 		delwin(stdscr);
153 		stdscr = newwin(Y_SIZE, X_SIZE, 0, 0);
154 	}
155 
156 	do {
157 		init_field();
158 		for (Level = Start_level; !Dead; Level++) {
159 			make_level();
160 			play_level();
161 		}
162 		if (My_pos.x > X_FIELDSIZE - 16)
163 			move(My_pos.y, X_FIELDSIZE - 16);
164 		else
165 			move(My_pos.y, My_pos.x);
166 		printw("AARRrrgghhhh....");
167 		refresh();
168 		score(score_wfd);
169 	} while (another());
170 	quit(0);
171 }
172 
173 /*
174  * quit:
175  *	Leave the program elegantly.
176  */
177 void
178 quit(int dummy)
179 {
180 	endwin();
181 	exit(0);
182 }
183 
184 /*
185  * another:
186  *	See if another game is desired
187  */
188 bool
189 another(void)
190 {
191 	int	y;
192 
193 #ifdef	FANCY
194 	if ((Stand_still || Pattern_roll) && !Newscore)
195 		return TRUE;
196 #endif
197 
198 	if (query("Another game?")) {
199 		if (Full_clear) {
200 			for (y = 1; y <= Num_scores; y++) {
201 				move(y, 1);
202 				clrtoeol();
203 			}
204 			refresh();
205 		}
206 		return TRUE;
207 	}
208 	return FALSE;
209 }
210