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.42 by root, Thu Jan 1 20:49:48 2009 UTC

1/* 1/*
2 * CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2001-2003,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify
9 * 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
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 * 12 *
13 * 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,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * 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
19 * along with this program; if not, write to the Free Software 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * 20 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de> 21 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 22 */
24 23
25#include <global.h> 24#include <global.h>
26#include <unistd.h> 25#include <unistd.h>
27 26
27regionvec regions;
28
28region * 29region *
29region::default_region () 30region::default_region ()
30{ 31{
31 for (region *reg = first_region; reg; reg = reg->next) 32 for_all_regions (rgn)
32 if (reg->fallback) 33 if (rgn->fallback)
33 return reg; 34 return rgn;
34 35
35 return first_region; 36 return regions [0];
36} 37}
37 38
38/* 39/*
39 * 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.
40 * 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
42 * 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
43 * message and returns NULL 44 * message and returns NULL
44 * used by the map parsing code. 45 * used by the map parsing code.
45 */ 46 */
46region * 47region *
47region::find (const char *name) 48region::find (shstr_cmp name)
48{ 49{
49 for (region *reg = first_region; reg; reg = reg->next) 50 for_all_regions (rgn)
50 if (!strcmp (reg->name, name)) 51 if (rgn->name == name)
51 return reg; 52 return rgn;
52 53
53 LOG (llevError, "region called %s requested, but not found, using fallback.\n", name); 54 LOG (llevError, "region called %s requested, but not found, using fallback.\n", &name);
54 55
55 return default_region (); 56 return default_region ();
56} 57}
57 58
58/* 59void
59 * Tries to find a region that 'name' corresponds to. 60region::do_destroy ()
60 * It looks, in order, for:
61 * an exact match to region name (case insensitive)
62 * an exact match to longname (case insensitive)
63 * a substring that matches to the longname (eg Kingdom)
64 * a substring that matches to the region name (eg nav)
65 * if it can find none of these it returns the first parentless region
66 * (there should be only one of these - the top level one)
67 * If we got a NULL, then just return the top level region
68 *
69 */
70region *
71region::find_fuzzy (const char *name)
72{ 61{
73 region *reg; 62 regions.erase (this);
74 char *substr;
75 char *p;
76 63
77 if (name == NULL) 64 attachable::do_destroy ();
78 {
79 for (reg = first_region; reg->parent; reg = reg->parent)
80 ;
81 65
82 return reg; 66 refcnt_dec ();
83 }
84
85 p = strchr (name, '\n');
86 if (p)
87 *p = '\0';
88
89 for (reg = first_region; reg; reg = reg->next)
90 if (!strcasecmp (reg->name, name))
91 return reg;
92
93 for (reg = first_region; reg; reg = reg->next)
94 if (reg->longname)
95 if (!strcasecmp (reg->longname, name))
96 return reg;
97
98 substr = NULL;
99 for (reg = first_region; reg; reg = reg->next)
100 if (reg->longname)
101 {
102 substr = strstr (reg->longname, name);
103 if (substr)
104 return reg;
105 }
106
107 for (reg = first_region; reg; reg = reg->next)
108 if (reg->longname)
109 {
110 /*
111 * 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
113 * 'scornarena', regardless of their order on the list so we only look at those
114 * regions with a longname set.
115 */
116 substr = strstr (reg->name, name);
117 if (substr)
118 return reg;
119 }
120
121 for (reg = first_region; reg; reg = reg->next)
122 {
123 substr = strstr (reg->name, name);
124 if (substr)
125 return reg;
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 ;
131
132 return reg;
133} 67}
134 68
135/* 69/*
136 * returns 1 if the player is in the region reg, or a child region thereof 70 * returns 1 if the player is in the region reg, or a child region thereof
137 * otherwise returns 0 71 * otherwise returns 0
138 * if passed a NULL region returns -1 72 * if passed a NULL region returns -1
139 */ 73 */
140
141static int 74static int
142region_is_child_of_region (const region * child, const region * r) 75region_is_child_of_region (const region * child, const region * r)
143{ 76{
144 77 if (!r)
145 if (r == NULL)
146 return -1; 78 return -1;
147 79
148 if (child == NULL) 80 if (!child)
149 return 0; 81 return 0;
150 82
151 if (!strcmp (child->name, r->name)) 83 if (child->name == r->name)
152 return 1; 84 return 1;
153 85
154 else if (child->parent != NULL) 86 if (child->parent)
155 return region_is_child_of_region (child->parent, r); 87 return region_is_child_of_region (child->parent, r);
156 else 88
157 return 0; 89 return 0;
158} 90}
159 91
160/** Returns an object which is an exit through which the player represented by op should be 92/** Returns an object which is an exit through which the player represented by op should be
161 * sent in order to be imprisoned. If there is no suitable place to which an exit can be 93 * sent in order to be imprisoned. If there is no suitable place to which an exit can be
162 * constructed, then NULL will be returned. The caller is responsible for freeing the object 94 * constructed, then NULL will be returned. The caller is responsible for freeing the object
163 * created by this function. 95 * created by this function.
164 */ 96 */
165object * 97object *
166get_jail_exit (object *op) 98get_jail_exit (object *op)
167{ 99{
168 region *reg;
169 object *exit;
170
171 if (op->type != PLAYER) 100 if (op->type != PLAYER)
172 { 101 {
173 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n"); 102 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n");
174 return NULL; 103 return NULL;
175 } 104 }
176 105
177 reg = op->region (); 106 region *reg = op->region ();
178 while (reg) 107 while (reg)
179 { 108 {
180 if (reg->jailmap) 109 if (reg->jailmap)
181 { 110 {
182 exit = object::create (); 111 object *exit = object::create ();
183 EXIT_PATH (exit) = reg->jailmap; 112 EXIT_PATH (exit) = reg->jailmap;
184 /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ 113 /* damned exits reset savebed and remove teleports, so the prisoner can't escape */
185 SET_FLAG (exit, FLAG_DAMNED); 114 SET_FLAG (exit, FLAG_DAMNED);
186 EXIT_X (exit) = reg->jailx; 115 EXIT_X (exit) = reg->jailx;
187 EXIT_Y (exit) = reg->jaily; 116 EXIT_Y (exit) = reg->jaily;
189 } 118 }
190 else 119 else
191 reg = reg->parent; 120 reg = reg->parent;
192 } 121 }
193 122
194 LOG (llevDebug, "No suitable jailmap for region %s was found.\n", &reg->name); 123 LOG (llevError, "No suitable jailmap for region %s was found.\n", &reg->name);
124
195 return NULL; 125 return 0;
196} 126}
197 127
198static void 128region *
199assign_region_parents (void) 129region::read (object_thawer &f)
200{ 130{
201 region *reg; 131 assert (f.kw == KW_region);
202 uint32 parent_count = 0;
203 uint32 region_count = 0;
204 132
205 for (reg = first_region; reg && reg->next; reg = reg->next) 133 region *rgn = new region;
134 rgn->refcnt_inc ();
135
136 f.get (rgn->name);
137 f.next ();
138
139 for (;;)
206 { 140 {
207 if (reg->parent_name) 141 switch (f.kw)
208 { 142 {
143 case KW_parent:
209 reg->parent = region::find (reg->parent_name); 144 rgn->parent = region::find (f.get_str ());
210 parent_count++; 145 break;
146
147 case KW_msg: f.get_ml (KW_endmsg, rgn->msg); break;
148 case KW_longname: f.get (rgn->longname); break;
149 case KW_jail_map: f.get (rgn->jailmap); break;
150 case KW_jail_x: f.get (rgn->jailx); break;
151 case KW_jail_y: f.get (rgn->jaily); break;
152 case KW_portal_map: f.get (rgn->portalmap);break;
153 case KW_portal_x: f.get (rgn->portalx); break;
154 case KW_portal_y: f.get (rgn->portaly); break;
155 case KW_fallback: f.get (rgn->fallback); break;
156 case KW_chance: f.get (rgn->treasure_density); break;
157
158 case KW_randomitems:
159 rgn->treasure = treasurelist::get (f.get_str ());
160 break;
161
162 case KW_end:
163 f.next ();
164
165 // cannot use find as that will request the default region
166 for_all_regions (old)
167 if (old->name == rgn->name)
168 {
169 old->destroy ();
170 break;
171 }
172
173 // just append
174 regions.push_back (rgn);
175 return rgn;
176
177 case KW_ERROR:
178 rgn->set_key_text (f.kw_str, f.value);
179 //fprintf (stderr, "region addkv(%s,%s)\n", f.kw_str, f.value);//D
180 break;
181
182 default:
183 if (!f.parse_error ("region", rgn->name))
184 {
185 rgn->destroy ();
186 return 0;
187 }
188 break;
211 } 189 }
212 190
213 region_count++; 191 f.next ();
214 } 192 }
215
216 LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count);
217} 193}
218 194
219/* 195/*
220 * First initialises the archtype hash-table (init_archetable()). 196 * First initialises the archtype hash-table (init_archetable()).
221 * Reads and parses the archetype file (with the first and second-pass 197 * Reads and parses the archetype file (with the first and second-pass
222 * functions). 198 * functions).
223 * Then initialises treasures by calling load_treasures().
224 */ 199 */
225void 200void
226init_regions (void) 201init_regions (void)
227{ 202{
228 FILE *fp; 203 if (!regions.size ())
229 char filename[MAX_BUF]; 204 {
230 int comp;
231
232 if (first_region != NULL) /* Only do this once */
233 return;
234
235 // make sure one region is always available 205 // make sure one region is always available
236 first_region = new region; 206 region *rgn = new region;
237 first_region->name = "<builtin>"; 207 rgn->name = "<builtin>";
238 first_region->longname = strdup ("Built-in Region"); 208 rgn->longname = "Built-in Region";
239 209 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 { 210 }
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} 211}
254 212
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