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