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

Comparing cf.schmorp.de/server/common/arch.C (file contents):
Revision 1.14 by root, Fri Sep 8 17:14:07 2006 UTC vs.
Revision 1.16 by root, Sun Sep 10 16:00:23 2006 UTC

1
1/* 2/*
2 * static char *rcsid_arch_c = 3 * static char *rcsid_arch_c =
3 * "$Id: arch.C,v 1.14 2006/09/08 17:14:07 root Exp $"; 4 * "$Id: arch.C,v 1.16 2006/09/10 16:00:23 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
65 HT
66 ht;
61#endif 67#endif
62 68
63/** 69/**
64 * GROS - This function retrieves an archetype given the name that appears 70 * GROS - This function retrieves an archetype given the name that appears
65 * during the game (for example, "writing pen" instead of "stylus"). 71 * during the game (for example, "writing pen" instead of "stylus").
72 * - the archetype found or null if nothing was found. 78 * - the archetype found or null if nothing was found.
73 */ 79 */
74archetype * 80archetype *
75find_archetype_by_object_name (const char *name) 81find_archetype_by_object_name (const char *name)
76{ 82{
77 archetype *at; 83 archetype *
84 at;
78 85
79 if (name == NULL) 86 if (name == NULL)
80 return (archetype *) NULL; 87 return (archetype *) NULL;
81 88
82 for (at = first_archetype; at != NULL; at = at->next) 89 for (at = first_archetype; at != NULL; at = at->next)
93 * except that it considers only items of the given type. 100 * except that it considers only items of the given type.
94 */ 101 */
95archetype * 102archetype *
96find_archetype_by_object_type_name (int type, const char *name) 103find_archetype_by_object_type_name (int type, const char *name)
97{ 104{
98 archetype *at; 105 archetype *
106 at;
99 107
100 if (name == NULL) 108 if (name == NULL)
101 return NULL; 109 return NULL;
102 110
103 for (at = first_archetype; at != NULL; at = at->next) 111 for (at = first_archetype; at != NULL; at = at->next)
204 * If count is 1, make a quick check on the name. 212 * 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. 213 * IF count is >1, we need to make plural name. Return if match.
206 * Last, make a check on the full name. 214 * Last, make a check on the full name.
207 */ 215 */
208int 216int
209item_matched_string (object * pl, object * op, const char *name) 217item_matched_string (object *pl, object *op, const char *name)
210{ 218{
211 char * 219 char *
212 cp, 220 cp,
213 local_name[MAX_BUF]; 221 local_name[MAX_BUF];
214 int 222 int
215 count, 223 count,
216 retval = 0; 224 retval = 0;
225
217 strcpy (local_name, name); /* strtok is destructive to name */ 226 strcpy (local_name, name); /* strtok is destructive to name */
218 227
219 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) 228 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
220 { 229 {
221 while (cp[0] == ' ') 230 while (cp[0] == ' ')
326 return; 335 return;
327 arch_init = 1; 336 arch_init = 1;
328 load_archetypes (); 337 load_archetypes ();
329 arch_init = 0; 338 arch_init = 0;
330 empty_archetype = find_archetype ("empty_archetype"); 339 empty_archetype = find_archetype ("empty_archetype");
340
331/* init_blocksview();*/ 341/* init_blocksview();*/
332} 342}
333 343
334/* 344/*
335 * Stores debug-information about how efficient the hashtable 345 * Stores debug-information about how efficient the hashtable
336 * used for archetypes has been in the static errmsg array. 346 * used for archetypes has been in the static errmsg array.
337 */ 347 */
338 348
339void 349void
340arch_info (object * op) 350arch_info (object *op)
341{ 351{
342 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp); 352 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp);
343 new_draw_info (NDI_BLACK, 0, op, errmsg); 353 new_draw_info (NDI_BLACK, 0, op, errmsg);
344} 354}
345 355
358 */ 368 */
359 369
360void 370void
361init_archetable (void) 371init_archetable (void)
362{ 372{
363 archetype *at; 373 archetype *
374 at;
364 375
365 LOG (llevDebug, " Setting up archetable...\n"); 376 LOG (llevDebug, " Setting up archetable...\n");
366 377
367 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 378 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
368 add_arch (at); 379 add_arch (at);
373/* 384/*
374 * Dumps an archetype to debug-level output. 385 * Dumps an archetype to debug-level output.
375 */ 386 */
376 387
377void 388void
378dump_arch (archetype * at) 389dump_arch (archetype *at)
379{ 390{
380 dump_object (&at->clone); 391 dump_object (&at->clone);
381} 392}
382 393
383/* 394/*
389void 400void
390dump_all_archetypes (void) 401dump_all_archetypes (void)
391{ 402{
392 archetype * 403 archetype *
393 at; 404 at;
405
394 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 406 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
395 { 407 {
396 dump_arch (at); 408 dump_arch (at);
397 fprintf (logfile, "%s\n", errmsg); 409 fprintf (logfile, "%s\n", errmsg);
398 } 410 }
414 else 426 else
415 next = at->next; 427 next = at->next;
416 428
417 delete 429 delete
418 at; 430 at;
431
419 i++; 432 i++;
420 } 433 }
421 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 434 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
422} 435}
423 436
424archetype::archetype () 437archetype::archetype ()
425{ 438{
426 clear_object (&clone); /* to initial state other also */ 439 clear_object (&clone); /* to initial state other also */
427 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ 440 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */
428 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ 441 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
429} 442}
430 443
431archetype::~archetype () 444archetype::~archetype ()
432{ 445{
433} 446}
437 * of archetype-structures. 450 * of archetype-structures.
438 */ 451 */
439void 452void
440first_arch_pass (object_thawer & fp) 453first_arch_pass (object_thawer & fp)
441{ 454{
455 archetype *
456 at, *
442 archetype *at, *head = NULL, *last_more = NULL; 457 head = NULL, *last_more = NULL;
443 458
444 at->clone.arch = first_archetype = at = new archetype; 459 at->clone.arch = first_archetype = at = new archetype;
445 460
446 while (int i = load_object (fp, &at->clone, 0)) 461 while (int i = load_object (fp, &at->clone, 0))
447 { 462 {
454 */ 469 */
455 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info)); 470 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info));
456 471
457 switch (i) 472 switch (i)
458 { 473 {
459 case LL_NORMAL: /* A new archetype, just link it with the previous */ 474 case LL_NORMAL: /* A new archetype, just link it with the previous */
460 if (last_more != NULL) 475 if (last_more != NULL)
461 last_more->next = at; 476 last_more->next = at;
462 if (head != NULL) 477 if (head != NULL)
463 head->next = at; 478 head->next = at;
464 head = last_more = at; 479 head = last_more = at;
465#if 0 480#if 0
466 if (!op->type) 481 if (!op->type)
467 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name); 482 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name);
468#endif 483#endif
469 at->tail_x = 0; 484 at->tail_x = 0;
470 at->tail_y = 0; 485 at->tail_y = 0;
471 break; 486 break;
472 487
473 case LL_MORE: /* Another part of the previous archetype, link it correctly */ 488 case LL_MORE: /* Another part of the previous archetype, link it correctly */
474 489
475 at->head = head; 490 at->head = head;
476 at->clone.head = &head->clone; 491 at->clone.head = &head->clone;
477 if (last_more != NULL) 492 if (last_more != NULL)
478 { 493 {
479 last_more->more = at; 494 last_more->more = at;
480 last_more->clone.more = &at->clone; 495 last_more->clone.more = &at->clone;
481 } 496 }
482 last_more = at; 497 last_more = at;
483 498
484 /* If this multipart image is still composed of individual small 499 /* If this multipart image is still composed of individual small
485 * images, don't set the tail_.. values. We can't use them anyways, 500 * 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 501 * 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. 502 * easier as just looking at the head, we know what to do.
488 */ 503 */
489 if (at->clone.face != head->clone.face) 504 if (at->clone.face != head->clone.face)
490 { 505 {
491 head->tail_x = 0; 506 head->tail_x = 0;
492 head->tail_y = 0; 507 head->tail_y = 0;
493 } 508 }
494 else 509 else
495 { 510 {
496 if (at->clone.x > head->tail_x) 511 if (at->clone.x > head->tail_x)
497 head->tail_x = at->clone.x; 512 head->tail_x = at->clone.x;
498 if (at->clone.y > head->tail_y) 513 if (at->clone.y > head->tail_y)
499 head->tail_y = at->clone.y; 514 head->tail_y = at->clone.y;
500 } 515 }
501 break; 516 break;
502 517
503 } 518 }
504 519
505 at = new archetype; 520 at = new archetype;
521
506 at->clone.arch = at; 522 at->clone.arch = at;
507 } 523 }
508 524
509 delete at; 525 delete at;
510} 526}
515 */ 531 */
516 532
517void 533void
518second_arch_pass (object_thawer & thawer) 534second_arch_pass (object_thawer & thawer)
519{ 535{
536 char
537 buf[MAX_BUF], *
520 char buf[MAX_BUF], *variable = buf, *argument, *cp; 538 variable = buf, *argument, *cp;
539 archetype *
521 archetype *at = NULL, *other; 540 at = NULL, *other;
522 541
523 while (fgets (buf, MAX_BUF, thawer) != NULL) 542 while (fgets (buf, MAX_BUF, thawer) != NULL)
524 { 543 {
525 if (*buf == '#') 544 if (*buf == '#')
526 continue; 545 continue;
553 { 572 {
554 if (at != NULL) 573 if (at != NULL)
555 { 574 {
556 treasurelist * 575 treasurelist *
557 tl = find_treasurelist (argument); 576 tl = find_treasurelist (argument);
577
558 if (tl == NULL) 578 if (tl == NULL)
559 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); 579 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument);
560 else 580 else
561 at->clone.randomitems = tl; 581 at->clone.randomitems = tl;
562 } 582 }
568void 588void
569check_generators (void) 589check_generators (void)
570{ 590{
571 archetype * 591 archetype *
572 at; 592 at;
593
573 for (at = first_archetype; at != NULL; at = at->next) 594 for (at = first_archetype; at != NULL; at = at->next)
574 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) 595 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); 596 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name);
576} 597}
577#endif 598#endif
586void 607void
587load_archetypes (void) 608load_archetypes (void)
588{ 609{
589 char 610 char
590 filename[MAX_BUF]; 611 filename[MAX_BUF];
612
591#if TIME_ARCH_LOAD 613#if TIME_ARCH_LOAD
592 struct timeval 614 struct timeval
593 tv1, 615 tv1,
594 tv2; 616 tv2;
595#endif 617#endif
630 * Creates and returns a new object which is a copy of the given archetype. 652 * Creates and returns a new object which is a copy of the given archetype.
631 * This function returns NULL on failure. 653 * This function returns NULL on failure.
632 */ 654 */
633 655
634object * 656object *
635arch_to_object (archetype * at) 657arch_to_object (archetype *at)
636{ 658{
637 object * 659 object *
638 op; 660 op;
661
639 if (at == NULL) 662 if (at == NULL)
640 { 663 {
641 if (warn_archetypes) 664 if (warn_archetypes)
642 LOG (llevError, "Couldn't find archetype.\n"); 665 LOG (llevError, "Couldn't find archetype.\n");
666
643 return NULL; 667 return NULL;
644 } 668 }
669
645 op = get_object (); 670 op = get_object ();
646 copy_object (&at->clone, op); 671 copy_object (&at->clone, op);
672 op->arch = at;
647 op->instantiate (); 673 op->instantiate ();
648 op->arch = at;
649 return op; 674 return op;
650} 675}
651 676
652/* 677/*
653 * Creates an object. This function is called by get_archetype() 678 * Creates an object. This function is called by get_archetype()
657 */ 682 */
658 683
659object * 684object *
660create_singularity (const char *name) 685create_singularity (const char *name)
661{ 686{
662 object *op; 687 object *
688 op;
689 char
663 char buf[MAX_BUF]; 690 buf[MAX_BUF];
691
664 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 692 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
665 op = get_object (); 693 op = get_object ();
666 op->name = op->name_pl = buf; 694 op->name = op->name_pl = buf;
667 SET_FLAG (op, FLAG_NO_PICK); 695 SET_FLAG (op, FLAG_NO_PICK);
668 return op; 696 return op;
674 */ 702 */
675 703
676object * 704object *
677get_archetype (const char *name) 705get_archetype (const char *name)
678{ 706{
679 archetype *at; 707 archetype *
708 at;
709
680 at = find_archetype (name); 710 at = find_archetype (name);
681 if (at == NULL) 711 if (at == NULL)
682 return create_singularity (name); 712 return create_singularity (name);
713
683 return arch_to_object (at); 714 return arch_to_object (at);
684} 715}
685 716
686/* 717/*
687 * Hash-function used by the arch-hashtable. 718 * Hash-function used by the arch-hashtable.
688 */ 719 */
689 720
690unsigned long 721unsigned long
691hasharch (const char *str, int tablesize) 722hasharch (const char *str, int tablesize)
692{ 723{
693 unsigned long hash = 0; 724 unsigned long
725 hash = 0;
694 unsigned int i = 0; 726 unsigned int
727 i = 0;
695 const char *p; 728 const char *
729 p;
696 730
697 /* use the one-at-a-time hash function, which supposedly is 731 /* use the one-at-a-time hash function, which supposedly is
698 * better than the djb2-like one used by perl5.005, but 732 * better than the djb2-like one used by perl5.005, but
699 * certainly is better then the bug used here before. 733 * certainly is better then the bug used here before.
700 * see http://burtleburtle.net/bob/hash/doobs.html 734 * see http://burtleburtle.net/bob/hash/doobs.html
725 name = shstr::find (name); 759 name = shstr::find (name);
726 760
727 if (!name) 761 if (!name)
728 return 0; 762 return 0;
729 763
730 HT::const_iterator i = ht.find ((size_t)name); 764 HT::const_iterator i = ht.find ((size_t) name);
731 765
732 if (i == ht.end ()) 766 if (i == ht.end ())
733 return 0; 767 return 0;
734 else 768 else
735 return i->second; 769 return i->second;
736#endif 770#endif
737 771
738 archetype *at; 772 archetype *
773 at;
739 unsigned long index; 774 unsigned long
775 index;
740 776
741 if (name == NULL) 777 if (name == NULL)
742 return (archetype *) NULL; 778 return (archetype *) NULL;
743 779
744 index = hasharch (name, ARCHTABLE); 780 index = hasharch (name, ARCHTABLE);
766 802
767static void 803static void
768add_arch (archetype *at) 804add_arch (archetype *at)
769{ 805{
770#if USE_UNORDERED_MAP 806#if USE_UNORDERED_MAP
771 ht.insert (std::make_pair ((size_t)(const char *)at->name, at)); 807 ht.insert (std::make_pair ((size_t) (const char *) at->name, at));
772#endif 808#endif
773 809
810 int
774 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 811 index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index;
775 812
776 for (;;) 813 for (;;)
777 { 814 {
778 if (arch_table[index] == NULL) 815 if (arch_table[index] == NULL)
779 { 816 {
834/* 871/*
835 * member: make instance from class 872 * member: make instance from class
836 */ 873 */
837 874
838object * 875object *
839object_create_arch (archetype * at) 876object_create_arch (archetype *at)
840{ 877{
841 object * 878 object *
842 op, * 879 op, *
843 prev = NULL, *head = NULL; 880 prev = NULL, *head = NULL;
844 881

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines