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.2 by root, Tue Aug 29 08:01:35 2006 UTC vs.
Revision 1.3 by root, Sun Sep 3 00:18:40 2006 UTC

1/* 1/*
2 * static char *rcsid_readable_c = 2 * static char *rcsid_readable_c =
3 * "$Id: readable.C,v 1.2 2006/08/29 08:01:35 root Exp $"; 3 * "$Id: readable.C,v 1.3 2006/09/03 00:18:40 root Exp $";
4 */ 4 */
5 5
6/* 6/*
7 CrossFire, A Multiplayer game for X-windows 7 CrossFire, A Multiplayer game for X-windows
8 8
77/* Moved these structures from struct.h to this file in 0.94.3 - they 77/* Moved these structures from struct.h to this file in 0.94.3 - they
78 * are not needed anyplace else, so why have them globally declared? 78 * are not needed anyplace else, so why have them globally declared?
79 */ 79 */
80 80
81/* 'title' and 'titlelist' are used by the readable code */ 81/* 'title' and 'titlelist' are used by the readable code */
82typedef struct titlestruct { 82typedef struct titlestruct : zero_initialised
83{
83 const char *name; /* the name of the book */ 84 shstr name; /* the name of the book */
84 const char *authour; /* the name of the book authour */ 85 shstr authour; /* the name of the book authour */
85 const char *archname; /* the archetype name of the book */ 86 shstr archname; /* the archetype name of the book */
86 int level; /* level of difficulty of this message */ 87 int level; /* level of difficulty of this message */
87 int size; /* size of the book message */ 88 int size; /* size of the book message */
88 int msg_index; /* an index value derived from book message */ 89 int msg_index; /* an index value derived from book message */
89 struct titlestruct *next; 90 struct titlestruct *next;
90} title; 91} title;
91 92
92typedef struct titleliststruct { 93typedef struct titleliststruct : zero_initialised
94{
93 int number; /* number of items in the list */ 95 int number; /* number of items in the list */
94 struct titlestruct *first_book; /* pointer to first book in this list */ 96 struct titlestruct *first_book; /* pointer to first book in this list */
95 struct titleliststruct *next; /* pointer to next book list */ 97 struct titleliststruct *next; /* pointer to next book list */
96} titlelist; 98} titlelist;
97
98 99
99/* special structure, used only by art_name_array[] */ 100/* special structure, used only by art_name_array[] */
100 101
101typedef struct namebytype 102typedef struct namebytype
102 { 103 {
540 *****************************************************************************/ 541 *****************************************************************************/
541 542
542static titlelist * 543static titlelist *
543get_empty_booklist (void) 544get_empty_booklist (void)
544{ 545{
545 titlelist *bl = (titlelist *) malloc (sizeof (titlelist)); 546 titlelist *bl = new titlelist;
546 if (bl == NULL) 547
547 fatal (OUT_OF_MEMORY);
548 bl->number = 0; 548 bl->number = 0;
549 bl->first_book = NULL; 549 bl->first_book = NULL;
550 bl->next = NULL; 550 bl->next = NULL;
551 return bl; 551 return bl;
552} 552}
553 553
554static title * 554static title *
555get_empty_book (void) 555get_empty_book (void)
556{ 556{
557 title *t = (title *) malloc (sizeof (title)); 557 title *t = new title;
558 if (t == NULL) 558
559 fatal (OUT_OF_MEMORY);
560 t->name = NULL; 559 t->name = NULL;
561 t->archname = NULL; 560 t->archname = NULL;
562 t->authour = NULL; 561 t->authour = NULL;
563 t->level = 0; 562 t->level = 0;
564 t->size = 0; 563 t->size = 0;
701 if (strlen (msgbuf) > BOOK_BUF) 700 if (strlen (msgbuf) > BOOK_BUF)
702 { 701 {
703 LOG (llevDebug, "Warning: this string exceeded max book buf size:"); 702 LOG (llevDebug, "Warning: this string exceeded max book buf size:");
704 LOG (llevDebug, " %s", msgbuf); 703 LOG (llevDebug, " %s", msgbuf);
705 } 704 }
706 tmp->name = add_string (msgbuf); 705 tmp->name = msgbuf;
707 tmp->next = first_msg; 706 tmp->next = first_msg;
708 first_msg = tmp; 707 first_msg = tmp;
709 nrofmsg++; 708 nrofmsg++;
710 continue; 709 continue;
711 } 710 }
712 else if (!strncmp (cp, "MSG", 3)) 711 else if (!strncmp (cp, "MSG", 3))
713 { 712 {
714 tmp = (linked_char *) malloc (sizeof (linked_char)); 713 tmp = new linked_char;
715 strcpy (msgbuf, " "); /* reset msgbuf for new message */ 714 strcpy (msgbuf, " "); /* reset msgbuf for new message */
716 continue; 715 continue;
717 } 716 }
718 else if (!buf_overflow (msgbuf, cp, HUGE_BUF - 1)) 717 else if (!buf_overflow (msgbuf, cp, HUGE_BUF - 1))
719 { 718 {
771 while (*cp == ' ') /* Skip blanks */ 770 while (*cp == ' ') /* Skip blanks */
772 cp++; 771 cp++;
773 if (!strncmp (cp, "title", 4)) 772 if (!strncmp (cp, "title", 4))
774 { 773 {
775 book = get_empty_book (); /* init new book entry */ 774 book = get_empty_book (); /* init new book entry */
776 book->name = add_string (strchr (cp, ' ') + 1); 775 book->name = strchr (cp, ' ') + 1;
777 type = -1; 776 type = -1;
778 nroftitle++; 777 nroftitle++;
779 continue; 778 continue;
780 } 779 }
781 if (!strncmp (cp, "authour", 4)) 780 if (!strncmp (cp, "authour", 4))
782 { 781 {
783 book->authour = add_string (strchr (cp, ' ') + 1); 782 book->authour = strchr (cp, ' ') + 1;
784 } 783 }
785 if (!strncmp (cp, "arch", 4)) 784 if (!strncmp (cp, "arch", 4))
786 { 785 {
787 book->archname = add_string (strchr (cp, ' ') + 1); 786 book->archname = strchr (cp, ' ') + 1;
788 } 787 }
789 else if (sscanf (cp, "level %d", &value)) 788 else if (sscanf (cp, "level %d", &value))
790 { 789 {
791 book->level = (uint16) value; 790 book->level = (uint16) value;
792 } 791 }
972 nbr = sizeof (light_book_name) / sizeof (char *); 971 nbr = sizeof (light_book_name) / sizeof (char *);
973 strcpy (name, light_book_name[RANDOM () % nbr]); 972 strcpy (name, light_book_name[RANDOM () % nbr]);
974 } 973 }
975 break; 974 break;
976 } 975 }
977 free_string (book->name); 976
978 book->name = add_string (name); 977 book->name = name;
979} 978}
980 979
981/* add_book_author() 980/* add_book_author()
982 * A lot like new_text_name above, but instead chooses an author 981 * A lot like new_text_name above, but instead chooses an author
983 * and sets op->title to that value 982 * and sets op->title to that value
1018 default: 1017 default:
1019 strcpy (name, book_author[RANDOM () % nbr]); 1018 strcpy (name, book_author[RANDOM () % nbr]);
1020 } 1019 }
1021 1020
1022 sprintf (title, "of %s", name); 1021 sprintf (title, "of %s", name);
1023 op->title = add_string (title); 1022 op->title = title;
1024} 1023}
1025 1024
1026/* unique_book() - check to see if the book title/msg is unique. We 1025/* unique_book() - check to see if the book title/msg is unique. We
1027 * go through the entire list of possibilities each time. If we find 1026 * go through the entire list of possibilities each time. If we find
1028 * a match, then unique_book returns true (because inst unique). 1027 * a match, then unique_book returns true (because inst unique).
1059 LOG (llevError, "add_book_to_list can't get booklist!\n"); 1058 LOG (llevError, "add_book_to_list can't get booklist!\n");
1060 return; 1059 return;
1061 } 1060 }
1062 1061
1063 t = get_empty_book (); 1062 t = get_empty_book ();
1064 t->name = add_string (book->name); 1063 t->name = book->name;
1065 t->authour = add_string (book->title); 1064 t->authour = book->title;
1066 t->size = strlen (book->msg); 1065 t->size = strlen (book->msg);
1067 t->msg_index = strtoint (book->msg); 1066 t->msg_index = strtoint (book->msg);
1068 t->archname = add_string (book->arch->name); 1067 t->archname = book->arch->name;
1069 t->level = book->level; 1068 t->level = book->level;
1070 1069
1071 t->next = tl->first_book; 1070 t->next = tl->first_book;
1072 tl->first_book = t; 1071 tl->first_book = t;
1073 tl->number++; 1072 tl->number++;
1116 object *tmpbook; 1115 object *tmpbook;
1117 1116
1118 /* alter book properties */ 1117 /* alter book properties */
1119 if ((tmpbook = get_archetype (t->archname)) != NULL) 1118 if ((tmpbook = get_archetype (t->archname)) != NULL)
1120 { 1119 {
1121 if (tmpbook->msg)
1122 free_string (book->msg);
1123 tmpbook->msg = add_string (book->msg); 1120 tmpbook->msg = book->msg;
1124 copy_object (tmpbook, book); 1121 copy_object (tmpbook, book);
1125 free_object (tmpbook); 1122 free_object (tmpbook);
1126 } 1123 }
1127 1124
1128 book->title = add_string (t->authour); 1125 book->title = t->authour;
1129 free_string (book->name);
1130 book->name = add_string (t->name); 1126 book->name = t->name;
1131 book->level = t->level; 1127 book->level = t->level;
1132 } 1128 }
1133 /* Don't have any default title, so lets make up a new one */ 1129 /* Don't have any default title, so lets make up a new one */
1134 else 1130 else
1135 { 1131 {
1136 int numb, maxnames = max_titles[msgtype]; 1132 int numb, maxnames = max_titles[msgtype];
1137 const char *old_title; 1133 const char *old_title;
1138 const char *old_name; 1134 const char *old_name;
1139 1135
1140 old_title = book->title ? add_string(book->title) : NULL; 1136 old_title = book->title;
1141 old_name = add_string(book->name); 1137 old_name = book->name;
1142 1138
1143 /* some pre-generated books have title already set (from 1139 /* some pre-generated books have title already set (from
1144 * maps), also don't bother looking for unique title if 1140 * maps), also don't bother looking for unique title if
1145 * we already used up all the available names! */ 1141 * we already used up all the available names! */
1146 1142
1156 { 1152 {
1157#ifdef ARCHIVE_DEBUG 1153#ifdef ARCHIVE_DEBUG
1158 LOG (llevDebug, "titles for list %d full (%d possible).\n", 1154 LOG (llevDebug, "titles for list %d full (%d possible).\n",
1159 msgtype, maxnames); 1155 msgtype, maxnames);
1160#endif 1156#endif
1161 if (old_title != NULL)
1162 free_string(old_title);
1163 free_string(old_name);
1164 break; 1157 break;
1165 } 1158 }
1166 /* shouldnt change map-maker books */ 1159 /* shouldnt change map-maker books */
1167 else if (!book->title) 1160 else if (!book->title)
1168 do 1161 do
1188#ifdef ARCHIVE_DEBUG 1181#ifdef ARCHIVE_DEBUG
1189 LOG (llevDebug, "Failed to obtain unique title for %s %s (names:%d/%d)\n", 1182 LOG (llevDebug, "Failed to obtain unique title for %s %s (names:%d/%d)\n",
1190 book->name, book->title, numb, maxnames); 1183 book->name, book->title, numb, maxnames);
1191#endif 1184#endif
1192 /* restore old book properties here */ 1185 /* restore old book properties here */
1193 free_string (book->name);
1194 free_string (book->title);
1195 if (old_title!=NULL)
1196 book->title = add_string (old_title); 1186 book->title = old_title;
1197 1187
1198 if (RANDOM () % 4) { 1188 if (RANDOM () % 4) {
1199 /* Lets give the book a description to individualize it some */ 1189 /* Lets give the book a description to individualize it some */
1200 char new_name[MAX_BUF]; 1190 char new_name[MAX_BUF];
1201 snprintf (new_name, MAX_BUF, "%s %s", book_descrpt[RANDOM () % nbr], old_name); 1191 snprintf (new_name, MAX_BUF, "%s %s", book_descrpt[RANDOM () % nbr], old_name);
1202 1192
1203 book->name = add_string (new_name); 1193 book->name = new_name;
1204 } else { 1194 } else {
1205 book->name = add_string (old_name); 1195 book->name = old_name;
1206 } 1196 }
1207 } 1197 }
1208 else if (book->title && strlen (book->msg) > 5) { /* archive if long msg texts */ 1198 else if (book->title && strlen (book->msg) > 5) { /* archive if long msg texts */
1209 add_book_to_list (book, msgtype); 1199 add_book_to_list (book, msgtype);
1210 } 1200 }
1211
1212 if (old_title != NULL)
1213 free_string(old_title);
1214 free_string(old_name);
1215
1216 } 1201 }
1217 break; 1202 break;
1218 } 1203 }
1219 1204
1220 default: 1205 default:
1313char * 1298char *
1314mon_desc (const object *mon) 1299mon_desc (const object *mon)
1315{ 1300{
1316 static char retbuf[HUGE_BUF]; 1301 static char retbuf[HUGE_BUF];
1317 1302
1318 sprintf (retbuf, " *** %s ***\n", mon->name); 1303 sprintf (retbuf, " *** %s ***\n", &mon->name);
1319 strcat (retbuf, describe_item(mon, NULL)); 1304 strcat (retbuf, describe_item(mon, NULL));
1320 1305
1321 return retbuf; 1306 return retbuf;
1322} 1307}
1323 1308
1467 do 1452 do
1468 { 1453 {
1469 temp = next; 1454 temp = next;
1470 next = next->next; 1455 next = next->next;
1471 } 1456 }
1472 while ((next != (linked_char *) NULL) && !RANDOM () % 2); 1457 while (next && !RANDOM () % 2);
1473 sprintf (buf, "%s A %s of %s", buf, temp->name, art->item->name); 1458 sprintf (buf, "%s A %s of %s", buf, &temp->name, &art->item->name);
1474 } 1459 }
1475 else /* default name is used */ 1460 else /* default name is used */
1476 sprintf (buf, "%s The %s of %s", buf, name, art->item->name); 1461 sprintf (buf, "%s The %s of %s", buf, name, &art->item->name);
1477 1462
1478 /* chance of finding */ 1463 /* chance of finding */
1479 chance = (int) (100 * ((float) art->chance / al->total_chance)); 1464 chance = (int) (100 * ((float) art->chance / al->total_chance));
1480 if (chance >= 20) 1465 if (chance >= 20)
1481 sprintf (sbuf, "an uncommon"); 1466 sprintf (sbuf, "an uncommon");
1605 if (!fl) 1590 if (!fl)
1606 fl = get_formulalist (1); /* safety */ 1591 fl = get_formulalist (1); /* safety */
1607 1592
1608 if (fl->total_chance == 0) 1593 if (fl->total_chance == 0)
1609 { 1594 {
1610 book->msg = add_string(" <indecipherable text>\n"); 1595 book->msg = "<indecipherable text>\n";
1611 new_text_name(book, 4); 1596 new_text_name(book, 4);
1612 add_author(book,4); 1597 add_author(book,4);
1613 return; 1598 return;
1614 } 1599 }
1615 1600
1620 if (chance <= 0) 1605 if (chance <= 0)
1621 break; 1606 break;
1622 } 1607 }
1623 1608
1624 if (!formula || formula->arch_names <= 0) { 1609 if (!formula || formula->arch_names <= 0) {
1625 book->msg = add_string(" <indecipherable text>\n"); 1610 book->msg = "<indecipherable text>\n";
1626 new_text_name(book, 4); 1611 new_text_name(book, 4);
1627 add_author(book,4); 1612 add_author(book,4);
1628 1613
1629 } else { 1614 } else {
1630 /* looks like a formula was found. Base the amount 1615 /* looks like a formula was found. Base the amount
1669 strcat(title, " "); 1654 strcat(title, " ");
1670 strcat(title, at->clone.title); 1655 strcat(title, at->clone.title);
1671 } 1656 }
1672 } 1657 }
1673 /* Lets name the book something meaningful ! */ 1658 /* Lets name the book something meaningful ! */
1674 if (book->name) free_string(book->name);
1675 book->name = add_string(title); 1659 book->name = title;
1676 if (book->title) {
1677 free_string(book->title);
1678 book->title = NULL; 1660 book->title = NULL;
1679 }
1680 1661
1681 /* ingredients to make it */ 1662 /* ingredients to make it */
1682 if (formula->ingred != NULL) 1663 if (formula->ingred != NULL)
1683 { 1664 {
1684 linked_char *next; 1665 linked_char *next;
1698 } 1679 }
1699 else 1680 else
1700 LOG (llevError, "formula_msg() no ingredient list for object %s of %s\n", 1681 LOG (llevError, "formula_msg() no ingredient list for object %s of %s\n",
1701 op_name, formula->title); 1682 op_name, formula->title);
1702 if (retbuf[strlen(retbuf)-1]!= '\n') strcat(retbuf, "\n"); 1683 if (retbuf[strlen(retbuf)-1]!= '\n') strcat(retbuf, "\n");
1703 if (book->msg) free_string(book->msg); 1684
1704 book->msg = add_string(retbuf); 1685 book->msg = retbuf;
1705 } 1686 }
1706} 1687}
1707 1688
1708 1689
1709/* msgfile_msg() - generate a message drawn randomly from a 1690/* msgfile_msg() - generate a message drawn randomly from a
2008 } 1989 }
2009 1990
2010 strcat (msgbuf, "\n"); /* safety -- we get ugly map saves/crashes w/o this */ 1991 strcat (msgbuf, "\n"); /* safety -- we get ugly map saves/crashes w/o this */
2011 if (strlen (msgbuf) > 1) 1992 if (strlen (msgbuf) > 1)
2012 { 1993 {
2013 if (book->msg)
2014 free_string (book->msg);
2015 book->msg = add_string (msgbuf); 1994 book->msg = msgbuf;
2016 /* lets give the "book" a new name, which may be a compound word */ 1995 /* lets give the "book" a new name, which may be a compound word */
2017 change_book (book, msg_type); 1996 change_book (book, msg_type);
2018 } 1997 }
2019 1998
2020} 1999}
2037 LOG (llevDebug, "freeing all book information\n"); 2016 LOG (llevDebug, "freeing all book information\n");
2038 2017
2039 for (tlist = booklist; tlist != NULL; tlist = tnext) 2018 for (tlist = booklist; tlist != NULL; tlist = tnext)
2040 { 2019 {
2041 tnext = tlist->next; 2020 tnext = tlist->next;
2021
2042 for (title1 = tlist->first_book; title1; title1 = titlenext) 2022 for (title1 = tlist->first_book; title1; title1 = titlenext)
2043 { 2023 {
2044 titlenext = title1->next; 2024 titlenext = title1->next;
2045 if (title1->name)
2046 free_string (title1->name);
2047 if (title1->authour)
2048 free_string (title1->authour);
2049 if (title1->archname)
2050 free_string (title1->archname);
2051 free (title1); 2025 delete title1;
2052 } 2026 }
2027
2053 free (tlist); 2028 delete tlist;
2054 } 2029 }
2055 for (lmsg = first_msg; lmsg; lmsg = nextmsg) 2030 for (lmsg = first_msg; lmsg; lmsg = nextmsg)
2056 { 2031 {
2057 nextmsg = lmsg->next; 2032 nextmsg = lmsg->next;
2058 if (lmsg->name)
2059 free_string (lmsg->name);
2060 free (lmsg); 2033 delete lmsg;
2061 } 2034 }
2062 for (monlink = first_mon_info; monlink; monlink = nextmon) 2035 for (monlink = first_mon_info; monlink; monlink = nextmon)
2063 { 2036 {
2064 nextmon = monlink->next; 2037 nextmon = monlink->next;
2065 free (monlink); 2038 free (monlink);
2100 while (bl) 2073 while (bl)
2101 { 2074 {
2102 for (book = bl->first_book; book; book = book->next) 2075 for (book = bl->first_book; book; book = book->next)
2103 if (book && book->authour) 2076 if (book && book->authour)
2104 { 2077 {
2105 fprintf (fp, "title %s\n", book->name); 2078 fprintf (fp, "title %s\n", &book->name);
2106 fprintf (fp, "authour %s\n", book->authour); 2079 fprintf (fp, "authour %s\n", &book->authour);
2107 fprintf (fp, "arch %s\n", book->archname); 2080 fprintf (fp, "arch %s\n", &book->archname);
2108 fprintf (fp, "level %d\n", book->level); 2081 fprintf (fp, "level %d\n", book->level);
2109 fprintf (fp, "type %d\n", index); 2082 fprintf (fp, "type %d\n", index);
2110 fprintf (fp, "size %d\n", book->size); 2083 fprintf (fp, "size %d\n", book->size);
2111 fprintf (fp, "index %d\n", book->msg_index); 2084 fprintf (fp, "index %d\n", book->msg_index);
2112 fprintf (fp, "end\n"); 2085 fprintf (fp, "end\n");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines