xref: /netbsd/games/trek/setup.c (revision 3ae4e09a)
1*3ae4e09aSdholland /*	$NetBSD: setup.c,v 1.13 2009/05/25 00:37:27 dholland Exp $	*/
2887dd216Scgd 
361f28255Scgd /*
4887dd216Scgd  * Copyright (c) 1980, 1993
5887dd216Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32ef383c95Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34887dd216Scgd #if 0
35887dd216Scgd static char sccsid[] = "@(#)setup.c	8.1 (Berkeley) 5/31/93";
36887dd216Scgd #else
37*3ae4e09aSdholland __RCSID("$NetBSD: setup.c,v 1.13 2009/05/25 00:37:27 dholland Exp $");
38887dd216Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
41ef383c95Schristos #include <stdio.h>
42ef383c95Schristos #include <math.h>
4332330650Smatt #include <string.h>
44ef383c95Schristos #include <unistd.h>
45ef383c95Schristos #include <stdlib.h>
46ef383c95Schristos #include <err.h>
476fb30065Schristos #include <limits.h>
4861f28255Scgd #include "trek.h"
4961f28255Scgd #include "getpar.h"
5061f28255Scgd 
5161f28255Scgd /*
5261f28255Scgd **  INITIALIZE THE GAME
5361f28255Scgd **
5461f28255Scgd **	The length, skill, and password are read, and the game
5561f28255Scgd **	is initialized.  It is far too difficult to describe all
5661f28255Scgd **	that goes on in here, but it is all straight-line code;
5761f28255Scgd **	give it a look.
5861f28255Scgd **
5961f28255Scgd **	Game restart and tournament games are handled here.
6061f28255Scgd */
6161f28255Scgd 
6250b862e2Sdholland const struct cvntab	Lentab[] = {
63ef383c95Schristos 	{ "s",		"hort",		(cmdfun)1,	0 },
64ef383c95Schristos 	{ "m",		"edium",	(cmdfun)2,	0 },
65ef383c95Schristos 	{ "l",		"ong",		(cmdfun)4,	0 },
66ef383c95Schristos 	{ "restart",	"",		(cmdfun)0,	0 },
67ef383c95Schristos 	{ NULL,		NULL,		NULL,		0 }
6861f28255Scgd };
6961f28255Scgd 
7050b862e2Sdholland const struct cvntab	Skitab[] = {
71ef383c95Schristos 	{ "n",		"ovice",	(cmdfun)1,	0 },
72ef383c95Schristos 	{ "f",		"air",		(cmdfun)2,	0 },
73ef383c95Schristos 	{ "g",		"ood",		(cmdfun)3,	0 },
74ef383c95Schristos 	{ "e",		"xpert",	(cmdfun)4,	0 },
75ef383c95Schristos 	{ "c",		"ommodore",	(cmdfun)5,	0 },
76ef383c95Schristos 	{ "i",		"mpossible",	(cmdfun)6,	0 },
77ef383c95Schristos 	{ NULL,		NULL,		NULL,		0 }
7861f28255Scgd };
7961f28255Scgd 
80ef383c95Schristos void
setup(void)81bf0917b6Sdholland setup(void)
8261f28255Scgd {
83b7e11e5cShubertf 	const struct cvntab		*r;
84ef383c95Schristos 	int		i, j;
8561f28255Scgd 	double			f;
8661f28255Scgd 	int			d;
8761f28255Scgd 	int			klump;
8861f28255Scgd 	int			ix, iy;
89ef383c95Schristos 	struct quad	*q;
9061f28255Scgd 	struct event		*e;
9161f28255Scgd 
9250b862e2Sdholland 	while (1) {
9361f28255Scgd 		r = getcodpar("What length game", Lentab);
94c4816c32Scgd 		Game.length = (long) r->value;
9550b862e2Sdholland 		if (Game.length == 0) {
9661f28255Scgd 			if (restartgame())
9761f28255Scgd 				continue;
9861f28255Scgd 			return;
9961f28255Scgd 		}
10061f28255Scgd 		break;
10161f28255Scgd 	}
10261f28255Scgd 	r = getcodpar("What skill game", Skitab);
103c4816c32Scgd 	Game.skill = (long) r->value;
10461f28255Scgd 	Game.tourn = 0;
10561f28255Scgd 	getstrpar("Enter a password", Game.passwd, 14, 0);
10650b862e2Sdholland 	if (strcmp(Game.passwd, "tournament") == 0) {
10761f28255Scgd 		getstrpar("Enter tournament code", Game.passwd, 14, 0);
10861f28255Scgd 		Game.tourn = 1;
10961f28255Scgd 		d = 0;
11061f28255Scgd 		for (i = 0; Game.passwd[i]; i++)
11161f28255Scgd 			d += Game.passwd[i] << i;
112*3ae4e09aSdholland 		srandom(d);
11361f28255Scgd 	}
11461f28255Scgd 	Param.bases = Now.bases = ranf(6 - Game.skill) + 2;
11561f28255Scgd 	if (Game.skill == 6)
11661f28255Scgd 		Param.bases = Now.bases = 1;
11761f28255Scgd 	Param.time = Now.time = 6.0 * Game.length + 2.0;
11861f28255Scgd 	i = Game.skill;
11961f28255Scgd 	j = Game.length;
12061f28255Scgd 	Param.klings = Now.klings = i * j * 3.5 * (franf() + 0.75);
12161f28255Scgd 	if (Param.klings < i * j * 5)
12261f28255Scgd 		Param.klings = Now.klings = i * j * 5;
12361f28255Scgd 	if (Param.klings <= i)		/* numerical overflow problems */
12461f28255Scgd 		Param.klings = Now.klings = 127;
12561f28255Scgd 	Param.energy = Ship.energy = 5000;
12661f28255Scgd 	Param.torped = Ship.torped = 10;
12761f28255Scgd 	Ship.ship = ENTERPRISE;
12861f28255Scgd 	Ship.shipname = "Enterprise";
12961f28255Scgd 	Param.shield = Ship.shield = 1500;
13061f28255Scgd 	Param.resource = Now.resource = Param.klings * Param.time;
13161f28255Scgd 	Param.reserves = Ship.reserves = (6 - Game.skill) * 2.0;
13261f28255Scgd 	Param.crew = Ship.crew = 387;
13361f28255Scgd 	Param.brigfree = Ship.brigfree = 400;
13461f28255Scgd 	Ship.shldup = 1;
13561f28255Scgd 	Ship.cond = GREEN;
13661f28255Scgd 	Ship.warp = 5.0;
13761f28255Scgd 	Ship.warp2 = 25.0;
13861f28255Scgd 	Ship.warp3 = 125.0;
13961f28255Scgd 	Ship.sinsbad = 0;
14061f28255Scgd 	Ship.cloaked = 0;
14161f28255Scgd 	Param.date = Now.date = (ranf(20) + 20) * 100;
14261f28255Scgd 	f = Game.skill;
14361f28255Scgd 	f = log(f + 0.5);
14461f28255Scgd 	for (i = 0; i < NDEV; i++)
14561f28255Scgd 		if (Device[i].name[0] == '*')
14661f28255Scgd 			Param.damfac[i] = 0;
14761f28255Scgd 		else
14861f28255Scgd 			Param.damfac[i] = f;
14961f28255Scgd 	/* these probabilities must sum to 1000 */
15061f28255Scgd 	Param.damprob[WARP] = 70;	/* warp drive		 7.0% */
15161f28255Scgd 	Param.damprob[SRSCAN] = 110;	/* short range scanners	11.0% */
15261f28255Scgd 	Param.damprob[LRSCAN] = 110;	/* long range scanners	11.0% */
15361f28255Scgd 	Param.damprob[PHASER] = 125;	/* phasers		12.5% */
15461f28255Scgd 	Param.damprob[TORPED] = 125;	/* photon torpedoes	12.5% */
15561f28255Scgd 	Param.damprob[IMPULSE] = 75;	/* impulse engines	 7.5% */
15661f28255Scgd 	Param.damprob[SHIELD] = 150;	/* shield control	15.0% */
15761f28255Scgd 	Param.damprob[COMPUTER] = 20;	/* computer		 2.0% */
15861f28255Scgd 	Param.damprob[SSRADIO] = 35;	/* subspace radio	 3.5% */
15961f28255Scgd 	Param.damprob[LIFESUP] = 30;	/* life support		 3.0% */
16061f28255Scgd 	Param.damprob[SINS] = 20;	/* navigation system	 2.0% */
16161f28255Scgd 	Param.damprob[CLOAK] = 50;	/* cloaking device	 5.0% */
16261f28255Scgd 	Param.damprob[XPORTER] = 80;	/* transporter		 8.0% */
16361f28255Scgd 	/* check to see that I didn't blow it */
16461f28255Scgd 	for (i = j = 0; i < NDEV; i++)
16561f28255Scgd 		j += Param.damprob[i];
16661f28255Scgd 	if (j != 1000)
167ef383c95Schristos 		errx(1, "Device probabilities sum to %d", j);
16861f28255Scgd 	Param.dockfac = 0.5;
16961f28255Scgd 	Param.regenfac = (5 - Game.skill) * 0.05;
17061f28255Scgd 	if (Param.regenfac < 0.0)
17161f28255Scgd 		Param.regenfac = 0.0;
17261f28255Scgd 	Param.warptime = 10;
17361f28255Scgd 	Param.stopengy = 50;
17461f28255Scgd 	Param.shupengy = 40;
17561f28255Scgd 	i = Game.skill;
17661f28255Scgd 	Param.klingpwr = 100 + 150 * i;
17761f28255Scgd 	if (i >= 6)
17861f28255Scgd 		Param.klingpwr += 150;
17961f28255Scgd 	Param.phasfac = 0.8;
18061f28255Scgd 	Param.hitfac = 0.5;
18161f28255Scgd 	Param.klingcrew = 200;
18261f28255Scgd 	Param.srndrprob = 0.0035;
18361f28255Scgd 	Param.moveprob[KM_OB] = 45;
18461f28255Scgd 	Param.movefac[KM_OB] = .09;
18561f28255Scgd 	Param.moveprob[KM_OA] = 40;
18661f28255Scgd 	Param.movefac[KM_OA] = -0.05;
18761f28255Scgd 	Param.moveprob[KM_EB] = 40;
18861f28255Scgd 	Param.movefac[KM_EB] = 0.075;
18961f28255Scgd 	Param.moveprob[KM_EA] = 25 + 5 * Game.skill;
19061f28255Scgd 	Param.movefac[KM_EA] = -0.06 * Game.skill;
19161f28255Scgd 	Param.moveprob[KM_LB] = 0;
19261f28255Scgd 	Param.movefac[KM_LB] = 0.0;
19361f28255Scgd 	Param.moveprob[KM_LA] = 10 + 10 * Game.skill;
19461f28255Scgd 	Param.movefac[KM_LA] = 0.25;
19561f28255Scgd 	Param.eventdly[E_SNOVA] = 0.5;
19661f28255Scgd 	Param.eventdly[E_LRTB] = 25.0;
19761f28255Scgd 	Param.eventdly[E_KATSB] = 1.0;
19861f28255Scgd 	Param.eventdly[E_KDESB] = 3.0;
19961f28255Scgd 	Param.eventdly[E_ISSUE] = 1.0;
20061f28255Scgd 	Param.eventdly[E_SNAP] = 0.5;
20161f28255Scgd 	Param.eventdly[E_ENSLV] = 0.5;
20261f28255Scgd 	Param.eventdly[E_REPRO] = 2.0;
20361f28255Scgd 	Param.navigcrud[0] = 1.50;
20461f28255Scgd 	Param.navigcrud[1] = 0.75;
20561f28255Scgd 	Param.cloakenergy = 1000;
20661f28255Scgd 	Param.energylow = 1000;
20750b862e2Sdholland 	for (i = 0; i < MAXEVENTS; i++) {
20861f28255Scgd 		e = &Event[i];
2096fb30065Schristos 		e->date = TOOLARGE;
21061f28255Scgd 		e->evcode = 0;
21161f28255Scgd 	}
21261f28255Scgd 	xsched(E_SNOVA, 1, 0, 0, 0);
21361f28255Scgd 	xsched(E_LRTB, Param.klings, 0, 0, 0);
21461f28255Scgd 	xsched(E_KATSB, 1, 0, 0, 0);
21561f28255Scgd 	xsched(E_ISSUE, 1, 0, 0, 0);
21661f28255Scgd 	xsched(E_SNAP, 1, 0, 0, 0);
21761f28255Scgd 	Ship.sectx = ranf(NSECTS);
21861f28255Scgd 	Ship.secty = ranf(NSECTS);
21961f28255Scgd 	Game.killk = Game.kills = Game.killb = 0;
22061f28255Scgd 	Game.deaths = Game.negenbar = 0;
22161f28255Scgd 	Game.captives = 0;
22261f28255Scgd 	Game.killinhab = 0;
22361f28255Scgd 	Game.helps = 0;
22461f28255Scgd 	Game.killed = 0;
22561f28255Scgd 	Game.snap = 0;
22661f28255Scgd 	Move.endgame = 0;
22761f28255Scgd 
22861f28255Scgd 	/* setup stars */
22950b862e2Sdholland 	for (i = 0; i < NQUADS; i++) {
23050b862e2Sdholland 		for (j = 0; j < NQUADS; j++) {
231e62c2681Schristos 			short s5;
23261f28255Scgd 			q = &Quad[i][j];
23361f28255Scgd 			q->klings = q->bases = 0;
23461f28255Scgd 			q->scanned = -1;
23561f28255Scgd 			q->stars = ranf(9) + 1;
236e62c2681Schristos 			q->holes = ranf(3);
237e62c2681Schristos 			s5 = q->stars / 5;
238e62c2681Schristos 			q->holes = q->holes > s5 ? q->holes - s5 : 0;
23961f28255Scgd 			q->qsystemname = 0;
24061f28255Scgd 		}
24150b862e2Sdholland 	}
24261f28255Scgd 
24361f28255Scgd 	/* select inhabited starsystems */
24450b862e2Sdholland 	for (d = 1; d < NINHAB; d++) {
24550b862e2Sdholland 		do {
24661f28255Scgd 			i = ranf(NQUADS);
24761f28255Scgd 			j = ranf(NQUADS);
24861f28255Scgd 			q = &Quad[i][j];
24961f28255Scgd 		} while (q->qsystemname);
25061f28255Scgd 		q->qsystemname = d;
25161f28255Scgd 	}
25261f28255Scgd 
25361f28255Scgd 	/* position starbases */
25450b862e2Sdholland 	for (i = 0; i < Param.bases; i++) {
25550b862e2Sdholland 		while (1) {
25661f28255Scgd 			ix = ranf(NQUADS);
25761f28255Scgd 			iy = ranf(NQUADS);
25861f28255Scgd 			q = &Quad[ix][iy];
25961f28255Scgd 			if (q->bases > 0)
26061f28255Scgd 				continue;
26161f28255Scgd 			break;
26261f28255Scgd 		}
26361f28255Scgd 		q->bases = 1;
26461f28255Scgd 		Now.base[i].x = ix;
26561f28255Scgd 		Now.base[i].y = iy;
26661f28255Scgd 		q->scanned = 1001;
26761f28255Scgd 		/* start the Enterprise near starbase */
26850b862e2Sdholland 		if (i == 0) {
26961f28255Scgd 			Ship.quadx = ix;
27061f28255Scgd 			Ship.quady = iy;
27161f28255Scgd 		}
27261f28255Scgd 	}
27361f28255Scgd 
27461f28255Scgd 	/* position klingons */
27550b862e2Sdholland 	for (i = Param.klings; i > 0; ) {
27661f28255Scgd 		klump = ranf(4) + 1;
27761f28255Scgd 		if (klump > i)
27861f28255Scgd 			klump = i;
27950b862e2Sdholland 		while (1) {
28061f28255Scgd 			ix = ranf(NQUADS);
28161f28255Scgd 			iy = ranf(NQUADS);
28261f28255Scgd 			q = &Quad[ix][iy];
28361f28255Scgd 			if (q->klings + klump > MAXKLQUAD)
28461f28255Scgd 				continue;
28561f28255Scgd 			q->klings += klump;
28661f28255Scgd 			i -= klump;
28761f28255Scgd 			break;
28861f28255Scgd 		}
28961f28255Scgd 	}
29061f28255Scgd 
29161f28255Scgd 	/* initialize this quadrant */
29261f28255Scgd 	printf("%d Klingons\n%d starbase", Param.klings, Param.bases);
29361f28255Scgd 	if (Param.bases > 1)
29461f28255Scgd 		printf("s");
29561f28255Scgd 	printf(" at %d,%d", Now.base[0].x, Now.base[0].y);
29661f28255Scgd 	for (i = 1; i < Param.bases; i++)
29761f28255Scgd 		printf(", %d,%d", Now.base[i].x, Now.base[i].y);
29861f28255Scgd 	printf("\nIt takes %d units to kill a Klingon\n", Param.klingpwr);
29961f28255Scgd 	Move.free = 0;
30061f28255Scgd 	initquad(0);
30161f28255Scgd 	srscan(1);
30261f28255Scgd 	attack(0);
30361f28255Scgd }
304