Source:NetHack 5.0.0/src/questpgr.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to questpgr.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/questpgr.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	questpgr.c	$NHDT-Date: 1704043695 2023/12/31 17:28:15 $  $NHDT-Branch: keni-luabits2 $:$NHDT-Revision: 1.87 $ */
2. /*      Copyright 1991, M. Stephenson                             */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. #include "dlb.h"
7. 
8. /*  quest-specific pager routines. */
9. 
10. #define QTEXT_FILE "quest.lua"
11. 
12. #ifdef TTY_GRAPHICS
13. #include "wintty.h"
14. #endif
15. 
16. staticfn const char *intermed(void);
17. /* sometimes find_qarti(gi.invent), and gi.invent can be null */
18. staticfn struct obj *find_qarti(struct obj *) NO_NNARGS;
19. staticfn const char *neminame(void);
20. staticfn const char *guardname(void);
21. staticfn const char *homebase(void);
22. staticfn void qtext_pronoun(char, char);
23. staticfn void convert_arg(char);
24. staticfn void convert_line(char *,char *);
25. staticfn void deliver_by_pline(const char *);
26. staticfn void deliver_by_window(const char *, int);
27. staticfn boolean skip_pager(boolean);
28. staticfn boolean com_pager_core(const char *, const char *, boolean, char **);
29. 
30. short
31. quest_info(int typ)
32. {
33.     switch (typ) {
34.     case 0:
35.         return gu.urole.questarti;
36.     case MS_LEADER:
37.         return gu.urole.ldrnum;
38.     case MS_NEMESIS:
39.         return gu.urole.neminum;
40.     case MS_GUARDIAN:
41.         return gu.urole.guardnum;
42.     default:
43.         impossible("quest_info(%d)", typ);
44.     }
45.     return 0;
46. }
47. 
48. /* return your role leader's name */
49. const char *
50. ldrname(void)
51. {
52.     int i = gu.urole.ldrnum;
53. 
54.     Sprintf(gn.nambuf, "%s%s", type_is_pname(&mons[i]) ? "" : "the ",
55.             mons[i].pmnames[NEUTRAL]);
56.     return gn.nambuf;
57. }
58. 
59. /* return your intermediate target string */
60. staticfn const char *
61. intermed(void)
62. {
63.     return gu.urole.intermed;
64. }
65. 
66. boolean
67. is_quest_artifact(struct obj *otmp)
68. {
69.     return (boolean) (otmp->oartifact == gu.urole.questarti);
70. }
71. 
72. staticfn struct obj *
73. find_qarti(struct obj *ochain)
74. {
75.     struct obj *otmp, *qarti;
76. 
77.     for (otmp = ochain; otmp; otmp = otmp->nobj) {
78.         if (is_quest_artifact(otmp))
79.             return otmp;
80.         if (Has_contents(otmp) && (qarti = find_qarti(otmp->cobj)) != 0)
81.             return qarti;
82.     }
83.     return (struct obj *) 0;
84. }
85. 
86. /* check several object chains for the quest artifact to determine
87.    whether it is present on the current level */
88. struct obj *
89. find_quest_artifact(unsigned whichchains)
90. {
91.     struct monst *mtmp;
92.     struct obj *qarti = 0;
93. 
94.     if ((whichchains & (1 << OBJ_INVENT)) != 0)
95.         qarti = find_qarti(gi.invent);
96.     if (!qarti && (whichchains & (1 << OBJ_FLOOR)) != 0)
97.         qarti = find_qarti(fobj);
98.     if (!qarti && (whichchains & (1 << OBJ_MINVENT)) != 0)
99.         for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
100.             if (DEADMONSTER(mtmp))
101.                 continue;
102.             if ((qarti = find_qarti(mtmp->minvent)) != 0)
103.                 break;
104.         }
105.     if (!qarti && (whichchains & (1 << OBJ_MIGRATING)) != 0) {
106.         /* check migrating objects and minvent of migrating monsters */
107.         for (mtmp = gm.migrating_mons; mtmp; mtmp = mtmp->nmon) {
108.             if (DEADMONSTER(mtmp))
109.                 continue;
110.             if ((qarti = find_qarti(mtmp->minvent)) != 0)
111.                 break;
112.         }
113.         if (!qarti)
114.             qarti = find_qarti(gm.migrating_objs);
115.     }
116.     if (!qarti && (whichchains & (1 << OBJ_BURIED)) != 0)
117.         qarti = find_qarti(svl.level.buriedobjlist);
118. 
119.     return qarti;
120. }
121. 
122. /* return your role nemesis' name */
123. staticfn const char *
124. neminame(void)
125. {
126.     int i = gu.urole.neminum;
127. 
128.     Sprintf(gn.nambuf, "%s%s", type_is_pname(&mons[i]) ? "" : "the ",
129.             mons[i].pmnames[NEUTRAL]);
130.     return gn.nambuf;
131. }
132. 
133. staticfn const char *
134. guardname(void) /* return your role leader's guard monster name */
135. {
136.     int i = gu.urole.guardnum;
137. 
138.     return mons[i].pmnames[NEUTRAL];
139. }
140. 
141. staticfn const char *
142. homebase(void) /* return your role leader's location */
143. {
144.     return gu.urole.homebase;
145. }
146. 
147. /* returns 1 if nemesis death message mentions noxious fumes, otherwise 0;
148.    does not display the message */
149. int
150. stinky_nemesis(struct monst *mon)
151. {
152.     char *mesg = 0;
153.     int res = 0;
154. 
155. #if 0
156.     /* get the quest text for dying nemesis; don't assume that mon is
157.        hero's own role's nemesis (overkill since m_detach() and nemdead()
158.        both make that assumption--valid for normal play but not necessarily
159.        valid for wizard mode) */
160.     int r, mndx = monsndx(mon->data);
161.     for (r = 0; roles[r].name.m || roles[r].name.f; ++r)
162.         if (roles[r].neminum == mndx) {
163.             (void) com_pager_core(roles[r].filecode, "killed_nemesis",
164.                                   FALSE, &mesg);
165.             break;
166.         }
167. #else
168.     nhUse(mon);
169.     /* since nemdead() just gave the message for hero's nemesis even if 'mon'
170.        is some other role's nemesis (feasible in wizard mode), base any gas
171.        cloud on the text that was shown even if not appropriate for 'mon' */
172.     (void) com_pager_core(gu.urole.filecode, "killed_nemesis", FALSE, &mesg);
173. #endif
174. 
175.     /* this is somewhat fragile; it assumes that when both {noxious or
176.        poisonous or toxic} and {gas or fumes} are present, the latter
177.        refers to the former rather than to something unrelated; it does
178.        make sure that fumes occurs after noxious rather than before */
179.     if (mesg) {
180.         char *p;
181. 
182.         /* change newlines into spaces to cope with "...noxious\nfumes..." */
183.         (void) strNsubst(mesg, "\n", " ", 0);
184. 
185.         if (((p = strstri(mesg, "noxious")) != 0
186.              || (p = strstri(mesg, "poisonous")) != 0
187.              || (p = strstri(mesg, "toxic")) != 0)
188.             && (strstri(p, " gas") || strstri(p, " fumes")))
189.             res = 1;
190. 
191.         free((genericptr_t) mesg);
192.     }
193.     return res;
194. }
195. 
196. /* replace deity, leader, nemesis, or artifact name with pronoun;
197.    overwrites cvt_buf[] */
198. staticfn void
199. qtext_pronoun(
200.     char who,   /* 'd' => deity, 'l' => leader, 'n' => nemesis, 'o' => arti */
201.     char which) /* 'h'|'H'|'i'|'I'|'j'|'J' */
202. {
203.     const char *pnoun;
204.     int godgend;
205.     char lwhich = lowc(which); /* H,I,J -> h,i,j */
206. 
207.     /*
208.      * Invalid subject (not d,l,n,o) yields neuter, singular result.
209.      *
210.      * For %o, treat all artifacts as neuter; some have plural names,
211.      * which genders[] doesn't handle; cvt_buf[] already contains name.
212.      */
213.     if (who == 'o'
214.         && (strstri(gc.cvt_buf, "Eyes ")
215.             || strcmpi(gc.cvt_buf, makesingular(gc.cvt_buf)))) {
216.         pnoun = (lwhich == 'h') ? "they"
217.                 : (lwhich == 'i') ? "them"
218.                 : (lwhich == 'j') ? "their" : "?";
219.     } else {
220.         godgend = (who == 'd') ? svq.quest_status.godgend
221.             : (who == 'l') ? svq.quest_status.ldrgend
222.             : (who == 'n') ? svq.quest_status.nemgend
223.             : 2; /* default to neuter */
224.         pnoun = (lwhich == 'h') ? genders[godgend].he
225.                 : (lwhich == 'i') ? genders[godgend].him
226.                 : (lwhich == 'j') ? genders[godgend].his : "?";
227.     }
228.     Strcpy(gc.cvt_buf, pnoun);
229.     /* capitalize for H,I,J */
230.     if (lwhich != which)
231.         gc.cvt_buf[0] = highc(gc.cvt_buf[0]);
232.     return;
233. }
234. 
235. staticfn void
236. convert_arg(char c)
237. {
238.     const char *str;
239. 
240.     switch (c) {
241.     case 'p':
242.         str = svp.plname;
243.         break;
244.     case 'c':
245.         str = (flags.female && gu.urole.name.f) ? gu.urole.name.f
246.                                                : gu.urole.name.m;
247.         break;
248.     case 'r':
249.         str = rank_of(u.ulevel, Role_switch, flags.female);
250.         break;
251.     case 'R':
252.         str = rank_of(MIN_QUEST_LEVEL, Role_switch, flags.female);
253.         break;
254.     case 's':
255.         str = (flags.female) ? "sister" : "brother";
256.         break;
257.     case 'S':
258.         str = (flags.female) ? "daughter" : "son";
259.         break;
260.     case 'l':
261.         str = ldrname();
262.         break;
263.     case 'i':
264.         str = intermed();
265.         break;
266.     case 'O':
267.     case 'o':
268.         str = the(artiname(gu.urole.questarti));
269.         if (c == 'O') {
270.             /* shorten "the Foo of Bar" to "the Foo"
271.                (buffer returned by the() is modifiable) */
272.             char *p = strstri(str, " of ");
273. 
274.             if (p)
275.                 *p = '\0';
276.         }
277.         break;
278.     case 'n':
279.         str = neminame();
280.         break;
281.     case 'g':
282.         str = guardname();
283.         break;
284.     case 'G':
285.         str = align_gtitle(u.ualignbase[A_ORIGINAL]);
286.         break;
287.     case 'H':
288.         str = homebase();
289.         break;
290.     case 'a':
291.         str = align_str(u.ualignbase[A_ORIGINAL]);
292.         break;
293.     case 'A':
294.         str = align_str(u.ualign.type);
295.         break;
296.     case 'd':
297.         str = align_gname(u.ualignbase[A_ORIGINAL]);
298.         break;
299.     case 'D':
300.         str = align_gname(A_LAWFUL);
301.         break;
302.     case 'C':
303.         str = "chaotic";
304.         break;
305.     case 'N':
306.         str = "neutral";
307.         break;
308.     case 'L':
309.         str = "lawful";
310.         break;
311.     case 'x':
312.         str = Blind ? "sense" : "see";
313.         break;
314.     case 'Z':
315.         str = svd.dungeons[0].dname;
316.         break;
317.     case '%':
318.         str = "%";
319.         break;
320.     default:
321.         str = "";
322.         break;
323.     }
324.     Strcpy(gc.cvt_buf, str);
325. }
326. 
327. staticfn void
328. convert_line(char *in_line, char *out_line)
329. {
330.     char *c, *cc;
331. 
332.     cc = out_line;
333.     for (c = in_line; *c; c++) {
334.         *cc = 0;
335.         switch (*c) {
336.         case '\r':
337.         case '\n':
338.             *(++cc) = 0;
339.             return;
340. 
341.         case '%':
342.             if (*(c + 1)) {
343.                 convert_arg(*(++c));
344.                 switch (*(++c)) {
345.                 /* insert "a"/"an" prefix */
346.                 case 'A':
347.                     Strcat(cc, An(gc.cvt_buf));
348.                     cc += strlen(cc);
349.                     continue; /* for */
350.                 case 'a':
351.                     Strcat(cc, an(gc.cvt_buf));
352.                     cc += strlen(cc);
353.                     continue; /* for */
354. 
355.                 /* capitalize */
356.                 case 'C':
357.                     gc.cvt_buf[0] = highc(gc.cvt_buf[0]);
358.                     break;
359. 
360.                 /* replace name with pronoun;
361.                    valid for %d, %l, %n, and %o */
362.                 case 'h': /* he/she */
363.                 case 'H': /* He/She */
364.                 case 'i': /* him/her */
365.                 case 'I':
366.                 case 'j': /* his/her */
367.                 case 'J':
368.                     if (strchr("dlno", lowc(*(c - 1))))
369.                         qtext_pronoun(*(c - 1), *c);
370.                     else
371.                         --c; /* default action */
372.                     break;
373. 
374.                 /* pluralize */
375.                 case 'P':
376.                     gc.cvt_buf[0] = highc(gc.cvt_buf[0]);
377.                     FALLTHROUGH;
378.                     /*FALLTHRU*/
379.                 case 'p':
380.                     Strcpy(gc.cvt_buf, makeplural(gc.cvt_buf));
381.                     break;
382. 
383.                 /* append possessive suffix */
384.                 case 'S':
385.                     gc.cvt_buf[0] = highc(gc.cvt_buf[0]);
386.                     FALLTHROUGH;
387.                     /*FALLTHRU*/
388.                 case 's':
389.                     Strcpy(gc.cvt_buf, s_suffix(gc.cvt_buf));
390.                     break;
391. 
392.                 /* strip any "the" prefix */
393.                 case 't':
394.                     if (!strncmpi(gc.cvt_buf, "the ", 4)) {
395.                         Strcat(cc, &gc.cvt_buf[4]);
396.                         cc += strlen(cc);
397.                         continue; /* for */
398.                     }
399.                     break;
400. 
401.                 default:
402.                     --c; /* undo switch increment */
403.                     break;
404.                 }
405.                 Strcat(cc, gc.cvt_buf);
406.                 cc += strlen(gc.cvt_buf);
407.                 break;
408.             }
409.             FALLTHROUGH;
410.             /* FALLTHRU */
411.         default:
412.             *cc++ = *c;
413.             break;
414.         }
415.         if (cc > &out_line[BUFSZ - 1])
416.             panic("convert_line: overflow");
417.     }
418.     *cc = 0;
419.     return;
420. }
421. 
422. staticfn void
423. deliver_by_pline(const char *str)
424. {
425.     char in_line[BUFSZ], out_line[BUFSZ];
426.     const char *msgp = str, *msgend = eos((char *) str);
427. 
428.     while (msgp < msgend) {
429.         /* copynchars() will stop at newline if it finds one */
430.         copynchars(in_line, msgp, (int) sizeof in_line - 1);
431.         msgp += strlen(in_line) + 1;
432. 
433.         convert_line(in_line, out_line);
434.         pline("%s", out_line);
435.     }
436. }
437. 
438. staticfn void
439. deliver_by_window(const char *msg, int how)
440. {
441.     char in_line[BUFSZ], out_line[BUFSZ];
442.     const char *msgp = msg, *msgend = eos((char *) msg);
443.     winid datawin = create_nhwindow(how);
444. 
445.     while (msgp < msgend) {
446.         /* copynchars() will stop at newline if it finds one */
447.         copynchars(in_line, msgp, (int) sizeof in_line - 1);
448.         msgp += strlen(in_line) + 1;
449. 
450.         convert_line(in_line, out_line);
451.         putstr(datawin, 0, out_line);
452.     }
453. 
454.     display_nhwindow(datawin, TRUE);
455.     destroy_nhwindow(datawin);
456. }
457. 
458. staticfn boolean
459. skip_pager(boolean common UNUSED)
460. {
461.     /* WIZKIT: suppress plot feedback if starting with quest artifact */
462.     if (program_state.wizkit_wishing)
463.         return TRUE;
464.     return FALSE;
465. }
466. 
467. staticfn boolean
468. com_pager_core(
469.     const char *section,
470.     const char *msgid,
471.     boolean showerror,
472.     char **rawtext)
473. {
474.     static const char *const howtoput[] = {
475.         "pline", "window", "text", "menu", "default", NULL
476.     };
477.     static const int howtoput2i[] = { 1, 2, 2, 3, 0, 0 };
478.     int output;
479.     lua_State *L;
480.     char *text = NULL, *synopsis = NULL, *fallback_msgid = NULL;
481.     boolean res = FALSE;
482.     nhl_sandbox_info sbi = {NHL_SB_SAFE, 1*1024*1024, 0, 1*1024*1024};
483. 
484.     if (skip_pager(TRUE))
485.         return FALSE;
486. 
487.     L = nhl_init(&sbi);
488.     if (!L) {
489.         if (showerror)
490.             impossible("com_pager: nhl_init() failed");
491.         goto compagerdone;
492.     }
493. 
494.     if (!nhl_loadlua(L, QTEXT_FILE)) {
495.         if (showerror)
496.             impossible("com_pager: %s not found.", QTEXT_FILE);
497.         goto compagerdone;
498.     }
499. 
500.     lua_settop(L, 0);
501.     lua_getglobal(L, "questtext");
502.     if (!lua_istable(L, -1)) {
503.         if (showerror)
504.             impossible("com_pager: questtext in %s is not a lua table",
505.                        QTEXT_FILE);
506.         goto compagerdone;
507.     }
508. 
509.     lua_getfield(L, -1, section);
510.     if (!lua_istable(L, -1)) {
511.         if (showerror)
512.             impossible("com_pager: questtext[%s] in %s is not a lua table",
513.                        section, QTEXT_FILE);
514.         goto compagerdone;
515.     }
516. 
517.  tryagain:
518.     lua_getfield(L, -1, fallback_msgid ? fallback_msgid : msgid);
519.     if (!lua_istable(L, -1)) {
520.         if (!fallback_msgid) {
521.             /* Do we have questtxt[msg_fallbacks][<msgid>]? */
522.             lua_getfield(L, -3, "msg_fallbacks");
523.             if (lua_istable(L, -1)) {
524.                 fallback_msgid = get_table_str_opt(L, msgid, NULL);
525.                 lua_pop(L, 2);
526.                 if (fallback_msgid)
527.                     goto tryagain;
528.             }
529.         }
530.         if (showerror) {
531.             if (!fallback_msgid)
532.                 impossible(
533.                       "com_pager: questtext[%s][%s] in %s is not a lua table",
534.                            section, msgid, QTEXT_FILE);
535.             else
536.                 impossible(
537.            "com_pager: questtext[%s][%s] and [][%s] in %s are not lua tables",
538.                            section, msgid, fallback_msgid, QTEXT_FILE);
539.         }
540.         goto compagerdone;
541.     }
542. 
543.     text = get_table_str_opt(L, "text", NULL);
544.     if (rawtext) {
545.         *rawtext = dupstr(text);
546.         res = TRUE;
547.         goto compagerdone;
548.     }
549.     synopsis = get_table_str_opt(L, "synopsis", NULL);
550.     output = howtoput2i[get_table_option(L, "output", "default", howtoput)];
551. 
552.     if (!text) {
553.         int nelems;
554. 
555.         lua_len(L, -1);
556.         nelems = (int) lua_tointeger(L, -1);
557.         lua_pop(L, 1);
558.         if (nelems < 2) {
559.             if (showerror)
560.                 impossible(
561.               "com_pager: questtext[%s][%s] in %s is not an array of strings",
562.                            section, fallback_msgid ? fallback_msgid : msgid,
563.                            QTEXT_FILE);
564.             goto compagerdone;
565.         }
566.         nelems = rn2(nelems) + 1;
567.         lua_pushinteger(L, nelems);
568.         lua_gettable(L, -2);
569.         text = dupstr(luaL_checkstring(L, -1));
570.     }
571. 
572.     /* switch from by_pline to by_window if line has multiple segments or
573.        is unreasonably long (the latter ought to checked after formatting
574.        conversions rather than before...) */
575.     if (output == 0 && (strchr(text, '\n') || strlen(text) >= BUFSZ - 1)) {
576.         output = 2;
577. 
578.         /*
579.          * FIXME:  should update quest.lua to include proper synopsis line
580.          * for any item subject to having its delivery converted to by_window.
581.          */
582.         if (!synopsis) {
583.             char tmpbuf[BUFSZ];
584. 
585.             Sprintf(tmpbuf, "[%.*s]", BUFSZ - 1 - 2, text);
586.             /* change every newline character to a space */
587.             (void) strNsubst(tmpbuf, "\n", " ", 0);
588.             synopsis = dupstr(tmpbuf);
589.         }
590.     }
591. 
592.     if (output == 0 || output == 1)
593.         deliver_by_pline(text);
594.     else
595.         deliver_by_window(text, (output == 3) ? NHW_MENU : NHW_TEXT);
596. 
597.     if (synopsis) {
598.         char in_line[BUFSZ], out_line[BUFSZ];
599. 
600. #if 0   /* not yet -- brackets need to be removed from quest.lua */
601.         Sprintf(in_line, "[%.*s]",
602.                 (int) (sizeof in_line - sizeof "[]"), synopsis);
603. #else
604.         Strcpy(in_line, synopsis);
605. #endif
606.         convert_line(in_line, out_line);
607.         /* bypass message delivery but be available for ^P recall */
608.         putmsghistory(out_line, FALSE);
609.     }
610.     res = TRUE;
611. 
612.  compagerdone:
613.     if (text)
614.         free((genericptr_t) text);
615.     if (synopsis)
616.         free((genericptr_t) synopsis);
617.     if (fallback_msgid)
618.         free((genericptr_t) fallback_msgid);
619.     nhl_done(L);
620.     return res;
621. }
622. 
623. void
624. com_pager(const char *msgid)
625. {
626.     (void) com_pager_core("common", msgid, TRUE, (char **) 0);
627. }
628. 
629. void
630. qt_pager(const char *msgid)
631. {
632.     if (!com_pager_core(gu.urole.filecode, msgid, FALSE, (char **) 0))
633.         (void) com_pager_core("common", msgid, TRUE, (char **) 0);
634. }
635. 
636. struct permonst *
637. qt_montype(void)
638. {
639.     int qpm;
640. 
641.     if (rn2(5)) {
642.         qpm = gu.urole.enemy1num;
643.         if (qpm != NON_PM && rn2(5) && !(svm.mvitals[qpm].mvflags & G_GENOD))
644.             return &mons[qpm];
645.         return mkclass(gu.urole.enemy1sym, 0);
646.     }
647.     qpm = gu.urole.enemy2num;
648.     if (qpm != NON_PM && rn2(5) && !(svm.mvitals[qpm].mvflags & G_GENOD))
649.         return &mons[qpm];
650.     return mkclass(gu.urole.enemy2sym, 0);
651. }
652. 
653. /* special levels can include a custom arrival message; display it */
654. void
655. deliver_splev_message(void)
656. {
657.     /* there's no provision for delivering via window instead of pline */
658.     if (gl.lev_message) {
659.         deliver_by_pline(gl.lev_message);
660. 
661.         free((genericptr_t) gl.lev_message);
662.         gl.lev_message = NULL;
663.     }
664. }
665. 
666. #undef QTEXT_FILE
667. 
668. /*questpgr.c*/