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