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