]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.end.c
1 /* $NetBSD: hack.end.c,v 1.9 2008/01/28 06:55:41 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.9 2008/01/28 06:55:41 dholland Exp $");
74 #define Sprintf (void) sprintf
91 (void) signal(SIGINT
, SIG_IGN
);
92 pline("Really quit?");
93 if (readchar() != 'y') {
94 (void) signal(SIGINT
, done1
);
96 (void) fflush(stdout
);
114 (void) signal(SIGINT
, SIG_IGN
);
115 (void) signal(SIGQUIT
, SIG_IGN
);
123 (void) signal(SIGHUP
, SIG_IGN
);
131 static char buf
[BUFSZ
];
132 pline("You die ...");
133 if (mtmp
->data
->mlet
== ' ') {
134 Sprintf(buf
, "the ghost of %s", (char *) mtmp
->mextra
);
136 } else if (mtmp
->mnamelth
) {
137 Sprintf(buf
, "%s called %s",
138 mtmp
->data
->mname
, NAME(mtmp
));
140 } else if (mtmp
->minvis
) {
141 Sprintf(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! */
159 if (wizard
&& *st1
== 'd') {
162 u
.uhpmax
= 100; /* arbitrary */
164 pline("For some reason you are still alive.");
174 (void) signal(SIGINT
, done_intr
);
175 (void) signal(SIGQUIT
, done_intr
);
176 (void) signal(SIGHUP
, done_hangup
);
177 if (*st1
== 'q' && u
.uhp
< 1) {
179 killer
= "quit while already on Charon's boat";
182 killer
= "starvation";
183 else if (*st1
== 'd' && st1
[1] == 'r')
185 else if (*st1
== 'p')
187 else if (*st1
== 't')
189 else if (!strchr("bcd", *st1
))
193 if (flags
.toplin
== 1)
195 if (strchr("bcds", *st1
)) {
200 if (!flags
.notombstone
)
204 killer
= st1
; /* after outrip() */
205 settty((char *) 0); /* does a clear_screen() */
207 printf("Goodbye %s %s...\n\n", pl_character
, plname
);
210 tmp
= u
.ugold
- u
.ugold0
;
213 if (*st1
== 'd' || *st1
== 'b')
216 u
.urexp
+= 50 * maxdlevel
;
218 u
.urexp
+= 1000 * ((maxdlevel
> 30) ? 10 : maxdlevel
- 20);
224 unsigned worthlessct
= 0;
225 boolean has_amulet
= FALSE
;
235 printf(" and %s", monnam(mtmp
));
237 u
.urexp
+= mtmp
->mhp
;
241 printf("\nescaped from the dungeon with %ld points,\n",
243 } else if (!done_stopprint
)
244 printf("You escaped from the dungeon with %ld points,\n",
246 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
247 if (otmp
->olet
== GEM_SYM
) {
248 objects
[otmp
->otyp
].oc_name_known
= 1;
249 i
= otmp
->quan
* objects
[otmp
->otyp
].g_val
;
251 worthlessct
+= otmp
->quan
;
256 printf("\t%s (worth %d Zorkmids),\n",
258 } else if (otmp
->olet
== AMULET_SYM
) {
260 i
= (otmp
->spe
< 0) ? 2 : 5000;
263 printf("\t%s (worth %d Zorkmids),\n",
265 if (otmp
->spe
>= 0) {
267 killer
= "escaped (with amulet)";
273 printf("\t%u worthless piece%s of coloured glass,\n",
274 worthlessct
, plur(worthlessct
));
277 } else if (!done_stopprint
)
278 printf("You %s on dungeon level %d with %ld points,\n",
279 st1
, dlevel
, u
.urexp
);
281 printf("and %ld piece%s of gold, after %ld move%s.\n",
282 u
.ugold
, plur(u
.ugold
), moves
, plur(moves
));
284 printf("You were level %u with a maximum of %d hit points when you %s.\n",
285 u
.ulevel
, u
.uhpmax
, st1
);
286 if (*st1
== 'e' && !done_stopprint
) {
287 getret(); /* all those pieces of coloured glass ... */
299 #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
303 #define POINTSMIN 1 /* must be > 0 */
304 #define ENTRYMAX 100 /* must be >= 10 */
305 #define PERS_IS_UID /* delete for PERSMAX per name; now per uid */
307 struct toptenentry
*tt_next
;
309 int level
, maxlvl
, hp
, maxhp
;
313 char name
[NAMSZ
+ 1];
314 char death
[DTHSZ
+ 1];
315 char date
[7];/* yymmdd */
322 int rank
, rank0
= -1, rank1
= 0;
323 int occ_cnt
= PERSMAX
;
324 struct toptenentry
*t0
, *t1
, *tprev
;
325 const char *recfile
= RECORD
;
326 const char *reclock
= "record_lock";
330 #define HUP if(!done_hup)
331 while (link(recfile
, reclock
) == -1) {
334 HUP
puts("I give up. Sorry.");
335 HUP
puts("Perhaps there is an old record_lock around?");
338 HUP
printf("Waiting for access to record file. (%d)\n",
340 HUP(void) fflush(stdout
);
343 if (!(rfile
= fopen(recfile
, "r"))) {
344 HUP
puts("Cannot open record file!");
347 HUP(void) putchar('\n');
349 /* create a new 'topten' entry */
352 t0
->maxlvl
= maxdlevel
;
354 t0
->maxhp
= u
.uhpmax
;
355 t0
->points
= u
.urexp
;
356 t0
->plchar
= pl_character
[0];
357 t0
->sex
= (flags
.female
? 'F' : 'M');
359 (void) strncpy(t0
->name
, plname
, NAMSZ
);
360 (t0
->name
)[NAMSZ
] = 0;
361 (void) strncpy(t0
->death
, killer
, DTHSZ
);
362 (t0
->death
)[DTHSZ
] = 0;
363 (void) strcpy(t0
->date
, getdate());
365 /* assure minimum number of points */
366 if (t0
->points
< POINTSMIN
)
369 t1
= tt_head
= newttentry();
371 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
373 if (fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
375 &t1
->level
, &t1
->maxlvl
,
376 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
377 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11
378 || t1
->points
< POINTSMIN
)
380 if (rank0
< 0 && t1
->points
< t0
->points
) {
388 flg
++; /* ask for a rewrite */
395 t1
->uid
== t0
->uid
&&
397 strncmp(t1
->name
, t0
->name
, NAMSZ
) == 0 &&
398 #endif /* PERS_IS_UID */
399 t1
->plchar
== t0
->plchar
&& --occ_cnt
<= 0) {
403 HUP
printf("You didn't beat your previous score of %ld points.\n\n",
411 if (rank
<= ENTRYMAX
) {
412 t1
= t1
->tt_next
= newttentry();
415 if (rank
> ENTRYMAX
) {
420 if (flg
) { /* rewrite record file */
421 (void) fclose(rfile
);
422 if (!(rfile
= fopen(recfile
, "w"))) {
423 HUP
puts("Cannot write record file\n");
429 puts("You made the top ten list!\n");
431 printf("You reached the %d%s place on the top %d list.\n\n",
432 rank0
, ordin(rank0
), ENTRYMAX
);
442 for (rank
= 1; t1
->points
!= 0; rank
++, t1
= t1
->tt_next
) {
444 fprintf(rfile
, "%6s %d %d %d %d %d %ld %c%c %s,%s\n",
446 t1
->level
, t1
->maxlvl
,
447 t1
->hp
, t1
->maxhp
, t1
->points
,
448 t1
->plchar
, t1
->sex
, t1
->name
, t1
->death
);
451 if (rank
> (int)flags
.end_top
&&
452 (rank
< rank0
- (int)flags
.end_around
|| rank
> rank0
+ (int)flags
.end_around
)
453 && (!flags
.end_own
||
457 strncmp(t1
->name
, t0
->name
, NAMSZ
)))
458 #endif /* PERS_IS_UID */
460 if (rank
== rank0
- (int)flags
.end_around
&&
461 rank0
> (int)flags
.end_top
+ (int)flags
.end_around
+ 1 &&
463 (void) putchar('\n');
465 (void) outentry(rank
, t1
, 0);
467 (void) outentry(rank
, t1
, 1);
469 int t0lth
= outentry(0, t0
, -1);
470 int t1lth
= outentry(rank
, t1
, t0lth
);
473 (void) outentry(0, t0
, t0lth
);
478 (void) outentry(0, t0
, 1);
479 (void) fclose(rfile
);
482 (void) unlink(reclock
);
490 (void) strcpy(linebuf
, "Number Points Name");
492 while (bp
< linebuf
+ COLNO
- 9)
494 (void) strcpy(bp
, "Hp [max]");
498 /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
500 outentry(int rank
, struct toptenentry
*t1
, int so
)
502 boolean quit
= FALSE
, gotkilled
= FALSE
, starv
= FALSE
;
507 Sprintf(eos(linebuf
), "%3d", rank
);
509 Sprintf(eos(linebuf
), " ");
510 Sprintf(eos(linebuf
), " %6ld %8s", t1
->points
, t1
->name
);
511 if (t1
->plchar
== 'X')
512 Sprintf(eos(linebuf
), " ");
514 Sprintf(eos(linebuf
), "-%c ", t1
->plchar
);
515 if (!strncmp("escaped", t1
->death
, 7)) {
516 if (!strcmp(" (with amulet)", t1
->death
+ 7))
517 Sprintf(eos(linebuf
), "escaped the dungeon with amulet");
519 Sprintf(eos(linebuf
), "escaped the dungeon [max level %d]",
522 if (!strncmp(t1
->death
, "quit", 4)) {
524 if (t1
->maxhp
< 3 * t1
->hp
&& t1
->maxlvl
< 4)
525 Sprintf(eos(linebuf
), "cravenly gave up");
527 Sprintf(eos(linebuf
), "quit");
528 } else if (!strcmp(t1
->death
, "choked"))
529 Sprintf(eos(linebuf
), "choked on %s food",
530 (t1
->sex
== 'F') ? "her" : "his");
531 else if (!strncmp(t1
->death
, "starv", 5))
532 Sprintf(eos(linebuf
), "starved to death"), starv
= TRUE
;
534 Sprintf(eos(linebuf
), "was killed"), gotkilled
= TRUE
;
535 Sprintf(eos(linebuf
), " on%s level %d",
536 (gotkilled
|| starv
) ? "" : " dungeon", t1
->level
);
537 if (t1
->maxlvl
!= t1
->level
)
538 Sprintf(eos(linebuf
), " [max %d]", t1
->maxlvl
);
539 if (quit
&& t1
->death
[4])
540 Sprintf(eos(linebuf
), t1
->death
+ 4);
543 Sprintf(eos(linebuf
), " by %s%s",
544 (!strncmp(t1
->death
, "trick", 5) || !strncmp(t1
->death
, "the ", 4))
546 strchr(vowels
, *t1
->death
) ? "an " : "a ",
548 Sprintf(eos(linebuf
), ".");
550 char *bp
= eos(linebuf
);
553 Sprintf(hpbuf
, (t1
->hp
> 0) ? itoa(t1
->hp
) : "-");
554 hppos
= COLNO
- 7 - strlen(hpbuf
);
555 if (bp
<= linebuf
+ hppos
) {
556 while (bp
< linebuf
+ hppos
)
558 (void) strcpy(bp
, hpbuf
);
559 Sprintf(eos(bp
), " [%d]", t1
->maxhp
);
565 char *bp
= eos(linebuf
);
568 while (bp
< linebuf
+ so
)
572 fputs(linebuf
, stdout
);
574 (void) putchar('\n');
576 return (strlen(linebuf
));
584 Sprintf(buf
, "%d", a
);
594 return ((dg
== 0 || dg
> 3 || n
/ 10 == 1) ? "th" : (dg
== 1) ? "st" :
595 (dg
== 2) ? "nd" : "rd");
602 (void) signal(SIGHUP
, SIG_IGN
);
603 for (x
= maxdlevel
; x
>= 0; x
--) {
605 (void) unlink(lock
); /* not all levels need be present */
609 #ifdef NOSAVEONHANGUP
615 (void) signal(SIGINT
, SIG_IGN
);
619 #endif /* NOSAVEONHANGUP */
630 /* it is the callers responsibility to check that there is room for c */
642 * Called with args from main if argc >= 0. In this case, list scores as
643 * requested. Otherwise, find scores for the current player (and list them
651 char **players
= NULL
;
654 struct toptenentry
*t1
, *t2
;
655 const char *recfile
= RECORD
;
660 long total_score
= 0L;
663 #endif /* nonsense */
664 int outflg
= (argc
>= -1);
669 #endif /* PERS_IS_UID */
671 if (!(rfile
= fopen(recfile
, "r"))) {
672 puts("Cannot open record file!");
675 if (argc
> 1 && !strncmp(argv
[1], "-s", 2)) {
679 } else if (!argv
[1][3] && strchr("CFKSTWX", argv
[1][2])) {
692 player0
= "hackplayer";
695 #endif /* PERS_IS_UID */
703 t1
= tt_head
= newttentry();
704 for (rank
= 1;; rank
++) {
705 if (fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
707 &t1
->level
, &t1
->maxlvl
,
708 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
709 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11)
714 if (!playerct
&& t1
->uid
== uid
)
717 #endif /* PERS_IS_UID */
718 for (i
= 0; i
< playerct
; i
++) {
719 if (strcmp(players
[i
], "all") == 0 ||
720 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
721 (players
[i
][0] == '-' &&
722 players
[i
][1] == t1
->plchar
&&
723 players
[i
][2] == 0) ||
724 (digit(players
[i
][0]) && rank
<= atoi(players
[i
])))
727 t1
= t1
->tt_next
= newttentry();
729 (void) fclose(rfile
);
732 printf("Cannot find any entries for ");
738 for (i
= 0; i
< playerct
; i
++)
739 printf("%s%s", players
[i
], (i
< playerct
- 1) ? ", " : ".\n");
740 printf("Call is: %s -s [playernames]\n", hname
);
748 for (rank
= 1; t1
->points
!= 0; rank
++, t1
= t2
) {
751 if (!playerct
&& t1
->uid
== uid
)
754 #endif /* PERS_IS_UID */
755 for (i
= 0; i
< playerct
; i
++) {
756 if (strcmp(players
[i
], "all") == 0 ||
757 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
758 (players
[i
][0] == '-' &&
759 players
[i
][1] == t1
->plchar
&&
760 players
[i
][2] == 0) ||
761 (digit(players
[i
][0]) && rank
<= atoi(players
[i
]))) {
764 (void) outentry(rank
, t1
, 0);
766 total_score
+= t1
->points
;
767 if (totcharct
< sizeof(totchars
) - 1)
768 totchars
[totcharct
++] = t1
->plchar
;
769 #endif /* nonsense */
776 totchars
[totcharct
] = 0;
779 * We would like to determine whether he is experienced. However, the
780 * information collected here only tells about the scores/roles that
781 * got into the topten (top 100?). We should maintain a .hacklog or
782 * something in his home directory.
784 flags
.beginner
= (total_score
< 6000);
785 for (i
= 0; i
< 6; i
++)
786 if (!strchr(totchars
, "CFKSTWX"[i
])) {
788 if (!pl_character
[0])
789 pl_character
[0] = "CFKSTWX"[i
];
792 #endif /* nonsense */