1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
3 | * |
3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 | * Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
8 | * Crossfire TRT is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
11 | * (at your option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the Free Software |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
21 | * |
20 | * |
22 | * The maintainer of this code can be reached at <crossfire@schmorp.de> |
21 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
23 | */ |
22 | */ |
24 | |
|
|
25 | |
23 | |
26 | #include <global.h> |
24 | #include <global.h> |
27 | #include <stdio.h> |
25 | #include <stdio.h> |
28 | |
26 | |
|
|
27 | #include "face.h" |
29 | #include "crc.h" |
28 | #include "crc.h" |
30 | |
29 | |
31 | facetile *new_faces; |
30 | faceidx blank_face, empty_face; |
32 | |
31 | |
33 | /* bmappair and xbm are used when looking for the image id numbers |
32 | facehash_t facehash; |
34 | * of a face by name. xbm is sorted alphabetically so that bsearch |
33 | std::vector<faceinfo> faces; |
35 | * can be used to quickly find the entry for a name. the number is |
|
|
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 | |
|
|
45 | struct bmappair |
|
|
46 | { |
|
|
47 | char *name; |
|
|
48 | unsigned int number; |
|
|
49 | }; |
|
|
50 | |
|
|
51 | static struct bmappair *xbm = NULL; |
|
|
52 | |
|
|
53 | facetile *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 | */ |
|
|
65 | static int nroffiles = 0; |
|
|
66 | int nrofpixmaps = 0; |
|
|
67 | |
|
|
68 | /** |
|
|
69 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
|
|
70 | */ |
|
|
71 | struct smoothing : zero_initialised |
|
|
72 | { |
|
|
73 | uint16 id; |
|
|
74 | uint16 smooth; |
|
|
75 | }; |
|
|
76 | |
|
|
77 | /** |
|
|
78 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
|
|
79 | * entries. It is sorted by smooth[].id. |
|
|
80 | */ |
|
|
81 | static struct smoothing *smooth = NULL; |
|
|
82 | int nrofsmooth = 0; |
|
|
83 | |
34 | |
84 | /* the only thing this table is used for now is to |
35 | /* the only thing this table is used for now is to |
85 | * translate the colorname in the magicmap field of the |
36 | * translate the colorname in the magicmap field of the |
86 | * face into a numeric index that is then sent to the |
37 | * face into a numeric index that is then sent to the |
87 | * client for magic map commands. The order of this table |
38 | * client for magic map commands. The order of this table |
… | |
… | |
101 | "brown", /* 10 */ |
52 | "brown", /* 10 */ |
102 | "yellow", /* 11 */ |
53 | "yellow", /* 11 */ |
103 | "khaki" /* 12 */ |
54 | "khaki" /* 12 */ |
104 | }; |
55 | }; |
105 | |
56 | |
106 | static int |
|
|
107 | compar (const struct bmappair *a, const struct bmappair *b) |
|
|
108 | { |
|
|
109 | return strcmp (a->name, b->name); |
|
|
110 | } |
|
|
111 | |
|
|
112 | static int |
|
|
113 | compar_smooth (const struct smoothing *a, const struct smoothing *b) |
|
|
114 | { |
|
|
115 | if (a->id < b->id) |
|
|
116 | return -1; |
|
|
117 | if (b->id < a->id) |
|
|
118 | return 1; |
|
|
119 | return 0; |
|
|
120 | } |
|
|
121 | |
|
|
122 | /* |
57 | /* |
123 | * Returns the matching color in the coloralias if found, |
58 | * Returns the matching color in the coloralias if found, |
124 | * 0 otherwise. Note that 0 will actually be black, so there is no |
59 | * 0 otherwise. Note that 0 will actually be black, so there is no |
125 | * way the calling function can tell if an error occurred or not |
60 | * way the calling function can tell if an error occurred or not |
126 | */ |
61 | */ |
… | |
… | |
130 | uint8 i; |
65 | uint8 i; |
131 | |
66 | |
132 | for (i = 0; i < sizeof (colorname) / sizeof (*colorname); i++) |
67 | for (i = 0; i < sizeof (colorname) / sizeof (*colorname); i++) |
133 | if (!strcmp (name, colorname[i])) |
68 | if (!strcmp (name, colorname[i])) |
134 | return i; |
69 | return i; |
|
|
70 | |
135 | LOG (llevError, "Unknown color: %s\n", name); |
71 | LOG (llevError, "Unknown color: %s\n", name); |
136 | return 0; |
72 | return 0; |
137 | } |
73 | } |
138 | |
74 | |
139 | /* This reads the lib/faces file, getting color and visibility information. |
75 | faceidx |
140 | * it is called by ReadBmapNames. |
76 | face_find (const char *name, faceidx defidx) |
141 | */ |
|
|
142 | static void |
|
|
143 | ReadFaceData (void) |
|
|
144 | { |
77 | { |
145 | char buf[MAX_BUF], *cp; |
78 | if (!name) |
146 | facetile *on_face = NULL; |
79 | return defidx; |
147 | FILE *fp; |
|
|
148 | |
80 | |
149 | sprintf (buf, "%s/faces", settings.datadir); |
81 | facehash_t::iterator i = facehash.find (name); |
150 | LOG (llevDebug, "Reading faces from %s...\n", buf); |
|
|
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 | |
82 | |
168 | cp = buf + 5; |
83 | return i == facehash.end () |
169 | cp[strlen (cp) - 1] = '\0'; /* remove newline */ |
84 | ? defidx : i->second; |
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 | } |
85 | } |
218 | |
86 | |
219 | /* This reads the bmaps file to get all the bitmap names and |
87 | faceinfo * |
220 | * stuff. It only needs to be done once, because it is player |
88 | face_info (faceidx idx) |
221 | * independent (ie, what display the person is on will not make a |
|
|
222 | * difference.) |
|
|
223 | */ |
|
|
224 | void |
|
|
225 | ReadBmapNames (void) |
|
|
226 | { |
89 | { |
227 | char buf[MAX_BUF], *p, *q; |
90 | assert (0 < (faceidx)-1); // faceidx must be unsigned |
228 | FILE *fp; |
|
|
229 | int value, nrofbmaps = 0, i; |
|
|
230 | size_t l; |
|
|
231 | crc32 crc; |
|
|
232 | |
91 | |
233 | sprintf (buf, "%s/bmaps", settings.datadir); |
92 | if (idx >= faces.size ()) |
234 | LOG (llevDebug, "Reading bmaps from %s...\n", buf); |
93 | return 0; |
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 | |
94 | |
241 | /* First count how many bitmaps we have, so we can allocate correctly */ |
95 | return &faces [idx]; |
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 | } |
96 | } |
330 | |
97 | |
331 | /* This returns an the face number of face 'name'. Number is constant |
98 | facedata * |
332 | * during an invocation, but not necessarily between versions (this |
99 | face_data (faceidx idx, int faceset) |
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 | */ |
|
|
344 | int |
|
|
345 | FindFace (const char *name, int error) |
|
|
346 | { |
100 | { |
347 | struct bmappair *bp, tmp; |
101 | if (faceinfo *f = face_info (idx)) |
348 | char *p; |
102 | return &(faceset && !f->type && f->data64.data.size () ? f->data64 : f->data32); |
349 | |
103 | |
350 | if (!name) |
104 | return 0; |
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 | } |
105 | } |
361 | |
106 | |
362 | /* Reads the smooth file to know how to smooth datas. |
|
|
363 | * the smooth file if made of 2 elements lines. |
|
|
364 | * lines starting with # are comment |
|
|
365 | * the first element of line is face to smooth |
|
|
366 | * the next element is the 16x2 faces picture |
|
|
367 | * used for smoothing |
|
|
368 | */ |
|
|
369 | int |
|
|
370 | ReadSmooth (void) |
|
|
371 | { |
|
|
372 | char buf[MAX_BUF], *p, *q; |
|
|
373 | FILE *fp; |
|
|
374 | int smoothcount = 0; |
|
|
375 | |
|
|
376 | sprintf (buf, "%s/smooth", settings.datadir); |
|
|
377 | LOG (llevDebug, "Reading smooth from %s...\n", buf); |
|
|
378 | if ((fp = fopen (buf, "r")) == NULL) |
|
|
379 | { |
|
|
380 | LOG (llevError, "Cannot open smooth file %s: %s\n", strerror (errno)); |
|
|
381 | exit (-1); |
|
|
382 | } |
|
|
383 | |
|
|
384 | /* First count how many smooth we have, so we can allocate correctly */ |
|
|
385 | while (fgets (buf, MAX_BUF, fp) != NULL) |
|
|
386 | if (buf[0] != '#' && buf[0] != '\n') |
|
|
387 | smoothcount++; |
|
|
388 | rewind (fp); |
|
|
389 | |
|
|
390 | smooth = new smoothing[smoothcount]; |
|
|
391 | |
|
|
392 | while (nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp) != NULL) |
|
|
393 | { |
|
|
394 | if (*buf == '#') |
|
|
395 | continue; |
|
|
396 | p = strchr (buf, ' '); |
|
|
397 | if (!p) |
|
|
398 | continue; |
|
|
399 | *p = '\0'; |
|
|
400 | q = buf; |
|
|
401 | smooth[nrofsmooth].id = FindFace (q, 0); |
|
|
402 | q = p + 1; |
|
|
403 | smooth[nrofsmooth].smooth = FindFace (q, 0); |
|
|
404 | nrofsmooth++; |
|
|
405 | } |
|
|
406 | fclose (fp); |
|
|
407 | |
|
|
408 | LOG (llevDebug, "done (got %d smooth entries)\n", nrofsmooth); |
|
|
409 | qsort (smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth); |
|
|
410 | return nrofsmooth; |
|
|
411 | } |
|
|
412 | |
|
|
413 | /** |
|
|
414 | * Find the smooth face for a given face. |
|
|
415 | * |
|
|
416 | * @param face the face to find the smoothing face for |
|
|
417 | * |
|
|
418 | * @param smoothed return value: set to smooth face |
|
|
419 | * |
|
|
420 | * @return 1=smooth face found, 0=no smooth face found |
|
|
421 | */ |
|
|
422 | int |
|
|
423 | FindSmooth (uint16 face, uint16 * smoothed) |
|
|
424 | { |
|
|
425 | struct smoothing *bp, tmp; |
|
|
426 | |
|
|
427 | tmp.id = face; |
|
|
428 | bp = (struct smoothing *) bsearch |
|
|
429 | (&tmp, smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth); |
|
|
430 | (*smoothed) = 0; |
|
|
431 | if (bp) |
|
|
432 | (*smoothed) = bp->smooth; |
|
|
433 | return bp ? 1 : 0; |
|
|
434 | } |
|
|
435 | |
|
|
436 | /** |
|
|
437 | * Deallocates memory allocated by ReadBmapNames() and ReadSmooth(). |
|
|
438 | */ |
|
|
439 | void |
|
|
440 | free_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 | } |
|
|