setup.c 8.4 KB

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