hack.end.c revision 1.11 1 1.11 dholland /* $NetBSD: hack.end.c,v 1.11 2009/06/07 18:30:39 dholland Exp $ */
2 1.4 christos
3 1.2 mycroft /*
4 1.6 jsm * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 1.6 jsm * Amsterdam
6 1.6 jsm * All rights reserved.
7 1.6 jsm *
8 1.6 jsm * Redistribution and use in source and binary forms, with or without
9 1.6 jsm * modification, are permitted provided that the following conditions are
10 1.6 jsm * met:
11 1.6 jsm *
12 1.6 jsm * - Redistributions of source code must retain the above copyright notice,
13 1.6 jsm * this list of conditions and the following disclaimer.
14 1.6 jsm *
15 1.6 jsm * - Redistributions in binary form must reproduce the above copyright
16 1.6 jsm * notice, this list of conditions and the following disclaimer in the
17 1.6 jsm * documentation and/or other materials provided with the distribution.
18 1.6 jsm *
19 1.6 jsm * - Neither the name of the Stichting Centrum voor Wiskunde en
20 1.6 jsm * Informatica, nor the names of its contributors may be used to endorse or
21 1.6 jsm * promote products derived from this software without specific prior
22 1.6 jsm * written permission.
23 1.6 jsm *
24 1.6 jsm * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 1.6 jsm * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.6 jsm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 1.6 jsm * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 1.6 jsm * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 1.6 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 1.6 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 1.6 jsm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 1.6 jsm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 1.6 jsm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 1.6 jsm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.6 jsm */
36 1.6 jsm
37 1.6 jsm /*
38 1.6 jsm * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
39 1.6 jsm * All rights reserved.
40 1.6 jsm *
41 1.6 jsm * Redistribution and use in source and binary forms, with or without
42 1.6 jsm * modification, are permitted provided that the following conditions
43 1.6 jsm * are met:
44 1.6 jsm * 1. Redistributions of source code must retain the above copyright
45 1.6 jsm * notice, this list of conditions and the following disclaimer.
46 1.6 jsm * 2. Redistributions in binary form must reproduce the above copyright
47 1.6 jsm * notice, this list of conditions and the following disclaimer in the
48 1.6 jsm * documentation and/or other materials provided with the distribution.
49 1.6 jsm * 3. The name of the author may not be used to endorse or promote products
50 1.6 jsm * derived from this software without specific prior written permission.
51 1.6 jsm *
52 1.6 jsm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 1.6 jsm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 1.6 jsm * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 1.6 jsm * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 1.6 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 1.6 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 1.6 jsm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 1.6 jsm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 1.6 jsm * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 1.6 jsm * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 1.2 mycroft */
63 1.2 mycroft
64 1.4 christos #include <sys/cdefs.h>
65 1.2 mycroft #ifndef lint
66 1.11 dholland __RCSID("$NetBSD: hack.end.c,v 1.11 2009/06/07 18:30:39 dholland Exp $");
67 1.4 christos #endif /* not lint */
68 1.1 cgd
69 1.4 christos #include <signal.h>
70 1.4 christos #include <unistd.h>
71 1.4 christos #include <stdlib.h>
72 1.1 cgd #include "hack.h"
73 1.4 christos #include "extern.h"
74 1.1 cgd #define Sprintf (void) sprintf
75 1.1 cgd
76 1.4 christos xchar maxdlevel = 1;
77 1.4 christos
78 1.4 christos int
79 1.11 dholland dodone(void)
80 1.4 christos {
81 1.4 christos done1(0);
82 1.4 christos return 0;
83 1.4 christos }
84 1.4 christos
85 1.1 cgd
86 1.4 christos /*ARGSUSED*/
87 1.1 cgd void
88 1.11 dholland done1(int n __unused)
89 1.1 cgd {
90 1.4 christos (void) signal(SIGINT, SIG_IGN);
91 1.1 cgd pline("Really quit?");
92 1.4 christos if (readchar() != 'y') {
93 1.4 christos (void) signal(SIGINT, done1);
94 1.1 cgd clrlin();
95 1.1 cgd (void) fflush(stdout);
96 1.4 christos if (multi > 0)
97 1.4 christos nomul(0);
98 1.1 cgd return;
99 1.1 cgd }
100 1.1 cgd done("quit");
101 1.1 cgd /* NOTREACHED */
102 1.1 cgd }
103 1.1 cgd
104 1.4 christos int done_stopprint;
105 1.4 christos int done_hup;
106 1.1 cgd
107 1.4 christos /*ARGSUSED*/
108 1.1 cgd void
109 1.11 dholland done_intr(int n __unused)
110 1.4 christos {
111 1.1 cgd done_stopprint++;
112 1.1 cgd (void) signal(SIGINT, SIG_IGN);
113 1.1 cgd (void) signal(SIGQUIT, SIG_IGN);
114 1.1 cgd }
115 1.1 cgd
116 1.1 cgd void
117 1.11 dholland done_hangup(int n)
118 1.4 christos {
119 1.1 cgd done_hup++;
120 1.1 cgd (void) signal(SIGHUP, SIG_IGN);
121 1.4 christos done_intr(n);
122 1.1 cgd }
123 1.1 cgd
124 1.4 christos void
125 1.11 dholland done_in_by(struct monst *mtmp)
126 1.4 christos {
127 1.4 christos static char buf[BUFSZ];
128 1.1 cgd pline("You die ...");
129 1.4 christos if (mtmp->data->mlet == ' ') {
130 1.1 cgd Sprintf(buf, "the ghost of %s", (char *) mtmp->mextra);
131 1.1 cgd killer = buf;
132 1.4 christos } else if (mtmp->mnamelth) {
133 1.1 cgd Sprintf(buf, "%s called %s",
134 1.1 cgd mtmp->data->mname, NAME(mtmp));
135 1.1 cgd killer = buf;
136 1.4 christos } else if (mtmp->minvis) {
137 1.1 cgd Sprintf(buf, "invisible %s", mtmp->data->mname);
138 1.1 cgd killer = buf;
139 1.4 christos } else
140 1.4 christos killer = mtmp->data->mname;
141 1.1 cgd done("died");
142 1.1 cgd }
143 1.1 cgd
144 1.4 christos /*
145 1.4 christos * called with arg "died", "drowned", "escaped", "quit", "choked",
146 1.4 christos * "panicked", "burned", "starved" or "tricked"
147 1.4 christos */
148 1.1 cgd /* Be careful not to call panic from here! */
149 1.4 christos void
150 1.11 dholland done(const char *st1)
151 1.1 cgd {
152 1.1 cgd
153 1.1 cgd #ifdef WIZARD
154 1.4 christos if (wizard && *st1 == 'd') {
155 1.1 cgd u.uswldtim = 0;
156 1.4 christos if (u.uhpmax < 0)
157 1.4 christos u.uhpmax = 100; /* arbitrary */
158 1.1 cgd u.uhp = u.uhpmax;
159 1.1 cgd pline("For some reason you are still alive.");
160 1.1 cgd flags.move = 0;
161 1.4 christos if (multi > 0)
162 1.4 christos multi = 0;
163 1.4 christos else
164 1.4 christos multi = -1;
165 1.1 cgd flags.botl = 1;
166 1.1 cgd return;
167 1.1 cgd }
168 1.4 christos #endif /* WIZARD */
169 1.1 cgd (void) signal(SIGINT, done_intr);
170 1.1 cgd (void) signal(SIGQUIT, done_intr);
171 1.1 cgd (void) signal(SIGHUP, done_hangup);
172 1.4 christos if (*st1 == 'q' && u.uhp < 1) {
173 1.1 cgd st1 = "died";
174 1.1 cgd killer = "quit while already on Charon's boat";
175 1.1 cgd }
176 1.4 christos if (*st1 == 's')
177 1.4 christos killer = "starvation";
178 1.4 christos else if (*st1 == 'd' && st1[1] == 'r')
179 1.4 christos killer = "drowning";
180 1.4 christos else if (*st1 == 'p')
181 1.4 christos killer = "panic";
182 1.4 christos else if (*st1 == 't')
183 1.4 christos killer = "trickery";
184 1.4 christos else if (!strchr("bcd", *st1))
185 1.4 christos killer = st1;
186 1.1 cgd paybill();
187 1.1 cgd clearlocks();
188 1.4 christos if (flags.toplin == 1)
189 1.4 christos more();
190 1.4 christos if (strchr("bcds", *st1)) {
191 1.1 cgd #ifdef WIZARD
192 1.4 christos if (!wizard)
193 1.4 christos #endif /* WIZARD */
194 1.4 christos savebones();
195 1.4 christos if (!flags.notombstone)
196 1.1 cgd outrip();
197 1.1 cgd }
198 1.4 christos if (*st1 == 'c')
199 1.4 christos killer = st1; /* after outrip() */
200 1.1 cgd settty((char *) 0); /* does a clear_screen() */
201 1.4 christos if (!done_stopprint)
202 1.1 cgd printf("Goodbye %s %s...\n\n", pl_character, plname);
203 1.4 christos {
204 1.4 christos long int tmp;
205 1.4 christos tmp = u.ugold - u.ugold0;
206 1.4 christos if (tmp < 0)
207 1.4 christos tmp = 0;
208 1.4 christos if (*st1 == 'd' || *st1 == 'b')
209 1.4 christos tmp -= tmp / 10;
210 1.4 christos u.urexp += tmp;
211 1.4 christos u.urexp += 50 * maxdlevel;
212 1.4 christos if (maxdlevel > 20)
213 1.4 christos u.urexp += 1000 * ((maxdlevel > 30) ? 10 : maxdlevel - 20);
214 1.4 christos }
215 1.4 christos if (*st1 == 'e') {
216 1.4 christos struct monst *mtmp;
217 1.4 christos struct obj *otmp;
218 1.4 christos int i;
219 1.4 christos unsigned worthlessct = 0;
220 1.4 christos boolean has_amulet = FALSE;
221 1.1 cgd
222 1.1 cgd killer = st1;
223 1.1 cgd keepdogs();
224 1.1 cgd mtmp = mydogs;
225 1.4 christos if (mtmp) {
226 1.4 christos if (!done_stopprint)
227 1.4 christos printf("You");
228 1.4 christos while (mtmp) {
229 1.4 christos if (!done_stopprint)
230 1.1 cgd printf(" and %s", monnam(mtmp));
231 1.4 christos if (mtmp->mtame)
232 1.1 cgd u.urexp += mtmp->mhp;
233 1.1 cgd mtmp = mtmp->nmon;
234 1.1 cgd }
235 1.4 christos if (!done_stopprint)
236 1.4 christos printf("\nescaped from the dungeon with %ld points,\n",
237 1.4 christos u.urexp);
238 1.4 christos } else if (!done_stopprint)
239 1.4 christos printf("You escaped from the dungeon with %ld points,\n",
240 1.4 christos u.urexp);
241 1.4 christos for (otmp = invent; otmp; otmp = otmp->nobj) {
242 1.4 christos if (otmp->olet == GEM_SYM) {
243 1.1 cgd objects[otmp->otyp].oc_name_known = 1;
244 1.4 christos i = otmp->quan * objects[otmp->otyp].g_val;
245 1.4 christos if (i == 0) {
246 1.1 cgd worthlessct += otmp->quan;
247 1.1 cgd continue;
248 1.1 cgd }
249 1.1 cgd u.urexp += i;
250 1.4 christos if (!done_stopprint)
251 1.4 christos printf("\t%s (worth %d Zorkmids),\n",
252 1.4 christos doname(otmp), i);
253 1.4 christos } else if (otmp->olet == AMULET_SYM) {
254 1.1 cgd otmp->known = 1;
255 1.1 cgd i = (otmp->spe < 0) ? 2 : 5000;
256 1.1 cgd u.urexp += i;
257 1.4 christos if (!done_stopprint)
258 1.4 christos printf("\t%s (worth %d Zorkmids),\n",
259 1.4 christos doname(otmp), i);
260 1.4 christos if (otmp->spe >= 0) {
261 1.1 cgd has_amulet = TRUE;
262 1.1 cgd killer = "escaped (with amulet)";
263 1.1 cgd }
264 1.1 cgd }
265 1.1 cgd }
266 1.4 christos if (worthlessct)
267 1.4 christos if (!done_stopprint)
268 1.4 christos printf("\t%u worthless piece%s of coloured glass,\n",
269 1.4 christos worthlessct, plur(worthlessct));
270 1.4 christos if (has_amulet)
271 1.4 christos u.urexp *= 2;
272 1.4 christos } else if (!done_stopprint)
273 1.4 christos printf("You %s on dungeon level %d with %ld points,\n",
274 1.4 christos st1, dlevel, u.urexp);
275 1.4 christos if (!done_stopprint)
276 1.4 christos printf("and %ld piece%s of gold, after %ld move%s.\n",
277 1.4 christos u.ugold, plur(u.ugold), moves, plur(moves));
278 1.4 christos if (!done_stopprint)
279 1.4 christos printf("You were level %u with a maximum of %d hit points when you %s.\n",
280 1.4 christos u.ulevel, u.uhpmax, st1);
281 1.4 christos if (*st1 == 'e' && !done_stopprint) {
282 1.1 cgd getret(); /* all those pieces of coloured glass ... */
283 1.1 cgd cls();
284 1.1 cgd }
285 1.1 cgd #ifdef WIZARD
286 1.4 christos if (!wizard)
287 1.4 christos #endif /* WIZARD */
288 1.1 cgd topten();
289 1.4 christos if (done_stopprint)
290 1.4 christos printf("\n\n");
291 1.1 cgd exit(0);
292 1.1 cgd }
293 1.1 cgd
294 1.1 cgd #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
295 1.1 cgd #define NAMSZ 8
296 1.1 cgd #define DTHSZ 40
297 1.1 cgd #define PERSMAX 1
298 1.1 cgd #define POINTSMIN 1 /* must be > 0 */
299 1.1 cgd #define ENTRYMAX 100 /* must be >= 10 */
300 1.1 cgd #define PERS_IS_UID /* delete for PERSMAX per name; now per uid */
301 1.1 cgd struct toptenentry {
302 1.1 cgd struct toptenentry *tt_next;
303 1.4 christos long int points;
304 1.4 christos int level, maxlvl, hp, maxhp;
305 1.4 christos int uid;
306 1.4 christos char plchar;
307 1.4 christos char sex;
308 1.4 christos char name[NAMSZ + 1];
309 1.4 christos char death[DTHSZ + 1];
310 1.4 christos char date[7];/* yymmdd */
311 1.4 christos } *tt_head;
312 1.4 christos
313 1.4 christos void
314 1.11 dholland topten(void)
315 1.4 christos {
316 1.4 christos int uid = getuid();
317 1.4 christos int rank, rank0 = -1, rank1 = 0;
318 1.4 christos int occ_cnt = PERSMAX;
319 1.4 christos struct toptenentry *t0, *t1, *tprev;
320 1.5 jsm const char *recfile = RECORD;
321 1.5 jsm const char *reclock = "record_lock";
322 1.4 christos int sleepct = 300;
323 1.4 christos FILE *rfile;
324 1.4 christos int flg = 0;
325 1.1 cgd #define HUP if(!done_hup)
326 1.4 christos while (link(recfile, reclock) == -1) {
327 1.4 christos HUP perror(reclock);
328 1.4 christos if (!sleepct--) {
329 1.4 christos HUP puts("I give up. Sorry.");
330 1.4 christos HUP puts("Perhaps there is an old record_lock around?");
331 1.1 cgd return;
332 1.1 cgd }
333 1.4 christos HUP printf("Waiting for access to record file. (%d)\n",
334 1.4 christos sleepct);
335 1.4 christos HUP(void) fflush(stdout);
336 1.1 cgd sleep(1);
337 1.1 cgd }
338 1.4 christos if (!(rfile = fopen(recfile, "r"))) {
339 1.4 christos HUP puts("Cannot open record file!");
340 1.1 cgd goto unlock;
341 1.1 cgd }
342 1.4 christos HUP(void) putchar('\n');
343 1.1 cgd
344 1.1 cgd /* create a new 'topten' entry */
345 1.1 cgd t0 = newttentry();
346 1.1 cgd t0->level = dlevel;
347 1.1 cgd t0->maxlvl = maxdlevel;
348 1.1 cgd t0->hp = u.uhp;
349 1.1 cgd t0->maxhp = u.uhpmax;
350 1.1 cgd t0->points = u.urexp;
351 1.1 cgd t0->plchar = pl_character[0];
352 1.1 cgd t0->sex = (flags.female ? 'F' : 'M');
353 1.1 cgd t0->uid = uid;
354 1.1 cgd (void) strncpy(t0->name, plname, NAMSZ);
355 1.1 cgd (t0->name)[NAMSZ] = 0;
356 1.1 cgd (void) strncpy(t0->death, killer, DTHSZ);
357 1.1 cgd (t0->death)[DTHSZ] = 0;
358 1.10 ginsbach (void) strcpy(t0->date, getdatestr());
359 1.1 cgd
360 1.1 cgd /* assure minimum number of points */
361 1.4 christos if (t0->points < POINTSMIN)
362 1.1 cgd t0->points = 0;
363 1.1 cgd
364 1.1 cgd t1 = tt_head = newttentry();
365 1.1 cgd tprev = 0;
366 1.1 cgd /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
367 1.4 christos for (rank = 1;;) {
368 1.4 christos if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
369 1.4 christos t1->date, &t1->uid,
370 1.4 christos &t1->level, &t1->maxlvl,
371 1.4 christos &t1->hp, &t1->maxhp, &t1->points,
372 1.4 christos &t1->plchar, &t1->sex, t1->name, t1->death) != 11
373 1.4 christos || t1->points < POINTSMIN)
374 1.1 cgd t1->points = 0;
375 1.4 christos if (rank0 < 0 && t1->points < t0->points) {
376 1.4 christos rank0 = rank++;
377 1.4 christos if (tprev == 0)
378 1.4 christos tt_head = t0;
379 1.4 christos else
380 1.4 christos tprev->tt_next = t0;
381 1.4 christos t0->tt_next = t1;
382 1.4 christos occ_cnt--;
383 1.4 christos flg++; /* ask for a rewrite */
384 1.4 christos } else
385 1.4 christos tprev = t1;
386 1.4 christos if (t1->points == 0)
387 1.4 christos break;
388 1.4 christos if (
389 1.1 cgd #ifdef PERS_IS_UID
390 1.4 christos t1->uid == t0->uid &&
391 1.1 cgd #else
392 1.4 christos strncmp(t1->name, t0->name, NAMSZ) == 0 &&
393 1.4 christos #endif /* PERS_IS_UID */
394 1.4 christos t1->plchar == t0->plchar && --occ_cnt <= 0) {
395 1.4 christos if (rank0 < 0) {
396 1.4 christos rank0 = 0;
397 1.4 christos rank1 = rank;
398 1.4 christos HUP printf("You didn't beat your previous score of %ld points.\n\n",
399 1.4 christos t1->points);
400 1.4 christos }
401 1.4 christos if (occ_cnt < 0) {
402 1.4 christos flg++;
403 1.4 christos continue;
404 1.4 christos }
405 1.1 cgd }
406 1.4 christos if (rank <= ENTRYMAX) {
407 1.4 christos t1 = t1->tt_next = newttentry();
408 1.4 christos rank++;
409 1.4 christos }
410 1.4 christos if (rank > ENTRYMAX) {
411 1.4 christos t1->points = 0;
412 1.4 christos break;
413 1.1 cgd }
414 1.1 cgd }
415 1.4 christos if (flg) { /* rewrite record file */
416 1.1 cgd (void) fclose(rfile);
417 1.4 christos if (!(rfile = fopen(recfile, "w"))) {
418 1.4 christos HUP puts("Cannot write record file\n");
419 1.1 cgd goto unlock;
420 1.1 cgd }
421 1.4 christos if (!done_stopprint)
422 1.4 christos if (rank0 > 0) {
423 1.4 christos if (rank0 <= 10)
424 1.4 christos puts("You made the top ten list!\n");
425 1.4 christos else
426 1.4 christos printf("You reached the %d%s place on the top %d list.\n\n",
427 1.4 christos rank0, ordin(rank0), ENTRYMAX);
428 1.4 christos }
429 1.1 cgd }
430 1.4 christos if (rank0 == 0)
431 1.4 christos rank0 = rank1;
432 1.4 christos if (rank0 <= 0)
433 1.4 christos rank0 = rank;
434 1.4 christos if (!done_stopprint)
435 1.4 christos outheader();
436 1.1 cgd t1 = tt_head;
437 1.4 christos for (rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) {
438 1.4 christos if (flg)
439 1.4 christos fprintf(rfile, "%6s %d %d %d %d %d %ld %c%c %s,%s\n",
440 1.4 christos t1->date, t1->uid,
441 1.4 christos t1->level, t1->maxlvl,
442 1.4 christos t1->hp, t1->maxhp, t1->points,
443 1.4 christos t1->plchar, t1->sex, t1->name, t1->death);
444 1.4 christos if (done_stopprint)
445 1.4 christos continue;
446 1.9 dholland if (rank > (int)flags.end_top &&
447 1.9 dholland (rank < rank0 - (int)flags.end_around || rank > rank0 + (int)flags.end_around)
448 1.4 christos && (!flags.end_own ||
449 1.1 cgd #ifdef PERS_IS_UID
450 1.4 christos t1->uid != t0->uid))
451 1.1 cgd #else
452 1.4 christos strncmp(t1->name, t0->name, NAMSZ)))
453 1.4 christos #endif /* PERS_IS_UID */
454 1.4 christos continue;
455 1.9 dholland if (rank == rank0 - (int)flags.end_around &&
456 1.9 dholland rank0 > (int)flags.end_top + (int)flags.end_around + 1 &&
457 1.4 christos !flags.end_own)
458 1.4 christos (void) putchar('\n');
459 1.4 christos if (rank != rank0)
460 1.4 christos (void) outentry(rank, t1, 0);
461 1.4 christos else if (!rank1)
462 1.4 christos (void) outentry(rank, t1, 1);
463 1.4 christos else {
464 1.4 christos int t0lth = outentry(0, t0, -1);
465 1.4 christos int t1lth = outentry(rank, t1, t0lth);
466 1.4 christos if (t1lth > t0lth)
467 1.4 christos t0lth = t1lth;
468 1.4 christos (void) outentry(0, t0, t0lth);
469 1.4 christos }
470 1.1 cgd }
471 1.4 christos if (rank0 >= rank)
472 1.4 christos if (!done_stopprint)
473 1.4 christos (void) outentry(0, t0, 1);
474 1.1 cgd (void) fclose(rfile);
475 1.7 christos free(t0);
476 1.1 cgd unlock:
477 1.1 cgd (void) unlink(reclock);
478 1.1 cgd }
479 1.1 cgd
480 1.4 christos void
481 1.11 dholland outheader(void)
482 1.4 christos {
483 1.4 christos char linebuf[BUFSZ];
484 1.4 christos char *bp;
485 1.1 cgd (void) strcpy(linebuf, "Number Points Name");
486 1.1 cgd bp = eos(linebuf);
487 1.4 christos while (bp < linebuf + COLNO - 9)
488 1.4 christos *bp++ = ' ';
489 1.1 cgd (void) strcpy(bp, "Hp [max]");
490 1.1 cgd puts(linebuf);
491 1.1 cgd }
492 1.1 cgd
493 1.1 cgd /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
494 1.1 cgd int
495 1.5 jsm outentry(int rank, struct toptenentry *t1, int so)
496 1.4 christos {
497 1.9 dholland boolean quit = FALSE, gotkilled = FALSE, starv = FALSE;
498 1.4 christos char linebuf[BUFSZ];
499 1.9 dholland
500 1.1 cgd linebuf[0] = 0;
501 1.4 christos if (rank)
502 1.4 christos Sprintf(eos(linebuf), "%3d", rank);
503 1.4 christos else
504 1.4 christos Sprintf(eos(linebuf), " ");
505 1.1 cgd Sprintf(eos(linebuf), " %6ld %8s", t1->points, t1->name);
506 1.4 christos if (t1->plchar == 'X')
507 1.4 christos Sprintf(eos(linebuf), " ");
508 1.4 christos else
509 1.4 christos Sprintf(eos(linebuf), "-%c ", t1->plchar);
510 1.4 christos if (!strncmp("escaped", t1->death, 7)) {
511 1.4 christos if (!strcmp(" (with amulet)", t1->death + 7))
512 1.4 christos Sprintf(eos(linebuf), "escaped the dungeon with amulet");
513 1.4 christos else
514 1.4 christos Sprintf(eos(linebuf), "escaped the dungeon [max level %d]",
515 1.4 christos t1->maxlvl);
516 1.1 cgd } else {
517 1.4 christos if (!strncmp(t1->death, "quit", 4)) {
518 1.4 christos quit = TRUE;
519 1.4 christos if (t1->maxhp < 3 * t1->hp && t1->maxlvl < 4)
520 1.4 christos Sprintf(eos(linebuf), "cravenly gave up");
521 1.4 christos else
522 1.4 christos Sprintf(eos(linebuf), "quit");
523 1.4 christos } else if (!strcmp(t1->death, "choked"))
524 1.4 christos Sprintf(eos(linebuf), "choked on %s food",
525 1.4 christos (t1->sex == 'F') ? "her" : "his");
526 1.4 christos else if (!strncmp(t1->death, "starv", 5))
527 1.4 christos Sprintf(eos(linebuf), "starved to death"), starv = TRUE;
528 1.4 christos else
529 1.9 dholland Sprintf(eos(linebuf), "was killed"), gotkilled = TRUE;
530 1.4 christos Sprintf(eos(linebuf), " on%s level %d",
531 1.9 dholland (gotkilled || starv) ? "" : " dungeon", t1->level);
532 1.4 christos if (t1->maxlvl != t1->level)
533 1.4 christos Sprintf(eos(linebuf), " [max %d]", t1->maxlvl);
534 1.4 christos if (quit && t1->death[4])
535 1.4 christos Sprintf(eos(linebuf), t1->death + 4);
536 1.4 christos }
537 1.9 dholland if (gotkilled)
538 1.4 christos Sprintf(eos(linebuf), " by %s%s",
539 1.4 christos (!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4))
540 1.4 christos ? "" :
541 1.4 christos strchr(vowels, *t1->death) ? "an " : "a ",
542 1.4 christos t1->death);
543 1.1 cgd Sprintf(eos(linebuf), ".");
544 1.4 christos if (t1->maxhp) {
545 1.4 christos char *bp = eos(linebuf);
546 1.4 christos char hpbuf[10];
547 1.4 christos int hppos;
548 1.4 christos Sprintf(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-");
549 1.4 christos hppos = COLNO - 7 - strlen(hpbuf);
550 1.4 christos if (bp <= linebuf + hppos) {
551 1.4 christos while (bp < linebuf + hppos)
552 1.4 christos *bp++ = ' ';
553 1.4 christos (void) strcpy(bp, hpbuf);
554 1.4 christos Sprintf(eos(bp), " [%d]", t1->maxhp);
555 1.4 christos }
556 1.4 christos }
557 1.4 christos if (so == 0)
558 1.4 christos puts(linebuf);
559 1.4 christos else if (so > 0) {
560 1.4 christos char *bp = eos(linebuf);
561 1.4 christos if (so >= COLNO)
562 1.4 christos so = COLNO - 1;
563 1.4 christos while (bp < linebuf + so)
564 1.4 christos *bp++ = ' ';
565 1.4 christos *bp = 0;
566 1.4 christos standoutbeg();
567 1.4 christos fputs(linebuf, stdout);
568 1.4 christos standoutend();
569 1.4 christos (void) putchar('\n');
570 1.4 christos }
571 1.4 christos return (strlen(linebuf));
572 1.4 christos }
573 1.4 christos
574 1.4 christos char *
575 1.11 dholland itoa(int a)
576 1.4 christos {
577 1.4 christos static char buf[12];
578 1.4 christos Sprintf(buf, "%d", a);
579 1.4 christos return (buf);
580 1.4 christos }
581 1.4 christos
582 1.5 jsm const char *
583 1.11 dholland ordin(int n)
584 1.4 christos {
585 1.9 dholland int dg = n % 10;
586 1.9 dholland
587 1.9 dholland return ((dg == 0 || dg > 3 || n / 10 == 1) ? "th" : (dg == 1) ? "st" :
588 1.9 dholland (dg == 2) ? "nd" : "rd");
589 1.4 christos }
590 1.4 christos
591 1.4 christos void
592 1.11 dholland clearlocks(void)
593 1.4 christos {
594 1.4 christos int x;
595 1.4 christos (void) signal(SIGHUP, SIG_IGN);
596 1.4 christos for (x = maxdlevel; x >= 0; x--) {
597 1.1 cgd glo(x);
598 1.1 cgd (void) unlink(lock); /* not all levels need be present */
599 1.1 cgd }
600 1.1 cgd }
601 1.1 cgd
602 1.1 cgd #ifdef NOSAVEONHANGUP
603 1.4 christos /*ARGSUSED*/
604 1.4 christos void
605 1.11 dholland hangup(int n __unused)
606 1.1 cgd {
607 1.1 cgd (void) signal(SIGINT, SIG_IGN);
608 1.1 cgd clearlocks();
609 1.1 cgd exit(1);
610 1.1 cgd }
611 1.4 christos #endif /* NOSAVEONHANGUP */
612 1.1 cgd
613 1.4 christos char *
614 1.11 dholland eos(char *s)
615 1.1 cgd {
616 1.4 christos while (*s)
617 1.4 christos s++;
618 1.4 christos return (s);
619 1.1 cgd }
620 1.1 cgd
621 1.1 cgd /* it is the callers responsibility to check that there is room for c */
622 1.4 christos void
623 1.11 dholland charcat(char *s, int c)
624 1.4 christos {
625 1.4 christos while (*s)
626 1.4 christos s++;
627 1.1 cgd *s++ = c;
628 1.1 cgd *s = 0;
629 1.1 cgd }
630 1.1 cgd
631 1.1 cgd /*
632 1.1 cgd * Called with args from main if argc >= 0. In this case, list scores as
633 1.1 cgd * requested. Otherwise, find scores for the current player (and list them
634 1.1 cgd * if argc == -1).
635 1.1 cgd */
636 1.4 christos void
637 1.11 dholland prscore(int argc, char **argv)
638 1.4 christos {
639 1.4 christos char **players = NULL;
640 1.4 christos int playerct;
641 1.4 christos int rank;
642 1.4 christos struct toptenentry *t1, *t2;
643 1.5 jsm const char *recfile = RECORD;
644 1.4 christos FILE *rfile;
645 1.4 christos int flg = 0;
646 1.4 christos int i;
647 1.1 cgd #ifdef nonsense
648 1.4 christos long total_score = 0L;
649 1.4 christos char totchars[10];
650 1.4 christos int totcharct = 0;
651 1.4 christos #endif /* nonsense */
652 1.4 christos int outflg = (argc >= -1);
653 1.1 cgd #ifdef PERS_IS_UID
654 1.4 christos int uid = -1;
655 1.1 cgd #else
656 1.4 christos char *player0;
657 1.4 christos #endif /* PERS_IS_UID */
658 1.1 cgd
659 1.4 christos if (!(rfile = fopen(recfile, "r"))) {
660 1.1 cgd puts("Cannot open record file!");
661 1.1 cgd return;
662 1.1 cgd }
663 1.4 christos if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
664 1.4 christos if (!argv[1][2]) {
665 1.1 cgd argc--;
666 1.1 cgd argv++;
667 1.4 christos } else if (!argv[1][3] && strchr("CFKSTWX", argv[1][2])) {
668 1.1 cgd argv[1]++;
669 1.1 cgd argv[1][0] = '-';
670 1.4 christos } else
671 1.4 christos argv[1] += 2;
672 1.1 cgd }
673 1.4 christos if (argc <= 1) {
674 1.1 cgd #ifdef PERS_IS_UID
675 1.1 cgd uid = getuid();
676 1.1 cgd playerct = 0;
677 1.1 cgd #else
678 1.1 cgd player0 = plname;
679 1.4 christos if (!*player0)
680 1.1 cgd player0 = "hackplayer";
681 1.1 cgd playerct = 1;
682 1.1 cgd players = &player0;
683 1.4 christos #endif /* PERS_IS_UID */
684 1.1 cgd } else {
685 1.1 cgd playerct = --argc;
686 1.1 cgd players = ++argv;
687 1.1 cgd }
688 1.4 christos if (outflg)
689 1.4 christos putchar('\n');
690 1.1 cgd
691 1.1 cgd t1 = tt_head = newttentry();
692 1.4 christos for (rank = 1;; rank++) {
693 1.4 christos if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
694 1.4 christos t1->date, &t1->uid,
695 1.4 christos &t1->level, &t1->maxlvl,
696 1.4 christos &t1->hp, &t1->maxhp, &t1->points,
697 1.4 christos &t1->plchar, &t1->sex, t1->name, t1->death) != 11)
698 1.1 cgd t1->points = 0;
699 1.4 christos if (t1->points == 0)
700 1.4 christos break;
701 1.1 cgd #ifdef PERS_IS_UID
702 1.4 christos if (!playerct && t1->uid == uid)
703 1.1 cgd flg++;
704 1.4 christos else
705 1.4 christos #endif /* PERS_IS_UID */
706 1.4 christos for (i = 0; i < playerct; i++) {
707 1.4 christos if (strcmp(players[i], "all") == 0 ||
708 1.4 christos strncmp(t1->name, players[i], NAMSZ) == 0 ||
709 1.4 christos (players[i][0] == '-' &&
710 1.4 christos players[i][1] == t1->plchar &&
711 1.4 christos players[i][2] == 0) ||
712 1.4 christos (digit(players[i][0]) && rank <= atoi(players[i])))
713 1.4 christos flg++;
714 1.4 christos }
715 1.4 christos t1 = t1->tt_next = newttentry();
716 1.1 cgd }
717 1.1 cgd (void) fclose(rfile);
718 1.4 christos if (!flg) {
719 1.4 christos if (outflg) {
720 1.4 christos printf("Cannot find any entries for ");
721 1.4 christos if (playerct < 1)
722 1.4 christos printf("you.\n");
723 1.4 christos else {
724 1.4 christos if (playerct > 1)
725 1.4 christos printf("any of ");
726 1.4 christos for (i = 0; i < playerct; i++)
727 1.4 christos printf("%s%s", players[i], (i < playerct - 1) ? ", " : ".\n");
728 1.4 christos printf("Call is: %s -s [playernames]\n", hname);
729 1.4 christos }
730 1.1 cgd }
731 1.4 christos return;
732 1.1 cgd }
733 1.4 christos if (outflg)
734 1.4 christos outheader();
735 1.1 cgd t1 = tt_head;
736 1.4 christos for (rank = 1; t1->points != 0; rank++, t1 = t2) {
737 1.1 cgd t2 = t1->tt_next;
738 1.1 cgd #ifdef PERS_IS_UID
739 1.4 christos if (!playerct && t1->uid == uid)
740 1.1 cgd goto outwithit;
741 1.1 cgd else
742 1.4 christos #endif /* PERS_IS_UID */
743 1.4 christos for (i = 0; i < playerct; i++) {
744 1.4 christos if (strcmp(players[i], "all") == 0 ||
745 1.4 christos strncmp(t1->name, players[i], NAMSZ) == 0 ||
746 1.4 christos (players[i][0] == '-' &&
747 1.4 christos players[i][1] == t1->plchar &&
748 1.4 christos players[i][2] == 0) ||
749 1.4 christos (digit(players[i][0]) && rank <= atoi(players[i]))) {
750 1.1 cgd outwithit:
751 1.4 christos if (outflg)
752 1.4 christos (void) outentry(rank, t1, 0);
753 1.1 cgd #ifdef nonsense
754 1.4 christos total_score += t1->points;
755 1.4 christos if (totcharct < sizeof(totchars) - 1)
756 1.4 christos totchars[totcharct++] = t1->plchar;
757 1.4 christos #endif /* nonsense */
758 1.4 christos break;
759 1.4 christos }
760 1.1 cgd }
761 1.1 cgd free((char *) t1);
762 1.1 cgd }
763 1.1 cgd #ifdef nonsense
764 1.1 cgd totchars[totcharct] = 0;
765 1.1 cgd
766 1.4 christos /*
767 1.4 christos * We would like to determine whether he is experienced. However, the
768 1.4 christos * information collected here only tells about the scores/roles that
769 1.4 christos * got into the topten (top 100?). We should maintain a .hacklog or
770 1.4 christos * something in his home directory.
771 1.4 christos */
772 1.1 cgd flags.beginner = (total_score < 6000);
773 1.4 christos for (i = 0; i < 6; i++)
774 1.4 christos if (!strchr(totchars, "CFKSTWX"[i])) {
775 1.4 christos flags.beginner = 1;
776 1.4 christos if (!pl_character[0])
777 1.4 christos pl_character[0] = "CFKSTWX"[i];
778 1.4 christos break;
779 1.4 christos }
780 1.4 christos #endif /* nonsense */
781 1.1 cgd }
782