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

Comparing deliantra/server/common/image.C (file contents):
Revision 1.18 by root, Sun Mar 4 19:36:12 2007 UTC vs.
Revision 1.19 by root, Sun Mar 11 02:12:44 2007 UTC

24 24
25 25
26#include <global.h> 26#include <global.h>
27#include <stdio.h> 27#include <stdio.h>
28 28
29#include "face.h"
29#include "crc.h" 30#include "crc.h"
30 31
31facetile *new_faces; 32faceidx blank_face, empty_face;
32 33
33/* bmappair and xbm are used when looking for the image id numbers 34facehash_t facehash;
34 * of a face by name. xbm is sorted alphabetically so that bsearch 35std::vector<faceinfo> faces;
35 * can be used to quickly find the entry for a name. the number is 36std::vector<facedata> face32, face64;
36 * then an index into the new_faces array.
37 * This data is redundant with new_face information - the difference
38 * is that this data gets sorted, and that doesn't necessarily happen
39 * with the new_face data - when accessing new_face[some number],
40 * that some number corresponds to the face at that number - for
41 * xbm, it may not. At current time, these do in fact match because
42 * the bmaps file is created in a sorted order.
43 */
44
45struct bmappair
46{
47 char *name;
48 unsigned int number;
49};
50
51static struct bmappair *xbm = NULL;
52
53facetile *blank_face, *dark_faces[3], *empty_face;
54
55/* nroffiles is the actual number of bitmaps defined.
56 * nrofpixmaps is the number of bitmaps loaded. With
57 * the automatic generation of the bmaps file, this is now equal
58 * to nroffiles.
59 *
60 * The xbm array (which contains name and number information, and
61 * is then sorted) contains nroffiles entries. the xbm_names
62 * array (which is used for converting the numeric face to
63 * a name) contains nrofpixmaps entries.
64 */
65static int nroffiles = 0;
66int nrofpixmaps = 0;
67 37
68/** 38/**
69 * id is the face to smooth, smooth is the 16x2 face used to smooth id. 39 * id is the face to smooth, smooth is the 16x2 face used to smooth id.
70 */ 40 */
71struct smoothing : zero_initialised 41struct smoothing : zero_initialised
102 "yellow", /* 11 */ 72 "yellow", /* 11 */
103 "khaki" /* 12 */ 73 "khaki" /* 12 */
104}; 74};
105 75
106static int 76static int
107compar (const struct bmappair *a, const struct bmappair *b)
108{
109 return strcmp (a->name, b->name);
110}
111
112static int
113compar_smooth (const struct smoothing *a, const struct smoothing *b) 77compar_smooth (const struct smoothing *a, const struct smoothing *b)
114{ 78{
115 if (a->id < b->id) 79 if (a->id < b->id)
116 return -1; 80 return -1;
117 if (b->id < a->id) 81 if (b->id < a->id)
130 uint8 i; 94 uint8 i;
131 95
132 for (i = 0; i < sizeof (colorname) / sizeof (*colorname); i++) 96 for (i = 0; i < sizeof (colorname) / sizeof (*colorname); i++)
133 if (!strcmp (name, colorname[i])) 97 if (!strcmp (name, colorname[i]))
134 return i; 98 return i;
99
135 LOG (llevError, "Unknown color: %s\n", name); 100 LOG (llevError, "Unknown color: %s\n", name);
136 return 0; 101 return 0;
137} 102}
138 103
139/* This reads the lib/faces file, getting color and visibility information. 104int
140 * it is called by ReadBmapNames. 105face_find (const char *name, int defidx)
141 */
142static void
143ReadFaceData (void)
144{ 106{
145 char buf[MAX_BUF], *cp; 107 facehash_t::iterator i = facehash.find (name);
146 facetile *on_face = NULL;
147 FILE *fp;
148 108
149 sprintf (buf, "%s/faces", settings.datadir); 109 return i == facehash.end ()
150 LOG (llevDebug, "Reading faces from %s...\n", buf); 110 ? defidx : i->second;
151 if ((fp = fopen (buf, "r")) == NULL)
152 {
153 LOG (llevError, "Cannot open faces file %s: %s\n", buf, strerror (errno));
154 exit (-1);
155 }
156 while (fgets (buf, MAX_BUF, fp) != NULL)
157 {
158 if (*buf == '#')
159 continue;
160 if (!strncmp (buf, "end", 3))
161 {
162 on_face = NULL;
163 }
164 else if (!strncmp (buf, "face", 4))
165 {
166 int tmp;
167
168 cp = buf + 5;
169 cp[strlen (cp) - 1] = '\0'; /* remove newline */
170
171 if ((tmp = FindFace (cp, -1)) == -1)
172 {
173 LOG (llevError, "Could not find face %s\n", cp);
174 continue;
175 }
176 on_face = &new_faces[tmp];
177 on_face->visibility = 0;
178 }
179 else if (on_face == NULL)
180 {
181 LOG (llevError, "Got line with no face set: %s\n", buf);
182 }
183 else if (!strncmp (buf, "color_fg", 8))
184 {
185 cp = buf + 9;
186 cp[strlen (cp) - 1] = '\0';
187 if (on_face->magicmap == 255)
188 on_face->magicmap = find_color (cp);
189 }
190 else if (!strncmp (buf, "color_bg", 8))
191 {
192 /* ignore it */
193 }
194 else if (!strncmp (buf, "visibility", 10))
195 {
196 on_face->visibility = atoi (buf + 11);
197 }
198 else if (!strncmp (buf, "magicmap", 8))
199 {
200 cp = buf + 9;
201 cp[strlen (cp) - 1] = '\0';
202 on_face->magicmap = find_color (cp);
203 }
204 else if (!strncmp (buf, "is_floor", 8))
205 {
206 int value = atoi (buf + 9);
207
208 if (value)
209 on_face->magicmap |= FACE_FLOOR;
210 }
211 else
212 LOG (llevDebug, "Got unknown line in faces file: %s\n", buf);
213 }
214
215 LOG (llevDebug, "done\n");
216 fclose (fp);
217} 111}
218 112
219/* This reads the bmaps file to get all the bitmap names and 113facedata *
220 * stuff. It only needs to be done once, because it is player 114face_data (int idx, int faceset)
221 * independent (ie, what display the person is on will not make a
222 * difference.)
223 */
224void
225ReadBmapNames (void)
226{ 115{
227 char buf[MAX_BUF], *p, *q; 116 return &(faceset ? face64 : face32)[idx];
228 FILE *fp;
229 int value, nrofbmaps = 0, i;
230 size_t l;
231 crc32 crc;
232
233 sprintf (buf, "%s/bmaps", settings.datadir);
234 LOG (llevDebug, "Reading bmaps from %s...\n", buf);
235 if ((fp = fopen (buf, "r")) == NULL)
236 {
237 LOG (llevError, "Cannot open bmaps file %s: %s\n", buf, strerror (errno));
238 exit (-1);
239 }
240
241 /* First count how many bitmaps we have, so we can allocate correctly */
242 while (fgets (buf, MAX_BUF, fp) != NULL)
243 if (buf[0] != '#' && buf[0] != '\n')
244 nrofbmaps++;
245 rewind (fp);
246
247 xbm = new bmappair[nrofbmaps];
248 memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps);
249
250 while (nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL)
251 {
252 if (*buf == '#')
253 continue;
254
255 p = (*buf == '\\') ? (buf + 1) : buf;
256 if (!(p = strtok (p, " \t")) || !(q = strtok (NULL, " \t\n")))
257 {
258 LOG (llevDebug, "Warning, syntax error: %s\n", buf);
259 continue;
260 }
261
262 value = atoi (p);
263 xbm[nroffiles].name = strdup (q);
264
265 /* We need to calculate the checksum of the bmaps file
266 * name->number mapping to send to the client. This does not
267 * need to match what sum or other utility may come up with -
268 * as long as we get the same results on the same real file
269 * data, it does the job as it lets the client know if
270 * the file has the same data or not.
271 */
272 crc (value);
273 crc (value >> 8);
274
275 for (l = 0; l < strlen (q); l++)
276 crc (q [l]);
277
278 xbm[nroffiles].number = value;
279 nroffiles++;
280 if (value >= nrofpixmaps)
281 nrofpixmaps = value + 1;
282 }
283
284 fclose (fp);
285
286 LOG (llevDebug, "done (got %d/%d/%d)\n", nrofpixmaps, nrofbmaps, nroffiles);
287
288 new_faces = new facetile[nrofpixmaps];
289
290 for (i = 0; i < nrofpixmaps; i++)
291 {
292 new_faces[i].name = "";
293 new_faces[i].number = i;
294 new_faces[i].visibility = 0;
295 new_faces[i].magicmap = 255;
296 }
297
298 for (i = 0; i < nroffiles; i++)
299 new_faces[xbm[i].number].name = xbm[i].name;
300
301 // non-pod datatype, likely not allowed
302 qsort (xbm, nroffiles, sizeof (struct bmappair), (int (*)(const void *, const void *)) compar);
303
304 ReadFaceData ();
305
306 for (i = 0; i < nrofpixmaps; i++)
307 {
308 if (new_faces[i].magicmap == 255)
309 {
310#if 0 /* Useful for initial debugging, not needed now */
311 LOG (llevDebug, "Face %s still had default magicmap, resetting to black\n", new_faces[i].name);
312#endif
313 new_faces[i].magicmap = 0;
314 }
315 }
316 /* Actually forcefully setting the colors here probably should not
317 * be done - it could easily create confusion.
318 */
319 blank_face = &new_faces[FindFace (BLANK_FACE_NAME, 0)];
320 blank_face->magicmap = find_color ("khaki") | FACE_FLOOR;
321
322 empty_face = &new_faces[FindFace (EMPTY_FACE_NAME, 0)];
323
324 dark_faces[0] = &new_faces[FindFace (DARK_FACE1_NAME, 0)];
325 dark_faces[1] = &new_faces[FindFace (DARK_FACE2_NAME, 0)];
326 dark_faces[2] = &new_faces[FindFace (DARK_FACE3_NAME, 0)];
327
328 bmaps_checksum = crc;
329}
330
331/* This returns an the face number of face 'name'. Number is constant
332 * during an invocation, but not necessarily between versions (this
333 * is because the faces are arranged in alphabetical order, so
334 * if a face is removed or added, all faces after that will now
335 * have a different number.
336 *
337 * the parameter error determines behaviour. If a face is
338 * not found, then error is returned. This can be useful if
339 * you want some default face used, or can be set to negative
340 * so that it will be known that the face could not be found
341 * (needed in client, so that it will know to request that image
342 * from the server)
343 */
344int
345FindFace (const char *name, int error)
346{
347 struct bmappair *bp, tmp;
348 char *p;
349
350 if (!name)
351 return error;
352
353 if ((p = strchr (name, '\n')))
354 *p = '\0';
355
356 tmp.name = (char *) name;
357 bp = (struct bmappair *) bsearch (&tmp, xbm, nroffiles, sizeof (struct bmappair), (int (*)(const void *, const void *)) compar);
358
359 return bp ? bp->number : error;
360} 117}
361 118
362/* Reads the smooth file to know how to smooth datas. 119/* Reads the smooth file to know how to smooth datas.
363 * the smooth file if made of 2 elements lines. 120 * the smooth file if made of 2 elements lines.
364 * lines starting with # are comment 121 * lines starting with # are comment
396 p = strchr (buf, ' '); 153 p = strchr (buf, ' ');
397 if (!p) 154 if (!p)
398 continue; 155 continue;
399 *p = '\0'; 156 *p = '\0';
400 q = buf; 157 q = buf;
401 smooth[nrofsmooth].id = FindFace (q, 0); 158 smooth[nrofsmooth].id = face_find (q);
402 q = p + 1; 159 q = p + 1;
403 smooth[nrofsmooth].smooth = FindFace (q, 0); 160 smooth[nrofsmooth].smooth = face_find (q);
404 nrofsmooth++; 161 nrofsmooth++;
405 } 162 }
406 fclose (fp); 163 fclose (fp);
407 164
408 LOG (llevDebug, "done (got %d smooth entries)\n", nrofsmooth); 165 LOG (llevDebug, "done (got %d smooth entries)\n", nrofsmooth);
431 if (bp) 188 if (bp)
432 (*smoothed) = bp->smooth; 189 (*smoothed) = bp->smooth;
433 return bp ? 1 : 0; 190 return bp ? 1 : 0;
434} 191}
435 192
436/**
437 * Deallocates memory allocated by ReadBmapNames() and ReadSmooth().
438 */
439void
440free_all_images (void)
441{
442 int i;
443
444 for (i = 0; i < nroffiles; i++)
445 free (xbm[i].name);
446
447 delete[]xbm;
448 delete[]new_faces;
449 delete[]smooth;
450}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines