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