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