Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / hack / hack.end.c
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.end.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.end.c,v 1.4 1999/11/16 10:26:36 marcel Exp $ */
4
5 #include "hack.h"
6 #include <stdio.h>
7 #include <signal.h>
8 #define Sprintf (void) sprintf
9 extern char plname[], pl_character[];
10 extern char *itoa(), *ordin(), *eos();
11
12 xchar maxdlevel = 1;
13
14 void
15 done1()
16 {
17         (void) signal(SIGINT,SIG_IGN);
18         pline("Really quit?");
19         if(readchar() != 'y') {
20                 (void) signal(SIGINT,done1);
21                 clrlin();
22                 (void) fflush(stdout);
23                 if(multi > 0) nomul(0);
24                 return;
25         }
26         done("quit");
27         /* NOTREACHED */
28 }
29
30 int done_stopprint;
31 int done_hup;
32
33 void
34 done_intr(){
35         done_stopprint++;
36         (void) signal(SIGINT, SIG_IGN);
37         (void) signal(SIGQUIT, SIG_IGN);
38 }
39
40 void
41 done_hangup(){
42         done_hup++;
43         (void) signal(SIGHUP, SIG_IGN);
44         done_intr();
45 }
46
47 done_in_by(mtmp) struct monst *mtmp; {
48 static char buf[BUFSZ];
49         pline("You die ...");
50         if(mtmp->data->mlet == ' '){
51                 Sprintf(buf, "the ghost of %s", (char *) mtmp->mextra);
52                 killer = buf;
53         } else if(mtmp->mnamelth) {
54                 Sprintf(buf, "%s called %s",
55                         mtmp->data->mname, NAME(mtmp));
56                 killer = buf;
57         } else if(mtmp->minvis) {
58                 Sprintf(buf, "invisible %s", mtmp->data->mname);
59                 killer = buf;
60         } else killer = mtmp->data->mname;
61         done("died");
62 }
63
64 /* called with arg "died", "drowned", "escaped", "quit", "choked", "panicked",
65    "burned", "starved" or "tricked" */
66 /* Be careful not to call panic from here! */
67 done(st1)
68 char *st1;
69 {
70
71 #ifdef WIZARD
72         if(wizard && *st1 == 'd'){
73                 u.uswldtim = 0;
74                 if(u.uhpmax < 0) u.uhpmax = 100;        /* arbitrary */
75                 u.uhp = u.uhpmax;
76                 pline("For some reason you are still alive.");
77                 flags.move = 0;
78                 if(multi > 0) multi = 0; else multi = -1;
79                 flags.botl = 1;
80                 return;
81         }
82 #endif WIZARD
83         (void) signal(SIGINT, done_intr);
84         (void) signal(SIGQUIT, done_intr);
85         (void) signal(SIGHUP, done_hangup);
86         if(*st1 == 'q' && u.uhp < 1){
87                 st1 = "died";
88                 killer = "quit while already on Charon's boat";
89         }
90         if(*st1 == 's') killer = "starvation"; else
91         if(*st1 == 'd' && st1[1] == 'r') killer = "drowning"; else
92         if(*st1 == 'p') killer = "panic"; else
93         if(*st1 == 't') killer = "trickery"; else
94         if(!index("bcd", *st1)) killer = st1;
95         paybill();
96         clearlocks();
97         if(flags.toplin == 1) more();
98         if(index("bcds", *st1)){
99 #ifdef WIZARD
100             if(!wizard)
101 #endif WIZARD
102                 savebones();
103                 if(!flags.notombstone)
104                         outrip();
105         }
106         if(*st1 == 'c') killer = st1;           /* after outrip() */
107         settty((char *) 0);     /* does a clear_screen() */
108         if(!done_stopprint)
109                 printf("Goodbye %s %s...\n\n", pl_character, plname);
110         { long int tmp;
111           tmp = u.ugold - u.ugold0;
112           if(tmp < 0)
113                 tmp = 0;
114           if(*st1 == 'd' || *st1 == 'b')
115                 tmp -= tmp/10;
116           u.urexp += tmp;
117           u.urexp += 50 * maxdlevel;
118           if(maxdlevel > 20)
119                 u.urexp += 1000*((maxdlevel > 30) ? 10 : maxdlevel - 20);
120         }
121         if(*st1 == 'e') {
122                 extern struct monst *mydogs;
123                 struct monst *mtmp;
124                 struct obj *otmp;
125                 int i;
126                 unsigned worthlessct = 0;
127                 boolean has_amulet = FALSE;
128
129                 killer = st1;
130                 keepdogs();
131                 mtmp = mydogs;
132                 if(mtmp) {
133                         if(!done_stopprint) printf("You");
134                         while(mtmp) {
135                                 if(!done_stopprint)
136                                         printf(" and %s", monnam(mtmp));
137                                 if(mtmp->mtame)
138                                         u.urexp += mtmp->mhp;
139                                 mtmp = mtmp->nmon;
140                         }
141                         if(!done_stopprint)
142                     printf("\nescaped from the dungeon with %ld points,\n",
143                         u.urexp);
144                 } else
145                 if(!done_stopprint)
146                   printf("You escaped from the dungeon with %ld points,\n",
147                     u.urexp);
148                 for(otmp = invent; otmp; otmp = otmp->nobj) {
149                         if(otmp->olet == GEM_SYM){
150                                 objects[otmp->otyp].oc_name_known = 1;
151                                 i = otmp->quan*objects[otmp->otyp].g_val;
152                                 if(i == 0) {
153                                         worthlessct += otmp->quan;
154                                         continue;
155                                 }
156                                 u.urexp += i;
157                                 if(!done_stopprint)
158                                   printf("\t%s (worth %d Zorkmids),\n",
159                                     doname(otmp), i);
160                         } else if(otmp->olet == AMULET_SYM) {
161                                 otmp->known = 1;
162                                 i = (otmp->spe < 0) ? 2 : 5000;
163                                 u.urexp += i;
164                                 if(!done_stopprint)
165                                   printf("\t%s (worth %d Zorkmids),\n",
166                                     doname(otmp), i);
167                                 if(otmp->spe >= 0) {
168                                         has_amulet = TRUE;
169                                         killer = "escaped (with amulet)";
170                                 }
171                         }
172                 }
173                 if(worthlessct) if(!done_stopprint)
174                   printf("\t%u worthless piece%s of coloured glass,\n",
175                   worthlessct, plur(worthlessct));
176                 if(has_amulet) u.urexp *= 2;
177         } else
178                 if(!done_stopprint)
179                   printf("You %s on dungeon level %d with %ld points,\n",
180                     st1, dlevel, u.urexp);
181         if(!done_stopprint)
182           printf("and %ld piece%s of gold, after %ld move%s.\n",
183             u.ugold, plur(u.ugold), moves, plur(moves));
184         if(!done_stopprint)
185   printf("You were level %u with a maximum of %d hit points when you %s.\n",
186             u.ulevel, u.uhpmax, st1);
187         if(*st1 == 'e' && !done_stopprint){
188                 getret();       /* all those pieces of coloured glass ... */
189                 cls();
190         }
191 #ifdef WIZARD
192         if(!wizard)
193 #endif WIZARD
194                 topten();
195         if(done_stopprint) printf("\n\n");
196         exit(0);
197 }
198
199 #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
200 #define NAMSZ   8
201 #define DTHSZ   40
202 #define PERSMAX 1
203 #define POINTSMIN       1       /* must be > 0 */
204 #define ENTRYMAX        100     /* must be >= 10 */
205 #define PERS_IS_UID             /* delete for PERSMAX per name; now per uid */
206 struct toptenentry {
207         struct toptenentry *tt_next;
208         long int points;
209         int level,maxlvl,hp,maxhp;
210         int uid;
211         char plchar;
212         char sex;
213         char name[NAMSZ+1];
214         char death[DTHSZ+1];
215         char date[7];           /* yymmdd */
216 } *tt_head;
217
218 topten(){
219         int uid = getuid();
220         int rank, rank0 = -1, rank1 = 0;
221         int occ_cnt = PERSMAX;
222         struct toptenentry *t0, *t1, *tprev;
223         char *recfile = RECORD;
224         char *reclock = "record_lock";
225         int sleepct = 300;
226         FILE *rfile;
227         int flg = 0;
228         extern char *getdate();
229 #define HUP     if(!done_hup)
230         while(link(recfile, reclock) == -1) {
231                 HUP perror(reclock);
232                 if(!sleepct--) {
233                         HUP puts("I give up. Sorry.");
234                         HUP puts("Perhaps there is an old record_lock around?");
235                         return;
236                 }
237                 HUP printf("Waiting for access to record file. (%d)\n",
238                         sleepct);
239                 HUP (void) fflush(stdout);
240                 sleep(1);
241         }
242         if(!(rfile = fopen(recfile,"r"))){
243                 HUP puts("Cannot open record file!");
244                 goto unlock;
245         }
246         HUP (void) putchar('\n');
247
248         /* create a new 'topten' entry */
249         t0 = newttentry();
250         t0->level = dlevel;
251         t0->maxlvl = maxdlevel;
252         t0->hp = u.uhp;
253         t0->maxhp = u.uhpmax;
254         t0->points = u.urexp;
255         t0->plchar = pl_character[0];
256         t0->sex = (flags.female ? 'F' : 'M');
257         t0->uid = uid;
258         (void) strncpy(t0->name, plname, NAMSZ);
259         (t0->name)[NAMSZ] = 0;
260         (void) strncpy(t0->death, killer, DTHSZ);
261         (t0->death)[DTHSZ] = 0;
262         (void) strcpy(t0->date, getdate());
263
264         /* assure minimum number of points */
265         if(t0->points < POINTSMIN)
266                 t0->points = 0;
267
268         t1 = tt_head = newttentry();
269         tprev = 0;
270         /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
271         for(rank = 1; ; ) {
272           if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
273                 t1->date, &t1->uid,
274                 &t1->level, &t1->maxlvl,
275                 &t1->hp, &t1->maxhp, &t1->points,
276                 &t1->plchar, &t1->sex, t1->name, t1->death) != 11
277           || t1->points < POINTSMIN)
278                         t1->points = 0;
279           if(rank0 < 0 && t1->points < t0->points) {
280                 rank0 = rank++;
281                 if(tprev == 0)
282                         tt_head = t0;
283                 else
284                         tprev->tt_next = t0;
285                 t0->tt_next = t1;
286                 occ_cnt--;
287                 flg++;          /* ask for a rewrite */
288           } else
289                 tprev = t1;
290           if(t1->points == 0) break;
291           if(
292 #ifdef PERS_IS_UID
293              t1->uid == t0->uid &&
294 #else
295              strncmp(t1->name, t0->name, NAMSZ) == 0 &&
296 #endif PERS_IS_UID
297              t1->plchar == t0->plchar && --occ_cnt <= 0){
298                 if(rank0 < 0){
299                         rank0 = 0;
300                         rank1 = rank;
301         HUP printf("You didn't beat your previous score of %ld points.\n\n",
302                                 t1->points);
303                 }
304                 if(occ_cnt < 0){
305                         flg++;
306                         continue;
307                 }
308           }
309           if(rank <= ENTRYMAX){
310                 t1 = t1->tt_next = newttentry();
311                 rank++;
312           }
313           if(rank > ENTRYMAX){
314                 t1->points = 0;
315                 break;
316           }
317         }
318         if(flg) {       /* rewrite record file */
319                 (void) fclose(rfile);
320                 if(!(rfile = fopen(recfile,"w"))){
321                         HUP puts("Cannot write record file\n");
322                         goto unlock;
323                 }
324
325                 if(!done_stopprint) if(rank0 > 0){
326                     if(rank0 <= 10)
327                         puts("You made the top ten list!\n");
328                     else
329                 printf("You reached the %d%s place on the top %d list.\n\n",
330                         rank0, ordin(rank0), ENTRYMAX);
331                 }
332         }
333         if(rank0 == 0) rank0 = rank1;
334         if(rank0 <= 0) rank0 = rank;
335         if(!done_stopprint) outheader();
336         t1 = tt_head;
337         for(rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) {
338           if(flg) fprintf(rfile,"%6s %d %d %d %d %d %ld %c%c %s,%s\n",
339             t1->date, t1->uid,
340             t1->level, t1->maxlvl,
341             t1->hp, t1->maxhp, t1->points,
342             t1->plchar, t1->sex, t1->name, t1->death);
343           if(done_stopprint) continue;
344           if(rank > flags.end_top &&
345             (rank < rank0-flags.end_around || rank > rank0+flags.end_around)
346             && (!flags.end_own ||
347 #ifdef PERS_IS_UID
348                                   t1->uid != t0->uid ))
349 #else
350                                   strncmp(t1->name, t0->name, NAMSZ)))
351 #endif PERS_IS_UID
352                 continue;
353           if(rank == rank0-flags.end_around &&
354              rank0 > flags.end_top+flags.end_around+1 &&
355              !flags.end_own)
356                 (void) putchar('\n');
357           if(rank != rank0)
358                 (void) outentry(rank, t1, 0);
359           else if(!rank1)
360                 (void) outentry(rank, t1, 1);
361           else {
362                 int t0lth = outentry(0, t0, -1);
363                 int t1lth = outentry(rank, t1, t0lth);
364                 if(t1lth > t0lth) t0lth = t1lth;
365                 (void) outentry(0, t0, t0lth);
366           }
367         }
368         if(rank0 >= rank) if(!done_stopprint)
369                 (void) outentry(0, t0, 1);
370         (void) fclose(rfile);
371 unlock:
372         (void) unlink(reclock);
373 }
374
375 outheader() {
376 char linebuf[BUFSZ];
377 char *bp;
378         (void) strcpy(linebuf, "Number Points  Name");
379         bp = eos(linebuf);
380         while(bp < linebuf + COLNO - 9) *bp++ = ' ';
381         (void) strcpy(bp, "Hp [max]");
382         puts(linebuf);
383 }
384
385 /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
386 int
387 outentry(rank,t1,so) struct toptenentry *t1; {
388 boolean quit = FALSE, killed = FALSE, starv = FALSE;
389 char linebuf[BUFSZ];
390         linebuf[0] = 0;
391         if(rank) Sprintf(eos(linebuf), "%3d", rank);
392                 else Sprintf(eos(linebuf), "   ");
393         Sprintf(eos(linebuf), " %6ld %8s", t1->points, t1->name);
394         if(t1->plchar == 'X') Sprintf(eos(linebuf), " ");
395         else Sprintf(eos(linebuf), "-%c ", t1->plchar);
396         if(!strncmp("escaped", t1->death, 7)) {
397           if(!strcmp(" (with amulet)", t1->death+7))
398             Sprintf(eos(linebuf), "escaped the dungeon with amulet");
399           else
400             Sprintf(eos(linebuf), "escaped the dungeon [max level %d]",
401               t1->maxlvl);
402         } else {
403           if(!strncmp(t1->death,"quit",4)) {
404             quit = TRUE;
405             if(t1->maxhp < 3*t1->hp && t1->maxlvl < 4)
406                 Sprintf(eos(linebuf), "cravenly gave up");
407             else
408                 Sprintf(eos(linebuf), "quit");
409           }
410           else if(!strcmp(t1->death,"choked"))
411             Sprintf(eos(linebuf), "choked on %s food",
412                 (t1->sex == 'F') ? "her" : "his");
413           else if(!strncmp(t1->death,"starv",5))
414             Sprintf(eos(linebuf), "starved to death"), starv = TRUE;
415           else Sprintf(eos(linebuf), "was killed"), killed = TRUE;
416           Sprintf(eos(linebuf), " on%s level %d",
417             (killed || starv) ? "" : " dungeon", t1->level);
418           if(t1->maxlvl != t1->level)
419             Sprintf(eos(linebuf), " [max %d]", t1->maxlvl);
420           if(quit && t1->death[4]) Sprintf(eos(linebuf), t1->death + 4);
421         }
422         if(killed) Sprintf(eos(linebuf), " by %s%s",
423           (!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4))
424                 ? "" :
425           index(vowels,*t1->death) ? "an " : "a ",
426           t1->death);
427         Sprintf(eos(linebuf), ".");
428         if(t1->maxhp) {
429           char *bp = eos(linebuf);
430           char hpbuf[10];
431           int hppos;
432           Sprintf(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-");
433           hppos = COLNO - 7 - strlen(hpbuf);
434           if(bp <= linebuf + hppos) {
435             while(bp < linebuf + hppos) *bp++ = ' ';
436             (void) strcpy(bp, hpbuf);
437             Sprintf(eos(bp), " [%d]", t1->maxhp);
438           }
439         }
440         if(so == 0) puts(linebuf);
441         else if(so > 0) {
442           char *bp = eos(linebuf);
443           if(so >= COLNO) so = COLNO-1;
444           while(bp < linebuf + so) *bp++ = ' ';
445           *bp = 0;
446           standoutbeg();
447           fputs(linebuf,stdout);
448           standoutend();
449           (void) putchar('\n');
450         }
451         return(strlen(linebuf));
452 }
453
454 char *
455 itoa(a) int a; {
456 static char buf[12];
457         Sprintf(buf,"%d",a);
458         return(buf);
459 }
460
461 char *
462 ordin(n) int n; {
463 int d = n%10;
464         return((d==0 || d>3 || n/10==1) ? "th" : (d==1) ? "st" :
465                 (d==2) ? "nd" : "rd");
466 }
467
468 clearlocks(){
469 int x;
470         (void) signal(SIGHUP,SIG_IGN);
471         for(x = maxdlevel; x >= 0; x--) {
472                 glo(x);
473                 (void) unlink(lock);    /* not all levels need be present */
474         }
475 }
476
477 #ifdef NOSAVEONHANGUP
478 hangup()
479 {
480         (void) signal(SIGINT, SIG_IGN);
481         clearlocks();
482         exit(1);
483 }
484 #endif NOSAVEONHANGUP
485
486 char *
487 eos(s)
488 char *s;
489 {
490         while(*s) s++;
491         return(s);
492 }
493
494 /* it is the callers responsibility to check that there is room for c */
495 charcat(s,c) char *s, c; {
496         while(*s) s++;
497         *s++ = c;
498         *s = 0;
499 }
500
501 /*
502  * Called with args from main if argc >= 0. In this case, list scores as
503  * requested. Otherwise, find scores for the current player (and list them
504  * if argc == -1).
505  */
506 prscore(argc,argv) int argc; char **argv; {
507         extern char *hname;
508         char **players;
509         int playerct;
510         int rank;
511         struct toptenentry *t1, *t2;
512         char *recfile = RECORD;
513         FILE *rfile;
514         int flg = 0;
515         int i;
516 #ifdef nonsense
517         long total_score = 0L;
518         char totchars[10];
519         int totcharct = 0;
520 #endif nonsense
521         int outflg = (argc >= -1);
522 #ifdef PERS_IS_UID
523         int uid = -1;
524 #else
525         char *player0;
526 #endif PERS_IS_UID
527
528         if(!(rfile = fopen(recfile,"r"))){
529                 puts("Cannot open record file!");
530                 return;
531         }
532
533         if(argc > 1 && !strncmp(argv[1], "-s", 2)){
534                 if(!argv[1][2]){
535                         argc--;
536                         argv++;
537                 } else if(!argv[1][3] && index("CFKSTWX", argv[1][2])) {
538                         argv[1]++;
539                         argv[1][0] = '-';
540                 } else  argv[1] += 2;
541         }
542         if(argc <= 1){
543 #ifdef PERS_IS_UID
544                 uid = getuid();
545                 playerct = 0;
546 #else
547                 player0 = plname;
548                 if(!*player0)
549                         player0 = "hackplayer";
550                 playerct = 1;
551                 players = &player0;
552 #endif PERS_IS_UID
553         } else {
554                 playerct = --argc;
555                 players = ++argv;
556         }
557         if(outflg) putchar('\n');
558
559         t1 = tt_head = newttentry();
560         for(rank = 1; ; rank++) {
561           if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
562                 t1->date, &t1->uid,
563                 &t1->level, &t1->maxlvl,
564                 &t1->hp, &t1->maxhp, &t1->points,
565                 &t1->plchar, &t1->sex, t1->name, t1->death) != 11)
566                         t1->points = 0;
567           if(t1->points == 0) break;
568 #ifdef PERS_IS_UID
569           if(!playerct && t1->uid == uid)
570                 flg++;
571           else
572 #endif PERS_IS_UID
573           for(i = 0; i < playerct; i++){
574                 if(strcmp(players[i], "all") == 0 ||
575                    strncmp(t1->name, players[i], NAMSZ) == 0 ||
576                   (players[i][0] == '-' &&
577                    players[i][1] == t1->plchar &&
578                    players[i][2] == 0) ||
579                   (digit(players[i][0]) && rank <= atoi(players[i])))
580                         flg++;
581           }
582           t1 = t1->tt_next = newttentry();
583         }
584         (void) fclose(rfile);
585         if(!flg) {
586             if(outflg) {
587                 printf("Cannot find any entries for ");
588                 if(playerct < 1) printf("you.\n");
589                 else {
590                   if(playerct > 1) printf("any of ");
591                   for(i=0; i<playerct; i++)
592                         printf("%s%s", players[i], (i<playerct-1)?", ":".\n");
593                   printf("Call is: %s -s [playernames]\n", hname);
594                 }
595             }
596             return;
597         }
598
599         if(outflg) outheader();
600         t1 = tt_head;
601         for(rank = 1; t1->points != 0; rank++, t1 = t2) {
602                 t2 = t1->tt_next;
603 #ifdef PERS_IS_UID
604                 if(!playerct && t1->uid == uid)
605                         goto outwithit;
606                 else
607 #endif PERS_IS_UID
608                 for(i = 0; i < playerct; i++){
609                         if(strcmp(players[i], "all") == 0 ||
610                            strncmp(t1->name, players[i], NAMSZ) == 0 ||
611                           (players[i][0] == '-' &&
612                            players[i][1] == t1->plchar &&
613                            players[i][2] == 0) ||
614                           (digit(players[i][0]) && rank <= atoi(players[i]))){
615                         outwithit:
616                                 if(outflg)
617                                     (void) outentry(rank, t1, 0);
618 #ifdef nonsense
619                                 total_score += t1->points;
620                                 if(totcharct < sizeof(totchars)-1)
621                                     totchars[totcharct++] = t1->plchar;
622 #endif nonsense
623                                 break;
624                         }
625                 }
626                 free((char *) t1);
627         }
628 #ifdef nonsense
629         totchars[totcharct] = 0;
630
631         /* We would like to determine whether he is experienced. However,
632            the information collected here only tells about the scores/roles
633            that got into the topten (top 100?). We should maintain a
634            .hacklog or something in his home directory. */
635         flags.beginner = (total_score < 6000);
636         for(i=0; i<6; i++)
637             if(!index(totchars, "CFKSTWX"[i])) {
638                 flags.beginner = 1;
639                 if(!pl_character[0]) pl_character[0] = "CFKSTWX"[i];
640                 break;
641         }
642 #endif nonsense
643 }