1 /*
2  * PROPRIETARY INFORMATION.  This software is proprietary to POWDER
3  * Development, and is not to be reproduced, transmitted, or disclosed
4  * in any way without written permission.
5  *
6  * Produced by:	Jeff Lait
7  *
8  *      	POWDER Development
9  *
10  * NAME:        encyc_support.cpp ( POWDER Library, C++ )
11  *
12  * COMMENTS:	This defines support functions for the encyclopedia.
13  */
14 
15 #include "glbdef.h"
16 #include <stdio.h>
17 #include "grammar.h"
18 #include "gfxengine.h"
19 #include "encyclopedia.h"
20 #include "encyc_support.h"
21 
22 //
23 // Purely internal functions:
24 //
25 
26 const encyclopedia_entry *
encyc_getentry(const char * bookname,int key)27 encyc_getentry(const char *bookname, int key)
28 {
29     const encyclopedia_book	*book;
30     int				 booknum;
31     int				 entrynum;
32 
33     for (booknum = 0; booknum < NUM_ENCYCLOPEDIA_BOOKS; booknum++)
34     {
35 	book = glb_encyclopedia[booknum];
36 
37 	if (strcmp(book->bookname, bookname))
38 	    continue;
39 
40 	for (entrynum = 0; entrynum < book->numentries; entrynum++)
41 	{
42 	    if (book->entries[entrynum]->key == key)
43 		return book->entries[entrynum];
44 	}
45     }
46 
47     return 0;
48 }
49 
50 //
51 // Externally available functions:
52 //
53 
54 bool
encyc_hasentry(const char * book,int key)55 encyc_hasentry(const char *book, int key)
56 {
57     const encyclopedia_entry	*entry;
58 
59     entry = encyc_getentry(book, key);
60 
61     if (entry)
62 	return true;
63     return false;
64 }
65 
66 void
encyc_pageentry(const char * book,int key)67 encyc_pageentry(const char *book, int key)
68 {
69     const encyclopedia_entry	*entry;
70     int				 i;
71 
72     entry = encyc_getentry(book, key);
73 
74     // Nothing to view?  Quit.
75     if (!entry)
76 	return;
77 
78     // Build word-wrapped display list.
79     for (i = 0; i < entry->numlines; i++)
80     {
81 	gfx_pager_addtext(entry->lines[i]);
82 	gfx_pager_newline();
83     }
84 }
85 
86 void
encyc_viewentry(const char * book,int key)87 encyc_viewentry(const char *book, int key)
88 {
89     const encyclopedia_entry	*entry;
90 
91     entry = encyc_getentry(book, key);
92 
93     // Nothing to view?  Quit.
94     if (!entry)
95 	return;
96 
97     encyc_pageentry(book, key);
98 
99     gfx_pager_display();
100 }
101 
102 void
encyc_viewSpellDescription(SPELL_NAMES spell)103 encyc_viewSpellDescription(SPELL_NAMES spell)
104 {
105     BUF			buf;
106 
107     // Plain description:
108     gfx_pager_addtext(glb_spelldefs[spell].name);
109     gfx_pager_newline();
110 
111     // Add standard spell stuff
112     gfx_pager_separator();
113 
114     if (glb_spelldefs[spell].mpcost)
115     {
116 	buf.sprintf("Magic Cost: %d", glb_spelldefs[spell].mpcost);
117 	gfx_pager_addtext(buf);
118 	gfx_pager_newline();
119     }
120     if (glb_spelldefs[spell].hpcost)
121     {
122 	buf.sprintf("Health Cost: %d", glb_spelldefs[spell].hpcost);
123 	gfx_pager_addtext(buf);
124 	gfx_pager_newline();
125     }
126     if (glb_spelldefs[spell].xpcost)
127     {
128 	buf.sprintf("EXP Cost: %d", glb_spelldefs[spell].xpcost);
129 	gfx_pager_addtext(buf);
130 	gfx_pager_newline();
131     }
132     buf.sprintf("Circle: ");
133     buf.strcat(gram_capitalize(glb_spelltypedefs[glb_spelldefs[spell].type].name));
134     gfx_pager_addtext(buf);
135     gfx_pager_newline();
136 
137     // Output any prereqs.
138     const u8 *prereq;
139 
140     prereq = (const u8 *) glb_spelldefs[spell].prereq;
141     if (prereq && *prereq)
142     {
143 	gfx_pager_addtext("This requires ");
144 
145 	while (*prereq)
146 	{
147 	    gfx_pager_addtext(glb_spelldefs[*prereq].name);
148 	    prereq++;
149 	    if (*prereq)
150 	    {
151 		if (!prereq[1])
152 		    gfx_pager_addtext(" and ");
153 		else
154 		    gfx_pager_addtext(", ");
155 	    }
156 	}
157 	gfx_pager_addtext(".");
158 	gfx_pager_newline();
159     }
160 
161     // Add encyclopedia entry, if any.
162     {
163 	const encyclopedia_entry	*entry;
164 
165 	entry = encyc_getentry("SPELL", spell);
166 	if (entry)
167 	{
168 	    gfx_pager_separator();
169 	    encyc_pageentry("SPELL", spell);
170 	}
171     }
172 
173 
174     gfx_pager_display();
175 }
176 
177 void
encyc_viewSkillDescription(SKILL_NAMES skill)178 encyc_viewSkillDescription(SKILL_NAMES skill)
179 {
180     // Plain description:
181     gfx_pager_addtext(glb_skilldefs[skill].name);
182     gfx_pager_newline();
183 
184     // Add standard skill stuff
185     gfx_pager_separator();
186 
187     // Output any prereqs.
188     const u8 *prereq;
189 
190     prereq = (const u8 *) glb_skilldefs[skill].prereq;
191     if (prereq && *prereq)
192     {
193 	gfx_pager_addtext("This requires ");
194 
195 	while (*prereq)
196 	{
197 	    gfx_pager_addtext(glb_skilldefs[*prereq].name);
198 	    prereq++;
199 	    if (*prereq)
200 	    {
201 		if (!prereq[1])
202 		    gfx_pager_addtext(" and ");
203 		else
204 		    gfx_pager_addtext(", ");
205 	    }
206 	}
207 	gfx_pager_addtext(".");
208 	gfx_pager_newline();
209     }
210 
211     // Add encyclopedia entry, if any.
212     {
213 	const encyclopedia_entry	*entry;
214 
215 	entry = encyc_getentry("SKILL", skill);
216 	if (entry)
217 	{
218 	    gfx_pager_separator();
219 	    encyc_pageentry("SKILL", skill);
220 	}
221     }
222 
223     gfx_pager_display();
224 }
225