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.2 by root, Tue Aug 29 08:01:36 2006 UTC vs.
Revision 1.19 by root, Mon Jan 29 14:46:01 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines