Source:NetHack 3.4.3/src/exper.c

From NetHackWiki
Revision as of 04:08, 5 April 2012 by Erica (talk | contribs) (missing one attack type <= AT_BUTT)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to 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

/*	SCCS Id: @(#)exper.c	3.4	2002/11/20	*/
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* 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.

#include "hack.h"

STATIC_DCL long FDECL(newuexp, (int));
STATIC_DCL int FDECL(enermod, (int));

newuexp

STATIC_OVL long
newuexp(lev)
int lev;
{
	if (lev < 10) return (10L * (1L << lev));
	if (lev < 20) return (10000L * (1L << (lev - 10)));
	return (10000000L * ((long)(lev - 19)));
}

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

STATIC_OVL int
enermod(en)
int en;
{
	switch (Role_switch) {
	case PM_PRIEST:
	case PM_WIZARD:
	    return(2 * en);
	case PM_HEALER:
	case PM_KNIGHT:
	    return((3 * en) / 2);
	case PM_BARBARIAN:
	case PM_VALKYRIE:
	    return((3 * en) / 4);
	default:
	    return (en);
	}
}

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

int
experience(mtmp, nk)	/* return # of exp points for mtmp after nk killed */
	register struct	monst *mtmp;
	register int	nk;
#if defined(macintosh) && (defined(__SC__) || defined(__MRC__))
# pragma unused(nk)
#endif
{
	register struct permonst *ptr = mtmp->data;
	int	i, tmp, tmp2;

	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.

/*	For higher ac values, give extra experience */
	if ((i = find_mac(mtmp)) < 3) tmp += (7 - i) * ((i < 0) ? 2 : 1);

/*	For very fast monsters, give extra experience */
	if (ptr->mmove > NORMAL_SPEED)
	    tmp += (ptr->mmove > (3*NORMAL_SPEED/2)) ? 5 : 3;

/*	For each "special" attack type give extra experience */
	for(i = 0; i < NATTK; i++) {

	    tmp2 = ptr->mattk[i].aatyp;
	    if(tmp2 > AT_BUTT) {

		if(tmp2 == AT_WEAP) tmp += 5;
		else if(tmp2 == AT_MAGC) tmp += 10;
		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.

	    }
	}

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

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

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

	    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!

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

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

	}

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

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

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

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

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

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

	return(tmp);
}

more_experienced

void
more_experienced(exp, rexp)
	register int exp, rexp;
{
	u.uexp += exp;
	u.urexp += 4*exp + rexp;
	if(exp
#ifdef SCORE_ON_BOTL
	   || flags.showscore
#endif
	   ) flags.botl = 1;
	if (u.urexp >= (Role_if(PM_WIZARD) ? 1000 : 2000))
		flags.beginner = 0;
}

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

void
losexp(drainer)		/* e.g., hit by drain life attack */
const char *drainer;	/* cause of death, if drain should be fatal */
{
	register int num;

#ifdef WIZARD
	/* override life-drain resistance when handling an explicit
	   wizard mode request to reduce level; never fatal though */
	if (drainer && !strcmp(drainer, "#levelchange"))
	    drainer = 0;
	else
#endif
	    if (resists_drli(&youmonst)) return;

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

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

	} else {
		if (drainer) {
			killer_format = KILLED_BY;
			killer = drainer;
			done(DIED);
		}
		/* no drainer or lifesaved */
		u.uexp = 0;
	}

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.

	num = newhp();
	u.uhpmax -= num;
	if (u.uhpmax < 1) u.uhpmax = 1;
	u.uhp -= num;
	if (u.uhp < 1) u.uhp = 1;
	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.

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

	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.

	u.uenmax -= num;
	if (u.uenmax < 0) u.uenmax = 0;
	u.uen -= num;
	if (u.uen < 0) u.uen = 0;
	else if (u.uen > u.uenmax) u.uen = u.uenmax;

Zero energy is fine, but not negative energy.

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

	flags.botl = 1;

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

}

newexplevel

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

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

void
pluslvl(incr)
boolean incr;	/* true iff via incremental experience growth */
{		/*	(false for potion of gain level)      */
	register int num;

	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.

	num = newhp();
	u.uhpmax += num;
	u.uhp += num;

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

	if (Upolyd) {
	    num = rnd(8);
	    u.mhmax += num;
	    u.mh += num;
	}

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.

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

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

enermod gives a bonus based on what role you are.

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

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

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

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

	}
	flags.botl = 1;

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

}

rndexp

/* compute a random amount of experience points suitable for the hero's
experience level:  base number of points needed to reach the current
level plus a random portion of what it takes to get to the next level */
long
rndexp(gaining)
boolean gaining;	/* gaining XP via potion vs setting XP for polyself */
{
	long minexp, maxexp, diff, factor, result;

	minexp = (u.ulevel == 1) ? 0L : newuexp(u.ulevel - 1);
	maxexp = newuexp(u.ulevel);
	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.

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

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

	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.

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

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

This guards against integer overflow.

	}
	return result;
}

/*exper.c*/