Abstract out damage checks.
[super-star-trek.git] / src / planets.c
1 #include "sst.h"
2
3 static char *classes[] = {"M","N","O"};
4
5 static int consumeTime(void) 
6 {
7 /* I think most of this avoidance was caused by overlay scheme.
8    Let's see what happens if all events can occur here */
9
10 //  double asave;
11     game.ididit = 1;
12 #if 0
13     /* Don't worry about this */
14     if (scheduled(FTBEAM) <= game.state.date+game.optime && game.state.remcom != 0 && game.condit != IHDOCKED) {
15         /* We are about to be tractor beamed -- operation fails */
16         return 1;
17     }
18 #endif
19 //      asave = scheduled(FSNOVA);
20 //      unschedule(FSNOVA); /* defer supernovas */
21     events();   /* Used to avoid if FSCMOVE is scheduled within time */
22 //      schedule(FSNOVA, asave-game.state.time);
23     /*fails if game over, quadrant super-novas or we've moved to new quadrant*/
24     if (game.alldone || game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova || game.justin != 0) return 1;
25     return 0;
26 }
27
28 void preport(void) 
29 {
30     bool iknow = false;
31     int i;
32     skip(1);
33     chew();
34     prout("Spock-  \"Planet report follows, Captain.\"");
35     skip(1);
36     for (i = 0; i < game.inplan; i++) {
37         if ((game.state.plnets[i].known != unknown
38             && game.state.plnets[i].crystals != 0)
39             || (idebug && game.state.plnets[i].w.x !=0)
40             ) {
41             iknow = true;
42             if (idebug && game.state.plnets[i].known==unknown)
43                 proutn("(Unknown) ");
44             proutn(cramlc(quadrant, game.state.plnets[i].w));
45             proutn("   class ");
46             proutn(classes[game.state.plnets[i].pclass]);
47             proutn("   ");
48             if (game.state.plnets[i].crystals <= 0) proutn("no ");
49             prout("dilithium crystals present.");
50             if (game.state.plnets[i].known==shuttle_down) 
51                 prout("    Shuttle Craft Galileo on surface.");
52         }
53     }
54     if (iknow==0) prout("No information available.");
55 }
56
57 void orbit(void) 
58 {
59     skip(1);
60     chew();
61     if (game.inorbit) {
62         prout("Already in standard orbit.");
63         return;
64     }
65     if (damaged(DWARPEN) && damaged(DIMPULS)) {
66         prout("Both warp and impulse engines damaged.");
67         return;
68     }
69     if (game.plnet.x == 0 || abs(game.sector.x-game.plnet.x) > 1 || abs(game.sector.y-game.plnet.y) > 1) {
70         crmshp();
71         prout(" not adjacent to planet.");
72         skip(1);
73         return;
74     }
75     game.optime = 0.02+0.03*Rand();
76     prout("Helmsman Sulu-  \"Entering standard orbit, Sir.\"");
77     newcnd();
78     if (consumeTime()) return;
79     game.height = (1400.0+7200.0*Rand());
80     prout("Sulu-  \"Entered orbit at altitude %.2f kilometers.\"", game.height);
81     game.inorbit = true;
82     game.ididit = true;
83 }
84
85 void sensor(void) 
86 {
87     skip(1);
88     chew();
89     if (damaged(DSRSENS)) {
90         prout("Short range sensors damaged.");
91         return;
92     }
93     if (!game.plnet.x && (game.options & OPTION_TTY)) {
94         prout("Spock- \"No planet in this quadrant, Captain.\"");
95         return;
96     }
97     if ((game.plnet.x != 0)&& (game.state.plnets[game.iplnet].known == unknown)) {
98         prout("Spock-  \"Sensor scan for %s-", cramlc(quadrant, game.quadrant));
99         skip(1);
100         prout("         Planet at %s is of class %s.",
101               cramlc(sector,game.plnet),
102               classes[game.state.plnets[game.iplnet].pclass]);
103         if (game.state.plnets[game.iplnet].known==shuttle_down) 
104             prout("         Sensors show Galileo still on surface.");
105         proutn("         Readings indicate");
106         if (game.state.plnets[game.iplnet].crystals == 0) proutn(" no");
107         prout(" dilithium crystals present.\"");
108         if (game.state.plnets[game.iplnet].known == unknown) game.state.plnets[game.iplnet].known = known;
109     }
110 }
111
112 void beam(void) 
113 {
114     chew();
115     skip(1);
116     if (damaged(DTRANSP)) {
117         prout("Transporter damaged.");
118         if (!damaged(DSHUTTL) && (game.state.plnets[game.iplnet].known==shuttle_down || game.iscraft == 1)) {
119             skip(1);
120             proutn("Spock-  \"May I suggest the shuttle craft, Sir?\" ");
121             if (ja() != 0) shuttle();
122         }
123         return;
124     }
125     if (!game.inorbit) {
126         crmshp();
127         prout(" not in standard orbit.");
128         return;
129     }
130     if (game.shldup) {
131         prout("Impossible to transport through shields.");
132         return;
133     }
134     if (game.state.plnets[game.iplnet].known==unknown) {
135         prout("Spock-  \"Captain, we have no information on this planet");
136         prout("  and Starfleet Regulations clearly state that in this situation");
137         prout("  you may not go down.\"");
138         return;
139     }
140     if (game.landed==1) {
141         /* Coming from planet */
142         if (game.state.plnets[game.iplnet].known==shuttle_down) {
143             proutn("Spock-  \"Wouldn't you rather take the Galileo?\" ");
144             if (ja() != 0) {
145                 chew();
146                 return;
147             }
148             prout("Your crew hides the Galileo to prevent capture by aliens.");
149         }
150         prout("Landing party assembled, ready to beam up.");
151         skip(1);
152         prout("Kirk whips out communicator...");
153         prouts("BEEP  BEEP  BEEP");
154         skip(2);
155         prout("\"Kirk to enterprise-  Lock on coordinates...energize.\"");
156     }
157     else {
158         /* Going to planet */
159         if (game.state.plnets[game.iplnet].crystals==0) {
160             prout("Spock-  \"Captain, I fail to see the logic in");
161             prout("  exploring a planet with no dilithium crystals.");
162             proutn("  Are you sure this is wise?\" ");
163             if (ja()==0) {
164                 chew();
165                 return;
166             }
167         }
168         prout("Scotty-  \"Transporter room ready, Sir.\"");
169         skip(1);
170         prout("Kirk, and landing party prepare to beam down to planet surface.");
171         skip(1);
172         prout("Kirk-  \"Energize.\"");
173     }
174     game.ididit=1;
175     skip(1);
176     prouts("WWHOOOIIIIIRRRRREEEE.E.E.  .  .  .  .   .    .");
177     skip(2);
178     if (Rand() > 0.98) {
179         prouts("BOOOIIIOOOIIOOOOIIIOIING . . .");
180         skip(2);
181         prout("Scotty-  \"Oh my God!  I've lost them.\"");
182         finish(FLOST);
183         return;
184     }
185     prouts(".    .   .  .  .  .  .E.E.EEEERRRRRIIIIIOOOHWW");
186     skip(2);
187     prout("Transport complete.");
188     game.landed = -game.landed;
189     if (game.landed==1 && game.state.plnets[game.iplnet].known==shuttle_down) {
190         prout("The shuttle craft Galileo is here!");
191     }
192     if (game.landed!=1 && game.imine==1) {
193         game.icrystl = 1;
194         game.cryprob = 0.05;
195     }
196     game.imine = 0;
197     return;
198 }
199
200 void mine(void) 
201 {
202     skip(1);
203     chew();
204     if (game.landed!= 1) {
205         prout("Mining party not on planet.");
206         return;
207     }
208     if (game.state.plnets[game.iplnet].crystals == MINED) {
209         prout("This planet has already been strip-mined for dilithium.");
210         return;
211     }
212     else if (game.state.plnets[game.iplnet].crystals == 0) {
213         prout("No dilithium crystals on this planet.");
214         return;
215     }
216     if (game.imine == 1) {
217         prout("You've already mined enough crystals for this trip.");
218         return;
219     }
220     if (game.icrystl == 1 && game.cryprob == 0.05) {
221         proutn("With all those fresh crystals aboard the ");
222         crmshp();
223         skip(1);
224         prout("there's no reason to mine more at this time.");
225         return;
226     }
227     game.optime = (0.1+0.2*Rand())*game.state.plnets[game.iplnet].pclass;
228     if (consumeTime()) return;
229     prout("Mining operation complete.");
230     game.state.plnets[game.iplnet].crystals = MINED;
231     game.imine = 1;
232     game.ididit=1;
233 }
234
235 void usecrystals(void) 
236 {
237     game.ididit=0;
238     skip(1);
239     chew();
240     if (game.icrystl!=1) {
241         prout("No dilithium crystals available.");
242         return;
243     }
244     if (game.energy >= 1000) {
245         prout("Spock-  \"Captain, Starfleet Regulations prohibit such an operation");
246         prout("  except when game.condition Yellow exists.");
247         return;
248     }
249     prout("Spock- \"Captain, I must warn you that loading");
250     prout("  raw dilithium crystals into the ship's power");
251     prout("  system may risk a severe explosion.");
252     proutn("  Are you sure this is wise?\" ");
253     if (ja()==0) {
254         chew();
255         return;
256     }
257     skip(1);
258     prout("Engineering Officer Scott-  \"(GULP) Aye Sir.");
259     prout("  Mr. Spock and I will try it.\"");
260     skip(1);
261     prout("Spock-  \"Crystals in place, Sir.");
262     prout("  Ready to activate circuit.\"");
263     skip(1);
264     prouts("Scotty-  \"Keep your fingers crossed, Sir!\"");
265     skip(1);
266     if (Rand() <= game.cryprob) {
267         prouts("  \"Activating now! - - No good!  It's***");
268         skip(2);
269         prouts("***RED ALERT!  RED A*L********************************");
270         skip(1);
271         stars();
272         prouts("******************   KA-BOOM!!!!   *******************");
273         skip(1);
274         kaboom();
275         return;
276     }
277     game.energy += 5000.0*(1.0 + 0.9*Rand());
278     prouts("  \"Activating now! - - ");
279     prout("The instruments");
280     prout("   are going crazy, but I think it's");
281     prout("   going to work!!  Congratulations, Sir!\"");
282     game.cryprob *= 2.0;
283     game.ididit=1;
284 }
285
286 void shuttle(void) 
287 {
288     chew();
289     skip(1);
290     if(damaged(DSHUTTL)) {
291         if (game.damage[DSHUTTL] == -1.0) {
292             if (game.inorbit && game.state.plnets[game.iplnet].known == shuttle_down)
293                 prout("Ye Faerie Queene has no shuttle craft bay to dock it at.");
294             else
295                 prout("Ye Faerie Queene had no shuttle craft.");
296         }
297         else if (game.damage[DSHUTTL] > 0)
298             prout("The Galileo is damaged.");
299         else /* game.damage[DSHUTTL] < 0 */ 
300             prout("Shuttle craft is now serving Big Macs.");
301         return;
302     }
303     if (!game.inorbit) {
304         crmshp();
305         prout(" not in standard orbit.");
306         return;
307     }
308     if ((game.state.plnets[game.iplnet].known != shuttle_down) && game.iscraft != 1) {
309         prout("Shuttle craft not currently available.");
310         return;
311     }
312     if (game.landed==-1 && game.state.plnets[game.iplnet].known==shuttle_down) {
313         prout("You will have to beam down to retrieve the shuttle craft.");
314         return;
315     }
316     if (game.shldup || game.condit == IHDOCKED) {
317         prout("Shuttle craft cannot pass through shields.");
318         return;
319     }
320     if (game.state.plnets[game.iplnet].known==unknown) {
321         prout("Spock-  \"Captain, we have no information on this planet");
322         prout("  and Starfleet Regulations clearly state that in this situation");
323         prout("  you may not fly down.\"");
324         return;
325     }
326     game.optime = 3.0e-5*game.height;
327     if (game.optime >= 0.8*game.state.remtime) {
328         prout("First Officer Spock-  \"Captain, I compute that such");
329         proutn("  a maneuver would require approximately 2d%% of our",
330                (int)(100*game.optime/game.state.remtime));
331         prout("remaining time.");
332         proutn("Are you sure this is wise?\" ");
333         if (ja()==0) {
334             game.optime = 0.0;
335             return;
336         }
337     }
338     if (game.landed == 1) {
339         /* Kirk on planet */
340         if (game.iscraft==1) {
341             /* Galileo on ship! */
342             if (!damaged(DTRANSP)) {
343                 proutn("Spock-  \"Would you rather use the transporter?\" ");
344                 if (ja() != 0) {
345                     beam();
346                     return;
347                 }
348                 proutn("Shuttle crew");
349             }
350             else
351                 proutn("Rescue party");
352             prout(" boards Galileo and swoops toward planet surface.");
353             game.iscraft = 0;
354             skip(1);
355             if (consumeTime()) return;
356             game.state.plnets[game.iplnet].known=shuttle_down;
357             prout("Trip complete.");
358             return;
359         }
360         else {
361             /* Ready to go back to ship */
362             prout("You and your mining party board the");
363             prout("shuttle craft for the trip back to the Enterprise.");
364             skip(1);
365             prout("The short hop begins . . .");
366             game.state.plnets[game.iplnet].known=known;
367             game.icraft = 1;
368             skip(1);
369             game.landed = -1;
370             if (consumeTime()) return;
371             game.iscraft = 1;
372             game.icraft = 0;
373             if (game.imine!=0) {
374                 game.icrystl = 1;
375                 game.cryprob = 0.05;
376             }
377             game.imine = 0;
378             prout("Trip complete.");
379             return;
380         }
381     }
382     else {
383         /* Kirk on ship */
384         /* and so is Galileo */
385         prout("Mining party assembles in the hangar deck,");
386         prout("ready to board the shuttle craft \"Galileo\".");
387         skip(1);
388         prouts("The hangar doors open; the trip begins.");
389         skip(1);
390         game.icraft = 1;
391         game.iscraft = 0;
392         if (consumeTime()) return;
393         game.state.plnets[game.iplnet].known = shuttle_down;
394         game.landed = 1;
395         game.icraft = 0;
396         prout("Trip complete");
397         return;
398     }
399 }
400
401 void deathray(void) 
402 {
403     double dprob, r = Rand();
404         
405     game.ididit = 0;
406     skip(1);
407     chew();
408     if (game.ship != IHE) {
409         prout("Ye Faerie Queene has no death ray.");
410         return;
411     }
412     if (game.nenhere==0) {
413         prout("Sulu-  \"But Sir, there are no enemies in this quadrant.\"");
414         return;
415     }
416     if (damaged(DDRAY)) {
417         prout("Death Ray is damaged.");
418         return;
419     }
420     prout("Spock-  \"Captain, the 'Experimental Death Ray'");
421     prout("  is highly unpredictible.  Considering the alternatives,");
422     proutn("  are you sure this is wise?\" ");
423     if (ja()==0) return;
424     prout("Spock-  \"Acknowledged.\"");
425     skip(1);
426     game.ididit=1;
427     prouts("WHOOEE ... WHOOEE ... WHOOEE ... WHOOEE");
428     skip(1);
429     prout("Crew scrambles in emergency preparation.");
430     prout("Spock and Scotty ready the death ray and");
431     prout("prepare to channel all ship's power to the device.");
432     skip(1);
433     prout("Spock-  \"Preparations complete, sir.\"");
434     prout("Kirk-  \"Engage!\"");
435     skip(1);
436     prouts("WHIRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR");
437     skip(1);
438     dprob = .30;
439     if (game.options & OPTION_PLAIN)
440         dprob = .5;
441     if (r > dprob) {
442         prouts("Sulu- \"Captain!  It's working!\"");
443         skip(2);
444         while (game.nenhere > 0)
445             deadkl(game.ks[1], game.quad[game.ks[1].x][game.ks[1].y],game.ks[1].x,game.ks[1].y);
446         prout("Ensign Chekov-  \"Congratulations, Captain!\"");
447         if (KLINGREM == 0) finish(FWON);
448         if ((game.options & OPTION_PLAIN) == 0) {
449             prout("Spock-  \"Captain, I believe the `Experimental Death Ray'");
450             if (Rand() <= 0.05) {
451                 prout("   is still operational.\"");
452             }
453             else {
454                 prout("   has been rendered nonfunctional.\"");
455                 game.damage[DDRAY] = 39.95;
456             }
457         }
458         return;
459     }
460     r = Rand(); // Pick failure method 
461     if (r <= .30) {
462         prouts("Sulu- \"Captain!  It's working!\"");
463         skip(1);
464         prouts("***RED ALERT!  RED ALERT!");
465         skip(1);
466         prout("***MATTER-ANTIMATTER IMPLOSION IMMINENT!");
467         skip(1);
468         prouts("***RED ALERT!  RED A*L********************************");
469         skip(1);
470         stars();
471         prouts("******************   KA-BOOM!!!!   *******************");
472         skip(1);
473         kaboom();
474         return;
475     }
476     if (r <= .55) {
477         prouts("Sulu- \"Captain!  Yagabandaghangrapl, brachriigringlanbla!\"");
478         skip(1);
479         prout("Lt. Uhura-  \"Graaeek!  Graaeek!\"");
480         skip(1);
481         prout("Spock-  \"Fascinating!  . . . All humans aboard");
482         prout("  have apparently been transformed into strange mutations.");
483         prout("  Vulcans do not seem to be affected.");
484         skip(1);
485         prout("Kirk-  \"Raauch!  Raauch!\"");
486         finish(FDRAY);
487         return;
488     }
489     if (r <= 0.75) {
490         int i,j;
491         prouts("Sulu- \"Captain!  It's   --WHAT?!?!\"");
492         skip(2);
493         proutn("Spock-  \"I believe the word is");
494         prouts(" *ASTONISHING*");
495         prout(" Mr. Sulu.");
496         for_sectors(i)
497             for_sectors(j)
498                 if (game.quad[i][j] == IHDOT) game.quad[i][j] = IHQUEST;
499         prout("  Captain, our quadrant is now infested with");
500         prouts(" - - - - - -  *THINGS*.");
501         skip(1);
502         prout("  I have no logical explanation.\"");
503         return;
504     }
505     prouts("Sulu- \"Captain!  The Death Ray is creating tribbles!\"");
506     skip(1);
507     prout("Scotty-  \"There are so many tribbles down here");
508     prout("  in Engineering, we can't move for 'em, Captain.\"");
509     finish(FTRIBBLE);
510     return;
511 }
512
513 char *systemname(int pindx)
514 {
515     static char *names[NINHAB] =
516     {
517         /*
518          * I used <http://www.memory-alpha.org> to find planets
519          * with references in ST:TOS.  Eath and the Alpha Centauri
520          * Colony have been omitted.
521          *
522          * Some planets marked Class G and P here will be displayed as class M
523          * because of the way planets are generated. This is a known bug.
524          */
525         "ERROR",
526         // Federation Worlds
527         "Andoria (Fesoan)",     /* several episodes */
528         "Tellar Prime (Miracht)",       /* TOS: "Journey to Babel" */
529         "Vulcan (T'Khasi)",     /* many episodes */
530         "Medusa",               /* TOS: "Is There in Truth No Beauty?" */
531         "Argelius II (Nelphia)",/* TOS: "Wolf in the Fold" ("IV" in BSD) */
532         "Ardana",               /* TOS: "The Cloud Minders" */
533         "Catulla (Cendo-Prae)", /* TOS: "The Way to Eden" */
534         "Gideon",               /* TOS: "The Mark of Gideon" */
535         "Aldebaran III",        /* TOS: "The Deadly Years" */
536         "Alpha Majoris I",      /* TOS: "Wolf in the Fold" */
537         "Altair IV",            /* TOS: "Amok Time */
538         "Ariannus",             /* TOS: "Let That Be Your Last Battlefield" */
539         "Benecia"               /* TOS: "The Conscience of the King" */
540         "Beta Niobe I (Sarpeidon)",     /* TOS: "All Our Yesterdays" */
541         "Alpha Carinae II",     /* TOS: "The Ultimate Computer" */
542         "Capella IV (Kohath)",  /* TOS: "Friday's Child" (Class G) */
543         "Daran V",              /* TOS: "For the World is Hollow and I Have Touched the Sky" */
544         "Deneb II",             /* TOS: "Wolf in the Fold" ("IV" in BSD) */
545         "Eminiar VII",          /* TOS: "A Taste of Armageddon" */
546         "Gamma Canaris IV",     /* TOS: "Metamorphosis" */
547         "Gamma Tranguli VI (Vaalel)",   /* TOS: "The Apple" */
548         "Ingraham B",           /* TOS: "Operation: Annihilate" */
549         "Janus IV",             /* TOS: "The Devil in the Dark" */
550         "Makus III",            /* TOS: "The Galileo Seven" */
551         "Marcos XII",           /* TOS: "And the Children Shall Lead", */
552         "Omega IV",             /* TOS: "The Omega Glory" */
553         "Regulus V",            /* TOS: "Amok Time */
554         "Deeva",                /* TOS: "Operation -- Annihilate!" */
555         /* Worlds from BSD Trek */
556         "Rigel II",             /* TOS: "Shore Leave" ("III" in BSD) */
557         "Beta III",             /* TOS: "The Return of the Archons" */
558         "Triacus",              /* TOS: "And the Children Shall Lead", */
559         "Exo III",              /* TOS: "What Are Little Girls Made Of?" (Class P) */
560 #if 0
561         // Others
562         "Hansen's Planet",      /* TOS: "The Galileo Seven" */
563         "Taurus IV",            /* TOS: "The Galileo Seven" (class G) */
564         "Antos IV (Doraphane)", /* TOS: "Whom Gods Destroy", "Who Mourns for Adonais?" */
565         "Izar",                 /* TOS: "Whom Gods Destroy" */
566         "Tiburon",              /* TOS: "The Way to Eden" */
567         "Merak II",             /* TOS: "The Cloud Minders" */ 
568         "Coridan (Desotriana)", /* TOS: "Journey to Babel" */
569         "Iotia",                /* TOS: "A Piece of the Action" */
570 #endif
571    };
572
573     return names[pindx];
574 }