ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/readable.C
(Generate patch)

Comparing deliantra/server/common/readable.C (file contents):
Revision 1.47 by root, Thu Oct 15 23:24:38 2009 UTC vs.
Revision 1.54 by root, Sun Nov 29 10:55:18 2009 UTC

108/* these are needed for creation of a linked list of 108/* these are needed for creation of a linked list of
109 * pointers to all (hostile) monster objects */ 109 * pointers to all (hostile) monster objects */
110 110
111static int nrofmon = 0, need_to_write_bookarchive = 0; 111static int nrofmon = 0, need_to_write_bookarchive = 0;
112 112
113
114/* this is needed to keep track of status of initialisation
115 * of the message file */
116static int nrofmsg = 0;
117
118/* first_msg is the started of the linked list of messages as read from
119 * the messages file
120 */
121static linked_char *first_msg = NULL;
122
123/* 113/*
124 * Spellpath information 114 * Spellpath information
125 */ 115 */
126
127static uint32 spellpathdef[NRSPELLPATHS] = { 116static uint32 spellpathdef[NRSPELLPATHS] = {
128 PATH_PROT, 117 PATH_PROT,
129 PATH_FIRE, 118 PATH_FIRE,
130 PATH_FROST, 119 PATH_FROST,
131 PATH_ELEC, 120 PATH_ELEC,
497 /*subtype 50 */ {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_GRAVESTONE_3, "readable-monument-gravestone-3"}, 486 /*subtype 50 */ {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_GRAVESTONE_3, "readable-monument-gravestone-3"},
498 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_1, "readable-monument-wall-1"}, 487 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_1, "readable-monument-wall-1"},
499 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_2, "readable-monument-wall-2"}, 488 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_2, "readable-monument-wall-2"},
500 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_3, "readable-monument-wall-3"} 489 {MSG_TYPE_MONUMENT, MSG_TYPE_MONUMENT_WALL_3, "readable-monument-wall-3"}
501}; 490};
502int last_readable_subtype = sizeof (readable_message_types) / sizeof (readable_message_type); 491static int last_readable_subtype = sizeof (readable_message_types) / sizeof (readable_message_type);
503 492
504static int max_titles[6] = { 493static int max_titles[6] = {
505 ((sizeof (light_book_name) / sizeof (char *)) + (sizeof (heavy_book_name) / sizeof (char *))) * (sizeof (book_author) / sizeof (char *)), 494 ((sizeof (light_book_name) / sizeof (char *)) + (sizeof (heavy_book_name) / sizeof (char *))) * (sizeof (book_author) / sizeof (char *)),
506 (sizeof (mon_book_name) / sizeof (char *)) * (sizeof (mon_author) / sizeof (char *)), 495 (sizeof (mon_book_name) / sizeof (char *)) * (sizeof (mon_author) / sizeof (char *)),
507 (sizeof (art_book_name) / sizeof (char *)) * (sizeof (art_author) / sizeof (char *)), 496 (sizeof (art_book_name) / sizeof (char *)) * (sizeof (art_author) / sizeof (char *)),
515 * Start of misc. readable functions used by others functions in this file 504 * Start of misc. readable functions used by others functions in this file
516 * 505 *
517 *****************************************************************************/ 506 *****************************************************************************/
518 507
519static titlelist * 508static titlelist *
520get_empty_booklist (void) 509get_empty_booklist ()
521{ 510{
522 titlelist *bl = new titlelist; 511 titlelist *bl = new titlelist;
523 512
524 bl->number = 0; 513 bl->number = 0;
525 bl->first_book = NULL; 514 bl->first_book = NULL;
526 bl->next = NULL; 515 bl->next = NULL;
527 return bl; 516 return bl;
528} 517}
529 518
530static title * 519static title *
531get_empty_book (void) 520get_empty_book ()
532{ 521{
533 title *t = new title; 522 title *t = new title;
534 523
535 t->name = NULL; 524 t->name = NULL;
536 t->archname = NULL; 525 t->archname = NULL;
619 tbuf = strtok (NULL, sbuf); 608 tbuf = strtok (NULL, sbuf);
620 } 609 }
621 return (char *) rbuf; 610 return (char *) rbuf;
622} 611}
623 612
624static int
625book_overflow (const char *buf1, const char *buf2, int booksize)
626{
627 if (buf_overflow (buf1, buf2, BOOK_BUF - 2) /* 2 less so always room for trailing \n */
628 || buf_overflow (buf1, buf2, booksize))
629 return 1;
630
631 return 0;
632}
633
634/***************************************************************************** 613/*****************************************************************************
635 * 614 *
636 * Start of initialisation related functions. 615 * Start of initialisation related functions.
637 * 616 *
638 ****************************************************************************/ 617 ****************************************************************************/
640/* init_book_archive() - if not called before, initialise the info list 619/* init_book_archive() - if not called before, initialise the info list
641 * This reads in the bookarch file into memory. bookarch is the file 620 * This reads in the bookarch file into memory. bookarch is the file
642 * created and updated across multiple runs of the program. 621 * created and updated across multiple runs of the program.
643 */ 622 */
644static void 623static void
645init_book_archive (void) 624init_book_archive ()
646{ 625{
647 FILE *fp; 626 FILE *fp;
648 int comp, nroftitle = 0; 627 int comp, nroftitle = 0;
649 char buf[MAX_BUF], fname[MAX_BUF], *cp; 628 char buf[MAX_BUF], fname[MAX_BUF], *cp;
650 title *book = NULL; 629 title *book = NULL;
731} 710}
732 711
733/* init_mon_info() - creates the linked list of pointers to 712/* init_mon_info() - creates the linked list of pointers to
734 * monster archetype objects if not called previously 713 * monster archetype objects if not called previously
735 */ 714 */
736
737static void 715static void
738init_mon_info (void) 716init_mon_info ()
739{ 717{
740 archetype *at; 718 archetype *at;
741 static int did_init_mon_info = 0; 719 static int did_init_mon_info = 0;
742 720
743 if (did_init_mon_info) 721 if (did_init_mon_info)
744 return; 722 return;
745 723
746 did_init_mon_info = 1; 724 did_init_mon_info = 1;
747 725
748 for_all_archetypes (at) 726 for_all_archetypes (at)
749 { 727 if (at->flag [FLAG_MONSTER]
750 if (QUERY_FLAG (at, FLAG_MONSTER) && (!QUERY_FLAG (at, FLAG_CHANGING) || QUERY_FLAG (at, FLAG_UNAGGRESSIVE))) 728 && at->is_head ()
729 && (!at->flag [FLAG_CHANGING] || at->flag [FLAG_UNAGGRESSIVE]))
751 { 730 {
752 objectlink *mon = new objectlink; 731 objectlink *mon = new objectlink;
753 732
754 mon->ob = at; 733 mon->ob = at;
755 mon->next = first_mon_info; 734 mon->next = first_mon_info;
756 first_mon_info = mon; 735 first_mon_info = mon;
757 nrofmon++; 736 nrofmon++;
758 } 737 }
759 }
760 738
761 LOG (llevDebug, "init_mon_info() got %d monsters\n", nrofmon); 739 LOG (llevDebug, "init_mon_info() got %d monsters\n", nrofmon);
762} 740}
763 741
764/* init_readable() - initialise linked lists utilized by 742/* init_readable() - initialise linked lists utilized by
766 * 744 *
767 * This is the function called by the main routine to initialise 745 * This is the function called by the main routine to initialise
768 * all the readable information. 746 * all the readable information.
769 */ 747 */
770void 748void
771init_readable (void) 749init_readable ()
772{ 750{
773 static int did_this; 751 static int did_this;
774 752
775 if (did_this) 753 if (did_this)
776 return; 754 return;
1215 break; 1193 break;
1216 1194
1217 /* didn't find a match */ 1195 /* didn't find a match */
1218 if (!mon) 1196 if (!mon)
1219 return NULL; 1197 return NULL;
1198
1220 if (mon->next) 1199 if (mon->next)
1221 return mon->next->ob; 1200 return mon->next->ob;
1222 else 1201 else
1223 return first_mon_info->ob; 1202 return first_mon_info->ob;
1224 1203
1226 1205
1227/* mon_info_msg() - generate a message detailing the properties 1206/* mon_info_msg() - generate a message detailing the properties
1228 * of a randomly selected monster. 1207 * of a randomly selected monster.
1229 */ 1208 */
1230static const char * 1209static const char *
1231mon_info_msg (int level, int booksize) 1210mon_info_msg (int level)
1232{ 1211{
1233 static dynbuf_text buf; buf.clear (); 1212 static dynbuf_text buf; buf.clear ();
1234 1213
1235 /*preamble */ 1214 /*preamble */
1236 buf << "This beastiary contains:\n"; 1215 buf << "This beastiary contains:\n";
1262 1241
1263/* artifact_msg() - generate a message detailing the properties 1242/* artifact_msg() - generate a message detailing the properties
1264 * of 1-6 artifacts drawn sequentially from the artifact list. 1243 * of 1-6 artifacts drawn sequentially from the artifact list.
1265 */ 1244 */
1266static const char * 1245static const char *
1267artifact_msg (int level, int booksize) 1246artifact_msg (int level)
1268{ 1247{
1269 artifactlist *al = NULL; 1248 artifactlist *al = NULL;
1270 artifact *art; 1249 artifact *art;
1271 int chance, i, type, index; 1250 int chance, i, type, index;
1272 int book_entries = level > 5 ? rndm (3) + rndm (3) + 2 : rndm (level) + 1; 1251 int book_entries = level > 5 ? rndm (3) + rndm (3) + 2 : rndm (level) + 1;
1391/* spellpath_msg() - generate a message detailing the member 1370/* spellpath_msg() - generate a message detailing the member
1392 * incantations/prayers (and some of their properties) belonging to 1371 * incantations/prayers (and some of their properties) belonging to
1393 * a given spellpath. 1372 * a given spellpath.
1394 */ 1373 */
1395static char * 1374static char *
1396spellpath_msg (int level, int booksize) 1375spellpath_msg (int level)
1397{ 1376{
1398 static dynbuf_text buf; buf.clear (); 1377 static dynbuf_text buf; buf.clear ();
1399 1378
1400 static char retbuf[BOOK_BUF]; 1379 static char retbuf[BOOK_BUF];
1401 char tmpbuf[BOOK_BUF]; 1380 char tmpbuf[BOOK_BUF];
1427 } 1406 }
1428 1407
1429 /* Geez, no spells were generated. */ 1408 /* Geez, no spells were generated. */
1430 if (!seen) 1409 if (!seen)
1431 if (rndm (4)) /* usually, lets make a recursive call... */ 1410 if (rndm (4)) /* usually, lets make a recursive call... */
1432 return spellpath_msg (level, booksize); 1411 return spellpath_msg (level);
1433 else /* give up, cause knowing no spells exist for path is info too. */ 1412 else /* give up, cause knowing no spells exist for path is info too. */
1434 buf << "- no known spells exist.\n"; 1413 buf << "- no known spells exist.\n";
1435 1414
1436 return buf; 1415 return buf;
1437} 1416}
1568 1547
1569/* god_info_msg() - generate a message detailing the properties 1548/* god_info_msg() - generate a message detailing the properties
1570 * of a random god. Used by the book hack. b.t. 1549 * of a random god. Used by the book hack. b.t.
1571 */ 1550 */
1572static const char * 1551static const char *
1573god_info_msg (int level, int booksize) 1552god_info_msg (int level)
1574{ 1553{
1575 const char *name = NULL; 1554 const char *name = NULL;
1576 object *god = pntr_to_god_obj (get_rand_god ()); 1555 object *god = pntr_to_god_obj (get_rand_god ());
1577 1556
1578 static dynbuf_text buf; buf.clear (); 1557 static dynbuf_text buf; buf.clear ();
1760 * 1739 *
1761 */ 1740 */
1762void 1741void
1763tailor_readable_ob (object *book, int msg_type) 1742tailor_readable_ob (object *book, int msg_type)
1764{ 1743{
1765 char msgbuf[BOOK_BUF];
1766 int level = book->level ? rndm (book->level) + 1 : 1; 1744 int level = book->level ? rndm (book->level) + 1 : 1;
1767 int book_buf_size;
1768 1745
1769 /* safety */ 1746 /* safety */
1770 if (book->type != BOOK) 1747 if (book->type != BOOK)
1771 return; 1748 return;
1772 1749
1773 if (level <= 0) 1750 if (level <= 0)
1774 return; /* if no level no point in doing any more... */ 1751 return; /* if no level no point in doing any more... */
1775
1776 /* Max text length this book can have. */
1777 book_buf_size = BOOKSIZE (book);
1778 1752
1779 /* &&& The message switch &&& */ 1753 /* &&& The message switch &&& */
1780 /* Below all of the possible types of messages in the "book"s. 1754 /* Below all of the possible types of messages in the "book"s.
1781 */ 1755 */
1782 /* 1756 /*
1785 * 2) make sure there is an entry in max_titles[] array. 1759 * 2) make sure there is an entry in max_titles[] array.
1786 * 3) make sure there are entries for your case in new_text_title() 1760 * 3) make sure there are entries for your case in new_text_title()
1787 * and add_authour(). 1761 * and add_authour().
1788 * 4) you may want separate authour/book name arrays in read.h 1762 * 4) you may want separate authour/book name arrays in read.h
1789 */ 1763 */
1764 const char *new_msg = "";
1790 msg_type = msg_type > 0 ? msg_type : rndm (8); 1765 msg_type = msg_type > 0 ? msg_type : rndm (8);
1791 switch (msg_type) 1766 switch (msg_type)
1792 { 1767 {
1793 case 1: /* monster attrib */ 1768 case 1: /* monster attrib */
1794 strcpy (msgbuf, mon_info_msg (level, book_buf_size)); 1769 new_msg = mon_info_msg (level);
1795 break; 1770 break;
1796 case 2: /* artifact attrib */ 1771 case 2: /* artifact attrib */
1797 strcpy (msgbuf, artifact_msg (level, book_buf_size)); 1772 new_msg = artifact_msg (level);
1798 break; 1773 break;
1799 case 3: /* grouping incantations/prayers by path */ 1774 case 3: /* grouping incantations/prayers by path */
1800 strcpy (msgbuf, spellpath_msg (level, book_buf_size)); 1775 new_msg = spellpath_msg (level);
1801 break; 1776 break;
1802 case 4: /* describe an alchemy formula */ 1777 case 4: /* describe an alchemy formula */
1803 make_formula_book (book, level); 1778 make_formula_book (book, level);
1804 /* make_formula_book already gives title */ 1779 /* make_formula_book already gives title */
1805 return; 1780 return;
1806 case 5: /* bits of information about a god */ 1781 case 5: /* bits of information about a god */
1807 strcpy (msgbuf, god_info_msg (level, book_buf_size)); 1782 new_msg = god_info_msg (level);
1808 break; 1783 break;
1809 case 0: /* use info list in lib/ */ 1784 case 0: /* use info list in lib/ */
1810 default: 1785 default:
1811 cfperl_make_book (book, level); 1786 cfperl_make_book (book, level);
1812 /* already gives title */ 1787 /* already gives title */
1813 return; 1788 return;
1814 } 1789 }
1815 1790
1816 strcat (msgbuf, "\n"); /* safety -- we get ugly map saves/crashes w/o this */
1817
1818 if (strlen (msgbuf) > 1) 1791 if (strlen (new_msg) > 1)
1819 { 1792 {
1820 book->msg = msgbuf; 1793 book->msg = new_msg;
1821 /* lets give the "book" a new name, which may be a compound word */ 1794 /* lets give the "book" a new name, which may be a compound word */
1822 change_book (book, msg_type); 1795 change_book (book, msg_type);
1823 } 1796 }
1824} 1797}
1825
1826
1827/*****************************************************************************
1828 *
1829 * Cleanup routine for readble stuff.
1830 *
1831 *****************************************************************************/
1832
1833void
1834free_all_readable (void)
1835{
1836 titlelist *tlist, *tnext;
1837 title *title1, *titlenext;
1838 linked_char *lmsg, *nextmsg;
1839 objectlink *monlink, *nextmon;
1840
1841 LOG (llevDebug, "freeing all book information\n");
1842
1843 for (tlist = booklist; tlist; tlist = tnext)
1844 {
1845 tnext = tlist->next;
1846
1847 for (title1 = tlist->first_book; title1; title1 = titlenext)
1848 {
1849 titlenext = title1->next;
1850 delete title1;
1851 }
1852
1853 delete tlist;
1854 }
1855
1856 for (lmsg = first_msg; lmsg; lmsg = nextmsg)
1857 {
1858 nextmsg = lmsg->next;
1859 delete lmsg;
1860 }
1861
1862 for (monlink = first_mon_info; monlink; monlink = nextmon)
1863 {
1864 nextmon = monlink->next;
1865 delete monlink;
1866 }
1867}
1868
1869 1798
1870/***************************************************************************** 1799/*****************************************************************************
1871 * 1800 *
1872 * Writeback routine for updating the bookarchive. 1801 * Writeback routine for updating the bookarchive.
1873 * 1802 *
1874 ****************************************************************************/ 1803 ****************************************************************************/
1875
1876/* write_book_archive() - write out the updated book archive */ 1804/* write_book_archive() - write out the updated book archive */
1877
1878void 1805void
1879write_book_archive (void) 1806write_book_archive ()
1880{ 1807{
1881 FILE *fp; 1808 FILE *fp;
1882 int index = 0; 1809 int index = 0;
1883 char fname[MAX_BUF]; 1810 char fname[MAX_BUF];
1884 title *book = NULL; 1811 title *book = NULL;
1885 titlelist *bl = get_titlelist (0); 1812 titlelist *bl = get_titlelist (0);
1886 1813
1887 /* If nothing changed, don't write anything */ 1814 /* If nothing changed, don't write anything */
1888 if (!need_to_write_bookarchive) 1815 if (!need_to_write_bookarchive)
1889 return; 1816 return;
1817
1890 need_to_write_bookarchive = 0; 1818 need_to_write_bookarchive = 0;
1891 1819
1892 sprintf (fname, "%s/bookarch", settings.localdir); 1820 sprintf (fname, "%s/bookarch", settings.localdir);
1893 LOG (llevDebug, "Updating book archive: %s...\n", fname); 1821 LOG (llevDebug, "Updating book archive: %s...\n", fname);
1894 1822
1895 if ((fp = fopen (fname, "w")) == NULL) 1823 if ((fp = fopen (fname, "w")) == NULL)
1896 {
1897 LOG (llevDebug, "Can't open book archive file %s\n", fname); 1824 LOG (llevDebug, "Can't open book archive file %s\n", fname);
1898 }
1899 else 1825 else
1900 { 1826 {
1901 while (bl) 1827 while (bl)
1902 { 1828 {
1903 for (book = bl->first_book; book; book = book->next) 1829 for (book = bl->first_book; book; book = book->next)
1910 fprintf (fp, "type %d\n", index); 1836 fprintf (fp, "type %d\n", index);
1911 fprintf (fp, "size %d\n", book->size); 1837 fprintf (fp, "size %d\n", book->size);
1912 fprintf (fp, "index %d\n", book->msg_index); 1838 fprintf (fp, "index %d\n", book->msg_index);
1913 fprintf (fp, "end\n"); 1839 fprintf (fp, "end\n");
1914 } 1840 }
1841
1915 bl = bl->next; 1842 bl = bl->next;
1916 index++; 1843 index++;
1917 } 1844 }
1845
1918 fclose (fp); 1846 fclose (fp);
1919 chmod (fname, SAVE_MODE); 1847 chmod (fname, SAVE_MODE);
1920 } 1848 }
1921} 1849}
1850
1922readable_message_type * 1851readable_message_type *
1923get_readable_message_type (object *readable) 1852get_readable_message_type (object *readable)
1924{ 1853{
1925 uint8 subtype = readable->subtype; 1854 uint8 subtype = readable->subtype;
1926 1855

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines