Difference between revisions of "Source:NetHack 3.4.3/src/exper.c"

From NetHackWiki
Jump to navigation Jump to search
(rndexp: A few more notes.)
m (rndexp: Typo fix. :))
Line 405: Line 405:
 
  <span id="line241">241.      result += (u.uexp - minexp);</span>
 
  <span id="line241">241.      result += (u.uexp - minexp);</span>
  
The reason for this should become clear if you realize that maxexp is 120000000 when the player is at level 30, since level 30 has no actual max experience. This probably only matters for [[farmer]]s.
+
The reason for this should become clear if you realize that maxexp is 110000000 when the player is at level 30, since level 30 has no actual max experience. This probably only matters for [[farmer]]s.
  
 
  <span id="line242">242.      /* avoid wrapping (over 400 blessed potions needed for that...) */</span>
 
  <span id="line242">242.      /* avoid wrapping (over 400 blessed potions needed for that...) */</span>

Revision as of 17:59, 13 October 2006

Below is the full text to src/exper.c from NetHack 3.4.3. To link to a particular line, write [[exper.c#line123]], for example.

Top of file

1.    /*	SCCS Id: @(#)exper.c	3.4	2002/11/20	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    

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.

5.    #include "hack.h"
6.    
7.    STATIC_DCL long FDECL(newuexp, (int));
8.    STATIC_DCL int FDECL(enermod, (int));
9.    

newuexp

10.   STATIC_OVL long
11.   newuexp(lev)
12.   int lev;
13.   {
14.   	if (lev < 10) return (10L * (1L << lev));
15.   	if (lev < 20) return (10000L * (1L << (lev - 10)));
16.   	return (10000000L * ((long)(lev - 19)));
17.   }
18.   

newuexp returns the number of experience points required for to level up beyond lev. As you can see there are three divisions: less than 10, 10 to 19, and more than 19. The "1L << X" notation (the "<<" of which indicates a bitshift) means the Xth power of 2. So 1L << 3 means two to the third power, or 8.

So to level up from 2 to 10 requires (ten * powers of two) experience points. To get to level two, you need 20 experience. To get to level three, 40 (which is cumulative with the twenty to get to level two, so only twenty more). Continuing, you need 80, 160, 320, 640, 1280, 2560, 5120 experience points to level up. The benefit of this system is that, to level up, you need to gain the experience equal to how much experience you had already gained from the beginning of the game to the start of your current level (unless of course you've lost experience).

The experience required to get to level 11 is 10000 * (1 << (10 - 10)), which is 10000. The powers of two are reused again: 20000; 40000; 80000; 160,000; 320,000; 640,000; 1,280,000; 2,560,000; 5,120,000. If line was deleted the experience to level would not change too much: 10240, 20480, 40960, and so on. The DevTeam probably just wanted rounder numbers.

Finally, the experience required to get to level 21 is 10,000,000. Each additional level requires ten million more points.

enermod

19.   STATIC_OVL int
20.   enermod(en)
21.   int en;
22.   {
23.   	switch (Role_switch) {
24.   	case PM_PRIEST:
25.   	case PM_WIZARD:
26.   	    return(2 * en);
27.   	case PM_HEALER:
28.   	case PM_KNIGHT:
29.   	    return((3 * en) / 2);
30.   	case PM_BARBARIAN:
31.   	case PM_VALKYRIE:
32.   	    return((3 * en) / 4);
33.   	default:
34.   	    return (en);
35.   	}
36.   }
37.   

enermod, used in the losexp and pluslvl functions, modifies your energy gains and losses based on which role you are. Wizards and Priests get a large energy bonus (2x); Healers and Knights get a smaller bonus (1.5x); Barbarians and Valkyries get a penalty (.75x). The other seven roles suffer no modification (1x).

experience

38.   int
39.   experience(mtmp, nk)	/* return # of exp points for mtmp after nk killed */
40.   	register struct	monst *mtmp;
41.   	register int	nk;
42.   #if defined(macintosh) && (defined(__SC__) || defined(__MRC__))
43.   # pragma unused(nk)
44.   #endif
45.   {
46.   	register struct permonst *ptr = mtmp->data;
47.   	int	i, tmp, tmp2;
48.   
49.   	tmp = 1 + mtmp->m_lev * mtmp->m_lev;

Start off with the square of the monster's level. Add one because mtmp->m_lev can be zero, and such is the case with, among others, jackals and kobolds.

50.   
51.   /*	For higher ac values, give extra experience */
52.   	if ((i = find_mac(mtmp)) < 3) tmp += (7 - i) * ((i < 0) ? 2 : 1);
53.   
54.   /*	For very fast monsters, give extra experience */
55.   	if (ptr->mmove > NORMAL_SPEED)
56.   	    tmp += (ptr->mmove > (3*NORMAL_SPEED/2)) ? 5 : 3;
57.   
58.   /*	For each "special" attack type give extra experience */
59.   	for(i = 0; i < NATTK; i++) {
60.   
61.   	    tmp2 = ptr->mattk[i].aatyp;
62.   	    if(tmp2 > AT_BUTT) {
63.   
64.   		if(tmp2 == AT_WEAP) tmp += 5;
65.   		else if(tmp2 == AT_MAGC) tmp += 10;
66.   		else tmp += 3;

AT_WEAP is monsters using swords and the like against you. AT_MAGC is obviously monsters using magical attacks. All other weapon types (such as engulfing or spitting venom) receive a smaller bonus, except those defined as less than or equal to AT_BUTT. These attack types that give no extra experience are AT_NONE which indicates a passive attack (as from an acid blob), AT_CLAW, AT_KICK, and AT_BUTT.

67.   	    }
68.   	}
69.   
70.   /*	For each "special" damage type give extra experience */
71.   	for(i = 0; i < NATTK; i++) {
72.   	    tmp2 = ptr->mattk[i].adtyp;
73.   	    if(tmp2 > AD_PHYS && tmp2 < AD_BLND) tmp += 2*mtmp->m_lev;

For each of the following attacks that your kill had, give extra experience equal to two times monster level:

74.   	    else if((tmp2 == AD_DRLI) || (tmp2 == AD_STON) ||
75.   	    		(tmp2 == AD_SLIM)) tmp += 50;

Vampires, cockatrices, green slimes, and other similar creatures give a 50-point bonus.

76.   	    else if(tmp != AD_PHYS) tmp += mtmp->m_lev;

This catches every other monster attack (except ordinary physical damage) and gives the monster-level's worth of experience. Examples of the many things caught by this include seduction, inducing lycanthropy, and healing wounds as from a nurse.

77.   		/* extra heavy damage bonus */
78.   	    if((int)(ptr->mattk[i].damd * ptr->mattk[i].damn) > 23)
79.   		tmp += mtmp->m_lev;

If the attack can roll a 24 or more on the damage dice then give an additional monster-level's bonus. Mumakil, for example, get this bonus for their first attack (4*12=48), but not for their second (2*6=12). This helps balance strong but slow monsters with monsters who have a lot of weaker attacks.

80.   	    if (tmp2 == AD_WRAP && ptr->mlet == S_EEL && !Amphibious)
81.   		tmp += 1000;

Receive a thousand-point bonus if you're killing sea monsters who can drown you, as long as you don't have magical breathing.

82.   	}
83.   
84.   /*	For certain "extra nasty" monsters, give even more */
85.   	if (extra_nasty(ptr)) tmp += (7 * mtmp->m_lev);

A monster is extra_nasty if it has the M2_NASTY flag. Such monsters are:

86.   
87.   /*	For higher level monsters, an additional bonus is given */
88.   	if(mtmp->m_lev > 8) tmp += 50;

Level 9 monsters include queen bees, winged gargoyles, mind flayers, giant mimics, and fire giants.

89.   
90.   #ifdef MAIL
91.   	/* Mail daemons put up no fight. */
92.   	if(mtmp->data == &mons[PM_MAIL_DAEMON]) tmp = 1;
93.   #endif

Interestingly, mail daemons, which are level 56, would have a rather large experience value without this explicit check.

94.   
95.   	return(tmp);
96.   }
97.   

more_experienced

98.   void
99.   more_experienced(exp, rexp)
100.  	register int exp, rexp;
101.  {
102.  	u.uexp += exp;
103.  	u.urexp += 4*exp + rexp;
104.  	if(exp
105.  #ifdef SCORE_ON_BOTL
106.  	   || flags.showscore
107.  #endif
108.  	   ) flags.botl = 1;
109.  	if (u.urexp >= (Role_if(PM_WIZARD) ? 1000 : 2000))
110.  		flags.beginner = 0;
111.  }
112.  

u.uexp stores your experience points, and u.urexp stores your score. Any gain in experience points also gains you four times that amount of score points, plus any bonus specified in rexp. Some actions will net you score points, but not experience points (such as identifying an unknown wand by zapping). For those, this function is called as more_experienced(0,X), for an amount of score points X.

losexp

113.  void
114.  losexp(drainer)		/* e.g., hit by drain life attack */
115.  const char *drainer;	/* cause of death, if drain should be fatal */
116.  {
117.  	register int num;
118.  
119.  #ifdef WIZARD
120.  	/* override life-drain resistance when handling an explicit
121.  	   wizard mode request to reduce level; never fatal though */
122.  	if (drainer && !strcmp(drainer, "#levelchange"))
123.  	    drainer = 0;
124.  	else
125.  #endif
126.  	    if (resists_drli(&youmonst)) return;

resists_drli reveals that undead, demons, lycanthropes, Death, and those wielding weapons such as Stormbringer and Excalibur resist leveldrain.

127.  
128.  	if (u.ulevel > 1) {
129.  		pline("%s level %d.", Goodbye(), u.ulevel--);
130.  		/* remove intrinsic abilities */
131.  		adjabil(u.ulevel + 1, u.ulevel);
132.  		reset_rndmonst(NON_PM);	/* new monster selection */

reset_rndmonst will facilitate the updating of which monsters can be generated based on your new experience level.

133.  	} else {
134.  		if (drainer) {
135.  			killer_format = KILLED_BY;
136.  			killer = drainer;
137.  			done(DIED);
138.  		}
139.  		/* no drainer or lifesaved */
140.  		u.uexp = 0;
141.  	}

Being drained to level zero kills you. drainer is zero if the wizmode command #levelchange was used, or your deity drained your level due to your insolence. The done function will activate a worn amulet of life saving, if you happen to be so lucky. In any case, you are set to zero experience points (and, obviously, experience level one) if you survive being drained to level zero.

142.  	num = newhp();
143.  	u.uhpmax -= num;
144.  	if (u.uhpmax < 1) u.uhpmax = 1;
145.  	u.uhp -= num;
146.  	if (u.uhp < 1) u.uhp = 1;
147.  	else if (u.uhp > u.uhpmax) u.uhp = u.uhpmax;
148.  

newhp looks at your role, race, experience level, and constitution to determine what amount of HP you'd gain if you leveled up. Since we're losing an experience level, we subtract, not add, the result of newhp. We also guarantee that we won't die due to HP loss; worrying about being drained to level 0 is enough.

149.  	if (u.ulevel < urole.xlev)
150.  	    num = rn1((int)ACURR(A_WIS)/2 + urole.enadv.lornd + urace.enadv.lornd,
151.  			urole.enadv.lofix + urace.enadv.lofix);
152.  	else
153.  	    num = rn1((int)ACURR(A_WIS)/2 + urole.enadv.hirnd + urace.enadv.hirnd,
154.  			urole.enadv.hifix + urace.enadv.hifix);

Roles have different cutoffs for when certain stats are gained. For example, as a Samurai, you gain 1d8 HP per level until level 10, at which point you begin gaining 1 HP per level. This calculation is using not HP but energy, which is clearly dependent on wisdom.

155.  	num = enermod(num);		/* M. Stephenson */

Wizards and Priests lose 2x energy, Healers and Knights lose 1.5x energy, Barbarians and Valkyries lose .75 energy. But that's only because they gained them at the same rate.

156.  	u.uenmax -= num;
157.  	if (u.uenmax < 0) u.uenmax = 0;
158.  	u.uen -= num;
159.  	if (u.uen < 0) u.uen = 0;
160.  	else if (u.uen > u.uenmax) u.uen = u.uenmax;
161.  

Zero energy is fine, but not negative energy.

162.  	if (u.uexp > 0)
163.  		u.uexp = newuexp(u.ulevel) - 1;

When we lose a level, we're always set to one experience away from levelling up, unless we would have been drained to level zero.

164.  	flags.botl = 1;

Since our level changed, we need to update the bottom lines.

165.  }
166.  

newexplevel

167.  /*
168.   * Make experience gaining similar to AD&D(tm), whereby you can at most go
169.   * up by one level at a time, extra expr possibly helping you along.
170.   * After all, how much real experience does one get shooting a wand of death
171.   * at a dragon created with a wand of polymorph??
172.   */
173.  void
174.  newexplevel()
175.  {
176.  	if (u.ulevel < MAXULEV && u.uexp >= newuexp(u.ulevel))
177.  	    pluslvl(TRUE);
178.  }
179.  

This is called every time experience is gained to make sure we're awarding a single level and only when it makes sense. pluslvl will handle the setting of your experience points when you would have gained multiple levels.

pluslvl

180.  void
181.  pluslvl(incr)
182.  boolean incr;	/* true iff via incremental experience growth */
183.  {		/*	(false for potion of gain level)      */
184.  	register int num;
185.  
186.  	if (!incr) You_feel("more experienced.");

Don't display "You feel more experienced." if you quaffed a potion of gain level; just the "Welcome to experience level X." is enough.

187.  	num = newhp();
188.  	u.uhpmax += num;
189.  	u.uhp += num;

newhp looks at your role, race, experience level, and constitution to determine what amount of HP you gain from leveling up.

190.  	if (Upolyd) {
191.  	    num = rnd(8);
192.  	    u.mhmax += num;
193.  	    u.mh += num;
194.  	}

Polymorph makes things easy: you always gain exactly 1d8 HP when you level up. Note that the old value of num is overwritten, and that we're using mhmax and mh, not uhpmax and uhp. The former is for your polymorphed form, the latter is for your original form.

195.  	if (u.ulevel < urole.xlev)
196.  	    num = rn1((int)ACURR(A_WIS)/2 + urole.enadv.lornd + urace.enadv.lornd,
197.  			urole.enadv.lofix + urace.enadv.lofix);
198.  	else
199.  	    num = rn1((int)ACURR(A_WIS)/2 + urole.enadv.hirnd + urace.enadv.hirnd,
200.  			urole.enadv.hifix + urace.enadv.hifix);

Roles have different cutoffs for when certain stats are gained. For example, as a Samurai, you gain 1d8 HP per level until level 10, at which point you begin gaining 1 HP per level. This calculation is using not HP but energy, which is clearly dependent on wisdom.

201.  	num = enermod(num);	/* M. Stephenson */
202.  	u.uenmax += num;
203.  	u.uen += num;

enermod gives a bonus based on what role you are.

204.  	if (u.ulevel < MAXULEV) {
205.  	    if (incr) {
206.  		long tmp = newuexp(u.ulevel + 1);
207.  		if (u.uexp >= tmp) u.uexp = tmp - 1;

This blocks us from levelling up multiple times with one kill. Similar to being life drained, we're set to one point away from the next level.

208.  	    } else {
209.  		u.uexp = newuexp(u.ulevel);

Quaffing a potion of gain level sets you at the very beginning of the level. The quaff code handles the case of blessed gain level setting your experience to about half way to the next level.

210.  	    }
211.  	    ++u.ulevel;
212.  	    if (u.ulevelmax < u.ulevel) u.ulevelmax = u.ulevel;

Update what our maximum level ever has been: this is to facilitate the potion of full healing's effect of restoring lost levels.

213.  	    pline("Welcome to experience level %d.", u.ulevel);
214.  	    adjabil(u.ulevel - 1, u.ulevel);	/* give new intrinsics */
215.  	    reset_rndmonst(NON_PM);		/* new monster selection */

reset_rndmonst will facilitate the updating of which monsters can be generated based on your new experience level.

216.  	}
217.  	flags.botl = 1;

Since our level changed, we need to update the bottom lines.

218.  }
219.  

rndexp

220.  /* compute a random amount of experience points suitable for the hero's
221.     experience level:  base number of points needed to reach the current
222.     level plus a random portion of what it takes to get to the next level */
223.  long
224.  rndexp(gaining)
225.  boolean gaining;	/* gaining XP via potion vs setting XP for polyself */
226.  {
227.  	long minexp, maxexp, diff, factor, result;
228.  
229.  	minexp = (u.ulevel == 1) ? 0L : newuexp(u.ulevel - 1);
230.  	maxexp = newuexp(u.ulevel);
231.  	diff = maxexp - minexp,  factor = 1L;

Should be obvious: minexp is the minimum exp for this level, maxexp is the maximum exp for this level (plus one, we'll see why momentarily). diff is the difference.

232.  	/* make sure that `diff' is an argument which rn2() can handle */
233.  	while (diff >= (long)LARGEST_INT)
234.  	    diff /= 2L,  factor *= 2L;

LARGEST_INT is defined as 32767. NetHack still tries to support 16-bit systems.

235.  	result = minexp + factor * (long)rn2((int)diff);

Now the reason why maxexp is max exp plus one. It's because the range of rn2 is 0 <= rn2(x) < x. result is now the new amount of experience points for this level.

236.  	/* 3.4.1:  if already at level 30, add to current experience
237.  	   points rather than to threshold needed to reach the current
238.  	   level; otherwise blessed potions of gain level can result
239.  	   in lowering the experience points instead of raising them */
240.  	if (u.ulevel == MAXULEV && gaining) {
241.  	    result += (u.uexp - minexp);

The reason for this should become clear if you realize that maxexp is 110000000 when the player is at level 30, since level 30 has no actual max experience. This probably only matters for farmers.

242.  	    /* avoid wrapping (over 400 blessed potions needed for that...) */
243.  	    if (result < u.uexp) result = u.uexp;

This guards against integer overflow.

244.  	}
245.  	return result;
246.  }
247.  
248.  /*exper.c*/