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