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.14 by root, Fri Sep 8 17:14:07 2006 UTC vs.
Revision 1.19 by root, Thu Sep 14 18:13:01 2006 UTC

1/*
2 * static char *rcsid_arch_c =
3 * "$Id: arch.C,v 1.14 2006/09/08 17:14:07 root Exp $";
4 */
5
6/* 1/*
7 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
8 3
9 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10 Copyright (C) 1992 Frank Tore Johansen 5 Copyright (C) 1992 Frank Tore Johansen
36#include <loader.h> 31#include <loader.h>
37 32
38/* IF set, does a little timing on the archetype load. */ 33/* IF set, does a little timing on the archetype load. */
39#define TIME_ARCH_LOAD 0 34#define TIME_ARCH_LOAD 0
40 35
41static void add_arch (archetype * at); 36static void add_arch (archetype *at);
42 37
43static archetype *arch_table[ARCHTABLE]; 38static archetype *arch_table[ARCHTABLE];
44int arch_cmp = 0; /* How many strcmp's */ 39int arch_cmp = 0; /* How many strcmp's */
45int arch_search = 0; /* How many searches */ 40int arch_search = 0; /* How many searches */
46int arch_init; /* True if doing arch initialization */ 41int arch_init; /* True if doing arch initialization */
53 * MSW 2003-04-29 48 * MSW 2003-04-29
54 */ 49 */
55 50
56#if USE_UNORDERED_MAP 51#if USE_UNORDERED_MAP
57// the hashtable 52// the hashtable
58typedef std::tr1::unordered_map<size_t, archetype *> HT; 53typedef std::tr1::unordered_map
54 <
55 std::size_t,
56 archetype *,
57 std::hash<size_t>,
58 std::equal_to<size_t>,
59 slice_allocator< std::pair<const std::size_t, archetype *> >
60 true,
61 > HT;
59 62
60static HT ht; 63static HT ht;
61#endif 64#endif
62 65
63/** 66/**
72 * - the archetype found or null if nothing was found. 75 * - the archetype found or null if nothing was found.
73 */ 76 */
74archetype * 77archetype *
75find_archetype_by_object_name (const char *name) 78find_archetype_by_object_name (const char *name)
76{ 79{
77 archetype *at; 80 archetype *
81 at;
78 82
79 if (name == NULL) 83 if (name == NULL)
80 return (archetype *) NULL; 84 return (archetype *) NULL;
81 85
82 for (at = first_archetype; at != NULL; at = at->next) 86 for (at = first_archetype; at != NULL; at = at->next)
93 * except that it considers only items of the given type. 97 * except that it considers only items of the given type.
94 */ 98 */
95archetype * 99archetype *
96find_archetype_by_object_type_name (int type, const char *name) 100find_archetype_by_object_type_name (int type, const char *name)
97{ 101{
98 archetype *at; 102 archetype *
103 at;
99 104
100 if (name == NULL) 105 if (name == NULL)
101 return NULL; 106 return NULL;
102 107
103 for (at = first_archetype; at != NULL; at = at->next) 108 for (at = first_archetype; at != NULL; at = at->next)
165 * but it otherwise had a big memory leak. 170 * but it otherwise had a big memory leak.
166 */ 171 */
167object * 172object *
168get_archetype_by_object_name (const char *name) 173get_archetype_by_object_name (const char *name)
169{ 174{
170 archetype * 175 archetype *at;
171 at;
172 char
173 tmpname[MAX_BUF]; 176 char tmpname[MAX_BUF];
174 int 177 int i;
175 i;
176 178
177 strncpy (tmpname, name, MAX_BUF - 1); 179 assign (tmpname, name);
178 tmpname[MAX_BUF - 1] = 0; 180
179 for (i = strlen (tmpname); i > 0; i--) 181 for (i = strlen (tmpname); i > 0; i--)
180 { 182 {
181 tmpname[i] = 0; 183 tmpname[i] = 0;
182 at = find_archetype_by_object_name (tmpname); 184 at = find_archetype_by_object_name (tmpname);
185
183 if (at != NULL) 186 if (at != NULL)
184 { 187 {
185 return arch_to_object (at); 188 return arch_to_object (at);
186 } 189 }
187 } 190 }
191
188 return create_singularity (name); 192 return create_singularity (name);
189} 193}
190 194
191 /* This is a subset of the parse_id command. Basically, name can be 195 /* This is a subset of the parse_id command. Basically, name can be
192 * a string seperated lists of things to match, with certain keywords. 196 * a string seperated lists of things to match, with certain keywords.
204 * If count is 1, make a quick check on the name. 208 * 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. 209 * IF count is >1, we need to make plural name. Return if match.
206 * Last, make a check on the full name. 210 * Last, make a check on the full name.
207 */ 211 */
208int 212int
209item_matched_string (object * pl, object * op, const char *name) 213item_matched_string (object *pl, object *op, const char *name)
210{ 214{
211 char * 215 char *
212 cp, 216 cp,
213 local_name[MAX_BUF]; 217 local_name[MAX_BUF];
214 int 218 int
215 count, 219 count,
216 retval = 0; 220 retval = 0;
221
217 strcpy (local_name, name); /* strtok is destructive to name */ 222 strcpy (local_name, name); /* strtok is destructive to name */
218 223
219 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) 224 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
220 { 225 {
221 while (cp[0] == ' ') 226 while (cp[0] == ' ')
326 return; 331 return;
327 arch_init = 1; 332 arch_init = 1;
328 load_archetypes (); 333 load_archetypes ();
329 arch_init = 0; 334 arch_init = 0;
330 empty_archetype = find_archetype ("empty_archetype"); 335 empty_archetype = find_archetype ("empty_archetype");
336
331/* init_blocksview();*/ 337/* init_blocksview();*/
332} 338}
333 339
334/* 340/*
335 * Stores debug-information about how efficient the hashtable 341 * Stores debug-information about how efficient the hashtable
336 * used for archetypes has been in the static errmsg array. 342 * used for archetypes has been in the static errmsg array.
337 */ 343 */
338 344
339void 345void
340arch_info (object * op) 346arch_info (object *op)
341{ 347{
342 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp); 348 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp);
343 new_draw_info (NDI_BLACK, 0, op, errmsg); 349 new_draw_info (NDI_BLACK, 0, op, errmsg);
344} 350}
345 351
358 */ 364 */
359 365
360void 366void
361init_archetable (void) 367init_archetable (void)
362{ 368{
363 archetype *at; 369 archetype *
370 at;
364 371
365 LOG (llevDebug, " Setting up archetable...\n"); 372 LOG (llevDebug, " Setting up archetable...\n");
366 373
367 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 374 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
368 add_arch (at); 375 add_arch (at);
373/* 380/*
374 * Dumps an archetype to debug-level output. 381 * Dumps an archetype to debug-level output.
375 */ 382 */
376 383
377void 384void
378dump_arch (archetype * at) 385dump_arch (archetype *at)
379{ 386{
380 dump_object (&at->clone); 387 dump_object (&at->clone);
381} 388}
382 389
383/* 390/*
389void 396void
390dump_all_archetypes (void) 397dump_all_archetypes (void)
391{ 398{
392 archetype * 399 archetype *
393 at; 400 at;
401
394 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 402 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
395 { 403 {
396 dump_arch (at); 404 dump_arch (at);
397 fprintf (logfile, "%s\n", errmsg); 405 fprintf (logfile, "%s\n", errmsg);
398 } 406 }
414 else 422 else
415 next = at->next; 423 next = at->next;
416 424
417 delete 425 delete
418 at; 426 at;
427
419 i++; 428 i++;
420 } 429 }
421 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 430 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
422} 431}
423 432
424archetype::archetype () 433archetype::archetype ()
425{ 434{
426 clear_object (&clone); /* to initial state other also */ 435 clear_object (&clone); /* to initial state other also */
427 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ 436 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */
428 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ 437 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
429} 438}
430 439
431archetype::~archetype () 440archetype::~archetype ()
432{ 441{
433} 442}
437 * of archetype-structures. 446 * of archetype-structures.
438 */ 447 */
439void 448void
440first_arch_pass (object_thawer & fp) 449first_arch_pass (object_thawer & fp)
441{ 450{
451 archetype *
452 at, *
442 archetype *at, *head = NULL, *last_more = NULL; 453 head = NULL, *last_more = NULL;
443 454
444 at->clone.arch = first_archetype = at = new archetype; 455 at->clone.arch = first_archetype = at = new archetype;
445 456
446 while (int i = load_object (fp, &at->clone, 0)) 457 while (int i = load_object (fp, &at->clone, 0))
447 { 458 {
454 */ 465 */
455 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info)); 466 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info));
456 467
457 switch (i) 468 switch (i)
458 { 469 {
459 case LL_NORMAL: /* A new archetype, just link it with the previous */ 470 case LL_NORMAL: /* A new archetype, just link it with the previous */
460 if (last_more != NULL) 471 if (last_more != NULL)
461 last_more->next = at; 472 last_more->next = at;
462 if (head != NULL) 473 if (head != NULL)
463 head->next = at; 474 head->next = at;
464 head = last_more = at; 475 head = last_more = at;
465#if 0 476#if 0
466 if (!op->type) 477 if (!op->type)
467 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name); 478 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name);
468#endif 479#endif
469 at->tail_x = 0; 480 at->tail_x = 0;
470 at->tail_y = 0; 481 at->tail_y = 0;
471 break; 482 break;
472 483
473 case LL_MORE: /* Another part of the previous archetype, link it correctly */ 484 case LL_MORE: /* Another part of the previous archetype, link it correctly */
474 485
475 at->head = head; 486 at->head = head;
476 at->clone.head = &head->clone; 487 at->clone.head = &head->clone;
477 if (last_more != NULL) 488 if (last_more != NULL)
478 { 489 {
479 last_more->more = at; 490 last_more->more = at;
480 last_more->clone.more = &at->clone; 491 last_more->clone.more = &at->clone;
481 } 492 }
482 last_more = at; 493 last_more = at;
483 494
484 /* If this multipart image is still composed of individual small 495 /* If this multipart image is still composed of individual small
485 * images, don't set the tail_.. values. We can't use them anyways, 496 * images, don't set the tail_.. values. We can't use them anyways,
486 * and setting these to zero makes the map sending to the client much 497 * and setting these to zero makes the map sending to the client much
487 * easier as just looking at the head, we know what to do. 498 * easier as just looking at the head, we know what to do.
488 */ 499 */
489 if (at->clone.face != head->clone.face) 500 if (at->clone.face != head->clone.face)
490 { 501 {
491 head->tail_x = 0; 502 head->tail_x = 0;
492 head->tail_y = 0; 503 head->tail_y = 0;
493 } 504 }
494 else 505 else
495 { 506 {
496 if (at->clone.x > head->tail_x) 507 if (at->clone.x > head->tail_x)
497 head->tail_x = at->clone.x; 508 head->tail_x = at->clone.x;
498 if (at->clone.y > head->tail_y) 509 if (at->clone.y > head->tail_y)
499 head->tail_y = at->clone.y; 510 head->tail_y = at->clone.y;
500 } 511 }
501 break; 512 break;
502 513
503 } 514 }
504 515
505 at = new archetype; 516 at = new archetype;
517
506 at->clone.arch = at; 518 at->clone.arch = at;
507 } 519 }
508 520
509 delete at; 521 delete at;
510} 522}
515 */ 527 */
516 528
517void 529void
518second_arch_pass (object_thawer & thawer) 530second_arch_pass (object_thawer & thawer)
519{ 531{
532 char
533 buf[MAX_BUF], *
520 char buf[MAX_BUF], *variable = buf, *argument, *cp; 534 variable = buf, *argument, *cp;
535 archetype *
521 archetype *at = NULL, *other; 536 at = NULL, *other;
522 537
523 while (fgets (buf, MAX_BUF, thawer) != NULL) 538 while (fgets (buf, MAX_BUF, thawer) != NULL)
524 { 539 {
525 if (*buf == '#') 540 if (*buf == '#')
526 continue; 541 continue;
553 { 568 {
554 if (at != NULL) 569 if (at != NULL)
555 { 570 {
556 treasurelist * 571 treasurelist *
557 tl = find_treasurelist (argument); 572 tl = find_treasurelist (argument);
573
558 if (tl == NULL) 574 if (tl == NULL)
559 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); 575 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument);
560 else 576 else
561 at->clone.randomitems = tl; 577 at->clone.randomitems = tl;
562 } 578 }
568void 584void
569check_generators (void) 585check_generators (void)
570{ 586{
571 archetype * 587 archetype *
572 at; 588 at;
589
573 for (at = first_archetype; at != NULL; at = at->next) 590 for (at = first_archetype; at != NULL; at = at->next)
574 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) 591 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL)
575 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); 592 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name);
576} 593}
577#endif 594#endif
586void 603void
587load_archetypes (void) 604load_archetypes (void)
588{ 605{
589 char 606 char
590 filename[MAX_BUF]; 607 filename[MAX_BUF];
608
591#if TIME_ARCH_LOAD 609#if TIME_ARCH_LOAD
592 struct timeval 610 struct timeval
593 tv1, 611 tv1,
594 tv2; 612 tv2;
595#endif 613#endif
630 * 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.
631 * This function returns NULL on failure. 649 * This function returns NULL on failure.
632 */ 650 */
633 651
634object * 652object *
635arch_to_object (archetype * at) 653arch_to_object (archetype *at)
636{ 654{
637 object * 655 object *
638 op; 656 op;
657
639 if (at == NULL) 658 if (at == NULL)
640 { 659 {
641 if (warn_archetypes) 660 if (warn_archetypes)
642 LOG (llevError, "Couldn't find archetype.\n"); 661 LOG (llevError, "Couldn't find archetype.\n");
662
643 return NULL; 663 return NULL;
644 } 664 }
665
645 op = get_object (); 666 op = get_object ();
646 copy_object (&at->clone, op); 667 copy_object (&at->clone, op);
668 op->arch = at;
647 op->instantiate (); 669 op->instantiate ();
648 op->arch = at;
649 return op; 670 return op;
650} 671}
651 672
652/* 673/*
653 * Creates an object. This function is called by get_archetype() 674 * Creates an object. This function is called by get_archetype()
657 */ 678 */
658 679
659object * 680object *
660create_singularity (const char *name) 681create_singularity (const char *name)
661{ 682{
662 object *op; 683 object *
684 op;
685 char
663 char buf[MAX_BUF]; 686 buf[MAX_BUF];
687
664 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 688 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
665 op = get_object (); 689 op = get_object ();
666 op->name = op->name_pl = buf; 690 op->name = op->name_pl = buf;
667 SET_FLAG (op, FLAG_NO_PICK); 691 SET_FLAG (op, FLAG_NO_PICK);
668 return op; 692 return op;
674 */ 698 */
675 699
676object * 700object *
677get_archetype (const char *name) 701get_archetype (const char *name)
678{ 702{
679 archetype *at; 703 archetype *
704 at;
705
680 at = find_archetype (name); 706 at = find_archetype (name);
681 if (at == NULL) 707 if (at == NULL)
682 return create_singularity (name); 708 return create_singularity (name);
709
683 return arch_to_object (at); 710 return arch_to_object (at);
684} 711}
685 712
686/* 713/*
687 * Hash-function used by the arch-hashtable. 714 * Hash-function used by the arch-hashtable.
688 */ 715 */
689 716
690unsigned long 717unsigned long
691hasharch (const char *str, int tablesize) 718hasharch (const char *str, int tablesize)
692{ 719{
693 unsigned long hash = 0; 720 unsigned long
721 hash = 0;
694 unsigned int i = 0; 722 unsigned int
723 i = 0;
695 const char *p; 724 const char *
725 p;
696 726
697 /* use the one-at-a-time hash function, which supposedly is 727 /* use the one-at-a-time hash function, which supposedly is
698 * better than the djb2-like one used by perl5.005, but 728 * better than the djb2-like one used by perl5.005, but
699 * certainly is better then the bug used here before. 729 * certainly is better then the bug used here before.
700 * see http://burtleburtle.net/bob/hash/doobs.html 730 * see http://burtleburtle.net/bob/hash/doobs.html
725 name = shstr::find (name); 755 name = shstr::find (name);
726 756
727 if (!name) 757 if (!name)
728 return 0; 758 return 0;
729 759
730 HT::const_iterator i = ht.find ((size_t)name); 760 AUTODECL (i, ht.find ((size_t) name));
731 761
732 if (i == ht.end ()) 762 if (i == ht.end ())
733 return 0; 763 return 0;
734 else 764 else
735 return i->second; 765 return i->second;
736#endif 766#endif
737 767
738 archetype *at; 768 archetype *
769 at;
739 unsigned long index; 770 unsigned long
771 index;
740 772
741 if (name == NULL) 773 if (name == NULL)
742 return (archetype *) NULL; 774 return (archetype *) NULL;
743 775
744 index = hasharch (name, ARCHTABLE); 776 index = hasharch (name, ARCHTABLE);
766 798
767static void 799static void
768add_arch (archetype *at) 800add_arch (archetype *at)
769{ 801{
770#if USE_UNORDERED_MAP 802#if USE_UNORDERED_MAP
771 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));
772#endif 804#endif
773 805
806 int
774 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 807 index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index;
775 808
776 for (;;) 809 for (;;)
777 { 810 {
778 if (arch_table[index] == NULL) 811 if (arch_table[index] == NULL)
779 { 812 {
834/* 867/*
835 * member: make instance from class 868 * member: make instance from class
836 */ 869 */
837 870
838object * 871object *
839object_create_arch (archetype * at) 872object_create_arch (archetype *at)
840{ 873{
841 object * 874 object *
842 op, * 875 op, *
843 prev = NULL, *head = NULL; 876 prev = NULL, *head = NULL;
844 877

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines