xref: /openbsd/games/trek/setup.c (revision 78b63d65)
1 /*	$OpenBSD: setup.c,v 1.3 1999/07/31 18:48:59 pjanzen Exp $	*/
2 /*	$NetBSD: setup.c,v 1.4 1995/04/24 12:26:06 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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)setup.c	8.1 (Berkeley) 5/31/93";
40 #else
41 static char rcsid[] = "$OpenBSD: setup.c,v 1.3 1999/07/31 18:48:59 pjanzen Exp $";
42 #endif
43 #endif /* not lint */
44 
45 #include <stdio.h>
46 #include <math.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <err.h>
50 #include "trek.h"
51 #include "getpar.h"
52 
53 /*
54 **  INITIALIZE THE GAME
55 **
56 **	The length, skill, and password are read, and the game
57 **	is initialized.  It is far too difficult to describe all
58 **	that goes on in here, but it is all straight-line code;
59 **	give it a look.
60 **
61 **	Game restart and tournament games are handled here.
62 */
63 
64 const struct cvntab	Lentab[] =
65 {
66 	{ "s",		"hort",		(cmdfun)1,	0 },
67 	{ "m",		"edium",	(cmdfun)2,	0 },
68 	{ "l",		"ong",		(cmdfun)4,	0 },
69 	{ "restart",	"",		(cmdfun)0,	0 },
70 	{ NULL,		NULL,		NULL,		0 }
71 };
72 
73 const struct cvntab	Skitab[] =
74 {
75 	{ "n",		"ovice",	(cmdfun)1,	0 },
76 	{ "f",		"air",		(cmdfun)2,	0 },
77 	{ "g",		"ood",		(cmdfun)3,	0 },
78 	{ "e",		"xpert",	(cmdfun)4,	0 },
79 	{ "c",		"ommodore",	(cmdfun)5,	0 },
80 	{ "i",		"mpossible",	(cmdfun)6,	0 },
81 	{ NULL,		NULL,		NULL,		0 }
82 };
83 
84 void
85 setup()
86 {
87 	const struct cvntab	*r;
88 	register int		i, j;
89 	double			f;
90 	int			d;
91 	int			klump;
92 	int			ix, iy;
93 	register struct quad	*q;
94 	struct event		*e;
95 
96 	while (1)
97 	{
98 		r = getcodpar("What length game", Lentab);
99 		Game.length = (long) r->value;
100 		if (Game.length == 0)
101 		{
102 			if (restartgame())
103 				continue;
104 			return;
105 		}
106 		break;
107 	}
108 	r = getcodpar("What skill game", Skitab);
109 	Game.skill = (long) r->value;
110 	Game.tourn = 0;
111 	getstrpar("Enter a password", Game.passwd, 14, 0);
112 	if (strcmp(Game.passwd, "tournament") == 0)
113 	{
114 		getstrpar("Enter tournament code", Game.passwd, 14, 0);
115 		Game.tourn = 1;
116 		d = 0;
117 		for (i = 0; Game.passwd[i]; i++)
118 			d += Game.passwd[i] << i;
119 		srand(d);
120 	}
121 	Param.bases = Now.bases = ranf(6 - Game.skill) + 2;
122 	if (Game.skill == 6)
123 		Param.bases = Now.bases = 1;
124 	Param.time = Now.time = 6.0 * Game.length + 2.0;
125 	i = Game.skill;
126 	j = Game.length;
127 	Param.klings = Now.klings = i * j * 3.5 * (franf() + 0.75);
128 	if (Param.klings < i * j * 5)
129 		Param.klings = Now.klings = i * j * 5;
130 	if (Param.klings <= i)		/* numerical overflow problems */
131 		Param.klings = Now.klings = 127;
132 	Param.energy = Ship.energy = 5000;
133 	Param.torped = Ship.torped = 10;
134 	Ship.ship = ENTERPRISE;
135 	Ship.shipname = "Enterprise";
136 	Param.shield = Ship.shield = 1500;
137 	Param.resource = Now.resource = Param.klings * Param.time;
138 	Param.reserves = Ship.reserves = (6 - Game.skill) * 2.0;
139 	Param.crew = Ship.crew = 387;
140 	Param.brigfree = Ship.brigfree = 400;
141 	Ship.shldup = 1;
142 	Ship.cond = GREEN;
143 	Ship.warp = 5.0;
144 	Ship.warp2 = 25.0;
145 	Ship.warp3 = 125.0;
146 	Ship.sinsbad = 0;
147 	Ship.cloaked = 0;
148 	Param.date = Now.date = (ranf(20) + 20) * 100;
149 	f = Game.skill;
150 	f = log(f + 0.5);
151 	for (i = 0; i < NDEV; i++)
152 		if (Device[i].name[0] == '*')
153 			Param.damfac[i] = 0;
154 		else
155 			Param.damfac[i] = f;
156 	/* these probabilities must sum to 1000 */
157 	Param.damprob[WARP] = 70;	/* warp drive		 7.0% */
158 	Param.damprob[SRSCAN] = 110;	/* short range scanners	11.0% */
159 	Param.damprob[LRSCAN] = 110;	/* long range scanners	11.0% */
160 	Param.damprob[PHASER] = 125;	/* phasers		12.5% */
161 	Param.damprob[TORPED] = 125;	/* photon torpedoes	12.5% */
162 	Param.damprob[IMPULSE] = 75;	/* impulse engines	 7.5% */
163 	Param.damprob[SHIELD] = 150;	/* shield control	15.0% */
164 	Param.damprob[COMPUTER] = 20;	/* computer		 2.0% */
165 	Param.damprob[SSRADIO] = 35;	/* subspace radio	 3.5% */
166 	Param.damprob[LIFESUP] = 30;	/* life support		 3.0% */
167 	Param.damprob[SINS] = 20;	/* navigation system	 2.0% */
168 	Param.damprob[CLOAK] = 50;	/* cloaking device	 5.0% */
169 	Param.damprob[XPORTER] = 80;	/* transporter		 8.0% */
170 	/* check to see that I didn't blow it */
171 	for (i = j = 0; i < NDEV; i++)
172 		j += Param.damprob[i];
173 	if (j != 1000)
174 		errx(1, "Device probabilities sum to %d", j);
175 	Param.dockfac = 0.5;
176 	Param.regenfac = (5 - Game.skill) * 0.05;
177 	if (Param.regenfac < 0.0)
178 		Param.regenfac = 0.0;
179 	Param.warptime = 10;
180 	Param.stopengy = 50;
181 	Param.shupengy = 40;
182 	i = Game.skill;
183 	Param.klingpwr = 100 + 150 * i;
184 	if (i >= 6)
185 		Param.klingpwr += 150;
186 	Param.phasfac = 0.8;
187 	Param.hitfac = 0.5;
188 	Param.klingcrew = 200;
189 	Param.srndrprob = 0.0035;
190 	Param.moveprob[KM_OB] = 45;
191 	Param.movefac[KM_OB] = .09;
192 	Param.moveprob[KM_OA] = 40;
193 	Param.movefac[KM_OA] = -0.05;
194 	Param.moveprob[KM_EB] = 40;
195 	Param.movefac[KM_EB] = 0.075;
196 	Param.moveprob[KM_EA] = 25 + 5 * Game.skill;
197 	Param.movefac[KM_EA] = -0.06 * Game.skill;
198 	Param.moveprob[KM_LB] = 0;
199 	Param.movefac[KM_LB] = 0.0;
200 	Param.moveprob[KM_LA] = 10 + 10 * Game.skill;
201 	Param.movefac[KM_LA] = 0.25;
202 	Param.eventdly[E_SNOVA] = 0.5;
203 	Param.eventdly[E_LRTB] = 25.0;
204 	Param.eventdly[E_KATSB] = 1.0;
205 	Param.eventdly[E_KDESB] = 3.0;
206 	Param.eventdly[E_ISSUE] = 1.0;
207 	Param.eventdly[E_SNAP] = 0.5;
208 	Param.eventdly[E_ENSLV] = 0.5;
209 	Param.eventdly[E_REPRO] = 2.0;
210 	Param.navigcrud[0] = 1.50;
211 	Param.navigcrud[1] = 0.75;
212 	Param.cloakenergy = 1000;
213 	Param.energylow = 1000;
214 	for (i = 0; i < MAXEVENTS; i++)
215 	{
216 		e = &Event[i];
217 		e->date = 1e50;
218 		e->evcode = 0;
219 	}
220 	xsched(E_SNOVA, 1, 0, 0, 0);
221 	xsched(E_LRTB, Param.klings, 0, 0, 0);
222 	xsched(E_KATSB, 1, 0, 0, 0);
223 	xsched(E_ISSUE, 1, 0, 0, 0);
224 	xsched(E_SNAP, 1, 0, 0, 0);
225 	Ship.sectx = ranf(NSECTS);
226 	Ship.secty = ranf(NSECTS);
227 	Game.killk = Game.kills = Game.killb = 0;
228 	Game.deaths = Game.negenbar = 0;
229 	Game.captives = 0;
230 	Game.killinhab = 0;
231 	Game.helps = 0;
232 	Game.killed = 0;
233 	Game.snap = 0;
234 	Move.endgame = 0;
235 
236 	/* setup stars */
237 	for (i = 0; i < NQUADS; i++)
238 		for (j = 0; j < NQUADS; j++)
239 		{
240 			q = &Quad[i][j];
241 			q->klings = q->bases = 0;
242 			q->scanned = -1;
243 			q->stars = ranf(9) + 1;
244 			q->holes = ranf(3) - q->stars / 5;
245 			q->qsystemname = 0;
246 		}
247 
248 	/* select inhabited starsystems */
249 	for (d = 1; d < NINHAB; d++)
250 	{
251 		do
252 		{
253 			i = ranf(NQUADS);
254 			j = ranf(NQUADS);
255 			q = &Quad[i][j];
256 		} while (q->qsystemname);
257 		q->qsystemname = d;
258 	}
259 
260 	/* position starbases */
261 	for (i = 0; i < Param.bases; i++)
262 	{
263 		while (1)
264 		{
265 			ix = ranf(NQUADS);
266 			iy = ranf(NQUADS);
267 			q = &Quad[ix][iy];
268 			if (q->bases > 0)
269 				continue;
270 			break;
271 		}
272 		q->bases = 1;
273 		Now.base[i].x = ix;
274 		Now.base[i].y = iy;
275 		q->scanned = 1001;
276 		/* start the Enterprise near starbase */
277 		if (i == 0)
278 		{
279 			Ship.quadx = ix;
280 			Ship.quady = iy;
281 		}
282 	}
283 
284 	/* position klingons */
285 	for (i = Param.klings; i > 0; )
286 	{
287 		klump = ranf(4) + 1;
288 		if (klump > i)
289 			klump = i;
290 		while (1)
291 		{
292 			ix = ranf(NQUADS);
293 			iy = ranf(NQUADS);
294 			q = &Quad[ix][iy];
295 			if (q->klings + klump > MAXKLQUAD)
296 				continue;
297 			q->klings += klump;
298 			i -= klump;
299 			break;
300 		}
301 	}
302 
303 	/* initialize this quadrant */
304 	printf("%d Klingons\n%d starbase", Param.klings, Param.bases);
305 	if (Param.bases > 1)
306 		printf("s");
307 	printf(" at %d,%d", Now.base[0].x, Now.base[0].y);
308 	for (i = 1; i < Param.bases; i++)
309 		printf(", %d,%d", Now.base[i].x, Now.base[i].y);
310 	printf("\nIt takes %d units to kill a Klingon\n", Param.klingpwr);
311 	Move.free = 0;
312 	initquad(0);
313 	srscan(1);
314 	attack(0);
315 }
316