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.25 by root, Thu Feb 15 21:07:48 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 *
199assign_region_parents (void) 187region::load (object_f &f)
200{ 188{
201 region *reg; 189 region *rgn = new region;
202 uint32 parent_count = 0; 190 f.get (rgn->name);
203 uint32 region_count = 0; 191 f.next_kv ();
204 192
205 for (reg = first_region; reg && reg->next; reg = reg->next) 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_kv ();
214 }
215
216 LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count);
217}
218
219/*
220 * Reads/parses the region file, and copies into a linked list
221 * of region structs.
222 */
223static bool
224parse_regions (object_thawer &fp)
225{
226 region *newreg;
227 region *reg;
228
229 newreg = NULL;
230 for (;;)
231 {
232 keyword kw = fp.get_kv ();
233
234 switch (kw)
235 {
236 case KW_EOF:
237 if (newreg)
238 {
239 LOG (llevError, "%s: end of file while reading regions.\n", fp.name);
240 return false;
241 }
242 else
243 return true;
244
245 case KW_end:
246 /* Place this new region last on the list, if the list is empty put it first */
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
258 default:
259 case KW_ERROR:
260 LOG (llevError, "%s: skipping errornous line (%s) while reading regions.\n", fp.name, fp.last_keyword);
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;
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 }
305 } 251 }
306} 252}
307 253
308/* 254/*
309 * First initialises the archtype hash-table (init_archetable()). 255 * First initialises the archtype hash-table (init_archetable()).
312 * Then initialises treasures by calling load_treasures(). 258 * Then initialises treasures by calling load_treasures().
313 */ 259 */
314void 260void
315init_regions (void) 261init_regions (void)
316{ 262{
317 char filename[MAX_BUF]; 263 if (!regions.size ())
318 int comp; 264 {
319
320 if (first_region != NULL) /* Only do this once */
321 return;
322
323 // make sure one region is always available 265 // make sure one region is always available
324 first_region = new region; 266 region *rgn = new region;
325 first_region->name = "<builtin>"; 267 rgn->name = "<builtin>";
326 first_region->longname = strdup ("Built-in Region"); 268 rgn->longname = "Built-in Region";
327 269 regions.push_back (rgn);
328 sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions);
329 LOG (llevDebug, "Reading regions from %s...\n", filename);
330
331 object_thawer fp (filename);
332
333 if (!fp)
334 { 270 }
335 LOG (llevError, " Can't open regions file %s in init_regions.\n", filename);
336 return;
337 }
338
339 parse_regions (fp);
340
341 assign_region_parents ();
342 LOG (llevDebug, " done\n");
343} 271}
272

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines