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