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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines