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