Get rid of FORTRANisms.
[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] < 1e30) {
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] < 1e30)
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] != 1e30) {
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 (x == 0 || x > GALSIZE || y == 0 || y > GALSIZE)
136                 proutn("  -1");
137             else {
138                 if (game.state.galaxy[x][y]<SUPERNOVA_PLACE) proutn(" %3d", game.state.galaxy[x][y]);
139                 else proutn("***");
140                 game.starch[x][y] = game.damage[DRADIO] > 0 ? game.state.galaxy[x][y]+SUPERNOVA_PLACE : 1;
141             }
142         }
143         prout(" ");
144     }
145 }
146
147 void dreprt(void) 
148 {
149     int jdam = FALSE, i;
150     chew();
151
152     for (i = 1; i <= NDEVICES; i++) {
153         if (game.damage[i] > 0.0) {
154             if (!jdam) {
155                 prout("DEVICE            -REPAIR TIMES-");
156                 prout("                IN FLIGHT   DOCKED");
157                 jdam = TRUE;
158             }
159             prout("  %16s %8.2f  %8.2f", 
160                   device[i],
161                   game.damage[i]+0.05,
162                   docfac*game.damage[i]+0.005);
163         }
164     }
165     if (!jdam) prout("All devices functional.");
166 }
167
168 void chart(int nn) 
169 {
170     int i,j;
171     char *cp;
172     chew();
173     if (stdamtim != 1e30 && stdamtim != game.state.date && condit == IHDOCKED) {
174         proutn("Spock-  \"I revised the Star Chart from the starbase's records.\"\n\r");
175     }
176     if (nn == 0) proutn("       STAR CHART FOR THE KNOWN GALAXY\n\r");
177     if (stdamtim != 1e30) {
178         if (condit == IHDOCKED) {
179             /* We are docked, so restore chart from base information */
180             stdamtim = game.state.date;
181             for (i=1; i <= GALSIZE ; i++)
182                 for (j=1; j <= GALSIZE; j++)
183                     if (game.starch[i][j] == 1) game.starch[i][j] = game.state.galaxy[i][j]+SUPERNOVA_PLACE;
184         }
185         else {
186             proutn("(Last surveillance update %d stardates ago.",
187                    (int)(game.state.date-stdamtim));
188         }
189     }
190
191     prout("      1    2    3    4    5    6    7    8");
192     for (i = 1; i <= GALSIZE; i++) {
193         proutn("%d |", i);
194         for (j = 1; j <= GALSIZE; j++) {
195             char buf[4];
196             proutn("  ");
197             if (game.starch[i][j] == CHART_UNKNOWN)
198                 strcpy(buf, ".1.");
199             else if (game.starch[i][j] == 0)
200                 strcpy(buf, "...");
201             else if (game.state.galaxy[i][j]>=SUPERNOVA_PLACE)
202                 strcpy(buf, "***");
203             else
204                 sprintf(buf, "%03d", game.state.galaxy[i][j]);
205             for (cp = buf; cp < buf + sizeof(buf); cp++)
206                 if (*cp == '0')
207                     *cp = '.';
208             proutn(buf);
209         }
210         proutn("  |");
211         if (i<GALSIZE) proutn("\n\r");
212     }
213     prout("");  /* flush output */
214 }
215
216 static void sectscan(int goodScan, int i, int j) 
217 {
218     if (goodScan || (abs(i-sectx)<= 1 && abs(j-secty) <= 1)){
219         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)){
220             switch (condit) {
221             case IHRED: textcolor(RED); break;
222             case IHGREEN: textcolor(GREEN); break;
223             case IHYELLOW: textcolor(YELLOW); break;
224             case IHDOCKED: textcolor(CYAN); break;
225             case IHDEAD: textcolor(BROWN);
226             }
227             if (game.quad[i][j] != ship) 
228                 highvideo();
229         }
230         if (game.quad[i][j] & DAMAGED) 
231             highvideo();
232         proutn("%c ",game.quad[i][j] & ~DAMAGED);
233         textcolor(DEFAULT);
234     }
235     else
236         proutn("- ");
237 }
238
239 static void status(int req) 
240 {
241     char *cp = NULL;
242     int t, dam = 0;
243     switch (req) {
244     case 1:
245         proutn("Stardate      %.1f, Time Left %.2f", game.state.date, game.state.remtime);
246         break;
247     case 2:
248         if (condit != IHDOCKED) newcnd();
249         switch (condit) {
250         case IHRED: cp = "RED"; break;
251         case IHGREEN: cp = "GREEN"; break;
252         case IHYELLOW: cp = "YELLOW"; break;
253         case IHDOCKED: cp = "DOCKED"; break;
254         case IHDEAD: cp="DEAD"; break;
255         }
256         for (t=0;t<=NDEVICES;t++)
257             if (game.damage[t]>0) dam++;
258         proutn("Condition     %s, %i DAMAGES", cp, dam);
259         break;
260     case 3:
261         proutn("Position      %d - %d , %d - %d",
262                quadx, quady, sectx, secty);
263         break;
264     case 4:
265         proutn("Life Support  ");
266         if (game.damage[DLIFSUP] != 0.0) {
267             if (condit == IHDOCKED)
268                 proutn("DAMAGED, Base provides");
269             else
270                 proutn("DAMAGED, reserves=%4.2f", lsupres);
271         }
272         else
273             proutn("ACTIVE");
274         break;
275     case 5:
276         proutn("Warp Factor   %.1f", warpfac);
277         break;
278     case 6:
279         proutn("Energy        %.2f", energy);
280         if (icrystl)    /* ESR */
281             proutn(" (have crystals)");
282         break;
283     case 7:
284         proutn("Torpedoes     %d", torps);
285         break;
286     case 8:
287         proutn("Shields       ");
288         if (game.damage[DSHIELD] != 0)
289             proutn("DAMAGED,");
290         else if (shldup)
291             proutn("UP,");
292         else
293             proutn("DOWN,");
294         proutn(" %d%% %.1f units",
295                (int)((100.0*shield)/inshld + 0.5), shield);
296         break;
297     case 9:
298         proutn("Klingons Left %d", game.state.remkl);
299         break;
300     case 10:
301         attakreport(1);
302         break;
303         /*
304          * Note: attakreport() can in some cases produce two lines of
305          * output.  If that happens, and QUADSIZE is the normal 10, items
306          * 11 and up will be printed past the bottom of the quadrant display.
307          * Under the curses display logic they will get lost because they're
308          * written outside the report window.
309          */
310     case 11:    /* ESR */
311         proutn("Bases Left    %d", game.state.rembase);
312         break;
313     }
314 }
315                 
316 int srscan(int l) 
317 {
318     static char requests[][3] =
319         {"","da","co","po","ls","wa","en","to","sh","kl","ti", "ba"};
320     int leftside=TRUE, rightside=TRUE, i, j, jj, req=0, nn=FALSE;
321     int goodScan=TRUE;
322     switch (l) {
323     case SCAN_FULL: // SRSCAN
324         if (game.damage[DSRSENS] != 0) {
325             /* Allow base's sensors if docked */
326             if (condit != IHDOCKED) {
327                 prout("   S.R. SENSORS DAMAGED!");
328                 goodScan=FALSE;
329             }
330             else
331                 prout("  [Using Base's sensors]");
332         }
333         else proutn("     Short-range scan\n\r");
334         if (goodScan) game.starch[quadx][quady] = game.damage[DRADIO]>0.0 ? game.state.galaxy[quadx][quady]+SUPERNOVA_PLACE:1;
335         scan();
336         if (isit("chart")) nn = TRUE;
337         if (isit("no")) rightside = FALSE;
338         chew();
339         proutn("    1 2 3 4 5 6 7 8 9 10\n\r");
340         break;
341     case SCAN_REQUEST:
342         while (scan() == IHEOL)
343             proutn("Information desired? ");
344         chew();
345         for (req = 1; req <= sizeof(requests)/sizeof(requests[0]); req++)
346             if (strncmp(citem,requests[req],min(2,strlen(citem)))==0)
347                 break;
348         if (req > sizeof(requests)/sizeof(requests[0])) {
349             prout("UNRECOGNIZED REQUEST. Legal requests are:\n"
350                   "  date, condition, position, lsupport, warpfactor,\n"
351                   "  energy, torpedoes, shields, klingons, time, bases.");
352             return FALSE;
353         }
354         // no break
355     case SCAN_STATUS: // STATUS
356         chew();
357         leftside = FALSE;
358         skip(1);
359         // no break
360     case SCAN_NO_LEFTSIDE: // REQUEST
361         leftside=FALSE;
362         break;
363     }
364     if (condit != IHDOCKED) newcnd();
365     for (i = 1; i <= max(QUADSIZE, sizeof(requests)/sizeof(requests[0])); i++) {
366         jj = (req!=0 ? req : i);
367         if (leftside && i <= QUADSIZE) {
368             proutn("%2d  ", i);
369             for (j = 1; j <= QUADSIZE; j++) {
370                 sectscan(goodScan, i, j);
371             }
372         }
373         if (rightside)
374             status(jj);
375         if (i<sizeof(requests)/sizeof(requests[0])) proutn("\n\r");
376         if (req!=0) return(goodScan);
377     }
378     prout("");
379     if (nn) chart(1);
380     return(goodScan);
381 }
382                         
383                         
384 void eta(void)
385 {
386     int ix1, ix2, iy1, iy2, prompt=FALSE;
387     int wfl;
388     double ttime, twarp, tpower;
389     if (game.damage[DCOMPTR] != 0.0) {
390         prout("COMPUTER DAMAGED, USE A POCKET CALCULATOR.");
391         skip(1);
392         return;
393     }
394     if (scan() != IHREAL) {
395         prompt = TRUE;
396         chew();
397         proutn("Destination quadrant and/or sector? ");
398         if (scan()!=IHREAL) {
399             huh();
400             return;
401         }
402     }
403     iy1 = aaitem +0.5;
404     if (scan() != IHREAL) {
405         huh();
406         return;
407     }
408     ix1 = aaitem + 0.5;
409     if (scan() == IHREAL) {
410         iy2 = aaitem + 0.5;
411         if (scan() != IHREAL) {
412             huh();
413             return;
414         }
415         ix2 = aaitem + 0.5;
416     }
417     else {
418         if (quady>ix1) ix2 = 1;
419         else ix2=QUADSIZE;
420         if (quadx>iy1) iy2 = 1;
421         else iy2=QUADSIZE;
422     }
423
424     if (ix1 > GALSIZE || ix1 < 1 || iy1 > GALSIZE || iy1 < 1 ||
425         ix2 > QUADSIZE || ix2 < 1 || iy2 > QUADSIZE || iy2 < 1) {
426         huh();
427         return;
428     }
429     dist = sqrt(square(iy1-quadx+0.1*(iy2-sectx))+
430                 square(ix1-quady+0.1*(ix2-secty)));
431     wfl = FALSE;
432
433     if (prompt) prout("Answer \"no\" if you don't know the value:");
434     while (TRUE) {
435         chew();
436         proutn("Time or arrival date? ");
437         if (scan()==IHREAL) {
438             ttime = aaitem;
439             if (ttime > game.state.date) ttime -= game.state.date; // Actually a star date
440             if (ttime <= 1e-10 ||
441                 (twarp=(floor(sqrt((10.0*dist)/ttime)*10.0)+1.0)/10.0) > 10) {
442                 prout("We'll never make it, sir.");
443                 chew();
444                 return;
445             }
446             if (twarp < 1.0) twarp = 1.0;
447             break;
448         }
449         chew();
450         proutn("Warp factor? ");
451         if (scan()== IHREAL) {
452             wfl = TRUE;
453             twarp = aaitem;
454             if (twarp<1.0 || twarp > 10.0) {
455                 huh();
456                 return;
457             }
458             break;
459         }
460         prout("Captain, certainly you can give me one of these.");
461     }
462     while (TRUE) {
463         chew();
464         ttime = (10.0*dist)/square(twarp);
465         tpower = dist*twarp*twarp*twarp*(shldup+1);
466         if (tpower >= energy) {
467             prout("Insufficient energy, sir.");
468             if (shldup==0 || tpower > energy*2.0) {
469                 if (!wfl) return;
470                 proutn("New warp factor to try? ");
471                 if (scan() == IHREAL) {
472                     wfl = TRUE;
473                     twarp = aaitem;
474                     if (twarp<1.0 || twarp > 10.0) {
475                         huh();
476                         return;
477                     }
478                     continue;
479                 }
480                 else {
481                     chew();
482                     skip(1);
483                     return;
484                 }
485             }
486             prout("But if you lower your shields,");
487             proutn("remaining");
488             tpower /= 2;
489         }
490         else
491             proutn("Remaining");
492         prout(" energy will be %.2f.", energy-tpower);
493         if (wfl) {
494             prout("And we will arrive at stardate %.2f.",
495                   game.state.date+ttime);
496         }
497         else if (twarp==1.0)
498             prout("Any warp speed is adequate.");
499         else {
500             prout("Minimum warp needed is %.2f,", twarp);
501             prout("and we will arrive at stardate %.2f.",
502                   game.state.date+ttime);
503         }
504         if (game.state.remtime < ttime)
505             prout("Unfortunately, the Federation will be destroyed by then.");
506         if (twarp > 6.0)
507             prout("You'll be taking risks at that speed, Captain");
508         if ((isatb==1 && game.state.isy == ix1 && game.state.isx == iy1 &&
509              game.future[FSCDBAS]< ttime+game.state.date)||
510             (game.future[FCDBAS]<ttime+game.state.date && baty==ix1 && batx == iy1))
511             prout("The starbase there will be destroyed by then.");
512         proutn("New warp factor to try? ");
513         if (scan() == IHREAL) {
514             wfl = TRUE;
515             twarp = aaitem;
516             if (twarp<1.0 || twarp > 10.0) {
517                 huh();
518                 return;
519             }
520         }
521         else {
522             chew();
523             skip(1);
524             return;
525         }
526     }
527                         
528 }