1 /* $NetBSD: hack.end.c,v 1.12 2009/06/07 20:13:18 dholland 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.
64 #include <sys/cdefs.h>
66 __RCSID("$NetBSD: hack.end.c,v 1.12 2009/06/07 20:13:18 dholland Exp $");
74 #define Snprintf (void) snprintf
80 static void topten(void);
81 static void outheader(void);
82 static int outentry(int, struct toptenentry
*, int);
83 static char *itoa(int);
84 static const char *ordin(int);
98 (void) signal(SIGINT
, SIG_IGN
);
99 pline("Really quit?");
100 if (readchar() != 'y') {
101 (void) signal(SIGINT
, done1
);
103 (void) fflush(stdout
);
112 static int done_stopprint
;
117 done_intr(int n __unused
)
120 (void) signal(SIGINT
, SIG_IGN
);
121 (void) signal(SIGQUIT
, SIG_IGN
);
128 (void) signal(SIGHUP
, SIG_IGN
);
133 done_in_by(struct monst
*mtmp
)
135 static char buf
[BUFSZ
];
136 pline("You die ...");
137 if (mtmp
->data
->mlet
== ' ') {
138 Snprintf(buf
, sizeof(buf
),
139 "the ghost of %s", (char *) mtmp
->mextra
);
141 } else if (mtmp
->mnamelth
) {
142 Snprintf(buf
, sizeof(buf
), "%s called %s",
143 mtmp
->data
->mname
, NAME(mtmp
));
145 } else if (mtmp
->minvis
) {
146 Snprintf(buf
, sizeof(buf
), "invisible %s", mtmp
->data
->mname
);
149 killer
= mtmp
->data
->mname
;
154 * called with arg "died", "drowned", "escaped", "quit", "choked",
155 * "panicked", "burned", "starved" or "tricked"
157 /* Be careful not to call panic from here! */
159 done(const char *st1
)
163 if (wizard
&& *st1
== 'd') {
166 u
.uhpmax
= 100; /* arbitrary */
168 pline("For some reason you are still alive.");
178 (void) signal(SIGINT
, done_intr
);
179 (void) signal(SIGQUIT
, done_intr
);
180 (void) signal(SIGHUP
, done_hangup
);
181 if (*st1
== 'q' && u
.uhp
< 1) {
183 killer
= "quit while already on Charon's boat";
186 killer
= "starvation";
187 else if (*st1
== 'd' && st1
[1] == 'r')
189 else if (*st1
== 'p')
191 else if (*st1
== 't')
193 else if (!strchr("bcd", *st1
))
197 if (flags
.toplin
== 1)
199 if (strchr("bcds", *st1
)) {
204 if (!flags
.notombstone
)
208 killer
= st1
; /* after outrip() */
209 settty((char *) 0); /* does a clear_screen() */
211 printf("Goodbye %s %s...\n\n", pl_character
, plname
);
214 tmp
= u
.ugold
- u
.ugold0
;
217 if (*st1
== 'd' || *st1
== 'b')
220 u
.urexp
+= 50 * maxdlevel
;
222 u
.urexp
+= 1000 * ((maxdlevel
> 30) ? 10 : maxdlevel
- 20);
228 unsigned worthlessct
= 0;
229 boolean has_amulet
= FALSE
;
239 printf(" and %s", monnam(mtmp
));
241 u
.urexp
+= mtmp
->mhp
;
245 printf("\nescaped from the dungeon with %ld points,\n",
247 } else if (!done_stopprint
)
248 printf("You escaped from the dungeon with %ld points,\n",
250 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
251 if (otmp
->olet
== GEM_SYM
) {
252 objects
[otmp
->otyp
].oc_name_known
= 1;
253 i
= otmp
->quan
* objects
[otmp
->otyp
].g_val
;
255 worthlessct
+= otmp
->quan
;
260 printf("\t%s (worth %d Zorkmids),\n",
262 } else if (otmp
->olet
== AMULET_SYM
) {
264 i
= (otmp
->spe
< 0) ? 2 : 5000;
267 printf("\t%s (worth %d Zorkmids),\n",
269 if (otmp
->spe
>= 0) {
271 killer
= "escaped (with amulet)";
277 printf("\t%u worthless piece%s of coloured glass,\n",
278 worthlessct
, plur(worthlessct
));
281 } else if (!done_stopprint
)
282 printf("You %s on dungeon level %d with %ld points,\n",
283 st1
, dlevel
, u
.urexp
);
285 printf("and %ld piece%s of gold, after %ld move%s.\n",
286 u
.ugold
, plur(u
.ugold
), moves
, plur(moves
));
288 printf("You were level %u with a maximum of %d hit points when you %s.\n",
289 u
.ulevel
, u
.uhpmax
, st1
);
290 if (*st1
== 'e' && !done_stopprint
) {
291 getret(); /* all those pieces of coloured glass ... */
303 #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
307 #define POINTSMIN 1 /* must be > 0 */
308 #define ENTRYMAX 100 /* must be >= 10 */
309 #define PERS_IS_UID /* delete for PERSMAX per name; now per uid */
311 struct toptenentry
*tt_next
;
313 int level
, maxlvl
, hp
, maxhp
;
317 char name
[NAMSZ
+ 1];
318 char death
[DTHSZ
+ 1];
319 char date
[7];/* yymmdd */
322 static struct toptenentry
*tt_head
;
328 int rank
, rank0
= -1, rank1
= 0;
329 int occ_cnt
= PERSMAX
;
330 struct toptenentry
*t0
, *t1
, *tprev
;
331 const char *recfile
= RECORD
;
332 const char *reclock
= "record_lock";
336 #define HUP if(!done_hup)
337 while (link(recfile
, reclock
) == -1) {
340 HUP
puts("I give up. Sorry.");
341 HUP
puts("Perhaps there is an old record_lock around?");
344 HUP
printf("Waiting for access to record file. (%d)\n",
346 HUP(void) fflush(stdout
);
349 if (!(rfile
= fopen(recfile
, "r"))) {
350 HUP
puts("Cannot open record file!");
353 HUP(void) putchar('\n');
355 /* create a new 'topten' entry */
358 t0
->maxlvl
= maxdlevel
;
360 t0
->maxhp
= u
.uhpmax
;
361 t0
->points
= u
.urexp
;
362 t0
->plchar
= pl_character
[0];
363 t0
->sex
= (flags
.female
? 'F' : 'M');
365 (void) strncpy(t0
->name
, plname
, NAMSZ
);
366 (t0
->name
)[NAMSZ
] = 0;
367 (void) strncpy(t0
->death
, killer
, DTHSZ
);
368 (t0
->death
)[DTHSZ
] = 0;
369 (void) strcpy(t0
->date
, getdatestr());
371 /* assure minimum number of points */
372 if (t0
->points
< POINTSMIN
)
375 t1
= tt_head
= newttentry();
377 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
379 if (fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
381 &t1
->level
, &t1
->maxlvl
,
382 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
383 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11
384 || t1
->points
< POINTSMIN
)
386 if (rank0
< 0 && t1
->points
< t0
->points
) {
394 flg
++; /* ask for a rewrite */
401 t1
->uid
== t0
->uid
&&
403 strncmp(t1
->name
, t0
->name
, NAMSZ
) == 0 &&
404 #endif /* PERS_IS_UID */
405 t1
->plchar
== t0
->plchar
&& --occ_cnt
<= 0) {
409 HUP
printf("You didn't beat your previous score of %ld points.\n\n",
417 if (rank
<= ENTRYMAX
) {
418 t1
= t1
->tt_next
= newttentry();
421 if (rank
> ENTRYMAX
) {
426 if (flg
) { /* rewrite record file */
427 (void) fclose(rfile
);
428 if (!(rfile
= fopen(recfile
, "w"))) {
429 HUP
puts("Cannot write record file\n");
435 puts("You made the top ten list!\n");
437 printf("You reached the %d%s place on the top %d list.\n\n",
438 rank0
, ordin(rank0
), ENTRYMAX
);
448 for (rank
= 1; t1
->points
!= 0; rank
++, t1
= t1
->tt_next
) {
450 fprintf(rfile
, "%6s %d %d %d %d %d %ld %c%c %s,%s\n",
452 t1
->level
, t1
->maxlvl
,
453 t1
->hp
, t1
->maxhp
, t1
->points
,
454 t1
->plchar
, t1
->sex
, t1
->name
, t1
->death
);
457 if (rank
> (int)flags
.end_top
&&
458 (rank
< rank0
- (int)flags
.end_around
|| rank
> rank0
+ (int)flags
.end_around
)
459 && (!flags
.end_own
||
463 strncmp(t1
->name
, t0
->name
, NAMSZ
)))
464 #endif /* PERS_IS_UID */
466 if (rank
== rank0
- (int)flags
.end_around
&&
467 rank0
> (int)flags
.end_top
+ (int)flags
.end_around
+ 1 &&
469 (void) putchar('\n');
471 (void) outentry(rank
, t1
, 0);
473 (void) outentry(rank
, t1
, 1);
475 int t0lth
= outentry(0, t0
, -1);
476 int t1lth
= outentry(rank
, t1
, t0lth
);
479 (void) outentry(0, t0
, t0lth
);
484 (void) outentry(0, t0
, 1);
485 (void) fclose(rfile
);
488 (void) unlink(reclock
);
496 (void) strcpy(linebuf
, "Number Points Name");
498 while (bp
< linebuf
+ COLNO
- 9)
500 (void) strcpy(bp
, "Hp [max]");
504 /* so>0: standout line; so=0: ordinary line; so<0: no output, return length */
506 outentry(int rank
, struct toptenentry
*t1
, int so
)
508 boolean quit
= FALSE
, gotkilled
= FALSE
, starv
= FALSE
;
516 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, "%3d", rank
);
518 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " ");
519 pos
= strlen(linebuf
);
521 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " %6ld %8s",
522 t1
->points
, t1
->name
);
523 pos
= strlen(linebuf
);
525 if (t1
->plchar
== 'X')
526 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " ");
528 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, "-%c ", t1
->plchar
);
529 pos
= strlen(linebuf
);
531 if (!strncmp("escaped", t1
->death
, 7)) {
532 if (!strcmp(" (with amulet)", t1
->death
+ 7))
533 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
534 "escaped the dungeon with amulet");
536 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
537 "escaped the dungeon [max level %d]",
539 pos
= strlen(linebuf
);
541 if (!strncmp(t1
->death
, "quit", 4)) {
543 if (t1
->maxhp
< 3 * t1
->hp
&& t1
->maxlvl
< 4)
544 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
547 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
549 } else if (!strcmp(t1
->death
, "choked")) {
550 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
552 (t1
->sex
== 'F') ? "her" : "his");
553 } else if (!strncmp(t1
->death
, "starv", 5)) {
554 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
558 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
562 pos
= strlen(linebuf
);
564 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " on%s level %d",
565 (gotkilled
|| starv
) ? "" : " dungeon", t1
->level
);
566 pos
= strlen(linebuf
);
568 if (t1
->maxlvl
!= t1
->level
)
569 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
570 " [max %d]", t1
->maxlvl
);
571 pos
= strlen(linebuf
);
573 if (quit
&& t1
->death
[4])
574 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
575 "%s", t1
->death
+ 4);
576 pos
= strlen(linebuf
);
579 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
, " by %s%s",
580 (!strncmp(t1
->death
, "trick", 5) || !strncmp(t1
->death
, "the ", 4))
582 strchr(vowels
, *t1
->death
) ? "an " : "a ",
584 pos
= strlen(linebuf
);
586 strlcat(linebuf
, ".", sizeof(linebuf
));
587 pos
= strlen(linebuf
);
592 strlcpy(hpbuf
, (t1
->hp
> 0) ? itoa(t1
->hp
) : "-", sizeof(hpbuf
));
593 hppos
= COLNO
- 7 - strlen(hpbuf
);
596 linebuf
[pos
++] = ' ';
597 (void) strlcpy(linebuf
+pos
, hpbuf
, sizeof(linebuf
)-pos
);
598 pos
= strlen(linebuf
);
599 Snprintf(linebuf
+pos
, sizeof(linebuf
)-pos
,
601 pos
= strlen(linebuf
);
609 while (pos
< (unsigned)so
)
610 linebuf
[pos
++] = ' ';
613 fputs(linebuf
, stdout
);
615 (void) putchar('\n');
617 return /*(strlen(linebuf))*/ pos
;
624 Snprintf(buf
, sizeof(buf
), "%d", a
);
633 return ((dg
== 0 || dg
> 3 || n
/ 10 == 1) ? "th" : (dg
== 1) ? "st" :
634 (dg
== 2) ? "nd" : "rd");
641 (void) signal(SIGHUP
, SIG_IGN
);
642 for (x
= maxdlevel
; x
>= 0; x
--) {
644 (void) unlink(lock
); /* not all levels need be present */
648 #ifdef NOSAVEONHANGUP
651 hangup(int n __unused
)
653 (void) signal(SIGINT
, SIG_IGN
);
657 #endif /* NOSAVEONHANGUP */
667 /* it is the callers responsibility to check that there is room for c */
669 charcat(char *s
, int c
)
678 * Called with args from main if argc >= 0. In this case, list scores as
679 * requested. Otherwise, find scores for the current player (and list them
683 prscore(int argc
, char **argv
)
685 char **players
= NULL
;
688 struct toptenentry
*t1
, *t2
;
689 const char *recfile
= RECORD
;
694 long total_score
= 0L;
697 #endif /* nonsense */
698 int outflg
= (argc
>= -1);
703 #endif /* PERS_IS_UID */
705 if (!(rfile
= fopen(recfile
, "r"))) {
706 puts("Cannot open record file!");
709 if (argc
> 1 && !strncmp(argv
[1], "-s", 2)) {
713 } else if (!argv
[1][3] && strchr("CFKSTWX", argv
[1][2])) {
726 player0
= "hackplayer";
729 #endif /* PERS_IS_UID */
737 t1
= tt_head
= newttentry();
738 for (rank
= 1;; rank
++) {
739 if (fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
741 &t1
->level
, &t1
->maxlvl
,
742 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
743 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11)
748 if (!playerct
&& t1
->uid
== uid
)
751 #endif /* PERS_IS_UID */
752 for (i
= 0; i
< playerct
; i
++) {
753 if (strcmp(players
[i
], "all") == 0 ||
754 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
755 (players
[i
][0] == '-' &&
756 players
[i
][1] == t1
->plchar
&&
757 players
[i
][2] == 0) ||
758 (digit(players
[i
][0]) && rank
<= atoi(players
[i
])))
761 t1
= t1
->tt_next
= newttentry();
763 (void) fclose(rfile
);
766 printf("Cannot find any entries for ");
772 for (i
= 0; i
< playerct
; i
++)
773 printf("%s%s", players
[i
], (i
< playerct
- 1) ? ", " : ".\n");
774 printf("Call is: %s -s [playernames]\n", hname
);
782 for (rank
= 1; t1
->points
!= 0; rank
++, t1
= t2
) {
785 if (!playerct
&& t1
->uid
== uid
)
788 #endif /* PERS_IS_UID */
789 for (i
= 0; i
< playerct
; i
++) {
790 if (strcmp(players
[i
], "all") == 0 ||
791 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
792 (players
[i
][0] == '-' &&
793 players
[i
][1] == t1
->plchar
&&
794 players
[i
][2] == 0) ||
795 (digit(players
[i
][0]) && rank
<= atoi(players
[i
]))) {
798 (void) outentry(rank
, t1
, 0);
800 total_score
+= t1
->points
;
801 if (totcharct
< sizeof(totchars
) - 1)
802 totchars
[totcharct
++] = t1
->plchar
;
803 #endif /* nonsense */
810 totchars
[totcharct
] = 0;
813 * We would like to determine whether he is experienced. However, the
814 * information collected here only tells about the scores/roles that
815 * got into the topten (top 100?). We should maintain a .hacklog or
816 * something in his home directory.
818 flags
.beginner
= (total_score
< 6000);
819 for (i
= 0; i
< 6; i
++)
820 if (!strchr(totchars
, "CFKSTWX"[i
])) {
822 if (!pl_character
[0])
823 pl_character
[0] = "CFKSTWX"[i
];
826 #endif /* nonsense */