Source:NetHack 5.0.0/src/write.c

From NetHackWiki
Jump to navigation Jump to search

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

1. /* NetHack 5.0	write.c	$NHDT-Date: 1702023275 2023/12/08 08:14:35 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.41 $ */
2. /* NetHack may be freely redistributed.  See license for details. */
3. 
4. #include "hack.h"
5. 
6. staticfn int cost(struct obj *) NONNULLARG1;
7. staticfn int write_ok(struct obj *) NO_NNARGS;
8. staticfn char *new_book_description(int, char *) NONNULL NONNULLPTRS;
9. 
10. /*
11.  * returns base cost of a scroll or a spellbook
12.  */
13. staticfn int
14. cost(struct obj *otmp)
15. {
16.     if (otmp->oclass == SPBOOK_CLASS)
17.         return (10 * objects[otmp->otyp].oc_level);
18. 
19.     switch (otmp->otyp) {
20. #ifdef MAIL_STRUCTURES
21.     case SCR_MAIL:
22.         return 2;
23. #endif
24.     case SCR_LIGHT:
25.     case SCR_GOLD_DETECTION:
26.     case SCR_FOOD_DETECTION:
27.     case SCR_MAGIC_MAPPING:
28.     case SCR_AMNESIA:
29.     case SCR_FIRE:
30.     case SCR_EARTH:
31.         return 8;
32.     case SCR_DESTROY_ARMOR:
33.     case SCR_CREATE_MONSTER:
34.     case SCR_PUNISHMENT:
35.         return 10;
36.     case SCR_CONFUSE_MONSTER:
37.         return 12;
38.     case SCR_IDENTIFY:
39.         return 14;
40.     case SCR_ENCHANT_ARMOR:
41.     case SCR_REMOVE_CURSE:
42.     case SCR_ENCHANT_WEAPON:
43.     case SCR_CHARGING:
44.         return 16;
45.     case SCR_SCARE_MONSTER:
46.     case SCR_STINKING_CLOUD:
47.     case SCR_TAMING:
48.     case SCR_TELEPORTATION:
49.         return 20;
50.     case SCR_GENOCIDE:
51.         return 30;
52.     case SCR_BLANK_PAPER:
53.     default:
54.         impossible("You can't write such a weird scroll!");
55.     }
56.     return 1000;
57. }
58. 
59. /* getobj callback for object to write on */
60. staticfn int
61. write_ok(struct obj *obj)
62. {
63.     if (!obj || (obj->oclass != SCROLL_CLASS && obj->oclass != SPBOOK_CLASS))
64.         return GETOBJ_EXCLUDE;
65. 
66.     if (obj->otyp == SCR_BLANK_PAPER || obj->otyp == SPE_BLANK_PAPER)
67.         return GETOBJ_SUGGEST;
68. 
69.     return GETOBJ_DOWNPLAY;
70. }
71. 
72. /* write -- applying a magic marker */
73. int
74. dowrite(struct obj *pen)
75. {
76.     struct obj *paper;
77.     char namebuf[BUFSZ] = DUMMY, *nm, *bp;
78.     struct obj *new_obj;
79.     int basecost, actualcost;
80.     int curseval;
81.     char qbuf[QBUFSZ];
82.     int first, last, i, deferred, deferralchance, real;
83.     boolean by_descr = FALSE;
84.     const char *typeword;
85.     int spell_knowledge;
86. 
87.     if (nohands(gy.youmonst.data)) {
88.         You("need hands to be able to write!");
89.         return ECMD_OK;
90.     } else if (Glib) {
91.         pline("%s from your %s.", Tobjnam(pen, "slip"),
92.               fingers_or_gloves(FALSE));
93.         dropx(pen);
94.         return ECMD_TIME;
95.     }
96. 
97.     /* get paper to write on */
98.     paper = getobj("write on", write_ok, GETOBJ_NOFLAGS);
99.     if (!paper)
100.         return ECMD_CANCEL;
101.     /* can't write on a novel (unless/until it's been converted into a blank
102.        spellbook), but we want messages saying so to avoid "spellbook" */
103.     typeword = (paper->otyp == SPE_NOVEL) ? "book"
104.                : (paper->oclass == SPBOOK_CLASS) ? "spellbook"
105.                  : "scroll";
106.     if (Blind) {
107.         if (!paper->dknown) {
108.             You("don't know whether that %s is blank or not.", typeword);
109.             return ECMD_OK;
110.         } else if (paper->oclass == SPBOOK_CLASS) {
111.             /* can't write a magic book while blind */
112.             pline("%s can't create braille text.",
113.                   upstart(ysimple_name(pen)));
114.             return ECMD_OK;
115.         }
116.     }
117.     observe_object(paper);
118.     if (paper->otyp != SCR_BLANK_PAPER && paper->otyp != SPE_BLANK_PAPER) {
119.         pline("That %s is not blank!", typeword);
120.         exercise(A_WIS, FALSE);
121.         return ECMD_TIME;
122.     }
123.     makeknown(SCR_BLANK_PAPER);
124. 
125.     /* what to write */
126.     Sprintf(qbuf, "What type of %s do you want to write?", typeword);
127.     getlin(qbuf, namebuf);
128.     (void) mungspaces(namebuf); /* remove any excess whitespace */
129.     if (namebuf[0] == '\033' || !namebuf[0])
130.         return ECMD_TIME;
131.     nm = namebuf;
132.     if (!strncmpi(nm, "scroll ", 7))
133.         nm += 7;
134.     else if (!strncmpi(nm, "spellbook ", 10))
135.         nm += 10;
136.     if (!strncmpi(nm, "of ", 3))
137.         nm += 3;
138. 
139.     if ((bp = strstri(nm, " armour")) != 0) {
140.         memcpy(bp, " armor ", 7);
141.         (void) mungspaces(bp + 1);        /* remove the extra space */
142.     }
143. 
144.     deferred = real = 0; /* not any scroll or book */
145.     deferralchance = 0;  /* incremented for each oc_uname match */
146.     first = svb.bases[(int) paper->oclass];
147.     last = svb.bases[(int) paper->oclass + 1] - 1;
148.     /* first loop: look for match with name/description */
149.     for (i = first; i <= last; i++) {
150.         /* extra shufflable descr not representing a real object */
151.         if (!OBJ_NAME(objects[i]))
152.             continue;
153. 
154.         if (!strcmpi(OBJ_NAME(objects[i]), nm)) {
155.             if (objects[i].oc_name_known
156.                 /* spellbooks can only be written by_name, so no need to
157.                    hold out for a 'better' by_descr match */
158.                 || paper->oclass == SPBOOK_CLASS) {
159.                 goto found;
160.             } else {
161.                 /* save item in case there are no better by_descr matches */
162.                 real = deferred = i;
163.                 break;
164.             }
165.         }
166. 
167.         if (!strcmpi(OBJ_DESCR(objects[i]), nm)) {
168.             by_descr = TRUE;
169.             goto found;
170.         }
171.     }
172.     /* second loop: look for match with user-assigned name */
173.     /* we will get here if 'nm' isn't a real scroll name/descr, or is the name
174.      * of a real scroll that hasn't been formally IDed. */
175.     for (i = first; i <= last; i++) {
176.         /* player might assign same name multiple times and if so,
177.            we choose one of those matches randomly */
178.         if (objects[i].oc_uname && !strcmpi(objects[i].oc_uname, nm)
179.             /* prefer attempting to write the real scroll type if
180.                the typename clobbers a real scroll and is known to
181.                be incorrect */
182.             && !(real && objects[i].oc_name_known)
183.             /*
184.              * First match: chance incremented to 1,
185.              *   !rn2(1) is 1, we remember i;
186.              * second match: chance incremented to 2,
187.              *   !rn2(2) has 1/2 chance to replace i;
188.              * third match: chance incremented to 3,
189.              *   !rn2(3) has 1/3 chance to replace i
190.              *   and 2/3 chance to keep previous 50:50
191.              *   choice; so on for higher match counts.
192.              */
193.             && !rn2(++deferralchance)) {
194.             deferred = i;
195.             /* writing by user-assigned name is same as by description:
196.                fails for books, works for scrolls (having an assigned
197.                type name guarantees presence on discoveries list) */
198.             by_descr = TRUE;
199.         }
200.     }
201. 
202.     if (deferred) {
203.         i = deferred;
204.         goto found;
205.     }
206. 
207.     There("is no such %s!", typeword);
208.     return ECMD_TIME;
209.  found:
210. 
211.     if (i == SCR_BLANK_PAPER || i == SPE_BLANK_PAPER) {
212.         You_cant("write that!");
213.         pline("It's obscene!");
214.         return ECMD_TIME;
215.     } else if (i == SPE_NOVEL) {
216.         boolean fanfic = !rn2(3), tearup = !rn2(3);
217. 
218.         if (!fanfic) {
219.             You("%s to write the Great Yendorian Novel, but %s inspiration.",
220.                 !tearup ? "prepare" : "try",
221.                 !Hallucination ? "lack" : "have too much");
222.         } else {
223.             You("%sproduce really %s fan-fiction.",
224.                 !tearup ? "start to " : "",
225.                 !Hallucination ? "lame" : "awesome");
226.         }
227.         if (!tearup) {
228.             You("give up on the idea.");
229.         } else {
230.             You("tear it up.");
231.             useup(paper);
232.         }
233.         return ECMD_TIME;
234.     } else if (i == SPE_BOOK_OF_THE_DEAD) {
235.         pline("No mere dungeon adventurer could write that.");
236.         return ECMD_TIME;
237.     } else if (by_descr && paper->oclass == SPBOOK_CLASS
238.                && !objects[i].oc_name_known) {
239.         /* can't write unknown spellbooks by description */
240.         pline("Unfortunately you don't have enough information to go on.");
241.         return ECMD_TIME;
242.     }
243. 
244.     /* KMH, conduct */
245.     if (!u.uconduct.literate++)
246.         livelog_printf(LL_CONDUCT,
247.                        "became literate by writing %s", an(typeword));
248. 
249.     new_obj = mksobj(i, FALSE, FALSE);
250.     new_obj->bknown = (paper->bknown && pen->bknown);
251. 
252.     /* shk imposes a flat rate per use, not based on actual charges used */
253.     check_unpaid(pen);
254. 
255.     /* see if there's enough ink */
256.     basecost = cost(new_obj);
257.     if (pen->spe < basecost / 2) {
258.         Your("marker is too dry to write that!");
259.         obfree(new_obj, (struct obj *) 0);
260.         return ECMD_TIME;
261.     }
262. 
263.     /* we're really going to write now, so calculate cost
264.      */
265.     actualcost = rn1(basecost / 2, basecost / 2);
266.     curseval = bcsign(pen) + bcsign(paper);
267.     exercise(A_WIS, TRUE);
268.     /* dry out marker */
269.     if (pen->spe < actualcost) {
270.         pen->spe = 0;
271.         Your("marker dries out!");
272.         /* scrolls disappear, spellbooks don't */
273.         if (paper->oclass == SPBOOK_CLASS) {
274.             pline_The("spellbook is left unfinished and your writing fades.");
275.             update_inventory(); /* pen charges */
276.         } else {
277.             pline_The("scroll is now useless and disappears!");
278.             useup(paper);
279.         }
280.         obfree(new_obj, (struct obj *) 0);
281.         return ECMD_TIME;
282.     }
283.     pen->spe -= actualcost;
284. 
285.     /*
286.      * Writing by name requires that the hero knows the scroll or
287.      * book type.  One has previously been read (and its effect
288.      * was evident) or been ID'd via scroll/spell/throne (or skill
289.      * for Wizards) and it will be on the discoveries list.
290.      * Unknown spellbooks can also be written by name if the hero
291.      * has fresh knowledge of the spell, or if the spell is almost
292.      * forgotten and the hero is Lucky (with a greater chance than
293.      * if the spell is unknown or forgotten).
294.      * (Previous versions allowed scrolls and books to be written
295.      * by type name if they were on the discoveries list via being
296.      * given a user-assigned name, even though doing the latter
297.      * doesn't--and shouldn't--make the actual type become known.)
298.      *
299.      * Writing by description requires that the hero knows the
300.      * description (a scroll's label, that is, since books by_descr
301.      * are rejected above).  This is done by checking to see if a
302.      * scroll with the same description has been encountered.
303.      *
304.      * Normal requirements can be overridden if hero is Lucky.
305.      */
306. 
307.     if (paper->oclass == SPBOOK_CLASS) {
308.         spell_knowledge = known_spell(new_obj->otyp);
309.     } else {
310.         spell_knowledge = spe_Unknown;
311.     }
312.     /* if known, then either by-name or by-descr works */
313.     if (!objects[new_obj->otyp].oc_name_known
314.         /* else if named, then only by-descr works */
315.         && !(by_descr && objects[new_obj->otyp].oc_encountered)
316.         /* else fresh knowledge of the spell works */
317.         && spell_knowledge != spe_Fresh
318.         /* and Luck might override after previous checks have failed */
319.         && rnl(((Role_if(PM_WIZARD) && paper->oclass != SPBOOK_CLASS)
320.                 || spell_knowledge == spe_GoingStale)
321.                ? 5 : 15)) {
322.         You("%s to write that.", by_descr ? "fail" : "don't know how");
323.         /* scrolls disappear, spellbooks don't */
324.         if (paper->oclass == SPBOOK_CLASS) {
325.             You(
326.       "write in your best handwriting:  \"My Diary\", but it quickly fades.");
327.             update_inventory(); /* pen charges */
328.         } else {
329.             if (by_descr) {
330.                 Strcpy(namebuf, OBJ_DESCR(objects[new_obj->otyp]));
331.                 wipeout_text(namebuf, (6 + MAXULEV - u.ulevel) / 6, 0);
332.             } else
333.                 Sprintf(namebuf, "%s was here!", svp.plname);
334.             You("write \"%s\" and the scroll disappears.", namebuf);
335.             useup(paper);
336.         }
337.         obfree(new_obj, (struct obj *) 0);
338.         return ECMD_TIME;
339.     }
340.     /* can write scrolls when blind, but requires luck too;
341.        attempts to write books when blind are caught above */
342.     if (Blind && rnl(3)) {
343.         /* writing while blind usually fails regardless of
344.            whether the target scroll is known; even if we
345.            have passed the write-an-unknown scroll test
346.            above we can still fail this one, so it's doubly
347.            hard to write an unknown scroll while blind */
348.         You("fail to write the scroll correctly and it disappears.");
349.         useup(paper);
350.         obfree(new_obj, (struct obj *) 0);
351.         return ECMD_TIME;
352.     }
353. 
354.     /* use up old scroll / spellbook */
355.     useup(paper);
356. 
357.     /* success */
358.     if (new_obj->oclass == SPBOOK_CLASS) {
359.         /* acknowledge the change in the object's description... */
360.         pline_The("spellbook warps strangely, then turns %s.",
361.                   new_book_description(new_obj->otyp, namebuf));
362.     }
363.     new_obj->blessed = (curseval > 0);
364.     new_obj->cursed = (curseval < 0);
365. #ifdef MAIL_STRUCTURES
366.     if (new_obj->otyp == SCR_MAIL)
367.         /* 0: delivered in-game via external event (or randomly for fake mail);
368.            1: from bones or wishing; 2: written with marker */
369.         new_obj->spe = 2;
370. #endif
371.     /* unlike alchemy, for example, a successful result yields the
372.        specifically chosen item so hero recognizes it even if blind;
373.        the exception is for being lucky writing an undiscovered scroll,
374.        where the label associated with the type-name isn't known yet;
375.        but if writing by description, the description is always known */
376.     new_obj->dknown = FALSE;
377.     if (objects[new_obj->otyp].oc_name_known || by_descr)
378.         observe_object(new_obj);
379. 
380.     new_obj = hold_another_object(new_obj, "Oops!  %s out of your grasp!",
381.                                   The(aobjnam(new_obj, "slip")),
382.                                   (const char *) 0);
383.     nhUse(new_obj); /* try to avoid complaint about dead assignment */
384.     return ECMD_TIME;
385. }
386. 
387. /* most book descriptions refer to cover appearance, so we can issue a
388.    message for converting a plain book into one of those with something
389.    like "the spellbook turns red" or "the spellbook turns ragged";
390.    but some descriptions refer to composition and "the book turns vellum"
391.    looks funny, so we want to insert "into " prior to such descriptions;
392.    even that's rather iffy, indicating that such descriptions probably
393.    ought to be eliminated (especially "cloth"!) */
394. staticfn char *
395. new_book_description(int booktype, char *outbuf)
396. {
397.     /* subset of description strings from objects.c; if it grows
398.        much, we may need to add a new flag field to objects[] instead */
399.     static const char *const compositions[] = {
400.         "parchment",
401.         "vellum",
402.         "cloth",
403. #if 0
404.         "canvas", "hardcover", /* not used */
405.         "papyrus", /* not applicable--can't be produced via writing */
406. #endif /*0*/
407.         0
408.     };
409.     const char *descr, *const *comp_p;
410. 
411.     descr = OBJ_DESCR(objects[booktype]);
412.     for (comp_p = compositions; *comp_p; ++comp_p)
413.         if (!strcmpi(descr, *comp_p))
414.             break;
415. 
416.     Sprintf(outbuf, "%s%s", *comp_p ? "into " : "", descr);
417.     return outbuf;
418. }
419. 
420. /*write.c*/