Simplify the starchart code.
[super-star-trek.git] / reports.c
1 #include "sst.h"
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "conio.h"
6
7 void attakreport(int l) {
8      if (!l) {
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         char *s1,*s2,*s3;
33
34         chew();
35         s1 = (thawed?"thawed ":"");
36         switch (length) {
37                 case 1: s2="short"; break;
38                 case 2: s2="medium"; break;
39                 case 4: s2="long"; break;
40                 default: s2="unknown length"; break;
41         }
42         switch (skill) {
43                 case 1: s3="novice"; break;
44                 case 2: s3="fair"; break;
45                 case 3: s3="good"; break;
46                 case 4: s3="expert"; break;
47                 case 5: s3="emeritus"; break;
48                 default: s3="skilled"; break;
49         }
50         prout("");
51         prout("You %s playing a %s%s %s game.",
52                    alldone? "were": "are now", s1, s2, s3);
53         if (skill>3 && thawed && !alldone) prout("No plaque is allowed.");
54         if (tourn) prout("This is tournament game %d.", tourn);
55         prout("Your secret password is \"%s\"",game.passwd);
56         proutn("%d of %d Klingons have been killed",
57                    game.state.killk+game.state.killc+game.state.nsckill, inkling);
58         if (game.state.killc) prout(", including %d Commander%s.", game.state.killc, game.state.killc==1?"":"s");
59         else if (game.state.killk+game.state.nsckill > 0) prout(", but no Commanders.");
60         else prout(".");
61         if (skill > 2) 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] != 1e30) {
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         int x, y;
118         chew();
119         if (game.damage[DLRSENS] != 0.0) {
120                 /* Now allow base's sensors if docked */
121                 if (condit != IHDOCKED) {
122                         prout("LONG-RANGE SENSORS DAMAGED.");
123                         return;
124                 }
125                 proutn("Starbase's long-range scan");
126         }
127         else {
128                 prout("Long-range scan");
129         }
130         for (x = quadx-1; x <= quadx+1; x++) {
131                 proutn(" ");
132                 for (y = quady-1; y <= quady+1; y++) {
133                         if (x == 0 || x > 8 || y == 0 || y > 8)
134                                 proutn("  -1");
135                         else {
136                                 if (game.state.galaxy[x][y]<1000) proutn(" %3d", game.state.galaxy[x][y]);
137                                 else proutn("***");
138                                 game.starch[x][y] = game.damage[DRADIO] > 0 ? game.state.galaxy[x][y]+1000 : 1;
139                         }
140                 }
141                 prout(" ");
142         }
143 }
144
145 void dreprt(void) {
146         int jdam = FALSE, i;
147         chew();
148
149         for (i = 1; i <= NDEVICES; i++) {
150                 if (game.damage[i] > 0.0) {
151                         if (!jdam) {
152                                 prout("DEVICE            -REPAIR TIMES-");
153                                 prout("                IN FLIGHT   DOCKED");
154                                 jdam = TRUE;
155                         }
156                         prout("  %16s %8.2f  %8.2f", 
157                                 device[i],
158                                 game.damage[i]+0.05,
159                                 docfac*game.damage[i]+0.005);
160                 }
161         }
162         if (!jdam) prout("All devices functional.");
163 }
164
165 void chart(int nn) {
166         int i,j;
167         char *cp;
168         chew();
169         if (stdamtim != 1e30 && stdamtim != game.state.date && condit == IHDOCKED) {
170                 proutn("Spock-  \"I revised the Star Chart from the starbase's records.\"\n\r");
171         }
172         if (nn == 0) proutn("       STAR CHART FOR THE KNOWN GALAXY\n\r");
173         if (stdamtim != 1e30) {
174                 if (condit == IHDOCKED) {
175                         /* We are docked, so restore chart from base information */
176                         stdamtim = game.state.date;
177                         for (i=1; i <= 8 ; i++)
178                                 for (j=1; j <= 8; j++)
179                                         if (game.starch[i][j] == 1) game.starch[i][j] = game.state.galaxy[i][j]+1000;
180                 }
181                 else {
182                     proutn("(Last surveillance update %d stardates ago.",
183                            (int)(game.state.date-stdamtim));
184                 }
185         }
186
187         prout("      1    2    3    4    5    6    7    8");
188         for (i = 1; i <= 8; i++) {
189                 proutn("%d |", i);
190                 for (j = 1; j <= 8; j++) {
191                         char buf[4];
192                         proutn("  ");
193                         if (game.starch[i][j] < 0)
194                                 strcpy(buf, ".1.");
195                         else if (game.starch[i][j] == 0)
196                                 strcpy(buf, "...");
197                         else if (game.state.galaxy[i][j]>=1000)
198                                 strcpy(buf, "***");
199                         else
200                                 sprintf(buf, "%03d", game.state.galaxy[i][j]);
201                         for (cp = buf; cp < buf + sizeof(buf); cp++)
202                             if (*cp == '0')
203                                 *cp = '.';
204                         proutn(buf);
205                 }
206                 proutn("  |");
207                 if (i<8) proutn("\n\r");
208         }
209         prout("");      /* flush output */
210 }
211                 
212                 
213 int srscan(int l) {
214         static char requests[][3] =
215                 {"","da","co","po","ls","wa","en","to","sh","kl","ti"};
216         char *cp = NULL;
217         int leftside=TRUE, rightside=TRUE, i, j, jj, k=0, nn=FALSE, t, dam=0;
218         int goodScan=TRUE;
219         switch (l) {
220                 case SCAN_FULL: // SRSCAN
221                         if (game.damage[DSRSENS] != 0) {
222                                 /* Allow base's sensors if docked */
223                                 if (condit != IHDOCKED) {
224                                         prout("   S.R. SENSORS DAMAGED!");
225                                         goodScan=FALSE;
226                                 }
227                                 else
228                                         prout("  [Using Base's sensors]");
229                         }
230                         else proutn("     Short-range scan\n\r");
231                         if (goodScan) game.starch[quadx][quady] = game.damage[DRADIO]>0.0 ? game.state.galaxy[quadx][quady]+1000:1;
232                         scan();
233                         if (isit("chart")) nn = TRUE;
234                         if (isit("no")) rightside = FALSE;
235                         chew();
236                         proutn("    1 2 3 4 5 6 7 8 9 10\n\r");
237                         break;
238                 case SCAN_REQUEST:
239                         while (scan() == IHEOL)
240                                 proutn("Information desired? ");
241                         chew();
242                         for (k = 1; k <= 10; k++)
243                                 if (strncmp(citem,requests[k],min(2,strlen(citem)))==0)
244                                         break;
245                         if (k > 10) {
246                                 prout("UNRECOGNIZED REQUEST. Legal requests are:\n"
247                                          "  date, condition, position, lsupport, warpfactor,\n"
248                                          "  energy, torpedoes, shields, klingons, time.");
249                                 return FALSE;
250                         }
251                         // no "break"
252                 case SCAN_STATUS: // STATUS
253                         chew();
254                         leftside = FALSE;
255                         skip(1);
256                 case SCAN_NO_LEFTSIDE: // REQUEST
257                         leftside=FALSE;
258                         break;
259         }
260         if (condit != IHDOCKED) newcnd();
261         for (i = 1; i <= 10; i++) {
262                 jj = (k!=0 ? k : i);
263                 if (leftside) {
264                         proutn("%2d  ", i);
265                         for (j = 1; j <= 10; j++) {
266                                 if (goodScan || (abs(i-sectx)<= 1 && abs(j-secty) <= 1)){
267                                    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)){
268                                         switch (condit) {
269                                                 case IHRED: textcolor(RED); break;
270                                                 case IHGREEN: textcolor(GREEN); break;
271                                                 case IHYELLOW: textcolor(YELLOW); break;
272                                                 case IHDOCKED: textcolor(LIGHTGRAY); break;
273                                                 case IHDEAD: textcolor(WHITE);
274                                         }
275                                         if (game.quad[i][j]!=ship) highvideo();
276                                    }
277                                    if (game.quad[i][j] & 128) highvideo();
278                                    proutn("%c ",game.quad[i][j] & 127);
279                                    textcolor(LIGHTGRAY);
280                                 }
281                                 else
282                                         proutn("- ");
283                         }
284                 }
285                 if (rightside) {
286                         switch (jj) {
287                                 case 1:
288                                         proutn("Stardate      %.1f, Time Left %.2f", game.state.date, game.state.remtime);
289                                         break;
290                                 case 2:
291                                         if (condit != IHDOCKED) newcnd();
292                                         switch (condit) {
293                                                 case IHRED: cp = "RED"; break;
294                                                 case IHGREEN: cp = "GREEN"; break;
295                                                 case IHYELLOW: cp = "YELLOW"; break;
296                                                 case IHDOCKED: cp = "DOCKED"; break;
297                                                 case IHDEAD: cp="DEAD"; break;
298                                         }
299                                         for (t=0;t<=NDEVICES;t++)
300                                             if (game.damage[t]>0) dam++;
301                                         proutn("Condition     %s, %i DAMAGES", cp, dam);
302                                         break;
303                                 case 3:
304                                         proutn("Position      %d - %d , %d - %d",
305                                             quadx, quady, sectx, secty);
306                                         break;
307                                 case 4:
308                                         proutn("Life Support  ");
309                                         if (game.damage[DLIFSUP] != 0.0) {
310                                                 if (condit == IHDOCKED)
311                                                         proutn("DAMAGED, Base provides");
312                                                 else
313                                                         proutn("DAMAGED, reserves=%4.2f", lsupres);
314                                         }
315                                         else
316                                                 proutn("ACTIVE");
317                                         break;
318                                 case 5:
319                                         proutn("Warp Factor   %.1f", warpfac);
320                                         break;
321                                 case 6:
322                                         proutn("Energy        %.2f", energy);
323                                         break;
324                                 case 7:
325                                         proutn("Torpedoes     %d", torps);
326                                         break;
327                                 case 8:
328                                         proutn("Shields       ");
329                                         if (game.damage[DSHIELD] != 0)
330                                                 proutn("DAMAGED,");
331                                         else if (shldup)
332                                                 proutn("UP,");
333                                         else
334                                                 proutn("DOWN,");
335                                         proutn(" %d%% %.1f units",
336                                                    (int)((100.0*shield)/inshld + 0.5), shield);
337                                         break;
338                                 case 9:
339                                         proutn("Klingons Left %d", game.state.remkl);
340                                         break;
341                                 case 10:
342                                         attakreport(1);
343                                         break;
344                         }
345                 }
346                 if (i<10) proutn("\n\r");
347                 if (k!=0) return(goodScan);
348         }
349         prout("");
350         if (nn) chart(1);
351         return(goodScan);
352 }
353                         
354                         
355 void eta(void) {
356         int ix1, ix2, iy1, iy2, prompt=FALSE;
357         int wfl;
358         double ttime, twarp, tpower;
359         if (game.damage[DCOMPTR] != 0.0) {
360                 prout("COMPUTER DAMAGED, USE A POCKET CALCULATOR.");
361                 skip(1);
362                 return;
363         }
364         if (scan() != IHREAL) {
365                 prompt = TRUE;
366                 chew();
367                 proutn("Destination quadrant and/or sector? ");
368                 if (scan()!=IHREAL) {
369                         huh();
370                         return;
371                 }
372         }
373         iy1 = aaitem +0.5;
374         if (scan() != IHREAL) {
375                 huh();
376                 return;
377         }
378         ix1 = aaitem + 0.5;
379         if (scan() == IHREAL) {
380                 iy2 = aaitem + 0.5;
381                 if (scan() != IHREAL) {
382                         huh();
383                         return;
384                 }
385                 ix2 = aaitem + 0.5;
386         }
387         else {
388                 if (quady>ix1) ix2 = 1;
389                 else ix2=10;
390                 if (quadx>iy1) iy2 = 1;
391                 else iy2=10;
392         }
393
394         if (ix1 > 8 || ix1 < 1 || iy1 > 8 || iy1 < 1 ||
395                 ix2 > 10 || ix2 < 1 || iy2 > 10 || iy2 < 1) {
396                 huh();
397                 return;
398         }
399         dist = sqrt(square(iy1-quadx+0.1*(iy2-sectx))+
400                                 square(ix1-quady+0.1*(ix2-secty)));
401         wfl = FALSE;
402
403         if (prompt) prout("Answer \"no\" if you don't know the value:");
404         while (TRUE) {
405                 chew();
406                 proutn("Time or arrival date? ");
407                 if (scan()==IHREAL) {
408                         ttime = aaitem;
409                         if (ttime > game.state.date) ttime -= game.state.date; // Actually a star date
410                         if (ttime <= 1e-10 ||
411                                 (twarp=(floor(sqrt((10.0*dist)/ttime)*10.0)+1.0)/10.0) > 10) {
412                                 prout("We'll never make it, sir.");
413                                 chew();
414                                 return;
415                         }
416                         if (twarp < 1.0) twarp = 1.0;
417                         break;
418                 }
419                 chew();
420                 proutn("Warp factor? ");
421                 if (scan()== IHREAL) {
422                         wfl = TRUE;
423                         twarp = aaitem;
424                         if (twarp<1.0 || twarp > 10.0) {
425                                 huh();
426                                 return;
427                         }
428                         break;
429                 }
430                 prout("Captain, certainly you can give me one of these.");
431         }
432         while (TRUE) {
433                 chew();
434                 ttime = (10.0*dist)/square(twarp);
435                 tpower = dist*twarp*twarp*twarp*(shldup+1);
436                 if (tpower >= energy) {
437                         prout("Insufficient energy, sir.");
438                         if (shldup==0 || tpower > energy*2.0) {
439                                 if (!wfl) return;
440                                 proutn("New warp factor to try? ");
441                                 if (scan() == IHREAL) {
442                                         wfl = TRUE;
443                                         twarp = aaitem;
444                                         if (twarp<1.0 || twarp > 10.0) {
445                                                 huh();
446                                                 return;
447                                         }
448                                         continue;
449                                 }
450                                 else {
451                                         chew();
452                                         skip(1);
453                                         return;
454                                 }
455                         }
456                         prout("But if you lower your shields,");
457                         proutn("remaining");
458                         tpower /= 2;
459                 }
460                 else
461                         proutn("Remaining");
462                 prout(" energy will be %.2f.", energy-tpower);
463                 if (wfl) {
464                         prout("And we will arrive at stardate %.2f.",
465                                 game.state.date+ttime);
466                 }
467                 else if (twarp==1.0)
468                         prout("Any warp speed is adequate.");
469                 else {
470                         prout("Minimum warp needed is %.2f,", twarp);
471                         prout("and we will arrive at stardate %.2f.",
472                                 game.state.date+ttime);
473                 }
474                 if (game.state.remtime < ttime)
475                         prout("Unfortunately, the Federation will be destroyed by then.");
476                 if (twarp > 6.0)
477                         prout("You'll be taking risks at that speed, Captain");
478                 if ((isatb==1 && game.state.isy == ix1 && game.state.isx == iy1 &&
479                          game.future[FSCDBAS]< ttime+game.state.date)||
480                         (game.future[FCDBAS]<ttime+game.state.date && baty==ix1 && batx == iy1))
481                         prout("The starbase there will be destroyed by then.");
482                 proutn("New warp factor to try? ");
483                 if (scan() == IHREAL) {
484                         wfl = TRUE;
485                         twarp = aaitem;
486                         if (twarp<1.0 || twarp > 10.0) {
487                                 huh();
488                                 return;
489                         }
490                 }
491                 else {
492                         chew();
493                         skip(1);
494                         return;
495                 }
496         }
497                         
498 }