games: Massive style(9) cleanup commit. Reduces differences to NetBSD.
[dragonfly.git] / games / hack / hack.end.c
blob6ba411cf0aa4a187977971de6ce4ce10d67ac93d
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 $ */
6 #include "hack.h"
7 #define Sprintf (void) sprintf
9 #define newttentry() alloc(sizeof(struct toptenentry))
10 #define NAMSZ 8
11 #define DTHSZ 40
12 #define PERSMAX 1
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 */
16 struct toptenentry {
17 struct toptenentry *tt_next;
18 long int points;
19 int level,maxlvl,hp,maxhp;
20 int uid;
21 char plchar;
22 char sex;
23 char name[NAMSZ+1];
24 char death[DTHSZ+1];
25 char date[7]; /* yymmdd */
26 } *tt_head;
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);
36 xchar maxdlevel = 1;
38 void
39 done1(int unused __unused)
41 signal(SIGINT, SIG_IGN);
42 pline("Really quit?");
43 if (readchar() != 'y') {
44 signal(SIGINT, done1);
45 clrlin();
46 fflush(stdout);
47 if (multi > 0)
48 nomul(0);
49 return;
51 done("quit");
52 /* NOTREACHED */
55 int done_stopprint;
56 int done_hup;
58 static void
59 done_intr(int unused __unused)
61 done_stopprint++;
62 signal(SIGINT, SIG_IGN);
63 signal(SIGQUIT, SIG_IGN);
66 static void
67 done_hangup(int unused __unused)
69 done_hup++;
70 signal(SIGHUP, SIG_IGN);
71 done_intr(0);
74 void
75 done_in_by(struct monst *mtmp)
77 static char buf[BUFSZ];
79 pline("You die ...");
80 if (mtmp->data->mlet == ' ') {
81 Sprintf(buf, "the ghost of %s", (char *)mtmp->mextra);
82 killer = buf;
83 } else if (mtmp->mnamelth) {
84 Sprintf(buf, "%s called %s",
85 mtmp->data->mname, NAME(mtmp));
86 killer = buf;
87 } else if (mtmp->minvis) {
88 Sprintf(buf, "invisible %s", mtmp->data->mname);
89 killer = buf;
90 } else
91 killer = mtmp->data->mname;
92 done("died");
96 * called with arg "died", "drowned", "escaped", "quit", "choked",
97 * "panicked", "burned", "starved" or "tricked"
99 /* Be careful not to call panic from here! */
100 void
101 done(const char *st1)
103 #ifdef WIZARD
104 if (wizard && *st1 == 'd') {
105 u.uswldtim = 0;
106 if (u.uhpmax < 0) /* arbitrary */
107 u.uhpmax = 100;
108 u.uhp = u.uhpmax;
109 pline("For some reason you are still alive.");
110 flags.move = 0;
111 if (multi > 0)
112 multi = 0;
113 else
114 multi = -1;
115 flags.botl = 1;
116 return;
118 #endif /* WIZARD */
119 signal(SIGINT, done_intr);
120 signal(SIGQUIT, done_intr);
121 signal(SIGHUP, done_hangup);
122 if (*st1 == 'q' && u.uhp < 1) {
123 st1 = "died";
124 killer = "quit while already on Charon's boat";
126 if (*st1 == 's')
127 killer = "starvation";
128 else if (*st1 == 'd' && st1[1] == 'r')
129 killer = "drowning";
130 else if (*st1 == 'p')
131 killer = "panic";
132 else if (*st1 == 't')
133 killer = "trickery";
134 else if (!strchr("bcd", *st1))
135 killer = st1;
136 paybill();
137 clearlocks();
138 if (flags.toplin == 1)
139 more();
140 if (strchr("bcds", *st1)) {
141 #ifdef WIZARD
142 if (!wizard)
143 #endif /* WIZARD */
144 savebones();
145 if (!flags.notombstone)
146 outrip();
148 if (*st1 == 'c') /* after outrip() */
149 killer = st1;
150 settty(NULL); /* does a clear_screen() */
151 if (!done_stopprint)
152 printf("Goodbye %s %s...\n\n", pl_character, plname);
154 long int tmp;
155 tmp = u.ugold - u.ugold0;
156 if (tmp < 0)
157 tmp = 0;
158 if (*st1 == 'd' || *st1 == 'b')
159 tmp -= tmp / 10;
160 u.urexp += tmp;
161 u.urexp += 50 * maxdlevel;
162 if (maxdlevel > 20)
163 u.urexp += 1000 * ((maxdlevel > 30) ? 10 : maxdlevel - 20);
165 if (*st1 == 'e') {
166 struct monst *mtmp;
167 struct obj *otmp;
168 int i;
169 unsigned worthlessct = 0;
170 boolean has_amulet = FALSE;
172 killer = st1;
173 keepdogs();
174 mtmp = mydogs;
175 if (mtmp) {
176 if (!done_stopprint)
177 printf("You");
178 while (mtmp) {
179 if (!done_stopprint)
180 printf(" and %s", monnam(mtmp));
181 if (mtmp->mtame)
182 u.urexp += mtmp->mhp;
183 mtmp = mtmp->nmon;
185 if (!done_stopprint)
186 printf("\nescaped from the dungeon with %ld points,\n",
187 u.urexp);
188 } else if (!done_stopprint)
189 printf("You escaped from the dungeon with %ld points,\n",
190 u.urexp);
191 for (otmp = invent; otmp; otmp = otmp->nobj) {
192 if (otmp->olet == GEM_SYM) {
193 objects[otmp->otyp].oc_name_known = 1;
194 i = otmp->quan * objects[otmp->otyp].g_val;
195 if (i == 0) {
196 worthlessct += otmp->quan;
197 continue;
199 u.urexp += i;
200 if (!done_stopprint)
201 printf("\t%s (worth %d Zorkmids),\n",
202 doname(otmp), i);
203 } else if (otmp->olet == AMULET_SYM) {
204 otmp->known = 1;
205 i = (otmp->spe < 0) ? 2 : 5000;
206 u.urexp += i;
207 if (!done_stopprint)
208 printf("\t%s (worth %d Zorkmids),\n",
209 doname(otmp), i);
210 if (otmp->spe >= 0) {
211 has_amulet = TRUE;
212 killer = "escaped (with amulet)";
216 if (worthlessct)
217 if (!done_stopprint)
218 printf("\t%u worthless piece%s of coloured glass,\n",
219 worthlessct, plur(worthlessct));
220 if (has_amulet)
221 u.urexp *= 2;
222 } else if (!done_stopprint)
223 printf("You %s on dungeon level %d with %ld points,\n",
224 st1, dlevel, u.urexp);
225 if (!done_stopprint)
226 printf("and %ld piece%s of gold, after %ld move%s.\n",
227 u.ugold, plur(u.ugold), moves, plur(moves));
228 if (!done_stopprint)
229 printf("You were level %u with a maximum of %d hit points when you %s.\n",
230 u.ulevel, u.uhpmax, st1);
231 if (*st1 == 'e' && !done_stopprint) {
232 getret(); /* all those pieces of coloured glass ... */
233 cls();
235 #ifdef WIZARD
236 if (!wizard)
237 #endif /* WIZARD */
238 topten();
239 if (done_stopprint)
240 printf("\n\n");
241 exit(0);
244 static void
245 topten(void)
247 int uid = getuid();
248 int rank, rank0 = -1, rank1 = 0;
249 int occ_cnt = PERSMAX;
250 struct toptenentry *t0, *t1, *tprev;
251 const char *recfile = RECORD;
252 const char *reclock = "record_lock";
253 int sleepct = 300;
254 FILE *rfile;
255 int flg = 0;
257 #define HUP if (!done_hup)
258 while (link(recfile, reclock) == -1) {
259 HUP perror(reclock);
260 if (!sleepct--) {
261 HUP puts("I give up. Sorry.");
262 HUP puts("Perhaps there is an old record_lock around?");
263 return;
265 HUP printf("Waiting for access to record file. (%d)\n",
266 sleepct);
267 HUP fflush(stdout);
268 sleep(1);
270 if (!(rfile = fopen(recfile, "r"))) {
271 HUP puts("Cannot open record file!");
272 goto unlock;
274 HUP putchar('\n');
276 /* create a new 'topten' entry */
277 t0 = newttentry();
278 t0->level = dlevel;
279 t0->maxlvl = maxdlevel;
280 t0->hp = u.uhp;
281 t0->maxhp = u.uhpmax;
282 t0->points = u.urexp;
283 t0->plchar = pl_character[0];
284 t0->sex = (flags.female ? 'F' : 'M');
285 t0->uid = uid;
286 strncpy(t0->name, plname, NAMSZ);
287 (t0->name)[NAMSZ] = 0;
288 strncpy(t0->death, killer, DTHSZ);
289 (t0->death)[DTHSZ] = 0;
290 strcpy(t0->date, getdate());
292 /* assure minimum number of points */
293 if (t0->points < POINTSMIN)
294 t0->points = 0;
296 t1 = tt_head = newttentry();
297 tprev = 0;
298 /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
299 for (rank = 1;;) {
300 if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
301 t1->date, &t1->uid,
302 &t1->level, &t1->maxlvl,
303 &t1->hp, &t1->maxhp, &t1->points,
304 &t1->plchar, &t1->sex, t1->name, t1->death) != 11
305 || t1->points < POINTSMIN)
306 t1->points = 0;
307 if (rank0 < 0 && t1->points < t0->points) {
308 rank0 = rank++;
309 if (tprev == 0)
310 tt_head = t0;
311 else
312 tprev->tt_next = t0;
313 t0->tt_next = t1;
314 occ_cnt--;
315 flg++; /* ask for a rewrite */
316 } else
317 tprev = t1;
318 if (t1->points == 0)
319 break;
320 if (
321 #ifdef PERS_IS_UID
322 t1->uid == t0->uid &&
323 #else
324 strncmp(t1->name, t0->name, NAMSZ) == 0 &&
325 #endif /* PERS_IS_UID */
326 t1->plchar == t0->plchar && --occ_cnt <= 0) {
327 if (rank0 < 0) {
328 rank0 = 0;
329 rank1 = rank;
330 HUP printf("You didn't beat your previous score of %ld points.\n\n",
331 t1->points);
333 if (occ_cnt < 0) {
334 flg++;
335 continue;
338 if (rank <= ENTRYMAX) {
339 t1 = t1->tt_next = newttentry();
340 rank++;
342 if (rank > ENTRYMAX) {
343 t1->points = 0;
344 break;
347 if (flg) { /* rewrite record file */
348 fclose(rfile);
349 if (!(rfile = fopen(recfile, "w"))) {
350 HUP puts("Cannot write record file\n");
351 goto unlock;
354 if (!done_stopprint)
355 if (rank0 > 0) {
356 if (rank0 <= 10)
357 puts("You made the top ten list!\n");
358 else
359 printf("You reached the %d%s place on the top %d list.\n\n",
360 rank0, ordin(rank0), ENTRYMAX);
363 if (rank0 == 0)
364 rank0 = rank1;
365 if (rank0 <= 0)
366 rank0 = rank;
367 if (!done_stopprint)
368 outheader();
369 t1 = tt_head;
370 for (rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) {
371 if (flg)
372 fprintf(rfile, "%6s %d %d %d %d %d %ld %c%c %s,%s\n",
373 t1->date, t1->uid,
374 t1->level, t1->maxlvl,
375 t1->hp, t1->maxhp, t1->points,
376 t1->plchar, t1->sex, t1->name, t1->death);
377 if (done_stopprint)
378 continue;
379 if (rank > (int)flags.end_top &&
380 (rank < rank0 - (int)flags.end_around || rank > rank0 +
381 (int)flags.end_around)
382 && (!flags.end_own ||
383 #ifdef PERS_IS_UID
384 t1->uid != t0->uid))
385 #else
386 strncmp(t1->name, t0->name, NAMSZ)))
387 #endif /* PERS_IS_UID */
388 continue;
389 if (rank == rank0 - (int)flags.end_around &&
390 rank0 > (int)flags.end_top + (int)flags.end_around + 1 &&
391 !flags.end_own)
392 putchar('\n');
393 if (rank != rank0)
394 outentry(rank, t1, 0);
395 else if (!rank1)
396 outentry(rank, t1, 1);
397 else {
398 int t0lth = outentry(0, t0, -1);
399 int t1lth = outentry(rank, t1, t0lth);
400 if (t1lth > t0lth)
401 t0lth = t1lth;
402 outentry(0, t0, t0lth);
405 if (rank0 >= rank)
406 if (!done_stopprint)
407 outentry(0, t0, 1);
408 fclose(rfile);
409 unlock:
410 unlink(reclock);
413 static void
414 outheader(void)
416 char linebuf[BUFSZ];
417 char *bp;
419 strcpy(linebuf, "Number Points Name");
420 bp = eos(linebuf);
421 while (bp < linebuf + COLNO - 9)
422 *bp++ = ' ';
423 strcpy(bp, "Hp [max]");
424 puts(linebuf);
427 /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
428 static int
429 outentry(int rank, struct toptenentry *t1, int so)
431 boolean quit = FALSE, dead = FALSE, starv = FALSE;
432 char linebuf[BUFSZ];
434 linebuf[0] = 0;
435 if (rank)
436 Sprintf(eos(linebuf), "%3d", rank);
437 else
438 Sprintf(eos(linebuf), " ");
439 Sprintf(eos(linebuf), " %6ld %8s", t1->points, t1->name);
440 if (t1->plchar == 'X')
441 Sprintf(eos(linebuf), " ");
442 else
443 Sprintf(eos(linebuf), "-%c ", t1->plchar);
444 if (!strncmp("escaped", t1->death, 7)) {
445 if (!strcmp(" (with amulet)", t1->death + 7))
446 Sprintf(eos(linebuf), "escaped the dungeon with amulet");
447 else
448 Sprintf(eos(linebuf), "escaped the dungeon [max level %d]",
449 t1->maxlvl);
450 } else {
451 if (!strncmp(t1->death, "quit", 4)) {
452 quit = TRUE;
453 if (t1->maxhp < 3 * t1->hp && t1->maxlvl < 4)
454 Sprintf(eos(linebuf), "cravenly gave up");
455 else
456 Sprintf(eos(linebuf), "quit");
457 } else if (!strcmp(t1->death, "choked")) {
458 Sprintf(eos(linebuf), "choked on %s food",
459 (t1->sex == 'F') ? "her" : "his");
460 } else if (!strncmp(t1->death, "starv", 5)) {
461 Sprintf(eos(linebuf), "starved to death");
462 starv = TRUE;
463 } else {
464 Sprintf(eos(linebuf), "was killed");
465 dead = TRUE;
467 Sprintf(eos(linebuf), " on%s level %d",
468 (dead || starv) ? "" : " dungeon", t1->level);
469 if (t1->maxlvl != t1->level)
470 Sprintf(eos(linebuf), " [max %d]", t1->maxlvl);
471 if (quit && t1->death[4])
472 Sprintf(eos(linebuf), t1->death + 4);
474 if (dead) {
475 Sprintf(eos(linebuf), " by %s%s",
476 (!strncmp(t1->death, "trick", 5) ||
477 !strncmp(t1->death, "the ", 4))
478 ? "" :
479 strchr(vowels, *t1->death) ? "an " : "a ",
480 t1->death);
482 Sprintf(eos(linebuf), ".");
483 if (t1->maxhp) {
484 char *bp = eos(linebuf);
485 char hpbuf[10];
486 int hppos;
488 Sprintf(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-");
489 hppos = COLNO - 7 - strlen(hpbuf);
490 if (bp <= linebuf + hppos) {
491 while (bp < linebuf + hppos)
492 *bp++ = ' ';
493 strcpy(bp, hpbuf);
494 Sprintf(eos(bp), " [%d]", t1->maxhp);
497 if (so == 0)
498 puts(linebuf);
499 else if (so > 0) {
500 char *bp = eos(linebuf);
501 if (so >= COLNO)
502 so = COLNO - 1;
503 while (bp < linebuf + so)
504 *bp++ = ' ';
505 *bp = 0;
506 standoutbeg();
507 fputs(linebuf, stdout);
508 standoutend();
509 putchar('\n');
511 return (strlen(linebuf));
514 static char *
515 itoa(int a)
517 static char buf[12];
519 Sprintf(buf, "%d", a);
520 return (buf);
523 static const char *
524 ordin(int n)
526 int d1 = n % 10;
528 return ((d1 == 0 || d1 > 3 || n / 10 == 1) ? "th" : (d1 == 1) ? "st" :
529 (d1 == 2) ? "nd" : "rd");
532 void
533 clearlocks(void)
535 int x;
537 signal(SIGHUP, SIG_IGN);
538 for (x = maxdlevel; x >= 0; x--) {
539 glo(x);
540 unlink(lock); /* not all levels need be present */
544 #ifdef NOSAVEONHANGUP
545 void
546 hangup(int unused __unused)
548 signal(SIGINT, SIG_IGN);
549 clearlocks();
550 exit(1);
552 #endif /* NOSAVEONHANGUP */
554 char *
555 eos(char *s)
557 while (*s)
558 s++;
559 return (s);
562 /* it is the callers responsibility to check that there is room for c */
563 void
564 charcat(char *s, char c)
566 while (*s)
567 s++;
568 *s++ = c;
569 *s = 0;
573 * Called with args from main if argc >= 0. In this case, list scores as
574 * requested. Otherwise, find scores for the current player (and list them
575 * if argc == -1).
577 void
578 prscore(int argc, char **argv)
580 char **players = NULL;
581 int playerct;
582 int rank;
583 struct toptenentry *t1, *t2;
584 const char *recfile = RECORD;
585 FILE *rfile;
586 int flg = 0;
587 int i;
588 #ifdef nonsense
589 long total_score = 0L;
590 char totchars[10];
591 int totcharct = 0;
592 #endif /* nonsense */
593 int outflg = (argc >= -1);
594 #ifdef PERS_IS_UID
595 int uid = -1;
596 #else
597 char *player0;
598 #endif /* PERS_IS_UID */
600 if (!(rfile = fopen(recfile, "r"))) {
601 puts("Cannot open record file!");
602 return;
605 if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
606 if (!argv[1][2]) {
607 argc--;
608 argv++;
609 } else if (!argv[1][3] && strchr("CFKSTWX", argv[1][2])) {
610 argv[1]++;
611 argv[1][0] = '-';
612 } else
613 argv[1] += 2;
615 if (argc <= 1) {
616 #ifdef PERS_IS_UID
617 uid = getuid();
618 playerct = 0;
619 #else
620 player0 = plname;
621 if (!*player0)
622 player0 = "hackplayer";
623 playerct = 1;
624 players = &player0;
625 #endif /* PERS_IS_UID */
626 } else {
627 playerct = --argc;
628 players = ++argv;
630 if (outflg)
631 putchar('\n');
633 t1 = tt_head = newttentry();
634 for (rank = 1;; rank++) {
635 if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
636 t1->date, &t1->uid,
637 &t1->level, &t1->maxlvl,
638 &t1->hp, &t1->maxhp, &t1->points,
639 &t1->plchar, &t1->sex, t1->name, t1->death) != 11)
640 t1->points = 0;
641 if (t1->points == 0)
642 break;
643 #ifdef PERS_IS_UID
644 if (!playerct && t1->uid == uid)
645 flg++;
646 else
647 #endif /* PERS_IS_UID */
648 for (i = 0; i < playerct; i++) {
649 if (strcmp(players[i], "all") == 0 ||
650 strncmp(t1->name, players[i], NAMSZ) == 0 ||
651 (players[i][0] == '-' &&
652 players[i][1] == t1->plchar &&
653 players[i][2] == 0) ||
654 (digit(players[i][0]) && rank <= atoi(players[i])))
655 flg++;
657 t1 = t1->tt_next = newttentry();
659 fclose(rfile);
660 if (!flg) {
661 if (outflg) {
662 printf("Cannot find any entries for ");
663 if (playerct < 1)
664 printf("you.\n");
665 else {
666 if (playerct > 1)
667 printf("any of ");
668 for (i = 0; i < playerct; i++)
669 printf("%s%s", players[i],
670 (i < playerct - 1) ? ", " : ".\n");
671 printf("Call is: %s -s [playernames]\n", hname);
674 return;
677 if (outflg)
678 outheader();
679 t1 = tt_head;
680 for (rank = 1; t1->points != 0; rank++, t1 = t2) {
681 t2 = t1->tt_next;
682 #ifdef PERS_IS_UID
683 if (!playerct && t1->uid == uid)
684 goto outwithit;
685 else
686 #endif /* PERS_IS_UID */
687 for (i = 0; i < playerct; i++) {
688 if (strcmp(players[i], "all") == 0 ||
689 strncmp(t1->name, players[i], NAMSZ) == 0 ||
690 (players[i][0] == '-' &&
691 players[i][1] == t1->plchar &&
692 players[i][2] == 0) ||
693 (digit(players[i][0]) && rank <=
694 atoi(players[i]))) {
695 outwithit:
696 if (outflg)
697 outentry(rank, t1, 0);
698 #ifdef nonsense
699 total_score += t1->points;
700 if (totcharct < sizeof(totchars) - 1)
701 totchars[totcharct++] = t1->plchar;
702 #endif /* nonsense */
703 break;
706 free(t1);
708 #ifdef nonsense
709 totchars[totcharct] = 0;
712 * We would like to determine whether he is experienced. However, the
713 * information collected here only tells about the scores/roles that
714 * got into the topten (top 100?). We should maintain a .hacklog or
715 * something in his home directory.
717 flags.beginner = (total_score < 6000);
718 for (i = 0; i < 6; i++)
719 if (!strchr(totchars, "CFKSTWX"[i])) {
720 flags.beginner = 1;
721 if (!pl_character[0])
722 pl_character[0] = "CFKSTWX"[i];
723 break;
725 #endif /* nonsense */