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 $ */
8 #define Sprintf (void) sprintf
9 extern char plname[], pl_character[];
10 extern char *itoa(), *ordin(), *eos();
17 (void) signal(SIGINT,SIG_IGN);
18 pline("Really quit?");
19 if(readchar() != 'y') {
20 (void) signal(SIGINT,done1);
22 (void) fflush(stdout);
23 if(multi > 0) nomul(0);
36 (void) signal(SIGINT, SIG_IGN);
37 (void) signal(SIGQUIT, SIG_IGN);
43 (void) signal(SIGHUP, SIG_IGN);
47 done_in_by(mtmp) struct monst *mtmp; {
48 static char buf[BUFSZ];
50 if(mtmp->data->mlet == ' '){
51 Sprintf(buf, "the ghost of %s", (char *) mtmp->mextra);
53 } else if(mtmp->mnamelth) {
54 Sprintf(buf, "%s called %s",
55 mtmp->data->mname, NAME(mtmp));
57 } else if(mtmp->minvis) {
58 Sprintf(buf, "invisible %s", mtmp->data->mname);
60 } else killer = mtmp->data->mname;
64 /* called with arg "died", "drowned", "escaped", "quit", "choked", "panicked",
65 "burned", "starved" or "tricked" */
66 /* Be careful not to call panic from here! */
72 if(wizard && *st1 == 'd'){
74 if(u.uhpmax < 0) u.uhpmax = 100; /* arbitrary */
76 pline("For some reason you are still alive.");
78 if(multi > 0) multi = 0; else multi = -1;
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){
88 killer = "quit while already on Charon's boat";
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;
97 if(flags.toplin == 1) more();
98 if(index("bcds", *st1)){
103 if(!flags.notombstone)
106 if(*st1 == 'c') killer = st1; /* after outrip() */
107 settty((char *) 0); /* does a clear_screen() */
109 printf("Goodbye %s %s...\n\n", pl_character, plname);
111 tmp = u.ugold - u.ugold0;
114 if(*st1 == 'd' || *st1 == 'b')
117 u.urexp += 50 * maxdlevel;
119 u.urexp += 1000*((maxdlevel > 30) ? 10 : maxdlevel - 20);
122 extern struct monst *mydogs;
126 unsigned worthlessct = 0;
127 boolean has_amulet = FALSE;
133 if(!done_stopprint) printf("You");
136 printf(" and %s", monnam(mtmp));
138 u.urexp += mtmp->mhp;
142 printf("\nescaped from the dungeon with %ld points,\n",
146 printf("You escaped from the dungeon with %ld points,\n",
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;
153 worthlessct += otmp->quan;
158 printf("\t%s (worth %d Zorkmids),\n",
160 } else if(otmp->olet == AMULET_SYM) {
162 i = (otmp->spe < 0) ? 2 : 5000;
165 printf("\t%s (worth %d Zorkmids),\n",
169 killer = "escaped (with amulet)";
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;
179 printf("You %s on dungeon level %d with %ld points,\n",
180 st1, dlevel, u.urexp);
182 printf("and %ld piece%s of gold, after %ld move%s.\n",
183 u.ugold, plur(u.ugold), moves, plur(moves));
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 ... */
195 if(done_stopprint) printf("\n\n");
199 #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
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 */
207 struct toptenentry *tt_next;
209 int level,maxlvl,hp,maxhp;
215 char date[7]; /* yymmdd */
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";
228 extern char *getdate();
229 #define HUP if(!done_hup)
230 while(link(recfile, reclock) == -1) {
233 HUP puts("I give up. Sorry.");
234 HUP puts("Perhaps there is an old record_lock around?");
237 HUP printf("Waiting for access to record file. (%d)\n",
239 HUP (void) fflush(stdout);
242 if(!(rfile = fopen(recfile,"r"))){
243 HUP puts("Cannot open record file!");
246 HUP (void) putchar('\n');
248 /* create a new 'topten' entry */
251 t0->maxlvl = maxdlevel;
253 t0->maxhp = u.uhpmax;
254 t0->points = u.urexp;
255 t0->plchar = pl_character[0];
256 t0->sex = (flags.female ? 'F' : 'M');
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());
264 /* assure minimum number of points */
265 if(t0->points < POINTSMIN)
268 t1 = tt_head = newttentry();
270 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
272 if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
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)
279 if(rank0 < 0 && t1->points < t0->points) {
287 flg++; /* ask for a rewrite */
290 if(t1->points == 0) break;
293 t1->uid == t0->uid &&
295 strncmp(t1->name, t0->name, NAMSZ) == 0 &&
297 t1->plchar == t0->plchar && --occ_cnt <= 0){
301 HUP printf("You didn't beat your previous score of %ld points.\n\n",
309 if(rank <= ENTRYMAX){
310 t1 = t1->tt_next = newttentry();
318 if(flg) { /* rewrite record file */
319 (void) fclose(rfile);
320 if(!(rfile = fopen(recfile,"w"))){
321 HUP puts("Cannot write record file\n");
325 if(!done_stopprint) if(rank0 > 0){
327 puts("You made the top ten list!\n");
329 printf("You reached the %d%s place on the top %d list.\n\n",
330 rank0, ordin(rank0), ENTRYMAX);
333 if(rank0 == 0) rank0 = rank1;
334 if(rank0 <= 0) rank0 = rank;
335 if(!done_stopprint) outheader();
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",
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 ||
348 t1->uid != t0->uid ))
350 strncmp(t1->name, t0->name, NAMSZ)))
353 if(rank == rank0-flags.end_around &&
354 rank0 > flags.end_top+flags.end_around+1 &&
356 (void) putchar('\n');
358 (void) outentry(rank, t1, 0);
360 (void) outentry(rank, t1, 1);
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);
368 if(rank0 >= rank) if(!done_stopprint)
369 (void) outentry(0, t0, 1);
370 (void) fclose(rfile);
372 (void) unlink(reclock);
378 (void) strcpy(linebuf, "Number Points Name");
380 while(bp < linebuf + COLNO - 9) *bp++ = ' ';
381 (void) strcpy(bp, "Hp [max]");
385 /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
387 outentry(rank,t1,so) struct toptenentry *t1; {
388 boolean quit = FALSE, killed = FALSE, starv = FALSE;
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");
400 Sprintf(eos(linebuf), "escaped the dungeon [max level %d]",
403 if(!strncmp(t1->death,"quit",4)) {
405 if(t1->maxhp < 3*t1->hp && t1->maxlvl < 4)
406 Sprintf(eos(linebuf), "cravenly gave up");
408 Sprintf(eos(linebuf), "quit");
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);
422 if(killed) Sprintf(eos(linebuf), " by %s%s",
423 (!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4))
425 index(vowels,*t1->death) ? "an " : "a ",
427 Sprintf(eos(linebuf), ".");
429 char *bp = eos(linebuf);
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);
440 if(so == 0) puts(linebuf);
442 char *bp = eos(linebuf);
443 if(so >= COLNO) so = COLNO-1;
444 while(bp < linebuf + so) *bp++ = ' ';
447 fputs(linebuf,stdout);
449 (void) putchar('\n');
451 return(strlen(linebuf));
464 return((d==0 || d>3 || n/10==1) ? "th" : (d==1) ? "st" :
465 (d==2) ? "nd" : "rd");
470 (void) signal(SIGHUP,SIG_IGN);
471 for(x = maxdlevel; x >= 0; x--) {
473 (void) unlink(lock); /* not all levels need be present */
477 #ifdef NOSAVEONHANGUP
480 (void) signal(SIGINT, SIG_IGN);
484 #endif NOSAVEONHANGUP
494 /* it is the callers responsibility to check that there is room for c */
495 charcat(s,c) char *s, c; {
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
506 prscore(argc,argv) int argc; char **argv; {
511 struct toptenentry *t1, *t2;
512 char *recfile = RECORD;
517 long total_score = 0L;
521 int outflg = (argc >= -1);
528 if(!(rfile = fopen(recfile,"r"))){
529 puts("Cannot open record file!");
533 if(argc > 1 && !strncmp(argv[1], "-s", 2)){
537 } else if(!argv[1][3] && index("CFKSTWX", argv[1][2])) {
549 player0 = "hackplayer";
557 if(outflg) putchar('\n');
559 t1 = tt_head = newttentry();
560 for(rank = 1; ; rank++) {
561 if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
563 &t1->level, &t1->maxlvl,
564 &t1->hp, &t1->maxhp, &t1->points,
565 &t1->plchar, &t1->sex, t1->name, t1->death) != 11)
567 if(t1->points == 0) break;
569 if(!playerct && t1->uid == 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])))
582 t1 = t1->tt_next = newttentry();
584 (void) fclose(rfile);
587 printf("Cannot find any entries for ");
588 if(playerct < 1) printf("you.\n");
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);
599 if(outflg) outheader();
601 for(rank = 1; t1->points != 0; rank++, t1 = t2) {
604 if(!playerct && t1->uid == 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]))){
617 (void) outentry(rank, t1, 0);
619 total_score += t1->points;
620 if(totcharct < sizeof(totchars)-1)
621 totchars[totcharct++] = t1->plchar;
629 totchars[totcharct] = 0;
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);
637 if(!index(totchars, "CFKSTWX"[i])) {
639 if(!pl_character[0]) pl_character[0] = "CFKSTWX"[i];