ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
(Generate patch)

Comparing deliantra/server/common/arch.C (file contents):
Revision 1.47 by root, Mon Apr 16 10:14:25 2007 UTC vs.
Revision 1.49 by root, Mon Apr 16 12:37:08 2007 UTC

275 } 275 }
276 276
277 return 0; 277 return 0;
278} 278}
279 279
280void
281init_archetypes (void)
282{
283 load_archetypes ();
284
285 empty_archetype = archetype::find ("empty_archetype");
286}
287
288archetype::archetype () 280archetype::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 */
319overwrite (archetype *at, object *op) 311overwrite (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
327archetype * 318 op->destroy ();
319}
320
321bool
328archetype::read (object_thawer &f) 322archetype::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 for (;;)
330 {
332 archetype *head = get (f.get_str ()); 331 archetype *at = get (f.get_str ());
333 unlink (head);
334 object *op = object::create (); 332 object *op = object::create ();
335 333
336 if (!op->parse_kv (f)) 334 if (!op->parse_kv (f))
337 { 335 goto fail;
338 op->destroy (true);
339 // leak head
340 return 0;
341 }
342 336
343 overwrite (head, op); 337 parts.push_back (std::make_pair (at, op));
344 op->destroy ();
345 338
346 for (archetype *prev = head; f.kw == KW_more; ) 339 if (f.kw != KW_more)
347 { 340 break;
341
348 f.next (); 342 f.next ();
349 assert (f.kw == KW_object); 343 assert (f.kw == KW_object);
344 }
350 345
351 archetype *more = get (f.get_str ()); 346 {
352 unlink (more); 347 archetype *head = parts.front ().first;
353 object *op = object::create ();
354 348
355 if (!op->parse_kv (f)) 349 // check that all archetypes belong to the same object or are heads
350 for (AUTODECL (p, parts.begin ()); p != parts.end (); ++p)
351 {
352 archetype *at = p->first;
353
354 if (at->head != head && at->head)
356 { 355 {
357 op->destroy (true); 356 LOG (llevError, "%s: unable to overwrite foreign non-head archetype with non-head archetype\n", &at->name);
358 // leak head more* 357 goto fail;
359 return 0;
360 } 358 }
361 359
362 overwrite (more, op); 360 if (at->next && at != head)
363 op->destroy (); 361 {
364 362 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; 363 goto fail;
366 if (more->clone.y > head->tail_y) head->tail_y = more->clone.y; 364 }
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 } 365 }
375 366
367 // sever chain of existing head object
368 for (archetype *more, *at = head; at; at = more)
369 {
370 more = at->more;
371
372 at->head = 0;
373 at->more = 0;
374 }
375
376 // replace/update head
377 overwrite (head, parts.front ().second);
378 head->tail_x = 0;
379 head->tail_y = 0;
380
381 // link into list of heads, if not already there
376 if (!head->next) 382 if (!head->next)
377 { 383 {
378 head->next = first_archetype; 384 head->next = first_archetype;
379 first_archetype = head; 385 first_archetype = head;
380 } 386 }
381 387
388 // reassemble new chain
389 archetype *prev = head;
390 for (AUTODECL (p, parts.begin () + 1); p != parts.end (); ++p)
391 {
392 archetype *at = p->first;
393 overwrite (at, p->second);
394
395 if (at->clone.x > head->tail_x) head->tail_x = at->clone.x;
396 if (at->clone.y > head->tail_y) head->tail_y = at->clone.y;
397
398 at->head = head;
399 at->clone.head = &head->clone;
400 prev->more = at;
401 prev->clone.more = &at->clone;
402
403 prev = at;
404 }
405 }
406
382 return head; 407 return true;
408
409fail:
410 for (AUTODECL (p, parts.begin ()); p != parts.end (); ++p)
411 p->second->destroy (true);
412
413 return false;
383} 414}
384 415
385/* 416/*
386 * Reads/parses the archetype-file, and copies into a linked list 417 * Reads/parses the archetype-file, and copies into a linked list
387 * of archetype-structures. 418 * of archetype-structures.
393 { 424 {
394 switch (f.kw) 425 switch (f.kw)
395 { 426 {
396 case KW_object: 427 case KW_object:
397 loading_arch = true; 428 loading_arch = true;
398 if (!archetype::read (f)) 429 if (!archetype::load (f))
399 { 430 {
400 loading_arch = false; 431 loading_arch = false;
401 return false; 432 return false;
402 } 433 }
403 434
419/* 450/*
420 * First initialises the archtype hash-table (init_archetable()). 451 * First initialises the archtype hash-table (init_archetable()).
421 * Reads and parses the archetype file (with the first and second-pass 452 * Reads and parses the archetype file (with the first and second-pass
422 * functions). 453 * functions).
423 */ 454 */
424void 455bool
425load_archetypes (void) 456load_archetype_file (const char *filename)
426{ 457{
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); 458 object_thawer f (filename);
433 459
434 f.next (); 460 f.next ();
435 461
462 // make sure the next - long - step is only done after a tick
463 coroapi::wait_for_tick_begin ();
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 coroapi::cede ();
475
476 return true;
442} 477}
443 478
444/* 479/*
445 * Creates and returns a new object which is a copy of the given archetype. 480 * Creates and returns a new object which is a copy of the given archetype.
446 * This function returns NULL on failure. 481 * This function returns NULL on failure.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines