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.21 by root, Thu Sep 14 22:33:58 2006 UTC vs.
Revision 1.38 by root, Fri Feb 2 21:50:43 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * CrossFire, A Multiplayer game for X-windows
3 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (C) 1992 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version. 11 * (at your option) any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 21 *
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 23 */
23 24
24#include <cassert> 25#include <cassert>
25 26
26#include <tr1/unordered_map>
27
28#include <global.h> 27#include <global.h>
29#include <arch.h>
30#include <funcpoint.h> 28#include <funcpoint.h>
31#include <loader.h> 29#include <loader.h>
32 30
33/* IF set, does a little timing on the archetype load. */ 31#include <tr1/functional>
34#define TIME_ARCH_LOAD 0 32#include <tr1/unordered_map>
35 33
36static void add_arch (archetype *at);
37
38static archetype *arch_table[ARCHTABLE];
39int arch_cmp = 0; /* How many strcmp's */
40int arch_search = 0; /* How many searches */
41int arch_init; /* True if doing arch initialization */ 34int arch_init; /* True if doing arch initialization */
42 35
43/* The naming of these functions is really poor - they are all 36/* The naming of these functions is really poor - they are all
44 * pretty much named '.._arch_...', but they may more may not 37 * pretty much named '.._arch_...', but they may more may not
45 * return archetypes. Some make the arch_to_object call, and thus 38 * return archetypes. Some make the arch_to_object call, and thus
46 * return an object. Perhaps those should be called 'archob' functions 39 * return an object. Perhaps those should be called 'archob' functions
47 * to denote they return an object derived from the archetype. 40 * to denote they return an object derived from the archetype.
48 * MSW 2003-04-29 41 * MSW 2003-04-29
49 */ 42 */
50 43
51#if USE_UNORDERED_MAP
52// the hashtable 44// the hashtable
53typedef std::tr1::unordered_map 45typedef std::tr1::unordered_map
54 < 46 <
55 std::size_t, 47 const char *,
56 archetype *, 48 arch_ptr,
57 std::hash<size_t>, 49 str_hash,
58 std::equal_to<size_t>, 50 str_equal,
59 slice_allocator< std::pair<const std::size_t, archetype *> > 51 slice_allocator< std::pair<const char *const, arch_ptr> >
60 true,
61 > HT; 52 > HT;
62 53
63static HT ht; 54static HT ht (5000);
64#endif
65 55
66/** 56/**
67 * GROS - This function retrieves an archetype given the name that appears 57 * GROS - This function retrieves an archetype given the name that appears
68 * during the game (for example, "writing pen" instead of "stylus"). 58 * during the game (for example, "writing pen" instead of "stylus").
69 * It does not use the hashtable system, but browse the whole archlist each time. 59 * It does not use the hashtable system, but browse the whole archlist each time.
118 * If type is -1, ew don't match on type. 108 * If type is -1, ew don't match on type.
119 */ 109 */
120object * 110object *
121get_archetype_by_skill_name (const char *skill, int type) 111get_archetype_by_skill_name (const char *skill, int type)
122{ 112{
123 archetype * 113 archetype *at;
124 at;
125 114
126 if (skill == NULL) 115 if (skill)
127 return NULL;
128
129 for (at = first_archetype; at != NULL; at = at->next) 116 for (at = first_archetype; at; at = at->next)
130 {
131 if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill))) 117 if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill)))
132 return arch_to_object (at); 118 return arch_to_object (at);
133 } 119
134 return NULL; 120 return 0;
135} 121}
136 122
137/* similiar to above - this returns the first archetype 123/* similiar to above - this returns the first archetype
138 * that matches both the type and subtype. type and subtype 124 * that matches both the type and subtype. type and subtype
139 * can be -1 to say ignore, but in this case, the match it does 125 * can be -1 to say ignore, but in this case, the match it does
179 for (i = strlen (tmpname); i > 0; i--) 165 for (i = strlen (tmpname); i > 0; i--)
180 { 166 {
181 tmpname[i] = 0; 167 tmpname[i] = 0;
182 at = find_archetype_by_object_name (tmpname); 168 at = find_archetype_by_object_name (tmpname);
183 169
184 if (at != NULL) 170 if (at)
185 return arch_to_object (at); 171 return arch_to_object (at);
186 } 172 }
187 173
188 return create_singularity (name); 174 return create_singularity (name);
189} 175}
206 * Last, make a check on the full name. 192 * Last, make a check on the full name.
207 */ 193 */
208int 194int
209item_matched_string (object *pl, object *op, const char *name) 195item_matched_string (object *pl, object *op, const char *name)
210{ 196{
211 char *
212 cp,
213 local_name[MAX_BUF]; 197 char *cp, local_name[MAX_BUF];
214 int 198 int count, retval = 0;
215 count,
216 retval = 0;
217 199
218 strcpy (local_name, name); /* strtok is destructive to name */ 200 strcpy (local_name, name); /* strtok is destructive to name */
219 201
220 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) 202 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
221 { 203 {
273 retval = 15; 255 retval = 15;
274 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) 256 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp)))
275 retval = 14; 257 retval = 14;
276 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) 258 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp)))
277 retval = 14; 259 retval = 14;
278
279 /* Do substring checks, so things like 'Str+1' will match. 260 /* Do substring checks, so things like 'Str+1' will match.
280 * retval of these should perhaps be lower - they are lower 261 * retval of these should perhaps be lower - they are lower
281 * then the specific strcasecmp aboves, but still higher than 262 * then the specific strcasecmp aboves, but still higher than
282 * some other match criteria. 263 * some other match criteria.
283 */ 264 */
285 retval = 12; 266 retval = 12;
286 else if (strstr (query_base_name (op, 0), cp)) 267 else if (strstr (query_base_name (op, 0), cp))
287 retval = 12; 268 retval = 12;
288 else if (strstr (query_short_name (op), cp)) 269 else if (strstr (query_short_name (op), cp))
289 retval = 12; 270 retval = 12;
290
291 /* Check against plural/non plural based on count. */ 271 /* Check against plural/non plural based on count. */
292 else if (count > 1 && !strcasecmp (cp, op->name_pl)) 272 else if (count > 1 && !strcasecmp (cp, op->name_pl))
293 {
294 retval = 6; 273 retval = 6;
295 }
296 else if (count == 1 && !strcasecmp (op->name, cp)) 274 else if (count == 1 && !strcasecmp (op->name, cp))
297 {
298 retval = 6; 275 retval = 6;
299 }
300 /* base name matched - not bad */ 276 /* base name matched - not bad */
301 else if (strcasecmp (cp, op->name) == 0 && !count) 277 else if (strcasecmp (cp, op->name) == 0 && !count)
302 retval = 4; 278 retval = 4;
303 /* Check for partial custom name, but give a real low priority */ 279 /* Check for partial custom name, but give a real low priority */
304 else if (op->custom_name && strstr (op->custom_name, cp)) 280 else if (op->custom_name && strstr (op->custom_name, cp))
306 282
307 if (retval) 283 if (retval)
308 { 284 {
309 if (pl->type == PLAYER) 285 if (pl->type == PLAYER)
310 pl->contr->count = count; 286 pl->contr->count = count;
287
311 return retval; 288 return retval;
312 } 289 }
313 } 290 }
291
314 return 0; 292 return 0;
315} 293}
316 294
317/* 295/*
318 * Initialises the internal linked list of archetypes (read from file). 296 * Initialises the internal linked list of archetypes (read from file).
333 311
334/* init_blocksview();*/ 312/* init_blocksview();*/
335} 313}
336 314
337/* 315/*
338 * Stores debug-information about how efficient the hashtable
339 * used for archetypes has been in the static errmsg array.
340 */
341
342void
343arch_info (object *op)
344{
345 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp);
346 new_draw_info (NDI_BLACK, 0, op, errmsg);
347}
348
349/*
350 * Initialise the hashtable used by the archetypes.
351 */
352
353void
354clear_archetable (void)
355{
356 memset ((void *) arch_table, 0, ARCHTABLE * sizeof (archetype *));
357}
358
359/*
360 * An alternative way to init the hashtable which is slower, but _works_... 316 * An alternative way to init the hashtable which is slower, but _works_...
361 */ 317 */
362
363void 318void
364init_archetable (void) 319init_archetable (void)
365{ 320{
366 archetype * 321 archetype *at;
367 at;
368 322
369 LOG (llevDebug, " Setting up archetable...\n"); 323 LOG (llevDebug, " Setting up archetable...\n");
370 324
371 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 325 for (at = first_archetype; at; at = at->more ? at->more : at->next)
372 add_arch (at); 326 at->hash_add ();
373 327
374 LOG (llevDebug, "done\n"); 328 LOG (llevDebug, "done\n");
375} 329}
376 330
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 331void
407free_all_archs (void) 332free_all_archs (void)
408{ 333{
409 archetype * 334 archetype *at, *next;
410 at, *
411 next;
412 int
413 i = 0, f = 0; 335 int i = 0, f = 0;
414 336
415 for (at = first_archetype; at != NULL; at = next) 337 for (at = first_archetype; at; at = next)
416 { 338 {
417 if (at->more) 339 if (at->more)
418 next = at->more; 340 next = at->more;
419 else 341 else
420 next = at->next; 342 next = at->next;
422 delete 344 delete
423 at; 345 at;
424 346
425 i++; 347 i++;
426 } 348 }
349
427 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 350 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
428} 351}
429 352
430archetype::archetype () 353archetype::archetype ()
431{ 354{
432 clear_object (&clone); /* to initial state other also */
433 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ 355 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */
434 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ 356 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
435} 357}
436 358
437archetype::~archetype () 359archetype::~archetype ()
438{ 360{
443 * of archetype-structures. 365 * of archetype-structures.
444 */ 366 */
445void 367void
446first_arch_pass (object_thawer & fp) 368first_arch_pass (object_thawer & fp)
447{ 369{
448 archetype * 370 archetype *head = 0, *last_more = 0;
449 at, *
450 head = NULL, *last_more = NULL;
451 371
372 archetype *at = new archetype;
452 at->clone.arch = first_archetype = at = new archetype; 373 at->clone.arch = first_archetype = at;
453 374
454 while (int i = load_object (fp, &at->clone, 0)) 375 while (int i = load_object (fp, &at->clone, 0))
455 { 376 {
456 at->clone.speed_left = (float) (-0.1); 377 at->clone.speed_left = (float) (-0.1);
457 /* copy the body_info to the body_used - this is only really 378 /* copy the body_info to the body_used - this is only really
524 */ 445 */
525 446
526void 447void
527second_arch_pass (object_thawer & thawer) 448second_arch_pass (object_thawer & thawer)
528{ 449{
529 char
530 buf[MAX_BUF], *
531 variable = buf, *argument, *cp; 450 char buf[MAX_BUF], *variable = buf, *argument, *cp;
532 archetype *
533 at = NULL, *other; 451 archetype *at = NULL, *other;
534 452
535 while (fgets (buf, MAX_BUF, thawer) != NULL) 453 while (fgets (buf, MAX_BUF, thawer) != NULL)
536 { 454 {
537 if (*buf == '#') 455 if (*buf == '#')
538 continue; 456 continue;
563 } 481 }
564 else if (!strcmp ("randomitems", variable)) 482 else if (!strcmp ("randomitems", variable))
565 { 483 {
566 if (at != NULL) 484 if (at != NULL)
567 { 485 {
568 treasurelist *
569 tl = find_treasurelist (argument); 486 treasurelist *tl = find_treasurelist (argument);
570 487
571 if (tl == NULL) 488 if (tl == NULL)
572 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); 489 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument);
573 else 490 else
574 at->clone.randomitems = tl; 491 at->clone.randomitems = tl;
579 496
580#ifdef DEBUG 497#ifdef DEBUG
581void 498void
582check_generators (void) 499check_generators (void)
583{ 500{
584 archetype * 501 archetype *at;
585 at;
586 502
587 for (at = first_archetype; at != NULL; at = at->next) 503 for (at = first_archetype; at != NULL; at = at->next)
588 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) 504 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); 505 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name);
590} 506}
598 */ 514 */
599 515
600void 516void
601load_archetypes (void) 517load_archetypes (void)
602{ 518{
603 char
604 filename[MAX_BUF]; 519 char filename[MAX_BUF];
605
606#if TIME_ARCH_LOAD
607 struct timeval
608 tv1,
609 tv2;
610#endif
611 520
612 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 521 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
613 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 522 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
614 523
615 { 524 {
616 object_thawer 525 object_thawer
617 thawer (filename); 526 thawer (filename);
618 527
619 clear_archetable ();
620 LOG (llevDebug, " arch-pass 1...\n"); 528 LOG (llevDebug, " arch-pass 1...\n");
621 first_arch_pass (thawer); 529 first_arch_pass (thawer);
622 LOG (llevDebug, " done\n"); 530 LOG (llevDebug, " done\n");
623 } 531 }
624 532
629 object_thawer 537 object_thawer
630 thawer (filename); 538 thawer (filename);
631 539
632 LOG (llevDebug, " loading treasure...\n"); 540 LOG (llevDebug, " loading treasure...\n");
633 load_treasures (); 541 load_treasures ();
542 LOG (llevDebug, " done\n");
634 LOG (llevDebug, " done\n arch-pass 2...\n"); 543 LOG (llevDebug, " arch-pass 2...\n");
635 second_arch_pass (thawer); 544 second_arch_pass (thawer);
636 LOG (llevDebug, " done\n"); 545 LOG (llevDebug, " done\n");
637#ifdef DEBUG 546#ifdef DEBUG
638 check_generators (); 547 check_generators ();
639#endif 548#endif
643 552
644/* 553/*
645 * Creates and returns a new object which is a copy of the given archetype. 554 * Creates and returns a new object which is a copy of the given archetype.
646 * This function returns NULL on failure. 555 * This function returns NULL on failure.
647 */ 556 */
648
649object * 557object *
650arch_to_object (archetype *at) 558arch_to_object (archetype *at)
651{ 559{
652 object * 560 object *op;
653 op;
654 561
655 if (at == NULL) 562 if (at == NULL)
656 { 563 {
657 if (warn_archetypes) 564 if (warn_archetypes)
658 LOG (llevError, "Couldn't find archetype.\n"); 565 LOG (llevError, "Couldn't find archetype.\n");
659 566
660 return NULL; 567 return NULL;
661 } 568 }
662 569
663 op = get_object (); 570 op = at->clone.clone ();
664 copy_object (&at->clone, op);
665 op->arch = at; 571 op->arch = at;
666 op->instantiate (); 572 op->instantiate ();
667 return op; 573 return op;
668} 574}
669 575
671 * Creates an object. This function is called by get_archetype() 577 * Creates an object. This function is called by get_archetype()
672 * if it fails to find the appropriate archetype. 578 * if it fails to find the appropriate archetype.
673 * Thus get_archetype() will be guaranteed to always return 579 * Thus get_archetype() will be guaranteed to always return
674 * an object, and never NULL. 580 * an object, and never NULL.
675 */ 581 */
676
677object * 582object *
678create_singularity (const char *name) 583create_singularity (const char *name)
679{ 584{
680 object * op; 585 object *op;
681 char buf[MAX_BUF]; 586 char buf[MAX_BUF];
682 587
683 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 588 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
684 op = get_object (); 589 op = object::create ();
685 op->name = op->name_pl = buf; 590 op->name = op->name_pl = buf;
686 SET_FLAG (op, FLAG_NO_PICK); 591 SET_FLAG (op, FLAG_NO_PICK);
687 return op; 592 return op;
688} 593}
689 594
690/* 595/*
691 * Finds which archetype matches the given name, and returns a new 596 * Finds which archetype matches the given name, and returns a new
692 * object containing a copy of the archetype. 597 * object containing a copy of the archetype.
693 */ 598 */
694
695object * 599object *
696get_archetype (const char *name) 600get_archetype (const char *name)
697{ 601{
698 archetype *at = archetype::find (name); 602 archetype *at = archetype::find (name);
699 603
735 639
736/* 640/*
737 * Finds, using the hashtable, which archetype matches the given name. 641 * Finds, using the hashtable, which archetype matches the given name.
738 * returns a pointer to the found archetype, otherwise NULL. 642 * returns a pointer to the found archetype, otherwise NULL.
739 */ 643 */
740
741archetype * 644archetype *
742archetype::find (const char *name) 645archetype::find (const char *name)
743{ 646{
744 if (!name) 647 if (!name)
745 return 0; 648 return 0;
746 649
747#if USE_UNORDERED_MAP
748 AUTODECL (i, ht.find ((size_t) name)); 650 AUTODECL (i, ht.find (name));
749 651
750 if (i == ht.end ()) 652 if (i == ht.end ())
751 return 0; 653 return 0;
752 else 654 else
753 return i->second; 655 return i->second;
754#endif
755
756 archetype *at;
757 unsigned long index;
758
759 index = hasharch (name, ARCHTABLE);
760 arch_search++;
761 for (;;)
762 {
763 at = arch_table[index];
764
765 if (at == NULL)
766 {
767 if (warn_archetypes)
768 LOG (llevError, "Couldn't find archetype %s\n", name);
769
770 return NULL;
771 }
772
773 arch_cmp++;
774
775 if (!strcmp ((const char *) at->name, name))
776 return at;
777
778 if (++index >= ARCHTABLE)
779 index = 0;
780 }
781} 656}
782 657
783/* 658/*
784 * Adds an archetype to the hashtable. 659 * Adds an archetype to the hashtable.
785 */ 660 */
786 661void
787static void 662archetype::hash_add ()
788add_arch (archetype *at)
789{ 663{
790#if USE_UNORDERED_MAP 664 ht.insert (std::make_pair (name, this));
791 ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); 665}
792#endif
793 666
794 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 667void
795 668archetype::hash_del ()
796 for (;;) 669{
797 { 670 ht.erase (name);
798 if (arch_table[index] == NULL)
799 {
800 arch_table[index] = at;
801 return;
802 }
803
804 if (++index == ARCHTABLE)
805 index = 0;
806
807 if (index == org_index)
808 fatal (ARCHTABLE_TOO_SMALL);
809 }
810} 671}
811 672
812/* 673/*
813 * Returns the first archetype using the given type. 674 * Returns the first archetype using the given type.
814 * Used in treasure-generation. 675 * Used in treasure-generation.
815 */ 676 */
816
817archetype * 677archetype *
818type_to_archetype (int type) 678type_to_archetype (int type)
819{ 679{
820 archetype *at; 680 archetype *at;
821 681
834 694
835object * 695object *
836clone_arch (int type) 696clone_arch (int type)
837{ 697{
838 archetype *at; 698 archetype *at;
839 object *op = get_object ();
840 699
841 if ((at = type_to_archetype (type)) == NULL) 700 if ((at = type_to_archetype (type)) == NULL)
842 { 701 {
843 LOG (llevError, "Can't clone archetype %d\n", type); 702 LOG (llevError, "Can't clone archetype %d\n", type);
844 free_object (op);
845 return NULL; 703 return 0;
846 } 704 }
847 705
848 copy_object (&at->clone, op); 706 object *op = at->clone.clone ();
849 op->instantiate (); 707 op->instantiate ();
850 return op; 708 return op;
851} 709}
852 710
853/* 711/*

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines