1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* Copyright (©) 2001-2003,2007 Mark Wedel & Crossfire Development Team |
6 |
* Copyright (©) 1992,2007 Frank Tore Johansen |
7 |
* |
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 |
10 |
* the Free Software Foundation, either version 3 of the License, or |
11 |
* (at your option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 |
* |
21 |
* The authors can be reached via e-mail to <support@deliantra.net> |
22 |
*/ |
23 |
|
24 |
#include <global.h> |
25 |
#include <unistd.h> |
26 |
|
27 |
regionvec regions; |
28 |
|
29 |
region * |
30 |
region::default_region () |
31 |
{ |
32 |
for_all_regions (rgn) |
33 |
if (rgn->fallback) |
34 |
return rgn; |
35 |
|
36 |
return regions [0]; |
37 |
} |
38 |
|
39 |
/* |
40 |
* Pass a char array, returns a pointer to the region of the same name. |
41 |
* if it can't find a region of the same name it returns the first region |
42 |
* with the 'fallback' property set. |
43 |
* if it can't find a matching name /or/ a fallback region it logs an info message |
44 |
* message and returns NULL |
45 |
* used by the map parsing code. |
46 |
*/ |
47 |
region * |
48 |
region::find (shstr_cmp name) |
49 |
{ |
50 |
for_all_regions (rgn) |
51 |
if (rgn->name == name) |
52 |
return rgn; |
53 |
|
54 |
LOG (llevError, "region called %s requested, but not found, using fallback.\n", &name); |
55 |
|
56 |
return default_region (); |
57 |
} |
58 |
|
59 |
void |
60 |
region::do_destroy () |
61 |
{ |
62 |
regions.erase (this); |
63 |
|
64 |
attachable::do_destroy (); |
65 |
|
66 |
refcnt_dec (); |
67 |
} |
68 |
|
69 |
/* |
70 |
* returns 1 if the player is in the region reg, or a child region thereof |
71 |
* otherwise returns 0 |
72 |
* if passed a NULL region returns -1 |
73 |
*/ |
74 |
static int |
75 |
region_is_child_of_region (const region * child, const region * r) |
76 |
{ |
77 |
if (!r) |
78 |
return -1; |
79 |
|
80 |
if (!child) |
81 |
return 0; |
82 |
|
83 |
if (child->name == r->name) |
84 |
return 1; |
85 |
|
86 |
if (child->parent) |
87 |
return region_is_child_of_region (child->parent, r); |
88 |
|
89 |
return 0; |
90 |
} |
91 |
|
92 |
/** Returns an object which is an exit through which the player represented by op should be |
93 |
* sent in order to be imprisoned. If there is no suitable place to which an exit can be |
94 |
* constructed, then NULL will be returned. The caller is responsible for freeing the object |
95 |
* created by this function. |
96 |
*/ |
97 |
object * |
98 |
get_jail_exit (object *op) |
99 |
{ |
100 |
if (op->type != PLAYER) |
101 |
{ |
102 |
LOG (llevError, "region.c: get_jail_exit called against non-player object.\n"); |
103 |
return NULL; |
104 |
} |
105 |
|
106 |
region *reg = op->region (); |
107 |
while (reg) |
108 |
{ |
109 |
if (reg->jailmap) |
110 |
{ |
111 |
object *exit = object::create (); |
112 |
EXIT_PATH (exit) = reg->jailmap; |
113 |
/* damned exits reset savebed and remove teleports, so the prisoner can't escape */ |
114 |
SET_FLAG (exit, FLAG_DAMNED); |
115 |
EXIT_X (exit) = reg->jailx; |
116 |
EXIT_Y (exit) = reg->jaily; |
117 |
return exit; |
118 |
} |
119 |
else |
120 |
reg = reg->parent; |
121 |
} |
122 |
|
123 |
LOG (llevError, "No suitable jailmap for region %s was found.\n", ®->name); |
124 |
|
125 |
return 0; |
126 |
} |
127 |
|
128 |
region * |
129 |
region::read (object_thawer &f) |
130 |
{ |
131 |
assert (f.kw == KW_region); |
132 |
|
133 |
region *rgn = new region; |
134 |
rgn->refcnt_inc (); |
135 |
|
136 |
f.get (rgn->name); |
137 |
f.next (); |
138 |
|
139 |
for (;;) |
140 |
{ |
141 |
switch (f.kw) |
142 |
{ |
143 |
case KW_parent: |
144 |
rgn->parent = region::find (f.get_str ()); |
145 |
break; |
146 |
|
147 |
case KW_msg: f.get_ml (KW_endmsg, rgn->msg); break; |
148 |
case KW_longname: f.get (rgn->longname); break; |
149 |
case KW_jail_map: f.get (rgn->jailmap); break; |
150 |
case KW_jail_x: f.get (rgn->jailx); break; |
151 |
case KW_jail_y: f.get (rgn->jaily); break; |
152 |
case KW_portal_map: f.get (rgn->portalmap);break; |
153 |
case KW_portal_x: f.get (rgn->portalx); break; |
154 |
case KW_portal_y: f.get (rgn->portaly); break; |
155 |
case KW_fallback: f.get (rgn->fallback); break; |
156 |
case KW_chance: f.get (rgn->treasure_density); break; |
157 |
|
158 |
case KW_randomitems: |
159 |
rgn->treasure = treasurelist::get (f.get_str ()); |
160 |
break; |
161 |
|
162 |
case KW_end: |
163 |
f.next (); |
164 |
|
165 |
// cannot use find as that will request the default region |
166 |
for_all_regions (old) |
167 |
if (old->name == rgn->name) |
168 |
{ |
169 |
old->destroy (); |
170 |
break; |
171 |
} |
172 |
|
173 |
// just append |
174 |
regions.push_back (rgn); |
175 |
return rgn; |
176 |
|
177 |
case KW_ERROR: |
178 |
rgn->set_key_text (f.kw_str, f.value); |
179 |
//fprintf (stderr, "region addkv(%s,%s)\n", f.kw_str, f.value);//D |
180 |
break; |
181 |
|
182 |
default: |
183 |
if (!f.parse_error ("region", rgn->name)) |
184 |
{ |
185 |
rgn->destroy (); |
186 |
return 0; |
187 |
} |
188 |
break; |
189 |
} |
190 |
|
191 |
f.next (); |
192 |
} |
193 |
} |
194 |
|
195 |
/* |
196 |
* First initialises the archtype hash-table (init_archetable()). |
197 |
* Reads and parses the archetype file (with the first and second-pass |
198 |
* functions). |
199 |
*/ |
200 |
void |
201 |
init_regions (void) |
202 |
{ |
203 |
if (!regions.size ()) |
204 |
{ |
205 |
// make sure one region is always available |
206 |
region *rgn = new region; |
207 |
rgn->name = "<builtin>"; |
208 |
rgn->longname = "Built-in Region"; |
209 |
regions.push_back (rgn); |
210 |
} |
211 |
} |
212 |
|