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.22 by root, Thu Sep 14 22:35:53 2006 UTC vs.
Revision 1.27 by root, Sat Dec 9 14:01:12 2006 UTC

21 The authors can be reached via e-mail at <crossfire@schmorp.de> 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
51#if USE_UNORDERED_MAP 49#if USE_UNORDERED_MAP
52// the hashtable 50// the hashtable
53typedef std::tr1::unordered_map 51typedef std::tr1::unordered_map
54 < 52 <
55 std::size_t, 53 std::size_t,
56 archetype *, 54 arch_ptr,
57 std::hash<size_t>, 55 std::tr1::hash<size_t>,
58 std::equal_to<size_t>, 56 std::equal_to<size_t>,
59 slice_allocator< std::pair<const std::size_t, archetype *> > 57 slice_allocator< std::pair<const std::size_t, archetype *> >
60 true, 58 true,
61 > HT; 59 > HT;
62 60
63static HT ht; 61static HT ht;
62#else
63static arch_ptr arch_table[ARCHTABLE];
64#endif 64#endif
65 65
66/** 66/**
67 * GROS - This function retrieves an archetype given the name that appears 67 * GROS - This function retrieves an archetype given the name that appears
68 * during the game (for example, "writing pen" instead of "stylus"). 68 * during the game (for example, "writing pen" instead of "stylus").
269 retval = 15; 269 retval = 15;
270 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) 270 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp)))
271 retval = 14; 271 retval = 14;
272 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) 272 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp)))
273 retval = 14; 273 retval = 14;
274
275 /* Do substring checks, so things like 'Str+1' will match. 274 /* Do substring checks, so things like 'Str+1' will match.
276 * retval of these should perhaps be lower - they are lower 275 * retval of these should perhaps be lower - they are lower
277 * then the specific strcasecmp aboves, but still higher than 276 * then the specific strcasecmp aboves, but still higher than
278 * some other match criteria. 277 * some other match criteria.
279 */ 278 */
281 retval = 12; 280 retval = 12;
282 else if (strstr (query_base_name (op, 0), cp)) 281 else if (strstr (query_base_name (op, 0), cp))
283 retval = 12; 282 retval = 12;
284 else if (strstr (query_short_name (op), cp)) 283 else if (strstr (query_short_name (op), cp))
285 retval = 12; 284 retval = 12;
286
287 /* Check against plural/non plural based on count. */ 285 /* Check against plural/non plural based on count. */
288 else if (count > 1 && !strcasecmp (cp, op->name_pl)) 286 else if (count > 1 && !strcasecmp (cp, op->name_pl))
289 {
290 retval = 6; 287 retval = 6;
291 }
292 else if (count == 1 && !strcasecmp (op->name, cp)) 288 else if (count == 1 && !strcasecmp (op->name, cp))
293 {
294 retval = 6; 289 retval = 6;
295 }
296 /* base name matched - not bad */ 290 /* base name matched - not bad */
297 else if (strcasecmp (cp, op->name) == 0 && !count) 291 else if (strcasecmp (cp, op->name) == 0 && !count)
298 retval = 4; 292 retval = 4;
299 /* Check for partial custom name, but give a real low priority */ 293 /* Check for partial custom name, but give a real low priority */
300 else if (op->custom_name && strstr (op->custom_name, cp)) 294 else if (op->custom_name && strstr (op->custom_name, cp))
302 296
303 if (retval) 297 if (retval)
304 { 298 {
305 if (pl->type == PLAYER) 299 if (pl->type == PLAYER)
306 pl->contr->count = count; 300 pl->contr->count = count;
301
307 return retval; 302 return retval;
308 } 303 }
309 } 304 }
305
310 return 0; 306 return 0;
311} 307}
312 308
313/* 309/*
314 * Initialises the internal linked list of archetypes (read from file). 310 * Initialises the internal linked list of archetypes (read from file).
362 archetype *at; 358 archetype *at;
363 359
364 LOG (llevDebug, " Setting up archetable...\n"); 360 LOG (llevDebug, " Setting up archetable...\n");
365 361
366 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)
367 add_arch (at); 363 at->hash_add ();
368 364
369 LOG (llevDebug, "done\n"); 365 LOG (llevDebug, "done\n");
370}
371
372/*
373 * Dumps an archetype to debug-level output.
374 */
375
376void
377dump_arch (archetype *at)
378{
379 dump_object (&at->clone);
380}
381
382/*
383 * Dumps _all_ archetypes to debug-level output.
384 * If you run crossfire with debug, and enter DM-mode, you can trigger
385 * this with the O key.
386 */
387
388void
389dump_all_archetypes (void)
390{
391 archetype *at;
392
393 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
394 {
395 dump_arch (at);
396 fprintf (logfile, "%s\n", errmsg);
397 }
398} 366}
399 367
400void 368void
401free_all_archs (void) 369free_all_archs (void)
402{ 370{
413 delete 381 delete
414 at; 382 at;
415 383
416 i++; 384 i++;
417 } 385 }
386
418 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 387 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
419} 388}
420 389
421archetype::archetype () 390archetype::archetype ()
422{ 391{
434 * of archetype-structures. 403 * of archetype-structures.
435 */ 404 */
436void 405void
437first_arch_pass (object_thawer & fp) 406first_arch_pass (object_thawer & fp)
438{ 407{
439 archetype *at, *head = NULL, *last_more = NULL; 408 archetype *head = 0, *last_more = 0;
440 409
410 archetype *at = new archetype;
441 at->clone.arch = first_archetype = at = new archetype; 411 at->clone.arch = first_archetype = at;
442 412
443 while (int i = load_object (fp, &at->clone, 0)) 413 while (int i = load_object (fp, &at->clone, 0))
444 { 414 {
445 at->clone.speed_left = (float) (-0.1); 415 at->clone.speed_left = (float) (-0.1);
446 /* 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
549 } 519 }
550 else if (!strcmp ("randomitems", variable)) 520 else if (!strcmp ("randomitems", variable))
551 { 521 {
552 if (at != NULL) 522 if (at != NULL)
553 { 523 {
554 treasurelist *
555 tl = find_treasurelist (argument); 524 treasurelist *tl = find_treasurelist (argument);
556 525
557 if (tl == NULL) 526 if (tl == NULL)
558 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);
559 else 528 else
560 at->clone.randomitems = tl; 529 at->clone.randomitems = tl;
584 553
585void 554void
586load_archetypes (void) 555load_archetypes (void)
587{ 556{
588 char filename[MAX_BUF]; 557 char filename[MAX_BUF];
589
590#if TIME_ARCH_LOAD
591 struct timeval
592 tv1,
593 tv2;
594#endif
595 558
596 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 559 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
597 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 560 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
598 561
599 { 562 {
764} 727}
765 728
766/* 729/*
767 * Adds an archetype to the hashtable. 730 * Adds an archetype to the hashtable.
768 */ 731 */
769 732void
770static void 733archetype::hash_add ()
771add_arch (archetype *at)
772{ 734{
773#if USE_UNORDERED_MAP 735#if USE_UNORDERED_MAP
774 ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); 736 ht.insert (std::make_pair ((size_t) (const char *) name, this));
775#endif 737#else
776 738
777 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 739 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
778 740
779 for (;;) 741 for (;;)
780 { 742 {
781 if (arch_table[index] == NULL) 743 if (!arch_table[index])
782 { 744 {
783 arch_table[index] = at; 745 arch_table[index] = this;
784 return; 746 break;
785 } 747 }
786 748
787 if (++index == ARCHTABLE) 749 if (++index == ARCHTABLE)
788 index = 0; 750 index = 0;
789 751
790 if (index == org_index) 752 if (index == org_index)
791 fatal (ARCHTABLE_TOO_SMALL); 753 fatal (ARCHTABLE_TOO_SMALL);
792 } 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
793} 782}
794 783
795/* 784/*
796 * Returns the first archetype using the given type. 785 * Returns the first archetype using the given type.
797 * Used in treasure-generation. 786 * Used in treasure-generation.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines