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.24 by root, Fri Feb 2 00:42:03 2007 UTC

23 */ 23 */
24 24
25#include <global.h> 25#include <global.h>
26#include <unistd.h> 26#include <unistd.h>
27 27
28#include "loader.h"
29
30regionvec regions;
31
28region * 32region *
29region::default_region () 33region::default_region ()
30{ 34{
31 for (region *reg = first_region; reg; reg = reg->next) 35 for_all_regions (rgn)
32 if (reg->fallback) 36 if (rgn->fallback)
33 return reg; 37 return rgn;
34 38
35 return first_region; 39 return regions [0];
36} 40}
37 41
38/* 42/*
39 * Pass a char array, returns a pointer to the region of the same name. 43 * 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 44 * if it can't find a region of the same name it returns the first region
44 * used by the map parsing code. 48 * used by the map parsing code.
45 */ 49 */
46region * 50region *
47region::find (const char *name) 51region::find (const char *name)
48{ 52{
49 for (region *reg = first_region; reg; reg = reg->next) 53 for_all_regions (rgn)
50 if (!strcmp (reg->name, name)) 54 if (!strcmp (rgn->name, name))
51 return reg; 55 return rgn;
52 56
53 LOG (llevError, "region called %s requested, but not found, using fallback.\n", name); 57 LOG (llevError, "region called %s requested, but not found, using fallback.\n", name);
54 58
55 return default_region (); 59 return default_region ();
56} 60}
68 * 72 *
69 */ 73 */
70region * 74region *
71region::find_fuzzy (const char *name) 75region::find_fuzzy (const char *name)
72{ 76{
73 region *reg; 77 if (!name)
74 char *substr; 78 return default_region ();
75 char *p;
76 79
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'); 80 char *p = strchr (name, '\n');
86 if (p) 81 if (p)
87 *p = '\0'; 82 *p = '\0';
88 83
89 for (reg = first_region; reg; reg = reg->next) 84 for_all_regions (rgn)
90 if (!strcasecmp (reg->name, name)) 85 if (!strcasecmp (rgn->name, name))
91 return reg; 86 return rgn;
92 87
93 for (reg = first_region; reg; reg = reg->next) 88 for_all_regions (rgn)
94 if (reg->longname) 89 if (rgn->longname)
95 if (!strcasecmp (reg->longname, name)) 90 if (!strcasecmp (rgn->longname, name))
96 return reg; 91 return rgn;
97 92
98 substr = NULL; 93 for_all_regions (rgn)
99 for (reg = first_region; reg; reg = reg->next)
100 if (reg->longname) 94 if (rgn->longname)
101 { 95 {
102 substr = strstr (reg->longname, name); 96 if (strstr (rgn->longname, name))
103 if (substr)
104 return reg; 97 return rgn;
105 } 98 }
106 99
107 for (reg = first_region; reg; reg = reg->next) 100 for_all_regions (rgn)
108 if (reg->longname) 101 if (rgn->longname)
109 { 102 {
110 /* 103 /*
111 * This is not a bug, we want the region that is most identifiably a discrete 104 * 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 105 * 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 106 * 'scornarena', regardless of their order on the list so we only look at those
114 * regions with a longname set. 107 * regions with a longname set.
115 */ 108 */
116 substr = strstr (reg->name, name); 109 if (strstr (rgn->name, name))
117 if (substr)
118 return reg; 110 return rgn;
119 } 111 }
120 112
121 for (reg = first_region; reg; reg = reg->next) 113 for_all_regions (rgn)
122 { 114 {
123 substr = strstr (reg->name, name); 115 if (strstr (rgn->name, name))
124 if (substr)
125 return reg; 116 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 ; 117 }
131 118
132 return reg; 119 return default_region ();
133} 120}
134 121
135/* 122/*
136 * returns 1 if the player is in the region reg, or a child region thereof 123 * returns 1 if the player is in the region reg, or a child region thereof
137 * otherwise returns 0 124 * otherwise returns 0
189 } 176 }
190 else 177 else
191 reg = reg->parent; 178 reg = reg->parent;
192 } 179 }
193 180
194 LOG (llevDebug, "No suitable jailmap for region %s was found.\n", &reg->name); 181 LOG (llevError, "No suitable jailmap for region %s was found.\n", &reg->name);
182
195 return NULL; 183 return 0;
196} 184}
197 185
198static void 186region *loader_region::get_region (const char *name)
199assign_region_parents (void)
200{ 187{
201 region *reg; 188 region *rgn = new region;
202 uint32 parent_count = 0; 189 rgn->name = name;
203 uint32 region_count = 0; 190 return rgn;
191}
204 192
205 for (reg = first_region; reg && reg->next; reg = reg->next) 193void loader_region::put_region (region *rgn)
194{
195 for_all_regions (old)
196 if (old->name == rgn->name)
206 { 197 {
207 if (reg->parent_name) 198 // replace, copy new values (ugly)
199 rgn->index = old->index;
200 *old = *rgn;
201 delete rgn;
202
203 return;
204 }
205
206 // just append
207 regions.push_back (rgn);
208}
209
210bool
211loader_base::parse_region (object_thawer &thawer, region *rgn)
212{
213 for (;;)
214 {
215 keyword kw = thawer.get_kv ();
216
217 switch (kw)
208 { 218 {
209 reg->parent = region::find (reg->parent_name); 219 case KW_parent:
210 parent_count++; 220 rgn->parent = region::find (thawer.get_str ());
221 break;
222
223 case KW_longname:
224 thawer.get (rgn->longname);
225 break;
226
227 case KW_jail_map:
228 thawer.get (rgn->jailmap);
229 break;
230
231 case KW_jail_x:
232 thawer.get (rgn->jailx);
233 break;
234
235 case KW_jail_y:
236 thawer.get (rgn->jaily);
237 break;
238
239 case KW_msg:
240 thawer.get_ml (KW_endmsg, rgn->msg);
241 break;
242
243 case KW_fallback:
244 thawer.get (rgn->fallback);
245 break;
246
247 case KW_end:
248 return true;
249
250 default:
251 if (!thawer.parse_error (kw, "region", rgn->name))
252 return false;
253 break;
211 } 254 }
212
213 region_count++;
214 } 255 }
215
216 LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count);
217} 256}
218 257
219/* 258/*
220 * First initialises the archtype hash-table (init_archetable()). 259 * First initialises the archtype hash-table (init_archetable()).
221 * Reads and parses the archetype file (with the first and second-pass 260 * Reads and parses the archetype file (with the first and second-pass
223 * Then initialises treasures by calling load_treasures(). 262 * Then initialises treasures by calling load_treasures().
224 */ 263 */
225void 264void
226init_regions (void) 265init_regions (void)
227{ 266{
228 FILE *fp; 267 if (!regions.size ())
229 char filename[MAX_BUF]; 268 {
230 int comp;
231
232 if (first_region != NULL) /* Only do this once */
233 return;
234
235 // make sure one region is always available 269 // make sure one region is always available
236 first_region = new region; 270 region *rgn = new region;
237 first_region->name = "<builtin>"; 271 rgn->name = "<builtin>";
238 first_region->longname = strdup ("Built-in Region"); 272 rgn->longname = "Built-in Region";
239 273 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 { 274 }
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} 275}
254 276
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