ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/region.C
(Generate patch)

Comparing deliantra/server/common/region.C (file contents):
Revision 1.12 by root, Sat Dec 30 10:16:10 2006 UTC vs.
Revision 1.19 by root, Mon Jan 29 14:46:01 2007 UTC

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
28region *
29region::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 */
36region * 46region *
37get_region_by_name (const char *region_name) 47region::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 }
55 }
56 LOG (llevInfo, "Got no region or fallback for region %s.\n", region_name);
57 return NULL;
58}
59 54
60/* This might need optimising at some point. */ 55 return default_region ();
61region *
62get_region_by_map (maptile *m)
63{
64 return get_region_by_name (get_name_of_region_for_map (m));
65}
66
67/*
68 * 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
70 * the map code.
71 */
72
73const char *
74get_name_of_region_for_map (const maptile *m)
75{
76 region *reg;
77
78 if (m->region != NULL)
79 return m->region->name;
80
81 for (reg = first_region; reg != NULL; reg = reg->next)
82 {
83 if (reg->fallback)
84 return reg->name;
85 }
86
87 LOG (llevInfo, "map %s had no region and I couldn't find a fallback to use.\n", &m->name);
88 return "unknown";
89} 56}
90 57
91/* 58/*
92 * Tries to find a region that 'name' corresponds to. 59 * Tries to find a region that 'name' corresponds to.
93 * It looks, in order, for: 60 * It looks, in order, for:
99 * (there should be only one of these - the top level one) 66 * (there should be only one of these - the top level one)
100 * If we got a NULL, then just return the top level region 67 * If we got a NULL, then just return the top level region
101 * 68 *
102 */ 69 */
103region * 70region *
104get_region_from_string (const char *name) 71region::find_fuzzy (const char *name)
105{ 72{
106 region *reg; 73 region *reg;
107 char *substr; 74 char *substr;
108 char *p; 75 char *p;
109 76
110 if (name == NULL) 77 if (name == NULL)
111 { 78 {
112 for (reg = first_region; reg->parent != NULL; reg = reg->parent); 79 for (reg = first_region; reg->parent; reg = reg->parent)
80 ;
81
113 return reg; 82 return reg;
114 } 83 }
84
115 p = strchr (name, '\n'); 85 p = strchr (name, '\n');
116 if (p) 86 if (p)
117 *p = '\0'; 87 *p = '\0';
88
118 for (reg = first_region; reg != NULL; reg = reg->next) 89 for (reg = first_region; reg; reg = reg->next)
119 if (!strcasecmp (reg->name, name)) 90 if (!strcasecmp (reg->name, name))
120 return reg; 91 return reg;
121 92
122 for (reg = first_region; reg != NULL; reg = reg->next) 93 for (reg = first_region; reg; reg = reg->next)
123 if (reg->longname != NULL) 94 if (reg->longname)
95 if (!strcasecmp (reg->longname, name))
96 return reg;
97
98 substr = NULL;
99 for (reg = first_region; reg; reg = reg->next)
100 if (reg->longname)
124 { 101 {
125 if (!strcasecmp (reg->longname, name)) 102 substr = strstr (reg->longname, name);
103 if (substr)
126 return reg; 104 return reg;
127 } 105 }
128 106
129 substr = NULL;
130 for (reg = first_region; reg != NULL; reg = reg->next) 107 for (reg = first_region; reg; reg = reg->next)
131 if (reg->longname != NULL) 108 if (reg->longname)
132 {
133 substr = strstr (reg->longname, name);
134 if (substr != NULL)
135 return reg;
136 }
137 for (reg = first_region; reg != NULL; reg = reg->next)
138 if (reg->longname != NULL)
139 { 109 {
140 /* 110 /*
141 * This is not a bug, we want the region that is most identifiably a discrete 111 * This is not a bug, we want the region that is most identifiably a discrete
142 * area in the game, eg if we have 'scor', we want to return 'scorn' and not 112 * area in the game, eg if we have 'scor', we want to return 'scorn' and not
143 * 'scornarena', regardless of their order on the list so we only look at those 113 * 'scornarena', regardless of their order on the list so we only look at those
144 * regions with a longname set. 114 * regions with a longname set.
145 */ 115 */
146 substr = strstr (reg->name, name); 116 substr = strstr (reg->name, name);
147 if (substr != NULL) 117 if (substr)
148 return reg; 118 return reg;
149 } 119 }
120
150 for (reg = first_region; reg != NULL; reg = reg->next) 121 for (reg = first_region; reg; reg = reg->next)
151 { 122 {
152 substr = strstr (reg->name, name); 123 substr = strstr (reg->name, name);
153 if (substr != NULL) 124 if (substr)
154 return reg; 125 return reg;
155 } 126 }
127
156 /* if we are still here, we are going to have to give up, and give the top level region */ 128 /* if we are still here, we are going to have to give up, and give the top level region */
157 for (reg = first_region; reg->parent != NULL; reg = reg->parent); 129 for (reg = first_region; reg->parent; reg = reg->parent)
130 ;
131
158 return reg; 132 return reg;
159} 133}
160 134
161/* 135/*
162 * returns 1 if the player is in the region reg, or a child region thereof 136 * returns 1 if the player is in the region reg, or a child region thereof
163 * otherwise returns 0 137 * otherwise returns 0
164 * if passed a NULL region returns -1 138 * if passed a NULL region returns -1
165 */ 139 */
166 140
167int 141static int
168region_is_child_of_region (const region * child, const region * r) 142region_is_child_of_region (const region * child, const region * r)
169{ 143{
170 144
171 if (r == NULL) 145 if (r == NULL)
172 return -1; 146 return -1;
147
173 if (child == NULL) 148 if (child == NULL)
174 return 0; 149 return 0;
150
175 if (!strcmp (child->name, r->name)) 151 if (!strcmp (child->name, r->name))
176 return 1; 152 return 1;
153
177 else if (child->parent != NULL) 154 else if (child->parent != NULL)
178 return region_is_child_of_region (child->parent, r); 155 return region_is_child_of_region (child->parent, r);
179 else 156 else
180 return 0; 157 return 0;
181} 158}
182 159
183/*
184 * the longname of a region is not a required field, any given region
185 * may want to not set it and use the parent's one instead. so, we:
186 * 1. check if a longname is set and if so return it.
187 * 2. check if there is a parent and try and call the function against that
188 * 3. return a obviously wrong string if we can't get a longname, this should
189 * never happen. We also log a debug message.
190 */
191const char *
192get_region_longname (const region * r)
193{
194
195 if (r->longname != NULL)
196 return r->longname;
197 else if (r->parent != NULL)
198 return get_region_longname (r->parent);
199 else
200 {
201 LOG (llevDebug, "NOTICE region %s has no parent and no longname.\n", r->name);
202 return "no name can be found for the current region";
203 }
204}
205
206const char *
207get_region_msg (const region * r)
208{
209 if (r->msg != NULL)
210 return r->msg;
211 else if (r->parent != NULL)
212 return get_region_msg (r->parent);
213 else
214 {
215 LOG (llevDebug, "NOTICE region %s has no parent and no msg.\n", r->name);
216 return "no description can be found for the current region";
217 }
218}
219
220/** Returns an object which is an exit through which the player represented by op should be 160/** Returns an object which is an exit through which the player represented by op should be
221 * sent in order to be imprisoned. If there is no suitable place to which an exit can be 161 * sent in order to be imprisoned. If there is no suitable place to which an exit can be
222 * constructed, then NULL will be returned. The caller is responsible for freeing the object 162 * constructed, then NULL will be returned. The caller is responsible for freeing the object
223 * created by this function. 163 * created by this function.
224 */ 164 */
231 if (op->type != PLAYER) 171 if (op->type != PLAYER)
232 { 172 {
233 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n"); 173 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n");
234 return NULL; 174 return NULL;
235 } 175 }
236 reg = get_region_by_map (op->map); 176
177 reg = op->region ();
237 while (reg != NULL) 178 while (reg)
238 { 179 {
239 if (reg->jailmap) 180 if (reg->jailmap)
240 { 181 {
241 exit = object::create (); 182 exit = object::create ();
242 EXIT_PATH (exit) = reg->jailmap; 183 EXIT_PATH (exit) = reg->jailmap;
247 return exit; 188 return exit;
248 } 189 }
249 else 190 else
250 reg = reg->parent; 191 reg = reg->parent;
251 } 192 }
193
252 LOG (llevDebug, "No suitable jailmap for region %s was found.\n", reg->name); 194 LOG (llevDebug, "No suitable jailmap for region %s was found.\n", &reg->name);
253 return NULL; 195 return NULL;
196}
197
198static void
199assign_region_parents (void)
200{
201 region *reg;
202 uint32 parent_count = 0;
203 uint32 region_count = 0;
204
205 for (reg = first_region; reg && reg->next; reg = reg->next)
206 {
207 if (reg->parent_name)
208 {
209 reg->parent = region::find (reg->parent_name);
210 parent_count++;
211 }
212
213 region_count++;
214 }
215
216 LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count);
254} 217}
255 218
256/* 219/*
257 * First initialises the archtype hash-table (init_archetable()). 220 * First initialises the archtype hash-table (init_archetable()).
258 * Reads and parses the archetype file (with the first and second-pass 221 * Reads and parses the archetype file (with the first and second-pass
267 int comp; 230 int comp;
268 231
269 if (first_region != NULL) /* Only do this once */ 232 if (first_region != NULL) /* Only do this once */
270 return; 233 return;
271 234
235 // make sure one region is always available
236 first_region = new region;
237 first_region->name = "<builtin>";
238 first_region->longname = strdup ("Built-in Region");
239
272 sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions); 240 sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions);
273 LOG (llevDebug, "Reading regions from %s...\n", filename); 241 LOG (llevDebug, "Reading regions from %s...\n", filename);
274 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) 242 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
275 { 243 {
276 LOG (llevError, " Can't open regions file %s in init_regions.\n", filename); 244 LOG (llevError, " Can't open regions file %s in init_regions.\n", filename);
277 return; 245 return;
278 } 246 }
247
279 parse_regions (fp); 248 parse_regions (fp);
280 assign_region_parents (); 249 assign_region_parents ();
281 LOG (llevDebug, " done\n"); 250 LOG (llevDebug, " done\n");
282 251
283 close_and_delete (fp, comp); 252 close_and_delete (fp, comp);
284}
285
286/*
287 * Allocates and zeros a region struct, this isn't free()'d anywhere, so might
288 * be a memory leak, but it shouldn't matter too much since it isn't called that
289 * often....
290 */
291
292region *
293get_region_struct (void)
294{
295 return new region;
296} 253}
297 254
298/* 255/*
299 * Reads/parses the region file, and copies into a linked list 256 * Reads/parses the region file, and copies into a linked list
300 * of region structs. 257 * of region structs.
346 * through. 303 * through.
347 */ 304 */
348 if (!strcmp (key, "region")) 305 if (!strcmp (key, "region"))
349 { 306 {
350 *end = 0; 307 *end = 0;
351 newreg = get_region_struct (); 308 newreg = new region;
352 newreg->name = strdup (value); 309 newreg->name = value;
353 } 310 }
354 else if (!strcmp (key, "parent")) 311 else if (!strcmp (key, "parent"))
355 { 312 {
356 /* 313 /*
357 * Note that this is in the initialisation code, so we don't actually 314 * Note that this is in the initialisation code, so we don't actually
358 * assign the pointer to the parent yet, because it might not have been 315 * assign the pointer to the parent yet, because it might not have been
359 * parsed. 316 * parsed.
360 */ 317 */
361 *end = 0; 318 *end = 0;
362 newreg->parent_name = strdup (value); 319 newreg->parent_name = value;
363 } 320 }
364 else if (!strcmp (key, "longname")) 321 else if (!strcmp (key, "longname"))
365 { 322 {
366 *end = 0; 323 *end = 0;
367 newreg->longname = strdup (value); 324 newreg->longname = strdup (value);
433 } 390 }
434 if (!key || strcmp (key, "nomore")) 391 if (!key || strcmp (key, "nomore"))
435 LOG (llevError, "Got premature eof on regions file!\n"); 392 LOG (llevError, "Got premature eof on regions file!\n");
436} 393}
437 394
438void
439assign_region_parents (void)
440{
441 region *reg;
442 uint32 parent_count = 0;
443 uint32 region_count = 0;
444
445 for (reg = first_region; reg != NULL && reg->next != NULL; reg = reg->next)
446 {
447 if (reg->parent_name != NULL)
448 {
449 reg->parent = get_region_by_name (reg->parent_name);
450 parent_count++;
451 }
452 region_count++;
453 }
454 LOG (llevDebug, "Assigned %u regions with %u parents.", region_count, parent_count);
455}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines