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.43 by root, Mon Oct 12 14:00:57 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 it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation; either version 2 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * 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 Affero GNU General Public License
19 * along with this program; if not, write to the Free Software 19 * and the GNU General Public License along with this program. If not, see
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * <http://www.gnu.org/licenses/>.
21 * 21 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 23 */
24 24
25#include <global.h> 25#include <global.h>
26#include <unistd.h> 26#include <unistd.h>
27 27
28regionvec regions;
29
28region * 30region *
29region::default_region () 31region::default_region ()
30{ 32{
31 for (region *reg = first_region; reg; reg = reg->next) 33 for_all_regions (rgn)
32 if (reg->fallback) 34 if (rgn->fallback)
33 return reg; 35 return rgn;
34 36
35 return first_region; 37 return regions [0];
36} 38}
37 39
38/* 40/*
39 * Pass a char array, returns a pointer to the region of the same name. 41 * 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 42 * 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 44 * if it can't find a matching name /or/ a fallback region it logs an info message
43 * message and returns NULL 45 * message and returns NULL
44 * used by the map parsing code. 46 * used by the map parsing code.
45 */ 47 */
46region * 48region *
47region::find (const char *name) 49region::find (shstr_cmp name)
48{ 50{
49 for (region *reg = first_region; reg; reg = reg->next) 51 for_all_regions (rgn)
50 if (!strcmp (reg->name, name)) 52 if (rgn->name == name)
51 return reg; 53 return rgn;
52 54
53 LOG (llevError, "region called %s requested, but not found, using fallback.\n", name); 55 LOG (llevError, "region called %s requested, but not found, using fallback.\n", &name);
54 56
55 return default_region (); 57 return default_region ();
56} 58}
57 59
58/* 60void
59 * Tries to find a region that 'name' corresponds to. 61region::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{ 62{
73 region *reg; 63 regions.erase (this);
74 char *substr;
75 char *p;
76 64
77 if (name == NULL) 65 attachable::do_destroy ();
78 {
79 for (reg = first_region; reg->parent; reg = reg->parent)
80 ;
81 66
82 return reg; 67 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} 68}
134 69
135/* 70/*
136 * returns 1 if the player is in the region reg, or a child region thereof 71 * returns 1 if the player is in the region reg, or a child region thereof
137 * otherwise returns 0 72 * otherwise returns 0
138 * if passed a NULL region returns -1 73 * if passed a NULL region returns -1
139 */ 74 */
140
141static int 75static int
142region_is_child_of_region (const region * child, const region * r) 76region_is_child_of_region (const region * child, const region * r)
143{ 77{
144 78 if (!r)
145 if (r == NULL)
146 return -1; 79 return -1;
147 80
148 if (child == NULL) 81 if (!child)
149 return 0; 82 return 0;
150 83
151 if (!strcmp (child->name, r->name)) 84 if (child->name == r->name)
152 return 1; 85 return 1;
153 86
154 else if (child->parent != NULL) 87 if (child->parent)
155 return region_is_child_of_region (child->parent, r); 88 return region_is_child_of_region (child->parent, r);
156 else 89
157 return 0; 90 return 0;
158} 91}
159 92
160/** Returns an object which is an exit through which the player represented by op should be 93/** 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 94 * 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 95 * constructed, then NULL will be returned. The caller is responsible for freeing the object
163 * created by this function. 96 * created by this function.
164 */ 97 */
165object * 98object *
166get_jail_exit (object *op) 99get_jail_exit (object *op)
167{ 100{
168 region *reg;
169 object *exit;
170
171 if (op->type != PLAYER) 101 if (op->type != PLAYER)
172 { 102 {
173 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n"); 103 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n");
174 return NULL; 104 return NULL;
175 } 105 }
176 106
177 reg = op->region (); 107 region *reg = op->region ();
178 while (reg) 108 while (reg)
179 { 109 {
180 if (reg->jailmap) 110 if (reg->jailmap)
181 { 111 {
182 exit = object::create (); 112 object *exit = object::create ();
183 EXIT_PATH (exit) = reg->jailmap; 113 EXIT_PATH (exit) = reg->jailmap;
184 /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ 114 /* damned exits reset savebed and remove teleports, so the prisoner can't escape */
185 SET_FLAG (exit, FLAG_DAMNED); 115 SET_FLAG (exit, FLAG_DAMNED);
186 EXIT_X (exit) = reg->jailx; 116 EXIT_X (exit) = reg->jailx;
187 EXIT_Y (exit) = reg->jaily; 117 EXIT_Y (exit) = reg->jaily;
189 } 119 }
190 else 120 else
191 reg = reg->parent; 121 reg = reg->parent;
192 } 122 }
193 123
194 LOG (llevDebug, "No suitable jailmap for region %s was found.\n", &reg->name); 124 LOG (llevError, "No suitable jailmap for region %s was found.\n", &reg->name);
125
195 return NULL; 126 return 0;
196} 127}
197 128
198static void 129region *
199assign_region_parents (void) 130region::read (object_thawer &f)
200{ 131{
201 region *reg; 132 assert (f.kw == KW_region);
202 uint32 parent_count = 0;
203 uint32 region_count = 0;
204 133
205 for (reg = first_region; reg && reg->next; reg = reg->next) 134 region *rgn = new region;
135 rgn->refcnt_inc ();
136
137 f.get (rgn->name);
138 f.next ();
139
140 for (;;)
206 { 141 {
207 if (reg->parent_name) 142 switch (f.kw)
208 { 143 {
144 case KW_parent:
209 reg->parent = region::find (reg->parent_name); 145 rgn->parent = region::find (f.get_str ());
210 parent_count++; 146 break;
147
148 case KW_msg: f.get_ml (KW_endmsg, rgn->msg); break;
149 case KW_longname: f.get (rgn->longname); break;
150 case KW_jail_map: f.get (rgn->jailmap); break;
151 case KW_jail_x: f.get (rgn->jailx); break;
152 case KW_jail_y: f.get (rgn->jaily); break;
153 case KW_portal_map: f.get (rgn->portalmap);break;
154 case KW_portal_x: f.get (rgn->portalx); break;
155 case KW_portal_y: f.get (rgn->portaly); break;
156 case KW_fallback: f.get (rgn->fallback); break;
157 case KW_chance: f.get (rgn->treasure_density); break;
158
159 case KW_randomitems:
160 rgn->treasure = treasurelist::get (f.get_str ());
161 break;
162
163 case KW_end:
164 f.next ();
165
166 // cannot use find as that will request the default region
167 for_all_regions (old)
168 if (old->name == rgn->name)
169 {
170 old->destroy ();
171 break;
172 }
173
174 // just append
175 regions.push_back (rgn);
176 return rgn;
177
178 case KW_ERROR:
179 rgn->set_key_text (f.kw_str, f.value);
180 //fprintf (stderr, "region addkv(%s,%s)\n", f.kw_str, f.value);//D
181 break;
182
183 default:
184 if (!f.parse_error ("region", rgn->name))
185 {
186 rgn->destroy ();
187 return 0;
188 }
189 break;
211 } 190 }
212 191
213 region_count++; 192 f.next ();
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 } 193 }
306} 194}
307 195
308/* 196/*
309 * First initialises the archtype hash-table (init_archetable()). 197 * First initialises the archtype hash-table (init_archetable()).
310 * Reads and parses the archetype file (with the first and second-pass 198 * Reads and parses the archetype file (with the first and second-pass
311 * functions). 199 * functions).
312 * Then initialises treasures by calling load_treasures().
313 */ 200 */
314void 201void
315init_regions (void) 202init_regions (void)
316{ 203{
317 char filename[MAX_BUF]; 204 if (!regions.size ())
318 int comp; 205 {
319
320 if (first_region != NULL) /* Only do this once */
321 return;
322
323 // make sure one region is always available 206 // make sure one region is always available
324 first_region = new region; 207 region *rgn = new region;
325 first_region->name = "<builtin>"; 208 rgn->name = "<builtin>";
326 first_region->longname = strdup ("Built-in Region"); 209 rgn->longname = "Built-in Region";
327 210 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 { 211 }
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} 212}
213

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines