1 | /* |
1 | /* |
2 | * static char *rcsid_image_c = |
2 | * static char *rcsid_image_c = |
3 | * "$Id: image.C,v 1.1 2006/08/13 17:16:00 elmex Exp $"; |
3 | * "$Id: image.C,v 1.2 2006/08/29 08:01:35 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /* |
6 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
7 | CrossFire, A Multiplayer game for X-windows |
8 | |
8 | |
… | |
… | |
145 | FILE *fp; |
145 | FILE *fp; |
146 | |
146 | |
147 | sprintf(buf,"%s/faces", settings.datadir); |
147 | sprintf(buf,"%s/faces", settings.datadir); |
148 | LOG(llevDebug,"Reading faces from %s...",buf); |
148 | LOG(llevDebug,"Reading faces from %s...",buf); |
149 | if ((fp=fopen(buf,"r"))==NULL) { |
149 | if ((fp=fopen(buf,"r"))==NULL) { |
150 | LOG(llevError, "Cannot open faces file %s: %s\n", buf, strerror_local(errno)); |
150 | LOG(llevError, "Cannot open faces file %s: %s\n", buf, strerror_local(errno)); |
151 | exit(-1); |
151 | exit(-1); |
152 | } |
152 | } |
153 | while (fgets(buf, MAX_BUF, fp)!=NULL) { |
153 | while (fgets(buf, MAX_BUF, fp)!=NULL) { |
154 | if (*buf=='#') continue; |
154 | if (*buf=='#') continue; |
155 | if (!strncmp(buf,"end",3)) { |
155 | if (!strncmp(buf,"end",3)) { |
156 | on_face = NULL; |
156 | on_face = NULL; |
157 | } |
157 | } |
158 | else if (!strncmp(buf,"face",4)) { |
158 | else if (!strncmp(buf,"face",4)) { |
159 | int tmp; |
159 | int tmp; |
160 | |
160 | |
161 | cp = buf + 5; |
161 | cp = buf + 5; |
162 | cp[strlen(cp)-1] = '\0'; /* remove newline */ |
162 | cp[strlen(cp)-1] = '\0'; /* remove newline */ |
163 | |
163 | |
164 | if ((tmp=FindFace(cp,-1))==-1) { |
164 | if ((tmp=FindFace(cp,-1))==-1) { |
165 | LOG(llevError,"Could not find face %s\n", cp); |
165 | LOG(llevError,"Could not find face %s\n", cp); |
166 | continue; |
166 | continue; |
167 | } |
167 | } |
168 | on_face = &new_faces[tmp]; |
168 | on_face = &new_faces[tmp]; |
169 | on_face->visibility=0; |
169 | on_face->visibility=0; |
170 | } |
170 | } |
171 | else if (on_face==NULL) { |
171 | else if (on_face==NULL) { |
172 | LOG(llevError,"Got line with no face set: %s\n", buf); |
172 | LOG(llevError,"Got line with no face set: %s\n", buf); |
173 | } |
173 | } |
174 | else if (!strncmp(buf,"color_fg",8)) { |
174 | else if (!strncmp(buf,"color_fg",8)) { |
175 | cp = buf + 9; |
175 | cp = buf + 9; |
176 | cp[strlen(cp)-1] = '\0'; |
176 | cp[strlen(cp)-1] = '\0'; |
177 | if (on_face->magicmap==255) on_face->magicmap=find_color(cp); |
177 | if (on_face->magicmap==255) on_face->magicmap=find_color(cp); |
178 | } |
178 | } |
179 | else if (!strncmp(buf,"color_bg",8)) { |
179 | else if (!strncmp(buf,"color_bg",8)) { |
180 | /* ignore it */ |
180 | /* ignore it */ |
181 | } |
181 | } |
182 | else if (!strncmp(buf,"visibility",10)) { |
182 | else if (!strncmp(buf,"visibility",10)) { |
183 | on_face->visibility = atoi(buf + 11); |
183 | on_face->visibility = atoi(buf + 11); |
184 | } |
184 | } |
185 | else if (!strncmp(buf,"magicmap",8)) { |
185 | else if (!strncmp(buf,"magicmap",8)) { |
186 | cp=buf+9; |
186 | cp=buf+9; |
187 | cp[strlen(cp)-1] = '\0'; |
187 | cp[strlen(cp)-1] = '\0'; |
188 | on_face->magicmap=find_color(cp); |
188 | on_face->magicmap=find_color(cp); |
189 | } |
189 | } |
190 | else if (!strncmp(buf,"is_floor",8)) { |
190 | else if (!strncmp(buf,"is_floor",8)) { |
191 | int value = atoi(buf+9); |
191 | int value = atoi(buf+9); |
192 | if (value) on_face->magicmap |= FACE_FLOOR; |
192 | if (value) on_face->magicmap |= FACE_FLOOR; |
193 | } |
193 | } |
194 | else LOG(llevDebug,"Got unknown line in faces file: %s\n", buf); |
194 | else LOG(llevDebug,"Got unknown line in faces file: %s\n", buf); |
195 | } |
195 | } |
196 | LOG(llevDebug,"done\n"); |
196 | LOG(llevDebug,"done\n"); |
197 | fclose(fp); |
197 | fclose(fp); |
198 | } |
198 | } |
199 | |
199 | |
… | |
… | |
211 | |
211 | |
212 | bmaps_checksum=0; |
212 | bmaps_checksum=0; |
213 | sprintf (buf,"%s/bmaps", settings.datadir); |
213 | sprintf (buf,"%s/bmaps", settings.datadir); |
214 | LOG(llevDebug,"Reading bmaps from %s...",buf); |
214 | LOG(llevDebug,"Reading bmaps from %s...",buf); |
215 | if ((fp=fopen(buf,"r"))==NULL) { |
215 | if ((fp=fopen(buf,"r"))==NULL) { |
216 | LOG(llevError, "Cannot open bmaps file %s: %s\n", buf, strerror_local(errno)); |
216 | LOG(llevError, "Cannot open bmaps file %s: %s\n", buf, strerror_local(errno)); |
217 | exit(-1); |
217 | exit(-1); |
218 | } |
218 | } |
219 | |
219 | |
220 | /* First count how many bitmaps we have, so we can allocate correctly */ |
220 | /* First count how many bitmaps we have, so we can allocate correctly */ |
221 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
221 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
222 | if(buf[0] != '#' && buf[0] != '\n' ) |
222 | if(buf[0] != '#' && buf[0] != '\n' ) |
223 | nrofbmaps++; |
223 | nrofbmaps++; |
224 | rewind(fp); |
224 | rewind(fp); |
225 | |
225 | |
226 | xbm = (struct bmappair *) malloc(sizeof(struct bmappair) * nrofbmaps); |
226 | xbm = (struct bmappair *) malloc(sizeof(struct bmappair) * nrofbmaps); |
227 | memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps); |
227 | memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps); |
228 | |
228 | |
229 | while(nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) { |
229 | while(nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) { |
230 | if (*buf == '#') |
230 | if (*buf == '#') |
231 | continue; |
231 | continue; |
232 | |
232 | |
233 | p = (*buf == '\\') ? (buf + 1): buf; |
233 | p = (*buf == '\\') ? (buf + 1): buf; |
234 | if (!(p = strtok (p , " \t")) || !(q = strtok (NULL , " \t\n"))) { |
234 | if (!(p = strtok (p , " \t")) || !(q = strtok (NULL , " \t\n"))) { |
235 | LOG(llevDebug,"Warning, syntax error: %s\n", buf); |
235 | LOG(llevDebug,"Warning, syntax error: %s\n", buf); |
236 | continue; |
236 | continue; |
237 | } |
237 | } |
238 | value = atoi (p); |
238 | value = atoi (p); |
239 | xbm[nroffiles].name = strdup_local(q); |
239 | xbm[nroffiles].name = strdup_local(q); |
240 | |
240 | |
241 | /* We need to calculate the checksum of the bmaps file |
241 | /* We need to calculate the checksum of the bmaps file |
242 | * name->number mapping to send to the client. This does not |
242 | * name->number mapping to send to the client. This does not |
243 | * need to match what sum or other utility may come up with - |
243 | * 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 |
244 | * 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 |
245 | * data, it does the job as it lets the client know if |
246 | * the file has the same data or not. |
246 | * the file has the same data or not. |
247 | */ |
247 | */ |
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); |
248 | ROTATE_RIGHT(bmaps_checksum); |
257 | bmaps_checksum += q[l]; |
249 | bmaps_checksum += value & 0xff; |
258 | bmaps_checksum &= 0xffffffff; |
250 | bmaps_checksum &= 0xffffffff; |
259 | } |
|
|
260 | |
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); |
|
|
257 | bmaps_checksum += q[l]; |
|
|
258 | bmaps_checksum &= 0xffffffff; |
|
|
259 | } |
|
|
260 | |
261 | xbm[nroffiles].number = value; |
261 | xbm[nroffiles].number = value; |
262 | nroffiles++; |
262 | nroffiles++; |
263 | if(value >= nrofpixmaps) |
263 | if(value >= nrofpixmaps) |
264 | nrofpixmaps = value+1; |
264 | nrofpixmaps = value+1; |
265 | } |
265 | } |
266 | fclose(fp); |
266 | fclose(fp); |
267 | |
267 | |
268 | LOG(llevDebug,"done (got %d/%d/%d)\n",nrofpixmaps,nrofbmaps,nroffiles); |
268 | LOG(llevDebug,"done (got %d/%d/%d)\n",nrofpixmaps,nrofbmaps,nroffiles); |
269 | |
269 | |
270 | new_faces = (New_Face *)malloc(sizeof(New_Face) * nrofpixmaps); |
270 | new_faces = (New_Face *)malloc(sizeof(New_Face) * nrofpixmaps); |
271 | for (i = 0; i < nrofpixmaps; i++) { |
271 | for (i = 0; i < nrofpixmaps; i++) { |
272 | new_faces[i].name = ""; |
272 | new_faces[i].name = ""; |
273 | new_faces[i].number = i; |
273 | new_faces[i].number = i; |
274 | new_faces[i].visibility=0; |
274 | new_faces[i].visibility=0; |
275 | new_faces[i].magicmap=255; |
275 | new_faces[i].magicmap=255; |
276 | } |
276 | } |
277 | for (i = 0; i < nroffiles; i++) { |
277 | for (i = 0; i < nroffiles; i++) { |
278 | new_faces[xbm[i].number].name = xbm[i].name; |
278 | new_faces[xbm[i].number].name = xbm[i].name; |
279 | } |
279 | } |
280 | |
280 | |
281 | qsort (xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
281 | qsort (xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
282 | |
282 | |
283 | ReadFaceData(); |
283 | ReadFaceData(); |
284 | |
284 | |
285 | for (i = 0; i < nrofpixmaps; i++) { |
285 | for (i = 0; i < nrofpixmaps; i++) { |
286 | if (new_faces[i].magicmap==255) { |
286 | if (new_faces[i].magicmap==255) { |
287 | #if 0 /* Useful for initial debugging, not needed now */ |
287 | #if 0 /* Useful for initial debugging, not needed now */ |
288 | LOG(llevDebug,"Face %s still had default magicmap, resetting to black\n", |
288 | LOG(llevDebug,"Face %s still had default magicmap, resetting to black\n", |
289 | new_faces[i].name); |
289 | new_faces[i].name); |
290 | #endif |
290 | #endif |
291 | new_faces[i].magicmap=0; |
291 | new_faces[i].magicmap=0; |
292 | } |
292 | } |
293 | } |
293 | } |
294 | /* Actually forcefully setting the colors here probably should not |
294 | /* Actually forcefully setting the colors here probably should not |
295 | * be done - it could easily create confusion. |
295 | * be done - it could easily create confusion. |
296 | */ |
296 | */ |
297 | blank_face = &new_faces[FindFace(BLANK_FACE_NAME, 0)]; |
297 | blank_face = &new_faces[FindFace(BLANK_FACE_NAME, 0)]; |
… | |
… | |
322 | int FindFace (const char *name, int error) { |
322 | int FindFace (const char *name, int error) { |
323 | struct bmappair *bp, tmp; |
323 | struct bmappair *bp, tmp; |
324 | char *p; |
324 | char *p; |
325 | |
325 | |
326 | if ((p = strchr (name, '\n'))) |
326 | if ((p = strchr (name, '\n'))) |
327 | *p = '\0'; |
327 | *p = '\0'; |
328 | |
328 | |
329 | tmp.name = (char *)name; |
329 | tmp.name = (char *)name; |
330 | bp = (struct bmappair *)bsearch |
330 | bp = (struct bmappair *)bsearch |
331 | (&tmp, xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
331 | (&tmp, xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
332 | |
332 | |
333 | return bp ? bp->number : error; |
333 | return bp ? bp->number : error; |
334 | } |
334 | } |
335 | |
335 | |
336 | /* Reads the smooth file to know how to smooth datas. |
336 | /* Reads the smooth file to know how to smooth datas. |
… | |
… | |
347 | |
347 | |
348 | bmaps_checksum=0; |
348 | bmaps_checksum=0; |
349 | sprintf (buf,"%s/smooth", settings.datadir); |
349 | sprintf (buf,"%s/smooth", settings.datadir); |
350 | LOG(llevDebug,"Reading smooth from %s...",buf); |
350 | LOG(llevDebug,"Reading smooth from %s...",buf); |
351 | if ((fp=fopen(buf,"r"))==NULL) { |
351 | if ((fp=fopen(buf,"r"))==NULL) { |
352 | LOG(llevError, "Cannot open smooth file %s: %s\n", strerror_local(errno)); |
352 | LOG(llevError, "Cannot open smooth file %s: %s\n", strerror_local(errno)); |
353 | exit(-1); |
353 | exit(-1); |
354 | } |
354 | } |
355 | |
355 | |
356 | /* First count how many smooth we have, so we can allocate correctly */ |
356 | /* First count how many smooth we have, so we can allocate correctly */ |
357 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
357 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
358 | if(buf[0] != '#' && buf[0] != '\n' ) |
358 | if(buf[0] != '#' && buf[0] != '\n' ) |
359 | smoothcount++; |
359 | smoothcount++; |
360 | rewind(fp); |
360 | rewind(fp); |
361 | |
361 | |
362 | smooth = (struct smoothing *) malloc(sizeof(struct smoothing) * (smoothcount)); |
362 | smooth = (struct smoothing *) malloc(sizeof(struct smoothing) * (smoothcount)); |
363 | memset (smooth, 0, sizeof (struct smoothing) * (smoothcount)); |
363 | memset (smooth, 0, sizeof (struct smoothing) * (smoothcount)); |
364 | |
364 | |
… | |
… | |
409 | void free_all_images(void) |
409 | void free_all_images(void) |
410 | { |
410 | { |
411 | int i; |
411 | int i; |
412 | |
412 | |
413 | for (i=0; i<nroffiles; i++) |
413 | for (i=0; i<nroffiles; i++) |
414 | free(xbm[i].name); |
414 | free(xbm[i].name); |
415 | free(xbm); |
415 | free(xbm); |
416 | free(new_faces); |
416 | free(new_faces); |
417 | free(smooth); |
417 | free(smooth); |
418 | } |
418 | } |