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