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() (struct toptenentry *) 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(__unused
int unused
)
41 signal(SIGINT
,SIG_IGN
);
42 pline("Really quit?");
43 if(readchar() != 'y') {
47 if(multi
> 0) nomul(0);
58 done_intr(__unused
int unused
)
61 signal(SIGINT
, SIG_IGN
);
62 signal(SIGQUIT
, SIG_IGN
);
66 done_hangup(__unused
int unused
)
69 signal(SIGHUP
, SIG_IGN
);
74 done_in_by(struct monst
*mtmp
)
76 static char buf
[BUFSZ
];
78 if(mtmp
->data
->mlet
== ' '){
79 Sprintf(buf
, "the ghost of %s", (char *) mtmp
->mextra
);
81 } else if(mtmp
->mnamelth
) {
82 Sprintf(buf
, "%s called %s",
83 mtmp
->data
->mname
, NAME(mtmp
));
85 } else if(mtmp
->minvis
) {
86 Sprintf(buf
, "invisible %s", mtmp
->data
->mname
);
88 } else killer
= mtmp
->data
->mname
;
92 /* called with arg "died", "drowned", "escaped", "quit", "choked", "panicked",
93 "burned", "starved" or "tricked" */
94 /* Be careful not to call panic from here! */
100 if(wizard
&& *st1
== 'd'){
102 if(u
.uhpmax
< 0) u
.uhpmax
= 100; /* arbitrary */
104 pline("For some reason you are still alive.");
106 if(multi
> 0) multi
= 0; else multi
= -1;
111 signal(SIGINT
, done_intr
);
112 signal(SIGQUIT
, done_intr
);
113 signal(SIGHUP
, done_hangup
);
114 if(*st1
== 'q' && u
.uhp
< 1){
116 killer
= "quit while already on Charon's boat";
118 if(*st1
== 's') killer
= "starvation"; else
119 if(*st1
== 'd' && st1
[1] == 'r') killer
= "drowning"; else
120 if(*st1
== 'p') killer
= "panic"; else
121 if(*st1
== 't') killer
= "trickery"; else
122 if(!index("bcd", *st1
)) killer
= st1
;
125 if(flags
.toplin
== 1) more();
126 if(index("bcds", *st1
)){
131 if(!flags
.notombstone
)
134 if(*st1
== 'c') killer
= st1
; /* after outrip() */
135 settty((char *) 0); /* does a clear_screen() */
137 printf("Goodbye %s %s...\n\n", pl_character
, plname
);
139 tmp
= u
.ugold
- u
.ugold0
;
142 if(*st1
== 'd' || *st1
== 'b')
145 u
.urexp
+= 50 * maxdlevel
;
147 u
.urexp
+= 1000*((maxdlevel
> 30) ? 10 : maxdlevel
- 20);
153 unsigned worthlessct
= 0;
154 boolean has_amulet
= FALSE
;
160 if(!done_stopprint
) printf("You");
163 printf(" and %s", monnam(mtmp
));
165 u
.urexp
+= mtmp
->mhp
;
169 printf("\nescaped from the dungeon with %ld points,\n",
173 printf("You escaped from the dungeon with %ld points,\n",
175 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
176 if(otmp
->olet
== GEM_SYM
){
177 objects
[otmp
->otyp
].oc_name_known
= 1;
178 i
= otmp
->quan
*objects
[otmp
->otyp
].g_val
;
180 worthlessct
+= otmp
->quan
;
185 printf("\t%s (worth %d Zorkmids),\n",
187 } else if(otmp
->olet
== AMULET_SYM
) {
189 i
= (otmp
->spe
< 0) ? 2 : 5000;
192 printf("\t%s (worth %d Zorkmids),\n",
196 killer
= "escaped (with amulet)";
200 if(worthlessct
) if(!done_stopprint
)
201 printf("\t%u worthless piece%s of coloured glass,\n",
202 worthlessct
, plur(worthlessct
));
203 if(has_amulet
) u
.urexp
*= 2;
206 printf("You %s on dungeon level %d with %ld points,\n",
207 st1
, dlevel
, u
.urexp
);
209 printf("and %ld piece%s of gold, after %ld move%s.\n",
210 u
.ugold
, plur(u
.ugold
), moves
, plur(moves
));
212 printf("You were level %u with a maximum of %d hit points when you %s.\n",
213 u
.ulevel
, u
.uhpmax
, st1
);
214 if(*st1
== 'e' && !done_stopprint
){
215 getret(); /* all those pieces of coloured glass ... */
222 if(done_stopprint
) printf("\n\n");
230 int rank
, rank0
= -1, rank1
= 0;
231 int occ_cnt
= PERSMAX
;
232 struct toptenentry
*t0
, *t1
, *tprev
;
233 const char *recfile
= RECORD
;
234 const char *reclock
= "record_lock";
238 #define HUP if(!done_hup)
239 while(link(recfile
, reclock
) == -1) {
242 HUP
puts("I give up. Sorry.");
243 HUP
puts("Perhaps there is an old record_lock around?");
246 HUP
printf("Waiting for access to record file. (%d)\n",
251 if(!(rfile
= fopen(recfile
,"r"))){
252 HUP
puts("Cannot open record file!");
257 /* create a new 'topten' entry */
260 t0
->maxlvl
= maxdlevel
;
262 t0
->maxhp
= u
.uhpmax
;
263 t0
->points
= u
.urexp
;
264 t0
->plchar
= pl_character
[0];
265 t0
->sex
= (flags
.female
? 'F' : 'M');
267 strncpy(t0
->name
, plname
, NAMSZ
);
268 (t0
->name
)[NAMSZ
] = 0;
269 strncpy(t0
->death
, killer
, DTHSZ
);
270 (t0
->death
)[DTHSZ
] = 0;
271 strcpy(t0
->date
, getdate());
273 /* assure minimum number of points */
274 if(t0
->points
< POINTSMIN
)
277 t1
= tt_head
= newttentry();
279 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
281 if(fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
283 &t1
->level
, &t1
->maxlvl
,
284 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
285 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11
286 || t1
->points
< POINTSMIN
)
288 if(rank0
< 0 && t1
->points
< t0
->points
) {
296 flg
++; /* ask for a rewrite */
299 if(t1
->points
== 0) break;
302 t1
->uid
== t0
->uid
&&
304 strncmp(t1
->name
, t0
->name
, NAMSZ
) == 0 &&
305 #endif /* PERS_IS_UID */
306 t1
->plchar
== t0
->plchar
&& --occ_cnt
<= 0){
310 HUP
printf("You didn't beat your previous score of %ld points.\n\n",
318 if(rank
<= ENTRYMAX
){
319 t1
= t1
->tt_next
= newttentry();
327 if(flg
) { /* rewrite record file */
329 if(!(rfile
= fopen(recfile
,"w"))){
330 HUP
puts("Cannot write record file\n");
334 if(!done_stopprint
) if(rank0
> 0){
336 puts("You made the top ten list!\n");
338 printf("You reached the %d%s place on the top %d list.\n\n",
339 rank0
, ordin(rank0
), ENTRYMAX
);
342 if(rank0
== 0) rank0
= rank1
;
343 if(rank0
<= 0) rank0
= rank
;
344 if(!done_stopprint
) outheader();
346 for(rank
= 1; t1
->points
!= 0; rank
++, t1
= t1
->tt_next
) {
347 if(flg
) fprintf(rfile
,"%6s %d %d %d %d %d %ld %c%c %s,%s\n",
349 t1
->level
, t1
->maxlvl
,
350 t1
->hp
, t1
->maxhp
, t1
->points
,
351 t1
->plchar
, t1
->sex
, t1
->name
, t1
->death
);
352 if(done_stopprint
) continue;
353 if(rank
> (int)flags
.end_top
&&
354 (rank
< rank0
-(int)flags
.end_around
|| rank
> rank0
+(int)flags
.end_around
)
355 && (!flags
.end_own
||
357 t1
->uid
!= t0
->uid
))
359 strncmp(t1
->name
, t0
->name
, NAMSZ
)))
360 #endif /* PERS_IS_UID */
362 if(rank
== rank0
-(int)flags
.end_around
&&
363 rank0
> (int)flags
.end_top
+(int)flags
.end_around
+1 &&
367 outentry(rank
, t1
, 0);
369 outentry(rank
, t1
, 1);
371 int t0lth
= outentry(0, t0
, -1);
372 int t1lth
= outentry(rank
, t1
, t0lth
);
373 if(t1lth
> t0lth
) t0lth
= t1lth
;
374 outentry(0, t0
, t0lth
);
377 if(rank0
>= rank
) if(!done_stopprint
)
389 strcpy(linebuf
, "Number Points Name");
391 while(bp
< linebuf
+ COLNO
- 9) *bp
++ = ' ';
392 strcpy(bp
, "Hp [max]");
396 /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
398 outentry(int rank
, struct toptenentry
*t1
, int so
)
400 boolean quit
= FALSE
, dead
= FALSE
, starv
= FALSE
;
403 if(rank
) Sprintf(eos(linebuf
), "%3d", rank
);
404 else Sprintf(eos(linebuf
), " ");
405 Sprintf(eos(linebuf
), " %6ld %8s", t1
->points
, t1
->name
);
406 if(t1
->plchar
== 'X') Sprintf(eos(linebuf
), " ");
407 else Sprintf(eos(linebuf
), "-%c ", t1
->plchar
);
408 if(!strncmp("escaped", t1
->death
, 7)) {
409 if(!strcmp(" (with amulet)", t1
->death
+7))
410 Sprintf(eos(linebuf
), "escaped the dungeon with amulet");
412 Sprintf(eos(linebuf
), "escaped the dungeon [max level %d]",
415 if(!strncmp(t1
->death
,"quit",4)) {
417 if(t1
->maxhp
< 3*t1
->hp
&& t1
->maxlvl
< 4)
418 Sprintf(eos(linebuf
), "cravenly gave up");
420 Sprintf(eos(linebuf
), "quit");
422 else if(!strcmp(t1
->death
,"choked"))
423 Sprintf(eos(linebuf
), "choked on %s food",
424 (t1
->sex
== 'F') ? "her" : "his");
425 else if(!strncmp(t1
->death
,"starv",5))
426 Sprintf(eos(linebuf
), "starved to death"), starv
= TRUE
;
427 else Sprintf(eos(linebuf
), "was killed"), dead
= TRUE
;
428 Sprintf(eos(linebuf
), " on%s level %d",
429 (dead
|| starv
) ? "" : " dungeon", t1
->level
);
430 if(t1
->maxlvl
!= t1
->level
)
431 Sprintf(eos(linebuf
), " [max %d]", t1
->maxlvl
);
432 if(quit
&& t1
->death
[4]) Sprintf(eos(linebuf
), t1
->death
+ 4);
434 if(dead
) Sprintf(eos(linebuf
), " by %s%s",
435 (!strncmp(t1
->death
, "trick", 5) || !strncmp(t1
->death
, "the ", 4))
437 index(vowels
,*t1
->death
) ? "an " : "a ",
439 Sprintf(eos(linebuf
), ".");
441 char *bp
= eos(linebuf
);
444 Sprintf(hpbuf
, (t1
->hp
> 0) ? itoa(t1
->hp
) : "-");
445 hppos
= COLNO
- 7 - strlen(hpbuf
);
446 if(bp
<= linebuf
+ hppos
) {
447 while(bp
< linebuf
+ hppos
) *bp
++ = ' ';
449 Sprintf(eos(bp
), " [%d]", t1
->maxhp
);
452 if(so
== 0) puts(linebuf
);
454 char *bp
= eos(linebuf
);
455 if(so
>= COLNO
) so
= COLNO
-1;
456 while(bp
< linebuf
+ so
) *bp
++ = ' ';
459 fputs(linebuf
,stdout
);
463 return(strlen(linebuf
));
478 return((d1
==0 || d1
>3 || n
/10==1) ? "th" : (d1
==1) ? "st" :
479 (d1
==2) ? "nd" : "rd");
486 signal(SIGHUP
,SIG_IGN
);
487 for(x
= maxdlevel
; x
>= 0; x
--) {
489 unlink(lock
); /* not all levels need be present */
493 #ifdef NOSAVEONHANGUP
495 hangup(__unused
int unused
)
497 signal(SIGINT
, SIG_IGN
);
501 #endif /* NOSAVEONHANGUP */
510 /* it is the callers responsibility to check that there is room for c */
512 charcat(char *s
, char c
)
520 * Called with args from main if argc >= 0. In this case, list scores as
521 * requested. Otherwise, find scores for the current player (and list them
525 prscore(int argc
, char **argv
)
527 char **players
= NULL
;
530 struct toptenentry
*t1
, *t2
;
531 const char *recfile
= RECORD
;
536 long total_score
= 0L;
539 #endif /* nonsense */
540 int outflg
= (argc
>= -1);
545 #endif /* PERS_IS_UID */
547 if(!(rfile
= fopen(recfile
,"r"))){
548 puts("Cannot open record file!");
552 if(argc
> 1 && !strncmp(argv
[1], "-s", 2)){
556 } else if(!argv
[1][3] && index("CFKSTWX", argv
[1][2])) {
568 player0
= "hackplayer";
571 #endif /* PERS_IS_UID */
576 if(outflg
) putchar('\n');
578 t1
= tt_head
= newttentry();
579 for(rank
= 1; ; rank
++) {
580 if(fscanf(rfile
, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
582 &t1
->level
, &t1
->maxlvl
,
583 &t1
->hp
, &t1
->maxhp
, &t1
->points
,
584 &t1
->plchar
, &t1
->sex
, t1
->name
, t1
->death
) != 11)
586 if(t1
->points
== 0) break;
588 if(!playerct
&& t1
->uid
== uid
)
591 #endif /* PERS_IS_UID */
592 for(i
= 0; i
< playerct
; i
++){
593 if(strcmp(players
[i
], "all") == 0 ||
594 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
595 (players
[i
][0] == '-' &&
596 players
[i
][1] == t1
->plchar
&&
597 players
[i
][2] == 0) ||
598 (digit(players
[i
][0]) && rank
<= atoi(players
[i
])))
601 t1
= t1
->tt_next
= newttentry();
606 printf("Cannot find any entries for ");
607 if(playerct
< 1) printf("you.\n");
609 if(playerct
> 1) printf("any of ");
610 for(i
=0; i
<playerct
; i
++)
611 printf("%s%s", players
[i
], (i
<playerct
-1)?", ":".\n");
612 printf("Call is: %s -s [playernames]\n", hname
);
618 if(outflg
) outheader();
620 for(rank
= 1; t1
->points
!= 0; rank
++, t1
= t2
) {
623 if(!playerct
&& t1
->uid
== uid
)
626 #endif /* PERS_IS_UID */
627 for(i
= 0; i
< playerct
; i
++){
628 if(strcmp(players
[i
], "all") == 0 ||
629 strncmp(t1
->name
, players
[i
], NAMSZ
) == 0 ||
630 (players
[i
][0] == '-' &&
631 players
[i
][1] == t1
->plchar
&&
632 players
[i
][2] == 0) ||
633 (digit(players
[i
][0]) && rank
<= atoi(players
[i
]))){
636 outentry(rank
, t1
, 0);
638 total_score
+= t1
->points
;
639 if(totcharct
< sizeof(totchars
)-1)
640 totchars
[totcharct
++] = t1
->plchar
;
641 #endif /* nonsense */
648 totchars
[totcharct
] = 0;
650 /* We would like to determine whether he is experienced. However,
651 the information collected here only tells about the scores/roles
652 that got into the topten (top 100?). We should maintain a
653 .hacklog or something in his home directory. */
654 flags
.beginner
= (total_score
< 6000);
656 if(!index(totchars
, "CFKSTWX"[i
])) {
658 if(!pl_character
[0]) pl_character
[0] = "CFKSTWX"[i
];
661 #endif /* nonsense */