1 | /* |
1 | /* |
2 | CrossFire, A Multiplayer game for X-windows |
2 | * CrossFire, A Multiplayer game for X-windows |
3 | |
3 | * |
|
|
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team |
5 | * Copyright (C) 2001-2003 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 authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | */ |
23 | */ |
23 | |
|
|
24 | |
24 | |
25 | #include <global.h> |
25 | #include <global.h> |
26 | #include <unistd.h> |
26 | #include <unistd.h> |
|
|
27 | |
|
|
28 | region * |
|
|
29 | region::default_region () |
|
|
30 | { |
|
|
31 | for (region *reg = first_region; reg; reg = reg->next) |
|
|
32 | if (reg->fallback) |
|
|
33 | return reg; |
|
|
34 | |
|
|
35 | return first_region; |
|
|
36 | } |
27 | |
37 | |
28 | /* |
38 | /* |
29 | * Pass a char array, returns a pointer to the region of the same name. |
39 | * Pass a char array, returns a pointer to the region of the same name. |
30 | * if it can't find a region of the same name it returns the first region |
40 | * if it can't find a region of the same name it returns the first region |
31 | * with the 'fallback' property set. |
41 | * with the 'fallback' property set. |
32 | * if it can't find a matching name /or/ a fallback region it logs an info message |
42 | * if it can't find a matching name /or/ a fallback region it logs an info message |
33 | * message and returns NULL |
43 | * message and returns NULL |
34 | * used by the map parsing code. |
44 | * used by the map parsing code. |
35 | */ |
45 | */ |
36 | region * |
46 | region * |
37 | get_region_by_name (const char *region_name) |
47 | region::find (const char *name) |
38 | { |
48 | { |
39 | region *reg; |
|
|
40 | char *p = strchr (region_name, '\n'); |
|
|
41 | |
|
|
42 | if (p) |
|
|
43 | *p = '\0'; |
|
|
44 | for (reg = first_region; reg != NULL; reg = reg->next) |
49 | for (region *reg = first_region; reg; reg = reg->next) |
45 | if (!strcmp (reg->name, region_name)) |
50 | if (!strcmp (reg->name, name)) |
46 | return reg; |
51 | return reg; |
47 | |
52 | |
48 | for (reg = first_region; reg != NULL; reg = reg->next) |
|
|
49 | { |
|
|
50 | if (reg->fallback) |
|
|
51 | { |
|
|
52 | LOG (llevDebug, "region called %s requested, but not found, fallback used.\n", region_name); |
53 | LOG (llevError, "region called %s requested, but not found, using fallback.\n", name); |
53 | return reg; |
54 | |
54 | } |
55 | return default_region (); |
55 | } |
|
|
56 | LOG (llevInfo, "Got no region or fallback for region %s.\n", region_name); |
|
|
57 | return NULL; |
|
|
58 | } |
56 | } |
59 | |
57 | |
60 | /* This might need optimising at some point. */ |
58 | /* This might need optimising at some point. */ |
61 | region * |
59 | region * |
62 | get_region_by_map (maptile *m) |
60 | get_region_by_map (maptile *m) |
63 | { |
61 | { |
64 | return get_region_by_name (get_name_of_region_for_map (m)); |
62 | return region::find (get_name_of_region_for_map (m)); |
65 | } |
63 | } |
66 | |
64 | |
67 | /* |
65 | /* |
68 | * 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 |
69 | * 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 |
… | |
… | |
73 | const char * |
71 | const char * |
74 | get_name_of_region_for_map (const maptile *m) |
72 | get_name_of_region_for_map (const maptile *m) |
75 | { |
73 | { |
76 | region *reg; |
74 | region *reg; |
77 | |
75 | |
78 | if (m->region != NULL) |
76 | if (m->default_region) |
79 | return m->region->name; |
77 | return m->default_region->name; |
|
|
78 | |
80 | for (reg = first_region; reg != NULL; reg = reg->next) |
79 | for (reg = first_region; reg; reg = reg->next) |
81 | { |
80 | { |
82 | if (reg->fallback) |
81 | if (reg->fallback) |
83 | return reg->name; |
82 | return reg->name; |
84 | } |
83 | } |
|
|
84 | |
85 | LOG (llevInfo, "map %s had no region and I couldn't find a fallback to use.\n", m->name); |
85 | LOG (llevInfo, "map %s had no region and I couldn't find a fallback to use.\n", &m->name); |
86 | return "unknown"; |
86 | return "unknown"; |
87 | } |
87 | } |
88 | |
88 | |
89 | /* |
89 | /* |
90 | * Tries to find a region that 'name' corresponds to. |
90 | * Tries to find a region that 'name' corresponds to. |
… | |
… | |
234 | reg = get_region_by_map (op->map); |
234 | reg = get_region_by_map (op->map); |
235 | while (reg != NULL) |
235 | while (reg != NULL) |
236 | { |
236 | { |
237 | if (reg->jailmap) |
237 | if (reg->jailmap) |
238 | { |
238 | { |
239 | exit = get_object (); |
239 | exit = object::create (); |
240 | EXIT_PATH (exit) = reg->jailmap; |
240 | EXIT_PATH (exit) = reg->jailmap; |
241 | /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ |
241 | /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ |
242 | SET_FLAG (exit, FLAG_DAMNED); |
242 | SET_FLAG (exit, FLAG_DAMNED); |
243 | EXIT_X (exit) = reg->jailx; |
243 | EXIT_X (exit) = reg->jailx; |
244 | EXIT_Y (exit) = reg->jaily; |
244 | EXIT_Y (exit) = reg->jaily; |
… | |
… | |
288 | */ |
288 | */ |
289 | |
289 | |
290 | region * |
290 | region * |
291 | get_region_struct (void) |
291 | get_region_struct (void) |
292 | { |
292 | { |
293 | |
|
|
294 | region *reg; |
|
|
295 | |
|
|
296 | reg = (region *) CALLOC (1, sizeof (region)); |
|
|
297 | if (reg == NULL) |
|
|
298 | fatal (OUT_OF_MEMORY); |
|
|
299 | |
|
|
300 | memset (reg, '\0', sizeof (region)); |
|
|
301 | |
|
|
302 | return reg; |
293 | return new region; |
303 | } |
294 | } |
304 | |
295 | |
305 | /* |
296 | /* |
306 | * Reads/parses the region file, and copies into a linked list |
297 | * Reads/parses the region file, and copies into a linked list |
307 | * of region structs. |
298 | * of region structs. |
… | |
… | |
354 | */ |
345 | */ |
355 | if (!strcmp (key, "region")) |
346 | if (!strcmp (key, "region")) |
356 | { |
347 | { |
357 | *end = 0; |
348 | *end = 0; |
358 | newreg = get_region_struct (); |
349 | newreg = get_region_struct (); |
359 | newreg->name = strdup_local (value); |
350 | newreg->name = strdup (value); |
360 | } |
351 | } |
361 | else if (!strcmp (key, "parent")) |
352 | else if (!strcmp (key, "parent")) |
362 | { |
353 | { |
363 | /* |
354 | /* |
364 | * Note that this is in the initialisation code, so we don't actually |
355 | * Note that this is in the initialisation code, so we don't actually |
365 | * assign the pointer to the parent yet, because it might not have been |
356 | * assign the pointer to the parent yet, because it might not have been |
366 | * parsed. |
357 | * parsed. |
367 | */ |
358 | */ |
368 | *end = 0; |
359 | *end = 0; |
369 | newreg->parent_name = strdup_local (value); |
360 | newreg->parent_name = strdup (value); |
370 | } |
361 | } |
371 | else if (!strcmp (key, "longname")) |
362 | else if (!strcmp (key, "longname")) |
372 | { |
363 | { |
373 | *end = 0; |
364 | *end = 0; |
374 | newreg->longname = strdup_local (value); |
365 | newreg->longname = strdup (value); |
375 | } |
366 | } |
376 | else if (!strcmp (key, "jail")) |
367 | else if (!strcmp (key, "jail")) |
377 | { |
368 | { |
378 | /* jail entries are of the form: /path/to/map x y */ |
369 | /* jail entries are of the form: /path/to/map x y */ |
379 | char path[MAX_BUF]; |
370 | char path[MAX_BUF]; |
… | |
… | |
382 | if (sscanf (value, "%[^ ] %d %d\n", path, &x, &y) != 3) |
373 | if (sscanf (value, "%[^ ] %d %d\n", path, &x, &y) != 3) |
383 | { |
374 | { |
384 | LOG (llevError, "region.c: malformated regions entry: jail %s\n", value); |
375 | LOG (llevError, "region.c: malformated regions entry: jail %s\n", value); |
385 | continue; |
376 | continue; |
386 | } |
377 | } |
387 | newreg->jailmap = strdup_local (path); |
378 | newreg->jailmap = strdup (path); |
388 | newreg->jailx = x; |
379 | newreg->jailx = x; |
389 | newreg->jaily = y; |
380 | newreg->jaily = y; |
390 | } |
381 | } |
391 | else if (!strcmp (key, "msg")) |
382 | else if (!strcmp (key, "msg")) |
392 | { |
383 | { |
… | |
… | |
404 | * There may be regions with empty messages (eg, msg/endmsg |
395 | * There may be regions with empty messages (eg, msg/endmsg |
405 | * with nothing between). When maps are loaded, this is done |
396 | * with nothing between). When maps are loaded, this is done |
406 | * so better do it here too... |
397 | * so better do it here too... |
407 | */ |
398 | */ |
408 | if (msgpos != 0) |
399 | if (msgpos != 0) |
409 | newreg->msg = strdup_local (msgbuf); |
400 | newreg->msg = strdup (msgbuf); |
410 | |
401 | |
411 | /* we have to reset msgpos, or the next region will store both msg blocks. */ |
402 | /* we have to reset msgpos, or the next region will store both msg blocks. */ |
412 | msgpos = 0; |
403 | msgpos = 0; |
413 | } |
404 | } |
414 | else if (!strcmp (key, "fallback")) |
405 | else if (!strcmp (key, "fallback")) |
… | |
… | |
451 | |
442 | |
452 | for (reg = first_region; reg != NULL && reg->next != NULL; reg = reg->next) |
443 | for (reg = first_region; reg != NULL && reg->next != NULL; reg = reg->next) |
453 | { |
444 | { |
454 | if (reg->parent_name != NULL) |
445 | if (reg->parent_name != NULL) |
455 | { |
446 | { |
456 | reg->parent = get_region_by_name (reg->parent_name); |
447 | reg->parent = region::find (reg->parent_name); |
457 | parent_count++; |
448 | parent_count++; |
458 | } |
449 | } |
|
|
450 | |
459 | region_count++; |
451 | region_count++; |
460 | } |
452 | } |
|
|
453 | |
461 | LOG (llevDebug, "Assigned %u regions with %u parents.", region_count, parent_count); |
454 | LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count); |
462 | } |
455 | } |