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.12 by root, Mon Sep 4 17:16:19 2006 UTC vs.
Revision 1.15 by root, Sun Sep 10 13:20:12 2006 UTC

1
1/* 2/*
2 * static char *rcsid_arch_c = 3 * static char *rcsid_arch_c =
3 * "$Id: arch.C,v 1.12 2006/09/04 17:16:19 root Exp $"; 4 * "$Id: arch.C,v 1.15 2006/09/10 13:20:12 root Exp $";
4 */ 5 */
5 6
6/* 7/*
7 CrossFire, A Multiplayer game for X-windows 8 CrossFire, A Multiplayer game for X-windows
8 9
36#include <loader.h> 37#include <loader.h>
37 38
38/* IF set, does a little timing on the archetype load. */ 39/* IF set, does a little timing on the archetype load. */
39#define TIME_ARCH_LOAD 0 40#define TIME_ARCH_LOAD 0
40 41
41static void add_arch (archetype * at); 42static void add_arch (archetype *at);
42 43
43static archetype *arch_table[ARCHTABLE]; 44static archetype *arch_table[ARCHTABLE];
44int arch_cmp = 0; /* How many strcmp's */ 45int arch_cmp = 0; /* How many strcmp's */
45int arch_search = 0; /* How many searches */ 46int arch_search = 0; /* How many searches */
46int arch_init; /* True if doing arch initialization */ 47int arch_init; /* True if doing arch initialization */
53 * MSW 2003-04-29 54 * MSW 2003-04-29
54 */ 55 */
55 56
56#if USE_UNORDERED_MAP 57#if USE_UNORDERED_MAP
57// the hashtable 58// the hashtable
58typedef std::tr1::unordered_map<size_t, archetype *> HT; 59typedef
60 std::tr1::unordered_map <
61size_t, archetype *>
62 HT;
59 63
60static HT ht; 64static HT
65 ht;
61#endif 66#endif
62 67
63/** 68/**
64 * GROS - This function retrieves an archetype given the name that appears 69 * GROS - This function retrieves an archetype given the name that appears
65 * during the game (for example, "writing pen" instead of "stylus"). 70 * during the game (for example, "writing pen" instead of "stylus").
72 * - the archetype found or null if nothing was found. 77 * - the archetype found or null if nothing was found.
73 */ 78 */
74archetype * 79archetype *
75find_archetype_by_object_name (const char *name) 80find_archetype_by_object_name (const char *name)
76{ 81{
77 archetype *at; 82 archetype *
83 at;
78 84
79 if (name == NULL) 85 if (name == NULL)
80 return (archetype *) NULL; 86 return (archetype *) NULL;
81 87
82 for (at = first_archetype; at != NULL; at = at->next) 88 for (at = first_archetype; at != NULL; at = at->next)
93 * except that it considers only items of the given type. 99 * except that it considers only items of the given type.
94 */ 100 */
95archetype * 101archetype *
96find_archetype_by_object_type_name (int type, const char *name) 102find_archetype_by_object_type_name (int type, const char *name)
97{ 103{
98 archetype *at; 104 archetype *
105 at;
99 106
100 if (name == NULL) 107 if (name == NULL)
101 return NULL; 108 return NULL;
102 109
103 for (at = first_archetype; at != NULL; at = at->next) 110 for (at = first_archetype; at != NULL; at = at->next)
204 * If count is 1, make a quick check on the name. 211 * If count is 1, make a quick check on the name.
205 * IF count is >1, we need to make plural name. Return if match. 212 * IF count is >1, we need to make plural name. Return if match.
206 * Last, make a check on the full name. 213 * Last, make a check on the full name.
207 */ 214 */
208int 215int
209item_matched_string (object * pl, object * op, const char *name) 216item_matched_string (object *pl, object *op, const char *name)
210{ 217{
211 char * 218 char *
212 cp, 219 cp,
213 local_name[MAX_BUF]; 220 local_name[MAX_BUF];
214 int 221 int
215 count, 222 count,
216 retval = 0; 223 retval = 0;
224
217 strcpy (local_name, name); /* strtok is destructive to name */ 225 strcpy (local_name, name); /* strtok is destructive to name */
218 226
219 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) 227 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
220 { 228 {
221 while (cp[0] == ' ') 229 while (cp[0] == ' ')
326 return; 334 return;
327 arch_init = 1; 335 arch_init = 1;
328 load_archetypes (); 336 load_archetypes ();
329 arch_init = 0; 337 arch_init = 0;
330 empty_archetype = find_archetype ("empty_archetype"); 338 empty_archetype = find_archetype ("empty_archetype");
339
331/* init_blocksview();*/ 340/* init_blocksview();*/
332} 341}
333 342
334/* 343/*
335 * Stores debug-information about how efficient the hashtable 344 * Stores debug-information about how efficient the hashtable
336 * used for archetypes has been in the static errmsg array. 345 * used for archetypes has been in the static errmsg array.
337 */ 346 */
338 347
339void 348void
340arch_info (object * op) 349arch_info (object *op)
341{ 350{
342 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp); 351 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp);
343 new_draw_info (NDI_BLACK, 0, op, errmsg); 352 new_draw_info (NDI_BLACK, 0, op, errmsg);
344} 353}
345 354
358 */ 367 */
359 368
360void 369void
361init_archetable (void) 370init_archetable (void)
362{ 371{
363 archetype *at; 372 archetype *
373 at;
364 374
365 LOG (llevDebug, " Setting up archetable...\n"); 375 LOG (llevDebug, " Setting up archetable...\n");
366 376
367 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 377 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
368 add_arch (at); 378 add_arch (at);
373/* 383/*
374 * Dumps an archetype to debug-level output. 384 * Dumps an archetype to debug-level output.
375 */ 385 */
376 386
377void 387void
378dump_arch (archetype * at) 388dump_arch (archetype *at)
379{ 389{
380 dump_object (&at->clone); 390 dump_object (&at->clone);
381} 391}
382 392
383/* 393/*
389void 399void
390dump_all_archetypes (void) 400dump_all_archetypes (void)
391{ 401{
392 archetype * 402 archetype *
393 at; 403 at;
404
394 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 405 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
395 { 406 {
396 dump_arch (at); 407 dump_arch (at);
397 fprintf (logfile, "%s\n", errmsg); 408 fprintf (logfile, "%s\n", errmsg);
398 } 409 }
412 if (at->more) 423 if (at->more)
413 next = at->more; 424 next = at->more;
414 else 425 else
415 next = at->next; 426 next = at->next;
416 427
417 delete 428 delete at;
418 at;
419 i++; 429 i++;
420 } 430 }
421 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 431 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
422} 432}
423 433
424/* 434archetype::archetype ()
425 * Allocates, initialises and returns the pointer to an archetype structure.
426 */
427// TODO: should become constructor
428archetype *
429get_archetype_struct (void)
430{ 435{
431 archetype *
432 arch;
433
434 arch = new archetype;
435
436 clear_object (&arch->clone); /* to initial state other also */ 436 clear_object (&clone); /* to initial state other also */
437 CLEAR_FLAG (&arch->clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ 437 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */
438 SET_FLAG (&arch->clone, FLAG_REMOVED); /* doesn't copy these flags... */ 438 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
439}
439 440
440 return arch; 441archetype::~archetype ()
442{
441} 443}
442 444
443/* 445/*
444 * Reads/parses the archetype-file, and copies into a linked list 446 * Reads/parses the archetype-file, and copies into a linked list
445 * of archetype-structures. 447 * of archetype-structures.
446 */ 448 */
447void 449void
448first_arch_pass (object_thawer & fp) 450first_arch_pass (object_thawer & fp)
449{ 451{
450 object *
451 op;
452 archetype * 452 archetype *
453 at, * 453 at, *
454 head = NULL, *last_more = NULL; 454 head = NULL, *last_more = NULL;
455 int
456 i;
457 455
458 op = get_object ();
459 op->arch = first_archetype = at = get_archetype_struct (); 456 at->clone.arch = first_archetype = at = new archetype;
460 457
461 while ((i = load_object (fp, op, 0))) 458 while (int i = load_object (fp, &at->clone, 0))
462 { 459 {
463 copy_object (op, &at->clone);
464 at->clone.speed_left = (float) (-0.1); 460 at->clone.speed_left = (float) (-0.1);
465 /* copy the body_info to the body_used - this is only really 461 /* copy the body_info to the body_used - this is only really
466 * need for monsters, but doesn't hurt to do it for everything. 462 * need for monsters, but doesn't hurt to do it for everything.
467 * by doing so, when a monster is created, it has good starting 463 * by doing so, when a monster is created, it has good starting
468 * values for the body_used info, so when items are created 464 * values for the body_used info, so when items are created
469 * for it, they can be properly equipped. 465 * for it, they can be properly equipped.
470 */ 466 */
471 memcpy (&at->clone.body_used, &op->body_info, sizeof (op->body_info)); 467 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info));
472 468
473 switch (i) 469 switch (i)
474 { 470 {
475 case LL_NORMAL: /* A new archetype, just link it with the previous */ 471 case LL_NORMAL: /* A new archetype, just link it with the previous */
476 if (last_more != NULL) 472 if (last_more != NULL)
477 last_more->next = at; 473 last_more->next = at;
478 if (head != NULL) 474 if (head != NULL)
479 head->next = at; 475 head->next = at;
480 head = last_more = at; 476 head = last_more = at;
481#if 0 477#if 0
482 if (!op->type) 478 if (!op->type)
483 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name); 479 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name);
484#endif 480#endif
485 at->tail_x = 0; 481 at->tail_x = 0;
486 at->tail_y = 0; 482 at->tail_y = 0;
487 break; 483 break;
488 484
489 case LL_MORE: /* Another part of the previous archetype, link it correctly */ 485 case LL_MORE: /* Another part of the previous archetype, link it correctly */
490 486
491 at->head = head; 487 at->head = head;
492 at->clone.head = &head->clone; 488 at->clone.head = &head->clone;
493 if (last_more != NULL) 489 if (last_more != NULL)
494 { 490 {
495 last_more->more = at; 491 last_more->more = at;
496 last_more->clone.more = &at->clone; 492 last_more->clone.more = &at->clone;
497 } 493 }
498 last_more = at; 494 last_more = at;
499 495
500 /* If this multipart image is still composed of individual small 496 /* If this multipart image is still composed of individual small
501 * images, don't set the tail_.. values. We can't use them anyways, 497 * images, don't set the tail_.. values. We can't use them anyways,
502 * and setting these to zero makes the map sending to the client much 498 * and setting these to zero makes the map sending to the client much
503 * easier as just looking at the head, we know what to do. 499 * easier as just looking at the head, we know what to do.
504 */ 500 */
505 if (at->clone.face != head->clone.face) 501 if (at->clone.face != head->clone.face)
506 { 502 {
507 head->tail_x = 0; 503 head->tail_x = 0;
508 head->tail_y = 0; 504 head->tail_y = 0;
509 } 505 }
510 else 506 else
511 { 507 {
512 if (at->clone.x > head->tail_x) 508 if (at->clone.x > head->tail_x)
513 head->tail_x = at->clone.x; 509 head->tail_x = at->clone.x;
514 if (at->clone.y > head->tail_y) 510 if (at->clone.y > head->tail_y)
515 head->tail_y = at->clone.y; 511 head->tail_y = at->clone.y;
516 } 512 }
517 break; 513 break;
518 514
519 } 515 }
520 516
521 at = get_archetype_struct (); 517 at = new archetype;
522 clear_object (op); 518
523 op->arch = at; 519 at->clone.arch = at;
524 } 520 }
525 521
526 delete 522 delete
527 at; 523 at;
528 free_object (op);
529} 524}
530 525
531/* 526/*
532 * Reads the archetype file once more, and links all pointers between 527 * Reads the archetype file once more, and links all pointers between
533 * archetypes. 528 * archetypes.
575 { 570 {
576 if (at != NULL) 571 if (at != NULL)
577 { 572 {
578 treasurelist * 573 treasurelist *
579 tl = find_treasurelist (argument); 574 tl = find_treasurelist (argument);
575
580 if (tl == NULL) 576 if (tl == NULL)
581 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); 577 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument);
582 else 578 else
583 at->clone.randomitems = tl; 579 at->clone.randomitems = tl;
584 } 580 }
590void 586void
591check_generators (void) 587check_generators (void)
592{ 588{
593 archetype * 589 archetype *
594 at; 590 at;
591
595 for (at = first_archetype; at != NULL; at = at->next) 592 for (at = first_archetype; at != NULL; at = at->next)
596 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) 593 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL)
597 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); 594 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name);
598} 595}
599#endif 596#endif
608void 605void
609load_archetypes (void) 606load_archetypes (void)
610{ 607{
611 char 608 char
612 filename[MAX_BUF]; 609 filename[MAX_BUF];
613 int 610
614 comp;
615#if TIME_ARCH_LOAD 611#if TIME_ARCH_LOAD
616 struct timeval 612 struct timeval
617 tv1, 613 tv1,
618 tv2; 614 tv2;
619#endif 615#endif
620 616
621 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 617 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
622 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 618 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
623 619
624 { 620 {
625 object_thawer
626 thawer (filename); 621 object_thawer thawer (filename);
627 622
628 clear_archetable (); 623 clear_archetable ();
629 LOG (llevDebug, " arch-pass 1...\n"); 624 LOG (llevDebug, " arch-pass 1...\n");
630 first_arch_pass (thawer); 625 first_arch_pass (thawer);
631 LOG (llevDebug, " done\n"); 626 LOG (llevDebug, " done\n");
633 628
634 init_archetable (); 629 init_archetable ();
635 warn_archetypes = 1; 630 warn_archetypes = 1;
636 631
637 { 632 {
638 object_thawer
639 thawer (filename); 633 object_thawer thawer (filename);
640 634
641 LOG (llevDebug, " loading treasure...\n"); 635 LOG (llevDebug, " loading treasure...\n");
642 load_treasures (); 636 load_treasures ();
643 LOG (llevDebug, " done\n arch-pass 2...\n"); 637 LOG (llevDebug, " done\n arch-pass 2...\n");
644 second_arch_pass (thawer); 638 second_arch_pass (thawer);
654 * Creates and returns a new object which is a copy of the given archetype. 648 * Creates and returns a new object which is a copy of the given archetype.
655 * This function returns NULL on failure. 649 * This function returns NULL on failure.
656 */ 650 */
657 651
658object * 652object *
659arch_to_object (archetype * at) 653arch_to_object (archetype *at)
660{ 654{
661 object * 655 object *
662 op; 656 op;
657
663 if (at == NULL) 658 if (at == NULL)
664 { 659 {
665 if (warn_archetypes) 660 if (warn_archetypes)
666 LOG (llevError, "Couldn't find archetype.\n"); 661 LOG (llevError, "Couldn't find archetype.\n");
662
667 return NULL; 663 return NULL;
668 } 664 }
665
669 op = get_object (); 666 op = get_object ();
670 copy_object (&at->clone, op); 667 copy_object (&at->clone, op);
668 op->arch = at;
671 op->instantiate (); 669 op->instantiate ();
672 op->arch = at;
673 return op; 670 return op;
674} 671}
675 672
676/* 673/*
677 * Creates an object. This function is called by get_archetype() 674 * Creates an object. This function is called by get_archetype()
685{ 682{
686 object * 683 object *
687 op; 684 op;
688 char 685 char
689 buf[MAX_BUF]; 686 buf[MAX_BUF];
687
690 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 688 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
691 op = get_object (); 689 op = get_object ();
692 op->name = op->name_pl = buf; 690 op->name = op->name_pl = buf;
693 SET_FLAG (op, FLAG_NO_PICK); 691 SET_FLAG (op, FLAG_NO_PICK);
694 return op; 692 return op;
702object * 700object *
703get_archetype (const char *name) 701get_archetype (const char *name)
704{ 702{
705 archetype * 703 archetype *
706 at; 704 at;
705
707 at = find_archetype (name); 706 at = find_archetype (name);
708 if (at == NULL) 707 if (at == NULL)
709 return create_singularity (name); 708 return create_singularity (name);
709
710 return arch_to_object (at); 710 return arch_to_object (at);
711} 711}
712 712
713/* 713/*
714 * Hash-function used by the arch-hashtable. 714 * Hash-function used by the arch-hashtable.
755 name = shstr::find (name); 755 name = shstr::find (name);
756 756
757 if (!name) 757 if (!name)
758 return 0; 758 return 0;
759 759
760 HT::const_iterator i = ht.find ((size_t)name); 760 HT::const_iterator i = ht.find ((size_t) name);
761 761
762 if (i == ht.end ()) 762 if (i == ht.end ())
763 return 0; 763 return 0;
764 else 764 else
765 return i->second; 765 return i->second;
766#endif 766#endif
767 767
768 archetype *at; 768 archetype *
769 at;
769 unsigned long index; 770 unsigned long
771 index;
770 772
771 if (name == NULL) 773 if (name == NULL)
772 return (archetype *) NULL; 774 return (archetype *) NULL;
773 775
774 index = hasharch (name, ARCHTABLE); 776 index = hasharch (name, ARCHTABLE);
796 798
797static void 799static void
798add_arch (archetype *at) 800add_arch (archetype *at)
799{ 801{
800#if USE_UNORDERED_MAP 802#if USE_UNORDERED_MAP
801 ht.insert (std::make_pair ((size_t)(const char *)at->name, at)); 803 ht.insert (std::make_pair ((size_t) (const char *) at->name, at));
802#endif 804#endif
803 805
806 int
804 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 807 index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index;
805 808
806 for (;;) 809 for (;;)
807 { 810 {
808 if (arch_table[index] == NULL) 811 if (arch_table[index] == NULL)
809 { 812 {
864/* 867/*
865 * member: make instance from class 868 * member: make instance from class
866 */ 869 */
867 870
868object * 871object *
869object_create_arch (archetype * at) 872object_create_arch (archetype *at)
870{ 873{
871 object * 874 object *
872 op, * 875 op, *
873 prev = NULL, *head = NULL; 876 prev = NULL, *head = NULL;
874 877

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines