Source:NetHack 3.6.0/src/write.c

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

Below is the full text to write.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/write.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	write.c	$NHDT-Date: 1446078770 2015/10/29 00:32:50 $  $NHDT-Branch: master $:$NHDT-Revision: 1.16 $ */
  2.  /* NetHack may be freely redistributed.  See license for details. */
  3.  
  4.  #include "hack.h"
  5.  
  6.  STATIC_DCL int FDECL(cost, (struct obj *));
  7.  STATIC_DCL boolean FDECL(label_known, (int, struct obj *));
  8.  STATIC_DCL char *FDECL(new_book_description, (int, char *));
  9.  

cost

  1.  /*
  2.   * returns basecost of a scroll or a spellbook
  3.   */
  4.  STATIC_OVL int
  5.  cost(otmp)
  6.  register struct obj *otmp;
  7.  {
  8.      if (otmp->oclass == SPBOOK_CLASS)
  9.          return (10 * objects[otmp->otyp].oc_level);
  10.  
  11.      switch (otmp->otyp) {
  12.  #ifdef MAIL
  13.      case SCR_MAIL:
  14.          return 2;
  15.  #endif
  16.      case SCR_LIGHT:
  17.      case SCR_GOLD_DETECTION:
  18.      case SCR_FOOD_DETECTION:
  19.      case SCR_MAGIC_MAPPING:
  20.      case SCR_AMNESIA:
  21.      case SCR_FIRE:
  22.      case SCR_EARTH:
  23.          return 8;
  24.      case SCR_DESTROY_ARMOR:
  25.      case SCR_CREATE_MONSTER:
  26.      case SCR_PUNISHMENT:
  27.          return 10;
  28.      case SCR_CONFUSE_MONSTER:
  29.          return 12;
  30.      case SCR_IDENTIFY:
  31.          return 14;
  32.      case SCR_ENCHANT_ARMOR:
  33.      case SCR_REMOVE_CURSE:
  34.      case SCR_ENCHANT_WEAPON:
  35.      case SCR_CHARGING:
  36.          return 16;
  37.      case SCR_SCARE_MONSTER:
  38.      case SCR_STINKING_CLOUD:
  39.      case SCR_TAMING:
  40.      case SCR_TELEPORTATION:
  41.          return 20;
  42.      case SCR_GENOCIDE:
  43.          return 30;
  44.      case SCR_BLANK_PAPER:
  45.      default:
  46.          impossible("You can't write such a weird scroll!");
  47.      }
  48.      return 1000;
  49.  }
  50.  

label_known

  1.  /* decide whether the hero knowns a particular scroll's label;
  2.     unfortunately, we can't track things are haven't been added to
  3.     the discoveries list and aren't present in current inventory,
  4.     so some scrolls with ought to yield True will end up False */
  5.  STATIC_OVL boolean
  6.  label_known(scrolltype, objlist)
  7.  int scrolltype;
  8.  struct obj *objlist;
  9.  {
  10.      struct obj *otmp;
  11.  
  12.      /* only scrolls */
  13.      if (objects[scrolltype].oc_class != SCROLL_CLASS)
  14.          return FALSE;
  15.      /* type known implies full discovery; otherwise,
  16.         user-assigned name implies partial discovery */
  17.      if (objects[scrolltype].oc_name_known || objects[scrolltype].oc_uname)
  18.          return TRUE;
  19.      /* check inventory, including carried containers with known contents */
  20.      for (otmp = objlist; otmp; otmp = otmp->nobj) {
  21.          if (otmp->otyp == scrolltype && otmp->dknown)
  22.              return TRUE;
  23.          if (Has_contents(otmp) && otmp->cknown
  24.              && label_known(scrolltype, otmp->cobj))
  25.              return TRUE;
  26.      }
  27.      /* not found */
  28.      return FALSE;
  29.  }
  30.  

dowrite

  1.  static NEARDATA const char write_on[] = { SCROLL_CLASS, SPBOOK_CLASS, 0 };
  2.  
  3.  /* write -- applying a magic marker */
  4.  int
  5.  dowrite(pen)
  6.  register struct obj *pen;
  7.  {
  8.      register struct obj *paper;
  9.      char namebuf[BUFSZ], *nm, *bp;
  10.      register struct obj *new_obj;
  11.      int basecost, actualcost;
  12.      int curseval;
  13.      char qbuf[QBUFSZ];
  14.      int first, last, i, deferred, deferralchance;
  15.      boolean by_descr = FALSE;
  16.      const char *typeword;
  17.  
  18.      if (nohands(youmonst.data)) {
  19.          You("need hands to be able to write!");
  20.          return 0;
  21.      } else if (Glib) {
  22.          pline("%s from your %s.", Tobjnam(pen, "slip"),
  23.                makeplural(body_part(FINGER)));
  24.          dropx(pen);
  25.          return 1;
  26.      }
  27.  
  28.      /* get paper to write on */
  29.      paper = getobj(write_on, "write on");
  30.      if (!paper)
  31.          return 0;
  32.      /* can't write on a novel (unless/until it's been converted into a blank
  33.         spellbook), but we want messages saying so to avoid "spellbook" */
  34.      typeword = (paper->otyp == SPE_NOVEL)
  35.                    ? "book"
  36.                    : (paper->oclass == SPBOOK_CLASS)
  37.                       ? "spellbook"
  38.                       : "scroll";
  39.      if (Blind) {
  40.          if (!paper->dknown) {
  41.              You("don't know if that %s is blank or not.", typeword);
  42.              return 1;
  43.          } else if (paper->oclass == SPBOOK_CLASS) {
  44.              /* can't write a magic book while blind */
  45.              pline("%s can't create braille text.",
  46.                    upstart(ysimple_name(pen)));
  47.              return 1;
  48.          }
  49.      }
  50.      paper->dknown = 1;
  51.      if (paper->otyp != SCR_BLANK_PAPER && paper->otyp != SPE_BLANK_PAPER) {
  52.          pline("That %s is not blank!", typeword);
  53.          exercise(A_WIS, FALSE);
  54.          return 1;
  55.      }
  56.  
  57.      /* what to write */
  58.      Sprintf(qbuf, "What type of %s do you want to write?", typeword);
  59.      getlin(qbuf, namebuf);
  60.      (void) mungspaces(namebuf); /* remove any excess whitespace */
  61.      if (namebuf[0] == '\033' || !namebuf[0])
  62.          return 1;
  63.      nm = namebuf;
  64.      if (!strncmpi(nm, "scroll ", 7))
  65.          nm += 7;
  66.      else if (!strncmpi(nm, "spellbook ", 10))
  67.          nm += 10;
  68.      if (!strncmpi(nm, "of ", 3))
  69.          nm += 3;
  70.  
  71.      if ((bp = strstri(nm, " armour")) != 0) {
  72.          (void) strncpy(bp, " armor ", 7); /* won't add '\0' */
  73.          (void) mungspaces(bp + 1);        /* remove the extra space */
  74.      }
  75.  
  76.      deferred = 0;       /* not any scroll or book */
  77.      deferralchance = 0; /* incremented for each oc_uname match */
  78.      first = bases[(int) paper->oclass];
  79.      last = bases[(int) paper->oclass + 1] - 1;
  80.      for (i = first; i <= last; i++) {
  81.          /* extra shufflable descr not representing a real object */
  82.          if (!OBJ_NAME(objects[i]))
  83.              continue;
  84.  
  85.          if (!strcmpi(OBJ_NAME(objects[i]), nm))
  86.              goto found;
  87.          if (!strcmpi(OBJ_DESCR(objects[i]), nm)) {
  88.              by_descr = TRUE;
  89.              goto found;
  90.          }
  91.          /* user-assigned name might match real name of a later
  92.             entry, so we don't simply use first match with it;
  93.             also, player might assign same name multiple times
  94.             and if so, we choose one of those matches randomly */
  95.          if (objects[i].oc_uname && !strcmpi(objects[i].oc_uname, nm)
  96.              /*
  97.               * First match: chance incremented to 1,
  98.               *   !rn2(1) is 1, we remember i;
  99.               * second match: chance incremented to 2,
  100.               *   !rn2(2) has 1/2 chance to replace i;
  101.               * third match: chance incremented to 3,
  102.               *   !rn2(3) has 1/3 chance to replace i
  103.               *   and 2/3 chance to keep previous 50:50
  104.               *   choice; so on for higher match counts.
  105.               */
  106.              && !rn2(++deferralchance))
  107.              deferred = i;
  108.      }
  109.      /* writing by user-assigned name is same as by description:
  110.         fails for books, works for scrolls (having an assigned
  111.         type name guarantees presence on discoveries list) */
  112.      if (deferred) {
  113.          i = deferred;
  114.          by_descr = TRUE;
  115.          goto found;
  116.      }
  117.  
  118.      There("is no such %s!", typeword);
  119.      return 1;
  120.  found:
  121.  
  122.      if (i == SCR_BLANK_PAPER || i == SPE_BLANK_PAPER) {
  123.          You_cant("write that!");
  124.          pline("It's obscene!");
  125.          return 1;
  126.      } else if (i == SPE_BOOK_OF_THE_DEAD) {
  127.          pline("No mere dungeon adventurer could write that.");
  128.          return 1;
  129.      } else if (by_descr && paper->oclass == SPBOOK_CLASS
  130.                 && !objects[i].oc_name_known) {
  131.          /* can't write unknown spellbooks by description */
  132.          pline("Unfortunately you don't have enough information to go on.");
  133.          return 1;
  134.      }
  135.  
  136.      /* KMH, conduct */
  137.      u.uconduct.literate++;
  138.  
  139.      new_obj = mksobj(i, FALSE, FALSE);
  140.      new_obj->bknown = (paper->bknown && pen->bknown);
  141.  
  142.      /* shk imposes a flat rate per use, not based on actual charges used */
  143.      check_unpaid(pen);
  144.  
  145.      /* see if there's enough ink */
  146.      basecost = cost(new_obj);
  147.      if (pen->spe < basecost / 2) {
  148.          Your("marker is too dry to write that!");
  149.          obfree(new_obj, (struct obj *) 0);
  150.          return 1;
  151.      }
  152.  
  153.      /* we're really going to write now, so calculate cost
  154.       */
  155.      actualcost = rn1(basecost / 2, basecost / 2);
  156.      curseval = bcsign(pen) + bcsign(paper);
  157.      exercise(A_WIS, TRUE);
  158.      /* dry out marker */
  159.      if (pen->spe < actualcost) {
  160.          pen->spe = 0;
  161.          Your("marker dries out!");
  162.          /* scrolls disappear, spellbooks don't */
  163.          if (paper->oclass == SPBOOK_CLASS) {
  164.              pline_The("spellbook is left unfinished and your writing fades.");
  165.              update_inventory(); /* pen charges */
  166.          } else {
  167.              pline_The("scroll is now useless and disappears!");
  168.              useup(paper);
  169.          }
  170.          obfree(new_obj, (struct obj *) 0);
  171.          return 1;
  172.      }
  173.      pen->spe -= actualcost;
  174.  
  175.      /*
  176.       * Writing by name requires that the hero knows the scroll or
  177.       * book type.  One has previously been read (and its effect
  178.       * was evident) or been ID'd via scroll/spell/throne and it
  179.       * will be on the discoveries list.
  180.       * (Previous versions allowed scrolls and books to be written
  181.       * by type name if they were on the discoveries list via being
  182.       * given a user-assigned name, even though doing the latter
  183.       * doesn't--and shouldn't--make the actual type become known.)
  184.       *
  185.       * Writing by description requires that the hero knows the
  186.       * description (a scroll's label, that is, since books by_descr
  187.       * are rejected above).  BUG:  We can only do this for known
  188.       * scrolls and for the case where the player has assigned a
  189.       * name to put it onto the discoveries list; we lack a way to
  190.       * track other scrolls which have been seen closely enough to
  191.       * read the label without then being ID'd or named.  The only
  192.       * exception is for currently carried inventory, where we can
  193.       * check for one [with its dknown bit set] of the same type.
  194.       *
  195.       * Normal requirements can be overridden if hero is Lucky.
  196.       */
  197.  
  198.      /* if known, then either by-name or by-descr works */
  199.      if (!objects[new_obj->otyp].oc_name_known
  200.          /* else if named, then only by-descr works */
  201.          && !(by_descr && label_known(new_obj->otyp, invent))
  202.          /* and Luck might override after both checks have failed */
  203.          && rnl(Role_if(PM_WIZARD) ? 5 : 15)) {
  204.          You("%s to write that.", by_descr ? "fail" : "don't know how");
  205.          /* scrolls disappear, spellbooks don't */
  206.          if (paper->oclass == SPBOOK_CLASS) {
  207.              You(
  208.        "write in your best handwriting:  \"My Diary\", but it quickly fades.");
  209.              update_inventory(); /* pen charges */
  210.          } else {
  211.              if (by_descr) {
  212.                  Strcpy(namebuf, OBJ_DESCR(objects[new_obj->otyp]));
  213.                  wipeout_text(namebuf, (6 + MAXULEV - u.ulevel) / 6, 0);
  214.              } else
  215.                  Sprintf(namebuf, "%s was here!", plname);
  216.              You("write \"%s\" and the scroll disappears.", namebuf);
  217.              useup(paper);
  218.          }
  219.          obfree(new_obj, (struct obj *) 0);
  220.          return 1;
  221.      }
  222.      /* can write scrolls when blind, but requires luck too;
  223.         attempts to write books when blind are caught above */
  224.      if (Blind && rnl(3)) {
  225.          /* writing while blind usually fails regardless of
  226.             whether the target scroll is known; even if we
  227.             have passed the write-an-unknown scroll test
  228.             above we can still fail this one, so it's doubly
  229.             hard to write an unknown scroll while blind */
  230.          You("fail to write the scroll correctly and it disappears.");
  231.          useup(paper);
  232.          obfree(new_obj, (struct obj *) 0);
  233.          return 1;
  234.      }
  235.  
  236.      /* useup old scroll / spellbook */
  237.      useup(paper);
  238.  
  239.      /* success */
  240.      if (new_obj->oclass == SPBOOK_CLASS) {
  241.          /* acknowledge the change in the object's description... */
  242.          pline_The("spellbook warps strangely, then turns %s.",
  243.                    new_book_description(new_obj->otyp, namebuf));
  244.      }
  245.      new_obj->blessed = (curseval > 0);
  246.      new_obj->cursed = (curseval < 0);
  247.  #ifdef MAIL
  248.      if (new_obj->otyp == SCR_MAIL)
  249.          new_obj->spe = 1;
  250.  #endif
  251.      new_obj =
  252.          hold_another_object(new_obj, "Oops!  %s out of your grasp!",
  253.                              The(aobjnam(new_obj, "slip")), (const char *) 0);
  254.      return 1;
  255.  }
  256.  

new_book_description

  1.  /* most book descriptions refer to cover appearance, so we can issue a
  2.     message for converting a plain book into one of those with something
  3.     like "the spellbook turns red" or "the spellbook turns ragged";
  4.     but some descriptions refer to composition and "the book turns vellum"
  5.     looks funny, so we want to insert "into " prior to such descriptions;
  6.     even that's rather iffy, indicating that such descriptions probably
  7.     ought to be eliminated (especially "cloth"!) */
  8.  STATIC_OVL char *
  9.  new_book_description(booktype, outbuf)
  10.  int booktype;
  11.  char *outbuf;
  12.  {
  13.      /* subset of description strings from objects.c; if it grows
  14.         much, we may need to add a new flag field to objects[] instead */
  15.      static const char *const compositions[] = {
  16.          "parchment",
  17.          "vellum",
  18.          "cloth",
  19.  #if 0
  20.          "canvas", "hardcover", /* not used */
  21.          "papyrus", /* not applicable--can't be produced via writing */
  22.  #endif /*0*/
  23.          0
  24.      };
  25.      const char *descr, *const *comp_p;
  26.  
  27.      descr = OBJ_DESCR(objects[booktype]);
  28.      for (comp_p = compositions; *comp_p; ++comp_p)
  29.          if (!strcmpi(descr, *comp_p))
  30.              break;
  31.  
  32.      Sprintf(outbuf, "%s%s", *comp_p ? "into " : "", descr);
  33.      return outbuf;
  34.  }
  35.  
  36.  /*write.c*/