Source:NetHack 3.4.0/topten.c

Below is the full text to topten.c from the source code of NetHack 3.4.0. To link to a particular line, write [[NetHack 3.4.0/topten.c#line123 ]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

1.   /*	SCCS Id: @(#)topten.c	3.4	2000/01/21	*/ 2.   /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3.    /* NetHack may be freely redistributed. See license for details. */ 4.     5.    #include "hack.h"  6.    #include "dlb.h"  7.    #ifdef SHORT_FILENAMES 8.   #include "patchlev.h"  9.    #else 10.  #include "patchlevel.h"  11. #endif 12.   13.   #ifdef VMS 14.   /* We don't want to rewrite the whole file, because that entails	 */ 15.   /* creating a new version which requires that the old one be deletable. */ 16.   # define UPDATE_RECORD_IN_PLACE 17.  #endif 18.   19.   /*  20.    * Updating in place can leave junk at the end of the file in some 21.   * circumstances (if it shrinks and the O.S. doesn't have a straightforward  22.    * way to truncate it). The trailing junk is harmless and the code 23.   * which reads the scores will ignore it. 24.   */  25.   #ifdef UPDATE_RECORD_IN_PLACE 26.  static long final_fpos; 27.  #endif 28.   29.   #define done_stopprint program_state.stopprint 30.   31.   #define newttentry (struct toptenentry *) alloc(sizeof(struct toptenentry)) 32.  #define dealloc_ttentry(ttent) free((genericptr_t) (ttent)) 33.  #define NAMSZ	10 34.  #define DTHSZ	100 35.  #define ROLESZ   3 36.  #define PERSMAX	 3		/* entries per name/uid per char. allowed */ 37.  #define POINTSMIN	1	/* must be > 0 */ 38.  #define ENTRYMAX	100	/* must be >= 10 */ 39.   40.   #if !defined(MICRO) && !defined(MAC) 41.  #define PERS_IS_UID		/* delete for PERSMAX per name; now per uid */ 42.  #endif 43.  struct toptenentry { 44.  	struct toptenentry *tt_next; 45.  #ifdef UPDATE_RECORD_IN_PLACE 46.  	long fpos; 47.  #endif 48.  	long points; 49.  	int deathdnum, deathlev; 50.  	int maxlvl, hp, maxhp, deaths; 51.  	int ver_major, ver_minor, patchlevel; 52.  	long deathdate, birthdate; 53.  	int uid; 54.  	char plrole[ROLESZ+1]; 55.  	char plrace[ROLESZ+1]; 56.  	char plgend[ROLESZ+1]; 57.  	char plalign[ROLESZ+1]; 58.  	char name[NAMSZ+1]; 59.  	char death[DTHSZ+1]; 60.  } *tt_head; 61.   62.   STATIC_DCL void FDECL(topten_print, (const char *)); 63.  STATIC_DCL void FDECL(topten_print_bold, (const char *)); 64.  STATIC_DCL xchar FDECL(observable_depth, (d_level *)); 65.  STATIC_DCL void NDECL(outheader); 66.  STATIC_DCL void FDECL(outentry, (int,struct toptenentry *,BOOLEAN_P)); 67.  STATIC_DCL void FDECL(readentry, (FILE *,struct toptenentry *)); 68.  STATIC_DCL void FDECL(writeentry, (FILE *,struct toptenentry *)); 69.  STATIC_DCL void FDECL(free_ttlist, (struct toptenentry *)); 70.  STATIC_DCL int FDECL(classmon, (char *,BOOLEAN_P)); 71.  STATIC_DCL int FDECL(score_wanted,  72.   		(BOOLEAN_P, int,struct toptenentry *,int,const char **,int)); 73.  #ifdef NO_SCAN_BRACK 74.  STATIC_DCL void FDECL(nsb_mung_line,(char*)); 75.  STATIC_DCL void FDECL(nsb_unmung_line,(char*)); 76.  #endif 77.   78.   /* must fit with end.c; used in rip.c */ 79.  NEARDATA const char *killed_by_prefix[] = { 80.  	"killed by ", "choked on ", "poisoned by ", "", "drowned in ", 81.  	"", "dissolved in ", "crushed to death by ", "petrified by ", 82.  	"turned to slime by ", "killed by ", "", "", "", "", "" 83.  };  84.    85.   static winid toptenwin = WIN_ERR; 86.   87.   STATIC_OVL void 88.  topten_print(x) 89.  const char *x; 90.  {  91.   	if (toptenwin == WIN_ERR) 92.  	    raw_print(x); 93.  	else 94.  	    putstr(toptenwin, ATR_NONE, x); 95.  }  96.    97.   STATIC_OVL void 98.  topten_print_bold(x) 99.  const char *x; 100. {  101.  	if (toptenwin == WIN_ERR) 102. 	    raw_print_bold(x); 103. 	else 104. 	    putstr(toptenwin, ATR_BOLD, x); 105. }  106.   107.  STATIC_OVL xchar 108. observable_depth(lev) 109. d_level *lev; 110. {  111.  #if 0	/* if we ever randomize the order of the elemental planes, we  112. must use a constant external representation in the record file */ 113. 	if (In_endgame(lev)) { 114. 	    if (Is_astralevel(lev))	 return -5; 115. 	    else if (Is_waterlevel(lev)) return -4; 116. 	    else if (Is_firelevel(lev))	 return -3; 117. 	    else if (Is_airlevel(lev))	 return -2; 118. 	    else if (Is_earthlevel(lev)) return -1; 119. 	    else			 return 0;	/* ? */ 120.  	} else 121. #endif 122. 	    return depth(lev); 123. }  124.   125.  STATIC_OVL void 126. readentry(rfile,tt) 127. FILE *rfile; 128. struct toptenentry *tt; 129. {  130.  #ifdef NO_SCAN_BRACK /* Version_ Pts DgnLevs_ Hp___ Died__Born id */ 131. 	static const char *fmt = "%d %d %d %ld %d %d %d %d %d %d %ld %ld %d%*c"; 132. 	static const char *fmt32 = "%c%c %s %s%*c"; 133. 	static const char *fmt33 = "%s %s %s %s %s %s%*c"; 134. #else 135. 	static const char *fmt = "%d.%d.%d %ld %d %d %d %d %d %d %ld %ld %d "; 136. 	static const char *fmt32 = "%c%c %[^,],%[^\n]%*c"; 137. 	static const char *fmt33 = "%s %s %s %s %[^,],%[^\n]%*c"; 138. #endif 139.  140.  #ifdef UPDATE_RECORD_IN_PLACE 141. 	/* note: fscanf below must read the record's terminating newline */ 142. 	final_fpos = tt->fpos = ftell(rfile); 143. #endif 144. #define TTFIELDS 13 145. 	if(fscanf(rfile, fmt, 146. 			&tt->ver_major, &tt->ver_minor, &tt->patchlevel, 147. 			&tt->points, &tt->deathdnum, &tt->deathlev, 148. 			&tt->maxlvl, &tt->hp, &tt->maxhp, &tt->deaths, 149. 			&tt->deathdate, &tt->birthdate, 150. 			&tt->uid) != TTFIELDS) 151. #undef TTFIELDS 152. 		tt->points = 0; 153. 	else { 154. 		/* Check for backwards compatibility */ 155. 		if (tt->ver_major < 3 ||  156.  				(tt->ver_major == 3 && tt->ver_minor < 3)) { 157. 			int i;  158. 159. 		    if (fscanf(rfile, fmt32, 160. 				tt->plrole, tt->plgend, 161. 				tt->name, tt->death) != 4) 162. 			tt->points = 0; 163. 		    tt->plrole[1] = '\0'; 164. 		    if ((i = str2role(tt->plrole)) >= 0) 165. 			Strcpy(tt->plrole, roles[i].filecode); 166. 		    Strcpy(tt->plrace, "?"); 167. 		    Strcpy(tt->plgend, (tt->plgend[0] == 'M') ? "Mal" : "Fem"); 168. 		    Strcpy(tt->plalign, "?"); 169. 		} else if (fscanf(rfile, fmt33, 170. 				tt->plrole, tt->plrace, tt->plgend, 171. 				tt->plalign, tt->name, tt->death) != 6) 172. 			tt->points = 0; 173. #ifdef NO_SCAN_BRACK 174. 		if(tt->points > 0) { 175. 			nsb_unmung_line(tt->name); 176. 			nsb_unmung_line(tt->death); 177. 		}  178.  #endif 179. 	}  180.   181.  	/* check old score entries for Y2K problem and fix whenever found */ 182. 	if (tt->points > 0) { 183. 		if (tt->birthdate < 19000000L) tt->birthdate += 19000000L; 184. 		if (tt->deathdate < 19000000L) tt->deathdate += 19000000L; 185. 	}  186.  }  187.   188.  STATIC_OVL void 189. writeentry(rfile,tt) 190. FILE *rfile; 191. struct toptenentry *tt; 192. {  193.  #ifdef NO_SCAN_BRACK 194. 	nsb_mung_line(tt->name); 195. 	nsb_mung_line(tt->death); 196. 	                   /* Version_ Pts DgnLevs_ Hp___ Died__Born id */ 197. 	(void) fprintf(rfile,"%d %d %d %ld %d %d %d %d %d %d %ld %ld %d ",  198.  #else  199.  	(void) fprintf(rfile,"%d.%d.%d %ld %d %d %d %d %d %d %ld %ld %d ", 200. #endif 201. 		tt->ver_major, tt->ver_minor, tt->patchlevel, 202. 		tt->points, tt->deathdnum, tt->deathlev, 203. 		tt->maxlvl, tt->hp, tt->maxhp, tt->deaths, 204. 		tt->deathdate, tt->birthdate, tt->uid);  205.  	if (tt->ver_major < 3 || 206. 			(tt->ver_major == 3 && tt->ver_minor < 3))  207.  #ifdef NO_SCAN_BRACK  208.  		(void) fprintf(rfile,"%c%c %s %s\n", 209. #else 210. 		(void) fprintf(rfile,"%c%c %s,%s\n",  211.  #endif  212.  			tt->plrole[0], tt->plgend[0],  213.  			onlyspace(tt->name) ? "_" : tt->name, tt->death); 214. 	else 215. #ifdef NO_SCAN_BRACK 216. 		(void) fprintf(rfile,"%s %s %s %s %s %s\n",  217.  #else  218.  		(void) fprintf(rfile,"%s %s %s %s %s,%s\n", 219. #endif 220. 			tt->plrole, tt->plrace, tt->plgend, tt->plalign, 221. 			onlyspace(tt->name) ? "_" : tt->name, tt->death); 222.   223.  #ifdef NO_SCAN_BRACK  224.  	nsb_unmung_line(tt->name);  225.  	nsb_unmung_line(tt->death);  226.  #endif  227.  }  228.   229.  STATIC_OVL void  230.  free_ttlist(tt)  231.  struct toptenentry *tt;  232.  {  233.  	struct toptenentry *ttnext;  234.   235.  	while (tt->points > 0) {  236.  		ttnext = tt->tt_next;  237.  		dealloc_ttentry(tt);  238.  		tt = ttnext;  239.  	}  240.  	dealloc_ttentry(tt);  241.  }  242.   243.  void  244.  topten(how)  245.  int how;  246.  {  247.  	int uid = getuid;  248.  	int rank, rank0 = -1, rank1 = 0;  249.  	int occ_cnt = PERSMAX;  250.  	register struct toptenentry *t0, *tprev;  251.  	struct toptenentry *t1;  252.  	FILE *rfile;  253.  	register int flg = 0;  254.  	boolean t0_used;  255.  #ifdef LOGFILE  256.  	FILE *lfile;  257.  #endif /* LOGFILE */  258.   259.  /* Under DICE 3.0, this crashes the system consistently, apparently due to  260. * corruption of *rfile somewhere. Until I figure this out, just cut out 261.  * topten support entirely - at least then the game exits cleanly. --AC 262.  */  263.  #ifdef _DCC 264. 	return; 265. #endif 266.  267.  /* If we are in the midst of a panic, cut out topten entirely. 268.  * topten uses alloc several times, which will lead to  269. * problems if the panic was the result of an alloc failure. 270.  */  271.  	if (program_state.panicking) 272. 		return; 273.  274.  	if (flags.toptenwin) { 275. 	    toptenwin = create_nhwindow(NHW_TEXT); 276. 	}  277.   278.  #if defined(UNIX) || defined(VMS) || defined(__EMX__) 279. #define HUP	if (!program_state.done_hup) 280. #else 281. #define HUP 282. #endif 283.  284.  #ifdef TOS 285. 	restore_colors;	/* make sure the screen is black on white */ 286. #endif 287. 	/* create a new 'topten' entry */ 288. 	t0_used = FALSE; 289. 	t0 = newttentry; 290. 	/* deepest_lev_reached is in terms of depth, and reporting the 291. 	 * deepest level reached in the dungeon death occurred in doesn't  292. * seem right, so we have to report the death level in depth terms 293. 	 * as well (which also seems reasonable since that's all the player  294.  	 * sees on the screen anyway) 295. 	 */  296.  	t0->ver_major = VERSION_MAJOR; 297. 	t0->ver_minor = VERSION_MINOR; 298. 	t0->patchlevel = PATCHLEVEL; 299. 	t0->points = u.urexp; 300. 	t0->deathdnum = u.uz.dnum; 301. 	t0->deathlev = observable_depth(&u.uz); 302. 	t0->maxlvl = deepest_lev_reached(TRUE); 303. 	t0->hp = u.uhp; 304. 	t0->maxhp = u.uhpmax; 305. 	t0->deaths = u.umortality; 306. 	t0->uid = uid; 307. 	(void) strncpy(t0->plrole, urole.filecode, ROLESZ); 308. 	t0->plrole[ROLESZ] = '\0'; 309. 	(void) strncpy(t0->plrace, urace.filecode, ROLESZ); 310. 	t0->plrace[ROLESZ] = '\0'; 311. 	(void) strncpy(t0->plgend, genders[flags.female].filecode, ROLESZ); 312. 	t0->plgend[ROLESZ] = '\0'; 313. 	(void) strncpy(t0->plalign, aligns[1-u.ualign.type].filecode, ROLESZ); 314. 	t0->plalign[ROLESZ] = '\0'; 315. 	(void) strncpy(t0->name, plname, NAMSZ); 316. 	t0->name[NAMSZ] = '\0'; 317. 	t0->death[0] = '\0'; 318. 	switch (killer_format) { 319. 		default: impossible("bad killer format?"); 320. 		case KILLED_BY_AN: 321. 			Strcat(t0->death, killed_by_prefix[how]); 322. 			(void) strncat(t0->death, an(killer),  323.  						DTHSZ-strlen(t0->death)); 324. 			break; 325. 		case KILLED_BY: 326. 			Strcat(t0->death, killed_by_prefix[how]); 327. 			(void) strncat(t0->death, killer,  328.  						DTHSZ-strlen(t0->death)); 329. 			break; 330. 		case NO_KILLER_PREFIX: 331. 			(void) strncat(t0->death, killer, DTHSZ); 332. 			break; 333. 	}  334.  	t0->birthdate = yyyymmdd(u.ubirthday); 335. 	t0->deathdate = yyyymmdd((time_t)0L); 336. 	t0->tt_next = 0; 337. #ifdef UPDATE_RECORD_IN_PLACE 338. 	t0->fpos = -1L; 339. #endif 340.  341.  #ifdef LOGFILE		/* used for debugging (who dies of what, where) */ 342. 	if (lock_file(LOGFILE, SCOREPREFIX, 10)) { 343. 	    if(!(lfile = fopen_datafile(LOGFILE, "a", TRUE))) { 344. 		HUP raw_print("Cannot open log file!"); 345. 	    } else { 346. 		writeentry(lfile, t0); 347. 		(void) fclose(lfile); 348. 	    }  349.  	    unlock_file(LOGFILE); 350. 	}  351.  #endif /* LOGFILE */ 352.  353.  	if (wizard || discover) { 354. 	    if (how != PANICKED) HUP { 355. 		char pbuf[BUFSZ]; 356. 		topten_print(""); 357. 		Sprintf(pbuf,  358.  	      "Since you were in %s mode, the score list will not be checked.",  359.  		    wizard ? "wizard" : "discover"); 360. 		topten_print(pbuf); 361. 	    }  362.  	    goto showwin; 363. 	}  364.   365.  	if (!lock_file(RECORD, SCOREPREFIX, 60)) 366. 		goto destroywin; 367.  368.  #ifdef UPDATE_RECORD_IN_PLACE 369. 	rfile = fopen_datafile(RECORD, "r+", TRUE); 370. #else 371. 	rfile = fopen_datafile(RECORD, "r", TRUE); 372. #endif 373.  374.  	if (!rfile) { 375. 		HUP raw_print("Cannot open record file!"); 376. 		unlock_file(RECORD); 377. 		goto destroywin; 378. 	}  379.   380.  	HUP topten_print(""); 381.  382.  	/* assure minimum number of points */ 383. 	if(t0->points < POINTSMIN) t0->points = 0; 384.  385.  	t1 = tt_head = newttentry; 386. 	tprev = 0; 387. 	/* rank0: -1 undefined, 0 not_on_list, n n_th on list */ 388. 	for(rank = 1; ; ) { 389. 	    readentry(rfile, t1); 390. 	    if (t1->points < POINTSMIN) t1->points = 0; 391. 	    if(rank0 < 0 && t1->points < t0->points) { 392. 		rank0 = rank++; 393. 		if(tprev == 0) 394. 			tt_head = t0; 395. 		else 396. 			tprev->tt_next = t0; 397. 		t0->tt_next = t1; 398. #ifdef UPDATE_RECORD_IN_PLACE 399. 		t0->fpos = t1->fpos;	/* insert here */ 400. #endif 401. 		t0_used = TRUE; 402. 		occ_cnt--; 403. 		flg++;		/* ask for a rewrite */ 404. 	    } else tprev = t1; 405.  406.  	    if(t1->points == 0) break; 407. 	    if(  408.  #ifdef PERS_IS_UID  409.  		t1->uid == t0->uid &&  410.  #else  411.  		strncmp(t1->name, t0->name, NAMSZ) == 0 &&  412.  #endif  413.  		!strncmp(t1->plrole, t0->plrole, ROLESZ) &&  414.  		--occ_cnt <= 0) { 415. 		    if(rank0 < 0) { 416. 			rank0 = 0; 417. 			rank1 = rank; 418. 			HUP { 419. 			    char pbuf[BUFSZ]; 420. 			    Sprintf(pbuf,  421.  			  "You didn't beat your previous score of %ld points.",  422.  				    t1->points); 423. 			    topten_print(pbuf); 424. 			    topten_print(""); 425. 			}  426.  		    }  427.  		    if(occ_cnt < 0) { 428. 			flg++; 429. 			continue; 430. 		    }  431.  		}  432.  	    if(rank <= ENTRYMAX) { 433. 		t1->tt_next = newttentry; 434. 		t1 = t1->tt_next; 435. 		rank++; 436. 	    }  437.  	    if(rank > ENTRYMAX) { 438. 		t1->points = 0; 439. 		break; 440. 	    }  441.  	}  442.  	if(flg) {	/* rewrite record file */ 443. #ifdef UPDATE_RECORD_IN_PLACE 444. 		(void) fseek(rfile, (t0->fpos >= 0 ? 445. 				     t0->fpos : final_fpos), SEEK_SET); 446. #else 447. 		(void) fclose(rfile); 448. 		if(!(rfile = fopen_datafile(RECORD, "w", TRUE))){ 449. 			HUP raw_print("Cannot write record file"); 450. 			unlock_file(RECORD); 451. 			free_ttlist(tt_head); 452. 			goto destroywin; 453. 		}  454.  #endif	/* UPDATE_RECORD_IN_PLACE */ 455. 		if(!done_stopprint) if(rank0 > 0){ 456. 		    if(rank0 <= 10) 457. 			topten_print("You made the top ten list!"); 458. 		    else { 459. 			char pbuf[BUFSZ]; 460. 			Sprintf(pbuf,  461.  			  "You reached the %d%s place on the top %d list.",  462.  				rank0, ordin(rank0), ENTRYMAX); 463. 			topten_print(pbuf); 464. 		    }  465.  		    topten_print(""); 466. 		}  467.  	}  468.  	if(rank0 == 0) rank0 = rank1; 469. 	if(rank0 <= 0) rank0 = rank; 470. 	if(!done_stopprint) outheader; 471. 	t1 = tt_head; 472. 	for(rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) { 473. 	    if(flg  474.  #ifdef UPDATE_RECORD_IN_PLACE  475.  		    && rank >= rank0  476.  #endif  477.  		) writeentry(rfile, t1); 478. 	    if (done_stopprint) continue; 479. 	    if (rank > flags.end_top &&  480.  		    (rank < rank0 - flags.end_around || 481. 		     rank > rank0 + flags.end_around) &&  482.  		    (!flags.end_own || 483. #ifdef PERS_IS_UID 484. 					t1->uid != t0->uid 485. #else 486. 					strncmp(t1->name, t0->name, NAMSZ) 487. #endif 488. 		)) continue; 489. 	    if (rank == rank0 - flags.end_around &&  490.  		    rank0 > flags.end_top + flags.end_around + 1 &&  491.  		    !flags.end_own) 492. 		topten_print(""); 493. 	    if(rank != rank0) 494. 		outentry(rank, t1, FALSE); 495. 	    else if(!rank1) 496. 		outentry(rank, t1, TRUE); 497. 	    else { 498. 		outentry(rank, t1, TRUE); 499. 		outentry(0, t0, TRUE); 500. 	    }  501.  	}  502.  	if(rank0 >= rank) if(!done_stopprint) 503. 		outentry(0, t0, TRUE); 504. #ifdef UPDATE_RECORD_IN_PLACE 505. 	if (flg) { 506. # ifdef TRUNCATE_FILE 507. 	    /* if a reasonable way to truncate a file exists, use it */ 508. 	    truncate_file(rfile); 509. # else 510. 	    /* use sentinel record rather than relying on truncation */ 511. 	    t1->points = 0L;	/* terminates file when read back in */ 512. 	    t1->ver_major = t1->ver_minor = t1->patchlevel = 0; 513. 	    t1->uid = t1->deathdnum = t1->deathlev = 0; 514. 	    t1->maxlvl = t1->hp = t1->maxhp = t1->deaths = 0; 515. 	    t1->plrole[0] = t1->plrace[0] = t1->plgend[0] = t1->plalign[0] = '-'; 516. 	    t1->plrole[1] = t1->plrace[1] = t1->plgend[1] = t1->plalign[1] = 0; 517. 	    t1->birthdate = t1->deathdate = yyyymmdd((time_t)0L); 518. 	    Strcpy(t1->name, "@"); 519. 	    Strcpy(t1->death, " \n"); 520. 	    writeentry(rfile, t1); 521. 	    (void) fflush(rfile); 522. # endif	/* TRUNCATE_FILE */ 523. 	}  524.  #endif	/* UPDATE_RECORD_IN_PLACE */ 525. 	(void) fclose(rfile); 526. 	unlock_file(RECORD); 527. 	free_ttlist(tt_head); 528.  529.    showwin: 530. 	if (flags.toptenwin && !done_stopprint) display_nhwindow(toptenwin, 1); 531.   destroywin: 532. 	if (!t0_used) dealloc_ttentry(t0); 533. 	if (flags.toptenwin) { 534. 	    destroy_nhwindow(toptenwin); 535. 	    toptenwin=WIN_ERR; 536. 	}  537.  }  538.   539.  STATIC_OVL void 540. outheader 541. {  542.  	char linebuf[BUFSZ]; 543. 	register char *bp; 544.  545.  	Strcpy(linebuf, " No  Points     Name"); 546. 	bp = eos(linebuf); 547. 	while(bp < linebuf + COLNO - 9) *bp++ = ' '; 548. 	Strcpy(bp, "Hp [max]"); 549. 	topten_print(linebuf); 550. }  551.   552.  /* so>0: standout line; so=0: ordinary line */ 553. STATIC_OVL void 554. outentry(rank, t1, so) 555. struct toptenentry *t1; 556. int rank; 557. boolean so; 558. {  559.  	boolean second_line = TRUE; 560. 	char linebuf[BUFSZ]; 561. 	char *bp, hpbuf[24], linebuf3[BUFSZ]; 562. 	int hppos, lngr; 563.  564.   565.  	linebuf[0] = '\0'; 566. 	if (rank) Sprintf(eos(linebuf), "%3d", rank); 567. 	else Strcat(linebuf, "   "); 568.  569.  	Sprintf(eos(linebuf), " %10ld  %.10s", t1->points, t1->name); 570. 	Sprintf(eos(linebuf), "-%s", t1->plrole); 571. 	if (t1->plrace[0] != '?') 572. 		Sprintf(eos(linebuf), "-%s", t1->plrace); 573. 	/* Printing of gender and alignment is intentional. It has been 574. 	 * part of the NetHack Geek Code, and illustrates a proper way to  575. * specify a character from the command line. 576. 	 */  577.  	Sprintf(eos(linebuf), "-%s", t1->plgend); 578. 	if (t1->plalign[0] != '?') 579. 		Sprintf(eos(linebuf), "-%s ", t1->plalign); 580. 	else 581. 		Strcat(linebuf, " "); 582. 	if (!strncmp("escaped", t1->death, 7)) { 583. 	    Sprintf(eos(linebuf), "escaped the dungeon %s[max level %d]",  584.  		    !strncmp(" (", t1->death + 7, 2) ? t1->death + 7 + 2 : "", 585. 		    t1->maxlvl);  586.  	    /* fixup for closing paren in "escaped... with...Amulet)[max..." */ 587.  	    if ((bp = index(linebuf, ')')) != 0)  588.  		*bp = (t1->deathdnum == astral_level.dnum) ? '\0' : ' ';  589.  	    second_line = FALSE;  590.  	} else if (!strncmp("ascended", t1->death, 8)) {  591.  	    Sprintf(eos(linebuf), "ascended to demigod%s-hood", 592. 		    (t1->plgend[0] == 'F') ? "dess" : ""); 593.  	    second_line = FALSE;  594.  	} else {  595.  	    if (!strncmp(t1->death, "quit", 4)) {  596.  		Strcat(linebuf, "quit");  597.  		second_line = FALSE;  598.  	    } else if (!strncmp(t1->death, "starv", 5)) {  599.  		Strcat(linebuf, "starved to death");  600.  		second_line = FALSE;  601.  	    } else if (!strncmp(t1->death, "choked", 6)) {  602.  		Sprintf(eos(linebuf), "choked on h%s food", 603. 			(t1->plgend[0] == 'F') ? "er" : "is"); 604.  	    } else if (!strncmp(t1->death, "poisoned", 8)) {  605.  		Strcat(linebuf, "was poisoned");  606.  	    } else if (!strncmp(t1->death, "crushed", 7)) {  607.  		Strcat(linebuf, "was crushed to death");  608.  	    } else if (!strncmp(t1->death, "petrified by ", 13)) {  609.  		Strcat(linebuf, "turned to stone");  610.  	    } else Strcat(linebuf, "died");  611.   612.  	    if (t1->deathdnum == astral_level.dnum) {  613.  		const char *arg, *fmt = " on the Plane of %s";  614.   615.  		switch (t1->deathlev) {  616.  		case -5:  617.  			fmt = " on the %s Plane";  618.  			arg = "Astral";	break;  619.  		case -4:  620.  			arg = "Water";	break;  621.  		case -3:  622.  			arg = "Fire";	break;  623.  		case -2:  624.  			arg = "Air";	break;  625.  		case -1:  626.  			arg = "Earth";	break;  627.  		default:  628.  			arg = "Void";	break;  629.  		}  630.  		Sprintf(eos(linebuf), fmt, arg); 631. 	    } else { 632. 		Sprintf(eos(linebuf), " in %s on level %d",  633.  			dungeons[t1->deathdnum].dname, t1->deathlev); 634. 		if (t1->deathlev != t1->maxlvl) 635. 		    Sprintf(eos(linebuf), " [max %d]", t1->maxlvl); 636. 	    }  637.   638.  	    /* kludge for "quit while already on Charon's boat" */ 639. 	    if (!strncmp(t1->death, "quit ", 5)) 640. 		Strcat(linebuf, t1->death + 4); 641. 	}  642.  	Strcat(linebuf, "."); 643.  644.  	/* Quit, starved, ascended, and escaped contain no second line */ 645. 	if (second_line) 646. 	    Sprintf(eos(linebuf), "  %c%s.", highc(*(t1->death)), t1->death+1); 647.  648.  	lngr = (int)strlen(linebuf); 649. 	if (t1->hp <= 0) hpbuf[0] = '-', hpbuf[1] = '\0'; 650. 	else Sprintf(hpbuf, "%d", t1->hp); 651. 	/* beginning of hp column after padding (not actually padded yet) */ 652. 	hppos = COLNO - (sizeof("  Hp [max]")-1); /* sizeof(str) includes \0 */ 653. 	while (lngr >= hppos) { 654. 	    for(bp = eos(linebuf);  655.  		    !(*bp == ' ' && (bp-linebuf < hppos));  656.  		    bp--) 657. 		;  658.  	    /* special case: if about to wrap in the middle of maximum 659. 	       dungeon depth reached, wrap in front of it instead */ 660. 	    if (bp > linebuf + 5 && !strncmp(bp - 5, " [max", 5)) bp -= 5; 661. 	    Strcpy(linebuf3, bp+1); 662. 	    *bp = 0; 663. 	    if (so) { 664. 		while (bp < linebuf + (COLNO-1)) *bp++ = ' '; 665. 		*bp = 0; 666. 		topten_print_bold(linebuf); 667. 	    } else 668. 		topten_print(linebuf); 669. 	    Sprintf(linebuf, "%15s %s", "", linebuf3); 670. 	    lngr = strlen(linebuf); 671. 	}  672.  	/* beginning of hp column not including padding */ 673. 	hppos = COLNO - 7 - (int)strlen(hpbuf); 674. 	bp = eos(linebuf); 675.  676.  	if (bp <= linebuf + hppos) { 677. 	    /* pad any necessary blanks to the hit point entry */ 678. 	    while (bp < linebuf + hppos) *bp++ = ' '; 679. 	    Strcpy(bp, hpbuf); 680. 	    Sprintf(eos(bp), " %s[%d]",  681.  		    (t1->maxhp < 10) ? "  " : (t1->maxhp < 100) ? " " : "",  682.  		    t1->maxhp); 683. 	}  684.   685.  	if (so) { 686. 	    bp = eos(linebuf); 687. 	    if (so >= COLNO) so = COLNO-1; 688. 	    while (bp < linebuf + so) *bp++ = ' '; 689. 	    *bp = 0; 690. 	    topten_print_bold(linebuf); 691. 	} else 692. 	    topten_print(linebuf); 693. }  694.   695.  STATIC_OVL int 696. score_wanted(current_ver, rank, t1, playerct, players, uid) 697. boolean current_ver; 698. int rank; 699. struct toptenentry *t1; 700. int playerct; 701. const char **players; 702. int uid; 703. {  704.  	int i;  705. 706. 	if (current_ver && (t1->ver_major != VERSION_MAJOR || 707. 			    t1->ver_minor != VERSION_MINOR || 708. 			    t1->patchlevel != PATCHLEVEL)) 709. 		return 0; 710.  711.  #ifdef PERS_IS_UID 712. 	if (!playerct && t1->uid == uid) 713. 		return 1; 714. #endif 715.  716.  	for (i = 0; i < playerct; i++) { 717. 	    if (players[i][0] == '-' && index("pr", players[i][1]) &&  718.                  players[i][2] == 0 && i + 1 < playerct) { 719. 		char *arg = (char *)players[i + 1]; 720. 		if ((players[i][1] == 'p' && 721. 		     str2role(arg) == str2role(t1->plrole)) ||  722.  		    (players[i][1] == 'r' && 723. 		     str2race(arg) == str2race(t1->plrace))) 724. 		    return 1; 725. 		i++; 726. 	    } else if (strcmp(players[i], "all") == 0 ||  727.  		    strncmp(t1->name, players[i], NAMSZ) == 0 ||  728.  		    (players[i][0] == '-' && 729. 		     players[i][1] == t1->plrole[0] && 730. 		     players[i][2] == 0) ||  731.  		    (digit(players[i][0]) && rank <= atoi(players[i]))) 732. 		return 1; 733. 	}  734.  	return 0; 735. }  736.   737.  /*  738.   * print selected parts of score list. 739.  * argc >= 2, with argv[0] untrustworthy (directory names, et al.), 740.  * and argv[1] starting with "-s". 741.  */  742.  void 743. prscore(argc,argv) 744. int argc; 745. char **argv; 746. {  747.  	const char **players; 748. 	int playerct, rank; 749. 	boolean current_ver = TRUE, init_done = FALSE; 750. 	register struct toptenentry *t1; 751. 	FILE *rfile; 752. 	boolean match_found = FALSE; 753. 	register int i;  754. char pbuf[BUFSZ]; 755. 	int uid = -1; 756. #ifndef PERS_IS_UID 757. 	const char *player0; 758. #endif 759.  760.  	if (argc < 2 || strncmp(argv[1], "-s", 2)) { 761. 		raw_printf("prscore: bad arguments (%d)", argc); 762. 		return; 763. 	}  764.   765.  	rfile = fopen_datafile(RECORD, "r", TRUE); 766. 	if (!rfile) { 767. 		raw_print("Cannot open record file!"); 768. 		return; 769. 	}  770.   771.  #ifdef	AMIGA 772. 	{  773.  	    extern winid amii_rawprwin; 774. 	    init_nhwindows(&argc, argv); 775. 	    amii_rawprwin = create_nhwindow(NHW_TEXT); 776. 	}  777.  #endif 778.  779.  	/* If the score list isn't after a game, we never went through 780. 	 * initialization. */ 781.  	if (wiz1_level.dlevel == 0) { 782. 		dlb_init; 783. 		init_dungeons; 784. 		init_done = TRUE; 785. 	}  786.   787.  	if (!argv[1][2]){	/* plain "-s" */ 788. 		argc--; 789. 		argv++; 790. #if 0 /* uses obsolete pl_classes[] */ 791. 	} else if (!argv[1][3] && index(pl_classes, argv[1][2])) { 792. 		/* may get this case instead of next accidentally, 793. 		 * but neither is listed in the documentation, so  794. * anything useful that happens is a bonus anyway */ 795. 		argv[1]++; 796. 		argv[1][0] = '-'; 797. #endif 798. 	} else	argv[1] += 2; 799.  800.  	if (argc > 1 && !strcmp(argv[1], "-v")) { 801. 		current_ver = FALSE; 802. 		argc--; 803. 		argv++; 804. 	}  805.   806.  	if (argc <= 1) { 807. #ifdef PERS_IS_UID 808. 		uid = getuid; 809. 		playerct = 0; 810. 		players = (const char **)0; 811. #else 812. 		player0 = plname; 813. 		if (!*player0) 814. # ifdef AMIGA 815. 			player0 = "all";	/* single user system */ 816. # else 817. 			player0 = "hackplayer"; 818. # endif 819. 		playerct = 1; 820. 		players = &player0; 821. #endif 822. 	} else { 823. 		playerct = --argc; 824. 		players = (const char **)++argv; 825. 	}  826.  	raw_print(""); 827.  828.  	t1 = tt_head = newttentry; 829. 	for (rank = 1; ; rank++) { 830. 	    readentry(rfile, t1); 831. 	    if (t1->points == 0) break; 832. 	    if (!match_found &&  833.  		    score_wanted(current_ver, rank, t1, playerct, players, uid)) 834. 		match_found = TRUE; 835. 	    t1->tt_next = newttentry; 836. 	    t1 = t1->tt_next; 837. 	}  838.   839.  	(void) fclose(rfile); 840. 	if (init_done) { 841. 	    free_dungeons; 842. 	    dlb_cleanup; 843. 	}  844.   845.  	if (match_found) { 846. 	    outheader; 847. 	    t1 = tt_head; 848. 	    for (rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) { 849. 		if (score_wanted(current_ver, rank, t1, playerct, players, uid)) 850. 		    (void) outentry(rank, t1, 0); 851. 	    }  852.  	} else { 853. 	    Sprintf(pbuf, "Cannot find any %sentries for ",  854.  				current_ver ? "current " : ""); 855. 	    if (playerct < 1) Strcat(pbuf, "you."); 856. 	    else { 857. 		if (playerct > 1) Strcat(pbuf, "any of "); 858. 		for (i = 0; i < playerct; i++) { 859. 		    Strcat(pbuf, players[i]); 860. 		    if (i < playerct-1) { 861. 			if (players[i][0] == '-' &&  862.  			    index("pr", players[i][1]) && players[i][2] == 0) 863. 			    Strcat(pbuf, " "); 864. 			else Strcat(pbuf, ":"); 865. 		    }  866.  		}  867.  	    }  868.  	    raw_print(pbuf); 869. 	    raw_printf("Usage: %s -s [-v] [maxrank] [playernames]",  870.   871.  			 hname); 872. 	    raw_printf("Player types are: [-p role] [-r race]"); 873. 	}  874.  	free_ttlist(tt_head); 875. #ifdef	AMIGA 876. 	{  877.  	    extern winid amii_rawprwin; 878. 	    display_nhwindow(amii_rawprwin, 1); 879. 	    destroy_nhwindow(amii_rawprwin); 880. 	    amii_rawprwin = WIN_ERR; 881. 	}  882.  #endif 883. }  884.   885.  STATIC_OVL int 886. classmon(plch, fem) 887. 	char *plch; 888. 	boolean fem; 889. {  890.  	int i;  891. 892. 	/* Look for this role in the role table */ 893. 	for (i = 0; roles[i].name.m; i++) 894. 	    if (!strncmp(plch, roles[i].filecode, ROLESZ)) { 895. 		if (fem && roles[i].femalenum != NON_PM) 896. 		    return roles[i].femalenum; 897. 		else if (roles[i].malenum != NON_PM) 898. 		    return roles[i].malenum; 899. 		else 900. 		    return PM_HUMAN; 901. 	    }  902.  	/* this might be from a 3.2.x score for former Elf class */ 903. 	if (!strcmp(plch, "E")) return PM_RANGER; 904.  905.  	impossible("What weird role is this? (%s)", plch); 906. 	return (PM_HUMAN_MUMMY); 907. }  908.   909.  /*  910.   * Get a random player name and class from the high score list, 911.  * and attach them to an object (for statues or morgue corpses). 912.  */  913.  struct obj * 914. tt_oname(otmp) 915. struct obj *otmp; 916. {  917.  	int rank; 918. 	register int i;  919. register struct toptenentry *tt; 920. 	FILE *rfile; 921. 	struct toptenentry tt_buf; 922.  923.  	if (!otmp) return((struct obj *) 0); 924.  925.  	rfile = fopen_datafile(RECORD, "r", TRUE); 926. 	if (!rfile) { 927. 		impossible("Cannot open record file!"); 928. 		return (struct obj *)0; 929. 	}  930.   931.  	tt = &tt_buf; 932. 	rank = rnd(10); 933. pickentry: 934. 	for(i = rank; i; i--) { 935. 	    readentry(rfile, tt); 936. 	    if(tt->points == 0) break; 937. 	}  938.   939.  	if(tt->points == 0) { 940. 		if(rank > 1) { 941. 			rank = 1; 942. 			rewind(rfile); 943. 			goto pickentry; 944. 		}  945.  		otmp = (struct obj *) 0; 946. 	} else { 947. 		/* reset timer in case corpse started out as lizard or troll */ 948. 		if (otmp->otyp == CORPSE) obj_stop_timers(otmp); 949. 		otmp->corpsenm = classmon(tt->plrole, (tt->plgend[0] == 'F')); 950. 		otmp->owt = weight(otmp); 951. 		otmp = oname(otmp, tt->name); 952. 		if (otmp->otyp == CORPSE) start_corpse_timeout(otmp); 953. 	}  954.   955.  	(void) fclose(rfile); 956. 	return otmp; 957. }  958.   959.  #ifdef NO_SCAN_BRACK 960. /* Lattice scanf isn't up to reading the scorefile. What */ 961. /* follows deals with that; I admit it's ugly. (KL) */ 962. /* Now generally available (KL) */ 963. STATIC_OVL void 964. nsb_mung_line(p) 965. 	char *p; 966. {  967.  	while ((p = index(p, ' ')) != 0) *p = '|'; 968. }  969.   970.  STATIC_OVL void 971. nsb_unmung_line(p) 972. 	char *p; 973. {  974.  	while ((p = index(p, '|')) != 0) *p = ' '; 975. }  976.  #endif /* NO_SCAN_BRACK */ 977.  978.  /*topten.c*/