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