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