… | |
… | |
309 | // dire hack, need to rationalise |
309 | // dire hack, need to rationalise |
310 | void |
310 | void |
311 | overwrite (archetype *at, object *op) |
311 | overwrite (archetype *at, object *op) |
312 | { |
312 | { |
313 | at->clone = *op; |
313 | at->clone = *op; |
314 | |
|
|
315 | at->clone.arch = at; |
314 | at->clone.arch = at; |
316 | //TODO: inv et al. |
315 | |
|
|
316 | at->clone.inv = op->inv; op->inv = 0; |
|
|
317 | |
|
|
318 | op->destroy (); |
317 | } |
319 | } |
318 | |
320 | |
319 | archetype * |
321 | archetype * |
320 | archetype::read (object_thawer &f) |
322 | archetype::read (object_thawer &f) |
321 | { |
323 | { |
322 | assert (f.kw == KW_object); |
324 | assert (f.kw == KW_object); |
323 | |
325 | |
324 | archetype *head = get (f.get_str ()); |
326 | loading_arch = true; // hack to tell parse_kv et al. to behave |
325 | unlink (head); |
327 | |
|
|
328 | typedef std::pair<archetype *, object *> part; |
|
|
329 | std::vector<part> parts; |
|
|
330 | |
|
|
331 | coroapi::cede_to_tick_every (100); |
|
|
332 | |
|
|
333 | for (;;) |
|
|
334 | { |
326 | object *op = object::create (); |
335 | object *op = object::create (); |
327 | |
336 | archetype *at = get (f.get_str ()); |
328 | if (!op->parse_kv (f)) |
337 | f.get (op->name); |
329 | { |
|
|
330 | op->destroy (true); |
|
|
331 | // leak head |
|
|
332 | return 0; |
|
|
333 | } |
|
|
334 | |
|
|
335 | overwrite (head, op); |
|
|
336 | op->destroy (); |
|
|
337 | |
|
|
338 | for (archetype *prev = head; f.kw == KW_more; ) |
|
|
339 | { |
|
|
340 | f.next (); |
338 | f.next (); |
341 | assert (f.kw == KW_object); |
|
|
342 | |
339 | |
|
|
340 | #if 0 |
|
|
341 | if (f.kw == KW_inherit) |
|
|
342 | { |
343 | archetype *more = get (f.get_str ()); |
343 | if (archetype *at = find (f.get_str ())) |
344 | unlink (more); |
344 | *op = at->clone; |
345 | object *op = object::create (); |
345 | else |
|
|
346 | LOG (llevError, "archetype '%s' tries to inherit from non-existent archetype '%s'.\n", |
|
|
347 | &at->name, f.get_str ()); |
|
|
348 | |
|
|
349 | f.next (); |
|
|
350 | } |
|
|
351 | #endif |
346 | |
352 | |
347 | if (!op->parse_kv (f)) |
353 | if (!op->parse_kv (f)) |
|
|
354 | goto fail; |
|
|
355 | |
|
|
356 | op->post_load_check (); |
|
|
357 | |
|
|
358 | parts.push_back (std::make_pair (at, op)); |
|
|
359 | |
|
|
360 | if (f.kw != KW_more) |
|
|
361 | break; |
|
|
362 | |
|
|
363 | f.next (); |
|
|
364 | |
|
|
365 | if (f.kw != KW_object) |
348 | { |
366 | { |
349 | op->destroy (true); |
367 | f.parse_error ("more object"); |
350 | // leak head more* |
368 | goto fail; |
351 | return 0; |
|
|
352 | } |
369 | } |
353 | |
|
|
354 | overwrite (more, op); |
|
|
355 | op->destroy (); |
|
|
356 | |
|
|
357 | if (more->clone.x > head->tail_x) head->tail_x = more->clone.x; |
|
|
358 | if (more->clone.y > head->tail_y) head->tail_y = more->clone.y; |
|
|
359 | |
|
|
360 | more->head = head; |
|
|
361 | more->clone.head = &head->clone; |
|
|
362 | prev->more = more; |
|
|
363 | prev->clone.more = &more->clone; |
|
|
364 | |
|
|
365 | prev = more; |
|
|
366 | } |
370 | } |
367 | |
371 | |
368 | if (!head->next) |
372 | { |
|
|
373 | archetype *head = parts.front ().first; |
|
|
374 | |
|
|
375 | // check that all archetypes belong to the same object or are heads |
|
|
376 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
369 | { |
377 | { |
|
|
378 | archetype *at = p->first; |
|
|
379 | |
|
|
380 | if (at->head != head && at->head) |
|
|
381 | { |
|
|
382 | LOG (llevError, "%s: unable to overwrite foreign non-head archetype with non-head archetype\n", &at->name); |
|
|
383 | goto fail; |
|
|
384 | } |
|
|
385 | |
|
|
386 | if (at->next && at != head) |
|
|
387 | { |
|
|
388 | LOG (llevError, "%s: unable to overwrite foreign head archetype with non-head archetype\n", &at->name); |
|
|
389 | goto fail; |
|
|
390 | } |
|
|
391 | } |
|
|
392 | |
|
|
393 | // sever chain of existing head object |
|
|
394 | for (archetype *more, *at = head; at; at = more) |
|
|
395 | { |
|
|
396 | more = at->more; |
|
|
397 | |
|
|
398 | at->head = 0; |
|
|
399 | at->more = 0; |
|
|
400 | } |
|
|
401 | |
|
|
402 | // replace/update head |
|
|
403 | overwrite (head, parts.front ().second); |
|
|
404 | head->tail_x = 0; |
|
|
405 | head->tail_y = 0; |
|
|
406 | |
|
|
407 | // link into list of heads, if not already there |
|
|
408 | if (!head->linked) |
|
|
409 | { |
|
|
410 | head->linked = true; |
370 | head->next = first_archetype; |
411 | head->next = first_archetype; |
371 | first_archetype = head; |
412 | first_archetype = head; |
372 | } |
413 | } |
373 | |
414 | |
|
|
415 | // reassemble new chain |
|
|
416 | archetype *prev = head; |
|
|
417 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
|
|
418 | { |
|
|
419 | archetype *at = p->first; |
|
|
420 | overwrite (at, p->second); |
|
|
421 | |
|
|
422 | if (at->clone.x > head->tail_x) head->tail_x = at->clone.x; |
|
|
423 | if (at->clone.y > head->tail_y) head->tail_y = at->clone.y; |
|
|
424 | |
|
|
425 | at->head = head; |
|
|
426 | at->clone.head = &head->clone; |
|
|
427 | prev->more = at; |
|
|
428 | prev->clone.more = &at->clone; |
|
|
429 | |
|
|
430 | prev = at; |
|
|
431 | } |
|
|
432 | |
|
|
433 | loading_arch = false; |
374 | return head; |
434 | return head; |
375 | } |
435 | } |
376 | |
436 | |
377 | /* |
437 | fail: |
378 | * Reads/parses the archetype-file, and copies into a linked list |
438 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
379 | * of archetype-structures. |
439 | p->second->destroy (true); |
380 | */ |
|
|
381 | static bool |
|
|
382 | load_archetypes (object_thawer &f) |
|
|
383 | { |
|
|
384 | for (;;) |
|
|
385 | { |
|
|
386 | switch (f.kw) |
|
|
387 | { |
|
|
388 | case KW_object: |
|
|
389 | loading_arch = true; |
|
|
390 | if (!archetype::read (f)) |
|
|
391 | { |
|
|
392 | loading_arch = false; |
|
|
393 | return false; |
|
|
394 | } |
|
|
395 | |
440 | |
396 | loading_arch = false; |
441 | loading_arch = false; |
397 | continue; |
442 | return 0; |
398 | |
|
|
399 | case KW_EOF: |
|
|
400 | return true; |
|
|
401 | |
|
|
402 | default: |
|
|
403 | if (!f.parse_error ("archetypes file")) |
|
|
404 | return false; |
|
|
405 | } |
|
|
406 | |
|
|
407 | f.next (); |
|
|
408 | } |
|
|
409 | } |
443 | } |
410 | |
444 | |
411 | /* |
445 | /* |
412 | * First initialises the archtype hash-table (init_archetable()). |
446 | * Initialize global archtype pointers: |
413 | * Reads and parses the archetype file (with the first and second-pass |
|
|
414 | * functions). |
|
|
415 | */ |
447 | */ |
416 | bool |
448 | void |
417 | load_archetype_file (const char *filename) |
449 | init_archetype_pointers () |
418 | { |
450 | { |
419 | object_thawer f (filename); |
451 | ring_arch = archetype::find ("ring"); |
420 | |
452 | amulet_arch = archetype::find ("amulet"); |
421 | f.next (); |
453 | staff_arch = archetype::find ("staff"); |
422 | |
454 | crown_arch = archetype::find ("crown"); |
423 | // make sure the next - long - step is only done after a tick |
|
|
424 | coroapi::wait_for_tick_begin (); |
|
|
425 | |
|
|
426 | if (!load_archetypes (f)) |
|
|
427 | return false; |
|
|
428 | |
|
|
429 | warn_archetypes = 1; |
|
|
430 | |
|
|
431 | empty_archetype = archetype::find ("empty_archetype"); |
455 | empty_archetype = archetype::find ("empty_archetype"); |
432 | if (!empty_archetype) |
|
|
433 | return false; |
|
|
434 | |
|
|
435 | coroapi::cede (); |
|
|
436 | |
|
|
437 | return true; |
|
|
438 | } |
456 | } |
439 | |
457 | |
440 | /* |
458 | /* |
441 | * Creates and returns a new object which is a copy of the given archetype. |
459 | * Creates and returns a new object which is a copy of the given archetype. |
442 | * This function returns NULL on failure. |
460 | * This function returns NULL on failure. |
… | |
… | |
444 | object * |
462 | object * |
445 | arch_to_object (archetype *at) |
463 | arch_to_object (archetype *at) |
446 | { |
464 | { |
447 | if (!at) |
465 | if (!at) |
448 | { |
466 | { |
449 | if (warn_archetypes) |
|
|
450 | LOG (llevError, "Couldn't find archetype.\n"); |
467 | LOG (llevError, "Couldn't find archetype.\n"); |
451 | |
|
|
452 | return NULL; |
468 | return 0; |
453 | } |
469 | } |
454 | |
470 | |
455 | object *op = at->clone.clone (); |
471 | object *op = at->clone.clone (); |
456 | op->arch = at; |
472 | op->arch = at; |
457 | op->instantiate (); |
473 | op->instantiate (); |
|
|
474 | |
458 | return op; |
475 | return op; |
459 | } |
476 | } |
460 | |
477 | |
461 | /* |
478 | /* |
462 | * Creates an object. This function is called by get_archetype() |
479 | * Creates an object. This function is called by get_archetype() |
… | |
… | |
491 | |
508 | |
492 | return arch_to_object (at); |
509 | return arch_to_object (at); |
493 | } |
510 | } |
494 | |
511 | |
495 | /* |
512 | /* |
496 | * Hash-function used by the arch-hashtable. |
|
|
497 | */ |
|
|
498 | |
|
|
499 | unsigned long |
|
|
500 | hasharch (const char *str, int tablesize) |
|
|
501 | { |
|
|
502 | unsigned long hash = 0; |
|
|
503 | unsigned int i = 0; |
|
|
504 | const char *p; |
|
|
505 | |
|
|
506 | /* use the one-at-a-time hash function, which supposedly is |
|
|
507 | * better than the djb2-like one used by perl5.005, but |
|
|
508 | * certainly is better then the bug used here before. |
|
|
509 | * see http://burtleburtle.net/bob/hash/doobs.html |
|
|
510 | */ |
|
|
511 | for (p = str; i < MAXSTRING && *p; p++, i++) |
|
|
512 | { |
|
|
513 | hash += *p; |
|
|
514 | hash += hash << 10; |
|
|
515 | hash ^= hash >> 6; |
|
|
516 | } |
|
|
517 | |
|
|
518 | hash += hash << 3; |
|
|
519 | hash ^= hash >> 11; |
|
|
520 | hash += hash << 15; |
|
|
521 | |
|
|
522 | return hash % tablesize; |
|
|
523 | } |
|
|
524 | |
|
|
525 | /* |
|
|
526 | * Finds, using the hashtable, which archetype matches the given name. |
513 | * Finds, using the hashtable, which archetype matches the given name. |
527 | * returns a pointer to the found archetype, otherwise NULL. |
514 | * returns a pointer to the found archetype, otherwise NULL. |
528 | */ |
515 | */ |
529 | archetype * |
516 | archetype * |
530 | archetype::find (const char *name) |
517 | archetype::find (const char *name) |
531 | { |
518 | { |
532 | if (!name) |
519 | if (!name) |
533 | return 0; |
520 | return 0; |
534 | |
521 | |
535 | AUTODECL (i, ht.find (name)); |
522 | auto (i, ht.find (name)); |
536 | |
523 | |
537 | if (i == ht.end ()) |
524 | if (i == ht.end ()) |
538 | return 0; |
525 | return 0; |
539 | else |
526 | else |
540 | return i->second; |
527 | return i->second; |
541 | } |
528 | } |
542 | |
529 | |
543 | archetype * |
530 | archetype * |
544 | archetype::get (const char *name) |
531 | archetype::get (const char *name) |
545 | { |
532 | { |
|
|
533 | if (!name) |
|
|
534 | { |
|
|
535 | LOG (llevError, "null archetype requested\n"); |
|
|
536 | name = "(null)"; |
|
|
537 | } |
|
|
538 | |
546 | archetype *at = find (name); |
539 | archetype *at = find (name); |
547 | |
540 | |
548 | if (!at) |
541 | if (!at) |
549 | { |
542 | { |
550 | archetypes.push_back (at = new archetype); |
543 | archetypes.push_back (at = new archetype); |
551 | at->name = name; |
544 | at->name = at->clone.name = at->clone.name_pl = name; |
552 | at->hash_add (); |
545 | at->hash_add (); |
553 | } |
546 | } |
554 | |
547 | |
555 | return at; |
548 | return at; |
556 | } |
549 | } |