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 /* $DragonFly: src/games/hack/hack.end.c,v 1.4 2005/05/22 03:37:05 y0netan1 Exp $ */
9 #define Sprintf (void) sprintf
10 extern char plname[], pl_character[];
11 extern char *itoa(), *eos();
12 static const char *ordin(int);
19 (void) signal(SIGINT,SIG_IGN);
20 pline("Really quit?");
21 if(readchar() != 'y') {
22 (void) signal(SIGINT,done1);
24 (void) fflush(stdout);
25 if(multi > 0) nomul(0);
38 (void) signal(SIGINT, SIG_IGN);
39 (void) signal(SIGQUIT, SIG_IGN);
45 (void) signal(SIGHUP, SIG_IGN);
49 done_in_by(mtmp) struct monst *mtmp; {
50 static char buf[BUFSZ];
52 if(mtmp->data->mlet == ' '){
53 Sprintf(buf, "the ghost of %s", (char *) mtmp->mextra);
55 } else if(mtmp->mnamelth) {
56 Sprintf(buf, "%s called %s",
57 mtmp->data->mname, NAME(mtmp));
59 } else if(mtmp->minvis) {
60 Sprintf(buf, "invisible %s", mtmp->data->mname);
62 } else killer = mtmp->data->mname;
66 /* called with arg "died", "drowned", "escaped", "quit", "choked", "panicked",
67 "burned", "starved" or "tricked" */
68 /* Be careful not to call panic from here! */
74 if(wizard && *st1 == 'd'){
76 if(u.uhpmax < 0) u.uhpmax = 100; /* arbitrary */
78 pline("For some reason you are still alive.");
80 if(multi > 0) multi = 0; else multi = -1;
85 (void) signal(SIGINT, done_intr);
86 (void) signal(SIGQUIT, done_intr);
87 (void) signal(SIGHUP, done_hangup);
88 if(*st1 == 'q' && u.uhp < 1){
90 killer = "quit while already on Charon's boat";
92 if(*st1 == 's') killer = "starvation"; else
93 if(*st1 == 'd' && st1[1] == 'r') killer = "drowning"; else
94 if(*st1 == 'p') killer = "panic"; else
95 if(*st1 == 't') killer = "trickery"; else
96 if(!index("bcd", *st1)) killer = st1;
99 if(flags.toplin == 1) more();
100 if(index("bcds", *st1)){
105 if(!flags.notombstone)
108 if(*st1 == 'c') killer = st1; /* after outrip() */
109 settty((char *) 0); /* does a clear_screen() */
111 printf("Goodbye %s %s...\n\n", pl_character, plname);
113 tmp = u.ugold - u.ugold0;
116 if(*st1 == 'd' || *st1 == 'b')
119 u.urexp += 50 * maxdlevel;
121 u.urexp += 1000*((maxdlevel > 30) ? 10 : maxdlevel - 20);
124 extern struct monst *mydogs;
128 unsigned worthlessct = 0;
129 boolean has_amulet = FALSE;
135 if(!done_stopprint) printf("You");
138 printf(" and %s", monnam(mtmp));
140 u.urexp += mtmp->mhp;
144 printf("\nescaped from the dungeon with %ld points,\n",
148 printf("You escaped from the dungeon with %ld points,\n",
150 for(otmp = invent; otmp; otmp = otmp->nobj) {
151 if(otmp->olet == GEM_SYM){
152 objects[otmp->otyp].oc_name_known = 1;
153 i = otmp->quan*objects[otmp->otyp].g_val;
155 worthlessct += otmp->quan;
160 printf("\t%s (worth %d Zorkmids),\n",
162 } else if(otmp->olet == AMULET_SYM) {
164 i = (otmp->spe < 0) ? 2 : 5000;
167 printf("\t%s (worth %d Zorkmids),\n",
171 killer = "escaped (with amulet)";
175 if(worthlessct) if(!done_stopprint)
176 printf("\t%u worthless piece%s of coloured glass,\n",
177 worthlessct, plur(worthlessct));
178 if(has_amulet) u.urexp *= 2;
181 printf("You %s on dungeon level %d with %ld points,\n",
182 st1, dlevel, u.urexp);
184 printf("and %ld piece%s of gold, after %ld move%s.\n",
185 u.ugold, plur(u.ugold), moves, plur(moves));
187 printf("You were level %u with a maximum of %d hit points when you %s.\n",
188 u.ulevel, u.uhpmax, st1);
189 if(*st1 == 'e' && !done_stopprint){
190 getret(); /* all those pieces of coloured glass ... */
197 if(done_stopprint) printf("\n\n");
201 #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
205 #define POINTSMIN 1 /* must be > 0 */
206 #define ENTRYMAX 100 /* must be >= 10 */
207 #define PERS_IS_UID /* delete for PERSMAX per name; now per uid */
209 struct toptenentry *tt_next;
211 int level,maxlvl,hp,maxhp;
217 char date[7]; /* yymmdd */
222 int rank, rank0 = -1, rank1 = 0;
223 int occ_cnt = PERSMAX;
224 struct toptenentry *t0, *t1, *tprev;
225 const char *recfile = RECORD;
226 const char *reclock = "record_lock";
230 extern char *getdate();
231 #define HUP if(!done_hup)
232 while(link(recfile, reclock) == -1) {
235 HUP puts("I give up. Sorry.");
236 HUP puts("Perhaps there is an old record_lock around?");
239 HUP printf("Waiting for access to record file. (%d)\n",
241 HUP (void) fflush(stdout);
244 if(!(rfile = fopen(recfile,"r"))){
245 HUP puts("Cannot open record file!");
248 HUP (void) putchar('\n');
250 /* create a new 'topten' entry */
253 t0->maxlvl = maxdlevel;
255 t0->maxhp = u.uhpmax;
256 t0->points = u.urexp;
257 t0->plchar = pl_character[0];
258 t0->sex = (flags.female ? 'F' : 'M');
260 (void) strncpy(t0->name, plname, NAMSZ);
261 (t0->name)[NAMSZ] = 0;
262 (void) strncpy(t0->death, killer, DTHSZ);
263 (t0->death)[DTHSZ] = 0;
264 (void) strcpy(t0->date, getdate());
266 /* assure minimum number of points */
267 if(t0->points < POINTSMIN)
270 t1 = tt_head = newttentry();
272 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
274 if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
276 &t1->level, &t1->maxlvl,
277 &t1->hp, &t1->maxhp, &t1->points,
278 &t1->plchar, &t1->sex, t1->name, t1->death) != 11
279 || t1->points < POINTSMIN)
281 if(rank0 < 0 && t1->points < t0->points) {
289 flg++; /* ask for a rewrite */
292 if(t1->points == 0) break;
295 t1->uid == t0->uid &&
297 strncmp(t1->name, t0->name, NAMSZ) == 0 &&
298 #endif /* PERS_IS_UID */
299 t1->plchar == t0->plchar && --occ_cnt <= 0){
303 HUP printf("You didn't beat your previous score of %ld points.\n\n",
311 if(rank <= ENTRYMAX){
312 t1 = t1->tt_next = newttentry();
320 if(flg) { /* rewrite record file */
321 (void) fclose(rfile);
322 if(!(rfile = fopen(recfile,"w"))){
323 HUP puts("Cannot write record file\n");
327 if(!done_stopprint) if(rank0 > 0){
329 puts("You made the top ten list!\n");
331 printf("You reached the %d%s place on the top %d list.\n\n",
332 rank0, ordin(rank0), ENTRYMAX);
335 if(rank0 == 0) rank0 = rank1;
336 if(rank0 <= 0) rank0 = rank;
337 if(!done_stopprint) outheader();
339 for(rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) {
340 if(flg) fprintf(rfile,"%6s %d %d %d %d %d %ld %c%c %s,%s\n",
342 t1->level, t1->maxlvl,
343 t1->hp, t1->maxhp, t1->points,
344 t1->plchar, t1->sex, t1->name, t1->death);
345 if(done_stopprint) continue;
346 if(rank > flags.end_top &&
347 (rank < rank0-flags.end_around || rank > rank0+flags.end_around)
348 && (!flags.end_own ||
350 t1->uid != t0->uid ))
352 strncmp(t1->name, t0->name, NAMSZ)))
353 #endif /* PERS_IS_UID */
355 if(rank == rank0-flags.end_around &&
356 rank0 > flags.end_top+flags.end_around+1 &&
358 (void) putchar('\n');
360 (void) outentry(rank, t1, 0);
362 (void) outentry(rank, t1, 1);
364 int t0lth = outentry(0, t0, -1);
365 int t1lth = outentry(rank, t1, t0lth);
366 if(t1lth > t0lth) t0lth = t1lth;
367 (void) outentry(0, t0, t0lth);
370 if(rank0 >= rank) if(!done_stopprint)
371 (void) outentry(0, t0, 1);
372 (void) fclose(rfile);
374 (void) unlink(reclock);
380 (void) strcpy(linebuf, "Number Points Name");
382 while(bp < linebuf + COLNO - 9) *bp++ = ' ';
383 (void) strcpy(bp, "Hp [max]");
387 /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
389 outentry(rank,t1,so) struct toptenentry *t1; {
390 boolean quit = FALSE, killed = FALSE, starv = FALSE;
393 if(rank) Sprintf(eos(linebuf), "%3d", rank);
394 else Sprintf(eos(linebuf), " ");
395 Sprintf(eos(linebuf), " %6ld %8s", t1->points, t1->name);
396 if(t1->plchar == 'X') Sprintf(eos(linebuf), " ");
397 else Sprintf(eos(linebuf), "-%c ", t1->plchar);
398 if(!strncmp("escaped", t1->death, 7)) {
399 if(!strcmp(" (with amulet)", t1->death+7))
400 Sprintf(eos(linebuf), "escaped the dungeon with amulet");
402 Sprintf(eos(linebuf), "escaped the dungeon [max level %d]",
405 if(!strncmp(t1->death,"quit",4)) {
407 if(t1->maxhp < 3*t1->hp && t1->maxlvl < 4)
408 Sprintf(eos(linebuf), "cravenly gave up");
410 Sprintf(eos(linebuf), "quit");
412 else if(!strcmp(t1->death,"choked"))
413 Sprintf(eos(linebuf), "choked on %s food",
414 (t1->sex == 'F') ? "her" : "his");
415 else if(!strncmp(t1->death,"starv",5))
416 Sprintf(eos(linebuf), "starved to death"), starv = TRUE;
417 else Sprintf(eos(linebuf), "was killed"), killed = TRUE;
418 Sprintf(eos(linebuf), " on%s level %d",
419 (killed || starv) ? "" : " dungeon", t1->level);
420 if(t1->maxlvl != t1->level)
421 Sprintf(eos(linebuf), " [max %d]", t1->maxlvl);
422 if(quit && t1->death[4]) Sprintf(eos(linebuf), t1->death + 4);
424 if(killed) Sprintf(eos(linebuf), " by %s%s",
425 (!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4))
427 index(vowels,*t1->death) ? "an " : "a ",
429 Sprintf(eos(linebuf), ".");
431 char *bp = eos(linebuf);
434 Sprintf(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-");
435 hppos = COLNO - 7 - strlen(hpbuf);
436 if(bp <= linebuf + hppos) {
437 while(bp < linebuf + hppos) *bp++ = ' ';
438 (void) strcpy(bp, hpbuf);
439 Sprintf(eos(bp), " [%d]", t1->maxhp);
442 if(so == 0) puts(linebuf);
444 char *bp = eos(linebuf);
445 if(so >= COLNO) so = COLNO-1;
446 while(bp < linebuf + so) *bp++ = ' ';
449 fputs(linebuf,stdout);
451 (void) putchar('\n');
453 return(strlen(linebuf));
467 return((d==0 || d>3 || n/10==1) ? "th" : (d==1) ? "st" :
468 (d==2) ? "nd" : "rd");
473 (void) signal(SIGHUP,SIG_IGN);
474 for(x = maxdlevel; x >= 0; x--) {
476 (void) unlink(lock); /* not all levels need be present */
480 #ifdef NOSAVEONHANGUP
483 (void) signal(SIGINT, SIG_IGN);
487 #endif /* NOSAVEONHANGUP */
497 /* it is the callers responsibility to check that there is room for c */
498 charcat(s,c) char *s, c; {
505 * Called with args from main if argc >= 0. In this case, list scores as
506 * requested. Otherwise, find scores for the current player (and list them
509 prscore(argc,argv) int argc; char **argv; {
514 struct toptenentry *t1, *t2;
515 const char *recfile = RECORD;
520 long total_score = 0L;
523 #endif /* nonsense */
524 int outflg = (argc >= -1);
529 #endif /* PERS_IS_UID */
531 if(!(rfile = fopen(recfile,"r"))){
532 puts("Cannot open record file!");
536 if(argc > 1 && !strncmp(argv[1], "-s", 2)){
540 } else if(!argv[1][3] && index("CFKSTWX", argv[1][2])) {
552 player0 = "hackplayer";
555 #endif /* PERS_IS_UID */
560 if(outflg) putchar('\n');
562 t1 = tt_head = newttentry();
563 for(rank = 1; ; rank++) {
564 if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
566 &t1->level, &t1->maxlvl,
567 &t1->hp, &t1->maxhp, &t1->points,
568 &t1->plchar, &t1->sex, t1->name, t1->death) != 11)
570 if(t1->points == 0) break;
572 if(!playerct && t1->uid == uid)
575 #endif /* PERS_IS_UID */
576 for(i = 0; i < playerct; i++){
577 if(strcmp(players[i], "all") == 0 ||
578 strncmp(t1->name, players[i], NAMSZ) == 0 ||
579 (players[i][0] == '-' &&
580 players[i][1] == t1->plchar &&
581 players[i][2] == 0) ||
582 (digit(players[i][0]) && rank <= atoi(players[i])))
585 t1 = t1->tt_next = newttentry();
587 (void) fclose(rfile);
590 printf("Cannot find any entries for ");
591 if(playerct < 1) printf("you.\n");
593 if(playerct > 1) printf("any of ");
594 for(i=0; i<playerct; i++)
595 printf("%s%s", players[i], (i<playerct-1)?", ":".\n");
596 printf("Call is: %s -s [playernames]\n", hname);
602 if(outflg) outheader();
604 for(rank = 1; t1->points != 0; rank++, t1 = t2) {
607 if(!playerct && t1->uid == uid)
610 #endif /* PERS_IS_UID */
611 for(i = 0; i < playerct; i++){
612 if(strcmp(players[i], "all") == 0 ||
613 strncmp(t1->name, players[i], NAMSZ) == 0 ||
614 (players[i][0] == '-' &&
615 players[i][1] == t1->plchar &&
616 players[i][2] == 0) ||
617 (digit(players[i][0]) && rank <= atoi(players[i]))){
620 (void) outentry(rank, t1, 0);
622 total_score += t1->points;
623 if(totcharct < sizeof(totchars)-1)
624 totchars[totcharct++] = t1->plchar;
625 #endif /* nonsense */
632 totchars[totcharct] = 0;
634 /* We would like to determine whether he is experienced. However,
635 the information collected here only tells about the scores/roles
636 that got into the topten (top 100?). We should maintain a
637 .hacklog or something in his home directory. */
638 flags.beginner = (total_score < 6000);
640 if(!index(totchars, "CFKSTWX"[i])) {
642 if(!pl_character[0]) pl_character[0] = "CFKSTWX"[i];
645 #endif /* nonsense */