1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
|
|
4 | * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / 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 |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
8 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify |
9 | * 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 |
10 | * the terms of the Affero GNU General Public License as published by the |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
12 | * option) any later version. |
12 | * |
13 | * |
13 | * 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, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * GNU General Public License for more details. |
17 | * |
18 | * |
18 | * 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 |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * and the GNU General Public License along with this program. If not, see |
|
|
21 | * <http://www.gnu.org/licenses/>. |
20 | * |
22 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
24 | */ |
23 | |
25 | |
24 | #include <cassert> |
26 | #include <cassert> |
|
|
27 | #include <functional> |
|
|
28 | |
|
|
29 | #include <flat_hash_map.hpp> |
25 | |
30 | |
26 | #include <global.h> |
31 | #include <global.h> |
27 | #include <funcpoint.h> |
|
|
28 | #include <loader.h> |
|
|
29 | |
32 | |
30 | #include <tr1/functional> |
|
|
31 | #include <tr1/unordered_map> |
|
|
32 | |
|
|
33 | archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing |
33 | archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing |
34 | archetype *archetype::empty; |
34 | arch_ptr archetype::empty; |
35 | |
35 | |
36 | // the hashtable |
36 | // the hashtable |
37 | typedef std::tr1::unordered_map |
37 | typedef ska::flat_hash_map |
38 | < |
38 | < |
39 | const char *, |
39 | const char *, |
40 | arch_ptr, |
40 | arch_ptr, |
41 | str_hash, |
41 | str_hash, |
42 | str_equal, |
42 | str_equal, |
43 | slice_allocator< std::pair<const char *const, arch_ptr> > |
43 | slice_allocator< std::pair<const char *const, arch_ptr> > |
44 | > HT; |
44 | > HT; |
45 | |
45 | |
46 | static HT ht (5000); |
46 | static HT ht (10000); |
47 | archvec archetypes; |
47 | archvec archetypes; |
|
|
48 | static unordered_vector<archetype *> allarch; |
|
|
49 | static int dirtycnt; |
48 | |
50 | |
49 | // the vector of other_arch references to be resolved |
51 | // the vector of other_arch references to be resolved |
50 | static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; |
52 | static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; |
51 | // the vector of loaded but not yet committed archetypes |
53 | // the vector of loaded but not yet committed archetypes |
52 | static std::vector<archetype *> postponed_arch; |
54 | static std::vector<archetype *> postponed_arch; |
|
|
55 | |
|
|
56 | //+GPL |
|
|
57 | |
|
|
58 | /* |
|
|
59 | * Creates an object. This function is called by archetype::get () |
|
|
60 | * if it fails to find the appropriate archetype. |
|
|
61 | * Thus archetype::get() will be guaranteed to always return |
|
|
62 | * an object, and never NULL. |
|
|
63 | */ |
|
|
64 | static object * |
|
|
65 | create_singularity (const char *name) |
|
|
66 | { |
|
|
67 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
|
|
68 | |
|
|
69 | if (!strcmp (name, "bug")) |
|
|
70 | abort (); |
|
|
71 | |
|
|
72 | object *op = archetype::get (shstr_bug); |
|
|
73 | op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name); |
|
|
74 | |
|
|
75 | return op; |
|
|
76 | } |
53 | |
77 | |
54 | /** |
78 | /** |
55 | * GROS - This function retrieves an archetype given the name that appears |
79 | * GROS - This function retrieves an archetype given the name that appears |
56 | * during the game (for example, "writing pen" instead of "stylus"). |
80 | * during the game (for example, "writing pen" instead of "stylus"). |
57 | * It does not use the hashtable system, but browse the whole archlist each time. |
81 | * It does not use the hashtable system, but browse the whole archlist each time. |
… | |
… | |
101 | { |
125 | { |
102 | shstr_cmp skill_cmp (skill); |
126 | shstr_cmp skill_cmp (skill); |
103 | |
127 | |
104 | for_all_archetypes (at) |
128 | for_all_archetypes (at) |
105 | if (at->skill == skill_cmp && (type == -1 || type == at->type)) |
129 | if (at->skill == skill_cmp && (type == -1 || type == at->type)) |
106 | return arch_to_object (at); |
130 | return at->instance (); |
107 | |
131 | |
108 | return 0; |
132 | return 0; |
109 | } |
133 | } |
110 | |
134 | |
111 | /* similiar to above - this returns the first archetype |
135 | /* similiar to above - this returns the first archetype |
… | |
… | |
132 | * - name: The name we're searching for (ex: "writing pen"); |
156 | * - name: The name we're searching for (ex: "writing pen"); |
133 | * Return value: |
157 | * Return value: |
134 | * - a corresponding object if found; a singularity object if not found. |
158 | * - a corresponding object if found; a singularity object if not found. |
135 | * Note by MSW - it appears that it takes the full name and keeps |
159 | * Note by MSW - it appears that it takes the full name and keeps |
136 | * shortening it until it finds a match. I re-wrote this so that it |
160 | * shortening it until it finds a match. I re-wrote this so that it |
137 | * doesn't malloc it each time - not that this function is used much, |
161 | * doesn't allocate it each time - not that this function is used much, |
138 | * but it otherwise had a big memory leak. |
162 | * but it otherwise had a big memory leak. |
139 | */ |
163 | */ |
140 | object * |
164 | object * |
141 | get_archetype_by_object_name (const char *name) |
165 | get_archetype_by_object_name (const char *name) |
142 | { |
166 | { |
143 | char tmpname[MAX_BUF]; |
167 | char tmpname[MAX_BUF]; |
144 | int i; |
|
|
145 | |
168 | |
146 | assign (tmpname, name); |
169 | assign (tmpname, name); |
147 | |
170 | |
148 | for (i = strlen (tmpname); i > 0; i--) |
171 | for (int i = strlen (tmpname); i > 0; i--) |
149 | { |
172 | { |
150 | tmpname[i] = 0; |
173 | tmpname[i] = 0; |
151 | |
174 | |
152 | if (archetype *at = find_archetype_by_object_name (tmpname)) |
175 | if (archetype *at = find_archetype_by_object_name (tmpname)) |
153 | return arch_to_object (at); |
176 | return at->instance (); |
154 | } |
177 | } |
155 | |
178 | |
156 | return create_singularity (name); |
179 | return create_singularity (name); |
157 | } |
180 | } |
158 | |
181 | |
… | |
… | |
190 | /* All is a very generic match - low match value */ |
213 | /* All is a very generic match - low match value */ |
191 | if (!strcmp (cp, "all")) |
214 | if (!strcmp (cp, "all")) |
192 | return 1; |
215 | return 1; |
193 | |
216 | |
194 | /* unpaid is a little more specific */ |
217 | /* unpaid is a little more specific */ |
195 | if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID)) |
218 | if (!strcmp (cp, "unpaid") && op->flag [FLAG_UNPAID]) |
196 | return 2; |
219 | return 2; |
197 | if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED))) |
220 | |
|
|
221 | if (!strcmp (cp, "cursed") && op->flag [FLAG_KNOWN_CURSED] && (op->flag [FLAG_CURSED] || op->flag [FLAG_DAMNED])) |
198 | return 2; |
222 | return 2; |
199 | |
223 | |
200 | if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED)) |
224 | if (!strcmp (cp, "unlocked") && !op->flag [FLAG_INV_LOCKED]) |
201 | return 2; |
225 | return 2; |
202 | |
226 | |
203 | /* Allow for things like '100 arrows' */ |
227 | /* Allow for things like '100 arrows' */ |
204 | if ((count = atoi (cp)) != 0) |
228 | if ((count = atoi (cp)) != 0) |
205 | { |
229 | { |
… | |
… | |
223 | * is because of the 'else' handling - we don't want to match on |
247 | * is because of the 'else' handling - we don't want to match on |
224 | * something and set a low retval, even though it may match a higher retcal |
248 | * something and set a low retval, even though it may match a higher retcal |
225 | * later. So keep it in descending order here, so we try for the best |
249 | * later. So keep it in descending order here, so we try for the best |
226 | * match first, and work downward. |
250 | * match first, and work downward. |
227 | */ |
251 | */ |
|
|
252 | const char *qbn0, *qbn1, *qsn; // query base name/short name caches |
|
|
253 | |
228 | if (!strcasecmp (cp, query_name (op))) |
254 | if (!strcasecmp (cp, query_name (op))) |
229 | retval = 20; |
255 | retval = 20; |
230 | else if (!strcasecmp (cp, query_short_name (op))) |
256 | else if (!strcasecmp (cp, qsn = query_short_name (op))) |
231 | retval = 18; |
257 | retval = 18; |
232 | else if (!strcasecmp (cp, query_base_name (op, 0))) |
258 | else if (!strcasecmp (cp, qbn0 = query_base_name (op, 0))) |
233 | retval = 16; |
259 | retval = 16; |
234 | else if (!strcasecmp (cp, query_base_name (op, 1))) |
260 | else if (!strcasecmp (cp, qbn1 = query_base_name (op, 1))) |
235 | retval = 16; |
261 | retval = 16; |
236 | else if (op->custom_name && !strcasecmp (cp, op->custom_name)) |
262 | else if (op->custom_name && !strcasecmp (cp, op->custom_name)) |
237 | retval = 15; |
263 | retval = 15; |
238 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
264 | else if (!strncasecmp (cp, qbn0, strlen (cp))) |
239 | retval = 14; |
265 | retval = 14; |
240 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
266 | else if (!strncasecmp (cp, qbn1, strlen (cp))) |
241 | retval = 14; |
267 | retval = 14; |
242 | /* Do substring checks, so things like 'Str+1' will match. |
268 | /* Do substring checks, so things like 'Str+1' will match. |
243 | * retval of these should perhaps be lower - they are lower |
269 | * retval of these should perhaps be lower - they are lower |
244 | * then the specific strcasecmp aboves, but still higher than |
270 | * then the specific strcasecmp aboves, but still higher than |
245 | * some other match criteria. |
271 | * some other match criteria. |
246 | */ |
272 | */ |
247 | else if (strstr (query_base_name (op, 1), cp)) |
273 | else if (strstr (qbn1, cp)) |
248 | retval = 12; |
274 | retval = 12; |
249 | else if (strstr (query_base_name (op, 0), cp)) |
275 | else if (strstr (qbn0, cp)) |
250 | retval = 12; |
276 | retval = 12; |
251 | else if (strstr (query_short_name (op), cp)) |
277 | else if (strstr (qsn, cp)) |
252 | retval = 12; |
278 | retval = 12; |
253 | /* Check against plural/non plural based on count. */ |
279 | /* Check against plural/non plural based on count. */ |
254 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
280 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
255 | retval = 6; |
281 | retval = 6; |
256 | else if (count == 1 && !strcasecmp (op->name, cp)) |
282 | else if (count == 1 && !strcasecmp (op->name, cp)) |
257 | retval = 6; |
283 | retval = 6; |
258 | /* base name matched - not bad */ |
284 | /* base name matched - not bad */ |
259 | else if (strcasecmp (cp, op->name) == 0 && !count) |
285 | else if (strcasecmp (cp, op->name) == 0 && !count) |
260 | retval = 4; |
286 | retval = 4; |
261 | /* Check for partial custom name, but give a real low priority */ |
287 | /* Check for partial custom name, but give a real low priority */ |
262 | else if (op->custom_name && strstr (op->custom_name, cp)) |
288 | else if (op->custom_name.contains (cp)) |
263 | retval = 3; |
289 | retval = 3; |
264 | |
290 | |
265 | if (retval) |
291 | if (retval) |
266 | { |
292 | { |
267 | if (pl->type == PLAYER) |
293 | if (pl->type == PLAYER) |
… | |
… | |
272 | } |
298 | } |
273 | |
299 | |
274 | return 0; |
300 | return 0; |
275 | } |
301 | } |
276 | |
302 | |
|
|
303 | //-GPL |
|
|
304 | |
|
|
305 | void |
|
|
306 | archetype::do_delete () |
|
|
307 | { |
|
|
308 | delete this; |
|
|
309 | } |
|
|
310 | |
277 | archetype::archetype (const char *name) |
311 | archetype::archetype (const char *name) |
278 | { |
312 | { |
279 | arch = this; |
313 | arch = this; |
280 | this->archname = this->name = this->name_pl = name; |
314 | this->archname = this->name = this->name_pl = name; |
281 | } |
315 | } |
… | |
… | |
286 | } |
320 | } |
287 | |
321 | |
288 | void |
322 | void |
289 | archetype::link () |
323 | archetype::link () |
290 | { |
324 | { |
291 | ht.insert (std::make_pair (archname, this)); |
|
|
292 | |
|
|
293 | if (!archetypes.contains (this)) |
325 | if (!archetypes.contains (this)) |
|
|
326 | { |
294 | archetypes.insert (this); |
327 | archetypes.insert (this); |
|
|
328 | ht.insert (std::make_pair (archname, this)); |
|
|
329 | } |
295 | } |
330 | } |
296 | |
331 | |
297 | void |
332 | void |
298 | archetype::unlink () |
333 | archetype::unlink () |
299 | { |
334 | { |
300 | ht.erase (archname); |
|
|
301 | |
|
|
302 | if (archetypes.contains (this)) |
335 | if (archetypes.contains (this)) |
|
|
336 | { |
303 | archetypes.erase (this); |
337 | archetypes.erase (this); |
|
|
338 | ht.erase (archname); |
|
|
339 | } |
304 | } |
340 | } |
305 | |
341 | |
306 | /* |
342 | /* |
307 | * Finds, using the hashtable, which archetype matches the given name. |
343 | * Finds, using the hashtable, which archetype matches the given name. |
308 | * returns a pointer to the found archetype, otherwise NULL. |
344 | * returns a pointer to the found archetype, otherwise NULL. |
… | |
… | |
319 | return 0; |
355 | return 0; |
320 | else |
356 | else |
321 | return i->second; |
357 | return i->second; |
322 | } |
358 | } |
323 | |
359 | |
|
|
360 | void |
|
|
361 | archetype::post_load_check () |
|
|
362 | { |
|
|
363 | assert (("obj_original MUST NOT be set for archetypes", !flag [FLAG_OBJ_ORIGINAL])); |
|
|
364 | |
|
|
365 | if (type == SKILL) |
|
|
366 | add_skill_archetype (this); |
|
|
367 | |
|
|
368 | object::post_load_check (); |
|
|
369 | } |
|
|
370 | |
324 | archetype * |
371 | archetype * |
325 | archetype::read (object_thawer &f) |
372 | archetype::read (object_thawer &f) |
326 | { |
373 | { |
327 | assert (f.kw == KW_object); |
374 | assert (f.kw == KW_object); |
328 | |
375 | |
… | |
… | |
337 | f.next (); |
384 | f.next (); |
338 | |
385 | |
339 | #if 0 |
386 | #if 0 |
340 | // implementing it here in the server does neither allow multiple inheritence |
387 | // implementing it here in the server does neither allow multiple inheritence |
341 | // nor does it cleanly "just override". it would allow use in map files, though, |
388 | // nor does it cleanly "just override". it would allow use in map files, though, |
342 | // and other resource files dynamically laoded (as opposed to being preprocessed). |
389 | // and other resource files dynamically loaded (as opposed to being preprocessed). |
343 | // not that any of this is relevant as of yet... |
390 | // not that any of this is relevant as of yet... |
344 | if (f.kw == KW_inherit) |
391 | if (f.kw == KW_inherit) |
345 | { |
392 | { |
346 | if (archetype *at = find (f.get_str ())) |
393 | if (archetype *at = find (f.get_str ())) |
347 | *op = at->clone; |
394 | *op = at->clone; |
… | |
… | |
377 | goto fail; |
424 | goto fail; |
378 | } |
425 | } |
379 | } |
426 | } |
380 | |
427 | |
381 | { |
428 | { |
382 | auto (at, parts.begin ()); |
|
|
383 | |
|
|
384 | archetype *new_head = parts.front (); |
429 | archetype *new_head = parts.front (); |
385 | archetype *old_head = find (new_head->archname); |
430 | archetype *old_head = find (new_head->archname); |
386 | |
431 | |
387 | if (old_head && !old_head->is_head ()) |
432 | if (old_head && !old_head->is_head ()) |
388 | { |
433 | { |
… | |
… | |
390 | &new_head->archname, &old_head->archname); |
435 | &new_head->archname, &old_head->archname); |
391 | goto fail; |
436 | goto fail; |
392 | } |
437 | } |
393 | |
438 | |
394 | // check that all archetypes belong to the same old object or are new |
439 | // check that all archetypes belong to the same old object or are new |
395 | for (auto (at, parts.begin ()); at != parts.end (); ++at) |
440 | for (auto &&at : parts) |
396 | { |
441 | { |
397 | archetype *new_part = *at; |
442 | archetype *new_part = at; |
398 | archetype *old_part = find (new_part->archname); |
443 | archetype *old_part = find (new_part->archname); |
399 | |
444 | |
400 | if (old_part && old_part->head_ () != old_head) |
445 | if (old_part && old_part->head_ () != old_head) |
401 | { |
446 | { |
402 | LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n", |
447 | LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n", |
… | |
… | |
404 | goto fail; |
449 | goto fail; |
405 | } |
450 | } |
406 | } |
451 | } |
407 | |
452 | |
408 | // assemble new chain |
453 | // assemble new chain |
409 | new_head->min_x = new_head->max_x = new_head->x; |
454 | new_head->max_x = new_head->x; |
410 | new_head->min_y = new_head->max_y = new_head->y; |
|
|
411 | |
455 | |
412 | archetype *less = new_head; |
456 | archetype *less = new_head; |
413 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
457 | for (auto &&p = parts.begin () + 1; p != parts.end (); ++p) |
414 | { |
458 | { |
415 | archetype *at = *p; |
459 | archetype *at = *p; |
416 | |
460 | |
417 | // some flags get inherited formt he head (probably a lot more) |
461 | // some flags get inherited from the head (probably a lot more) |
418 | // doing it here doesn't feel too cozy, but it allows code |
462 | // doing it here doesn't feel too cozy, but it allows code |
419 | // to ignore head checks for these flags, which saves time |
463 | // to ignore head checks for these flags, which saves time |
420 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
464 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
421 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
465 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
422 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
466 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
423 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
467 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
424 | |
468 | |
425 | if (at->x < new_head->min_x) new_head->min_x = at->x; |
|
|
426 | if (at->y < new_head->min_y) new_head->min_y = at->y; |
|
|
427 | if (at->x > new_head->max_x) new_head->max_x = at->x; |
469 | new_head->max_x = max (new_head->max_x, at->x); |
428 | if (at->y > new_head->max_y) new_head->max_y = at->y; |
|
|
429 | |
470 | |
430 | at->head = new_head; |
471 | at->head = new_head; |
431 | less->more = at; |
472 | less->more = at; |
432 | less = at; |
473 | less = at; |
433 | } |
474 | } |
… | |
… | |
436 | |
477 | |
437 | return new_head; |
478 | return new_head; |
438 | } |
479 | } |
439 | |
480 | |
440 | fail: |
481 | fail: |
441 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
482 | for (auto &&at : parts) |
442 | (*p)->destroy (true); |
483 | at->destroy (); |
443 | |
484 | |
444 | return 0; |
485 | return 0; |
445 | } |
486 | } |
446 | |
487 | |
447 | void |
488 | void |
… | |
… | |
453 | |
494 | |
454 | void |
495 | void |
455 | archetype::commit_load () |
496 | archetype::commit_load () |
456 | { |
497 | { |
457 | // unlink old archetypes and link in new ones */ |
498 | // unlink old archetypes and link in new ones */ |
458 | for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p) |
499 | for (auto &&at : postponed_arch) |
459 | { |
500 | { |
460 | archetype *at = *p; |
|
|
461 | |
|
|
462 | if (archetype *old = find (at->archname)) |
501 | if (archetype *old = find (at->archname)) |
463 | old->unlink (); |
502 | old->unlink (); |
464 | |
503 | |
|
|
504 | allarch.push_back (at); |
|
|
505 | |
465 | at->link (); |
506 | at->link (); |
|
|
507 | ++dirtycnt; |
466 | } |
508 | } |
467 | |
509 | |
468 | postponed_arch.clear (); |
510 | postponed_arch.clear (); |
469 | |
511 | |
470 | // now resolve arch references |
512 | // now resolve arch references |
471 | for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p) |
513 | for (auto &&p : postponed_arch_ref) // not yet C++17 |
472 | { |
514 | { |
473 | arch_ptr *ap = p->first; |
515 | arch_ptr *ap = p.first; |
474 | archetype *at = find (p->second); |
516 | archetype *at = find (p.second); |
475 | |
517 | |
476 | if (!at) |
518 | if (!at) |
477 | LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second); |
519 | LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p.second); |
478 | |
520 | |
479 | *ap = at; |
521 | *ap = at; |
480 | } |
522 | } |
481 | |
523 | |
482 | postponed_arch_ref.clear (); |
524 | postponed_arch_ref.clear (); |
483 | |
525 | |
484 | empty = find (shstr_empty_archetype); |
526 | empty = find (shstr_empty_archetype); |
485 | } |
527 | } |
486 | |
528 | |
487 | /* |
529 | void |
488 | * Creates and returns a new object which is a copy of the given archetype. |
530 | archetype::gc () |
489 | * This function returns NULL on failure. |
|
|
490 | */ |
|
|
491 | object * |
|
|
492 | arch_to_object (archetype *at) |
|
|
493 | { |
531 | { |
494 | if (!at) |
532 | int cnt = max (1, min<int> (allarch.size () / 128, dirtycnt)); |
|
|
533 | dirtycnt = max (0, dirtycnt - cnt); |
|
|
534 | |
|
|
535 | do |
495 | { |
536 | { |
496 | LOG (llevError, "Couldn't find archetype.\n"); |
537 | static int idx; |
|
|
538 | |
|
|
539 | if (idx >= allarch.size ()) |
|
|
540 | if (idx) |
|
|
541 | idx = 0; |
|
|
542 | else |
497 | return 0; |
543 | return; |
498 | } |
|
|
499 | |
544 | |
500 | object *op = at->clone (); |
545 | archetype *at = allarch [idx]; |
501 | op->arch = at; |
|
|
502 | op->instantiate (); |
|
|
503 | |
546 | |
504 | return op; |
547 | if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object |
|
|
548 | ++idx; |
|
|
549 | else |
|
|
550 | { |
|
|
551 | //LOG (llevDebug, "garbage collect arch %s", &at->archname); |
|
|
552 | assert (at->arch == at); // verify that refcnt == 1 is truly valid |
|
|
553 | allarch.erase (idx); |
|
|
554 | |
|
|
555 | // break chain |
|
|
556 | for (object *op = at->head_ (); op; ) |
|
|
557 | { |
|
|
558 | object *next = op->more; |
|
|
559 | op->head = 0; |
|
|
560 | op->more = 0; |
|
|
561 | op = next; |
|
|
562 | } |
|
|
563 | |
|
|
564 | at->destroy (); |
|
|
565 | at->arch = 0; |
|
|
566 | } |
|
|
567 | } |
|
|
568 | while (--cnt); |
505 | } |
569 | } |
506 | |
570 | |
507 | object * |
571 | object * |
508 | archetype::instance () |
572 | archetype::instance () |
509 | { |
573 | { |
510 | return arch_to_object (this); |
574 | object *op = clone (); |
511 | } |
575 | op->instantiate (); |
512 | |
|
|
513 | /* |
|
|
514 | * Creates an object. This function is called by get_archetype() |
|
|
515 | * if it fails to find the appropriate archetype. |
|
|
516 | * Thus get_archetype() will be guaranteed to always return |
|
|
517 | * an object, and never NULL. |
|
|
518 | */ |
|
|
519 | object * |
|
|
520 | create_singularity (const char *name) |
|
|
521 | { |
|
|
522 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
|
|
523 | |
|
|
524 | if (!strcmp (name, "bug")) |
|
|
525 | abort (); |
|
|
526 | |
|
|
527 | char buf[MAX_BUF]; |
|
|
528 | sprintf (buf, "bug, please report (%s)", name); |
|
|
529 | |
|
|
530 | object *op = get_archetype ("bug"); |
|
|
531 | op->name = op->name_pl = buf; |
|
|
532 | |
|
|
533 | return op; |
576 | return op; |
534 | } |
577 | } |
|
|
578 | |
|
|
579 | //+GPL |
535 | |
580 | |
536 | /* |
581 | /* |
537 | * Finds which archetype matches the given name, and returns a new |
582 | * Finds which archetype matches the given name, and returns a new |
538 | * object containing a copy of the archetype. |
583 | * object containing a copy of the archetype. |
539 | */ |
584 | */ |
540 | object * |
585 | object * |
541 | get_archetype (const char *name) |
586 | archetype::get (const char *name) |
542 | { |
587 | { |
543 | archetype *at = archetype::find (name); |
588 | archetype *at = find (name); |
544 | |
589 | |
545 | if (!at) |
590 | if (!at) |
546 | return create_singularity (name); |
591 | return create_singularity (name); |
547 | |
592 | |
548 | return arch_to_object (at); |
593 | return at->instance (); |
549 | } |
594 | } |
550 | |
595 | |
551 | /* |
596 | /* |
552 | * Returns the first archetype using the given type. |
597 | * Returns the first archetype using the given type. |
553 | * Used in treasure-generation. |
598 | * Used in treasure-generation. |
554 | */ |
599 | */ |
555 | archetype * |
600 | static archetype * |
556 | type_to_archetype (int type) |
601 | type_to_archetype (int type) |
557 | { |
602 | { |
558 | for_all_archetypes (at) |
603 | for_all_archetypes (at) |
559 | if (at->type == type && at->head_ () != at) |
604 | if (at->type == type && at->head_ () != at) |
560 | return at; |
605 | return at; |
… | |
… | |
568 | * Used in treasure-generation. |
613 | * Used in treasure-generation. |
569 | */ |
614 | */ |
570 | object * |
615 | object * |
571 | clone_arch (int type) |
616 | clone_arch (int type) |
572 | { |
617 | { |
573 | archetype *at; |
618 | archetype *at = type_to_archetype (type); |
574 | |
619 | |
575 | if ((at = type_to_archetype (type)) == NULL) |
620 | if (!at) |
576 | { |
621 | { |
577 | LOG (llevError, "Can't clone archetype %d\n", type); |
622 | LOG (llevError, "Can't clone archetype %d\n", type); |
578 | return 0; |
623 | return 0; |
579 | } |
624 | } |
580 | |
625 | |
581 | object *op = at->clone (); |
626 | return at->instance (); |
582 | op->instantiate (); |
|
|
583 | return op; |
|
|
584 | } |
627 | } |
585 | |
628 | |
586 | /* |
629 | /* |
587 | * member: make instance from class |
630 | * member: make instance from class |
588 | */ |
631 | */ |
… | |
… | |
591 | { |
634 | { |
592 | object *op, *prev = 0, *head = 0; |
635 | object *op, *prev = 0, *head = 0; |
593 | |
636 | |
594 | while (at) |
637 | while (at) |
595 | { |
638 | { |
596 | op = arch_to_object (at); |
639 | op = at->instance (); |
597 | |
640 | |
598 | op->x = at->x; |
641 | op->x = at->x; |
599 | op->y = at->y; |
642 | op->y = at->y; |
600 | |
643 | |
601 | if (head) |
644 | if (head) |
… | |
… | |
609 | } |
652 | } |
610 | |
653 | |
611 | return head; |
654 | return head; |
612 | } |
655 | } |
613 | |
656 | |
|
|
657 | //-GPL |
|
|
658 | |