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 | |
|
|
28 | regionvec regions; |
|
|
29 | |
28 | region * |
30 | region * |
29 | region::default_region () |
31 | region::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 | */ |
46 | region * |
48 | region * |
47 | region::find (const char *name) |
49 | region::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 | /* |
60 | void |
59 | * Tries to find a region that 'name' corresponds to. |
61 | region::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 | */ |
|
|
70 | region * |
|
|
71 | region::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 | } |
|
|
69 | |
|
|
70 | //+GPL |
134 | |
71 | |
135 | /* |
72 | /* |
136 | * returns 1 if the player is in the region reg, or a child region thereof |
73 | * returns 1 if the player is in the region reg, or a child region thereof |
137 | * otherwise returns 0 |
74 | * otherwise returns 0 |
138 | * if passed a NULL region returns -1 |
75 | * if passed a NULL region returns -1 |
139 | */ |
76 | */ |
140 | |
|
|
141 | static int |
77 | static int |
142 | region_is_child_of_region (const region * child, const region * r) |
78 | region_is_child_of_region (const region * child, const region * r) |
143 | { |
79 | { |
144 | |
80 | if (!r) |
145 | if (r == NULL) |
|
|
146 | return -1; |
81 | return -1; |
147 | |
82 | |
148 | if (child == NULL) |
83 | if (!child) |
149 | return 0; |
84 | return 0; |
150 | |
85 | |
151 | if (!strcmp (child->name, r->name)) |
86 | if (child->name == r->name) |
152 | return 1; |
87 | return 1; |
153 | |
88 | |
154 | else if (child->parent != NULL) |
89 | if (child->parent) |
155 | return region_is_child_of_region (child->parent, r); |
90 | return region_is_child_of_region (child->parent, r); |
156 | else |
91 | |
157 | return 0; |
92 | return 0; |
158 | } |
93 | } |
159 | |
94 | |
160 | /** Returns an object which is an exit through which the player represented by op should be |
95 | /** 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 |
96 | * 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 |
97 | * constructed, then NULL will be returned. The caller is responsible for freeing the object |
163 | * created by this function. |
98 | * created by this function. |
164 | */ |
99 | */ |
165 | object * |
100 | object * |
166 | get_jail_exit (object *op) |
101 | get_jail_exit (object *op) |
167 | { |
102 | { |
168 | region *reg; |
|
|
169 | object *exit; |
|
|
170 | |
|
|
171 | if (op->type != PLAYER) |
103 | if (op->type != PLAYER) |
172 | { |
104 | { |
173 | LOG (llevError, "region.c: get_jail_exit called against non-player object.\n"); |
105 | LOG (llevError, "region.c: get_jail_exit called against non-player object.\n"); |
174 | return NULL; |
106 | return NULL; |
175 | } |
107 | } |
176 | |
108 | |
177 | reg = op->region (); |
109 | region *reg = op->region (); |
178 | while (reg) |
110 | while (reg) |
179 | { |
111 | { |
180 | if (reg->jailmap) |
112 | if (reg->jailmap) |
181 | { |
113 | { |
182 | exit = object::create (); |
114 | object *exit = object::create (); |
183 | EXIT_PATH (exit) = reg->jailmap; |
115 | EXIT_PATH (exit) = reg->jailmap; |
184 | /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ |
116 | /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ |
185 | SET_FLAG (exit, FLAG_DAMNED); |
117 | SET_FLAG (exit, FLAG_DAMNED); |
186 | EXIT_X (exit) = reg->jailx; |
118 | EXIT_X (exit) = reg->jailx; |
187 | EXIT_Y (exit) = reg->jaily; |
119 | EXIT_Y (exit) = reg->jaily; |
… | |
… | |
189 | } |
121 | } |
190 | else |
122 | else |
191 | reg = reg->parent; |
123 | reg = reg->parent; |
192 | } |
124 | } |
193 | |
125 | |
194 | LOG (llevDebug, "No suitable jailmap for region %s was found.\n", ®->name); |
126 | LOG (llevError, "No suitable jailmap for region %s was found.\n", ®->name); |
|
|
127 | |
195 | return NULL; |
128 | return 0; |
|
|
129 | } |
|
|
130 | |
|
|
131 | //-GPL |
|
|
132 | |
|
|
133 | region * |
|
|
134 | region::read (object_thawer &f) |
|
|
135 | { |
|
|
136 | assert (f.kw == KW_region); |
|
|
137 | |
|
|
138 | region *rgn = new region; |
|
|
139 | rgn->refcnt_inc (); |
|
|
140 | |
|
|
141 | f.get (rgn->name); |
|
|
142 | f.next (); |
|
|
143 | |
|
|
144 | for (;;) |
|
|
145 | { |
|
|
146 | switch (f.kw) |
|
|
147 | { |
|
|
148 | case KW_parent: |
|
|
149 | rgn->parent = region::find (f.get_str ()); |
|
|
150 | break; |
|
|
151 | |
|
|
152 | case KW_msg: f.get_ml (KW_endmsg, rgn->msg); break; |
|
|
153 | case KW_longname: f.get (rgn->longname); break; |
|
|
154 | case KW_jail_map: f.get (rgn->jailmap); break; |
|
|
155 | case KW_jail_x: f.get (rgn->jailx); break; |
|
|
156 | case KW_jail_y: f.get (rgn->jaily); break; |
|
|
157 | case KW_portal_map: f.get (rgn->portalmap);break; |
|
|
158 | case KW_portal_x: f.get (rgn->portalx); break; |
|
|
159 | case KW_portal_y: f.get (rgn->portaly); break; |
|
|
160 | case KW_fallback: f.get (rgn->fallback); break; |
|
|
161 | case KW_chance: f.get (rgn->treasure_density); break; |
|
|
162 | |
|
|
163 | case KW_randomitems: |
|
|
164 | rgn->treasure = treasurelist::get (f.get_str ()); |
|
|
165 | break; |
|
|
166 | |
|
|
167 | case KW_end: |
|
|
168 | f.next (); |
|
|
169 | |
|
|
170 | // cannot use find as that will request the default region |
|
|
171 | for_all_regions (old) |
|
|
172 | if (old->name == rgn->name) |
|
|
173 | { |
|
|
174 | old->destroy (); |
|
|
175 | break; |
|
|
176 | } |
|
|
177 | |
|
|
178 | // just append |
|
|
179 | regions.push_back (rgn); |
|
|
180 | return rgn; |
|
|
181 | |
|
|
182 | case KW_ERROR: |
|
|
183 | rgn->set_key_text (f.kw_str, f.value); |
|
|
184 | //fprintf (stderr, "region addkv(%s,%s)\n", f.kw_str, f.value);//D |
|
|
185 | break; |
|
|
186 | |
|
|
187 | default: |
|
|
188 | if (!f.parse_error ("region", rgn->name)) |
|
|
189 | { |
|
|
190 | rgn->destroy (); |
|
|
191 | return 0; |
|
|
192 | } |
|
|
193 | break; |
|
|
194 | } |
|
|
195 | |
|
|
196 | f.next (); |
|
|
197 | } |
196 | } |
198 | } |
197 | |
199 | |
198 | static void |
200 | static void |
199 | assign_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); |
|
|
217 | } |
|
|
218 | |
|
|
219 | /* |
|
|
220 | * Reads/parses the region file, and copies into a linked list |
|
|
221 | * of region structs. |
|
|
222 | */ |
|
|
223 | static bool |
|
|
224 | parse_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 | } |
|
|
306 | } |
|
|
307 | |
|
|
308 | /* |
|
|
309 | * First initialises the archtype hash-table (init_archetable()). |
|
|
310 | * Reads and parses the archetype file (with the first and second-pass |
|
|
311 | * functions). |
|
|
312 | * Then initialises treasures by calling load_treasures(). |
|
|
313 | */ |
|
|
314 | void |
|
|
315 | init_regions (void) |
201 | init_regions (void) |
316 | { |
202 | { |
317 | char filename[MAX_BUF]; |
203 | if (!regions.size ()) |
318 | int comp; |
204 | { |
319 | |
|
|
320 | if (first_region != NULL) /* Only do this once */ |
|
|
321 | return; |
|
|
322 | |
|
|
323 | // make sure one region is always available |
205 | // make sure one region is always available |
324 | first_region = new region; |
206 | region *rgn = new region; |
325 | first_region->name = "<builtin>"; |
207 | rgn->name = "<builtin>"; |
326 | first_region->longname = strdup ("Built-in Region"); |
208 | rgn->longname = "Built-in Region"; |
327 | |
209 | 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 | { |
210 | } |
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 | } |
211 | } |
|
|
212 | |