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