Hide some differences between 0-origin and 1-origin addressing.
[super-star-trek.git] / 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     ididit = 1;
12 #if 0
13     /* Don't wory about this */
14     if (future[FTBEAM] <= game.state.date+Time && game.state.remcom != 0 && condit != IHDOCKED) {
15         /* We are about to be tractor beamed -- operation fails */
16         return 1;
17     }
18 #endif
19 //      asave = future[FSNOVA];
20 //      future[FSNOVA] = 1e30; /* defer supernovas */
21     events();   /* Used to avoid if future[FSCMOVE] within time */
22 //      future[FSNOVA] = asave;
23     /*fails if game over, quadrant super-novas or we've moved to new quadrant*/
24     if (alldone || game.state.galaxy[quadx][quady].supernova || justin != 0) return 1;
25     return 0;
26 }
27
28 void preport(void) 
29 {
30     int iknow = 0, i;
31     skip(1);
32     chew();
33     prout("Spock-  \"Planet report follows, Captain.\"");
34     skip(1);
35     for (i = 0; i < inplan; i++) {
36         if (game.state.plnets[i].known != unknown
37 #ifdef DEBUG
38             || ( idebug && game.state.plnets[i].x !=0)
39 #endif
40             ) {
41             iknow = 1;
42 #ifdef DEBUG
43             if (idebug && game.state.plnets[i].known==unknown) proutn("(Unknown) ");
44 #endif
45             proutn(cramlc(quadrant, game.state.plnets[i].x, game.state.plnets[i].y));
46             proutn("   class ");
47             proutn(classes[game.state.plnets[i].pclass]);
48             proutn("   ");
49             if (game.state.plnets[i].crystals == 0) proutn("no ");
50             prout("dilithium crystals present.");
51             if (game.state.plnets[i].known==shuttle_down) 
52                 prout("    Shuttle Craft Galileo on surface.");
53         }
54     }
55     if (iknow==0) prout("No information available.");
56 }
57
58 void orbit(void) 
59 {
60     skip(1);
61     chew();
62     if (inorbit!=0) {
63         prout("Already in standard orbit.");
64         return;
65     }
66     if (game.damage[DWARPEN] != 0 && game.damage[DIMPULS] != 0) {
67         prout("Both warp and impulse engines damaged.");
68         return;
69     }
70     if (plnetx == 0 || abs(sectx-plnetx) > 1 || abs(secty-plnety) > 1) {
71         crmshp();
72         prout(" not adjacent to planet.\n");
73         return;
74     }
75     Time = 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     inorbit = 1;
82     ididit=1;
83 }
84
85 void sensor(int force) 
86 {
87     skip(1);
88     chew();
89     if (game.damage[DSRSENS] != 0.0) {
90         prout("Short range sensors damaged.");
91         return;
92     }
93     if (!plnetx && ((game.state.plnets[iplnet].known == unknown || force))) {
94         prout("Spock- \"No planet in this quadrant, Captain.\"");
95         return;
96     }
97     if ((plnetx != 0)&&(game.state.plnets[iplnet].known == unknown)) {
98         prout("Spock-  \"Sensor scan for %s-", cramlc(quadrant, quadx, quady));
99         skip(1);
100         prout("         Planet at %s is of class %s.", 
101               cramlc(sector, plnetx, plnety),
102               classes[game.state.plnets[iplnet].pclass]);
103         if (game.state.plnets[iplnet].known==shuttle_down) 
104             prout("         Sensors show Galileo still on surface.");
105         proutn("         Readings indicate");
106         if (game.state.plnets[iplnet].crystals == 0) proutn(" no");
107         prout(" dilithium crystals present.\"");
108         if (game.state.plnets[iplnet].known == unknown) game.state.plnets[iplnet].known = known;
109     }
110 }
111
112 void beam(void) 
113 {
114     chew();
115     skip(1);
116     if (game.damage[DTRANSP] != 0) {
117         prout("Transporter damaged.");
118         if (game.damage[DSHUTTL]==0 && (game.state.plnets[iplnet].known==shuttle_down || 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 (inorbit==0) {
126         crmshp();
127         prout(" not in standard orbit.");
128         return;
129     }
130     if (shldup!=0) {
131         prout("Impossible to transport through shields.");
132         return;
133     }
134     if (game.state.plnets[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 (landed==1) {
141         /* Coming from planet */
142         if (game.state.plnets[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[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     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     landed = -landed;
189     if (landed==1 && game.state.plnets[iplnet].known==shuttle_down) {
190         prout("The shuttle craft Galileo is here!");
191     }
192     if (landed!=1 && imine==1) {
193         icrystl = 1;
194         cryprob = 0.05;
195     }
196     imine = 0;
197     return;
198 }
199
200 void mine(void) 
201 {
202     skip(1);
203     chew();
204     if (landed!= 1) {
205         prout("Mining party not on planet.");
206         return;
207     }
208     if (game.state.plnets[iplnet].crystals == 0) {
209         prout("No dilithium crystals on this planet.");
210         return;
211     }
212     if (imine == 1) {
213         prout("You've already mined enough crystals for this trip.");
214         return;
215     }
216     if (icrystl == 1 && cryprob == 0.05) {
217         proutn("With all those fresh crystals aboard the ");
218         crmshp();
219         skip(1);
220         prout("there's no reason to mine more at this time.");
221         return;
222     }
223     Time = (0.1+0.2*Rand())*game.state.plnets[iplnet].pclass;
224     if (consumeTime()) return;
225     prout("Mining operation complete.");
226     imine = 1;
227     ididit=1;
228 }
229
230 void usecrystals(void) 
231 {
232     ididit=0;
233     skip(1);
234     chew();
235     if (icrystl!=1) {
236         prout("No dilithium crystals available.");
237         return;
238     }
239     if (energy >= 1000) {
240         prout("Spock-  \"Captain, Starfleet Regulations prohibit such an operation");
241         prout("  except when condition Yellow exists.");
242         return;
243     }
244     prout("Spock- \"Captain, I must warn you that loading");
245     prout("  raw dilithium crystals into the ship's power");
246     prout("  system may risk a severe explosion.");
247     proutn("  Are you sure this is wise?\" ");
248     if (ja()==0) {
249         chew();
250         return;
251     }
252     skip(1);
253     prout("Engineering Officer Scott-  \"(GULP) Aye Sir.");
254     prout("  Mr. Spock and I will try it.\"");
255     skip(1);
256     prout("Spock-  \"Crystals in place, Sir.");
257     prout("  Ready to activate circuit.\"");
258     skip(1);
259     prouts("Scotty-  \"Keep your fingers crossed, Sir!\"");
260     skip(1);
261     if (Rand() <= cryprob) {
262         prouts("  \"Activating now! - - No good!  It's***");
263         skip(2);
264         prouts("***RED ALERT!  RED A*L********************************");
265         skip(1);
266         stars();
267         prouts("******************   KA-BOOM!!!!   *******************");
268         skip(1);
269         kaboom();
270         return;
271     }
272     energy += 5000.0*(1.0 + 0.9*Rand());
273     prouts("  \"Activating now! - - ");
274     prout("The instruments");
275     prout("   are going crazy, but I think it's");
276     prout("   going to work!!  Congratulations, Sir!\"");
277     cryprob *= 2.0;
278     ididit=1;
279 }
280
281 void shuttle(void) 
282 {
283     chew();
284     skip(1);
285     if(game.damage[DSHUTTL] != 0.0) {
286         if (game.damage[DSHUTTL] == -1.0) {
287             if (inorbit && game.state.plnets[iplnet].known == shuttle_down)
288                 prout("Ye Faerie Queene has no shuttle craft bay to dock it at.");
289             else
290                 prout("Ye Faerie Queene had no shuttle craft.");
291         }
292         else if (game.damage[DSHUTTL] > 0)
293             prout("The Galileo is damaged.");
294         else prout("Shuttle craft is now serving Big Macs.");
295         return;
296     }
297     if (inorbit==0) {
298         crmshp();
299         prout(" not in standard orbit.");
300         return;
301     }
302     if ((game.state.plnets[iplnet].known != shuttle_down) && iscraft != 1) {
303         prout("Shuttle craft not currently available.");
304         return;
305     }
306     if (landed==-1 && game.state.plnets[iplnet].known==shuttle_down) {
307         prout("You will have to beam down to retrieve the shuttle craft.");
308         return;
309     }
310     if (shldup!=0 || condit == IHDOCKED) {
311         prout("Shuttle craft cannot pass through shields.");
312         return;
313     }
314     if (game.state.plnets[iplnet].known==unknown) {
315         prout("Spock-  \"Captain, we have no information on this planet");
316         prout("  and Starfleet Regulations clearly state that in this situation");
317         prout("  you may not fly down.\"");
318         return;
319     }
320     Time = 3.0e-5*game.height;
321     if (Time >= 0.8*game.state.remtime) {
322         prout("First Officer Spock-  \"Captain, I compute that such");
323         proutn("  a maneuver would require approximately 2d%% of our",
324                (int)(100*Time/game.state.remtime));
325         prout("remaining time.");
326         proutn("Are you sure this is wise?\" ");
327         if (ja()==0) {
328             Time = 0.0;
329             return;
330         }
331     }
332     if (landed == 1) {
333         /* Kirk on planet */
334         if (iscraft==1) {
335             /* Galileo on ship! */
336             if (game.damage[DTRANSP]==0) {
337                 proutn("Spock-  \"Would you rather use the transporter?\" ");
338                 if (ja() != 0) {
339                     beam();
340                     return;
341                 }
342                 proutn("Shuttle crew");
343             }
344             else
345                 proutn("Rescue party");
346             prout(" boards Galileo and swoops toward planet surface.");
347             iscraft = 0;
348             skip(1);
349             if (consumeTime()) return;
350             game.state.plnets[iplnet].known=shuttle_down;
351             prout("Trip complete.");
352             return;
353         }
354         else {
355             /* Ready to go back to ship */
356             prout("You and your mining party board the");
357             prout("shuttle craft for the trip back to the Enterprise.");
358             skip(1);
359             prout("The short hop begins . . .");
360             game.state.plnets[iplnet].known=known;
361             icraft = 1;
362             skip(1);
363             landed = -1;
364             if (consumeTime()) return;
365             iscraft = 1;
366             icraft = 0;
367             if (imine!=0) {
368                 icrystl = 1;
369                 cryprob = 0.05;
370             }
371             imine = 0;
372             prout("Trip complete.");
373             return;
374         }
375     }
376     else {
377         /* Kirk on ship */
378         /* and so is Galileo */
379         prout("Mining party assembles in the hangar deck,");
380         prout("ready to board the shuttle craft \"Galileo\".");
381         skip(1);
382         prouts("The hangar doors open; the trip begins.");
383         skip(1);
384         icraft = 1;
385         iscraft = 0;
386         if (consumeTime()) return;
387         game.state.plnets[iplnet].known = shuttle_down;
388         landed = 1;
389         icraft = 0;
390         prout("Trip complete");
391         return;
392     }
393 }
394
395 void deathray(void) 
396 {
397     double r = Rand();
398         
399     ididit = 0;
400     skip(1);
401     chew();
402     if (ship != IHE) {
403         prout("Ye Faerie Queene has no death ray.");
404         return;
405     }
406     if (nenhere==0) {
407         prout("Sulu-  \"But Sir, there are no enemies in this quadrant.\"");
408         return;
409     }
410     if (game.damage[DDRAY] > 0.0) {
411         prout("Death Ray is damaged.");
412         return;
413     }
414     prout("Spock-  \"Captain, the 'Experimental Death Ray'");
415     prout("  is highly unpredictible.  Considering the alternatives,");
416     proutn("  are you sure this is wise?\" ");
417     if (ja()==0) return;
418     prout("Spock-  \"Acknowledged.\"");
419     skip(1);
420     ididit=1;
421     prouts("WHOOEE ... WHOOEE ... WHOOEE ... WHOOEE");
422     skip(1);
423     prout("Crew scrambles in emergency preparation.");
424     prout("Spock and Scotty ready the death ray and");
425     prout("prepare to channel all ship's power to the device.");
426     skip(1);
427     prout("Spock-  \"Preparations complete, sir.\"");
428     prout("Kirk-  \"Engage!\"");
429     skip(1);
430     prouts("WHIRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR");
431     skip(1);
432     if (r > .30) {
433         prouts("Sulu- \"Captain!  It's working!\"");
434         skip(2);
435         while (nenhere > 0)
436             deadkl(game.kx[1],game.ky[1],game.quad[game.kx[1]][game.ky[1]],game.kx[1],game.ky[1]);
437         prout("Ensign Chekov-  \"Congratulations, Captain!\"");
438         if (game.state.remkl == 0) finish(FWON);
439         prout("Spock-  \"Captain, I believe the `Experimental Death Ray'");
440         if (Rand() <= 0.05) {
441             prout("   is still operational.\"");
442         }
443         else {
444             prout("   has been rendered nonfunctional.\"");
445             game.damage[DDRAY] = 39.95;
446         }
447         return;
448     }
449     r = Rand(); // Pick failure method 
450     if (r <= .30) {
451         prouts("Sulu- \"Captain!  It's working!\"");
452         skip(1);
453         prouts("***RED ALERT!  RED ALERT!");
454         skip(1);
455         prout("***MATTER-ANTIMATTER IMPLOSION IMMINENT!");
456         skip(1);
457         prouts("***RED ALERT!  RED A*L********************************");
458         skip(1);
459         stars();
460         prouts("******************   KA-BOOM!!!!   *******************");
461         skip(1);
462         kaboom();
463         return;
464     }
465     if (r <= .55) {
466         prouts("Sulu- \"Captain!  Yagabandaghangrapl, brachriigringlanbla!\"");
467         skip(1);
468         prout("Lt. Uhura-  \"Graaeek!  Graaeek!\"");
469         skip(1);
470         prout("Spock-  \"Fascinating!  . . . All humans aboard");
471         prout("  have apparently been transformed into strange mutations.");
472         prout("  Vulcans do not seem to be affected.");
473         skip(1);
474         prout("Kirk-  \"Raauch!  Raauch!\"");
475         finish(FDRAY);
476         return;
477     }
478     if (r <= 0.75) {
479         int i,j;
480         prouts("Sulu- \"Captain!  It's   --WHAT?!?!\"");
481         skip(2);
482         proutn("Spock-  \"I believe the word is");
483         prouts(" *ASTONISHING*");
484         prout(" Mr. Sulu.");
485         for_sectors(i)
486             for_sectors(j)
487                 if (game.quad[i][j] == IHDOT) game.quad[i][j] = IHQUEST;
488         prout("  Captain, our quadrant is now infested with");
489         prouts(" - - - - - -  *THINGS*.");
490         skip(1);
491         prout("  I have no logical explanation.\"");
492         return;
493     }
494     prouts("Sulu- \"Captain!  The Death Ray is creating tribbles!\"");
495     skip(1);
496     prout("Scotty-  \"There are so many tribbles down here");
497     prout("  in Engineering, we can't move for 'em, Captain.\"");
498     finish(FTRIBBLE);
499     return;
500 }