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