xref: /netbsd-src/games/trek/main.c (revision 4511a017c166dba311b5566fe7dbf6e2a22914fc)
1 /*	$NetBSD: main.c,v 1.23 2011/08/29 20:30:37 joerg 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
40 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 5/31/93";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.23 2011/08/29 20:30:37 joerg Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <stdio.h>
47 #include <setjmp.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <err.h>
51 #include <time.h>
52 #include <sys/types.h>
53 #include "trek.h"
54 #include "getpar.h"
55 
56 
57 /*
58 **	 ####  #####    #    ####          #####  ####   #####  #   #
59 **	#        #     # #   #   #           #    #   #  #      #  #
60 **	 ###     #    #####  ####            #    ####   ###    ###
61 **	    #    #    #   #  #  #            #    #  #   #      #  #
62 **	####     #    #   #  #   #           #    #   #  #####  #   #
63 **
64 **	C version by Eric P. Allman 5/76 (U.C. Berkeley) with help
65 **		from Jeff Poskanzer and Pete Rubinstein.
66 **
67 **	I also want to thank everyone here at Berkeley who
68 **	where crazy enough to play the undebugged game.  I want to
69 **	particularly thank Nick Whyte, who made considerable
70 **	suggestions regarding the content of the game.  Why, I'll
71 **	never forget the time he suggested the name for the
72 **	"capture" command.
73 **
74 **	Please send comments, questions, and suggestions about this
75 **		game to:
76 **			Eric P. Allman
77 **			Project INGRES
78 **			Electronics Research Laboratory
79 **			Cory Hall
80 **			University of California
81 **			Berkeley, California  94720
82 **
83 **	If you make ANY changes in the game, I sure would like to
84 **	know about them.  It is sort of an ongoing project for me,
85 **	and I very much want to put in any bug fixes and improvements
86 **	that you might come up with.
87 **
88 **	FORTRASH version by Kay R. Fisher (DEC) "and countless others".
89 **	That was adapted from the "original BASIC program" (ha!) by
90 **		Mike Mayfield (Centerline Engineering).
91 **
92 **	Additional inspiration taken from FORTRAN version by
93 **		David Matuszek and Paul Reynolds which runs on the CDC
94 **		7600 at Lawrence Berkeley Lab, maintained there by
95 **		Andy Davidson.  This version is also available at LLL
96 **		and at LMSC.  In all fairness, this version was the
97 **		major inspiration for this version of the game (trans-
98 **		lation:  I ripped off a whole lot of code).
99 **
100 **	Minor other input from the "Battelle Version 7A" by Joe Miller
101 **		(Graphics Systems Group, Battelle-Columbus Labs) and
102 **		Ross Pavlac (Systems Programmer, Battelle Memorial
103 **		Institute).  That version was written in December '74
104 **		and extensively modified June '75.  It was adapted
105 **		from the FTN version by Ron Williams of CDC Sunnyvale,
106 **		which was adapted from the Basic version distributed
107 **		by DEC.  It also had "neat stuff swiped" from T. T.
108 **		Terry and Jim Korp (University of Texas), Hicks (Penn
109 **		U.), and Rick Maus (Georgia Tech).  Unfortunately, it
110 **		was not as readable as it could have been and so the
111 **		translation effort was severely hampered.  None the
112 **		less, I got the idea of inhabited starsystems from this
113 **		version.
114 **
115 **	Permission is given for use, copying, and modification of
116 **		all or part of this program and related documentation,
117 **		provided that all reference to the authors are maintained.
118 **
119 **
120 **********************************************************************
121 **
122 **  NOTES TO THE MAINTAINER:
123 **
124 **	There is a compilation option xTRACE which must be set for any
125 **	trace information to be generated.  It is probably defined in
126 **	the version that you get.  It can be removed, however, if you
127 **	have trouble finding room in core.
128 **
129 **	Many things in trek are not as clear as they might be, but are
130 **	done to reduce space.  I compile with the -f and -O flags.  I
131 **	am constrained to running with non-separated I/D space, since
132 **	we don't have doubleing point hardware here; even if we did, I
133 **	would like trek to be available to the large number of people
134 **	who either have an 11/40 or do not have FP hardware.  I also
135 **	found it desirable to make the code run reentrant, so this
136 **	added even more space constraints.
137 **
138 **	I use the portable C library to do my I/O.  This is done be-
139 **	cause I wanted the game easily transportable to other C
140 **	implementations, and because I was too lazy to do the doubleing
141 **	point input myself.  Little did I know.  The portable C library
142 **	released by Bell Labs has more bugs than you would believe, so
143 **	I ended up rewriting the whole blessed thing.  Trek excercises
144 **	many of the bugs in it, as well as bugs in some of the section
145 **	III UNIX routines.  We have fixed them here.  One main problem
146 **	was a bug in alloc() that caused it to always ask for a large
147 **	hunk of memory, which worked fine unless you were almost out,
148 **	which I inevitably was.  If you want the code for all of this
149 **	stuff, it is also available through me.
150 **
151 ***********************************************************************
152 */
153 
154 jmp_buf env;
155 
156 __dead static void
usage(const char * av0)157 usage(const char *av0)
158 {
159 	errx(1, "Usage: %s [-fs]", av0);
160 }
161 
162 int
main(int argc,char ** argv)163 main(int argc, char **argv)
164 {
165 	int ch;
166 
167 	/* Revoke setgid privileges */
168 	setgid(getgid());
169 
170 	/* Default to fast mode */
171 	Etc.fast = 1;
172 
173 	srandom((long) time(NULL));
174 
175 	while ((ch = getopt(argc, argv, "fst")) != -1) {
176 		switch (ch) {
177 		  case 'f':	/* set fast mode */
178 			Etc.fast++;
179 			break;
180 
181 		  case 's':	/* set slow mode */
182 			Etc.fast = 0;
183 			break;
184 
185 #ifdef xTRACE
186 		  case 't':	/* trace */
187 			Trace++;
188 			break;
189 #endif
190 
191 		  default:
192 			usage(argv[0]);
193 		}
194 	}
195 	if (optind < argc)
196 		usage(argv[0]);
197 
198 	printf("\n   * * *   S T A R   T R E K   * * *\n\n"
199 	       "Press return to continue.\n");
200 
201 	if (setjmp(env)) {
202 		if ( !getynpar("Another game") )
203 			exit(0);
204 	}
205 
206 	do {
207 		setup();
208 		play();
209 	} while (getynpar("Another game"));
210 
211 	fflush(stdout);
212 	return 0;
213 }
214