1 /* NetHack 3.6 questpgr.c $NHDT-Date: 1448541043 2015/11/26 12:30:43 $ $NHDT-Branch: master $:$NHDT-Revision: 1.36 $ */
2 /* Copyright 1991, M. Stephenson */
3 /* NetHack may be freely redistributed. See license for details. */
8 /* quest-specific pager routines. */
12 #define QTEXT_FILE "quest.dat"
18 /* from sp_lev.c, for deliver_splev_message() */
19 extern char *lev_message;
21 static void NDECL(dump_qtlist);
22 static void FDECL(Fread, (genericptr_t, int, int, dlb *));
23 STATIC_DCL struct qtmsg *FDECL(construct_qtlist, (long));
24 STATIC_DCL const char *NDECL(intermed);
25 STATIC_DCL const char *NDECL(neminame);
26 STATIC_DCL const char *NDECL(guardname);
27 STATIC_DCL const char *NDECL(homebase);
28 STATIC_DCL void FDECL(qtext_pronoun, (CHAR_P, CHAR_P));
29 STATIC_DCL struct qtmsg *FDECL(msg_in, (struct qtmsg *, int));
30 STATIC_DCL void FDECL(convert_arg, (CHAR_P));
31 STATIC_DCL void FDECL(convert_line, (char *,char *));
32 STATIC_DCL void FDECL(deliver_by_pline, (struct qtmsg *));
33 STATIC_DCL void FDECL(deliver_by_window, (struct qtmsg *, int));
34 STATIC_DCL boolean FDECL(skip_pager, (BOOLEAN_P));
36 static char cvt_buf[64];
37 static struct qtlists qt_list;
39 /* used by ldrname() and neminame(), then copied into cvt_buf */
40 static char nambuf[sizeof cvt_buf];
42 /* dump the character msg list to check appearance;
43 build with DEBUG enabled and use DEBUGFILES=questpgr.c
44 in sysconf file or environment */
51 if (!explicitdebug(__FILE__))
54 for (msg = qt_list.chrole; msg->msgnum > 0; msg++) {
55 (void) dlb_fseek(msg_file, msg->offset, SEEK_SET);
56 deliver_by_window(msg, NHW_MAP);
63 Fread(ptr, size, nitems, stream)
70 if ((cnt = dlb_fread(ptr, size, nitems, stream)) != nitems) {
71 panic("PREMATURE EOF ON QUEST TEXT FILE! Expected %d bytes, got %d",
72 (size * nitems), (size * cnt));
76 STATIC_OVL struct qtmsg *
77 construct_qtlist(hdr_offset)
80 struct qtmsg *msg_list;
83 (void) dlb_fseek(msg_file, hdr_offset, SEEK_SET);
84 Fread(&n_msgs, sizeof(int), 1, msg_file);
85 msg_list = (struct qtmsg *) alloc((unsigned) (n_msgs + 1)
86 * sizeof (struct qtmsg));
91 Fread((genericptr_t) msg_list, n_msgs * sizeof (struct qtmsg), 1,
94 msg_list[n_msgs].msgnum = -1;
102 char qt_classes[N_HDR][LEN_HDR];
103 long qt_offsets[N_HDR];
105 msg_file = dlb_fopen(QTEXT_FILE, RDBMODE);
107 panic("CANNOT OPEN QUEST TEXT FILE %s.", QTEXT_FILE);
110 * Read in the number of classes, then the ID's & offsets for
114 Fread(&n_classes, sizeof (int), 1, msg_file);
115 Fread(&qt_classes[0][0], sizeof (char) * LEN_HDR, n_classes, msg_file);
116 Fread(qt_offsets, sizeof (long), n_classes, msg_file);
119 * Now construct the message lists for quick reference later
120 * on when we are actually paging the messages out.
123 qt_list.common = qt_list.chrole = (struct qtmsg *) 0;
125 for (i = 0; i < n_classes; i++) {
126 if (!strncmp(COMMON_ID, qt_classes[i], LEN_HDR))
127 qt_list.common = construct_qtlist(qt_offsets[i]);
128 else if (!strncmp(urole.filecode, qt_classes[i], LEN_HDR))
129 qt_list.chrole = construct_qtlist(qt_offsets[i]);
130 #if 0 /* UNUSED but available */
131 else if (!strncmp(urace.filecode, qt_classes[i], LEN_HDR))
132 qt_list.chrace = construct_qtlist(qt_offsets[i]);
136 if (!qt_list.common || !qt_list.chrole)
137 impossible("load_qtlist: cannot load quest text.");
139 return; /* no ***DON'T*** close the msg_file */
142 /* called at program exit */
147 (void) dlb_fclose(msg_file), msg_file = 0;
149 free((genericptr_t) qt_list.common), qt_list.common = 0;
151 free((genericptr_t) qt_list.chrole), qt_list.chrole = 0;
161 return urole.questarti;
165 return urole.neminum;
167 return urole.guardnum;
169 impossible("quest_info(%d)", typ);
174 /* return your role leader's name */
178 int i = urole.ldrnum;
181 Sprintf(nambuf, "%s%s", type_is_pname(&mons[i]) ? "" : "the ",
184 Strcpy(nambuf, mons[i].mname);
189 /* return your intermediate target string */
190 STATIC_OVL const char *
193 return urole.intermed;
197 is_quest_artifact(otmp)
200 return (boolean) (otmp->oartifact == urole.questarti);
203 /* return your role nemesis' name */
204 STATIC_OVL const char *
207 int i = urole.neminum;
210 Sprintf(nambuf, "%s%s", type_is_pname(&mons[i]) ? "" : "the ",
213 Strcpy(nambuf, mons[i].mname);
218 STATIC_OVL const char *
219 guardname() /* return your role leader's guard monster name */
221 int i = urole.guardnum;
223 return mons[i].mname;
226 STATIC_OVL const char *
227 homebase() /* return your role leader's location */
229 return urole.homebase;
232 /* replace deity, leader, nemesis, or artifact name with pronoun;
233 overwrites cvt_buf[] */
235 qtext_pronoun(who, which)
236 char who, /* 'd' => deity, 'l' => leader, 'n' => nemesis, 'o' => artifact */
237 which; /* 'h'|'H'|'i'|'I'|'j'|'J' */
241 char lwhich = lowc(which); /* H,I,J -> h,i,j */
244 * Invalid subject (not d,l,n,o) yields neuter, singular result.
246 * For %o, treat all artifacts as neuter; some have plural names,
247 * which genders[] doesn't handle; cvt_buf[] already contains name.
250 && (strstri(cvt_buf, "Eyes ")
251 || strcmpi(cvt_buf, makesingular(cvt_buf)))) {
252 pnoun = (lwhich == 'h') ? "they"
253 : (lwhich == 'i') ? "them"
254 : (lwhich == 'j') ? "their" : "?";
256 g = (who == 'd') ? quest_status.godgend
257 : (who == 'l') ? quest_status.ldrgend
258 : (who == 'n') ? quest_status.nemgend
259 : 2; /* default to neuter */
260 pnoun = (lwhich == 'h') ? genders[g].he
261 : (lwhich == 'i') ? genders[g].him
262 : (lwhich == 'j') ? genders[g].his : "?";
264 Strcpy(cvt_buf, pnoun);
265 /* capitalize for H,I,J */
267 cvt_buf[0] = highc(cvt_buf[0]);
271 STATIC_OVL struct qtmsg *
272 msg_in(qtm_list, msgnum)
273 struct qtmsg *qtm_list;
276 struct qtmsg *qt_msg;
278 for (qt_msg = qtm_list; qt_msg->msgnum > 0; qt_msg++)
279 if (qt_msg->msgnum == msgnum)
282 return (struct qtmsg *) 0;
289 register const char *str;
296 str = (flags.female && urole.name.f) ? urole.name.f : urole.name.m;
299 str = rank_of(u.ulevel, Role_switch, flags.female);
302 str = rank_of(MIN_QUEST_LEVEL, Role_switch, flags.female);
306 str = (flags.female) ? "sister" : "brother";
308 str = (flags.female) ? "
\96\85" : "
\92Ã";
312 str = (flags.female) ? "daughter" : "son";
314 str = (flags.female) ? "
\96º" : "
\91§
\8eq";
324 str = the(artiname(urole.questarti));
326 /* shorten "the Foo of Bar" to "the Foo"
327 (buffer returned by the() is modifiable) */
328 char *p = strstri(str, " of ");
341 str = align_gtitle(u.ualignbase[A_ORIGINAL]);
347 str = align_str(u.ualignbase[A_ORIGINAL]);
350 str = align_str(u.ualign.type);
353 str = align_gname(u.ualignbase[A_ORIGINAL]);
356 str = align_gname(A_LAWFUL);
374 str = "
\92\81\8f\98";
378 str = Blind ? "sense" : "see";
380 str = Blind ? "
\8a´
\82¶" : "
\8c©";
383 str = dungeons[0].dname;
392 Strcpy(cvt_buf, str);
396 convert_line(in_line, out_line)
397 char *in_line, *out_line;
403 for (c = xcrypt(in_line, xbuf); *c; c++) {
415 /* insert "a"/"an" prefix */
417 Strcat(cc, An(cvt_buf));
421 Strcat(cc, an(cvt_buf));
427 cvt_buf[0] = highc(cvt_buf[0]);
430 /* replace name with pronoun;
431 valid for %d, %l, %n, and %o */
432 case 'h': /* he/she */
433 case 'H': /* He/She */
434 case 'i': /* him/her */
436 case 'j': /* his/her */
438 if (index("dlno", lowc(*(c - 1))))
439 qtext_pronoun(*(c - 1), *c);
441 --c; /* default action */
446 cvt_buf[0] = highc(cvt_buf[0]);
448 Strcpy(cvt_buf, makeplural(cvt_buf));
451 /* append possessive suffix */
453 cvt_buf[0] = highc(cvt_buf[0]);
455 Strcpy(cvt_buf, s_suffix(cvt_buf));
458 /* strip any "the" prefix */
460 if (!strncmpi(cvt_buf, "the ", 4)) {
461 Strcat(cc, &cvt_buf[4]);
468 --c; /* undo switch increment */
472 cc += strlen(cvt_buf);
474 } /* else fall through */
481 if (cc > &out_line[BUFSZ-1])
482 panic("convert_line: overflow");
488 deliver_by_pline(qt_msg)
489 struct qtmsg *qt_msg;
492 char in_line[BUFSZ], out_line[BUFSZ];
495 for (size = 0; size < qt_msg->size; size += (long) strlen(in_line)) {
496 (void) dlb_fgets(in_line, sizeof in_line, msg_file);
497 convert_line(in_line, out_line);
498 pline("%s", out_line);
503 deliver_by_window(qt_msg, how)
504 struct qtmsg *qt_msg;
508 char in_line[BUFSZ], out_line[BUFSZ];
509 boolean qtdump = (how == NHW_MAP);
510 winid datawin = create_nhwindow(qtdump ? NHW_TEXT : how);
516 /* when dumping quest messages at startup, all of them are passed to
517 * deliver_by_window(), even if normally given to deliver_by_pline()
519 Sprintf(buf, "msgnum: %d, delivery: %c",
520 qt_msg->msgnum, qt_msg->delivery);
521 putstr(datawin, 0, buf);
522 putstr(datawin, 0, "");
525 for (size = 0; size < qt_msg->size; size += (long) strlen(in_line)) {
526 (void) dlb_fgets(in_line, sizeof in_line, msg_file);
527 convert_line(in_line, out_line);
528 putstr(datawin, 0, out_line);
530 display_nhwindow(datawin, TRUE);
531 destroy_nhwindow(datawin);
533 /* block messages delivered by window aren't kept in message history
534 but have a one-line summary which is put there for ^P recall */
536 if (qt_msg->summary_size) {
537 (void) dlb_fgets(in_line, sizeof in_line, msg_file);
538 convert_line(in_line, out_line);
540 } else if (qt_msg->delivery == 'c') { /* skip for 'qtdump' of 'p' */
541 /* delivery 'c' and !summary_size, summary expected but not present;
542 this doesn't prefix the number with role code vs 'general'
543 but should be good enough for summary verification purposes */
544 Sprintf(out_line, "[missing block message summary for #%05d]",
549 putmsghistory(out_line, FALSE);
556 /* WIZKIT: suppress plot feedback if starting with quest artifact */
557 if (program_state.wizkit_wishing)
559 if (!(common ? qt_list.common : qt_list.chrole)) {
560 panic("%s: no %s quest text data available",
561 common ? "com_pager" : "qt_pager",
562 common ? "common" : "role-specific");
573 struct qtmsg *qt_msg;
575 if (skip_pager(TRUE))
578 if (!(qt_msg = msg_in(qt_list.common, msgnum))) {
579 impossible("com_pager: message %d not found.", msgnum);
583 (void) dlb_fseek(msg_file, qt_msg->offset, SEEK_SET);
584 if (qt_msg->delivery == 'p')
585 deliver_by_pline(qt_msg);
586 else if (msgnum == 1)
587 deliver_by_window(qt_msg, NHW_MENU);
589 deliver_by_window(qt_msg, NHW_TEXT);
597 struct qtmsg *qt_msg;
599 if (skip_pager(FALSE))
602 if (!(qt_msg = msg_in(qt_list.chrole, msgnum))) {
603 impossible("qt_pager: message %d not found.", msgnum);
607 (void) dlb_fseek(msg_file, qt_msg->offset, SEEK_SET);
608 if (qt_msg->delivery == 'p' && strcmp(windowprocs.name, "X11"))
609 deliver_by_pline(qt_msg);
611 deliver_by_window(qt_msg, NHW_TEXT);
621 qpm = urole.enemy1num;
622 if (qpm != NON_PM && rn2(5) && !(mvitals[qpm].mvflags & G_GENOD))
624 return mkclass(urole.enemy1sym, 0);
626 qpm = urole.enemy2num;
627 if (qpm != NON_PM && rn2(5) && !(mvitals[qpm].mvflags & G_GENOD))
629 return mkclass(urole.enemy2sym, 0);
632 /* special levels can include a custom arrival message; display it */
634 deliver_splev_message()
636 char *str, *nl, in_line[BUFSZ], out_line[BUFSZ];
638 /* there's no provision for delivering via window instead of pline */
640 /* lev_message can span multiple lines using embedded newline chars;
641 any segments too long to fit within in_line[] will be truncated */
642 for (str = lev_message; *str; str = nl + 1) {
643 /* copying will stop at newline if one is present */
644 copynchars(in_line, str, (int) (sizeof in_line) - 1);
646 /* convert_line() expects encrypted input */
647 (void) xcrypt(in_line, in_line);
648 convert_line(in_line, out_line);
649 pline("%s", out_line);
651 if ((nl = index(str, '\n')) == 0)
652 break; /* done if no newline */
655 free((genericptr_t) lev_message);