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