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