Eliminate nasty macro holdovers.
[super-star-trek.git] / src / moving.c
1 #include <unistd.h>
2 #include "sstlinux.h"
3 #include "sst.h"
4
5 static void getcd(int, int);
6
7 void imove(void) 
8 {
9     double angle, deltax, deltay, bigger, x, y,
10         finald, finalx, finaly, stopegy, probf;
11     int trbeam = 0, n, l, ix=0, iy=0, kink, kinks, iquad;
12
13     if (game.inorbit) {
14         prout("Helmsman Sulu- \"Leaving standard orbit.\"");
15         game.inorbit = FALSE;
16     }
17
18     angle = ((15.0 - game.direc) * 0.5235988);
19     deltax = -sin(angle);
20     deltay = cos(angle);
21     if (fabs(deltax) > fabs(deltay))
22         bigger = fabs(deltax);
23     else
24         bigger = fabs(deltay);
25                 
26     deltay /= bigger;
27     deltax /= bigger;
28
29     /* If tractor beam is to occur, don't move full distance */
30     if (game.state.date+game.optime >= game.future[FTBEAM]) {
31         trbeam = 1;
32         game.condit = IHRED;
33         game.dist = game.dist*(game.future[FTBEAM]-game.state.date)/game.optime + 0.1;
34         game.optime = game.future[FTBEAM] - game.state.date + 1e-5;
35     }
36     /* Move within the quadrant */
37     game.quad[game.sectx][game.secty] = IHDOT;
38     x = game.sectx;
39     y = game.secty;
40     n = 10.0*game.dist*bigger+0.5;
41
42     if (n > 0) {
43         for (l = 1; l <= n; l++) {
44             ix = (x += deltax) + 0.5;
45             iy = (y += deltay) + 0.5;
46             if (!VALID_SECTOR(ix, iy)) {
47                 /* Leaving quadrant -- allow final enemy attack */
48                 /* Don't do it if being pushed by Nova */
49                 if (game.nenhere != 0 && game.iattak != 2) {
50                     newcnd();
51                     for_local_enemies(l) {
52                         finald = sqrt((ix-game.kx[l])*(double)(ix-game.kx[l]) +
53                                       (iy-game.ky[l])*(double)(iy-game.ky[l]));
54                         game.kavgd[l] = 0.5 * (finald+game.kdist[l]);
55                     }
56                     /*
57                      * Stas Sergeev added the game.condition
58                      * that attacks only happen if Klingons
59                      * are present and your skill is good.
60                      */
61                     if (game.skill > SKILL_GOOD && game.klhere > 0 && !game.state.galaxy[game.quadx][game.quady].supernova)
62                         attack(0);
63                     if (game.alldone) return;
64                 }
65                 /* compute final position -- new quadrant and sector */
66                 x = QUADSIZE*(game.quadx-1)+game.sectx;
67                 y = QUADSIZE*(game.quady-1)+game.secty;
68                 ix = x+10.0*game.dist*bigger*deltax+0.5;
69                 iy = y+10.0*game.dist*bigger*deltay+0.5;
70                 /* check for edge of galaxy */
71                 kinks = 0;
72                 do {
73                     kink = 0;
74                     if (ix <= 0) {
75                         ix = -ix + 1;
76                         kink = 1;
77                     }
78                     if (iy <= 0) {
79                         iy = -iy + 1;
80                         kink = 1;
81                     }
82                     if (ix > GALSIZE*QUADSIZE) {
83                         ix = (GALSIZE*QUADSIZE*2)+1 - ix;
84                         kink = 1;
85                     }
86                     if (iy > GALSIZE*QUADSIZE) {
87                         iy = (GALSIZE*QUADSIZE*2)+1 - iy;
88                         kink = 1;
89                     }
90                     if (kink) kinks = 1;
91                 } while (kink);
92
93                 if (kinks) {
94                     game.nkinks += 1;
95                     if (game.nkinks == 3) {
96                         /* Three strikes -- you're out! */
97                         finish(FNEG3);
98                         return;
99                     }
100                     skip(1);
101                     prout("YOU HAVE ATTEMPTED TO CROSS THE NEGATIVE ENERGY BARRIER");
102                     prout("AT THE EDGE OF THE GALAXY.  THE THIRD TIME YOU TRY THIS,");
103                     prout("YOU WILL BE DESTROYED.");
104                 }
105                 /* Compute final position in new quadrant */
106                 if (trbeam) return; /* Don't bother if we are to be beamed */
107                 game.quadx = (ix+(QUADSIZE-1))/QUADSIZE;
108                 game.quady = (iy+(QUADSIZE-1))/QUADSIZE;
109                 game.sectx = ix - QUADSIZE*(game.quadx-1);
110                 game.secty = iy - QUADSIZE*(game.quady-1);
111                 skip(1);
112                 prout("Entering %s.",
113                       cramlc(quadrant, game.quadx, game.quady));
114                 game.quad[game.sectx][game.secty] = game.ship;
115                 newqad(0);
116                 if (game.skill>SKILL_NOVICE) attack(0);
117                 return;
118             }
119             iquad = game.quad[ix][iy];
120             if (iquad != IHDOT) {
121                 /* object encountered in flight path */
122                 stopegy = 50.0*game.dist/game.optime;
123                 game.dist=0.1*sqrt((game.sectx-ix)*(double)(game.sectx-ix) +
124                               (game.secty-iy)*(double)(game.secty-iy));
125                 switch (iquad) {
126                 case IHT: /* Ram a Tholian */
127                 case IHK: /* Ram enemy ship */
128                 case IHC:
129                 case IHS:
130                 case IHR:
131                 case IHQUEST:
132                     game.sectx = ix;
133                     game.secty = iy;
134                     ram(0, iquad, game.sectx, game.secty);
135                     finalx = game.sectx;
136                     finaly = game.secty;
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, ix, iy));
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 (l=0;l<NDEVICES;l++)
153                         if (game.damage[l]>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, ix,iy));
170                     prout(";");
171                     proutn("Emergency stop required ");
172                     prout("%2d units of energy.", (int)stopegy);
173                     game.energy -= stopegy;
174                     finalx = x-deltax+0.5;
175                     game.sectx = finalx;
176                     finaly = y-deltay+0.5;
177                     game.secty = finaly;
178                     if (game.energy <= 0) {
179                         finish(FNRG);
180                         return;
181                     }
182                     break;
183                 }
184                 goto no_quad_change;    /* sorry! */
185             }
186         }
187         game.dist = 0.1*sqrt((game.sectx-ix)*(double)(game.sectx-ix) +
188                         (game.secty-iy)*(double)(game.secty-iy));
189         game.sectx = ix;
190         game.secty = iy;
191     }
192     finalx = game.sectx;
193     finaly = game.secty;
194 no_quad_change:
195     /* No quadrant change -- compute new avg enemy distances */
196     game.quad[game.sectx][game.secty] = game.ship;
197     if (game.nenhere) {
198         for_local_enemies(l) {
199             finald = sqrt((ix-game.kx[l])*(double)(ix-game.kx[l]) +
200                           (iy-game.ky[l])*(double)(iy-game.ky[l]));
201             game.kavgd[l] = 0.5 * (finald+game.kdist[l]);
202             game.kdist[l] = finald;
203         }
204         sortkl();
205         if (!game.state.galaxy[game.quadx][game.quady].supernova && game.iattak == 0)
206             attack(0);
207         for_local_enemies(l) game.kavgd[l] = game.kdist[l];
208     }
209     newcnd();
210     game.iattak = 0;
211     drawmaps(0);
212     setwnd(message_window);
213     return;
214 }
215
216 void dock(int l) 
217 {
218     chew();
219     if (game.condit == IHDOCKED && l) {
220         prout("Already docked.");
221         return;
222     }
223     if (game.inorbit) {
224         prout("You must first leave standard orbit.");
225         return;
226     }
227     if (game.basex==0 || abs(game.sectx-game.basex) > 1 || abs(game.secty-game.basey) > 1) {
228         crmshp();
229         prout(" not adjacent to base.");
230         return;
231     }
232     game.condit = IHDOCKED;
233     if (l) prout("Docked.");
234     game.ididit=1;
235     if (game.energy < game.inenrg) game.energy = game.inenrg;
236     game.shield = game.inshld;
237     game.torps = game.intorps;
238     game.lsupres = game.inlsr;
239     if (game.damage[DRADIO] == 0.0 &&
240         (game.future[FCDBAS] < FOREVER || game.isatb == 1) && game.iseenit == 0) {
241         /* get attack report from base */
242         prout("Lt. Uhura- \"Captain, an important message from the starbase:\"");
243         attakreport(0);
244         game.iseenit = 1;
245     }
246 }
247
248 static void getcd(int isprobe, int akey) {
249         /* This program originally required input in terms of a (clock)
250            direction and distance. Somewhere in history, it was changed to
251            cartesian coordinates. So we need to convert. I think
252            "manual" input should still be done this way -- it's a real
253            pain if the computer isn't working! Manual mode is still confusing
254            because it involves giving x and y motions, yet the coordinates
255            are always displayed y - x, where +y is downward! */
256
257         
258         int irowq=game.quadx, icolq=game.quady, irows, icols, itemp=0, iprompt=0, key=0;
259         double xi, xj, xk, xl;
260         double deltax, deltay;
261         int automatic = -1;
262
263         /* Get course direction and distance. If user types bad values, return
264            with DIREC = -1.0. */
265
266         game.direc = -1.0;
267         
268         if (game.landed == 1 && !isprobe) {
269                 prout("Dummy! You can't leave standard orbit until you");
270                 proutn("are back aboard the ");
271                 crmshp();
272                 prout(".");
273                 chew();
274                 return;
275         }
276         while (automatic == -1) {
277                 if (game.damage[DCOMPTR]) {
278                         if (isprobe)
279                                 prout("Computer damaged; manual navigation only");
280                         else
281                                 prout("Computer damaged; manual movement only");
282                         chew();
283                         automatic = 0;
284                         key = IHEOL;
285                         break;
286                 }
287                 if (isprobe && akey != -1) {
288                         /* For probe launch, use pre-scaned 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 = 1;
298                         chew();
299                 }
300                 else if (key == IHALPHA) {
301                         if (isit("manual")) {
302                                 automatic =0;
303                                 key = scan();
304                                 break;
305                         }
306                         else if (isit("automatic")) {
307                                 automatic = 1;
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                         automatic = 0;
323                         break;
324                 }
325         }
326
327         if (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 = 1;
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                         irows = xk + 0.5;
363                         icols = 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                                 irows = icols = 5;
371                         }
372                         else {
373                                 irows = xi + 0.5;
374                                 icols = xj + 0.5;
375                         }
376                         itemp = 1;
377                 }
378                 if (!VALID_QUADRANT(icolq,irowq)||!VALID_SECTOR(icols,irows)) {
379                     huh();
380                     return;
381                 }
382                 skip(1);
383                 if (!isprobe) {
384                         if (itemp) {
385                                 if (iprompt) {
386                                         prout("Helmsman Sulu- \"Course locked in for %s.\"",
387                                                 cramlc(sector, irows, icols));
388                                 }
389                         }
390                         else prout("Ensign Chekov- \"Course laid in, Captain.\"");
391                 }
392                 deltax = icolq - game.quady + 0.1*(icols-game.secty);
393                 deltay = game.quadx - irowq + 0.1*(game.sectx-irows);
394         }
395         else { /* manual */
396                 while (key == IHEOL) {
397                         proutn("X and Y displacements- ");
398                         chew();
399                         iprompt = 1;
400                         key = scan();
401                 }
402                 itemp = 2;
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 == 2 && !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
434 void impuls(void) 
435 {
436     double power;
437
438     game.ididit = 0;
439     if (game.damage[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() == 0) return;
477     }
478     /* Activate impulse engines and pay the cost */
479     imove();
480     game.ididit = 1;
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(int i) 
491 {
492     int blooey=0, twarp=0, iwarp;
493     double power;
494
495     if (i!=2) { /* Not WARPX entry */
496         game.ididit = 0;
497         if (game.damage[DWARPEN] > 10.0) {
498             chew();
499             skip(1);
500             prout("Engineer Scott- \"The impulse engines are damaged, Sir.\"");
501             return;
502         }
503         if (game.damage[DWARPEN] > 0.0 && game.warpfac > 4.0) {
504             chew();
505             skip(1);
506             prout("Engineer Scott- \"Sorry, Captain. Until this damage");
507             prout("  is repaired, I can only give you warp 4.\"");
508             return;
509         }
510                         
511         /* Read in course and distance */
512         getcd(FALSE, 0);
513         if (game.direc == -1.0) return;
514
515         /* Make sure starship has enough energy for the trip */
516         power = (game.dist+0.05)*game.warpfac*game.warpfac*game.warpfac*(game.shldup+1);
517
518
519         if (power >= game.energy) {
520             /* Insufficient power for trip */
521             game.ididit = 0;
522             skip(1);
523             prout("Engineering to bridge--");
524             if (game.shldup==0 || 0.5*power > game.energy) {
525                 iwarp = pow((game.energy/(game.dist+0.05)), 0.333333333);
526                 if (iwarp <= 0) {
527                     prout("We can't do it, Captain. We haven't the energy.");
528                 }
529                 else {
530                     proutn("We haven't the energy, but we could do it at warp %d", iwarp);
531                     if (game.shldup) {
532                         prout(",");
533                         prout("if you'll lower the shields.");
534                     }
535                     else
536                         prout(".");
537                 }
538             }
539             else
540                 prout("We haven't the energy to go that far with the shields up.");
541             return;
542         }
543                                                 
544         /* Make sure enough time is left for the trip */
545         game.optime = 10.0*game.dist/game.wfacsq;
546         if (game.optime >= 0.8*game.state.remtime) {
547             skip(1);
548             prout("First Officer Spock- \"Captain, I compute that such");
549             proutn("  a trip would require approximately %2.0f",
550                    100.0*game.optime/game.state.remtime);
551             prout(" percent of our");
552             proutn("  remaining time.  Are you sure this is wise?\" ");
553             if (ja() == 0) { game.ididit = 0; game.optime=0; return;}
554         }
555     }
556     /* Entry WARPX */
557     if (game.warpfac > 6.0) {
558         /* Decide if engine damage will occur */
559         double prob = game.dist*(6.0-game.warpfac)*(6.0-game.warpfac)/66.666666666;
560         if (prob > Rand()) {
561             blooey = 1;
562             game.dist = Rand()*game.dist;
563         }
564         /* Decide if time warp will occur */
565         if (0.5*game.dist*pow(7.0,game.warpfac-10.0) > Rand()) twarp=1;
566 #ifdef DEBUG
567         if (game.idebug &&game.warpfac==10 && twarp==0) {
568             blooey=0;
569             proutn("Force time warp? ");
570             if (ja()==1) twarp=1;
571         }
572 #endif
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.sectx;
590             y = game.secty;
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 = 0;
599                     twarp = 0;
600                 }
601             }
602         }
603     }
604                                 
605
606     /* Activate Warp Engines and pay the cost */
607     imove();
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 = 1;
621     return;
622 }
623
624
625
626 void setwrp(void) 
627 {
628     int key;
629     double oldfac;
630         
631     while ((key=scan()) == IHEOL) {
632         chew();
633         proutn("Warp factor- ");
634     }
635     chew();
636     if (key != IHREAL) {
637         huh();
638         return;
639     }
640     if (game.damage[DWARPEN] > 10.0) {
641         prout("Warp engines inoperative.");
642         return;
643     }
644     if (game.damage[DWARPEN] > 0.0 && aaitem > 4.0) {
645         prout("Engineer Scott- \"I'm doing my best, Captain,");
646         prout("  but right now we can only go warp 4.\"");
647         return;
648     }
649     if (aaitem > 10.0) {
650         prout("Helmsman Sulu- \"Our top speed is warp 10, Captain.\"");
651         return;
652     }
653     if (aaitem < 1.0) {
654         prout("Helmsman Sulu- \"We can't go below warp 1, Captain.\"");
655         return;
656     }
657     oldfac = game.warpfac;
658     game.warpfac = aaitem;
659     game.wfacsq=game.warpfac*game.warpfac;
660     if (game.warpfac <= oldfac || game.warpfac <= 6.0) {
661         proutn("Helmsman Sulu- \"Warp factor %d, Captain.\"", 
662                (int)game.warpfac);
663         return;
664     }
665     if (game.warpfac < 8.00) {
666         prout("Engineer Scott- \"Aye, but our maximum safe speed is warp 6.\"");
667         return;
668     }
669     if (game.warpfac == 10.0) {
670         prout("Engineer Scott- \"Aye, Captain, we'll try it.\"");
671         return;
672     }
673     prout("Engineer Scott- \"Aye, Captain, but our engines may not take it.\"");
674     return;
675 }
676
677 void atover(int igrab) 
678 {
679     double power, distreq;
680
681     chew();
682     /* is captain on planet? */
683     if (game.landed==1) {
684         if (game.damage[DTRANSP]) {
685             finish(FPNOVA);
686             return;
687         }
688         prout("Scotty rushes to the transporter controls.");
689         if (game.shldup) {
690             prout("But with the shields up it's hopeless.");
691             finish(FPNOVA);
692         }
693         prouts("His desperate attempt to rescue you . . .");
694         if (Rand() <= 0.5) {
695             prout("fails.");
696             finish(FPNOVA);
697             return;
698         }
699         prout("SUCCEEDS!");
700         if (game.imine) {
701             game.imine = 0;
702             proutn("The crystals mined were ");
703             if (Rand() <= 0.25) {
704                 prout("lost.");
705             }
706             else {
707                 prout("saved.");
708                 game.icrystl = 1;
709             }
710         }
711     }
712     if (igrab) return;
713
714     /* Check to see if captain in shuttle craft */
715     if (game.icraft) finish(FSTRACTOR);
716     if (game.alldone) return;
717
718     /* Inform captain of attempt to reach safety */
719     skip(1);
720     do {
721         if (game.justin) {
722             prouts("***RED ALERT!  READ ALERT!");
723             skip(1);
724             proutn("The ");
725             crmshp();
726             prout(" has stopped in a quadrant containing");
727             prouts("   a supernova.");
728             skip(2);
729         }
730         proutn("***Emergency automatic override attempts to hurl ");
731         crmshp();
732         skip(1);
733         prout("safely out of quadrant.");
734         if (game.damage[DRADIO] == 0.0)
735             game.state.galaxy[game.quadx][game.quady].charted = TRUE;
736         /* Try to use warp engines */
737         if (game.damage[DWARPEN]) {
738             skip(1);
739             prout("Warp engines damaged.");
740             finish(FSNOVAED);
741             return;
742         }
743         game.warpfac = 6.0+2.0*Rand();
744         game.wfacsq = game.warpfac * game.warpfac;
745         prout("Warp factor set to %d", (int)game.warpfac);
746         power = 0.75*game.energy;
747         game.dist = power/(game.warpfac*game.warpfac*game.warpfac*(game.shldup+1));
748         distreq = 1.4142+Rand();
749         if (distreq < game.dist) game.dist = distreq;
750         game.optime = 10.0*game.dist/game.wfacsq;
751         game.direc = 12.0*Rand();       /* How dumb! */
752         game.justin = 0;
753         game.inorbit = 0;
754         warp(2);
755         if (game.justin == 0) {
756             /* This is bad news, we didn't leave quadrant. */
757             if (game.alldone) return;
758             skip(1);
759             prout("Insufficient energy to leave quadrant.");
760             finish(FSNOVAED);
761             return;
762         }
763     } while 
764         /* Repeat if another snova */
765         (game.state.galaxy[game.quadx][game.quady].supernova);
766     if (KLINGREM==0) 
767         finish(FWON); /* Snova killed remaining enemy. */
768 }
769
770 void timwrp() 
771 {
772     int l, gotit;
773     prout("***TIME WARP ENTERED.");
774     if (game.state.snap && Rand() < 0.5) {
775         /* Go back in time */
776         prout("You are traveling backwards in time %d stardates.",
777               (int)(game.state.date-game.snapsht.date));
778         game.state = game.snapsht;
779         game.state.snap = 0;
780         if (game.state.remcom) {
781             game.future[FTBEAM] = game.state.date + expran(game.intime/game.state.remcom);
782             game.future[FBATTAK] = game.state.date + expran(0.3*game.intime);
783         }
784         game.future[FSNOVA] = game.state.date + expran(0.5*game.intime);
785         game.future[FSNAP] = game.state.date +expran(0.25*game.state.remtime); /* next snapshot will
786                                                                                   be sooner */
787         if (game.state.nscrem) game.future[FSCMOVE] = 0.2777;
788         game.isatb = 0;
789         game.future[FCDBAS] = game.future[FSCDBAS] = FOREVER;
790         game.batx = game.baty = 0;
791
792         /* Make sure Galileo is consistant -- Snapshot may have been taken
793            when on planet, which would give us two Galileos! */
794         gotit = 0;
795         for (l = 0; l < game.inplan; l++) {
796             if (game.state.plnets[l].known == shuttle_down) {
797                 gotit = 1;
798                 if (game.iscraft==1 && game.ship==IHE) {
799                     prout("Checkov-  \"Security reports the Galileo has disappeared, Sir!");
800                     game.iscraft = 0;
801                 }
802             }
803         }
804         /* Likewise, if in the original time the Galileo was abandoned, but
805            was on ship earlier, it would have vanished -- lets restore it */
806         if (game.iscraft==0 && gotit==0 && game.damage[DSHUTTL] >= 0.0) {
807             prout("Checkov-  \"Security reports the Galileo has reappeared in the dock!\"");
808             game.iscraft = 1;
809         }
810         /* 
811          * There used to be code to do the actual reconstrction here,
812          * but the starchart is now part of the snapshotted galaxy state.
813          */
814         prout("Spock has reconstructed a correct star chart from memory");
815     }
816     else {
817         /* Go forward in time */
818         game.optime = -0.5*game.intime*log(Rand());
819         prout("You are traveling forward in time %d stardates.", (int)game.optime);
820         /* cheat to make sure no tractor beams occur during time warp */
821         game.future[FTBEAM] += game.optime;
822         game.damage[DRADIO] += game.optime;
823     }
824     newqad(0);
825     events();   /* Stas Sergeev added this -- do pending events */
826 }
827
828 void probe(void) 
829 {
830     double angle, bigger;
831     int key;
832     /* New code to launch a deep space probe */
833     if (game.nprobes == 0) {
834         chew();
835         skip(1);
836         if (game.ship == IHE) 
837             prout("Engineer Scott- \"We have no more deep space probes, Sir.\"");
838         else
839             prout("Ye Faerie Queene has no deep space probes.");
840         return;
841     }
842     if (game.damage[DDSP] != 0.0) {
843         chew();
844         skip(1);
845         prout("Engineer Scott- \"The probe launcher is damaged, Sir.\"");
846         return;
847     }
848     if (game.future[FDSPROB] != FOREVER) {
849         chew();
850         skip(1);
851         if (game.damage[DRADIO] != 0 && game.condit != IHDOCKED) {
852             prout("Spock-  \"Records show the previous probe has not yet");
853             prout("   reached its destination.\"");
854         }
855         else
856             prout("Uhura- \"The previous probe is still reporting data, Sir.\"");
857         return;
858     }
859     key = scan();
860
861     if (key == IHEOL) {
862         /* slow mode, so let Kirk know how many probes there are left */
863         prout(game.nprobes==1 ? "%d probe left." : "%d probes left.", game.nprobes);
864         proutn("Are you sure you want to fire a probe? ");
865         if (ja()==0) return;
866     }
867
868     game.isarmed = FALSE;
869     if (key == IHALPHA && strcmp(citem,"armed") == 0) {
870         game.isarmed = TRUE;
871         key = scan();
872     }
873     else if (key == IHEOL) {
874         proutn("Arm NOVAMAX warhead? ");
875         game.isarmed = ja();
876     }
877     getcd(TRUE, key);
878     if (game.direc == -1.0) return;
879     game.nprobes--;
880     angle = ((15.0 - game.direc) * 0.5235988);
881     game.probeinx = -sin(angle);
882     game.probeiny = cos(angle);
883     if (fabs(game.probeinx) > fabs(game.probeiny))
884         bigger = fabs(game.probeinx);
885     else
886         bigger = fabs(game.probeiny);
887                 
888     game.probeiny /= bigger;
889     game.probeinx /= bigger;
890     game.proben = 10.0*game.dist*bigger +0.5;
891     game.probex = game.quadx*QUADSIZE + game.sectx - 1; // We will use better packing than original
892     game.probey = game.quady*QUADSIZE + game.secty - 1;
893     game.probecx = game.quadx;
894     game.probecy = game.quady;
895     game.future[FDSPROB] = game.state.date + 0.01; // Time to move one sector
896     prout("Ensign Chekov-  \"The deep space probe is launched, Captain.\"");
897     game.ididit = 1;
898     return;
899 }
900
901 void help(void) 
902 {
903     /* There's more than one way to move in this game! */
904     double ddist, xdist, probf;
905     int line = 0, l, ix, iy;
906
907     chew();
908     /* Test for game.conditions which prevent calling for help */
909     if (game.condit == IHDOCKED) {
910         prout("Lt. Uhura-  \"But Captain, we're already docked.\"");
911         return;
912     }
913     if (game.damage[DRADIO] != 0) {
914         prout("Subspace radio damaged.");
915         return;
916     }
917     if (game.state.rembase==0) {
918         prout("Lt. Uhura-  \"Captain, I'm not getting any response from Starbase.\"");
919         return;
920     }
921     if (game.landed == 1) {
922         proutn("You must be aboard the ");
923         crmshp();
924         prout(".");
925         return;
926     }
927     /* OK -- call for help from nearest starbase */
928     game.nhelp++;
929     if (game.basex!=0) {
930         /* There's one in this quadrant */
931         ddist = sqrt(square(game.basex-game.sectx)+square(game.basey-game.secty));
932     }
933     else {
934         ddist = FOREVER;
935         for_starbases(l) {
936             xdist=10.0*sqrt(square(game.state.baseqx[l]-game.quadx)+square(game.state.baseqy[l]-game.quady));
937             if (xdist < ddist) {
938                 ddist = xdist;
939                 line = l;
940             }
941         }
942         /* Since starbase not in quadrant, set up new quadrant */
943         game.quadx = game.state.baseqx[line];
944         game.quady = game.state.baseqy[line];
945         newqad(1);
946     }
947     /* dematerialize starship */
948     game.quad[game.sectx][game.secty]=IHDOT;
949     proutn("Starbase in %s responds--", cramlc(quadrant, game.quadx, game.quady));
950     proutn("");
951     crmshp();
952     prout(" dematerializes.");
953     game.sectx=0;
954     for (l = 1; l <= 5; l++) {
955         ix = game.basex+3.0*Rand()-1;
956         iy = game.basey+3.0*Rand()-1;
957         if (VALID_SECTOR(ix,iy) && game.quad[ix][iy]==IHDOT) {
958             /* found one -- finish up */
959             game.sectx=ix;
960             game.secty=iy;
961             break;
962         }
963     }
964     if (game.sectx==0){
965         prout("You have been lost in space...");
966         finish(FMATERIALIZE);
967         return;
968     }
969     /* Give starbase three chances to rematerialize starship */
970     probf = pow((1.0 - pow(0.98,ddist)), 0.33333333);
971     for (l = 1; l <= 3; l++) {
972         switch (l) {
973         case 1: proutn("1st"); break;
974         case 2: proutn("2nd"); break;
975         case 3: proutn("3rd"); break;
976         }
977         proutn(" attempt to re-materialize ");
978         crmshp();
979         switch (l){
980         case 1: game.quad[ix][iy]=IHMATER0;
981             break;
982         case 2: game.quad[ix][iy]=IHMATER1;
983             break;
984         case 3: game.quad[ix][iy]=IHMATER2;
985             break;
986         }
987         textcolor(RED);
988         warble();
989         if (Rand() > probf) break;
990         prout("fails.");
991         delay(500);
992         textcolor(DEFAULT);
993     }
994     if (l > 3) {
995         game.quad[ix][iy]=IHQUEST;
996         game.alive = 0;
997         drawmaps(1);
998         setwnd(message_window);
999         finish(FMATERIALIZE);
1000         return;
1001     }
1002     game.quad[ix][iy]=game.ship;
1003     textcolor(GREEN);
1004     prout("succeeds.");
1005     textcolor(DEFAULT);
1006     dock(0);
1007     skip(1);
1008     prout("Lt. Uhura-  \"Captain, we made it!\"");
1009 }