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 (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Deliantra 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 3 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, see <http://www.gnu.org/licenses/>. |
19 | * and the GNU General Public License along with this program. If not, see |
|
|
20 | * <http://www.gnu.org/licenses/>. |
20 | * |
21 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | #include <cassert> |
25 | #include <cassert> |
25 | |
26 | |
26 | #include <global.h> |
27 | #include <global.h> |
27 | #include <funcpoint.h> |
|
|
28 | #include <loader.h> |
28 | #include <loader.h> |
29 | |
29 | |
30 | #include <tr1/functional> |
30 | #include <tr1/functional> |
31 | #include <tr1/unordered_map> |
31 | #include <tr1/unordered_map> |
32 | |
32 | |
33 | /* The naming of these functions is really poor - they are all |
|
|
34 | * pretty much named '.._arch_...', but they may more may not |
|
|
35 | * return archetypes. Some make the arch_to_object call, and thus |
|
|
36 | * return an object. Perhaps those should be called 'archob' functions |
|
|
37 | * to denote they return an object derived from the archetype. |
|
|
38 | * MSW 2003-04-29 |
|
|
39 | */ |
|
|
40 | |
|
|
41 | 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 | arch_ptr archetype::empty; |
42 | |
35 | |
43 | // the hashtable |
36 | // the hashtable |
44 | typedef std::tr1::unordered_map |
37 | typedef std::tr1::unordered_map |
45 | < |
38 | < |
46 | const char *, |
39 | const char *, |
… | |
… | |
48 | str_hash, |
41 | str_hash, |
49 | str_equal, |
42 | str_equal, |
50 | slice_allocator< std::pair<const char *const, arch_ptr> > |
43 | slice_allocator< std::pair<const char *const, arch_ptr> > |
51 | > HT; |
44 | > HT; |
52 | |
45 | |
53 | static HT ht (5000); |
46 | static HT ht (10000); |
54 | archvec archetypes; |
47 | archvec archetypes; |
|
|
48 | static unordered_vector<archetype *> allarch; |
|
|
49 | static int dirtycnt; |
|
|
50 | |
|
|
51 | // the vector of other_arch references to be resolved |
|
|
52 | static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; |
|
|
53 | // the vector of loaded but not yet committed archetypes |
|
|
54 | static std::vector<archetype *> postponed_arch; |
55 | |
55 | |
56 | /** |
56 | /** |
57 | * GROS - This function retrieves an archetype given the name that appears |
57 | * GROS - This function retrieves an archetype given the name that appears |
58 | * during the game (for example, "writing pen" instead of "stylus"). |
58 | * during the game (for example, "writing pen" instead of "stylus"). |
59 | * It does not use the hashtable system, but browse the whole archlist each time. |
59 | * It does not use the hashtable system, but browse the whole archlist each time. |
… | |
… | |
103 | { |
103 | { |
104 | shstr_cmp skill_cmp (skill); |
104 | shstr_cmp skill_cmp (skill); |
105 | |
105 | |
106 | for_all_archetypes (at) |
106 | for_all_archetypes (at) |
107 | if (at->skill == skill_cmp && (type == -1 || type == at->type)) |
107 | if (at->skill == skill_cmp && (type == -1 || type == at->type)) |
108 | return arch_to_object (at); |
108 | return at->instance (); |
109 | |
109 | |
110 | return 0; |
110 | return 0; |
111 | } |
111 | } |
112 | |
112 | |
113 | /* similiar to above - this returns the first archetype |
113 | /* similiar to above - this returns the first archetype |
… | |
… | |
134 | * - name: The name we're searching for (ex: "writing pen"); |
134 | * - name: The name we're searching for (ex: "writing pen"); |
135 | * Return value: |
135 | * Return value: |
136 | * - a corresponding object if found; a singularity object if not found. |
136 | * - a corresponding object if found; a singularity object if not found. |
137 | * Note by MSW - it appears that it takes the full name and keeps |
137 | * Note by MSW - it appears that it takes the full name and keeps |
138 | * shortening it until it finds a match. I re-wrote this so that it |
138 | * shortening it until it finds a match. I re-wrote this so that it |
139 | * doesn't malloc it each time - not that this function is used much, |
139 | * doesn't allocate it each time - not that this function is used much, |
140 | * but it otherwise had a big memory leak. |
140 | * but it otherwise had a big memory leak. |
141 | */ |
141 | */ |
142 | object * |
142 | object * |
143 | get_archetype_by_object_name (const char *name) |
143 | get_archetype_by_object_name (const char *name) |
144 | { |
144 | { |
145 | char tmpname[MAX_BUF]; |
145 | char tmpname[MAX_BUF]; |
146 | int i; |
|
|
147 | |
146 | |
148 | assign (tmpname, name); |
147 | assign (tmpname, name); |
149 | |
148 | |
150 | for (i = strlen (tmpname); i > 0; i--) |
149 | for (int i = strlen (tmpname); i > 0; i--) |
151 | { |
150 | { |
152 | tmpname[i] = 0; |
151 | tmpname[i] = 0; |
153 | |
152 | |
154 | if (archetype *at = find_archetype_by_object_name (tmpname)) |
153 | if (archetype *at = find_archetype_by_object_name (tmpname)) |
155 | return arch_to_object (at); |
154 | return at->instance (); |
156 | } |
155 | } |
157 | |
156 | |
158 | return create_singularity (name); |
157 | return create_singularity (name); |
159 | } |
158 | } |
160 | |
159 | |
… | |
… | |
194 | return 1; |
193 | return 1; |
195 | |
194 | |
196 | /* unpaid is a little more specific */ |
195 | /* unpaid is a little more specific */ |
197 | if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID)) |
196 | if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID)) |
198 | return 2; |
197 | return 2; |
|
|
198 | |
199 | if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED))) |
199 | if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED))) |
200 | return 2; |
200 | return 2; |
201 | |
201 | |
202 | if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED)) |
202 | if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED)) |
203 | return 2; |
203 | return 2; |
… | |
… | |
225 | * is because of the 'else' handling - we don't want to match on |
225 | * is because of the 'else' handling - we don't want to match on |
226 | * something and set a low retval, even though it may match a higher retcal |
226 | * something and set a low retval, even though it may match a higher retcal |
227 | * later. So keep it in descending order here, so we try for the best |
227 | * later. So keep it in descending order here, so we try for the best |
228 | * match first, and work downward. |
228 | * match first, and work downward. |
229 | */ |
229 | */ |
|
|
230 | const char *qbn0, *qbn1, *qsn; // query base name/short name caches |
|
|
231 | |
230 | if (!strcasecmp (cp, query_name (op))) |
232 | if (!strcasecmp (cp, query_name (op))) |
231 | retval = 20; |
233 | retval = 20; |
232 | else if (!strcasecmp (cp, query_short_name (op))) |
234 | else if (!strcasecmp (cp, qsn = query_short_name (op))) |
233 | retval = 18; |
235 | retval = 18; |
234 | else if (!strcasecmp (cp, query_base_name (op, 0))) |
236 | else if (!strcasecmp (cp, qbn0 = query_base_name (op, 0))) |
235 | retval = 16; |
237 | retval = 16; |
236 | else if (!strcasecmp (cp, query_base_name (op, 1))) |
238 | else if (!strcasecmp (cp, qbn1 = query_base_name (op, 1))) |
237 | retval = 16; |
239 | retval = 16; |
238 | else if (op->custom_name && !strcasecmp (cp, op->custom_name)) |
240 | else if (op->custom_name && !strcasecmp (cp, op->custom_name)) |
239 | retval = 15; |
241 | retval = 15; |
240 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
242 | else if (!strncasecmp (cp, qbn0, strlen (cp))) |
241 | retval = 14; |
243 | retval = 14; |
242 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
244 | else if (!strncasecmp (cp, qbn1, strlen (cp))) |
243 | retval = 14; |
245 | retval = 14; |
244 | /* Do substring checks, so things like 'Str+1' will match. |
246 | /* Do substring checks, so things like 'Str+1' will match. |
245 | * retval of these should perhaps be lower - they are lower |
247 | * retval of these should perhaps be lower - they are lower |
246 | * then the specific strcasecmp aboves, but still higher than |
248 | * then the specific strcasecmp aboves, but still higher than |
247 | * some other match criteria. |
249 | * some other match criteria. |
248 | */ |
250 | */ |
249 | else if (strstr (query_base_name (op, 1), cp)) |
251 | else if (strstr (qbn1, cp)) |
250 | retval = 12; |
252 | retval = 12; |
251 | else if (strstr (query_base_name (op, 0), cp)) |
253 | else if (strstr (qbn0, cp)) |
252 | retval = 12; |
254 | retval = 12; |
253 | else if (strstr (query_short_name (op), cp)) |
255 | else if (strstr (qsn, cp)) |
254 | retval = 12; |
256 | retval = 12; |
255 | /* Check against plural/non plural based on count. */ |
257 | /* Check against plural/non plural based on count. */ |
256 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
258 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
257 | retval = 6; |
259 | retval = 6; |
258 | else if (count == 1 && !strcasecmp (op->name, cp)) |
260 | else if (count == 1 && !strcasecmp (op->name, cp)) |
259 | retval = 6; |
261 | retval = 6; |
260 | /* base name matched - not bad */ |
262 | /* base name matched - not bad */ |
261 | else if (strcasecmp (cp, op->name) == 0 && !count) |
263 | else if (strcasecmp (cp, op->name) == 0 && !count) |
262 | retval = 4; |
264 | retval = 4; |
263 | /* Check for partial custom name, but give a real low priority */ |
265 | /* Check for partial custom name, but give a real low priority */ |
264 | else if (op->custom_name && strstr (op->custom_name, cp)) |
266 | else if (op->custom_name.contains (cp)) |
265 | retval = 3; |
267 | retval = 3; |
266 | |
268 | |
267 | if (retval) |
269 | if (retval) |
268 | { |
270 | { |
269 | if (pl->type == PLAYER) |
271 | if (pl->type == PLAYER) |
… | |
… | |
276 | return 0; |
278 | return 0; |
277 | } |
279 | } |
278 | |
280 | |
279 | archetype::archetype (const char *name) |
281 | archetype::archetype (const char *name) |
280 | { |
282 | { |
281 | stub = true; |
|
|
282 | arch = this; |
283 | arch = this; |
283 | this->archname = this->name = this->name_pl = name; |
284 | this->archname = this->name = this->name_pl = name; |
284 | } |
285 | } |
285 | |
286 | |
286 | archetype::~archetype () |
287 | archetype::~archetype () |
… | |
… | |
289 | } |
290 | } |
290 | |
291 | |
291 | void |
292 | void |
292 | archetype::link () |
293 | archetype::link () |
293 | { |
294 | { |
294 | ht.insert (std::make_pair (archname, this)); |
|
|
295 | |
|
|
296 | if (!archetypes.contains (this)) |
295 | if (!archetypes.contains (this)) |
|
|
296 | { |
297 | archetypes.insert (this); |
297 | archetypes.insert (this); |
|
|
298 | ht.insert (std::make_pair (archname, this)); |
|
|
299 | } |
298 | } |
300 | } |
299 | |
301 | |
300 | void |
302 | void |
301 | archetype::unlink () |
303 | archetype::unlink () |
302 | { |
304 | { |
303 | ht.erase (archname); |
|
|
304 | |
|
|
305 | if (archetypes.contains (this)) |
305 | if (archetypes.contains (this)) |
|
|
306 | { |
306 | archetypes.erase (this); |
307 | archetypes.erase (this); |
|
|
308 | ht.erase (archname); |
|
|
309 | } |
307 | } |
310 | } |
308 | |
311 | |
309 | /* |
312 | /* |
310 | * Finds, using the hashtable, which archetype matches the given name. |
313 | * Finds, using the hashtable, which archetype matches the given name. |
311 | * returns a pointer to the found archetype, otherwise NULL. |
314 | * returns a pointer to the found archetype, otherwise NULL. |
… | |
… | |
323 | else |
326 | else |
324 | return i->second; |
327 | return i->second; |
325 | } |
328 | } |
326 | |
329 | |
327 | archetype * |
330 | archetype * |
328 | archetype::get (const char *name) |
|
|
329 | { |
|
|
330 | if (!name) |
|
|
331 | { |
|
|
332 | LOG (llevError, "null archetype requested\n"); |
|
|
333 | name = "(null)"; |
|
|
334 | } |
|
|
335 | |
|
|
336 | if (loading_arch && !strcmp (&loading_arch->archname, name)) |
|
|
337 | return loading_arch; |
|
|
338 | |
|
|
339 | archetype *at = find (name); |
|
|
340 | |
|
|
341 | if (!at) |
|
|
342 | { |
|
|
343 | at = new archetype (name); |
|
|
344 | at->link (); |
|
|
345 | } |
|
|
346 | |
|
|
347 | return at; |
|
|
348 | } |
|
|
349 | |
|
|
350 | archetype * |
|
|
351 | archetype::read (object_thawer &f) |
331 | archetype::read (object_thawer &f) |
352 | { |
332 | { |
353 | assert (f.kw == KW_object); |
333 | assert (f.kw == KW_object); |
354 | |
334 | |
355 | std::vector<archetype *> parts; |
335 | std::vector<archetype *> parts; |
356 | |
336 | |
357 | coroapi::cede_to_tick (); |
337 | coroapi::cede_to_tick (); |
358 | |
338 | |
359 | for (;;) |
339 | for (;;) |
360 | { |
340 | { |
361 | // the archetype might have been referenced earlier |
|
|
362 | // so try to find an existing stub archetype first |
|
|
363 | archetype *at = find (f.get_str ()); |
341 | archetype *at = new archetype (f.get_str ()); |
364 | |
|
|
365 | if (!at || !at->stub) |
|
|
366 | at = new archetype (f.get_str ()); |
|
|
367 | |
342 | |
368 | f.next (); |
343 | f.next (); |
369 | |
344 | |
370 | #if 0 |
345 | #if 0 |
371 | // implementing it here in the server does neither allow multiple inheritence |
346 | // implementing it here in the server does neither allow multiple inheritence |
… | |
… | |
434 | &new_part->archname, &((archetype *)old_part->head_ ())->archname); |
409 | &new_part->archname, &((archetype *)old_part->head_ ())->archname); |
435 | goto fail; |
410 | goto fail; |
436 | } |
411 | } |
437 | } |
412 | } |
438 | |
413 | |
439 | // deactivate existing archetype |
|
|
440 | for (archetype *at = old_head; at; at = (archetype *)at->more) |
|
|
441 | at->unlink (); |
|
|
442 | |
|
|
443 | // assemble new chain |
414 | // assemble new chain |
444 | new_head->min_x = new_head->max_x = new_head->x; |
415 | new_head->min_x = new_head->max_x = new_head->x; |
445 | new_head->min_y = new_head->max_y = new_head->y; |
416 | new_head->min_y = new_head->max_y = new_head->y; |
446 | |
417 | |
447 | archetype *less = new_head; |
418 | archetype *less = new_head; |
448 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
419 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
449 | { |
420 | { |
450 | archetype *at = *p; |
421 | archetype *at = *p; |
451 | |
422 | |
452 | // some flags get inherited formt he head (probably a lot more) |
423 | // some flags get inherited from the head (probably a lot more) |
453 | // doing it here doesn't feel too cozy, but it allows code |
424 | // doing it here doesn't feel too cozy, but it allows code |
454 | // to ignore head checks for these flags, which saves time |
425 | // to ignore head checks for these flags, which saves time |
455 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
426 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
456 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
427 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
457 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
428 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
458 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
429 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
459 | |
430 | |
460 | if (at->x < new_head->min_x) new_head->min_x = at->x; |
431 | new_head->min_x = min (new_head->min_x, at->x); |
461 | if (at->y < new_head->min_y) new_head->min_y = at->y; |
432 | new_head->min_y = min (new_head->min_y, at->y); |
462 | if (at->x > new_head->max_x) new_head->max_x = at->x; |
433 | new_head->max_x = max (new_head->max_x, at->x); |
463 | if (at->y > new_head->max_y) new_head->max_y = at->y; |
434 | new_head->max_y = max (new_head->max_y, at->y); |
464 | |
435 | |
465 | at->head = new_head; |
436 | at->head = new_head; |
466 | less->more = at; |
437 | less->more = at; |
467 | less = at; |
438 | less = at; |
468 | } |
439 | } |
469 | |
440 | |
470 | // now activate it |
441 | postponed_arch.insert (postponed_arch.end (), parts.begin (), parts.end ()); |
471 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
|
|
472 | { |
|
|
473 | archetype *at = *p; |
|
|
474 | at->stub = false; |
|
|
475 | at->link (); |
|
|
476 | } |
|
|
477 | |
442 | |
478 | return new_head; |
443 | return new_head; |
479 | } |
444 | } |
480 | |
445 | |
481 | fail: |
446 | fail: |
482 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
447 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
483 | (*p)->destroy (true); |
448 | (*p)->destroy (); |
484 | |
449 | |
485 | return 0; |
450 | return 0; |
486 | } |
451 | } |
487 | |
452 | |
488 | /* |
|
|
489 | * Initialize global archtype pointers: |
|
|
490 | */ |
|
|
491 | void |
453 | void |
492 | init_archetype_pointers () |
454 | archetype::postpone_arch_ref (arch_ptr &ref, const_utf8_string other_arch) |
493 | { |
455 | { |
494 | ring_arch = archetype::find ("ring"); |
456 | ref = 0; |
495 | amulet_arch = archetype::find ("amulet"); |
457 | postponed_arch_ref.push_back (std::pair<arch_ptr *, shstr>(&ref, shstr (other_arch))); |
496 | staff_arch = archetype::find ("staff"); |
|
|
497 | crown_arch = archetype::find ("crown"); |
|
|
498 | empty_archetype = archetype::find ("empty_archetype"); |
|
|
499 | } |
458 | } |
500 | |
459 | |
501 | /* |
460 | void |
502 | * Creates and returns a new object which is a copy of the given archetype. |
461 | archetype::commit_load () |
503 | * This function returns NULL on failure. |
|
|
504 | */ |
|
|
505 | object * |
|
|
506 | arch_to_object (archetype *at) |
|
|
507 | { |
462 | { |
|
|
463 | // unlink old archetypes and link in new ones */ |
|
|
464 | for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p) |
|
|
465 | { |
|
|
466 | archetype *at = *p; |
|
|
467 | |
|
|
468 | if (archetype *old = find (at->archname)) |
|
|
469 | old->unlink (); |
|
|
470 | |
|
|
471 | allarch.push_back (at); |
|
|
472 | |
|
|
473 | at->link (); |
|
|
474 | ++dirtycnt; |
|
|
475 | } |
|
|
476 | |
|
|
477 | postponed_arch.clear (); |
|
|
478 | |
|
|
479 | // now resolve arch references |
|
|
480 | for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p) |
|
|
481 | { |
|
|
482 | arch_ptr *ap = p->first; |
|
|
483 | archetype *at = find (p->second); |
|
|
484 | |
508 | if (!at) |
485 | if (!at) |
|
|
486 | LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second); |
|
|
487 | |
|
|
488 | *ap = at; |
509 | { |
489 | } |
510 | LOG (llevError, "Couldn't find archetype.\n"); |
490 | |
|
|
491 | postponed_arch_ref.clear (); |
|
|
492 | |
|
|
493 | empty = find (shstr_empty_archetype); |
|
|
494 | } |
|
|
495 | |
|
|
496 | void |
|
|
497 | archetype::gc () |
|
|
498 | { |
|
|
499 | int cnt = max (1, min (allarch.size () / 128, dirtycnt)); |
|
|
500 | dirtycnt = max (0, dirtycnt - cnt); |
|
|
501 | |
|
|
502 | do |
|
|
503 | { |
|
|
504 | static int idx; |
|
|
505 | |
|
|
506 | if (idx >= allarch.size ()) |
|
|
507 | if (idx) |
|
|
508 | idx = 0; |
|
|
509 | else |
511 | return 0; |
510 | return; |
512 | } |
|
|
513 | |
511 | |
|
|
512 | archetype *at = allarch [idx]; |
|
|
513 | |
|
|
514 | if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object |
|
|
515 | ++idx; |
|
|
516 | else |
|
|
517 | { |
|
|
518 | LOG (llevDebug, "garbage collect arch %s", &at->archname); |
|
|
519 | assert (at->arch == at); // verify that refcnt == 1 is truly valid |
|
|
520 | allarch.erase (idx); |
|
|
521 | |
|
|
522 | // break chain |
|
|
523 | for (object *op = at->head_ (); op; ) |
|
|
524 | { |
|
|
525 | object *next = op->more; |
|
|
526 | op->head = 0; |
|
|
527 | op->more = 0; |
|
|
528 | op = next; |
|
|
529 | } |
|
|
530 | |
|
|
531 | at->destroy (); |
|
|
532 | at->arch = 0; |
|
|
533 | } |
|
|
534 | } |
|
|
535 | while (--cnt); |
|
|
536 | } |
|
|
537 | |
|
|
538 | object * |
|
|
539 | archetype::instance () |
|
|
540 | { |
514 | object *op = at->clone (); |
541 | object *op = clone (); |
515 | op->arch = at; |
|
|
516 | op->instantiate (); |
542 | op->instantiate (); |
517 | |
|
|
518 | return op; |
543 | return op; |
519 | } |
|
|
520 | |
|
|
521 | object * |
|
|
522 | archetype::instance () |
|
|
523 | { |
|
|
524 | return arch_to_object (this); |
|
|
525 | } |
544 | } |
526 | |
545 | |
527 | /* |
546 | /* |
528 | * Creates an object. This function is called by get_archetype() |
547 | * Creates an object. This function is called by get_archetype() |
529 | * if it fails to find the appropriate archetype. |
548 | * if it fails to find the appropriate archetype. |
… | |
… | |
536 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
555 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
537 | |
556 | |
538 | if (!strcmp (name, "bug")) |
557 | if (!strcmp (name, "bug")) |
539 | abort (); |
558 | abort (); |
540 | |
559 | |
541 | char buf[MAX_BUF]; |
|
|
542 | sprintf (buf, "bug, please report (%s)", name); |
|
|
543 | |
|
|
544 | object *op = get_archetype ("bug"); |
560 | object *op = archetype::get (shstr_bug); |
545 | op->name = op->name_pl = buf; |
561 | op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name); |
546 | |
562 | |
547 | return op; |
563 | return op; |
548 | } |
564 | } |
549 | |
565 | |
550 | /* |
566 | /* |
… | |
… | |
552 | * object containing a copy of the archetype. |
568 | * object containing a copy of the archetype. |
553 | */ |
569 | */ |
554 | object * |
570 | object * |
555 | get_archetype (const char *name) |
571 | get_archetype (const char *name) |
556 | { |
572 | { |
|
|
573 | return archetype::get (name); |
|
|
574 | } |
|
|
575 | |
|
|
576 | object * |
|
|
577 | archetype::get (const char *name) |
|
|
578 | { |
557 | archetype *at = archetype::find (name); |
579 | archetype *at = find (name); |
558 | |
580 | |
559 | if (!at) |
581 | if (!at) |
560 | return create_singularity (name); |
582 | return create_singularity (name); |
561 | |
583 | |
562 | return arch_to_object (at); |
584 | return at->instance (); |
563 | } |
585 | } |
564 | |
586 | |
565 | /* |
587 | /* |
566 | * Returns the first archetype using the given type. |
588 | * Returns the first archetype using the given type. |
567 | * Used in treasure-generation. |
589 | * Used in treasure-generation. |
… | |
… | |
582 | * Used in treasure-generation. |
604 | * Used in treasure-generation. |
583 | */ |
605 | */ |
584 | object * |
606 | object * |
585 | clone_arch (int type) |
607 | clone_arch (int type) |
586 | { |
608 | { |
587 | archetype *at; |
609 | archetype *at = type_to_archetype (type); |
588 | |
610 | |
589 | if ((at = type_to_archetype (type)) == NULL) |
611 | if (!at) |
590 | { |
612 | { |
591 | LOG (llevError, "Can't clone archetype %d\n", type); |
613 | LOG (llevError, "Can't clone archetype %d\n", type); |
592 | return 0; |
614 | return 0; |
593 | } |
615 | } |
594 | |
616 | |
595 | object *op = at->clone (); |
617 | return at->instance (); |
596 | op->instantiate (); |
|
|
597 | return op; |
|
|
598 | } |
618 | } |
599 | |
619 | |
600 | /* |
620 | /* |
601 | * member: make instance from class |
621 | * member: make instance from class |
602 | */ |
622 | */ |
… | |
… | |
605 | { |
625 | { |
606 | object *op, *prev = 0, *head = 0; |
626 | object *op, *prev = 0, *head = 0; |
607 | |
627 | |
608 | while (at) |
628 | while (at) |
609 | { |
629 | { |
610 | op = arch_to_object (at); |
630 | op = at->instance (); |
611 | |
631 | |
612 | op->x = at->x; |
632 | op->x = at->x; |
613 | op->y = at->y; |
633 | op->y = at->y; |
614 | |
634 | |
615 | if (head) |
635 | if (head) |