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