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