126d3baa13ecd63c12158781a8ddfeefdf7c3a97
[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 {
9     double angle, deltax, deltay, bigger, x, y,
10         finald, finalx, finaly, stopegy, probf;
11     int trbeam = 0, n, l, kink, kinks, iquad;
12     coord w;
13
14     w.x = w.y = 0;
15     if (game.inorbit) {
16         prout(_("Helmsman Sulu- \"Leaving standard orbit.\""));
17         game.inorbit = false;
18     }
19
20     angle = ((15.0 - game.direc) * 0.5235988);
21     deltax = -sin(angle);
22     deltay = cos(angle);
23     if (fabs(deltax) > fabs(deltay))
24         bigger = fabs(deltax);
25     else
26         bigger = fabs(deltay);
27                 
28     deltay /= bigger;
29     deltax /= bigger;
30
31     /* If tractor beam is to occur, don't move full distance */
32     if (game.state.date+game.optime >= scheduled(FTBEAM)) {
33         trbeam = 1;
34         game.condit = IHRED;
35         game.dist = game.dist*(scheduled(FTBEAM)-game.state.date)/game.optime + 0.1;
36         game.optime = scheduled(FTBEAM) - game.state.date + 1e-5;
37     }
38     /* Move within the quadrant */
39     game.quad[game.sector.x][game.sector.y] = IHDOT;
40     x = game.sector.x;
41     y = game.sector.y;
42     n = 10.0*game.dist*bigger+0.5;
43
44     if (n > 0) {
45         for (l = 1; l <= n; l++) {
46             w.x = (x += deltax) + 0.5;
47             w.y = (y += deltay) + 0.5;
48             if (!VALID_SECTOR(w.x, w.y)) {
49                 /* Leaving quadrant -- allow final enemy attack */
50                 /* Don't do it if being pushed by Nova */
51                 if (game.nenhere != 0 && game.iattak != 2) {
52                     newcnd();
53                     for_local_enemies(l) {
54                         finald = sqrt((w.x-game.ks[l].x)*(double)(w.x-game.ks[l].x) +
55                                       (w.y-game.ks[l].y)*(double)(w.y-game.ks[l].y));
56                         game.kavgd[l] = 0.5 * (finald+game.kdist[l]);
57                     }
58                     /*
59                      * Stas Sergeev added the game.condition
60                      * that attacks only happen if Klingons
61                      * are present and your skill is good.
62                      */
63                     if (game.skill > SKILL_GOOD && game.klhere > 0 && !game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
64                         attack(0);
65                     if (game.alldone) return;
66                 }
67                 /* compute final position -- new quadrant and sector */
68                 x = QUADSIZE*(game.quadrant.x-1)+game.sector.x;
69                 y = QUADSIZE*(game.quadrant.y-1)+game.sector.y;
70                 w.x = x+10.0*game.dist*bigger*deltax+0.5;
71                 w.y = y+10.0*game.dist*bigger*deltay+0.5;
72                 /* check for edge of galaxy */
73                 kinks = 0;
74                 do {
75                     kink = 0;
76                     if (w.x <= 0) {
77                         w.x = -w.x + 1;
78                         kink = 1;
79                     }
80                     if (w.y <= 0) {
81                         w.y = -w.y + 1;
82                         kink = 1;
83                     }
84                     if (w.x > GALSIZE*QUADSIZE) {
85                         w.x = (GALSIZE*QUADSIZE*2)+1 - w.x;
86                         kink = 1;
87                     }
88                     if (w.y > GALSIZE*QUADSIZE) {
89                         w.y = (GALSIZE*QUADSIZE*2)+1 - w.y;
90                         kink = 1;
91                     }
92                     if (kink) kinks = 1;
93                 } while (kink);
94
95                 if (kinks) {
96                     game.nkinks += 1;
97                     if (game.nkinks == 3) {
98                         /* Three strikes -- you're out! */
99                         finish(FNEG3);
100                         return;
101                     }
102                     skip(1);
103                     prout(_("YOU HAVE ATTEMPTED TO CROSS THE NEGATIVE ENERGY BARRIER"));
104                     prout(_("AT THE EDGE OF THE GALAXY.  THE THIRD TIME YOU TRY THIS,"));
105                     prout(_("YOU WILL BE DESTROYED."));
106                 }
107                 /* Compute final position in new quadrant */
108                 if (trbeam) return; /* Don't bother if we are to be beamed */
109                 game.quadrant.x = (w.x+(QUADSIZE-1))/QUADSIZE;
110                 game.quadrant.y = (w.y+(QUADSIZE-1))/QUADSIZE;
111                 game.sector.x = w.x - QUADSIZE*(game.quadrant.x-1);
112                 game.sector.y = w.y - QUADSIZE*(game.quadrant.y-1);
113                 skip(1);
114                 prout(_("Entering %s."), cramlc(quadrant, game.quadrant));
115                 game.quad[game.sector.x][game.sector.y] = game.ship;
116                 newqad(0);
117                 if (game.skill>SKILL_NOVICE) attack(0);
118                 return;
119             }
120             iquad = game.quad[w.x][w.y];
121             if (iquad != IHDOT) {
122                 /* object encountered in flight path */
123                 stopegy = 50.0*game.dist/game.optime;
124                 game.dist=0.1*sqrt((game.sector.x-w.x)*(double)(game.sector.x-w.x) +
125                               (game.sector.y-w.y)*(double)(game.sector.y-w.y));
126                 switch (iquad) {
127                 case IHT: /* Ram a Tholian */
128                 case IHK: /* Ram enemy ship */
129                 case IHC:
130                 case IHS:
131                 case IHR:
132                 case IHQUEST:
133                     game.sector.x = w.x;
134                     game.sector.y = w.y;
135                     ram(0, iquad, game.sector);
136                     finalx = game.sector.x;
137                     finaly = game.sector.y;
138                     break;
139                 case IHBLANK:
140                     skip(1);
141                     prouts(_("***RED ALERT!  RED ALERT!"));
142                     skip(1);
143                     proutn("***");
144                     crmshp();
145                     proutn(_(" pulled into black hole at "));
146                     prout(cramlc(sector, w));
147                     /*
148                      * Getting pulled into a black hole was certain
149                      * death in Almy's original.  Stas Sergeev added a
150                      * possibility that you'll get timewarped instead.
151                      */
152                     n=0;
153                     for (l=0;l<NDEVICES;l++)
154                         if (game.damage[l]>0) 
155                             n++;
156                     probf=pow(1.4,(game.energy+game.shield)/5000.0-1.0)*pow(1.3,1.0/(n+1)-1.0);
157                     if ((game.options & OPTION_BLKHOLE) && Rand()>probf) 
158                         timwrp();
159                     else 
160                         finish(FHOLE);
161                     return;
162                 default:
163                     /* something else */
164                     skip(1);
165                     crmshp();
166                     if (iquad == IHWEB)
167                         proutn(_(" encounters Tholian web at "));
168                     else
169                         proutn(_(" blocked by object at "));
170                     proutn(cramlc(sector, w));
171                     prout(";");
172                     proutn(_("Emergency stop required "));
173                     prout(_("%2d units of energy."), (int)stopegy);
174                     game.energy -= stopegy;
175                     finalx = x-deltax+0.5;
176                     game.sector.x = finalx;
177                     finaly = y-deltay+0.5;
178                     game.sector.y = finaly;
179                     if (game.energy <= 0) {
180                         finish(FNRG);
181                         return;
182                     }
183                     break;
184                 }
185                 goto no_quad_change;    /* sorry! */
186             }
187         }
188         game.dist = 0.1*sqrt((game.sector.x-w.x)*(double)(game.sector.x-w.x) +
189                         (game.sector.y-w.y)*(double)(game.sector.y-w.y));
190         game.sector.x = w.x;
191         game.sector.y = w.y;
192     }
193     finalx = game.sector.x;
194     finaly = game.sector.y;
195 no_quad_change:
196     /* No quadrant change -- compute new avg enemy distances */
197     game.quad[game.sector.x][game.sector.y] = game.ship;
198     if (game.nenhere) {
199         for_local_enemies(l) {
200             finald = sqrt((w.x-game.ks[l].x)*(double)(w.x-game.ks[l].x) +
201                           (w.y-game.ks[l].y)*(double)(w.y-game.ks[l].y));
202             game.kavgd[l] = 0.5 * (finald+game.kdist[l]);
203             game.kdist[l] = finald;
204         }
205         sortkl();
206         if (!game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova && game.iattak == 0)
207             attack(0);
208         for_local_enemies(l) game.kavgd[l] = game.kdist[l];
209     }
210     newcnd();
211     game.iattak = 0;
212     drawmaps(0);
213     setwnd(message_window);
214     return;
215 }
216
217 void dock(bool verbose) 
218 {
219     chew();
220     if (game.condit == IHDOCKED && verbose) {
221         prout(_("Already docked."));
222         return;
223     }
224     if (game.inorbit) {
225         prout(_("You must first leave standard orbit."));
226         return;
227     }
228     if (game.base.x==0 || abs(game.sector.x-game.base.x) > 1 || abs(game.sector.y-game.base.y) > 1) {
229         crmshp();
230         prout(_(" not adjacent to base."));
231         return;
232     }
233     game.condit = IHDOCKED;
234     if (verbose) prout(_("Docked."));
235     game.ididit=1;
236     if (game.energy < game.inenrg) game.energy = game.inenrg;
237     game.shield = game.inshld;
238     game.torps = game.intorps;
239     game.lsupres = game.inlsr;
240     game.state.crew = FULLCREW;
241     if (!damaged(DRADIO) &&
242         (is_scheduled(FCDBAS) || game.isatb == 1) && game.iseenit == 0) {
243         /* get attack report from base */
244         prout(_("Lt. Uhura- \"Captain, an important message from the starbase:\""));
245         attakreport(0);
246         game.iseenit = 1;
247     }
248 }
249
250 static void getcd(bool isprobe, int akey) {
251         /* This program originally required input in terms of a (clock)
252            direction and distance. Somewhere in history, it was changed to
253            cartesian coordinates. So we need to convert. I think
254            "manual" input should still be done this way -- it's a real
255            pain if the computer isn't working! Manual mode is still confusing
256            because it involves giving x and y motions, yet the coordinates
257            are always displayed y - x, where +y is downward! */
258
259         
260         int irowq=game.quadrant.x, icolq=game.quadrant.y, itemp=0, iprompt=0, key=0;
261         double xi, xj, xk, xl;
262         double deltax, deltay;
263         int automatic = -1;
264         coord incr;
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 == 1 && !isprobe) {
272                 prout(_("Dummy! You can't leave standard orbit until you"));
273                 proutn(_("are back aboard the "));
274                 crmshp();
275                 prout(".");
276                 chew();
277                 return;
278         }
279         while (automatic == -1) {
280                 if (damaged(DCOMPTR)) {
281                         if (isprobe)
282                                 prout(_("Computer damaged; manual navigation only"));
283                         else
284                                 prout(_("Computer damaged; manual movement only"));
285                         chew();
286                         automatic = 0;
287                         key = IHEOL;
288                         break;
289                 }
290                 if (isprobe && akey != -1) {
291                         /* For probe launch, use pre-scaned value first time */
292                         key = akey;
293                         akey = -1;
294                 }
295                 else 
296                         key = scan();
297
298                 if (key == IHEOL) {
299                         proutn(_("Manual or automatic- "));
300                         iprompt = 1;
301                         chew();
302                 }
303                 else if (key == IHALPHA) {
304                         if (isit("manual")) {
305                                 automatic =0;
306                                 key = scan();
307                                 break;
308                         }
309                         else if (isit("automatic")) {
310                                 automatic = 1;
311                                 key = scan();
312                                 break;
313                         }
314                         else {
315                                 huh();
316                                 chew();
317                                 return;
318                         }
319                 }
320                 else { /* numeric */
321                         if (isprobe)
322                                 prout(_("(Manual navigation assumed.)"));
323                         else
324                                 prout(_("(Manual movement assumed.)"));
325                         automatic = 0;
326                         break;
327                 }
328         }
329
330         if (automatic) {
331                 while (key == IHEOL) {
332                         if (isprobe)
333                                 proutn(_("Target quadrant or quadrant&sector- "));
334                         else
335                                 proutn(_("Destination sector or quadrant&sector- "));
336                         chew();
337                         iprompt = 1;
338                         key = scan();
339                 }
340
341                 if (key != IHREAL) {
342                         huh();
343                         return;
344                 }
345                 xi = aaitem;
346                 key = scan();
347                 if (key != IHREAL){
348                         huh();
349                         return;
350                 }
351                 xj = aaitem;
352                 key = scan();
353                 if (key == IHREAL) {
354                         /* both quadrant and sector specified */
355                         xk = aaitem;
356                         key = scan();
357                         if (key != IHREAL) {
358                                 huh();
359                                 return;
360                         }
361                         xl = aaitem;
362
363                         irowq = xi + 0.5;
364                         icolq = xj + 0.5;
365                         incr.y = xk + 0.5;
366                         incr.x = xl + 0.5;
367                 }
368                 else {
369                         if (isprobe) {
370                                 /* only quadrant specified -- go to center of dest quad */
371                                 irowq = xi + 0.5;
372                                 icolq = xj + 0.5;
373                                 incr.y = incr.x = 5;
374                         }
375                         else {
376                                 incr.y = xi + 0.5;
377                                 incr.x = xj + 0.5;
378                         }
379                         itemp = 1;
380                 }
381                 if (!VALID_QUADRANT(icolq,irowq)||!VALID_SECTOR(incr.x,incr.y)) {
382                     huh();
383                     return;
384                 }
385                 skip(1);
386                 if (!isprobe) {
387                         if (itemp) {
388                                 if (iprompt) {
389                                         prout(_("Helmsman Sulu- \"Course locked in for %s.\""),
390                                                 cramlc(sector, incr));
391                                 }
392                         }
393                         else prout(_("Ensign Chekov- \"Course laid in, Captain.\""));
394                 }
395                 deltax = icolq - game.quadrant.y + 0.1*(incr.x-game.sector.y);
396                 deltay = game.quadrant.x - irowq + 0.1*(game.sector.x-incr.y);
397         }
398         else { /* manual */
399                 while (key == IHEOL) {
400                         proutn(_("X and Y displacements- "));
401                         chew();
402                         iprompt = 1;
403                         key = scan();
404                 }
405                 itemp = 2;
406                 if (key != IHREAL) {
407                         huh();
408                         return;
409                 }
410                 deltax = aaitem;
411                 key = scan();
412                 if (key != IHREAL) {
413                         huh();
414                         return;
415                 }
416                 deltay = aaitem;
417         }
418         /* Check for zero movement */
419         if (deltax == 0 && deltay == 0) {
420                 chew();
421                 return;
422         }
423         if (itemp == 2 && !isprobe) {
424                 skip(1);
425                 prout(_("Helmsman Sulu- \"Aye, Sir.\""));
426         }
427         game.dist = sqrt(deltax*deltax + deltay*deltay);
428         game.direc = atan2(deltax, deltay)*1.90985932;
429         if (game.direc < 0.0) game.direc += 12.0;
430         chew();
431         return;
432
433 }
434                 
435
436
437 void impuls(void) 
438 {
439     double power;
440
441     game.ididit = 0;
442     if (damaged(DIMPULS)) {
443         chew();
444         skip(1);
445         prout(_("Engineer Scott- \"The impulse engines are damaged, Sir.\""));
446         return;
447     }
448
449     if (game.energy > 30.0) {
450         getcd(false, 0);
451         if (game.direc == -1.0) return;
452         power = 20.0 + 100.0*game.dist;
453     }
454     else
455         power = 30.0;
456
457     if (power >= game.energy) {
458         /* Insufficient power for trip */
459         skip(1);
460         prout(_("First Officer Spock- \"Captain, the impulse engines"));
461         prout(_("require 20.0 units to engage, plus 100.0 units per"));
462         if (game.energy > 30) {
463             proutn(_("quadrant.  We can go, therefore, a maximum of %d"),
464                    (int)(0.01 * (game.energy-20.0)-0.05));
465             prout(_(" quadrants.\""));
466         }
467         else {
468             prout(_("quadrant.  They are, therefore, useless.\""));
469         }
470         chew();
471         return;
472     }
473     /* Make sure enough time is left for the trip */
474     game.optime = game.dist/0.095;
475     if (game.optime >= game.state.remtime) {
476         prout(_("First Officer Spock- \"Captain, our speed under impulse"));
477         prout(_("power is only 0.95 sectors per stardate. Are you sure"));
478         proutn(_("we dare spend the time?\" "));
479         if (ja() == 0) return;
480     }
481     /* Activate impulse engines and pay the cost */
482     imove();
483     game.ididit = 1;
484     if (game.alldone) return;
485     power = 20.0 + 100.0*game.dist;
486     game.energy -= power;
487     game.optime = game.dist/0.095;
488     if (game.energy <= 0) finish(FNRG);
489     return;
490 }
491
492
493 void warp(bool timewarp) 
494 {
495     int blooey=0, twarp=0, iwarp;
496     double power;
497
498     if (!timewarp) { /* Not WARPX entry */
499         game.ididit = 0;
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 = 0;
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() == 0) { game.ididit = 0; 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 = 1;
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=1;
569         if (idebug && game.warpfac==10 && twarp==0) {
570             blooey=0;
571             proutn("=== Force time warp? ");
572             if (ja()==1) twarp=1;
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 = 0;
600                     twarp = 0;
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 = 1;
622     return;
623 }
624
625
626
627 void setwrp(void) 
628 {
629     int key;
630     double oldfac;
631         
632     while ((key=scan()) == IHEOL) {
633         chew();
634         proutn(_("Warp factor- "));
635     }
636     chew();
637     if (key != IHREAL) {
638         huh();
639         return;
640     }
641     if (game.damage[DWARPEN] > 10.0) {
642         prout(_("Warp engines inoperative."));
643         return;
644     }
645     if (damaged(DWARPEN) && aaitem > 4.0) {
646         prout(_("Engineer Scott- \"I'm doing my best, Captain,"));
647         prout(_("  but right now we can only go warp 4.\""));
648         return;
649     }
650     if (aaitem > 10.0) {
651         prout(_("Helmsman Sulu- \"Our top speed is warp 10, Captain.\""));
652         return;
653     }
654     if (aaitem < 1.0) {
655         prout(_("Helmsman Sulu- \"We can't go below warp 1, Captain.\""));
656         return;
657     }
658     oldfac = game.warpfac;
659     game.warpfac = aaitem;
660     game.wfacsq=game.warpfac*game.warpfac;
661     if (game.warpfac <= oldfac || game.warpfac <= 6.0) {
662         proutn(_("Helmsman Sulu- \"Warp factor %d, Captain.\""),
663                (int)game.warpfac);
664         return;
665     }
666     if (game.warpfac < 8.00) {
667         prout(_("Engineer Scott- \"Aye, but our maximum safe speed is warp 6.\""));
668         return;
669     }
670     if (game.warpfac == 10.0) {
671         prout(_("Engineer Scott- \"Aye, Captain, we'll try it.\""));
672         return;
673     }
674     prout(_("Engineer Scott- \"Aye, Captain, but our engines may not take it.\""));
675     return;
676 }
677
678 void atover(int igrab) 
679 {
680     double power, distreq;
681
682     chew();
683     /* is captain on planet? */
684     if (game.landed==1) {
685         if (damaged(DTRANSP)) {
686             finish(FPNOVA);
687             return;
688         }
689         prout(_("Scotty rushes to the transporter controls."));
690         if (game.shldup) {
691             prout(_("But with the shields up it's hopeless."));
692             finish(FPNOVA);
693         }
694         prouts(_("His desperate attempt to rescue you . . ."));
695         if (Rand() <= 0.5) {
696             prout(_("fails."));
697             finish(FPNOVA);
698             return;
699         }
700         prout(_("SUCCEEDS!"));
701         if (game.imine) {
702             game.imine = 0;
703             proutn(_("The crystals mined were "));
704             if (Rand() <= 0.25) {
705                 prout(_("lost."));
706             }
707             else {
708                 prout(_("saved."));
709                 game.icrystl = 1;
710             }
711         }
712     }
713     if (igrab) return;
714
715     /* Check to see if captain in shuttle craft */
716     if (game.icraft) finish(FSTRACTOR);
717     if (game.alldone) return;
718
719     /* Inform captain of attempt to reach safety */
720     skip(1);
721     do {
722         if (game.justin) {
723             prouts(_("***RED ALERT!  RED ALERT!"));
724             skip(1);
725             proutn(_("The "));
726             crmshp();
727             prout(_(" has stopped in a quadrant containing"));
728             prouts(_("   a supernova."));
729             skip(2);
730         }
731         proutn(_("***Emergency automatic override attempts to hurl "));
732         crmshp();
733         skip(1);
734         prout(_("safely out of quadrant."));
735         if (!damaged(DRADIO))
736             game.state.galaxy[game.quadrant.x][game.quadrant.y].charted = true;
737         /* Try to use warp engines */
738         if (damaged(DWARPEN)) {
739             skip(1);
740             prout(_("Warp engines damaged."));
741             finish(FSNOVAED);
742             return;
743         }
744         game.warpfac = 6.0+2.0*Rand();
745         game.wfacsq = game.warpfac * game.warpfac;
746         prout(_("Warp factor set to %d"), (int)game.warpfac);
747         power = 0.75*game.energy;
748         game.dist = power/(game.warpfac*game.warpfac*game.warpfac*(game.shldup+1));
749         distreq = 1.4142+Rand();
750         if (distreq < game.dist) game.dist = distreq;
751         game.optime = 10.0*game.dist/game.wfacsq;
752         game.direc = 12.0*Rand();       /* How dumb! */
753         game.justin = false;
754         game.inorbit = false;
755         warp(true);
756         if (!game.justin) {
757             /* This is bad news, we didn't leave quadrant. */
758             if (game.alldone) return;
759             skip(1);
760             prout(_("Insufficient energy to leave quadrant."));
761             finish(FSNOVAED);
762             return;
763         }
764     } while 
765         /* Repeat if another snova */
766         (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova);
767     if (KLINGREM==0) 
768         finish(FWON); /* Snova killed remaining enemy. */
769 }
770
771 void timwrp() 
772 {
773     int l, gotit;
774     prout(_("***TIME WARP ENTERED."));
775     if (game.state.snap && Rand() < 0.5) {
776         /* Go back in time */
777         prout(_("You are traveling backwards in time %d stardates."),
778               (int)(game.state.date-game.snapsht.date));
779         game.state = game.snapsht;
780         game.state.snap = 0;
781         if (game.state.remcom) {
782             schedule(FTBEAM, expran(game.intime/game.state.remcom));
783             schedule(FBATTAK, expran(0.3*game.intime));
784         }
785         schedule(FSNOVA, expran(0.5*game.intime));
786         /* next snapshot will be sooner */
787         schedule(FSNAP, expran(0.25*game.state.remtime));
788                                 
789         if (game.state.nscrem) schedule(FSCMOVE, 0.2777);
790         game.isatb = 0;
791         unschedule(FCDBAS);
792         unschedule(FSCDBAS);
793         game.battle.x = game.battle.y = 0;
794
795         /* Make sure Galileo is consistant -- Snapshot may have been taken
796            when on planet, which would give us two Galileos! */
797         gotit = 0;
798         for (l = 0; l < game.inplan; l++) {
799             if (game.state.plnets[l].known == shuttle_down) {
800                 gotit = 1;
801                 if (game.iscraft==1 && game.ship==IHE) {
802                     prout(_("Checkov-  \"Security reports the Galileo has disappeared, Sir!"));
803                     game.iscraft = 0;
804                 }
805             }
806         }
807         /* Likewise, if in the original time the Galileo was abandoned, but
808            was on ship earlier, it would have vanished -- lets restore it */
809         if (game.iscraft==0 && gotit==0 && game.damage[DSHUTTL] >= 0.0) {
810             prout(_("Checkov-  \"Security reports the Galileo has reappeared in the dock!\""));
811             game.iscraft = 1;
812         }
813         /* 
814          * There used to be code to do the actual reconstrction here,
815          * but the starchart is now part of the snapshotted galaxy state.
816          */
817         prout(_("Spock has reconstructed a correct star chart from memory"));
818     }
819     else {
820         /* Go forward in time */
821         game.optime = -0.5*game.intime*log(Rand());
822         prout(_("You are traveling forward in time %d stardates."), (int)game.optime);
823         /* cheat to make sure no tractor beams occur during time warp */
824         postpone(FTBEAM, game.optime);
825         game.damage[DRADIO] += game.optime;
826     }
827     newqad(0);
828     events();   /* Stas Sergeev added this -- do pending events */
829 }
830
831 void probe(void) 
832 {
833     double angle, bigger;
834     int key;
835     /* New code to launch a deep space probe */
836     if (game.nprobes == 0) {
837         chew();
838         skip(1);
839         if (game.ship == IHE) 
840             prout(_("Engineer Scott- \"We have no more deep space probes, Sir.\""));
841         else
842             prout(_("Ye Faerie Queene has no deep space probes."));
843         return;
844     }
845     if (damaged(DDSP)) {
846         chew();
847         skip(1);
848         prout(_("Engineer Scott- \"The probe launcher is damaged, Sir.\""));
849         return;
850     }
851     if (is_scheduled(FDSPROB)) {
852         chew();
853         skip(1);
854         if (damaged(DRADIO) && game.condit != IHDOCKED) {
855             prout(_("Spock-  \"Records show the previous probe has not yet"));
856             prout(_("   reached its destination.\""));
857         }
858         else
859             prout(_("Uhura- \"The previous probe is still reporting data, Sir.\""));
860         return;
861     }
862     key = scan();
863
864     if (key == IHEOL) {
865         /* slow mode, so let Kirk know how many probes there are left */
866         prout(game.nprobes==1 ? _("%d probe left.") : _("%d probes left."), game.nprobes);
867         proutn(_("Are you sure you want to fire a probe? "));
868         if (ja()==0) return;
869     }
870
871     game.isarmed = false;
872     if (key == IHALPHA && strcmp(citem,"armed") == 0) {
873         game.isarmed = true;
874         key = scan();
875     }
876     else if (key == IHEOL) {
877         proutn(_("Arm NOVAMAX warhead? "));
878         game.isarmed = ja();
879     }
880     getcd(true, key);
881     if (game.direc == -1.0) return;
882     game.nprobes--;
883     angle = ((15.0 - game.direc) * 0.5235988);
884     game.probeinx = -sin(angle);
885     game.probeiny = cos(angle);
886     if (fabs(game.probeinx) > fabs(game.probeiny))
887         bigger = fabs(game.probeinx);
888     else
889         bigger = fabs(game.probeiny);
890                 
891     game.probeiny /= bigger;
892     game.probeinx /= bigger;
893     game.proben = 10.0*game.dist*bigger +0.5;
894     game.probex = game.quadrant.x*QUADSIZE + game.sector.x - 1; // We will use better packing than original
895     game.probey = game.quadrant.y*QUADSIZE + game.sector.y - 1;
896     game.probec = game.quadrant;
897     schedule(FDSPROB, 0.01); // Time to move one sector
898     prout(_("Ensign Chekov-  \"The deep space probe is launched, Captain.\""));
899     game.ididit = 1;
900     return;
901 }
902
903 void mayday(void) 
904 {
905     /* There's more than one way to move in this game! */
906     double ddist, xdist, probf;
907     int line = 0, l, ix, iy;
908
909     chew();
910     /* Test for game.conditions which prevent calling for help */
911     if (game.condit == IHDOCKED) {
912         prout(_("Lt. Uhura-  \"But Captain, we're already docked.\""));
913         return;
914     }
915     if (damaged(DRADIO)) {
916         prout(_("Subspace radio damaged."));
917         return;
918     }
919     if (game.state.rembase==0) {
920         prout(_("Lt. Uhura-  \"Captain, I'm not getting any response from Starbase.\""));
921         return;
922     }
923     if (game.landed == 1) {
924         proutn(_("You must be aboard the "));
925         crmshp();
926         prout(".");
927         return;
928     }
929     /* OK -- call for help from nearest starbase */
930     game.nhelp++;
931     if (game.base.x!=0) {
932         /* There's one in this quadrant */
933         ddist = sqrt(square(game.base.x-game.sector.x)+square(game.base.y-game.sector.y));
934     }
935     else {
936         ddist = FOREVER;
937         for_starbases(l) {
938             xdist=10.0*sqrt(square(game.state.baseq[l].x-game.quadrant.x)+square(game.state.baseq[l].y-game.quadrant.y));
939             if (xdist < ddist) {
940                 ddist = xdist;
941                 line = l;
942             }
943         }
944         /* Since starbase not in quadrant, set up new quadrant */
945         game.quadrant = game.state.baseq[line];
946         newqad(1);
947     }
948     /* dematerialize starship */
949     game.quad[game.sector.x][game.sector.y]=IHDOT;
950     proutn(_("Starbase in %s responds--"), cramlc(quadrant, game.quadrant));
951     proutn("");
952     crmshp();
953     prout(_(" dematerializes."));
954     game.sector.x=0;
955     for (l = 1; l <= 5; l++) {
956         ix = game.base.x+3.0*Rand()-1;
957         iy = game.base.y+3.0*Rand()-1;
958         if (VALID_SECTOR(ix,iy) && game.quad[ix][iy]==IHDOT) {
959             /* found one -- finish up */
960             game.sector.x=ix;
961             game.sector.y=iy;
962             break;
963         }
964     }
965     if (game.sector.x==0){
966         prout(_("You have been lost in space..."));
967         finish(FMATERIALIZE);
968         return;
969     }
970     /* Give starbase three chances to rematerialize starship */
971     probf = pow((1.0 - pow(0.98,ddist)), 0.33333333);
972     for (l = 1; l <= 3; l++) {
973         switch (l) {
974         case 1: proutn(_("1st")); break;
975         case 2: proutn(_("2nd")); break;
976         case 3: proutn(_("3rd")); break;
977         }
978         proutn(_(" attempt to re-materialize "));
979         crmshp();
980         switch (l){
981         case 1: game.quad[ix][iy]=IHMATER0;
982             break;
983         case 2: game.quad[ix][iy]=IHMATER1;
984             break;
985         case 3: game.quad[ix][iy]=IHMATER2;
986             break;
987         }
988         textcolor(RED);
989         warble();
990         if (Rand() > probf) break;
991         prout(_("fails."));
992         delay(500);
993         textcolor(DEFAULT);
994     }
995     if (l > 3) {
996         game.quad[ix][iy]=IHQUEST;
997         game.alive = 0;
998         drawmaps(1);
999         setwnd(message_window);
1000         finish(FMATERIALIZE);
1001         return;
1002     }
1003     game.quad[ix][iy]=game.ship;
1004     textcolor(GREEN);
1005     prout(_("succeeds."));
1006     textcolor(DEFAULT);
1007     dock(0);
1008     skip(1);
1009     prout(_("Lt. Uhura-  \"Captain, we made it!\""));
1010 }