1 | /* |
1 | /* |
2 | CrossFire, A Multiplayer game for X-windows |
2 | * CrossFire, A Multiplayer game |
3 | |
3 | * |
|
|
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | |
7 | * |
7 | This program is free software; you can redistribute it and/or modify |
8 | * This program is free software; you can redistribute it and/or modify |
8 | 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 |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. |
11 | * (at your option) any later version. |
11 | |
12 | * |
12 | 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, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
16 | |
17 | * |
17 | 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 |
18 | along with this program; if not, write to the Free Software |
19 | * along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | |
21 | * |
21 | The maintainer of this code can be reached at <crossfire@schmorp.de> |
22 | * The maintainer of this code can be reached at <crossfire@schmorp.de> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | |
25 | |
25 | #include <global.h> |
26 | #include <global.h> |
26 | #include <stdio.h> |
27 | #include <stdio.h> |
27 | |
28 | |
|
|
29 | #include "crc.h" |
|
|
30 | |
28 | New_Face *new_faces; |
31 | facetile *new_faces; |
29 | |
32 | |
30 | /* bmappair and xbm are used when looking for the image id numbers |
33 | /* bmappair and xbm are used when looking for the image id numbers |
31 | * of a face by name. xbm is sorted alphabetically so that bsearch |
34 | * of a face by name. xbm is sorted alphabetically so that bsearch |
32 | * can be used to quickly find the entry for a name. the number is |
35 | * can be used to quickly find the entry for a name. the number is |
33 | * then an index into the new_faces array. |
36 | * then an index into the new_faces array. |
… | |
… | |
43 | { |
46 | { |
44 | char *name; |
47 | char *name; |
45 | unsigned int number; |
48 | unsigned int number; |
46 | }; |
49 | }; |
47 | |
50 | |
48 | void free (bmappair *); // guard to catch free when delete should be used |
|
|
49 | |
|
|
50 | static struct bmappair *xbm = NULL; |
51 | static struct bmappair *xbm = NULL; |
51 | |
52 | |
52 | /* Following can just as easily be pointers, but |
|
|
53 | * it is easier to keep them like this. |
|
|
54 | */ |
|
|
55 | New_Face *blank_face, *dark_faces[3], *empty_face, *smooth_face; |
53 | facetile *blank_face, *dark_faces[3], *empty_face; |
56 | |
|
|
57 | |
54 | |
58 | /* nroffiles is the actual number of bitmaps defined. |
55 | /* nroffiles is the actual number of bitmaps defined. |
59 | * nrofpixmaps is the number of bitmaps loaded. With |
56 | * nrofpixmaps is the number of bitmaps loaded. With |
60 | * the automatic generation of the bmaps file, this is now equal |
57 | * the automatic generation of the bmaps file, this is now equal |
61 | * to nroffiles. |
58 | * to nroffiles. |
… | |
… | |
69 | int nrofpixmaps = 0; |
66 | int nrofpixmaps = 0; |
70 | |
67 | |
71 | /** |
68 | /** |
72 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
69 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
73 | */ |
70 | */ |
74 | struct smoothing:zero_initialised |
71 | struct smoothing : zero_initialised |
75 | { |
72 | { |
76 | uint16 id; |
73 | uint16 id; |
77 | uint16 smooth; |
74 | uint16 smooth; |
78 | }; |
75 | }; |
79 | |
|
|
80 | void free (smoothing *); // guard to catch free when delete should be used |
|
|
81 | |
76 | |
82 | /** |
77 | /** |
83 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
78 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
84 | * entries. It is sorted by smooth[].id. |
79 | * entries. It is sorted by smooth[].id. |
85 | */ |
80 | */ |
… | |
… | |
111 | static int |
106 | static int |
112 | compar (const struct bmappair *a, const struct bmappair *b) |
107 | compar (const struct bmappair *a, const struct bmappair *b) |
113 | { |
108 | { |
114 | return strcmp (a->name, b->name); |
109 | return strcmp (a->name, b->name); |
115 | } |
110 | } |
|
|
111 | |
116 | static int |
112 | static int |
117 | compar_smooth (const struct smoothing *a, const struct smoothing *b) |
113 | compar_smooth (const struct smoothing *a, const struct smoothing *b) |
118 | { |
114 | { |
119 | if (a->id < b->id) |
115 | if (a->id < b->id) |
120 | return -1; |
116 | return -1; |
121 | if (b->id < a->id) |
117 | if (b->id < a->id) |
122 | return 1; |
118 | return 1; |
123 | return 0; |
119 | return 0; |
124 | } |
120 | } |
125 | |
121 | |
126 | |
|
|
127 | /* |
122 | /* |
128 | * Returns the matching color in the coloralias if found, |
123 | * Returns the matching color in the coloralias if found, |
129 | * 0 otherwise. Note that 0 will actually be black, so there is no |
124 | * 0 otherwise. Note that 0 will actually be black, so there is no |
130 | * way the calling function can tell if an error occurred or not |
125 | * way the calling function can tell if an error occurred or not |
131 | */ |
126 | */ |
132 | |
|
|
133 | static uint8 |
127 | static uint8 |
134 | find_color (const char *name) |
128 | find_color (const char *name) |
135 | { |
129 | { |
136 | uint8 i; |
130 | uint8 i; |
137 | |
131 | |
… | |
… | |
143 | } |
137 | } |
144 | |
138 | |
145 | /* This reads the lib/faces file, getting color and visibility information. |
139 | /* This reads the lib/faces file, getting color and visibility information. |
146 | * it is called by ReadBmapNames. |
140 | * it is called by ReadBmapNames. |
147 | */ |
141 | */ |
148 | |
|
|
149 | static void |
142 | static void |
150 | ReadFaceData (void) |
143 | ReadFaceData (void) |
151 | { |
144 | { |
152 | char buf[MAX_BUF], *cp; |
145 | char buf[MAX_BUF], *cp; |
153 | New_Face *on_face = NULL; |
146 | facetile *on_face = NULL; |
154 | FILE *fp; |
147 | FILE *fp; |
155 | |
148 | |
156 | sprintf (buf, "%s/faces", settings.datadir); |
149 | sprintf (buf, "%s/faces", settings.datadir); |
157 | LOG (llevDebug, "Reading faces from %s...", buf); |
150 | LOG (llevDebug, "Reading faces from %s...\n", buf); |
158 | if ((fp = fopen (buf, "r")) == NULL) |
151 | if ((fp = fopen (buf, "r")) == NULL) |
159 | { |
152 | { |
160 | LOG (llevError, "Cannot open faces file %s: %s\n", buf, strerror (errno)); |
153 | LOG (llevError, "Cannot open faces file %s: %s\n", buf, strerror (errno)); |
161 | exit (-1); |
154 | exit (-1); |
162 | } |
155 | } |
… | |
… | |
216 | on_face->magicmap |= FACE_FLOOR; |
209 | on_face->magicmap |= FACE_FLOOR; |
217 | } |
210 | } |
218 | else |
211 | else |
219 | LOG (llevDebug, "Got unknown line in faces file: %s\n", buf); |
212 | LOG (llevDebug, "Got unknown line in faces file: %s\n", buf); |
220 | } |
213 | } |
|
|
214 | |
221 | LOG (llevDebug, "done\n"); |
215 | LOG (llevDebug, "done\n"); |
222 | fclose (fp); |
216 | fclose (fp); |
223 | } |
217 | } |
224 | |
218 | |
225 | /* This reads the bmaps file to get all the bitmap names and |
219 | /* This reads the bmaps file to get all the bitmap names and |
226 | * stuff. It only needs to be done once, because it is player |
220 | * stuff. It only needs to be done once, because it is player |
227 | * independent (ie, what display the person is on will not make a |
221 | * independent (ie, what display the person is on will not make a |
228 | * difference.) |
222 | * difference.) |
229 | */ |
223 | */ |
230 | |
|
|
231 | void |
224 | void |
232 | ReadBmapNames (void) |
225 | ReadBmapNames (void) |
233 | { |
226 | { |
234 | char buf[MAX_BUF], *p, *q; |
227 | char buf[MAX_BUF], *p, *q; |
235 | FILE *fp; |
228 | FILE *fp; |
236 | int value, nrofbmaps = 0, i; |
229 | int value, nrofbmaps = 0, i; |
237 | size_t l; |
230 | size_t l; |
|
|
231 | crc32 crc; |
238 | |
232 | |
239 | bmaps_checksum = 0; |
|
|
240 | sprintf (buf, "%s/bmaps", settings.datadir); |
233 | sprintf (buf, "%s/bmaps", settings.datadir); |
241 | LOG (llevDebug, "Reading bmaps from %s...", buf); |
234 | LOG (llevDebug, "Reading bmaps from %s...\n", buf); |
242 | if ((fp = fopen (buf, "r")) == NULL) |
235 | if ((fp = fopen (buf, "r")) == NULL) |
243 | { |
236 | { |
244 | LOG (llevError, "Cannot open bmaps file %s: %s\n", buf, strerror (errno)); |
237 | LOG (llevError, "Cannot open bmaps file %s: %s\n", buf, strerror (errno)); |
245 | exit (-1); |
238 | exit (-1); |
246 | } |
239 | } |
… | |
… | |
263 | if (!(p = strtok (p, " \t")) || !(q = strtok (NULL, " \t\n"))) |
256 | if (!(p = strtok (p, " \t")) || !(q = strtok (NULL, " \t\n"))) |
264 | { |
257 | { |
265 | LOG (llevDebug, "Warning, syntax error: %s\n", buf); |
258 | LOG (llevDebug, "Warning, syntax error: %s\n", buf); |
266 | continue; |
259 | continue; |
267 | } |
260 | } |
|
|
261 | |
268 | value = atoi (p); |
262 | value = atoi (p); |
269 | xbm[nroffiles].name = strdup (q); |
263 | xbm[nroffiles].name = strdup (q); |
270 | |
264 | |
271 | /* We need to calculate the checksum of the bmaps file |
265 | /* We need to calculate the checksum of the bmaps file |
272 | * name->number mapping to send to the client. This does not |
266 | * name->number mapping to send to the client. This does not |
273 | * need to match what sum or other utility may come up with - |
267 | * need to match what sum or other utility may come up with - |
274 | * as long as we get the same results on the same real file |
268 | * as long as we get the same results on the same real file |
275 | * data, it does the job as it lets the client know if |
269 | * data, it does the job as it lets the client know if |
276 | * the file has the same data or not. |
270 | * the file has the same data or not. |
277 | */ |
271 | */ |
278 | ROTATE_RIGHT (bmaps_checksum); |
272 | crc (value); |
279 | bmaps_checksum += value & 0xff; |
273 | crc (value >> 8); |
280 | bmaps_checksum &= 0xffffffff; |
|
|
281 | |
274 | |
282 | ROTATE_RIGHT (bmaps_checksum); |
|
|
283 | bmaps_checksum += (value >> 8) & 0xff; |
|
|
284 | bmaps_checksum &= 0xffffffff; |
|
|
285 | for (l = 0; l < strlen (q); l++) |
275 | for (l = 0; l < strlen (q); l++) |
286 | { |
276 | crc (q [l]); |
287 | ROTATE_RIGHT (bmaps_checksum); |
|
|
288 | bmaps_checksum += q[l]; |
|
|
289 | bmaps_checksum &= 0xffffffff; |
|
|
290 | } |
|
|
291 | |
277 | |
292 | xbm[nroffiles].number = value; |
278 | xbm[nroffiles].number = value; |
293 | nroffiles++; |
279 | nroffiles++; |
294 | if (value >= nrofpixmaps) |
280 | if (value >= nrofpixmaps) |
295 | nrofpixmaps = value + 1; |
281 | nrofpixmaps = value + 1; |
296 | } |
282 | } |
|
|
283 | |
297 | fclose (fp); |
284 | fclose (fp); |
298 | |
285 | |
299 | LOG (llevDebug, "done (got %d/%d/%d)\n", nrofpixmaps, nrofbmaps, nroffiles); |
286 | LOG (llevDebug, "done (got %d/%d/%d)\n", nrofpixmaps, nrofbmaps, nroffiles); |
300 | |
287 | |
301 | new_faces = new New_Face[nrofpixmaps]; |
288 | new_faces = new facetile[nrofpixmaps]; |
302 | |
289 | |
303 | for (i = 0; i < nrofpixmaps; i++) |
290 | for (i = 0; i < nrofpixmaps; i++) |
304 | { |
291 | { |
305 | new_faces[i].name = ""; |
292 | new_faces[i].name = ""; |
306 | new_faces[i].number = i; |
293 | new_faces[i].number = i; |
307 | new_faces[i].visibility = 0; |
294 | new_faces[i].visibility = 0; |
308 | new_faces[i].magicmap = 255; |
295 | new_faces[i].magicmap = 255; |
309 | } |
296 | } |
|
|
297 | |
310 | for (i = 0; i < nroffiles; i++) |
298 | for (i = 0; i < nroffiles; i++) |
311 | { |
|
|
312 | new_faces[xbm[i].number].name = xbm[i].name; |
299 | new_faces[xbm[i].number].name = xbm[i].name; |
313 | } |
|
|
314 | |
300 | |
315 | // non-pod datatype, likely not allowed |
301 | // non-pod datatype, likely not allowed |
316 | qsort (xbm, nroffiles, sizeof (struct bmappair), (int (*)(const void *, const void *)) compar); |
302 | qsort (xbm, nroffiles, sizeof (struct bmappair), (int (*)(const void *, const void *)) compar); |
317 | |
303 | |
318 | ReadFaceData (); |
304 | ReadFaceData (); |
… | |
… | |
337 | |
323 | |
338 | dark_faces[0] = &new_faces[FindFace (DARK_FACE1_NAME, 0)]; |
324 | dark_faces[0] = &new_faces[FindFace (DARK_FACE1_NAME, 0)]; |
339 | dark_faces[1] = &new_faces[FindFace (DARK_FACE2_NAME, 0)]; |
325 | dark_faces[1] = &new_faces[FindFace (DARK_FACE2_NAME, 0)]; |
340 | dark_faces[2] = &new_faces[FindFace (DARK_FACE3_NAME, 0)]; |
326 | dark_faces[2] = &new_faces[FindFace (DARK_FACE3_NAME, 0)]; |
341 | |
327 | |
342 | smooth_face = &new_faces[FindFace (SMOOTH_FACE_NAME, 0)]; |
328 | bmaps_checksum = crc; |
343 | } |
329 | } |
344 | |
330 | |
345 | /* This returns an the face number of face 'name'. Number is constant |
331 | /* This returns an the face number of face 'name'. Number is constant |
346 | * during an invocation, but not necessarily between versions (this |
332 | * during an invocation, but not necessarily between versions (this |
347 | * is because the faces are arranged in alphabetical order, so |
333 | * is because the faces are arranged in alphabetical order, so |
… | |
… | |
385 | { |
371 | { |
386 | char buf[MAX_BUF], *p, *q; |
372 | char buf[MAX_BUF], *p, *q; |
387 | FILE *fp; |
373 | FILE *fp; |
388 | int smoothcount = 0; |
374 | int smoothcount = 0; |
389 | |
375 | |
390 | bmaps_checksum = 0; |
|
|
391 | sprintf (buf, "%s/smooth", settings.datadir); |
376 | sprintf (buf, "%s/smooth", settings.datadir); |
392 | LOG (llevDebug, "Reading smooth from %s...", buf); |
377 | LOG (llevDebug, "Reading smooth from %s...\n", buf); |
393 | if ((fp = fopen (buf, "r")) == NULL) |
378 | if ((fp = fopen (buf, "r")) == NULL) |
394 | { |
379 | { |
395 | LOG (llevError, "Cannot open smooth file %s: %s\n", strerror (errno)); |
380 | LOG (llevError, "Cannot open smooth file %s: %s\n", strerror (errno)); |
396 | exit (-1); |
381 | exit (-1); |
397 | } |
382 | } |