1 | /* |
1 | /* |
2 | * static char *rcsid_map_c = |
2 | * static char *rcsid_map_c = |
3 | * "$Id: region.C,v 1.1 2006/08/13 17:16:00 elmex Exp $"; |
3 | * "$Id: region.C,v 1.2 2006/08/29 08:01:36 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 | |
… | |
… | |
43 | region *get_region_by_name(const char *region_name) { |
43 | region *get_region_by_name(const char *region_name) { |
44 | region *reg; |
44 | region *reg; |
45 | char *p = strchr(region_name, '\n'); |
45 | char *p = strchr(region_name, '\n'); |
46 | if (p) *p = '\0'; |
46 | if (p) *p = '\0'; |
47 | for (reg=first_region;reg!=NULL;reg=reg->next) |
47 | for (reg=first_region;reg!=NULL;reg=reg->next) |
48 | if (!strcmp(reg->name, region_name)) return reg; |
48 | if (!strcmp(reg->name, region_name)) return reg; |
49 | |
49 | |
50 | for (reg=first_region;reg!=NULL;reg=reg->next) { |
50 | for (reg=first_region;reg!=NULL;reg=reg->next) { |
51 | if (reg->fallback) { |
51 | if (reg->fallback) { |
52 | LOG(llevDebug,"region called %s requested, but not found, fallback used.\n", region_name); |
52 | LOG(llevDebug,"region called %s requested, but not found, fallback used.\n", region_name); |
53 | return reg; |
53 | return reg; |
54 | } |
54 | } |
55 | } |
55 | } |
56 | LOG(llevInfo,"Got no region or fallback for region %s.\n", region_name); |
56 | LOG(llevInfo,"Got no region or fallback for region %s.\n", region_name); |
57 | return NULL; |
57 | return NULL; |
58 | } |
58 | } |
59 | |
59 | |
60 | /* This might need optimising at some point. */ |
60 | /* This might need optimising at some point. */ |
61 | region *get_region_by_map(mapstruct *m) { |
61 | region *get_region_by_map(mapstruct *m) { |
62 | return get_region_by_name(get_name_of_region_for_map(m)); |
62 | return get_region_by_name(get_name_of_region_for_map(m)); |
63 | } |
63 | } |
64 | |
64 | |
65 | /* |
65 | /* |
66 | * Since we won't assume all maps have a region set properly, we need an |
66 | * Since we won't assume all maps have a region set properly, we need an |
67 | * explicit check that it is, this is much nicer here than scattered throughout |
67 | * explicit check that it is, this is much nicer here than scattered throughout |
68 | * the map code. |
68 | * the map code. |
69 | */ |
69 | */ |
70 | |
70 | |
71 | const char *get_name_of_region_for_map(const mapstruct *m) { |
71 | const char *get_name_of_region_for_map(const mapstruct *m) { |
72 | region *reg; |
72 | region *reg; |
73 | if (m->region!=NULL) return m->region->name; |
73 | if (m->region!=NULL) return m->region->name; |
74 | for (reg=first_region;reg!=NULL;reg=reg->next) { |
74 | for (reg=first_region;reg!=NULL;reg=reg->next) { |
75 | if (reg->fallback) return reg->name; |
75 | if (reg->fallback) return reg->name; |
76 | } |
76 | } |
77 | LOG(llevInfo,"map %s had no region and I couldn't find a fallback to use.\n", m->name); |
77 | LOG(llevInfo,"map %s had no region and I couldn't find a fallback to use.\n", m->name); |
78 | return "unknown"; |
78 | return "unknown"; |
79 | } |
79 | } |
80 | |
80 | |
81 | /* |
81 | /* |
82 | * Tries to find a region that 'name' corresponds to. |
82 | * Tries to find a region that 'name' corresponds to. |
83 | * It looks, in order, for: |
83 | * It looks, in order, for: |
… | |
… | |
100 | return reg; |
100 | return reg; |
101 | } |
101 | } |
102 | p = strchr(name, '\n'); |
102 | p = strchr(name, '\n'); |
103 | if (p) *p = '\0'; |
103 | if (p) *p = '\0'; |
104 | for (reg=first_region;reg!=NULL;reg=reg->next) |
104 | for (reg=first_region;reg!=NULL;reg=reg->next) |
105 | if (!strcasecmp(reg->name, name)) return reg; |
105 | if (!strcasecmp(reg->name, name)) return reg; |
106 | |
106 | |
107 | for (reg=first_region;reg!=NULL;reg=reg->next) |
107 | for (reg=first_region;reg!=NULL;reg=reg->next) |
108 | if (reg->longname != NULL) { |
108 | if (reg->longname != NULL) { |
109 | if (!strcasecmp(reg->longname, name)) return reg; |
109 | if (!strcasecmp(reg->longname, name)) return reg; |
110 | } |
110 | } |
111 | |
111 | |
112 | substr=NULL; |
112 | substr=NULL; |
113 | for (reg=first_region;reg!=NULL;reg=reg->next) |
113 | for (reg=first_region;reg!=NULL;reg=reg->next) |
114 | if (reg->longname != NULL) { |
114 | if (reg->longname != NULL) { |
115 | substr=strstr(reg->longname, name); |
115 | substr=strstr(reg->longname, name); |
116 | if (substr != NULL) return reg; |
116 | if (substr != NULL) return reg; |
117 | } |
117 | } |
118 | for (reg=first_region;reg!=NULL;reg=reg->next) |
118 | for (reg=first_region;reg!=NULL;reg=reg->next) |
119 | if (reg->longname != NULL) { |
119 | if (reg->longname != NULL) { |
120 | /* |
120 | /* |
121 | * This is not a bug, we want the region that is most identifiably a discrete |
121 | * This is not a bug, we want the region that is most identifiably a discrete |
122 | * area in the game, eg if we have 'scor', we want to return 'scorn' and not |
122 | * area in the game, eg if we have 'scor', we want to return 'scorn' and not |
123 | * 'scornarena', regardless of their order on the list so we only look at those |
123 | * 'scornarena', regardless of their order on the list so we only look at those |
124 | * regions with a longname set. |
124 | * regions with a longname set. |
125 | */ |
125 | */ |
126 | substr=strstr(reg->name, name); |
126 | substr=strstr(reg->name, name); |
127 | if (substr != NULL) return reg; |
127 | if (substr != NULL) return reg; |
128 | } |
128 | } |
129 | for (reg=first_region;reg!=NULL;reg=reg->next) { |
129 | for (reg=first_region;reg!=NULL;reg=reg->next) { |
130 | substr=strstr(reg->name, name); |
130 | substr=strstr(reg->name, name); |
131 | if (substr != NULL) return reg; |
131 | if (substr != NULL) return reg; |
132 | } |
132 | } |
133 | /* if we are still here, we are going to have to give up, and give the top level region */ |
133 | /* if we are still here, we are going to have to give up, and give the top level region */ |
134 | for (reg=first_region;reg->parent!=NULL;reg=reg->parent); |
134 | for (reg=first_region;reg->parent!=NULL;reg=reg->parent); |
135 | return reg; |
135 | return reg; |
136 | } |
136 | } |
… | |
… | |
144 | int region_is_child_of_region(const region *child, const region *r) { |
144 | int region_is_child_of_region(const region *child, const region *r) { |
145 | |
145 | |
146 | if (r==NULL) |
146 | if (r==NULL) |
147 | return -1; |
147 | return -1; |
148 | if (child == NULL) |
148 | if (child == NULL) |
149 | return 0; |
149 | return 0; |
150 | if (!strcmp(child->name, r->name)) |
150 | if (!strcmp(child->name, r->name)) |
151 | return 1; |
151 | return 1; |
152 | else if(child->parent!=NULL) |
152 | else if(child->parent!=NULL) |
153 | return region_is_child_of_region(child->parent,r); |
153 | return region_is_child_of_region(child->parent,r); |
154 | else return 0; |
154 | else return 0; |
155 | } |
155 | } |
156 | |
156 | |
… | |
… | |
192 | */ |
192 | */ |
193 | object *get_jail_exit(object *op) { |
193 | object *get_jail_exit(object *op) { |
194 | region *reg; |
194 | region *reg; |
195 | object *exit; |
195 | object *exit; |
196 | if (op->type != PLAYER) { |
196 | if (op->type != PLAYER) { |
197 | LOG(llevError, "region.c: get_jail_exit called against non-player object.\n"); |
197 | LOG(llevError, "region.c: get_jail_exit called against non-player object.\n"); |
198 | return NULL; |
198 | return NULL; |
199 | } |
199 | } |
200 | reg=get_region_by_map(op->map); |
200 | reg=get_region_by_map(op->map); |
201 | while (reg!=NULL) { |
201 | while (reg!=NULL) { |
202 | if (reg->jailmap) { |
202 | if (reg->jailmap) { |
203 | exit=get_object(); |
203 | exit=get_object(); |
204 | EXIT_PATH(exit)=add_string(reg->jailmap); |
204 | EXIT_PATH(exit)=add_string(reg->jailmap); |
205 | /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ |
205 | /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ |
206 | SET_FLAG(exit, FLAG_DAMNED); |
206 | SET_FLAG(exit, FLAG_DAMNED); |
207 | EXIT_X(exit) = reg->jailx; |
207 | EXIT_X(exit) = reg->jailx; |
208 | EXIT_Y(exit) = reg->jaily; |
208 | EXIT_Y(exit) = reg->jaily; |
209 | return exit; |
209 | return exit; |
210 | } |
210 | } |
211 | else reg=reg->parent; |
211 | else reg=reg->parent; |
212 | } |
212 | } |
213 | LOG(llevDebug,"No suitable jailmap for region %s was found.\n", reg->name); |
213 | LOG(llevDebug,"No suitable jailmap for region %s was found.\n", reg->name); |
214 | return NULL; |
214 | return NULL; |
215 | } |
215 | } |
216 | |
216 | |
… | |
… | |
229 | return; |
229 | return; |
230 | |
230 | |
231 | sprintf(filename,"%s/%s/%s",settings.datadir,settings.mapdir,settings.regions); |
231 | sprintf(filename,"%s/%s/%s",settings.datadir,settings.mapdir,settings.regions); |
232 | LOG(llevDebug,"Reading regions from %s...\n",filename); |
232 | LOG(llevDebug,"Reading regions from %s...\n",filename); |
233 | if((fp=open_and_uncompress(filename,0,&comp))==NULL) { |
233 | if((fp=open_and_uncompress(filename,0,&comp))==NULL) { |
234 | LOG(llevError," Can't open regions file %s in init_regions.\n", filename); |
234 | LOG(llevError," Can't open regions file %s in init_regions.\n", filename); |
235 | return; |
235 | return; |
236 | } |
236 | } |
237 | parse_regions(fp); |
237 | parse_regions(fp); |
238 | assign_region_parents(); |
238 | assign_region_parents(); |
239 | LOG(llevDebug," done\n"); |
239 | LOG(llevDebug," done\n"); |
240 | |
240 | |
241 | close_and_delete(fp, comp); |
241 | close_and_delete(fp, comp); |
242 | } |
242 | } |
243 | |
243 | |
244 | /* |
244 | /* |
… | |
… | |
271 | char buf[HUGE_BUF], msgbuf[HUGE_BUF], *key=NULL, *value, *end; |
271 | char buf[HUGE_BUF], msgbuf[HUGE_BUF], *key=NULL, *value, *end; |
272 | int msgpos=0; |
272 | int msgpos=0; |
273 | |
273 | |
274 | newreg = NULL; |
274 | newreg = NULL; |
275 | while (fgets(buf, HUGE_BUF-1, fp)!=NULL) { |
275 | while (fgets(buf, HUGE_BUF-1, fp)!=NULL) { |
276 | buf[HUGE_BUF-1] = 0; |
276 | buf[HUGE_BUF-1] = 0; |
277 | key = buf; |
277 | key = buf; |
278 | while (isspace(*key)) key++; |
278 | while (isspace(*key)) key++; |
279 | if (*key == 0) continue; /* empty line */ |
279 | if (*key == 0) continue; /* empty line */ |
280 | value = strchr(key, ' '); |
280 | value = strchr(key, ' '); |
281 | if (!value) { |
281 | if (!value) { |
282 | end = strchr(key, '\n'); |
282 | end = strchr(key, '\n'); |
283 | *end=0; |
283 | *end=0; |
284 | } else { |
284 | } else { |
285 | *value = 0; |
285 | *value = 0; |
286 | value++; |
286 | value++; |
287 | while (isspace(*value)) value++; |
287 | while (isspace(*value)) value++; |
288 | end = strchr(value, '\n'); |
288 | end = strchr(value, '\n'); |
289 | } |
289 | } |
290 | |
290 | |
291 | /* |
291 | /* |
292 | * This is a bizzare mutated form of the map and archetype parser |
292 | * This is a bizzare mutated form of the map and archetype parser |
293 | * rolled into one. Key is the field name, value is what it should |
293 | * rolled into one. Key is the field name, value is what it should |
294 | * be set to. |
294 | * be set to. |
295 | * We've already done the work to null terminate key, |
295 | * We've already done the work to null terminate key, |
296 | * and strip off any leading spaces for both of these. |
296 | * and strip off any leading spaces for both of these. |
297 | * We have not touched the newline at the end of the line - |
297 | * We have not touched the newline at the end of the line - |
298 | * these might be needed for some values. the end pointer |
298 | * these might be needed for some values. the end pointer |
299 | * points to the first of the newlines. |
299 | * points to the first of the newlines. |
300 | * value could be NULL! It would be easy enough to just point |
300 | * value could be NULL! It would be easy enough to just point |
301 | * this to "" to prevent cores, but that would let more errors slide |
301 | * this to "" to prevent cores, but that would let more errors slide |
302 | * through. |
302 | * through. |
303 | */ |
303 | */ |
304 | if (!strcmp(key,"region")) { |
304 | if (!strcmp(key,"region")) { |
305 | *end=0; |
305 | *end=0; |
306 | newreg=get_region_struct(); |
306 | newreg=get_region_struct(); |
307 | newreg->name = strdup_local(value); |
307 | newreg->name = strdup_local(value); |
308 | } |
308 | } |
309 | else if (!strcmp(key,"parent")) { |
309 | else if (!strcmp(key,"parent")) { |
310 | /* |
310 | /* |
311 | * Note that this is in the initialisation code, so we don't actually |
311 | * Note that this is in the initialisation code, so we don't actually |
312 | * assign the pointer to the parent yet, because it might not have been |
312 | * assign the pointer to the parent yet, because it might not have been |
313 | * parsed. |
313 | * parsed. |
314 | */ |
314 | */ |
315 | *end=0; |
315 | *end=0; |
316 | newreg->parent_name = strdup_local(value); |
316 | newreg->parent_name = strdup_local(value); |
317 | } |
317 | } |
318 | else if (!strcmp(key,"longname")) { |
318 | else if (!strcmp(key,"longname")) { |
319 | *end=0; |
319 | *end=0; |
320 | newreg->longname = strdup_local(value); |
320 | newreg->longname = strdup_local(value); |
321 | } |
321 | } |
322 | else if (!strcmp(key,"jail")) { |
322 | else if (!strcmp(key,"jail")) { |
323 | /* jail entries are of the form: /path/to/map x y */ |
323 | /* jail entries are of the form: /path/to/map x y */ |
324 | char path[MAX_BUF]; |
324 | char path[MAX_BUF]; |
325 | int x,y; |
325 | int x,y; |
326 | if (sscanf(value, "%[^ ] %d %d\n", path, &x, &y) != 3) { |
326 | if (sscanf(value, "%[^ ] %d %d\n", path, &x, &y) != 3) { |
327 | LOG(llevError, "region.c: malformated regions entry: jail %s\n", value); |
327 | LOG(llevError, "region.c: malformated regions entry: jail %s\n", value); |
328 | continue; |
328 | continue; |
329 | } |
329 | } |
330 | newreg->jailmap = strdup_local(path); |
330 | newreg->jailmap = strdup_local(path); |
331 | newreg->jailx = x; |
331 | newreg->jailx = x; |
332 | newreg->jaily = y; |
332 | newreg->jaily = y; |
333 | } |
333 | } |
334 | else if (!strcmp(key,"msg")) { |
334 | else if (!strcmp(key,"msg")) { |
335 | while (fgets(buf, HUGE_BUF-1, fp)!=NULL) { |
335 | while (fgets(buf, HUGE_BUF-1, fp)!=NULL) { |
336 | if (!strcmp(buf,"endmsg\n")) break; |
336 | if (!strcmp(buf,"endmsg\n")) break; |
337 | else { |
337 | else { |
338 | strcpy(msgbuf+msgpos, buf); |
338 | strcpy(msgbuf+msgpos, buf); |
339 | msgpos += strlen(buf); |
339 | msgpos += strlen(buf); |
340 | } |
340 | } |
341 | } |
341 | } |
342 | /* |
342 | /* |
343 | * There may be regions with empty messages (eg, msg/endmsg |
343 | * There may be regions with empty messages (eg, msg/endmsg |
344 | * with nothing between). When maps are loaded, this is done |
344 | * with nothing between). When maps are loaded, this is done |
345 | * so better do it here too... |
345 | * so better do it here too... |
346 | */ |
346 | */ |
347 | if (msgpos != 0) |
347 | if (msgpos != 0) |
348 | newreg->msg = strdup_local(msgbuf); |
348 | newreg->msg = strdup_local(msgbuf); |
349 | |
349 | |
350 | /* we have to reset msgpos, or the next region will store both msg blocks.*/ |
350 | /* we have to reset msgpos, or the next region will store both msg blocks.*/ |
351 | msgpos=0; |
351 | msgpos=0; |
352 | } |
352 | } |
353 | else if (!strcmp(key,"fallback")) { |
353 | else if (!strcmp(key,"fallback")) { |
354 | *end=0; |
354 | *end=0; |
355 | newreg->fallback = atoi(value); |
355 | newreg->fallback = atoi(value); |
356 | } |
356 | } |
357 | else if (!strcmp(key,"end")) { |
357 | else if (!strcmp(key,"end")) { |
358 | /* Place this new region last on the list, if the list is empty put it first */ |
358 | /* Place this new region last on the list, if the list is empty put it first */ |
359 | for (reg=first_region;reg!=NULL&®->next!=NULL;reg=reg->next); |
359 | for (reg=first_region;reg!=NULL&®->next!=NULL;reg=reg->next); |
360 | |
360 | |
361 | if (reg==NULL) first_region=newreg; |
361 | if (reg==NULL) first_region=newreg; |
362 | else reg->next=newreg; |
362 | else reg->next=newreg; |
363 | newreg = NULL; |
363 | newreg = NULL; |
364 | } |
364 | } |
365 | else if (!strcmp(key,"nomore")) { |
365 | else if (!strcmp(key,"nomore")) { |
366 | /* we have reached the end of the region specs....*/ |
366 | /* we have reached the end of the region specs....*/ |
367 | break; |
367 | break; |
368 | } |
368 | } |
369 | else { |
369 | else { |
370 | /* we should never get here, if we have, then something is wrong */ |
370 | /* we should never get here, if we have, then something is wrong */ |
371 | LOG(llevError, "Got unknown value in region file: %s %s\n", key, value); |
371 | LOG(llevError, "Got unknown value in region file: %s %s\n", key, value); |
372 | } |
372 | } |
373 | } |
373 | } |
374 | if (!key || strcmp(key,"nomore")) |
374 | if (!key || strcmp(key,"nomore")) |
375 | LOG(llevError, "Got premature eof on regions file!\n"); |
375 | LOG(llevError, "Got premature eof on regions file!\n"); |
376 | } |
376 | } |
377 | |
377 | |
378 | void assign_region_parents(void) { |
378 | void assign_region_parents(void) { |
379 | region *reg; |
379 | region *reg; |
380 | uint32 parent_count=0; |
380 | uint32 parent_count=0; |
381 | uint32 region_count=0; |
381 | uint32 region_count=0; |
382 | for (reg=first_region;reg!=NULL&®->next!=NULL;reg=reg->next) { |
382 | for (reg=first_region;reg!=NULL&®->next!=NULL;reg=reg->next) { |
383 | if (reg->parent_name!=NULL) { |
383 | if (reg->parent_name!=NULL) { |
384 | reg->parent=get_region_by_name(reg->parent_name); |
384 | reg->parent=get_region_by_name(reg->parent_name); |
385 | parent_count++; |
385 | parent_count++; |
386 | } |
386 | } |
387 | region_count++; |
387 | region_count++; |
388 | } |
388 | } |
389 | LOG(llevDebug, "Assigned %u regions with %u parents.", region_count, parent_count); |
389 | LOG(llevDebug, "Assigned %u regions with %u parents.", region_count, parent_count); |
390 | } |
390 | } |