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