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