merge the autotools branch
[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 (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", 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 (skill > SKILL_FAIR) prout("The Super Commander has %sbeen destroyed.",
62                                   game.state.nscrem?"not ":"");
63     if (game.state.rembase != inbase) {
64         proutn("There ");
65         if (inbase-game.state.rembase==1) proutn("has been 1 base");
66         else {
67             proutn("have been %d bases", inbase-game.state.rembase);
68         }
69         prout(" destroyed, %d remaining.", game.state.rembase);
70     }
71     else prout("There are %d bases.", inbase);
72     if (game.damage[DRADIO] == 0.0 || condit == IHDOCKED || iseenit) {
73         /* Don't report this if not seen and
74            either the radio is dead or not at base! */
75         attakreport(0);
76         iseenit = 1;
77     }
78     if (casual) prout("%d casualt%s suffered so far.",
79                       casual, casual==1? "y" : "ies");
80     if (nhelp) prout("There were %d call%s for help.",
81                      nhelp, nhelp==1 ? "" : "s");
82     if (ship == IHE) {
83         proutn("You have ");
84         if (nprobes) proutn("%d", nprobes);
85         else proutn("no");
86         proutn(" deep space probe");
87         if (nprobes!=1) proutn("s");
88         prout(".");
89     }
90     if ((game.damage[DRADIO] == 0.0 || condit == IHDOCKED)&&
91         game.future[FDSPROB] != FOREVER) {
92         if (isarmed) 
93             proutn("An armed deep space probe is in");
94         else
95             proutn("A deep space probe is in");
96         proutn(cramlc(quadrant, probecx, probecy));
97         prout(".");
98     }
99     if (icrystl) {
100         if (cryprob <= .05)
101             prout("Dilithium crystals aboard ship... not yet used.");
102         else {
103             int i=0;
104             double ai = 0.05;
105             while (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 (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 = quadx-1; x <= quadx+1; x++) {
132         proutn(" ");
133         for (y = quady-1; y <= 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                   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     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 (lastchart < game.state.date && 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 > lastchart)
202         prout("(Last surveillance update %d stardates ago).",
203               (int)(game.state.date-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 == quadx && j == 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 == quadx && j == quady)
223                 proutn(">");
224             else
225                 proutn(" ");
226         }
227         proutn("  |");
228         if (i<GALSIZE) skip(1);
229     }
230 }
231
232 static void sectscan(int goodScan, int i, int j) 
233 {
234     if (goodScan || (abs(i-sectx)<= 1 && abs(j-secty) <= 1)){
235         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)){
236             switch (condit) {
237             case IHRED: textcolor(RED); break;
238             case IHGREEN: textcolor(GREEN); break;
239             case IHYELLOW: textcolor(YELLOW); break;
240             case IHDOCKED: textcolor(CYAN); break;
241             case IHDEAD: textcolor(BROWN);
242             }
243             if (game.quad[i][j] != ship) 
244                 highvideo();
245         }
246         proutn("%c ",game.quad[i][j]);
247         textcolor(DEFAULT);
248     }
249     else
250         proutn("- ");
251 }
252
253 static void status(int req) 
254 {
255     char *cp = NULL;
256     int t, dam = 0;
257     switch (req) {
258     case 1:
259         proutn("Stardate      %.1f, Time Left %.2f", game.state.date, game.state.remtime);
260         break;
261     case 2:
262         if (condit != IHDOCKED) newcnd();
263         switch (condit) {
264         case IHRED: cp = "RED"; break;
265         case IHGREEN: cp = "GREEN"; break;
266         case IHYELLOW: cp = "YELLOW"; break;
267         case IHDOCKED: cp = "DOCKED"; break;
268         case IHDEAD: cp="DEAD"; break;
269         }
270         for (t=0;t<NDEVICES;t++)
271             if (game.damage[t]>0) 
272                 dam++;
273         proutn("Condition     %s, %i DAMAGES", cp, dam);
274         break;
275     case 3:
276         proutn("Position      %d - %d , %d - %d",
277                quadx, quady, sectx, secty);
278         break;
279     case 4:
280         proutn("Life Support  ");
281         if (game.damage[DLIFSUP] != 0.0) {
282             if (condit == IHDOCKED)
283                 proutn("DAMAGED, Base provides");
284             else
285                 proutn("DAMAGED, reserves=%4.2f", lsupres);
286         }
287         else
288             proutn("ACTIVE");
289         break;
290     case 5:
291         proutn("Warp Factor   %.1f", warpfac);
292         break;
293     case 6:
294         proutn("Energy        %.2f", energy);
295         if (icrystl && (game.options & OPTION_SHOWME))  /* ESR */
296             proutn(" (have crystals)");
297         break;
298     case 7:
299         proutn("Torpedoes     %d", torps);
300         break;
301     case 8:
302         proutn("Shields       ");
303         if (game.damage[DSHIELD] != 0)
304             proutn("DAMAGED,");
305         else if (shldup)
306             proutn("UP,");
307         else
308             proutn("DOWN,");
309         proutn(" %d%% %.1f units",
310                (int)((100.0*shield)/inshld + 0.5), shield);
311         break;
312     case 9:
313         proutn("Klingons Left %d", KLINGREM);
314         break;
315     case 10:
316         attakreport(1);
317         break;
318     }
319 }
320                 
321 int srscan(int l) 
322 {
323     static char requests[][3] =
324         {"","da","co","po","ls","wa","en","to","sh","kl","ti"};
325     int leftside=TRUE, rightside=TRUE, i, j, jj, req=0, nn=FALSE;
326     int goodScan=TRUE;
327     switch (l) {
328     case SCAN_FULL: // SRSCAN
329         if (game.damage[DSRSENS] != 0) {
330             /* Allow base's sensors if docked */
331             if (condit != IHDOCKED) {
332                 prout("   S.R. SENSORS DAMAGED!");
333                 goodScan=FALSE;
334             }
335             else
336                 prout("  [Using Base's sensors]");
337         }
338         else prout("     Short-range scan");
339         if (goodScan && !game.damage[DRADIO]) { 
340             game.state.chart[quadx][quady].klingons = game.state.galaxy[quadx][quady].klingons;
341             game.state.chart[quadx][quady].starbase = game.state.galaxy[quadx][quady].starbase;
342             game.state.chart[quadx][quady].stars = game.state.galaxy[quadx][quady].stars;
343             game.state.galaxy[quadx][quady].charted = TRUE;
344         }
345         scan();
346         if (isit("chart")) nn = TRUE;
347         if (isit("no")) rightside = FALSE;
348         chew();
349         prout("    1 2 3 4 5 6 7 8 9 10");
350         break;
351     case SCAN_REQUEST:
352         while (scan() == IHEOL)
353             proutn("Information desired? ");
354         chew();
355         for (req = 1; req <= sizeof(requests)/sizeof(requests[0]); req++)
356             if (strncmp(citem,requests[req],min(2,strlen(citem)))==0)
357                 break;
358         if (req > sizeof(requests)/sizeof(requests[0])) {
359             prout("UNRECOGNIZED REQUEST. Legal requests are:");
360             prout("  date, condition, position, lsupport, warpfactor,");
361             prout("  energy, torpedoes, shields, klingons, time, bases.");
362             return FALSE;
363         }
364         // no break
365     case SCAN_STATUS: // STATUS
366         chew();
367         leftside = FALSE;
368         skip(1);
369         // no break
370     case SCAN_NO_LEFTSIDE: // REQUEST
371         leftside=FALSE;
372         break;
373     }
374     if (condit != IHDOCKED) newcnd();
375     for (i = 1; i <= max(QUADSIZE, sizeof(requests)/sizeof(requests[0])); i++) {
376         jj = (req!=0 ? req : i);
377         if (leftside && i <= QUADSIZE) {
378             proutn("%2d  ", i);
379             for_sectors(j) {
380                 sectscan(goodScan, i, j);
381             }
382         }
383         if (rightside)
384             status(jj);
385         if (i<sizeof(requests)/sizeof(requests[0])) skip(1);
386         if (req!=0) return(goodScan);
387     }
388     prout("");
389     if (nn) chart(1);
390     return(goodScan);
391 }
392                         
393                         
394 void eta(void)
395 {
396     int ix1, ix2, iy1, iy2, prompt=FALSE;
397     int wfl;
398     double ttime, twarp, tpower;
399     if (game.damage[DCOMPTR] != 0.0) {
400         prout("COMPUTER DAMAGED, USE A POCKET CALCULATOR.");
401         skip(1);
402         return;
403     }
404     if (scan() != IHREAL) {
405         prompt = TRUE;
406         chew();
407         proutn("Destination quadrant and/or sector? ");
408         if (scan()!=IHREAL) {
409             huh();
410             return;
411         }
412     }
413     iy1 = aaitem +0.5;
414     if (scan() != IHREAL) {
415         huh();
416         return;
417     }
418     ix1 = aaitem + 0.5;
419     if (scan() == IHREAL) {
420         iy2 = aaitem + 0.5;
421         if (scan() != IHREAL) {
422             huh();
423             return;
424         }
425         ix2 = aaitem + 0.5;
426     }
427     else {
428         if (quady>ix1) ix2 = 1;
429         else ix2=QUADSIZE;
430         if (quadx>iy1) iy2 = 1;
431         else iy2=QUADSIZE;
432     }
433
434     if (!VALID_QUADRANT(ix1, iy1) || !VALID_SECTOR(ix2, iy2)) {
435         huh();
436         return;
437     }
438     dist = sqrt(square(iy1-quadx+0.1*(iy2-sectx))+
439                 square(ix1-quady+0.1*(ix2-secty)));
440     wfl = FALSE;
441
442     if (prompt) prout("Answer \"no\" if you don't know the value:");
443     while (TRUE) {
444         chew();
445         proutn("Time or arrival date? ");
446         if (scan()==IHREAL) {
447             ttime = aaitem;
448             if (ttime > game.state.date) ttime -= game.state.date; // Actually a star date
449             if (ttime <= 1e-10 ||
450                 (twarp=(floor(sqrt((10.0*dist)/ttime)*10.0)+1.0)/10.0) > 10) {
451                 prout("We'll never make it, sir.");
452                 chew();
453                 return;
454             }
455             if (twarp < 1.0) twarp = 1.0;
456             break;
457         }
458         chew();
459         proutn("Warp factor? ");
460         if (scan()== IHREAL) {
461             wfl = TRUE;
462             twarp = aaitem;
463             if (twarp<1.0 || twarp > 10.0) {
464                 huh();
465                 return;
466             }
467             break;
468         }
469         prout("Captain, certainly you can give me one of these.");
470     }
471     while (TRUE) {
472         chew();
473         ttime = (10.0*dist)/square(twarp);
474         tpower = dist*twarp*twarp*twarp*(shldup+1);
475         if (tpower >= energy) {
476             prout("Insufficient energy, sir.");
477             if (shldup==0 || tpower > energy*2.0) {
478                 if (!wfl) return;
479                 proutn("New warp factor to try? ");
480                 if (scan() == IHREAL) {
481                     wfl = TRUE;
482                     twarp = aaitem;
483                     if (twarp<1.0 || twarp > 10.0) {
484                         huh();
485                         return;
486                     }
487                     continue;
488                 }
489                 else {
490                     chew();
491                     skip(1);
492                     return;
493                 }
494             }
495             prout("But if you lower your shields,");
496             proutn("remaining");
497             tpower /= 2;
498         }
499         else
500             proutn("Remaining");
501         prout(" energy will be %.2f.", energy-tpower);
502         if (wfl) {
503             prout("And we will arrive at stardate %.2f.",
504                   game.state.date+ttime);
505         }
506         else if (twarp==1.0)
507             prout("Any warp speed is adequate.");
508         else {
509             prout("Minimum warp needed is %.2f,", twarp);
510             prout("and we will arrive at stardate %.2f.",
511                   game.state.date+ttime);
512         }
513         if (game.state.remtime < ttime)
514             prout("Unfortunately, the Federation will be destroyed by then.");
515         if (twarp > 6.0)
516             prout("You'll be taking risks at that speed, Captain");
517         if ((isatb==1 && game.state.isy == ix1 && game.state.isx == iy1 &&
518              game.future[FSCDBAS]< ttime+game.state.date)||
519             (game.future[FCDBAS]<ttime+game.state.date && baty==ix1 && batx == iy1))
520             prout("The starbase there will be destroyed by then.");
521         proutn("New warp factor to try? ");
522         if (scan() == IHREAL) {
523             wfl = TRUE;
524             twarp = aaitem;
525             if (twarp<1.0 || twarp > 10.0) {
526                 huh();
527                 return;
528             }
529         }
530         else {
531             chew();
532             skip(1);
533             return;
534         }
535     }
536                         
537 }