xref: /openbsd/games/robots/main.c (revision 264ca280)
1 /*	$OpenBSD: main.c,v 1.25 2016/03/07 12:07:57 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 	extern int	optind;
60 	char		*home;
61 #ifdef FANCY
62 	char		*sp;
63 #endif
64 
65 	if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
66 		err(1, "pledge");
67 
68 	home = getenv("HOME");
69 	if (home == NULL || *home == '\0')
70 		err(1, "getenv");
71 
72 	ret = snprintf(Scorefile, sizeof(Scorefile), "%s/%s", home,
73 	    ".robots.scores");
74 	if (ret < 0 || ret >= PATH_MAX)
75 		errc(1, ENAMETOOLONG, "%s/%s", home, ".robots.scores");
76 
77 	if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) < 0)
78 		score_err = errno;
79 
80 	show_only = FALSE;
81 	while ((ch = getopt(ac, av, "srajt")) != -1)
82 		switch (ch) {
83 		case 's':
84 			show_only = TRUE;
85 			break;
86 		case 'r':
87 			Real_time = TRUE;
88 			/* Could be a command-line option */
89 			tv.tv_sec = 3;
90 			break;
91 		case 'a':
92 			Start_level = 4;
93 			break;
94 		case 'j':
95 			Jump = TRUE;
96 			break;
97 		case 't':
98 			Teleport = TRUE;
99 			break;
100 		case '?':
101 		default:
102 			usage();
103 		}
104 	ac -= optind;
105 	av += optind;
106 
107 	if (ac > 1)
108 		usage();
109 	if (ac == 1) {
110 		if (strlcpy(Scorefile, av[0], sizeof(Scorefile)) >=
111 		    sizeof(Scorefile))
112 			errc(1, ENAMETOOLONG, "%s", av[0]);
113 		if (score_wfd >= 0)
114 			close(score_wfd);
115 		/* This file requires no special privileges. */
116 		if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) < 0)
117 			score_err = errno;
118 #ifdef	FANCY
119 		sp = strrchr(Scorefile, '/');
120 		if (sp == NULL)
121 			sp = Scorefile;
122 		if (strcmp(sp, "pattern_roll") == 0)
123 			Pattern_roll = TRUE;
124 		else if (strcmp(sp, "stand_still") == 0)
125 			Stand_still = TRUE;
126 		if (Pattern_roll || Stand_still)
127 			Teleport = TRUE;
128 #endif
129 	}
130 
131 	if (show_only) {
132 		show_score();
133 		return 0;
134 	}
135 
136 	if (score_wfd < 0) {
137 		warnx("%s: %s; no scores will be saved", Scorefile,
138 			strerror(score_err));
139 		sleep(1);
140 	}
141 
142 	initscr();
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 		move(My_pos.y, My_pos.x);
163 		printw("AARRrrgghhhh....");
164 		refresh();
165 		score(score_wfd);
166 	} while (another());
167 	quit(0);
168 }
169 
170 /*
171  * quit:
172  *	Leave the program elegantly.
173  */
174 void
175 quit(int dummy)
176 {
177 	endwin();
178 	exit(0);
179 }
180 
181 /*
182  * another:
183  *	See if another game is desired
184  */
185 bool
186 another(void)
187 {
188 	int	y;
189 
190 #ifdef	FANCY
191 	if ((Stand_still || Pattern_roll) && !Newscore)
192 		return TRUE;
193 #endif
194 
195 	if (query("Another game?")) {
196 		if (Full_clear) {
197 			for (y = 1; y <= Num_scores; y++) {
198 				move(y, 1);
199 				clrtoeol();
200 			}
201 			refresh();
202 		}
203 		return TRUE;
204 	}
205 	return FALSE;
206 }
207