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