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