1 /* $NetBSD: hack.end.c,v 1.19 2020/02/07 22:04:02 fox Exp $ */
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * - Neither the name of the Stichting Centrum voor Wiskunde en
20 * Informatica, nor the names of its contributors may be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39 * All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 #define Snprintf (void) snprintf
75 static void topten(void);
76 static void outheader(void);
77 static int outentry(int, struct toptenentry
*, int);
78 static char *itoa(int);
79 static const char *ordin(int);
93 (void) signal(SIGINT
, SIG_IGN
);
94 pline("Really quit?");
95 if (readchar() != 'y') {
96 (void) signal(SIGINT
, done1
);
98 (void) fflush(stdout
);
107 static int done_stopprint
;
112 done_intr(int n __unused
)
115 (void) signal(SIGINT
, SIG_IGN
);
116 (void) signal(SIGQUIT
, SIG_IGN
);
123 (void) signal(SIGHUP
, SIG_IGN
);
128 done_in_by(struct monst
*mtmp
)
130 static char buf
[BUFSZ
];
131 pline("You die ...");
132 if (mtmp
->data
->mlet
== ' ') {
133 Snprintf(buf
, sizeof(buf
),
134 "the ghost of %s", (char *) mtmp
->mextra
);
136 } else if (mtmp
->mnamelth
) {
137 Snprintf(buf
, sizeof(buf
), "%s called %s",
138 mtmp
->data
->mname
, NAME(mtmp
));
140 } else if (mtmp
->minvis
) {
141 Snprintf(buf
, sizeof(buf
), "invisible %s", mtmp
->data
->mname
);
144 killer
= mtmp
->data
->mname
;
149 * called with arg "died", "drowned", "escaped", "quit", "choked",
150 * "panicked", "burned", "starved" or "tricked"
152 /* Be careful not to call panic from here! */
154 done(const char *st1
)
158 if (wizard
&& *st1
== 'd') {
161 u
.uhpmax
= 100; /* arbitrary */
163 pline("For some reason you are still alive.");
173 (void) signal(SIGINT
, done_intr
);
174 (void) signal(SIGQUIT
, done_intr
);
175 (void) signal(SIGHUP
, done_hangup
);
176 if (*st1
== 'q' && u
.uhp
< 1) {
178 killer
= "quit while already on Charon's boat";
181 killer
= "starvation";
182 else if (*st1
== 'd' && st1
[1] == 'r')
184 else if (*st1
== 'p')
186 else if (*st1
== 't')
188 else if (!strchr("bcd", *st1
))
192 if (flags
.toplin
== 1)
194 if (strchr("bcds", *st1
)) {
199 if (!flags
.notombstone
)
203 killer
= st1
; /* after outrip() */
204 settty(NULL
); /* does a clear_screen() */
206 printf("Goodbye %s %s...\n\n", pl_character
, plname
);
209 tmp
= u
.ugold
- u
.ugold0
;
212 if (*st1
== 'd' || *st1
== 'b')
215 u
.urexp
+= 50 * maxdlevel
;
217 u
.urexp
+= 1000 * ((maxdlevel
> 30) ? 10 : maxdlevel
- 20);
223 unsigned worthlessct
= 0;
224 boolean has_amulet
= FALSE
;
234 printf(" and %s", monnam(mtmp
));
236 u
.urexp
+= mtmp
->mhp
;
240 printf("\nescaped from the dungeon with %ld points,\n",
242 } else if (!done_stopprint
)
243 printf("You escaped from the dungeon with %ld points,\n",
245 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
246 if (otmp
->olet
== GEM_SYM
) {
247 objects
[otmp
->otyp
].oc_name_known
= 1;
248 i
= otmp
->quan
* objects
[otmp
->otyp
].g_val
;
250 worthlessct
+= otmp
->quan
;
255 printf("\t%s (worth %d Zorkmids),\n",
257 } else if (otmp
->olet
== AMULET_SYM
) {
259 i
= (otmp
->spe
< 0) ? 2 : 5000;
262 printf("\t%s (worth %d Zorkmids),\n",
264 if (otmp
->spe
>= 0) {
266 killer
= "escaped (with amulet)";
272 printf("\t%u worthless piece%s of coloured glass,\n",
273 worthlessct
, plur(worthlessct
));
276 } else if (!done_stopprint
)
277 printf("You %s on dungeon level %d with %ld points,\n",
278 st1
, dlevel
, u
.urexp
);
280 printf("and %ld piece%s of gold, after %ld move%s.\n",
281 u
.ugold
, plur(u
.ugold
), moves
, plur(moves
));
283 printf("You were level %u with a maximum of %d hit points when you %s.\n",
284 u
.ulevel
, u
.uhpmax
, st1
);
285 if (*st1
== 'e' && !done_stopprint
) {
286 getret(); /* all those pieces of coloured glass ... */
298 #define newttentry() ((struct toptenentry *) alloc(sizeof(struct toptenentry)))
302 #define POINTSMIN 1 /* must be > 0 */
303 #define ENTRYMAX 100 /* must be >= 10 */
304 #define PERS_IS_UID /* delete for PERSMAX per name; now per uid */
306 struct toptenentry
*tt_next
;
308 int level
, maxlvl
, hp
, maxhp
;
312 char name
[NAMSZ
+ 1];
313 char death
[DTHSZ
+ 1];
314 char date
[7];/* yymmdd */
317 static struct toptenentry
*tt_head
;
323 int rank
, rank0
= -1, rank1
= 0;
324 int occ_cnt
= PERSMAX
;
325 struct toptenentry
*t0
, *t1
, *tprev
;
326 const char *recfile
= RECORD
;
327 const char *reclock
= "record_lock";
331 #define HUP if(!done_hup)
332 while (link(recfile
, reclock
) == -1) {
335 HUP
puts("I give up. Sorry.");
336 HUP
puts("Perhaps there is an old record_lock around?");
339 HUP
printf("Waiting for access to record file. (%d)\n",
341 HUP(void) fflush(stdout
);
344 if (!(rfile
= fopen(recfile
, "r"))) {
345 HUP
puts("Cannot open record file!");
348 HUP(void) putchar('\n');
350 /* create a new 'topten' entry */
353 t0
->maxlvl
= maxdlevel
;
355 t0
->maxhp
= u
.uhpmax
;
356 t0
->points
= u
.urexp
;
357 t0
->plchar
= pl_character
[0];
358 t0
->sex
= (flags
.female
? 'F' : 'M');
360 (void) strncpy(t0
->name
, plname
, NAMSZ
);
361 (t0
->name
)[NAMSZ
] = 0;
362 (void) strncpy(t0
->death
, killer
, DTHSZ
);
363 (t0
->death
)[DTHSZ
] = 0;
364 (void) strcpy(t0
->date
, getdatestr());
366 /* assure minimum number of points */
367 if (t0
->points
< POINTSMIN
)
370 t1
= tt_head
= newttentry();
372 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
374 if (fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
376 &t1
->level
, &t1
->maxlvl
,
377 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
378 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11
379 || t1
->points
< POINTSMIN
)
381 if (rank0
< 0 && t1
->points
< t0
->points
) {
389 flg
++; /* ask for a rewrite */
396 t1
->uid
== t0
->uid
&&
398 strncmp(t1
->name
, t0
->name
, NAMSZ
) == 0 &&
399 #endif /* PERS_IS_UID */
400 t1
->plchar
== t0
->plchar
&& --occ_cnt
<= 0) {
404 HUP
printf("You didn't beat your previous score of %ld points.\n\n",
412 if (rank
<= ENTRYMAX
) {
413 t1
= t1
->tt_next
= newttentry();
416 if (rank
> ENTRYMAX
) {
421 if (flg
) { /* rewrite record file */
422 (void) fclose(rfile
);
423 if (!(rfile
= fopen(recfile
, "w"))) {
424 HUP
puts("Cannot write record file\n");
430 puts("You made the top ten list!\n");
432 printf("You reached the %d%s place on the top %d list.\n\n",
433 rank0
, ordin(rank0
), ENTRYMAX
);
443 for (rank
= 1; t1
->points
!= 0; rank
++, t1
= t1
->tt_next
) {
445 fprintf(rfile
, "%6s %d %d %d %d %d %ld %c%c %s,%s\n",
447 t1
->level
, t1
->maxlvl
,
448 t1
->hp
, t1
->maxhp
, t1
->points
,
449 t1
->plchar
, t1
->sex
, t1
->name
, t1
->death
);
452 if (rank
> (int)flags
.end_top
&&
453 (rank
< rank0
- (int)flags
.end_around
|| rank
> rank0
+ (int)flags
.end_around
)
454 && (!flags
.end_own
||
458 strncmp(t1
->name
, t0
->name
, NAMSZ
)))
459 #endif /* PERS_IS_UID */
461 if (rank
== rank0
- (int)flags
.end_around
&&
462 rank0
> (int)flags
.end_top
+ (int)flags
.end_around
+ 1 &&
464 (void) putchar('\n');
466 (void) outentry(rank
, t1
, 0);
468 (void) outentry(rank
, t1
, 1);
470 int t0lth
= outentry(0, t0
, -1);
471 int t1lth
= outentry(rank
, t1
, t0lth
);
474 (void) outentry(0, t0
, t0lth
);
479 (void) outentry(0, t0
, 1);
480 (void) fclose(rfile
);
483 (void) unlink(reclock
);
491 (void) strcpy(linebuf
, "Number Points Name");
493 while (bp
< linebuf
+ COLNO
- 9)
495 (void) strcpy(bp
, "Hp [max]");
499 /* so>0: standout line; so=0: ordinary line; so<0: no output, return length */
501 outentry(int rank
, struct toptenentry
*t1
, int so
)
503 boolean quit
= FALSE
, gotkilled
= FALSE
, starv
= FALSE
;
511 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, "%3d", rank
);
513 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " ");
514 pos
= strlen(linebuf
);
516 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " %6ld %8s",
517 t1
->points
, t1
->name
);
518 pos
= strlen(linebuf
);
520 if (t1
->plchar
== 'X')
521 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " ");
523 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, "-%c ", t1
->plchar
);
524 pos
= strlen(linebuf
);
526 if (!strncmp("escaped", t1
->death
, 7)) {
527 if (!strcmp(" (with amulet)", t1
->death
+ 7))
528 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
529 "escaped the dungeon with amulet");
531 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
532 "escaped the dungeon [max level %d]",
534 pos
= strlen(linebuf
);
536 if (!strncmp(t1
->death
, "quit", 4)) {
538 if (t1
->maxhp
< 3 * t1
->hp
&& t1
->maxlvl
< 4)
539 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
542 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
544 } else if (!strcmp(t1
->death
, "choked")) {
545 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
547 (t1
->sex
== 'F') ? "her" : "his");
548 } else if (!strncmp(t1
->death
, "starv", 5)) {
549 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
553 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
557 pos
= strlen(linebuf
);
559 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " on%s level %d",
560 (gotkilled
|| starv
) ? "" : " dungeon", t1
->level
);
561 pos
= strlen(linebuf
);
563 if (t1
->maxlvl
!= t1
->level
)
564 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
565 " [max %d]", t1
->maxlvl
);
566 pos
= strlen(linebuf
);
568 if (quit
&& t1
->death
[4])
569 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
570 "%s", t1
->death
+ 4);
571 pos
= strlen(linebuf
);
574 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " by %s%s",
575 (!strncmp(t1
->death
, "trick", 5) || !strncmp(t1
->death
, "the ", 4))
577 strchr(vowels
, *t1
->death
) ? "an " : "a ",
579 pos
= strlen(linebuf
);
581 strlcat(linebuf
, ".", sizeof(linebuf
));
582 pos
= strlen(linebuf
);
587 strlcpy(hpbuf
, (t1
->hp
> 0) ? itoa(t1
->hp
) : "-", sizeof(hpbuf
));
588 hppos
= COLNO
- 7 - strlen(hpbuf
);
591 linebuf
[pos
++] = ' ';
592 (void) strlcpy(linebuf
+pos
, hpbuf
, sizeof(linebuf
)-pos
);
593 pos
= strlen(linebuf
);
594 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
596 pos
= strlen(linebuf
);
604 while (pos
< (unsigned)so
)
605 linebuf
[pos
++] = ' ';
608 fputs(linebuf
, stdout
);
610 (void) putchar('\n');
612 return /*(strlen(linebuf))*/ pos
;
619 Snprintf(buf
, sizeof(buf
), "%d", a
);
628 return ((dg
== 0 || dg
> 3 || n
/ 10 == 1) ? "th" : (dg
== 1) ? "st" :
629 (dg
== 2) ? "nd" : "rd");
636 (void) signal(SIGHUP
, SIG_IGN
);
637 for (x
= maxdlevel
; x
>= 0; x
--) {
639 (void) unlink(lock
); /* not all levels need be present */
643 #ifdef NOSAVEONHANGUP
646 hang_up(int n __unused
)
648 (void) signal(SIGINT
, SIG_IGN
);
652 #endif /* NOSAVEONHANGUP */
662 /* it is the callers responsibility to check that there is room for c */
664 charcat(char *s
, int c
)
673 * Called with args from main if argc >= 0. In this case, list scores as
674 * requested. Otherwise, find scores for the current player (and list them
678 prscore(int argc
, char **argv
)
680 char **players
= NULL
;
683 struct toptenentry
*t1
, *t2
;
684 const char *recfile
= RECORD
;
689 long total_score
= 0L;
692 #endif /* nonsense */
693 int outflg
= (argc
>= -1);
698 #endif /* PERS_IS_UID */
700 if (!(rfile
= fopen(recfile
, "r"))) {
701 puts("Cannot open record file!");
704 if (argc
> 1 && !strncmp(argv
[1], "-s", 2)) {
708 } else if (!argv
[1][3] && strchr("CFKSTWX", argv
[1][2])) {
721 player0
= "hackplayer";
724 #endif /* PERS_IS_UID */
732 t1
= tt_head
= newttentry();
733 for (rank
= 1;; rank
++) {
734 if (fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
736 &t1
->level
, &t1
->maxlvl
,
737 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
738 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11)
743 if (!playerct
&& t1
->uid
== uid
)
746 #endif /* PERS_IS_UID */
747 for (i
= 0; i
< playerct
; i
++) {
748 if (strcmp(players
[i
], "all") == 0 ||
749 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
750 (players
[i
][0] == '-' &&
751 players
[i
][1] == t1
->plchar
&&
752 players
[i
][2] == 0) ||
753 (digit(players
[i
][0]) && rank
<= atoi(players
[i
])))
756 t1
= t1
->tt_next
= newttentry();
758 (void) fclose(rfile
);
761 printf("Cannot find any entries for ");
767 for (i
= 0; i
< playerct
; i
++)
768 printf("%s%s", players
[i
], (i
< playerct
- 1) ? ", " : ".\n");
769 printf("Call is: %s -s [playernames]\n", hname
);
777 for (rank
= 1; t1
->points
!= 0; rank
++, t1
= t2
) {
780 if (!playerct
&& t1
->uid
== uid
)
783 #endif /* PERS_IS_UID */
784 for (i
= 0; i
< playerct
; i
++) {
785 if (strcmp(players
[i
], "all") == 0 ||
786 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
787 (players
[i
][0] == '-' &&
788 players
[i
][1] == t1
->plchar
&&
789 players
[i
][2] == 0) ||
790 (digit(players
[i
][0]) && rank
<= atoi(players
[i
]))) {
793 (void) outentry(rank
, t1
, 0);
795 total_score
+= t1
->points
;
796 if (totcharct
< sizeof(totchars
) - 1)
797 totchars
[totcharct
++] = t1
->plchar
;
798 #endif /* nonsense */
805 totchars
[totcharct
] = 0;
808 * We would like to determine whether he is experienced. However, the
809 * information collected here only tells about the scores/roles that
810 * got into the topten (top 100?). We should maintain a .hacklog or
811 * something in his home directory.
813 flags
.beginner
= (total_score
< 6000);
814 for (i
= 0; i
< 6; i
++)
815 if (!strchr(totchars
, "CFKSTWX"[i
])) {
817 if (!pl_character
[0])
818 pl_character
[0] = "CFKSTWX"[i
];
821 #endif /* nonsense */