1 | /* |
1 | /* |
2 | * static char *rcsid_image_c = |
|
|
3 | * "$Id: image.C,v 1.1 2006/08/13 17:16:00 elmex Exp $"; |
|
|
4 | */ |
|
|
5 | |
|
|
6 | /* |
|
|
7 | CrossFire, A Multiplayer game for X-windows |
2 | * CrossFire, A Multiplayer game |
8 | |
3 | * |
|
|
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
9 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
10 | Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (C) 1992 Frank Tore Johansen |
11 | |
7 | * |
12 | 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 |
13 | 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 |
14 | the Free Software Foundation; either version 2 of the License, or |
10 | * the Free Software Foundation; either version 2 of the License, or |
15 | (at your option) any later version. |
11 | * (at your option) any later version. |
16 | |
12 | * |
17 | 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, |
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
21 | |
17 | * |
22 | 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 |
23 | along with this program; if not, write to the Free Software |
19 | * along with this program; if not, write to the Free Software |
24 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 | |
21 | * |
26 | The maintainer of this code can be reached at crossfire-devel@real-time.com |
22 | * The maintainer of this code can be reached at <crossfire@schmorp.de> |
27 | */ |
23 | */ |
28 | |
24 | |
29 | |
25 | |
30 | #include <global.h> |
26 | #include <global.h> |
31 | #include <stdio.h> |
27 | #include <stdio.h> |
32 | |
28 | |
33 | New_Face *new_faces; |
29 | facetile *new_faces; |
34 | |
30 | |
35 | /* bmappair and xbm are used when looking for the image id numbers |
31 | /* bmappair and xbm are used when looking for the image id numbers |
36 | * of a face by name. xbm is sorted alphabetically so that bsearch |
32 | * of a face by name. xbm is sorted alphabetically so that bsearch |
37 | * can be used to quickly find the entry for a name. the number is |
33 | * can be used to quickly find the entry for a name. the number is |
38 | * then an index into the new_faces array. |
34 | * then an index into the new_faces array. |
… | |
… | |
42 | * that some number corresponds to the face at that number - for |
38 | * that some number corresponds to the face at that number - for |
43 | * xbm, it may not. At current time, these do in fact match because |
39 | * xbm, it may not. At current time, these do in fact match because |
44 | * the bmaps file is created in a sorted order. |
40 | * the bmaps file is created in a sorted order. |
45 | */ |
41 | */ |
46 | |
42 | |
47 | struct bmappair { |
43 | struct bmappair |
|
|
44 | { |
48 | char *name; |
45 | char *name; |
49 | unsigned int number; |
46 | unsigned int number; |
50 | }; |
47 | }; |
51 | |
48 | |
|
|
49 | void free (bmappair *); // guard to catch free when delete should be used |
|
|
50 | |
52 | static struct bmappair *xbm=NULL; |
51 | static struct bmappair *xbm = NULL; |
53 | |
52 | |
54 | /* Following can just as easily be pointers, but |
53 | /* Following can just as easily be pointers, but |
55 | * it is easier to keep them like this. |
54 | * it is easier to keep them like this. |
56 | */ |
55 | */ |
57 | New_Face *blank_face, *dark_faces[3], *empty_face, *smooth_face; |
56 | facetile *blank_face, *dark_faces[3], *empty_face, *smooth_face; |
58 | |
57 | |
59 | |
58 | |
60 | /* nroffiles is the actual number of bitmaps defined. |
59 | /* nroffiles is the actual number of bitmaps defined. |
61 | * nrofpixmaps is the number of bitmaps loaded. With |
60 | * nrofpixmaps is the number of bitmaps loaded. With |
62 | * the automatic generation of the bmaps file, this is now equal |
61 | * the automatic generation of the bmaps file, this is now equal |
… | |
… | |
71 | int nrofpixmaps = 0; |
70 | int nrofpixmaps = 0; |
72 | |
71 | |
73 | /** |
72 | /** |
74 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
73 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
75 | */ |
74 | */ |
76 | struct smoothing { |
75 | struct smoothing:zero_initialised |
|
|
76 | { |
77 | uint16 id; |
77 | uint16 id; |
78 | uint16 smooth; |
78 | uint16 smooth; |
79 | }; |
79 | }; |
|
|
80 | |
|
|
81 | void free (smoothing *); // guard to catch free when delete should be used |
80 | |
82 | |
81 | /** |
83 | /** |
82 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
84 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
83 | * entries. It is sorted by smooth[].id. |
85 | * entries. It is sorted by smooth[].id. |
84 | */ |
86 | */ |
85 | static struct smoothing *smooth=NULL; |
87 | static struct smoothing *smooth = NULL; |
86 | int nrofsmooth=0; |
88 | int nrofsmooth = 0; |
87 | |
89 | |
88 | /* the only thing this table is used for now is to |
90 | /* the only thing this table is used for now is to |
89 | * translate the colorname in the magicmap field of the |
91 | * translate the colorname in the magicmap field of the |
90 | * face into a numeric index that is then sent to the |
92 | * face into a numeric index that is then sent to the |
91 | * client for magic map commands. The order of this table |
93 | * client for magic map commands. The order of this table |
92 | * must match that of the NDI colors in include/newclient.h. |
94 | * must match that of the NDI colors in include/newclient.h. |
93 | */ |
95 | */ |
94 | static const char *const colorname[] = { |
96 | static const char *const colorname[] = { |
95 | "black", /* 0 */ |
97 | "black", /* 0 */ |
96 | "white", /* 1 */ |
98 | "white", /* 1 */ |
97 | "blue", /* 2 */ |
99 | "blue", /* 2 */ |
98 | "red", /* 3 */ |
100 | "red", /* 3 */ |
99 | "orange", /* 4 */ |
101 | "orange", /* 4 */ |
100 | "light_blue", /* 5 */ |
102 | "light_blue", /* 5 */ |
101 | "dark_orange", /* 6 */ |
103 | "dark_orange", /* 6 */ |
102 | "green", /* 7 */ |
104 | "green", /* 7 */ |
103 | "light_green", /* 8 */ |
105 | "light_green", /* 8 */ |
104 | "grey", /* 9 */ |
106 | "grey", /* 9 */ |
105 | "brown", /* 10 */ |
107 | "brown", /* 10 */ |
106 | "yellow", /* 11 */ |
108 | "yellow", /* 11 */ |
107 | "khaki" /* 12 */ |
109 | "khaki" /* 12 */ |
108 | }; |
110 | }; |
109 | |
111 | |
|
|
112 | static int |
110 | static int compar (const struct bmappair *a, const struct bmappair *b) { |
113 | compar (const struct bmappair *a, const struct bmappair *b) |
|
|
114 | { |
111 | return strcmp (a->name, b->name); |
115 | return strcmp (a->name, b->name); |
112 | } |
116 | } |
|
|
117 | |
|
|
118 | static int |
113 | static int compar_smooth (const struct smoothing *a, const struct smoothing *b) { |
119 | compar_smooth (const struct smoothing *a, const struct smoothing *b) |
|
|
120 | { |
114 | if (a->id<b->id) |
121 | if (a->id < b->id) |
115 | return -1; |
122 | return -1; |
116 | if (b->id<a->id) |
123 | if (b->id < a->id) |
117 | return 1; |
|
|
118 | return 0; |
124 | return 1; |
|
|
125 | return 0; |
119 | } |
126 | } |
120 | |
|
|
121 | |
127 | |
122 | /* |
128 | /* |
123 | * Returns the matching color in the coloralias if found, |
129 | * Returns the matching color in the coloralias if found, |
124 | * 0 otherwise. Note that 0 will actually be black, so there is no |
130 | * 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 |
131 | * way the calling function can tell if an error occurred or not |
126 | */ |
132 | */ |
127 | |
133 | static uint8 |
128 | static uint8 find_color(const char *name) { |
134 | find_color (const char *name) |
|
|
135 | { |
129 | uint8 i; |
136 | uint8 i; |
|
|
137 | |
130 | for(i=0;i<sizeof(colorname)/sizeof(*colorname);i++) |
138 | for (i = 0; i < sizeof (colorname) / sizeof (*colorname); i++) |
131 | if(!strcmp(name,colorname[i])) |
139 | if (!strcmp (name, colorname[i])) |
132 | return i; |
140 | return i; |
133 | LOG(llevError,"Unknown color: %s\n",name); |
141 | LOG (llevError, "Unknown color: %s\n", name); |
134 | return 0; |
142 | return 0; |
135 | } |
143 | } |
136 | |
144 | |
137 | /* This reads the lib/faces file, getting color and visibility information. |
145 | /* This reads the lib/faces file, getting color and visibility information. |
138 | * it is called by ReadBmapNames. |
146 | * it is called by ReadBmapNames. |
139 | */ |
147 | */ |
140 | |
148 | static void |
141 | static void ReadFaceData(void) |
149 | ReadFaceData (void) |
142 | { |
150 | { |
143 | char buf[MAX_BUF], *cp; |
151 | char buf[MAX_BUF], *cp; |
144 | New_Face *on_face=NULL; |
152 | facetile *on_face = NULL; |
145 | FILE *fp; |
153 | FILE *fp; |
146 | |
154 | |
147 | sprintf(buf,"%s/faces", settings.datadir); |
155 | sprintf (buf, "%s/faces", settings.datadir); |
148 | LOG(llevDebug,"Reading faces from %s...",buf); |
156 | LOG (llevDebug, "Reading faces from %s...\n", buf); |
149 | if ((fp=fopen(buf,"r"))==NULL) { |
157 | if ((fp = fopen (buf, "r")) == NULL) |
|
|
158 | { |
150 | LOG(llevError, "Cannot open faces file %s: %s\n", buf, strerror_local(errno)); |
159 | LOG (llevError, "Cannot open faces file %s: %s\n", buf, strerror (errno)); |
151 | exit(-1); |
160 | exit (-1); |
152 | } |
161 | } |
153 | while (fgets(buf, MAX_BUF, fp)!=NULL) { |
162 | while (fgets (buf, MAX_BUF, fp) != NULL) |
154 | if (*buf=='#') continue; |
163 | { |
|
|
164 | if (*buf == '#') |
|
|
165 | continue; |
155 | if (!strncmp(buf,"end",3)) { |
166 | if (!strncmp (buf, "end", 3)) |
|
|
167 | { |
156 | on_face = NULL; |
168 | on_face = NULL; |
157 | } |
169 | } |
158 | else if (!strncmp(buf,"face",4)) { |
170 | else if (!strncmp (buf, "face", 4)) |
|
|
171 | { |
159 | int tmp; |
172 | int tmp; |
160 | |
173 | |
161 | cp = buf + 5; |
174 | cp = buf + 5; |
162 | cp[strlen(cp)-1] = '\0'; /* remove newline */ |
175 | cp[strlen (cp) - 1] = '\0'; /* remove newline */ |
163 | |
176 | |
164 | if ((tmp=FindFace(cp,-1))==-1) { |
177 | if ((tmp = FindFace (cp, -1)) == -1) |
|
|
178 | { |
165 | LOG(llevError,"Could not find face %s\n", cp); |
179 | LOG (llevError, "Could not find face %s\n", cp); |
166 | continue; |
180 | continue; |
167 | } |
181 | } |
168 | on_face = &new_faces[tmp]; |
182 | on_face = &new_faces[tmp]; |
169 | on_face->visibility=0; |
183 | on_face->visibility = 0; |
170 | } |
184 | } |
171 | else if (on_face==NULL) { |
185 | else if (on_face == NULL) |
|
|
186 | { |
172 | LOG(llevError,"Got line with no face set: %s\n", buf); |
187 | LOG (llevError, "Got line with no face set: %s\n", buf); |
173 | } |
188 | } |
174 | else if (!strncmp(buf,"color_fg",8)) { |
189 | else if (!strncmp (buf, "color_fg", 8)) |
|
|
190 | { |
175 | cp = buf + 9; |
191 | cp = buf + 9; |
176 | cp[strlen(cp)-1] = '\0'; |
192 | cp[strlen (cp) - 1] = '\0'; |
177 | if (on_face->magicmap==255) on_face->magicmap=find_color(cp); |
193 | if (on_face->magicmap == 255) |
178 | } |
194 | on_face->magicmap = find_color (cp); |
|
|
195 | } |
179 | else if (!strncmp(buf,"color_bg",8)) { |
196 | else if (!strncmp (buf, "color_bg", 8)) |
|
|
197 | { |
180 | /* ignore it */ |
198 | /* ignore it */ |
181 | } |
199 | } |
182 | else if (!strncmp(buf,"visibility",10)) { |
200 | else if (!strncmp (buf, "visibility", 10)) |
|
|
201 | { |
183 | on_face->visibility = atoi(buf + 11); |
202 | on_face->visibility = atoi (buf + 11); |
184 | } |
203 | } |
185 | else if (!strncmp(buf,"magicmap",8)) { |
204 | else if (!strncmp (buf, "magicmap", 8)) |
186 | cp=buf+9; |
205 | { |
|
|
206 | cp = buf + 9; |
187 | cp[strlen(cp)-1] = '\0'; |
207 | cp[strlen (cp) - 1] = '\0'; |
188 | on_face->magicmap=find_color(cp); |
208 | on_face->magicmap = find_color (cp); |
189 | } |
209 | } |
190 | else if (!strncmp(buf,"is_floor",8)) { |
210 | else if (!strncmp (buf, "is_floor", 8)) |
|
|
211 | { |
191 | int value = atoi(buf+9); |
212 | int value = atoi (buf + 9); |
192 | if (value) on_face->magicmap |= FACE_FLOOR; |
213 | |
193 | } |
214 | if (value) |
|
|
215 | on_face->magicmap |= FACE_FLOOR; |
|
|
216 | } |
|
|
217 | else |
194 | else LOG(llevDebug,"Got unknown line in faces file: %s\n", buf); |
218 | LOG (llevDebug, "Got unknown line in faces file: %s\n", buf); |
195 | } |
219 | } |
|
|
220 | |
196 | LOG(llevDebug,"done\n"); |
221 | LOG (llevDebug, "done\n"); |
197 | fclose(fp); |
222 | fclose (fp); |
198 | } |
223 | } |
199 | |
224 | |
200 | /* This reads the bmaps file to get all the bitmap names and |
225 | /* This reads the bmaps file to get all the bitmap names and |
201 | * stuff. It only needs to be done once, because it is player |
226 | * stuff. It only needs to be done once, because it is player |
202 | * independent (ie, what display the person is on will not make a |
227 | * independent (ie, what display the person is on will not make a |
203 | * difference.) |
228 | * difference.) |
204 | */ |
229 | */ |
205 | |
230 | void |
206 | void ReadBmapNames (void) { |
231 | ReadBmapNames (void) |
|
|
232 | { |
207 | char buf[MAX_BUF], *p, *q; |
233 | char buf[MAX_BUF], *p, *q; |
208 | FILE *fp; |
234 | FILE *fp; |
209 | int value, nrofbmaps = 0, i; |
235 | int value, nrofbmaps = 0, i; |
210 | size_t l; |
236 | size_t l; |
211 | |
237 | |
212 | bmaps_checksum=0; |
238 | bmaps_checksum = 0; |
213 | sprintf (buf,"%s/bmaps", settings.datadir); |
239 | sprintf (buf, "%s/bmaps", settings.datadir); |
214 | LOG(llevDebug,"Reading bmaps from %s...",buf); |
240 | LOG (llevDebug, "Reading bmaps from %s...\n", buf); |
215 | if ((fp=fopen(buf,"r"))==NULL) { |
241 | if ((fp = fopen (buf, "r")) == NULL) |
|
|
242 | { |
216 | LOG(llevError, "Cannot open bmaps file %s: %s\n", buf, strerror_local(errno)); |
243 | LOG (llevError, "Cannot open bmaps file %s: %s\n", buf, strerror (errno)); |
217 | exit(-1); |
244 | exit (-1); |
218 | } |
|
|
219 | |
245 | } |
|
|
246 | |
220 | /* First count how many bitmaps we have, so we can allocate correctly */ |
247 | /* First count how many bitmaps we have, so we can allocate correctly */ |
221 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
248 | while (fgets (buf, MAX_BUF, fp) != NULL) |
222 | if(buf[0] != '#' && buf[0] != '\n' ) |
249 | if (buf[0] != '#' && buf[0] != '\n') |
223 | nrofbmaps++; |
250 | nrofbmaps++; |
224 | rewind(fp); |
251 | rewind (fp); |
225 | |
252 | |
226 | xbm = (struct bmappair *) malloc(sizeof(struct bmappair) * nrofbmaps); |
253 | xbm = new bmappair[nrofbmaps]; |
227 | memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps); |
254 | memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps); |
228 | |
255 | |
229 | while(nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) { |
256 | while (nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) |
|
|
257 | { |
230 | if (*buf == '#') |
258 | if (*buf == '#') |
231 | continue; |
259 | continue; |
232 | |
260 | |
233 | p = (*buf == '\\') ? (buf + 1): buf; |
261 | p = (*buf == '\\') ? (buf + 1) : buf; |
234 | if (!(p = strtok (p , " \t")) || !(q = strtok (NULL , " \t\n"))) { |
262 | if (!(p = strtok (p, " \t")) || !(q = strtok (NULL, " \t\n"))) |
|
|
263 | { |
235 | LOG(llevDebug,"Warning, syntax error: %s\n", buf); |
264 | LOG (llevDebug, "Warning, syntax error: %s\n", buf); |
236 | continue; |
265 | continue; |
237 | } |
266 | } |
238 | value = atoi (p); |
267 | value = atoi (p); |
239 | xbm[nroffiles].name = strdup_local(q); |
268 | xbm[nroffiles].name = strdup (q); |
240 | |
269 | |
241 | /* We need to calculate the checksum of the bmaps file |
270 | /* We need to calculate the checksum of the bmaps file |
242 | * name->number mapping to send to the client. This does not |
271 | * name->number mapping to send to the client. This does not |
243 | * need to match what sum or other utility may come up with - |
272 | * need to match what sum or other utility may come up with - |
244 | * as long as we get the same results on the same real file |
273 | * as long as we get the same results on the same real file |
245 | * data, it does the job as it lets the client know if |
274 | * data, it does the job as it lets the client know if |
246 | * the file has the same data or not. |
275 | * the file has the same data or not. |
247 | */ |
276 | */ |
248 | ROTATE_RIGHT(bmaps_checksum); |
|
|
249 | bmaps_checksum += value & 0xff; |
|
|
250 | bmaps_checksum &= 0xffffffff; |
|
|
251 | |
|
|
252 | ROTATE_RIGHT(bmaps_checksum); |
|
|
253 | bmaps_checksum += (value >> 8) & 0xff; |
|
|
254 | bmaps_checksum &= 0xffffffff; |
|
|
255 | for (l=0; l<strlen(q); l++) { |
|
|
256 | ROTATE_RIGHT(bmaps_checksum); |
277 | ROTATE_RIGHT (bmaps_checksum); |
257 | bmaps_checksum += q[l]; |
278 | bmaps_checksum += value & 0xff; |
258 | bmaps_checksum &= 0xffffffff; |
279 | bmaps_checksum &= 0xffffffff; |
259 | } |
|
|
260 | |
280 | |
|
|
281 | ROTATE_RIGHT (bmaps_checksum); |
|
|
282 | bmaps_checksum += (value >> 8) & 0xff; |
|
|
283 | bmaps_checksum &= 0xffffffff; |
|
|
284 | for (l = 0; l < strlen (q); l++) |
|
|
285 | { |
|
|
286 | ROTATE_RIGHT (bmaps_checksum); |
|
|
287 | bmaps_checksum += q[l]; |
|
|
288 | bmaps_checksum &= 0xffffffff; |
|
|
289 | } |
|
|
290 | |
261 | xbm[nroffiles].number = value; |
291 | xbm[nroffiles].number = value; |
262 | nroffiles++; |
292 | nroffiles++; |
263 | if(value >= nrofpixmaps) |
293 | if (value >= nrofpixmaps) |
264 | nrofpixmaps = value+1; |
294 | nrofpixmaps = value + 1; |
265 | } |
295 | } |
266 | fclose(fp); |
296 | fclose (fp); |
267 | |
297 | |
268 | LOG(llevDebug,"done (got %d/%d/%d)\n",nrofpixmaps,nrofbmaps,nroffiles); |
298 | LOG (llevDebug, "done (got %d/%d/%d)\n", nrofpixmaps, nrofbmaps, nroffiles); |
269 | |
299 | |
270 | new_faces = (New_Face *)malloc(sizeof(New_Face) * nrofpixmaps); |
300 | new_faces = new facetile[nrofpixmaps]; |
|
|
301 | |
271 | for (i = 0; i < nrofpixmaps; i++) { |
302 | for (i = 0; i < nrofpixmaps; i++) |
|
|
303 | { |
272 | new_faces[i].name = ""; |
304 | new_faces[i].name = ""; |
273 | new_faces[i].number = i; |
305 | new_faces[i].number = i; |
274 | new_faces[i].visibility=0; |
306 | new_faces[i].visibility = 0; |
275 | new_faces[i].magicmap=255; |
307 | new_faces[i].magicmap = 255; |
276 | } |
308 | } |
277 | for (i = 0; i < nroffiles; i++) { |
309 | for (i = 0; i < nroffiles; i++) |
|
|
310 | { |
278 | new_faces[xbm[i].number].name = xbm[i].name; |
311 | new_faces[xbm[i].number].name = xbm[i].name; |
279 | } |
312 | } |
280 | |
313 | |
|
|
314 | // non-pod datatype, likely not allowed |
281 | qsort (xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
315 | qsort (xbm, nroffiles, sizeof (struct bmappair), (int (*)(const void *, const void *)) compar); |
282 | |
316 | |
283 | ReadFaceData(); |
317 | ReadFaceData (); |
284 | |
318 | |
285 | for (i = 0; i < nrofpixmaps; i++) { |
319 | for (i = 0; i < nrofpixmaps; i++) |
|
|
320 | { |
286 | if (new_faces[i].magicmap==255) { |
321 | if (new_faces[i].magicmap == 255) |
|
|
322 | { |
287 | #if 0 /* Useful for initial debugging, not needed now */ |
323 | #if 0 /* Useful for initial debugging, not needed now */ |
288 | LOG(llevDebug,"Face %s still had default magicmap, resetting to black\n", |
324 | LOG (llevDebug, "Face %s still had default magicmap, resetting to black\n", new_faces[i].name); |
289 | new_faces[i].name); |
|
|
290 | #endif |
325 | #endif |
291 | new_faces[i].magicmap=0; |
326 | new_faces[i].magicmap = 0; |
292 | } |
327 | } |
293 | } |
328 | } |
294 | /* Actually forcefully setting the colors here probably should not |
329 | /* Actually forcefully setting the colors here probably should not |
295 | * be done - it could easily create confusion. |
330 | * be done - it could easily create confusion. |
296 | */ |
331 | */ |
297 | blank_face = &new_faces[FindFace(BLANK_FACE_NAME, 0)]; |
332 | blank_face = &new_faces[FindFace (BLANK_FACE_NAME, 0)]; |
298 | blank_face->magicmap = find_color ("khaki") | FACE_FLOOR; |
333 | blank_face->magicmap = find_color ("khaki") | FACE_FLOOR; |
299 | |
334 | |
300 | empty_face = &new_faces[FindFace(EMPTY_FACE_NAME, 0)]; |
335 | empty_face = &new_faces[FindFace (EMPTY_FACE_NAME, 0)]; |
301 | |
336 | |
302 | dark_faces[0] = &new_faces[FindFace (DARK_FACE1_NAME,0)]; |
337 | dark_faces[0] = &new_faces[FindFace (DARK_FACE1_NAME, 0)]; |
303 | dark_faces[1] = &new_faces[FindFace (DARK_FACE2_NAME,0)]; |
338 | dark_faces[1] = &new_faces[FindFace (DARK_FACE2_NAME, 0)]; |
304 | dark_faces[2] = &new_faces[FindFace (DARK_FACE3_NAME,0)]; |
339 | dark_faces[2] = &new_faces[FindFace (DARK_FACE3_NAME, 0)]; |
305 | |
340 | |
306 | smooth_face = &new_faces[FindFace(SMOOTH_FACE_NAME,0)]; |
341 | smooth_face = &new_faces[FindFace (SMOOTH_FACE_NAME, 0)]; |
307 | } |
342 | } |
308 | |
343 | |
309 | /* This returns an the face number of face 'name'. Number is constant |
344 | /* This returns an the face number of face 'name'. Number is constant |
310 | * during an invocation, but not necessarily between versions (this |
345 | * during an invocation, but not necessarily between versions (this |
311 | * is because the faces are arranged in alphabetical order, so |
346 | * is because the faces are arranged in alphabetical order, so |
… | |
… | |
317 | * you want some default face used, or can be set to negative |
352 | * you want some default face used, or can be set to negative |
318 | * so that it will be known that the face could not be found |
353 | * so that it will be known that the face could not be found |
319 | * (needed in client, so that it will know to request that image |
354 | * (needed in client, so that it will know to request that image |
320 | * from the server) |
355 | * from the server) |
321 | */ |
356 | */ |
|
|
357 | int |
322 | int FindFace (const char *name, int error) { |
358 | FindFace (const char *name, int error) |
|
|
359 | { |
323 | struct bmappair *bp, tmp; |
360 | struct bmappair *bp, tmp; |
324 | char *p; |
361 | char *p; |
325 | |
362 | |
|
|
363 | if (!name) |
|
|
364 | return error; |
|
|
365 | |
326 | if ((p = strchr (name, '\n'))) |
366 | if ((p = strchr (name, '\n'))) |
327 | *p = '\0'; |
367 | *p = '\0'; |
328 | |
368 | |
329 | tmp.name = (char *)name; |
369 | tmp.name = (char *) name; |
330 | bp = (struct bmappair *)bsearch |
|
|
331 | (&tmp, xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
370 | bp = (struct bmappair *) bsearch (&tmp, xbm, nroffiles, sizeof (struct bmappair), (int (*)(const void *, const void *)) compar); |
332 | |
371 | |
333 | return bp ? bp->number : error; |
372 | return bp ? bp->number : error; |
334 | } |
373 | } |
335 | |
374 | |
336 | /* Reads the smooth file to know how to smooth datas. |
375 | /* Reads the smooth file to know how to smooth datas. |
337 | * the smooth file if made of 2 elements lines. |
376 | * the smooth file if made of 2 elements lines. |
338 | * lines starting with # are comment |
377 | * lines starting with # are comment |
339 | * the first element of line is face to smooth |
378 | * the first element of line is face to smooth |
340 | * the next element is the 16x2 faces picture |
379 | * the next element is the 16x2 faces picture |
341 | * used for smoothing |
380 | * used for smoothing |
342 | */ |
381 | */ |
|
|
382 | int |
343 | int ReadSmooth (void) { |
383 | ReadSmooth (void) |
|
|
384 | { |
344 | char buf[MAX_BUF], *p, *q; |
385 | char buf[MAX_BUF], *p, *q; |
345 | FILE *fp; |
386 | FILE *fp; |
346 | int smoothcount = 0; |
387 | int smoothcount = 0; |
347 | |
388 | |
348 | bmaps_checksum=0; |
389 | bmaps_checksum = 0; |
349 | sprintf (buf,"%s/smooth", settings.datadir); |
390 | sprintf (buf, "%s/smooth", settings.datadir); |
350 | LOG(llevDebug,"Reading smooth from %s...",buf); |
391 | LOG (llevDebug, "Reading smooth from %s...\n", buf); |
351 | if ((fp=fopen(buf,"r"))==NULL) { |
392 | if ((fp = fopen (buf, "r")) == NULL) |
|
|
393 | { |
352 | LOG(llevError, "Cannot open smooth file %s: %s\n", strerror_local(errno)); |
394 | LOG (llevError, "Cannot open smooth file %s: %s\n", strerror (errno)); |
353 | exit(-1); |
395 | exit (-1); |
354 | } |
396 | } |
355 | |
397 | |
356 | /* First count how many smooth we have, so we can allocate correctly */ |
398 | /* First count how many smooth we have, so we can allocate correctly */ |
357 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
399 | while (fgets (buf, MAX_BUF, fp) != NULL) |
358 | if(buf[0] != '#' && buf[0] != '\n' ) |
400 | if (buf[0] != '#' && buf[0] != '\n') |
359 | smoothcount++; |
401 | smoothcount++; |
360 | rewind(fp); |
402 | rewind (fp); |
361 | |
403 | |
362 | smooth = (struct smoothing *) malloc(sizeof(struct smoothing) * (smoothcount)); |
404 | smooth = new smoothing[smoothcount]; |
363 | memset (smooth, 0, sizeof (struct smoothing) * (smoothcount)); |
|
|
364 | |
405 | |
365 | while(nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp)!=NULL) { |
406 | while (nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp) != NULL) |
|
|
407 | { |
366 | if (*buf == '#') |
408 | if (*buf == '#') |
367 | continue; |
409 | continue; |
368 | p=strchr(buf,' '); |
410 | p = strchr (buf, ' '); |
369 | if (!p) |
411 | if (!p) |
370 | continue; |
412 | continue; |
371 | *p='\0'; |
413 | *p = '\0'; |
372 | q=buf; |
414 | q = buf; |
373 | smooth[nrofsmooth].id=FindFace(q,0); |
415 | smooth[nrofsmooth].id = FindFace (q, 0); |
374 | q=p+1; |
416 | q = p + 1; |
375 | smooth[nrofsmooth].smooth=FindFace(q,0); |
417 | smooth[nrofsmooth].smooth = FindFace (q, 0); |
376 | nrofsmooth++; |
418 | nrofsmooth++; |
377 | } |
419 | } |
378 | fclose(fp); |
420 | fclose (fp); |
379 | |
421 | |
380 | LOG(llevDebug,"done (got %d smooth entries)\n",nrofsmooth); |
422 | LOG (llevDebug, "done (got %d smooth entries)\n", nrofsmooth); |
381 | qsort (smooth, nrofsmooth, sizeof(struct smoothing), (int (*)(const void*, const void*))compar_smooth); |
423 | qsort (smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth); |
382 | return nrofsmooth; |
424 | return nrofsmooth; |
383 | } |
425 | } |
384 | |
426 | |
385 | /** |
427 | /** |
386 | * Find the smooth face for a given face. |
428 | * Find the smooth face for a given face. |
387 | * |
429 | * |
… | |
… | |
389 | * |
431 | * |
390 | * @param smoothed return value: set to smooth face |
432 | * @param smoothed return value: set to smooth face |
391 | * |
433 | * |
392 | * @return 1=smooth face found, 0=no smooth face found |
434 | * @return 1=smooth face found, 0=no smooth face found |
393 | */ |
435 | */ |
|
|
436 | int |
394 | int FindSmooth (uint16 face, uint16* smoothed) { |
437 | FindSmooth (uint16 face, uint16 * smoothed) |
|
|
438 | { |
395 | struct smoothing *bp, tmp; |
439 | struct smoothing *bp, tmp; |
396 | |
440 | |
397 | tmp.id = face; |
441 | tmp.id = face; |
398 | bp = (struct smoothing *)bsearch |
442 | bp = (struct smoothing *) bsearch |
399 | (&tmp, smooth, nrofsmooth, sizeof(struct smoothing), (int (*)(const void*, const void*))compar_smooth); |
443 | (&tmp, smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth); |
400 | (*smoothed)=0; |
444 | (*smoothed) = 0; |
401 | if (bp) |
445 | if (bp) |
402 | (*smoothed)=bp->smooth; |
446 | (*smoothed) = bp->smooth; |
403 | return bp ? 1 : 0; |
447 | return bp ? 1 : 0; |
404 | } |
448 | } |
405 | |
449 | |
406 | /** |
450 | /** |
407 | * Deallocates memory allocated by ReadBmapNames() and ReadSmooth(). |
451 | * Deallocates memory allocated by ReadBmapNames() and ReadSmooth(). |
408 | */ |
452 | */ |
|
|
453 | void |
409 | void free_all_images(void) |
454 | free_all_images (void) |
410 | { |
455 | { |
411 | int i; |
456 | int i; |
412 | |
457 | |
413 | for (i=0; i<nroffiles; i++) |
458 | for (i = 0; i < nroffiles; i++) |
414 | free(xbm[i].name); |
459 | free (xbm[i].name); |
415 | free(xbm); |
460 | |
416 | free(new_faces); |
461 | delete[]xbm; |
417 | free(smooth); |
462 | delete[]new_faces; |
|
|
463 | delete[]smooth; |
418 | } |
464 | } |