NetBSD/games/hack/hack.end.c

828 lines
21 KiB
C
Raw Normal View History

2011-08-07 00:29:37 +04:00
/* $NetBSD: hack.end.c,v 1.16 2011/08/06 20:29:37 dholland Exp $ */
1997-10-19 20:56:41 +04:00
1993-08-02 21:16:36 +04:00
/*
* Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
* Amsterdam
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Stichting Centrum voor Wiskunde en
* Informatica, nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1993-08-02 21:16:36 +04:00
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
1993-08-02 21:16:36 +04:00
#ifndef lint
2011-08-07 00:29:37 +04:00
__RCSID("$NetBSD: hack.end.c,v 1.16 2011/08/06 20:29:37 dholland Exp $");
1997-10-19 20:56:41 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
#include <signal.h>
1997-10-19 20:56:41 +04:00
#include <unistd.h>
#include <stdlib.h>
#include "hack.h"
#include "extern.h"
#define Snprintf (void) snprintf
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
xchar maxdlevel = 1;
1993-03-21 12:45:37 +03:00
2009-08-12 11:28:40 +04:00
struct toptenentry;
static void topten(void);
static void outheader(void);
static int outentry(int, struct toptenentry *, int);
static char *itoa(int);
static const char *ordin(int);
1997-10-19 20:56:41 +04:00
int
dodone(void)
1997-10-19 20:56:41 +04:00
{
done1(0);
return 0;
}
/*ARGSUSED*/
1993-03-21 12:45:37 +03:00
void
done1(int n __unused)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
(void) signal(SIGINT, SIG_IGN);
1993-03-21 12:45:37 +03:00
pline("Really quit?");
1997-10-19 20:56:41 +04:00
if (readchar() != 'y') {
(void) signal(SIGINT, done1);
1993-03-21 12:45:37 +03:00
clrlin();
(void) fflush(stdout);
1997-10-19 20:56:41 +04:00
if (multi > 0)
nomul(0);
1993-03-21 12:45:37 +03:00
return;
}
done("quit");
/* NOTREACHED */
}
2009-08-12 11:28:40 +04:00
static int done_stopprint;
static int done_hup;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
/*ARGSUSED*/
2009-08-12 11:28:40 +04:00
static void
done_intr(int n __unused)
1997-10-19 20:56:41 +04:00
{
1993-03-21 12:45:37 +03:00
done_stopprint++;
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
}
2009-08-12 11:28:40 +04:00
static void
done_hangup(int n)
1997-10-19 20:56:41 +04:00
{
1993-03-21 12:45:37 +03:00
done_hup++;
(void) signal(SIGHUP, SIG_IGN);
1997-10-19 20:56:41 +04:00
done_intr(n);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
done_in_by(struct monst *mtmp)
1997-10-19 20:56:41 +04:00
{
static char buf[BUFSZ];
1993-03-21 12:45:37 +03:00
pline("You die ...");
1997-10-19 20:56:41 +04:00
if (mtmp->data->mlet == ' ') {
Snprintf(buf, sizeof(buf),
"the ghost of %s", (char *) mtmp->mextra);
1993-03-21 12:45:37 +03:00
killer = buf;
1997-10-19 20:56:41 +04:00
} else if (mtmp->mnamelth) {
Snprintf(buf, sizeof(buf), "%s called %s",
1993-03-21 12:45:37 +03:00
mtmp->data->mname, NAME(mtmp));
killer = buf;
1997-10-19 20:56:41 +04:00
} else if (mtmp->minvis) {
Snprintf(buf, sizeof(buf), "invisible %s", mtmp->data->mname);
1993-03-21 12:45:37 +03:00
killer = buf;
1997-10-19 20:56:41 +04:00
} else
killer = mtmp->data->mname;
1993-03-21 12:45:37 +03:00
done("died");
}
1997-10-19 20:56:41 +04:00
/*
* called with arg "died", "drowned", "escaped", "quit", "choked",
* "panicked", "burned", "starved" or "tricked"
*/
1993-03-21 12:45:37 +03:00
/* Be careful not to call panic from here! */
1997-10-19 20:56:41 +04:00
void
done(const char *st1)
1993-03-21 12:45:37 +03:00
{
#ifdef WIZARD
1997-10-19 20:56:41 +04:00
if (wizard && *st1 == 'd') {
1993-03-21 12:45:37 +03:00
u.uswldtim = 0;
1997-10-19 20:56:41 +04:00
if (u.uhpmax < 0)
u.uhpmax = 100; /* arbitrary */
1993-03-21 12:45:37 +03:00
u.uhp = u.uhpmax;
pline("For some reason you are still alive.");
flags.move = 0;
1997-10-19 20:56:41 +04:00
if (multi > 0)
multi = 0;
else
multi = -1;
1993-03-21 12:45:37 +03:00
flags.botl = 1;
return;
}
1997-10-19 20:56:41 +04:00
#endif /* WIZARD */
1993-03-21 12:45:37 +03:00
(void) signal(SIGINT, done_intr);
(void) signal(SIGQUIT, done_intr);
(void) signal(SIGHUP, done_hangup);
1997-10-19 20:56:41 +04:00
if (*st1 == 'q' && u.uhp < 1) {
1993-03-21 12:45:37 +03:00
st1 = "died";
killer = "quit while already on Charon's boat";
}
1997-10-19 20:56:41 +04:00
if (*st1 == 's')
killer = "starvation";
else if (*st1 == 'd' && st1[1] == 'r')
killer = "drowning";
else if (*st1 == 'p')
killer = "panic";
else if (*st1 == 't')
killer = "trickery";
else if (!strchr("bcd", *st1))
killer = st1;
1993-03-21 12:45:37 +03:00
paybill();
clearlocks();
1997-10-19 20:56:41 +04:00
if (flags.toplin == 1)
more();
if (strchr("bcds", *st1)) {
1993-03-21 12:45:37 +03:00
#ifdef WIZARD
1997-10-19 20:56:41 +04:00
if (!wizard)
#endif /* WIZARD */
savebones();
if (!flags.notombstone)
1993-03-21 12:45:37 +03:00
outrip();
}
1997-10-19 20:56:41 +04:00
if (*st1 == 'c')
killer = st1; /* after outrip() */
1993-03-21 12:45:37 +03:00
settty((char *) 0); /* does a clear_screen() */
1997-10-19 20:56:41 +04:00
if (!done_stopprint)
1993-03-21 12:45:37 +03:00
printf("Goodbye %s %s...\n\n", pl_character, plname);
1997-10-19 20:56:41 +04:00
{
long int tmp;
tmp = u.ugold - u.ugold0;
if (tmp < 0)
tmp = 0;
if (*st1 == 'd' || *st1 == 'b')
tmp -= tmp / 10;
u.urexp += tmp;
u.urexp += 50 * maxdlevel;
if (maxdlevel > 20)
u.urexp += 1000 * ((maxdlevel > 30) ? 10 : maxdlevel - 20);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (*st1 == 'e') {
struct monst *mtmp;
struct obj *otmp;
int i;
unsigned worthlessct = 0;
boolean has_amulet = FALSE;
1993-03-21 12:45:37 +03:00
killer = st1;
keepdogs();
mtmp = mydogs;
1997-10-19 20:56:41 +04:00
if (mtmp) {
if (!done_stopprint)
printf("You");
while (mtmp) {
if (!done_stopprint)
1993-03-21 12:45:37 +03:00
printf(" and %s", monnam(mtmp));
1997-10-19 20:56:41 +04:00
if (mtmp->mtame)
1993-03-21 12:45:37 +03:00
u.urexp += mtmp->mhp;
mtmp = mtmp->nmon;
}
1997-10-19 20:56:41 +04:00
if (!done_stopprint)
printf("\nescaped from the dungeon with %ld points,\n",
u.urexp);
} else if (!done_stopprint)
printf("You escaped from the dungeon with %ld points,\n",
u.urexp);
for (otmp = invent; otmp; otmp = otmp->nobj) {
if (otmp->olet == GEM_SYM) {
1993-03-21 12:45:37 +03:00
objects[otmp->otyp].oc_name_known = 1;
1997-10-19 20:56:41 +04:00
i = otmp->quan * objects[otmp->otyp].g_val;
if (i == 0) {
1993-03-21 12:45:37 +03:00
worthlessct += otmp->quan;
continue;
}
u.urexp += i;
1997-10-19 20:56:41 +04:00
if (!done_stopprint)
printf("\t%s (worth %d Zorkmids),\n",
doname(otmp), i);
} else if (otmp->olet == AMULET_SYM) {
1993-03-21 12:45:37 +03:00
otmp->known = 1;
i = (otmp->spe < 0) ? 2 : 5000;
u.urexp += i;
1997-10-19 20:56:41 +04:00
if (!done_stopprint)
printf("\t%s (worth %d Zorkmids),\n",
doname(otmp), i);
if (otmp->spe >= 0) {
1993-03-21 12:45:37 +03:00
has_amulet = TRUE;
killer = "escaped (with amulet)";
}
}
}
1997-10-19 20:56:41 +04:00
if (worthlessct)
if (!done_stopprint)
printf("\t%u worthless piece%s of coloured glass,\n",
worthlessct, plur(worthlessct));
if (has_amulet)
u.urexp *= 2;
} else if (!done_stopprint)
printf("You %s on dungeon level %d with %ld points,\n",
st1, dlevel, u.urexp);
if (!done_stopprint)
printf("and %ld piece%s of gold, after %ld move%s.\n",
u.ugold, plur(u.ugold), moves, plur(moves));
if (!done_stopprint)
printf("You were level %u with a maximum of %d hit points when you %s.\n",
u.ulevel, u.uhpmax, st1);
if (*st1 == 'e' && !done_stopprint) {
1993-03-21 12:45:37 +03:00
getret(); /* all those pieces of coloured glass ... */
cls();
}
#ifdef WIZARD
1997-10-19 20:56:41 +04:00
if (!wizard)
#endif /* WIZARD */
1993-03-21 12:45:37 +03:00
topten();
1997-10-19 20:56:41 +04:00
if (done_stopprint)
printf("\n\n");
1993-03-21 12:45:37 +03:00
exit(0);
}
#define newttentry() ((struct toptenentry *) alloc(sizeof(struct toptenentry)))
1993-03-21 12:45:37 +03:00
#define NAMSZ 8
#define DTHSZ 40
#define PERSMAX 1
#define POINTSMIN 1 /* must be > 0 */
#define ENTRYMAX 100 /* must be >= 10 */
#define PERS_IS_UID /* delete for PERSMAX per name; now per uid */
struct toptenentry {
struct toptenentry *tt_next;
1997-10-19 20:56:41 +04:00
long int points;
int level, maxlvl, hp, maxhp;
int uid;
char plchar;
char sex;
char name[NAMSZ + 1];
char death[DTHSZ + 1];
char date[7];/* yymmdd */
2009-08-12 11:28:40 +04:00
};
1993-03-21 12:45:37 +03:00
2009-08-12 11:28:40 +04:00
static struct toptenentry *tt_head;
static void
topten(void)
1997-10-19 20:56:41 +04:00
{
int uid = getuid();
int rank, rank0 = -1, rank1 = 0;
int occ_cnt = PERSMAX;
struct toptenentry *t0, *t1, *tprev;
const char *recfile = RECORD;
const char *reclock = "record_lock";
1997-10-19 20:56:41 +04:00
int sleepct = 300;
FILE *rfile;
int flg = 0;
1993-03-21 12:45:37 +03:00
#define HUP if(!done_hup)
1997-10-19 20:56:41 +04:00
while (link(recfile, reclock) == -1) {
HUP perror(reclock);
if (!sleepct--) {
HUP puts("I give up. Sorry.");
HUP puts("Perhaps there is an old record_lock around?");
1993-03-21 12:45:37 +03:00
return;
}
1997-10-19 20:56:41 +04:00
HUP printf("Waiting for access to record file. (%d)\n",
sleepct);
HUP(void) fflush(stdout);
1993-03-21 12:45:37 +03:00
sleep(1);
}
1997-10-19 20:56:41 +04:00
if (!(rfile = fopen(recfile, "r"))) {
HUP puts("Cannot open record file!");
1993-03-21 12:45:37 +03:00
goto unlock;
}
1997-10-19 20:56:41 +04:00
HUP(void) putchar('\n');
1993-03-21 12:45:37 +03:00
/* create a new 'topten' entry */
t0 = newttentry();
t0->level = dlevel;
t0->maxlvl = maxdlevel;
t0->hp = u.uhp;
t0->maxhp = u.uhpmax;
t0->points = u.urexp;
t0->plchar = pl_character[0];
t0->sex = (flags.female ? 'F' : 'M');
t0->uid = uid;
(void) strncpy(t0->name, plname, NAMSZ);
(t0->name)[NAMSZ] = 0;
(void) strncpy(t0->death, killer, DTHSZ);
(t0->death)[DTHSZ] = 0;
(void) strcpy(t0->date, getdatestr());
1993-03-21 12:45:37 +03:00
/* assure minimum number of points */
1997-10-19 20:56:41 +04:00
if (t0->points < POINTSMIN)
1993-03-21 12:45:37 +03:00
t0->points = 0;
t1 = tt_head = newttentry();
tprev = 0;
/* rank0: -1 undefined, 0 not_on_list, n n_th on list */
1997-10-19 20:56:41 +04:00
for (rank = 1;;) {
if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
t1->date, &t1->uid,
&t1->level, &t1->maxlvl,
&t1->hp, &t1->maxhp, &t1->points,
&t1->plchar, &t1->sex, t1->name, t1->death) != 11
|| t1->points < POINTSMIN)
1993-03-21 12:45:37 +03:00
t1->points = 0;
1997-10-19 20:56:41 +04:00
if (rank0 < 0 && t1->points < t0->points) {
rank0 = rank++;
if (tprev == 0)
tt_head = t0;
else
tprev->tt_next = t0;
t0->tt_next = t1;
occ_cnt--;
flg++; /* ask for a rewrite */
} else
tprev = t1;
if (t1->points == 0)
break;
if (
1993-03-21 12:45:37 +03:00
#ifdef PERS_IS_UID
1997-10-19 20:56:41 +04:00
t1->uid == t0->uid &&
1993-03-21 12:45:37 +03:00
#else
1997-10-19 20:56:41 +04:00
strncmp(t1->name, t0->name, NAMSZ) == 0 &&
#endif /* PERS_IS_UID */
t1->plchar == t0->plchar && --occ_cnt <= 0) {
if (rank0 < 0) {
rank0 = 0;
rank1 = rank;
HUP printf("You didn't beat your previous score of %ld points.\n\n",
t1->points);
}
if (occ_cnt < 0) {
flg++;
continue;
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (rank <= ENTRYMAX) {
t1 = t1->tt_next = newttentry();
rank++;
}
if (rank > ENTRYMAX) {
t1->points = 0;
break;
1993-03-21 12:45:37 +03:00
}
}
1997-10-19 20:56:41 +04:00
if (flg) { /* rewrite record file */
1993-03-21 12:45:37 +03:00
(void) fclose(rfile);
1997-10-19 20:56:41 +04:00
if (!(rfile = fopen(recfile, "w"))) {
HUP puts("Cannot write record file\n");
1993-03-21 12:45:37 +03:00
goto unlock;
}
1997-10-19 20:56:41 +04:00
if (!done_stopprint)
if (rank0 > 0) {
if (rank0 <= 10)
puts("You made the top ten list!\n");
else
printf("You reached the %d%s place on the top %d list.\n\n",
rank0, ordin(rank0), ENTRYMAX);
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (rank0 == 0)
rank0 = rank1;
if (rank0 <= 0)
rank0 = rank;
if (!done_stopprint)
outheader();
1993-03-21 12:45:37 +03:00
t1 = tt_head;
1997-10-19 20:56:41 +04:00
for (rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) {
if (flg)
fprintf(rfile, "%6s %d %d %d %d %d %ld %c%c %s,%s\n",
t1->date, t1->uid,
t1->level, t1->maxlvl,
t1->hp, t1->maxhp, t1->points,
t1->plchar, t1->sex, t1->name, t1->death);
if (done_stopprint)
continue;
2008-01-28 09:55:41 +03:00
if (rank > (int)flags.end_top &&
(rank < rank0 - (int)flags.end_around || rank > rank0 + (int)flags.end_around)
1997-10-19 20:56:41 +04:00
&& (!flags.end_own ||
1993-03-21 12:45:37 +03:00
#ifdef PERS_IS_UID
1997-10-19 20:56:41 +04:00
t1->uid != t0->uid))
1993-03-21 12:45:37 +03:00
#else
1997-10-19 20:56:41 +04:00
strncmp(t1->name, t0->name, NAMSZ)))
#endif /* PERS_IS_UID */
continue;
2008-01-28 09:55:41 +03:00
if (rank == rank0 - (int)flags.end_around &&
rank0 > (int)flags.end_top + (int)flags.end_around + 1 &&
1997-10-19 20:56:41 +04:00
!flags.end_own)
(void) putchar('\n');
if (rank != rank0)
(void) outentry(rank, t1, 0);
else if (!rank1)
(void) outentry(rank, t1, 1);
else {
int t0lth = outentry(0, t0, -1);
int t1lth = outentry(rank, t1, t0lth);
if (t1lth > t0lth)
t0lth = t1lth;
(void) outentry(0, t0, t0lth);
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (rank0 >= rank)
if (!done_stopprint)
(void) outentry(0, t0, 1);
1993-03-21 12:45:37 +03:00
(void) fclose(rfile);
2006-05-14 02:45:11 +04:00
free(t0);
1993-03-21 12:45:37 +03:00
unlock:
(void) unlink(reclock);
}
2009-08-12 11:28:40 +04:00
static void
outheader(void)
1997-10-19 20:56:41 +04:00
{
char linebuf[BUFSZ];
char *bp;
1993-03-21 12:45:37 +03:00
(void) strcpy(linebuf, "Number Points Name");
bp = eos(linebuf);
1997-10-19 20:56:41 +04:00
while (bp < linebuf + COLNO - 9)
*bp++ = ' ';
1993-03-21 12:45:37 +03:00
(void) strcpy(bp, "Hp [max]");
puts(linebuf);
}
/* so>0: standout line; so=0: ordinary line; so<0: no output, return length */
2009-08-12 11:28:40 +04:00
static int
outentry(int rank, struct toptenentry *t1, int so)
1997-10-19 20:56:41 +04:00
{
2008-01-28 09:55:41 +03:00
boolean quit = FALSE, gotkilled = FALSE, starv = FALSE;
1997-10-19 20:56:41 +04:00
char linebuf[BUFSZ];
size_t pos;
linebuf[0] = '\0';
pos = 0;
2008-01-28 09:55:41 +03:00
1997-10-19 20:56:41 +04:00
if (rank)
Snprintf(linebuf+pos, sizeof(linebuf)-pos, "%3d", rank);
1997-10-19 20:56:41 +04:00
else
Snprintf(linebuf+pos, sizeof(linebuf)-pos, " ");
pos = strlen(linebuf);
Snprintf(linebuf+pos, sizeof(linebuf)-pos, " %6ld %8s",
t1->points, t1->name);
pos = strlen(linebuf);
1997-10-19 20:56:41 +04:00
if (t1->plchar == 'X')
Snprintf(linebuf+pos, sizeof(linebuf)-pos, " ");
1997-10-19 20:56:41 +04:00
else
Snprintf(linebuf+pos, sizeof(linebuf)-pos, "-%c ", t1->plchar);
pos = strlen(linebuf);
1997-10-19 20:56:41 +04:00
if (!strncmp("escaped", t1->death, 7)) {
if (!strcmp(" (with amulet)", t1->death + 7))
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
"escaped the dungeon with amulet");
1997-10-19 20:56:41 +04:00
else
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
"escaped the dungeon [max level %d]",
1997-10-19 20:56:41 +04:00
t1->maxlvl);
pos = strlen(linebuf);
1993-03-21 12:45:37 +03:00
} else {
1997-10-19 20:56:41 +04:00
if (!strncmp(t1->death, "quit", 4)) {
quit = TRUE;
if (t1->maxhp < 3 * t1->hp && t1->maxlvl < 4)
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
"cravenly gave up");
1997-10-19 20:56:41 +04:00
else
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
"quit");
} else if (!strcmp(t1->death, "choked")) {
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
"choked on %s food",
1997-10-19 20:56:41 +04:00
(t1->sex == 'F') ? "her" : "his");
} else if (!strncmp(t1->death, "starv", 5)) {
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
"starved to death");
starv = TRUE;
} else {
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
"was killed");
gotkilled = TRUE;
}
pos = strlen(linebuf);
Snprintf(linebuf+pos, sizeof(linebuf)-pos, " on%s level %d",
2008-01-28 09:55:41 +03:00
(gotkilled || starv) ? "" : " dungeon", t1->level);
pos = strlen(linebuf);
1997-10-19 20:56:41 +04:00
if (t1->maxlvl != t1->level)
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
" [max %d]", t1->maxlvl);
pos = strlen(linebuf);
1997-10-19 20:56:41 +04:00
if (quit && t1->death[4])
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
"%s", t1->death + 4);
pos = strlen(linebuf);
1993-03-21 12:45:37 +03:00
}
if (gotkilled) {
Snprintf(linebuf+pos, sizeof(linebuf)-pos, " by %s%s",
1997-10-19 20:56:41 +04:00
(!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4))
? "" :
strchr(vowels, *t1->death) ? "an " : "a ",
t1->death);
pos = strlen(linebuf);
}
strlcat(linebuf, ".", sizeof(linebuf));
pos = strlen(linebuf);
1997-10-19 20:56:41 +04:00
if (t1->maxhp) {
char hpbuf[10];
unsigned hppos;
strlcpy(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-", sizeof(hpbuf));
1997-10-19 20:56:41 +04:00
hppos = COLNO - 7 - strlen(hpbuf);
if (pos <= hppos) {
while (pos < hppos)
linebuf[pos++] = ' ';
(void) strlcpy(linebuf+pos, hpbuf, sizeof(linebuf)-pos);
pos = strlen(linebuf);
Snprintf(linebuf+pos, sizeof(linebuf)-pos,
" [%d]", t1->maxhp);
pos = strlen(linebuf);
1997-10-19 20:56:41 +04:00
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (so == 0)
puts(linebuf);
else if (so > 0) {
if (so >= COLNO)
so = COLNO - 1;
while (pos < (unsigned)so)
linebuf[pos++] = ' ';
linebuf[pos] = '\0';
1997-10-19 20:56:41 +04:00
standoutbeg();
fputs(linebuf, stdout);
standoutend();
(void) putchar('\n');
1993-03-21 12:45:37 +03:00
}
return /*(strlen(linebuf))*/ pos;
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static char *
itoa(int a)
1997-10-19 20:56:41 +04:00
{
static char buf[12];
Snprintf(buf, sizeof(buf), "%d", a);
1997-10-19 20:56:41 +04:00
return (buf);
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static const char *
ordin(int n)
1997-10-19 20:56:41 +04:00
{
2008-01-28 09:55:41 +03:00
int dg = n % 10;
return ((dg == 0 || dg > 3 || n / 10 == 1) ? "th" : (dg == 1) ? "st" :
(dg == 2) ? "nd" : "rd");
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
clearlocks(void)
1997-10-19 20:56:41 +04:00
{
int x;
(void) signal(SIGHUP, SIG_IGN);
for (x = maxdlevel; x >= 0; x--) {
1993-03-21 12:45:37 +03:00
glo(x);
(void) unlink(lock); /* not all levels need be present */
}
}
#ifdef NOSAVEONHANGUP
1997-10-19 20:56:41 +04:00
/*ARGSUSED*/
void
hang_up(int n __unused)
1993-03-21 12:45:37 +03:00
{
(void) signal(SIGINT, SIG_IGN);
clearlocks();
exit(1);
}
1997-10-19 20:56:41 +04:00
#endif /* NOSAVEONHANGUP */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
char *
eos(char *s)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
while (*s)
s++;
return (s);
1993-03-21 12:45:37 +03:00
}
/* it is the callers responsibility to check that there is room for c */
1997-10-19 20:56:41 +04:00
void
charcat(char *s, int c)
1997-10-19 20:56:41 +04:00
{
while (*s)
s++;
1993-03-21 12:45:37 +03:00
*s++ = c;
*s = 0;
}
/*
* Called with args from main if argc >= 0. In this case, list scores as
* requested. Otherwise, find scores for the current player (and list them
* if argc == -1).
*/
1997-10-19 20:56:41 +04:00
void
prscore(int argc, char **argv)
1997-10-19 20:56:41 +04:00
{
char **players = NULL;
int playerct;
int rank;
struct toptenentry *t1, *t2;
const char *recfile = RECORD;
1997-10-19 20:56:41 +04:00
FILE *rfile;
int flg = 0;
int i;
1993-03-21 12:45:37 +03:00
#ifdef nonsense
1997-10-19 20:56:41 +04:00
long total_score = 0L;
char totchars[10];
int totcharct = 0;
#endif /* nonsense */
int outflg = (argc >= -1);
1993-03-21 12:45:37 +03:00
#ifdef PERS_IS_UID
1997-10-19 20:56:41 +04:00
int uid = -1;
1993-03-21 12:45:37 +03:00
#else
1997-10-19 20:56:41 +04:00
char *player0;
#endif /* PERS_IS_UID */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (!(rfile = fopen(recfile, "r"))) {
1993-03-21 12:45:37 +03:00
puts("Cannot open record file!");
return;
}
1997-10-19 20:56:41 +04:00
if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
if (!argv[1][2]) {
1993-03-21 12:45:37 +03:00
argc--;
argv++;
1997-10-19 20:56:41 +04:00
} else if (!argv[1][3] && strchr("CFKSTWX", argv[1][2])) {
1993-03-21 12:45:37 +03:00
argv[1]++;
argv[1][0] = '-';
1997-10-19 20:56:41 +04:00
} else
argv[1] += 2;
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (argc <= 1) {
1993-03-21 12:45:37 +03:00
#ifdef PERS_IS_UID
uid = getuid();
playerct = 0;
#else
player0 = plname;
1997-10-19 20:56:41 +04:00
if (!*player0)
1993-03-21 12:45:37 +03:00
player0 = "hackplayer";
playerct = 1;
players = &player0;
1997-10-19 20:56:41 +04:00
#endif /* PERS_IS_UID */
1993-03-21 12:45:37 +03:00
} else {
playerct = --argc;
players = ++argv;
}
1997-10-19 20:56:41 +04:00
if (outflg)
putchar('\n');
1993-03-21 12:45:37 +03:00
t1 = tt_head = newttentry();
1997-10-19 20:56:41 +04:00
for (rank = 1;; rank++) {
if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
t1->date, &t1->uid,
&t1->level, &t1->maxlvl,
&t1->hp, &t1->maxhp, &t1->points,
&t1->plchar, &t1->sex, t1->name, t1->death) != 11)
1993-03-21 12:45:37 +03:00
t1->points = 0;
1997-10-19 20:56:41 +04:00
if (t1->points == 0)
break;
1993-03-21 12:45:37 +03:00
#ifdef PERS_IS_UID
1997-10-19 20:56:41 +04:00
if (!playerct && t1->uid == uid)
1993-03-21 12:45:37 +03:00
flg++;
1997-10-19 20:56:41 +04:00
else
#endif /* PERS_IS_UID */
for (i = 0; i < playerct; i++) {
if (strcmp(players[i], "all") == 0 ||
strncmp(t1->name, players[i], NAMSZ) == 0 ||
(players[i][0] == '-' &&
players[i][1] == t1->plchar &&
players[i][2] == 0) ||
(digit(players[i][0]) && rank <= atoi(players[i])))
flg++;
}
t1 = t1->tt_next = newttentry();
1993-03-21 12:45:37 +03:00
}
(void) fclose(rfile);
1997-10-19 20:56:41 +04:00
if (!flg) {
if (outflg) {
printf("Cannot find any entries for ");
if (playerct < 1)
printf("you.\n");
else {
if (playerct > 1)
printf("any of ");
for (i = 0; i < playerct; i++)
printf("%s%s", players[i], (i < playerct - 1) ? ", " : ".\n");
printf("Call is: %s -s [playernames]\n", hname);
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
return;
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (outflg)
outheader();
1993-03-21 12:45:37 +03:00
t1 = tt_head;
1997-10-19 20:56:41 +04:00
for (rank = 1; t1->points != 0; rank++, t1 = t2) {
1993-03-21 12:45:37 +03:00
t2 = t1->tt_next;
#ifdef PERS_IS_UID
1997-10-19 20:56:41 +04:00
if (!playerct && t1->uid == uid)
1993-03-21 12:45:37 +03:00
goto outwithit;
else
1997-10-19 20:56:41 +04:00
#endif /* PERS_IS_UID */
for (i = 0; i < playerct; i++) {
if (strcmp(players[i], "all") == 0 ||
strncmp(t1->name, players[i], NAMSZ) == 0 ||
(players[i][0] == '-' &&
players[i][1] == t1->plchar &&
players[i][2] == 0) ||
(digit(players[i][0]) && rank <= atoi(players[i]))) {
1993-03-21 12:45:37 +03:00
outwithit:
1997-10-19 20:56:41 +04:00
if (outflg)
(void) outentry(rank, t1, 0);
1993-03-21 12:45:37 +03:00
#ifdef nonsense
1997-10-19 20:56:41 +04:00
total_score += t1->points;
if (totcharct < sizeof(totchars) - 1)
totchars[totcharct++] = t1->plchar;
#endif /* nonsense */
break;
}
1993-03-21 12:45:37 +03:00
}
2011-08-07 00:29:37 +04:00
free(t1);
1993-03-21 12:45:37 +03:00
}
#ifdef nonsense
totchars[totcharct] = 0;
1997-10-19 20:56:41 +04:00
/*
* We would like to determine whether he is experienced. However, the
* information collected here only tells about the scores/roles that
* got into the topten (top 100?). We should maintain a .hacklog or
* something in his home directory.
*/
1993-03-21 12:45:37 +03:00
flags.beginner = (total_score < 6000);
1997-10-19 20:56:41 +04:00
for (i = 0; i < 6; i++)
if (!strchr(totchars, "CFKSTWX"[i])) {
flags.beginner = 1;
if (!pl_character[0])
pl_character[0] = "CFKSTWX"[i];
break;
}
#endif /* nonsense */
1993-03-21 12:45:37 +03:00
}