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