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