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.17 by root, Tue Sep 12 19:20:06 2006 UTC vs.
Revision 1.27 by root, Sat Dec 9 14:01:12 2006 UTC

16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 20
21 The authors can be reached via e-mail at crossfire-devel@real-time.com 21 The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 22*/
23 23
24#include <cassert> 24#include <cassert>
25 25
26#include <tr1/unordered_map>
27
28#include <global.h> 26#include <global.h>
29#include <arch.h>
30#include <funcpoint.h> 27#include <funcpoint.h>
31#include <loader.h> 28#include <loader.h>
32 29
33/* IF set, does a little timing on the archetype load. */ 30#define USE_UNORDERED_MAP 0
34#define TIME_ARCH_LOAD 0
35 31
36static void add_arch (archetype *at); 32#if USE_UNORDERED_MAP
33# include <tr1/functional>
34# include <tr1/unordered_map>
35#endif
37 36
38static archetype *arch_table[ARCHTABLE];
39int arch_cmp = 0; /* How many strcmp's */ 37int arch_cmp = 0; /* How many strcmp's */
40int arch_search = 0; /* How many searches */ 38int arch_search = 0; /* How many searches */
41int arch_init; /* True if doing arch initialization */ 39int arch_init; /* True if doing arch initialization */
42 40
43/* The naming of these functions is really poor - they are all 41/* The naming of these functions is really poor - they are all
48 * MSW 2003-04-29 46 * MSW 2003-04-29
49 */ 47 */
50 48
51#if USE_UNORDERED_MAP 49#if USE_UNORDERED_MAP
52// the hashtable 50// the hashtable
53typedef
54 std::tr1::unordered_map < 51typedef std::tr1::unordered_map
55size_t, archetype *> 52 <
53 std::size_t,
54 arch_ptr,
55 std::tr1::hash<size_t>,
56 std::equal_to<size_t>,
57 slice_allocator< std::pair<const std::size_t, archetype *> >
58 true,
56 HT; 59 > HT;
57 60
58static 61static HT ht;
59 HT 62#else
60 ht; 63static arch_ptr arch_table[ARCHTABLE];
61#endif 64#endif
62 65
63/** 66/**
64 * GROS - This function retrieves an archetype given the name that appears 67 * GROS - This function retrieves an archetype given the name that appears
65 * during the game (for example, "writing pen" instead of "stylus"). 68 * during the game (for example, "writing pen" instead of "stylus").
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 * 80 archetype *at;
78 at;
79 81
80 if (name == NULL) 82 if (name == NULL)
81 return (archetype *) NULL; 83 return (archetype *) NULL;
82 84
83 for (at = first_archetype; at != NULL; at = at->next) 85 for (at = first_archetype; at != NULL; at = at->next)
94 * except that it considers only items of the given type. 96 * except that it considers only items of the given type.
95 */ 97 */
96archetype * 98archetype *
97find_archetype_by_object_type_name (int type, const char *name) 99find_archetype_by_object_type_name (int type, const char *name)
98{ 100{
99 archetype * 101 archetype *at;
100 at;
101 102
102 if (name == NULL) 103 if (name == NULL)
103 return NULL; 104 return NULL;
104 105
105 for (at = first_archetype; at != NULL; at = at->next) 106 for (at = first_archetype; at != NULL; at = at->next)
179 { 180 {
180 tmpname[i] = 0; 181 tmpname[i] = 0;
181 at = find_archetype_by_object_name (tmpname); 182 at = find_archetype_by_object_name (tmpname);
182 183
183 if (at != NULL) 184 if (at != NULL)
184 {
185 return arch_to_object (at); 185 return arch_to_object (at);
186 }
187 } 186 }
188 187
189 return create_singularity (name); 188 return create_singularity (name);
190} 189}
191 190
207 * Last, make a check on the full name. 206 * Last, make a check on the full name.
208 */ 207 */
209int 208int
210item_matched_string (object *pl, object *op, const char *name) 209item_matched_string (object *pl, object *op, const char *name)
211{ 210{
212 char *
213 cp,
214 local_name[MAX_BUF]; 211 char *cp, local_name[MAX_BUF];
215 int 212 int count, retval = 0;
216 count,
217 retval = 0;
218 213
219 strcpy (local_name, name); /* strtok is destructive to name */ 214 strcpy (local_name, name); /* strtok is destructive to name */
220 215
221 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) 216 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
222 { 217 {
274 retval = 15; 269 retval = 15;
275 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) 270 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp)))
276 retval = 14; 271 retval = 14;
277 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) 272 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp)))
278 retval = 14; 273 retval = 14;
279
280 /* Do substring checks, so things like 'Str+1' will match. 274 /* Do substring checks, so things like 'Str+1' will match.
281 * retval of these should perhaps be lower - they are lower 275 * retval of these should perhaps be lower - they are lower
282 * then the specific strcasecmp aboves, but still higher than 276 * then the specific strcasecmp aboves, but still higher than
283 * some other match criteria. 277 * some other match criteria.
284 */ 278 */
286 retval = 12; 280 retval = 12;
287 else if (strstr (query_base_name (op, 0), cp)) 281 else if (strstr (query_base_name (op, 0), cp))
288 retval = 12; 282 retval = 12;
289 else if (strstr (query_short_name (op), cp)) 283 else if (strstr (query_short_name (op), cp))
290 retval = 12; 284 retval = 12;
291
292 /* Check against plural/non plural based on count. */ 285 /* Check against plural/non plural based on count. */
293 else if (count > 1 && !strcasecmp (cp, op->name_pl)) 286 else if (count > 1 && !strcasecmp (cp, op->name_pl))
294 {
295 retval = 6; 287 retval = 6;
296 }
297 else if (count == 1 && !strcasecmp (op->name, cp)) 288 else if (count == 1 && !strcasecmp (op->name, cp))
298 {
299 retval = 6; 289 retval = 6;
300 }
301 /* base name matched - not bad */ 290 /* base name matched - not bad */
302 else if (strcasecmp (cp, op->name) == 0 && !count) 291 else if (strcasecmp (cp, op->name) == 0 && !count)
303 retval = 4; 292 retval = 4;
304 /* Check for partial custom name, but give a real low priority */ 293 /* Check for partial custom name, but give a real low priority */
305 else if (op->custom_name && strstr (op->custom_name, cp)) 294 else if (op->custom_name && strstr (op->custom_name, cp))
307 296
308 if (retval) 297 if (retval)
309 { 298 {
310 if (pl->type == PLAYER) 299 if (pl->type == PLAYER)
311 pl->contr->count = count; 300 pl->contr->count = count;
301
312 return retval; 302 return retval;
313 } 303 }
314 } 304 }
305
315 return 0; 306 return 0;
316} 307}
317 308
318/* 309/*
319 * Initialises the internal linked list of archetypes (read from file). 310 * Initialises the internal linked list of archetypes (read from file).
324void 315void
325init_archetypes (void) 316init_archetypes (void)
326{ /* called from add_player() and edit() */ 317{ /* called from add_player() and edit() */
327 if (first_archetype != NULL) /* Only do this once */ 318 if (first_archetype != NULL) /* Only do this once */
328 return; 319 return;
320
329 arch_init = 1; 321 arch_init = 1;
330 load_archetypes (); 322 load_archetypes ();
331 arch_init = 0; 323 arch_init = 0;
332 empty_archetype = find_archetype ("empty_archetype"); 324 empty_archetype = archetype::find ("empty_archetype");
333 325
334/* init_blocksview();*/ 326/* init_blocksview();*/
335} 327}
336 328
337/* 329/*
361 */ 353 */
362 354
363void 355void
364init_archetable (void) 356init_archetable (void)
365{ 357{
366 archetype * 358 archetype *at;
367 at;
368 359
369 LOG (llevDebug, " Setting up archetable...\n"); 360 LOG (llevDebug, " Setting up archetable...\n");
370 361
371 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 362 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
372 add_arch (at); 363 at->hash_add ();
373 364
374 LOG (llevDebug, "done\n"); 365 LOG (llevDebug, "done\n");
375} 366}
376 367
377/*
378 * Dumps an archetype to debug-level output.
379 */
380
381void
382dump_arch (archetype *at)
383{
384 dump_object (&at->clone);
385}
386
387/*
388 * Dumps _all_ archetypes to debug-level output.
389 * If you run crossfire with debug, and enter DM-mode, you can trigger
390 * this with the O key.
391 */
392
393void
394dump_all_archetypes (void)
395{
396 archetype *
397 at;
398
399 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
400 {
401 dump_arch (at);
402 fprintf (logfile, "%s\n", errmsg);
403 }
404}
405
406void 368void
407free_all_archs (void) 369free_all_archs (void)
408{ 370{
409 archetype * 371 archetype *at, *next;
410 at, *
411 next;
412 int
413 i = 0, f = 0; 372 int i = 0, f = 0;
414 373
415 for (at = first_archetype; at != NULL; at = next) 374 for (at = first_archetype; at != NULL; at = next)
416 { 375 {
417 if (at->more) 376 if (at->more)
418 next = at->more; 377 next = at->more;
422 delete 381 delete
423 at; 382 at;
424 383
425 i++; 384 i++;
426 } 385 }
386
427 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 387 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
428} 388}
429 389
430archetype::archetype () 390archetype::archetype ()
431{ 391{
443 * of archetype-structures. 403 * of archetype-structures.
444 */ 404 */
445void 405void
446first_arch_pass (object_thawer & fp) 406first_arch_pass (object_thawer & fp)
447{ 407{
448 archetype * 408 archetype *head = 0, *last_more = 0;
449 at, *
450 head = NULL, *last_more = NULL;
451 409
410 archetype *at = new archetype;
452 at->clone.arch = first_archetype = at = new archetype; 411 at->clone.arch = first_archetype = at;
453 412
454 while (int i = load_object (fp, &at->clone, 0)) 413 while (int i = load_object (fp, &at->clone, 0))
455 { 414 {
456 at->clone.speed_left = (float) (-0.1); 415 at->clone.speed_left = (float) (-0.1);
457 /* copy the body_info to the body_used - this is only really 416 /* copy the body_info to the body_used - this is only really
524 */ 483 */
525 484
526void 485void
527second_arch_pass (object_thawer & thawer) 486second_arch_pass (object_thawer & thawer)
528{ 487{
529 char
530 buf[MAX_BUF], *
531 variable = buf, *argument, *cp; 488 char buf[MAX_BUF], *variable = buf, *argument, *cp;
532 archetype *
533 at = NULL, *other; 489 archetype *at = NULL, *other;
534 490
535 while (fgets (buf, MAX_BUF, thawer) != NULL) 491 while (fgets (buf, MAX_BUF, thawer) != NULL)
536 { 492 {
537 if (*buf == '#') 493 if (*buf == '#')
538 continue; 494 continue;
546 cp--; 502 cp--;
547 } 503 }
548 } 504 }
549 if (!strcmp ("Object", variable)) 505 if (!strcmp ("Object", variable))
550 { 506 {
551 if ((at = find_archetype (argument)) == NULL) 507 if ((at = archetype::find (argument)) == NULL)
552 LOG (llevError, "Warning: failed to find arch %s\n", argument); 508 LOG (llevError, "Warning: failed to find arch %s\n", argument);
553 } 509 }
554 else if (!strcmp ("other_arch", variable)) 510 else if (!strcmp ("other_arch", variable))
555 { 511 {
556 if (at != NULL && at->clone.other_arch == NULL) 512 if (at != NULL && at->clone.other_arch == NULL)
557 { 513 {
558 if ((other = find_archetype (argument)) == NULL) 514 if ((other = archetype::find (argument)) == NULL)
559 LOG (llevError, "Warning: failed to find other_arch %s\n", argument); 515 LOG (llevError, "Warning: failed to find other_arch %s\n", argument);
560 else if (at != NULL) 516 else if (at != NULL)
561 at->clone.other_arch = other; 517 at->clone.other_arch = other;
562 } 518 }
563 } 519 }
564 else if (!strcmp ("randomitems", variable)) 520 else if (!strcmp ("randomitems", variable))
565 { 521 {
566 if (at != NULL) 522 if (at != NULL)
567 { 523 {
568 treasurelist *
569 tl = find_treasurelist (argument); 524 treasurelist *tl = find_treasurelist (argument);
570 525
571 if (tl == NULL) 526 if (tl == NULL)
572 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); 527 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument);
573 else 528 else
574 at->clone.randomitems = tl; 529 at->clone.randomitems = tl;
579 534
580#ifdef DEBUG 535#ifdef DEBUG
581void 536void
582check_generators (void) 537check_generators (void)
583{ 538{
584 archetype * 539 archetype *at;
585 at;
586 540
587 for (at = first_archetype; at != NULL; at = at->next) 541 for (at = first_archetype; at != NULL; at = at->next)
588 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) 542 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL)
589 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); 543 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name);
590} 544}
598 */ 552 */
599 553
600void 554void
601load_archetypes (void) 555load_archetypes (void)
602{ 556{
603 char
604 filename[MAX_BUF]; 557 char filename[MAX_BUF];
605
606#if TIME_ARCH_LOAD
607 struct timeval
608 tv1,
609 tv2;
610#endif
611 558
612 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 559 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
613 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 560 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
614 561
615 { 562 {
647 */ 594 */
648 595
649object * 596object *
650arch_to_object (archetype *at) 597arch_to_object (archetype *at)
651{ 598{
652 object * 599 object *op;
653 op;
654 600
655 if (at == NULL) 601 if (at == NULL)
656 { 602 {
657 if (warn_archetypes) 603 if (warn_archetypes)
658 LOG (llevError, "Couldn't find archetype.\n"); 604 LOG (llevError, "Couldn't find archetype.\n");
675 */ 621 */
676 622
677object * 623object *
678create_singularity (const char *name) 624create_singularity (const char *name)
679{ 625{
680 object * 626 object *op;
681 op;
682 char
683 buf[MAX_BUF]; 627 char buf[MAX_BUF];
684 628
685 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 629 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
686 op = get_object (); 630 op = get_object ();
687 op->name = op->name_pl = buf; 631 op->name = op->name_pl = buf;
688 SET_FLAG (op, FLAG_NO_PICK); 632 SET_FLAG (op, FLAG_NO_PICK);
695 */ 639 */
696 640
697object * 641object *
698get_archetype (const char *name) 642get_archetype (const char *name)
699{ 643{
700 archetype * 644 archetype *at = archetype::find (name);
701 at;
702 645
703 at = find_archetype (name); 646 if (!at)
704 if (at == NULL)
705 return create_singularity (name); 647 return create_singularity (name);
706 648
707 return arch_to_object (at); 649 return arch_to_object (at);
708} 650}
709 651
712 */ 654 */
713 655
714unsigned long 656unsigned long
715hasharch (const char *str, int tablesize) 657hasharch (const char *str, int tablesize)
716{ 658{
717 unsigned long 659 unsigned long hash = 0;
718 hash = 0;
719 unsigned int 660 unsigned int i = 0;
720 i = 0;
721 const char * 661 const char *p;
722 p;
723 662
724 /* use the one-at-a-time hash function, which supposedly is 663 /* use the one-at-a-time hash function, which supposedly is
725 * better than the djb2-like one used by perl5.005, but 664 * better than the djb2-like one used by perl5.005, but
726 * certainly is better then the bug used here before. 665 * certainly is better then the bug used here before.
727 * see http://burtleburtle.net/bob/hash/doobs.html 666 * see http://burtleburtle.net/bob/hash/doobs.html
744 * Finds, using the hashtable, which archetype matches the given name. 683 * Finds, using the hashtable, which archetype matches the given name.
745 * returns a pointer to the found archetype, otherwise NULL. 684 * returns a pointer to the found archetype, otherwise NULL.
746 */ 685 */
747 686
748archetype * 687archetype *
749find_archetype (const char *name) 688archetype::find (const char *name)
750{ 689{
751#if USE_UNORDERED_MAP
752 name = shstr::find (name);
753
754 if (!name) 690 if (!name)
755 return 0; 691 return 0;
756 692
757 HT::const_iterator i = ht.find ((size_t) name); 693#if USE_UNORDERED_MAP
694 AUTODECL (i, ht.find ((size_t) name));
758 695
759 if (i == ht.end ()) 696 if (i == ht.end ())
760 return 0; 697 return 0;
761 else 698 else
762 return i->second; 699 return i->second;
763#endif 700#endif
764 701
765 archetype * 702 archetype *at;
766 at;
767 unsigned long 703 unsigned long index;
768 index;
769
770 if (name == NULL)
771 return (archetype *) NULL;
772 704
773 index = hasharch (name, ARCHTABLE); 705 index = hasharch (name, ARCHTABLE);
774 arch_search++; 706 arch_search++;
775 for (;;) 707 for (;;)
776 { 708 {
777 at = arch_table[index]; 709 at = arch_table[index];
710
778 if (at == NULL) 711 if (at == NULL)
779 { 712 {
780 if (warn_archetypes) 713 if (warn_archetypes)
781 LOG (llevError, "Couldn't find archetype %s\n", name); 714 LOG (llevError, "Couldn't find archetype %s\n", name);
715
782 return NULL; 716 return NULL;
783 } 717 }
718
784 arch_cmp++; 719 arch_cmp++;
720
785 if (!strcmp ((const char *) at->name, name)) 721 if (!strcmp ((const char *) at->name, name))
786 return at; 722 return at;
723
787 if (++index >= ARCHTABLE) 724 if (++index >= ARCHTABLE)
788 index = 0; 725 index = 0;
789 } 726 }
790} 727}
791 728
792/* 729/*
793 * Adds an archetype to the hashtable. 730 * Adds an archetype to the hashtable.
794 */ 731 */
795 732void
796static void 733archetype::hash_add ()
797add_arch (archetype *at)
798{ 734{
799#if USE_UNORDERED_MAP 735#if USE_UNORDERED_MAP
800 ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); 736 ht.insert (std::make_pair ((size_t) (const char *) name, this));
801#endif 737#else
802 738
803 int
804 index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 739 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
805 740
806 for (;;) 741 for (;;)
807 { 742 {
808 if (arch_table[index] == NULL) 743 if (!arch_table[index])
809 { 744 {
810 arch_table[index] = at; 745 arch_table[index] = this;
811 return; 746 break;
812 } 747 }
813 748
814 if (++index == ARCHTABLE) 749 if (++index == ARCHTABLE)
815 index = 0; 750 index = 0;
816 751
817 if (index == org_index) 752 if (index == org_index)
818 fatal (ARCHTABLE_TOO_SMALL); 753 fatal (ARCHTABLE_TOO_SMALL);
819 } 754 }
755#endif
756}
757
758void
759archetype::hash_del ()
760{
761#if USE_UNORDERED_MAP
762# error remove this from HT
763#else
764
765 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
766
767 for (;;)
768 {
769 if (arch_table[index] == this)
770 {
771 arch_table[index] = 0;
772 break;
773 }
774
775 if (++index == ARCHTABLE)
776 index = 0;
777
778 if (index == org_index)
779 break;
780 }
781#endif
820} 782}
821 783
822/* 784/*
823 * Returns the first archetype using the given type. 785 * Returns the first archetype using the given type.
824 * Used in treasure-generation. 786 * Used in treasure-generation.
825 */ 787 */
826 788
827archetype * 789archetype *
828type_to_archetype (int type) 790type_to_archetype (int type)
829{ 791{
830 archetype * 792 archetype *at;
831 at;
832 793
833 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 794 for (at = first_archetype; at; at = at->more == 0 ? at->next : at->more)
834 if (at->clone.type == type) 795 if (at->clone.type == type)
835 return at; 796 return at;
797
836 return NULL; 798 return 0;
837} 799}
838 800
839/* 801/*
840 * Returns a new object copied from the first archetype matching 802 * Returns a new object copied from the first archetype matching
841 * the given type. 803 * the given type.
843 */ 805 */
844 806
845object * 807object *
846clone_arch (int type) 808clone_arch (int type)
847{ 809{
848 archetype * 810 archetype *at;
849 at;
850 object *
851 op = get_object (); 811 object *op = get_object ();
852 812
853 if ((at = type_to_archetype (type)) == NULL) 813 if ((at = type_to_archetype (type)) == NULL)
854 { 814 {
855 LOG (llevError, "Can't clone archetype %d\n", type); 815 LOG (llevError, "Can't clone archetype %d\n", type);
856 free_object (op); 816 free_object (op);
857 return NULL; 817 return NULL;
858 } 818 }
819
859 copy_object (&at->clone, op); 820 copy_object (&at->clone, op);
860 op->instantiate (); 821 op->instantiate ();
861 return op; 822 return op;
862} 823}
863 824
866 */ 827 */
867 828
868object * 829object *
869object_create_arch (archetype *at) 830object_create_arch (archetype *at)
870{ 831{
871 object * 832 object *op, *prev = 0, *head = 0;
872 op, *
873 prev = NULL, *head = NULL;
874 833
875 while (at) 834 while (at)
876 { 835 {
877 op = arch_to_object (at); 836 op = arch_to_object (at);
878 op->x = at->clone.x; 837 op->x = at->clone.x;
879 op->y = at->clone.y; 838 op->y = at->clone.y;
839
880 if (head) 840 if (head)
881 op->head = head, prev->more = op; 841 op->head = head, prev->more = op;
842
882 if (!head) 843 if (!head)
883 head = op; 844 head = op;
845
884 prev = op; 846 prev = op;
885 at = at->more; 847 at = at->more;
886 } 848 }
849
887 return (head); 850 return (head);
888} 851}
889 852
890/*** end of arch.c ***/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines