| 1 |
elmex |
1.1 |
/* |
| 2 |
root |
1.71 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
| 3 |
pippijn |
1.37 |
* |
| 4 |
root |
1.73 |
* Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
| 5 |
root |
1.61 |
* Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
| 6 |
|
|
* Copyright (©) 1992,2007 Frank Tore Johansen |
| 7 |
pippijn |
1.37 |
* |
| 8 |
root |
1.71 |
* Deliantra is free software: you can redistribute it and/or modify |
| 9 |
root |
1.68 |
* 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 |
pippijn |
1.37 |
* |
| 13 |
root |
1.68 |
* 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 |
pippijn |
1.37 |
* |
| 18 |
root |
1.68 |
* 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 |
root |
1.61 |
* |
| 21 |
root |
1.71 |
* The authors can be reached via e-mail to <support@deliantra.net> |
| 22 |
pippijn |
1.37 |
*/ |
| 23 |
elmex |
1.1 |
|
| 24 |
root |
1.12 |
#include <cassert> |
| 25 |
|
|
|
| 26 |
elmex |
1.1 |
#include <global.h> |
| 27 |
|
|
#include <loader.h> |
| 28 |
|
|
|
| 29 |
root |
1.38 |
#include <tr1/functional> |
| 30 |
|
|
#include <tr1/unordered_map> |
| 31 |
elmex |
1.1 |
|
| 32 |
root |
1.74 |
archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing |
| 33 |
|
|
arch_ptr archetype::empty; |
| 34 |
root |
1.46 |
|
| 35 |
root |
1.12 |
// the hashtable |
| 36 |
root |
1.18 |
typedef std::tr1::unordered_map |
| 37 |
|
|
< |
| 38 |
root |
1.38 |
const char *, |
| 39 |
root |
1.26 |
arch_ptr, |
| 40 |
root |
1.38 |
str_hash, |
| 41 |
|
|
str_equal, |
| 42 |
|
|
slice_allocator< std::pair<const char *const, arch_ptr> > |
| 43 |
root |
1.18 |
> HT; |
| 44 |
|
|
|
| 45 |
root |
1.74 |
static HT ht (10000); |
| 46 |
root |
1.62 |
archvec archetypes; |
| 47 |
root |
1.74 |
static unordered_vector<archetype *> allarch; |
| 48 |
|
|
static int dirtycnt; |
| 49 |
root |
1.12 |
|
| 50 |
root |
1.73 |
// the vector of other_arch references to be resolved |
| 51 |
|
|
static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; |
| 52 |
|
|
// the vector of loaded but not yet committed archetypes |
| 53 |
|
|
static std::vector<archetype *> postponed_arch; |
| 54 |
|
|
|
| 55 |
elmex |
1.1 |
/** |
| 56 |
|
|
* GROS - This function retrieves an archetype given the name that appears |
| 57 |
|
|
* during the game (for example, "writing pen" instead of "stylus"). |
| 58 |
|
|
* It does not use the hashtable system, but browse the whole archlist each time. |
| 59 |
|
|
* I suggest not to use it unless you really need it because of performance issue. |
| 60 |
|
|
* It is currently used by scripting extensions (create-object). |
| 61 |
|
|
* Params: |
| 62 |
|
|
* - name: the name we're searching for (ex: "writing pen"); |
| 63 |
|
|
* Return value: |
| 64 |
|
|
* - the archetype found or null if nothing was found. |
| 65 |
|
|
*/ |
| 66 |
root |
1.12 |
archetype * |
| 67 |
|
|
find_archetype_by_object_name (const char *name) |
| 68 |
|
|
{ |
| 69 |
root |
1.39 |
shstr_cmp name_cmp (name); |
| 70 |
elmex |
1.1 |
|
| 71 |
root |
1.63 |
for_all_archetypes (at) |
| 72 |
root |
1.66 |
if (at->name == name_cmp) |
| 73 |
root |
1.39 |
return at; |
| 74 |
elmex |
1.1 |
|
| 75 |
root |
1.39 |
return 0; |
| 76 |
elmex |
1.1 |
} |
| 77 |
|
|
|
| 78 |
|
|
/** |
| 79 |
|
|
* This function retrieves an archetype by type and name that appears during |
| 80 |
|
|
* the game. It is basically the same as find_archetype_by_object_name() |
| 81 |
|
|
* except that it considers only items of the given type. |
| 82 |
|
|
*/ |
| 83 |
root |
1.12 |
archetype * |
| 84 |
|
|
find_archetype_by_object_type_name (int type, const char *name) |
| 85 |
|
|
{ |
| 86 |
root |
1.39 |
shstr_cmp name_cmp (name); |
| 87 |
elmex |
1.1 |
|
| 88 |
root |
1.63 |
for_all_archetypes (at) |
| 89 |
root |
1.66 |
if (at->name == name_cmp && at->type == type) |
| 90 |
root |
1.39 |
return at; |
| 91 |
elmex |
1.1 |
|
| 92 |
root |
1.39 |
return 0; |
| 93 |
elmex |
1.1 |
} |
| 94 |
|
|
|
| 95 |
|
|
/* This is a lot like the above function. Instead, we are trying to match |
| 96 |
|
|
* the arch->skill values. type is the type of object to match |
| 97 |
|
|
* against (eg, to only match against skills or only skill objects for example). |
| 98 |
|
|
* If type is -1, ew don't match on type. |
| 99 |
|
|
*/ |
| 100 |
root |
1.12 |
object * |
| 101 |
|
|
get_archetype_by_skill_name (const char *skill, int type) |
| 102 |
|
|
{ |
| 103 |
root |
1.39 |
shstr_cmp skill_cmp (skill); |
| 104 |
elmex |
1.1 |
|
| 105 |
root |
1.63 |
for_all_archetypes (at) |
| 106 |
|
|
if (at->skill == skill_cmp && (type == -1 || type == at->type)) |
| 107 |
root |
1.39 |
return arch_to_object (at); |
| 108 |
root |
1.35 |
|
| 109 |
|
|
return 0; |
| 110 |
elmex |
1.1 |
} |
| 111 |
|
|
|
| 112 |
|
|
/* similiar to above - this returns the first archetype |
| 113 |
|
|
* that matches both the type and subtype. type and subtype |
| 114 |
|
|
* can be -1 to say ignore, but in this case, the match it does |
| 115 |
|
|
* may not be very useful. This function is most useful when |
| 116 |
|
|
* subtypes are known to be unique for a particular type |
| 117 |
|
|
* (eg, skills) |
| 118 |
|
|
*/ |
| 119 |
root |
1.12 |
archetype * |
| 120 |
|
|
get_archetype_by_type_subtype (int type, int subtype) |
| 121 |
|
|
{ |
| 122 |
root |
1.63 |
for_all_archetypes (at) |
| 123 |
|
|
if ((type == -1 || type == at->type) && (subtype == -1 || subtype == at->subtype)) |
| 124 |
root |
1.39 |
return at; |
| 125 |
elmex |
1.1 |
|
| 126 |
root |
1.39 |
return 0; |
| 127 |
elmex |
1.1 |
} |
| 128 |
|
|
|
| 129 |
|
|
/** |
| 130 |
|
|
* GROS - this returns a new object given the name that appears during the game |
| 131 |
|
|
* (for example, "writing pen" instead of "stylus"). |
| 132 |
|
|
* Params: |
| 133 |
|
|
* - name: The name we're searching for (ex: "writing pen"); |
| 134 |
|
|
* Return value: |
| 135 |
|
|
* - a corresponding object if found; a singularity object if not found. |
| 136 |
|
|
* Note by MSW - it appears that it takes the full name and keeps |
| 137 |
|
|
* shortening it until it finds a match. I re-wrote this so that it |
| 138 |
|
|
* doesn't malloc it each time - not that this function is used much, |
| 139 |
|
|
* but it otherwise had a big memory leak. |
| 140 |
|
|
*/ |
| 141 |
root |
1.12 |
object * |
| 142 |
|
|
get_archetype_by_object_name (const char *name) |
| 143 |
|
|
{ |
| 144 |
root |
1.17 |
char tmpname[MAX_BUF]; |
| 145 |
|
|
int i; |
| 146 |
|
|
|
| 147 |
|
|
assign (tmpname, name); |
| 148 |
root |
1.12 |
|
| 149 |
|
|
for (i = strlen (tmpname); i > 0; i--) |
| 150 |
|
|
{ |
| 151 |
|
|
tmpname[i] = 0; |
| 152 |
root |
1.17 |
|
| 153 |
root |
1.39 |
if (archetype *at = find_archetype_by_object_name (tmpname)) |
| 154 |
root |
1.20 |
return arch_to_object (at); |
| 155 |
elmex |
1.1 |
} |
| 156 |
root |
1.17 |
|
| 157 |
root |
1.12 |
return create_singularity (name); |
| 158 |
elmex |
1.1 |
} |
| 159 |
|
|
|
| 160 |
root |
1.65 |
/* This is a subset of the parse_id command. Basically, name can be |
| 161 |
|
|
* a string seperated lists of things to match, with certain keywords. |
| 162 |
|
|
* pl is the player (only needed to set count properly) |
| 163 |
|
|
* op is the item we are trying to match. Calling function takes care |
| 164 |
|
|
* of what action might need to be done and if it is valid |
| 165 |
|
|
* (pickup, drop, etc.) Return NONZERO if we have a match. A higher |
| 166 |
|
|
* value means a better match. 0 means no match. |
| 167 |
|
|
* |
| 168 |
|
|
* Brief outline of the procedure: |
| 169 |
|
|
* We take apart the name variable into the individual components. |
| 170 |
|
|
* cases for 'all' and unpaid are pretty obvious. |
| 171 |
|
|
* Next, we check for a count (either specified in name, or in the |
| 172 |
|
|
* player object.) |
| 173 |
|
|
* If count is 1, make a quick check on the name. |
| 174 |
|
|
* IF count is >1, we need to make plural name. Return if match. |
| 175 |
|
|
* Last, make a check on the full name. |
| 176 |
|
|
*/ |
| 177 |
root |
1.12 |
int |
| 178 |
root |
1.15 |
item_matched_string (object *pl, object *op, const char *name) |
| 179 |
elmex |
1.1 |
{ |
| 180 |
root |
1.22 |
char *cp, local_name[MAX_BUF]; |
| 181 |
|
|
int count, retval = 0; |
| 182 |
root |
1.15 |
|
| 183 |
root |
1.40 |
assign (local_name, name); /* strtok is destructive to name */ |
| 184 |
root |
1.12 |
|
| 185 |
|
|
for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
| 186 |
|
|
{ |
| 187 |
|
|
while (cp[0] == ' ') |
| 188 |
|
|
++cp; /* get rid of spaces */ |
| 189 |
|
|
|
| 190 |
|
|
/* LOG(llevDebug,"Trying to match %s\n", cp); */ |
| 191 |
|
|
/* All is a very generic match - low match value */ |
| 192 |
|
|
if (!strcmp (cp, "all")) |
| 193 |
|
|
return 1; |
| 194 |
|
|
|
| 195 |
|
|
/* unpaid is a little more specific */ |
| 196 |
|
|
if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID)) |
| 197 |
|
|
return 2; |
| 198 |
|
|
if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED))) |
| 199 |
|
|
return 2; |
| 200 |
|
|
|
| 201 |
|
|
if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED)) |
| 202 |
|
|
return 2; |
| 203 |
|
|
|
| 204 |
|
|
/* Allow for things like '100 arrows' */ |
| 205 |
|
|
if ((count = atoi (cp)) != 0) |
| 206 |
|
|
{ |
| 207 |
|
|
cp = strchr (cp, ' '); |
| 208 |
|
|
while (cp && cp[0] == ' ') |
| 209 |
|
|
++cp; /* get rid of spaces */ |
| 210 |
|
|
} |
| 211 |
|
|
else |
| 212 |
|
|
{ |
| 213 |
|
|
if (pl->type == PLAYER) |
| 214 |
|
|
count = pl->contr->count; |
| 215 |
|
|
else |
| 216 |
|
|
count = 0; |
| 217 |
|
|
} |
| 218 |
|
|
|
| 219 |
|
|
if (!cp || cp[0] == '\0' || count < 0) |
| 220 |
|
|
return 0; |
| 221 |
|
|
|
| 222 |
|
|
|
| 223 |
|
|
/* The code here should go from highest retval to lowest. That |
| 224 |
|
|
* is because of the 'else' handling - we don't want to match on |
| 225 |
|
|
* something and set a low retval, even though it may match a higher retcal |
| 226 |
|
|
* later. So keep it in descending order here, so we try for the best |
| 227 |
|
|
* match first, and work downward. |
| 228 |
|
|
*/ |
| 229 |
|
|
if (!strcasecmp (cp, query_name (op))) |
| 230 |
|
|
retval = 20; |
| 231 |
|
|
else if (!strcasecmp (cp, query_short_name (op))) |
| 232 |
|
|
retval = 18; |
| 233 |
|
|
else if (!strcasecmp (cp, query_base_name (op, 0))) |
| 234 |
|
|
retval = 16; |
| 235 |
|
|
else if (!strcasecmp (cp, query_base_name (op, 1))) |
| 236 |
|
|
retval = 16; |
| 237 |
|
|
else if (op->custom_name && !strcasecmp (cp, op->custom_name)) |
| 238 |
|
|
retval = 15; |
| 239 |
|
|
else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
| 240 |
|
|
retval = 14; |
| 241 |
|
|
else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
| 242 |
|
|
retval = 14; |
| 243 |
|
|
/* Do substring checks, so things like 'Str+1' will match. |
| 244 |
|
|
* retval of these should perhaps be lower - they are lower |
| 245 |
|
|
* then the specific strcasecmp aboves, but still higher than |
| 246 |
|
|
* some other match criteria. |
| 247 |
|
|
*/ |
| 248 |
|
|
else if (strstr (query_base_name (op, 1), cp)) |
| 249 |
|
|
retval = 12; |
| 250 |
|
|
else if (strstr (query_base_name (op, 0), cp)) |
| 251 |
|
|
retval = 12; |
| 252 |
|
|
else if (strstr (query_short_name (op), cp)) |
| 253 |
|
|
retval = 12; |
| 254 |
|
|
/* Check against plural/non plural based on count. */ |
| 255 |
|
|
else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
| 256 |
root |
1.23 |
retval = 6; |
| 257 |
root |
1.12 |
else if (count == 1 && !strcasecmp (op->name, cp)) |
| 258 |
root |
1.23 |
retval = 6; |
| 259 |
root |
1.12 |
/* base name matched - not bad */ |
| 260 |
|
|
else if (strcasecmp (cp, op->name) == 0 && !count) |
| 261 |
|
|
retval = 4; |
| 262 |
|
|
/* Check for partial custom name, but give a real low priority */ |
| 263 |
|
|
else if (op->custom_name && strstr (op->custom_name, cp)) |
| 264 |
|
|
retval = 3; |
| 265 |
|
|
|
| 266 |
|
|
if (retval) |
| 267 |
|
|
{ |
| 268 |
|
|
if (pl->type == PLAYER) |
| 269 |
|
|
pl->contr->count = count; |
| 270 |
root |
1.23 |
|
| 271 |
root |
1.12 |
return retval; |
| 272 |
root |
1.7 |
} |
| 273 |
elmex |
1.1 |
} |
| 274 |
root |
1.23 |
|
| 275 |
root |
1.12 |
return 0; |
| 276 |
elmex |
1.1 |
} |
| 277 |
|
|
|
| 278 |
root |
1.63 |
archetype::archetype (const char *name) |
| 279 |
root |
1.12 |
{ |
| 280 |
root |
1.63 |
arch = this; |
| 281 |
|
|
this->archname = this->name = this->name_pl = name; |
| 282 |
root |
1.46 |
} |
| 283 |
root |
1.12 |
|
| 284 |
root |
1.46 |
archetype::~archetype () |
| 285 |
|
|
{ |
| 286 |
root |
1.63 |
unlink (); |
| 287 |
elmex |
1.1 |
} |
| 288 |
|
|
|
| 289 |
root |
1.63 |
void |
| 290 |
|
|
archetype::link () |
| 291 |
elmex |
1.1 |
{ |
| 292 |
root |
1.63 |
if (!archetypes.contains (this)) |
| 293 |
root |
1.74 |
{ |
| 294 |
|
|
archetypes.insert (this); |
| 295 |
|
|
ht.insert (std::make_pair (archname, this)); |
| 296 |
|
|
} |
| 297 |
elmex |
1.1 |
} |
| 298 |
|
|
|
| 299 |
root |
1.46 |
void |
| 300 |
root |
1.63 |
archetype::unlink () |
| 301 |
|
|
{ |
| 302 |
|
|
if (archetypes.contains (this)) |
| 303 |
root |
1.74 |
{ |
| 304 |
|
|
archetypes.erase (this); |
| 305 |
|
|
ht.erase (archname); |
| 306 |
|
|
} |
| 307 |
root |
1.63 |
} |
| 308 |
|
|
|
| 309 |
|
|
/* |
| 310 |
|
|
* Finds, using the hashtable, which archetype matches the given name. |
| 311 |
|
|
* returns a pointer to the found archetype, otherwise NULL. |
| 312 |
|
|
*/ |
| 313 |
|
|
archetype * |
| 314 |
|
|
archetype::find (const char *name) |
| 315 |
|
|
{ |
| 316 |
|
|
if (!name) |
| 317 |
|
|
return 0; |
| 318 |
|
|
|
| 319 |
|
|
auto (i, ht.find (name)); |
| 320 |
|
|
|
| 321 |
|
|
if (i == ht.end ()) |
| 322 |
|
|
return 0; |
| 323 |
|
|
else |
| 324 |
|
|
return i->second; |
| 325 |
|
|
} |
| 326 |
|
|
|
| 327 |
|
|
archetype * |
| 328 |
root |
1.53 |
archetype::read (object_thawer &f) |
| 329 |
root |
1.12 |
{ |
| 330 |
root |
1.43 |
assert (f.kw == KW_object); |
| 331 |
|
|
|
| 332 |
root |
1.63 |
std::vector<archetype *> parts; |
| 333 |
root |
1.12 |
|
| 334 |
root |
1.72 |
coroapi::cede_to_tick (); |
| 335 |
root |
1.52 |
|
| 336 |
root |
1.49 |
for (;;) |
| 337 |
root |
1.43 |
{ |
| 338 |
root |
1.73 |
archetype *at = new archetype (f.get_str ()); |
| 339 |
root |
1.63 |
|
| 340 |
root |
1.54 |
f.next (); |
| 341 |
root |
1.49 |
|
| 342 |
root |
1.56 |
#if 0 |
| 343 |
root |
1.63 |
// implementing it here in the server does neither allow multiple inheritence |
| 344 |
|
|
// nor does it cleanly "just override". it would allow use in map files, though, |
| 345 |
|
|
// and other resource files dynamically laoded (as opposed to being preprocessed). |
| 346 |
|
|
// not that any of this is relevant as of yet... |
| 347 |
root |
1.55 |
if (f.kw == KW_inherit) |
| 348 |
|
|
{ |
| 349 |
|
|
if (archetype *at = find (f.get_str ())) |
| 350 |
|
|
*op = at->clone; |
| 351 |
|
|
else |
| 352 |
|
|
LOG (llevError, "archetype '%s' tries to inherit from non-existent archetype '%s'.\n", |
| 353 |
root |
1.63 |
&at->archname, f.get_str ()); |
| 354 |
root |
1.55 |
|
| 355 |
|
|
f.next (); |
| 356 |
|
|
} |
| 357 |
root |
1.56 |
#endif |
| 358 |
root |
1.55 |
|
| 359 |
root |
1.69 |
loading_arch = at; // hack to tell parse_kv et al. to behave |
| 360 |
|
|
bool parse_ok = at->parse_kv (f); |
| 361 |
|
|
loading_arch = 0; |
| 362 |
|
|
|
| 363 |
|
|
if (!parse_ok) |
| 364 |
root |
1.49 |
goto fail; |
| 365 |
|
|
|
| 366 |
root |
1.69 |
loading_arch = at; // hack to tell parse_kv et al. to behave |
| 367 |
root |
1.63 |
at->post_load_check (); |
| 368 |
root |
1.69 |
loading_arch = 0; |
| 369 |
root |
1.59 |
|
| 370 |
root |
1.63 |
parts.push_back (at); |
| 371 |
root |
1.12 |
|
| 372 |
root |
1.49 |
if (f.kw != KW_more) |
| 373 |
|
|
break; |
| 374 |
root |
1.46 |
|
| 375 |
root |
1.43 |
f.next (); |
| 376 |
root |
1.54 |
|
| 377 |
|
|
if (f.kw != KW_object) |
| 378 |
|
|
{ |
| 379 |
|
|
f.parse_error ("more object"); |
| 380 |
|
|
goto fail; |
| 381 |
|
|
} |
| 382 |
root |
1.49 |
} |
| 383 |
root |
1.43 |
|
| 384 |
root |
1.49 |
{ |
| 385 |
root |
1.63 |
auto (at, parts.begin ()); |
| 386 |
root |
1.12 |
|
| 387 |
root |
1.63 |
archetype *new_head = parts.front (); |
| 388 |
|
|
archetype *old_head = find (new_head->archname); |
| 389 |
|
|
|
| 390 |
root |
1.64 |
if (old_head && !old_head->is_head ()) |
| 391 |
|
|
{ |
| 392 |
|
|
LOG (llevError, "%s: unable to overwrite non-head archetype '%s' with head archetype, skipping.\n", |
| 393 |
|
|
&new_head->archname, &old_head->archname); |
| 394 |
|
|
goto fail; |
| 395 |
|
|
} |
| 396 |
root |
1.63 |
|
| 397 |
|
|
// check that all archetypes belong to the same old object or are new |
| 398 |
|
|
for (auto (at, parts.begin ()); at != parts.end (); ++at) |
| 399 |
root |
1.49 |
{ |
| 400 |
root |
1.63 |
archetype *new_part = *at; |
| 401 |
|
|
archetype *old_part = find (new_part->archname); |
| 402 |
root |
1.49 |
|
| 403 |
root |
1.63 |
if (old_part && old_part->head_ () != old_head) |
| 404 |
root |
1.49 |
{ |
| 405 |
root |
1.63 |
LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n", |
| 406 |
|
|
&new_part->archname, &((archetype *)old_part->head_ ())->archname); |
| 407 |
root |
1.49 |
goto fail; |
| 408 |
|
|
} |
| 409 |
|
|
} |
| 410 |
|
|
|
| 411 |
root |
1.64 |
// assemble new chain |
| 412 |
root |
1.63 |
new_head->min_x = new_head->max_x = new_head->x; |
| 413 |
|
|
new_head->min_y = new_head->max_y = new_head->y; |
| 414 |
root |
1.49 |
|
| 415 |
root |
1.63 |
archetype *less = new_head; |
| 416 |
root |
1.51 |
for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
| 417 |
root |
1.49 |
{ |
| 418 |
root |
1.63 |
archetype *at = *p; |
| 419 |
root |
1.49 |
|
| 420 |
root |
1.74 |
// some flags get inherited from the head (probably a lot more) |
| 421 |
root |
1.67 |
// doing it here doesn't feel too cozy, but it allows code |
| 422 |
|
|
// to ignore head checks for these flags, which saves time |
| 423 |
|
|
at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
| 424 |
|
|
at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
| 425 |
|
|
at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
| 426 |
|
|
at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
| 427 |
|
|
|
| 428 |
root |
1.74 |
new_head->min_x = min (new_head->min_x, at->x); |
| 429 |
|
|
new_head->min_y = min (new_head->min_y, at->y); |
| 430 |
|
|
new_head->max_x = max (new_head->max_x, at->x); |
| 431 |
|
|
new_head->max_y = max (new_head->max_y, at->y); |
| 432 |
root |
1.49 |
|
| 433 |
root |
1.64 |
at->head = new_head; |
| 434 |
|
|
less->more = at; |
| 435 |
|
|
less = at; |
| 436 |
root |
1.63 |
} |
| 437 |
root |
1.49 |
|
| 438 |
root |
1.73 |
postponed_arch.insert (postponed_arch.end (), parts.begin (), parts.end ()); |
| 439 |
root |
1.53 |
|
| 440 |
root |
1.63 |
return new_head; |
| 441 |
root |
1.49 |
} |
| 442 |
root |
1.43 |
|
| 443 |
root |
1.49 |
fail: |
| 444 |
root |
1.51 |
for (auto (p, parts.begin ()); p != parts.end (); ++p) |
| 445 |
root |
1.63 |
(*p)->destroy (true); |
| 446 |
root |
1.43 |
|
| 447 |
root |
1.53 |
return 0; |
| 448 |
root |
1.43 |
} |
| 449 |
|
|
|
| 450 |
root |
1.53 |
void |
| 451 |
root |
1.73 |
archetype::postpone_arch_ref (arch_ptr &ref, const_utf8_string other_arch) |
| 452 |
|
|
{ |
| 453 |
|
|
ref = 0; |
| 454 |
|
|
postponed_arch_ref.push_back (std::pair<arch_ptr *, shstr>(&ref, shstr (other_arch))); |
| 455 |
|
|
} |
| 456 |
|
|
|
| 457 |
|
|
void |
| 458 |
|
|
archetype::commit_load () |
| 459 |
root |
1.43 |
{ |
| 460 |
root |
1.73 |
// unlink old archetypes and link in new ones */ |
| 461 |
|
|
for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p) |
| 462 |
|
|
{ |
| 463 |
|
|
archetype *at = *p; |
| 464 |
|
|
|
| 465 |
|
|
if (archetype *old = find (at->archname)) |
| 466 |
|
|
old->unlink (); |
| 467 |
|
|
|
| 468 |
root |
1.74 |
allarch.push_back (at); |
| 469 |
|
|
|
| 470 |
root |
1.73 |
at->link (); |
| 471 |
root |
1.74 |
++dirtycnt; |
| 472 |
root |
1.73 |
} |
| 473 |
|
|
|
| 474 |
|
|
postponed_arch.clear (); |
| 475 |
|
|
|
| 476 |
|
|
// now resolve arch references |
| 477 |
|
|
for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p) |
| 478 |
|
|
{ |
| 479 |
|
|
arch_ptr *ap = p->first; |
| 480 |
|
|
archetype *at = find (p->second); |
| 481 |
|
|
|
| 482 |
|
|
if (!at) |
| 483 |
|
|
LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second); |
| 484 |
|
|
|
| 485 |
|
|
*ap = at; |
| 486 |
|
|
} |
| 487 |
|
|
|
| 488 |
|
|
postponed_arch_ref.clear (); |
| 489 |
|
|
|
| 490 |
|
|
empty = find (shstr_empty_archetype); |
| 491 |
elmex |
1.1 |
} |
| 492 |
|
|
|
| 493 |
root |
1.74 |
void |
| 494 |
|
|
archetype::gc () |
| 495 |
|
|
{ |
| 496 |
|
|
int cnt = max (1, min (allarch.size () / 128, dirtycnt)); |
| 497 |
|
|
dirtycnt = max (0, dirtycnt - cnt); |
| 498 |
|
|
|
| 499 |
|
|
do |
| 500 |
|
|
{ |
| 501 |
|
|
static int idx; |
| 502 |
|
|
|
| 503 |
|
|
if (idx >= allarch.size ()) |
| 504 |
|
|
if (idx) |
| 505 |
|
|
idx = 0; |
| 506 |
|
|
else |
| 507 |
|
|
return; |
| 508 |
|
|
|
| 509 |
|
|
archetype *at = allarch [idx]; |
| 510 |
|
|
|
| 511 |
|
|
if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object |
| 512 |
|
|
++idx; |
| 513 |
|
|
else |
| 514 |
|
|
{ |
| 515 |
|
|
LOG (llevDebug, "garbage collect arch %s", at->debug_desc ()); |
| 516 |
|
|
assert (at->arch == at); // verify that refcnt == 1 is truely valid |
| 517 |
|
|
allarch.erase (idx); |
| 518 |
|
|
at->arch = 0; |
| 519 |
|
|
at->more = 0; |
| 520 |
|
|
at->destroy (); |
| 521 |
|
|
} |
| 522 |
|
|
} |
| 523 |
|
|
while (--cnt); |
| 524 |
|
|
} |
| 525 |
|
|
|
| 526 |
elmex |
1.1 |
/* |
| 527 |
|
|
* Creates and returns a new object which is a copy of the given archetype. |
| 528 |
|
|
* This function returns NULL on failure. |
| 529 |
|
|
*/ |
| 530 |
root |
1.12 |
object * |
| 531 |
root |
1.15 |
arch_to_object (archetype *at) |
| 532 |
root |
1.12 |
{ |
| 533 |
root |
1.43 |
if (!at) |
| 534 |
root |
1.12 |
{ |
| 535 |
root |
1.53 |
LOG (llevError, "Couldn't find archetype.\n"); |
| 536 |
|
|
return 0; |
| 537 |
root |
1.12 |
} |
| 538 |
root |
1.15 |
|
| 539 |
root |
1.63 |
object *op = at->clone (); |
| 540 |
root |
1.15 |
op->arch = at; |
| 541 |
root |
1.2 |
op->instantiate (); |
| 542 |
root |
1.53 |
|
| 543 |
elmex |
1.1 |
return op; |
| 544 |
|
|
} |
| 545 |
|
|
|
| 546 |
root |
1.70 |
object * |
| 547 |
|
|
archetype::instance () |
| 548 |
|
|
{ |
| 549 |
|
|
return arch_to_object (this); |
| 550 |
|
|
} |
| 551 |
|
|
|
| 552 |
elmex |
1.1 |
/* |
| 553 |
|
|
* Creates an object. This function is called by get_archetype() |
| 554 |
|
|
* if it fails to find the appropriate archetype. |
| 555 |
|
|
* Thus get_archetype() will be guaranteed to always return |
| 556 |
|
|
* an object, and never NULL. |
| 557 |
|
|
*/ |
| 558 |
root |
1.12 |
object * |
| 559 |
|
|
create_singularity (const char *name) |
| 560 |
|
|
{ |
| 561 |
root |
1.65 |
LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
| 562 |
|
|
|
| 563 |
|
|
if (!strcmp (name, "bug")) |
| 564 |
|
|
abort (); |
| 565 |
|
|
|
| 566 |
root |
1.20 |
char buf[MAX_BUF]; |
| 567 |
root |
1.66 |
sprintf (buf, "bug, please report (%s)", name); |
| 568 |
root |
1.15 |
|
| 569 |
root |
1.65 |
object *op = get_archetype ("bug"); |
| 570 |
root |
1.10 |
op->name = op->name_pl = buf; |
| 571 |
root |
1.65 |
|
| 572 |
elmex |
1.1 |
return op; |
| 573 |
|
|
} |
| 574 |
|
|
|
| 575 |
|
|
/* |
| 576 |
|
|
* Finds which archetype matches the given name, and returns a new |
| 577 |
|
|
* object containing a copy of the archetype. |
| 578 |
|
|
*/ |
| 579 |
root |
1.12 |
object * |
| 580 |
|
|
get_archetype (const char *name) |
| 581 |
|
|
{ |
| 582 |
root |
1.20 |
archetype *at = archetype::find (name); |
| 583 |
root |
1.15 |
|
| 584 |
root |
1.20 |
if (!at) |
| 585 |
root |
1.12 |
return create_singularity (name); |
| 586 |
root |
1.15 |
|
| 587 |
root |
1.12 |
return arch_to_object (at); |
| 588 |
elmex |
1.1 |
} |
| 589 |
|
|
|
| 590 |
root |
1.76 |
object * |
| 591 |
|
|
archetype::get (const char *name) |
| 592 |
|
|
{ |
| 593 |
|
|
return get_archetype (name); |
| 594 |
|
|
} |
| 595 |
|
|
|
| 596 |
elmex |
1.1 |
/* |
| 597 |
|
|
* Returns the first archetype using the given type. |
| 598 |
|
|
* Used in treasure-generation. |
| 599 |
|
|
*/ |
| 600 |
root |
1.12 |
archetype * |
| 601 |
|
|
type_to_archetype (int type) |
| 602 |
|
|
{ |
| 603 |
root |
1.63 |
for_all_archetypes (at) |
| 604 |
|
|
if (at->type == type && at->head_ () != at) |
| 605 |
elmex |
1.1 |
return at; |
| 606 |
root |
1.20 |
|
| 607 |
|
|
return 0; |
| 608 |
elmex |
1.1 |
} |
| 609 |
|
|
|
| 610 |
|
|
/* |
| 611 |
|
|
* Returns a new object copied from the first archetype matching |
| 612 |
|
|
* the given type. |
| 613 |
|
|
* Used in treasure-generation. |
| 614 |
|
|
*/ |
| 615 |
root |
1.12 |
object * |
| 616 |
|
|
clone_arch (int type) |
| 617 |
|
|
{ |
| 618 |
root |
1.20 |
archetype *at; |
| 619 |
elmex |
1.1 |
|
| 620 |
root |
1.12 |
if ((at = type_to_archetype (type)) == NULL) |
| 621 |
|
|
{ |
| 622 |
|
|
LOG (llevError, "Can't clone archetype %d\n", type); |
| 623 |
root |
1.30 |
return 0; |
| 624 |
root |
1.12 |
} |
| 625 |
root |
1.20 |
|
| 626 |
root |
1.63 |
object *op = at->clone (); |
| 627 |
root |
1.3 |
op->instantiate (); |
| 628 |
elmex |
1.1 |
return op; |
| 629 |
|
|
} |
| 630 |
|
|
|
| 631 |
|
|
/* |
| 632 |
|
|
* member: make instance from class |
| 633 |
|
|
*/ |
| 634 |
root |
1.12 |
object * |
| 635 |
root |
1.15 |
object_create_arch (archetype *at) |
| 636 |
elmex |
1.1 |
{ |
| 637 |
root |
1.20 |
object *op, *prev = 0, *head = 0; |
| 638 |
elmex |
1.1 |
|
| 639 |
root |
1.12 |
while (at) |
| 640 |
|
|
{ |
| 641 |
|
|
op = arch_to_object (at); |
| 642 |
root |
1.63 |
|
| 643 |
|
|
op->x = at->x; |
| 644 |
|
|
op->y = at->y; |
| 645 |
root |
1.20 |
|
| 646 |
root |
1.12 |
if (head) |
| 647 |
|
|
op->head = head, prev->more = op; |
| 648 |
root |
1.20 |
|
| 649 |
root |
1.12 |
if (!head) |
| 650 |
|
|
head = op; |
| 651 |
root |
1.20 |
|
| 652 |
root |
1.12 |
prev = op; |
| 653 |
root |
1.63 |
at = (archetype *)at->more; |
| 654 |
elmex |
1.1 |
} |
| 655 |
root |
1.20 |
|
| 656 |
root |
1.63 |
return head; |
| 657 |
elmex |
1.1 |
} |
| 658 |
|
|
|