Source:NetHack 3.6.1/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.1. To link to a particular line, write [[Source:NetHack 3.6.1/src/write.c#line123]], for example.

Top of file

  1.  /* NetHack 3.6	write.c	$NHDT-Date: 1450261366 2015/12/16 10:22:46 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.17 $ */
  2.  /* 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.  
  2.  #include "hack.h"
  3.  
  4.  STATIC_DCL int FDECL(cost, (struct obj *));
  5.  STATIC_DCL boolean FDECL(label_known, (int, struct obj *));
  6.  STATIC_DCL char *FDECL(new_book_description, (int, char *));
  7.  

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 that 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.  
  31.  static NEARDATA const char write_on[] = { SCROLL_CLASS, SPBOOK_CLASS, 0 };
  32.  

dowrite

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

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