cb6e197b51e5c5625693382f65f9fd830767e42f
[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 == 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 (navmode == unspecified) {
280         if (damaged(DNAVSYS)) {
281             if (isprobe)
282                 prout(_("Computer damaged; manual navigation only"));
283             else
284                 prout(_("Computer damaged; manual movement only"));
285             chew();
286             navmode = manual;
287             key = IHEOL;
288             break;
289         }
290         if (isprobe && akey != -1) {
291             /* For probe launch, use pre-scanned 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 = true;
301             chew();
302         }
303         else if (key == IHALPHA) {
304             if (isit("manual")) {
305                 navmode = manual;
306                 key = scan();
307                 break;
308             }
309             else if (isit("automatic")) {
310                 navmode = automatic;
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             navmode = automatic;
326             break;
327         }
328     }
329
330     if (navmode == 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 = true;
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 = normal;
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 > curt) {
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 = true;
403             key = scan();
404         }
405         itemp = verbose;
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 == verbose && !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 void impuls(void) 
437 /* move under impulse power */
438 {
439     double power;
440
441     game.ididit = false;
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() == false) return;
480     }
481     /* Activate impulse engines and pay the cost */
482     imove();
483     game.ididit = true;
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 /* move under warp drive */
495 {
496     int iwarp;
497     bool blooey = false, twarp = false;
498     double power;
499
500     if (!timewarp) { /* Not WARPX entry */
501         game.ididit = false;
502         if (game.damage[DWARPEN] > 10.0) {
503             chew();
504             skip(1);
505             prout(_("Engineer Scott- \"The impulse engines are damaged, Sir.\""));
506             return;
507         }
508         if (damaged(DWARPEN) && game.warpfac > 4.0) {
509             chew();
510             skip(1);
511             prout(_("Engineer Scott- \"Sorry, Captain. Until this damage"));
512             prout(_("  is repaired, I can only give you warp 4.\""));
513             return;
514         }
515                         
516         /* Read in course and distance */
517         getcd(false, 0);
518         if (game.direc == -1.0) return;
519
520         /* Make sure starship has enough energy for the trip */
521         power = (game.dist+0.05)*game.warpfac*game.warpfac*game.warpfac*(game.shldup+1);
522
523
524         if (power >= game.energy) {
525             /* Insufficient power for trip */
526             game.ididit = false;
527             skip(1);
528             prout(_("Engineering to bridge--"));
529             if (!game.shldup || 0.5*power > game.energy) {
530                 iwarp = pow((game.energy/(game.dist+0.05)), 0.333333333);
531                 if (iwarp <= 0) {
532                     prout(_("We can't do it, Captain. We don't have enough energy."));
533                 }
534                 else {
535                     proutn(_("We don't have enough energy, but we could do it at warp %d"), iwarp);
536                     if (game.shldup) {
537                         prout(",");
538                         prout(_("if you'll lower the shields."));
539                     }
540                     else
541                         prout(".");
542                 }
543             }
544             else
545                 prout(_("We haven't the energy to go that far with the shields up."));
546             return;
547         }
548                                                 
549         /* Make sure enough time is left for the trip */
550         game.optime = 10.0*game.dist/game.wfacsq;
551         if (game.optime >= 0.8*game.state.remtime) {
552             skip(1);
553             prout(_("First Officer Spock- \"Captain, I compute that such"));
554             proutn(_("  a trip would require approximately %2.0f"),
555                    100.0*game.optime/game.state.remtime);
556             prout(_(" percent of our"));
557             proutn(_("  remaining time.  Are you sure this is wise?\" "));
558             if (ja() == false) { game.ididit = false; game.optime=0; return;}
559         }
560     }
561     /* Entry WARPX */
562     if (game.warpfac > 6.0) {
563         /* Decide if engine damage will occur */
564         double prob = game.dist*(6.0-game.warpfac)*(6.0-game.warpfac)/66.666666666;
565         if (prob > Rand()) {
566             blooey = true;
567             game.dist = Rand()*game.dist;
568         }
569         /* Decide if time warp will occur */
570         if (0.5*game.dist*pow(7.0,game.warpfac-10.0) > Rand()) twarp = true;
571         if (idebug && game.warpfac==10 && !twarp) {
572             blooey = false;
573             proutn("=== Force time warp? ");
574             if (ja() == true) twarp = true;
575         }
576         if (blooey || twarp) {
577             /* If time warp or engine damage, check path */
578             /* If it is obstructed, don't do warp or damage */
579             double angle = ((15.0-game.direc)*0.5235998);
580             double deltax = -sin(angle);
581             double deltay = cos(angle);
582             double bigger, x, y;
583             int n, l, ix, iy;
584             if (fabs(deltax) > fabs(deltay))
585                 bigger = fabs(deltax);
586             else
587                 bigger = fabs(deltay);
588                         
589             deltax /= bigger;
590             deltay /= bigger;
591             n = 10.0 * game.dist * bigger +0.5;
592             x = game.sector.x;
593             y = game.sector.y;
594             for (l = 1; l <= n; l++) {
595                 x += deltax;
596                 ix = x + 0.5;
597                 y += deltay;
598                 iy = y +0.5;
599                 if (!VALID_SECTOR(ix, iy)) break;
600                 if (game.quad[ix][iy] != IHDOT) {
601                     blooey = false;
602                     twarp = false;
603                 }
604             }
605         }
606     }
607                                 
608
609     /* Activate Warp Engines and pay the cost */
610     imove();
611     if (game.alldone) return;
612     game.energy -= game.dist*game.warpfac*game.warpfac*game.warpfac*(game.shldup+1);
613     if (game.energy <= 0) finish(FNRG);
614     game.optime = 10.0*game.dist/game.wfacsq;
615     if (twarp) timwrp();
616     if (blooey) {
617         game.damage[DWARPEN] = game.damfac*(3.0*Rand()+1.0);
618         skip(1);
619         prout(_("Engineering to bridge--"));
620         prout(_("  Scott here.  The warp engines are damaged."));
621         prout(_("  We'll have to reduce speed to warp 4."));
622     }
623     game.ididit = true;
624     return;
625 }
626
627
628
629 void setwrp(void) 
630 /* change the warp factor */
631 {
632     int key;
633     double oldfac;
634         
635     while ((key=scan()) == IHEOL) {
636         chew();
637         proutn(_("Warp factor- "));
638     }
639     chew();
640     if (key != IHREAL) {
641         huh();
642         return;
643     }
644     if (game.damage[DWARPEN] > 10.0) {
645         prout(_("Warp engines inoperative."));
646         return;
647     }
648     if (damaged(DWARPEN) && aaitem > 4.0) {
649         prout(_("Engineer Scott- \"I'm doing my best, Captain,"));
650         prout(_("  but right now we can only go warp 4.\""));
651         return;
652     }
653     if (aaitem > 10.0) {
654         prout(_("Helmsman Sulu- \"Our top speed is warp 10, Captain.\""));
655         return;
656     }
657     if (aaitem < 1.0) {
658         prout(_("Helmsman Sulu- \"We can't go below warp 1, Captain.\""));
659         return;
660     }
661     oldfac = game.warpfac;
662     game.warpfac = aaitem;
663     game.wfacsq=game.warpfac*game.warpfac;
664     if (game.warpfac <= oldfac || game.warpfac <= 6.0) {
665         prout(_("Helmsman Sulu- \"Warp factor %d, Captain.\""),
666                (int)game.warpfac);
667         return;
668     }
669     if (game.warpfac < 8.00) {
670         prout(_("Engineer Scott- \"Aye, but our maximum safe speed is warp 6.\""));
671         return;
672     }
673     if (game.warpfac == 10.0) {
674         prout(_("Engineer Scott- \"Aye, Captain, we'll try it.\""));
675         return;
676     }
677     prout(_("Engineer Scott- \"Aye, Captain, but our engines may not take it.\""));
678     return;
679 }
680
681 void atover(bool igrab) 
682 /* cope with being tossed out of quadrant by supernova or yanked by beam */
683 {
684     double power, distreq;
685
686     chew();
687     /* is captain on planet? */
688     if (game.landed==1) {
689         if (damaged(DTRANSP)) {
690             finish(FPNOVA);
691             return;
692         }
693         prout(_("Scotty rushes to the transporter controls."));
694         if (game.shldup) {
695             prout(_("But with the shields up it's hopeless."));
696             finish(FPNOVA);
697         }
698         prouts(_("His desperate attempt to rescue you . . ."));
699         if (Rand() <= 0.5) {
700             prout(_("fails."));
701             finish(FPNOVA);
702             return;
703         }
704         prout(_("SUCCEEDS!"));
705         if (game.imine) {
706             game.imine = false;
707             proutn(_("The crystals mined were "));
708             if (Rand() <= 0.25) {
709                 prout(_("lost."));
710             }
711             else {
712                 prout(_("saved."));
713                 game.icrystl = true;
714             }
715         }
716     }
717     if (igrab) return;
718
719     /* Check to see if captain in shuttle craft */
720     if (game.icraft) finish(FSTRACTOR);
721     if (game.alldone) return;
722
723     /* Inform captain of attempt to reach safety */
724     skip(1);
725     do {
726         if (game.justin) {
727             prouts(_("***RED ALERT!  RED ALERT!"));
728             skip(1);
729             proutn(_("The "));
730             crmshp();
731             prout(_(" has stopped in a quadrant containing"));
732             prouts(_("   a supernova."));
733             skip(2);
734         }
735         proutn(_("***Emergency automatic override attempts to hurl "));
736         crmshp();
737         skip(1);
738         prout(_("safely out of quadrant."));
739         if (!damaged(DRADIO))
740             game.state.galaxy[game.quadrant.x][game.quadrant.y].charted = true;
741         /* Try to use warp engines */
742         if (damaged(DWARPEN)) {
743             skip(1);
744             prout(_("Warp engines damaged."));
745             finish(FSNOVAED);
746             return;
747         }
748         game.warpfac = 6.0+2.0*Rand();
749         game.wfacsq = game.warpfac * game.warpfac;
750         prout(_("Warp factor set to %d"), (int)game.warpfac);
751         power = 0.75*game.energy;
752         game.dist = power/(game.warpfac*game.warpfac*game.warpfac*(game.shldup+1));
753         distreq = 1.4142+Rand();
754         if (distreq < game.dist) game.dist = distreq;
755         game.optime = 10.0*game.dist/game.wfacsq;
756         game.direc = 12.0*Rand();       /* How dumb! */
757         game.justin = false;
758         game.inorbit = false;
759         warp(true);
760         if (!game.justin) {
761             /* This is bad news, we didn't leave quadrant. */
762             if (game.alldone) return;
763             skip(1);
764             prout(_("Insufficient energy to leave quadrant."));
765             finish(FSNOVAED);
766             return;
767         }
768     } while 
769         /* Repeat if another snova */
770         (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova);
771     if (KLINGREM==0) 
772         finish(FWON); /* Snova killed remaining enemy. */
773 }
774
775 void timwrp() 
776 /* let's do the time warp again */
777 {
778     int l;
779     bool gotit;
780     prout(_("***TIME WARP ENTERED."));
781     if (game.state.snap && Rand() < 0.5) {
782         /* Go back in time */
783         prout(_("You are traveling backwards in time %d stardates."),
784               (int)(game.state.date-game.snapsht.date));
785         game.state = game.snapsht;
786         game.state.snap = 0;
787         if (game.state.remcom) {
788             schedule(FTBEAM, expran(game.intime/game.state.remcom));
789             schedule(FBATTAK, expran(0.3*game.intime));
790         }
791         schedule(FSNOVA, expran(0.5*game.intime));
792         /* next snapshot will be sooner */
793         schedule(FSNAP, expran(0.25*game.state.remtime));
794                                 
795         if (game.state.nscrem) schedule(FSCMOVE, 0.2777);
796         game.isatb = 0;
797         unschedule(FCDBAS);
798         unschedule(FSCDBAS);
799         game.battle.x = game.battle.y = 0;
800
801         /* Make sure Galileo is consistant -- Snapshot may have been taken
802            when on planet, which would give us two Galileos! */
803         gotit = false;
804         for (l = 0; l < game.inplan; l++) {
805             if (game.state.plnets[l].known == shuttle_down) {
806                 gotit = true;
807                 if (game.iscraft==1 && game.ship==IHE) {
808                     prout(_("Checkov-  \"Security reports the Galileo has disappeared, Sir!"));
809                     game.iscraft = 0;
810                 }
811             }
812         }
813         /* Likewise, if in the original time the Galileo was abandoned, but
814            was on ship earlier, it would have vanished -- lets restore it */
815         if (game.iscraft==0 && !gotit && game.damage[DSHUTTL] >= 0.0) {
816             prout(_("Checkov-  \"Security reports the Galileo has reappeared in the dock!\""));
817             game.iscraft = 1;
818         }
819         /* 
820          * There used to be code to do the actual reconstrction here,
821          * but the starchart is now part of the snapshotted galaxy state.
822          */
823         prout(_("Spock has reconstructed a correct star chart from memory"));
824     }
825     else {
826         /* Go forward in time */
827         game.optime = -0.5*game.intime*log(Rand());
828         prout(_("You are traveling forward in time %d stardates."), (int)game.optime);
829         /* cheat to make sure no tractor beams occur during time warp */
830         postpone(FTBEAM, game.optime);
831         game.damage[DRADIO] += game.optime;
832     }
833     newqad(false);
834     events();   /* Stas Sergeev added this -- do pending events */
835 }
836
837 void probe(void) 
838 /* launch deep-space probe */
839 {
840     double angle, bigger;
841     int key;
842     /* New code to launch a deep space probe */
843     if (game.nprobes == 0) {
844         chew();
845         skip(1);
846         if (game.ship == IHE) 
847             prout(_("Engineer Scott- \"We have no more deep space probes, Sir.\""));
848         else
849             prout(_("Ye Faerie Queene has no deep space probes."));
850         return;
851     }
852     if (damaged(DDSP)) {
853         chew();
854         skip(1);
855         prout(_("Engineer Scott- \"The probe launcher is damaged, Sir.\""));
856         return;
857     }
858     if (is_scheduled(FDSPROB)) {
859         chew();
860         skip(1);
861         if (damaged(DRADIO) && game.condition != docked) {
862             prout(_("Spock-  \"Records show the previous probe has not yet"));
863             prout(_("   reached its destination.\""));
864         }
865         else
866             prout(_("Uhura- \"The previous probe is still reporting data, Sir.\""));
867         return;
868     }
869     key = scan();
870
871     if (key == IHEOL) {
872         /* slow mode, so let Kirk know how many probes there are left */
873         prout(game.nprobes==1 ? _("%d probe left.") : _("%d probes left."), game.nprobes);
874         proutn(_("Are you sure you want to fire a probe? "));
875         if (ja() == false) return;
876     }
877
878     game.isarmed = false;
879     if (key == IHALPHA && strcmp(citem,"armed") == 0) {
880         game.isarmed = true;
881         key = scan();
882     }
883     else if (key == IHEOL) {
884         proutn(_("Arm NOVAMAX warhead? "));
885         game.isarmed = ja();
886     }
887     getcd(true, key);
888     if (game.direc == -1.0) return;
889     game.nprobes--;
890     angle = ((15.0 - game.direc) * 0.5235988);
891     game.probeinx = -sin(angle);
892     game.probeiny = cos(angle);
893     if (fabs(game.probeinx) > fabs(game.probeiny))
894         bigger = fabs(game.probeinx);
895     else
896         bigger = fabs(game.probeiny);
897                 
898     game.probeiny /= bigger;
899     game.probeinx /= bigger;
900     game.proben = 10.0*game.dist*bigger +0.5;
901     game.probex = game.quadrant.x*QUADSIZE + game.sector.x - 1; // We will use better packing than original
902     game.probey = game.quadrant.y*QUADSIZE + game.sector.y - 1;
903     game.probec = game.quadrant;
904     schedule(FDSPROB, 0.01); // Time to move one sector
905     prout(_("Ensign Chekov-  \"The deep space probe is launched, Captain.\""));
906     game.ididit = true;
907     return;
908 }
909
910 /*
911  *      Here's how the mayday code works:
912  *
913  *      First, the closest starbase is selected.  If there is a
914  *      a starbase in your own quadrant, you are in good shape.
915  *      This distance takes quadrant distances into account only.
916  *
917  *      A magic number is computed based on the distance which acts
918  *      as the probability that you will be rematerialized.  You
919  *      get three tries.
920  *
921  *      When it is determined that you should be able to be remater-
922  *      ialized (i.e., when the probability thing mentioned above
923  *      comes up positive), you are put into that quadrant (anywhere).
924  *      Then, we try to see if there is a spot adjacent to the star-
925  *      base.  If not, you can't be rematerialized!!!  Otherwise,
926  *      it drops you there.  It only tries five times to find a spot
927  *      to drop you.  After that, it's your problem.
928  */
929
930 void mayday(void) 
931 /* yell for help from nearest starbase */
932 {
933     /* There's more than one way to move in this game! */
934     double ddist, xdist, probf;
935     int line = 0, m, ix, iy;
936
937     chew();
938     /* Test for conditions which prevent calling for help */
939     if (game.condition == docked) {
940         prout(_("Lt. Uhura-  \"But Captain, we're already docked.\""));
941         return;
942     }
943     if (damaged(DRADIO)) {
944         prout(_("Subspace radio damaged."));
945         return;
946     }
947     if (game.state.rembase==0) {
948         prout(_("Lt. Uhura-  \"Captain, I'm not getting any response from Starbase.\""));
949         return;
950     }
951     if (game.landed == 1) {
952         proutn(_("You must be aboard the "));
953         crmshp();
954         prout(".");
955         return;
956     }
957     /* OK -- call for help from nearest starbase */
958     game.nhelp++;
959     if (game.base.x!=0) {
960         /* There's one in this quadrant */
961         ddist = distance(game.base, game.sector);
962     }
963     else {
964         ddist = FOREVER;
965         for_starbases(m) {
966             xdist = QUADSIZE * distance(game.state.baseq[m], game.quadrant);
967             if (xdist < ddist) {
968                 ddist = xdist;
969                 line = m;
970             }
971         }
972         /* Since starbase not in quadrant, set up new quadrant */
973         game.quadrant = game.state.baseq[line];
974         newqad(true);
975     }
976     /* dematerialize starship */
977     game.quad[game.sector.x][game.sector.y]=IHDOT;
978     proutn(_("Starbase in %s responds--"), cramlc(quadrant, game.quadrant));
979     proutn("");
980     crmshp();
981     prout(_(" dematerializes."));
982     game.sector.x=0;
983     for (m = 1; m <= 5; m++) {
984         ix = game.base.x+3.0*Rand()-1;
985         iy = game.base.y+3.0*Rand()-1;
986         if (VALID_SECTOR(ix,iy) && game.quad[ix][iy]==IHDOT) {
987             /* found one -- finish up */
988             game.sector.x=ix;
989             game.sector.y=iy;
990             break;
991         }
992     }
993     if (game.sector.x==0){
994         prout(_("You have been lost in space..."));
995         finish(FMATERIALIZE);
996         return;
997     }
998     /* Give starbase three chances to rematerialize starship */
999     probf = pow((1.0 - pow(0.98,ddist)), 0.33333333);
1000     for (m = 1; m <= 3; m++) {
1001         switch (m) {
1002         case 1: proutn(_("1st")); break;
1003         case 2: proutn(_("2nd")); break;
1004         case 3: proutn(_("3rd")); break;
1005         }
1006         proutn(_(" attempt to re-materialize "));
1007         crmshp();
1008         switch (m){
1009         case 1: game.quad[ix][iy]=IHMATER0;
1010             break;
1011         case 2: game.quad[ix][iy]=IHMATER1;
1012             break;
1013         case 3: game.quad[ix][iy]=IHMATER2;
1014             break;
1015         }
1016         textcolor(RED);
1017         warble();
1018         if (Rand() > probf) break;
1019         prout(_("fails."));
1020         delay(500);
1021         textcolor(DEFAULT);
1022     }
1023     if (m > 3) {
1024         game.quad[ix][iy]=IHQUEST;
1025         game.alive = 0;
1026         drawmaps(1);
1027         setwnd(message_window);
1028         finish(FMATERIALIZE);
1029         return;
1030     }
1031     game.quad[ix][iy]=game.ship;
1032     textcolor(GREEN);
1033     prout(_("succeeds."));
1034     textcolor(DEFAULT);
1035     dock(false);
1036     skip(1);
1037     prout(_("Lt. Uhura-  \"Captain, we made it!\""));
1038 }