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.41 by root, Thu Jan 1 11:41:17 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/* 59/*
68 * 69 *
69 */ 70 */
70region * 71region *
71region::find_fuzzy (const char *name) 72region::find_fuzzy (const char *name)
72{ 73{
73 region *reg; 74 if (!name)
74 char *substr; 75 return default_region ();
75 char *p;
76 76
77 if (name == NULL) 77 // TODO: bug, changes possibly const string
78 {
79 for (reg = first_region; reg->parent; reg = reg->parent)
80 ;
81
82 return reg;
83 }
84
85 p = strchr (name, '\n'); 78 char *p = strchr (name, '\n');
86 if (p) 79 if (p)
87 *p = '\0'; 80 *p = '\0';
88 81
89 for (reg = first_region; reg; reg = reg->next) 82 for_all_regions (rgn)
90 if (!strcasecmp (reg->name, name)) 83 if (rgn->name.eq_nc (name))
91 return reg; 84 return rgn;
92 85
93 for (reg = first_region; reg; reg = reg->next) 86 for_all_regions (rgn)
94 if (reg->longname) 87 if (rgn->longname.eq_nc (name))
95 if (!strcasecmp (reg->longname, name))
96 return reg; 88 return rgn;
97 89
98 substr = NULL; 90 for_all_regions (rgn)
99 for (reg = first_region; reg; reg = reg->next) 91 if (rgn->longname.contains (name))
100 if (reg->longname)
101 {
102 substr = strstr (reg->longname, name);
103 if (substr)
104 return reg; 92 return rgn;
105 }
106 93
107 for (reg = first_region; reg; reg = reg->next) 94 for_all_regions (rgn)
108 if (reg->longname)
109 {
110 /* 95 /*
111 * This is not a bug, we want the region that is most identifiably a discrete 96 * 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 97 * 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 98 * 'scornarena', regardless of their order on the list so we only look at those
114 * regions with a longname set. 99 * regions with a longname set.
115 */ 100 */
116 substr = strstr (reg->name, name); 101 if (rgn->longname && rgn->name.contains (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; 102 return rgn;
126 }
127 103
128 /* if we are still here, we are going to have to give up, and give the top level region */ 104 for_all_regions (rgn)
129 for (reg = first_region; reg->parent; reg = reg->parent) 105 if (rgn->name.contains (name))
130 ;
131
132 return reg; 106 return rgn;
107
108 return default_region ();
109}
110
111void
112region::do_destroy ()
113{
114 regions.erase (this);
115
116 attachable::do_destroy ();
117
118 refcnt_dec ();
133} 119}
134 120
135/* 121/*
136 * returns 1 if the player is in the region reg, or a child region thereof 122 * returns 1 if the player is in the region reg, or a child region thereof
137 * otherwise returns 0 123 * otherwise returns 0
138 * if passed a NULL region returns -1 124 * if passed a NULL region returns -1
139 */ 125 */
140
141static int 126static int
142region_is_child_of_region (const region * child, const region * r) 127region_is_child_of_region (const region * child, const region * r)
143{ 128{
144 129 if (!r)
145 if (r == NULL)
146 return -1; 130 return -1;
147 131
148 if (child == NULL) 132 if (!child)
149 return 0; 133 return 0;
150 134
151 if (!strcmp (child->name, r->name)) 135 if (child->name == r->name)
152 return 1; 136 return 1;
153 137
154 else if (child->parent != NULL) 138 if (child->parent)
155 return region_is_child_of_region (child->parent, r); 139 return region_is_child_of_region (child->parent, r);
156 else 140
157 return 0; 141 return 0;
158} 142}
159 143
160/** Returns an object which is an exit through which the player represented by op should be 144/** 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 145 * 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 146 * constructed, then NULL will be returned. The caller is responsible for freeing the object
163 * created by this function. 147 * created by this function.
164 */ 148 */
165object * 149object *
166get_jail_exit (object *op) 150get_jail_exit (object *op)
167{ 151{
168 region *reg;
169 object *exit;
170
171 if (op->type != PLAYER) 152 if (op->type != PLAYER)
172 { 153 {
173 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n"); 154 LOG (llevError, "region.c: get_jail_exit called against non-player object.\n");
174 return NULL; 155 return NULL;
175 } 156 }
176 157
177 reg = op->region (); 158 region *reg = op->region ();
178 while (reg) 159 while (reg)
179 { 160 {
180 if (reg->jailmap) 161 if (reg->jailmap)
181 { 162 {
182 exit = object::create (); 163 object *exit = object::create ();
183 EXIT_PATH (exit) = reg->jailmap; 164 EXIT_PATH (exit) = reg->jailmap;
184 /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ 165 /* damned exits reset savebed and remove teleports, so the prisoner can't escape */
185 SET_FLAG (exit, FLAG_DAMNED); 166 SET_FLAG (exit, FLAG_DAMNED);
186 EXIT_X (exit) = reg->jailx; 167 EXIT_X (exit) = reg->jailx;
187 EXIT_Y (exit) = reg->jaily; 168 EXIT_Y (exit) = reg->jaily;
189 } 170 }
190 else 171 else
191 reg = reg->parent; 172 reg = reg->parent;
192 } 173 }
193 174
194 LOG (llevDebug, "No suitable jailmap for region %s was found.\n", &reg->name); 175 LOG (llevError, "No suitable jailmap for region %s was found.\n", &reg->name);
176
195 return NULL; 177 return 0;
196} 178}
197 179
198static void 180region *
199assign_region_parents (void) 181region::read (object_thawer &f)
200{ 182{
201 region *reg; 183 assert (f.kw == KW_region);
202 uint32 parent_count = 0;
203 uint32 region_count = 0;
204 184
205 for (reg = first_region; reg && reg->next; reg = reg->next) 185 region *rgn = new region;
186 rgn->refcnt_inc ();
187
188 f.get (rgn->name);
189 f.next ();
190
191 for (;;)
206 { 192 {
207 if (reg->parent_name) 193 switch (f.kw)
208 { 194 {
195 case KW_parent:
209 reg->parent = region::find (reg->parent_name); 196 rgn->parent = region::find (f.get_str ());
210 parent_count++; 197 break;
198
199 case KW_msg: f.get_ml (KW_endmsg, rgn->msg); break;
200 case KW_longname: f.get (rgn->longname); break;
201 case KW_jail_map: f.get (rgn->jailmap); break;
202 case KW_jail_x: f.get (rgn->jailx); break;
203 case KW_jail_y: f.get (rgn->jaily); break;
204 case KW_portal_map: f.get (rgn->portalmap);break;
205 case KW_portal_x: f.get (rgn->portalx); break;
206 case KW_portal_y: f.get (rgn->portaly); break;
207 case KW_fallback: f.get (rgn->fallback); break;
208 case KW_chance: f.get (rgn->treasure_density); break;
209
210 case KW_randomitems:
211 rgn->treasure = treasurelist::get (f.get_str ());
212 break;
213
214 case KW_end:
215 f.next ();
216
217 // cannot use find as that will request the default region
218 for_all_regions (old)
219 if (old->name == rgn->name)
220 {
221 old->destroy ();
222 break;
223 }
224
225 // just append
226 regions.push_back (rgn);
227 return rgn;
228
229 case KW_ERROR:
230 rgn->set_key_text (f.kw_str, f.value);
231 //fprintf (stderr, "region addkv(%s,%s)\n", f.kw_str, f.value);//D
232 break;
233
234 default:
235 if (!f.parse_error ("region", rgn->name))
236 {
237 rgn->destroy ();
238 return 0;
239 }
240 break;
211 } 241 }
212 242
213 region_count++; 243 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 } 244 }
306} 245}
307 246
308/* 247/*
309 * First initialises the archtype hash-table (init_archetable()). 248 * First initialises the archtype hash-table (init_archetable()).
310 * Reads and parses the archetype file (with the first and second-pass 249 * Reads and parses the archetype file (with the first and second-pass
311 * functions). 250 * functions).
312 * Then initialises treasures by calling load_treasures().
313 */ 251 */
314void 252void
315init_regions (void) 253init_regions (void)
316{ 254{
317 char filename[MAX_BUF]; 255 if (!regions.size ())
318 int comp; 256 {
319
320 if (first_region != NULL) /* Only do this once */
321 return;
322
323 // make sure one region is always available 257 // make sure one region is always available
324 first_region = new region; 258 region *rgn = new region;
325 first_region->name = "<builtin>"; 259 rgn->name = "<builtin>";
326 first_region->longname = strdup ("Built-in Region"); 260 rgn->longname = "Built-in Region";
327 261 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 { 262 }
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} 263}
264

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines