Stas's fixes for torpedo and phaser animation.
[super-star-trek.git] / reports.c
1 #include "sst.h"
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 void attakreport(int curt) 
7 {
8     if (!curt) {
9         if (game.future[FCDBAS] < FOREVER) {
10             prout("Starbase in %s is currently under Commander attack.",
11                   cramlc(quadrant, batx, baty));
12             prout("It can hold out until Stardate %d.", 
13                   (int)game.future[FCDBAS]);
14         }
15         if (isatb == 1) {
16             prout("Starbase in %s is under Super-commander attack.",
17                   cramlc(quadrant, game.state.isx, game.state.isy));
18             prout("It can hold out until Stardate %d.", 
19                   (int)game.future[FSCDBAS]);
20         }
21     } else {
22         if (game.future[FCDBAS] < FOREVER)
23             proutn("Base in %i - %i attacked by C. Alive until %.1f", batx, baty, game.future[FCDBAS]);
24         if (isatb == 1)
25             proutn("Base in %i - %i attacked by S. Alive until %.1f", game.state.isx, game.state.isy, game.future[FSCDBAS]);
26     }
27     clreol();
28 }
29         
30
31 void report(void) 
32 {
33     char *s1,*s2,*s3;
34
35     chew();
36     s1 = (thawed?"thawed ":"");
37     switch (length) {
38     case 1: s2="short"; break;
39     case 2: s2="medium"; break;
40     case 4: s2="long"; break;
41     default: s2="unknown length"; break;
42     }
43     switch (skill) {
44     case SKILL_NOVICE: s3="novice"; break;
45     case SKILL_FAIR: s3="fair"; break;
46     case SKILL_GOOD: s3="good"; break;
47     case SKILL_EXPERT: s3="expert"; break;
48     case SKILL_EMERITUS: s3="emeritus"; break;
49     default: s3="skilled"; break;
50     }
51     prout("");
52     prout("You %s playing a %s%s %s game.",
53           alldone? "were": "are now", s1, s2, s3);
54     if (skill>SKILL_GOOD && thawed && !alldone) prout("No plaque is allowed.");
55     if (tourn) prout("This is tournament game %d.", tourn);
56     prout("Your secret password is \"%s\"",game.passwd);
57     proutn("%d of %d Klingons have been killed",
58            game.state.killk+game.state.killc+game.state.nsckill, inkling);
59     if (game.state.killc) prout(", including %d Commander%s.", game.state.killc, game.state.killc==1?"":"s");
60     else if (game.state.killk+game.state.nsckill > 0) prout(", but no Commanders.");
61     else prout(".");
62     if (skill > SKILL_FAIR) prout("The Super Commander has %sbeen destroyed.",
63                                   game.state.nscrem?"not ":"");
64     if (game.state.rembase != inbase) {
65         proutn("There ");
66         if (inbase-game.state.rembase==1) proutn("has been 1 base");
67         else {
68             proutn("have been %d bases", inbase-game.state.rembase);
69         }
70         prout(" destroyed, %d remaining.", game.state.rembase);
71     }
72     else prout("There are %d bases.", inbase);
73     if (game.damage[DRADIO] == 0.0 || condit == IHDOCKED || iseenit) {
74         /* Don't report this if not seen and
75            either the radio is dead or not at base! */
76         attakreport(0);
77         iseenit = 1;
78     }
79     if (casual) prout("%d casualt%s suffered so far.",
80                       casual, casual==1? "y" : "ies");
81     if (nhelp) prout("There were %d call%s for help.",
82                      nhelp, nhelp==1 ? "" : "s");
83     if (ship == IHE) {
84         proutn("You have ");
85         if (nprobes) proutn("%d", nprobes);
86         else proutn("no");
87         proutn(" deep space probe");
88         if (nprobes!=1) proutn("s");
89         prout(".");
90     }
91     if ((game.damage[DRADIO] == 0.0 || condit == IHDOCKED)&&
92         game.future[FDSPROB] != FOREVER) {
93         if (isarmed) 
94             proutn("An armed deep space probe is in");
95         else
96             proutn("A deep space probe is in");
97         proutn(cramlc(quadrant, probecx, probecy));
98         prout(".");
99     }
100     if (icrystl) {
101         if (cryprob <= .05)
102             prout("Dilithium crystals aboard ship... not yet used.");
103         else {
104             int i=0;
105             double ai = 0.05;
106             while (cryprob > ai) {
107                 ai *= 2.0;
108                 i++;
109             }
110             prout("Dilithium crystals have been used %d time%s.",
111                   i, i==1? "" : "s");
112         }
113     }
114     skip(1);
115 }
116         
117 void lrscan(void) 
118 {
119     int x, y;
120     chew();
121     if (game.damage[DLRSENS] != 0.0) {
122         /* Now allow base's sensors if docked */
123         if (condit != IHDOCKED) {
124             prout("LONG-RANGE SENSORS DAMAGED.");
125             return;
126         }
127         proutn("Starbase's long-range scan");
128     }
129     else {
130         prout("Long-range scan");
131     }
132     for (x = quadx-1; x <= quadx+1; x++) {
133         proutn(" ");
134         for (y = quady-1; y <= quady+1; y++) {
135             if (!VALID_QUADRANT(x, y))
136                 proutn("  -1");
137             else {
138                 if (!game.damage[DRADIO])
139                     game.state.galaxy[x][y].charted = TRUE;
140                 game.state.chart[x][y].klingons = game.state.galaxy[x][y].klingons;
141                 game.state.chart[x][y].starbase = game.state.galaxy[x][y].starbase;
142                 game.state.chart[x][y].stars = game.state.galaxy[x][y].stars;
143                 if (game.state.galaxy[x][y].supernova) 
144                     proutn(" ***");
145                 else
146                     proutn(" %3d", game.state.chart[x][y].klingons*100 + game.state.chart[x][y].starbase * 10 + game.state.chart[x][y].stars);
147             }
148         }
149         prout(" ");
150     }
151 }
152
153 void dreprt(void) 
154 {
155     int jdam = FALSE, i;
156     chew();
157
158     for (i = 0; i < NDEVICES; i++) {
159         if (game.damage[i] > 0.0) {
160             if (!jdam) {
161                 prout("DEVICE            -REPAIR TIMES-");
162                 prout("                IN FLIGHT   DOCKED");
163                 jdam = TRUE;
164             }
165             prout("  %16s %8.2f  %8.2f", 
166                   device[i],
167                   game.damage[i]+0.05,
168                   docfac*game.damage[i]+0.005);
169         }
170     }
171     if (!jdam) prout("All devices functional.");
172 }
173
174 void rechart(void)
175 /* update the chart in the Enterprise's computer from galaxy data */
176 {
177     int i, j;
178     lastchart = game.state.date;
179     for_quadrants(i)
180         for_quadrants(j) 
181             if (game.state.galaxy[i][j].charted) {
182                 game.state.chart[i][j].klingons = game.state.galaxy[i][j].klingons;
183                 game.state.chart[i][j].starbase = game.state.galaxy[i][j].starbase;
184                 game.state.chart[i][j].stars = game.state.galaxy[i][j].stars;
185             }
186 }
187
188 void chart(int nn) 
189 {
190     int i,j;
191     char *cp;
192     chew();
193
194     if (game.damage[DRADIO] == 0.0)
195         rechart();
196
197     if (lastchart < game.state.date && condit == IHDOCKED) {
198         proutn("Spock-  \"I revised the Star Chart from the starbase's records.\"\n");
199         rechart();
200     }
201
202     if (nn == 0) proutn("       STAR CHART FOR THE KNOWN GALAXY\n");
203     if (game.state.date > lastchart)
204         prout("(Last surveillance update %d stardates ago).",
205               (int)(game.state.date-lastchart));
206     prout("      1    2    3    4    5    6    7    8");
207     for_quadrants(i) {
208         proutn("%d |", i);
209         for_quadrants(j) {
210             char buf[4];
211             if ((game.options & OPTION_SHOWME) && i == quadx && j == quady)
212                 proutn("<");
213             else
214                 proutn(" ");
215             if (game.state.galaxy[i][j].supernova)
216                 strcpy(buf, "***");
217             else if (!game.state.galaxy[i][j].charted && game.state.galaxy[i][j].starbase)
218                 strcpy(buf, ".1.");
219             else if (game.state.galaxy[i][j].charted)
220                 sprintf(buf, "%d%d%d", game.state.chart[i][j].klingons, game.state.chart[i][j].starbase, game.state.chart[i][j].stars);
221             else
222                 strcpy(buf, "...");
223             for (cp = buf; cp < buf + sizeof(buf); cp++)
224                 if (*cp == '0')
225                     *cp = '.';
226             proutn(buf);
227             if ((game.options & OPTION_SHOWME) && i == quadx && j == quady)
228                 proutn(">");
229             else
230                 proutn(" ");
231         }
232         proutn("  |");
233         if (i<GALSIZE) proutn("\n");
234     }
235     prout("");  /* flush output */
236 }
237
238 static void sectscan(int goodScan, int i, int j) 
239 {
240     if (goodScan || (abs(i-sectx)<= 1 && abs(j-secty) <= 1)){
241         if ((game.quad[i][j]==IHMATER0)||(game.quad[i][j]==IHMATER1)||(game.quad[i][j]==IHMATER2)||(game.quad[i][j]==IHE)||(game.quad[i][j]==IHF)){
242             switch (condit) {
243             case IHRED: textcolor(RED); break;
244             case IHGREEN: textcolor(GREEN); break;
245             case IHYELLOW: textcolor(YELLOW); break;
246             case IHDOCKED: textcolor(CYAN); break;
247             case IHDEAD: textcolor(BROWN);
248             }
249             if (game.quad[i][j] != ship) 
250                 highvideo();
251         }
252         proutn("%c ",game.quad[i][j]);
253         textcolor(DEFAULT);
254     }
255     else
256         proutn("- ");
257 }
258
259 static void status(int req) 
260 {
261     char *cp = NULL;
262     int t, dam = 0;
263     switch (req) {
264     case 1:
265         proutn("Stardate      %.1f, Time Left %.2f", game.state.date, game.state.remtime);
266         break;
267     case 2:
268         if (condit != IHDOCKED) newcnd();
269         switch (condit) {
270         case IHRED: cp = "RED"; break;
271         case IHGREEN: cp = "GREEN"; break;
272         case IHYELLOW: cp = "YELLOW"; break;
273         case IHDOCKED: cp = "DOCKED"; break;
274         case IHDEAD: cp="DEAD"; break;
275         }
276         for (t=0;t<NDEVICES;t++)
277             if (game.damage[t]>0) 
278                 dam++;
279         proutn("Condition     %s, %i DAMAGES", cp, dam);
280         break;
281     case 3:
282         proutn("Position      %d - %d , %d - %d",
283                quadx, quady, sectx, secty);
284         break;
285     case 4:
286         proutn("Life Support  ");
287         if (game.damage[DLIFSUP] != 0.0) {
288             if (condit == IHDOCKED)
289                 proutn("DAMAGED, Base provides");
290             else
291                 proutn("DAMAGED, reserves=%4.2f", lsupres);
292         }
293         else
294             proutn("ACTIVE");
295         break;
296     case 5:
297         proutn("Warp Factor   %.1f", warpfac);
298         break;
299     case 6:
300         proutn("Energy        %.2f", energy);
301         if (icrystl && (game.options & OPTION_SHOWME))  /* ESR */
302             proutn(" (have crystals)");
303         break;
304     case 7:
305         proutn("Torpedoes     %d", torps);
306         break;
307     case 8:
308         proutn("Shields       ");
309         if (game.damage[DSHIELD] != 0)
310             proutn("DAMAGED,");
311         else if (shldup)
312             proutn("UP,");
313         else
314             proutn("DOWN,");
315         proutn(" %d%% %.1f units",
316                (int)((100.0*shield)/inshld + 0.5), shield);
317         break;
318     case 9:
319         proutn("Klingons Left %d", game.state.remkl);
320         break;
321     case 10:
322         attakreport(1);
323         break;
324     }
325 }
326                 
327 int srscan(int l) 
328 {
329     static char requests[][3] =
330         {"","da","co","po","ls","wa","en","to","sh","kl","ti"};
331     int leftside=TRUE, rightside=TRUE, i, j, jj, req=0, nn=FALSE;
332     int goodScan=TRUE;
333     switch (l) {
334     case SCAN_FULL: // SRSCAN
335         if (game.damage[DSRSENS] != 0) {
336             /* Allow base's sensors if docked */
337             if (condit != IHDOCKED) {
338                 prout("   S.R. SENSORS DAMAGED!");
339                 goodScan=FALSE;
340             }
341             else
342                 prout("  [Using Base's sensors]");
343         }
344         else proutn("     Short-range scan");
345         if (goodScan && !game.damage[DRADIO]) { 
346             game.state.chart[quadx][quady].klingons = game.state.galaxy[quadx][quady].klingons;
347             game.state.chart[quadx][quady].starbase = game.state.galaxy[quadx][quady].starbase;
348             game.state.chart[quadx][quady].stars = game.state.galaxy[quadx][quady].stars;
349             game.state.galaxy[quadx][quady].charted = TRUE;
350         }
351         scan();
352         if (isit("chart")) nn = TRUE;
353         if (isit("no")) rightside = FALSE;
354         chew();
355         proutn("    1 2 3 4 5 6 7 8 9 10\n");
356         break;
357     case SCAN_REQUEST:
358         while (scan() == IHEOL)
359             proutn("Information desired? ");
360         chew();
361         for (req = 1; req <= sizeof(requests)/sizeof(requests[0]); req++)
362             if (strncmp(citem,requests[req],min(2,strlen(citem)))==0)
363                 break;
364         if (req > sizeof(requests)/sizeof(requests[0])) {
365             prout("UNRECOGNIZED REQUEST. Legal requests are:\n"
366                   "  date, condition, position, lsupport, warpfactor,\n"
367                   "  energy, torpedoes, shields, klingons, time, bases.");
368             return FALSE;
369         }
370         // no break
371     case SCAN_STATUS: // STATUS
372         chew();
373         leftside = FALSE;
374         skip(1);
375         // no break
376     case SCAN_NO_LEFTSIDE: // REQUEST
377         leftside=FALSE;
378         break;
379     }
380     if (condit != IHDOCKED) newcnd();
381     for (i = 1; i <= max(QUADSIZE, sizeof(requests)/sizeof(requests[0])); i++) {
382         jj = (req!=0 ? req : i);
383         if (leftside && i <= QUADSIZE) {
384             proutn("%2d  ", i);
385             for_sectors(j) {
386                 sectscan(goodScan, i, j);
387             }
388         }
389         if (rightside)
390             status(jj);
391         if (i<sizeof(requests)/sizeof(requests[0])) proutn("\n");
392         if (req!=0) return(goodScan);
393     }
394     prout("");
395     if (nn) chart(1);
396     return(goodScan);
397 }
398                         
399                         
400 void eta(void)
401 {
402     int ix1, ix2, iy1, iy2, prompt=FALSE;
403     int wfl;
404     double ttime, twarp, tpower;
405     if (game.damage[DCOMPTR] != 0.0) {
406         prout("COMPUTER DAMAGED, USE A POCKET CALCULATOR.");
407         skip(1);
408         return;
409     }
410     if (scan() != IHREAL) {
411         prompt = TRUE;
412         chew();
413         proutn("Destination quadrant and/or sector? ");
414         if (scan()!=IHREAL) {
415             huh();
416             return;
417         }
418     }
419     iy1 = aaitem +0.5;
420     if (scan() != IHREAL) {
421         huh();
422         return;
423     }
424     ix1 = aaitem + 0.5;
425     if (scan() == IHREAL) {
426         iy2 = aaitem + 0.5;
427         if (scan() != IHREAL) {
428             huh();
429             return;
430         }
431         ix2 = aaitem + 0.5;
432     }
433     else {
434         if (quady>ix1) ix2 = 1;
435         else ix2=QUADSIZE;
436         if (quadx>iy1) iy2 = 1;
437         else iy2=QUADSIZE;
438     }
439
440     if (!VALID_QUADRANT(ix1, iy1) || !VALID_SECTOR(ix2, iy2)) {
441         huh();
442         return;
443     }
444     dist = sqrt(square(iy1-quadx+0.1*(iy2-sectx))+
445                 square(ix1-quady+0.1*(ix2-secty)));
446     wfl = FALSE;
447
448     if (prompt) prout("Answer \"no\" if you don't know the value:");
449     while (TRUE) {
450         chew();
451         proutn("Time or arrival date? ");
452         if (scan()==IHREAL) {
453             ttime = aaitem;
454             if (ttime > game.state.date) ttime -= game.state.date; // Actually a star date
455             if (ttime <= 1e-10 ||
456                 (twarp=(floor(sqrt((10.0*dist)/ttime)*10.0)+1.0)/10.0) > 10) {
457                 prout("We'll never make it, sir.");
458                 chew();
459                 return;
460             }
461             if (twarp < 1.0) twarp = 1.0;
462             break;
463         }
464         chew();
465         proutn("Warp factor? ");
466         if (scan()== IHREAL) {
467             wfl = TRUE;
468             twarp = aaitem;
469             if (twarp<1.0 || twarp > 10.0) {
470                 huh();
471                 return;
472             }
473             break;
474         }
475         prout("Captain, certainly you can give me one of these.");
476     }
477     while (TRUE) {
478         chew();
479         ttime = (10.0*dist)/square(twarp);
480         tpower = dist*twarp*twarp*twarp*(shldup+1);
481         if (tpower >= energy) {
482             prout("Insufficient energy, sir.");
483             if (shldup==0 || tpower > energy*2.0) {
484                 if (!wfl) return;
485                 proutn("New warp factor to try? ");
486                 if (scan() == IHREAL) {
487                     wfl = TRUE;
488                     twarp = aaitem;
489                     if (twarp<1.0 || twarp > 10.0) {
490                         huh();
491                         return;
492                     }
493                     continue;
494                 }
495                 else {
496                     chew();
497                     skip(1);
498                     return;
499                 }
500             }
501             prout("But if you lower your shields,");
502             proutn("remaining");
503             tpower /= 2;
504         }
505         else
506             proutn("Remaining");
507         prout(" energy will be %.2f.", energy-tpower);
508         if (wfl) {
509             prout("And we will arrive at stardate %.2f.",
510                   game.state.date+ttime);
511         }
512         else if (twarp==1.0)
513             prout("Any warp speed is adequate.");
514         else {
515             prout("Minimum warp needed is %.2f,", twarp);
516             prout("and we will arrive at stardate %.2f.",
517                   game.state.date+ttime);
518         }
519         if (game.state.remtime < ttime)
520             prout("Unfortunately, the Federation will be destroyed by then.");
521         if (twarp > 6.0)
522             prout("You'll be taking risks at that speed, Captain");
523         if ((isatb==1 && game.state.isy == ix1 && game.state.isx == iy1 &&
524              game.future[FSCDBAS]< ttime+game.state.date)||
525             (game.future[FCDBAS]<ttime+game.state.date && baty==ix1 && batx == iy1))
526             prout("The starbase there will be destroyed by then.");
527         proutn("New warp factor to try? ");
528         if (scan() == IHREAL) {
529             wfl = TRUE;
530             twarp = aaitem;
531             if (twarp<1.0 || twarp > 10.0) {
532                 huh();
533                 return;
534             }
535         }
536         else {
537             chew();
538             skip(1);
539             return;
540         }
541     }
542                         
543 }