8495cd0d1063b2576e1bc4905891327e038ba130
[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(bool novapush)
8 /* movement execution for warp, impulse, 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 && !novapush) {
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(false);
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(false);
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)
201             attack(false);
202         for_local_enemies(m) game.kavgd[m] = game.kdist[m];
203     }
204     newcnd();
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.condition == docked && 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 (!is_valid(game.base) || 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.condition = docked;
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) {
237         /* get attack report from base */
238         prout(_("Lt. Uhura- \"Captain, an important message from the starbase:\""));
239         attakreport(false);
240         game.iseenit = true;
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 && !isprobe) {
271         prout(_("Dummy! You can't leave standard orbit until you"));
272         proutn(_("are back aboard the ship."));
273         chew();
274         return;
275     }
276     while (navmode == unspecified) {
277         if (damaged(DNAVSYS)) {
278             if (isprobe)
279                 prout(_("Computer damaged; manual navigation only"));
280             else
281                 prout(_("Computer damaged; manual movement only"));
282             chew();
283             navmode = manual;
284             key = IHEOL;
285             break;
286         }
287         if (isprobe && akey != -1) {
288             /* For probe launch, use pre-scanned value first time */
289             key = akey;
290             akey = -1;
291         }
292         else 
293             key = scan();
294
295         if (key == IHEOL) {
296             proutn(_("Manual or automatic- "));
297             iprompt = true;
298             chew();
299         }
300         else if (key == IHALPHA) {
301             if (isit("manual")) {
302                 navmode = manual;
303                 key = scan();
304                 break;
305             }
306             else if (isit("automatic")) {
307                 navmode = automatic;
308                 key = scan();
309                 break;
310             }
311             else {
312                 huh();
313                 chew();
314                 return;
315             }
316         }
317         else { /* numeric */
318             if (isprobe)
319                 prout(_("(Manual navigation assumed.)"));
320             else
321                 prout(_("(Manual movement assumed.)"));
322             navmode = automatic;
323             break;
324         }
325     }
326
327     if (navmode == automatic) {
328         while (key == IHEOL) {
329             if (isprobe)
330                 proutn(_("Target quadrant or quadrant&sector- "));
331             else
332                 proutn(_("Destination sector or quadrant&sector- "));
333             chew();
334             iprompt = true;
335             key = scan();
336         }
337
338         if (key != IHREAL) {
339             huh();
340             return;
341         }
342         xi = aaitem;
343         key = scan();
344         if (key != IHREAL){
345             huh();
346             return;
347         }
348         xj = aaitem;
349         key = scan();
350         if (key == IHREAL) {
351             /* both quadrant and sector specified */
352             xk = aaitem;
353             key = scan();
354             if (key != IHREAL) {
355                 huh();
356                 return;
357             }
358             xl = aaitem;
359
360             irowq = xi + 0.5;
361             icolq = xj + 0.5;
362             incr.y = xk + 0.5;
363             incr.x = xl + 0.5;
364         }
365         else {
366             if (isprobe) {
367                 /* only quadrant specified -- go to center of dest quad */
368                 irowq = xi + 0.5;
369                 icolq = xj + 0.5;
370                 incr.y = incr.x = 5;
371             }
372             else {
373                 incr.y = xi + 0.5;
374                 incr.x = xj + 0.5;
375             }
376             itemp = normal;
377         }
378         if (!VALID_QUADRANT(icolq,irowq)||!VALID_SECTOR(incr.x,incr.y)) {
379             huh();
380             return;
381         }
382         skip(1);
383         if (!isprobe) {
384             if (itemp > curt) {
385                 if (iprompt) {
386                     prout(_("Helmsman Sulu- \"Course locked in for %s.\""),
387                           cramlc(sector, incr));
388                 }
389             }
390             else prout(_("Ensign Chekov- \"Course laid in, Captain.\""));
391         }
392         deltax = icolq - game.quadrant.y + 0.1*(incr.x-game.sector.y);
393         deltay = game.quadrant.x - irowq + 0.1*(game.sector.x-incr.y);
394     }
395     else { /* manual */
396         while (key == IHEOL) {
397             proutn(_("X and Y displacements- "));
398             chew();
399             iprompt = true;
400             key = scan();
401         }
402         itemp = verbose;
403         if (key != IHREAL) {
404             huh();
405             return;
406         }
407         deltax = aaitem;
408         key = scan();
409         if (key != IHREAL) {
410             huh();
411             return;
412         }
413         deltay = aaitem;
414     }
415     /* Check for zero movement */
416     if (deltax == 0 && deltay == 0) {
417         chew();
418         return;
419     }
420     if (itemp == verbose && !isprobe) {
421         skip(1);
422         prout(_("Helmsman Sulu- \"Aye, Sir.\""));
423     }
424     game.dist = sqrt(deltax*deltax + deltay*deltay);
425     game.direc = atan2(deltax, deltay)*1.90985932;
426     if (game.direc < 0.0) game.direc += 12.0;
427     chew();
428     return;
429 }
430                 
431
432
433 void impuls(void) 
434 /* move under impulse power */
435 {
436     double power;
437
438     game.ididit = false;
439     if (damaged(DIMPULS)) {
440         chew();
441         skip(1);
442         prout(_("Engineer Scott- \"The impulse engines are damaged, Sir.\""));
443         return;
444     }
445
446     if (game.energy > 30.0) {
447         getcd(false, 0);
448         if (game.direc == -1.0) return;
449         power = 20.0 + 100.0*game.dist;
450     }
451     else
452         power = 30.0;
453
454     if (power >= game.energy) {
455         /* Insufficient power for trip */
456         skip(1);
457         prout(_("First Officer Spock- \"Captain, the impulse engines"));
458         prout(_("require 20.0 units to engage, plus 100.0 units per"));
459         if (game.energy > 30) {
460             proutn(_("quadrant.  We can go, therefore, a maximum of %d"),
461                    (int)(0.01 * (game.energy-20.0)-0.05));
462             prout(_(" quadrants.\""));
463         }
464         else {
465             prout(_("quadrant.  They are, therefore, useless.\""));
466         }
467         chew();
468         return;
469     }
470     /* Make sure enough time is left for the trip */
471     game.optime = game.dist/0.095;
472     if (game.optime >= game.state.remtime) {
473         prout(_("First Officer Spock- \"Captain, our speed under impulse"));
474         prout(_("power is only 0.95 sectors per stardate. Are you sure"));
475         proutn(_("we dare spend the time?\" "));
476         if (ja() == false) return;
477     }
478     /* Activate impulse engines and pay the cost */
479     imove(false);
480     game.ididit = true;
481     if (game.alldone) return;
482     power = 20.0 + 100.0*game.dist;
483     game.energy -= power;
484     game.optime = game.dist/0.095;
485     if (game.energy <= 0) finish(FNRG);
486     return;
487 }
488
489
490 void warp(bool timewarp)
491 /* move under warp drive */
492 {
493     int iwarp;
494     bool blooey = false, twarp = false;
495     double power;
496
497     if (!timewarp) { /* Not WARPX entry */
498         game.ididit = false;
499         if (game.damage[DWARPEN] > 10.0) {
500             chew();
501             skip(1);
502             prout(_("Engineer Scott- \"The impulse engines are damaged, Sir.\""));
503             return;
504         }
505         if (damaged(DWARPEN) && game.warpfac > 4.0) {
506             chew();
507             skip(1);
508             prout(_("Engineer Scott- \"Sorry, Captain. Until this damage"));
509             prout(_("  is repaired, I can only give you warp 4.\""));
510             return;
511         }
512                         
513         /* Read in course and distance */
514         getcd(false, 0);
515         if (game.direc == -1.0) return;
516
517         /* Make sure starship has enough energy for the trip */
518         power = (game.dist+0.05)*game.warpfac*game.warpfac*game.warpfac*(game.shldup+1);
519
520
521         if (power >= game.energy) {
522             /* Insufficient power for trip */
523             game.ididit = false;
524             skip(1);
525             prout(_("Engineering to bridge--"));
526             if (!game.shldup || 0.5*power > game.energy) {
527                 iwarp = pow((game.energy/(game.dist+0.05)), 0.333333333);
528                 if (iwarp <= 0) {
529                     prout(_("We can't do it, Captain. We don't have enough energy."));
530                 }
531                 else {
532                     proutn(_("We don't have enough energy, but we could do it at warp %d"), iwarp);
533                     if (game.shldup) {
534                         prout(",");
535                         prout(_("if you'll lower the shields."));
536                     }
537                     else
538                         prout(".");
539                 }
540             }
541             else
542                 prout(_("We haven't the energy to go that far with the shields up."));
543             return;
544         }
545                                                 
546         /* Make sure enough time is left for the trip */
547         game.optime = 10.0*game.dist/game.wfacsq;
548         if (game.optime >= 0.8*game.state.remtime) {
549             skip(1);
550             prout(_("First Officer Spock- \"Captain, I compute that such"));
551             proutn(_("  a trip would require approximately %2.0f"),
552                    100.0*game.optime/game.state.remtime);
553             prout(_(" percent of our"));
554             proutn(_("  remaining time.  Are you sure this is wise?\" "));
555             if (ja() == false) { game.ididit = false; game.optime=0; return;}
556         }
557     }
558     /* Entry WARPX */
559     if (game.warpfac > 6.0) {
560         /* Decide if engine damage will occur */
561         double prob = game.dist*(6.0-game.warpfac)*(6.0-game.warpfac)/66.666666666;
562         if (prob > Rand()) {
563             blooey = true;
564             game.dist = Rand()*game.dist;
565         }
566         /* Decide if time warp will occur */
567         if (0.5*game.dist*pow(7.0,game.warpfac-10.0) > Rand()) twarp = true;
568         if (idebug && game.warpfac==10 && !twarp) {
569             blooey = false;
570             proutn("=== Force time warp? ");
571             if (ja() == true) twarp = true;
572         }
573         if (blooey || twarp) {
574             /* If time warp or engine damage, check path */
575             /* If it is obstructed, don't do warp or damage */
576             double angle = ((15.0-game.direc)*0.5235998);
577             double deltax = -sin(angle);
578             double deltay = cos(angle);
579             double bigger, x, y;
580             int n, l, ix, iy;
581             if (fabs(deltax) > fabs(deltay))
582                 bigger = fabs(deltax);
583             else
584                 bigger = fabs(deltay);
585                         
586             deltax /= bigger;
587             deltay /= bigger;
588             n = 10.0 * game.dist * bigger +0.5;
589             x = game.sector.x;
590             y = game.sector.y;
591             for (l = 1; l <= n; l++) {
592                 x += deltax;
593                 ix = x + 0.5;
594                 y += deltay;
595                 iy = y +0.5;
596                 if (!VALID_SECTOR(ix, iy)) break;
597                 if (game.quad[ix][iy] != IHDOT) {
598                     blooey = false;
599                     twarp = false;
600                 }
601             }
602         }
603     }
604                                 
605
606     /* Activate Warp Engines and pay the cost */
607     imove(false);
608     if (game.alldone) return;
609     game.energy -= game.dist*game.warpfac*game.warpfac*game.warpfac*(game.shldup+1);
610     if (game.energy <= 0) finish(FNRG);
611     game.optime = 10.0*game.dist/game.wfacsq;
612     if (twarp) timwrp();
613     if (blooey) {
614         game.damage[DWARPEN] = game.damfac*(3.0*Rand()+1.0);
615         skip(1);
616         prout(_("Engineering to bridge--"));
617         prout(_("  Scott here.  The warp engines are damaged."));
618         prout(_("  We'll have to reduce speed to warp 4."));
619     }
620     game.ididit = true;
621     return;
622 }
623
624
625
626 void setwrp(void) 
627 /* change the warp factor */
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         prout(_("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(bool igrab) 
679 /* cope with being tossed out of quadrant by supernova or yanked by beam */
680 {
681     double power, distreq;
682
683     chew();
684     /* is captain on planet? */
685     if (game.landed) {
686         if (damaged(DTRANSP)) {
687             finish(FPNOVA);
688             return;
689         }
690         prout(_("Scotty rushes to the transporter controls."));
691         if (game.shldup) {
692             prout(_("But with the shields up it's hopeless."));
693             finish(FPNOVA);
694         }
695         prouts(_("His desperate attempt to rescue you . . ."));
696         if (Rand() <= 0.5) {
697             prout(_("fails."));
698             finish(FPNOVA);
699             return;
700         }
701         prout(_("SUCCEEDS!"));
702         if (game.imine) {
703             game.imine = false;
704             proutn(_("The crystals mined were "));
705             if (Rand() <= 0.25) {
706                 prout(_("lost."));
707             }
708             else {
709                 prout(_("saved."));
710                 game.icrystl = true;
711             }
712         }
713     }
714     if (igrab) return;
715
716     /* Check to see if captain in shuttle craft */
717     if (game.icraft) finish(FSTRACTOR);
718     if (game.alldone) return;
719
720     /* Inform captain of attempt to reach safety */
721     skip(1);
722     do {
723         if (game.justin) {
724             prouts(_("***RED ALERT!  RED ALERT!"));
725             skip(1);
726             proutn(_("The "));
727             crmshp();
728             prout(_(" has stopped in a quadrant containing"));
729             prouts(_("   a supernova."));
730             skip(2);
731         }
732         proutn(_("***Emergency automatic override attempts to hurl "));
733         crmshp();
734         skip(1);
735         prout(_("safely out of quadrant."));
736         if (!damaged(DRADIO))
737             game.state.galaxy[game.quadrant.x][game.quadrant.y].charted = true;
738         /* Try to use warp engines */
739         if (damaged(DWARPEN)) {
740             skip(1);
741             prout(_("Warp engines damaged."));
742             finish(FSNOVAED);
743             return;
744         }
745         game.warpfac = 6.0+2.0*Rand();
746         game.wfacsq = game.warpfac * game.warpfac;
747         prout(_("Warp factor set to %d"), (int)game.warpfac);
748         power = 0.75*game.energy;
749         game.dist = power/(game.warpfac*game.warpfac*game.warpfac*(game.shldup+1));
750         distreq = 1.4142+Rand();
751         if (distreq < game.dist) game.dist = distreq;
752         game.optime = 10.0*game.dist/game.wfacsq;
753         game.direc = 12.0*Rand();       /* How dumb! */
754         game.justin = false;
755         game.inorbit = false;
756         warp(true);
757         if (!game.justin) {
758             /* This is bad news, we didn't leave quadrant. */
759             if (game.alldone) return;
760             skip(1);
761             prout(_("Insufficient energy to leave quadrant."));
762             finish(FSNOVAED);
763             return;
764         }
765     } while 
766         /* Repeat if another snova */
767         (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova);
768     if (KLINGREM==0) 
769         finish(FWON); /* Snova killed remaining enemy. */
770 }
771
772 void timwrp() 
773 /* let's do the time warp again */
774 {
775     int l;
776     bool gotit;
777     prout(_("***TIME WARP ENTERED."));
778     if (game.state.snap && Rand() < 0.5) {
779         /* Go back in time */
780         prout(_("You are traveling backwards in time %d stardates."),
781               (int)(game.state.date-game.snapsht.date));
782         game.state = game.snapsht;
783         game.state.snap = false;
784         if (game.state.remcom) {
785             schedule(FTBEAM, expran(game.intime/game.state.remcom));
786             schedule(FBATTAK, expran(0.3*game.intime));
787         }
788         schedule(FSNOVA, expran(0.5*game.intime));
789         /* next snapshot will be sooner */
790         schedule(FSNAP, expran(0.25*game.state.remtime));
791                                 
792         if (game.state.nscrem) schedule(FSCMOVE, 0.2777);
793         game.isatb = 0;
794         unschedule(FCDBAS);
795         unschedule(FSCDBAS);
796         invalidate(game.battle);
797
798         /* Make sure Galileo is consistant -- Snapshot may have been taken
799            when on planet, which would give us two Galileos! */
800         gotit = false;
801         for (l = 0; l < game.inplan; l++) {
802             if (game.state.plnets[l].known == shuttle_down) {
803                 gotit = true;
804                 if (game.iscraft == onship && game.ship==IHE) {
805                     prout(_("Checkov-  \"Security reports the Galileo has disappeared, Sir!"));
806                     game.iscraft = offship;
807                 }
808             }
809         }
810         /* Likewise, if in the original time the Galileo was abandoned, but
811            was on ship earlier, it would have vanished -- lets restore it */
812         if (game.iscraft == offship && !gotit && game.damage[DSHUTTL] >= 0.0) {
813             prout(_("Checkov-  \"Security reports the Galileo has reappeared in the dock!\""));
814             game.iscraft = onship;
815         }
816         /* 
817          * There used to be code to do the actual reconstrction here,
818          * but the starchart is now part of the snapshotted galaxy state.
819          */
820         prout(_("Spock has reconstructed a correct star chart from memory"));
821     }
822     else {
823         /* Go forward in time */
824         game.optime = -0.5*game.intime*log(Rand());
825         prout(_("You are traveling forward in time %d stardates."), (int)game.optime);
826         /* cheat to make sure no tractor beams occur during time warp */
827         postpone(FTBEAM, game.optime);
828         game.damage[DRADIO] += game.optime;
829     }
830     newqad(false);
831     events();   /* Stas Sergeev added this -- do pending events */
832 }
833
834 void probe(void) 
835 /* launch deep-space probe */
836 {
837     double angle, bigger;
838     int key;
839     /* New code to launch a deep space probe */
840     if (game.nprobes == 0) {
841         chew();
842         skip(1);
843         if (game.ship == IHE) 
844             prout(_("Engineer Scott- \"We have no more deep space probes, Sir.\""));
845         else
846             prout(_("Ye Faerie Queene has no deep space probes."));
847         return;
848     }
849     if (damaged(DDSP)) {
850         chew();
851         skip(1);
852         prout(_("Engineer Scott- \"The probe launcher is damaged, Sir.\""));
853         return;
854     }
855     if (is_scheduled(FDSPROB)) {
856         chew();
857         skip(1);
858         if (damaged(DRADIO) && game.condition != docked) {
859             prout(_("Spock-  \"Records show the previous probe has not yet"));
860             prout(_("   reached its destination.\""));
861         }
862         else
863             prout(_("Uhura- \"The previous probe is still reporting data, Sir.\""));
864         return;
865     }
866     key = scan();
867
868     if (key == IHEOL) {
869         /* slow mode, so let Kirk know how many probes there are left */
870         prout(game.nprobes==1 ? _("%d probe left.") : _("%d probes left."), game.nprobes);
871         proutn(_("Are you sure you want to fire a probe? "));
872         if (ja() == false) return;
873     }
874
875     game.isarmed = false;
876     if (key == IHALPHA && strcmp(citem,"armed") == 0) {
877         game.isarmed = true;
878         key = scan();
879     }
880     else if (key == IHEOL) {
881         proutn(_("Arm NOVAMAX warhead? "));
882         game.isarmed = ja();
883     }
884     getcd(true, key);
885     if (game.direc == -1.0) return;
886     game.nprobes--;
887     angle = ((15.0 - game.direc) * 0.5235988);
888     game.probeinx = -sin(angle);
889     game.probeiny = cos(angle);
890     if (fabs(game.probeinx) > fabs(game.probeiny))
891         bigger = fabs(game.probeinx);
892     else
893         bigger = fabs(game.probeiny);
894                 
895     game.probeiny /= bigger;
896     game.probeinx /= bigger;
897     game.proben = 10.0*game.dist*bigger +0.5;
898     game.probex = game.quadrant.x*QUADSIZE + game.sector.x - 1; // We will use better packing than original
899     game.probey = game.quadrant.y*QUADSIZE + game.sector.y - 1;
900     game.probec = game.quadrant;
901     schedule(FDSPROB, 0.01); // Time to move one sector
902     prout(_("Ensign Chekov-  \"The deep space probe is launched, Captain.\""));
903     game.ididit = true;
904     return;
905 }
906
907 /*
908  *      Here's how the mayday code works:
909  *
910  *      First, the closest starbase is selected.  If there is a
911  *      a starbase in your own quadrant, you are in good shape.
912  *      This distance takes quadrant distances into account only.
913  *
914  *      A magic number is computed based on the distance which acts
915  *      as the probability that you will be rematerialized.  You
916  *      get three tries.
917  *
918  *      When it is determined that you should be able to be remater-
919  *      ialized (i.e., when the probability thing mentioned above
920  *      comes up positive), you are put into that quadrant (anywhere).
921  *      Then, we try to see if there is a spot adjacent to the star-
922  *      base.  If not, you can't be rematerialized!!!  Otherwise,
923  *      it drops you there.  It only tries five times to find a spot
924  *      to drop you.  After that, it's your problem.
925  */
926
927 void mayday(void) 
928 /* yell for help from nearest starbase */
929 {
930     /* There's more than one way to move in this game! */
931     double ddist, xdist, probf;
932     int line = 0, m, ix, iy;
933
934     chew();
935     /* Test for conditions which prevent calling for help */
936     if (game.condition == docked) {
937         prout(_("Lt. Uhura-  \"But Captain, we're already docked.\""));
938         return;
939     }
940     if (damaged(DRADIO)) {
941         prout(_("Subspace radio damaged."));
942         return;
943     }
944     if (game.state.rembase==0) {
945         prout(_("Lt. Uhura-  \"Captain, I'm not getting any response from Starbase.\""));
946         return;
947     }
948     if (game.landed) {
949         proutn(_("You must be aboard the "));
950         crmshp();
951         prout(".");
952         return;
953     }
954     /* OK -- call for help from nearest starbase */
955     game.nhelp++;
956     if (game.base.x!=0) {
957         /* There's one in this quadrant */
958         ddist = distance(game.base, game.sector);
959     }
960     else {
961         ddist = FOREVER;
962         for_starbases(m) {
963             xdist = QUADSIZE * distance(game.state.baseq[m], game.quadrant);
964             if (xdist < ddist) {
965                 ddist = xdist;
966                 line = m;
967             }
968         }
969         /* Since starbase not in quadrant, set up new quadrant */
970         game.quadrant = game.state.baseq[line];
971         newqad(true);
972     }
973     /* dematerialize starship */
974     game.quad[game.sector.x][game.sector.y]=IHDOT;
975     proutn(_("Starbase in %s responds--"), cramlc(quadrant, game.quadrant));
976     crmshp();
977     prout(_(" dematerializes."));
978     game.sector.x=0;
979     for (m = 1; m <= 5; m++) {
980         ix = game.base.x+3.0*Rand()-1;
981         iy = game.base.y+3.0*Rand()-1;
982         if (VALID_SECTOR(ix,iy) && game.quad[ix][iy]==IHDOT) {
983             /* found one -- finish up */
984             game.sector.x=ix;
985             game.sector.y=iy;
986             break;
987         }
988     }
989     if (!is_valid(game.sector)){
990         prout(_("You have been lost in space..."));
991         finish(FMATERIALIZE);
992         return;
993     }
994     /* Give starbase three chances to rematerialize starship */
995     probf = pow((1.0 - pow(0.98,ddist)), 0.33333333);
996     for (m = 1; m <= 3; m++) {
997         switch (m) {
998         case 1: proutn(_("1st")); break;
999         case 2: proutn(_("2nd")); break;
1000         case 3: proutn(_("3rd")); break;
1001         }
1002         proutn(_(" attempt to re-materialize "));
1003         crmshp();
1004         switch (m){
1005         case 1: game.quad[ix][iy]=IHMATER0;
1006             break;
1007         case 2: game.quad[ix][iy]=IHMATER1;
1008             break;
1009         case 3: game.quad[ix][iy]=IHMATER2;
1010             break;
1011         }
1012         textcolor(RED);
1013         warble();
1014         if (Rand() > probf) break;
1015         prout(_("fails."));
1016         delay(500);
1017         textcolor(DEFAULT);
1018     }
1019     if (m > 3) {
1020         game.quad[ix][iy]=IHQUEST;
1021         game.alive = false;
1022         drawmaps(1);
1023         setwnd(message_window);
1024         finish(FMATERIALIZE);
1025         return;
1026     }
1027     game.quad[ix][iy]=game.ship;
1028     textcolor(GREEN);
1029     prout(_("succeeds."));
1030     textcolor(DEFAULT);
1031     dock(false);
1032     skip(1);
1033     prout(_("Lt. Uhura-  \"Captain, we made it!\""));
1034 }
1035
1036 /*
1037 **  Abandon Ship
1038 **
1039 **      The ship is abandoned.  If your current ship is the Faire
1040 **      Queene, or if your shuttlecraft is dead, you're out of
1041 **      luck.  You need the shuttlecraft in order for the captain
1042 **      (that's you!!) to escape.
1043 **
1044 **      Your crew can beam to an inhabited starsystem in the
1045 **      quadrant, if there is one and if the transporter is working.
1046 **      If there is no inhabited starsystem, or if the transporter
1047 **      is out, they are left to die in outer space.
1048 **
1049 **      If there are no starbases left, you are captured by the
1050 **      Klingons, who torture you mercilessly.  However, if there
1051 **      is at least one starbase, you are returned to the
1052 **      Federation in a prisoner of war exchange.  Of course, this
1053 **      can't happen unless you have taken some prisoners.
1054 **
1055 */
1056
1057 void abandn(void) 
1058 /* abandon ship */
1059 {
1060     int nb, l;
1061     struct quadrant *q;
1062
1063     chew();
1064     if (game.condition==docked) {
1065         if (game.ship!=IHE) {
1066             prout(_("You cannot abandon Ye Faerie Queene."));
1067             return;
1068         }
1069     }
1070     else {
1071         /* Must take shuttle craft to exit */
1072         if (game.damage[DSHUTTL]==-1) {
1073             prout(_("Ye Faerie Queene has no shuttle craft."));
1074             return;
1075         }
1076         if (game.damage[DSHUTTL]<0) {
1077             prout(_("Shuttle craft now serving Big Macs."));
1078             return;
1079         }
1080         if (game.damage[DSHUTTL]>0) {
1081             prout(_("Shuttle craft damaged."));
1082             return;
1083         }
1084         if (game.landed) {
1085             prout(_("You must be aboard the ship."));
1086             return;
1087         }
1088         if (game.iscraft != onship) {
1089             prout(_("Shuttle craft not currently available."));
1090             return;
1091         }
1092         /* Print abandon ship messages */
1093         skip(1);
1094         prouts(_("***ABANDON SHIP!  ABANDON SHIP!"));
1095         skip(1);
1096         prouts(_("***ALL HANDS ABANDON SHIP!"));
1097         skip(2);
1098         prout(_("Captain and crew escape in shuttle craft."));
1099         if (game.state.rembase==0) {
1100             /* Oops! no place to go... */
1101             finish(FABANDN);
1102             return;
1103         }
1104         q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
1105         /* Dispose of crew */
1106         if (!(game.options & OPTION_WORLDS) && !damaged(DTRANSP)) {
1107             prout(_("Remainder of ship's complement beam down"));
1108             prout(_("to nearest habitable planet."));
1109         } else if (q->planet != NOPLANET && !damaged(DTRANSP)) {
1110             prout(_("Remainder of ship's complement beam down"));
1111             prout(_("to %s."), systnames[q->planet]);
1112         } else {
1113             prout(_("Entire crew of %d left to die in outer space."),
1114                     game.state.crew);
1115             game.casual += game.state.crew;
1116             game.abandoned += game.state.crew;
1117         }
1118
1119         /* If at least one base left, give 'em the Faerie Queene */
1120         skip(1);
1121         game.icrystl = false; /* crystals are lost */
1122         game.nprobes = 0; /* No probes */
1123         prout(_("You are captured by Klingons and released to"));
1124         prout(_("the Federation in a prisoner-of-war exchange."));
1125         nb = Rand()*game.state.rembase+1;
1126         /* Set up quadrant and position FQ adjacient to base */
1127         if (!same(game.quadrant, game.state.baseq[nb])) {
1128             game.quadrant = game.state.baseq[nb];
1129             game.sector.x = game.sector.y = 5;
1130             newqad(true);
1131         }
1132         for (;;) {
1133             /* position next to base by trial and error */
1134             game.quad[game.sector.x][game.sector.y] = IHDOT;
1135             for_sectors(l) {
1136                 game.sector.x = 3.0*Rand() - 1.0 + game.base.x;
1137                 game.sector.y = 3.0*Rand() - 1.0 + game.base.y;
1138                 if (VALID_SECTOR(game.sector.x, game.sector.y) &&
1139                     game.quad[game.sector.x][game.sector.y] == IHDOT) break;
1140             }
1141             if (l < QUADSIZE+1) break; /* found a spot */
1142             game.sector.x=QUADSIZE/2;
1143             game.sector.y=QUADSIZE/2;
1144             newqad(true);
1145         }
1146     }
1147     /* Get new commission */
1148     game.quad[game.sector.x][game.sector.y] = game.ship = IHF;
1149     game.state.crew = FULLCREW;
1150     prout(_("Starfleet puts you in command of another ship,"));
1151     prout(_("the Faerie Queene, which is antiquated but,"));
1152     prout(_("still useable."));
1153     if (game.icrystl) prout(_("The dilithium crystals have been moved."));
1154     game.imine = false;
1155     game.iscraft = offship; /* Galileo disappears */
1156     /* Resupply ship */
1157     game.condition=docked;
1158     for (l = 0; l < NDEVICES; l++) 
1159         game.damage[l] = 0.0;
1160     game.damage[DSHUTTL] = -1;
1161     game.energy = game.inenrg = 3000.0;
1162     game.shield = game.inshld = 1250.0;
1163     game.torps = game.intorps = 6;
1164     game.lsupres=game.inlsr=3.0;
1165     game.shldup=false;
1166     game.warpfac=5.0;
1167     game.wfacsq=25.0;
1168     return;
1169 }