ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/readable.C
Revision: 1.70
Committed: Sat Dec 1 20:22:12 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.69: +14 -14 lines
Log Message:
slight cleanup

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7 * Copyright (©) 1992 Frank Tore Johansen
8 *
9 * Deliantra is free software: you can redistribute it and/or modify it under
10 * the terms of the Affero GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the Affero GNU General Public License
20 * and the GNU General Public License along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 *
23 * The authors can be reached via e-mail to <support@deliantra.net>
24 */
25
26 /* This file contains code relevant to the BOOKS hack -- designed
27 * to allow randomly occuring messages in non-magical texts.
28 */
29
30 /* laid down initial file - dec 1995. -b.t. thomas@astro.psu.edu */
31
32 #include <global.h>
33 #include <book.h>
34 #include <living.h>
35 #include <spells.h>
36
37 /* Define this if you want to archive book titles by contents.
38 * This option should enforce UNIQUE combinations of titles,authors and
39 * msg contents during and *between* game sessions.
40 * Note: a slight degeneracy exists since books are archived based on an integer
41 * index value calculated from the message text (similar to alchemy formulae).
42 * Sometimes two widely different messages have the same index value (rare). In
43 * this case, it is possible to occasionally generate 2 books with same title and
44 * different message content. Not really a bug, but rather a feature. This action
45 * should keeps player on their toes ;).
46 * Also, note that there is *finite* space available for archiving message and titles.
47 * Once this space is used, books will stop being archived. Not a serious problem
48 * under the current regime, since there are generally fewer possible (random)
49 * messages than space available on the titlelists.
50 * One exception (for sure) are the monster messages. But no worries, you should
51 * see all of the monster info in some order (but not all possble combinations)
52 * before the monster titlelist space is run out. You can increase titlelist
53 * space by increasing the array sizes for the monster book_authours and book_names
54 * (see max_titles[] array and include/read.h). Since the unique_book algorthm is
55 * kinda stupid, this program *may* slow down program execution if defined (but I don't
56 * think its a significant problem, at least, I have no problems running this option
57 * on a Sparc 10! Also, once archive title lists are filled and/or all possible msg
58 * combinations have been generated, unique_book isnt called anymore. It takes 5-10
59 * sessions for this to happen).
60 * Final note: the game remembers book/title/msg combinations from reading the
61 * file lib/bookarch. If you REMOVE this file, you will lose your archive. So
62 * be sure to copy it over to the new lib directory when you change versions.
63 * -b.t.
64 */
65
66 /* This flag is useful to see what kind of output messages are created */
67
68 /* #define BOOK_MSG_DEBUG */
69
70 /* This flag is useful for debuging archiving action */
71
72 /* #define ARCHIVE_DEBUG */
73
74 /* Moved these structures from struct.h to this file in 0.94.3 - they
75 * are not needed anyplace else, so why have them globally declared?
76 */
77
78 /* 'title' and 'titlelist' are used by the readable code */
79 struct title : zero_initialised
80 {
81 shstr name; /* the name of the book */
82 shstr authour; /* the name of the book authour */
83 shstr archname; /* the archetype name of the book */
84 int level; /* level of difficulty of this message */
85 int size; /* size of the book message */
86 int msg_index; /* an index value derived from book message */
87 title *next;
88 };
89
90 struct titlelist : zero_initialised
91 {
92 int number; /* number of items in the list */
93 title *first_book; /* pointer to first book in this list */
94 titlelist *next; /* pointer to next book list */
95 };
96
97 /* special structure, used only by art_name_array[] */
98 struct arttypename
99 {
100 const char *name; /* generic name to call artifacts of this type */
101 int type; /* matching type */
102 };
103
104 /* booklist is the buffer of books read in from the bookarch file */
105 static titlelist *booklist = NULL;
106
107 static objectlink *first_mon_info = NULL;
108
109 /* these are needed for creation of a linked list of
110 * pointers to all (hostile) monster objects */
111
112 static int nrofmon = 0, need_to_write_bookarchive = 0;
113
114 /*
115 * Spellpath information
116 */
117 static uint32 spellpathdef[NRSPELLPATHS] = {
118 PATH_PROT,
119 PATH_FIRE,
120 PATH_FROST,
121 PATH_ELEC,
122 PATH_MISSILE,
123 PATH_SELF,
124 PATH_SUMMON,
125 PATH_ABJURE,
126 PATH_RESTORE,
127 PATH_DETONATE,
128 PATH_MIND,
129 PATH_CREATE,
130 PATH_TELE,
131 PATH_INFO,
132 PATH_TRANSMUTE,
133 PATH_TRANSFER,
134 PATH_TURNING,
135 PATH_WOUNDING,
136 PATH_DEATH,
137 PATH_LIGHT
138 };
139
140 static const char *const path_book_name[] = {
141 "codex",
142 "compendium",
143 "exposition",
144 "tables",
145 "treatise"
146 };
147
148 /* used by spellpath texts */
149 static const char *const path_author[] = {
150 "aether",
151 "astral byways",
152 "connections",
153 "the Grey Council",
154 "deep pathways",
155 "knowledge",
156 "magic",
157 "mystic ways",
158 "pathways",
159 "power",
160 "spells",
161 "transforms",
162 "the mystic veil",
163 "unknown spells"
164 };
165
166 /*
167 * Artiface/item information
168 */
169
170 /* if it isnt listed here, then art_attr_msg will never generate
171 * a message for this type of artifact. -b.t. */
172
173 static arttypename art_name_array[] = {
174 {"Helmet", HELMET},
175 {"Amulet", AMULET},
176 {"Shield", SHIELD},
177 {"Bracers", BRACERS},
178 {"Boots", BOOTS},
179 {"Cloak", CLOAK},
180 {"Gloves", GLOVES},
181 {"Girdle", GIRDLE},
182 {"Ring", RING},
183 {"Horn", HORN},
184 {"Missile Weapon", BOW},
185 {"Missile", ARROW},
186 {"Hand Weapon", WEAPON},
187 {"Artifact", SKILL},
188 {"Food", FOOD},
189 {"Body Armour", ARMOUR}
190 };
191
192 static const char *const art_book_name[] = {
193 "collection",
194 "file",
195 "files",
196 "guide",
197 "handbook",
198 "index",
199 "inventory",
200 "list",
201 "listing",
202 "record",
203 "record book"
204 };
205
206 /* used by artifact texts */
207 static const char *const art_author[] = {
208 "ancient things",
209 "artifacts",
210 "Havlor", /* ancient warrior scribe :) */
211 "items",
212 "lost artifacts",
213 "the ancients",
214 "useful things"
215 };
216
217 /*
218 * Monster book information
219 */
220
221 static const char *const mon_book_name[] = {
222 "beastuary",
223 "catalog",
224 "compilation",
225 "collection",
226 "encyclopedia",
227 "guide",
228 "handbook",
229 "list",
230 "manual",
231 "notes",
232 "record",
233 "register",
234 "volume"
235 };
236
237
238 /* used by monster beastuary texts */
239 static const char *const mon_author[] = {
240 "beasts",
241 "creatures",
242 "dezidens",
243 "dwellers",
244 "evil nature",
245 "life",
246 "monsters",
247 "nature",
248 "new life",
249 "residents",
250 "the spawn",
251 "the living",
252 "things"
253 };
254
255 /*
256 * God book information
257 */
258
259 static const char *const gods_book_name[] = {
260 "devotional",
261 "devout notes",
262 "divine text",
263 "divine work",
264 "holy book",
265 "holy record",
266 "moral text",
267 "sacred guide",
268 "testament",
269 "transcript"
270 };
271
272 /* used by gods texts */
273 static const char *const gods_author[] = {
274 "cults",
275 "joy",
276 "lasting curse",
277 "madness",
278 "religions",
279 "the dead",
280 "the gods",
281 "the heirophant",
282 "the poor priest",
283 "the priestess",
284 "pain",
285 "white"
286 };
287
288
289 /*
290 * Alchemy (formula) information
291 */
292
293 static const char *const formula_book_name[] = {
294 "cookbook",
295 "formulary",
296 "lab book",
297 "lab notes",
298 "recipe book",
299 "experiment record",
300 "work plan",
301 "design notes"
302 };
303
304 /* this isn't used except for empty books */
305 static const char *const formula_author[] = {
306 "Albertus Magnus",
307 "alchemy",
308 "balms",
309 "creation",
310 "dusts",
311 "magical manufacture",
312 "making",
313 "philosophical items",
314 "potions",
315 "powders",
316 "the cauldron",
317 "the lamp black",
318 "transmutation",
319 "waters"
320 };
321
322 /*
323 * Generic book information
324 */
325
326 /* used by msg file and 'generic' books */
327 static const char *const light_book_name[] = {
328 "calendar",
329 "datebook",
330 "diary",
331 "guidebook",
332 "handbook",
333 "ledger",
334 "notes",
335 "notebook",
336 "octavo",
337 "pamphlet",
338 "practicum",
339 "script",
340 "transcript"
341 };
342
343 static const char *const heavy_book_name[] = {
344 "catalog",
345 "compendium",
346 "guide",
347 "manual",
348 "opus",
349 "tome",
350 "treatise",
351 "volume",
352 "work"
353 };
354
355
356 /* used by 'generic' books */
357 static const char *const book_author[] = {
358 "Abdulah",
359 "Al'hezred",
360 "Alywn",
361 "Arundel",
362 "Arvind",
363 "Aerlingas",
364 "Bacon",
365 "Baliqendii",
366 "Bosworth",
367 "Beathis",
368 "Bertil",
369 "Cauchy",
370 "Chakrabarti",
371 "der Waalis",
372 "Dirk",
373 "Djwimii",
374 "Eisenstaadt",
375 "Fendris",
376 "Frank",
377 "Habbi",
378 "Harlod",
379 "Ichibod",
380 "Janus",
381 "June",
382 "Magnuson",
383 "Nandii",
384 "Nitfeder",
385 "Norris",
386 "Parael",
387 "Penhew",
388 "Sophia",
389 "Skilly",
390 "Tahir",
391 "Thockmorton",
392 "Thomas",
393 "van Helsing",
394 "van Pelt",
395 "Voormis",
396 "Xavier",
397 "Xeno",
398 "Zardoz",
399 "Zagy"
400 };
401
402 static const char *const book_descrpt[] = {
403 "ancient",
404 "cryptic",
405 "cryptical",
406 "dusty",
407 "hierarchical",
408 "grizzled",
409 "gold-guilt",
410 "great",
411 "lost",
412 "magnificent",
413 "musty",
414 "mythical",
415 "mystical",
416 "rustic",
417 "stained",
418 "silvered",
419 "transcendental",
420 "weathered"
421 };
422
423 /* Each line of this array is a readable subtype
424 * Be careful to keep the order. If you add readable subtype, add them
425 * at the bottom of the list. Never delete a subtype because index is used as
426 * subtype paramater in arch files!
427 */
428 static readable_message_type readable_message_types[] = {
429 /*subtype 0 */ {0, 0, "info"},
430 /* book messages subtypes */
431 /*subtype 1 */ {MSG_TYPE_BOOK, MSG_TYPE_BOOK_CLASP_1, "readable-book-clasp-1"},
432 {MSG_TYPE_BOOK, MSG_TYPE_BOOK_CLASP_2, "readable-book-clasp-2"},
433 {MSG_TYPE_BOOK, MSG_TYPE_BOOK_ELEGANT_1, "readable-book-elegant-1"},
434 {MSG_TYPE_BOOK, MSG_TYPE_BOOK_ELEGANT_2, "readable-book-elegant-2"},
435 {MSG_TYPE_BOOK, MSG_TYPE_BOOK_QUARTO_1, "readable-book-quarto-1"},
436 {MSG_TYPE_BOOK, MSG_TYPE_BOOK_QUARTO_2, "readable-book-quarto-2"},
437 {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_EVOKER, "readable-book-spell-evocation"},
438 {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_PRAYER, "readable-book-spell-praying"},
439 {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_PYRO, "readable-book-spell-pyromancy"},
440 /*subtype 10 */ {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_SORCERER, "readable-book-spell-sorcery"},
441 {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_SUMMONER, "readable-book-spell-summoning"},
442 /* card messages subtypes */
443 {MSG_TYPE_CARD, MSG_TYPE_CARD_SIMPLE_1, "readable-card-simple-1"},
444 {MSG_TYPE_CARD, MSG_TYPE_CARD_SIMPLE_2, "readable-card-simple-2"},
445 {MSG_TYPE_CARD, MSG_TYPE_CARD_SIMPLE_3, "readable-card-simple-3"},
446 {MSG_TYPE_CARD, MSG_TYPE_CARD_ELEGANT_1, "readable-card-elegant-1"},
447 {MSG_TYPE_CARD, MSG_TYPE_CARD_ELEGANT_2, "readable-card-elegant-2"},
448 {MSG_TYPE_CARD, MSG_TYPE_CARD_ELEGANT_3, "readable-card-elegant-3"},
449 {MSG_TYPE_CARD, MSG_TYPE_CARD_STRANGE_1, "readable-card-strange-1"},
450 {MSG_TYPE_CARD, MSG_TYPE_CARD_STRANGE_2, "readable-card-strange-2"},
451 /*subtype 20 */ {MSG_TYPE_CARD, MSG_TYPE_CARD_STRANGE_3, "readable-card-strange-3"},
452 {MSG_TYPE_CARD, MSG_TYPE_CARD_MONEY_1, "readable-card-money-1"},
453 {MSG_TYPE_CARD, MSG_TYPE_CARD_MONEY_2, "readable-card-money-2"},
454 {MSG_TYPE_CARD, MSG_TYPE_CARD_MONEY_3, "readable-card-money-3"},
455
456 /* Paper messages subtypes */
457 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_NOTE_1, "readable-paper-note-1"},
458 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_NOTE_2, "readable-paper-note-2"},
459 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_NOTE_3, "readable-paper-note-3"},
460 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_LETTER_OLD_1, "readable-paper-letter-old-1"},
461 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_LETTER_OLD_2, "readable-paper-letter-old-2"},
462 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_LETTER_NEW_1, "readable-paper-letter-new-1"},
463 /*subtype 30 */ {MSG_TYPE_PAPER, MSG_TYPE_PAPER_LETTER_NEW_2, "readable-paper-letter-new-2"},
464 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_ENVELOPE_1, "readable-paper-envelope-1"},
465 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_ENVELOPE_2, "readable-paper-envelope-2"},
466 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_OLD_1, "readable-paper-scroll-old-1"},
467 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_OLD_2, "readable-paper-scroll-old-2"},
468 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_NEW_1, "readable-paper-scroll-new-1"},
469 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_NEW_2, "readable-paper-scroll-new-2"},
470 {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_MAGIC, "readable-paper-scroll-magic"},
471
472 /* road signs messages subtypes */
473 {MSG_TYPE_SIGN, MSG_TYPE_SIGN_BASIC, "readable-sign-basic"},
474 {MSG_TYPE_SIGN, MSG_TYPE_SIGN_DIR_LEFT, "readable-sign-dir-left"},
475 /*subtype 40 */ {MSG_TYPE_SIGN, MSG_TYPE_SIGN_DIR_RIGHT, "readable-sign-dir-right"},
476 {MSG_TYPE_SIGN, MSG_TYPE_SIGN_DIR_BOTH, "readable-sign-dir-both"},
477
478 /* stones and monument messages */
479 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STONE_1, "readable-monument-stone-1"},
480 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STONE_2, "readable-monument-stone-2"},
481 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STONE_3, "readable-monument-stone-3"},
482 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STATUE_1, "readable-monument-statue-1"},
483 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STATUE_2, "readable-monument-statue-2"},
484 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STATUE_3, "readable-monument-statue-3"},
485 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_GRAVESTONE_1, "readable-monument-gravestone-1"},
486 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_GRAVESTONE_2, "readable-monument-gravestone-2"},
487 /*subtype 50 */ {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_GRAVESTONE_3, "readable-monument-gravestone-3"},
488 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_1, "readable-monument-wall-1"},
489 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_2, "readable-monument-wall-2"},
490 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_3, "readable-monument-wall-3"}
491 };
492 static int last_readable_subtype = sizeof (readable_message_types) / sizeof (readable_message_type);
493
494 static int max_titles[6] = {
495 ((sizeof (light_book_name) / sizeof (char *)) + (sizeof (heavy_book_name) / sizeof (char *))) * (sizeof (book_author) / sizeof (char *)),
496 (sizeof (mon_book_name) / sizeof (char *)) * (sizeof (mon_author) / sizeof (char *)),
497 (sizeof (art_book_name) / sizeof (char *)) * (sizeof (art_author) / sizeof (char *)),
498 (sizeof (path_book_name) / sizeof (char *)) * (sizeof (path_author) / sizeof (char *)),
499 (sizeof (formula_book_name) / sizeof (char *)) * (sizeof (formula_author) / sizeof (char *)),
500 (sizeof (gods_book_name) / sizeof (char *)) * (sizeof (gods_author) / sizeof (char *))
501 };
502
503 /******************************************************************************
504 *
505 * Start of misc. readable functions used by others functions in this file
506 *
507 *****************************************************************************/
508
509 static titlelist *
510 get_empty_booklist ()
511 {
512 titlelist *bl = new titlelist;
513
514 bl->number = 0;
515 bl->first_book = NULL;
516 bl->next = NULL;
517 return bl;
518 }
519
520 static title *
521 get_empty_book ()
522 {
523 title *t = new title;
524
525 t->name = NULL;
526 t->archname = NULL;
527 t->authour = NULL;
528 t->level = 0;
529 t->size = 0;
530 t->msg_index = 0;
531 t->next = NULL;
532 return t;
533 }
534
535 /* get_titlelist() - returns pointer to the title list referanced by i */
536
537 static titlelist *
538 get_titlelist (int i)
539 {
540 if (!booklist)
541 booklist = get_empty_booklist ();
542
543 titlelist *tl = booklist;
544 int number = i;
545
546 if (number < 0)
547 return tl;
548
549 while (tl && number)
550 {
551 if (!tl->next)
552 tl->next = get_empty_booklist ();
553
554 tl = tl->next;
555 --number;
556 }
557
558 return tl;
559 }
560
561 /* HANDMADE STRING FUNCTIONS.., perhaps these belong in another file
562 * (shstr.c ?), but the quantity BOOK_BUF will need to be defined. */
563
564 /* nstrtok() - simple routine to return the number of list
565 * items in buf1 as separated by the value of buf2
566 */
567 static int
568 nstrtok (const char *buf1, const char *buf2)
569 {
570 char *tbuf, sbuf[12], buf[MAX_BUF];
571 int number = 0;
572
573 if (!buf1 || !buf2)
574 return 0;
575
576 strcpy (buf, buf1);
577 strcpy (sbuf, buf2);
578
579 tbuf = strtok (buf, sbuf);
580 while (tbuf)
581 {
582 number++;
583 tbuf = strtok (NULL, sbuf);
584 }
585 return number;
586 }
587
588 /* strtoktolin() - takes a string in buf1 and separates it into
589 * a list of strings delimited by buf2. Then returns a comma
590 * separated string w/ decent punctuation.
591 */
592 static char *
593 strtoktolin (const char *buf1, const char *buf2)
594 {
595 int maxi, i = nstrtok (buf1, buf2);
596 char *tbuf, buf[MAX_BUF], sbuf[12];
597 static char rbuf[BOOK_BUF];
598
599 maxi = i;
600 strcpy (buf, buf1);
601 strcpy (sbuf, buf2);
602 strcpy (rbuf, " ");
603 tbuf = strtok (buf, sbuf);
604 while (tbuf && i > 0)
605 {
606 strcat (rbuf, tbuf);
607
608 i--;
609 if (i == 1 && maxi > 1)
610 strcat (rbuf, " and ");
611 else if (i > 0 && maxi > 1)
612 strcat (rbuf, ", ");
613 else
614 strcat (rbuf, ".");
615
616 tbuf = strtok (NULL, sbuf);
617 }
618
619 return rbuf;
620 }
621
622 /*****************************************************************************
623 *
624 * Start of initialisation related functions.
625 *
626 ****************************************************************************/
627
628 /* init_book_archive() - if not called before, initialise the info list
629 * This reads in the bookarch file into memory. bookarch is the file
630 * created and updated across multiple runs of the program.
631 */
632 static void
633 init_book_archive ()
634 {
635 int nroftitle = 0;
636 titlelist *bl = get_empty_booklist ();
637
638 object_thawer thawer (settings.localdir, "bookarch");
639
640 if (!thawer)
641 {
642 LOG (llevDebug, "could not read bookarch file\n");
643 return;
644 }
645
646 while (thawer.kw)
647 {
648 if (thawer.kw != KW_title)
649 if (!thawer.parse_error ("bookarch file"))
650 break;
651
652 title *book = get_empty_book (); /* init new book entry */
653 thawer.get (book->name);
654
655 int type = -1;
656
657 for (;;)
658 {
659 thawer.next ();
660
661 switch (thawer.kw)
662 {
663 case KW_type: thawer.get (type ); break;
664 case KW_authour: thawer.get (book->authour ); break;
665 case KW_arch: thawer.get (book->archname ); break;
666 case KW_level: thawer.get (book->level ); break;
667 case KW_size: thawer.get (book->size ); break;
668 case KW_index: thawer.get (book->msg_index); break;
669
670 case KW_end:
671 /* link it */
672 bl = get_titlelist (type);
673 book->next = bl->first_book;
674 bl->first_book = book;
675 bl->number++;
676 ++nroftitle;
677 goto book_done;
678
679 default:
680 delete book;
681 goto book_done;
682 }
683 }
684
685 book_done:
686 thawer.next ();
687 }
688
689 LOG (llevDebug, "book archives(used/avail): \n");
690 int i;
691 for (bl = booklist, i = 0; bl && i < sizeof (max_titles) / sizeof (*max_titles); bl = bl->next, i++)
692 LOG (llevDebug, " (%d/%d)\n", bl->number, max_titles[i]);
693
694 LOG (llevDebug, "init_book_archive() got %d titles.\n", nroftitle);
695 }
696
697 /* init_mon_info() - creates the linked list of pointers to
698 * monster archetype objects if not called previously
699 */
700 static void
701 init_mon_info ()
702 {
703 archetype *at;
704 static int did_init_mon_info = 0;
705
706 if (did_init_mon_info)
707 return;
708
709 did_init_mon_info = 1;
710
711 for_all_archetypes (at)
712 if (at->flag [FLAG_MONSTER]
713 && at->is_head ()
714 && (!at->flag [FLAG_CHANGING] || at->flag [FLAG_UNAGGRESSIVE]))
715 {
716 objectlink *mon = new objectlink;
717
718 mon->ob = at;
719 mon->next = first_mon_info;
720 first_mon_info = mon;
721 nrofmon++;
722 }
723
724 LOG (llevDebug, "init_mon_info() got %d monsters\n", nrofmon);
725 }
726
727 /* init_readable() - initialise linked lists utilized by
728 * message functions in tailor_readable_ob()
729 *
730 * This is the function called by the main routine to initialise
731 * all the readable information.
732 */
733 void
734 init_readable ()
735 {
736 static int did_this;
737
738 if (did_this)
739 return;
740
741 did_this = 1;
742
743 LOG (llevDebug, "Initialising reading data...\n");
744 init_book_archive ();
745 init_mon_info ();
746 LOG (llevDebug, " Done\n");
747 }
748
749 /*****************************************************************************
750 *
751 * This is the start of the administrative functions when creating
752 * new books (ie, updating title and the like)
753 *
754 *****************************************************************************/
755
756 /* find_title() - Search the titlelist (based on msgtype) to see if
757 * book matches something already there. IF so, return that title.
758 */
759 static title *
760 find_title (const object *book, int msgtype)
761 {
762 title *t = NULL;
763 titlelist *tl = get_titlelist (msgtype);
764 int length = strlen (book->msg);
765 int index = strtoint (book->msg);
766
767 if (msgtype < 0)
768 return (title *) NULL;
769
770 if (tl)
771 t = tl->first_book;
772
773 while (t)
774 if (t->size == length && t->msg_index == index)
775 break;
776 else
777 t = t->next;
778
779 #ifdef ARCHIVE_DEBUG
780 if (t)
781 LOG (llevDebug, "Found title match (list %d): %s %s (%d)\n", msgtype, &t->name, &t->authour, t->msg_index);
782 #endif
783
784 return t;
785 }
786
787 /* new_text_name() - Only for objects of type BOOK. SPELLBOOK stuff is
788 * handled directly in change_book_name(). Names are based on text
789 * msgtype
790 * this sets book book->name based on msgtype given. What name
791 * is given is based on various criteria
792 */
793 static void
794 new_text_name (object *book, int msgtype)
795 {
796 const char *name;
797
798 if (book->type != BOOK)
799 return;
800
801 switch (msgtype)
802 {
803 case 1: /*monster */
804 name = mon_book_name[rndm (ecb_array_length (mon_book_name))];
805 break;
806 case 2: /*artifact */
807 name = art_book_name[rndm (ecb_array_length (art_book_name))];
808 break;
809 case 3: /*spellpath */
810 name = path_book_name[rndm (ecb_array_length (path_book_name))];
811 break;
812 case 4: /*alchemy */
813 name = formula_book_name[rndm (ecb_array_length (formula_book_name))];
814 break;
815 case 5: /*gods */
816 name = gods_book_name[rndm (ecb_array_length (gods_book_name))];
817 break;
818 case 6: /*msg file */
819 default:
820 name = book->weight > 2000 /* based on weight */
821 ? heavy_book_name [rndm (ecb_array_length (heavy_book_name))]
822 : light_book_name [rndm (ecb_array_length (light_book_name))];
823 break;
824 }
825
826 book->name = name;
827 }
828
829 /* add_book_author()
830 * A lot like new_text_name above, but instead chooses an author
831 * and sets op->title to that value
832 */
833
834 static void
835 add_author (object *op, int msgtype)
836 {
837 const char *name;
838
839 if (msgtype < 0 || strlen (op->msg) < 5)
840 return;
841
842 switch (msgtype)
843 {
844 case 1: /* monster */
845 name = mon_author[rndm (ecb_array_length (mon_author))];
846 break;
847 case 2: /* artifacts */
848 name = art_author[rndm (ecb_array_length (art_author))];
849 break;
850 case 3: /* spellpath */
851 name = path_author[rndm (ecb_array_length (path_author))];
852 break;
853 case 4: /* alchemy */
854 name = formula_author[rndm (ecb_array_length (formula_author))];
855 break;
856 case 5: /* gods */
857 name = gods_author[rndm (ecb_array_length (gods_author))];
858 break;
859 case 6: /* msg file */
860 default:
861 name = book_author[rndm (ecb_array_length (book_author))];
862 }
863
864 op->title = format ("of %s", name);
865 }
866
867 /* unique_book() - check to see if the book title/msg is unique. We
868 * go through the entire list of possibilities each time. If we find
869 * a match, then unique_book returns true (because inst unique).
870 */
871
872 static int
873 unique_book (const object *book, int msgtype)
874 {
875 if (!booklist)
876 return 1; /* No archival entries! Must be unique! */
877
878 /* Go through the booklist. If the author and name match, not unique so
879 * return 0.
880 */
881 for (title *test = get_titlelist (msgtype)->first_book; test; test = test->next)
882 if (test->name == book->name && book->title == test->authour)
883 return 0;
884
885 return 1;
886 }
887
888 /* add_book_to_list() */
889
890 static void
891 add_book_to_list (const object *book, int msgtype)
892 {
893 titlelist *tl = get_titlelist (msgtype);
894 title *t;
895
896 if (!tl)
897 {
898 LOG (llevError, "add_book_to_list can't get booklist!\n");
899 return;
900 }
901
902 t = get_empty_book ();
903 t->name = book->name;
904 t->authour = book->title;
905 t->size = strlen (book->msg);
906 t->msg_index = strtoint (book->msg);
907 t->archname = book->arch->archname;
908 t->level = book->level;
909
910 t->next = tl->first_book;
911 tl->first_book = t;
912 tl->number++;
913
914 /* We have stuff we need to write now */
915 need_to_write_bookarchive = 1;
916
917 #ifdef ARCHIVE_DEBUG
918 LOG (llevDebug, "Archiving new title: %s %s (%d)\n", &book->name, &book->title, msgtype);
919 #endif
920
921 }
922
923
924 /* change_book() - give a new, fancier name to generated
925 * objects of type BOOK and SPELLBOOK.
926 * Aug 96 I changed this so we will attempt to create consistent
927 * authour/title and message content for BOOKs. Also, we will
928 * alter books that match archive entries to the archival
929 * levels and architypes. -b.t.
930 */
931
932 #define MAX_TITLE_CHECK 20
933
934 static void
935 change_book (object *book, int msgtype)
936 {
937 int nbr = sizeof (book_descrpt) / sizeof (char *);
938
939 switch (book->type)
940 {
941 case BOOK:
942 {
943 titlelist *tl = get_titlelist (msgtype);
944 title *t = NULL;
945 int tries = 0;
946
947 /* look to see if our msg already been archived. If so, alter
948 * the book to match the archival text. If we fail to match,
949 * then we archive the new title/name/msg combo if there is
950 * room on the titlelist.
951 */
952
953 if ((strlen (book->msg) > 5) && (t = find_title (book, msgtype)))
954 {
955 /* alter book properties */
956 if (object *tmpbook = archetype::get (t->archname))
957 {
958 tmpbook->msg = book->msg;
959 tmpbook->copy_to (book);
960 tmpbook->destroy ();
961 }
962
963 book->title = t->authour;
964 book->name = t->name;
965 book->level = t->level;
966 }
967 /* Don't have any default title, so lets make up a new one */
968 else
969 {
970 int numb, maxnames = max_titles[msgtype];
971 const char *old_title;
972 const char *old_name;
973
974 old_title = book->title;
975 old_name = book->name;
976
977 /* some pre-generated books have title already set (from
978 * maps), also don't bother looking for unique title if
979 * we already used up all the available names! */
980
981 if (!tl)
982 {
983 LOG (llevError, "change_book_name(): can't find title list\n");
984 numb = 0;
985 }
986 else
987 numb = tl->number;
988
989 if (numb == maxnames)
990 {
991 #ifdef ARCHIVE_DEBUG
992 LOG (llevDebug, "titles for list %d full (%d possible).\n", msgtype, maxnames);
993 #endif
994 }
995 /* shouldnt change map-maker books */
996 else if (!book->title)
997 do
998 {
999 /* random book name */
1000 new_text_name (book, msgtype);
1001 add_author (book, msgtype); /* random author */
1002 tries++;
1003 }
1004 while (!unique_book (book, msgtype) && tries < MAX_TITLE_CHECK);
1005
1006 /* Now deal with 2 cases.
1007 * 1)If no space for a new title exists lets just restore
1008 * the old book properties. Remember, if the book had
1009 * matchd an older entry on the titlelist, we shouldnt
1010 * have called this routine in the first place!
1011 * 2) If we got a unique title, we need to add it to
1012 * the list.
1013 */
1014
1015 if (tries == MAX_TITLE_CHECK || numb == maxnames)
1016 { /* got to check maxnames again */
1017 #ifdef ARCHIVE_DEBUG
1018 LOG (llevDebug, "Failed to obtain unique title for %s %s (names:%d/%d)\n", &book->name, &book->title, numb, maxnames);
1019 #endif
1020 /* restore old book properties here */
1021 book->title = old_title;
1022
1023 if (rndm (4))
1024 /* Lets give the book a description to individualize it some */
1025 book->name = format ("%s %s", book_descrpt[rndm (nbr)], old_name);
1026 else
1027 book->name = old_name;
1028 }
1029 else if (book->title && strlen (book->msg) > 5)
1030 { /* archive if long msg texts */
1031 add_book_to_list (book, msgtype);
1032 }
1033 }
1034 break;
1035 }
1036
1037 default:
1038 LOG (llevError, "change_book_name() called w/ illegal obj type.\n");
1039 return;
1040 }
1041 }
1042
1043 /*****************************************************************************
1044 *
1045 * This is the start of the area that generates the actual contents
1046 * of the book.
1047 *
1048 *****************************************************************************/
1049
1050 /*****************************************************************************
1051 * Monster msg generation code.
1052 ****************************************************************************/
1053
1054 /* get_random_mon() - returns a random monster slected from linked
1055 * list of all monsters in the current game. If level is non-zero,
1056 * then only monsters greater than that level will be returned.
1057 * Changed 971225 to be greater than equal to level passed. Also
1058 * made choosing by level more random.
1059 */
1060 object *
1061 get_random_mon (int level)
1062 {
1063 objectlink *mon = first_mon_info;
1064 int i = 0, monnr;
1065
1066 /* safety check. Problem w/ init_mon_info list? */
1067 if (!nrofmon || !mon)
1068 return (object *) NULL;
1069
1070 if (!level)
1071 {
1072 /* lets get a random monster from the mon_info linked list */
1073 monnr = rndm (nrofmon);
1074
1075 for (mon = first_mon_info, i = 0; mon; mon = mon->next)
1076 if (i++ == monnr)
1077 break;
1078
1079 if (!mon)
1080 {
1081 LOG (llevError, "get_random_mon: Didn't find a monster when we should have\n");
1082 return NULL;
1083 }
1084 return mon->ob;
1085 }
1086
1087 /* Case where we are searching by level. Redone 971225 to be clearer
1088 * and more random. Before, it looks like it took a random monster from
1089 * the list, and then returned the first monster after that which was
1090 * appropriate level. This wasn't very random because if you had a
1091 * bunch of low level monsters and then a high level one, if the random
1092 * determine took one of the low level ones, it would just forward to the
1093 * high level one and return that. Thus, monsters that immediatly followed
1094 * a bunch of low level monsters would be more heavily returned. It also
1095 * means some of the dragons would be poorly represented, since they
1096 * are a group of high level monsters all around each other.
1097 */
1098
1099 /* First count number of monsters meeting level criteria */
1100 for (mon = first_mon_info, i = 0; mon; mon = mon->next)
1101 if (mon->ob->level >= level)
1102 i++;
1103
1104 if (i == 0)
1105 {
1106 LOG (llevError, "get_random_mon() couldn't return monster for level %d\n", level);
1107 return NULL;
1108 }
1109
1110 monnr = rndm (i);
1111 for (mon = first_mon_info; mon; mon = mon->next)
1112 if (mon->ob->level >= level && monnr-- == 0)
1113 return mon->ob;
1114
1115 if (!mon)
1116 {
1117 LOG (llevError, "get_random_mon(): didn't find a monster when we should have\n");
1118 return NULL;
1119 }
1120 return NULL; /* Should be unreached, by keeps warnings down */
1121 }
1122
1123 /*
1124 * Returns a description of the monster. This really needs to be
1125 * redone, as describe_item really gives a pretty internal description.
1126 */
1127 static const char *
1128 mon_desc (const object *mon)
1129 {
1130 static dynbuf_text buf; buf.clear ();
1131
1132 buf.printf ("B<< %s >>\r", &mon->name);
1133 buf << describe_item (mon, 0);
1134
1135 return buf;
1136 }
1137
1138
1139 /* This function returns the next monsters after 'tmp'. If no match is
1140 * found, it returns NULL (changed 0.94.3 to do this, since the
1141 * calling function (mon_info_msg) seems to expect that.
1142 */
1143 static object *
1144 get_next_mon (object *tmp)
1145 {
1146 objectlink *mon;
1147
1148 for (mon = first_mon_info; mon; mon = mon->next)
1149 if (mon->ob == tmp)
1150 break;
1151
1152 /* didn't find a match */
1153 if (!mon)
1154 return NULL;
1155
1156 if (mon->next)
1157 return mon->next->ob;
1158 else
1159 return first_mon_info->ob;
1160
1161 }
1162
1163 /* mon_info_msg() - generate a message detailing the properties
1164 * of a randomly selected monster.
1165 */
1166 static const char *
1167 mon_info_msg (int level)
1168 {
1169 static dynbuf_text buf; buf.clear ();
1170
1171 /*preamble */
1172 buf << "This beastiary contains:\n";
1173
1174 /* lets print info on as many monsters as will fit in our
1175 * document.
1176 * 8-96 Had to change this a bit, otherwise there would
1177 * have been an impossibly large number of combinations
1178 * of text! (and flood out the available number of titles
1179 * in the archive in a snap!) -b.t.
1180 */
1181 object *tmp = get_random_mon (level * 3);
1182 while (tmp && buf.size () < BOOK_BUF)
1183 {
1184 /* monster description */
1185 buf.printf ("\n%s\n", mon_desc (tmp));
1186
1187 /* Note that the value this returns is not based on level */
1188 tmp = get_next_mon (tmp);
1189 }
1190
1191 return buf;
1192 }
1193
1194
1195 /*****************************************************************************
1196 * Artifact msg generation code.
1197 ****************************************************************************/
1198
1199 /* artifact_msg() - generate a message detailing the properties
1200 * of 1-6 artifacts drawn sequentially from the artifact list.
1201 */
1202 static const char *
1203 artifact_msg (int level)
1204 {
1205 artifactlist *al = NULL;
1206 artifact *art;
1207 int chance, i, type, index;
1208 int book_entries = level > 5 ? rndm (3) + rndm (3) + 2 : rndm (level) + 1;
1209 const char *ch;
1210 char name[MAX_BUF];
1211 object *tmp = NULL;
1212
1213 static dynbuf_text buf; buf.clear ();
1214
1215 /* values greater than 5 create msg buffers that are too big! */
1216 if (book_entries > 5)
1217 book_entries = 5;
1218
1219 /* lets determine what kind of artifact type randomly.
1220 * Right now legal artifacts only come from those listed
1221 * in art_name_array. Also, we check to be sure an artifactlist
1222 * for that type exists!
1223 */
1224 i = 0;
1225 do
1226 {
1227 index = rndm (ecb_array_length (art_name_array));
1228 type = art_name_array[index].type;
1229 al = find_artifactlist (type);
1230 i++;
1231 }
1232 while ((al == NULL) && (i < 10));
1233
1234 if (i == 10) /* Unable to find a message */
1235 return "None";
1236
1237 /* There is no reason to start on the artifact list at the begining. Lets
1238 * take our starting position randomly... */
1239 art = al->items;
1240 for (i = rndm (level) + rndm (2) + 1; i > 0; i--)
1241 {
1242 if (!art)
1243 art = al->items; /* hmm, out of stuff, loop back around */
1244
1245 art = art->next;
1246 }
1247
1248 /* the base 'generic' name for our artifact */
1249 assign (name, art_name_array[index].name);
1250
1251 /* Ok, lets print out the contents */
1252 buf.printf ("Herein %s detailed %s...\n",
1253 book_entries > 1 ? "are" : "is",
1254 book_entries > 1 ? "some artifacts" : "an artifact");
1255
1256 /* artifact msg attributes loop. Lets keep adding entries to the 'book'
1257 * as long as we have space up to the allowed max # (book_entires)
1258 */
1259 while (book_entries > 0 && buf.size () < BOOK_BUF)
1260 {
1261
1262 if (!art)
1263 art = al->items;
1264
1265 buf << '\n';
1266
1267 /* Name */
1268 if (art->allowed && art->allowed->name != shstr_All)
1269 {
1270 linked_char *temp, *next = art->allowed;
1271
1272 do
1273 {
1274 temp = next;
1275 next = next->next;
1276 }
1277 while (next && rndm (2));
1278
1279 buf.printf ("A B<< %s of %s >>", &temp->name, &art->item->name);
1280 }
1281 else /* default name is used */
1282 buf.printf ("The B<< %s of %s >>", name, &art->item->name);
1283
1284 buf << " is ";
1285
1286 /* chance of finding */
1287 chance = (int) (100 * ((float) art->chance / al->total_chance));
1288 if (chance >= 20)
1289 buf << "an uncommon";
1290 else if (chance >= 10)
1291 buf << "an unusual";
1292 else if (chance >= 5)
1293 buf << "a rare";
1294 else
1295 buf << "a very rare";
1296
1297 /* value of artifact */
1298 buf << " item with a value that is " << art->item->value << " times normal.\n";
1299
1300 /* include the message about the artifact, if exists, and book
1301 * level is kinda high */
1302 if (art->item->msg
1303 && rndm (4) + 1 < level)
1304 buf << art->item->msg;
1305
1306 /* properties of the artifact */
1307 tmp = object::create ();
1308 add_abilities (tmp, art->item);
1309 tmp->type = type;
1310 tmp->set_flag (FLAG_IDENTIFIED);
1311 if ((ch = describe_item (tmp, 0)) && strlen (ch) > 1)
1312 buf << "\rProperties of this artifact include:\r" << ch << "\n";
1313
1314 tmp->destroy ();
1315
1316 art = art->next;
1317 book_entries--;
1318 }
1319
1320 return buf;
1321 }
1322
1323 /*****************************************************************************
1324 * Spellpath message generation
1325 *****************************************************************************/
1326
1327 /* spellpath_msg() - generate a message detailing the member
1328 * incantations/prayers (and some of their properties) belonging to
1329 * a given spellpath.
1330 */
1331 static char *
1332 spellpath_msg (int level)
1333 {
1334 static dynbuf_text buf; buf.clear ();
1335
1336 int path = rndm (NRSPELLPATHS), prayers = rndm (2);
1337 uint32 pnum = (path == -1) ? PATH_NULL : spellpathdef[path];
1338 archetype *at;
1339
1340 /* Preamble */
1341 buf << "Herein are detailed the names of "
1342 << (prayers ? "prayers" : "incantations");
1343
1344 if (path == -1)
1345 buf << " having no known spell path.\n";
1346 else
1347 buf << " belonging to the path of B<< " << spellpathnames[path] << " >>:\n\n";
1348
1349 int seen = 0;
1350
1351 for_all_archetypes (at)
1352 /* Determine if this is an appropriate spell. Must
1353 * be of matching path, must be of appropriate type (prayer
1354 * or not), and must be within the valid level range.
1355 */
1356 if (at->type == SPELL && at->path_attuned & pnum &&
1357 ((at->stats.grace && prayers) || (at->stats.sp && !prayers)) && (at->level < (level * 8)))
1358 {
1359 seen = 1;
1360 buf << at->object::name << '\r';
1361 }
1362
1363 /* Geez, no spells were generated. */
1364 if (!seen)
1365 if (rndm (4)) /* usually, lets make a recursive call... */
1366 return spellpath_msg (level);
1367 else /* give up, cause knowing no spells exist for path is info too. */
1368 buf << "- no known spells exist.\n";
1369
1370 return buf;
1371 }
1372
1373 /* formula_msg() - generate a message detailing the properties
1374 * of a randomly selected alchemical formula.
1375 */
1376 static void
1377 make_formula_book (object *book, int level)
1378 {
1379 char title[MAX_BUF];
1380 recipelist *fl;
1381 recipe *formula = NULL;
1382 int chance;
1383
1384 static dynbuf_text buf; buf.clear ();
1385
1386 /* the higher the book level, the more complex (ie number of
1387 * ingredients) the formula can be.
1388 */
1389 fl = get_formulalist (rndm (level) / 3 + 1);
1390
1391 if (!fl)
1392 fl = get_formulalist (1); /* safety */
1393
1394 if (fl->total_chance == 0)
1395 {
1396 book->msg = "<indecipherable text>\n";
1397 new_text_name (book, 4);
1398 add_author (book, 4);
1399 return;
1400 }
1401
1402 /* get a random formula, weighted by its bookchance */
1403 chance = rndm (fl->total_chance);
1404 for (formula = fl->items; formula; formula = formula->next)
1405 {
1406 chance -= formula->chance;
1407
1408 if (chance <= 0)
1409 break;
1410 }
1411
1412 if (!formula || formula->arch_names <= 0)
1413 {
1414 book->msg = "<indecipherable text>\n";
1415 new_text_name (book, 4);
1416 add_author (book, 4);
1417 return;
1418 }
1419
1420 /* looks like a formula was found. Base the amount
1421 * of information on the booklevel and the spellevel
1422 * of the formula. */
1423
1424 const char *op_name = formula->arch_name [rndm (formula->arch_names)];
1425 archetype *at;
1426
1427 /* preamble */
1428 buf << "Herein is described a project using B<< "
1429 << (formula->skill ? &formula->skill : "an unknown skill")
1430 << " >>:\n\n";
1431
1432 if ((at = archetype::find (op_name)))
1433 op_name = at->object::name;
1434 else
1435 LOG (llevError, "formula_msg() can't find arch %s for formula.\n", op_name);
1436
1437 /* item name */
1438 if (formula->title != shstr_NONE)
1439 {
1440 buf.printf ("The B<< %s of %s >>", op_name, &formula->title);
1441 /* This results in things like pile of philo. sulfur.
1442 * while philo. sulfur may look better, without this,
1443 * you get things like 'the wise' because its missing the
1444 * water of section.
1445 */
1446 sprintf (title, "%s: %s of %s",
1447 formula_book_name [rndm (sizeof (formula_book_name) / sizeof (char *))], op_name, &formula->title);
1448 }
1449 else
1450 {
1451 buf << "The B<< " << op_name;
1452
1453 sprintf (title, "%s: %s", formula_book_name [rndm (sizeof (formula_book_name) / sizeof (char *))], op_name);
1454 if (at->title)
1455 {
1456 buf << " " << at->title;
1457 strcat (title, " ");
1458 strcat (title, at->title);
1459 }
1460
1461 buf << " >>";
1462 }
1463
1464 /* Lets name the book something meaningful ! */
1465 book->name = title;
1466 book->title = NULL;
1467
1468 /* ingredients to make it */
1469 if (formula->ingred)
1470 {
1471 linked_char *next;
1472 archetype *at;
1473
1474 at = archetype::find (formula->cauldron);
1475
1476 buf.printf (" may be made at %s using the following ingredients:\n\n",
1477 at ? query_name (at) : "an appropriate place");
1478
1479 for (next = formula->ingred; next; next = next->next)
1480 buf << next->name << '\r';
1481 }
1482 else
1483 LOG (llevError, "formula_msg() no ingredient list for object %s of %s\n", op_name, &formula->title);
1484
1485 book->msg = buf;
1486 }
1487
1488 #define DESCRIBE_PATH(retbuf, variable, name) \
1489 if(variable) { \
1490 int i,j=0; \
1491 strcat(retbuf,"(" name ": "); \
1492 for(i=0; i<NRSPELLPATHS; i++) \
1493 if(variable & (1<<i)) { \
1494 if (j) \
1495 strcat(retbuf,", "); \
1496 else \
1497 j = 1; \
1498 strcat(retbuf, spellpathnames[i]); \
1499 } \
1500 strcat(retbuf,")"); \
1501 }
1502
1503 /* god_info_msg() - generate a message detailing the properties
1504 * of a random god. Used by the book hack. b.t.
1505 */
1506 static const char *
1507 god_info_msg (int level)
1508 {
1509 const char *name = NULL;
1510 object *god = pntr_to_god_obj (get_rand_god ());
1511
1512 static dynbuf_text buf; buf.clear ();
1513
1514 if (!god)
1515 return 0; /* oops, problems... */
1516
1517 name = god->name;
1518
1519 /* preamble.. */
1520 buf << "This document contains knowledge concerning the diety B<< "
1521 << name << " >>";
1522
1523 /* Always have as default information the god's descriptive terms. */
1524 if (nstrtok (god->msg, ",") > 0)
1525 buf << ", known as" << strtoktolin (god->msg, ",");
1526 else
1527 buf << "...";
1528
1529 buf << "\n\n";
1530
1531 int introlen = buf.size (); /* so we will know if no new info is added later */
1532
1533 /* Information about the god is random, and based on the level of the
1534 * 'book'. Probably there is a more intellegent way to implement
1535 * this ...
1536 */
1537 while (level > 0 && buf.size () < BOOK_BUF)
1538 {
1539 if (level == 2 && rndm (2))
1540 { /* enemy god */
1541 const char *enemy = god->title;
1542
1543 if (enemy)
1544 buf.printf ("The gods %s and %s are enemies.\r", name, enemy);
1545 }
1546
1547 if (level == 3 && rndm (2))
1548 { /* enemy race, what the god's holy word effects */
1549 const char *enemy = god->slaying;
1550 int i;
1551
1552 if (enemy && !(god->path_denied & PATH_TURNING))
1553 if ((i = nstrtok (enemy, ",")) > 0)
1554 {
1555 buf << "The holy words of " << name
1556 << " have the power to slay creatures belonging to the ";
1557
1558 if (i > 1)
1559 buf << "following races:" << strtoktolin (enemy, ",");
1560 else
1561 buf << "race of" << strtoktolin (enemy, ",");
1562
1563 buf << '\r';
1564 }
1565 }
1566
1567 if (level == 4 && rndm (2))
1568 { /* Priest of god gets these protect,vulnerable... */
1569 if (const char *cp = describe_resistance (god, 1))
1570 { /* This god does have protections */
1571 buf << name
1572 << " has a potent aura which is extended to"
1573 " faithful priests. The effects of this aura include: "
1574 << cp
1575 << ".\r";
1576 }
1577 }
1578
1579 if (level == 5 && rndm (2))
1580 { /* aligned race, summoning */
1581 const char *race = god->race; /* aligned race */
1582 int i;
1583
1584 if (race && !(god->path_denied & PATH_SUMMON))
1585 if ((i = nstrtok (race, ",")) > 0)
1586 {
1587 buf << "Creatures sacred to " << name << " include the ";
1588 if (i > 1)
1589 buf << "following races:" << strtoktolin (race, ",");
1590 else
1591 buf << "race of" << strtoktolin (race, ",");
1592
1593 buf << '\r';
1594 }
1595 }
1596
1597 if (level == 6 && rndm (2))
1598 { /* blessing,curse properties of the god */
1599 if (const char *cp = describe_resistance (god, 1))
1600 { /* This god does have protections */
1601 buf << "The priests of " << name
1602 << " are known to be able to "
1603 "bestow a blessing which makes the recipient "
1604 << cp
1605 << '\r';
1606 }
1607 }
1608
1609 if (level == 8 && rndm (2))
1610 { /* immunity, holy possession */
1611 buf << "The priests of " << name
1612 << " are known to make cast a mighty"
1613 " prayer of possession";
1614
1615 int first = 1;
1616
1617 for (int i = 0; i < NROFATTACKS; i++)
1618 if (god->resist[i] == 100)
1619 {
1620 if (first)
1621 {
1622 buf << " which gives the recipient";
1623 first = 0;
1624 }
1625 else
1626 buf << ", ";
1627
1628 buf << " immunity to " << attacktype_desc[i];
1629 }
1630
1631 buf << ".\r";
1632 }
1633
1634 if (level == 12 && rndm (2))
1635 { /* spell paths */
1636 //TODO:
1637 int has_effect = 0, tmpvar;
1638 char tmpbuf[MAX_BUF];
1639
1640 sprintf (tmpbuf, "It is rarely known fact that the priests of %s\n", name);
1641 strcat (tmpbuf, "are mystically transformed. Effects of this include:\n");
1642
1643 if ((tmpvar = god->path_attuned))
1644 {
1645 has_effect = 1;
1646 DESCRIBE_PATH (tmpbuf, tmpvar, "Attuned");
1647 }
1648
1649 if ((tmpvar = god->path_repelled))
1650 {
1651 has_effect = 1;
1652 DESCRIBE_PATH (tmpbuf, tmpvar, "Repelled");
1653 }
1654
1655 if ((tmpvar = god->path_denied))
1656 {
1657 has_effect = 1;
1658 DESCRIBE_PATH (tmpbuf, tmpvar, "Denied");
1659 }
1660
1661 if (has_effect)
1662 buf << tmpbuf << '\r';
1663 else
1664 buf << '\r';
1665 }
1666
1667 level--;
1668 }
1669
1670 if (buf.size () == introlen)
1671 /* we got no information beyond the preamble! */
1672 buf << "[Unfortunately the rest of the information is hopelessly garbled!]";
1673
1674 return buf;
1675 }
1676
1677 /* tailor_readable_ob()- The main routine. This chooses a random
1678 * message to put in given readable object (type==BOOK) which will
1679 * be referred hereafter as a 'book'. We use the book level to de-
1680 * termine the value of the information we will insert. Higher
1681 * values mean the book will (generally) have better/more info.
1682 * See individual cases as to how this will be utilized.
1683 * "Book" name/content length are based on the weight of the
1684 * document. If the value of msg_type is negative, we will randomly
1685 * choose the kind of message to generate.
1686 * -b.t. thomas@astro.psu.edu
1687 *
1688 * book is the object we are creating into.
1689 * If msg_type is a positive value, we use that to determine the
1690 * message type - otherwise a random value is used.
1691 *
1692 */
1693 void
1694 tailor_readable_ob (object *book, int msg_type)
1695 {
1696 int level = book->level ? rndm (book->level) + 1 : 1;
1697
1698 /* safety */
1699 if (book->type != BOOK)
1700 return;
1701
1702 if (level <= 0)
1703 return; /* if no level no point in doing any more... */
1704
1705 /* &&& The message switch &&& */
1706 /* Below all of the possible types of messages in the "book"s.
1707 */
1708 /*
1709 * IF you add a new type of book msg, you will have to do several things.
1710 * 1) make sure there is an entry in the msg switch below!
1711 * 2) make sure there is an entry in max_titles[] array.
1712 * 3) make sure there are entries for your case in new_text_title()
1713 * and add_authour().
1714 * 4) you may want separate authour/book name arrays in read.h
1715 */
1716 const char *new_msg = "";
1717 msg_type = msg_type > 0 ? msg_type : rndm (8);
1718 switch (msg_type)
1719 {
1720 case 1: /* monster attrib */
1721 new_msg = mon_info_msg (level);
1722 break;
1723 case 2: /* artifact attrib */
1724 new_msg = artifact_msg (level);
1725 break;
1726 case 3: /* grouping incantations/prayers by path */
1727 new_msg = spellpath_msg (level);
1728 break;
1729 case 4: /* describe an alchemy formula */
1730 make_formula_book (book, level);
1731 /* make_formula_book already gives title */
1732 return;
1733 case 5: /* bits of information about a god */
1734 new_msg = god_info_msg (level);
1735 break;
1736 case 0: /* use info list in lib/ */
1737 default:
1738 cfperl_make_book (book, level);
1739 /* already gives title */
1740 return;
1741 }
1742
1743 if (strlen (new_msg) > 1)
1744 {
1745 book->msg = new_msg;
1746 /* lets give the "book" a new name, which may be a compound word */
1747 change_book (book, msg_type);
1748 }
1749 }
1750
1751 /*****************************************************************************
1752 *
1753 * Writeback routine for updating the bookarchive.
1754 *
1755 ****************************************************************************/
1756 /* write_book_archive() - write out the updated book archive */
1757 void
1758 write_book_archive ()
1759 {
1760 FILE *fp;
1761 int index = 0;
1762 char fname[MAX_BUF];
1763 title *book = NULL;
1764 titlelist *bl = get_titlelist (0);
1765
1766 /* If nothing changed, don't write anything */
1767 if (!need_to_write_bookarchive)
1768 return;
1769
1770 need_to_write_bookarchive = 0;
1771
1772 sprintf (fname, "%s/bookarch", settings.localdir);
1773 LOG (llevDebug, "Updating book archive: %s...\n", fname);
1774
1775 if ((fp = fopen (fname, "w")) == NULL)
1776 LOG (llevDebug, "Can't open book archive file %s\n", fname);
1777 else
1778 {
1779 while (bl)
1780 {
1781 for (book = bl->first_book; book; book = book->next)
1782 if (book && book->authour)
1783 {
1784 fprintf (fp, "title %s\n", &book->name);
1785 fprintf (fp, "authour %s\n", &book->authour);
1786 fprintf (fp, "arch %s\n", &book->archname);
1787 fprintf (fp, "level %d\n", book->level);
1788 fprintf (fp, "type %d\n", index);
1789 fprintf (fp, "size %d\n", book->size);
1790 fprintf (fp, "index %d\n", book->msg_index);
1791 fprintf (fp, "end\n");
1792 }
1793
1794 bl = bl->next;
1795 index++;
1796 }
1797
1798 fclose (fp);
1799 chmod (fname, SAVE_MODE);
1800 }
1801 }
1802
1803 readable_message_type *
1804 get_readable_message_type (object *readable)
1805 {
1806 uint8 subtype = readable->subtype;
1807
1808 if (subtype > last_readable_subtype)
1809 return &readable_message_types[0];
1810
1811 return &readable_message_types[subtype];
1812 }