Source:NetHack 3.1.0/write.c

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

Warning! This is the source code from an old release. For the latest release, see Source code

1.   /*	SCCS Id: @(#)write.c	3.1	91/01/04 2.   /* NetHack may be freely redistributed. See license for details. */ 3.     4.    #include "hack.h"  5. 6.   static int FDECL(cost,(struct obj *)); 7.    8.    /*  9.     * returns basecost of a scroll or a spellbook 10.   */  11.   static int 12.  cost(otmp) 13.  register struct obj *otmp; 14.  {  15.    16.   	if (otmp->oclass == SPBOOK_CLASS) 17.  		return(10 * objects[otmp->otyp].oc_level); 18.   19.   	switch(otmp->otyp)  { 20.  # ifdef MAIL 21.  	case SCR_MAIL: 22.  		return(2); 23.  /*		break; */ 24.  # endif 25.  	case SCR_LIGHT: 26.  	case SCR_GOLD_DETECTION: 27.  	case SCR_FOOD_DETECTION: 28.  	case SCR_MAGIC_MAPPING: 29.  	case SCR_AMNESIA: 30.  	case SCR_FIRE: 31.  		return(8); 32.  /*		break; */ 33.  	case SCR_DESTROY_ARMOR: 34.  	case SCR_CREATE_MONSTER: 35.  	case SCR_PUNISHMENT: 36.  		return(10); 37.  /*		break; */ 38.  	case SCR_CONFUSE_MONSTER: 39.  		return(12); 40.  /*		break; */ 41.  	case SCR_IDENTIFY: 42.  		return(14); 43.  /*		break; */ 44.  	case SCR_ENCHANT_ARMOR: 45.  	case SCR_REMOVE_CURSE: 46.  	case SCR_ENCHANT_WEAPON: 47.  	case SCR_CHARGING: 48.  		return(16); 49.  /*		break; */ 50.  	case SCR_SCARE_MONSTER: 51.  	case SCR_TAMING: 52.  	case SCR_TELEPORTATION: 53.  		return(20); 54.  /*		break; */ 55.  	case SCR_GENOCIDE: 56.  		return(30); 57.  /*		break; */ 58.  	case SCR_BLANK_PAPER: 59.  	default: 60.  		impossible("You can't write such a weird scroll!"); 61.  	}  62.   	return(1000); 63.  }  64.    65.   static const char NEARDATA write_on[] = { SCROLL_CLASS, SPBOOK_CLASS, 0 }; 66.   67.   int 68.  dowrite(pen) 69.  register struct obj *pen; 70.  {  71.   	register struct obj *paper; 72.  	char namebuf[BUFSZ], scrbuf[BUFSZ]; 73.  	register struct obj *new_obj; 74.  	int basecost, actualcost; 75.  	int curseval; 76.  	char qbuf[QBUFSZ]; 77.  	  78.   	if(!pen) 79.  		return(0); 80.  	/* already tested before only call of dowrite (from doapply) 81.  	if(pen->otyp != MAGIC_MARKER)  { 82.  		You("can't write with that!"); 83.  		return(0); 84.  	}  85.   	*/  86.    87.   	/* get paper to write on */ 88.  	paper = getobj(write_on,"write on"); 89.  	if(!paper) 90.  		return(0); 91.  	if(Blind && !paper->dknown) { 92.  		You("can't tell if that %s's blank or not!",  93.   		      paper->oclass == SPBOOK_CLASS ? "spellbook" :  94.   		      "scroll"); 95.  		return(1); 96.  	}  97.   	paper->dknown = 1; 98.  	if(paper->otyp != SCR_BLANK_PAPER && paper->otyp != SPE_BLANK_PAPER) { 99.  		You("fool, that %s's not blank!",  100.  		    paper->oclass == SPBOOK_CLASS ? "spellbook" :  101.  		    "scroll"); 102. 		return(1); 103. 	}  104.   105.  	/* what to write */ 106. 	Sprintf(qbuf, "What type of %s do you want to write? ",  107.  	      paper->oclass == SPBOOK_CLASS ? "spellbook" :  108.  	      "scroll"); 109. 	getlin(qbuf, namebuf); 110. 	if(namebuf[0] == '\033' || !namebuf[0]) 111. 		return(1); 112. 	scrbuf[0] = '\0'; 113. 	if (paper->oclass == SPBOOK_CLASS) { 114. 		if(strncmp(namebuf,"spellbook of ",13) != 0) 115. 			Strcpy(scrbuf,"spellbook of "); 116. 	}  117.  	else if(strncmp(namebuf,"scroll of ",10) != 0) 118. 		Strcpy(scrbuf,"scroll of "); 119. 	Strcat(scrbuf,namebuf); 120. 	new_obj = readobjnam(scrbuf); 121.  122.  	new_obj->bknown = (paper->bknown && pen->bknown); 123.  124.  	if((new_obj->oclass != SCROLL_CLASS || 125. 	              new_obj->otyp == SCR_BLANK_PAPER)  126.  	    && (new_obj->oclass != SPBOOK_CLASS || 127.                       new_obj->otyp == SPE_BLANK_PAPER)) { 128. 		You("can't write that!"); 129. 		pline("It's obscene!"); 130. 		obfree(new_obj, (struct obj *) 0); /* pb@ethz.uucp */ 131. 		return(1); 132. 	}  133.   134.  	/* see if there's enough ink */ 135. 	basecost = cost(new_obj); 136. 	if(pen->spe < basecost/2)  { 137. 		Your("marker is too dry to write that!"); 138. 		obfree(new_obj, (struct obj *) 0); 139. 		return(1); 140. 	}  141.   142.  	/* we're really going to write now, so calculate cost 143. 	 */  144.  	actualcost = rn1(basecost/2,basecost/2); 145. 	curseval = bcsign(pen) + bcsign(paper); 146. 	exercise(A_WIS, TRUE); 147. 	/* dry out marker */ 148. 	if(pen->spe < actualcost)  { 149. 		Your("marker dries out!"); 150. 		/* scrolls disappear, spellbooks don't */ 151. 		if (paper->oclass == SPBOOK_CLASS) 152. 			pline("The spellbook is left unfinished."); 153. 		else { 154. 			pline("The scroll is now useless and disappears!"); 155. 			useup(paper); 156. 		}  157.  		pen->spe = 0; 158. 		obfree(new_obj, (struct obj *) 0); 159. 		return(1); 160. 	}  161.  	pen->spe -= actualcost; 162.  163.  	/* can't write if we don't know it - unless we're lucky */ 164. 	if(!(objects[new_obj->otyp].oc_name_known) &&  165.  	   !(objects[new_obj->otyp].oc_uname) &&  166.  	   (rnl(pl_character[0] == 'W' ? 3 : 15))) { 167. 		You("don't know how to write that!"); 168. 		/* scrolls disappear, spellbooks don't */ 169. 		if (paper->oclass == SPBOOK_CLASS) 170. 			You("write in your best handwriting:  \"My Diary\"."); 171. 		else { 172. 			You("write \"%s was here!\" and the scroll disappears.",plname); 173. 			useup(paper); 174. 		}  175.  		obfree(new_obj, (struct obj *) 0); 176. 		return(1); 177. 	}  178.   179.  	/* useup old scroll / spellbook */ 180. 	useup(paper); 181.  182.  	/* now you know it! */ 183.  	makeknown(new_obj->otyp); 184.  185.  	/* success */ 186. 	new_obj = addinv(new_obj); 187. 	new_obj->blessed = (curseval > 0); 188. 	new_obj->cursed = (curseval < 0); 189. 	prinv(NULL, new_obj, 1L); 190. #ifdef MAIL 191. 	if (new_obj->otyp == SCR_MAIL) new_obj->spe = 1; 192. #endif 193. 	new_obj->known = 1; 194. 	return(1); 195. }  196.   197.  /*write.c*/