Incorporate BSD-Trek visual-scan code, just in case.
[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.condition == docked || 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 = true;
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.condition == docked)
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.condition != docked) {
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.condition == docked) {
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.condition) {
245             case red: textcolor(RED); break;
246             case green: textcolor(GREEN); break;
247             case yellow: textcolor(YELLOW); break;
248             case docked: textcolor(CYAN); break;
249             case dead: 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.condition != docked) newcnd();
272         switch (game.condition) {
273         case red: cp = "RED"; break;
274         case green: cp = "GREEN"; break;
275         case yellow: cp = "YELLOW"; break;
276         case docked: cp = "DOCKED"; break;
277         case dead: 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.condition == docked)
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 void srscan(scantype type)
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 (type) {
350     case SCAN_FULL: // SRSCAN
351         if (damaged(DSRSENS)) {
352             /* Allow base's sensors if docked */
353             if (game.condition != docked) {
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;
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.condition != docked) 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;
409     }
410     prout("");
411     if (title) chart(true);
412 }
413                         
414                         
415 void eta(void)
416 /* use computer to get estimated time of arrival for a warp jump */
417 {
418     coord w1, w2;
419     bool wfl, prompt = false;
420     double ttime, twarp, tpower;
421     if (damaged(DCOMPTR)) {
422         prout(_("COMPUTER DAMAGED, USE A POCKET CALCULATOR."));
423         skip(1);
424         return;
425     }
426     if (scan() != IHREAL) {
427         prompt = true;
428         chew();
429         proutn(_("Destination quadrant and/or sector? "));
430         if (scan()!=IHREAL) {
431             huh();
432             return;
433         }
434     }
435     w1.y = aaitem +0.5;
436     if (scan() != IHREAL) {
437         huh();
438         return;
439     }
440     w1.x = aaitem + 0.5;
441     if (scan() == IHREAL) {
442         w2.y = aaitem + 0.5;
443         if (scan() != IHREAL) {
444             huh();
445             return;
446         }
447         w2.x = aaitem + 0.5;
448     }
449     else {
450         if (game.quadrant.y>w1.x) w2.x = 1;
451         else w2.x=QUADSIZE;
452         if (game.quadrant.x>w1.y) w2.y = 1;
453         else w2.y=QUADSIZE;
454     }
455
456     if (!VALID_QUADRANT(w1.x, w1.y) || !VALID_SECTOR(w2.x, w2.y)) {
457         huh();
458         return;
459     }
460     game.dist = sqrt(square(w1.y-game.quadrant.y+0.1*(w2.y-game.sector.y))+
461                 square(w1.x-game.quadrant.x+0.1*(w2.x-game.sector.x)));
462     wfl = false;
463
464     if (prompt) prout(_("Answer \"no\" if you don't know the value:"));
465     for (;;) {
466         chew();
467         proutn(_("Time or arrival date? "));
468         if (scan()==IHREAL) {
469             ttime = aaitem;
470             if (ttime > game.state.date) ttime -= game.state.date; // Actually a star date
471             if (ttime <= 1e-10 ||
472                 (twarp=(floor(sqrt((10.0*game.dist)/ttime)*10.0)+1.0)/10.0) > 10) {
473                 prout(_("We'll never make it, sir."));
474                 chew();
475                 return;
476             }
477             if (twarp < 1.0) twarp = 1.0;
478             break;
479         }
480         chew();
481         proutn(_("Warp factor? "));
482         if (scan()== IHREAL) {
483             wfl = true;
484             twarp = aaitem;
485             if (twarp<1.0 || twarp > 10.0) {
486                 huh();
487                 return;
488             }
489             break;
490         }
491         prout(_("Captain, certainly you can give me one of these."));
492     }
493     for (;;) {
494         chew();
495         ttime = (10.0*game.dist)/square(twarp);
496         tpower = game.dist*twarp*twarp*twarp*(game.shldup+1);
497         if (tpower >= game.energy) {
498             prout(_("Insufficient energy, sir."));
499             if (!game.shldup || tpower > game.energy*2.0) {
500                 if (!wfl) return;
501                 proutn(_("New warp factor to try? "));
502                 if (scan() == IHREAL) {
503                     wfl = true;
504                     twarp = aaitem;
505                     if (twarp<1.0 || twarp > 10.0) {
506                         huh();
507                         return;
508                     }
509                     continue;
510                 }
511                 else {
512                     chew();
513                     skip(1);
514                     return;
515                 }
516             }
517             prout(_("But if you lower your shields,"));
518             proutn(_("remaining"));
519             tpower /= 2;
520         }
521         else
522             proutn(_("Remaining"));
523         prout(_(" energy will be %.2f."), game.energy-tpower);
524         if (wfl) {
525             prout(_("And we will arrive at stardate %.2f."),
526                   game.state.date+ttime);
527         }
528         else if (twarp==1.0)
529             prout(_("Any warp speed is adequate."));
530         else {
531             prout(_("Minimum warp needed is %.2f,"), twarp);
532             prout(_("and we will arrive at stardate %.2f."),
533                   game.state.date+ttime);
534         }
535         if (game.state.remtime < ttime)
536             prout(_("Unfortunately, the Federation will be destroyed by then."));
537         if (twarp > 6.0)
538             prout(_("You'll be taking risks at that speed, Captain"));
539         if ((game.isatb==1 && same(game.state.kscmdr, w1) &&
540              scheduled(FSCDBAS)< ttime+game.state.date)||
541             (scheduled(FCDBAS)<ttime+game.state.date && same(game.battle, w1)))
542             prout(_("The starbase there will be destroyed by then."));
543         proutn(_("New warp factor to try? "));
544         if (scan() == IHREAL) {
545             wfl = true;
546             twarp = aaitem;
547             if (twarp<1.0 || twarp > 10.0) {
548                 huh();
549                 return;
550             }
551         }
552         else {
553             chew();
554             skip(1);
555             return;
556         }
557     }
558                         
559 }
560
561 #if BSD_BUG_FOR_BUG
562 /*
563  *      A visual scan is made in a particular direction of three sectors
564  *      in the general direction specified.  This takes time, and
565  *      Klingons can attack you, so it should be done only when sensors
566  *      are out.  Code swiped from BSD-Trek.  Not presently used, as we
567  *      automatically display all adjacent sectors on the short-range
568  *      scan even when short-range sensors are out.
569  */
570
571 /* This struct[] has the delta x, delta y for particular directions */
572 coord visdelta[] =
573 {
574     {-1,-1},
575     {-1, 0},
576     {-1, 1},
577     {0,  1},
578     {1,  1},
579     {1,  0},
580     {1, -1},
581     {0, -1},
582     {-1,-1},
583     {-1, 0},
584     {-1, 1},
585 };
586
587 void visual(void)
588 {
589     int         co, ix, iy;
590     coord       *v;
591
592     if (scan() != IHREAL) {
593         chew();
594         proutn(_("Direction? "));
595         if (scan()!=IHREAL) {
596             huh();
597             return;
598         }
599     }
600     if (aaitem < 0.0 || aaitem > 360.0)
601         return;
602     co = (aaitem + 22) / 45;
603     v = &visdelta[co];
604     ix = game.sector.x + v->x;
605     iy = game.sector.y + v->y;
606     if (ix < 0 || ix >= QUADSIZE || iy < 0 || iy >= QUADSIZE)
607         co = '?';
608     else
609         co = game.quad[ix][iy];
610     printf("%d,%d %c ", ix, iy, co);
611     v++;
612     ix = game.sector.x + v->x;
613     iy = game.sector.y + v->y;
614     if (ix < 0 || ix >= QUADSIZE || iy < 0 || iy >= QUADSIZE)
615         co = '?';
616     else
617         co = game.quad[ix][iy];
618     printf("%c ", co);
619     v++;
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("%c %d,%d\n", co, ix, iy);
627     game.optime = 0.5;
628     game.ididit = true;
629 }
630 #endif