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