More uses of distance() macro. Eliminate some magic numbers.
[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, iquad;
13     coord w, final;
14     bool trbeam = false;
15
16     w.x = w.y = 0;
17     if (game.inorbit) {
18         prout(_("Helmsman Sulu- \"Leaving standard orbit.\""));
19         game.inorbit = false;
20     }
21
22     angle = ((15.0 - game.direc) * 0.5235988);
23     deltax = -sin(angle);
24     deltay = cos(angle);
25     if (fabs(deltax) > fabs(deltay))
26         bigger = fabs(deltax);
27     else
28         bigger = fabs(deltay);
29                 
30     deltay /= bigger;
31     deltax /= bigger;
32
33     /* If tractor beam is to occur, don't move full distance */
34     if (game.state.date+game.optime >= scheduled(FTBEAM)) {
35         trbeam = true;
36         game.condit = IHRED;
37         game.dist = game.dist*(scheduled(FTBEAM)-game.state.date)/game.optime + 0.1;
38         game.optime = scheduled(FTBEAM) - game.state.date + 1e-5;
39     }
40     /* Move within the quadrant */
41     game.quad[game.sector.x][game.sector.y] = IHDOT;
42     x = game.sector.x;
43     y = game.sector.y;
44     n = 10.0*game.dist*bigger+0.5;
45
46     if (n > 0) {
47         for (m = 1; m <= n; m++) {
48             w.x = (x += deltax) + 0.5;
49             w.y = (y += deltay) + 0.5;
50             if (!VALID_SECTOR(w.x, w.y)) {
51                 /* Leaving quadrant -- allow final enemy attack */
52                 /* Don't do it if being pushed by Nova */
53                 if (game.nenhere != 0 && game.iattak != 2) {
54                     newcnd();
55                     for_local_enemies(m) {
56                         finald = distance(w, game.ks[m]);
57                         game.kavgd[m] = 0.5 * (finald + game.kdist[m]);
58                     }
59                     /*
60                      * Stas Sergeev added the game.condition
61                      * that attacks only happen if Klingons
62                      * are present and your skill is good.
63                      */
64                     if (game.skill > SKILL_GOOD && game.klhere > 0 && !game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
65                         attack(0);
66                     if (game.alldone) return;
67                 }
68                 /* compute final position -- new quadrant and sector */
69                 x = QUADSIZE*(game.quadrant.x-1)+game.sector.x;
70                 y = QUADSIZE*(game.quadrant.y-1)+game.sector.y;
71                 w.x = x+10.0*game.dist*bigger*deltax+0.5;
72                 w.y = y+10.0*game.dist*bigger*deltay+0.5;
73                 /* check for edge of galaxy */
74                 kinks = 0;
75                 do {
76                     kink = 0;
77                     if (w.x <= 0) {
78                         w.x = -w.x + 1;
79                         kink = 1;
80                     }
81                     if (w.y <= 0) {
82                         w.y = -w.y + 1;
83                         kink = 1;
84                     }
85                     if (w.x > GALSIZE*QUADSIZE) {
86                         w.x = (GALSIZE*QUADSIZE*2)+1 - w.x;
87                         kink = 1;
88                     }
89                     if (w.y > GALSIZE*QUADSIZE) {
90                         w.y = (GALSIZE*QUADSIZE*2)+1 - w.y;
91                         kink = 1;
92                     }
93                     if (kink) kinks = 1;
94                 } while (kink);
95
96                 if (kinks) {
97                     game.nkinks += 1;
98                     if (game.nkinks == 3) {
99                         /* Three strikes -- you're out! */
100                         finish(FNEG3);
101                         return;
102                     }
103                     skip(1);
104                     prout(_("YOU HAVE ATTEMPTED TO CROSS THE NEGATIVE ENERGY BARRIER"));
105                     prout(_("AT THE EDGE OF THE GALAXY.  THE THIRD TIME YOU TRY THIS,"));
106                     prout(_("YOU WILL BE DESTROYED."));
107                 }
108                 /* Compute final position in new quadrant */
109                 if (trbeam) return; /* Don't bother if we are to be beamed */
110                 game.quadrant.x = (w.x+(QUADSIZE-1))/QUADSIZE;
111                 game.quadrant.y = (w.y+(QUADSIZE-1))/QUADSIZE;
112                 game.sector.x = w.x - QUADSIZE*(game.quadrant.x-1);
113                 game.sector.y = w.y - QUADSIZE*(game.quadrant.y-1);
114                 skip(1);
115                 prout(_("Entering %s."), cramlc(quadrant, game.quadrant));
116                 game.quad[game.sector.x][game.sector.y] = game.ship;
117                 newqad(false);
118                 if (game.skill>SKILL_NOVICE) attack(0);
119                 return;
120             }
121             iquad = game.quad[w.x][w.y];
122             if (iquad != IHDOT) {
123                 /* object encountered in flight path */
124                 stopegy = 50.0*game.dist/game.optime;
125                 game.dist = distance(game.sector, w) / (QUADSIZE * 1.0);
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 = w;
134                     ram(0, iquad, game.sector);
135                     final = game.sector;
136                     break;
137                 case IHBLANK:
138                     skip(1);
139                     prouts(_("***RED ALERT!  RED ALERT!"));
140                     skip(1);
141                     proutn("***");
142                     crmshp();
143                     proutn(_(" pulled into black hole at "));
144                     prout(cramlc(sector, w));
145                     /*
146                      * Getting pulled into a black hole was certain
147                      * death in Almy's original.  Stas Sergeev added a
148                      * possibility that you'll get timewarped instead.
149                      */
150                     n=0;
151                     for (m=0;m<NDEVICES;m++)
152                         if (game.damage[m]>0) 
153                             n++;
154                     probf=pow(1.4,(game.energy+game.shield)/5000.0-1.0)*pow(1.3,1.0/(n+1)-1.0);
155                     if ((game.options & OPTION_BLKHOLE) && Rand()>probf) 
156                         timwrp();
157                     else 
158                         finish(FHOLE);
159                     return;
160                 default:
161                     /* something else */
162                     skip(1);
163                     crmshp();
164                     if (iquad == IHWEB)
165                         proutn(_(" encounters Tholian web at "));
166                     else
167                         proutn(_(" blocked by object at "));
168                     proutn(cramlc(sector, w));
169                     prout(";");
170                     proutn(_("Emergency stop required "));
171                     prout(_("%2d units of energy."), (int)stopegy);
172                     game.energy -= stopegy;
173                     final.x = x-deltax+0.5;
174                     final.y = y-deltay+0.5;
175                     game.sector = final;
176                     if (game.energy <= 0) {
177                         finish(FNRG);
178                         return;
179                     }
180                     break;
181                 }
182                 goto no_quad_change;    /* sorry! */
183             }
184         }
185         game.dist = distance(game.sector, w) / (QUADSIZE * 1.0);
186         game.sector = w;
187     }
188     final = game.sector;
189 no_quad_change:
190     /* No quadrant change -- compute new avg enemy distances */
191     game.quad[game.sector.x][game.sector.y] = game.ship;
192     if (game.nenhere) {
193         for_local_enemies(m) {
194             finald = distance(w, game.ks[m]);
195             game.kavgd[m] = 0.5 * (finald+game.kdist[m]);
196             game.kdist[m] = finald;
197         }
198         sortkl();
199         if (!game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova && game.iattak == 0)
200             attack(0);
201         for_local_enemies(m) game.kavgd[m] = game.kdist[m];
202     }
203     newcnd();
204     game.iattak = 0;
205     drawmaps(0);
206     setwnd(message_window);
207     return;
208 }
209
210 void dock(bool verbose) 
211 /* dock our ship at a starbase */
212 {
213     chew();
214     if (game.condit == IHDOCKED && verbose) {
215         prout(_("Already docked."));
216         return;
217     }
218     if (game.inorbit) {
219         prout(_("You must first leave standard orbit."));
220         return;
221     }
222     if (game.base.x==0 || abs(game.sector.x-game.base.x) > 1 || abs(game.sector.y-game.base.y) > 1) {
223         crmshp();
224         prout(_(" not adjacent to base."));
225         return;
226     }
227     game.condit = IHDOCKED;
228     if (verbose) prout(_("Docked."));
229     game.ididit = true;
230     if (game.energy < game.inenrg) game.energy = game.inenrg;
231     game.shield = game.inshld;
232     game.torps = game.intorps;
233     game.lsupres = game.inlsr;
234     game.state.crew = FULLCREW;
235     if (!damaged(DRADIO) &&
236         (is_scheduled(FCDBAS) || game.isatb == 1) && game.iseenit == 0) {
237         /* get attack report from base */
238         prout(_("Lt. Uhura- \"Captain, an important message from the starbase:\""));
239         attakreport(false);
240         game.iseenit = 1;
241     }
242 }
243
244 /* 
245  * This program originally required input in terms of a (clock)
246  * direction and distance. Somewhere in history, it was changed to
247  * cartesian coordinates. So we need to convert. I think
248  * "manual" input should still be done this way -- it's a real
249  * pain if the computer isn't working! Manual mode is still confusing
250  * because it involves giving x and y motions, yet the coordinates
251  * are always displayed y - x, where +y is downward!
252  */
253
254 static void getcd(bool isprobe, int akey)
255 /* get course and distance */
256 {
257     int irowq=game.quadrant.x, icolq=game.quadrant.y, key=0;
258     double xi, xj, xk, xl;
259     double deltax, deltay;
260     enum {unspecified, manual, automatic} navmode = unspecified;
261     enum {curt, normal, verbose} itemp = curt;
262     coord incr;
263     bool iprompt = false;
264
265     /* Get course direction and distance. If user types bad values, return
266        with DIREC = -1.0. */
267
268     game.direc = -1.0;
269         
270     if (game.landed == 1 && !isprobe) {
271         prout(_("Dummy! You can't leave standard orbit until you"));
272         proutn(_("are back aboard the "));
273         crmshp();
274         prout(".");
275         chew();
276         return;
277     }
278     while (navmode == unspecified) {
279         if (damaged(DCOMPTR)) {
280             if (isprobe)
281                 prout(_("Computer damaged; manual navigation only"));
282             else
283                 prout(_("Computer damaged; manual movement only"));
284             chew();
285             navmode = manual;
286             key = IHEOL;
287             break;
288         }
289         if (isprobe && akey != -1) {
290             /* For probe launch, use pre-scanned value first time */
291             key = akey;
292             akey = -1;
293         }
294         else 
295             key = scan();
296
297         if (key == IHEOL) {
298             proutn(_("Manual or automatic- "));
299             iprompt = true;
300             chew();
301         }
302         else if (key == IHALPHA) {
303             if (isit("manual")) {
304                 navmode = manual;
305                 key = scan();
306                 break;
307             }
308             else if (isit("automatic")) {
309                 navmode = automatic;
310                 key = scan();
311                 break;
312             }
313             else {
314                 huh();
315                 chew();
316                 return;
317             }
318         }
319         else { /* numeric */
320             if (isprobe)
321                 prout(_("(Manual navigation assumed.)"));
322             else
323                 prout(_("(Manual movement assumed.)"));
324             navmode = automatic;
325             break;
326         }
327     }
328
329     if (navmode == automatic) {
330         while (key == IHEOL) {
331             if (isprobe)
332                 proutn(_("Target quadrant or quadrant&sector- "));
333             else
334                 proutn(_("Destination sector or quadrant&sector- "));
335             chew();
336             iprompt = true;
337             key = scan();
338         }
339
340         if (key != IHREAL) {
341             huh();
342             return;
343         }
344         xi = aaitem;
345         key = scan();
346         if (key != IHREAL){
347             huh();
348             return;
349         }
350         xj = aaitem;
351         key = scan();
352         if (key == IHREAL) {
353             /* both quadrant and sector specified */
354             xk = aaitem;
355             key = scan();
356             if (key != IHREAL) {
357                 huh();
358                 return;
359             }
360             xl = aaitem;
361
362             irowq = xi + 0.5;
363             icolq = xj + 0.5;
364             incr.y = xk + 0.5;
365             incr.x = xl + 0.5;
366         }
367         else {
368             if (isprobe) {
369                 /* only quadrant specified -- go to center of dest quad */
370                 irowq = xi + 0.5;
371                 icolq = xj + 0.5;
372                 incr.y = incr.x = 5;
373             }
374             else {
375                 incr.y = xi + 0.5;
376                 incr.x = xj + 0.5;
377             }
378             itemp = normal;
379         }
380         if (!VALID_QUADRANT(icolq,irowq)||!VALID_SECTOR(incr.x,incr.y)) {
381             huh();
382             return;
383         }
384         skip(1);
385         if (!isprobe) {
386             if (itemp > curt) {
387                 if (iprompt) {
388                     prout(_("Helmsman Sulu- \"Course locked in for %s.\""),
389                           cramlc(sector, incr));
390                 }
391             }
392             else prout(_("Ensign Chekov- \"Course laid in, Captain.\""));
393         }
394         deltax = icolq - game.quadrant.y + 0.1*(incr.x-game.sector.y);
395         deltay = game.quadrant.x - irowq + 0.1*(game.sector.x-incr.y);
396     }
397     else { /* manual */
398         while (key == IHEOL) {
399             proutn(_("X and Y displacements- "));
400             chew();
401             iprompt = true;
402             key = scan();
403         }
404         itemp = verbose;
405         if (key != IHREAL) {
406             huh();
407             return;
408         }
409         deltax = aaitem;
410         key = scan();
411         if (key != IHREAL) {
412             huh();
413             return;
414         }
415         deltay = aaitem;
416     }
417     /* Check for zero movement */
418     if (deltax == 0 && deltay == 0) {
419         chew();
420         return;
421     }
422     if (itemp == verbose && !isprobe) {
423         skip(1);
424         prout(_("Helmsman Sulu- \"Aye, Sir.\""));
425     }
426     game.dist = sqrt(deltax*deltax + deltay*deltay);
427     game.direc = atan2(deltax, deltay)*1.90985932;
428     if (game.direc < 0.0) game.direc += 12.0;
429     chew();
430     return;
431 }
432                 
433
434
435 void impuls(void) 
436 /* move under impulse power */
437 {
438     double power;
439
440     game.ididit = false;
441     if (damaged(DIMPULS)) {
442         chew();
443         skip(1);
444         prout(_("Engineer Scott- \"The impulse engines are damaged, Sir.\""));
445         return;
446     }
447
448     if (game.energy > 30.0) {
449         getcd(false, 0);
450         if (game.direc == -1.0) return;
451         power = 20.0 + 100.0*game.dist;
452     }
453     else
454         power = 30.0;
455
456     if (power >= game.energy) {
457         /* Insufficient power for trip */
458         skip(1);
459         prout(_("First Officer Spock- \"Captain, the impulse engines"));
460         prout(_("require 20.0 units to engage, plus 100.0 units per"));
461         if (game.energy > 30) {
462             proutn(_("quadrant.  We can go, therefore, a maximum of %d"),
463                    (int)(0.01 * (game.energy-20.0)-0.05));
464             prout(_(" quadrants.\""));
465         }
466         else {
467             prout(_("quadrant.  They are, therefore, useless.\""));
468         }
469         chew();
470         return;
471     }
472     /* Make sure enough time is left for the trip */
473     game.optime = game.dist/0.095;
474     if (game.optime >= game.state.remtime) {
475         prout(_("First Officer Spock- \"Captain, our speed under impulse"));
476         prout(_("power is only 0.95 sectors per stardate. Are you sure"));
477         proutn(_("we dare spend the time?\" "));
478         if (ja() == false) return;
479     }
480     /* Activate impulse engines and pay the cost */
481     imove();
482     game.ididit = true;
483     if (game.alldone) return;
484     power = 20.0 + 100.0*game.dist;
485     game.energy -= power;
486     game.optime = game.dist/0.095;
487     if (game.energy <= 0) finish(FNRG);
488     return;
489 }
490
491
492 void warp(bool timewarp)
493 /* move under warp drive */
494 {
495     int iwarp;
496     bool blooey = false, twarp = false;
497     double power;
498
499     if (!timewarp) { /* Not WARPX entry */
500         game.ididit = false;
501         if (game.damage[DWARPEN] > 10.0) {
502             chew();
503             skip(1);
504             prout(_("Engineer Scott- \"The impulse engines are damaged, Sir.\""));
505             return;
506         }
507         if (damaged(DWARPEN) && game.warpfac > 4.0) {
508             chew();
509             skip(1);
510             prout(_("Engineer Scott- \"Sorry, Captain. Until this damage"));
511             prout(_("  is repaired, I can only give you warp 4.\""));
512             return;
513         }
514                         
515         /* Read in course and distance */
516         getcd(false, 0);
517         if (game.direc == -1.0) return;
518
519         /* Make sure starship has enough energy for the trip */
520         power = (game.dist+0.05)*game.warpfac*game.warpfac*game.warpfac*(game.shldup+1);
521
522
523         if (power >= game.energy) {
524             /* Insufficient power for trip */
525             game.ididit = false;
526             skip(1);
527             prout(_("Engineering to bridge--"));
528             if (!game.shldup || 0.5*power > game.energy) {
529                 iwarp = pow((game.energy/(game.dist+0.05)), 0.333333333);
530                 if (iwarp <= 0) {
531                     prout(_("We can't do it, Captain. We don't have enough energy."));
532                 }
533                 else {
534                     proutn(_("We don't have enough energy, but we could do it at warp %d"), iwarp);
535                     if (game.shldup) {
536                         prout(",");
537                         prout(_("if you'll lower the shields."));
538                     }
539                     else
540                         prout(".");
541                 }
542             }
543             else
544                 prout(_("We haven't the energy to go that far with the shields up."));
545             return;
546         }
547                                                 
548         /* Make sure enough time is left for the trip */
549         game.optime = 10.0*game.dist/game.wfacsq;
550         if (game.optime >= 0.8*game.state.remtime) {
551             skip(1);
552             prout(_("First Officer Spock- \"Captain, I compute that such"));
553             proutn(_("  a trip would require approximately %2.0f"),
554                    100.0*game.optime/game.state.remtime);
555             prout(_(" percent of our"));
556             proutn(_("  remaining time.  Are you sure this is wise?\" "));
557             if (ja() == false) { game.ididit = false; game.optime=0; return;}
558         }
559     }
560     /* Entry WARPX */
561     if (game.warpfac > 6.0) {
562         /* Decide if engine damage will occur */
563         double prob = game.dist*(6.0-game.warpfac)*(6.0-game.warpfac)/66.666666666;
564         if (prob > Rand()) {
565             blooey = true;
566             game.dist = Rand()*game.dist;
567         }
568         /* Decide if time warp will occur */
569         if (0.5*game.dist*pow(7.0,game.warpfac-10.0) > Rand()) twarp = true;
570         if (idebug && game.warpfac==10 && !twarp) {
571             blooey = false;
572             proutn("=== Force time warp? ");
573             if (ja() == true) twarp = true;
574         }
575         if (blooey || twarp) {
576             /* If time warp or engine damage, check path */
577             /* If it is obstructed, don't do warp or damage */
578             double angle = ((15.0-game.direc)*0.5235998);
579             double deltax = -sin(angle);
580             double deltay = cos(angle);
581             double bigger, x, y;
582             int n, l, ix, iy;
583             if (fabs(deltax) > fabs(deltay))
584                 bigger = fabs(deltax);
585             else
586                 bigger = fabs(deltay);
587                         
588             deltax /= bigger;
589             deltay /= bigger;
590             n = 10.0 * game.dist * bigger +0.5;
591             x = game.sector.x;
592             y = game.sector.y;
593             for (l = 1; l <= n; l++) {
594                 x += deltax;
595                 ix = x + 0.5;
596                 y += deltay;
597                 iy = y +0.5;
598                 if (!VALID_SECTOR(ix, iy)) break;
599                 if (game.quad[ix][iy] != IHDOT) {
600                     blooey = false;
601                     twarp = false;
602                 }
603             }
604         }
605     }
606                                 
607
608     /* Activate Warp Engines and pay the cost */
609     imove();
610     if (game.alldone) return;
611     game.energy -= game.dist*game.warpfac*game.warpfac*game.warpfac*(game.shldup+1);
612     if (game.energy <= 0) finish(FNRG);
613     game.optime = 10.0*game.dist/game.wfacsq;
614     if (twarp) timwrp();
615     if (blooey) {
616         game.damage[DWARPEN] = game.damfac*(3.0*Rand()+1.0);
617         skip(1);
618         prout(_("Engineering to bridge--"));
619         prout(_("  Scott here.  The warp engines are damaged."));
620         prout(_("  We'll have to reduce speed to warp 4."));
621     }
622     game.ididit = true;
623     return;
624 }
625
626
627
628 void setwrp(void) 
629 /* change the warp factor */
630 {
631     int key;
632     double oldfac;
633         
634     while ((key=scan()) == IHEOL) {
635         chew();
636         proutn(_("Warp factor- "));
637     }
638     chew();
639     if (key != IHREAL) {
640         huh();
641         return;
642     }
643     if (game.damage[DWARPEN] > 10.0) {
644         prout(_("Warp engines inoperative."));
645         return;
646     }
647     if (damaged(DWARPEN) && aaitem > 4.0) {
648         prout(_("Engineer Scott- \"I'm doing my best, Captain,"));
649         prout(_("  but right now we can only go warp 4.\""));
650         return;
651     }
652     if (aaitem > 10.0) {
653         prout(_("Helmsman Sulu- \"Our top speed is warp 10, Captain.\""));
654         return;
655     }
656     if (aaitem < 1.0) {
657         prout(_("Helmsman Sulu- \"We can't go below warp 1, Captain.\""));
658         return;
659     }
660     oldfac = game.warpfac;
661     game.warpfac = aaitem;
662     game.wfacsq=game.warpfac*game.warpfac;
663     if (game.warpfac <= oldfac || game.warpfac <= 6.0) {
664         prout(_("Helmsman Sulu- \"Warp factor %d, Captain.\""),
665                (int)game.warpfac);
666         return;
667     }
668     if (game.warpfac < 8.00) {
669         prout(_("Engineer Scott- \"Aye, but our maximum safe speed is warp 6.\""));
670         return;
671     }
672     if (game.warpfac == 10.0) {
673         prout(_("Engineer Scott- \"Aye, Captain, we'll try it.\""));
674         return;
675     }
676     prout(_("Engineer Scott- \"Aye, Captain, but our engines may not take it.\""));
677     return;
678 }
679
680 void atover(bool igrab) 
681 /* cope with being tossed out of quadrant by supernova or yanked by beam */
682 {
683     double power, distreq;
684
685     chew();
686     /* is captain on planet? */
687     if (game.landed==1) {
688         if (damaged(DTRANSP)) {
689             finish(FPNOVA);
690             return;
691         }
692         prout(_("Scotty rushes to the transporter controls."));
693         if (game.shldup) {
694             prout(_("But with the shields up it's hopeless."));
695             finish(FPNOVA);
696         }
697         prouts(_("His desperate attempt to rescue you . . ."));
698         if (Rand() <= 0.5) {
699             prout(_("fails."));
700             finish(FPNOVA);
701             return;
702         }
703         prout(_("SUCCEEDS!"));
704         if (game.imine) {
705             game.imine = false;
706             proutn(_("The crystals mined were "));
707             if (Rand() <= 0.25) {
708                 prout(_("lost."));
709             }
710             else {
711                 prout(_("saved."));
712                 game.icrystl = true;
713             }
714         }
715     }
716     if (igrab) return;
717
718     /* Check to see if captain in shuttle craft */
719     if (game.icraft) finish(FSTRACTOR);
720     if (game.alldone) return;
721
722     /* Inform captain of attempt to reach safety */
723     skip(1);
724     do {
725         if (game.justin) {
726             prouts(_("***RED ALERT!  RED ALERT!"));
727             skip(1);
728             proutn(_("The "));
729             crmshp();
730             prout(_(" has stopped in a quadrant containing"));
731             prouts(_("   a supernova."));
732             skip(2);
733         }
734         proutn(_("***Emergency automatic override attempts to hurl "));
735         crmshp();
736         skip(1);
737         prout(_("safely out of quadrant."));
738         if (!damaged(DRADIO))
739             game.state.galaxy[game.quadrant.x][game.quadrant.y].charted = true;
740         /* Try to use warp engines */
741         if (damaged(DWARPEN)) {
742             skip(1);
743             prout(_("Warp engines damaged."));
744             finish(FSNOVAED);
745             return;
746         }
747         game.warpfac = 6.0+2.0*Rand();
748         game.wfacsq = game.warpfac * game.warpfac;
749         prout(_("Warp factor set to %d"), (int)game.warpfac);
750         power = 0.75*game.energy;
751         game.dist = power/(game.warpfac*game.warpfac*game.warpfac*(game.shldup+1));
752         distreq = 1.4142+Rand();
753         if (distreq < game.dist) game.dist = distreq;
754         game.optime = 10.0*game.dist/game.wfacsq;
755         game.direc = 12.0*Rand();       /* How dumb! */
756         game.justin = false;
757         game.inorbit = false;
758         warp(true);
759         if (!game.justin) {
760             /* This is bad news, we didn't leave quadrant. */
761             if (game.alldone) return;
762             skip(1);
763             prout(_("Insufficient energy to leave quadrant."));
764             finish(FSNOVAED);
765             return;
766         }
767     } while 
768         /* Repeat if another snova */
769         (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova);
770     if (KLINGREM==0) 
771         finish(FWON); /* Snova killed remaining enemy. */
772 }
773
774 void timwrp() 
775 /* let's do the time warp again */
776 {
777     int l, 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 = 0;
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 = 0;
802         for (l = 0; l < game.inplan; l++) {
803             if (game.state.plnets[l].known == shuttle_down) {
804                 gotit = 1;
805                 if (game.iscraft==1 && game.ship==IHE) {
806                     prout(_("Checkov-  \"Security reports the Galileo has disappeared, Sir!"));
807                     game.iscraft = 0;
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==0 && gotit==0 && game.damage[DSHUTTL] >= 0.0) {
814             prout(_("Checkov-  \"Security reports the Galileo has reappeared in the dock!\""));
815             game.iscraft = 1;
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.condit != IHDOCKED) {
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 void mayday(void) 
909 /* yell for help from nearest starbase */
910 {
911     /* There's more than one way to move in this game! */
912     double ddist, xdist, probf;
913     int line = 0, m, ix, iy;
914
915     chew();
916     /* Test for game.conditions which prevent calling for help */
917     if (game.condit == IHDOCKED) {
918         prout(_("Lt. Uhura-  \"But Captain, we're already docked.\""));
919         return;
920     }
921     if (damaged(DRADIO)) {
922         prout(_("Subspace radio damaged."));
923         return;
924     }
925     if (game.state.rembase==0) {
926         prout(_("Lt. Uhura-  \"Captain, I'm not getting any response from Starbase.\""));
927         return;
928     }
929     if (game.landed == 1) {
930         proutn(_("You must be aboard the "));
931         crmshp();
932         prout(".");
933         return;
934     }
935     /* OK -- call for help from nearest starbase */
936     game.nhelp++;
937     if (game.base.x!=0) {
938         /* There's one in this quadrant */
939         ddist = distance(game.base, game.sector);
940     }
941     else {
942         ddist = FOREVER;
943         for_starbases(m) {
944             xdist = QUADSIZE * distance(game.state.baseq[m], game.quadrant);
945             if (xdist < ddist) {
946                 ddist = xdist;
947                 line = m;
948             }
949         }
950         /* Since starbase not in quadrant, set up new quadrant */
951         game.quadrant = game.state.baseq[line];
952         newqad(true);
953     }
954     /* dematerialize starship */
955     game.quad[game.sector.x][game.sector.y]=IHDOT;
956     proutn(_("Starbase in %s responds--"), cramlc(quadrant, game.quadrant));
957     proutn("");
958     crmshp();
959     prout(_(" dematerializes."));
960     game.sector.x=0;
961     for (m = 1; m <= 5; m++) {
962         ix = game.base.x+3.0*Rand()-1;
963         iy = game.base.y+3.0*Rand()-1;
964         if (VALID_SECTOR(ix,iy) && game.quad[ix][iy]==IHDOT) {
965             /* found one -- finish up */
966             game.sector.x=ix;
967             game.sector.y=iy;
968             break;
969         }
970     }
971     if (game.sector.x==0){
972         prout(_("You have been lost in space..."));
973         finish(FMATERIALIZE);
974         return;
975     }
976     /* Give starbase three chances to rematerialize starship */
977     probf = pow((1.0 - pow(0.98,ddist)), 0.33333333);
978     for (m = 1; m <= 3; m++) {
979         switch (m) {
980         case 1: proutn(_("1st")); break;
981         case 2: proutn(_("2nd")); break;
982         case 3: proutn(_("3rd")); break;
983         }
984         proutn(_(" attempt to re-materialize "));
985         crmshp();
986         switch (m){
987         case 1: game.quad[ix][iy]=IHMATER0;
988             break;
989         case 2: game.quad[ix][iy]=IHMATER1;
990             break;
991         case 3: game.quad[ix][iy]=IHMATER2;
992             break;
993         }
994         textcolor(RED);
995         warble();
996         if (Rand() > probf) break;
997         prout(_("fails."));
998         delay(500);
999         textcolor(DEFAULT);
1000     }
1001     if (m > 3) {
1002         game.quad[ix][iy]=IHQUEST;
1003         game.alive = 0;
1004         drawmaps(1);
1005         setwnd(message_window);
1006         finish(FMATERIALIZE);
1007         return;
1008     }
1009     game.quad[ix][iy]=game.ship;
1010     textcolor(GREEN);
1011     prout(_("succeeds."));
1012     textcolor(DEFAULT);
1013     dock(false);
1014     skip(1);
1015     prout(_("Lt. Uhura-  \"Captain, we made it!\""));
1016 }