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 $ */
6 #define Sprintf (void) sprintf
8 #define newttentry() alloc(sizeof(struct toptenentry))
12 #define POINTSMIN 1 /* must be > 0 */
13 #define ENTRYMAX 100 /* must be >= 10 */
14 #define PERS_IS_UID /* delete for PERSMAX per name; now per uid */
16 struct toptenentry
*tt_next
;
18 int level
,maxlvl
,hp
,maxhp
;
24 char date
[7]; /* yymmdd */
27 static void done_intr(int);
28 static void done_hangup(int);
29 static void topten(void);
30 static void outheader(void);
31 static int outentry(int, struct toptenentry
*, int);
32 static char *itoa(int);
33 static const char *ordin(int);
38 done1(int unused __unused
)
40 signal(SIGINT
, SIG_IGN
);
41 pline("Really quit?");
42 if (readchar() != 'y') {
43 signal(SIGINT
, done1
);
58 done_intr(int unused __unused
)
61 signal(SIGINT
, SIG_IGN
);
62 signal(SIGQUIT
, SIG_IGN
);
66 done_hangup(int unused __unused
)
69 signal(SIGHUP
, SIG_IGN
);
74 done_in_by(struct monst
*mtmp
)
76 static char buf
[BUFSZ
];
79 if (mtmp
->data
->mlet
== ' ') {
80 Sprintf(buf
, "the ghost of %s", (char *)mtmp
->mextra
);
82 } else if (mtmp
->mnamelth
) {
83 Sprintf(buf
, "%s called %s",
84 mtmp
->data
->mname
, NAME(mtmp
));
86 } else if (mtmp
->minvis
) {
87 Sprintf(buf
, "invisible %s", mtmp
->data
->mname
);
90 killer
= mtmp
->data
->mname
;
95 * called with arg "died", "drowned", "escaped", "quit", "choked",
96 * "panicked", "burned", "starved" or "tricked"
98 /* Be careful not to call panic from here! */
100 done(const char *st1
)
103 if (wizard
&& *st1
== 'd') {
105 if (u
.uhpmax
< 0) /* arbitrary */
108 pline("For some reason you are still alive.");
118 signal(SIGINT
, done_intr
);
119 signal(SIGQUIT
, done_intr
);
120 signal(SIGHUP
, done_hangup
);
121 if (*st1
== 'q' && u
.uhp
< 1) {
123 killer
= "quit while already on Charon's boat";
126 killer
= "starvation";
127 else if (*st1
== 'd' && st1
[1] == 'r')
129 else if (*st1
== 'p')
131 else if (*st1
== 't')
133 else if (!strchr("bcd", *st1
))
137 if (flags
.toplin
== 1)
139 if (strchr("bcds", *st1
)) {
144 if (!flags
.notombstone
)
147 if (*st1
== 'c') /* after outrip() */
149 settty(NULL
); /* does a clear_screen() */
151 printf("Goodbye %s %s...\n\n", pl_character
, plname
);
154 tmp
= u
.ugold
- u
.ugold0
;
157 if (*st1
== 'd' || *st1
== 'b')
160 u
.urexp
+= 50 * maxdlevel
;
162 u
.urexp
+= 1000 * ((maxdlevel
> 30) ? 10 : maxdlevel
- 20);
168 unsigned worthlessct
= 0;
169 boolean has_amulet
= FALSE
;
179 printf(" and %s", monnam(mtmp
));
181 u
.urexp
+= mtmp
->mhp
;
185 printf("\nescaped from the dungeon with %ld points,\n",
187 } else if (!done_stopprint
)
188 printf("You escaped from the dungeon with %ld points,\n",
190 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
191 if (otmp
->olet
== GEM_SYM
) {
192 objects
[otmp
->otyp
].oc_name_known
= 1;
193 i
= otmp
->quan
* objects
[otmp
->otyp
].g_val
;
195 worthlessct
+= otmp
->quan
;
200 printf("\t%s (worth %d Zorkmids),\n",
202 } else if (otmp
->olet
== AMULET_SYM
) {
204 i
= (otmp
->spe
< 0) ? 2 : 5000;
207 printf("\t%s (worth %d Zorkmids),\n",
209 if (otmp
->spe
>= 0) {
211 killer
= "escaped (with amulet)";
217 printf("\t%u worthless piece%s of coloured glass,\n",
218 worthlessct
, plur(worthlessct
));
221 } else if (!done_stopprint
)
222 printf("You %s on dungeon level %d with %ld points,\n",
223 st1
, dlevel
, u
.urexp
);
225 printf("and %ld piece%s of gold, after %ld move%s.\n",
226 u
.ugold
, plur(u
.ugold
), moves
, plur(moves
));
228 printf("You were level %u with a maximum of %d hit points when you %s.\n",
229 u
.ulevel
, u
.uhpmax
, st1
);
230 if (*st1
== 'e' && !done_stopprint
) {
231 getret(); /* all those pieces of coloured glass ... */
247 int rank
, rank0
= -1, rank1
= 0;
248 int occ_cnt
= PERSMAX
;
249 struct toptenentry
*t0
, *t1
, *tprev
;
250 const char *recfile
= RECORD
;
251 const char *reclock
= "record_lock";
256 #define HUP if (!done_hup)
257 while (link(recfile
, reclock
) == -1) {
260 HUP
puts("I give up. Sorry.");
261 HUP
puts("Perhaps there is an old record_lock around?");
264 HUP
printf("Waiting for access to record file. (%d)\n",
269 if (!(rfile
= fopen(recfile
, "r"))) {
270 HUP
puts("Cannot open record file!");
275 /* create a new 'topten' entry */
278 t0
->maxlvl
= maxdlevel
;
280 t0
->maxhp
= u
.uhpmax
;
281 t0
->points
= u
.urexp
;
282 t0
->plchar
= pl_character
[0];
283 t0
->sex
= (flags
.female
? 'F' : 'M');
285 strncpy(t0
->name
, plname
, NAMSZ
);
286 (t0
->name
)[NAMSZ
] = 0;
287 strncpy(t0
->death
, killer
, DTHSZ
);
288 (t0
->death
)[DTHSZ
] = 0;
289 strcpy(t0
->date
, getdate());
291 /* assure minimum number of points */
292 if (t0
->points
< POINTSMIN
)
295 t1
= tt_head
= newttentry();
297 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
299 if (fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
301 &t1
->level
, &t1
->maxlvl
,
302 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
303 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11
304 || t1
->points
< POINTSMIN
)
306 if (rank0
< 0 && t1
->points
< t0
->points
) {
314 flg
++; /* ask for a rewrite */
321 t1
->uid
== t0
->uid
&&
323 strncmp(t1
->name
, t0
->name
, NAMSZ
) == 0 &&
324 #endif /* PERS_IS_UID */
325 t1
->plchar
== t0
->plchar
&& --occ_cnt
<= 0) {
329 HUP
printf("You didn't beat your previous score of %ld points.\n\n",
337 if (rank
<= ENTRYMAX
) {
338 t1
= t1
->tt_next
= newttentry();
341 if (rank
> ENTRYMAX
) {
346 if (flg
) { /* rewrite record file */
348 if (!(rfile
= fopen(recfile
, "w"))) {
349 HUP
puts("Cannot write record file\n");
356 puts("You made the top ten list!\n");
358 printf("You reached the %d%s place on the top %d list.\n\n",
359 rank0
, ordin(rank0
), ENTRYMAX
);
369 for (rank
= 1; t1
->points
!= 0; rank
++, t1
= t1
->tt_next
) {
371 fprintf(rfile
, "%6s %d %d %d %d %d %ld %c%c %s,%s\n",
373 t1
->level
, t1
->maxlvl
,
374 t1
->hp
, t1
->maxhp
, t1
->points
,
375 t1
->plchar
, t1
->sex
, t1
->name
, t1
->death
);
378 if (rank
> (int)flags
.end_top
&&
379 (rank
< rank0
- (int)flags
.end_around
|| rank
> rank0
+
380 (int)flags
.end_around
)
381 && (!flags
.end_own
||
385 strncmp(t1
->name
, t0
->name
, NAMSZ
)))
386 #endif /* PERS_IS_UID */
388 if (rank
== rank0
- (int)flags
.end_around
&&
389 rank0
> (int)flags
.end_top
+ (int)flags
.end_around
+ 1 &&
393 outentry(rank
, t1
, 0);
395 outentry(rank
, t1
, 1);
397 int t0lth
= outentry(0, t0
, -1);
398 int t1lth
= outentry(rank
, t1
, t0lth
);
401 outentry(0, t0
, t0lth
);
418 strcpy(linebuf
, "Number Points Name");
420 while (bp
< linebuf
+ COLNO
- 9)
422 strcpy(bp
, "Hp [max]");
426 /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
428 outentry(int rank
, struct toptenentry
*t1
, int so
)
430 boolean quit
= FALSE
, dead
= FALSE
, starv
= FALSE
;
435 Sprintf(eos(linebuf
), "%3d", rank
);
437 Sprintf(eos(linebuf
), " ");
438 Sprintf(eos(linebuf
), " %6ld %8s", t1
->points
, t1
->name
);
439 if (t1
->plchar
== 'X')
440 Sprintf(eos(linebuf
), " ");
442 Sprintf(eos(linebuf
), "-%c ", t1
->plchar
);
443 if (!strncmp("escaped", t1
->death
, 7)) {
444 if (!strcmp(" (with amulet)", t1
->death
+ 7))
445 Sprintf(eos(linebuf
), "escaped the dungeon with amulet");
447 Sprintf(eos(linebuf
), "escaped the dungeon [max level %d]",
450 if (!strncmp(t1
->death
, "quit", 4)) {
452 if (t1
->maxhp
< 3 * t1
->hp
&& t1
->maxlvl
< 4)
453 Sprintf(eos(linebuf
), "cravenly gave up");
455 Sprintf(eos(linebuf
), "quit");
456 } else if (!strcmp(t1
->death
, "choked")) {
457 Sprintf(eos(linebuf
), "choked on %s food",
458 (t1
->sex
== 'F') ? "her" : "his");
459 } else if (!strncmp(t1
->death
, "starv", 5)) {
460 Sprintf(eos(linebuf
), "starved to death");
463 Sprintf(eos(linebuf
), "was killed");
466 Sprintf(eos(linebuf
), " on%s level %d",
467 (dead
|| starv
) ? "" : " dungeon", t1
->level
);
468 if (t1
->maxlvl
!= t1
->level
)
469 Sprintf(eos(linebuf
), " [max %d]", t1
->maxlvl
);
470 if (quit
&& t1
->death
[4])
471 Sprintf(eos(linebuf
), "%s", t1
->death
+ 4);
474 Sprintf(eos(linebuf
), " by %s%s",
475 (!strncmp(t1
->death
, "trick", 5) ||
476 !strncmp(t1
->death
, "the ", 4))
478 strchr(vowels
, *t1
->death
) ? "an " : "a ",
481 Sprintf(eos(linebuf
), ".");
483 char *bp
= eos(linebuf
);
487 Sprintf(hpbuf
, "%s", (t1
->hp
> 0) ? itoa(t1
->hp
) : "-");
488 hppos
= COLNO
- 7 - strlen(hpbuf
);
489 if (bp
<= linebuf
+ hppos
) {
490 while (bp
< linebuf
+ hppos
)
493 Sprintf(eos(bp
), " [%d]", t1
->maxhp
);
499 char *bp
= eos(linebuf
);
502 while (bp
< linebuf
+ so
)
506 fputs(linebuf
, stdout
);
510 return (strlen(linebuf
));
518 Sprintf(buf
, "%d", a
);
527 return ((d1
== 0 || d1
> 3 || n
/ 10 == 1) ? "th" : (d1
== 1) ? "st" :
528 (d1
== 2) ? "nd" : "rd");
536 signal(SIGHUP
, SIG_IGN
);
537 for (x
= maxdlevel
; x
>= 0; x
--) {
539 unlink(lock
); /* not all levels need be present */
543 #ifdef NOSAVEONHANGUP
545 hangup(int unused __unused
)
547 signal(SIGINT
, SIG_IGN
);
551 #endif /* NOSAVEONHANGUP */
561 /* it is the callers responsibility to check that there is room for c */
563 charcat(char *s
, char c
)
572 * Called with args from main if argc >= 0. In this case, list scores as
573 * requested. Otherwise, find scores for the current player (and list them
577 prscore(int argc
, char **argv
)
579 char **players
= NULL
;
582 struct toptenentry
*t1
, *t2
;
583 const char *recfile
= RECORD
;
588 long total_score
= 0L;
591 #endif /* nonsense */
592 int outflg
= (argc
>= -1);
597 #endif /* PERS_IS_UID */
599 if (!(rfile
= fopen(recfile
, "r"))) {
600 puts("Cannot open record file!");
604 if (argc
> 1 && !strncmp(argv
[1], "-s", 2)) {
608 } else if (!argv
[1][3] && strchr("CFKSTWX", argv
[1][2])) {
621 player0
= "hackplayer";
624 #endif /* PERS_IS_UID */
632 t1
= tt_head
= newttentry();
633 for (rank
= 1;; rank
++) {
634 if (fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
636 &t1
->level
, &t1
->maxlvl
,
637 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
638 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11)
643 if (!playerct
&& t1
->uid
== uid
)
646 #endif /* PERS_IS_UID */
647 for (i
= 0; i
< playerct
; i
++) {
648 if (strcmp(players
[i
], "all") == 0 ||
649 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
650 (players
[i
][0] == '-' &&
651 players
[i
][1] == t1
->plchar
&&
652 players
[i
][2] == 0) ||
653 (digit(players
[i
][0]) && rank
<= atoi(players
[i
])))
656 t1
= t1
->tt_next
= newttentry();
661 printf("Cannot find any entries for ");
667 for (i
= 0; i
< playerct
; i
++)
668 printf("%s%s", players
[i
],
669 (i
< playerct
- 1) ? ", " : ".\n");
670 printf("Call is: %s -s [playernames]\n", hname
);
679 for (rank
= 1; t1
->points
!= 0; rank
++, t1
= t2
) {
682 if (!playerct
&& t1
->uid
== uid
)
685 #endif /* PERS_IS_UID */
686 for (i
= 0; i
< playerct
; i
++) {
687 if (strcmp(players
[i
], "all") == 0 ||
688 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
689 (players
[i
][0] == '-' &&
690 players
[i
][1] == t1
->plchar
&&
691 players
[i
][2] == 0) ||
692 (digit(players
[i
][0]) && rank
<=
696 outentry(rank
, t1
, 0);
698 total_score
+= t1
->points
;
699 if (totcharct
< sizeof(totchars
) - 1)
700 totchars
[totcharct
++] = t1
->plchar
;
701 #endif /* nonsense */
708 totchars
[totcharct
] = 0;
711 * We would like to determine whether he is experienced. However, the
712 * information collected here only tells about the scores/roles that
713 * got into the topten (top 100?). We should maintain a .hacklog or
714 * something in his home directory.
716 flags
.beginner
= (total_score
< 6000);
717 for (i
= 0; i
< 6; i
++)
718 if (!strchr(totchars
, "CFKSTWX"[i
])) {
720 if (!pl_character
[0])
721 pl_character
[0] = "CFKSTWX"[i
];
724 #endif /* nonsense */