Source:NetHack 3.6.7/src/exper.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to exper.c from the source code of NetHack 3.6.7. To link to a particular line, write [[Source:NetHack 3.6.7/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 3.6	exper.c	$NHDT-Date: 1562114352 2019/07/03 00:39:12 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.33 $ */
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. STATIC_DCL int FDECL(enermod, (int));
12. 
13. long
14. newuexp(lev)
15. int lev;
16. {
17.     if (lev < 1) /* for newuexp(u.ulevel - 1) when u.ulevel is 1 */
18.         return 0L;
19.     if (lev < 10)
20.         return (10L * (1L << lev));
21.     if (lev < 20)
22.         return (10000L * (1L << (lev - 10)));
23.     return (10000000L * ((long) (lev - 19)));
24. }
25. 
26. STATIC_OVL int
27. enermod(en)
28. int en;
29. {
30.     switch (Role_switch) {
31.     case PM_PRIEST:
32.     case PM_WIZARD:
33.         return (2 * en);
34.     case PM_HEALER:
35.     case PM_KNIGHT:
36.         return ((3 * en) / 2);
37.     case PM_BARBARIAN:
38.     case PM_VALKYRIE:
39.         return ((3 * en) / 4);
40.     default:
41.         return en;
42.     }
43. }
44. 
45. /* calculate spell power/energy points for new level */
46. int
47. newpw()
48. {
49.     int en = 0, enrnd, enfix;
50. 
51.     if (u.ulevel == 0) {
52.         en = urole.enadv.infix + urace.enadv.infix;
53.         if (urole.enadv.inrnd > 0)
54.             en += rnd(urole.enadv.inrnd);
55.         if (urace.enadv.inrnd > 0)
56.             en += rnd(urace.enadv.inrnd);
57.     } else {
58.         enrnd = (int) ACURR(A_WIS) / 2;
59.         if (u.ulevel < urole.xlev) {
60.             enrnd += urole.enadv.lornd + urace.enadv.lornd;
61.             enfix = urole.enadv.lofix + urace.enadv.lofix;
62.         } else {
63.             enrnd += urole.enadv.hirnd + urace.enadv.hirnd;
64.             enfix = urole.enadv.hifix + urace.enadv.hifix;
65.         }
66.         en = enermod(rn1(enrnd, enfix));
67.     }
68.     if (en <= 0)
69.         en = 1;
70.     if (u.ulevel < MAXULEV)
71.         u.ueninc[u.ulevel] = (xchar) en;
72.     return en;
73. }
74. 
75. /* return # of exp points for mtmp after nk killed */
76. int
77. experience(mtmp, nk)
78. register struct monst *mtmp;
79. register int nk;
80. {
81.     register struct permonst *ptr = mtmp->data;
82.     int i, tmp, tmp2;
83. 
84.     tmp = 1 + mtmp->m_lev * mtmp->m_lev;
85. 
86.     /*  For higher ac values, give extra experience */
87.     if ((i = find_mac(mtmp)) < 3)
88.         tmp += (7 - i) * ((i < 0) ? 2 : 1);
89. 
90.     /*  For very fast monsters, give extra experience */
91.     if (ptr->mmove > NORMAL_SPEED)
92.         tmp += (ptr->mmove > (3 * NORMAL_SPEED / 2)) ? 5 : 3;
93. 
94.     /*  For each "special" attack type give extra experience */
95.     for (i = 0; i < NATTK; i++) {
96.         tmp2 = ptr->mattk[i].aatyp;
97.         if (tmp2 > AT_BUTT) {
98.             if (tmp2 == AT_WEAP)
99.                 tmp += 5;
100.             else if (tmp2 == AT_MAGC)
101.                 tmp += 10;
102.             else
103.                 tmp += 3;
104.         }
105.     }
106. 
107.     /*  For each "special" damage type give extra experience */
108.     for (i = 0; i < NATTK; i++) {
109.         tmp2 = ptr->mattk[i].adtyp;
110.         if (tmp2 > AD_PHYS && tmp2 < AD_BLND)
111.             tmp += 2 * mtmp->m_lev;
112.         else if ((tmp2 == AD_DRLI) || (tmp2 == AD_STON) || (tmp2 == AD_SLIM))
113.             tmp += 50;
114.         else if (tmp2 != AD_PHYS)
115.             tmp += mtmp->m_lev;
116.         /* extra heavy damage bonus */
117.         if ((int) (ptr->mattk[i].damd * ptr->mattk[i].damn) > 23)
118.             tmp += mtmp->m_lev;
119.         if (tmp2 == AD_WRAP && ptr->mlet == S_EEL && !Amphibious)
120.             tmp += 1000;
121.     }
122. 
123.     /*  For certain "extra nasty" monsters, give even more */
124.     if (extra_nasty(ptr))
125.         tmp += (7 * mtmp->m_lev);
126. 
127.     /*  For higher level monsters, an additional bonus is given */
128.     if (mtmp->m_lev > 8)
129.         tmp += 50;
130. 
131. #ifdef MAIL
132.     /* Mail daemons put up no fight. */
133.     if (mtmp->data == &mons[PM_MAIL_DAEMON])
134.         tmp = 1;
135. #endif
136. 
137.     if (mtmp->mrevived || mtmp->mcloned) {
138.         /*
139.          *      Reduce experience awarded for repeated killings of
140.          *      "the same monster".  Kill count includes all of this
141.          *      monster's type which have been killed--including the
142.          *      current monster--regardless of how they were created.
143.          *        1.. 20        full experience
144.          *       21.. 40        xp / 2
145.          *       41.. 80        xp / 4
146.          *       81..120        xp / 8
147.          *      121..180        xp / 16
148.          *      181..240        xp / 32
149.          *      241..255+       xp / 64
150.          */
151.         for (i = 0, tmp2 = 20; nk > tmp2 && tmp > 1; ++i) {
152.             tmp = (tmp + 1) / 2;
153.             nk -= tmp2;
154.             if (i & 1)
155.                 tmp2 += 20;
156.         }
157.     }
158. 
159.     return (tmp);
160. }
161. 
162. void
163. more_experienced(exper, rexp)
164. register int exper, rexp;
165. {
166.     long oldexp = u.uexp,
167.          oldrexp = u.urexp,
168.          newexp = oldexp + exper,
169.          rexpincr = 4 * exper + rexp,
170.          newrexp = oldrexp + rexpincr;
171. 
172.     /* cap experience and score on wraparound */
173.     if (newexp < 0 && exper > 0)
174.         newexp = LONG_MAX;
175.     if (newrexp < 0 && rexpincr > 0)
176.         newrexp = LONG_MAX;
177. 
178.     if (newexp != oldexp) {
179.         u.uexp = newexp;
180.         if (flags.showexp)
181.             context.botl = TRUE;
182.         /* even when experience points aren't being shown, experience level
183.            might be highlighted with a percentage highlight rule and that
184.            percentage depends upon experience points */
185.         if (!context.botl && exp_percent_changing())
186.             context.botl = TRUE;
187.     }
188.     /* newrexp will always differ from oldrexp unless they're LONG_MAX */
189.     if (newrexp != oldrexp) {
190.         u.urexp = newrexp;
191. #ifdef SCORE_ON_BOTL
192.         if (flags.showscore)
193.             context.botl = TRUE;
194. #endif
195.     }
196.     if (u.urexp >= (Role_if(PM_WIZARD) ? 1000 : 2000))
197.         flags.beginner = 0;
198. }
199. 
200. /* e.g., hit by drain life attack */
201. void
202. losexp(drainer)
203. const char *drainer; /* cause of death, if drain should be fatal */
204. {
205.     register int num;
206. 
207.     /* override life-drain resistance when handling an explicit
208.        wizard mode request to reduce level; never fatal though */
209.     if (drainer && !strcmp(drainer, "#levelchange"))
210.         drainer = 0;
211.     else if (resists_drli(&youmonst))
212.         return;
213. 
214.     if (u.ulevel > 1) {
215.         pline("%s level %d.", Goodbye(), u.ulevel--);
216.         /* remove intrinsic abilities */
217.         adjabil(u.ulevel + 1, u.ulevel);
218.         reset_rndmonst(NON_PM); /* new monster selection */
219.     } else {
220.         if (drainer) {
221.             killer.format = KILLED_BY;
222.             if (killer.name != drainer)
223.                 Strcpy(killer.name, drainer);
224.             done(DIED);
225.         }
226.         /* no drainer or lifesaved */
227.         u.uexp = 0;
228.     }
229.     num = (int) u.uhpinc[u.ulevel];
230.     u.uhpmax -= num;
231.     if (u.uhpmax < 1)
232.         u.uhpmax = 1;
233.     u.uhp -= num;
234.     if (u.uhp < 1)
235.         u.uhp = 1;
236.     else if (u.uhp > u.uhpmax)
237.         u.uhp = u.uhpmax;
238. 
239.     num = (int) u.ueninc[u.ulevel];
240.     u.uenmax -= num;
241.     if (u.uenmax < 0)
242.         u.uenmax = 0;
243.     u.uen -= num;
244.     if (u.uen < 0)
245.         u.uen = 0;
246.     else if (u.uen > u.uenmax)
247.         u.uen = u.uenmax;
248. 
249.     if (u.uexp > 0)
250.         u.uexp = newuexp(u.ulevel) - 1;
251. 
252.     if (Upolyd) {
253.         num = monhp_per_lvl(&youmonst);
254.         u.mhmax -= num;
255.         u.mh -= num;
256.         if (u.mh <= 0)
257.             rehumanize();
258.     }
259. 
260.     context.botl = TRUE;
261. }
262. 
263. /*
264.  * Make experience gaining similar to AD&D(tm), whereby you can at most go
265.  * up by one level at a time, extra expr possibly helping you along.
266.  * After all, how much real experience does one get shooting a wand of death
267.  * at a dragon created with a wand of polymorph??
268.  */
269. void
270. newexplevel()
271. {
272.     if (u.ulevel < MAXULEV && u.uexp >= newuexp(u.ulevel))
273.         pluslvl(TRUE);
274. }
275. 
276. void
277. pluslvl(incr)
278. boolean incr; /* true iff via incremental experience growth */
279. {             /*        (false for potion of gain level)    */
280.     int hpinc, eninc;
281. 
282.     if (!incr)
283.         You_feel("more experienced.");
284. 
285.     /* increase hit points (when polymorphed, do monster form first
286.        in order to retain normal human/whatever increase for later) */
287.     if (Upolyd) {
288.         hpinc = monhp_per_lvl(&youmonst);
289.         u.mhmax += hpinc;
290.         u.mh += hpinc;
291.     }
292.     hpinc = newhp();
293.     u.uhpmax += hpinc;
294.     u.uhp += hpinc;
295. 
296.     /* increase spell power/energy points */
297.     eninc = newpw();
298.     u.uenmax += eninc;
299.     u.uen += eninc;
300. 
301.     /* increase level (unless already maxxed) */
302.     if (u.ulevel < MAXULEV) {
303.         /* increase experience points to reflect new level */
304.         if (incr) {
305.             long tmp = newuexp(u.ulevel + 1);
306.             if (u.uexp >= tmp)
307.                 u.uexp = tmp - 1;
308.         } else {
309.             u.uexp = newuexp(u.ulevel);
310.         }
311.         ++u.ulevel;
312.         pline("Welcome %sto experience level %d.",
313.               (u.ulevelmax < u.ulevel) ? "" : "back ",
314.               u.ulevel);
315.         if (u.ulevelmax < u.ulevel)
316.             u.ulevelmax = u.ulevel;
317.         adjabil(u.ulevel - 1, u.ulevel); /* give new intrinsics */
318.         reset_rndmonst(NON_PM);          /* new monster selection */
319.     }
320.     context.botl = TRUE;
321. }
322. 
323. /* compute a random amount of experience points suitable for the hero's
324.    experience level:  base number of points needed to reach the current
325.    level plus a random portion of what it takes to get to the next level */
326. long
327. rndexp(gaining)
328. boolean gaining; /* gaining XP via potion vs setting XP for polyself */
329. {
330.     long minexp, maxexp, diff, factor, result;
331. 
332.     minexp = (u.ulevel == 1) ? 0L : newuexp(u.ulevel - 1);
333.     maxexp = newuexp(u.ulevel);
334.     diff = maxexp - minexp, factor = 1L;
335.     /* make sure that `diff' is an argument which rn2() can handle */
336.     while (diff >= (long) LARGEST_INT)
337.         diff /= 2L, factor *= 2L;
338.     result = minexp + factor * (long) rn2((int) diff);
339.     /* 3.4.1:  if already at level 30, add to current experience
340.        points rather than to threshold needed to reach the current
341.        level; otherwise blessed potions of gain level can result
342.        in lowering the experience points instead of raising them */
343.     if (u.ulevel == MAXULEV && gaining) {
344.         result += (u.uexp - minexp);
345.         /* avoid wrapping (over 400 blessed potions needed for that...) */
346.         if (result < u.uexp)
347.             result = u.uexp;
348.     }
349.     return result;
350. }
351. 
352. /*exper.c*/