Source:NetHack 3.6.0/src/exper.c

From NetHackWiki
(Redirected from Exper.c)
Jump to: navigation, search

Below is the full text to exper.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/exper.c#line123]], 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.

Top of file

  1.  /* NetHack 3.6	exper.c	$NHDT-Date: 1446975467 2015/11/08 09:37:47 $  $NHDT-Branch: master $:$NHDT-Revision: 1.26 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  #include "hack.h"
  6.  #include <limits.h>
  7.  
  8.  STATIC_DCL long FDECL(newuexp, (int));
  9.  STATIC_DCL int FDECL(enermod, (int));
  10.  

newuexp

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

enermod

  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.  }
  19.  

newpw

  1.  /* calculate spell power/energy points for new level */
  2.  int
  3.  newpw()
  4.  {
  5.      int en = 0, enrnd, enfix;
  6.  
  7.      if (u.ulevel == 0) {
  8.          en = urole.enadv.infix + urace.enadv.infix;
  9.          if (urole.enadv.inrnd > 0)
  10.              en += rnd(urole.enadv.inrnd);
  11.          if (urace.enadv.inrnd > 0)
  12.              en += rnd(urace.enadv.inrnd);
  13.      } else {
  14.          enrnd = (int) ACURR(A_WIS) / 2;
  15.          if (u.ulevel < urole.xlev) {
  16.              enrnd += urole.enadv.lornd + urace.enadv.lornd;
  17.              enfix = urole.enadv.lofix + urace.enadv.lofix;
  18.          } else {
  19.              enrnd += urole.enadv.hirnd + urace.enadv.hirnd;
  20.              enfix = urole.enadv.hifix + urace.enadv.hifix;
  21.          }
  22.          en = enermod(rn1(enrnd, enfix));
  23.      }
  24.      if (en <= 0)
  25.          en = 1;
  26.      if (u.ulevel < MAXULEV)
  27.          u.ueninc[u.ulevel] = (xchar) en;
  28.      return en;
  29.  }
  30.  

experience

  1.  /* return # of exp points for mtmp after nk killed */
  2.  int
  3.  experience(mtmp, nk)
  4.  register struct monst *mtmp;
  5.  register int nk;
  6.  {
  7.      register struct permonst *ptr = mtmp->data;
  8.      int i, tmp, tmp2;
  9.  
  10.      tmp = 1 + mtmp->m_lev * mtmp->m_lev;
  11.  
  12.      /*  For higher ac values, give extra experience */
  13.      if ((i = find_mac(mtmp)) < 3)
  14.          tmp += (7 - i) * ((i < 0) ? 2 : 1);
  15.  
  16.      /*  For very fast monsters, give extra experience */
  17.      if (ptr->mmove > NORMAL_SPEED)
  18.          tmp += (ptr->mmove > (3 * NORMAL_SPEED / 2)) ? 5 : 3;
  19.  
  20.      /*  For each "special" attack type give extra experience */
  21.      for (i = 0; i < NATTK; i++) {
  22.          tmp2 = ptr->mattk[i].aatyp;
  23.          if (tmp2 > AT_BUTT) {
  24.              if (tmp2 == AT_WEAP)
  25.                  tmp += 5;
  26.              else if (tmp2 == AT_MAGC)
  27.                  tmp += 10;
  28.              else
  29.                  tmp += 3;
  30.          }
  31.      }
  32.  
  33.      /*  For each "special" damage type give extra experience */
  34.      for (i = 0; i < NATTK; i++) {
  35.          tmp2 = ptr->mattk[i].adtyp;
  36.          if (tmp2 > AD_PHYS && tmp2 < AD_BLND)
  37.              tmp += 2 * mtmp->m_lev;
  38.          else if ((tmp2 == AD_DRLI) || (tmp2 == AD_STON) || (tmp2 == AD_SLIM))
  39.              tmp += 50;
  40.          else if (tmp2 != AD_PHYS)
  41.              tmp += mtmp->m_lev;
  42.          /* extra heavy damage bonus */
  43.          if ((int) (ptr->mattk[i].damd * ptr->mattk[i].damn) > 23)
  44.              tmp += mtmp->m_lev;
  45.          if (tmp2 == AD_WRAP && ptr->mlet == S_EEL && !Amphibious)
  46.              tmp += 1000;
  47.      }
  48.  
  49.      /*  For certain "extra nasty" monsters, give even more */
  50.      if (extra_nasty(ptr))
  51.          tmp += (7 * mtmp->m_lev);
  52.  
  53.      /*  For higher level monsters, an additional bonus is given */
  54.      if (mtmp->m_lev > 8)
  55.          tmp += 50;
  56.  
  57.  #ifdef MAIL
  58.      /* Mail daemons put up no fight. */
  59.      if (mtmp->data == &mons[PM_MAIL_DAEMON])
  60.          tmp = 1;
  61.  #endif
  62.  
  63.      if (mtmp->mrevived || mtmp->mcloned) {
  64.          /*
  65.           *      Reduce experience awarded for repeated killings of
  66.           *      "the same monster".  Kill count includes all of this
  67.           *      monster's type which have been killed--including the
  68.           *      current monster--regardless of how they were created.
  69.           *        1.. 20        full experience
  70.           *       21.. 40        xp / 2
  71.           *       41.. 80        xp / 4
  72.           *       81..120        xp / 8
  73.           *      121..180        xp / 16
  74.           *      181..240        xp / 32
  75.           *      241..255+       xp / 64
  76.           */
  77.          for (i = 0, tmp2 = 20; nk > tmp2 && tmp > 1; ++i) {
  78.              tmp = (tmp + 1) / 2;
  79.              nk -= tmp2;
  80.              if (i & 1)
  81.                  tmp2 += 20;
  82.          }
  83.      }
  84.  
  85.      return (tmp);
  86.  }
  87.  

more_experienced

  1.  void
  2.  more_experienced(exper, rexp)
  3.  register int exper, rexp;
  4.  {
  5.      long newexp = u.uexp + exper;
  6.      long rexpincr = 4 * exper + rexp;
  7.      long newrexp = u.urexp + rexpincr;
  8.  
  9.      /* cap experience and score on wraparound */
  10.      if (newexp < 0 && exper > 0)
  11.          newexp = LONG_MAX;
  12.      if (newrexp < 0 && rexpincr > 0)
  13.          newrexp = LONG_MAX;
  14.      u.uexp = newexp;
  15.      u.urexp = newrexp;
  16.  
  17.      if (exper
  18.  #ifdef SCORE_ON_BOTL
  19.          || flags.showscore
  20.  #endif
  21.          )
  22.          context.botl = 1;
  23.      if (u.urexp >= (Role_if(PM_WIZARD) ? 1000 : 2000))
  24.          flags.beginner = 0;
  25.  }
  26.  

losexp

  1.  /* e.g., hit by drain life attack */
  2.  void
  3.  losexp(drainer)
  4.  const char *drainer; /* cause of death, if drain should be fatal */
  5.  {
  6.      register int num;
  7.  
  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 if (resists_drli(&youmonst))
  13.          return;
  14.  
  15.      if (u.ulevel > 1) {
  16.          pline("%s level %d.", Goodbye(), u.ulevel--);
  17.          /* remove intrinsic abilities */
  18.          adjabil(u.ulevel + 1, u.ulevel);
  19.          reset_rndmonst(NON_PM); /* new monster selection */
  20.      } else {
  21.          if (drainer) {
  22.              killer.format = KILLED_BY;
  23.              if (killer.name != drainer)
  24.                  Strcpy(killer.name, drainer);
  25.              done(DIED);
  26.          }
  27.          /* no drainer or lifesaved */
  28.          u.uexp = 0;
  29.      }
  30.      num = (int) u.uhpinc[u.ulevel];
  31.      u.uhpmax -= num;
  32.      if (u.uhpmax < 1)
  33.          u.uhpmax = 1;
  34.      u.uhp -= num;
  35.      if (u.uhp < 1)
  36.          u.uhp = 1;
  37.      else if (u.uhp > u.uhpmax)
  38.          u.uhp = u.uhpmax;
  39.  
  40.      num = (int) u.ueninc[u.ulevel];
  41.      u.uenmax -= num;
  42.      if (u.uenmax < 0)
  43.          u.uenmax = 0;
  44.      u.uen -= num;
  45.      if (u.uen < 0)
  46.          u.uen = 0;
  47.      else if (u.uen > u.uenmax)
  48.          u.uen = u.uenmax;
  49.  
  50.      if (u.uexp > 0)
  51.          u.uexp = newuexp(u.ulevel) - 1;
  52.  
  53.      if (Upolyd) {
  54.          num = monhp_per_lvl(&youmonst);
  55.          u.mhmax -= num;
  56.          u.mh -= num;
  57.          if (u.mh <= 0)
  58.              rehumanize();
  59.      }
  60.  
  61.      context.botl = 1;
  62.  }
  63.  

newexplevel

  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.  }
  13.  

pluslvl

  1.  void
  2.  pluslvl(incr)
  3.  boolean incr; /* true iff via incremental experience growth */
  4.  {             /*        (false for potion of gain level)    */
  5.      int hpinc, eninc;
  6.  
  7.      if (!incr)
  8.          You_feel("more experienced.");
  9.  
  10.      /* increase hit points (when polymorphed, do monster form first
  11.         in order to retain normal human/whatever increase for later) */
  12.      if (Upolyd) {
  13.          hpinc = monhp_per_lvl(&youmonst);
  14.          u.mhmax += hpinc;
  15.          u.mh += hpinc;
  16.      }
  17.      hpinc = newhp();
  18.      u.uhpmax += hpinc;
  19.      u.uhp += hpinc;
  20.  
  21.      /* increase spell power/energy points */
  22.      eninc = newpw();
  23.      u.uenmax += eninc;
  24.      u.uen += eninc;
  25.  
  26.      /* increase level (unless already maxxed) */
  27.      if (u.ulevel < MAXULEV) {
  28.          /* increase experience points to reflect new level */
  29.          if (incr) {
  30.              long tmp = newuexp(u.ulevel + 1);
  31.              if (u.uexp >= tmp)
  32.                  u.uexp = tmp - 1;
  33.          } else {
  34.              u.uexp = newuexp(u.ulevel);
  35.          }
  36.          ++u.ulevel;
  37.          if (u.ulevelmax < u.ulevel)
  38.              u.ulevelmax = u.ulevel;
  39.          pline("Welcome to experience level %d.", u.ulevel);
  40.          adjabil(u.ulevel - 1, u.ulevel); /* give new intrinsics */
  41.          reset_rndmonst(NON_PM);          /* new monster selection */
  42.      }
  43.      context.botl = 1;
  44.  }
  45.  

rndexp

  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;
  13.      /* make sure that `diff' is an argument which rn2() can handle */
  14.      while (diff >= (long) LARGEST_INT)
  15.          diff /= 2L, factor *= 2L;
  16.      result = minexp + factor * (long) rn2((int) diff);
  17.      /* 3.4.1:  if already at level 30, add to current experience
  18.         points rather than to threshold needed to reach the current
  19.         level; otherwise blessed potions of gain level can result
  20.         in lowering the experience points instead of raising them */
  21.      if (u.ulevel == MAXULEV && gaining) {
  22.          result += (u.uexp - minexp);
  23.          /* avoid wrapping (over 400 blessed potions needed for that...) */
  24.          if (result < u.uexp)
  25.              result = u.uexp;
  26.      }
  27.      return result;
  28.  }
  29.  
  30.  /*exper.c*/