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.12 by root, Mon Sep 4 17:16:19 2006 UTC vs.
Revision 1.18 by root, Tue Sep 12 20:55:40 2006 UTC

1/*
2 * static char *rcsid_arch_c =
3 * "$Id: arch.C,v 1.12 2006/09/04 17:16:19 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
424/* 433archetype::archetype ()
425 * Allocates, initialises and returns the pointer to an archetype structure.
426 */
427// TODO: should become constructor
428archetype *
429get_archetype_struct (void)
430{ 434{
431 archetype *
432 arch;
433
434 arch = new archetype;
435
436 clear_object (&arch->clone); /* to initial state other also */ 435 clear_object (&clone); /* to initial state other also */
437 CLEAR_FLAG (&arch->clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ 436 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */
438 SET_FLAG (&arch->clone, FLAG_REMOVED); /* doesn't copy these flags... */ 437 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
438}
439 439
440 return arch; 440archetype::~archetype ()
441{
441} 442}
442 443
443/* 444/*
444 * Reads/parses the archetype-file, and copies into a linked list 445 * Reads/parses the archetype-file, and copies into a linked list
445 * of archetype-structures. 446 * of archetype-structures.
446 */ 447 */
447void 448void
448first_arch_pass (object_thawer & fp) 449first_arch_pass (object_thawer & fp)
449{ 450{
450 object *
451 op;
452 archetype * 451 archetype *
453 at, * 452 at, *
454 head = NULL, *last_more = NULL; 453 head = NULL, *last_more = NULL;
455 int
456 i;
457 454
458 op = get_object ();
459 op->arch = first_archetype = at = get_archetype_struct (); 455 at->clone.arch = first_archetype = at = new archetype;
460 456
461 while ((i = load_object (fp, op, 0))) 457 while (int i = load_object (fp, &at->clone, 0))
462 { 458 {
463 copy_object (op, &at->clone);
464 at->clone.speed_left = (float) (-0.1); 459 at->clone.speed_left = (float) (-0.1);
465 /* copy the body_info to the body_used - this is only really 460 /* copy the body_info to the body_used - this is only really
466 * need for monsters, but doesn't hurt to do it for everything. 461 * need for monsters, but doesn't hurt to do it for everything.
467 * by doing so, when a monster is created, it has good starting 462 * by doing so, when a monster is created, it has good starting
468 * values for the body_used info, so when items are created 463 * values for the body_used info, so when items are created
469 * for it, they can be properly equipped. 464 * for it, they can be properly equipped.
470 */ 465 */
471 memcpy (&at->clone.body_used, &op->body_info, sizeof (op->body_info)); 466 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info));
472 467
473 switch (i) 468 switch (i)
474 { 469 {
475 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 */
476 if (last_more != NULL) 471 if (last_more != NULL)
477 last_more->next = at; 472 last_more->next = at;
478 if (head != NULL) 473 if (head != NULL)
479 head->next = at; 474 head->next = at;
480 head = last_more = at; 475 head = last_more = at;
481#if 0 476#if 0
482 if (!op->type) 477 if (!op->type)
483 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);
484#endif 479#endif
485 at->tail_x = 0; 480 at->tail_x = 0;
486 at->tail_y = 0; 481 at->tail_y = 0;
487 break; 482 break;
488 483
489 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 */
490 485
491 at->head = head; 486 at->head = head;
492 at->clone.head = &head->clone; 487 at->clone.head = &head->clone;
493 if (last_more != NULL) 488 if (last_more != NULL)
494 { 489 {
495 last_more->more = at; 490 last_more->more = at;
496 last_more->clone.more = &at->clone; 491 last_more->clone.more = &at->clone;
497 } 492 }
498 last_more = at; 493 last_more = at;
499 494
500 /* If this multipart image is still composed of individual small 495 /* If this multipart image is still composed of individual small
501 * 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,
502 * 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
503 * 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.
504 */ 499 */
505 if (at->clone.face != head->clone.face) 500 if (at->clone.face != head->clone.face)
506 { 501 {
507 head->tail_x = 0; 502 head->tail_x = 0;
508 head->tail_y = 0; 503 head->tail_y = 0;
509 } 504 }
510 else 505 else
511 { 506 {
512 if (at->clone.x > head->tail_x) 507 if (at->clone.x > head->tail_x)
513 head->tail_x = at->clone.x; 508 head->tail_x = at->clone.x;
514 if (at->clone.y > head->tail_y) 509 if (at->clone.y > head->tail_y)
515 head->tail_y = at->clone.y; 510 head->tail_y = at->clone.y;
516 } 511 }
517 break; 512 break;
518 513
519 } 514 }
520 515
521 at = get_archetype_struct (); 516 at = new archetype;
522 clear_object (op); 517
523 op->arch = at; 518 at->clone.arch = at;
524 } 519 }
525 520
526 delete 521 delete at;
527 at;
528 free_object (op);
529} 522}
530 523
531/* 524/*
532 * Reads the archetype file once more, and links all pointers between 525 * Reads the archetype file once more, and links all pointers between
533 * archetypes. 526 * archetypes.
575 { 568 {
576 if (at != NULL) 569 if (at != NULL)
577 { 570 {
578 treasurelist * 571 treasurelist *
579 tl = find_treasurelist (argument); 572 tl = find_treasurelist (argument);
573
580 if (tl == NULL) 574 if (tl == NULL)
581 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);
582 else 576 else
583 at->clone.randomitems = tl; 577 at->clone.randomitems = tl;
584 } 578 }
590void 584void
591check_generators (void) 585check_generators (void)
592{ 586{
593 archetype * 587 archetype *
594 at; 588 at;
589
595 for (at = first_archetype; at != NULL; at = at->next) 590 for (at = first_archetype; at != NULL; at = at->next)
596 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)
597 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);
598} 593}
599#endif 594#endif
608void 603void
609load_archetypes (void) 604load_archetypes (void)
610{ 605{
611 char 606 char
612 filename[MAX_BUF]; 607 filename[MAX_BUF];
613 int 608
614 comp;
615#if TIME_ARCH_LOAD 609#if TIME_ARCH_LOAD
616 struct timeval 610 struct timeval
617 tv1, 611 tv1,
618 tv2; 612 tv2;
619#endif 613#endif
654 * 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.
655 * This function returns NULL on failure. 649 * This function returns NULL on failure.
656 */ 650 */
657 651
658object * 652object *
659arch_to_object (archetype * at) 653arch_to_object (archetype *at)
660{ 654{
661 object * 655 object *
662 op; 656 op;
657
663 if (at == NULL) 658 if (at == NULL)
664 { 659 {
665 if (warn_archetypes) 660 if (warn_archetypes)
666 LOG (llevError, "Couldn't find archetype.\n"); 661 LOG (llevError, "Couldn't find archetype.\n");
662
667 return NULL; 663 return NULL;
668 } 664 }
665
669 op = get_object (); 666 op = get_object ();
670 copy_object (&at->clone, op); 667 copy_object (&at->clone, op);
668 op->arch = at;
671 op->instantiate (); 669 op->instantiate ();
672 op->arch = at;
673 return op; 670 return op;
674} 671}
675 672
676/* 673/*
677 * Creates an object. This function is called by get_archetype() 674 * Creates an object. This function is called by get_archetype()
685{ 682{
686 object * 683 object *
687 op; 684 op;
688 char 685 char
689 buf[MAX_BUF]; 686 buf[MAX_BUF];
687
690 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 688 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
691 op = get_object (); 689 op = get_object ();
692 op->name = op->name_pl = buf; 690 op->name = op->name_pl = buf;
693 SET_FLAG (op, FLAG_NO_PICK); 691 SET_FLAG (op, FLAG_NO_PICK);
694 return op; 692 return op;
702object * 700object *
703get_archetype (const char *name) 701get_archetype (const char *name)
704{ 702{
705 archetype * 703 archetype *
706 at; 704 at;
705
707 at = find_archetype (name); 706 at = find_archetype (name);
708 if (at == NULL) 707 if (at == NULL)
709 return create_singularity (name); 708 return create_singularity (name);
709
710 return arch_to_object (at); 710 return arch_to_object (at);
711} 711}
712 712
713/* 713/*
714 * Hash-function used by the arch-hashtable. 714 * Hash-function used by the arch-hashtable.
755 name = shstr::find (name); 755 name = shstr::find (name);
756 756
757 if (!name) 757 if (!name)
758 return 0; 758 return 0;
759 759
760 HT::const_iterator i = ht.find ((size_t)name); 760 HT::const_iterator i = ht.find ((size_t) name);
761 761
762 if (i == ht.end ()) 762 if (i == ht.end ())
763 return 0; 763 return 0;
764 else 764 else
765 return i->second; 765 return i->second;
766#endif 766#endif
767 767
768 archetype *at; 768 archetype *
769 at;
769 unsigned long index; 770 unsigned long
771 index;
770 772
771 if (name == NULL) 773 if (name == NULL)
772 return (archetype *) NULL; 774 return (archetype *) NULL;
773 775
774 index = hasharch (name, ARCHTABLE); 776 index = hasharch (name, ARCHTABLE);
796 798
797static void 799static void
798add_arch (archetype *at) 800add_arch (archetype *at)
799{ 801{
800#if USE_UNORDERED_MAP 802#if USE_UNORDERED_MAP
801 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));
802#endif 804#endif
803 805
806 int
804 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 807 index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index;
805 808
806 for (;;) 809 for (;;)
807 { 810 {
808 if (arch_table[index] == NULL) 811 if (arch_table[index] == NULL)
809 { 812 {
864/* 867/*
865 * member: make instance from class 868 * member: make instance from class
866 */ 869 */
867 870
868object * 871object *
869object_create_arch (archetype * at) 872object_create_arch (archetype *at)
870{ 873{
871 object * 874 object *
872 op, * 875 op, *
873 prev = NULL, *head = NULL; 876 prev = NULL, *head = NULL;
874 877

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines