Partial merge of I/O management.
[super-star-trek.git] / planets.c
1 #include "sst.h"
2
3 static int height;
4
5 static char *classes[] = {"M","N","O"};
6
7 static int consumeTime(void) {
8 /* I think most of this avoidance was caused by overlay scheme.
9    Let's see what happens if all events can occur here */
10
11 //      double asave;
12         ididit = 1;
13 #if 0
14         /* Don't wory about this */
15         if (future[FTBEAM] <= game.state.date+Time && game.state.remcom != 0 && condit != IHDOCKED) {
16                 /* We are about to be tractor beamed -- operation fails */
17                 return 1;
18         }
19 #endif
20 //      asave = future[FSNOVA];
21 //      future[FSNOVA] = 1e30; /* defer supernovas */
22         events();       /* Used to avoid if future[FSCMOVE] within time */
23 //      future[FSNOVA] = asave;
24         /*fails if game over, quadrant super-novas or we've moved to new quadrant*/
25         if (alldone || game.state.galaxy[quadx][quady] == 1000 || justin != 0) return 1;
26         return 0;
27 }
28
29 void preport(void) {
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         skip(1);
60         chew();
61         if (inorbit!=0) {
62                 prout("Already in standard orbit.");
63                 return;
64         }
65         if (game.damage[DWARPEN] != 0 && game.damage[DIMPULS] != 0) {
66                 prout("Both warp and impulse engines damaged.");
67                 return;
68         }
69         if (plnetx == 0 || abs(sectx-plnetx) > 1 || abs(secty-plnety) > 1) {
70                 crmshp();
71                 prout(" not adjacient to planet.\n");
72                 return;
73         }
74         Time = 0.02+0.03*Rand();
75         prout("Helmsman Sulu-  \"Entering standard orbit, Sir.\"");
76         newcnd();
77         if (consumeTime()) return;
78         height = (1400.+7200.*Rand());
79         prout("Sulu-  \"Entered orbit at altitude %.2f kilometers.\"", height);
80         inorbit = 1;
81         ididit=1;
82 }
83
84 void sensor(void) {
85 #ifndef SERGEEV
86         skip(1);
87         chew();
88         if (game.damage[DSRSENS] != 0.0) {
89                 prout("Short range sensors damaged.");
90                 return;
91         }
92         if (plnetx == 0) {
93                 prout("No planet in this quadrant.");
94                 return;
95         }
96 #else
97     if (game.damage[DSRSENS] != 0.0) return;
98     if ((plnetx != 0)&&(game.state.plnets[iplnet].known == 0)) {
99         prout("Spock-  \"Sensor scan for %s-", cramlc(quadrant, quadx, quady));
100         skip(1);
101         prout("         Planet at %s is of class %s.", 
102                cramlc(sector, plnetx, plnety),
103                classes[game.state.plnets[iplnet].pclass]);
104         if (game.state.plnets[iplnet].known==shuttle_down) 
105                 prout("         Sensors show Galileo still on surface.");
106         proutn("         Readings indicate");
107         if (game.state.plnets[iplnet].crystals == 0) proutn(" no");
108         prout(" dilithium crystals present.\"");
109         if (game.state.plnets[iplnet].known == unknown) game.state.plnets[iplnet].known = known;
110     }
111 #endif /* SERGEEV */
112 }
113
114 void beam(void) {
115         chew();
116         skip(1);
117         if (game.damage[DTRANSP] != 0) {
118                 prout("Transporter damaged.");
119                 if (game.damage[DSHUTTL]==0 && (game.state.plnets[iplnet].known==shuttle_down || iscraft == 1)) {
120                         skip(1);
121                         proutn("Spock-  \"May I suggest the shuttle craft, Sir?\" ");
122                         if (ja() != 0) shuttle();
123                 }
124                 return;
125         }
126         if (inorbit==0) {
127                 crmshp();
128                 prout(" not in standard orbit.");
129                 return;
130         }
131         if (shldup!=0) {
132                 prout("Impossible to transport through shields.");
133                 return;
134         }
135         if (game.state.plnets[iplnet].known==unknown) {
136                 prout("Spock-  \"Captain, we have no information on this planet");
137                 prout("  and Starfleet Regulations clearly state that in this situation");
138                 prout("  you may not go down.\"");
139                 return;
140         }
141         if (landed==1) {
142                 /* Coming from planet */
143                 if (game.state.plnets[iplnet].known==shuttle_down) {
144                         proutn("Spock-  \"Wouldn't you rather take the Galileo?\" ");
145                         if (ja() != 0) {
146                                 chew();
147                                 return;
148                         }
149                         prout("Your crew hides the Galileo to prevent capture by aliens.");
150                 }
151                 prout("Landing party assembled, ready to beam up.");
152                 skip(1);
153                 prout("Kirk whips out communicator...");
154                 prouts("BEEP  BEEP  BEEP");
155                 skip(2);
156                 prout("\"Kirk to enterprise-  Lock on coordinates...energize.\"");
157         }
158         else {
159                 /* Going to planet */
160                 if (game.state.plnets[iplnet].crystals==0) {
161                         prout("Spock-  \"Captain, I fail to see the logic in");
162                         prout("  exploring a planet with no dilithium crystals.");
163                         proutn("  Are you sure this is wise?\" ");
164                         if (ja()==0) {
165                                 chew();
166                                 return;
167                         }
168                 }
169                 prout("Scotty-  \"Transporter room ready, Sir.\"");
170                 skip(1);
171                 prout("Kirk, and landing party prepare to beam down to planet surface.");
172                 skip(1);
173                 prout("Kirk-  \"Energize.\"");
174         }
175         ididit=1;
176         skip(1);
177         prouts("WWHOOOIIIIIRRRRREEEE.E.E.  .  .  .  .   .    .");
178         skip(2);
179         if (Rand() > 0.98) {
180                 prouts("BOOOIIIOOOIIOOOOIIIOIING . . .");
181                 skip(2);
182                 prout("Scotty-  \"Oh my God!  I've lost them.\"");
183                 finish(FLOST);
184                 return;
185         }
186         prouts(".    .   .  .  .  .  .E.E.EEEERRRRRIIIIIOOOHWW");
187         skip(2);
188         prout("Transport complete.");
189         landed = -landed;
190         if (landed==1 && game.state.plnets[iplnet].known==shuttle_down) {
191                 prout("The shuttle craft Galileo is here!");
192         }
193         if (landed!=1 && imine==1) {
194                 icrystl = 1;
195                 cryprob = 0.05;
196         }
197         imine = 0;
198         return;
199 }
200
201 void mine(void) {
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         ididit=0;
232         skip(1);
233         chew();
234         if (icrystl!=1) {
235                 prout("No dilithium crystals available.");
236                 return;
237         }
238         if (energy >= 1000) {
239                 prout("Spock-  \"Captain, Starfleet Regulations prohibit such an operation");
240                 prout("  except when condition Yellow exists.");
241                 return;
242         }
243         prout("Spock- \"Captain, I must warn you that loading");
244         prout("  raw dilithium crystals into the ship's power");
245         prout("  system may risk a severe explosion.");
246         proutn("  Are you sure this is wise?\" ");
247         if (ja()==0) {
248                 chew();
249                 return;
250         }
251         skip(1);
252         prout("Engineering Officer Scott-  \"(GULP) Aye Sir.");
253         prout("  Mr. Spock and I will try it.\"");
254         skip(1);
255         prout("Spock-  \"Crystals in place, Sir.");
256         prout("  Ready to activate circuit.\"");
257         skip(1);
258         prouts("Scotty-  \"Keep your fingers crossed, Sir!\"");
259         skip(1);
260         if (Rand() <= cryprob) {
261                 prouts("  \"Activating now! - - No good!  It's***");
262                 skip(2);
263                 prouts("***RED ALERT!  RED A*L********************************");
264                 skip(1);
265                 stars();
266                 prouts("******************   KA-BOOM!!!!   *******************");
267                 skip(1);
268                 kaboom();
269                 return;
270         }
271         energy += 5000.0*(1.0 + 0.9*Rand());
272         prouts("  \"Activating now! - - ");
273         prout("The instruments");
274         prout("   are going crazy, but I think it's");
275         prout("   going to work!!  Congratulations, Sir!\"");
276         cryprob *= 2.0;
277         ididit=1;
278 }
279
280 void shuttle(void) {
281
282         chew();
283         skip(1);
284         if(game.damage[DSHUTTL] != 0.0) {
285                 if (game.damage[DSHUTTL] == -1.0) {
286                         if (inorbit && game.state.plnets[iplnet].known == shuttle_down)
287                                 prout("Ye Faerie Queene has no shuttle craft bay to dock it at.");
288                         else
289                                 prout("Ye Faerie Queene had no shuttle craft.");
290                 }
291                 else if (game.damage[DSHUTTL] > 0)
292                         prout("The Galileo is damaged.");
293                 else prout("Shuttle craft is now serving Big Macs.");
294                 return;
295         }
296         if (inorbit==0) {
297                 crmshp();
298                 prout(" not in standard orbit.");
299                 return;
300         }
301         if ((game.state.plnets[iplnet].known != shuttle_down) && iscraft != 1) {
302                 prout("Shuttle craft not currently available.");
303                 return;
304         }
305         if (landed==-1 && game.state.plnets[iplnet].known==shuttle_down) {
306                 prout("You will have to beam down to retrieve the shuttle craft.");
307                 return;
308         }
309         if (shldup!=0 || condit == IHDOCKED) {
310                 prout("Shuttle craft cannot pass through shields.");
311                 return;
312         }
313         if (game.state.plnets[iplnet].known==unknown) {
314                 prout("Spock-  \"Captain, we have no information on this planet");
315                 prout("  and Starfleet Regulations clearly state that in this situation");
316                 prout("  you may not fly down.\"");
317                 return;
318         }
319         Time = 3.0e-5*height;
320         if (Time >= 0.8*game.state.remtime) {
321                 prout("First Officer Spock-  \"Captain, I compute that such");
322                 proutn("  a maneuver would require approximately 2d%% of our",
323                       (int)(100*Time/game.state.remtime));
324                 prout("remaining time.");
325                 proutn("Are you sure this is wise?\" ");
326                 if (ja()==0) {
327                         Time = 0.0;
328                         return;
329                 }
330         }
331         if (landed == 1) {
332                 /* Kirk on planet */
333                 if (iscraft==1) {
334                         /* Galileo on ship! */
335                         if (game.damage[DTRANSP]==0) {
336                                 proutn("Spock-  \"Would you rather use the transporter?\" ");
337                                 if (ja() != 0) {
338                                         beam();
339                                         return;
340                                 }
341                                 proutn("Shuttle crew");
342                         }
343                         else
344                                 proutn("Rescue party");
345                         prout(" boards Galileo and swoops toward planet surface.");
346                         iscraft = 0;
347                         skip(1);
348                         if (consumeTime()) return;
349                         game.state.plnets[iplnet].known=shuttle_down;
350                         prout("Trip complete.");
351                         return;
352                 }
353                 else {
354                         /* Ready to go back to ship */
355                         prout("You and your mining party board the");
356                         prout("shuttle craft for the trip back to the Enterprise.");
357                         skip(1);
358                         prout("The short hop begins . . .");
359                         game.state.plnets[iplnet].known=known;
360                         icraft = 1;
361                         skip(1);
362                         landed = -1;
363                         if (consumeTime()) return;
364                         iscraft = 1;
365                         icraft = 0;
366                         if (imine!=0) {
367                                 icrystl = 1;
368                                 cryprob = 0.05;
369                         }
370                         imine = 0;
371                         prout("Trip complete.");
372                         return;
373                 }
374         }
375         else {
376                 /* Kirk on ship */
377                 /* and so is Galileo */
378                 prout("Mining party assembles in the hangar deck,");
379                 prout("ready to board the shuttle craft \"Galileo\".");
380                 skip(1);
381                 prouts("The hangar doors open; the trip begins.");
382                 skip(1);
383                 icraft = 1;
384                 iscraft = 0;
385                 if (consumeTime()) return;
386                 game.state.plnets[iplnet].known = shuttle_down;
387                 landed = 1;
388                 icraft = 0;
389                 prout("Trip complete");
390                 return;
391         }
392 }
393
394 void deathray(void) {
395         double r = Rand();
396         
397         ididit = 0;
398         skip(1);
399         chew();
400         if (ship != IHE) {
401                 prout("Ye Faerie Queene has no death ray.");
402                 return;
403         }
404         if (nenhere==0) {
405                 prout("Sulu-  \"But Sir, there are no enemies in this quadrant.\"");
406                 return;
407         }
408         if (game.damage[DDRAY] > 0.0) {
409                 prout("Death Ray is damaged.");
410                 return;
411         }
412         prout("Spock-  \"Captain, the 'Experimental Death Ray'");
413         prout("  is highly unpredictible.  Considering the alternatives,");
414         proutn("  are you sure this is wise?\" ");
415         if (ja()==0) return;
416         prout("Spock-  \"Acknowledged.\"");
417         skip(1);
418         ididit=1;
419         prouts("WHOOEE ... WHOOEE ... WHOOEE ... WHOOEE");
420         skip(1);
421         prout("Crew scrambles in emergency preparation.");
422         prout("Spock and Scotty ready the death ray and");
423         prout("prepare to channel all ship's power to the device.");
424         skip(1);
425         prout("Spock-  \"Preparations complete, sir.\"");
426         prout("Kirk-  \"Engage!\"");
427         skip(1);
428         prouts("WHIRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR");
429         skip(1);
430         if (r > .30) {
431                 prouts("Sulu- \"Captain!  It's working!\"");
432                 skip(2);
433                 while (nenhere > 0)
434                         deadkl(game.kx[1],game.ky[1],game.quad[game.kx[1]][game.ky[1]],game.kx[1],game.ky[1]);
435                 prout("Ensign Chekov-  \"Congratulations, Captain!\"");
436                 if (game.state.remkl == 0) finish(FWON);
437                 prout("Spock-  \"Captain, I believe the `Experimental Death Ray'");
438                 if (Rand() <= 0.05) {
439                         prout("   is still operational.\"");
440                 }
441                 else {
442                         prout("   has been rendered nonfunctional.\"");
443                         game.damage[DDRAY] = 39.95;
444                 }
445                 return;
446         }
447         r = Rand();     // Pick failure method 
448         if (r <= .30) {
449                 prouts("Sulu- \"Captain!  It's working!\"");
450                 skip(1);
451                 prouts("***RED ALERT!  RED ALERT!");
452                 skip(1);
453                 prout("***MATTER-ANTIMATTER IMPLOSION IMMINENT!");
454                 skip(1);
455                 prouts("***RED ALERT!  RED A*L********************************");
456                 skip(1);
457                 stars();
458                 prouts("******************   KA-BOOM!!!!   *******************");
459                 skip(1);
460                 kaboom();
461                 return;
462         }
463         if (r <= .55) {
464                 prouts("Sulu- \"Captain!  Yagabandaghangrapl, brachriigringlanbla!\"");
465                 skip(1);
466                 prout("Lt. Uhura-  \"Graaeek!  Graaeek!\"");
467                 skip(1);
468                 prout("Spock-  \"Facinating!  . . . All humans aboard");
469                 prout("  have apparently been transformed into strange mutations.");
470                 prout("  Vulcans do not seem to be affected.");
471                 skip(1);
472                 prout("Kirk-  \"Raauch!  Raauch!\"");
473                 finish(FDRAY);
474                 return;
475         }
476         if (r <= 0.75) {
477                 int i,j;
478                 prouts("Sulu- \"Captain!  It's   --WHAT?!?!\"");
479                 skip(2);
480                 proutn("Spock-  \"I believe the word is");
481                 prouts(" *ASTONISHING*");
482                 prout(" Mr. Sulu.");
483                 for (i=1; i<=10; i++)
484                         for (j=1; j<=10; j++)
485                                 if (game.quad[i][j] == IHDOT) game.quad[i][j] = IHQUEST;
486                 prout("  Captain, our quadrant is now infested with");
487                 prouts(" - - - - - -  *THINGS*.");
488                 skip(1);
489                 prout("  I have no logical explanation.\"");
490                 return;
491         }
492         prouts("Sulu- \"Captain!  The Death Ray is creating tribbles!\"");
493         skip(1);
494         prout("Scotty-  \"There are so many tribbles down here");
495         prout("  in Engineering, we can't move for 'em, Captain.\"");
496         finish(FTRIBBLE);
497         return;
498 }