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