… | |
… | |
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 | |
329 | coroapi::cede_to_tick_every (100); |
331 | coroapi::cede_to_tick_every (100); |
330 | |
332 | |
331 | for (;;) |
333 | for (;;) |
332 | { |
334 | { |
|
|
335 | object *op = object::create (); |
333 | archetype *at = get (f.get_str ()); |
336 | archetype *at = get (f.get_str ()); |
334 | object *op = object::create (); |
337 | f.get (op->name); |
|
|
338 | f.next (); |
335 | |
339 | |
336 | if (!op->parse_kv (f)) |
340 | if (!op->parse_kv (f)) |
337 | goto fail; |
341 | goto fail; |
338 | |
342 | |
339 | parts.push_back (std::make_pair (at, op)); |
343 | parts.push_back (std::make_pair (at, op)); |
340 | |
344 | |
341 | if (f.kw != KW_more) |
345 | if (f.kw != KW_more) |
342 | break; |
346 | break; |
343 | |
347 | |
344 | f.next (); |
348 | f.next (); |
|
|
349 | |
345 | assert (f.kw == KW_object); |
350 | if (f.kw != KW_object) |
|
|
351 | { |
|
|
352 | f.parse_error ("more object"); |
|
|
353 | goto fail; |
|
|
354 | } |
346 | } |
355 | } |
347 | |
356 | |
348 | { |
357 | { |
349 | archetype *head = parts.front ().first; |
358 | archetype *head = parts.front ().first; |
350 | |
359 | |
… | |
… | |
403 | prev->more = at; |
412 | prev->more = at; |
404 | prev->clone.more = &at->clone; |
413 | prev->clone.more = &at->clone; |
405 | |
414 | |
406 | prev = at; |
415 | prev = at; |
407 | } |
416 | } |
|
|
417 | |
|
|
418 | loading_arch = false; |
|
|
419 | return head; |
408 | } |
420 | } |
409 | |
|
|
410 | return true; |
|
|
411 | |
421 | |
412 | fail: |
422 | fail: |
413 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
423 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
414 | p->second->destroy (true); |
424 | p->second->destroy (true); |
415 | |
425 | |
416 | return false; |
|
|
417 | } |
|
|
418 | |
|
|
419 | /* |
|
|
420 | * Reads/parses the archetype-file, and copies into a linked list |
|
|
421 | * of archetype-structures. |
|
|
422 | */ |
|
|
423 | static bool |
|
|
424 | load_archetypes (object_thawer &f) |
|
|
425 | { |
|
|
426 | for (;;) |
|
|
427 | { |
|
|
428 | switch (f.kw) |
|
|
429 | { |
|
|
430 | case KW_object: |
|
|
431 | loading_arch = true; |
|
|
432 | if (!archetype::load (f)) |
|
|
433 | { |
|
|
434 | loading_arch = false; |
|
|
435 | return false; |
|
|
436 | } |
|
|
437 | |
|
|
438 | loading_arch = false; |
426 | loading_arch = false; |
439 | continue; |
427 | return 0; |
440 | |
|
|
441 | case KW_EOF: |
|
|
442 | return true; |
|
|
443 | |
|
|
444 | default: |
|
|
445 | if (!f.parse_error ("archetypes file")) |
|
|
446 | return false; |
|
|
447 | } |
|
|
448 | |
|
|
449 | f.next (); |
|
|
450 | } |
|
|
451 | } |
428 | } |
452 | |
429 | |
453 | /* |
430 | /* |
454 | * First initialises the archtype hash-table (init_archetable()). |
431 | * Initialize global archtype pointers: |
455 | * Reads and parses the archetype file (with the first and second-pass |
|
|
456 | * functions). |
|
|
457 | */ |
432 | */ |
458 | bool |
433 | void |
459 | load_archetype_file (const char *filename) |
434 | init_archetype_pointers () |
460 | { |
435 | { |
461 | object_thawer f (filename); |
436 | ring_arch = archetype::find ("ring"); |
462 | |
437 | amulet_arch = archetype::find ("amulet"); |
463 | f.next (); |
438 | staff_arch = archetype::find ("staff"); |
464 | |
439 | crown_arch = archetype::find ("crown"); |
465 | if (!load_archetypes (f)) |
|
|
466 | return false; |
|
|
467 | |
|
|
468 | warn_archetypes = 1; |
|
|
469 | |
|
|
470 | empty_archetype = archetype::find ("empty_archetype"); |
440 | empty_archetype = archetype::find ("empty_archetype"); |
471 | if (!empty_archetype) |
|
|
472 | return false; |
|
|
473 | |
|
|
474 | return true; |
|
|
475 | } |
441 | } |
476 | |
442 | |
477 | /* |
443 | /* |
478 | * Creates and returns a new object which is a copy of the given archetype. |
444 | * Creates and returns a new object which is a copy of the given archetype. |
479 | * This function returns NULL on failure. |
445 | * This function returns NULL on failure. |
… | |
… | |
481 | object * |
447 | object * |
482 | arch_to_object (archetype *at) |
448 | arch_to_object (archetype *at) |
483 | { |
449 | { |
484 | if (!at) |
450 | if (!at) |
485 | { |
451 | { |
486 | if (warn_archetypes) |
|
|
487 | LOG (llevError, "Couldn't find archetype.\n"); |
452 | LOG (llevError, "Couldn't find archetype.\n"); |
488 | |
|
|
489 | return NULL; |
453 | return 0; |
490 | } |
454 | } |
491 | |
455 | |
492 | object *op = at->clone.clone (); |
456 | object *op = at->clone.clone (); |
493 | op->arch = at; |
457 | op->arch = at; |
494 | op->instantiate (); |
458 | op->instantiate (); |
|
|
459 | |
495 | return op; |
460 | return op; |
496 | } |
461 | } |
497 | |
462 | |
498 | /* |
463 | /* |
499 | * Creates an object. This function is called by get_archetype() |
464 | * Creates an object. This function is called by get_archetype() |