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.20 by root, Mon Jan 29 15:04:21 2007 UTC vs.
Revision 1.21 by root, Thu Feb 1 19:15:38 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)
206 { 194{
207 if (reg->parent_name) 195 for_all_regions (old)
196 if (old->name == rgn->name)
208 { 197 {
209 reg->parent = region::find (reg->parent_name); 198 // replace, copy new values (ugly)
210 parent_count++; 199 rgn->index = old->index;
200 *old = *rgn;
201 delete rgn;
202
203 return;
211 } 204 }
212 205
213 region_count++; 206 // just append
214 } 207 regions.push_back (rgn);
215
216 LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count);
217} 208}
218 209
219/* 210bool
220 * Reads/parses the region file, and copies into a linked list 211loader_base::parse_region (object_thawer &thawer, region *rgn)
221 * of region structs.
222 */
223static bool
224parse_regions (object_thawer &fp)
225{ 212{
226 region *newreg;
227 region *reg;
228
229 newreg = NULL;
230 for (;;) 213 for (;;)
231 { 214 {
232 keyword kw = fp.get_kv (); 215 keyword kw = thawer.get_kv ();
233 216
234 switch (kw) 217 switch (kw)
235 { 218 {
219 case KW_parent:
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
236 case KW_EOF: 239 case KW_msg:
237 if (newreg) 240 thawer.get_ml (KW_endmsg, rgn->msg);
238 {
239 LOG (llevError, "%s: end of file while reading regions.\n", fp.name);
240 return false;
241 }
242 else 241 break;
243 return true; 242
243 case KW_fallback:
244 thawer.get (rgn->fallback);
245 break;
244 246
245 case KW_end: 247 case KW_end:
246 /* Place this new region last on the list, if the list is empty put it first */ 248 return true;
247 for (reg = first_region; reg && reg->next; reg = reg->next)
248 ;
249
250 if (!reg)
251 first_region = newreg;
252 else
253 reg->next = newreg;
254
255 newreg = 0;
256 break;
257 249
258 default: 250 default:
259 case KW_ERROR: 251 if (!thawer.parse_error (kw, "region", rgn->name))
260 LOG (llevError, "%s: skipping errornous line (%s) while reading regions.\n", fp.name, fp.last_keyword); 252 return false;
261 break;
262
263 case KW_region:
264 newreg = new region;
265 fp.get (newreg->name);
266 break;
267
268 case KW_parent:
269 /*
270 * Note that this is in the initialisation code, so we don't actually
271 * assign the pointer to the parent yet, because it might not have been
272 * parsed.
273 */
274 fp.get (newreg->parent_name);
275 break;
276
277 case KW_longname:
278 newreg->longname = strdup (fp.get_str ());
279 break; 253 break;
280
281 case KW_jail_map:
282 fp.get (newreg->jailmap);
283 break;
284
285 case KW_jail_x:
286 fp.get (newreg->jailx);
287 break;
288
289 case KW_jail_y:
290 fp.get (newreg->jaily);
291 break;
292
293 case KW_msg:
294 fp.get_ml (KW_endmsg, newreg->msg);
295 break;
296
297 case KW_fallback:
298 fp.get (newreg->fallback);
299 break;
300
301 case KW_nomore:
302 /* we have reached the end of the region specs.... */
303 return true;
304 } 254 }
305 } 255 }
306} 256}
307 257
308/* 258/*
315init_regions (void) 265init_regions (void)
316{ 266{
317 char filename[MAX_BUF]; 267 char filename[MAX_BUF];
318 int comp; 268 int comp;
319 269
320 if (first_region != NULL) /* Only do this once */ 270 if (!regions.size ())
321 return; 271 {
322
323 // make sure one region is always available 272 // make sure one region is always available
324 first_region = new region; 273 region *rgn = new region;
325 first_region->name = "<builtin>"; 274 rgn->name = "<builtin>";
326 first_region->longname = strdup ("Built-in Region"); 275 rgn->longname = "Built-in Region";
276 regions.push_back (rgn);
277 }
327 278
328 sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions); 279 sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions);
329 LOG (llevDebug, "Reading regions from %s...\n", filename); 280 LOG (llevDebug, "Reading regions from %s...\n", filename);
330 281
331 object_thawer fp (filename); 282 loader_region loader;
332 283
333 if (!fp) 284 if (!loader.load (filename))
334 { 285 {
335 LOG (llevError, " Can't open regions file %s in init_regions.\n", filename); 286 LOG (llevError, " Can't open regions file %s in init_regions.\n", filename);
336 return; 287 return;
337 } 288 }
338 289
339 parse_regions (fp); 290 if (!loader.load (filename))
291 {
292 LOG (llevError, " Can't open regions file %s in init_regions.\n", filename);
293 return;
294 }
340 295
341 assign_region_parents ();
342 LOG (llevDebug, " done\n"); 296 LOG (llevDebug, " done\n");
343} 297}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines