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) |
76 | if (m->default_region) |
79 | return m->region->name; |
77 | return m->default_region->name; |
80 | |
78 | |
81 | for (reg = first_region; reg != NULL; reg = reg->next) |
79 | for (reg = first_region; reg; reg = reg->next) |
82 | { |
80 | { |
83 | if (reg->fallback) |
81 | if (reg->fallback) |
84 | return reg->name; |
82 | return reg->name; |
85 | } |
83 | } |
86 | |
84 | |
… | |
… | |
444 | |
442 | |
445 | 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) |
446 | { |
444 | { |
447 | if (reg->parent_name != NULL) |
445 | if (reg->parent_name != NULL) |
448 | { |
446 | { |
449 | reg->parent = get_region_by_name (reg->parent_name); |
447 | reg->parent = region::find (reg->parent_name); |
450 | parent_count++; |
448 | parent_count++; |
451 | } |
449 | } |
|
|
450 | |
452 | region_count++; |
451 | region_count++; |
453 | } |
452 | } |
|
|
453 | |
454 | LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count); |
454 | LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count); |
455 | } |
455 | } |