pc-hack

PC HACK 3.61 source code (archival)
git clone http://frotz.net/git/pc-hack.git
Log | Files | Refs

end.c (14573B)


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