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