Merge from vendor branch LIBSTDC++:
[dragonfly.git] / games / trek / main.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)main.c   8.1 (Berkeley) 5/31/93
35  * $FreeBSD: src/games/trek/main.c,v 1.7.2.1 2001/03/05 12:11:14 kris Exp $
36  * $DragonFly: src/games/trek/main.c,v 1.2 2003/06/17 04:25:25 dillon Exp $
37  */
38
39 # include       "trek.h"
40 # include       <stdio.h>
41 # include       <sgtty.h>
42 # include       <setjmp.h>
43 # include       <stdlib.h>
44
45 # define        PRIO            00      /* default priority */
46
47 int     Mother  = 51 + (51 << 8);
48
49 /*
50 **       ####  #####    #    ####          #####  ####   #####  #   #
51 **      #        #     # #   #   #           #    #   #  #      #  #
52 **       ###     #    #####  ####            #    ####   ###    ###
53 **          #    #    #   #  #  #            #    #  #   #      #  #
54 **      ####     #    #   #  #   #           #    #   #  #####  #   #
55 **
56 **      C version by Eric P. Allman 5/76 (U.C. Berkeley) with help
57 **              from Jeff Poskanzer and Pete Rubinstein.
58 **
59 **      I also want to thank everyone here at Berkeley who
60 **      where crazy enough to play the undebugged game.  I want to
61 **      particularly thank Nick Whyte, who made considerable
62 **      suggestions regarding the content of the game.  Why, I'll
63 **      never forget the time he suggested the name for the
64 **      "capture" command.
65 **
66 **      Please send comments, questions, and suggestions about this
67 **              game to:
68 **                      Eric P. Allman
69 **                      Project INGRES
70 **                      Electronics Research Laboratory
71 **                      Cory Hall
72 **                      University of California
73 **                      Berkeley, California  94720
74 **
75 **      If you make ANY changes in the game, I sure would like to
76 **      know about them.  It is sort of an ongoing project for me,
77 **      and I very much want to put in any bug fixes and improvements
78 **      that you might come up with.
79 **
80 **      FORTRASH version by Kay R. Fisher (DEC) "and countless others".
81 **      That was adapted from the "original BASIC program" (ha!) by
82 **              Mike Mayfield (Centerline Engineering).
83 **
84 **      Additional inspiration taken from FORTRAN version by
85 **              David Matuszek and Paul Reynolds which runs on the CDC
86 **              7600 at Lawrence Berkeley Lab, maintained there by
87 **              Andy Davidson.  This version is also available at LLL
88 **              and at LMSC.  In all fairness, this version was the
89 **              major inspiration for this version of the game (trans-
90 **              lation:  I ripped off a whole lot of code).
91 **
92 **      Minor other input from the "Battelle Version 7A" by Joe Miller
93 **              (Graphics Systems Group, Battelle-Columbus Labs) and
94 **              Ross Pavlac (Systems Programmer, Battelle Memorial
95 **              Institute).  That version was written in December '74
96 **              and extensively modified June '75.  It was adapted
97 **              from the FTN version by Ron Williams of CDC Sunnyvale,
98 **              which was adapted from the Basic version distributed
99 **              by DEC.  It also had "neat stuff swiped" from T. T.
100 **              Terry and Jim Korp (University of Texas), Hicks (Penn
101 **              U.), and Rick Maus (Georgia Tech).  Unfortunately, it
102 **              was not as readable as it could have been and so the
103 **              translation effort was severely hampered.  None the
104 **              less, I got the idea of inhabited starsystems from this
105 **              version.
106 **
107 **      Permission is given for use, copying, and modification of
108 **              all or part of this program and related documentation,
109 **              provided that all reference to the authors are maintained.
110 **
111 **
112 **********************************************************************
113 **
114 **  NOTES TO THE MAINTAINER:
115 **
116 **      There is a compilation option xTRACE which must be set for any
117 **      trace information to be generated.  It is probably defined in
118 **      the version that you get.  It can be removed, however, if you
119 **      have trouble finding room in core.
120 **
121 **      Many things in trek are not as clear as they might be, but are
122 **      done to reduce space.  I compile with the -f and -O flags.  I
123 **      am constrained to running with non-separated I/D space, since
124 **      we don't have doubleing point hardware here; even if we did, I
125 **      would like trek to be available to the large number of people
126 **      who either have an 11/40 or do not have FP hardware.  I also
127 **      found it desirable to make the code run reentrant, so this
128 **      added even more space constraints.
129 **
130 **      I use the portable C library to do my I/O.  This is done be-
131 **      cause I wanted the game easily transportable to other C
132 **      implementations, and because I was too lazy to do the doubleing
133 **      point input myself.  Little did I know.  The portable C library
134 **      released by Bell Labs has more bugs than you would believe, so
135 **      I ended up rewriting the whole blessed thing.  Trek excercises
136 **      many of the bugs in it, as well as bugs in some of the section
137 **      III UNIX routines.  We have fixed them here.  One main problem
138 **      was a bug in alloc() that caused it to always ask for a large
139 **      hunk of memory, which worked fine unless you were almost out,
140 **      which I inevitably was.  If you want the code for all of this
141 **      stuff, it is also available through me.
142 **
143 ***********************************************************************
144 */
145
146 jmp_buf env;
147
148 main(argc, argv)
149 int     argc;
150 char    **argv;
151 {
152         /* extern FILE          *f_log; */
153         char            opencode;
154         int                     prio;
155         int             ac;
156         char            **av;
157         struct  sgttyb          argp;
158
159         /* revoke */
160         setgid(getgid());
161
162         av = argv;
163         ac = argc;
164         av++;
165         srandomdev();
166         opencode = 'w';
167         prio = PRIO;
168         if (gtty(1, &argp) == 0)
169         {
170                 if ((argp.sg_ispeed ) < B1200)
171                         Etc.fast++;
172         }
173         while (ac > 1 && av[0][0] == '-')
174         {
175                 switch (av[0][1])
176                 {
177                   case 'a':     /* append to log file */
178                         opencode = 'a';
179                         break;
180
181                   case 'f':     /* set fast mode */
182                         Etc.fast++;
183                         break;
184
185                   case 's':     /* set slow mode */
186                         Etc.fast = 0;
187                         break;
188
189 #               ifdef xTRACE
190                   case 't':     /* trace */
191                         if (getuid() != Mother)
192                                 goto badflag;
193                         Trace++;
194                         break;
195 #               endif
196
197                   case 'p':     /* set priority */
198                         if (getuid() != Mother)
199                                 goto badflag;
200                         prio = atoi(av[0] + 2);
201                         break;
202
203                   default:
204                   badflag:
205                         printf("Invalid option: %s\n", av[0]);
206
207                 }
208                 ac--;
209                 av++;
210         }
211         if (ac > 2)
212                 syserr(0, "arg count");
213                 /*
214         if (ac > 1)
215                 f_log = fopen(av[0], opencode);
216                 */
217
218         printf("\n   * * *   S T A R   T R E K   * * *\n\nPress return to continue.\n");
219
220         if (setjmp(env))
221         {
222                 if ( !getynpar("Another game") )
223                         exit(0);
224         }
225         do
226         {
227                 setup();
228                 play();
229         } while (getynpar("Another game"));
230
231         fflush(stdout);
232 }