1 | /* |
1 | /* |
2 | * static char *rcsid_init_c = |
2 | * static char *rcsid_init_c = |
3 | * "$Id: init.C,v 1.1 2006/08/13 17:16:00 elmex Exp $"; |
3 | * "$Id: init.C,v 1.5 2006/09/04 11:07:59 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 | |
… | |
… | |
141 | int online=0; |
141 | int online=0; |
142 | |
142 | |
143 | /* If this file doesn't exist, not a big deal */ |
143 | /* If this file doesn't exist, not a big deal */ |
144 | sprintf(filename,"%s/%s/.emergency",settings.datadir, settings.mapdir); |
144 | sprintf(filename,"%s/%s/.emergency",settings.datadir, settings.mapdir); |
145 | if ((fp = fopen(filename, "r"))!=NULL) { |
145 | if ((fp = fopen(filename, "r"))!=NULL) { |
146 | while (fgets(tmpbuf, MAX_BUF-1, fp)) { |
146 | while (fgets(tmpbuf, MAX_BUF-1, fp)) { |
147 | if (tmpbuf[0] == '#') continue; /* ignore comments */ |
147 | if (tmpbuf[0] == '#') continue; /* ignore comments */ |
148 | |
148 | |
149 | if (online == 0) { |
149 | if (online == 0) { |
150 | tmpbuf[strlen(tmpbuf)-1] = 0; /* kill newline */ |
150 | tmpbuf[strlen(tmpbuf)-1] = 0; /* kill newline */ |
151 | settings.emergency_mapname = strdup_local(tmpbuf); |
151 | settings.emergency_mapname = strdup_local(tmpbuf); |
152 | } |
152 | } |
153 | else if (online == 1) { |
153 | else if (online == 1) { |
154 | settings.emergency_x = atoi(tmpbuf); |
154 | settings.emergency_x = atoi(tmpbuf); |
155 | } |
155 | } |
156 | |
156 | |
157 | else if (online == 2) { |
157 | else if (online == 2) { |
158 | settings.emergency_y = atoi(tmpbuf); |
158 | settings.emergency_y = atoi(tmpbuf); |
159 | } |
159 | } |
160 | online++; |
160 | online++; |
161 | if (online>2) break; |
161 | if (online>2) break; |
162 | } |
162 | } |
163 | fclose(fp); |
163 | fclose(fp); |
164 | if (online<=2) |
164 | if (online<=2) |
165 | LOG(llevError,"Online read partial data from %s\n", filename); |
165 | LOG(llevError,"Online read partial data from %s\n", filename); |
166 | LOG(llevDebug,"Emergency mappath reset to %s (%d, %d)\n", settings.emergency_mapname, |
166 | LOG(llevDebug,"Emergency mappath reset to %s (%d, %d)\n", settings.emergency_mapname, |
167 | settings.emergency_x, settings.emergency_y); |
167 | settings.emergency_x, settings.emergency_y); |
168 | } |
168 | } |
169 | } |
169 | } |
170 | |
170 | |
171 | |
171 | |
172 | /* |
172 | /* |
… | |
… | |
179 | */ |
179 | */ |
180 | |
180 | |
181 | void init_library(void) { |
181 | void init_library(void) { |
182 | init_environ(); |
182 | init_environ(); |
183 | init_globals(); |
183 | init_globals(); |
184 | init_hash_table(); |
|
|
185 | init_objects(); |
184 | init_objects(); |
186 | init_vars(); |
185 | init_vars(); |
187 | init_block(); |
186 | init_block(); |
188 | ReadBmapNames (); |
187 | ReadBmapNames (); |
189 | ReadSmooth(); |
188 | ReadSmooth(); |
… | |
… | |
230 | * Might use environment-variables as default for some of them. |
229 | * Might use environment-variables as default for some of them. |
231 | */ |
230 | */ |
232 | |
231 | |
233 | void init_globals(void) { |
232 | void init_globals(void) { |
234 | if (settings.logfilename[0] == 0) { |
233 | if (settings.logfilename[0] == 0) { |
235 | logfile = stderr; |
234 | logfile = stderr; |
236 | } |
235 | } |
237 | else if ((logfile=fopen(settings.logfilename, "a"))==NULL) { |
236 | else if ((logfile=fopen(settings.logfilename, "a"))==NULL) { |
238 | fprintf(stderr,"Unable to open %s as the logfile - will use stderr instead\n", |
237 | fprintf(stderr,"Unable to open %s as the logfile - will use stderr instead\n", |
239 | settings.logfilename); |
238 | settings.logfilename); |
240 | logfile = stderr; |
239 | logfile = stderr; |
241 | } |
240 | } |
242 | else { |
241 | else { |
243 | setvbuf(logfile, NULL, _IOLBF, 0); |
242 | setvbuf(logfile, NULL, _IOLBF, 0); |
244 | } |
243 | } |
245 | exiting = 0; |
244 | exiting = 0; |
246 | first_player=NULL; |
245 | first_player=NULL; |
247 | first_friendly_object=NULL; |
246 | first_friendly_object=NULL; |
248 | first_map=NULL; |
247 | first_map=NULL; |
… | |
… | |
255 | nrofartifacts = 0; |
254 | nrofartifacts = 0; |
256 | nrofallowedstr=0; |
255 | nrofallowedstr=0; |
257 | ring_arch = NULL; |
256 | ring_arch = NULL; |
258 | amulet_arch = NULL; |
257 | amulet_arch = NULL; |
259 | staff_arch = NULL; |
258 | staff_arch = NULL; |
260 | undead_name = add_string("undead"); |
|
|
261 | trying_emergency_save = 0; |
259 | trying_emergency_save = 0; |
262 | num_animations=0; |
260 | num_animations=0; |
263 | animations=NULL; |
|
|
264 | animations_allocated=0; |
261 | animations_allocated=0; |
265 | init_defaults(); |
262 | init_defaults(); |
266 | } |
263 | } |
267 | |
264 | |
268 | /* |
265 | /* |
… | |
… | |
274 | void init_objects(void) { |
271 | void init_objects(void) { |
275 | int i; |
272 | int i; |
276 | /* Initialize all objects: */ |
273 | /* Initialize all objects: */ |
277 | objects=NULL; |
274 | objects=NULL; |
278 | active_objects = NULL; |
275 | active_objects = NULL; |
279 | |
|
|
280 | #ifdef MEMORY_DEBUG |
|
|
281 | free_objects=NULL; |
|
|
282 | #else |
|
|
283 | free_objects=objarray; |
|
|
284 | objarray[0].prev=NULL, |
|
|
285 | objarray[0].next= &objarray[1], |
|
|
286 | SET_FLAG(&objarray[0], FLAG_REMOVED); |
|
|
287 | SET_FLAG(&objarray[0], FLAG_FREED); |
|
|
288 | for(i=1;i<STARTMAX-1;i++) { |
|
|
289 | objarray[i].next= &objarray[i+1]; |
|
|
290 | objarray[i].prev= &objarray[i-1]; |
|
|
291 | SET_FLAG(&objarray[i], FLAG_REMOVED); |
|
|
292 | SET_FLAG(&objarray[i], FLAG_FREED); |
|
|
293 | } |
|
|
294 | objarray[STARTMAX-1].next=NULL; |
|
|
295 | objarray[STARTMAX-1].prev= &objarray[STARTMAX-2]; |
|
|
296 | SET_FLAG(&objarray[STARTMAX-1], FLAG_REMOVED); |
|
|
297 | SET_FLAG(&objarray[STARTMAX-1], FLAG_FREED); |
|
|
298 | #endif |
|
|
299 | } |
276 | } |
300 | |
277 | |
301 | /* |
278 | /* |
302 | * Initialises global variables which can be changed by options. |
279 | * Initialises global variables which can be changed by options. |
303 | * Called by init_library(). |
280 | * Called by init_library(). |
… | |
… | |
310 | |
287 | |
311 | |
288 | |
312 | void init_dynamic (void) { |
289 | void init_dynamic (void) { |
313 | archetype *at = first_archetype; |
290 | archetype *at = first_archetype; |
314 | while (at) { |
291 | while (at) { |
315 | if (at->clone.type == MAP) { |
292 | if (at->clone.type == MAP) { |
316 | if (at->clone.race) { |
293 | if (at->clone.race) { |
317 | strcpy (first_map_ext_path, at->clone.race); |
294 | strcpy (first_map_ext_path, at->clone.race); |
318 | } |
295 | } |
319 | if (EXIT_PATH (&at->clone)) { |
296 | if (EXIT_PATH (&at->clone)) { |
320 | strcpy (first_map_path, EXIT_PATH (&at->clone)); |
297 | strcpy (first_map_path, EXIT_PATH (&at->clone)); |
321 | return; |
298 | return; |
322 | } |
299 | } |
323 | } |
300 | } |
324 | at = at->next; |
301 | at = at->next; |
325 | } |
302 | } |
326 | LOG(llevDebug,"You Need a archetype called 'map' and it have to contain start map\n"); |
303 | LOG(llevDebug,"You Need a archetype called 'map' and it have to contain start map\n"); |
327 | exit (-1); |
304 | exit (-1); |
328 | } |
305 | } |
329 | |
306 | |
… | |
… | |
339 | char filename[MAX_BUF]; |
316 | char filename[MAX_BUF]; |
340 | FILE *fp; |
317 | FILE *fp; |
341 | |
318 | |
342 | sprintf(filename, "%s/clockdata", settings.localdir); |
319 | sprintf(filename, "%s/clockdata", settings.localdir); |
343 | if ((fp = fopen(filename, "w")) == NULL) { |
320 | if ((fp = fopen(filename, "w")) == NULL) { |
344 | LOG(llevError, "Cannot open %s for writing\n", filename); |
321 | LOG(llevError, "Cannot open %s for writing\n", filename); |
345 | return; |
322 | return; |
346 | } |
323 | } |
347 | fprintf(fp, "%lu", todtick); |
324 | fprintf(fp, "%lu", todtick); |
348 | fclose(fp); |
325 | fclose(fp); |
349 | } |
326 | } |
350 | |
327 | |
… | |
… | |
366 | |
343 | |
367 | sprintf(filename, "%s/clockdata", settings.localdir); |
344 | sprintf(filename, "%s/clockdata", settings.localdir); |
368 | LOG(llevDebug, "Reading clockdata from %s...", filename); |
345 | LOG(llevDebug, "Reading clockdata from %s...", filename); |
369 | if ((fp = fopen(filename, "r")) == NULL) { |
346 | if ((fp = fopen(filename, "r")) == NULL) { |
370 | LOG(llevError, "Can't open %s.\n", filename); |
347 | LOG(llevError, "Can't open %s.\n", filename); |
371 | todtick = 0; |
348 | todtick = 0; |
372 | write_todclock(); |
349 | write_todclock(); |
373 | return; |
350 | return; |
374 | } |
351 | } |
375 | fscanf(fp, "%lu", &todtick); |
352 | fscanf(fp, "%lu", &todtick); |
376 | LOG(llevDebug, "todtick=%lu\n", todtick); |
353 | LOG(llevDebug, "todtick=%lu\n", todtick); |
377 | fclose(fp); |
354 | fclose(fp); |
378 | } |
355 | } |
… | |
… | |
400 | |
377 | |
401 | sprintf(filename, "%s/attackmess", settings.datadir); |
378 | sprintf(filename, "%s/attackmess", settings.datadir); |
402 | LOG(llevDebug, "Reading attack messages from %s...", filename); |
379 | LOG(llevDebug, "Reading attack messages from %s...", filename); |
403 | if ((fp = open_and_uncompress(filename, 0, &comp)) == NULL) { |
380 | if ((fp = open_and_uncompress(filename, 0, &comp)) == NULL) { |
404 | LOG(llevError, "Can't open %s.\n", filename); |
381 | LOG(llevError, "Can't open %s.\n", filename); |
405 | return; |
382 | return; |
406 | } |
383 | } |
407 | |
384 | |
408 | level = 0; |
385 | level = 0; |
409 | while (fgets(buf, MAX_BUF, fp)!=NULL) { |
386 | while (fgets(buf, MAX_BUF, fp)!=NULL) { |
410 | if (*buf=='#') continue; |
387 | if (*buf=='#') continue; |
411 | if((cp=strchr(buf,'\n'))!=NULL) |
388 | if((cp=strchr(buf,'\n'))!=NULL) |
412 | *cp='\0'; |
389 | *cp='\0'; |
413 | cp=buf; |
390 | cp=buf; |
414 | while(*cp==' ') /* Skip blanks */ |
391 | while(*cp==' ') /* Skip blanks */ |
415 | cp++; |
392 | cp++; |
416 | |
393 | |
417 | if (strncmp(cp, "TYPE:", 5)==0) { |
394 | if (strncmp(cp, "TYPE:", 5)==0) { |
418 | p = strtok(buf, ":"); |
395 | p = strtok(buf, ":"); |
419 | p = strtok(NULL, ":"); |
396 | p = strtok(NULL, ":"); |
420 | if (mode == 1) { |
397 | if (mode == 1) { |
421 | attack_mess[mess][level].level = -1; |
398 | attack_mess[mess][level].level = -1; |
422 | attack_mess[mess][level].buf1 = NULL; |
399 | attack_mess[mess][level].buf1 = NULL; |
423 | attack_mess[mess][level].buf2 = NULL; |
400 | attack_mess[mess][level].buf2 = NULL; |
424 | attack_mess[mess][level].buf3 = NULL; |
401 | attack_mess[mess][level].buf3 = NULL; |
425 | } |
402 | } |
426 | level = 0; |
403 | level = 0; |
427 | mess = atoi(p); |
404 | mess = atoi(p); |
428 | mode = 1; |
405 | mode = 1; |
429 | continue; |
406 | continue; |
430 | } |
407 | } |
431 | if (mode==1) { |
408 | if (mode==1) { |
432 | p = strtok(buf, "="); |
409 | p = strtok(buf, "="); |
433 | attack_mess[mess][level].level = atoi(buf); |
410 | attack_mess[mess][level].level = atoi(buf); |
434 | p = strtok(NULL, "="); |
411 | p = strtok(NULL, "="); |
435 | if (p != NULL) |
412 | if (p != NULL) |
436 | attack_mess[mess][level].buf1 = strdup_local(p); |
413 | attack_mess[mess][level].buf1 = strdup_local(p); |
437 | else |
414 | else |
438 | attack_mess[mess][level].buf1 = strdup_local(""); |
415 | attack_mess[mess][level].buf1 = strdup_local(""); |
439 | mode = 2; |
416 | mode = 2; |
440 | continue; |
417 | continue; |
441 | } else if (mode==2) { |
418 | } else if (mode==2) { |
442 | p = strtok(buf, "="); |
419 | p = strtok(buf, "="); |
443 | attack_mess[mess][level].level = atoi(buf); |
420 | attack_mess[mess][level].level = atoi(buf); |
444 | p = strtok(NULL, "="); |
421 | p = strtok(NULL, "="); |
445 | if (p != NULL) |
422 | if (p != NULL) |
446 | attack_mess[mess][level].buf2 = strdup_local(p); |
423 | attack_mess[mess][level].buf2 = strdup_local(p); |
447 | else |
424 | else |
448 | attack_mess[mess][level].buf2 = strdup_local(""); |
425 | attack_mess[mess][level].buf2 = strdup_local(""); |
449 | mode = 3; |
426 | mode = 3; |
450 | continue; |
427 | continue; |
451 | } else if (mode==3) { |
428 | } else if (mode==3) { |
452 | p = strtok(buf, "="); |
429 | p = strtok(buf, "="); |
453 | attack_mess[mess][level].level = atoi(buf); |
430 | attack_mess[mess][level].level = atoi(buf); |
454 | p = strtok(NULL, "="); |
431 | p = strtok(NULL, "="); |
455 | if (p != NULL) |
432 | if (p != NULL) |
456 | attack_mess[mess][level].buf3 = strdup_local(p); |
433 | attack_mess[mess][level].buf3 = strdup_local(p); |
457 | else |
434 | else |
458 | attack_mess[mess][level].buf3 = strdup_local(""); |
435 | attack_mess[mess][level].buf3 = strdup_local(""); |
459 | mode = 1; |
436 | mode = 1; |
460 | level++; |
437 | level++; |
461 | total++; |
438 | total++; |
462 | continue; |
439 | continue; |
463 | } |
440 | } |
464 | } |
441 | } |
465 | LOG(llevDebug, "got %d messages in %d categories.\n", total, mess+1); |
442 | LOG(llevDebug, "got %d messages in %d categories.\n", total, mess+1); |
466 | close_and_delete(fp, comp); |
443 | close_and_delete(fp, comp); |
467 | } |
444 | } |