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