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