1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.end.c - version 1.0.3 */
7 #define Sprintf (void) sprintf
8 extern char plname[], pl_character[];
9 extern char *itoa(), *ordin(), *eos();
16 (void) signal(SIGINT,SIG_IGN);
17 pline("Really quit?");
18 if(readchar() != 'y') {
19 (void) signal(SIGINT,done1);
21 (void) fflush(stdout);
22 if(multi > 0) nomul(0);
35 (void) signal(SIGINT, SIG_IGN);
36 (void) signal(SIGQUIT, SIG_IGN);
42 (void) signal(SIGHUP, SIG_IGN);
46 done_in_by(mtmp) register struct monst *mtmp; {
47 static char buf[BUFSZ];
49 if(mtmp->data->mlet == ' '){
50 Sprintf(buf, "the ghost of %s", (char *) mtmp->mextra);
52 } else if(mtmp->mnamelth) {
53 Sprintf(buf, "%s called %s",
54 mtmp->data->mname, NAME(mtmp));
56 } else if(mtmp->minvis) {
57 Sprintf(buf, "invisible %s", mtmp->data->mname);
59 } else killer = mtmp->data->mname;
63 /* called with arg "died", "drowned", "escaped", "quit", "choked", "panicked",
64 "burned", "starved" or "tricked" */
65 /* Be careful not to call panic from here! */
71 if(wizard && *st1 == 'd'){
73 if(u.uhpmax < 0) u.uhpmax = 100; /* arbitrary */
75 pline("For some reason you are still alive.");
77 if(multi > 0) multi = 0; else multi = -1;
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){
87 killer = "quit while already on Charon's boat";
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;
96 if(flags.toplin == 1) more();
97 if(index("bcds", *st1)){
102 if(!flags.notombstone)
105 if(*st1 == 'c') killer = st1; /* after outrip() */
106 settty((char *) 0); /* does a clear_screen() */
108 printf("Goodbye %s %s...\n\n", pl_character, plname);
110 tmp = u.ugold - u.ugold0;
113 if(*st1 == 'd' || *st1 == 'b')
116 u.urexp += 50 * maxdlevel;
118 u.urexp += 1000*((maxdlevel > 30) ? 10 : maxdlevel - 20);
121 extern struct monst *mydogs;
122 register struct monst *mtmp;
123 register struct obj *otmp;
125 register unsigned worthlessct = 0;
126 boolean has_amulet = FALSE;
132 if(!done_stopprint) printf("You");
135 printf(" and %s", monnam(mtmp));
137 u.urexp += mtmp->mhp;
141 printf("\nescaped from the dungeon with %ld points,\n",
145 printf("You escaped from the dungeon with %ld points,\n",
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;
152 worthlessct += otmp->quan;
157 printf("\t%s (worth %d Zorkmids),\n",
159 } else if(otmp->olet == AMULET_SYM) {
161 i = (otmp->spe < 0) ? 2 : 5000;
164 printf("\t%s (worth %d Zorkmids),\n",
168 killer = "escaped (with amulet)";
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;
178 printf("You %s on dungeon level %d with %ld points,\n",
179 st1, dlevel, u.urexp);
181 printf("and %ld piece%s of gold, after %ld move%s.\n",
182 u.ugold, plur(u.ugold), moves, plur(moves));
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 ... */
194 if(done_stopprint) printf("\n\n");
198 #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
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 */
206 struct toptenentry *tt_next;
208 int level,maxlvl,hp,maxhp;
214 char date[7]; /* yymmdd */
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";
227 extern char *getdate();
228 #define HUP if(!done_hup)
229 while(link(recfile, reclock) == -1) {
232 HUP puts("I give up. Sorry.");
233 HUP puts("Perhaps there is an old record_lock around?");
236 HUP printf("Waiting for access to record file. (%d)\n",
238 HUP (void) fflush(stdout);
241 if(!(rfile = fopen(recfile,"r"))){
242 HUP puts("Cannot open record file!");
245 HUP (void) putchar('\n');
247 /* create a new 'topten' entry */
250 t0->maxlvl = maxdlevel;
252 t0->maxhp = u.uhpmax;
253 t0->points = u.urexp;
254 t0->plchar = pl_character[0];
255 t0->sex = (flags.female ? 'F' : 'M');
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());
263 /* assure minimum number of points */
264 if(t0->points < POINTSMIN)
267 t1 = tt_head = newttentry();
269 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
271 if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
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)
278 if(rank0 < 0 && t1->points < t0->points) {
286 flg++; /* ask for a rewrite */
289 if(t1->points == 0) break;
292 t1->uid == t0->uid &&
294 strncmp(t1->name, t0->name, NAMSZ) == 0 &&
296 t1->plchar == t0->plchar && --occ_cnt <= 0){
300 HUP printf("You didn't beat your previous score of %ld points.\n\n",
308 if(rank <= ENTRYMAX){
309 t1 = t1->tt_next = newttentry();
317 if(flg) { /* rewrite record file */
318 (void) fclose(rfile);
319 if(!(rfile = fopen(recfile,"w"))){
320 HUP puts("Cannot write record file\n");
324 if(!done_stopprint) if(rank0 > 0){
326 puts("You made the top ten list!\n");
328 printf("You reached the %d%s place on the top %d list.\n\n",
329 rank0, ordin(rank0), ENTRYMAX);
332 if(rank0 == 0) rank0 = rank1;
333 if(rank0 <= 0) rank0 = rank;
334 if(!done_stopprint) outheader();
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",
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 ||
347 t1->uid != t0->uid ))
349 strncmp(t1->name, t0->name, NAMSZ)))
352 if(rank == rank0-flags.end_around &&
353 rank0 > flags.end_top+flags.end_around+1 &&
355 (void) putchar('\n');
357 (void) outentry(rank, t1, 0);
359 (void) outentry(rank, t1, 1);
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);
367 if(rank0 >= rank) if(!done_stopprint)
368 (void) outentry(0, t0, 1);
369 (void) fclose(rfile);
371 (void) unlink(reclock);
377 (void) strcpy(linebuf, "Number Points Name");
379 while(bp < linebuf + COLNO - 9) *bp++ = ' ';
380 (void) strcpy(bp, "Hp [max]");
384 /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
386 outentry(rank,t1,so) register struct toptenentry *t1; {
387 boolean quit = FALSE, killed = FALSE, starv = FALSE;
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");
399 Sprintf(eos(linebuf), "escaped the dungeon [max level %d]",
402 if(!strncmp(t1->death,"quit",4)) {
404 if(t1->maxhp < 3*t1->hp && t1->maxlvl < 4)
405 Sprintf(eos(linebuf), "cravenly gave up");
407 Sprintf(eos(linebuf), "quit");
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);
421 if(killed) Sprintf(eos(linebuf), " by %s%s",
422 (!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4))
424 index(vowels,*t1->death) ? "an " : "a ",
426 Sprintf(eos(linebuf), ".");
428 register char *bp = eos(linebuf);
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);
439 if(so == 0) puts(linebuf);
441 register char *bp = eos(linebuf);
442 if(so >= COLNO) so = COLNO-1;
443 while(bp < linebuf + so) *bp++ = ' ';
446 fputs(linebuf,stdout);
448 (void) putchar('\n');
450 return(strlen(linebuf));
462 register int d = n%10;
463 return((d==0 || d>3 || n/10==1) ? "th" : (d==1) ? "st" :
464 (d==2) ? "nd" : "rd");
469 (void) signal(SIGHUP,SIG_IGN);
470 for(x = maxdlevel; x >= 0; x--) {
472 (void) unlink(lock); /* not all levels need be present */
476 #ifdef NOSAVEONHANGUP
479 (void) signal(SIGINT, SIG_IGN);
483 #endif NOSAVEONHANGUP
493 /* it is the callers responsibility to check that there is room for c */
494 charcat(s,c) register char *s, c; {
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
505 prscore(argc,argv) int argc; char **argv; {
510 register struct toptenentry *t1, *t2;
511 char *recfile = RECORD;
516 long total_score = 0L;
520 int outflg = (argc >= -1);
527 if(!(rfile = fopen(recfile,"r"))){
528 puts("Cannot open record file!");
532 if(argc > 1 && !strncmp(argv[1], "-s", 2)){
536 } else if(!argv[1][3] && index("CFKSTWX", argv[1][2])) {
548 player0 = "hackplayer";
556 if(outflg) putchar('\n');
558 t1 = tt_head = newttentry();
559 for(rank = 1; ; rank++) {
560 if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
562 &t1->level, &t1->maxlvl,
563 &t1->hp, &t1->maxhp, &t1->points,
564 &t1->plchar, &t1->sex, t1->name, t1->death) != 11)
566 if(t1->points == 0) break;
568 if(!playerct && t1->uid == 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])))
581 t1 = t1->tt_next = newttentry();
583 (void) fclose(rfile);
586 printf("Cannot find any entries for ");
587 if(playerct < 1) printf("you.\n");
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);
598 if(outflg) outheader();
600 for(rank = 1; t1->points != 0; rank++, t1 = t2) {
603 if(!playerct && t1->uid == 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]))){
616 (void) outentry(rank, t1, 0);
618 total_score += t1->points;
619 if(totcharct < sizeof(totchars)-1)
620 totchars[totcharct++] = t1->plchar;
628 totchars[totcharct] = 0;
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);
636 if(!index(totchars, "CFKSTWX"[i])) {
638 if(!pl_character[0]) pl_character[0] = "CFKSTWX"[i];