b2ee2b7d2bd04e0d1429812aa68eca380e8f82a3
[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.battle));
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.kscmdr));
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.battle.x, game.battle.y, scheduled(FCDBAS));
24         if (game.isatb)
25             proutn("Base in %i - %i attacked by S. Alive until %.1f", game.state.kscmdr.x, game.state.kscmdr.y, 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 (!damaged(DRADIO) || 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 ((!damaged(DRADIO) || 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.probec));
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 (damaged(DLRSENS)) {
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.quadrant.x-1; x <= game.quadrant.x+1; x++) {
132         proutn(" ");
133         for (y = game.quadrant.y-1; y <= game.quadrant.y+1; y++) {
134             if (!VALID_QUADRANT(x, y))
135                 proutn("  -1");
136             else {
137                 if (!damaged(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     bool jdam = false;
155     int i;
156     chew();
157
158     for (i = 0; i < NDEVICES; i++) {
159         if (damaged(i)) {
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                   game.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     game.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 (!damaged(DRADIO))
194         rechart();
195
196     if (game.lastchart < game.state.date && game.condit == IHDOCKED) {
197         prout("Spock-  \"I revised the Star Chart from the starbase's records.\"");
198         rechart();
199     }
200
201     if (nn == 0) prout("       STAR CHART FOR THE KNOWN GALAXY");
202     if (game.state.date > game.lastchart)
203         prout("(Last surveillance update %d stardates ago).",
204               (int)(game.state.date-game.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 == game.quadrant.x && j == game.quadrant.y)
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 == game.quadrant.x && j == game.quadrant.y)
224                 proutn(">");
225             else
226                 proutn(" ");
227         }
228         proutn("  |");
229         if (i<GALSIZE) skip(1);
230     }
231     prout("");
232 }
233
234 static void sectscan(int goodScan, int i, int j) 
235 {
236     if (goodScan || (abs(i-game.sector.x)<= 1 && abs(j-game.sector.y) <= 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 (game.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] != game.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 (game.condit != IHDOCKED) newcnd();
265         switch (game.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                game.quadrant.x, game.quadrant.y, game.sector.x, game.sector.y);
280         break;
281     case 4:
282         proutn("Life Support  ");
283         if (damaged(DLIFSUP)) {
284             if (game.condit == IHDOCKED)
285                 proutn("DAMAGED, Base provides");
286             else
287                 proutn("DAMAGED, reserves=%4.2f", game.lsupres);
288         }
289         else
290             proutn("ACTIVE");
291         break;
292     case 5:
293         proutn("Warp Factor   %.1f", game.warpfac);
294         break;
295     case 6:
296         proutn("Energy        %.2f", game.energy);
297         if (game.icrystl && (game.options & OPTION_SHOWME))     /* ESR */
298             proutn(" (have crystals)");
299         break;
300     case 7:
301         proutn("Torpedoes     %d", game.torps);
302         break;
303     case 8:
304         proutn("Shields       ");
305         if (damaged(DSHIELD))
306             proutn("DAMAGED,");
307         else if (game.shldup)
308             proutn("UP,");
309         else
310             proutn("DOWN,");
311         proutn(" %d%% %.1f units",
312                (int)((100.0*game.shield)/game.inshld + 0.5), game.shield);
313         break;
314     case 9:
315         proutn("Klingons Left %d", KLINGREM);
316         break;
317     case 10:
318         if (game.options & OPTION_WORLDS) {
319             int here = game.state.galaxy[game.quadrant.x][game.quadrant.y].planet;
320             if (here != NOPLANET && game.state.plnets[here].inhabited != UNINHABITED)
321                 proutn("Major system  %s", systemname(here));
322             else
323                 proutn("Sector is uninhabited");
324         }
325
326         break;
327     case 11:
328         attakreport(1);
329         break;
330     }
331 }
332                 
333 int srscan(int l) 
334 {
335     /* the "sy" request is undocumented */
336     static char requests[][3] =
337         {"","da","co","po","ls","wa","en","to","sh","kl","sy", "ti"};
338     
339     int i, j, jj, req=0;
340     int goodScan=true, leftside=true, rightside=true, nn=false; 
341     switch (l) {
342     case SCAN_FULL: // SRSCAN
343         if (damaged(DSRSENS)) {
344             /* Allow base's sensors if docked */
345             if (game.condit != IHDOCKED) {
346                 prout("   S.R. SENSORS DAMAGED!");
347                 goodScan=false;
348             }
349             else
350                 prout("  [Using Base's sensors]");
351         }
352         else prout("     Short-range scan");
353         if (goodScan && !damaged(DRADIO)) { 
354             game.state.chart[game.quadrant.x][game.quadrant.y].klingons = game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons;
355             game.state.chart[game.quadrant.x][game.quadrant.y].starbase = game.state.galaxy[game.quadrant.x][game.quadrant.y].starbase;
356             game.state.chart[game.quadrant.x][game.quadrant.y].stars = game.state.galaxy[game.quadrant.x][game.quadrant.y].stars;
357             game.state.galaxy[game.quadrant.x][game.quadrant.y].charted = true;
358         }
359         scan();
360         if (isit("chart")) nn = true;
361         if (isit("no")) rightside = false;
362         chew();
363         prout("    1 2 3 4 5 6 7 8 9 10");
364         break;
365     case SCAN_REQUEST:
366         while (scan() == IHEOL)
367             proutn("Information desired? ");
368         chew();
369         for (req = 1; req <= sizeof(requests)/sizeof(requests[0]); req++)
370             if (strncmp(citem,requests[req],min(2,strlen(citem)))==0)
371                 break;
372         if (req > sizeof(requests)/sizeof(requests[0])) {
373             prout("UNRECOGNIZED REQUEST. Legal requests are:");
374             prout("  date, condition, position, lsupport, warpfactor,");
375             prout("  energy, torpedoes, shields, klingons, time, system, bases.");
376             return false;
377         }
378         // no break
379     case SCAN_STATUS: // STATUS
380         chew();
381         leftside = false;
382         skip(1);
383         // no break
384     case SCAN_NO_LEFTSIDE: // REQUEST
385         leftside=false;
386         break;
387     }
388     if (game.condit != IHDOCKED) newcnd();
389     for (i = 1; i <= max(QUADSIZE, sizeof(requests)/sizeof(requests[0])); i++) {
390         jj = (req!=0 ? req : i);
391         if (leftside && i <= QUADSIZE) {
392             proutn("%2d  ", i);
393             for_sectors(j) {
394                 sectscan(goodScan, i, j);
395             }
396         }
397         if (rightside)
398             status(jj);
399         if (i<sizeof(requests)/sizeof(requests[0])) skip(1);
400         if (req!=0) return(goodScan);
401     }
402     prout("");
403     if (nn) chart(1);
404     return(goodScan);
405 }
406                         
407                         
408 void eta(void)
409 {
410     int ix1, ix2, iy1, iy2;
411     bool wfl, prompt = false;
412     double ttime, twarp, tpower;
413     if (damaged(DCOMPTR)) {
414         prout("COMPUTER DAMAGED, USE A POCKET CALCULATOR.");
415         skip(1);
416         return;
417     }
418     if (scan() != IHREAL) {
419         prompt = true;
420         chew();
421         proutn("Destination quadrant and/or sector? ");
422         if (scan()!=IHREAL) {
423             huh();
424             return;
425         }
426     }
427     iy1 = aaitem +0.5;
428     if (scan() != IHREAL) {
429         huh();
430         return;
431     }
432     ix1 = aaitem + 0.5;
433     if (scan() == IHREAL) {
434         iy2 = aaitem + 0.5;
435         if (scan() != IHREAL) {
436             huh();
437             return;
438         }
439         ix2 = aaitem + 0.5;
440     }
441     else {
442         if (game.quadrant.y>ix1) ix2 = 1;
443         else ix2=QUADSIZE;
444         if (game.quadrant.x>iy1) iy2 = 1;
445         else iy2=QUADSIZE;
446     }
447
448     if (!VALID_QUADRANT(ix1, iy1) || !VALID_SECTOR(ix2, iy2)) {
449         huh();
450         return;
451     }
452     game.dist = sqrt(square(iy1-game.quadrant.x+0.1*(iy2-game.sector.x))+
453                 square(ix1-game.quadrant.y+0.1*(ix2-game.sector.y)));
454     wfl = false;
455
456     if (prompt) prout("Answer \"no\" if you don't know the value:");
457     for (;;) {
458         chew();
459         proutn("Time or arrival date? ");
460         if (scan()==IHREAL) {
461             ttime = aaitem;
462             if (ttime > game.state.date) ttime -= game.state.date; // Actually a star date
463             if (ttime <= 1e-10 ||
464                 (twarp=(floor(sqrt((10.0*game.dist)/ttime)*10.0)+1.0)/10.0) > 10) {
465                 prout("We'll never make it, sir.");
466                 chew();
467                 return;
468             }
469             if (twarp < 1.0) twarp = 1.0;
470             break;
471         }
472         chew();
473         proutn("Warp factor? ");
474         if (scan()== IHREAL) {
475             wfl = true;
476             twarp = aaitem;
477             if (twarp<1.0 || twarp > 10.0) {
478                 huh();
479                 return;
480             }
481             break;
482         }
483         prout("Captain, certainly you can give me one of these.");
484     }
485     for (;;) {
486         chew();
487         ttime = (10.0*game.dist)/square(twarp);
488         tpower = game.dist*twarp*twarp*twarp*(game.shldup+1);
489         if (tpower >= game.energy) {
490             prout("Insufficient energy, sir.");
491             if (!game.shldup || tpower > game.energy*2.0) {
492                 if (!wfl) return;
493                 proutn("New warp factor to try? ");
494                 if (scan() == IHREAL) {
495                     wfl = true;
496                     twarp = aaitem;
497                     if (twarp<1.0 || twarp > 10.0) {
498                         huh();
499                         return;
500                     }
501                     continue;
502                 }
503                 else {
504                     chew();
505                     skip(1);
506                     return;
507                 }
508             }
509             prout("But if you lower your shields,");
510             proutn("remaining");
511             tpower /= 2;
512         }
513         else
514             proutn("Remaining");
515         prout(" game.energy will be %.2f.", game.energy-tpower);
516         if (wfl) {
517             prout("And we will arrive at stardate %.2f.",
518                   game.state.date+ttime);
519         }
520         else if (twarp==1.0)
521             prout("Any warp speed is adequate.");
522         else {
523             prout("Minimum warp needed is %.2f,", twarp);
524             prout("and we will arrive at stardate %.2f.",
525                   game.state.date+ttime);
526         }
527         if (game.state.remtime < ttime)
528             prout("Unfortunately, the Federation will be destroyed by then.");
529         if (twarp > 6.0)
530             prout("You'll be taking risks at that speed, Captain");
531         if ((game.isatb==1 && game.state.kscmdr.y == iy1 && game.state.kscmdr.x == ix1 &&
532              scheduled(FSCDBAS)< ttime+game.state.date)||
533             (scheduled(FCDBAS)<ttime+game.state.date && game.battle.y==iy1 && game.battle.x == ix1))
534             prout("The starbase there will be destroyed by then.");
535         proutn("New warp factor to try? ");
536         if (scan() == IHREAL) {
537             wfl = true;
538             twarp = aaitem;
539             if (twarp<1.0 || twarp > 10.0) {
540                 huh();
541                 return;
542             }
543         }
544         else {
545             chew();
546             skip(1);
547             return;
548         }
549     }
550                         
551 }