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