Source:NetHack 3.6.3/src/topten.c
Jump to navigation
Jump to search
Below is the full text to topten.c from the source code of NetHack 3.6.3. To link to a particular line, write [[Source:NetHack 3.6.3/src/topten.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 3.6 topten.c $NHDT-Date: 1450451497 2015/12/18 15:11:37 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.44 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2012. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. #include "dlb.h" 8. #ifdef SHORT_FILENAMES 9. #include "patchlev.h" 10. #else 11. #include "patchlevel.h" 12. #endif 13. 14. #ifdef VMS 15. /* We don't want to rewrite the whole file, because that entails 16. creating a new version which requires that the old one be deletable. */ 17. #define UPDATE_RECORD_IN_PLACE 18. #endif 19. 20. /* 21. * Updating in place can leave junk at the end of the file in some 22. * circumstances (if it shrinks and the O.S. doesn't have a straightforward 23. * way to truncate it). The trailing junk is harmless and the code 24. * which reads the scores will ignore it. 25. */ 26. #ifdef UPDATE_RECORD_IN_PLACE 27. static long final_fpos; 28. #endif 29. 30. #define done_stopprint program_state.stopprint 31. 32. #define newttentry() (struct toptenentry *) alloc(sizeof (struct toptenentry)) 33. #define dealloc_ttentry(ttent) free((genericptr_t) (ttent)) 34. #ifndef NAMSZ 35. /* Changing NAMSZ can break your existing record/logfile */ 36. #define NAMSZ 10 37. #endif 38. #define DTHSZ 100 39. #define ROLESZ 3 40. 41. struct toptenentry { 42. struct toptenentry *tt_next; 43. #ifdef UPDATE_RECORD_IN_PLACE 44. long fpos; 45. #endif 46. long points; 47. int deathdnum, deathlev; 48. int maxlvl, hp, maxhp, deaths; 49. int ver_major, ver_minor, patchlevel; 50. long deathdate, birthdate; 51. int uid; 52. char plrole[ROLESZ + 1]; 53. char plrace[ROLESZ + 1]; 54. char plgend[ROLESZ + 1]; 55. char plalign[ROLESZ + 1]; 56. char name[NAMSZ + 1]; 57. char death[DTHSZ + 1]; 58. } * tt_head; 59. /* size big enough to read in all the string fields at once; includes 60. room for separating space or trailing newline plus string terminator */ 61. #define SCANBUFSZ (4 * (ROLESZ + 1) + (NAMSZ + 1) + (DTHSZ + 1) + 1) 62. 63. STATIC_DCL void FDECL(topten_print, (const char *)); 64. STATIC_DCL void FDECL(topten_print_bold, (const char *)); 65. STATIC_DCL void NDECL(outheader); 66. STATIC_DCL void FDECL(outentry, (int, struct toptenentry *, BOOLEAN_P)); 67. STATIC_DCL void FDECL(discardexcess, (FILE *)); 68. STATIC_DCL void FDECL(readentry, (FILE *, struct toptenentry *)); 69. STATIC_DCL void FDECL(writeentry, (FILE *, struct toptenentry *)); 70. #ifdef XLOGFILE 71. STATIC_DCL void FDECL(writexlentry, (FILE *, struct toptenentry *, int)); 72. STATIC_DCL long NDECL(encodexlogflags); 73. STATIC_DCL long NDECL(encodeconduct); 74. STATIC_DCL long NDECL(encodeachieve); 75. #endif 76. STATIC_DCL void FDECL(free_ttlist, (struct toptenentry *)); 77. STATIC_DCL int FDECL(classmon, (char *, BOOLEAN_P)); 78. STATIC_DCL int FDECL(score_wanted, (BOOLEAN_P, int, struct toptenentry *, int, 79. const char **, int)); 80. #ifdef NO_SCAN_BRACK 81. STATIC_DCL void FDECL(nsb_mung_line, (char *)); 82. STATIC_DCL void FDECL(nsb_unmung_line, (char *)); 83. #endif 84. 85. static winid toptenwin = WIN_ERR; 86. 87. /* "killed by",&c ["an"] 'killer.name' */ 88. void 89. formatkiller(buf, siz, how, incl_helpless) 90. char *buf; 91. unsigned siz; 92. int how; 93. boolean incl_helpless; 94. { 95. static NEARDATA const char *const killed_by_prefix[] = { 96. /* DIED, CHOKING, POISONING, STARVING, */ 97. "killed by ", "choked on ", "poisoned by ", "died of ", 98. /* DROWNING, BURNING, DISSOLVED, CRUSHING, */ 99. "drowned in ", "burned by ", "dissolved in ", "crushed to death by ", 100. /* STONING, TURNED_SLIME, GENOCIDED, */ 101. "petrified by ", "turned to slime by ", "killed by ", 102. /* PANICKED, TRICKED, QUIT, ESCAPED, ASCENDED */ 103. "", "", "", "", "" 104. }; 105. unsigned l; 106. char c, *kname = killer.name; 107. 108. buf[0] = '\0'; /* lint suppression */ 109. switch (killer.format) { 110. default: 111. impossible("bad killer format? (%d)", killer.format); 112. /*FALLTHRU*/ 113. case NO_KILLER_PREFIX: 114. break; 115. case KILLED_BY_AN: 116. kname = an(kname); 117. /*FALLTHRU*/ 118. case KILLED_BY: 119. (void) strncat(buf, killed_by_prefix[how], siz - 1); 120. l = strlen(buf); 121. buf += l, siz -= l; 122. break; 123. } 124. /* Copy kname into buf[]. 125. * Object names and named fruit have already been sanitized, but 126. * monsters can have "called 'arbitrary text'" attached to them, 127. * so make sure that that text can't confuse field splitting when 128. * record, logfile, or xlogfile is re-read at some later point. 129. */ 130. while (--siz > 0) { 131. c = *kname++; 132. if (!c) 133. break; 134. else if (c == ',') 135. c = ';'; 136. /* 'xlogfile' doesn't really need protection for '=', but 137. fixrecord.awk for corrupted 3.6.0 'record' does (only 138. if using xlogfile rather than logfile to repair record) */ 139. else if (c == '=') 140. c = '_'; 141. /* tab is not possible due to use of mungspaces() when naming; 142. it would disrupt xlogfile parsing if it were present */ 143. else if (c == '\t') 144. c = ' '; 145. *buf++ = c; 146. } 147. *buf = '\0'; 148. 149. if (incl_helpless && multi) { 150. /* X <= siz: 'sizeof "string"' includes 1 for '\0' terminator */ 151. if (multi_reason && strlen(multi_reason) + sizeof ", while " <= siz) 152. Sprintf(buf, ", while %s", multi_reason); 153. /* either multi_reason wasn't specified or wouldn't fit */ 154. else if (sizeof ", while helpless" <= siz) 155. Strcpy(buf, ", while helpless"); 156. /* else extra death info won't fit, so leave it out */ 157. } 158. } 159. 160. STATIC_OVL void 161. topten_print(x) 162. const char *x; 163. { 164. if (toptenwin == WIN_ERR) 165. raw_print(x); 166. else 167. putstr(toptenwin, ATR_NONE, x); 168. } 169. 170. STATIC_OVL void 171. topten_print_bold(x) 172. const char *x; 173. { 174. if (toptenwin == WIN_ERR) 175. raw_print_bold(x); 176. else 177. putstr(toptenwin, ATR_BOLD, x); 178. } 179. 180. int 181. observable_depth(lev) 182. d_level *lev; 183. { 184. #if 0 185. /* if we ever randomize the order of the elemental planes, we 186. must use a constant external representation in the record file */ 187. if (In_endgame(lev)) { 188. if (Is_astralevel(lev)) 189. return -5; 190. else if (Is_waterlevel(lev)) 191. return -4; 192. else if (Is_firelevel(lev)) 193. return -3; 194. else if (Is_airlevel(lev)) 195. return -2; 196. else if (Is_earthlevel(lev)) 197. return -1; 198. else 199. return 0; /* ? */ 200. } else 201. #endif 202. return depth(lev); 203. } 204. 205. /* throw away characters until current record has been entirely consumed */ 206. STATIC_OVL void 207. discardexcess(rfile) 208. FILE *rfile; 209. { 210. int c; 211. 212. do { 213. c = fgetc(rfile); 214. } while (c != '\n' && c != EOF); 215. } 216. 217. STATIC_OVL void 218. readentry(rfile, tt) 219. FILE *rfile; 220. struct toptenentry *tt; 221. { 222. char inbuf[SCANBUFSZ], s1[SCANBUFSZ], s2[SCANBUFSZ], s3[SCANBUFSZ], 223. s4[SCANBUFSZ], s5[SCANBUFSZ], s6[SCANBUFSZ]; 224. 225. #ifdef NO_SCAN_BRACK /* Version_ Pts DgnLevs_ Hp___ Died__Born id */ 226. static const char fmt[] = "%d %d %d %ld %d %d %d %d %d %d %ld %ld %d%*c"; 227. static const char fmt32[] = "%c%c %s %s%*c"; 228. static const char fmt33[] = "%s %s %s %s %s %s%*c"; 229. #else 230. static const char fmt[] = "%d.%d.%d %ld %d %d %d %d %d %d %ld %ld %d "; 231. static const char fmt32[] = "%c%c %[^,],%[^\n]%*c"; 232. static const char fmt33[] = "%s %s %s %s %[^,],%[^\n]%*c"; 233. #endif 234. 235. #ifdef UPDATE_RECORD_IN_PLACE 236. /* note: input below must read the record's terminating newline */ 237. final_fpos = tt->fpos = ftell(rfile); 238. #endif 239. #define TTFIELDS 13 240. if (fscanf(rfile, fmt, &tt->ver_major, &tt->ver_minor, &tt->patchlevel, 241. &tt->points, &tt->deathdnum, &tt->deathlev, &tt->maxlvl, 242. &tt->hp, &tt->maxhp, &tt->deaths, &tt->deathdate, 243. &tt->birthdate, &tt->uid) != TTFIELDS) { 244. #undef TTFIELDS 245. tt->points = 0; 246. discardexcess(rfile); 247. } else { 248. /* load remainder of record into a local buffer; 249. this imposes an implicit length limit of SCANBUFSZ 250. on every string field extracted from the buffer */ 251. if (!fgets(inbuf, sizeof inbuf, rfile)) { 252. /* sscanf will fail and tt->points will be set to 0 */ 253. *inbuf = '\0'; 254. } else if (!index(inbuf, '\n')) { 255. Strcpy(&inbuf[sizeof inbuf - 2], "\n"); 256. discardexcess(rfile); 257. } 258. /* Check for backwards compatibility */ 259. if (tt->ver_major < 3 || (tt->ver_major == 3 && tt->ver_minor < 3)) { 260. int i; 261. 262. if (sscanf(inbuf, fmt32, tt->plrole, tt->plgend, s1, s2) == 4) { 263. tt->plrole[1] = tt->plgend[1] = '\0'; /* read via %c */ 264. copynchars(tt->name, s1, (int) (sizeof tt->name) - 1); 265. copynchars(tt->death, s2, (int) (sizeof tt->death) - 1); 266. } else 267. tt->points = 0; 268. tt->plrole[1] = '\0'; 269. if ((i = str2role(tt->plrole)) >= 0) 270. Strcpy(tt->plrole, roles[i].filecode); 271. Strcpy(tt->plrace, "?"); 272. Strcpy(tt->plgend, (tt->plgend[0] == 'M') ? "Mal" : "Fem"); 273. Strcpy(tt->plalign, "?"); 274. } else if (sscanf(inbuf, fmt33, s1, s2, s3, s4, s5, s6) == 6) { 275. copynchars(tt->plrole, s1, (int) (sizeof tt->plrole) - 1); 276. copynchars(tt->plrace, s2, (int) (sizeof tt->plrace) - 1); 277. copynchars(tt->plgend, s3, (int) (sizeof tt->plgend) - 1); 278. copynchars(tt->plalign, s4, (int) (sizeof tt->plalign) - 1); 279. copynchars(tt->name, s5, (int) (sizeof tt->name) - 1); 280. copynchars(tt->death, s6, (int) (sizeof tt->death) - 1); 281. } else 282. tt->points = 0; 283. #ifdef NO_SCAN_BRACK 284. if (tt->points > 0) { 285. nsb_unmung_line(tt->name); 286. nsb_unmung_line(tt->death); 287. } 288. #endif 289. } 290. 291. /* check old score entries for Y2K problem and fix whenever found */ 292. if (tt->points > 0) { 293. if (tt->birthdate < 19000000L) 294. tt->birthdate += 19000000L; 295. if (tt->deathdate < 19000000L) 296. tt->deathdate += 19000000L; 297. } 298. } 299. 300. STATIC_OVL void 301. writeentry(rfile, tt) 302. FILE *rfile; 303. struct toptenentry *tt; 304. { 305. static const char fmt32[] = "%c%c "; /* role,gender */ 306. static const char fmt33[] = "%s %s %s %s "; /* role,race,gndr,algn */ 307. #ifndef NO_SCAN_BRACK 308. static const char fmt0[] = "%d.%d.%d %ld %d %d %d %d %d %d %ld %ld %d "; 309. static const char fmtX[] = "%s,%s\n"; 310. #else /* NO_SCAN_BRACK */ 311. static const char fmt0[] = "%d %d %d %ld %d %d %d %d %d %d %ld %ld %d "; 312. static const char fmtX[] = "%s %s\n"; 313. 314. nsb_mung_line(tt->name); 315. nsb_mung_line(tt->death); 316. #endif 317. 318. (void) fprintf(rfile, fmt0, tt->ver_major, tt->ver_minor, tt->patchlevel, 319. tt->points, tt->deathdnum, tt->deathlev, tt->maxlvl, 320. tt->hp, tt->maxhp, tt->deaths, tt->deathdate, 321. tt->birthdate, tt->uid); 322. if (tt->ver_major < 3 || (tt->ver_major == 3 && tt->ver_minor < 3)) 323. (void) fprintf(rfile, fmt32, tt->plrole[0], tt->plgend[0]); 324. else 325. (void) fprintf(rfile, fmt33, tt->plrole, tt->plrace, tt->plgend, 326. tt->plalign); 327. (void) fprintf(rfile, fmtX, onlyspace(tt->name) ? "_" : tt->name, 328. tt->death); 329. 330. #ifdef NO_SCAN_BRACK 331. nsb_unmung_line(tt->name); 332. nsb_unmung_line(tt->death); 333. #endif 334. } 335. 336. #ifdef XLOGFILE 337. 338. /* as tab is never used in eg. plname or death, no need to mangle those. */ 339. STATIC_OVL void 340. writexlentry(rfile, tt, how) 341. FILE *rfile; 342. struct toptenentry *tt; 343. int how; 344. { 345. #define Fprintf (void) fprintf 346. #define XLOG_SEP '\t' /* xlogfile field separator. */ 347. char buf[BUFSZ], tmpbuf[DTHSZ + 1]; 348. 349. Sprintf(buf, "version=%d.%d.%d", tt->ver_major, tt->ver_minor, 350. tt->patchlevel); 351. Sprintf(eos(buf), "%cpoints=%ld%cdeathdnum=%d%cdeathlev=%d", XLOG_SEP, 352. tt->points, XLOG_SEP, tt->deathdnum, XLOG_SEP, tt->deathlev); 353. Sprintf(eos(buf), "%cmaxlvl=%d%chp=%d%cmaxhp=%d", XLOG_SEP, tt->maxlvl, 354. XLOG_SEP, tt->hp, XLOG_SEP, tt->maxhp); 355. Sprintf(eos(buf), "%cdeaths=%d%cdeathdate=%ld%cbirthdate=%ld%cuid=%d", 356. XLOG_SEP, tt->deaths, XLOG_SEP, tt->deathdate, XLOG_SEP, 357. tt->birthdate, XLOG_SEP, tt->uid); 358. Fprintf(rfile, "%s", buf); 359. Sprintf(buf, "%crole=%s%crace=%s%cgender=%s%calign=%s", XLOG_SEP, 360. tt->plrole, XLOG_SEP, tt->plrace, XLOG_SEP, tt->plgend, XLOG_SEP, 361. tt->plalign); 362. /* make a copy of death reason that doesn't include ", while helpless" */ 363. formatkiller(tmpbuf, sizeof tmpbuf, how, FALSE); 364. Fprintf(rfile, "%s%cname=%s%cdeath=%s", 365. buf, /* (already includes separator) */ 366. XLOG_SEP, plname, XLOG_SEP, tmpbuf); 367. if (multi) 368. Fprintf(rfile, "%cwhile=%s", XLOG_SEP, 369. multi_reason ? multi_reason : "helpless"); 370. Fprintf(rfile, "%cconduct=0x%lx%cturns=%ld%cachieve=0x%lx", XLOG_SEP, 371. encodeconduct(), XLOG_SEP, moves, XLOG_SEP, encodeachieve()); 372. Fprintf(rfile, "%crealtime=%ld%cstarttime=%ld%cendtime=%ld", XLOG_SEP, 373. (long) urealtime.realtime, XLOG_SEP, 374. (long) ubirthday, XLOG_SEP, (long) urealtime.finish_time); 375. Fprintf(rfile, "%cgender0=%s%calign0=%s", XLOG_SEP, 376. genders[flags.initgend].filecode, XLOG_SEP, 377. aligns[1 - u.ualignbase[A_ORIGINAL]].filecode); 378. Fprintf(rfile, "%cflags=0x%lx", XLOG_SEP, encodexlogflags()); 379. Fprintf(rfile, "\n"); 380. #undef XLOG_SEP 381. } 382. 383. STATIC_OVL long 384. encodexlogflags() 385. { 386. long e = 0L; 387. 388. if (wizard) 389. e |= 1L << 0; 390. if (discover) 391. e |= 1L << 1; 392. if (!u.uroleplay.numbones) 393. e |= 1L << 2; 394. 395. return e; 396. } 397. 398. STATIC_OVL long 399. encodeconduct() 400. { 401. long e = 0L; 402. 403. if (!u.uconduct.food) 404. e |= 1L << 0; 405. if (!u.uconduct.unvegan) 406. e |= 1L << 1; 407. if (!u.uconduct.unvegetarian) 408. e |= 1L << 2; 409. if (!u.uconduct.gnostic) 410. e |= 1L << 3; 411. if (!u.uconduct.weaphit) 412. e |= 1L << 4; 413. if (!u.uconduct.killer) 414. e |= 1L << 5; 415. if (!u.uconduct.literate) 416. e |= 1L << 6; 417. if (!u.uconduct.polypiles) 418. e |= 1L << 7; 419. if (!u.uconduct.polyselfs) 420. e |= 1L << 8; 421. if (!u.uconduct.wishes) 422. e |= 1L << 9; 423. if (!u.uconduct.wisharti) 424. e |= 1L << 10; 425. if (!num_genocides()) 426. e |= 1L << 11; 427. 428. return e; 429. } 430. 431. STATIC_OVL long 432. encodeachieve() 433. { 434. long r = 0L; 435. 436. if (u.uachieve.bell) 437. r |= 1L << 0; 438. if (u.uachieve.enter_gehennom) 439. r |= 1L << 1; 440. if (u.uachieve.menorah) 441. r |= 1L << 2; 442. if (u.uachieve.book) 443. r |= 1L << 3; 444. if (u.uevent.invoked) 445. r |= 1L << 4; 446. if (u.uachieve.amulet) 447. r |= 1L << 5; 448. if (In_endgame(&u.uz)) 449. r |= 1L << 6; 450. if (Is_astralevel(&u.uz)) 451. r |= 1L << 7; 452. if (u.uachieve.ascended) 453. r |= 1L << 8; 454. if (u.uachieve.mines_luckstone) 455. r |= 1L << 9; 456. if (u.uachieve.finish_sokoban) 457. r |= 1L << 10; 458. if (u.uachieve.killed_medusa) 459. r |= 1L << 11; 460. if (u.uroleplay.blind) 461. r |= 1L << 12; 462. if (u.uroleplay.nudist) 463. r |= 1L << 13; 464. 465. return r; 466. } 467. 468. #endif /* XLOGFILE */ 469. 470. STATIC_OVL void 471. free_ttlist(tt) 472. struct toptenentry *tt; 473. { 474. struct toptenentry *ttnext; 475. 476. while (tt->points > 0) { 477. ttnext = tt->tt_next; 478. dealloc_ttentry(tt); 479. tt = ttnext; 480. } 481. dealloc_ttentry(tt); 482. } 483. 484. void 485. topten(how, when) 486. int how; 487. time_t when; 488. { 489. int uid = getuid(); 490. int rank, rank0 = -1, rank1 = 0; 491. int occ_cnt = sysopt.persmax; 492. register struct toptenentry *t0, *tprev; 493. struct toptenentry *t1; 494. FILE *rfile; 495. register int flg = 0; 496. boolean t0_used; 497. #ifdef LOGFILE 498. FILE *lfile; 499. #endif /* LOGFILE */ 500. #ifdef XLOGFILE 501. FILE *xlfile; 502. #endif /* XLOGFILE */ 503. 504. #ifdef _DCC 505. /* Under DICE 3.0, this crashes the system consistently, apparently due to 506. * corruption of *rfile somewhere. Until I figure this out, just cut out 507. * topten support entirely - at least then the game exits cleanly. --AC 508. */ 509. return; 510. #endif 511. 512. /* If we are in the midst of a panic, cut out topten entirely. 513. * topten uses alloc() several times, which will lead to 514. * problems if the panic was the result of an alloc() failure. 515. */ 516. if (program_state.panicking) 517. return; 518. 519. if (iflags.toptenwin) { 520. toptenwin = create_nhwindow(NHW_TEXT); 521. } 522. 523. #if defined(UNIX) || defined(VMS) || defined(__EMX__) 524. #define HUP if (!program_state.done_hup) 525. #else 526. #define HUP 527. #endif 528. 529. #ifdef TOS 530. restore_colors(); /* make sure the screen is black on white */ 531. #endif 532. /* create a new 'topten' entry */ 533. t0_used = FALSE; 534. t0 = newttentry(); 535. t0->ver_major = VERSION_MAJOR; 536. t0->ver_minor = VERSION_MINOR; 537. t0->patchlevel = PATCHLEVEL; 538. t0->points = u.urexp; 539. t0->deathdnum = u.uz.dnum; 540. /* deepest_lev_reached() is in terms of depth(), and reporting the 541. * deepest level reached in the dungeon death occurred in doesn't 542. * seem right, so we have to report the death level in depth() terms 543. * as well (which also seems reasonable since that's all the player 544. * sees on the screen anyway) 545. */ 546. t0->deathlev = observable_depth(&u.uz); 547. t0->maxlvl = deepest_lev_reached(TRUE); 548. t0->hp = u.uhp; 549. t0->maxhp = u.uhpmax; 550. t0->deaths = u.umortality; 551. t0->uid = uid; 552. copynchars(t0->plrole, urole.filecode, ROLESZ); 553. copynchars(t0->plrace, urace.filecode, ROLESZ); 554. copynchars(t0->plgend, genders[flags.female].filecode, ROLESZ); 555. copynchars(t0->plalign, aligns[1 - u.ualign.type].filecode, ROLESZ); 556. copynchars(t0->name, plname, NAMSZ); 557. formatkiller(t0->death, sizeof t0->death, how, TRUE); 558. t0->birthdate = yyyymmdd(ubirthday); 559. t0->deathdate = yyyymmdd(when); 560. t0->tt_next = 0; 561. #ifdef UPDATE_RECORD_IN_PLACE 562. t0->fpos = -1L; 563. #endif 564. 565. #ifdef LOGFILE /* used for debugging (who dies of what, where) */ 566. if (lock_file(LOGFILE, SCOREPREFIX, 10)) { 567. if (!(lfile = fopen_datafile(LOGFILE, "a", SCOREPREFIX))) { 568. HUP raw_print("Cannot open log file!"); 569. } else { 570. writeentry(lfile, t0); 571. (void) fclose(lfile); 572. } 573. unlock_file(LOGFILE); 574. } 575. #endif /* LOGFILE */ 576. #ifdef XLOGFILE 577. if (lock_file(XLOGFILE, SCOREPREFIX, 10)) { 578. if (!(xlfile = fopen_datafile(XLOGFILE, "a", SCOREPREFIX))) { 579. HUP raw_print("Cannot open extended log file!"); 580. } else { 581. writexlentry(xlfile, t0, how); 582. (void) fclose(xlfile); 583. } 584. unlock_file(XLOGFILE); 585. } 586. #endif /* XLOGFILE */ 587. 588. if (wizard || discover) { 589. if (how != PANICKED) 590. HUP { 591. char pbuf[BUFSZ]; 592. 593. topten_print(""); 594. Sprintf(pbuf, 595. "Since you were in %s mode, the score list will not be checked.", 596. wizard ? "wizard" : "discover"); 597. topten_print(pbuf); 598. } 599. goto showwin; 600. } 601. 602. if (!lock_file(RECORD, SCOREPREFIX, 60)) 603. goto destroywin; 604. 605. #ifdef UPDATE_RECORD_IN_PLACE 606. rfile = fopen_datafile(RECORD, "r+", SCOREPREFIX); 607. #else 608. rfile = fopen_datafile(RECORD, "r", SCOREPREFIX); 609. #endif 610. 611. if (!rfile) { 612. HUP raw_print("Cannot open record file!"); 613. unlock_file(RECORD); 614. goto destroywin; 615. } 616. 617. HUP topten_print(""); 618. 619. /* assure minimum number of points */ 620. if (t0->points < sysopt.pointsmin) 621. t0->points = 0; 622. 623. t1 = tt_head = newttentry(); 624. tprev = 0; 625. /* rank0: -1 undefined, 0 not_on_list, n n_th on list */ 626. for (rank = 1;;) { 627. readentry(rfile, t1); 628. if (t1->points < sysopt.pointsmin) 629. t1->points = 0; 630. if (rank0 < 0 && t1->points < t0->points) { 631. rank0 = rank++; 632. if (tprev == 0) 633. tt_head = t0; 634. else 635. tprev->tt_next = t0; 636. t0->tt_next = t1; 637. #ifdef UPDATE_RECORD_IN_PLACE 638. t0->fpos = t1->fpos; /* insert here */ 639. #endif 640. t0_used = TRUE; 641. occ_cnt--; 642. flg++; /* ask for a rewrite */ 643. } else 644. tprev = t1; 645. 646. if (t1->points == 0) 647. break; 648. if ((sysopt.pers_is_uid ? t1->uid == t0->uid 649. : strncmp(t1->name, t0->name, NAMSZ) == 0) 650. && !strncmp(t1->plrole, t0->plrole, ROLESZ) && --occ_cnt <= 0) { 651. if (rank0 < 0) { 652. rank0 = 0; 653. rank1 = rank; 654. HUP { 655. char pbuf[BUFSZ]; 656. 657. Sprintf(pbuf, 658. "You didn't beat your previous score of %ld points.", 659. t1->points); 660. topten_print(pbuf); 661. topten_print(""); 662. } 663. } 664. if (occ_cnt < 0) { 665. flg++; 666. continue; 667. } 668. } 669. if (rank <= sysopt.entrymax) { 670. t1->tt_next = newttentry(); 671. t1 = t1->tt_next; 672. rank++; 673. } 674. if (rank > sysopt.entrymax) { 675. t1->points = 0; 676. break; 677. } 678. } 679. if (flg) { /* rewrite record file */ 680. #ifdef UPDATE_RECORD_IN_PLACE 681. (void) fseek(rfile, (t0->fpos >= 0 ? t0->fpos : final_fpos), 682. SEEK_SET); 683. #else 684. (void) fclose(rfile); 685. if (!(rfile = fopen_datafile(RECORD, "w", SCOREPREFIX))) { 686. HUP raw_print("Cannot write record file"); 687. unlock_file(RECORD); 688. free_ttlist(tt_head); 689. goto destroywin; 690. } 691. #endif /* UPDATE_RECORD_IN_PLACE */ 692. if (!done_stopprint) 693. if (rank0 > 0) { 694. if (rank0 <= 10) { 695. topten_print("You made the top ten list!"); 696. } else { 697. char pbuf[BUFSZ]; 698. 699. Sprintf(pbuf, 700. "You reached the %d%s place on the top %d list.", 701. rank0, ordin(rank0), sysopt.entrymax); 702. topten_print(pbuf); 703. } 704. topten_print(""); 705. } 706. } 707. if (rank0 == 0) 708. rank0 = rank1; 709. if (rank0 <= 0) 710. rank0 = rank; 711. if (!done_stopprint) 712. outheader(); 713. t1 = tt_head; 714. for (rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) { 715. if (flg 716. #ifdef UPDATE_RECORD_IN_PLACE 717. && rank >= rank0 718. #endif 719. ) 720. writeentry(rfile, t1); 721. if (done_stopprint) 722. continue; 723. if (rank > flags.end_top && (rank < rank0 - flags.end_around 724. || rank > rank0 + flags.end_around) 725. && (!flags.end_own 726. || (sysopt.pers_is_uid 727. ? t1->uid == t0->uid 728. : strncmp(t1->name, t0->name, NAMSZ) == 0))) 729. continue; 730. if (rank == rank0 - flags.end_around 731. && rank0 > flags.end_top + flags.end_around + 1 && !flags.end_own) 732. topten_print(""); 733. if (rank != rank0) 734. outentry(rank, t1, FALSE); 735. else if (!rank1) 736. outentry(rank, t1, TRUE); 737. else { 738. outentry(rank, t1, TRUE); 739. outentry(0, t0, TRUE); 740. } 741. } 742. if (rank0 >= rank) 743. if (!done_stopprint) 744. outentry(0, t0, TRUE); 745. #ifdef UPDATE_RECORD_IN_PLACE 746. if (flg) { 747. #ifdef TRUNCATE_FILE 748. /* if a reasonable way to truncate a file exists, use it */ 749. truncate_file(rfile); 750. #else 751. /* use sentinel record rather than relying on truncation */ 752. t1->points = 0L; /* terminates file when read back in */ 753. t1->ver_major = t1->ver_minor = t1->patchlevel = 0; 754. t1->uid = t1->deathdnum = t1->deathlev = 0; 755. t1->maxlvl = t1->hp = t1->maxhp = t1->deaths = 0; 756. t1->plrole[0] = t1->plrace[0] = t1->plgend[0] = t1->plalign[0] = '-'; 757. t1->plrole[1] = t1->plrace[1] = t1->plgend[1] = t1->plalign[1] = 0; 758. t1->birthdate = t1->deathdate = yyyymmdd((time_t) 0L); 759. Strcpy(t1->name, "@"); 760. Strcpy(t1->death, "<eod>\n"); 761. writeentry(rfile, t1); 762. (void) fflush(rfile); 763. #endif /* TRUNCATE_FILE */ 764. } 765. #endif /* UPDATE_RECORD_IN_PLACE */ 766. (void) fclose(rfile); 767. unlock_file(RECORD); 768. free_ttlist(tt_head); 769. 770. showwin: 771. if (iflags.toptenwin && !done_stopprint) 772. display_nhwindow(toptenwin, 1); 773. destroywin: 774. if (!t0_used) 775. dealloc_ttentry(t0); 776. if (iflags.toptenwin) { 777. destroy_nhwindow(toptenwin); 778. toptenwin = WIN_ERR; 779. } 780. } 781. 782. STATIC_OVL void 783. outheader() 784. { 785. char linebuf[BUFSZ]; 786. register char *bp; 787. 788. Strcpy(linebuf, " No Points Name"); 789. bp = eos(linebuf); 790. while (bp < linebuf + COLNO - 9) 791. *bp++ = ' '; 792. Strcpy(bp, "Hp [max]"); 793. topten_print(linebuf); 794. } 795. 796. /* so>0: standout line; so=0: ordinary line */ 797. STATIC_OVL void 798. outentry(rank, t1, so) 799. struct toptenentry *t1; 800. int rank; 801. boolean so; 802. { 803. boolean second_line = TRUE; 804. char linebuf[BUFSZ]; 805. char *bp, hpbuf[24], linebuf3[BUFSZ]; 806. int hppos, lngr; 807. 808. linebuf[0] = '\0'; 809. if (rank) 810. Sprintf(eos(linebuf), "%3d", rank); 811. else 812. Strcat(linebuf, " "); 813. 814. Sprintf(eos(linebuf), " %10ld %.10s", t1->points ? t1->points : u.urexp, 815. t1->name); 816. Sprintf(eos(linebuf), "-%s", t1->plrole); 817. if (t1->plrace[0] != '?') 818. Sprintf(eos(linebuf), "-%s", t1->plrace); 819. /* Printing of gender and alignment is intentional. It has been 820. * part of the NetHack Geek Code, and illustrates a proper way to 821. * specify a character from the command line. 822. */ 823. Sprintf(eos(linebuf), "-%s", t1->plgend); 824. if (t1->plalign[0] != '?') 825. Sprintf(eos(linebuf), "-%s ", t1->plalign); 826. else 827. Strcat(linebuf, " "); 828. if (!strncmp("escaped", t1->death, 7)) { 829. Sprintf(eos(linebuf), "escaped the dungeon %s[max level %d]", 830. !strncmp(" (", t1->death + 7, 2) ? t1->death + 7 + 2 : "", 831. t1->maxlvl); 832. /* fixup for closing paren in "escaped... with...Amulet)[max..." */ 833. if ((bp = index(linebuf, ')')) != 0) 834. *bp = (t1->deathdnum == astral_level.dnum) ? '\0' : ' '; 835. second_line = FALSE; 836. } else if (!strncmp("ascended", t1->death, 8)) { 837. Sprintf(eos(linebuf), "ascended to demigod%s-hood", 838. (t1->plgend[0] == 'F') ? "dess" : ""); 839. second_line = FALSE; 840. } else { 841. if (!strncmp(t1->death, "quit", 4)) { 842. Strcat(linebuf, "quit"); 843. second_line = FALSE; 844. } else if (!strncmp(t1->death, "died of st", 10)) { 845. Strcat(linebuf, "starved to death"); 846. second_line = FALSE; 847. } else if (!strncmp(t1->death, "choked", 6)) { 848. Sprintf(eos(linebuf), "choked on h%s food", 849. (t1->plgend[0] == 'F') ? "er" : "is"); 850. } else if (!strncmp(t1->death, "poisoned", 8)) { 851. Strcat(linebuf, "was poisoned"); 852. } else if (!strncmp(t1->death, "crushed", 7)) { 853. Strcat(linebuf, "was crushed to death"); 854. } else if (!strncmp(t1->death, "petrified by ", 13)) { 855. Strcat(linebuf, "turned to stone"); 856. } else 857. Strcat(linebuf, "died"); 858. 859. if (t1->deathdnum == astral_level.dnum) { 860. const char *arg, *fmt = " on the Plane of %s"; 861. 862. switch (t1->deathlev) { 863. case -5: 864. fmt = " on the %s Plane"; 865. arg = "Astral"; 866. break; 867. case -4: 868. arg = "Water"; 869. break; 870. case -3: 871. arg = "Fire"; 872. break; 873. case -2: 874. arg = "Air"; 875. break; 876. case -1: 877. arg = "Earth"; 878. break; 879. default: 880. arg = "Void"; 881. break; 882. } 883. Sprintf(eos(linebuf), fmt, arg); 884. } else { 885. Sprintf(eos(linebuf), " in %s", dungeons[t1->deathdnum].dname); 886. if (t1->deathdnum != knox_level.dnum) 887. Sprintf(eos(linebuf), " on level %d", t1->deathlev); 888. if (t1->deathlev != t1->maxlvl) 889. Sprintf(eos(linebuf), " [max %d]", t1->maxlvl); 890. } 891. 892. /* kludge for "quit while already on Charon's boat" */ 893. if (!strncmp(t1->death, "quit ", 5)) 894. Strcat(linebuf, t1->death + 4); 895. } 896. Strcat(linebuf, "."); 897. 898. /* Quit, starved, ascended, and escaped contain no second line */ 899. if (second_line) 900. Sprintf(eos(linebuf), " %c%s.", highc(*(t1->death)), t1->death + 1); 901. 902. lngr = (int) strlen(linebuf); 903. if (t1->hp <= 0) 904. hpbuf[0] = '-', hpbuf[1] = '\0'; 905. else 906. Sprintf(hpbuf, "%d", t1->hp); 907. /* beginning of hp column after padding (not actually padded yet) */ 908. hppos = COLNO - (sizeof(" Hp [max]") - 1); /* sizeof(str) includes \0 */ 909. while (lngr >= hppos) { 910. for (bp = eos(linebuf); !(*bp == ' ' && (bp - linebuf < hppos)); bp--) 911. ; 912. /* special case: word is too long, wrap in the middle */ 913. if (linebuf + 15 >= bp) 914. bp = linebuf + hppos - 1; 915. /* special case: if about to wrap in the middle of maximum 916. dungeon depth reached, wrap in front of it instead */ 917. if (bp > linebuf + 5 && !strncmp(bp - 5, " [max", 5)) 918. bp -= 5; 919. if (*bp != ' ') 920. Strcpy(linebuf3, bp); 921. else 922. Strcpy(linebuf3, bp + 1); 923. *bp = 0; 924. if (so) { 925. while (bp < linebuf + (COLNO - 1)) 926. *bp++ = ' '; 927. *bp = 0; 928. topten_print_bold(linebuf); 929. } else 930. topten_print(linebuf); 931. Sprintf(linebuf, "%15s %s", "", linebuf3); 932. lngr = strlen(linebuf); 933. } 934. /* beginning of hp column not including padding */ 935. hppos = COLNO - 7 - (int) strlen(hpbuf); 936. bp = eos(linebuf); 937. 938. if (bp <= linebuf + hppos) { 939. /* pad any necessary blanks to the hit point entry */ 940. while (bp < linebuf + hppos) 941. *bp++ = ' '; 942. Strcpy(bp, hpbuf); 943. Sprintf(eos(bp), " %s[%d]", 944. (t1->maxhp < 10) ? " " : (t1->maxhp < 100) ? " " : "", 945. t1->maxhp); 946. } 947. 948. if (so) { 949. bp = eos(linebuf); 950. if (so >= COLNO) 951. so = COLNO - 1; 952. while (bp < linebuf + so) 953. *bp++ = ' '; 954. *bp = 0; 955. topten_print_bold(linebuf); 956. } else 957. topten_print(linebuf); 958. } 959. 960. STATIC_OVL int 961. score_wanted(current_ver, rank, t1, playerct, players, uid) 962. boolean current_ver; 963. int rank; 964. struct toptenentry *t1; 965. int playerct; 966. const char **players; 967. int uid; 968. { 969. int i; 970. 971. if (current_ver 972. && (t1->ver_major != VERSION_MAJOR || t1->ver_minor != VERSION_MINOR 973. || t1->patchlevel != PATCHLEVEL)) 974. return 0; 975. 976. if (sysopt.pers_is_uid && !playerct && t1->uid == uid) 977. return 1; 978. 979. for (i = 0; i < playerct; i++) { 980. if (players[i][0] == '-' && index("pr", players[i][1]) 981. && players[i][2] == 0 && i + 1 < playerct) { 982. const char *arg = players[i + 1]; 983. if ((players[i][1] == 'p' 984. && str2role(arg) == str2role(t1->plrole)) 985. || (players[i][1] == 'r' 986. && str2race(arg) == str2race(t1->plrace))) 987. return 1; 988. i++; 989. } else if (strcmp(players[i], "all") == 0 990. || strncmp(t1->name, players[i], NAMSZ) == 0 991. || (players[i][0] == '-' && players[i][1] == t1->plrole[0] 992. && players[i][2] == 0) 993. || (digit(players[i][0]) && rank <= atoi(players[i]))) 994. return 1; 995. } 996. return 0; 997. } 998. 999. /* 1000. * print selected parts of score list. 1001. * argc >= 2, with argv[0] untrustworthy (directory names, et al.), 1002. * and argv[1] starting with "-s". 1003. */ 1004. void 1005. prscore(argc, argv) 1006. int argc; 1007. char **argv; 1008. { 1009. const char **players; 1010. int playerct, rank; 1011. boolean current_ver = TRUE, init_done = FALSE; 1012. register struct toptenentry *t1; 1013. FILE *rfile; 1014. boolean match_found = FALSE; 1015. register int i; 1016. char pbuf[BUFSZ]; 1017. int uid = -1; 1018. const char *player0; 1019. 1020. if (argc < 2 || strncmp(argv[1], "-s", 2)) { 1021. raw_printf("prscore: bad arguments (%d)", argc); 1022. return; 1023. } 1024. 1025. rfile = fopen_datafile(RECORD, "r", SCOREPREFIX); 1026. if (!rfile) { 1027. raw_print("Cannot open record file!"); 1028. return; 1029. } 1030. 1031. #ifdef AMIGA 1032. { 1033. extern winid amii_rawprwin; 1034. 1035. init_nhwindows(&argc, argv); 1036. amii_rawprwin = create_nhwindow(NHW_TEXT); 1037. } 1038. #endif 1039. 1040. /* If the score list isn't after a game, we never went through 1041. * initialization. */ 1042. if (wiz1_level.dlevel == 0) { 1043. dlb_init(); 1044. init_dungeons(); 1045. init_done = TRUE; 1046. } 1047. 1048. if (!argv[1][2]) { /* plain "-s" */ 1049. argc--; 1050. argv++; 1051. } else 1052. argv[1] += 2; 1053. 1054. if (argc > 1 && !strcmp(argv[1], "-v")) { 1055. current_ver = FALSE; 1056. argc--; 1057. argv++; 1058. } 1059. 1060. if (argc <= 1) { 1061. if (sysopt.pers_is_uid) { 1062. uid = getuid(); 1063. playerct = 0; 1064. players = (const char **) 0; 1065. } else { 1066. player0 = plname; 1067. if (!*player0) 1068. #ifdef AMIGA 1069. player0 = "all"; /* single user system */ 1070. #else 1071. player0 = "hackplayer"; 1072. #endif 1073. playerct = 1; 1074. players = &player0; 1075. } 1076. } else { 1077. playerct = --argc; 1078. players = (const char **) ++argv; 1079. } 1080. raw_print(""); 1081. 1082. t1 = tt_head = newttentry(); 1083. for (rank = 1;; rank++) { 1084. readentry(rfile, t1); 1085. if (t1->points == 0) 1086. break; 1087. if (!match_found 1088. && score_wanted(current_ver, rank, t1, playerct, players, uid)) 1089. match_found = TRUE; 1090. t1->tt_next = newttentry(); 1091. t1 = t1->tt_next; 1092. } 1093. 1094. (void) fclose(rfile); 1095. if (init_done) { 1096. free_dungeons(); 1097. dlb_cleanup(); 1098. } 1099. 1100. if (match_found) { 1101. outheader(); 1102. t1 = tt_head; 1103. for (rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) { 1104. if (score_wanted(current_ver, rank, t1, playerct, players, uid)) 1105. (void) outentry(rank, t1, FALSE); 1106. } 1107. } else { 1108. Sprintf(pbuf, "Cannot find any %sentries for ", 1109. current_ver ? "current " : ""); 1110. if (playerct < 1) 1111. Strcat(pbuf, "you."); 1112. else { 1113. if (playerct > 1) 1114. Strcat(pbuf, "any of "); 1115. for (i = 0; i < playerct; i++) { 1116. /* stop printing players if there are too many to fit */ 1117. if (strlen(pbuf) + strlen(players[i]) + 2 >= BUFSZ) { 1118. if (strlen(pbuf) < BUFSZ - 4) 1119. Strcat(pbuf, "..."); 1120. else 1121. Strcpy(pbuf + strlen(pbuf) - 4, "..."); 1122. break; 1123. } 1124. Strcat(pbuf, players[i]); 1125. if (i < playerct - 1) { 1126. if (players[i][0] == '-' && index("pr", players[i][1]) 1127. && players[i][2] == 0) 1128. Strcat(pbuf, " "); 1129. else 1130. Strcat(pbuf, ":"); 1131. } 1132. } 1133. } 1134. raw_print(pbuf); 1135. raw_printf("Usage: %s -s [-v] <playertypes> [maxrank] [playernames]", 1136. 1137. hname); 1138. raw_printf("Player types are: [-p role] [-r race]"); 1139. } 1140. free_ttlist(tt_head); 1141. #ifdef AMIGA 1142. { 1143. extern winid amii_rawprwin; 1144. 1145. display_nhwindow(amii_rawprwin, 1); 1146. destroy_nhwindow(amii_rawprwin); 1147. amii_rawprwin = WIN_ERR; 1148. } 1149. #endif 1150. } 1151. 1152. STATIC_OVL int 1153. classmon(plch, fem) 1154. char *plch; 1155. boolean fem; 1156. { 1157. int i; 1158. 1159. /* Look for this role in the role table */ 1160. for (i = 0; roles[i].name.m; i++) 1161. if (!strncmp(plch, roles[i].filecode, ROLESZ)) { 1162. if (fem && roles[i].femalenum != NON_PM) 1163. return roles[i].femalenum; 1164. else if (roles[i].malenum != NON_PM) 1165. return roles[i].malenum; 1166. else 1167. return PM_HUMAN; 1168. } 1169. /* this might be from a 3.2.x score for former Elf class */ 1170. if (!strcmp(plch, "E")) 1171. return PM_RANGER; 1172. 1173. impossible("What weird role is this? (%s)", plch); 1174. return PM_HUMAN_MUMMY; 1175. } 1176. 1177. /* 1178. * Get a random player name and class from the high score list, 1179. */ 1180. struct toptenentry * 1181. get_rnd_toptenentry() 1182. { 1183. int rank, i; 1184. FILE *rfile; 1185. register struct toptenentry *tt; 1186. static struct toptenentry tt_buf; 1187. 1188. rfile = fopen_datafile(RECORD, "r", SCOREPREFIX); 1189. if (!rfile) { 1190. impossible("Cannot open record file!"); 1191. return NULL; 1192. } 1193. 1194. tt = &tt_buf; 1195. rank = rnd(sysopt.tt_oname_maxrank); 1196. pickentry: 1197. for (i = rank; i; i--) { 1198. readentry(rfile, tt); 1199. if (tt->points == 0) 1200. break; 1201. } 1202. 1203. if (tt->points == 0) { 1204. if (rank > 1) { 1205. rank = 1; 1206. rewind(rfile); 1207. goto pickentry; 1208. } 1209. tt = NULL; 1210. } 1211. 1212. (void) fclose(rfile); 1213. return tt; 1214. } 1215. 1216. 1217. /* 1218. * Attach random player name and class from high score list 1219. * to an object (for statues or morgue corpses). 1220. */ 1221. struct obj * 1222. tt_oname(otmp) 1223. struct obj *otmp; 1224. { 1225. struct toptenentry *tt; 1226. if (!otmp) 1227. return (struct obj *) 0; 1228. 1229. tt = get_rnd_toptenentry(); 1230. 1231. if (!tt) 1232. return (struct obj *) 0; 1233. 1234. set_corpsenm(otmp, classmon(tt->plrole, (tt->plgend[0] == 'F'))); 1235. otmp = oname(otmp, tt->name); 1236. 1237. return otmp; 1238. } 1239. 1240. #ifdef NO_SCAN_BRACK 1241. /* Lattice scanf isn't up to reading the scorefile. What */ 1242. /* follows deals with that; I admit it's ugly. (KL) */ 1243. /* Now generally available (KL) */ 1244. STATIC_OVL void 1245. nsb_mung_line(p) 1246. char *p; 1247. { 1248. while ((p = index(p, ' ')) != 0) 1249. *p = '|'; 1250. } 1251. 1252. STATIC_OVL void 1253. nsb_unmung_line(p) 1254. char *p; 1255. { 1256. while ((p = index(p, '|')) != 0) 1257. *p = ' '; 1258. } 1259. #endif /* NO_SCAN_BRACK */ 1260. 1261. /*topten.c*/