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.4 by root, Sun Sep 10 16:00:23 2006 UTC vs.
Revision 1.36 by root, Thu Aug 30 07:28:25 2007 UTC

1
2/* 1/*
3 * static char *rcsid_map_c = 2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
4 * "$Id: region.C,v 1.4 2006/09/10 16:00:23 root Exp $"; 3 *
5 */ 4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
6
7/*
8 CrossFire, A Multiplayer game for X-windows
9
10 Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2001-2003,2007 Mark Wedel & Crossfire Development Team
11 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
12 7 *
13 This program is free software; you can redistribute it and/or modify 8 * Crossfire TRT is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version. 11 * (at your option) any later version.
17 12 *
18 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details. 16 * GNU General Public License for more details.
22 17 *
23 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 *
26 21 * The authors can be reached via e-mail to <crossfire@schmorp.de>
27 The authors can be reached via e-mail at crossfire-devel@real-time.com
28*/ 22 */
29
30 23
31#include <global.h> 24#include <global.h>
32
33#ifndef WIN32 /* ---win32 exclude header */
34# include <unistd.h> 25#include <unistd.h>
35#endif /* win32 */ 26
27regionvec regions;
28
29region *
30region::default_region ()
31{
32 for_all_regions (rgn)
33 if (rgn->fallback)
34 return rgn;
35
36 return regions [0];
37}
36 38
37/* 39/*
38 * Pass a char array, returns a pointer to the region of the same name. 40 * Pass a char array, returns a pointer to the region of the same name.
39 * if it can't find a region of the same name it returns the first region 41 * if it can't find a region of the same name it returns the first region
40 * with the 'fallback' property set. 42 * with the 'fallback' property set.
41 * if it can't find a matching name /or/ a fallback region it logs an info message 43 * if it can't find a matching name /or/ a fallback region it logs an info message
42 * message and returns NULL 44 * message and returns NULL
43 * used by the map parsing code. 45 * used by the map parsing code.
44 */ 46 */
45region * 47region *
46get_region_by_name (const char *region_name) 48region::find (const char *name)
47{ 49{
48 region *reg; 50 for_all_regions (rgn)
49 char *p = strchr (region_name, '\n');
50
51 if (p)
52 *p = '\0';
53 for (reg = first_region; reg != NULL; reg = reg->next)
54 if (!strcmp (reg->name, region_name)) 51 if (!strcmp (rgn->name, name))
55 return reg; 52 return rgn;
56 53
57 for (reg = first_region; reg != NULL; reg = reg->next)
58 {
59 if (reg->fallback)
60 {
61 LOG (llevDebug, "region called %s requested, but not found, fallback used.\n", region_name); 54 LOG (llevError, "region called %s requested, but not found, using fallback.\n", name);
62 return reg;
63 }
64 }
65 LOG (llevInfo, "Got no region or fallback for region %s.\n", region_name);
66 return NULL;
67}
68 55
69/* This might need optimising at some point. */ 56 return default_region ();
70region *
71get_region_by_map (mapstruct *m)
72{
73 return get_region_by_name (get_name_of_region_for_map (m));
74}
75
76/*
77 * Since we won't assume all maps have a region set properly, we need an
78 * explicit check that it is, this is much nicer here than scattered throughout
79 * the map code.
80 */
81
82const char *
83get_name_of_region_for_map (const mapstruct *m)
84{
85 region *reg;
86
87 if (m->region != NULL)
88 return m->region->name;
89 for (reg = first_region; reg != NULL; reg = reg->next)
90 {
91 if (reg->fallback)
92 return reg->name;
93 }
94 LOG (llevInfo, "map %s had no region and I couldn't find a fallback to use.\n", m->name);
95 return "unknown";
96} 57}
97 58
98/* 59/*
99 * Tries to find a region that 'name' corresponds to. 60 * Tries to find a region that 'name' corresponds to.
100 * It looks, in order, for: 61 * It looks, in order, for:
106 * (there should be only one of these - the top level one) 67 * (there should be only one of these - the top level one)
107 * If we got a NULL, then just return the top level region 68 * If we got a NULL, then just return the top level region
108 * 69 *
109 */ 70 */
110region * 71region *
111get_region_from_string (const char *name) 72region::find_fuzzy (const char *name)
112{ 73{
113 region *reg; 74 if (!name)
114 char *substr; 75 return default_region ();
115 char *p;
116 76
117 if (name == NULL)
118 {
119 for (reg = first_region; reg->parent != NULL; reg = reg->parent);
120 return reg;
121 }
122 p = strchr (name, '\n'); 77 char *p = strchr (name, '\n');
123 if (p) 78 if (p)
124 *p = '\0'; 79 *p = '\0';
125 for (reg = first_region; reg != NULL; reg = reg->next) 80
81 for_all_regions (rgn)
126 if (!strcasecmp (reg->name, name)) 82 if (!strcasecmp (rgn->name, name))
127 return reg; 83 return rgn;
128 84
129 for (reg = first_region; reg != NULL; reg = reg->next) 85 for_all_regions (rgn)
130 if (reg->longname != NULL) 86 if (rgn->longname)
87 if (!strcasecmp (rgn->longname, name))
88 return rgn;
89
90 for_all_regions (rgn)
91 if (rgn->longname)
131 { 92 {
132 if (!strcasecmp (reg->longname, name)) 93 if (strstr (rgn->longname, name))
133 return reg; 94 return rgn;
134 } 95 }
135 96
136 substr = NULL; 97 for_all_regions (rgn)
137 for (reg = first_region; reg != NULL; reg = reg->next)
138 if (reg->longname != NULL) 98 if (rgn->longname)
139 {
140 substr = strstr (reg->longname, name);
141 if (substr != NULL)
142 return reg;
143 }
144 for (reg = first_region; reg != NULL; reg = reg->next)
145 if (reg->longname != NULL)
146 { 99 {
147 /* 100 /*
148 * This is not a bug, we want the region that is most identifiably a discrete 101 * This is not a bug, we want the region that is most identifiably a discrete
149 * area in the game, eg if we have 'scor', we want to return 'scorn' and not 102 * area in the game, eg if we have 'scor', we want to return 'scorn' and not
150 * 'scornarena', regardless of their order on the list so we only look at those 103 * 'scornarena', regardless of their order on the list so we only look at those
151 * regions with a longname set. 104 * regions with a longname set.
152 */ 105 */
153 substr = strstr (reg->name, name); 106 if (strstr (rgn->name, name))
154 if (substr != NULL)
155 return reg; 107 return rgn;
156 } 108 }
157 for (reg = first_region; reg != NULL; reg = reg->next) 109
110 for_all_regions (rgn)
158 { 111 {
159 substr = strstr (reg->name, name); 112 if (strstr (rgn->name, name))
160 if (substr != NULL)
161 return reg; 113 return rgn;
162 } 114 }
163 /* if we are still here, we are going to have to give up, and give the top level region */ 115
164 for (reg = first_region; reg->parent != NULL; reg = reg->parent); 116 return default_region ();
165 return reg; 117}
118
119void
120region::do_destroy ()
121{
122 regions.erase (this);
123
124 attachable::do_destroy ();
125
126 refcnt_dec ();
166} 127}
167 128
168/* 129/*
169 * returns 1 if the player is in the region reg, or a child region thereof 130 * returns 1 if the player is in the region reg, or a child region thereof
170 * otherwise returns 0 131 * otherwise returns 0
171 * if passed a NULL region returns -1 132 * if passed a NULL region returns -1
172 */ 133 */
173 134static int
174int
175region_is_child_of_region (const region * child, const region * r) 135region_is_child_of_region (const region * child, const region * r)
176{ 136{
177 137
178 if (r == NULL) 138 if (r == NULL)
179 return -1; 139 return -1;
140
180 if (child == NULL) 141 if (child == NULL)
181 return 0; 142 return 0;
143
182 if (!strcmp (child->name, r->name)) 144 if (!strcmp (child->name, r->name))
183 return 1; 145 return 1;
146
184 else if (child->parent != NULL) 147 else if (child->parent != NULL)
185 return region_is_child_of_region (child->parent, r); 148 return region_is_child_of_region (child->parent, r);
186 else 149 else
187 return 0; 150 return 0;
188} 151}
189 152
190/*
191 * the longname of a region is not a required field, any given region
192 * may want to not set it and use the parent's one instead. so, we:
193 * 1. check if a longname is set and if so return it.
194 * 2. check if there is a parent and try and call the function against that
195 * 3. return a obviously wrong string if we can't get a longname, this should
196 * never happen. We also log a debug message.
197 */
198const char *
199get_region_longname (const region * r)
200{
201
202 if (r->longname != NULL)
203 return r->longname;
204 else if (r->parent != NULL)
205 return get_region_longname (r->parent);
206 else
207 {
208 LOG (llevDebug, "NOTICE region %s has no parent and no longname.\n", r->name);
209 return "no name can be found for the current region";
210 }
211}
212
213const char *
214get_region_msg (const region * r)
215{
216 if (r->msg != NULL)
217 return r->msg;
218 else if (r->parent != NULL)
219 return get_region_msg (r->parent);
220 else
221 {
222 LOG (llevDebug, "NOTICE region %s has no parent and no msg.\n", r->name);
223 return "no description can be found for the current region";
224 }
225}
226
227/** Returns an object which is an exit through which the player represented by op should be 153/** Returns an object which is an exit through which the player represented by op should be
228 * sent in order to be imprisoned. If there is no suitable place to which an exit can be 154 * sent in order to be imprisoned. If there is no suitable place to which an exit can be
229 * constructed, then NULL will be returned. The caller is responsible for freeing the object 155 * constructed, then NULL will be returned. The caller is responsible for freeing the object
230 * created by this function. 156 * created by this function.
231 */ 157 */
238 if (op->type != PLAYER) 164 if (op->type != PLAYER)
239 { 165 {
240 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n"); 166 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n");
241 return NULL; 167 return NULL;
242 } 168 }
243 reg = get_region_by_map (op->map); 169
170 reg = op->region ();
244 while (reg != NULL) 171 while (reg)
245 { 172 {
246 if (reg->jailmap) 173 if (reg->jailmap)
247 { 174 {
248 exit = get_object (); 175 exit = object::create ();
249 EXIT_PATH (exit) = reg->jailmap; 176 EXIT_PATH (exit) = reg->jailmap;
250 /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ 177 /* damned exits reset savebed and remove teleports, so the prisoner can't escape */
251 SET_FLAG (exit, FLAG_DAMNED); 178 SET_FLAG (exit, FLAG_DAMNED);
252 EXIT_X (exit) = reg->jailx; 179 EXIT_X (exit) = reg->jailx;
253 EXIT_Y (exit) = reg->jaily; 180 EXIT_Y (exit) = reg->jaily;
254 return exit; 181 return exit;
255 } 182 }
256 else 183 else
257 reg = reg->parent; 184 reg = reg->parent;
258 } 185 }
186
259 LOG (llevDebug, "No suitable jailmap for region %s was found.\n", reg->name); 187 LOG (llevError, "No suitable jailmap for region %s was found.\n", &reg->name);
188
260 return NULL; 189 return 0;
190}
191
192region *
193region::read (object_thawer &f)
194{
195 assert (f.kw == KW_region);
196
197 region *rgn = new region;
198 rgn->refcnt_inc ();
199
200 f.get (rgn->name);
201 f.next ();
202
203 for (;;)
204 {
205 switch (f.kw)
206 {
207 case KW_parent:
208 rgn->parent = region::find (f.get_str ());
209 break;
210
211 case KW_msg: f.get_ml (KW_endmsg, rgn->msg); break;
212 case KW_longname: f.get (rgn->longname); break;
213 case KW_jail_map: f.get (rgn->jailmap); break;
214 case KW_jail_x: f.get (rgn->jailx); break;
215 case KW_jail_y: f.get (rgn->jaily); break;
216 case KW_portal_map: f.get (rgn->portalmap);break;
217 case KW_portal_x: f.get (rgn->portalx); break;
218 case KW_portal_y: f.get (rgn->portaly); break;
219 case KW_fallback: f.get (rgn->fallback); break;
220 case KW_chance: f.get (rgn->treasure_density); break;
221
222 case KW_randomitems:
223 rgn->treasure = treasurelist::get (f.get_str ());
224 break;
225
226 case KW_end:
227 f.next ();
228
229 // cannot use find as that will request the default region
230 for_all_regions (old)
231 if (old->name == rgn->name)
232 {
233 old->destroy ();
234 break;
235 }
236
237 // just append
238 regions.push_back (rgn);
239 return rgn;
240
241 case KW_ERROR:
242 rgn->set_key_text (f.kw_str, f.value);
243 //fprintf (stderr, "region addkv(%s,%s)\n", f.kw_str, f.value);//D
244 break;
245
246 default:
247 if (!f.parse_error ("region", rgn->name))
248 {
249 rgn->destroy ();
250 return 0;
251 }
252 break;
253 }
254
255 f.next ();
256 }
261} 257}
262 258
263/* 259/*
264 * First initialises the archtype hash-table (init_archetable()). 260 * First initialises the archtype hash-table (init_archetable()).
265 * Reads and parses the archetype file (with the first and second-pass 261 * Reads and parses the archetype file (with the first and second-pass
266 * functions). 262 * functions).
267 * Then initialises treasures by calling load_treasures().
268 */ 263 */
269void 264void
270init_regions (void) 265init_regions (void)
271{ 266{
272 FILE *fp; 267 if (!regions.size ())
273 char filename[MAX_BUF];
274 int comp;
275
276 if (first_region != NULL) /* Only do this once */
277 return;
278
279 sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions);
280 LOG (llevDebug, "Reading regions from %s...\n", filename);
281 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
282 {
283 LOG (llevError, " Can't open regions file %s in init_regions.\n", filename);
284 return;
285 } 268 {
286 parse_regions (fp); 269 // make sure one region is always available
287 assign_region_parents (); 270 region *rgn = new region;
288 LOG (llevDebug, " done\n"); 271 rgn->name = "<builtin>";
289 272 rgn->longname = "Built-in Region";
290 close_and_delete (fp, comp); 273 regions.push_back (rgn);
291}
292
293/*
294 * Allocates and zeros a region struct, this isn't free()'d anywhere, so might
295 * be a memory leak, but it shouldn't matter too much since it isn't called that
296 * often....
297 */
298
299region *
300get_region_struct (void)
301{
302
303 region *reg;
304
305 reg = (region *) CALLOC (1, sizeof (region));
306 if (reg == NULL)
307 fatal (OUT_OF_MEMORY);
308
309 memset (reg, '\0', sizeof (region));
310
311 return reg;
312}
313
314/*
315 * Reads/parses the region file, and copies into a linked list
316 * of region structs.
317 */
318void
319parse_regions (FILE * fp)
320{
321 region *newreg;
322 region *reg;
323
324 char buf[HUGE_BUF], msgbuf[HUGE_BUF], *key = NULL, *value, *end;
325 int msgpos = 0;
326
327 newreg = NULL;
328 while (fgets (buf, HUGE_BUF - 1, fp) != NULL)
329 { 274 }
330 buf[HUGE_BUF - 1] = 0;
331 key = buf;
332 while (isspace (*key))
333 key++;
334 if (*key == 0)
335 continue; /* empty line */
336 value = strchr (key, ' ');
337 if (!value)
338 {
339 end = strchr (key, '\n');
340 *end = 0;
341 }
342 else
343 {
344 *value = 0;
345 value++;
346 while (isspace (*value))
347 value++;
348 end = strchr (value, '\n');
349 }
350
351 /*
352 * This is a bizzare mutated form of the map and archetype parser
353 * rolled into one. Key is the field name, value is what it should
354 * be set to.
355 * We've already done the work to null terminate key,
356 * and strip off any leading spaces for both of these.
357 * We have not touched the newline at the end of the line -
358 * these might be needed for some values. the end pointer
359 * points to the first of the newlines.
360 * value could be NULL! It would be easy enough to just point
361 * this to "" to prevent cores, but that would let more errors slide
362 * through.
363 */
364 if (!strcmp (key, "region"))
365 {
366 *end = 0;
367 newreg = get_region_struct ();
368 newreg->name = strdup_local (value);
369 }
370 else if (!strcmp (key, "parent"))
371 {
372 /*
373 * Note that this is in the initialisation code, so we don't actually
374 * assign the pointer to the parent yet, because it might not have been
375 * parsed.
376 */
377 *end = 0;
378 newreg->parent_name = strdup_local (value);
379 }
380 else if (!strcmp (key, "longname"))
381 {
382 *end = 0;
383 newreg->longname = strdup_local (value);
384 }
385 else if (!strcmp (key, "jail"))
386 {
387 /* jail entries are of the form: /path/to/map x y */
388 char path[MAX_BUF];
389 int x, y;
390
391 if (sscanf (value, "%[^ ] %d %d\n", path, &x, &y) != 3)
392 {
393 LOG (llevError, "region.c: malformated regions entry: jail %s\n", value);
394 continue;
395 }
396 newreg->jailmap = strdup_local (path);
397 newreg->jailx = x;
398 newreg->jaily = y;
399 }
400 else if (!strcmp (key, "msg"))
401 {
402 while (fgets (buf, HUGE_BUF - 1, fp) != NULL)
403 {
404 if (!strcmp (buf, "endmsg\n"))
405 break;
406 else
407 {
408 strcpy (msgbuf + msgpos, buf);
409 msgpos += strlen (buf);
410 }
411 }
412 /*
413 * There may be regions with empty messages (eg, msg/endmsg
414 * with nothing between). When maps are loaded, this is done
415 * so better do it here too...
416 */
417 if (msgpos != 0)
418 newreg->msg = strdup_local (msgbuf);
419
420 /* we have to reset msgpos, or the next region will store both msg blocks. */
421 msgpos = 0;
422 }
423 else if (!strcmp (key, "fallback"))
424 {
425 *end = 0;
426 newreg->fallback = atoi (value);
427 }
428 else if (!strcmp (key, "end"))
429 {
430 /* Place this new region last on the list, if the list is empty put it first */
431 for (reg = first_region; reg != NULL && reg->next != NULL; reg = reg->next);
432
433 if (reg == NULL)
434 first_region = newreg;
435 else
436 reg->next = newreg;
437 newreg = NULL;
438 }
439 else if (!strcmp (key, "nomore"))
440 {
441 /* we have reached the end of the region specs.... */
442 break;
443 }
444 else
445 {
446 /* we should never get here, if we have, then something is wrong */
447 LOG (llevError, "Got unknown value in region file: %s %s\n", key, value);
448 }
449 }
450 if (!key || strcmp (key, "nomore"))
451 LOG (llevError, "Got premature eof on regions file!\n");
452} 275}
453 276
454void
455assign_region_parents (void)
456{
457 region *reg;
458 uint32 parent_count = 0;
459 uint32 region_count = 0;
460
461 for (reg = first_region; reg != NULL && reg->next != NULL; reg = reg->next)
462 {
463 if (reg->parent_name != NULL)
464 {
465 reg->parent = get_region_by_name (reg->parent_name);
466 parent_count++;
467 }
468 region_count++;
469 }
470 LOG (llevDebug, "Assigned %u regions with %u parents.", region_count, parent_count);
471}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines