/* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team * Copyright (©) 1992,2007 Frank Tore Johansen * * Deliantra is free software: you can redistribute it and/or modify it under * the terms of the Affero GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the Affero GNU General Public License * and the GNU General Public License along with this program. If not, see * . * * The authors can be reached via e-mail to */ /* This file contains code relevant to the BOOKS hack -- designed * to allow randomly occuring messages in non-magical texts. */ /* laid down initial file - dec 1995. -b.t. thomas@astro.psu.edu */ #include #include #include #include /* Define this if you want to archive book titles by contents. * This option should enforce UNIQUE combinations of titles,authors and * msg contents during and *between* game sessions. * Note: a slight degeneracy exists since books are archived based on an integer * index value calculated from the message text (similar to alchemy formulae). * Sometimes two widely different messages have the same index value (rare). In * this case, it is possible to occasionally generate 2 books with same title and * different message content. Not really a bug, but rather a feature. This action * should keeps player on their toes ;). * Also, note that there is *finite* space available for archiving message and titles. * Once this space is used, books will stop being archived. Not a serious problem * under the current regime, since there are generally fewer possible (random) * messages than space available on the titlelists. * One exception (for sure) are the monster messages. But no worries, you should * see all of the monster info in some order (but not all possble combinations) * before the monster titlelist space is run out. You can increase titlelist * space by increasing the array sizes for the monster book_authours and book_names * (see max_titles[] array and include/read.h). Since the unique_book algorthm is * kinda stupid, this program *may* slow down program execution if defined (but I don't * think its a significant problem, at least, I have no problems running this option * on a Sparc 10! Also, once archive title lists are filled and/or all possible msg * combinations have been generated, unique_book isnt called anymore. It takes 5-10 * sessions for this to happen). * Final note: the game remembers book/title/msg combinations from reading the * file lib/bookarch. If you REMOVE this file, you will lose your archive. So * be sure to copy it over to the new lib directory when you change versions. * -b.t. */ /* This flag is useful to see what kind of output messages are created */ /* #define BOOK_MSG_DEBUG */ /* This flag is useful for debuging archiving action */ /* #define ARCHIVE_DEBUG */ /* Moved these structures from struct.h to this file in 0.94.3 - they * are not needed anyplace else, so why have them globally declared? */ /* 'title' and 'titlelist' are used by the readable code */ struct title : zero_initialised { shstr name; /* the name of the book */ shstr authour; /* the name of the book authour */ shstr archname; /* the archetype name of the book */ int level; /* level of difficulty of this message */ int size; /* size of the book message */ int msg_index; /* an index value derived from book message */ title *next; }; struct titlelist : zero_initialised { int number; /* number of items in the list */ title *first_book; /* pointer to first book in this list */ titlelist *next; /* pointer to next book list */ }; /* special structure, used only by art_name_array[] */ struct arttypename { const char *name; /* generic name to call artifacts of this type */ int type; /* matching type */ }; /* booklist is the buffer of books read in from the bookarch file */ static titlelist *booklist = NULL; static objectlink *first_mon_info = NULL; /* these are needed for creation of a linked list of * pointers to all (hostile) monster objects */ static int nrofmon = 0, need_to_write_bookarchive = 0; /* * Spellpath information */ static uint32 spellpathdef[NRSPELLPATHS] = { PATH_PROT, PATH_FIRE, PATH_FROST, PATH_ELEC, PATH_MISSILE, PATH_SELF, PATH_SUMMON, PATH_ABJURE, PATH_RESTORE, PATH_DETONATE, PATH_MIND, PATH_CREATE, PATH_TELE, PATH_INFO, PATH_TRANSMUTE, PATH_TRANSFER, PATH_TURNING, PATH_WOUNDING, PATH_DEATH, PATH_LIGHT }; static const char *const path_book_name[] = { "codex", "compendium", "exposition", "tables", "treatise" }; /* used by spellpath texts */ static const char *const path_author[] = { "aether", "astral byways", "connections", "the Grey Council", "deep pathways", "knowledge", "magic", "mystic ways", "pathways", "power", "spells", "transforms", "the mystic veil", "unknown spells" }; /* * Artiface/item information */ /* if it isnt listed here, then art_attr_msg will never generate * a message for this type of artifact. -b.t. */ static arttypename art_name_array[] = { {"Helmet", HELMET}, {"Amulet", AMULET}, {"Shield", SHIELD}, {"Bracers", BRACERS}, {"Boots", BOOTS}, {"Cloak", CLOAK}, {"Gloves", GLOVES}, {"Girdle", GIRDLE}, {"Ring", RING}, {"Horn", HORN}, {"Missile Weapon", BOW}, {"Missile", ARROW}, {"Hand Weapon", WEAPON}, {"Artifact", SKILL}, {"Food", FOOD}, {"Body Armour", ARMOUR} }; static const char *const art_book_name[] = { "collection", "file", "files", "guide", "handbook", "index", "inventory", "list", "listing", "record", "record book" }; /* used by artifact texts */ static const char *const art_author[] = { "ancient things", "artifacts", "Havlor", /* ancient warrior scribe :) */ "items", "lost artifacts", "the ancients", "useful things" }; /* * Monster book information */ static const char *const mon_book_name[] = { "beastuary", "catalog", "compilation", "collection", "encyclopedia", "guide", "handbook", "list", "manual", "notes", "record", "register", "volume" }; /* used by monster beastuary texts */ static const char *const mon_author[] = { "beasts", "creatures", "dezidens", "dwellers", "evil nature", "life", "monsters", "nature", "new life", "residents", "the spawn", "the living", "things" }; /* * God book information */ static const char *const gods_book_name[] = { "devotional", "devout notes", "divine text", "divine work", "holy book", "holy record", "moral text", "sacred guide", "testament", "transcript" }; /* used by gods texts */ static const char *const gods_author[] = { "cults", "joy", "lasting curse", "madness", "religions", "the dead", "the gods", "the heirophant", "the poor priest", "the priestess", "pain", "white" }; /* * Alchemy (formula) information */ static const char *const formula_book_name[] = { "cookbook", "formulary", "lab book", "lab notes", "recipe book", "experiment record", "work plan", "design notes" }; /* this isn't used except for empty books */ static const char *const formula_author[] = { "Albertus Magnus", "alchemy", "balms", "creation", "dusts", "magical manufacture", "making", "philosophical items", "potions", "powders", "the cauldron", "the lamp black", "transmutation", "waters" }; /* * Generic book information */ /* used by msg file and 'generic' books */ static const char *const light_book_name[] = { "calendar", "datebook", "diary", "guidebook", "handbook", "ledger", "notes", "notebook", "octavo", "pamphlet", "practicum", "script", "transcript" }; static const char *const heavy_book_name[] = { "catalog", "compendium", "guide", "manual", "opus", "tome", "treatise", "volume", "work" }; /* used by 'generic' books */ static const char *const book_author[] = { "Abdulah", "Al'hezred", "Alywn", "Arundel", "Arvind", "Aerlingas", "Bacon", "Baliqendii", "Bosworth", "Beathis", "Bertil", "Cauchy", "Chakrabarti", "der Waalis", "Dirk", "Djwimii", "Eisenstaadt", "Fendris", "Frank", "Habbi", "Harlod", "Ichibod", "Janus", "June", "Magnuson", "Nandii", "Nitfeder", "Norris", "Parael", "Penhew", "Sophia", "Skilly", "Tahir", "Thockmorton", "Thomas", "van Helsing", "van Pelt", "Voormis", "Xavier", "Xeno", "Zardoz", "Zagy" }; static const char *const book_descrpt[] = { "ancient", "cryptic", "cryptical", "dusty", "hiearchical", "grizzled", "gold-guilt", "great", "lost", "magnificent", "musty", "mythical", "mystical", "rustic", "stained", "silvered", "transcendental", "weathered" }; /* Each line of this array is a readable subtype * Be careful to keep the order. If you add readable subtype, add them * at the bottom of the list. Never delete a subtype because index is used as * subtype paramater in arch files! */ static readable_message_type readable_message_types[] = { /*subtype 0 */ {0, 0, "info"}, /* book messages subtypes */ /*subtype 1 */ {MSG_TYPE_BOOK, MSG_TYPE_BOOK_CLASP_1, "readable-book-clasp-1"}, {MSG_TYPE_BOOK, MSG_TYPE_BOOK_CLASP_2, "readable-book-clasp-2"}, {MSG_TYPE_BOOK, MSG_TYPE_BOOK_ELEGANT_1, "readable-book-elegant-1"}, {MSG_TYPE_BOOK, MSG_TYPE_BOOK_ELEGANT_2, "readable-book-elegant-2"}, {MSG_TYPE_BOOK, MSG_TYPE_BOOK_QUARTO_1, "readable-book-quarto-1"}, {MSG_TYPE_BOOK, MSG_TYPE_BOOK_QUARTO_2, "readable-book-quarto-2"}, {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_EVOKER, "readable-book-spell-evocation"}, {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_PRAYER, "readable-book-spell-praying"}, {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_PYRO, "readable-book-spell-pyromancy"}, /*subtype 10 */ {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_SORCERER, "readable-book-spell-sorcery"}, {MSG_TYPE_BOOK, MSG_TYPE_BOOK_SPELL_SUMMONER, "readable-book-spell-summoning"}, /* card messages subtypes */ {MSG_TYPE_CARD, MSG_TYPE_CARD_SIMPLE_1, "readable-card-simple-1"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_SIMPLE_2, "readable-card-simple-2"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_SIMPLE_3, "readable-card-simple-3"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_ELEGANT_1, "readable-card-elegant-1"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_ELEGANT_2, "readable-card-elegant-2"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_ELEGANT_3, "readable-card-elegant-3"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_STRANGE_1, "readable-card-strange-1"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_STRANGE_2, "readable-card-strange-2"}, /*subtype 20 */ {MSG_TYPE_CARD, MSG_TYPE_CARD_STRANGE_3, "readable-card-strange-3"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_MONEY_1, "readable-card-money-1"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_MONEY_2, "readable-card-money-2"}, {MSG_TYPE_CARD, MSG_TYPE_CARD_MONEY_3, "readable-card-money-3"}, /* Paper messages subtypes */ {MSG_TYPE_PAPER, MSG_TYPE_PAPER_NOTE_1, "readable-paper-note-1"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_NOTE_2, "readable-paper-note-2"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_NOTE_3, "readable-paper-note-3"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_LETTER_OLD_1, "readable-paper-letter-old-1"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_LETTER_OLD_2, "readable-paper-letter-old-2"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_LETTER_NEW_1, "readable-paper-letter-new-1"}, /*subtype 30 */ {MSG_TYPE_PAPER, MSG_TYPE_PAPER_LETTER_NEW_2, "readable-paper-letter-new-2"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_ENVELOPE_1, "readable-paper-envelope-1"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_ENVELOPE_2, "readable-paper-envelope-2"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_OLD_1, "readable-paper-scroll-old-1"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_OLD_2, "readable-paper-scroll-old-2"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_NEW_1, "readable-paper-scroll-new-1"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_NEW_2, "readable-paper-scroll-new-2"}, {MSG_TYPE_PAPER, MSG_TYPE_PAPER_SCROLL_MAGIC, "readable-paper-scroll-magic"}, /* road signs messages subtypes */ {MSG_TYPE_SIGN, MSG_TYPE_SIGN_BASIC, "readable-sign-basic"}, {MSG_TYPE_SIGN, MSG_TYPE_SIGN_DIR_LEFT, "readable-sign-dir-left"}, /*subtype 40 */ {MSG_TYPE_SIGN, MSG_TYPE_SIGN_DIR_RIGHT, "readable-sign-dir-right"}, {MSG_TYPE_SIGN, MSG_TYPE_SIGN_DIR_BOTH, "readable-sign-dir-both"}, /* stones and monument messages */ {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STONE_1, "readable-monument-stone-1"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STONE_2, "readable-monument-stone-2"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STONE_3, "readable-monument-stone-3"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STATUE_1, "readable-monument-statue-1"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STATUE_2, "readable-monument-statue-2"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_STATUE_3, "readable-monument-statue-3"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_GRAVESTONE_1, "readable-monument-gravestone-1"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_GRAVESTONE_2, "readable-monument-gravestone-2"}, /*subtype 50 */ {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_GRAVESTONE_3, "readable-monument-gravestone-3"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_1, "readable-monument-wall-1"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_2, "readable-monument-wall-2"}, {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_3, "readable-monument-wall-3"} }; static int last_readable_subtype = sizeof (readable_message_types) / sizeof (readable_message_type); static int max_titles[6] = { ((sizeof (light_book_name) / sizeof (char *)) + (sizeof (heavy_book_name) / sizeof (char *))) * (sizeof (book_author) / sizeof (char *)), (sizeof (mon_book_name) / sizeof (char *)) * (sizeof (mon_author) / sizeof (char *)), (sizeof (art_book_name) / sizeof (char *)) * (sizeof (art_author) / sizeof (char *)), (sizeof (path_book_name) / sizeof (char *)) * (sizeof (path_author) / sizeof (char *)), (sizeof (formula_book_name) / sizeof (char *)) * (sizeof (formula_author) / sizeof (char *)), (sizeof (gods_book_name) / sizeof (char *)) * (sizeof (gods_author) / sizeof (char *)) }; /****************************************************************************** * * Start of misc. readable functions used by others functions in this file * *****************************************************************************/ static titlelist * get_empty_booklist () { titlelist *bl = new titlelist; bl->number = 0; bl->first_book = NULL; bl->next = NULL; return bl; } static title * get_empty_book () { title *t = new title; t->name = NULL; t->archname = NULL; t->authour = NULL; t->level = 0; t->size = 0; t->msg_index = 0; t->next = NULL; return t; } /* get_titlelist() - returns pointer to the title list referanced by i */ static titlelist * get_titlelist (int i) { titlelist *tl = booklist; int number = i; if (number < 0) return tl; while (tl && number) { if (!tl->next) tl->next = get_empty_booklist (); tl = tl->next; number--; } return tl; } /* HANDMADE STRING FUNCTIONS.., perhaps these belong in another file * (shstr.c ?), but the quantity BOOK_BUF will need to be defined. */ /* nstrtok() - simple routine to return the number of list * items in buf1 as separated by the value of buf2 */ static int nstrtok (const char *buf1, const char *buf2) { char *tbuf, sbuf[12], buf[MAX_BUF]; int number = 0; if (!buf1 || !buf2) return 0; sprintf (buf, "%s", buf1); sprintf (sbuf, "%s", buf2); tbuf = strtok (buf, sbuf); while (tbuf) { number++; tbuf = strtok (NULL, sbuf); } return number; } /* strtoktolin() - takes a string in buf1 and separates it into * a list of strings delimited by buf2. Then returns a comma * separated string w/ decent punctuation. */ static char * strtoktolin (const char *buf1, const char *buf2) { int maxi, i = nstrtok (buf1, buf2); char *tbuf, buf[MAX_BUF], sbuf[12]; static char rbuf[BOOK_BUF]; maxi = i; strcpy (buf, buf1); strcpy (sbuf, buf2); strcpy (rbuf, " "); tbuf = strtok (buf, sbuf); while (tbuf && i > 0) { strcat (rbuf, tbuf); i--; if (i == 1 && maxi > 1) strcat (rbuf, " and "); else if (i > 0 && maxi > 1) strcat (rbuf, ", "); else strcat (rbuf, "."); tbuf = strtok (NULL, sbuf); } return (char *) rbuf; } /***************************************************************************** * * Start of initialisation related functions. * ****************************************************************************/ /* init_book_archive() - if not called before, initialise the info list * This reads in the bookarch file into memory. bookarch is the file * created and updated across multiple runs of the program. */ static void init_book_archive () { FILE *fp; int comp, nroftitle = 0; char buf[MAX_BUF], fname[MAX_BUF], *cp; title *book = NULL; titlelist *bl = get_empty_booklist (); static int did_init_barch; if (did_init_barch) return; did_init_barch = 1; if (!booklist) booklist = bl; sprintf (fname, "%s/bookarch", settings.localdir); LOG (llevDebug, " Reading bookarch from %s...\n", fname); if ((fp = open_and_uncompress (fname, 0, &comp)) != NULL) { int value, type = 0; size_t i; while (fgets (buf, MAX_BUF, fp) != NULL) { if (*buf == '#') continue; if ((cp = strchr (buf, '\n')) != NULL) *cp = '\0'; cp = buf; while (*cp == ' ') /* Skip blanks */ cp++; if (!strncmp (cp, "title", 4)) { book = get_empty_book (); /* init new book entry */ book->name = strchr (cp, ' ') + 1; type = -1; nroftitle++; continue; } if (!strncmp (cp, "authour", 4)) { book->authour = strchr (cp, ' ') + 1; } if (!strncmp (cp, "arch", 4)) { book->archname = strchr (cp, ' ') + 1; } else if (sscanf (cp, "level %d", &value)) { book->level = (uint16) value; } else if (sscanf (cp, "type %d", &value)) { type = (uint16) value; } else if (sscanf (cp, "size %d", &value)) { book->size = (uint16) value; } else if (sscanf (cp, "index %d", &value)) { book->msg_index = (uint16) value; } else if (!strncmp (cp, "end", 3)) { /* link it */ bl = get_titlelist (type); book->next = bl->first_book; bl->first_book = book; bl->number++; } } LOG (llevDebug, "book archives(used/avail): \n"); for (bl = booklist, i = 0; bl != NULL && i < sizeof (max_titles) / sizeof (*max_titles); bl = bl->next, i++) { LOG (llevDebug, " (%d/%d)\n", bl->number, max_titles[i]); } close_and_delete (fp, comp); } #ifdef BOOK_MSG_DEBUG LOG (llevDebug, "init_book_archive() got %d titles.\n", nroftitle); #endif LOG (llevDebug, " done.\n"); } /* init_mon_info() - creates the linked list of pointers to * monster archetype objects if not called previously */ static void init_mon_info () { archetype *at; static int did_init_mon_info = 0; if (did_init_mon_info) return; did_init_mon_info = 1; for_all_archetypes (at) if (at->flag [FLAG_MONSTER] && at->is_head () && (!at->flag [FLAG_CHANGING] || at->flag [FLAG_UNAGGRESSIVE])) { objectlink *mon = new objectlink; mon->ob = at; mon->next = first_mon_info; first_mon_info = mon; nrofmon++; } LOG (llevDebug, "init_mon_info() got %d monsters\n", nrofmon); } /* init_readable() - initialise linked lists utilized by * message functions in tailor_readable_ob() * * This is the function called by the main routine to initialise * all the readable information. */ void init_readable () { static int did_this; if (did_this) return; did_this = 1; LOG (llevDebug, "Initialising reading data...\n"); init_book_archive (); init_mon_info (); LOG (llevDebug, " Done\n"); } /***************************************************************************** * * This is the start of the administrative functions when creating * new books (ie, updating title and the like) * *****************************************************************************/ /* find_title() - Search the titlelist (based on msgtype) to see if * book matches something already there. IF so, return that title. */ static title * find_title (const object *book, int msgtype) { title *t = NULL; titlelist *tl = get_titlelist (msgtype); int length = strlen (book->msg); int index = strtoint (book->msg); if (msgtype < 0) return (title *) NULL; if (tl) t = tl->first_book; while (t) if (t->size == length && t->msg_index == index) break; else t = t->next; #ifdef ARCHIVE_DEBUG if (t) LOG (llevDebug, "Found title match (list %d): %s %s (%d)\n", msgtype, &t->name, &t->authour, t->msg_index); #endif return t; } /* new_text_name() - Only for objects of type BOOK. SPELLBOOK stuff is * handled directly in change_book_name(). Names are based on text * msgtype * this sets book book->name based on msgtype given. What name * is given is based on various criteria */ static void new_text_name (object *book, int msgtype) { int nbr; char name[MAX_BUF]; if (book->type != BOOK) return; switch (msgtype) { case 1: /*monster */ nbr = sizeof (mon_book_name) / sizeof (char *); assign (name, mon_book_name[rndm (nbr)]); break; case 2: /*artifact */ nbr = sizeof (art_book_name) / sizeof (char *); assign (name, art_book_name[rndm (nbr)]); break; case 3: /*spellpath */ nbr = sizeof (path_book_name) / sizeof (char *); assign (name, path_book_name[rndm (nbr)]); break; case 4: /*alchemy */ nbr = sizeof (formula_book_name) / sizeof (char *); assign (name, formula_book_name[rndm (nbr)]); break; case 5: /*gods */ nbr = sizeof (gods_book_name) / sizeof (char *); assign (name, gods_book_name[rndm (nbr)]); break; case 6: /*msg file */ default: if (book->weight > 2000) { /* based on weight */ nbr = sizeof (heavy_book_name) / sizeof (char *); assign (name, heavy_book_name[rndm (nbr)]); } else if (book->weight < 2001) { nbr = sizeof (light_book_name) / sizeof (char *); assign (name, light_book_name[rndm (nbr)]); } break; } book->name = name; } /* add_book_author() * A lot like new_text_name above, but instead chooses an author * and sets op->title to that value */ static void add_author (object *op, int msgtype) { char title[MAX_BUF], name[MAX_BUF]; int nbr = sizeof (book_author) / sizeof (char *); if (msgtype < 0 || strlen (op->msg) < 5) return; switch (msgtype) { case 1: /* monster */ nbr = sizeof (mon_author) / sizeof (char *); assign (name, mon_author[rndm (nbr)]); break; case 2: /* artifacts */ nbr = sizeof (art_author) / sizeof (char *); assign (name, art_author[rndm (nbr)]); break; case 3: /* spellpath */ nbr = sizeof (path_author) / sizeof (char *); assign (name, path_author[rndm (nbr)]); break; case 4: /* alchemy */ nbr = sizeof (formula_author) / sizeof (char *); assign (name, formula_author[rndm (nbr)]); break; case 5: /* gods */ nbr = sizeof (gods_author) / sizeof (char *); assign (name, gods_author[rndm (nbr)]); break; case 6: /* msg file */ default: assign (name, book_author[rndm (nbr)]); } sprintf (title, "of %s", name); op->title = title; } /* unique_book() - check to see if the book title/msg is unique. We * go through the entire list of possibilities each time. If we find * a match, then unique_book returns true (because inst unique). */ static int unique_book (const object *book, int msgtype) { if (!booklist) return 1; /* No archival entries! Must be unique! */ /* Go through the booklist. If the author and name match, not unique so * return 0. */ for (title *test = get_titlelist (msgtype)->first_book; test; test = test->next) if (test->name == book->name && book->title == test->authour) return 0; return 1; } /* add_book_to_list() */ static void add_book_to_list (const object *book, int msgtype) { titlelist *tl = get_titlelist (msgtype); title *t; if (!tl) { LOG (llevError, "add_book_to_list can't get booklist!\n"); return; } t = get_empty_book (); t->name = book->name; t->authour = book->title; t->size = strlen (book->msg); t->msg_index = strtoint (book->msg); t->archname = book->arch->archname; t->level = book->level; t->next = tl->first_book; tl->first_book = t; tl->number++; /* We have stuff we need to write now */ need_to_write_bookarchive = 1; #ifdef ARCHIVE_DEBUG LOG (llevDebug, "Archiving new title: %s %s (%d)\n", &book->name, &book->title, msgtype); #endif } /* change_book() - give a new, fancier name to generated * objects of type BOOK and SPELLBOOK. * Aug 96 I changed this so we will attempt to create consistent * authour/title and message content for BOOKs. Also, we will * alter books that match archive entries to the archival * levels and architypes. -b.t. */ #define MAX_TITLE_CHECK 20 static void change_book (object *book, int msgtype) { int nbr = sizeof (book_descrpt) / sizeof (char *); switch (book->type) { case BOOK: { titlelist *tl = get_titlelist (msgtype); title *t = NULL; int tries = 0; /* look to see if our msg already been archived. If so, alter * the book to match the archival text. If we fail to match, * then we archive the new title/name/msg combo if there is * room on the titlelist. */ if ((strlen (book->msg) > 5) && (t = find_title (book, msgtype))) { /* alter book properties */ if (object *tmpbook = get_archetype (t->archname)) { tmpbook->msg = book->msg; tmpbook->copy_to (book); tmpbook->destroy (); } book->title = t->authour; book->name = t->name; book->level = t->level; } /* Don't have any default title, so lets make up a new one */ else { int numb, maxnames = max_titles[msgtype]; const char *old_title; const char *old_name; old_title = book->title; old_name = book->name; /* some pre-generated books have title already set (from * maps), also don't bother looking for unique title if * we already used up all the available names! */ if (!tl) { LOG (llevError, "change_book_name(): can't find title list\n"); numb = 0; } else numb = tl->number; if (numb == maxnames) { #ifdef ARCHIVE_DEBUG LOG (llevDebug, "titles for list %d full (%d possible).\n", msgtype, maxnames); #endif } /* shouldnt change map-maker books */ else if (!book->title) do { /* random book name */ new_text_name (book, msgtype); add_author (book, msgtype); /* random author */ tries++; } while (!unique_book (book, msgtype) && tries < MAX_TITLE_CHECK); /* Now deal with 2 cases. * 1)If no space for a new title exists lets just restore * the old book properties. Remember, if the book had * matchd an older entry on the titlelist, we shouldnt * have called this routine in the first place! * 2) If we got a unique title, we need to add it to * the list. */ if (tries == MAX_TITLE_CHECK || numb == maxnames) { /* got to check maxnames again */ #ifdef ARCHIVE_DEBUG LOG (llevDebug, "Failed to obtain unique title for %s %s (names:%d/%d)\n", &book->name, &book->title, numb, maxnames); #endif /* restore old book properties here */ book->title = old_title; if (rndm (4)) { /* Lets give the book a description to individualize it some */ char new_name[MAX_BUF]; snprintf (new_name, MAX_BUF, "%s %s", book_descrpt[rndm (nbr)], old_name); book->name = new_name; } else { book->name = old_name; } } else if (book->title && strlen (book->msg) > 5) { /* archive if long msg texts */ add_book_to_list (book, msgtype); } } break; } default: LOG (llevError, "change_book_name() called w/ illegal obj type.\n"); return; } } /***************************************************************************** * * This is the start of the area that generates the actual contents * of the book. * *****************************************************************************/ /***************************************************************************** * Monster msg generation code. ****************************************************************************/ /* get_random_mon() - returns a random monster slected from linked * list of all monsters in the current game. If level is non-zero, * then only monsters greater than that level will be returned. * Changed 971225 to be greater than equal to level passed. Also * made choosing by level more random. */ object * get_random_mon (int level) { objectlink *mon = first_mon_info; int i = 0, monnr; /* safety check. Problem w/ init_mon_info list? */ if (!nrofmon || !mon) return (object *) NULL; if (!level) { /* lets get a random monster from the mon_info linked list */ monnr = rndm (nrofmon); for (mon = first_mon_info, i = 0; mon; mon = mon->next) if (i++ == monnr) break; if (!mon) { LOG (llevError, "get_random_mon: Didn't find a monster when we should have\n"); return NULL; } return mon->ob; } /* Case where we are searching by level. Redone 971225 to be clearer * and more random. Before, it looks like it took a random monster from * the list, and then returned the first monster after that which was * appropriate level. This wasn't very random because if you had a * bunch of low level monsters and then a high level one, if the random * determine took one of the low level ones, it would just forward to the * high level one and return that. Thus, monsters that immediatly followed * a bunch of low level monsters would be more heavily returned. It also * means some of the dragons would be poorly represented, since they * are a group of high level monsters all around each other. */ /* First count number of monsters meeting level criteria */ for (mon = first_mon_info, i = 0; mon; mon = mon->next) if (mon->ob->level >= level) i++; if (i == 0) { LOG (llevError, "get_random_mon() couldn't return monster for level %d\n", level); return NULL; } monnr = rndm (i); for (mon = first_mon_info; mon; mon = mon->next) if (mon->ob->level >= level && monnr-- == 0) return mon->ob; if (!mon) { LOG (llevError, "get_random_mon(): didn't find a monster when we should have\n"); return NULL; } return NULL; /* Should be unreached, by keeps warnings down */ } /* * Returns a description of the monster. This really needs to be * redone, as describe_item really gives a pretty internal description. */ static const char * mon_desc (const object *mon) { static dynbuf_text buf; buf.clear (); buf.printf ("B<< %s >>\r", &mon->name); buf << describe_item (mon, 0); return buf; } /* This function returns the next monsters after 'tmp'. If no match is * found, it returns NULL (changed 0.94.3 to do this, since the * calling function (mon_info_msg) seems to expect that. */ static object * get_next_mon (object *tmp) { objectlink *mon; for (mon = first_mon_info; mon; mon = mon->next) if (mon->ob == tmp) break; /* didn't find a match */ if (!mon) return NULL; if (mon->next) return mon->next->ob; else return first_mon_info->ob; } /* mon_info_msg() - generate a message detailing the properties * of a randomly selected monster. */ static const char * mon_info_msg (int level) { static dynbuf_text buf; buf.clear (); /*preamble */ buf << "This beastiary contains:\n"; /* lets print info on as many monsters as will fit in our * document. * 8-96 Had to change this a bit, otherwise there would * have been an impossibly large number of combinations * of text! (and flood out the available number of titles * in the archive in a snap!) -b.t. */ object *tmp = get_random_mon (level * 3); while (tmp && buf.size () < BOOK_BUF) { /* monster description */ buf.printf ("\n%s\n", mon_desc (tmp)); /* Note that the value this returns is not based on level */ tmp = get_next_mon (tmp); } return buf; } /***************************************************************************** * Artifact msg generation code. ****************************************************************************/ /* artifact_msg() - generate a message detailing the properties * of 1-6 artifacts drawn sequentially from the artifact list. */ static const char * artifact_msg (int level) { artifactlist *al = NULL; artifact *art; int chance, i, type, index; int book_entries = level > 5 ? rndm (3) + rndm (3) + 2 : rndm (level) + 1; const char *ch; char name[MAX_BUF]; object *tmp = NULL; static dynbuf_text buf; buf.clear (); /* values greater than 5 create msg buffers that are too big! */ if (book_entries > 5) book_entries = 5; /* lets determine what kind of artifact type randomly. * Right now legal artifacts only come from those listed * in art_name_array. Also, we check to be sure an artifactlist * for that type exists! */ i = 0; do { index = rndm (sizeof (art_name_array) / sizeof (arttypename)); type = art_name_array[index].type; al = find_artifactlist (type); i++; } while ((al == NULL) && (i < 10)); if (i == 10) /* Unable to find a message */ return "None"; /* There is no reason to start on the artifact list at the begining. Lets * take our starting position randomly... */ art = al->items; for (i = rndm (level) + rndm (2) + 1; i > 0; i--) { if (!art) art = al->items; /* hmm, out of stuff, loop back around */ art = art->next; } /* the base 'generic' name for our artifact */ assign (name, art_name_array[index].name); /* Ok, lets print out the contents */ buf.printf ("Herein %s detailed %s...\n", book_entries > 1 ? "are" : "is", book_entries > 1 ? "some artifacts" : "an artifact"); /* artifact msg attributes loop. Lets keep adding entries to the 'book' * as long as we have space up to the allowed max # (book_entires) */ while (book_entries > 0 && buf.size () < BOOK_BUF) { if (!art) art = al->items; buf << '\n'; /* Name */ if (art->allowed && art->allowed->name != shstr_All) { linked_char *temp, *next = art->allowed; do { temp = next; next = next->next; } while (next && rndm (2)); buf.printf ("A B<< %s of %s >>", &temp->name, &art->item->name); } else /* default name is used */ buf.printf ("The B<< %s of %s >>", name, &art->item->name); buf << " is "; /* chance of finding */ chance = (int) (100 * ((float) art->chance / al->total_chance)); if (chance >= 20) buf << "an uncommon"; else if (chance >= 10) buf << "an unusual"; else if (chance >= 5) buf << "a rare"; else buf << "a very rare"; /* value of artifact */ buf << " item with a value that is " << art->item->value << " times normal.\n"; /* include the message about the artifact, if exists, and book * level is kinda high */ if (art->item->msg && rndm (4) + 1 < level) buf << art->item->msg; /* properties of the artifact */ tmp = object::create (); add_abilities (tmp, art->item); tmp->type = type; SET_FLAG (tmp, FLAG_IDENTIFIED); if ((ch = describe_item (tmp, 0)) && strlen (ch) > 1) buf << "\rProperties of this artifact include:\r" << ch << "\n"; tmp->destroy (); art = art->next; book_entries--; } return buf; } /***************************************************************************** * Spellpath message generation *****************************************************************************/ /* spellpath_msg() - generate a message detailing the member * incantations/prayers (and some of their properties) belonging to * a given spellpath. */ static char * spellpath_msg (int level) { static dynbuf_text buf; buf.clear (); static char retbuf[BOOK_BUF]; char tmpbuf[BOOK_BUF]; int path = rndm (NRSPELLPATHS), prayers = rndm (2); uint32 pnum = (path == -1) ? PATH_NULL : spellpathdef[path]; archetype *at; /* Preamble */ buf << "Herein are detailed the names of " << (prayers ? "prayers" : "incantations"); if (path == -1) buf << " having no known spell path.\n"; else buf << " belonging to the path of B<< " << spellpathnames[path] << " >>:\n\n"; int seen = 0; for_all_archetypes (at) /* Determine if this is an appropriate spell. Must * be of matching path, must be of appropriate type (prayer * or not), and must be within the valid level range. */ if (at->type == SPELL && at->path_attuned & pnum && ((at->stats.grace && prayers) || (at->stats.sp && !prayers)) && (at->level < (level * 8))) { seen = 1; buf << at->object::name << '\r'; } /* Geez, no spells were generated. */ if (!seen) if (rndm (4)) /* usually, lets make a recursive call... */ return spellpath_msg (level); else /* give up, cause knowing no spells exist for path is info too. */ buf << "- no known spells exist.\n"; return buf; } /* formula_msg() - generate a message detailing the properties * of a randomly selected alchemical formula. */ static void make_formula_book (object *book, int level) { char title[MAX_BUF]; recipelist *fl; recipe *formula = NULL; int chance; static dynbuf_text buf; buf.clear (); /* the higher the book level, the more complex (ie number of * ingredients) the formula can be. */ fl = get_formulalist (rndm (level) / 3 + 1); if (!fl) fl = get_formulalist (1); /* safety */ if (fl->total_chance == 0) { book->msg = "\n"; new_text_name (book, 4); add_author (book, 4); return; } /* get a random formula, weighted by its bookchance */ chance = rndm (fl->total_chance); for (formula = fl->items; formula; formula = formula->next) { chance -= formula->chance; if (chance <= 0) break; } if (!formula || formula->arch_names <= 0) { book->msg = "\n"; new_text_name (book, 4); add_author (book, 4); return; } /* looks like a formula was found. Base the amount * of information on the booklevel and the spellevel * of the formula. */ const char *op_name = formula->arch_name [rndm (formula->arch_names)]; archetype *at; /* preamble */ buf << "Herein is described a project using B<< " << (formula->skill ? &formula->skill : "an unknown skill") << " >>:\n\n"; if ((at = archetype::find (op_name))) op_name = at->object::name; else LOG (llevError, "formula_msg() can't find arch %s for formula.\n", op_name); /* item name */ if (formula->title != shstr_NONE) { buf.printf ("The B<< %s of %s >>", op_name, &formula->title); /* This results in things like pile of philo. sulfur. * while philo. sulfur may look better, without this, * you get things like 'the wise' because its missing the * water of section. */ sprintf (title, "%s: %s of %s", formula_book_name [rndm (sizeof (formula_book_name) / sizeof (char *))], op_name, &formula->title); } else { buf << "The B<< " << op_name; sprintf (title, "%s: %s", formula_book_name [rndm (sizeof (formula_book_name) / sizeof (char *))], op_name); if (at->title) { buf << " " << at->title; strcat (title, " "); strcat (title, at->title); } buf << " >>"; } /* Lets name the book something meaningful ! */ book->name = title; book->title = NULL; /* ingredients to make it */ if (formula->ingred) { linked_char *next; archetype *at; at = archetype::find (formula->cauldron); buf.printf (" may be made at %s using the following ingredients:\n\n", at ? query_name (at) : "an appropriate place"); for (next = formula->ingred; next; next = next->next) buf << next->name << '\r'; } else LOG (llevError, "formula_msg() no ingredient list for object %s of %s\n", op_name, &formula->title); book->msg = buf; } #define DESCRIBE_PATH(retbuf, variable, name) \ if(variable) { \ int i,j=0; \ strcat(retbuf,"(" name ": "); \ for(i=0; iname; /* preamble.. */ buf << "This document contains knowledge concerning the diety B<< " << name << " >>"; /* Always have as default information the god's descriptive terms. */ if (nstrtok (god->msg, ",") > 0) buf << ", known as" << strtoktolin (god->msg, ","); else buf << "..."; buf << "\n\n"; int introlen = buf.size (); /* so we will know if no new info is added later */ /* Information about the god is random, and based on the level of the * 'book'. Probably there is a more intellegent way to implement * this ... */ while (level > 0 && buf.size () < BOOK_BUF) { if (level == 2 && rndm (2)) { /* enemy god */ const char *enemy = god->title; if (enemy) buf.printf ("The gods %s and %s are enemies.\r", name, enemy); } if (level == 3 && rndm (2)) { /* enemy race, what the god's holy word effects */ const char *enemy = god->slaying; int i; if (enemy && !(god->path_denied & PATH_TURNING)) if ((i = nstrtok (enemy, ",")) > 0) { char tmpbuf[MAX_BUF]; buf << "The holy words of " << name << " have the power to slay creatures belonging to the "; if (i > 1) buf << "following races:" << strtoktolin (enemy, ","); else buf << "race of" << strtoktolin (enemy, ","); buf << '\r'; } } if (level == 4 && rndm (2)) { /* Priest of god gets these protect,vulnerable... */ if (const char *cp = describe_resistance (god, 1)) { /* This god does have protections */ buf << name << " has a potent aura which is extended to" " faithful priests. The effects of this aura include: " << cp << ".\r"; } } if (level == 5 && rndm (2)) { /* aligned race, summoning */ const char *race = god->race; /* aligned race */ int i; if (race && !(god->path_denied & PATH_SUMMON)) if ((i = nstrtok (race, ",")) > 0) { buf << "Creatures sacred to " << name << " include the "; if (i > 1) buf << "following races:" << strtoktolin (race, ","); else buf << "race of" << strtoktolin (race, ","); buf << '\r'; } } if (level == 6 && rndm (2)) { /* blessing,curse properties of the god */ if (const char *cp = describe_resistance (god, 1)) { /* This god does have protections */ buf << "The priests of " << name << " are known to be able to " "bestow a blessing which makes the recipient " << cp << '\r'; } } if (level == 8 && rndm (2)) { /* immunity, holy possession */ buf << "The priests of " << name << " are known to make cast a mighty" " prayer of possession"; int first = 1; for (int i = 0; i < NROFATTACKS; i++) if (god->resist[i] == 100) { if (first) { buf << " which gives the recipient"; first = 0; } else buf << ", "; buf << " immunity to " << attacktype_desc[i]; } buf << ".\r"; } if (level == 12 && rndm (2)) { /* spell paths */ //TODO: int has_effect = 0, tmpvar; char tmpbuf[MAX_BUF]; sprintf (tmpbuf, "\n"); sprintf (tmpbuf, "It is rarely known fact that the priests of %s\n", name); strcat (tmpbuf, "are mystically transformed. Effects of this include:\n"); if ((tmpvar = god->path_attuned)) { has_effect = 1; DESCRIBE_PATH (tmpbuf, tmpvar, "Attuned"); } if ((tmpvar = god->path_repelled)) { has_effect = 1; DESCRIBE_PATH (tmpbuf, tmpvar, "Repelled"); } if ((tmpvar = god->path_denied)) { has_effect = 1; DESCRIBE_PATH (tmpbuf, tmpvar, "Denied"); } if (has_effect) buf << tmpbuf << '\r'; else buf << '\r'; } level--; } if (buf.size () == introlen) /* we got no information beyond the preamble! */ buf << "[Unfortunately the rest of the information is hopelessly garbled!]"; return buf; } /* tailor_readable_ob()- The main routine. This chooses a random * message to put in given readable object (type==BOOK) which will * be referred hereafter as a 'book'. We use the book level to de- * termine the value of the information we will insert. Higher * values mean the book will (generally) have better/more info. * See individual cases as to how this will be utilized. * "Book" name/content length are based on the weight of the * document. If the value of msg_type is negative, we will randomly * choose the kind of message to generate. * -b.t. thomas@astro.psu.edu * * book is the object we are creating into. * If msg_type is a positive value, we use that to determine the * message type - otherwise a random value is used. * */ void tailor_readable_ob (object *book, int msg_type) { int level = book->level ? rndm (book->level) + 1 : 1; /* safety */ if (book->type != BOOK) return; if (level <= 0) return; /* if no level no point in doing any more... */ /* &&& The message switch &&& */ /* Below all of the possible types of messages in the "book"s. */ /* * IF you add a new type of book msg, you will have to do several things. * 1) make sure there is an entry in the msg switch below! * 2) make sure there is an entry in max_titles[] array. * 3) make sure there are entries for your case in new_text_title() * and add_authour(). * 4) you may want separate authour/book name arrays in read.h */ const char *new_msg = ""; msg_type = msg_type > 0 ? msg_type : rndm (8); switch (msg_type) { case 1: /* monster attrib */ new_msg = mon_info_msg (level); break; case 2: /* artifact attrib */ new_msg = artifact_msg (level); break; case 3: /* grouping incantations/prayers by path */ new_msg = spellpath_msg (level); break; case 4: /* describe an alchemy formula */ make_formula_book (book, level); /* make_formula_book already gives title */ return; case 5: /* bits of information about a god */ new_msg = god_info_msg (level); break; case 0: /* use info list in lib/ */ default: cfperl_make_book (book, level); /* already gives title */ return; } if (strlen (new_msg) > 1) { book->msg = new_msg; /* lets give the "book" a new name, which may be a compound word */ change_book (book, msg_type); } } /***************************************************************************** * * Writeback routine for updating the bookarchive. * ****************************************************************************/ /* write_book_archive() - write out the updated book archive */ void write_book_archive () { FILE *fp; int index = 0; char fname[MAX_BUF]; title *book = NULL; titlelist *bl = get_titlelist (0); /* If nothing changed, don't write anything */ if (!need_to_write_bookarchive) return; need_to_write_bookarchive = 0; sprintf (fname, "%s/bookarch", settings.localdir); LOG (llevDebug, "Updating book archive: %s...\n", fname); if ((fp = fopen (fname, "w")) == NULL) LOG (llevDebug, "Can't open book archive file %s\n", fname); else { while (bl) { for (book = bl->first_book; book; book = book->next) if (book && book->authour) { fprintf (fp, "title %s\n", &book->name); fprintf (fp, "authour %s\n", &book->authour); fprintf (fp, "arch %s\n", &book->archname); fprintf (fp, "level %d\n", book->level); fprintf (fp, "type %d\n", index); fprintf (fp, "size %d\n", book->size); fprintf (fp, "index %d\n", book->msg_index); fprintf (fp, "end\n"); } bl = bl->next; index++; } fclose (fp); chmod (fname, SAVE_MODE); } } readable_message_type * get_readable_message_type (object *readable) { uint8 subtype = readable->subtype; if (subtype > last_readable_subtype) return &readable_message_types[0]; return &readable_message_types[subtype]; }