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.18 by root, Sat Jan 27 23:59:29 2007 UTC vs.
Revision 1.19 by root, Mon Jan 29 14:46:01 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines