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