Source:NetHack 5.0.0/src/exper.c
Jump to navigation
Jump to search
Below is the full text to exper.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/exper.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 5.0 exper.c $NHDT-Date: 1706133782 2024/01/24 22:03:02 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.62 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2007. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. #ifndef LONG_MAX 8. #include <limits.h> 9. #endif 10. 11. staticfn int enermod(int); 12. 13. long 14. newuexp(int lev) 15. { 16. if (lev < 1) /* for newuexp(u.ulevel - 1) when u.ulevel is 1 */ 17. return 0L; 18. if (lev < 10) 19. return (10L * (1L << lev)); 20. if (lev < 20) 21. return (10000L * (1L << (lev - 10))); 22. return (10000000L * ((long) (lev - 19))); 23. } 24. 25. staticfn int 26. enermod(int en) 27. { 28. switch (Role_switch) { 29. case PM_CLERIC: 30. case PM_WIZARD: 31. return (2 * en); 32. case PM_HEALER: 33. case PM_KNIGHT: 34. return ((3 * en) / 2); 35. case PM_BARBARIAN: 36. case PM_VALKYRIE: 37. return ((3 * en) / 4); 38. default: 39. return en; 40. } 41. } 42. 43. /* calculate spell power/energy points for new level */ 44. int 45. newpw(void) 46. { 47. int en = 0, enrnd, enfix; 48. 49. if (u.ulevel == 0) { 50. en = gu.urole.enadv.infix + gu.urace.enadv.infix; 51. if (gu.urole.enadv.inrnd > 0) 52. en += rnd(gu.urole.enadv.inrnd); 53. if (gu.urace.enadv.inrnd > 0) 54. en += rnd(gu.urace.enadv.inrnd); 55. } else { 56. enrnd = (int) ACURR(A_WIS) / 2; 57. if (u.ulevel < gu.urole.xlev) { 58. enrnd += gu.urole.enadv.lornd + gu.urace.enadv.lornd; 59. enfix = gu.urole.enadv.lofix + gu.urace.enadv.lofix; 60. } else { 61. enrnd += gu.urole.enadv.hirnd + gu.urace.enadv.hirnd; 62. enfix = gu.urole.enadv.hifix + gu.urace.enadv.hifix; 63. } 64. en = enermod(rn1(enrnd, enfix)); 65. } 66. if (en <= 0) 67. en = 1; 68. if (u.ulevel < MAXULEV) { 69. /* remember increment; future level drain could take it away again */ 70. u.ueninc[u.ulevel] = (xint16) en; 71. } else { 72. /* after level 30, throttle energy gains from extra experience; 73. once max reaches 600, further increments will be just 1 more */ 74. char lim = 4 - u.uenmax / 200; 75. 76. lim = max(lim, 1); 77. if (en > lim) 78. en = lim; 79. } 80. return en; 81. } 82. 83. /* return # of exp points for mtmp after nk killed */ 84. int 85. experience(struct monst *mtmp, int nk) 86. { 87. struct permonst *ptr = mtmp->data; 88. int i, tmp, tmp2; 89. 90. tmp = 1 + mtmp->m_lev * mtmp->m_lev; 91. 92. /* For higher ac values, give extra experience */ 93. if ((i = find_mac(mtmp)) < 3) 94. tmp += (7 - i) * ((i < 0) ? 2 : 1); 95. 96. /* For very fast monsters, give extra experience */ 97. if (ptr->mmove > NORMAL_SPEED) 98. tmp += (ptr->mmove > (3 * NORMAL_SPEED / 2)) ? 5 : 3; 99. 100. /* For each "special" attack type give extra experience */ 101. for (i = 0; i < NATTK; i++) { 102. tmp2 = ptr->mattk[i].aatyp; 103. if (tmp2 > AT_BUTT) { 104. if (tmp2 == AT_WEAP) 105. tmp += 5; 106. else if (tmp2 == AT_MAGC) 107. tmp += 10; 108. else 109. tmp += 3; 110. } 111. } 112. 113. /* For each "special" damage type give extra experience */ 114. for (i = 0; i < NATTK; i++) { 115. tmp2 = ptr->mattk[i].adtyp; 116. if (tmp2 > AD_PHYS && tmp2 < AD_BLND) 117. tmp += 2 * mtmp->m_lev; 118. else if ((tmp2 == AD_DRLI) || (tmp2 == AD_STON) || (tmp2 == AD_SLIM)) 119. tmp += 50; 120. else if (tmp2 != AD_PHYS) 121. tmp += mtmp->m_lev; 122. /* extra heavy damage bonus */ 123. if ((int) (ptr->mattk[i].damd * ptr->mattk[i].damn) > 23) 124. tmp += mtmp->m_lev; 125. if (tmp2 == AD_WRAP && ptr->mlet == S_EEL && !Amphibious) 126. tmp += 1000; 127. } 128. 129. /* For certain "extra nasty" monsters, give even more */ 130. if (extra_nasty(ptr)) 131. tmp += (7 * mtmp->m_lev); 132. 133. /* For higher level monsters, an additional bonus is given */ 134. if (mtmp->m_lev > 8) 135. tmp += 50; 136. 137. #ifdef MAIL_STRUCTURES 138. /* Mail daemons put up no fight. */ 139. if (mtmp->data == &mons[PM_MAIL_DAEMON]) 140. tmp = 1; 141. #endif 142. 143. if (mtmp->mrevived || mtmp->mcloned) { 144. /* 145. * Reduce experience awarded for repeated killings of 146. * "the same monster". Kill count includes all of this 147. * monster's type which have been killed--including the 148. * current monster--regardless of how they were created. 149. * 1.. 20 full experience 150. * 21.. 40 xp / 2 151. * 41.. 80 xp / 4 152. * 81..120 xp / 8 153. * 121..180 xp / 16 154. * 181..240 xp / 32 155. * 241..255+ xp / 64 156. */ 157. for (i = 0, tmp2 = 20; nk > tmp2 && tmp > 1; ++i) { 158. tmp = (tmp + 1) / 2; 159. nk -= tmp2; 160. if (i & 1) 161. tmp2 += 20; 162. } 163. } 164. 165. return (tmp); 166. } 167. 168. void 169. more_experienced(int exper, int rexp) 170. { 171. long oldexp = u.uexp, 172. oldrexp = u.urexp, 173. newexp = oldexp + exper, 174. rexpincr = 4 * exper + rexp, 175. newrexp = oldrexp + rexpincr; 176. 177. /* cap experience and score on wraparound */ 178. if (newexp < 0 && exper > 0) 179. newexp = LONG_MAX; 180. if (newrexp < 0 && rexpincr > 0) 181. newrexp = LONG_MAX; 182. 183. if (newexp != oldexp) { 184. u.uexp = newexp; 185. if (flags.showexp) 186. disp.botl = TRUE; 187. /* even when experience points aren't being shown, experience level 188. might be highlighted with a percentage highlight rule and that 189. percentage depends upon experience points */ 190. if (!disp.botl && exp_percent_changing()) 191. disp.botl = TRUE; 192. } 193. /* newrexp will always differ from oldrexp unless they're LONG_MAX */ 194. if (newrexp != oldrexp) { 195. u.urexp = newrexp; 196. #ifdef SCORE_ON_BOTL 197. if (flags.showscore) 198. disp.botl = TRUE; 199. #endif 200. } 201. if (u.urexp >= (Role_if(PM_WIZARD) ? 1000 : 2000)) 202. flags.beginner = FALSE; 203. } 204. 205. /* e.g., hit by drain life attack */ 206. void 207. losexp( 208. const char *drainer) /* cause of death, if drain should be fatal */ 209. { 210. int num, uhpmin, olduhpmax; 211. 212. /* override life-drain resistance when handling an explicit 213. wizard mode request to reduce level; never fatal though */ 214. if (drainer && !strcmp(drainer, "#levelchange")) 215. drainer = 0; 216. else if (resists_drli(&gy.youmonst)) 217. return; 218. 219. /* level-loss message; "Goodbye level 1." is fatal; divine anger 220. (drainer==NULL) resets a level 1 character to 0 experience points 221. without reducing level and that isn't fatal so suppress the message 222. in that situation */ 223. if (u.ulevel > 1 || drainer) 224. pline("%s level %d.", Goodbye(), u.ulevel); 225. 226. if (u.ulevel > 1) { 227. u.ulevel -= 1; 228. /* remove intrinsic abilities */ 229. adjabil(u.ulevel + 1, u.ulevel); 230. livelog_printf(LL_MINORAC, "lost experience level %d", u.ulevel + 1); 231. SoundAchievement(0, sa2_xpleveldown, 0); 232. } else { /* u.ulevel==1 */ 233. if (drainer) { 234. svk.killer.format = KILLED_BY; 235. if (svk.killer.name != drainer) 236. Strcpy(svk.killer.name, drainer); 237. done(DIED); 238. } 239. /* no drainer or lifesaved */ 240. if (u.ulevel > 1) 241. /* can happen during debug fuzzing if fuzzer_savelife() uses 242. a blessed potion of restore ability to restore lost levels */ 243. return; 244. u.uexp = 0; 245. livelog_printf(LL_MINORAC, "lost all experience"); 246. } 247. assert(u.ulevel >= 0 && u.ulevel < MAXULEV); /* valid array index */ 248. 249. olduhpmax = u.uhpmax; 250. uhpmin = minuhpmax(10); /* same minimum as is used by life-saving */ 251. num = (int) u.uhpinc[u.ulevel]; 252. u.uhpmax -= num; 253. if (u.uhpmax < uhpmin) 254. setuhpmax(uhpmin, TRUE); 255. /* uhpmax might try to go up if it has previously been reduced by 256. strength loss or by a fire trap or by an attack by Death which 257. all use a different minimum than life-saving or experience loss; 258. we don't allow it to go up because that contradicts assumptions 259. elsewhere (such as healing wielder who drains with Stormbringer) */ 260. if (u.uhpmax > olduhpmax) 261. setuhpmax(olduhpmax, TRUE); 262. 263. u.uhp -= num; 264. if (u.uhp < 1) 265. u.uhp = 1; 266. else if (u.uhp > u.uhpmax) 267. u.uhp = u.uhpmax; 268. 269. num = (int) u.ueninc[u.ulevel]; 270. u.uenmax -= num; 271. if (u.uenmax < 0) 272. u.uenmax = 0; 273. u.uen -= num; 274. if (u.uen < 0) 275. u.uen = 0; 276. else if (u.uen > u.uenmax) 277. u.uen = u.uenmax; 278. 279. if (u.uexp > 0) 280. u.uexp = newuexp(u.ulevel) - 1; 281. 282. if (Upolyd) { 283. num = monhp_per_lvl(&gy.youmonst); 284. u.mhmax -= num; 285. u.mh -= num; 286. if (u.mh <= 0) 287. rehumanize(); 288. } 289. 290. disp.botl = TRUE; 291. } 292. 293. /* 294. * Make experience gaining similar to AD&D(tm), whereby you can at most go 295. * up by one level at a time, extra expr possibly helping you along. 296. * After all, how much real experience does one get shooting a wand of death 297. * at a dragon created with a wand of polymorph?? 298. */ 299. void 300. newexplevel(void) 301. { 302. if (u.ulevel < MAXULEV && u.uexp >= newuexp(u.ulevel)) 303. pluslvl(TRUE); 304. } 305. 306. void 307. pluslvl( 308. boolean incr) /* True: incremental experience growth; 309. * False: potion of gain level or wraith corpse 310. * or wizard mode #levelchange */ 311. { 312. int hpinc, eninc; 313. 314. if (!incr) 315. You_feel("more experienced."); 316. 317. /* increase hit points (when polymorphed, do monster form first 318. in order to retain normal human/whatever increase for later) */ 319. if (Upolyd) { 320. hpinc = monhp_per_lvl(&gy.youmonst); 321. u.mh += hpinc; 322. setuhpmax(u.mhmax, FALSE); /* acts as setmhmax() when Upolyd */ 323. } 324. hpinc = newhp(); 325. u.uhp += hpinc; 326. setuhpmax(u.uhpmax + hpinc, TRUE); /* will lower u.uhp if it exceeds 327. * u.uhpmax */ 328. 329. /* increase spell power/energy points */ 330. eninc = newpw(); 331. u.uenmax += eninc; 332. if (u.uenmax > u.uenpeak) 333. u.uenpeak = u.uenmax; 334. u.uen += eninc; 335. 336. /* increase level (unless already maxxed) */ 337. if (u.ulevel < MAXULEV) { 338. int old_ach_cnt, newrank, oldrank = xlev_to_rank(u.ulevel); 339. 340. /* increase experience points to reflect new level */ 341. if (incr) { 342. long tmp = newuexp(u.ulevel + 1); 343. 344. if (u.uexp >= tmp) 345. u.uexp = tmp - 1; 346. } else { 347. u.uexp = newuexp(u.ulevel); 348. } 349. ++u.ulevel; 350. pline("Welcome %sto experience level %d.", 351. (u.ulevelmax < u.ulevel) ? "" : "back ", 352. u.ulevel); 353. if (u.ulevelmax < u.ulevel) 354. u.ulevelmax = u.ulevel; 355. adjabil(u.ulevel - 1, u.ulevel); /* give new intrinsics */ 356. SoundAchievement(0, sa2_xplevelup, 0); 357. old_ach_cnt = count_achievements(); 358. newrank = xlev_to_rank(u.ulevel); 359. if (newrank > oldrank) 360. record_achievement(achieve_rank(newrank)); 361. /* a new rank achievement will log its own message; log a simpler 362. message here if we didn't just get an achievement (so when rank 363. hasn't changed or hero just regained a lost level and the rank 364. achievement doesn't get repeated) */ 365. if (count_achievements() == old_ach_cnt) 366. livelog_printf(LL_MINORAC, "%sgained experience level %d", 367. (u.ulevel <= u.ulevelpeak) ? "re" : "", u.ulevel); 368. if (u.ulevel > u.ulevelpeak) 369. u.ulevelpeak = u.ulevel; 370. } 371. disp.botl = TRUE; 372. } 373. 374. /* compute a random amount of experience points suitable for the hero's 375. experience level: base number of points needed to reach the current 376. level plus a random portion of what it takes to get to the next level */ 377. long 378. rndexp(boolean gaining) /* gaining XP via potion vs setting XP for polyself */ 379. { 380. long minexp, maxexp, diff, factor, result; 381. 382. minexp = (u.ulevel == 1) ? 0L : newuexp(u.ulevel - 1); 383. maxexp = newuexp(u.ulevel); 384. diff = maxexp - minexp, factor = 1L; 385. /* make sure that `diff' is an argument which rn2() can handle */ 386. while (diff >= (long) LARGEST_INT) 387. diff /= 2L, factor *= 2L; 388. result = minexp + factor * (long) rn2((int) diff); 389. /* 3.4.1: if already at level 30, add to current experience 390. points rather than to threshold needed to reach the current 391. level; otherwise blessed potions of gain level can result 392. in lowering the experience points instead of raising them */ 393. if (u.ulevel == MAXULEV && gaining) { 394. result += (u.uexp - minexp); 395. /* avoid wrapping (over 400 blessed potions needed for that...) */ 396. if (result < u.uexp) 397. result = u.uexp; 398. } 399. return result; 400. } 401. 402. /*exper.c*/