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.19 by root, Mon Jan 29 14:46:01 2007 UTC vs.
Revision 1.26 by root, Fri Feb 16 19:43:41 2007 UTC

23 */ 23 */
24 24
25#include <global.h> 25#include <global.h>
26#include <unistd.h> 26#include <unistd.h>
27 27
28regionvec regions;
29
28region * 30region *
29region::default_region () 31region::default_region ()
30{ 32{
31 for (region *reg = first_region; reg; reg = reg->next) 33 for_all_regions (rgn)
32 if (reg->fallback) 34 if (rgn->fallback)
33 return reg; 35 return rgn;
34 36
35 return first_region; 37 return regions [0];
36} 38}
37 39
38/* 40/*
39 * Pass a char array, returns a pointer to the region of the same name. 41 * Pass a char array, returns a pointer to the region of the same name.
40 * if it can't find a region of the same name it returns the first region 42 * if it can't find a region of the same name it returns the first region
44 * used by the map parsing code. 46 * used by the map parsing code.
45 */ 47 */
46region * 48region *
47region::find (const char *name) 49region::find (const char *name)
48{ 50{
49 for (region *reg = first_region; reg; reg = reg->next) 51 for_all_regions (rgn)
50 if (!strcmp (reg->name, name)) 52 if (!strcmp (rgn->name, name))
51 return reg; 53 return rgn;
52 54
53 LOG (llevError, "region called %s requested, but not found, using fallback.\n", name); 55 LOG (llevError, "region called %s requested, but not found, using fallback.\n", name);
54 56
55 return default_region (); 57 return default_region ();
56} 58}
68 * 70 *
69 */ 71 */
70region * 72region *
71region::find_fuzzy (const char *name) 73region::find_fuzzy (const char *name)
72{ 74{
73 region *reg; 75 if (!name)
74 char *substr; 76 return default_region ();
75 char *p;
76 77
77 if (name == NULL)
78 {
79 for (reg = first_region; reg->parent; reg = reg->parent)
80 ;
81
82 return reg;
83 }
84
85 p = strchr (name, '\n'); 78 char *p = strchr (name, '\n');
86 if (p) 79 if (p)
87 *p = '\0'; 80 *p = '\0';
88 81
89 for (reg = first_region; reg; reg = reg->next) 82 for_all_regions (rgn)
90 if (!strcasecmp (reg->name, name)) 83 if (!strcasecmp (rgn->name, name))
91 return reg; 84 return rgn;
92 85
93 for (reg = first_region; reg; reg = reg->next) 86 for_all_regions (rgn)
94 if (reg->longname) 87 if (rgn->longname)
95 if (!strcasecmp (reg->longname, name)) 88 if (!strcasecmp (rgn->longname, name))
96 return reg; 89 return rgn;
97 90
98 substr = NULL; 91 for_all_regions (rgn)
99 for (reg = first_region; reg; reg = reg->next)
100 if (reg->longname) 92 if (rgn->longname)
101 { 93 {
102 substr = strstr (reg->longname, name); 94 if (strstr (rgn->longname, name))
103 if (substr)
104 return reg; 95 return rgn;
105 } 96 }
106 97
107 for (reg = first_region; reg; reg = reg->next) 98 for_all_regions (rgn)
108 if (reg->longname) 99 if (rgn->longname)
109 { 100 {
110 /* 101 /*
111 * This is not a bug, we want the region that is most identifiably a discrete 102 * This is not a bug, we want the region that is most identifiably a discrete
112 * area in the game, eg if we have 'scor', we want to return 'scorn' and not 103 * area in the game, eg if we have 'scor', we want to return 'scorn' and not
113 * 'scornarena', regardless of their order on the list so we only look at those 104 * 'scornarena', regardless of their order on the list so we only look at those
114 * regions with a longname set. 105 * regions with a longname set.
115 */ 106 */
116 substr = strstr (reg->name, name); 107 if (strstr (rgn->name, name))
117 if (substr)
118 return reg; 108 return rgn;
119 } 109 }
120 110
121 for (reg = first_region; reg; reg = reg->next) 111 for_all_regions (rgn)
122 { 112 {
123 substr = strstr (reg->name, name); 113 if (strstr (rgn->name, name))
124 if (substr)
125 return reg; 114 return rgn;
126 }
127
128 /* if we are still here, we are going to have to give up, and give the top level region */
129 for (reg = first_region; reg->parent; reg = reg->parent)
130 ; 115 }
131 116
132 return reg; 117 return default_region ();
133} 118}
134 119
135/* 120/*
136 * returns 1 if the player is in the region reg, or a child region thereof 121 * returns 1 if the player is in the region reg, or a child region thereof
137 * otherwise returns 0 122 * otherwise returns 0
189 } 174 }
190 else 175 else
191 reg = reg->parent; 176 reg = reg->parent;
192 } 177 }
193 178
194 LOG (llevDebug, "No suitable jailmap for region %s was found.\n", &reg->name); 179 LOG (llevError, "No suitable jailmap for region %s was found.\n", &reg->name);
180
195 return NULL; 181 return 0;
196} 182}
197 183
198static void 184region *
199assign_region_parents (void) 185region::read (object_thawer &f)
200{ 186{
201 region *reg; 187 assert (f.kw == KW_region);
202 uint32 parent_count = 0;
203 uint32 region_count = 0;
204 188
205 for (reg = first_region; reg && reg->next; reg = reg->next) 189 region *rgn = new region;
190 f.get (rgn->name);
191 f.next ();
192
193 for (;;)
206 { 194 {
207 if (reg->parent_name) 195 switch (f.kw)
208 { 196 {
197 case KW_parent:
209 reg->parent = region::find (reg->parent_name); 198 rgn->parent = region::find (f.get_str ());
210 parent_count++; 199 break;
200
201 case KW_longname:
202 f.get (rgn->longname);
203 break;
204
205 case KW_jail_map:
206 f.get (rgn->jailmap);
207 break;
208
209 case KW_jail_x:
210 f.get (rgn->jailx);
211 break;
212
213 case KW_jail_y:
214 f.get (rgn->jaily);
215 break;
216
217 case KW_msg:
218 f.get_ml (KW_endmsg, rgn->msg);
219 break;
220
221 case KW_fallback:
222 f.get (rgn->fallback);
223 break;
224
225 case KW_end:
226 for_all_regions (old)
227 if (old->name == rgn->name)
228 {
229 // replace, copy new values (ugly)
230 rgn->index = old->index;
231 *old = *rgn;
232 delete rgn;
233
234 return old;
235 }
236
237 // just append
238 regions.push_back (rgn);
239 return rgn;
240
241 default:
242 if (!f.parse_error ("region", rgn->name))
243 {
244 delete rgn;
245 return 0;
246 }
247 break;
211 } 248 }
212 249
213 region_count++; 250 f.next ();
214 } 251 }
215
216 LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count);
217} 252}
218 253
219/* 254/*
220 * First initialises the archtype hash-table (init_archetable()). 255 * First initialises the archtype hash-table (init_archetable()).
221 * Reads and parses the archetype file (with the first and second-pass 256 * Reads and parses the archetype file (with the first and second-pass
223 * Then initialises treasures by calling load_treasures(). 258 * Then initialises treasures by calling load_treasures().
224 */ 259 */
225void 260void
226init_regions (void) 261init_regions (void)
227{ 262{
228 FILE *fp; 263 if (!regions.size ())
229 char filename[MAX_BUF]; 264 {
230 int comp;
231
232 if (first_region != NULL) /* Only do this once */
233 return;
234
235 // make sure one region is always available 265 // make sure one region is always available
236 first_region = new region; 266 region *rgn = new region;
237 first_region->name = "<builtin>"; 267 rgn->name = "<builtin>";
238 first_region->longname = strdup ("Built-in Region"); 268 rgn->longname = "Built-in Region";
239 269 regions.push_back (rgn);
240 sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions);
241 LOG (llevDebug, "Reading regions from %s...\n", filename);
242 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
243 { 270 }
244 LOG (llevError, " Can't open regions file %s in init_regions.\n", filename);
245 return;
246 }
247
248 parse_regions (fp);
249 assign_region_parents ();
250 LOG (llevDebug, " done\n");
251
252 close_and_delete (fp, comp);
253} 271}
254 272
255/*
256 * Reads/parses the region file, and copies into a linked list
257 * of region structs.
258 */
259void
260parse_regions (FILE * fp)
261{
262 region *newreg;
263 region *reg;
264
265 char buf[HUGE_BUF], msgbuf[HUGE_BUF], *key = NULL, *value, *end;
266 int msgpos = 0;
267
268 newreg = NULL;
269 while (fgets (buf, HUGE_BUF - 1, fp) != NULL)
270 {
271 buf[HUGE_BUF - 1] = 0;
272 key = buf;
273 while (isspace (*key))
274 key++;
275 if (*key == 0)
276 continue; /* empty line */
277 value = strchr (key, ' ');
278 if (!value)
279 {
280 end = strchr (key, '\n');
281 *end = 0;
282 }
283 else
284 {
285 *value = 0;
286 value++;
287 while (isspace (*value))
288 value++;
289 end = strchr (value, '\n');
290 }
291
292 /*
293 * This is a bizzare mutated form of the map and archetype parser
294 * rolled into one. Key is the field name, value is what it should
295 * be set to.
296 * We've already done the work to null terminate key,
297 * and strip off any leading spaces for both of these.
298 * We have not touched the newline at the end of the line -
299 * these might be needed for some values. the end pointer
300 * points to the first of the newlines.
301 * value could be NULL! It would be easy enough to just point
302 * this to "" to prevent cores, but that would let more errors slide
303 * through.
304 */
305 if (!strcmp (key, "region"))
306 {
307 *end = 0;
308 newreg = new region;
309 newreg->name = value;
310 }
311 else if (!strcmp (key, "parent"))
312 {
313 /*
314 * Note that this is in the initialisation code, so we don't actually
315 * assign the pointer to the parent yet, because it might not have been
316 * parsed.
317 */
318 *end = 0;
319 newreg->parent_name = value;
320 }
321 else if (!strcmp (key, "longname"))
322 {
323 *end = 0;
324 newreg->longname = strdup (value);
325 }
326 else if (!strcmp (key, "jail"))
327 {
328 /* jail entries are of the form: /path/to/map x y */
329 char path[MAX_BUF];
330 int x, y;
331
332 if (sscanf (value, "%[^ ] %d %d\n", path, &x, &y) != 3)
333 {
334 LOG (llevError, "region.c: malformated regions entry: jail %s\n", value);
335 continue;
336 }
337 newreg->jailmap = strdup (path);
338 newreg->jailx = x;
339 newreg->jaily = y;
340 }
341 else if (!strcmp (key, "msg"))
342 {
343 while (fgets (buf, HUGE_BUF - 1, fp) != NULL)
344 {
345 if (!strcmp (buf, "endmsg\n"))
346 break;
347 else
348 {
349 strcpy (msgbuf + msgpos, buf);
350 msgpos += strlen (buf);
351 }
352 }
353 /*
354 * There may be regions with empty messages (eg, msg/endmsg
355 * with nothing between). When maps are loaded, this is done
356 * so better do it here too...
357 */
358 if (msgpos != 0)
359 newreg->msg = strdup (msgbuf);
360
361 /* we have to reset msgpos, or the next region will store both msg blocks. */
362 msgpos = 0;
363 }
364 else if (!strcmp (key, "fallback"))
365 {
366 *end = 0;
367 newreg->fallback = atoi (value);
368 }
369 else if (!strcmp (key, "end"))
370 {
371 /* Place this new region last on the list, if the list is empty put it first */
372 for (reg = first_region; reg != NULL && reg->next != NULL; reg = reg->next);
373
374 if (reg == NULL)
375 first_region = newreg;
376 else
377 reg->next = newreg;
378 newreg = NULL;
379 }
380 else if (!strcmp (key, "nomore"))
381 {
382 /* we have reached the end of the region specs.... */
383 break;
384 }
385 else
386 {
387 /* we should never get here, if we have, then something is wrong */
388 LOG (llevError, "Got unknown value in region file: %s %s\n", key, value);
389 }
390 }
391 if (!key || strcmp (key, "nomore"))
392 LOG (llevError, "Got premature eof on regions file!\n");
393}
394

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines