Make sure this builds with -DSERGEEV.
[super-star-trek.git] / finish.c
1 #include "sst.h"
2 #include <string.h>
3 #include <time.h>
4
5 void dstrct() {
6         /* Finish with a BANG! */
7         chew();
8         if (game.damage[DCOMPTR] != 0.0) {
9                 prout("Computer damaged; cannot execute destruct sequence.");
10                 return;
11         }
12         prouts("---WORKING---"); skip(1);
13         prouts("SELF-DESTRUCT-SEQUENCE-ACTIVATED"); skip(1);
14         prouts("   10"); skip(1);
15         prouts("       9"); skip(1);
16         prouts("          8"); skip(1);
17         prouts("             7"); skip(1);
18         prouts("                6"); skip(1);
19         skip(1);
20         prout("ENTER-CORRECT-PASSWORD-TO-CONTINUE-");
21         skip(1);
22         prout("SELF-DESTRUCT-SEQUENCE-OTHERWISE-");
23         skip(1);
24         prout("SELF-DESTRUCT-SEQUENCE-WILL-BE-ABORTED");
25         skip(1);
26         scan();
27         chew();
28         if (strcmp(game.passwd, citem) != 0) {
29                 prouts("PASSWORD-REJECTED;"); skip(1);
30                 prouts("CONTINUITY-EFFECTED");
31                 skip(2);
32                 return;
33         }
34         prouts("PASSWORD-ACCEPTED"); skip(1);
35         prouts("                   5"); skip(1);
36         prouts("                      4"); skip(1);
37         prouts("                         3"); skip(1);
38         prouts("                            2"); skip(1);
39         prouts("                              1"); skip(1);
40         if (Rand() < 0.15) {
41                 prouts("GOODBYE-CRUEL-WORLD");
42                 skip(1);
43         }
44         kaboom();
45 }
46
47 void kaboom(void) {
48         stars();
49         if (ship==IHE) prouts("***");
50         prouts("********* Entropy of ");
51         crmshp();
52         prouts(" maximized *********");
53         skip(1);
54         stars();
55         skip(1);
56         if (nenhere != 0) {
57                 double whammo = 25.0 * energy;
58                 int l=1;
59                 while (l <= nenhere) {
60                         if (game.kpower[l]*game.kdist[l] <= whammo) 
61                                 deadkl(game.kx[l],game.ky[l], game.quad[game.kx[l]][game.ky[l]], game.kx[l], game.ky[l]);
62                         l++;
63                 }
64         }
65         finish(FDILITHIUM);
66 }
67                                 
68
69 void finish(FINTYPE ifin) {
70         int igotit = 0;
71         alldone = 1;
72         skip(3);
73         prout("It is stardate %.1f.", game.state.date);
74         skip(1);
75         switch (ifin) {
76                 case FWON: // Game has been won
77                         if (game.state.nromrem != 0)
78                                 prout("The remaining %d Romulans surrender to Starfleet Command.",
79                                            game.state.nromrem);
80
81                         prout("You have smashed the Klingon invasion fleet and saved");
82                         prout("the Federation.");
83                         gamewon=1;
84                         if (alive) {
85                                 double badpt;
86                                 badpt = 5.*game.state.starkl + casual + 10.*game.state.nplankl +
87                                                 45.*nhelp+100.*game.state.basekl;
88                                 if (ship == IHF) badpt += 100.0;
89                                 else if (ship == 0) badpt += 200.0;
90                                 if (badpt < 100.0) badpt = 0.0; // Close enough!
91                                 if (game.state.date-indate < 5.0 ||
92                                         // killsPerDate >= RateMax
93                                         (game.state.killk+game.state.killc+game.state.nsckill)/(game.state.date-indate) >=
94                                         0.1*skill*(skill+1.0) + 0.1 + 0.008*badpt) {
95                                         skip(1);
96                                         prout("In fact, you have done so well that Starfleet Command");
97                                         switch (skill) {
98                                                 case 1:
99                                                         prout("promotes you one step in rank from \"Novice\" to \"Fair\".");
100                                                         break;
101                                                 case 2:
102                                                         prout("promotes you one step in rank from \"Fair\" to \"Good\".");
103                                                         break;
104                                                 case 3:
105                                                         prout("promotes you one step in rank from \"Good\" to \"Expert\".");
106                                                         break;
107                                                 case 4:
108                                                         prout("promotes you to Commodore Emeritus.");
109                                                         skip(1);
110                                                         prout("Now that you think you're really good, try playing");
111                                                         prout("the \"Emeritus\" game. It will splatter your ego.");
112                                                         break;
113                                                 case 5:
114                                                         skip(1);
115                                                         proutn("Computer-  ");
116                                                         prouts("ERROR-ERROR-ERROR-ERROR");
117                                                         skip(2);
118                                                         prouts("  YOUR-SKILL-HAS-EXCEEDED-THE-CAPACITY-OF-THIS-PROGRAM");
119                                                         skip(1);
120                                                         prouts("  THIS-PROGRAM-MUST-SURVIVE");
121                                                         skip(1);
122                                                         prouts("  THIS-PROGRAM-MUST-SURVIVE");
123                                                         skip(1);
124                                                         prouts("  THIS-PROGRAM-MUST-SURVIVE");
125                                                         skip(1);
126                                                         prouts("  THIS-PROGRAM-MUST?- MUST ? - SUR? ? -?  VI");
127                                                         skip(2);
128                                                         prout("Now you can retire and write your own Star Trek game!");
129                                                         skip(1);
130                                                         break;
131                                         }
132                                         if (skill > 3) {
133                                                 if (thawed
134 #ifdef DEBUG
135                                                         && !idebug
136 #endif
137                                                         )
138                                                         prout("You cannot get a citation, so...");
139                                                 else {
140                                                         proutn("Do you want your Commodore Emeritus Citation printed? ");
141                                                         chew();
142                                                         if (ja()) {
143                                                                 igotit = 1;
144                                                         }
145                                                 }
146                                         }
147                                 }
148                                 // Only grant long life if alive (original didn't!)
149                                 skip(1);
150                                 prout("LIVE LONG AND PROSPER.");
151                         }
152                         score();
153                         if (igotit != 0) plaque();
154                         return;
155                 case FDEPLETE: // Federation Resources Depleted
156                         prout("Your time has run out and the Federation has been");
157                         prout("conquered.  Your starship is now Klingon property,");
158                         prout("and you are put on trial as a war criminal.  On the");
159                         proutn("basis of your record, you are ");
160                         if (game.state.remkl*3.0 > inkling) {
161                                 prout("aquitted.");
162                                 skip(1);
163                                 prout("LIVE LONG AND PROSPER.");
164                         }
165                         else {
166                                 prout("found guilty and");
167                                 prout("sentenced to death by slow torture.");
168                                 alive = 0;
169                         }
170                         score();
171                         return;
172                 case FLIFESUP:
173                         prout("Your life support reserves have run out, and");
174                         prout("you die of thirst, starvation, and asphyxiation.");
175                         prout("Your starship is a derelict in space.");
176                         break;
177                 case FNRG:
178                         prout("Your energy supply is exhausted.");
179                         skip(1);
180                         prout("Your starship is a derelict in space.");
181                         break;
182                 case FBATTLE:
183                         proutn("The ");
184                         crmshp();
185                         prout("has been destroyed in battle.");
186                         skip(1);
187                         prout("Dulce et decorum est pro patria mori.");
188                         break;
189                 case FNEG3:
190                         prout("You have made three attempts to cross the negative energy");
191                         prout("barrier which surrounds the galaxy.");
192                         skip(1);
193                         prout("Your navigation is abominable.");
194                         score();
195                         return;
196                 case FNOVA:
197                         prout("Your starship has been destroyed by a nova.");
198                         prout("That was a great shot.");
199                         skip(1);
200                         break;
201                 case FSNOVAED:
202                         proutn("The ");
203                         crmshp();
204                         prout(" has been fried by a supernova.");
205                         prout("...Not even cinders remain...");
206                         break;
207                 case FABANDN:
208                         prout("You have been captured by the Klingons. If you still");
209                         prout("had a starbase to be returned to, you would have been");
210                         prout("repatriated and given another chance. Since you have");
211                         prout("no starbases, you will be mercilessly tortured to death.");
212                         break;
213                 case FDILITHIUM:
214                         prout("Your starship is now an expanding cloud of subatomic particles");
215                         break;
216                 case FMATERIALIZE:
217                         prout("Starbase was unable to re-materialize your starship.");
218                         prout("Sic transit gloria muntdi");
219                         break;
220                 case FPHASER:
221                         proutn("The ");
222                         crmshp();
223                         prout(" has been cremated by its own phasers.");
224                         break;
225                 case FLOST:
226                         prout("You and your landing party have been");
227                         prout("converted to energy, disipating through space.");
228                         break;
229                 case FMINING:
230                         prout("You are left with your landing party on");
231                         prout("a wild jungle planet inhabited by primitive cannibals.");
232                         skip(1);
233                         prout("They are very fond of \"Captain Kirk\" soup.");
234                         skip(1);
235                         proutn("Without your leadership, the ");
236                         crmshp();
237                         prout(" is destroyed.");
238                         break;
239                 case FDPLANET:
240                         prout("You and your mining party perish.");
241                         skip(1);
242                         prout("That was a great shot.");
243                         skip(1);
244                         break;
245                 case FSSC:
246                         prout("The Galileo is instantly annihilated by the supernova.");
247                         // no break;
248                 case FPNOVA:
249                         prout("You and your mining party are atomized.");
250                         skip(1);
251                         proutn("Mr. Spock takes command of the ");
252                         crmshp();
253                         prout(" and");
254                         prout("joins the Romulans, reigning terror on the Federation.");
255                         break;
256                 case FSTRACTOR:
257                         prout("The shuttle craft Galileo is also caught,");
258                         prout("and breaks up under the strain.");
259                         skip(1);
260                         prout("Your debris is scattered for millions of miles.");
261                         proutn("Without your leadership, the ");
262                         crmshp();
263                         prout(" is destroyed.");
264                         break;
265                 case FDRAY:
266                         prout("The mutants attack and kill Spock.");
267                         prout("Your ship is captured by Klingons, and");
268                         prout("your crew is put on display in a Klingon zoo.");
269                         break;
270                 case FTRIBBLE:
271                         prout("Tribbles consume all remaining water,");
272                         prout("food, and oxygen on your ship.");
273                         skip(1);
274                         prout("You die of thirst, starvation, and asphyxiation.");
275                         prout("Your starship is a derelict in space.");
276                         break;
277                 case FHOLE:
278                         prout("Your ship is drawn to the center of the black hole.");
279                         prout("You are crushed into extremely dense matter.");
280                         break;
281         }
282         if (ship==IHF) ship= 0;
283         else if (ship == IHE) ship = IHF;
284         alive = 0;
285         if (game.state.remkl != 0) {
286                 double goodies = game.state.remres/inresor;
287                 double baddies = (game.state.remkl + 2.0*game.state.remcom)/(inkling+2.0*incom);
288                 if (goodies/baddies >= 1.0+0.5*Rand()) {
289                         prout("As a result of your actions, a treaty with the Klingon");
290                         prout("Empire has been signed. The terms of the treaty are");
291                         if (goodies/baddies >= 3.0+Rand()) {
292                                 prout("favorable to the Federation.");
293                                 skip(1);
294                                 prout("Congratulations!");
295                         }
296                         else
297                                 prout("highly unfavorable to the Federation.");
298                 }
299                 else
300                         prout("The Federation will be destroyed.");
301         }
302         else {
303                 prout("Since you took the last Klingon with you, you are a");
304                 prout("martyr and a hero. Someday maybe they'll erect a");
305                 prout("statue in your memory. Rest in peace, and try not");
306                 prout("to think about pigeons.");
307                 gamewon = 1;
308         }
309         score();
310 }
311
312 void score(void) {
313         double timused = game.state.date - indate;
314         int ithperd, iwon, klship;
315
316         iskill = skill;
317         if ((timused == 0 || game.state.remkl != 0) && timused < 5.0) timused = 5.0;
318         perdate = (game.state.killc + game.state.killk + game.state.nsckill)/timused;
319         ithperd = 500*perdate + 0.5;
320         iwon = 0;
321         if (gamewon) iwon = 100*skill;
322         if (ship == IHE) klship = 0;
323         else if (ship == IHF) klship = 1;
324         else klship = 2;
325         if (gamewon == 0) game.state.nromrem = 0; // None captured if no win
326         iscore = 10*game.state.killk + 50*game.state.killc + ithperd + iwon
327                          - 100*game.state.basekl - 100*klship - 45*nhelp -5*game.state.starkl - casual
328                          + 20*game.state.nromkl + 200*game.state.nsckill - 10*game.state.nplankl + game.state.nromrem;
329         if (alive == 0) iscore -= 200;
330         skip(2);
331         prout("Your score --");
332         if (game.state.nromkl)
333                 prout("%6d Romulans destroyed                 %5d",
334                            game.state.nromkl,20*game.state.nromkl);
335         if (game.state.nromrem)
336                 prout("%6d Romulans captured                  %5d",
337                            game.state.nromrem, game.state.nromrem);
338         if (game.state.killk)
339                 prout("%6d ordinary Klingons destroyed        %5d",
340                            game.state.killk, 10*game.state.killk);
341         if (game.state.killc)
342                 prout("%6d Klingon commanders destroyed       %5d",
343                            game.state.killc, 50*game.state.killc);
344         if (game.state.nsckill)
345                 prout("%6d Super-Commander destroyed          %5d",
346                            game.state.nsckill, 200*game.state.nsckill);
347         if (ithperd)
348                 prout("%6.2f Klingons per stardate              %5d",
349                            perdate, ithperd);
350         if (game.state.starkl)
351                 prout("%6d stars destroyed by your action     %5d",
352                            game.state.starkl, -5*game.state.starkl);
353         if (game.state.nplankl)
354                 prout("%6d planets destroyed by your action   %5d",
355                            game.state.nplankl, -10*game.state.nplankl);
356         if (game.state.basekl)
357                 prout("%6d bases destroyed by your action     %5d",
358                            game.state.basekl, -100*game.state.basekl);
359         if (nhelp)
360                 prout("%6d calls for help from starbase       %5d",
361                            nhelp, -45*nhelp);
362         if (casual)
363                 prout("%6d casualties incurred                %5d",
364                            casual, -casual);
365         if (klship)
366                 prout("%6d ship(s) lost or destroyed          %5d",
367                            klship, -100*klship);
368         if (alive==0)
369                 prout("Penalty for getting yourself killed        -200");
370         if (gamewon) {
371                 proutn("Bonus for winning ");
372                 switch (skill) {
373                         case 1: proutn("Novice game  "); break;
374                         case 2: proutn("Fair game    "); break;
375                         case 3: proutn("Good game    "); break;
376                         case 4: proutn("Expert game  "); break;
377                         case 5: proutn("Emeritus game"); break;
378                 }
379                 prout("           %5d", iwon);
380         }
381         skip(1);
382         prout("TOTAL SCORE                               %5d", iscore);
383 }
384
385 void plaque(void) {
386         FILE *fp=NULL;
387         time_t t;
388         char *timestring;
389         int nskip;
390         char winner[128];
391
392         skip(2);
393         
394         while (fp == NULL) {
395                 proutn("File or device name for your plaque: ");
396                 cgetline(winner, sizeof(winner));
397                 fp = fopen(winner, "w");
398                 if (fp==NULL) {
399                         prout("Invalid name.");
400                 }
401         }
402
403         proutn("Enter name to go on plaque (up to 30 characters): ");
404         cgetline(winner, sizeof(winner));
405         /* The 38 below must be 64 for 132-column paper */
406         nskip = 38 - strlen(winner)/2;
407
408         fprintf(fp,"\n\n\n\n");
409         /* --------DRAW ENTERPRISE PICTURE. */
410         fprintf(fp, "                                       EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n" );
411         fprintf(fp, "                                      EEE                      E  : :                                         :  E\n" );
412         fprintf(fp, "                                    EE   EEE                   E  : :                   NCC-1701              :  E\n");
413         fprintf(fp, "EEEEEEEEEEEEEEEE        EEEEEEEEEEEEEEE  : :                              : E\n");
414         fprintf(fp, " E                                     EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n");
415         fprintf(fp, "                      EEEEEEEEE               EEEEEEEEEEEEE                 E  E\n");
416         fprintf(fp, "                               EEEEEEE   EEEEE    E          E              E  E\n");
417         fprintf(fp, "                                      EEE           E          E            E  E\n");
418         fprintf(fp, "                                                       E         E          E  E\n");
419         fprintf(fp, "                                                         EEEEEEEEEEEEE      E  E\n");
420         fprintf(fp, "                                                      EEE :           EEEEEEE  EEEEEEEE\n");
421         fprintf(fp, "                                                    :E    :                 EEEE       E\n");
422         fprintf(fp, "                                                   .-E   -:-----                       E\n");
423         fprintf(fp, "                                                    :E    :                            E\n");
424         fprintf(fp, "                                                      EE  :                    EEEEEEEE\n");
425         fprintf(fp, "                                                       EEEEEEEEEEEEEEEEEEEEEEE\n");
426         fprintf(fp, "\n\n\n");
427         fprintf(fp, "                                                       U. S. S. ENTERPRISE\n");
428         fprintf(fp, "\n\n\n\n");
429         fprintf(fp, "                                  For demonstrating outstanding ability as a starship captain\n");
430         fprintf(fp, "\n");
431         fprintf(fp, "                                                Starfleet Command bestows to you\n");
432         fprintf(fp, "\n");
433         fprintf(fp,"%*s%s\n\n", nskip, "", winner);
434         fprintf(fp, "                                                           the rank of\n\n");
435         fprintf(fp, "                                                       \"Commodore Emeritus\"\n\n");
436         fprintf(fp, "                                                          ");
437         switch (iskill) {
438                 case 4: fprintf(fp," Expert level\n\n"); break;
439                 case 5: fprintf(fp,"Emeritus level\n\n"); break;
440                 default: fprintf(fp," Cheat level\n\n"); break;
441         }
442         t = time(NULL);
443         timestring = ctime(&t);
444         fprintf(fp, "                                                 This day of %.6s %.4s, %.8s\n\n",
445                         timestring+4, timestring+20, timestring+11);
446         fprintf(fp,"                                                        Your score:  %d\n\n", iscore);
447         fprintf(fp,"                                                    Klingons per stardate:  %.2f\n", perdate);
448         fclose(fp);
449 }