Source:NetHack 3.4.3/src/exper.c

From NetHackWiki
Jump to: navigation, search

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[edit]

  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. */

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. #include "hack.h"
  2.  
  3. STATIC_DCL long FDECL(newuexp, (int));
  4. STATIC_DCL int FDECL(enermod, (int));

newuexp[edit]

  1. STATIC_OVL long
  2. newuexp(lev)
  3. int lev;
  4. {
  5. 	if (lev < 10) return (10L * (1L << lev));
  6. 	if (lev < 20) return (10000L * (1L << (lev - 10)));
  7. 	return (10000000L * ((long)(lev - 19)));
  8. }

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[edit]

  1. STATIC_OVL int
  2. enermod(en)
  3. int en;
  4. {
  5. 	switch (Role_switch) {
  6. 	case PM_PRIEST:
  7. 	case PM_WIZARD:
  8. 	    return(2 * en);
  9. 	case PM_HEALER:
  10. 	case PM_KNIGHT:
  11. 	    return((3 * en) / 2);
  12. 	case PM_BARBARIAN:
  13. 	case PM_VALKYRIE:
  14. 	    return((3 * en) / 4);
  15. 	default:
  16. 	    return (en);
  17. 	}
  18. }

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[edit]

  1. int
  2. experience(mtmp, nk)	/* return # of exp points for mtmp after nk killed */
  3. 	register struct	monst *mtmp;
  4. 	register int	nk;
  5. #if defined(macintosh) && (defined(__SC__) || defined(__MRC__))
  6. # pragma unused(nk)
  7. #endif
  8. {
  9. 	register struct permonst *ptr = mtmp->data;
  10. 	int	i, tmp, tmp2;
  11.  
  12. 	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.

  1. /*	For higher ac values, give extra experience */
  2. 	if ((i = find_mac(mtmp)) < 3) tmp += (7 - i) * ((i < 0) ? 2 : 1);
  3.  
  4. /*	For very fast monsters, give extra experience */
  5. 	if (ptr->mmove > NORMAL_SPEED)
  6. 	    tmp += (ptr->mmove > (3*NORMAL_SPEED/2)) ? 5 : 3;
  7.  
  8. /*	For each "special" attack type give extra experience */
  9. 	for(i = 0; i < NATTK; i++) {
  10.  
  11. 	    tmp2 = ptr->mattk[i].aatyp;
  12. 	    if(tmp2 > AT_BUTT) {
  13.  
  14. 		if(tmp2 == AT_WEAP) tmp += 5;
  15. 		else if(tmp2 == AT_MAGC) tmp += 10;
  16. 		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_BITE, AT_KICK, and AT_BUTT.

  1. 	    }
  2. 	}
  3.  
  4. /*	For each "special" damage type give extra experience */
  5. 	for(i = 0; i < NATTK; i++) {
  6. 	    tmp2 = ptr->mattk[i].adtyp;
  1. 	    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:

  1. 	    else if((tmp2 == AD_DRLI) || (tmp2 == AD_STON) ||
  2. 	    		(tmp2 == AD_SLIM)) tmp += 50;

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

  1. 	    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.

Correction/Bug: This should catch every other monster attack, but tmp is used instead of tmp2. tmp would only be equal to AD_PHYS (1) if the monster is level 0, and hasn't had any tmp modifiers to this point. If effect, this gives the monster's level worth of experience for every single attack that hasn't already been covered in the if statements so far... even NO_ATTKs. So a potential of 7 * monster's level worth of experience!

  1. 		/* extra heavy damage bonus */
  2. 	    if((int)(ptr->mattk[i].damd * ptr->mattk[i].damn) > 23)
  3. 		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.

  1. 	    if (tmp2 == AD_WRAP && ptr->mlet == S_EEL && !Amphibious)
  2. 		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.

  1. 	}
  2.  
  3. /*	For certain "extra nasty" monsters, give even more */
  4. 	if (extra_nasty(ptr)) tmp += (7 * mtmp->m_lev);

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

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

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

  1. #ifdef MAIL
  2. 	/* Mail daemons put up no fight. */
  3. 	if(mtmp->data == &mons[PM_MAIL_DAEMON]) tmp = 1;
  4. #endif

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

  1. 	return(tmp);
  2. }

more_experienced[edit]

  1. void
  2. more_experienced(exp, rexp)
  3. 	register int exp, rexp;
  4. {
  5. 	u.uexp += exp;
  6. 	u.urexp += 4*exp + rexp;
  7. 	if(exp
  8. #ifdef SCORE_ON_BOTL
  9. 	   || flags.showscore
  10. #endif
  11. 	   ) flags.botl = 1;
  12. 	if (u.urexp >= (Role_if(PM_WIZARD) ? 1000 : 2000))
  13. 		flags.beginner = 0;
  14. }

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[edit]

  1. void
  2. losexp(drainer)		/* e.g., hit by drain life attack */
  3. const char *drainer;	/* cause of death, if drain should be fatal */
  4. {
  5. 	register int num;
  6.  
  7. #ifdef WIZARD
  8. 	/* override life-drain resistance when handling an explicit
  9. 	   wizard mode request to reduce level; never fatal though */
  10. 	if (drainer && !strcmp(drainer, "#levelchange"))
  11. 	    drainer = 0;
  12. 	else
  13. #endif
  14. 	    if (resists_drli(&youmonst)) return;

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

  1. 	if (u.ulevel > 1) {
  2. 		pline("%s level %d.", Goodbye(), u.ulevel--);
  3. 		/* remove intrinsic abilities */
  4. 		adjabil(u.ulevel + 1, u.ulevel);
  5. 		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.

  1. 	} else {
  2. 		if (drainer) {
  3. 			killer_format = KILLED_BY;
  4. 			killer = drainer;
  5. 			done(DIED);
  6. 		}
  7. 		/* no drainer or lifesaved */
  8. 		u.uexp = 0;
  9. 	}

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.

  1. 	num = newhp();
  2. 	u.uhpmax -= num;
  3. 	if (u.uhpmax < 1) u.uhpmax = 1;
  4. 	u.uhp -= num;
  5. 	if (u.uhp < 1) u.uhp = 1;
  6. 	else if (u.uhp > u.uhpmax) u.uhp = u.uhpmax;

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.

  1. 	if (u.ulevel < urole.xlev)
  2. 	    num = rn1((int)ACURR(A_WIS)/2 + urole.enadv.lornd + urace.enadv.lornd,
  3. 			urole.enadv.lofix + urace.enadv.lofix);
  4. 	else
  5. 	    num = rn1((int)ACURR(A_WIS)/2 + urole.enadv.hirnd + urace.enadv.hirnd,
  6. 			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.

  1. 	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.

  1. 	u.uenmax -= num;
  2. 	if (u.uenmax < 0) u.uenmax = 0;
  3. 	u.uen -= num;
  4. 	if (u.uen < 0) u.uen = 0;
  5. 	else if (u.uen > u.uenmax) u.uen = u.uenmax;

Zero energy is fine, but not negative energy.

  1. 	if (u.uexp > 0)
  2. 		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.

  1. 	flags.botl = 1;

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

  1. }

newexplevel[edit]

  1. /*
  2. * Make experience gaining similar to AD&D(tm), whereby you can at most go
  3. * up by one level at a time, extra expr possibly helping you along.
  4. * After all, how much real experience does one get shooting a wand of death
  5. * at a dragon created with a wand of polymorph??
  6. */
  7. void
  8. newexplevel()
  9. {
  10. 	if (u.ulevel < MAXULEV && u.uexp >= newuexp(u.ulevel))
  11. 	    pluslvl(TRUE);
  12. }

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[edit]

  1. void
  2. pluslvl(incr)
  3. boolean incr;	/* true iff via incremental experience growth */
  4. {		/*	(false for potion of gain level)      */
  5. 	register int num;
  6.  
  7. 	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.

  1. 	num = newhp();
  2. 	u.uhpmax += num;
  3. 	u.uhp += num;

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

  1. 	if (Upolyd) {
  2. 	    num = rnd(8);
  3. 	    u.mhmax += num;
  4. 	    u.mh += num;
  5. 	}

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.

  1. 	if (u.ulevel < urole.xlev)
  2. 	    num = rn1((int)ACURR(A_WIS)/2 + urole.enadv.lornd + urace.enadv.lornd,
  3. 			urole.enadv.lofix + urace.enadv.lofix);
  4. 	else
  5. 	    num = rn1((int)ACURR(A_WIS)/2 + urole.enadv.hirnd + urace.enadv.hirnd,
  6. 			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.

  1. 	num = enermod(num);	/* M. Stephenson */
  2. 	u.uenmax += num;
  3. 	u.uen += num;

enermod gives a bonus based on what role you are.

  1. 	if (u.ulevel < MAXULEV) {
  2. 	    if (incr) {
  3. 		long tmp = newuexp(u.ulevel + 1);
  4. 		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.

  1. 	    } else {
  2. 		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.

  1. 	    }
  2. 	    ++u.ulevel;
  3. 	    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.

  1. 	    pline("Welcome to experience level %d.", u.ulevel);
  2. 	    adjabil(u.ulevel - 1, u.ulevel);	/* give new intrinsics */
  3. 	    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.

  1. 	}
  2. 	flags.botl = 1;

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

  1. }

rndexp[edit]

  1. /* compute a random amount of experience points suitable for the hero's
  2. experience level:  base number of points needed to reach the current
  3. level plus a random portion of what it takes to get to the next level */
  4. long
  5. rndexp(gaining)
  6. boolean gaining;	/* gaining XP via potion vs setting XP for polyself */
  7. {
  8. 	long minexp, maxexp, diff, factor, result;
  9.  
  10. 	minexp = (u.ulevel == 1) ? 0L : newuexp(u.ulevel - 1);
  11. 	maxexp = newuexp(u.ulevel);
  12. 	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.

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

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

  1. 	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.

  1. 	/* 3.4.1:  if already at level 30, add to current experience
  2. 	   points rather than to threshold needed to reach the current
  3. 	   level; otherwise blessed potions of gain level can result
  4. 	   in lowering the experience points instead of raising them */
  5. 	if (u.ulevel == MAXULEV && gaining) {
  6. 	    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.

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

This guards against integer overflow.

  1. 	}
  2. 	return result;
  3. }
  4.  
  5. /*exper.c*/