… | |
… | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | * |
21 | * |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
23 | */ |
23 | */ |
24 | |
24 | |
25 | |
|
|
26 | #include <global.h> |
25 | #include <global.h> |
27 | #include <unistd.h> |
26 | #include <unistd.h> |
|
|
27 | |
|
|
28 | region * |
|
|
29 | region::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 | } |
28 | |
37 | |
29 | /* |
38 | /* |
30 | * 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. |
31 | * 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 |
32 | * with the 'fallback' property set. |
41 | * with the 'fallback' property set. |
33 | * 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 |
34 | * message and returns NULL |
43 | * message and returns NULL |
35 | * used by the map parsing code. |
44 | * used by the map parsing code. |
36 | */ |
45 | */ |
37 | region * |
46 | region * |
38 | get_region_by_name (const char *region_name) |
47 | region::find (const char *name) |
39 | { |
48 | { |
40 | region *reg; |
|
|
41 | char *p = strchr (region_name, '\n'); |
|
|
42 | |
|
|
43 | if (p) |
|
|
44 | *p = '\0'; |
|
|
45 | for (reg = first_region; reg != NULL; reg = reg->next) |
49 | for (region *reg = first_region; reg; reg = reg->next) |
46 | if (!strcmp (reg->name, region_name)) |
50 | if (!strcmp (reg->name, name)) |
47 | return reg; |
51 | return reg; |
48 | |
52 | |
49 | for (reg = first_region; reg != NULL; reg = reg->next) |
|
|
50 | { |
|
|
51 | if (reg->fallback) |
|
|
52 | { |
|
|
53 | LOG (llevDebug, "region called %s requested, but not found, fallback used.\n", region_name); |
53 | LOG (llevError, "region called %s requested, but not found, using fallback.\n", name); |
54 | return reg; |
|
|
55 | } |
|
|
56 | } |
|
|
57 | LOG (llevInfo, "Got no region or fallback for region %s.\n", region_name); |
|
|
58 | return NULL; |
|
|
59 | } |
|
|
60 | |
54 | |
61 | /* This might need optimising at some point. */ |
55 | return default_region (); |
62 | region * |
|
|
63 | get_region_by_map (maptile *m) |
|
|
64 | { |
|
|
65 | return get_region_by_name (get_name_of_region_for_map (m)); |
|
|
66 | } |
|
|
67 | |
|
|
68 | /* |
|
|
69 | * Since we won't assume all maps have a region set properly, we need an |
|
|
70 | * explicit check that it is, this is much nicer here than scattered throughout |
|
|
71 | * the map code. |
|
|
72 | */ |
|
|
73 | |
|
|
74 | const char * |
|
|
75 | get_name_of_region_for_map (const maptile *m) |
|
|
76 | { |
|
|
77 | region *reg; |
|
|
78 | |
|
|
79 | if (m->region) |
|
|
80 | return m->region->name; |
|
|
81 | |
|
|
82 | for (reg = first_region; reg != NULL; reg = reg->next) |
|
|
83 | { |
|
|
84 | if (reg->fallback) |
|
|
85 | return reg->name; |
|
|
86 | } |
|
|
87 | |
|
|
88 | LOG (llevInfo, "map %s had no region and I couldn't find a fallback to use.\n", &m->name); |
|
|
89 | return "unknown"; |
|
|
90 | } |
56 | } |
91 | |
57 | |
92 | /* |
58 | /* |
93 | * Tries to find a region that 'name' corresponds to. |
59 | * Tries to find a region that 'name' corresponds to. |
94 | * It looks, in order, for: |
60 | * It looks, in order, for: |
… | |
… | |
100 | * (there should be only one of these - the top level one) |
66 | * (there should be only one of these - the top level one) |
101 | * 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 |
102 | * |
68 | * |
103 | */ |
69 | */ |
104 | region * |
70 | region * |
105 | get_region_from_string (const char *name) |
71 | region::find_fuzzy (const char *name) |
106 | { |
72 | { |
107 | region *reg; |
73 | region *reg; |
108 | char *substr; |
74 | char *substr; |
109 | char *p; |
75 | char *p; |
110 | |
76 | |
111 | if (name == NULL) |
77 | if (name == NULL) |
112 | { |
78 | { |
113 | for (reg = first_region; reg->parent != NULL; reg = reg->parent); |
79 | for (reg = first_region; reg->parent; reg = reg->parent) |
|
|
80 | ; |
|
|
81 | |
114 | return reg; |
82 | return reg; |
115 | } |
83 | } |
|
|
84 | |
116 | p = strchr (name, '\n'); |
85 | p = strchr (name, '\n'); |
117 | if (p) |
86 | if (p) |
118 | *p = '\0'; |
87 | *p = '\0'; |
|
|
88 | |
119 | for (reg = first_region; reg != NULL; reg = reg->next) |
89 | for (reg = first_region; reg; reg = reg->next) |
120 | if (!strcasecmp (reg->name, name)) |
90 | if (!strcasecmp (reg->name, name)) |
121 | return reg; |
91 | return reg; |
122 | |
92 | |
123 | for (reg = first_region; reg != NULL; reg = reg->next) |
93 | for (reg = first_region; reg; reg = reg->next) |
124 | if (reg->longname != NULL) |
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) |
125 | { |
101 | { |
126 | if (!strcasecmp (reg->longname, name)) |
102 | substr = strstr (reg->longname, name); |
|
|
103 | if (substr) |
127 | return reg; |
104 | return reg; |
128 | } |
105 | } |
129 | |
106 | |
130 | substr = NULL; |
|
|
131 | for (reg = first_region; reg != NULL; reg = reg->next) |
107 | for (reg = first_region; reg; reg = reg->next) |
132 | if (reg->longname != NULL) |
108 | if (reg->longname) |
133 | { |
|
|
134 | substr = strstr (reg->longname, name); |
|
|
135 | if (substr != NULL) |
|
|
136 | return reg; |
|
|
137 | } |
|
|
138 | for (reg = first_region; reg != NULL; reg = reg->next) |
|
|
139 | if (reg->longname != NULL) |
|
|
140 | { |
109 | { |
141 | /* |
110 | /* |
142 | * 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 |
143 | * 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 |
144 | * '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 |
145 | * regions with a longname set. |
114 | * regions with a longname set. |
146 | */ |
115 | */ |
147 | substr = strstr (reg->name, name); |
116 | substr = strstr (reg->name, name); |
148 | if (substr != NULL) |
117 | if (substr) |
149 | return reg; |
118 | return reg; |
150 | } |
119 | } |
|
|
120 | |
151 | for (reg = first_region; reg != NULL; reg = reg->next) |
121 | for (reg = first_region; reg; reg = reg->next) |
152 | { |
122 | { |
153 | substr = strstr (reg->name, name); |
123 | substr = strstr (reg->name, name); |
154 | if (substr != NULL) |
124 | if (substr) |
155 | return reg; |
125 | return reg; |
156 | } |
126 | } |
|
|
127 | |
157 | /* 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 */ |
158 | for (reg = first_region; reg->parent != NULL; reg = reg->parent); |
129 | for (reg = first_region; reg->parent; reg = reg->parent) |
|
|
130 | ; |
|
|
131 | |
159 | return reg; |
132 | return reg; |
160 | } |
133 | } |
161 | |
134 | |
162 | /* |
135 | /* |
163 | * 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 |
164 | * otherwise returns 0 |
137 | * otherwise returns 0 |
165 | * if passed a NULL region returns -1 |
138 | * if passed a NULL region returns -1 |
166 | */ |
139 | */ |
167 | |
140 | |
168 | int |
141 | static int |
169 | region_is_child_of_region (const region * child, const region * r) |
142 | region_is_child_of_region (const region * child, const region * r) |
170 | { |
143 | { |
171 | |
144 | |
172 | if (r == NULL) |
145 | if (r == NULL) |
173 | return -1; |
146 | return -1; |
|
|
147 | |
174 | if (child == NULL) |
148 | if (child == NULL) |
175 | return 0; |
149 | return 0; |
|
|
150 | |
176 | if (!strcmp (child->name, r->name)) |
151 | if (!strcmp (child->name, r->name)) |
177 | return 1; |
152 | return 1; |
|
|
153 | |
178 | else if (child->parent != NULL) |
154 | else if (child->parent != NULL) |
179 | return region_is_child_of_region (child->parent, r); |
155 | return region_is_child_of_region (child->parent, r); |
180 | else |
156 | else |
181 | return 0; |
157 | return 0; |
182 | } |
158 | } |
183 | |
159 | |
184 | /* |
|
|
185 | * the longname of a region is not a required field, any given region |
|
|
186 | * may want to not set it and use the parent's one instead. so, we: |
|
|
187 | * 1. check if a longname is set and if so return it. |
|
|
188 | * 2. check if there is a parent and try and call the function against that |
|
|
189 | * 3. return a obviously wrong string if we can't get a longname, this should |
|
|
190 | * never happen. We also log a debug message. |
|
|
191 | */ |
|
|
192 | const char * |
|
|
193 | get_region_longname (const region * r) |
|
|
194 | { |
|
|
195 | |
|
|
196 | if (r->longname != NULL) |
|
|
197 | return r->longname; |
|
|
198 | else if (r->parent != NULL) |
|
|
199 | return get_region_longname (r->parent); |
|
|
200 | else |
|
|
201 | { |
|
|
202 | LOG (llevDebug, "NOTICE region %s has no parent and no longname.\n", r->name); |
|
|
203 | return "no name can be found for the current region"; |
|
|
204 | } |
|
|
205 | } |
|
|
206 | |
|
|
207 | const char * |
|
|
208 | get_region_msg (const region * r) |
|
|
209 | { |
|
|
210 | if (r->msg != NULL) |
|
|
211 | return r->msg; |
|
|
212 | else if (r->parent != NULL) |
|
|
213 | return get_region_msg (r->parent); |
|
|
214 | else |
|
|
215 | { |
|
|
216 | LOG (llevDebug, "NOTICE region %s has no parent and no msg.\n", r->name); |
|
|
217 | return "no description can be found for the current region"; |
|
|
218 | } |
|
|
219 | } |
|
|
220 | |
|
|
221 | /** 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 |
222 | * 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 |
223 | * 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 |
224 | * created by this function. |
163 | * created by this function. |
225 | */ |
164 | */ |
… | |
… | |
232 | if (op->type != PLAYER) |
171 | if (op->type != PLAYER) |
233 | { |
172 | { |
234 | 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"); |
235 | return NULL; |
174 | return NULL; |
236 | } |
175 | } |
237 | reg = get_region_by_map (op->map); |
176 | |
|
|
177 | reg = op->region (); |
238 | while (reg != NULL) |
178 | while (reg) |
239 | { |
179 | { |
240 | if (reg->jailmap) |
180 | if (reg->jailmap) |
241 | { |
181 | { |
242 | exit = object::create (); |
182 | exit = object::create (); |
243 | EXIT_PATH (exit) = reg->jailmap; |
183 | EXIT_PATH (exit) = reg->jailmap; |
… | |
… | |
248 | return exit; |
188 | return exit; |
249 | } |
189 | } |
250 | else |
190 | else |
251 | reg = reg->parent; |
191 | reg = reg->parent; |
252 | } |
192 | } |
|
|
193 | |
253 | 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", ®->name); |
254 | return NULL; |
195 | return NULL; |
|
|
196 | } |
|
|
197 | |
|
|
198 | 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); |
255 | } |
217 | } |
256 | |
218 | |
257 | /* |
219 | /* |
258 | * First initialises the archtype hash-table (init_archetable()). |
220 | * First initialises the archtype hash-table (init_archetable()). |
259 | * 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 |
… | |
… | |
268 | int comp; |
230 | int comp; |
269 | |
231 | |
270 | if (first_region != NULL) /* Only do this once */ |
232 | if (first_region != NULL) /* Only do this once */ |
271 | return; |
233 | return; |
272 | |
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 | |
273 | sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions); |
240 | sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions); |
274 | LOG (llevDebug, "Reading regions from %s...\n", filename); |
241 | LOG (llevDebug, "Reading regions from %s...\n", filename); |
275 | if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
242 | if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
276 | { |
243 | { |
277 | 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); |
278 | return; |
245 | return; |
279 | } |
246 | } |
|
|
247 | |
280 | parse_regions (fp); |
248 | parse_regions (fp); |
281 | assign_region_parents (); |
249 | assign_region_parents (); |
282 | LOG (llevDebug, " done\n"); |
250 | LOG (llevDebug, " done\n"); |
283 | |
251 | |
284 | close_and_delete (fp, comp); |
252 | close_and_delete (fp, comp); |
285 | } |
|
|
286 | |
|
|
287 | /* |
|
|
288 | * Allocates and zeros a region struct, this isn't free()'d anywhere, so might |
|
|
289 | * be a memory leak, but it shouldn't matter too much since it isn't called that |
|
|
290 | * often.... |
|
|
291 | */ |
|
|
292 | |
|
|
293 | region * |
|
|
294 | get_region_struct (void) |
|
|
295 | { |
|
|
296 | return new region; |
|
|
297 | } |
253 | } |
298 | |
254 | |
299 | /* |
255 | /* |
300 | * Reads/parses the region file, and copies into a linked list |
256 | * Reads/parses the region file, and copies into a linked list |
301 | * of region structs. |
257 | * of region structs. |
… | |
… | |
347 | * through. |
303 | * through. |
348 | */ |
304 | */ |
349 | if (!strcmp (key, "region")) |
305 | if (!strcmp (key, "region")) |
350 | { |
306 | { |
351 | *end = 0; |
307 | *end = 0; |
352 | newreg = get_region_struct (); |
308 | newreg = new region; |
353 | newreg->name = strdup (value); |
309 | newreg->name = value; |
354 | } |
310 | } |
355 | else if (!strcmp (key, "parent")) |
311 | else if (!strcmp (key, "parent")) |
356 | { |
312 | { |
357 | /* |
313 | /* |
358 | * 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 |
359 | * 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 |
360 | * parsed. |
316 | * parsed. |
361 | */ |
317 | */ |
362 | *end = 0; |
318 | *end = 0; |
363 | newreg->parent_name = strdup (value); |
319 | newreg->parent_name = value; |
364 | } |
320 | } |
365 | else if (!strcmp (key, "longname")) |
321 | else if (!strcmp (key, "longname")) |
366 | { |
322 | { |
367 | *end = 0; |
323 | *end = 0; |
368 | newreg->longname = strdup (value); |
324 | newreg->longname = strdup (value); |
… | |
… | |
434 | } |
390 | } |
435 | if (!key || strcmp (key, "nomore")) |
391 | if (!key || strcmp (key, "nomore")) |
436 | LOG (llevError, "Got premature eof on regions file!\n"); |
392 | LOG (llevError, "Got premature eof on regions file!\n"); |
437 | } |
393 | } |
438 | |
394 | |
439 | void |
|
|
440 | assign_region_parents (void) |
|
|
441 | { |
|
|
442 | region *reg; |
|
|
443 | uint32 parent_count = 0; |
|
|
444 | uint32 region_count = 0; |
|
|
445 | |
|
|
446 | for (reg = first_region; reg != NULL && reg->next != NULL; reg = reg->next) |
|
|
447 | { |
|
|
448 | if (reg->parent_name != NULL) |
|
|
449 | { |
|
|
450 | reg->parent = get_region_by_name (reg->parent_name); |
|
|
451 | parent_count++; |
|
|
452 | } |
|
|
453 | region_count++; |
|
|
454 | } |
|
|
455 | LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count); |
|
|
456 | } |
|
|