Rollup patch.
[super-star-trek.git] / src / moving.c
1 #include <unistd.h>
2 #include "sstlinux.h"
3 #include "sst.h"
4
5 static void getcd(bool, int);
6
7 void imove(void)
8 /* movement execution for warp, impule, supernova, and tractor-beam events */
9 {
10     double angle, deltax, deltay, bigger, x, y,
11         finald, stopegy, probf;
12     int n, m, kink, kinks;
13     feature iquad;
14     coord w, final;
15     bool trbeam = false;
16
17     w.x = w.y = 0;
18     if (game.inorbit) {
19         prout(_("Helmsman Sulu- \"Leaving standard orbit.\""));
20         game.inorbit = false;
21     }
22
23     angle = ((15.0 - game.direc) * 0.5235988);
24     deltax = -sin(angle);
25     deltay = cos(angle);
26     if (fabs(deltax) > fabs(deltay))
27         bigger = fabs(deltax);
28     else
29         bigger = fabs(deltay);
30                 
31     deltay /= bigger;
32     deltax /= bigger;
33
34     /* If tractor beam is to occur, don't move full distance */
35     if (game.state.date+game.optime >= scheduled(FTBEAM)) {
36         trbeam = true;
37         game.condition = red;
38         game.dist = game.dist*(scheduled(FTBEAM)-game.state.date)/game.optime + 0.1;
39         game.optime = scheduled(FTBEAM) - game.state.date + 1e-5;
40     }
41     /* Move within the quadrant */
42     game.quad[game.sector.x][game.sector.y] = IHDOT;
43     x = game.sector.x;
44     y = game.sector.y;
45     n = 10.0*game.dist*bigger+0.5;
46
47     if (n > 0) {
48         for (m = 1; m <= n; m++) {
49             w.x = (x += deltax) + 0.5;
50             w.y = (y += deltay) + 0.5;
51             if (!VALID_SECTOR(w.x, w.y)) {
52                 /* Leaving quadrant -- allow final enemy attack */
53                 /* Don't do it if being pushed by Nova */
54                 if (game.nenhere != 0 && game.iattak != 2) {
55                     newcnd();
56                     for_local_enemies(m) {
57                         finald = distance(w, game.ks[m]);
58                         game.kavgd[m] = 0.5 * (finald + game.kdist[m]);
59                     }
60                     /*
61                      * Stas Sergeev added the condition
62                      * that attacks only happen if Klingons
63                      * are present and your skill is good.
64                      */
65                     if (game.skill > SKILL_GOOD && game.klhere > 0 && !game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
66                         attack(0);
67                     if (game.alldone) return;
68                 }
69                 /* compute final position -- new quadrant and sector */
70                 x = QUADSIZE*(game.quadrant.x-1)+game.sector.x;
71                 y = QUADSIZE*(game.quadrant.y-1)+game.sector.y;
72                 w.x = x+10.0*game.dist*bigger*deltax+0.5;
73                 w.y = y+10.0*game.dist*bigger*deltay+0.5;
74                 /* check for edge of galaxy */
75                 kinks = 0;
76                 do {
77                     kink = 0;
78                     if (w.x <= 0) {
79                         w.x = -w.x + 1;
80                         kink = 1;
81                     }
82                     if (w.y <= 0) {
83                         w.y = -w.y + 1;
84                         kink = 1;
85                     }
86                     if (w.x > GALSIZE*QUADSIZE) {
87                         w.x = (GALSIZE*QUADSIZE*2)+1 - w.x;
88                         kink = 1;
89                     }
90                     if (w.y > GALSIZE*QUADSIZE) {
91                         w.y = (GALSIZE*QUADSIZE*2)+1 - w.y;
92                         kink = 1;
93                     }
94                     if (kink) kinks = 1;
95                 } while (kink);
96
97                 if (kinks) {
98                     game.nkinks += 1;
99                     if (game.nkinks == 3) {
100                         /* Three strikes -- you're out! */
101                         finish(FNEG3);
102                         return;
103                     }
104                     skip(1);
105                     prout(_("YOU HAVE ATTEMPTED TO CROSS THE NEGATIVE ENERGY BARRIER"));
106                     prout(_("AT THE EDGE OF THE GALAXY.  THE THIRD TIME YOU TRY THIS,"));
107                     prout(_("YOU WILL BE DESTROYED."));
108                 }
109                 /* Compute final position in new quadrant */
110                 if (trbeam) return; /* Don't bother if we are to be beamed */
111                 game.quadrant.x = (w.x+(QUADSIZE-1))/QUADSIZE;
112                 game.quadrant.y = (w.y+(QUADSIZE-1))/QUADSIZE;
113                 game.sector.x = w.x - QUADSIZE*(game.quadrant.x-1);
114                 game.sector.y = w.y - QUADSIZE*(game.quadrant.y-1);
115                 skip(1);
116                 prout(_("Entering %s."), cramlc(quadrant, game.quadrant));
117                 game.quad[game.sector.x][game.sector.y] = game.ship;
118                 newqad(false);
119                 if (game.skill>SKILL_NOVICE) attack(0);
120                 return;
121             }
122             iquad = game.quad[w.x][w.y];
123             if (iquad != IHDOT) {
124                 /* object encountered in flight path */
125                 stopegy = 50.0*game.dist/game.optime;
126                 game.dist = distance(game.sector, w) / (QUADSIZE * 1.0);
127                 switch (iquad) {
128                 case IHT: /* Ram a Tholian */
129                 case IHK: /* Ram enemy ship */
130                 case IHC:
131                 case IHS:
132                 case IHR:
133                 case IHQUEST:
134                     game.sector = w;
135                     ram(false, iquad, game.sector);
136                     final = game.sector;
137                     break;
138                 case IHBLANK:
139                     skip(1);
140                     prouts(_("***RED ALERT!  RED ALERT!"));
141                     skip(1);
142                     proutn("***");
143                     crmshp();
144                     proutn(_(" pulled into black hole at "));
145                     prout(cramlc(sector, w));
146                     /*
147                      * Getting pulled into a black hole was certain
148                      * death in Almy's original.  Stas Sergeev added a
149                      * possibility that you'll get timewarped instead.
150                      */
151                     n=0;
152                     for (m=0;m<NDEVICES;m++)
153                         if (game.damage[m]>0) 
154                             n++;
155                     probf=pow(1.4,(game.energy+game.shield)/5000.0-1.0)*pow(1.3,1.0/(n+1)-1.0);
156                     if ((game.options & OPTION_BLKHOLE) && Rand()>probf) 
157                         timwrp();
158                     else 
159                         finish(FHOLE);
160                     return;
161                 default:
162                     /* something else */
163                     skip(1);
164                     crmshp();
165                     if (iquad == IHWEB)
166                         proutn(_(" encounters Tholian web at "));
167                     else
168                         proutn(_(" blocked by object at "));
169                     proutn(cramlc(sector, w));
170                     prout(";");
171                     proutn(_("Emergency stop required "));
172                     prout(_("%2d units of energy."), (int)stopegy);
173                     game.energy -= stopegy;
174                     final.x = x-deltax+0.5;
175                     final.y = y-deltay+0.5;
176                     game.sector = final;
177                     if (game.energy <= 0) {
178                         finish(FNRG);
179                         return;
180                     }
181                     break;
182                 }
183                 goto no_quad_change;    /* sorry! */
184             }
185         }
186         game.dist = distance(game.sector, w) / (QUADSIZE * 1.0);
187         game.sector = w;
188     }
189     final = game.sector;
190 no_quad_change:
191     /* No quadrant change -- compute new avg enemy distances */
192     game.quad[game.sector.x][game.sector.y] = game.ship;
193     if (game.nenhere) {
194         for_local_enemies(m) {
195             finald = distance(w, game.ks[m]);
196             game.kavgd[m] = 0.5 * (finald+game.kdist[m]);
197             game.kdist[m] = finald;
198         }
199         sortkl();
200         if (!game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova && game.iattak == 0)
201             attack(0);
202         for_local_enemies(m) game.kavgd[m] = game.kdist[m];
203     }
204     newcnd();
205     game.iattak = 0;
206     drawmaps(0);
207     setwnd(message_window);
208     return;
209 }
210
211 void dock(bool verbose) 
212 /* dock our ship at a starbase */
213 {
214     chew();
215     if (game.condition == docked && verbose) {
216         prout(_("Already docked."));
217         return;
218     }
219     if (game.inorbit) {
220         prout(_("You must first leave standard orbit."));
221         return;
222     }
223     if (game.base.x==0 || abs(game.sector.x-game.base.x) > 1 || abs(game.sector.y-game.base.y) > 1) {
224         crmshp();
225         prout(_(" not adjacent to base."));
226         return;
227     }
228     game.condition = docked;
229     if (verbose) prout(_("Docked."));
230     game.ididit = true;
231     if (game.energy < game.inenrg) game.energy = game.inenrg;
232     game.shield = game.inshld;
233     game.torps = game.intorps;
234     game.lsupres = game.inlsr;
235     game.state.crew = FULLCREW;
236     if (!damaged(DRADIO) &&
237         (is_scheduled(FCDBAS) || game.isatb == 1) && !game.iseenit) {
238         /* get attack report from base */
239         prout(_("Lt. Uhura- \"Captain, an important message from the starbase:\""));
240         attakreport(false);
241         game.iseenit = true;
242     }
243 }
244
245 /* 
246  * This program originally required input in terms of a (clock)
247  * direction and distance. Somewhere in history, it was changed to
248  * cartesian coordinates. So we need to convert. I think
249  * "manual" input should still be done this way -- it's a real
250  * pain if the computer isn't working! Manual mode is still confusing
251  * because it involves giving x and y motions, yet the coordinates
252  * are always displayed y - x, where +y is downward!
253  */
254
255 static void getcd(bool isprobe, int akey)
256 /* get course and distance */
257 {
258     int irowq=game.quadrant.x, icolq=game.quadrant.y, key=0;
259     double xi, xj, xk, xl;
260     double deltax, deltay;
261     enum {unspecified, manual, automatic} navmode = unspecified;
262     enum {curt, normal, verbose} itemp = curt;
263     coord incr;
264     bool iprompt = false;
265
266     /* Get course direction and distance. If user types bad values, return
267        with DIREC = -1.0. */
268
269     game.direc = -1.0;
270         
271     if (game.landed && !isprobe) {
272         prout(_("Dummy! You can't leave standard orbit until you"));
273         proutn(_("are back aboard the ship."));
274         chew();
275         return;
276     }
277     while (navmode == unspecified) {
278         if (damaged(DNAVSYS)) {
279             if (isprobe)
280                 prout(_("Computer damaged; manual navigation only"));
281             else
282                 prout(_("Computer damaged; manual movement only"));
283             chew();
284             navmode = manual;
285             key = IHEOL;
286             break;
287         }
288         if (isprobe && akey != -1) {
289             /* For probe launch, use pre-scanned value first time */
290             key = akey;
291             akey = -1;
292         }
293         else 
294             key = scan();
295
296         if (key == IHEOL) {
297             proutn(_("Manual or automatic- "));
298             iprompt = true;
299             chew();
300         }
301         else if (key == IHALPHA) {
302             if (isit("manual")) {
303                 navmode = manual;
304                 key = scan();
305                 break;
306             }
307             else if (isit("automatic")) {
308                 navmode = automatic;
309                 key = scan();
310                 break;
311             }
312             else {
313                 huh();
314                 chew();
315                 return;
316             }
317         }
318         else { /* numeric */
319             if (isprobe)
320                 prout(_("(Manual navigation assumed.)"));
321             else
322                 prout(_("(Manual movement assumed.)"));
323             navmode = automatic;
324             break;
325         }
326     }
327
328     if (navmode == automatic) {
329         while (key == IHEOL) {
330             if (isprobe)
331                 proutn(_("Target quadrant or quadrant&sector- "));
332             else
333                 proutn(_("Destination sector or quadrant&sector- "));
334             chew();
335             iprompt = true;
336             key = scan();
337         }
338
339         if (key != IHREAL) {
340             huh();
341             return;
342         }
343         xi = aaitem;
344         key = scan();
345         if (key != IHREAL){
346             huh();
347             return;
348         }
349         xj = aaitem;
350         key = scan();
351         if (key == IHREAL) {
352             /* both quadrant and sector specified */
353             xk = aaitem;
354             key = scan();
355             if (key != IHREAL) {
356                 huh();
357                 return;
358             }
359             xl = aaitem;
360
361             irowq = xi + 0.5;
362             icolq = xj + 0.5;
363             incr.y = xk + 0.5;
364             incr.x = xl + 0.5;
365         }
366         else {
367             if (isprobe) {
368                 /* only quadrant specified -- go to center of dest quad */
369                 irowq = xi + 0.5;
370                 icolq = xj + 0.5;
371                 incr.y = incr.x = 5;
372             }
373             else {
374                 incr.y = xi + 0.5;
375                 incr.x = xj + 0.5;
376             }
377             itemp = normal;
378         }
379         if (!VALID_QUADRANT(icolq,irowq)||!VALID_SECTOR(incr.x,incr.y)) {
380             huh();
381             return;
382         }
383         skip(1);
384         if (!isprobe) {
385             if (itemp > curt) {
386                 if (iprompt) {
387                     prout(_("Helmsman Sulu- \"Course locked in for %s.\""),
388                           cramlc(sector, incr));
389                 }
390             }
391             else prout(_("Ensign Chekov- \"Course laid in, Captain.\""));
392         }
393         deltax = icolq - game.quadrant.y + 0.1*(incr.x-game.sector.y);
394         deltay = game.quadrant.x - irowq + 0.1*(game.sector.x-incr.y);
395     }
396     else { /* manual */
397         while (key == IHEOL) {
398             proutn(_("X and Y displacements- "));
399             chew();
400             iprompt = true;
401             key = scan();
402         }
403         itemp = verbose;
404         if (key != IHREAL) {
405             huh();
406             return;
407         }
408         deltax = aaitem;
409         key = scan();
410         if (key != IHREAL) {
411             huh();
412             return;
413         }
414         deltay = aaitem;
415     }
416     /* Check for zero movement */
417     if (deltax == 0 && deltay == 0) {
418         chew();
419         return;
420     }
421     if (itemp == verbose && !isprobe) {
422         skip(1);
423         prout(_("Helmsman Sulu- \"Aye, Sir.\""));
424     }
425     game.dist = sqrt(deltax*deltax + deltay*deltay);
426     game.direc = atan2(deltax, deltay)*1.90985932;
427     if (game.direc < 0.0) game.direc += 12.0;
428     chew();
429     return;
430 }
431                 
432
433
434 void impuls(void) 
435 /* move under impulse power */
436 {
437     double power;
438
439     game.ididit = false;
440     if (damaged(DIMPULS)) {
441         chew();
442         skip(1);
443         prout(_("Engineer Scott- \"The impulse engines are damaged, Sir.\""));
444         return;
445     }
446
447     if (game.energy > 30.0) {
448         getcd(false, 0);
449         if (game.direc == -1.0) return;
450         power = 20.0 + 100.0*game.dist;
451     }
452     else
453         power = 30.0;
454
455     if (power >= game.energy) {
456         /* Insufficient power for trip */
457         skip(1);
458         prout(_("First Officer Spock- \"Captain, the impulse engines"));
459         prout(_("require 20.0 units to engage, plus 100.0 units per"));
460         if (game.energy > 30) {
461             proutn(_("quadrant.  We can go, therefore, a maximum of %d"),
462                    (int)(0.01 * (game.energy-20.0)-0.05));
463             prout(_(" quadrants.\""));
464         }
465         else {
466             prout(_("quadrant.  They are, therefore, useless.\""));
467         }
468         chew();
469         return;
470     }
471     /* Make sure enough time is left for the trip */
472     game.optime = game.dist/0.095;
473     if (game.optime >= game.state.remtime) {
474         prout(_("First Officer Spock- \"Captain, our speed under impulse"));
475         prout(_("power is only 0.95 sectors per stardate. Are you sure"));
476         proutn(_("we dare spend the time?\" "));
477         if (ja() == false) return;
478     }
479     /* Activate impulse engines and pay the cost */
480     imove();
481     game.ididit = true;
482     if (game.alldone) return;
483     power = 20.0 + 100.0*game.dist;
484     game.energy -= power;
485     game.optime = game.dist/0.095;
486     if (game.energy <= 0) finish(FNRG);
487     return;
488 }
489
490
491 void warp(bool timewarp)
492 /* move under warp drive */
493 {
494     int iwarp;
495     bool blooey = false, twarp = false;
496     double power;
497
498     if (!timewarp) { /* Not WARPX entry */
499         game.ididit = false;
500         if (game.damage[DWARPEN] > 10.0) {
501             chew();
502             skip(1);
503             prout(_("Engineer Scott- \"The impulse engines are damaged, Sir.\""));
504             return;
505         }
506         if (damaged(DWARPEN) && game.warpfac > 4.0) {
507             chew();
508             skip(1);
509             prout(_("Engineer Scott- \"Sorry, Captain. Until this damage"));
510             prout(_("  is repaired, I can only give you warp 4.\""));
511             return;
512         }
513                         
514         /* Read in course and distance */
515         getcd(false, 0);
516         if (game.direc == -1.0) return;
517
518         /* Make sure starship has enough energy for the trip */
519         power = (game.dist+0.05)*game.warpfac*game.warpfac*game.warpfac*(game.shldup+1);
520
521
522         if (power >= game.energy) {
523             /* Insufficient power for trip */
524             game.ididit = false;
525             skip(1);
526             prout(_("Engineering to bridge--"));
527             if (!game.shldup || 0.5*power > game.energy) {
528                 iwarp = pow((game.energy/(game.dist+0.05)), 0.333333333);
529                 if (iwarp <= 0) {
530                     prout(_("We can't do it, Captain. We don't have enough energy."));
531                 }
532                 else {
533                     proutn(_("We don't have enough energy, but we could do it at warp %d"), iwarp);
534                     if (game.shldup) {
535                         prout(",");
536                         prout(_("if you'll lower the shields."));
537                     }
538                     else
539                         prout(".");
540                 }
541             }
542             else
543                 prout(_("We haven't the energy to go that far with the shields up."));
544             return;
545         }
546                                                 
547         /* Make sure enough time is left for the trip */
548         game.optime = 10.0*game.dist/game.wfacsq;
549         if (game.optime >= 0.8*game.state.remtime) {
550             skip(1);
551             prout(_("First Officer Spock- \"Captain, I compute that such"));
552             proutn(_("  a trip would require approximately %2.0f"),
553                    100.0*game.optime/game.state.remtime);
554             prout(_(" percent of our"));
555             proutn(_("  remaining time.  Are you sure this is wise?\" "));
556             if (ja() == false) { game.ididit = false; game.optime=0; return;}
557         }
558     }
559     /* Entry WARPX */
560     if (game.warpfac > 6.0) {
561         /* Decide if engine damage will occur */
562         double prob = game.dist*(6.0-game.warpfac)*(6.0-game.warpfac)/66.666666666;
563         if (prob > Rand()) {
564             blooey = true;
565             game.dist = Rand()*game.dist;
566         }
567         /* Decide if time warp will occur */
568         if (0.5*game.dist*pow(7.0,game.warpfac-10.0) > Rand()) twarp = true;
569         if (idebug && game.warpfac==10 && !twarp) {
570             blooey = false;
571             proutn("=== Force time warp? ");
572             if (ja() == true) twarp = true;
573         }
574         if (blooey || twarp) {
575             /* If time warp or engine damage, check path */
576             /* If it is obstructed, don't do warp or damage */
577             double angle = ((15.0-game.direc)*0.5235998);
578             double deltax = -sin(angle);
579             double deltay = cos(angle);
580             double bigger, x, y;
581             int n, l, ix, iy;
582             if (fabs(deltax) > fabs(deltay))
583                 bigger = fabs(deltax);
584             else
585                 bigger = fabs(deltay);
586                         
587             deltax /= bigger;
588             deltay /= bigger;
589             n = 10.0 * game.dist * bigger +0.5;
590             x = game.sector.x;
591             y = game.sector.y;
592             for (l = 1; l <= n; l++) {
593                 x += deltax;
594                 ix = x + 0.5;
595                 y += deltay;
596                 iy = y +0.5;
597                 if (!VALID_SECTOR(ix, iy)) break;
598                 if (game.quad[ix][iy] != IHDOT) {
599                     blooey = false;
600                     twarp = false;
601                 }
602             }
603         }
604     }
605                                 
606
607     /* Activate Warp Engines and pay the cost */
608     imove();
609     if (game.alldone) return;
610     game.energy -= game.dist*game.warpfac*game.warpfac*game.warpfac*(game.shldup+1);
611     if (game.energy <= 0) finish(FNRG);
612     game.optime = 10.0*game.dist/game.wfacsq;
613     if (twarp) timwrp();
614     if (blooey) {
615         game.damage[DWARPEN] = game.damfac*(3.0*Rand()+1.0);
616         skip(1);
617         prout(_("Engineering to bridge--"));
618         prout(_("  Scott here.  The warp engines are damaged."));
619         prout(_("  We'll have to reduce speed to warp 4."));
620     }
621     game.ididit = true;
622     return;
623 }
624
625
626
627 void setwrp(void) 
628 /* change the warp factor */
629 {
630     int key;
631     double oldfac;
632         
633     while ((key=scan()) == IHEOL) {
634         chew();
635         proutn(_("Warp factor- "));
636     }
637     chew();
638     if (key != IHREAL) {
639         huh();
640         return;
641     }
642     if (game.damage[DWARPEN] > 10.0) {
643         prout(_("Warp engines inoperative."));
644         return;
645     }
646     if (damaged(DWARPEN) && aaitem > 4.0) {
647         prout(_("Engineer Scott- \"I'm doing my best, Captain,"));
648         prout(_("  but right now we can only go warp 4.\""));
649         return;
650     }
651     if (aaitem > 10.0) {
652         prout(_("Helmsman Sulu- \"Our top speed is warp 10, Captain.\""));
653         return;
654     }
655     if (aaitem < 1.0) {
656         prout(_("Helmsman Sulu- \"We can't go below warp 1, Captain.\""));
657         return;
658     }
659     oldfac = game.warpfac;
660     game.warpfac = aaitem;
661     game.wfacsq=game.warpfac*game.warpfac;
662     if (game.warpfac <= oldfac || game.warpfac <= 6.0) {
663         prout(_("Helmsman Sulu- \"Warp factor %d, Captain.\""),
664                (int)game.warpfac);
665         return;
666     }
667     if (game.warpfac < 8.00) {
668         prout(_("Engineer Scott- \"Aye, but our maximum safe speed is warp 6.\""));
669         return;
670     }
671     if (game.warpfac == 10.0) {
672         prout(_("Engineer Scott- \"Aye, Captain, we'll try it.\""));
673         return;
674     }
675     prout(_("Engineer Scott- \"Aye, Captain, but our engines may not take it.\""));
676     return;
677 }
678
679 void atover(bool igrab) 
680 /* cope with being tossed out of quadrant by supernova or yanked by beam */
681 {
682     double power, distreq;
683
684     chew();
685     /* is captain on planet? */
686     if (game.landed) {
687         if (damaged(DTRANSP)) {
688             finish(FPNOVA);
689             return;
690         }
691         prout(_("Scotty rushes to the transporter controls."));
692         if (game.shldup) {
693             prout(_("But with the shields up it's hopeless."));
694             finish(FPNOVA);
695         }
696         prouts(_("His desperate attempt to rescue you . . ."));
697         if (Rand() <= 0.5) {
698             prout(_("fails."));
699             finish(FPNOVA);
700             return;
701         }
702         prout(_("SUCCEEDS!"));
703         if (game.imine) {
704             game.imine = false;
705             proutn(_("The crystals mined were "));
706             if (Rand() <= 0.25) {
707                 prout(_("lost."));
708             }
709             else {
710                 prout(_("saved."));
711                 game.icrystl = true;
712             }
713         }
714     }
715     if (igrab) return;
716
717     /* Check to see if captain in shuttle craft */
718     if (game.icraft) finish(FSTRACTOR);
719     if (game.alldone) return;
720
721     /* Inform captain of attempt to reach safety */
722     skip(1);
723     do {
724         if (game.justin) {
725             prouts(_("***RED ALERT!  RED ALERT!"));
726             skip(1);
727             proutn(_("The "));
728             crmshp();
729             prout(_(" has stopped in a quadrant containing"));
730             prouts(_("   a supernova."));
731             skip(2);
732         }
733         proutn(_("***Emergency automatic override attempts to hurl "));
734         crmshp();
735         skip(1);
736         prout(_("safely out of quadrant."));
737         if (!damaged(DRADIO))
738             game.state.galaxy[game.quadrant.x][game.quadrant.y].charted = true;
739         /* Try to use warp engines */
740         if (damaged(DWARPEN)) {
741             skip(1);
742             prout(_("Warp engines damaged."));
743             finish(FSNOVAED);
744             return;
745         }
746         game.warpfac = 6.0+2.0*Rand();
747         game.wfacsq = game.warpfac * game.warpfac;
748         prout(_("Warp factor set to %d"), (int)game.warpfac);
749         power = 0.75*game.energy;
750         game.dist = power/(game.warpfac*game.warpfac*game.warpfac*(game.shldup+1));
751         distreq = 1.4142+Rand();
752         if (distreq < game.dist) game.dist = distreq;
753         game.optime = 10.0*game.dist/game.wfacsq;
754         game.direc = 12.0*Rand();       /* How dumb! */
755         game.justin = false;
756         game.inorbit = false;
757         warp(true);
758         if (!game.justin) {
759             /* This is bad news, we didn't leave quadrant. */
760             if (game.alldone) return;
761             skip(1);
762             prout(_("Insufficient energy to leave quadrant."));
763             finish(FSNOVAED);
764             return;
765         }
766     } while 
767         /* Repeat if another snova */
768         (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova);
769     if (KLINGREM==0) 
770         finish(FWON); /* Snova killed remaining enemy. */
771 }
772
773 void timwrp() 
774 /* let's do the time warp again */
775 {
776     int l;
777     bool gotit;
778     prout(_("***TIME WARP ENTERED."));
779     if (game.state.snap && Rand() < 0.5) {
780         /* Go back in time */
781         prout(_("You are traveling backwards in time %d stardates."),
782               (int)(game.state.date-game.snapsht.date));
783         game.state = game.snapsht;
784         game.state.snap = false;
785         if (game.state.remcom) {
786             schedule(FTBEAM, expran(game.intime/game.state.remcom));
787             schedule(FBATTAK, expran(0.3*game.intime));
788         }
789         schedule(FSNOVA, expran(0.5*game.intime));
790         /* next snapshot will be sooner */
791         schedule(FSNAP, expran(0.25*game.state.remtime));
792                                 
793         if (game.state.nscrem) schedule(FSCMOVE, 0.2777);
794         game.isatb = 0;
795         unschedule(FCDBAS);
796         unschedule(FSCDBAS);
797         game.battle.x = game.battle.y = 0;
798
799         /* Make sure Galileo is consistant -- Snapshot may have been taken
800            when on planet, which would give us two Galileos! */
801         gotit = false;
802         for (l = 0; l < game.inplan; l++) {
803             if (game.state.plnets[l].known == shuttle_down) {
804                 gotit = true;
805                 if (game.iscraft == onship && game.ship==IHE) {
806                     prout(_("Checkov-  \"Security reports the Galileo has disappeared, Sir!"));
807                     game.iscraft = offship;
808                 }
809             }
810         }
811         /* Likewise, if in the original time the Galileo was abandoned, but
812            was on ship earlier, it would have vanished -- lets restore it */
813         if (game.iscraft == offship && !gotit && game.damage[DSHUTTL] >= 0.0) {
814             prout(_("Checkov-  \"Security reports the Galileo has reappeared in the dock!\""));
815             game.iscraft = onship;
816         }
817         /* 
818          * There used to be code to do the actual reconstrction here,
819          * but the starchart is now part of the snapshotted galaxy state.
820          */
821         prout(_("Spock has reconstructed a correct star chart from memory"));
822     }
823     else {
824         /* Go forward in time */
825         game.optime = -0.5*game.intime*log(Rand());
826         prout(_("You are traveling forward in time %d stardates."), (int)game.optime);
827         /* cheat to make sure no tractor beams occur during time warp */
828         postpone(FTBEAM, game.optime);
829         game.damage[DRADIO] += game.optime;
830     }
831     newqad(false);
832     events();   /* Stas Sergeev added this -- do pending events */
833 }
834
835 void probe(void) 
836 /* launch deep-space probe */
837 {
838     double angle, bigger;
839     int key;
840     /* New code to launch a deep space probe */
841     if (game.nprobes == 0) {
842         chew();
843         skip(1);
844         if (game.ship == IHE) 
845             prout(_("Engineer Scott- \"We have no more deep space probes, Sir.\""));
846         else
847             prout(_("Ye Faerie Queene has no deep space probes."));
848         return;
849     }
850     if (damaged(DDSP)) {
851         chew();
852         skip(1);
853         prout(_("Engineer Scott- \"The probe launcher is damaged, Sir.\""));
854         return;
855     }
856     if (is_scheduled(FDSPROB)) {
857         chew();
858         skip(1);
859         if (damaged(DRADIO) && game.condition != docked) {
860             prout(_("Spock-  \"Records show the previous probe has not yet"));
861             prout(_("   reached its destination.\""));
862         }
863         else
864             prout(_("Uhura- \"The previous probe is still reporting data, Sir.\""));
865         return;
866     }
867     key = scan();
868
869     if (key == IHEOL) {
870         /* slow mode, so let Kirk know how many probes there are left */
871         prout(game.nprobes==1 ? _("%d probe left.") : _("%d probes left."), game.nprobes);
872         proutn(_("Are you sure you want to fire a probe? "));
873         if (ja() == false) return;
874     }
875
876     game.isarmed = false;
877     if (key == IHALPHA && strcmp(citem,"armed") == 0) {
878         game.isarmed = true;
879         key = scan();
880     }
881     else if (key == IHEOL) {
882         proutn(_("Arm NOVAMAX warhead? "));
883         game.isarmed = ja();
884     }
885     getcd(true, key);
886     if (game.direc == -1.0) return;
887     game.nprobes--;
888     angle = ((15.0 - game.direc) * 0.5235988);
889     game.probeinx = -sin(angle);
890     game.probeiny = cos(angle);
891     if (fabs(game.probeinx) > fabs(game.probeiny))
892         bigger = fabs(game.probeinx);
893     else
894         bigger = fabs(game.probeiny);
895                 
896     game.probeiny /= bigger;
897     game.probeinx /= bigger;
898     game.proben = 10.0*game.dist*bigger +0.5;
899     game.probex = game.quadrant.x*QUADSIZE + game.sector.x - 1; // We will use better packing than original
900     game.probey = game.quadrant.y*QUADSIZE + game.sector.y - 1;
901     game.probec = game.quadrant;
902     schedule(FDSPROB, 0.01); // Time to move one sector
903     prout(_("Ensign Chekov-  \"The deep space probe is launched, Captain.\""));
904     game.ididit = true;
905     return;
906 }
907
908 /*
909  *      Here's how the mayday code works:
910  *
911  *      First, the closest starbase is selected.  If there is a
912  *      a starbase in your own quadrant, you are in good shape.
913  *      This distance takes quadrant distances into account only.
914  *
915  *      A magic number is computed based on the distance which acts
916  *      as the probability that you will be rematerialized.  You
917  *      get three tries.
918  *
919  *      When it is determined that you should be able to be remater-
920  *      ialized (i.e., when the probability thing mentioned above
921  *      comes up positive), you are put into that quadrant (anywhere).
922  *      Then, we try to see if there is a spot adjacent to the star-
923  *      base.  If not, you can't be rematerialized!!!  Otherwise,
924  *      it drops you there.  It only tries five times to find a spot
925  *      to drop you.  After that, it's your problem.
926  */
927
928 void mayday(void) 
929 /* yell for help from nearest starbase */
930 {
931     /* There's more than one way to move in this game! */
932     double ddist, xdist, probf;
933     int line = 0, m, ix, iy;
934
935     chew();
936     /* Test for conditions which prevent calling for help */
937     if (game.condition == docked) {
938         prout(_("Lt. Uhura-  \"But Captain, we're already docked.\""));
939         return;
940     }
941     if (damaged(DRADIO)) {
942         prout(_("Subspace radio damaged."));
943         return;
944     }
945     if (game.state.rembase==0) {
946         prout(_("Lt. Uhura-  \"Captain, I'm not getting any response from Starbase.\""));
947         return;
948     }
949     if (game.landed) {
950         proutn(_("You must be aboard the "));
951         crmshp();
952         prout(".");
953         return;
954     }
955     /* OK -- call for help from nearest starbase */
956     game.nhelp++;
957     if (game.base.x!=0) {
958         /* There's one in this quadrant */
959         ddist = distance(game.base, game.sector);
960     }
961     else {
962         ddist = FOREVER;
963         for_starbases(m) {
964             xdist = QUADSIZE * distance(game.state.baseq[m], game.quadrant);
965             if (xdist < ddist) {
966                 ddist = xdist;
967                 line = m;
968             }
969         }
970         /* Since starbase not in quadrant, set up new quadrant */
971         game.quadrant = game.state.baseq[line];
972         newqad(true);
973     }
974     /* dematerialize starship */
975     game.quad[game.sector.x][game.sector.y]=IHDOT;
976     proutn(_("Starbase in %s responds--"), cramlc(quadrant, game.quadrant));
977     proutn("");
978     crmshp();
979     prout(_(" dematerializes."));
980     game.sector.x=0;
981     for (m = 1; m <= 5; m++) {
982         ix = game.base.x+3.0*Rand()-1;
983         iy = game.base.y+3.0*Rand()-1;
984         if (VALID_SECTOR(ix,iy) && game.quad[ix][iy]==IHDOT) {
985             /* found one -- finish up */
986             game.sector.x=ix;
987             game.sector.y=iy;
988             break;
989         }
990     }
991     if (game.sector.x==0){
992         prout(_("You have been lost in space..."));
993         finish(FMATERIALIZE);
994         return;
995     }
996     /* Give starbase three chances to rematerialize starship */
997     probf = pow((1.0 - pow(0.98,ddist)), 0.33333333);
998     for (m = 1; m <= 3; m++) {
999         switch (m) {
1000         case 1: proutn(_("1st")); break;
1001         case 2: proutn(_("2nd")); break;
1002         case 3: proutn(_("3rd")); break;
1003         }
1004         proutn(_(" attempt to re-materialize "));
1005         crmshp();
1006         switch (m){
1007         case 1: game.quad[ix][iy]=IHMATER0;
1008             break;
1009         case 2: game.quad[ix][iy]=IHMATER1;
1010             break;
1011         case 3: game.quad[ix][iy]=IHMATER2;
1012             break;
1013         }
1014         textcolor(RED);
1015         warble();
1016         if (Rand() > probf) break;
1017         prout(_("fails."));
1018         delay(500);
1019         textcolor(DEFAULT);
1020     }
1021     if (m > 3) {
1022         game.quad[ix][iy]=IHQUEST;
1023         game.alive = false;
1024         drawmaps(1);
1025         setwnd(message_window);
1026         finish(FMATERIALIZE);
1027         return;
1028     }
1029     game.quad[ix][iy]=game.ship;
1030     textcolor(GREEN);
1031     prout(_("succeeds."));
1032     textcolor(DEFAULT);
1033     dock(false);
1034     skip(1);
1035     prout(_("Lt. Uhura-  \"Captain, we made it!\""));
1036 }
1037
1038 /*
1039 **  Abandon Ship
1040 **
1041 **      The ship is abandoned.  If your current ship is the Faire
1042 **      Queene, or if your shuttlecraft is dead, you're out of
1043 **      luck.  You need the shuttlecraft in order for the captain
1044 **      (that's you!!) to escape.
1045 **
1046 **      Your crew can beam to an inhabited starsystem in the
1047 **      quadrant, if there is one and if the transporter is working.
1048 **      If there is no inhabited starsystem, or if the transporter
1049 **      is out, they are left to die in outer space.
1050 **
1051 **      If there are no starbases left, you are captured by the
1052 **      Klingons, who torture you mercilessly.  However, if there
1053 **      is at least one starbase, you are returned to the
1054 **      Federation in a prisoner of war exchange.  Of course, this
1055 **      can't happen unless you have taken some prisoners.
1056 **
1057 */
1058
1059 void abandn(void) 
1060 /* abandon ship */
1061 {
1062     int nb, l;
1063     struct quadrant *q;
1064
1065     chew();
1066     if (game.condition==docked) {
1067         if (game.ship!=IHE) {
1068             prout(_("You cannot abandon Ye Faerie Queene."));
1069             return;
1070         }
1071     }
1072     else {
1073         /* Must take shuttle craft to exit */
1074         if (game.damage[DSHUTTL]==-1) {
1075             prout(_("Ye Faerie Queene has no shuttle craft."));
1076             return;
1077         }
1078         if (game.damage[DSHUTTL]<0) {
1079             prout(_("Shuttle craft now serving Big Macs."));
1080             return;
1081         }
1082         if (game.damage[DSHUTTL]>0) {
1083             prout(_("Shuttle craft damaged."));
1084             return;
1085         }
1086         if (game.landed) {
1087             prout(_("You must be aboard the Enterprise."));
1088             return;
1089         }
1090         if (game.iscraft != onship) {
1091             prout(_("Shuttle craft not currently available."));
1092             return;
1093         }
1094         /* Print abandon ship messages */
1095         skip(1);
1096         prouts(_("***ABANDON SHIP!  ABANDON SHIP!"));
1097         skip(1);
1098         prouts(_("***ALL HANDS ABANDON SHIP!"));
1099         skip(2);
1100         prout(_("Captain and crew escape in shuttle craft."));
1101         if (game.state.rembase==0) {
1102             /* Oops! no place to go... */
1103             finish(FABANDN);
1104             return;
1105         }
1106         q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
1107         /* Dispose of crew */
1108         if (!(game.options & OPTION_WORLDS) && !damaged(DTRANSP)) {
1109             prout(_("Remainder of ship's complement beam down"));
1110             prout(_("to nearest habitable planet."));
1111         } else if (q->planet != NOPLANET && !damaged(DTRANSP)) {
1112             prout(_("Remainder of ship's complement beam down"));
1113             prout(_("to %s."), systnames[q->planet]);
1114         } else {
1115             prout(_("Entire crew of %d left to die in outer space."));
1116             game.casual += game.state.crew;
1117             game.abandoned += game.state.crew;
1118         }
1119
1120         /* If at least one base left, give 'em the Faerie Queene */
1121         skip(1);
1122         game.icrystl = false; /* crystals are lost */
1123         game.nprobes = 0; /* No probes */
1124         prout(_("You are captured by Klingons and released to"));
1125         prout(_("the Federation in a prisoner-of-war exchange."));
1126         nb = Rand()*game.state.rembase+1;
1127         /* Set up quadrant and position FQ adjacient to base */
1128         if (!same(game.quadrant, game.state.baseq[nb])) {
1129             game.quadrant = game.state.baseq[nb];
1130             game.sector.x = game.sector.y = 5;
1131             newqad(true);
1132         }
1133         for (;;) {
1134             /* position next to base by trial and error */
1135             game.quad[game.sector.x][game.sector.y] = IHDOT;
1136             for_sectors(l) {
1137                 game.sector.x = 3.0*Rand() - 1.0 + game.base.x;
1138                 game.sector.y = 3.0*Rand() - 1.0 + game.base.y;
1139                 if (VALID_SECTOR(game.sector.x, game.sector.y) &&
1140                     game.quad[game.sector.x][game.sector.y] == IHDOT) break;
1141             }
1142             if (l < QUADSIZE+1) break; /* found a spot */
1143             game.sector.x=QUADSIZE/2;
1144             game.sector.y=QUADSIZE/2;
1145             newqad(true);
1146         }
1147     }
1148     /* Get new commission */
1149     game.quad[game.sector.x][game.sector.y] = game.ship = IHF;
1150     game.state.crew = FULLCREW;
1151     prout(_("Starfleet puts you in command of another ship,"));
1152     prout(_("the Faerie Queene, which is antiquated but,"));
1153     prout(_("still useable."));
1154     if (game.icrystl) prout(_("The dilithium crystals have been moved."));
1155     game.imine = false;
1156     game.iscraft = offship; /* Galileo disappears */
1157     /* Resupply ship */
1158     game.condition=docked;
1159     for (l = 0; l < NDEVICES; l++) 
1160         game.damage[l] = 0.0;
1161     game.damage[DSHUTTL] = -1;
1162     game.energy = game.inenrg = 3000.0;
1163     game.shield = game.inshld = 1250.0;
1164     game.torps = game.intorps = 6;
1165     game.lsupres=game.inlsr=3.0;
1166     game.shldup=false;
1167     game.warpfac=5.0;
1168     game.wfacsq=25.0;
1169     return;
1170 }