Source:NetHack 3.4.3/src/write.c

From NetHackWiki
(Redirected from Source:Ref/dowrite)
Jump to: navigation, search

Below is the full text to src/write.c from NetHack 3.4.3. To link to a particular line, write [[write.c#line123]], for example.

Top of file

  1. /*	SCCS Id: @(#)write.c	3.4	2001/11/29	*/
  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. #include "hack.h"
  2.  
  3. STATIC_DCL int FDECL(cost,(struct obj *));

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.  
  9. 	if (otmp->oclass == SPBOOK_CLASS)
  10. 		return(10 * objects[otmp->otyp].oc_level);
  11.  
  12. 	switch (otmp->otyp) {
  13. # ifdef MAIL
  14. 	case SCR_MAIL:
  15. 		return(2);
  16. /*		break; */
  17. # endif
  18. 	case SCR_LIGHT:
  19. 	case SCR_GOLD_DETECTION:
  20. 	case SCR_FOOD_DETECTION:
  21. 	case SCR_MAGIC_MAPPING:
  22. 	case SCR_AMNESIA:
  23. 	case SCR_FIRE:
  24. 	case SCR_EARTH:
  25. 		return(8);
  26. /*		break; */
  27. 	case SCR_DESTROY_ARMOR:
  28. 	case SCR_CREATE_MONSTER:
  29. 	case SCR_PUNISHMENT:
  30. 		return(10);
  31. /*		break; */
  32. 	case SCR_CONFUSE_MONSTER:
  33. 		return(12);
  34. /*		break; */
  35. 	case SCR_IDENTIFY:
  36. 		return(14);
  37. /*		break; */
  38. 	case SCR_ENCHANT_ARMOR:
  39. 	case SCR_REMOVE_CURSE:
  40. 	case SCR_ENCHANT_WEAPON:
  41. 	case SCR_CHARGING:
  42. 		return(16);
  43. /*		break; */
  44. 	case SCR_SCARE_MONSTER:
  45. 	case SCR_STINKING_CLOUD:
  46. 	case SCR_TAMING:
  47. 	case SCR_TELEPORTATION:
  48. 		return(20);
  49. /*		break; */
  50. 	case SCR_GENOCIDE:
  51. 		return(30);
  52. /*		break; */
  53. 	case SCR_BLANK_PAPER:
  54. 	default:
  55. 		impossible("You can't write such a weird scroll!");
  56. 	}
  57. 	return(1000);
  58. }
  1. static NEARDATA const char write_on[] = { SCROLL_CLASS, SPBOOK_CLASS, 0 };

dowrite

  1. int
  2. dowrite(pen)
  3. register struct obj *pen;
  4. {
  5. 	register struct obj *paper;
  6. 	char namebuf[BUFSZ], *nm, *bp;
  7. 	register struct obj *new_obj;
  8. 	int basecost, actualcost;
  9. 	int curseval;
  10. 	char qbuf[QBUFSZ];
  11. 	int first, last, i;
  12. 	boolean by_descr = FALSE;
  13. 	const char *typeword;
  14.  
  15. 	if (nohands(youmonst.data)) {
  16. 	    You("need hands to be able to write!");
  17. 	    return 0;
  18. 	} else if (Glib) {
  19. 	    pline("%s from your %s.",
  20. 		  Tobjnam(pen, "slip"), makeplural(body_part(FINGER)));
  21. 	    dropx(pen);
  22. 	    return 1;
  23. 	}
  24.  
  25. 	/* get paper to write on */
  26. 	paper = getobj(write_on,"write on");
  27. 	if(!paper)
  28. 		return(0);
  29. 	typeword = (paper->oclass == SPBOOK_CLASS) ? "spellbook" : "scroll";
  30. 	if(Blind && !paper->dknown) {
  31. 		You("don't know if that %s is blank or not!", typeword);
  32. 		return(1);
  33. 	}
  34. 	paper->dknown = 1;
  35. 	if(paper->otyp != SCR_BLANK_PAPER && paper->otyp != SPE_BLANK_PAPER) {
  36. 		pline("That %s is not blank!", typeword);
  37. 		exercise(A_WIS, FALSE);
  38. 		return(1);
  39. 	}
  40.  
  41. 	/* what to write */
  42. 	Sprintf(qbuf, "What type of %s do you want to write?", typeword);
  43. 	getlin(qbuf, namebuf);
  44. 	(void)mungspaces(namebuf);	/* remove any excess whitespace */
  45. 	if(namebuf[0] == '\033' || !namebuf[0])
  46. 		return(1);
  47. 	nm = namebuf;
  48. 	if (!strncmpi(nm, "scroll ", 7)) nm += 7;
  49. 	else if (!strncmpi(nm, "spellbook ", 10)) nm += 10;
  50. 	if (!strncmpi(nm, "of ", 3)) nm += 3;
  51.  
  52. 	if ((bp = strstri(nm, " armour")) != 0) {
  53. 		(void)strncpy(bp, " armor ", 7);	/* won't add '\0' */
  54. 		(void)mungspaces(bp + 1);	/* remove the extra space */
  55. 	}
  56.  
  57. 	first = bases[(int)paper->oclass];
  58. 	last = bases[(int)paper->oclass + 1] - 1;
  59. 	for (i = first; i <= last; i++) {
  60. 		/* extra shufflable descr not representing a real object */
  61. 		if (!OBJ_NAME(objects[i])) continue;
  62.  
  63. 		if (!strcmpi(OBJ_NAME(objects[i]), nm))
  64. 			goto found;
  65. 		if (!strcmpi(OBJ_DESCR(objects[i]), nm)) {
  66. 			by_descr = TRUE;
  67. 			goto found;
  68. 		}
  69. 	}
  70.  
  71. 	There("is no such %s!", typeword);
  72. 	return 1;
  73. found:
  74.  
  75. 	if (i == SCR_BLANK_PAPER || i == SPE_BLANK_PAPER) {
  76. 		You_cant("write that!");
  77. 		pline("It's obscene!");
  78. 		return 1;
  79. 	} else if (i == SPE_BOOK_OF_THE_DEAD) {
  80. 		pline("No mere dungeon adventurer could write that.");
  81. 		return 1;
  82. 	} else if (by_descr && paper->oclass == SPBOOK_CLASS &&
  83. 		    !objects[i].oc_name_known) {
  84. 		/* can't write unknown spellbooks by description */
  85. 		pline(
  86. 		  "Unfortunately you don't have enough information to go on.");
  87. 		return 1;
  88. 	}
  89.  
  90. 	/* KMH, conduct */
  91. 	u.uconduct.literate++;
  92.  
  93. 	new_obj = mksobj(i, FALSE, FALSE);
  94. 	new_obj->bknown = (paper->bknown && pen->bknown);
  95.  
  96. 	/* shk imposes a flat rate per use, not based on actual charges used */
  97. 	check_unpaid(pen);
  98.  
  99. 	/* see if there's enough ink */
  100. 	basecost = cost(new_obj);
  101. 	if(pen->spe < basecost/2)  {
  102. 		Your("marker is too dry to write that!");
  103. 		obfree(new_obj, (struct obj *) 0);
  104. 		return(1);
  105. 	}
  106.  
  107. 	/* we're really going to write now, so calculate cost
  108. 	 */
  109. 	actualcost = rn1(basecost/2,basecost/2);
  110. 	curseval = bcsign(pen) + bcsign(paper);
  111. 	exercise(A_WIS, TRUE);
  112. 	/* dry out marker */
  113. 	if (pen->spe < actualcost) {
  114. 		pen->spe = 0;
  115. 		Your("marker dries out!");
  116. 		/* scrolls disappear, spellbooks don't */
  117. 		if (paper->oclass == SPBOOK_CLASS) {
  118. 			pline_The(
  119. 		       "spellbook is left unfinished and your writing fades.");
  120. 			update_inventory();	/* pen charges */
  121. 		} else {
  122. 			pline_The("scroll is now useless and disappears!");
  123. 			useup(paper);
  124. 		}
  125. 		obfree(new_obj, (struct obj *) 0);
  126. 		return(1);
  127. 	}
  128. 	pen->spe -= actualcost;
  129.  
  130. 	/* can't write if we don't know it - unless we're lucky */
  131. 	if(!(objects[new_obj->otyp].oc_name_known) &&
  132. 	   !(objects[new_obj->otyp].oc_uname) &&
  133. 	   (rnl(Role_if(PM_WIZARD) ? 3 : 15))) {
  134. 		You("%s to write that!", by_descr ? "fail" : "don't know how");
  135. 		/* scrolls disappear, spellbooks don't */
  136. 		if (paper->oclass == SPBOOK_CLASS) {
  137. 			You(
  138. "write in your best handwriting:  \"My Diary\", but it quickly fades.");
  139. 			update_inventory();	/* pen charges */
  140. 		} else {
  141. 			if (by_descr) {
  142. 			    Strcpy(namebuf, OBJ_DESCR(objects[new_obj->otyp]));
  143. 			    wipeout_text(namebuf, (6+MAXULEV - u.ulevel)/6, 0);
  144. 			} else
  145. 			    Sprintf(namebuf, "%s was here!", plname);
  146. 			You("write \"%s\" and the scroll disappears.", namebuf);
  147. 			useup(paper);
  148. 		}
  149. 		obfree(new_obj, (struct obj *) 0);
  150. 		return(1);
  151. 	}
  152.  
  153. 	/* useup old scroll / spellbook */
  154. 	useup(paper);
  155.  
  156. 	/* success */
  157. 	if (new_obj->oclass == SPBOOK_CLASS) {
  158. 		/* acknowledge the change in the object's description... */
  159. 		pline_The("spellbook warps strangely, then turns %s.",
  160. 		      OBJ_DESCR(objects[new_obj->otyp]));
  161. 	}
  162. 	new_obj->blessed = (curseval > 0);
  163. 	new_obj->cursed = (curseval < 0);
  164. #ifdef MAIL
  165. 	if (new_obj->otyp == SCR_MAIL) new_obj->spe = 1;
  166. #endif
  167. 	new_obj = hold_another_object(new_obj, "Oops!  %s out of your grasp!",
  168. 					       The(aobjnam(new_obj, "slip")),
  169. 					       (const char *)0);
  170. 	return(1);
  171. }
  172.  
  173. /*write.c*/