startrek.c 39 KB

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