startrek.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. /*
  2. * startrek.c
  3. *
  4. * Super Star Trek Classic (v1.1)
  5. * Retro Star Trek Game
  6. * C Port Copyright (C) 1996 <Chris Nystrom>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * in any way that you wish. _Star Trek_ is a trademark of Paramount
  10. * I think.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * This is a C port of an old BASIC program: the classic Super Star Trek
  17. * game. It comes from the book _BASIC Computer Games_ edited by David Ahl
  18. * of Creative Computing fame. It was published in 1978 by Workman Publishing,
  19. * 1 West 39 Street, New York, New York, and the ISBN is: 0-89489-052-3.
  20. *
  21. * See http://www.cactus.org/~nystrom/startrek.html for more info.
  22. *
  23. * Contact Author of C port at:
  24. *
  25. * Chris Nystrom
  26. * 1013 Prairie Dove Circle
  27. * Austin, Texas 78758
  28. *
  29. * E-Mail: cnystrom@gmail.com, nystrom@cactus.org
  30. *
  31. * BASIC -> Conversion Issues
  32. *
  33. * - String Names changed from A$ to sA
  34. * - Arrays changed from G(8,8) to g[9][9] so indexes can
  35. * stay the same.
  36. *
  37. * Here is the original BASIC header:
  38. *
  39. * SUPER STARTREK - MAY 16, 1978 - REQUIRES 24K MEMORY
  40. *
  41. *** **** STAR TREK **** ****
  42. *** SIMULATION OF A MISSION OF THE STARSHIP ENTERPRISE,
  43. *** AS SEEN ON THE STAR TREK TV SHOW.
  44. *** ORIGINAL PROGRAM BY MIKE MAYFIELD, MODIFIED VERSION
  45. *** PUBLISHED IN DEC'S "101 BASIC GAMES", BY DAVE AHL.
  46. *** MODIFICATIONS TO THE LATTER (PLUS DEBUGGING) BY BOB
  47. *** LEEDOM - APRIL & DECEMBER 1974,
  48. *** WITH A LITTLE HELP FROM HIS FRIENDS . . .
  49. *** COMMENTS, EPITHETS, AND SUGGESTIONS SOLICITED --
  50. *** SEND TO: R. C. LEEDOM
  51. *** WESTINGHOUSE DEFENSE & ELECTRONICS SYSTEMS CNTR.
  52. *** BOX 746, M.S. 338
  53. *** BALTIMORE, MD 21203
  54. ***
  55. *** CONVERTED TO MICROSOFT 8 K BASIC 3/16/78 BY JOHN BORDERS
  56. *** LINE NUMBERS FROM VERSION STREK7 OF 1/12/75 PRESERVED AS
  57. *** MUCH AS POSSIBLE WHILE USING MULTIPLE STATMENTS PER LINE
  58. *
  59. */
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #include <math.h>
  64. #include <time.h>
  65. #ifndef FALSE
  66. #define FALSE 0
  67. #endif
  68. #ifndef TRUE
  69. #define TRUE ! FALSE
  70. #endif
  71. /* Standard Line Length */
  72. #define MAXLEN 255
  73. /* Standard Terminal Sizes */
  74. #define MAXROW 24
  75. #define MAXCOL 80
  76. /* Standard Page Size */
  77. #define MAXLINES 66
  78. /* Useful typedefs */
  79. typedef int bool;
  80. typedef char line[MAXCOL];
  81. typedef char string[MAXLEN];
  82. /* Function Declarations */
  83. void intro(void);
  84. void new_game(void);
  85. void initialize(void);
  86. void new_quadrant(void);
  87. void course_control(void);
  88. void complete_maneuver(void);
  89. void exceed_quadrant_limits(void);
  90. void maneuver_energy(void);
  91. void short_range_scan(void);
  92. void long_range_scan(void);
  93. void phaser_control(void);
  94. void photon_torpedoes(void);
  95. void torpedo_hit(void);
  96. void damage_control(void);
  97. void sheild_control(void);
  98. void library_computer(void);
  99. void galactic_record(void);
  100. void status_report(void);
  101. void torpedo_data(void);
  102. void nav_data(void);
  103. void dirdist_calc(void);
  104. void galaxy_map(void);
  105. void end_of_time(void);
  106. void resign_commision(void);
  107. void won_game(void);
  108. void end_of_game(void);
  109. void klingons_move(void);
  110. void klingons_shoot(void);
  111. void repair_damage(void);
  112. void find_empty_place(void);
  113. void insert_in_quadrant(void);
  114. void get_device_name(void);
  115. void string_compare(void);
  116. void quadrant_name(void);
  117. int function_d(int i);
  118. int function_r(void);
  119. void mid_str(char *a, char *b, int x, int y);
  120. int cint(double d);
  121. void compute_vector(void);
  122. void sub1(void);
  123. void sub2(void);
  124. void showfile(char *filename);
  125. int openfile(char * sFilename, char * sMode);
  126. void closefile(void);
  127. int getline(char *s);
  128. void randomize(void);
  129. int get_rand(int iSpread);
  130. double rnd(void);
  131. /* Global Variables */
  132. int b3; /* Starbases in Quadrant */
  133. int b4, b5; /* Starbase Location in sector */
  134. int b9; /* Total Starbases */
  135. /* @@@ int c[2][10] = */ /* Used for location and movement */
  136. int c[3][10] = /* modified to match MS BASIC array indicies */
  137. {
  138. { 0 },
  139. { 0, 0, -1, -1, -1, 0, 1, 1, 1, 0 },
  140. { 1, 1, 1, 0, -1, -1, -1, 0, 1, 1 }
  141. };
  142. int d0; /* Docked flag */
  143. int d1; /* Damage Repair Flag */
  144. int e; /* Current Energy */
  145. int e0 = 3000; /* Starting Energy */
  146. int g[9][9]; /* Galaxy */
  147. int g5; /* Quadrant name flag */
  148. int k[4][4]; /* Klingon Data */
  149. int k3; /* Klingons in Quadrant */
  150. int k7; /* Klingons at start */
  151. int k9; /* Total Klingons left */
  152. int n; /* Number of secors to travel */
  153. int p; /* Photon Torpedoes left */
  154. int p0 = 10; /* Photon Torpedo capacity */
  155. int q1, q2; /* Quadrant Position of Enterprise */
  156. int r1, r2; /* Temporary Location Corrdinates */
  157. int s; /* Current shield value */
  158. int s3; /* Stars in quadrant */
  159. int s8; /* Quadrant locating index */
  160. int s9 = 200; /* Klingon Power */
  161. int t0; /* Starting Stardate */
  162. int t9; /* End of time */
  163. int z[9][9]; /* Cumulative Record of Galaxy */
  164. int z3; /* string_compare return value */
  165. int z1, z2; /* Temporary Sector Coordinates */
  166. int z4, z5; /* Temporary quadrant coordinates */
  167. double a, c1; /* Used by Library Computer */
  168. double d[9]; /* Damage Array */
  169. double d4; /* Used for computing damage repair time */
  170. double s1, s2; /* Current Sector Position of Enterprise */
  171. double t; /* Current Stardate */
  172. double w1; /* Warp Factor */
  173. double x, y, x1, x2; /* Navigational coordinates */
  174. char sA[4]; /* An Object in a Sector */
  175. char sC[7]; /* Condition */
  176. char sQ[194]; /* Visual Display of Quadrant */
  177. string sG2; /* Used to pass string results */
  178. FILE *stream;
  179. bool bFlag = FALSE; /* Prevent multiple file opens */
  180. void
  181. reads(char* buffer)
  182. {
  183. fflush(stdout);
  184. gets(buffer);
  185. }
  186. /* Main Program */
  187. int
  188. main(void)
  189. {
  190. intro();
  191. new_game();
  192. /* @@@ exit(0); */ /* causes a warning in C++ */
  193. return(0);
  194. }
  195. void
  196. intro(void)
  197. {
  198. string sTemp;
  199. printf ("\n\n");
  200. printf (" *************************************\n");
  201. printf (" * *\n");
  202. printf (" * *\n");
  203. printf (" * * * Super Star Trek * * *\n");
  204. printf (" * *\n");
  205. printf (" * *\n");
  206. printf (" *************************************\n\n\n\n\n");
  207. printf("\nDo you need instructions (y/n): ");
  208. reads(sTemp);
  209. if (sTemp[0] == 'y' || sTemp[0] == 'Y')
  210. showfile("startrek.doc");
  211. printf ("\n\n\n\n\n\n\n");
  212. printf(" ------*------\n");
  213. printf(" ------------- `--- ------'\n");
  214. printf(" `-------- --' / /\n");
  215. printf(" \\\\------- --\n");
  216. printf(" '-----------'\n");
  217. printf("\n The USS Enterprise --- NCC - 1701\n\n\n");
  218. randomize();
  219. t = (get_rand(20) + 20) * 100;
  220. }
  221. void
  222. new_game(void)
  223. {
  224. string sTemp;
  225. initialize();
  226. new_quadrant();
  227. short_range_scan();
  228. while(1)
  229. {
  230. if (s + e <= 10 && (e < 10 || d[7] < 0))
  231. {
  232. printf("\n** Fatal Error ** ");
  233. printf("You've just stranded your ship in space.\n\n");
  234. printf("You have insufficient maneuvering energy,");
  235. printf(" and Shield Control is presently\n");
  236. printf("incapable of cross circuiting to engine room!!\n\n");
  237. end_of_time();
  238. }
  239. printf("Command? ");
  240. reads(sTemp);
  241. printf("\n");
  242. if (! strncmp(sTemp, "nav", 3))
  243. course_control();
  244. else if (! strncmp(sTemp, "srs", 3))
  245. short_range_scan();
  246. else if (! strncmp(sTemp, "lrs", 3))
  247. long_range_scan();
  248. else if (! strncmp(sTemp, "pha", 3))
  249. phaser_control();
  250. else if (! strncmp(sTemp, "tor", 3))
  251. photon_torpedoes();
  252. else if (! strncmp(sTemp, "she", 3))
  253. sheild_control();
  254. else if (! strncmp(sTemp, "dam", 3))
  255. damage_control();
  256. else if (! strncmp(sTemp, "com", 3))
  257. library_computer();
  258. else if (! strncmp(sTemp, "xxx", 3))
  259. resign_commision();
  260. else
  261. {
  262. printf("Enter one of the following:\n\n");
  263. printf(" nav - To Set Course\n");
  264. printf(" srs - Short Range Sensors\n");
  265. printf(" lrs - Long Range Sensors\n");
  266. printf(" pha - Phasers\n");
  267. printf(" tor - Photon Torpedoes\n");
  268. printf(" she - Sheild Control\n");
  269. printf(" dam - Damage Control\n");
  270. printf(" com - Library Computer\n");
  271. printf(" xxx - Resign Command\n");
  272. printf("\n");
  273. }
  274. }
  275. }
  276. void
  277. initialize(void)
  278. {
  279. int i, j;
  280. char sX[2] = "";
  281. char sX0[4] = "is";
  282. /* InItialize time */
  283. /* @@@ t0 = t; */
  284. t0 = (int)t;
  285. t9 = 25 + get_rand(10);
  286. /* Initialize Enterprise */
  287. d0 = 0;
  288. e = e0;
  289. p = p0;
  290. s = 0;
  291. q1 = function_r();
  292. q2 = function_r();
  293. s1 = (double) function_r();
  294. s2 = (double) function_r();
  295. for (i = 1; i <= 8; i++)
  296. d[i] = 0.0;
  297. /* Setup What Exists in Galaxy */
  298. for (i = 1; i <= 8; i++)
  299. for (j = 1; j <= 8; j++)
  300. {
  301. k3 = 0;
  302. z[i][j] = 0;
  303. r1 = get_rand(100);
  304. if (r1 > 98)
  305. k3 = 3;
  306. else if (r1 > 95)
  307. k3 = 2;
  308. else if (r1 > 80)
  309. k3 = 1;
  310. k9 = k9 + k3;
  311. b3 = 0;
  312. if (get_rand(100) > 96)
  313. b3 = 1;
  314. b9 = b9 + b3;
  315. g[i][j] = k3 * 100 + b3 * 10 + function_r();
  316. }
  317. if (k9 > t9)
  318. t9 = k9 + 1;
  319. if (b9 == 0)
  320. {
  321. if (g[q1][q2] < 200)
  322. {
  323. g[q1][q2] = g[q1][q2] + 100;
  324. k9++;
  325. }
  326. g[q1][q2] = g[q1][q2] + 10;
  327. b9++;
  328. q1 = function_r();
  329. q2 = function_r();
  330. }
  331. k7 = k9;
  332. if (b9 != 1)
  333. {
  334. strcpy(sX, "s");
  335. strcpy(sX0, "are");
  336. }
  337. printf("Your orders are as follows:\n\n");
  338. printf(" Destroy the %d Klingon warships which have invaded\n", k9);
  339. printf(" the galaxy before they can attack Federation Headquarters\n");
  340. printf(" on stardate %d. This gives you %d days. There %s\n",
  341. t0 + t9, t9, sX0);
  342. printf(" %d starbase%s in the galaxy for resupplying your ship.\n\n",
  343. b9, sX);
  344. printf("Hit any key to accept command. ");
  345. getchar();
  346. }
  347. void
  348. new_quadrant(void)
  349. {
  350. int i;
  351. z4 = q1;
  352. z5 = q2;
  353. k3 = 0;
  354. b3 = 0;
  355. s3 = 0;
  356. g5 = 0;
  357. d4 = (double) get_rand(100) / 100 / 50;
  358. z[q1][q2] = g[q1][q2];
  359. if (q1 >= 1 && q1 <= 8 && q2 >= 1 && q2 <= 8)
  360. {
  361. quadrant_name();
  362. if (t0 != t)
  363. printf("Now entering %s quadrant...\n\n", sG2);
  364. else
  365. {
  366. printf("\nYour mission begins with your starship located\n");
  367. printf("in the galactic quadrant %s.\n\n", sG2);
  368. }
  369. }
  370. /* @@@ k3 = g[q1][q2] * .01; */
  371. k3 = (int)(g[q1][q2] * .01);
  372. /* @@@ b3 = g[q1][q2] * .1 - 10 * k3; */
  373. b3 = (int)(g[q1][q2] * .1 - 10 * k3);
  374. s3 = g[q1][q2] - 100 * k3 - 10 * b3;
  375. if (k3 > 0)
  376. {
  377. printf("Combat Area Condition Red\n");
  378. if (s < 200)
  379. printf("Shields Dangerously Low\n");
  380. }
  381. for (i = 1; i <= 3; i++)
  382. {
  383. k[i][1] = 0;
  384. k[i][2] = 0;
  385. k[i][3] = 0;
  386. }
  387. for (i = 0; i <= 192; i++)
  388. sQ[i] = ' ';
  389. sQ[193] = '\0';
  390. /* Position Enterprise, then Klingons, Starbases, and stars */
  391. strcpy(sA, "<*>");
  392. /* @@@ z1 = cint(s1); */
  393. z1 = (int)s1;
  394. /* @@@ z2 = cint(s2); */
  395. z2 = (int)s2;
  396. insert_in_quadrant();
  397. if (k3 > 0)
  398. {
  399. for (i = 1; i <= k3; i++)
  400. {
  401. find_empty_place();
  402. strcpy(sA, "+K+");
  403. z1 = r1;
  404. z2 = r2;
  405. insert_in_quadrant();
  406. k[i][1] = r1;
  407. k[i][2] = r2;
  408. k[i][3] = 100 + get_rand(200);
  409. }
  410. }
  411. if (b3 > 0)
  412. {
  413. find_empty_place();
  414. strcpy(sA, ">!<");
  415. z1 = r1;
  416. z2 = r2;
  417. insert_in_quadrant();
  418. b4 = r1;
  419. b5 = r2;
  420. }
  421. for (i = 1; i <= s3; i++)
  422. {
  423. find_empty_place();
  424. strcpy(sA, " * ");
  425. z1 = r1;
  426. z2 = r2;
  427. insert_in_quadrant();
  428. }
  429. }
  430. void
  431. course_control(void)
  432. {
  433. register i;
  434. /* @@@ int c2, c3, q4, q5; */
  435. int q4, q5;
  436. string sTemp;
  437. double c1;
  438. char sX[4] = "8";
  439. printf("Course (0-9): ");
  440. reads(sTemp);
  441. printf("\n");
  442. c1 = atof(sTemp);
  443. if (c1 == 9.0)
  444. c1 = 1.0;
  445. if (c1 < 0 || c1 > 9.0)
  446. {
  447. printf("Lt. Sulu roports:\n");
  448. printf(" Incorrect course data, sir!\n\n");
  449. return;
  450. }
  451. if (d[1] < 0.0)
  452. strcpy(sX, "0.2");
  453. printf("Warp Factor (0-%s): ", sX);
  454. reads(sTemp);
  455. printf("\n");
  456. w1 = atof(sTemp);
  457. if (d[1] < 0.0 && w1 > 0.21)
  458. {
  459. printf("Warp Engines are damaged. ");
  460. printf("Maximum speed = Warp 0.2.\n\n");
  461. return;
  462. }
  463. if (w1 <= 0.0)
  464. return;
  465. if (w1 > 8.1)
  466. {
  467. printf("Chief Engineer Scott reports:\n");
  468. printf(" The engines won't take warp %4.1f!\n\n", w1);
  469. return;
  470. }
  471. n = cint(w1 * 8.0); /* @@@ note: this is a real round in the original basic */
  472. if (e - n < 0)
  473. {
  474. printf("Engineering reports:\n");
  475. printf(" Insufficient energy available for maneuvering");
  476. printf(" at warp %4.1f!\n\n", w1);
  477. if (s >= n && d[7] >= 0.0)
  478. {
  479. printf("Deflector Control Room acknowledges:\n");
  480. printf(" %d units of energy presently deployed to shields.\n", s);
  481. }
  482. return;
  483. }
  484. klingons_move();
  485. repair_damage();
  486. strcpy(sA, " ");
  487. /* @@@ z1 = cint(s1); */
  488. z1 = (int)s1;
  489. /* @@@ z2 = cint(s2); */
  490. z2 = (int)s2;
  491. insert_in_quadrant();
  492. /* @@@ c2 = cint(c1); */
  493. /* @@@ c3 = c2 + 1; */
  494. /* @@@ x1 = c[0][c2] + (c[0][c3] - c[0][c2]) * (c1 - c2); */
  495. /* @@@ x2 = c[1][c2] + (c[1][c3] - c[1][c2]) * (c1 - c2); */
  496. x1 = c[1][(int)c1] + (c[1][(int)c1 + 1] - c[1][(int)c1]) * (c1 - (int)c1);
  497. x2 = c[2][(int)c1] + (c[2][(int)c1 + 1] - c[2][(int)c1]) * (c1 - (int)c1);
  498. x = s1;
  499. y = s2;
  500. q4 = q1;
  501. q5 = q2;
  502. for (i = 1; i <= n; i++)
  503. {
  504. s1 = s1 + x1;
  505. s2 = s2 + x2;
  506. /* @@@ z1 = cint(s1); */
  507. z1 = (int)s1;
  508. /* @@@ z2 = cint(s2); */
  509. z2 = (int)s2;
  510. if (z1 < 1 || z1 >= 9 || z2 < 1 || z2 >= 9)
  511. {
  512. exceed_quadrant_limits();
  513. complete_maneuver();
  514. return;
  515. }
  516. string_compare();
  517. if (z3 != 1) /* Sector not empty */
  518. {
  519. s1 = s1 - x1;
  520. s2 = s2 - x2;
  521. printf("Warp Engines shut down at sector ");
  522. printf("%d, %d due to bad navigation.\n\n", z1, z2);
  523. i = n + 1;
  524. }
  525. }
  526. complete_maneuver();
  527. }
  528. void
  529. complete_maneuver(void)
  530. {
  531. double t8;
  532. strcpy(sA, "<*>");
  533. /* @@@ z1 = cint(s1); */
  534. z1 = (int)s1;
  535. /* @@@ z2 = cint(s2); */
  536. z2 = (int)s2;
  537. insert_in_quadrant();
  538. maneuver_energy();
  539. t8 = 1.0;
  540. if (w1 < 1.0)
  541. t8 = w1;
  542. t = t + t8;
  543. if (t > t0 + t9)
  544. end_of_time();
  545. short_range_scan();
  546. }
  547. void
  548. exceed_quadrant_limits(void)
  549. {
  550. int x5 = 0; /* Outside galaxy flag */
  551. /* @@@ x = (8 * (q1 - 1)) + x + (n * x1); */
  552. x = (8 * q1) + x + (n * x1);
  553. /* @@@ y = (8 * (q2 - 1)) + y + (n * x2); */
  554. y = (8 * q2) + y + (n * x2);
  555. /* @@@ q1 = cint(x / 8.0); */
  556. q1 = (int)(x / 8.0);
  557. /* @@@ q2 = cint(y / 8.0); */
  558. q2 = (int)(y / 8.0);
  559. /* @@@ s1 = x - ((q1 - 1) * 8); */
  560. s1 = x - (q1 * 8);
  561. /* @@@ s2 = y - ((q2 - 1) * 8); */
  562. s2 = y - (q2 * 8);
  563. /* @@@ if (cint(s1) == 0) */
  564. if ((int)s1 == 0)
  565. {
  566. q1 = q1 - 1;
  567. s1 = s1 + 8.0;
  568. }
  569. /* @@@ if (cint(s2) == 0) */
  570. if ((int)s2 == 0)
  571. {
  572. q2 = q2 - 1;
  573. s2 = s2 + 8.0;
  574. }
  575. /* check if outside galaxy */
  576. if (q1 < 1)
  577. {
  578. x5 = 1;
  579. q1 = 1;
  580. s1 = 1.0;
  581. }
  582. if (q1 > 8)
  583. {
  584. x5 = 1;
  585. q1 = 8;
  586. s1 = 8.0;
  587. }
  588. if (q2 < 1)
  589. {
  590. x5 = 1;
  591. q2 = 1;
  592. s2 = 1.0;
  593. }
  594. if (q2 > 8)
  595. {
  596. x5 = 1;
  597. q2 = 8;
  598. s2 = 8.0;
  599. }
  600. if (x5 == 1)
  601. {
  602. printf("LT. Uhura reports:\n");
  603. printf(" Message from Starfleet Command:\n\n");
  604. printf(" Permission to attempt crossing of galactic perimeter\n");
  605. printf(" is hereby *denied*. Shut down your engines.\n\n");
  606. printf("Chief Engineer Scott reports:\n");
  607. /* @@@ printf(" Warp Engines shut down at sector %d, ", cint(s1)); */
  608. printf(" Warp Engines shut down at sector %d, ", (int)s1);
  609. /* @@@ printf("%d of quadrant %d, %d.\n\n", cint(s2), q1, q2); */
  610. printf("%d of quadrant %d, %d.\n\n", (int)s2, q1, q2);
  611. }
  612. /* else
  613. new_quadrant(); @@@ this causes bugs when bouncing off galaxy walls.
  614. basically, if you bounce very far, your quadrant contents
  615. won't match your LRS. Cool huh? */
  616. maneuver_energy();
  617. /* this section has a different order in the original.
  618. t = t + 1;
  619. if (t > t0 + t9)
  620. end_of_time();
  621. */
  622. if (t > t0 + t9)
  623. end_of_time();
  624. /* @@@ what does this do?? It's in the original.
  625. if (8 * q1 + q2 = 8 * q4 + q5)
  626. {
  627. complete_maneuver();
  628. }
  629. */
  630. t = t + 1;
  631. new_quadrant();
  632. }
  633. void
  634. maneuver_energy(void)
  635. {
  636. e = e - n - 10;
  637. if (e >= 0)
  638. return;
  639. printf("Shield Control supplies energy to complete maneuver.\n\n");
  640. s = s + e;
  641. e = 0;
  642. if (s <= 0)
  643. s = 0;
  644. }
  645. void
  646. short_range_scan(void)
  647. {
  648. register i, j;
  649. strcpy(sC, "GREEN");
  650. if (e < e0 * .1)
  651. strcpy(sC, "YELLOW");
  652. if (k3 > 0)
  653. strcpy(sC, "*RED*");
  654. /* @@@ need to clear the docked flag here */
  655. d0 = 0;
  656. /* @@@ for (i = s1 - 1; i <= s1 + 1; i++) */
  657. for (i = (int)(s1 - 1); i <= (int)(s1 + 1); i++)
  658. /* @@@ for (j = s2 - 1; j <= s2 + 1; j++) */
  659. for (j = (int)(s2 - 1); j <= (int)(s2 + 1); j++)
  660. if (i >= 1 && i <= 8 && j >= 1 && j <= 8)
  661. {
  662. strcpy(sA, ">!<");
  663. z1 = i;
  664. z2 = j;
  665. string_compare();
  666. if (z3 == 1)
  667. {
  668. d0 = 1;
  669. strcpy(sC, "DOCKED");
  670. e = e0;
  671. p = p0;
  672. printf("Shields dropped for docking purposes.\n");
  673. s = 0;
  674. }
  675. }
  676. if (d[2] < 0.0)
  677. {
  678. printf("\n*** Short Range Sensors are out ***\n");
  679. return;
  680. }
  681. printf("------------------------\n");
  682. for (i = 0; i < 8; i++)
  683. {
  684. for (j = 0; j < 24; j++)
  685. putchar(sQ[i * 24 + j]);
  686. if (i == 0)
  687. printf(" Stardate %d\n", (int) t);
  688. if (i == 1)
  689. printf(" Condition %s\n", sC);
  690. if (i == 2)
  691. printf(" Quadrant %d, %d\n", q1, q2);
  692. if (i == 3)
  693. /* @@@ printf(" Sector %d, %d\n", cint(s1), cint(s2)); */
  694. printf(" Sector %d, %d\n", (int)s1, (int)s2);
  695. if (i == 4)
  696. printf(" Photon Torpedoes %d\n", p);
  697. if (i == 5)
  698. printf(" Total Energy %d\n", e + s);
  699. if (i == 6)
  700. printf(" Shields %d\n", s);
  701. if (i == 7)
  702. printf(" Klingons Remaining %d\n", k9);
  703. }
  704. printf("------------------------\n\n");
  705. return;
  706. }
  707. void
  708. long_range_scan(void)
  709. {
  710. register i, j;
  711. if (d[3] < 0.0)
  712. {
  713. printf("Long Range Sensors are inoperable.\n");
  714. return;
  715. }
  716. printf("Long Range Scan for Quadrant %d, %d\n\n", q1, q2);
  717. for (i = q1 - 1; i <= q1 + 1; i++)
  718. {
  719. printf("--------------------\n:");
  720. for (j = q2 - 1; j <= q2 + 1; j++)
  721. if (i > 0 && i <= 8 && j > 0 && j <= 8)
  722. {
  723. z[i][j] = g[i][j];
  724. printf(" %3.3d :", z[i][j]);
  725. }
  726. else
  727. printf(" *** :");
  728. printf("\n");
  729. }
  730. printf("--------------------\n\n");
  731. }
  732. void
  733. phaser_control(void)
  734. {
  735. register i;
  736. int iEnergy;
  737. int h1, h;
  738. string sTemp;
  739. if (d[4] < 0.0)
  740. {
  741. printf("Phasers Inoperative\n\n");
  742. return;
  743. }
  744. if (k3 <= 0)
  745. {
  746. printf("Science Officer Spock reports:\n");
  747. printf(" 'Sensors show no enemy ships in this quadrant'\n\n");
  748. return;
  749. }
  750. if (d[8] < 0.0)
  751. /* @@@ printf("Computer failure happers accuracy.\n"); */
  752. printf("Computer failure hampers accuracy.\n");
  753. printf("Phasers locked on target;\n");
  754. printf("Energy available = %d units\n\n", e);
  755. printf("Number of units to fire: ");
  756. reads(sTemp);
  757. printf("\n");
  758. iEnergy = atoi(sTemp);
  759. if (iEnergy <= 0)
  760. return;
  761. if (e - iEnergy < 0)
  762. {
  763. printf("Not enough energy available.\n\n");
  764. return;
  765. }
  766. e = e - iEnergy;
  767. if (d[8] < 0.0)
  768. /* @@@ iEnergy = iEnergy * rnd(); */
  769. iEnergy = (int)(iEnergy * rnd());
  770. h1 = iEnergy / k3;
  771. for (i = 1; i <= 3; i++)
  772. {
  773. if (k[i][3] > 0)
  774. {
  775. /* @@@ h = (h1 / function_d(0) * (rnd() + 2)); */
  776. h = (int)(h1 / function_d(0) * (rnd() + 2));
  777. if (h <= .15 * k[i][3])
  778. {
  779. printf("Sensors show no damage to enemy at ");
  780. printf("%d, %d\n\n", k[i][1], k[i][2]);
  781. }
  782. else
  783. {
  784. k[i][3] = k[i][3] - h;
  785. printf("%d unit hit on Klingon at sector ", h);
  786. printf("%d, %d\n", k[i][1], k[i][2]);
  787. if (k[i][3] <= 0)
  788. {
  789. printf("*** Klingon Destroyed ***\n\n");
  790. k3--;
  791. k9--;
  792. z1 = k[i][1];
  793. z2 = k[i][2];
  794. strcpy(sA, " ");
  795. insert_in_quadrant();
  796. k[i][3] = 0;
  797. g[q1][q2] = g[q1][q2] - 100;
  798. z[q1][q2] = g[q1][q2];
  799. if (k9 <= 0)
  800. won_game();
  801. }
  802. else
  803. /* @@@ printf("\n"); */
  804. printf(" (Sensors show %d units remaining.)\n\n", k[i][3]);
  805. }
  806. }
  807. }
  808. klingons_shoot();
  809. }
  810. void
  811. photon_torpedoes(void)
  812. {
  813. /* @@@ int c2, c3, x3, y3, x5; */
  814. int x3, y3, x5;
  815. string sTemp;
  816. double c1;
  817. if (p <= 0)
  818. {
  819. printf("All photon torpedoes expended\n");
  820. return;
  821. }
  822. if (d[5] < 0.0)
  823. {
  824. printf("Photon Tubes not operational\n");
  825. return;
  826. }
  827. printf("Course (0-9): ");
  828. reads(sTemp);
  829. printf("\n");
  830. c1 = atof(sTemp);
  831. if (c1 == 9.0)
  832. c1 = 1.0;
  833. /* @@@ if (c1 < 0 || c1 > 9.0) */
  834. if (c1 < 1.0 || c1 > 9.0)
  835. {
  836. printf("Ensign Chekov roports:\n");
  837. printf(" Incorrect course data, sir!\n\n");
  838. return;
  839. }
  840. e = e - 2;
  841. p--;
  842. /* @@@ c2 = cint(c1); */
  843. /* @@@ c3 = c2 + 1; */
  844. /* @@@ x1 = c[0][c2] + (c[0][c3] - c[0][c2]) * (c1 - c2); */
  845. /* @@@ x2 = c[1][c2] + (c[1][c3] - c[1][c2]) * (c1 - c2); */
  846. x1 = c[1][(int)c1] + (c[1][(int)c1 + 1] - c[1][(int)c1]) * (c1 - (int)c1);
  847. x2 = c[2][(int)c1] + (c[2][(int)c1 + 1] - c[2][(int)c1]) * (c1 - (int)c1);
  848. x = s1 + x1;
  849. y = s2 + x2;
  850. x3 = cint(x); /* @@@ note: this is a true integer round in the MS BASIC version */
  851. y3 = cint(y); /* @@@ note: this is a true integer round in the MS BASIC version */
  852. x5 = 0;
  853. printf("Torpedo Track:\n");
  854. while (x3 >= 1 && x3 <= 8 && y3 >= 1 && y3 <= 8)
  855. {
  856. printf(" %d, %d\n", x3, y3);
  857. strcpy(sA, " ");
  858. z1 = x3;
  859. z2 = y3;
  860. string_compare();
  861. if (z3 == 0)
  862. {
  863. torpedo_hit();
  864. klingons_shoot();
  865. return;
  866. }
  867. x = x + x1;
  868. y = y + x2;
  869. x3 = cint(x); /* @@@ note: this is a true integer round in the MS BASIC version */
  870. y3 = cint(y); /* @@@ note: this is a true integer round in the MS BASIC version */
  871. }
  872. printf("Torpedo Missed\n\n");
  873. klingons_shoot();
  874. }
  875. void
  876. torpedo_hit(void)
  877. {
  878. int i, x3, y3;
  879. x3 = cint(x); /* @@@ note: this is a true integer round in the MS BASIC version */
  880. y3 = cint(y); /* @@@ note: this is a true integer round in the MS BASIC version */
  881. z3 = 0;
  882. strcpy(sA, " * ");
  883. string_compare();
  884. if (z3 == 1)
  885. {
  886. printf("Star at %d, %d absorbed torpedo energy.\n\n", x3, y3);
  887. return;
  888. }
  889. strcpy(sA, "+K+");
  890. string_compare();
  891. if (z3 == 1)
  892. {
  893. printf("*** Klingon Destroyed ***\n\n");
  894. k3--;
  895. k9--;
  896. if (k9 <= 0)
  897. won_game();
  898. for (i=0; i<=3; i++)
  899. if (x3 == k[i][1] && y3 == k[i][2])
  900. k[i][3] = 0;
  901. }
  902. strcpy(sA, ">!<");
  903. string_compare();
  904. if (z3 == 1)
  905. {
  906. printf("*** Starbase Destroyed ***\n");
  907. b3--;
  908. b9--;
  909. if (b9 <= 0 && k9 <= t - t0 - t9)
  910. {
  911. printf("That does it, Captain!!");
  912. printf("You are hereby relieved of command\n");
  913. printf("and sentanced to 99 stardates of hard");
  914. printf("labor on Cygnus 12!!\n");
  915. resign_commision();
  916. }
  917. printf("Starfleet Command reviewing your record to consider\n");
  918. printf("court martial!\n\n");
  919. d0 = 0; /* Undock */
  920. }
  921. z1 = x3;
  922. z2 = y3;
  923. strcpy(sA," ");
  924. insert_in_quadrant();
  925. g[q1][q2] = (k3 * 100) + (b3 * 10) + s3;
  926. z[q1][q2] = g[q1][q2];
  927. }
  928. void
  929. damage_control(void)
  930. {
  931. int a1;
  932. double d3 = 0.0;
  933. register i;
  934. if (d[6] < 0.0)
  935. {
  936. printf("Damage Control report not available.\n");
  937. if (d0 == 0)
  938. return;
  939. d3 = 0.0;
  940. for (i = 1; i <= 8; i++)
  941. if (d[i] < 0.0)
  942. d3 = d3 + .1;
  943. if (d3 == 0.0)
  944. return;
  945. d3 = d3 + d4;
  946. if (d3 >= 1.0)
  947. d3 = 0.9;
  948. printf("\nTechnicians standing by to effect repairs to your");
  949. /* @@@ printf("ship; Will you authorize the repair order (Y/N)? "); */
  950. printf("ship;\nEstimated time to repair: %4.2f stardates.\n", d3);
  951. printf("Will you authorize the repair order (Y/N)? ");
  952. a1 = getchar();
  953. if (a1 == 'Y' || a1 == 'y')
  954. {
  955. for (i = 1; i <= 8; i++)
  956. if (d[i] < 0.0)
  957. d[i] = 0.0;
  958. t = t + d3 + 0.1;
  959. }
  960. }
  961. printf("Device State of Repair\n");
  962. for (r1 = 1; r1 <= 8; r1++)
  963. {
  964. get_device_name();
  965. printf(sG2);
  966. /* @@@ for (i = 1; i < 25 - strlen(sG2); i++) */
  967. for (i = 1; i < 25 - (int)strlen(sG2); i++)
  968. printf(" ");
  969. /* @@@ printf("%4.1f\n", d[r1]); */
  970. printf("%4.2f\n", d[r1]);
  971. }
  972. printf("\n");
  973. }
  974. void
  975. sheild_control(void)
  976. {
  977. int i;
  978. string sTemp;
  979. if (d[7] < 0.0)
  980. {
  981. printf("Sheild Control inoperable\n");
  982. return;
  983. }
  984. printf("Energy available = %d\n\n", e + s);
  985. printf("Input number of units to shields: ");
  986. reads(sTemp);
  987. printf("\n");
  988. i = atoi(sTemp);
  989. if (i < 0 || s == i)
  990. {
  991. printf("<Sheilds Unchanged>\n\n");
  992. return;
  993. }
  994. if (i >= e + s)
  995. {
  996. printf("Sheild Control Reports:\n");
  997. printf(" 'This is not the Federation Treasury.'\n");
  998. printf("<Sheilds Unchanged>\n\n");
  999. return;
  1000. }
  1001. e = e + s - i;
  1002. s = i;
  1003. printf("Deflector Control Room report:\n");
  1004. printf(" 'Shields now at %d units per your command.'\n\n", s);
  1005. }
  1006. void
  1007. library_computer(void)
  1008. {
  1009. string sTemp;
  1010. if (d[8] < 0.0)
  1011. {
  1012. printf("Library Computer inoperable\n");
  1013. return;
  1014. }
  1015. printf("Computer active and awating command: ");
  1016. reads(sTemp);
  1017. printf("\n");
  1018. if (! strncmp(sTemp, "0", 1))
  1019. galactic_record();
  1020. else if (! strncmp(sTemp, "1", 1))
  1021. status_report();
  1022. else if (! strncmp(sTemp, "2", 1))
  1023. torpedo_data();
  1024. else if (! strncmp(sTemp, "3", 1))
  1025. nav_data();
  1026. else if (! strncmp(sTemp, "4", 1))
  1027. dirdist_calc();
  1028. else if (! strncmp(sTemp, "5", 1))
  1029. galaxy_map();
  1030. else
  1031. {
  1032. printf("Functions available from Library-Computer:\n\n");
  1033. printf(" 0 = Cumulative Galactic Record\n");
  1034. printf(" 1 = Status Report\n");
  1035. printf(" 2 = Photon Torpedo Data\n");
  1036. printf(" 3 = Starbase Nav Data\n");
  1037. printf(" 4 = Direction/Distance Calculator\n");
  1038. printf(" 5 = Galaxy 'Region Name' Map\n\n");
  1039. }
  1040. }
  1041. void
  1042. galactic_record(void)
  1043. {
  1044. int i, j;
  1045. printf("\n Computer Record of Galaxy for Quadrant %d,%d\n\n", q1, q2);
  1046. printf(" 1 2 3 4 5 6 7 8\n");
  1047. for (i = 1; i <= 8; i++)
  1048. {
  1049. printf(" ----- ----- ----- ----- ----- ----- ----- -----\n");
  1050. printf("%d", i);
  1051. for (j = 1; j <= 8; j++)
  1052. {
  1053. printf(" ");
  1054. if (z[i][j] == 0)
  1055. printf("***");
  1056. else
  1057. printf("%3.3d", z[i][j]);
  1058. }
  1059. printf("\n");
  1060. }
  1061. printf(" ----- ----- ----- ----- ----- ----- ----- -----\n\n");
  1062. }
  1063. void
  1064. status_report(void)
  1065. {
  1066. char sX[2] = "";
  1067. printf(" Status Report:\n\n");
  1068. if (k9 > 1)
  1069. strcpy(sX, "s");
  1070. printf("Klingon%s Left: %d\n", sX, k9);
  1071. printf("Mission must be completed in %4.1f stardates\n",
  1072. /* @@@ .1 * cint((t0 + t9 - t) * 10)); */
  1073. .1 * (int)((t0 + t9 - t) * 10));
  1074. if (b9 < 1)
  1075. {
  1076. printf("Your stupidity has left you on your own in the galaxy\n");
  1077. printf(" -- you have no starbases left!\n");
  1078. }
  1079. else
  1080. {
  1081. strcpy(sX, "s");
  1082. if (b9 < 2)
  1083. strcpy(sX, "");
  1084. printf("The Federation is maintaining %d starbase%s in the galaxy\n",
  1085. b9, sX);
  1086. }
  1087. printf("\n");
  1088. }
  1089. void
  1090. torpedo_data(void)
  1091. {
  1092. int i;
  1093. char sX[2] = "";
  1094. if (k3 <= 0)
  1095. {
  1096. printf("Science Officer Spock reports:\n");
  1097. printf(" 'Sensors show no enemy ships in this quadrant.'\n\n");
  1098. return;
  1099. }
  1100. if (k3 > 1)
  1101. strcpy(sX, "s");
  1102. printf("From Enterprise to Klingon battlecriuser%s:\n\n", sX);
  1103. for (i = 1; i <= 3; i++)
  1104. {
  1105. if (k[i][3] > 0)
  1106. {
  1107. w1 = k[i][1];
  1108. x = k[i][2];
  1109. c1 = s1;
  1110. a = s2;
  1111. compute_vector();
  1112. }
  1113. }
  1114. }
  1115. void
  1116. nav_data(void)
  1117. {
  1118. if (b3 <= 0)
  1119. {
  1120. printf("Mr. Spock reports,\n");
  1121. printf(" 'Sensors show no starbases in this quadrant.'\n\n");
  1122. return;
  1123. }
  1124. w1 = b4;
  1125. x = b5;
  1126. c1 = s1;
  1127. a = s2;
  1128. compute_vector();
  1129. }
  1130. void
  1131. dirdist_calc(void)
  1132. {
  1133. string sTemp;
  1134. printf("Direction/Distance Calculator\n\n");
  1135. printf("You are at quadrant %d,%d sector %d,%d\n\n", q1, q2,
  1136. /* @@@ cint(s1), cint(s2)); */
  1137. (int)s1, (int)s2);
  1138. printf("Please enter initial X coordinate: ");
  1139. reads(sTemp);
  1140. c1 = atoi(sTemp);
  1141. printf("Please enter initial Y coordinate: ");
  1142. reads(sTemp);
  1143. a = atoi(sTemp);
  1144. printf("Please enter final X coordinate: ");
  1145. reads(sTemp);
  1146. w1 = atoi(sTemp);
  1147. printf("Please enter final Y coordinate: ");
  1148. reads(sTemp);
  1149. x = atoi(sTemp);
  1150. compute_vector();
  1151. }
  1152. void
  1153. galaxy_map(void)
  1154. {
  1155. int i, j, j0;
  1156. g5 = 1;
  1157. printf("\n The Galaxy\n\n");
  1158. printf(" 1 2 3 4 5 6 7 8\n");
  1159. for (i = 1; i <= 8; i++)
  1160. {
  1161. printf(" ----- ----- ----- ----- ----- ----- ----- -----\n");
  1162. printf("%d ", i);
  1163. z4 = i;
  1164. z5 = 1;
  1165. quadrant_name();
  1166. j0 = (int)(11 - (strlen(sG2) / 2));
  1167. for (j = 0; j < j0; j++)
  1168. printf(" ");
  1169. printf(sG2);
  1170. for (j = 0; j < j0; j++)
  1171. printf(" ");
  1172. if (! (strlen(sG2) % 2))
  1173. printf(" ");
  1174. z5 = 5;
  1175. quadrant_name();
  1176. j0 = (int)(12 - (strlen(sG2) / 2));
  1177. for (j = 0; j < j0; j++)
  1178. printf(" ");
  1179. printf(sG2);
  1180. printf("\n");
  1181. }
  1182. printf(" ----- ----- ----- ----- ----- ----- ----- -----\n\n");
  1183. }
  1184. void
  1185. compute_vector(void)
  1186. {
  1187. x = x - a;
  1188. a = c1 - w1;
  1189. if (x <= 0.0)
  1190. {
  1191. if (a > 0.0)
  1192. {
  1193. c1 = 3.0;
  1194. sub2();
  1195. return;
  1196. }
  1197. else
  1198. {
  1199. c1 = 5.0;
  1200. sub1();
  1201. return;
  1202. }
  1203. }
  1204. else if (a < 0.0)
  1205. {
  1206. c1 = 7.0;
  1207. sub2();
  1208. return;
  1209. }
  1210. else
  1211. {
  1212. c1 = 1.0;
  1213. sub1();
  1214. return;
  1215. }
  1216. }
  1217. void
  1218. sub1(void)
  1219. {
  1220. x = fabs(x);
  1221. a = fabs(a);
  1222. if (a <= x)
  1223. printf(" DIRECTION = %4.2f\n", c1 + (a / x));
  1224. else
  1225. printf(" DIRECTION = %4.2f\n", c1 + (((a * 2) - x) / a));
  1226. printf(" DISTANCE = %4.2f\n\n", (x > a) ? x : a);
  1227. }
  1228. void
  1229. sub2(void)
  1230. {
  1231. x = fabs(x);
  1232. a = fabs(a);
  1233. if (a >= x)
  1234. printf(" DIRECTION = %4.2f\n", c1 + (x / a));
  1235. else
  1236. /* @@@ printf(" DIRECTION = %4.2f\n\n", c1 + (((x * 2) - a) / x)); */
  1237. printf(" DIRECTION = %4.2f\n", c1 + (((x * 2) - a) / x));
  1238. /* @@@ printf(" DISTANCE = %4.2f\n", (x > a) ? x : a); */
  1239. printf(" DISTANCE = %4.2f\n\n", (x > a) ? x : a);
  1240. }
  1241. void
  1242. ship_destroyed(void)
  1243. {
  1244. printf("The Enterprise has been destroyed. ");
  1245. printf("The Federation will be conquered.\n\n");
  1246. end_of_time();
  1247. }
  1248. void
  1249. end_of_time(void)
  1250. {
  1251. printf("It is stardate %d.\n\n", (int) t);
  1252. resign_commision();
  1253. }
  1254. void
  1255. resign_commision(void)
  1256. {
  1257. printf("There were %d Klingon Battlecruisers left at the", k9);
  1258. printf(" end of your mission.\n\n");
  1259. end_of_game();
  1260. }
  1261. void
  1262. won_game(void)
  1263. {
  1264. printf("Congradulations, Captain! The last Klingon Battle Cruiser\n");
  1265. printf("menacing the Federation has been destoyed.\n\n");
  1266. if (t - t0 > 0)
  1267. printf("Your efficiency rating is %4.2f\n", 1000 * pow(k7 / (t - t0), 2));
  1268. end_of_game();
  1269. }
  1270. void
  1271. end_of_game(void)
  1272. {
  1273. string sTemp;
  1274. if (b9 > 0)
  1275. {
  1276. printf("The Federation is in need of a new starship commander");
  1277. printf(" for a similar mission.\n");
  1278. printf("If there is a volunteer, let him step forward and");
  1279. printf(" enter 'aye': ");
  1280. reads(sTemp);
  1281. printf("\n");
  1282. if (! strncmp(sTemp, "aye", 3))
  1283. new_game();
  1284. }
  1285. exit(0);
  1286. }
  1287. void
  1288. klingons_move(void)
  1289. {
  1290. int i;
  1291. for (i = 1; i <= 3; i++)
  1292. {
  1293. if (k[i][3] > 0)
  1294. {
  1295. strcpy(sA, " ");
  1296. z1 = k[i][1];
  1297. z2 = k[i][2];
  1298. insert_in_quadrant();
  1299. find_empty_place();
  1300. k[i][1] = z1;
  1301. k[i][2] = z2;
  1302. strcpy(sA, "+K+");
  1303. insert_in_quadrant();
  1304. }
  1305. }
  1306. klingons_shoot();
  1307. }
  1308. void
  1309. klingons_shoot(void)
  1310. {
  1311. int h, i;
  1312. if (k3 <= 0)
  1313. return;
  1314. if (d0 != 0)
  1315. {
  1316. printf("Starbase shields protect the Enterprise\n\n");
  1317. return;
  1318. }
  1319. for (i = 1; i <= 3; i++)
  1320. {
  1321. if (k[i][3] > 0)
  1322. {
  1323. h = (int) ((k[i][3] / function_d(i)) * (2 + rnd()));
  1324. s = s - h;
  1325. /* @@@ k[i][3] = k[i][3] / (3 + rnd()); */
  1326. k[i][3] = (int)(k[i][3] / (3 + rnd()));
  1327. printf("%d unit hit on Enterprise from sector ", h);
  1328. printf("%d, %d\n", k[i][1], k[i][2]);
  1329. if (s <= 0)
  1330. {
  1331. printf("\n");
  1332. ship_destroyed();
  1333. }
  1334. printf(" <Shields down to %d units>\n\n", s);
  1335. if (h >= 20)
  1336. {
  1337. if (rnd() <= 0.6 || (h / s) > 0.2)
  1338. {
  1339. r1 = function_r();
  1340. d[r1] = d[r1] - (h / s) - (0.5 * rnd());
  1341. get_device_name();
  1342. printf("Damage Control reports\n");
  1343. printf(" '%s' damaged by hit\n\n", sG2);
  1344. }
  1345. }
  1346. }
  1347. }
  1348. }
  1349. void
  1350. repair_damage(void)
  1351. {
  1352. int i;
  1353. double d6; /* Repair Factor */
  1354. d6 = w1;
  1355. if (w1 >= 1.0)
  1356. d6 = w1 / 10;
  1357. for (i = 1; i <= 8; i++)
  1358. {
  1359. if (d[i] < 0.0)
  1360. {
  1361. d[i] = d[i] + d6;
  1362. if (d[i] > -0.1 && d[i] < 0)
  1363. d[i] = -0.1;
  1364. else if (d[i] >= 0.0)
  1365. {
  1366. if (d1 != 1)
  1367. d1 = 1;
  1368. printf("Damage Control report:\n");
  1369. r1 = i;
  1370. get_device_name();
  1371. printf(" %s repair completed\n\n", sG2);
  1372. }
  1373. }
  1374. }
  1375. if (rnd() <= 0.2)
  1376. {
  1377. r1 = function_r();
  1378. if (rnd() < .6)
  1379. {
  1380. d[r1] = d[r1] - (rnd() * 5.0 + 1.0);
  1381. printf("Damage Control report:\n");
  1382. get_device_name();
  1383. printf(" %s damaged\n\n", sG2);
  1384. }
  1385. else
  1386. {
  1387. d[r1] = d[r1] + (rnd() * 3.0 + 1.0);
  1388. printf("Damage Control report:\n");
  1389. get_device_name();
  1390. printf(" %s state of repair improved\n\n", sG2);
  1391. }
  1392. }
  1393. }
  1394. /* Misc Functions and Subroutines */
  1395. void
  1396. find_empty_place(void)
  1397. {
  1398. /* @@@ while (z3 == 0) this is a nasty one.*/
  1399. do
  1400. {
  1401. r1 = function_r();
  1402. r2 = function_r();
  1403. strcpy(sA, " ");
  1404. z1 = r1;
  1405. z2 = r2;
  1406. string_compare();
  1407. } while (z3 == 0);
  1408. z3 = 0;
  1409. }
  1410. void
  1411. insert_in_quadrant(void)
  1412. {
  1413. int i, j = 0;
  1414. /* @@@ s8 = ((z2 - 1) * 3) + ((z1 - 1) * 24) + 1; */
  1415. s8 = ((int)(z2 - 0.5) * 3) + ((int)(z1 - 0.5) * 24) + 1;
  1416. for (i = s8 - 1; i <= s8 + 1; i++)
  1417. sQ[i] = sA[j++];
  1418. return;
  1419. }
  1420. void
  1421. get_device_name(void)
  1422. {
  1423. static char * device_name[] = {
  1424. "", "Warp Engines","Short Range Sensors","Long Range Sensors",
  1425. "Phaser Control","Photon Tubes","Damage Control","Sheild Control",
  1426. "Library-Computer"};
  1427. if (r1 < 0 || r1 > 8)
  1428. r1 = 0;
  1429. strcpy(sG2, device_name[r1]);
  1430. return;
  1431. }
  1432. void
  1433. string_compare(void)
  1434. {
  1435. int i;
  1436. char sB[4];
  1437. z1 = (int)(z1 + 0.5);
  1438. z2 = (int)(z2 + 0.5);
  1439. s8 = ((z2 - 1) * 3) + ((z1 - 1) * 24) + 1;
  1440. mid_str(sB, sQ, s8, 3);
  1441. i = strncmp(sB, sA, 3);
  1442. if (i == 0)
  1443. z3 = 1;
  1444. else
  1445. z3 = 0;
  1446. return;
  1447. }
  1448. void
  1449. quadrant_name(void)
  1450. {
  1451. static char * quad_name[] = {"","Antares","Rigel","Procyon","Vega",
  1452. "Canopus","Altair","Sagittarius","Pollux","Sirius","Deneb","Capella",
  1453. "Betelgeuse","Aldebaran","Regulus","Arcturus","Spica"};
  1454. static char * sect_name[] = {""," I"," II"," III"," IV"};
  1455. if (z4 < 1 || z4 > 8 || z5 < 1 || z5 > 8)
  1456. strcpy(sG2, "Unknown");
  1457. if (z5 <= 4)
  1458. strcpy(sG2, quad_name[z4]);
  1459. else
  1460. strcpy(sG2, quad_name[z4+8]);
  1461. if (g5 != 1)
  1462. {
  1463. if (z5 > 4)
  1464. z5 = z5 - 4;
  1465. strcat(sG2, sect_name[z5]);
  1466. }
  1467. return;
  1468. }
  1469. int
  1470. function_d(int i)
  1471. {
  1472. int j;
  1473. /* @@@ j = sqrt(pow((k[i][1] - s1), 2) + pow((k[i][2] - s2), 2)); */
  1474. j = (int)sqrt(pow((k[i][1] - s1), 2) + pow((k[i][2] - s2), 2));
  1475. return j;
  1476. }
  1477. int
  1478. function_r(void)
  1479. {
  1480. return(get_rand(8));
  1481. }
  1482. void
  1483. mid_str(char *a, char *b, int x, int y)
  1484. {
  1485. --x;
  1486. y += x;
  1487. /* @@@ while (x < y && x <= strlen(b)) */
  1488. while (x < y && x <= (int)strlen(b))
  1489. *a++ = *(b + x++);
  1490. *a = '\0';
  1491. }
  1492. /* Round off floating point numbers instead of truncating */
  1493. int
  1494. cint (double d)
  1495. {
  1496. int i;
  1497. i = (int) (d + 0.5);
  1498. return(i);
  1499. }
  1500. void
  1501. showfile(char *filename)
  1502. {
  1503. line lBuffer;
  1504. int iRow = 0;
  1505. if (openfile(filename, "r") != 0)
  1506. return;
  1507. while (getline(lBuffer) != 0)
  1508. {
  1509. printf(lBuffer);
  1510. if (iRow++ > MAXROW - 3)
  1511. {
  1512. getchar();
  1513. iRow = 0;
  1514. }
  1515. }
  1516. closefile();
  1517. }
  1518. int
  1519. openfile(char * sFilename, char * sMode)
  1520. {
  1521. if (bFlag || (stream = fopen (sFilename, sMode)) == NULL)
  1522. {
  1523. fprintf (stderr, "\nError - Unable to open file: %s.\n\n", sFilename);
  1524. return 1;
  1525. }
  1526. bFlag = TRUE;
  1527. return 0;
  1528. }
  1529. void
  1530. closefile(void)
  1531. {
  1532. if (! bFlag)
  1533. fclose(stream);
  1534. bFlag = FALSE;
  1535. }
  1536. int
  1537. getline(char *s)
  1538. {
  1539. fflush(stdout);
  1540. if (fgets(s, MAXCOL, stream) == NULL)
  1541. return(0);
  1542. else
  1543. return(strlen(s));
  1544. }
  1545. /* Seed the randomizer with the timer */
  1546. void
  1547. randomize(void)
  1548. {
  1549. time_t timer;
  1550. srand ((unsigned) time (&timer));
  1551. }
  1552. /* Returns an integer from 1 to iSpread */
  1553. int
  1554. get_rand(int iSpread)
  1555. {
  1556. return((rand() % iSpread) + 1);
  1557. }
  1558. double
  1559. rnd(void)
  1560. {
  1561. double d;
  1562. d = rand() / (double) RAND_MAX;
  1563. return(d);
  1564. }