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.5 2006/08/21 19:45:32 pavalos Exp $ */
7 #define Sprintf (void) sprintf
9 #define newttentry() alloc(sizeof(struct toptenentry))
13 #define POINTSMIN 1 /* must be > 0 */
14 #define ENTRYMAX 100 /* must be >= 10 */
15 #define PERS_IS_UID /* delete for PERSMAX per name; now per uid */
17 struct toptenentry *tt_next;
19 int level,maxlvl,hp,maxhp;
25 char date[7]; /* yymmdd */
28 static void done_intr(int);
29 static void done_hangup(int);
30 static void topten(void);
31 static void outheader(void);
32 static int outentry(int, struct toptenentry *, int);
33 static char *itoa(int);
34 static const char *ordin(int);
39 done1(int unused __unused)
41 signal(SIGINT, SIG_IGN);
42 pline("Really quit?");
43 if (readchar() != 'y') {
44 signal(SIGINT, done1);
59 done_intr(int unused __unused)
62 signal(SIGINT, SIG_IGN);
63 signal(SIGQUIT, SIG_IGN);
67 done_hangup(int unused __unused)
70 signal(SIGHUP, SIG_IGN);
75 done_in_by(struct monst *mtmp)
77 static char buf[BUFSZ];
80 if (mtmp->data->mlet == ' ') {
81 Sprintf(buf, "the ghost of %s", (char *)mtmp->mextra);
83 } else if (mtmp->mnamelth) {
84 Sprintf(buf, "%s called %s",
85 mtmp->data->mname, NAME(mtmp));
87 } else if (mtmp->minvis) {
88 Sprintf(buf, "invisible %s", mtmp->data->mname);
91 killer = mtmp->data->mname;
96 * called with arg "died", "drowned", "escaped", "quit", "choked",
97 * "panicked", "burned", "starved" or "tricked"
99 /* Be careful not to call panic from here! */
101 done(const char *st1)
104 if (wizard && *st1 == 'd') {
106 if (u.uhpmax < 0) /* arbitrary */
109 pline("For some reason you are still alive.");
119 signal(SIGINT, done_intr);
120 signal(SIGQUIT, done_intr);
121 signal(SIGHUP, done_hangup);
122 if (*st1 == 'q' && u.uhp < 1) {
124 killer = "quit while already on Charon's boat";
127 killer = "starvation";
128 else if (*st1 == 'd' && st1[1] == 'r')
130 else if (*st1 == 'p')
132 else if (*st1 == 't')
134 else if (!strchr("bcd", *st1))
138 if (flags.toplin == 1)
140 if (strchr("bcds", *st1)) {
145 if (!flags.notombstone)
148 if (*st1 == 'c') /* after outrip() */
150 settty(NULL); /* does a clear_screen() */
152 printf("Goodbye %s %s...\n\n", pl_character, plname);
155 tmp = u.ugold - u.ugold0;
158 if (*st1 == 'd' || *st1 == 'b')
161 u.urexp += 50 * maxdlevel;
163 u.urexp += 1000 * ((maxdlevel > 30) ? 10 : maxdlevel - 20);
169 unsigned worthlessct = 0;
170 boolean has_amulet = FALSE;
180 printf(" and %s", monnam(mtmp));
182 u.urexp += mtmp->mhp;
186 printf("\nescaped from the dungeon with %ld points,\n",
188 } else if (!done_stopprint)
189 printf("You escaped from the dungeon with %ld points,\n",
191 for (otmp = invent; otmp; otmp = otmp->nobj) {
192 if (otmp->olet == GEM_SYM) {
193 objects[otmp->otyp].oc_name_known = 1;
194 i = otmp->quan * objects[otmp->otyp].g_val;
196 worthlessct += otmp->quan;
201 printf("\t%s (worth %d Zorkmids),\n",
203 } else if (otmp->olet == AMULET_SYM) {
205 i = (otmp->spe < 0) ? 2 : 5000;
208 printf("\t%s (worth %d Zorkmids),\n",
210 if (otmp->spe >= 0) {
212 killer = "escaped (with amulet)";
218 printf("\t%u worthless piece%s of coloured glass,\n",
219 worthlessct, plur(worthlessct));
222 } else if (!done_stopprint)
223 printf("You %s on dungeon level %d with %ld points,\n",
224 st1, dlevel, u.urexp);
226 printf("and %ld piece%s of gold, after %ld move%s.\n",
227 u.ugold, plur(u.ugold), moves, plur(moves));
229 printf("You were level %u with a maximum of %d hit points when you %s.\n",
230 u.ulevel, u.uhpmax, st1);
231 if (*st1 == 'e' && !done_stopprint) {
232 getret(); /* all those pieces of coloured glass ... */
248 int rank, rank0 = -1, rank1 = 0;
249 int occ_cnt = PERSMAX;
250 struct toptenentry *t0, *t1, *tprev;
251 const char *recfile = RECORD;
252 const char *reclock = "record_lock";
257 #define HUP if (!done_hup)
258 while (link(recfile, reclock) == -1) {
261 HUP puts("I give up. Sorry.");
262 HUP puts("Perhaps there is an old record_lock around?");
265 HUP printf("Waiting for access to record file. (%d)\n",
270 if (!(rfile = fopen(recfile, "r"))) {
271 HUP puts("Cannot open record file!");
276 /* create a new 'topten' entry */
279 t0->maxlvl = maxdlevel;
281 t0->maxhp = u.uhpmax;
282 t0->points = u.urexp;
283 t0->plchar = pl_character[0];
284 t0->sex = (flags.female ? 'F' : 'M');
286 strncpy(t0->name, plname, NAMSZ);
287 (t0->name)[NAMSZ] = 0;
288 strncpy(t0->death, killer, DTHSZ);
289 (t0->death)[DTHSZ] = 0;
290 strcpy(t0->date, getdate());
292 /* assure minimum number of points */
293 if (t0->points < POINTSMIN)
296 t1 = tt_head = newttentry();
298 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
300 if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
302 &t1->level, &t1->maxlvl,
303 &t1->hp, &t1->maxhp, &t1->points,
304 &t1->plchar, &t1->sex, t1->name, t1->death) != 11
305 || t1->points < POINTSMIN)
307 if (rank0 < 0 && t1->points < t0->points) {
315 flg++; /* ask for a rewrite */
322 t1->uid == t0->uid &&
324 strncmp(t1->name, t0->name, NAMSZ) == 0 &&
325 #endif /* PERS_IS_UID */
326 t1->plchar == t0->plchar && --occ_cnt <= 0) {
330 HUP printf("You didn't beat your previous score of %ld points.\n\n",
338 if (rank <= ENTRYMAX) {
339 t1 = t1->tt_next = newttentry();
342 if (rank > ENTRYMAX) {
347 if (flg) { /* rewrite record file */
349 if (!(rfile = fopen(recfile, "w"))) {
350 HUP puts("Cannot write record file\n");
357 puts("You made the top ten list!\n");
359 printf("You reached the %d%s place on the top %d list.\n\n",
360 rank0, ordin(rank0), ENTRYMAX);
370 for (rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) {
372 fprintf(rfile, "%6s %d %d %d %d %d %ld %c%c %s,%s\n",
374 t1->level, t1->maxlvl,
375 t1->hp, t1->maxhp, t1->points,
376 t1->plchar, t1->sex, t1->name, t1->death);
379 if (rank > (int)flags.end_top &&
380 (rank < rank0 - (int)flags.end_around || rank > rank0 +
381 (int)flags.end_around)
382 && (!flags.end_own ||
386 strncmp(t1->name, t0->name, NAMSZ)))
387 #endif /* PERS_IS_UID */
389 if (rank == rank0 - (int)flags.end_around &&
390 rank0 > (int)flags.end_top + (int)flags.end_around + 1 &&
394 outentry(rank, t1, 0);
396 outentry(rank, t1, 1);
398 int t0lth = outentry(0, t0, -1);
399 int t1lth = outentry(rank, t1, t0lth);
402 outentry(0, t0, t0lth);
419 strcpy(linebuf, "Number Points Name");
421 while (bp < linebuf + COLNO - 9)
423 strcpy(bp, "Hp [max]");
427 /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
429 outentry(int rank, struct toptenentry *t1, int so)
431 boolean quit = FALSE, dead = FALSE, starv = FALSE;
436 Sprintf(eos(linebuf), "%3d", rank);
438 Sprintf(eos(linebuf), " ");
439 Sprintf(eos(linebuf), " %6ld %8s", t1->points, t1->name);
440 if (t1->plchar == 'X')
441 Sprintf(eos(linebuf), " ");
443 Sprintf(eos(linebuf), "-%c ", t1->plchar);
444 if (!strncmp("escaped", t1->death, 7)) {
445 if (!strcmp(" (with amulet)", t1->death + 7))
446 Sprintf(eos(linebuf), "escaped the dungeon with amulet");
448 Sprintf(eos(linebuf), "escaped the dungeon [max level %d]",
451 if (!strncmp(t1->death, "quit", 4)) {
453 if (t1->maxhp < 3 * t1->hp && t1->maxlvl < 4)
454 Sprintf(eos(linebuf), "cravenly gave up");
456 Sprintf(eos(linebuf), "quit");
457 } else if (!strcmp(t1->death, "choked")) {
458 Sprintf(eos(linebuf), "choked on %s food",
459 (t1->sex == 'F') ? "her" : "his");
460 } else if (!strncmp(t1->death, "starv", 5)) {
461 Sprintf(eos(linebuf), "starved to death");
464 Sprintf(eos(linebuf), "was killed");
467 Sprintf(eos(linebuf), " on%s level %d",
468 (dead || starv) ? "" : " dungeon", t1->level);
469 if (t1->maxlvl != t1->level)
470 Sprintf(eos(linebuf), " [max %d]", t1->maxlvl);
471 if (quit && t1->death[4])
472 Sprintf(eos(linebuf), t1->death + 4);
475 Sprintf(eos(linebuf), " by %s%s",
476 (!strncmp(t1->death, "trick", 5) ||
477 !strncmp(t1->death, "the ", 4))
479 strchr(vowels, *t1->death) ? "an " : "a ",
482 Sprintf(eos(linebuf), ".");
484 char *bp = eos(linebuf);
488 Sprintf(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-");
489 hppos = COLNO - 7 - strlen(hpbuf);
490 if (bp <= linebuf + hppos) {
491 while (bp < linebuf + hppos)
494 Sprintf(eos(bp), " [%d]", t1->maxhp);
500 char *bp = eos(linebuf);
503 while (bp < linebuf + so)
507 fputs(linebuf, stdout);
511 return (strlen(linebuf));
519 Sprintf(buf, "%d", a);
528 return ((d1 == 0 || d1 > 3 || n / 10 == 1) ? "th" : (d1 == 1) ? "st" :
529 (d1 == 2) ? "nd" : "rd");
537 signal(SIGHUP, SIG_IGN);
538 for (x = maxdlevel; x >= 0; x--) {
540 unlink(lock); /* not all levels need be present */
544 #ifdef NOSAVEONHANGUP
546 hangup(int unused __unused)
548 signal(SIGINT, SIG_IGN);
552 #endif /* NOSAVEONHANGUP */
562 /* it is the callers responsibility to check that there is room for c */
564 charcat(char *s, char c)
573 * Called with args from main if argc >= 0. In this case, list scores as
574 * requested. Otherwise, find scores for the current player (and list them
578 prscore(int argc, char **argv)
580 char **players = NULL;
583 struct toptenentry *t1, *t2;
584 const char *recfile = RECORD;
589 long total_score = 0L;
592 #endif /* nonsense */
593 int outflg = (argc >= -1);
598 #endif /* PERS_IS_UID */
600 if (!(rfile = fopen(recfile, "r"))) {
601 puts("Cannot open record file!");
605 if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
609 } else if (!argv[1][3] && strchr("CFKSTWX", argv[1][2])) {
622 player0 = "hackplayer";
625 #endif /* PERS_IS_UID */
633 t1 = tt_head = newttentry();
634 for (rank = 1;; rank++) {
635 if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
637 &t1->level, &t1->maxlvl,
638 &t1->hp, &t1->maxhp, &t1->points,
639 &t1->plchar, &t1->sex, t1->name, t1->death) != 11)
644 if (!playerct && t1->uid == uid)
647 #endif /* PERS_IS_UID */
648 for (i = 0; i < playerct; i++) {
649 if (strcmp(players[i], "all") == 0 ||
650 strncmp(t1->name, players[i], NAMSZ) == 0 ||
651 (players[i][0] == '-' &&
652 players[i][1] == t1->plchar &&
653 players[i][2] == 0) ||
654 (digit(players[i][0]) && rank <= atoi(players[i])))
657 t1 = t1->tt_next = newttentry();
662 printf("Cannot find any entries for ");
668 for (i = 0; i < playerct; i++)
669 printf("%s%s", players[i],
670 (i < playerct - 1) ? ", " : ".\n");
671 printf("Call is: %s -s [playernames]\n", hname);
680 for (rank = 1; t1->points != 0; rank++, t1 = t2) {
683 if (!playerct && t1->uid == uid)
686 #endif /* PERS_IS_UID */
687 for (i = 0; i < playerct; i++) {
688 if (strcmp(players[i], "all") == 0 ||
689 strncmp(t1->name, players[i], NAMSZ) == 0 ||
690 (players[i][0] == '-' &&
691 players[i][1] == t1->plchar &&
692 players[i][2] == 0) ||
693 (digit(players[i][0]) && rank <=
697 outentry(rank, t1, 0);
699 total_score += t1->points;
700 if (totcharct < sizeof(totchars) - 1)
701 totchars[totcharct++] = t1->plchar;
702 #endif /* nonsense */
709 totchars[totcharct] = 0;
712 * We would like to determine whether he is experienced. However, the
713 * information collected here only tells about the scores/roles that
714 * got into the topten (top 100?). We should maintain a .hacklog or
715 * something in his home directory.
717 flags.beginner = (total_score < 6000);
718 for (i = 0; i < 6; i++)
719 if (!strchr(totchars, "CFKSTWX"[i])) {
721 if (!pl_character[0])
722 pl_character[0] = "CFKSTWX"[i];
725 #endif /* nonsense */