… | |
… | |
316 | at->clone.inv = op->inv; op->inv = 0; |
316 | at->clone.inv = op->inv; op->inv = 0; |
317 | |
317 | |
318 | op->destroy (); |
318 | op->destroy (); |
319 | } |
319 | } |
320 | |
320 | |
321 | bool |
321 | archetype * |
322 | archetype::load (object_thawer &f) |
322 | archetype::read (object_thawer &f) |
323 | { |
323 | { |
324 | assert (f.kw == KW_object); |
324 | assert (f.kw == KW_object); |
|
|
325 | |
|
|
326 | loading_arch = true; // hack to tell parse_kv et al. to behave |
325 | |
327 | |
326 | typedef std::pair<archetype *, object *> part; |
328 | typedef std::pair<archetype *, object *> part; |
327 | std::vector<part> parts; |
329 | std::vector<part> parts; |
328 | |
330 | |
|
|
331 | coroapi::cede_to_tick_every (100); |
|
|
332 | |
329 | for (;;) |
333 | for (;;) |
330 | { |
334 | { |
331 | archetype *at = get (f.get_str ()); |
335 | archetype *at = get (f.get_str ()); |
332 | object *op = object::create (); |
336 | object *op = object::create (); |
333 | |
337 | |
… | |
… | |
345 | |
349 | |
346 | { |
350 | { |
347 | archetype *head = parts.front ().first; |
351 | archetype *head = parts.front ().first; |
348 | |
352 | |
349 | // check that all archetypes belong to the same object or are heads |
353 | // check that all archetypes belong to the same object or are heads |
350 | for (AUTODECL (p, parts.begin ()); p != parts.end (); ++p) |
354 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
351 | { |
355 | { |
352 | archetype *at = p->first; |
356 | archetype *at = p->first; |
353 | |
357 | |
354 | if (at->head != head && at->head) |
358 | if (at->head != head && at->head) |
355 | { |
359 | { |
… | |
… | |
377 | overwrite (head, parts.front ().second); |
381 | overwrite (head, parts.front ().second); |
378 | head->tail_x = 0; |
382 | head->tail_x = 0; |
379 | head->tail_y = 0; |
383 | head->tail_y = 0; |
380 | |
384 | |
381 | // link into list of heads, if not already there |
385 | // link into list of heads, if not already there |
382 | if (!head->next) |
386 | if (!head->linked) |
383 | { |
387 | { |
|
|
388 | head->linked = true; |
384 | head->next = first_archetype; |
389 | head->next = first_archetype; |
385 | first_archetype = head; |
390 | first_archetype = head; |
386 | } |
391 | } |
387 | |
392 | |
388 | // reassemble new chain |
393 | // reassemble new chain |
389 | archetype *prev = head; |
394 | archetype *prev = head; |
390 | for (AUTODECL (p, parts.begin () + 1); p != parts.end (); ++p) |
395 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
391 | { |
396 | { |
392 | archetype *at = p->first; |
397 | archetype *at = p->first; |
393 | overwrite (at, p->second); |
398 | overwrite (at, p->second); |
394 | |
399 | |
395 | if (at->clone.x > head->tail_x) head->tail_x = at->clone.x; |
400 | if (at->clone.x > head->tail_x) head->tail_x = at->clone.x; |
… | |
… | |
400 | prev->more = at; |
405 | prev->more = at; |
401 | prev->clone.more = &at->clone; |
406 | prev->clone.more = &at->clone; |
402 | |
407 | |
403 | prev = at; |
408 | prev = at; |
404 | } |
409 | } |
|
|
410 | |
|
|
411 | loading_arch = false; |
|
|
412 | return head; |
405 | } |
413 | } |
406 | |
414 | |
407 | return true; |
|
|
408 | |
|
|
409 | fail: |
415 | fail: |
410 | for (AUTODECL (p, parts.begin ()); p != parts.end (); ++p) |
416 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
411 | p->second->destroy (true); |
417 | p->second->destroy (true); |
412 | |
418 | |
413 | return false; |
|
|
414 | } |
|
|
415 | |
|
|
416 | /* |
|
|
417 | * Reads/parses the archetype-file, and copies into a linked list |
|
|
418 | * of archetype-structures. |
|
|
419 | */ |
|
|
420 | static bool |
|
|
421 | load_archetypes (object_thawer &f) |
|
|
422 | { |
|
|
423 | for (;;) |
|
|
424 | { |
|
|
425 | switch (f.kw) |
|
|
426 | { |
|
|
427 | case KW_object: |
|
|
428 | loading_arch = true; |
|
|
429 | if (!archetype::load (f)) |
|
|
430 | { |
|
|
431 | loading_arch = false; |
|
|
432 | return false; |
|
|
433 | } |
|
|
434 | |
|
|
435 | loading_arch = false; |
419 | loading_arch = false; |
436 | continue; |
420 | return 0; |
437 | |
|
|
438 | case KW_EOF: |
|
|
439 | return true; |
|
|
440 | |
|
|
441 | default: |
|
|
442 | if (!f.parse_error ("archetypes file")) |
|
|
443 | return false; |
|
|
444 | } |
|
|
445 | |
|
|
446 | f.next (); |
|
|
447 | } |
|
|
448 | } |
421 | } |
449 | |
422 | |
450 | /* |
423 | /* |
451 | * First initialises the archtype hash-table (init_archetable()). |
424 | * Initialize global archtype pointers: |
452 | * Reads and parses the archetype file (with the first and second-pass |
|
|
453 | * functions). |
|
|
454 | */ |
425 | */ |
455 | bool |
426 | void |
456 | load_archetype_file (const char *filename) |
427 | init_archetype_pointers () |
457 | { |
428 | { |
458 | object_thawer f (filename); |
429 | ring_arch = archetype::find ("ring"); |
459 | |
430 | amulet_arch = archetype::find ("amulet"); |
460 | f.next (); |
431 | staff_arch = archetype::find ("staff"); |
461 | |
432 | crown_arch = archetype::find ("crown"); |
462 | // make sure the next - long - step is only done after a tick |
|
|
463 | coroapi::wait_for_tick_begin (); |
|
|
464 | |
|
|
465 | if (!load_archetypes (f)) |
|
|
466 | return false; |
|
|
467 | |
|
|
468 | warn_archetypes = 1; |
|
|
469 | |
|
|
470 | empty_archetype = archetype::find ("empty_archetype"); |
433 | empty_archetype = archetype::find ("empty_archetype"); |
471 | if (!empty_archetype) |
|
|
472 | return false; |
|
|
473 | |
|
|
474 | coroapi::cede (); |
|
|
475 | |
|
|
476 | return true; |
|
|
477 | } |
434 | } |
478 | |
435 | |
479 | /* |
436 | /* |
480 | * Creates and returns a new object which is a copy of the given archetype. |
437 | * Creates and returns a new object which is a copy of the given archetype. |
481 | * This function returns NULL on failure. |
438 | * This function returns NULL on failure. |
… | |
… | |
483 | object * |
440 | object * |
484 | arch_to_object (archetype *at) |
441 | arch_to_object (archetype *at) |
485 | { |
442 | { |
486 | if (!at) |
443 | if (!at) |
487 | { |
444 | { |
488 | if (warn_archetypes) |
|
|
489 | LOG (llevError, "Couldn't find archetype.\n"); |
445 | LOG (llevError, "Couldn't find archetype.\n"); |
490 | |
|
|
491 | return NULL; |
446 | return 0; |
492 | } |
447 | } |
493 | |
448 | |
494 | object *op = at->clone.clone (); |
449 | object *op = at->clone.clone (); |
495 | op->arch = at; |
450 | op->arch = at; |
496 | op->instantiate (); |
451 | op->instantiate (); |
|
|
452 | |
497 | return op; |
453 | return op; |
498 | } |
454 | } |
499 | |
455 | |
500 | /* |
456 | /* |
501 | * Creates an object. This function is called by get_archetype() |
457 | * Creates an object. This function is called by get_archetype() |
… | |
… | |
530 | |
486 | |
531 | return arch_to_object (at); |
487 | return arch_to_object (at); |
532 | } |
488 | } |
533 | |
489 | |
534 | /* |
490 | /* |
535 | * Hash-function used by the arch-hashtable. |
|
|
536 | */ |
|
|
537 | |
|
|
538 | unsigned long |
|
|
539 | hasharch (const char *str, int tablesize) |
|
|
540 | { |
|
|
541 | unsigned long hash = 0; |
|
|
542 | unsigned int i = 0; |
|
|
543 | const char *p; |
|
|
544 | |
|
|
545 | /* use the one-at-a-time hash function, which supposedly is |
|
|
546 | * better than the djb2-like one used by perl5.005, but |
|
|
547 | * certainly is better then the bug used here before. |
|
|
548 | * see http://burtleburtle.net/bob/hash/doobs.html |
|
|
549 | */ |
|
|
550 | for (p = str; i < MAXSTRING && *p; p++, i++) |
|
|
551 | { |
|
|
552 | hash += *p; |
|
|
553 | hash += hash << 10; |
|
|
554 | hash ^= hash >> 6; |
|
|
555 | } |
|
|
556 | |
|
|
557 | hash += hash << 3; |
|
|
558 | hash ^= hash >> 11; |
|
|
559 | hash += hash << 15; |
|
|
560 | |
|
|
561 | return hash % tablesize; |
|
|
562 | } |
|
|
563 | |
|
|
564 | /* |
|
|
565 | * Finds, using the hashtable, which archetype matches the given name. |
491 | * Finds, using the hashtable, which archetype matches the given name. |
566 | * returns a pointer to the found archetype, otherwise NULL. |
492 | * returns a pointer to the found archetype, otherwise NULL. |
567 | */ |
493 | */ |
568 | archetype * |
494 | archetype * |
569 | archetype::find (const char *name) |
495 | archetype::find (const char *name) |
570 | { |
496 | { |
571 | if (!name) |
497 | if (!name) |
572 | return 0; |
498 | return 0; |
573 | |
499 | |
574 | AUTODECL (i, ht.find (name)); |
500 | auto (i, ht.find (name)); |
575 | |
501 | |
576 | if (i == ht.end ()) |
502 | if (i == ht.end ()) |
577 | return 0; |
503 | return 0; |
578 | else |
504 | else |
579 | return i->second; |
505 | return i->second; |