Source:NetHack 1.4f/topten.c
(Redirected from NetHack 1.4f/topten.c)
Jump to navigation
Jump to search
Below is the full text to topten.c from the source code of NetHack 1.4f. To link to a particular line, write [[NetHack 1.4f/topten.c#line123]], for example.
Warning! This is the source code from an old release. For the latest release, see Source code
Screenshots and source code from Hack are used under the CWI license.
1. /* SCCS Id: @(#)topten.c 1.4 87/08/08 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /* topten.c - version 1.0 */ 4. 5. #include <stdio.h> 6. #include "hack.h" 7. #ifdef GENIX 8. #define void int /* jhn - mod to prevent compiler from bombing */ 9. #endif 10. 11. #define Sprintf (void) sprintf 12. extern char plname[], pl_character[]; 13. extern char *itoa(), *ordin(), *eos(); 14. extern int done_hup, done_stopprint; 15. 16. #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry)) 17. #define NAMSZ 10 18. #define DTHSZ 40 19. #define PERSMAX 3 20. #define POINTSMIN 1 /* must be > 0 */ 21. #define ENTRYMAX 100 /* must be >= 10 */ 22. #ifndef MSDOS 23. #define PERS_IS_UID /* delete for PERSMAX per name; now per uid */ 24. #endif 25. struct toptenentry { 26. struct toptenentry *tt_next; 27. long int points; 28. int level,maxlvl,hp,maxhp; 29. int uid; 30. char plchar; 31. char sex; 32. char name[NAMSZ+1]; 33. char death[DTHSZ+1]; 34. char date[7]; /* yymmdd */ 35. } *tt_head; 36. 37. topten(){ 38. int uid = getuid(); 39. int rank, rank0 = -1, rank1 = 0; 40. int occ_cnt = PERSMAX; 41. register struct toptenentry *t0, *t1, *tprev; 42. char *recfile = RECORD; 43. #ifdef UNIX 44. char *reclock = "record_lock"; 45. #endif 46. int sleepct = 300; 47. FILE *rfile; 48. register flg = 0; 49. extern char *getdate(); 50. #ifndef DGK 51. #define HUP if(!done_hup) 52. #else 53. #define HUP 54. #endif 55. #ifdef UNIX 56. while(link(recfile, reclock) == -1) { 57. HUP perror(reclock); 58. if(!sleepct--) { 59. HUP puts("I give up. Sorry."); 60. HUP puts("Perhaps there is an old record_lock around?"); 61. return; 62. } 63. HUP printf("Waiting for access to record file. (%d)\n", 64. sleepct); 65. HUP (void) fflush(stdout); 66. sleep(1); 67. } 68. #endif 69. if(!(rfile = fopen(recfile,"r"))){ 70. HUP puts("Cannot open record file!"); 71. goto unlock; 72. } 73. HUP (void) putchar('\n'); 74. 75. /* create a new 'topten' entry */ 76. t0 = newttentry(); 77. t0->level = dlevel; 78. t0->maxlvl = maxdlevel; 79. t0->hp = u.uhp; 80. t0->maxhp = u.uhpmax; 81. t0->points = u.urexp; 82. t0->plchar = pl_character[0]; 83. t0->sex = (flags.female ? 'F' : 'M'); 84. t0->uid = uid; 85. (void) strncpy(t0->name, plname, NAMSZ); 86. (t0->name)[NAMSZ] = 0; 87. (void) strncpy(t0->death, killer, DTHSZ); 88. (t0->death)[DTHSZ] = 0; 89. (void) strcpy(t0->date, getdate()); 90. 91. /* assure minimum number of points */ 92. if(t0->points < POINTSMIN) 93. t0->points = 0; 94. 95. t1 = tt_head = newttentry(); 96. tprev = 0; 97. /* rank0: -1 undefined, 0 not_on_list, n n_th on list */ 98. for(rank = 1; ; ) { 99. if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]", 100. t1->date, &t1->uid, 101. &t1->level, &t1->maxlvl, 102. &t1->hp, &t1->maxhp, &t1->points, 103. &t1->plchar, &t1->sex, t1->name, t1->death) != 11 104. || t1->points < POINTSMIN) 105. t1->points = 0; 106. if(rank0 < 0 && t1->points < t0->points) { 107. rank0 = rank++; 108. if(tprev == 0) 109. tt_head = t0; 110. else 111. tprev->tt_next = t0; 112. t0->tt_next = t1; 113. occ_cnt--; 114. flg++; /* ask for a rewrite */ 115. } else 116. tprev = t1; 117. if(t1->points == 0) break; 118. if( 119. #ifdef PERS_IS_UID 120. t1->uid == t0->uid && 121. #else 122. strncmp(t1->name, t0->name, NAMSZ) == 0 && 123. #endif 124. t1->plchar == t0->plchar && --occ_cnt <= 0){ 125. if(rank0 < 0){ 126. rank0 = 0; 127. rank1 = rank; 128. HUP printf("You didn't beat your previous score of %ld points.\n\n", 129. t1->points); 130. } 131. if(occ_cnt < 0){ 132. flg++; 133. continue; 134. } 135. } 136. if(rank <= ENTRYMAX){ 137. t1 = t1->tt_next = newttentry(); 138. rank++; 139. } 140. if(rank > ENTRYMAX){ 141. t1->points = 0; 142. break; 143. } 144. } 145. if(flg) { /* rewrite record file */ 146. (void) fclose(rfile); 147. if(!(rfile = fopen(recfile,"w"))){ 148. HUP puts("Cannot write record file\n"); 149. goto unlock; 150. } 151. 152. if(!done_stopprint) if(rank0 > 0){ 153. if(rank0 <= 10) 154. puts("You made the top ten list!\n"); 155. else 156. printf("You reached the %d%s place on the top %d list.\n\n", 157. rank0, ordin(rank0), ENTRYMAX); 158. } 159. } 160. if(rank0 == 0) rank0 = rank1; 161. if(rank0 <= 0) rank0 = rank; 162. if(!done_stopprint) outheader(); 163. t1 = tt_head; 164. for(rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) { 165. if(flg) fprintf(rfile,"%6s %d %d %d %d %d %ld %c%c %s,%s\n", 166. t1->date, t1->uid, 167. t1->level, t1->maxlvl, 168. t1->hp, t1->maxhp, t1->points, 169. t1->plchar, t1->sex, t1->name, t1->death); 170. if(done_stopprint) continue; 171. if(rank > flags.end_top && 172. (rank < rank0-flags.end_around || rank > rank0+flags.end_around) 173. && (!flags.end_own || 174. #ifdef PERS_IS_UID 175. t1->uid != t0->uid )) 176. #else 177. strncmp(t1->name, t0->name, NAMSZ))) 178. #endif 179. continue; 180. if(rank == rank0-flags.end_around && 181. rank0 > flags.end_top+flags.end_around+1 && 182. !flags.end_own) 183. (void) putchar('\n'); 184. if(rank != rank0) 185. (void) outentry(rank, t1, 0); 186. else if(!rank1) 187. (void) outentry(rank, t1, 1); 188. else { 189. int t0lth = outentry(0, t0, -1); 190. int t1lth = outentry(rank, t1, t0lth); 191. if(t1lth > t0lth) t0lth = t1lth; 192. (void) outentry(0, t0, t0lth); 193. } 194. } 195. if(rank0 >= rank) if(!done_stopprint) 196. (void) outentry(0, t0, 1); 197. (void) fclose(rfile); 198. unlock: ; 199. #ifdef UNIX 200. (void) unlink(reclock); 201. #endif 202. } 203. 204. outheader() { 205. char linebuf[BUFSZ]; 206. register char *bp; 207. (void) strcpy(linebuf, "Number Points Name"); 208. bp = eos(linebuf); 209. while(bp < linebuf + COLNO - 9) *bp++ = ' '; 210. (void) strcpy(bp, "Hp [max]"); 211. puts(linebuf); 212. } 213. 214. /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */ 215. int 216. outentry(rank,t1,so) register struct toptenentry *t1; { 217. boolean quit = FALSE, killed = FALSE, starv = FALSE; 218. char linebuf[BUFSZ]; 219. linebuf[0] = 0; 220. if(rank) Sprintf(eos(linebuf), "%3d", rank); 221. else Sprintf(eos(linebuf), " "); 222. #ifdef DGKMOD 223. Sprintf(eos(linebuf), " %6ld %10s", t1->points, t1->name); 224. #else 225. Sprintf(eos(linebuf), " %6ld %8s", t1->points, t1->name); 226. #endif 227. if(t1->plchar == 'X') Sprintf(eos(linebuf), " "); 228. else Sprintf(eos(linebuf), "-%c ", t1->plchar); 229. if(!strncmp("escaped", t1->death, 7)) { 230. if(!strcmp(" (with amulet)", t1->death+7)) 231. Sprintf(eos(linebuf), "escaped the dungeon with amulet"); 232. else 233. Sprintf(eos(linebuf), "escaped the dungeon [max level %d]", 234. t1->maxlvl); 235. } else { 236. if(!strncmp(t1->death,"quit",4)) { 237. quit = TRUE; 238. if(t1->maxhp < 3*t1->hp && t1->maxlvl < 4) 239. Sprintf(eos(linebuf), "cravenly gave up"); 240. else 241. Sprintf(eos(linebuf), "quit"); 242. } 243. else if(!strcmp(t1->death,"choked")) 244. Sprintf(eos(linebuf), "choked on %s food", 245. (t1->sex == 'F') ? "her" : "his"); 246. else if(!strncmp(t1->death,"starv",5)) 247. Sprintf(eos(linebuf), "starved to death"), starv = TRUE; 248. else Sprintf(eos(linebuf), "was killed"), killed = TRUE; 249. Sprintf(eos(linebuf), " on%s level %d", 250. (killed || starv) ? "" : " dungeon", t1->level); 251. if(t1->maxlvl != t1->level) 252. Sprintf(eos(linebuf), " [max %d]", t1->maxlvl); 253. if(quit && t1->death[4]) Sprintf(eos(linebuf), t1->death + 4); 254. } 255. if(killed) Sprintf(eos(linebuf), " by %s%s", 256. (!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4)) 257. ? "" : 258. index(vowels,*t1->death) ? "an " : "a ", 259. t1->death); 260. Sprintf(eos(linebuf), "."); 261. if(t1->maxhp) { 262. register char *bp = eos(linebuf); 263. char hpbuf[10]; 264. int hppos; 265. Sprintf(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-"); 266. hppos = COLNO - 7 - strlen(hpbuf); 267. if(bp <= linebuf + hppos) { 268. while(bp < linebuf + hppos) *bp++ = ' '; 269. (void) strcpy(bp, hpbuf); 270. Sprintf(eos(bp), " [%d]", t1->maxhp); 271. } 272. } 273. if(so == 0) puts(linebuf); 274. else if(so > 0) { 275. register char *bp = eos(linebuf); 276. if(so >= COLNO) so = COLNO-1; 277. while(bp < linebuf + so) *bp++ = ' '; 278. *bp = 0; 279. standoutbeg(); 280. fputs(linebuf,stdout); 281. standoutend(); 282. (void) putchar('\n'); 283. } 284. return(strlen(linebuf)); 285. } 286. 287. char * 288. itoa(a) int a; { 289. static char buf[12]; 290. Sprintf(buf,"%d",a); 291. return(buf); 292. } 293. 294. char * 295. ordin(n) int n; { 296. register int d = n%10; 297. return((d==0 || d>3 || n/10==1) ? "th" : (d==1) ? "st" : 298. (d==2) ? "nd" : "rd"); 299. } 300. 301. char * 302. eos(s) 303. register char *s; 304. { 305. while(*s) s++; 306. return(s); 307. } 308. 309. /* 310. * Called with args from main if argc >= 0. In this case, list scores as 311. * requested. Otherwise, find scores for the current player (and list them 312. * if argc == -1). 313. */ 314. prscore(argc,argv) int argc; char **argv; { 315. extern char *hname; 316. char **players; 317. int playerct; 318. int rank; 319. register struct toptenentry *t1, *t2; 320. char *recfile = RECORD; 321. FILE *rfile; 322. register flg = 0; 323. register int i; 324. #ifdef nonsense 325. long total_score = 0L; 326. char totchars[10]; 327. int totcharct = 0; 328. #endif 329. int outflg = (argc >= -1); 330. #ifdef PERS_IS_UID 331. int uid = -1; 332. #else 333. char *player0; 334. #endif 335. 336. if(!(rfile = fopen(recfile,"r"))){ 337. puts("Cannot open record file!"); 338. return; 339. } 340. 341. if(argc > 1 && !strncmp(argv[1], "-s", 2)){ 342. if(!argv[1][2]){ 343. argc--; 344. argv++; 345. } else if(!argv[1][3] && index("CFKSTWX", argv[1][2])) { 346. argv[1]++; 347. argv[1][0] = '-'; 348. } else argv[1] += 2; 349. } 350. if(argc <= 1){ 351. #ifdef PERS_IS_UID 352. uid = getuid(); 353. playerct = 0; 354. #else 355. player0 = plname; 356. if(!*player0) 357. player0 = "hackplayer"; 358. playerct = 1; 359. players = &player0; 360. #endif 361. } else { 362. playerct = --argc; 363. players = ++argv; 364. } 365. if(outflg) putchar('\n'); 366. 367. t1 = tt_head = newttentry(); 368. for(rank = 1; ; rank++) { 369. if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]", 370. t1->date, &t1->uid, 371. &t1->level, &t1->maxlvl, 372. &t1->hp, &t1->maxhp, &t1->points, 373. &t1->plchar, &t1->sex, t1->name, t1->death) != 11) 374. t1->points = 0; 375. if(t1->points == 0) break; 376. #ifdef PERS_IS_UID 377. if(!playerct && t1->uid == uid) 378. flg++; 379. else 380. #endif 381. for(i = 0; i < playerct; i++){ 382. if(strcmp(players[i], "all") == 0 || 383. strncmp(t1->name, players[i], NAMSZ) == 0 || 384. (players[i][0] == '-' && 385. players[i][1] == t1->plchar && 386. players[i][2] == 0) || 387. (digit(players[i][0]) && rank <= atoi(players[i]))) 388. flg++; 389. } 390. t1 = t1->tt_next = newttentry(); 391. } 392. (void) fclose(rfile); 393. if(!flg) { 394. if(outflg) { 395. printf("Cannot find any entries for "); 396. if(playerct < 1) printf("you.\n"); 397. else { 398. if(playerct > 1) printf("any of "); 399. for(i=0; i<playerct; i++) 400. printf("%s%s", players[i], (i<playerct-1)?", ":".\n"); 401. printf("Call is: %s -s [playernames]\n", hname); 402. } 403. } 404. return; 405. } 406. 407. if(outflg) outheader(); 408. t1 = tt_head; 409. for(rank = 1; t1->points != 0; rank++, t1 = t2) { 410. t2 = t1->tt_next; 411. #ifdef PERS_IS_UID 412. if(!playerct && t1->uid == uid) 413. goto outwithit; 414. else 415. #endif 416. for(i = 0; i < playerct; i++){ 417. if(strcmp(players[i], "all") == 0 || 418. strncmp(t1->name, players[i], NAMSZ) == 0 || 419. (players[i][0] == '-' && 420. players[i][1] == t1->plchar && 421. players[i][2] == 0) || 422. (digit(players[i][0]) && rank <= atoi(players[i]))){ 423. outwithit: 424. if(outflg) 425. (void) outentry(rank, t1, 0); 426. #ifdef nonsense 427. total_score += t1->points; 428. if(totcharct < sizeof(totchars)-1) 429. totchars[totcharct++] = t1->plchar; 430. #endif 431. break; 432. } 433. } 434. free((char *) t1); 435. } 436. #ifdef nonsense 437. totchars[totcharct] = 0; 438. 439. /* We would like to determine whether he is experienced. However, 440. the information collected here only tells about the scores/roles 441. that got into the topten (top 100?). We should maintain a 442. .hacklog or something in his home directory. */ 443. flags.beginner = (total_score < 6000); 444. for(i=0; i<6; i++) 445. if(!index(totchars, "CFKSTWX"[i])) { 446. flags.beginner = 1; 447. if(!pl_character[0]) pl_character[0] = "CFKSTWX"[i]; 448. break; 449. } 450. #endif /* nonsense /**/ 451. }