… | |
… | |
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; |
325 | } |
|
|
326 | |
317 | |
327 | archetype * |
318 | op->destroy (); |
|
|
319 | } |
|
|
320 | |
|
|
321 | bool |
328 | archetype::read (object_thawer &f) |
322 | archetype::load (object_thawer &f) |
329 | { |
323 | { |
330 | assert (f.kw == KW_object); |
324 | assert (f.kw == KW_object); |
331 | |
325 | |
|
|
326 | typedef std::pair<archetype *, object *> part; |
|
|
327 | std::vector<part> parts; |
|
|
328 | |
|
|
329 | coroapi::cede_to_tick_every (100); |
|
|
330 | |
|
|
331 | for (;;) |
|
|
332 | { |
332 | archetype *head = get (f.get_str ()); |
333 | archetype *at = get (f.get_str ()); |
333 | unlink (head); |
|
|
334 | object *op = object::create (); |
334 | object *op = object::create (); |
335 | |
335 | |
336 | if (!op->parse_kv (f)) |
336 | if (!op->parse_kv (f)) |
337 | { |
337 | goto fail; |
338 | op->destroy (true); |
|
|
339 | // leak head |
|
|
340 | return 0; |
|
|
341 | } |
|
|
342 | |
338 | |
343 | overwrite (head, op); |
339 | parts.push_back (std::make_pair (at, op)); |
344 | op->destroy (); |
|
|
345 | |
340 | |
346 | for (archetype *prev = head; f.kw == KW_more; ) |
341 | if (f.kw != KW_more) |
347 | { |
342 | break; |
|
|
343 | |
348 | f.next (); |
344 | f.next (); |
349 | assert (f.kw == KW_object); |
345 | assert (f.kw == KW_object); |
|
|
346 | } |
350 | |
347 | |
351 | archetype *more = get (f.get_str ()); |
348 | { |
352 | unlink (more); |
349 | archetype *head = parts.front ().first; |
353 | object *op = object::create (); |
|
|
354 | |
350 | |
355 | if (!op->parse_kv (f)) |
351 | // check that all archetypes belong to the same object or are heads |
|
|
352 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
|
|
353 | { |
|
|
354 | archetype *at = p->first; |
|
|
355 | |
|
|
356 | if (at->head != head && at->head) |
356 | { |
357 | { |
357 | op->destroy (true); |
358 | LOG (llevError, "%s: unable to overwrite foreign non-head archetype with non-head archetype\n", &at->name); |
358 | // leak head more* |
359 | goto fail; |
359 | return 0; |
|
|
360 | } |
360 | } |
361 | |
361 | |
362 | overwrite (more, op); |
362 | if (at->next && at != head) |
363 | op->destroy (); |
363 | { |
364 | |
364 | 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; |
365 | goto fail; |
366 | if (more->clone.y > head->tail_y) head->tail_y = more->clone.y; |
366 | } |
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 | } |
367 | } |
375 | |
368 | |
376 | if (!head->next) |
369 | // sever chain of existing head object |
|
|
370 | for (archetype *more, *at = head; at; at = more) |
377 | { |
371 | { |
|
|
372 | more = at->more; |
|
|
373 | |
|
|
374 | at->head = 0; |
|
|
375 | at->more = 0; |
|
|
376 | } |
|
|
377 | |
|
|
378 | // replace/update head |
|
|
379 | overwrite (head, parts.front ().second); |
|
|
380 | head->tail_x = 0; |
|
|
381 | head->tail_y = 0; |
|
|
382 | |
|
|
383 | // link into list of heads, if not already there |
|
|
384 | if (!head->linked) |
|
|
385 | { |
|
|
386 | head->linked = true; |
378 | head->next = first_archetype; |
387 | head->next = first_archetype; |
379 | first_archetype = head; |
388 | first_archetype = head; |
380 | } |
389 | } |
381 | |
390 | |
|
|
391 | // reassemble new chain |
|
|
392 | archetype *prev = head; |
|
|
393 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
|
|
394 | { |
|
|
395 | archetype *at = p->first; |
|
|
396 | overwrite (at, p->second); |
|
|
397 | |
|
|
398 | if (at->clone.x > head->tail_x) head->tail_x = at->clone.x; |
|
|
399 | if (at->clone.y > head->tail_y) head->tail_y = at->clone.y; |
|
|
400 | |
|
|
401 | at->head = head; |
|
|
402 | at->clone.head = &head->clone; |
|
|
403 | prev->more = at; |
|
|
404 | prev->clone.more = &at->clone; |
|
|
405 | |
|
|
406 | prev = at; |
|
|
407 | } |
|
|
408 | } |
|
|
409 | |
382 | return head; |
410 | return true; |
|
|
411 | |
|
|
412 | fail: |
|
|
413 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
|
|
414 | p->second->destroy (true); |
|
|
415 | |
|
|
416 | return false; |
383 | } |
417 | } |
384 | |
418 | |
385 | /* |
419 | /* |
386 | * Reads/parses the archetype-file, and copies into a linked list |
420 | * Reads/parses the archetype-file, and copies into a linked list |
387 | * of archetype-structures. |
421 | * of archetype-structures. |
… | |
… | |
393 | { |
427 | { |
394 | switch (f.kw) |
428 | switch (f.kw) |
395 | { |
429 | { |
396 | case KW_object: |
430 | case KW_object: |
397 | loading_arch = true; |
431 | loading_arch = true; |
398 | if (!archetype::read (f)) |
432 | if (!archetype::load (f)) |
399 | { |
433 | { |
400 | loading_arch = false; |
434 | loading_arch = false; |
401 | return false; |
435 | return false; |
402 | } |
436 | } |
403 | |
437 | |
… | |
… | |
419 | /* |
453 | /* |
420 | * First initialises the archtype hash-table (init_archetable()). |
454 | * First initialises the archtype hash-table (init_archetable()). |
421 | * Reads and parses the archetype file (with the first and second-pass |
455 | * Reads and parses the archetype file (with the first and second-pass |
422 | * functions). |
456 | * functions). |
423 | */ |
457 | */ |
424 | void |
458 | bool |
425 | load_archetypes (void) |
459 | load_archetype_file (const char *filename) |
426 | { |
460 | { |
427 | char filename[MAX_BUF]; |
|
|
428 | |
|
|
429 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
|
|
430 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
|
|
431 | |
|
|
432 | object_thawer f (filename); |
461 | object_thawer f (filename); |
433 | |
462 | |
434 | f.next (); |
463 | f.next (); |
435 | |
464 | |
436 | if (!load_archetypes (f)) |
465 | if (!load_archetypes (f)) |
437 | cleanup ("unable to load archetypes"); |
466 | return false; |
438 | |
467 | |
439 | warn_archetypes = 1; |
468 | warn_archetypes = 1; |
440 | |
469 | |
441 | load_treasures (); |
470 | empty_archetype = archetype::find ("empty_archetype"); |
|
|
471 | if (!empty_archetype) |
|
|
472 | return false; |
|
|
473 | |
|
|
474 | return true; |
442 | } |
475 | } |
443 | |
476 | |
444 | /* |
477 | /* |
445 | * Creates and returns a new object which is a copy of the given archetype. |
478 | * Creates and returns a new object which is a copy of the given archetype. |
446 | * This function returns NULL on failure. |
479 | * This function returns NULL on failure. |
… | |
… | |
495 | |
528 | |
496 | return arch_to_object (at); |
529 | return arch_to_object (at); |
497 | } |
530 | } |
498 | |
531 | |
499 | /* |
532 | /* |
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. |
533 | * Finds, using the hashtable, which archetype matches the given name. |
531 | * returns a pointer to the found archetype, otherwise NULL. |
534 | * returns a pointer to the found archetype, otherwise NULL. |
532 | */ |
535 | */ |
533 | archetype * |
536 | archetype * |
534 | archetype::find (const char *name) |
537 | archetype::find (const char *name) |
535 | { |
538 | { |
536 | if (!name) |
539 | if (!name) |
537 | return 0; |
540 | return 0; |
538 | |
541 | |
539 | AUTODECL (i, ht.find (name)); |
542 | auto (i, ht.find (name)); |
540 | |
543 | |
541 | if (i == ht.end ()) |
544 | if (i == ht.end ()) |
542 | return 0; |
545 | return 0; |
543 | else |
546 | else |
544 | return i->second; |
547 | return i->second; |