ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/object.C
(Generate patch)

Comparing deliantra/server/common/object.C (file contents):
Revision 1.10 by root, Thu Aug 31 17:54:14 2006 UTC vs.
Revision 1.15 by root, Mon Sep 4 13:55:54 2006 UTC

1/* 1/*
2 * static char *rcsid_object_c = 2 * static char *rcsid_object_c =
3 * "$Id: object.C,v 1.10 2006/08/31 17:54:14 root Exp $"; 3 * "$Id: object.C,v 1.15 2006/09/04 13:55:54 root Exp $";
4 */ 4 */
5 5
6/* 6/*
7 CrossFire, A Multiplayer game for X-windows 7 CrossFire, A Multiplayer game for X-windows
8 8
37#endif /* win32 */ 37#endif /* win32 */
38#include <object.h> 38#include <object.h>
39#include <funcpoint.h> 39#include <funcpoint.h>
40#include <skills.h> 40#include <skills.h>
41#include <loader.h> 41#include <loader.h>
42#ifdef MEMORY_DEBUG
43int nroffreeobjects = 0;
44int nrofallocobjects = 0; 42int nrofallocobjects = 0;
45#undef OBJ_EXPAND
46#define OBJ_EXPAND 1
47#else
48object objarray[STARTMAX]; /* All objects, allocated this way at first */
49int nroffreeobjects = STARTMAX; /* How many OBs allocated and free (free) */
50int nrofallocobjects = STARTMAX; /* How many OBs allocated (free + used) */
51#endif
52 43
53object *objects; /* Pointer to the list of used objects */ 44object *objects; /* Pointer to the list of used objects */
54object *free_objects; /* Pointer to the list of unused objects */
55object *active_objects; /* List of active objects that need to be processed */ 45object *active_objects; /* List of active objects that need to be processed */
56 46
57short freearr_x[SIZEOFFREE]= 47short freearr_x[SIZEOFFREE]=
58 {0,0,1,1,1,0,-1,-1,-1,0,1,2,2,2,2,2,1,0,-1,-2,-2,-2,-2,-2,-1, 48 {0,0,1,1,1,0,-1,-1,-1,0,1,2,2,2,2,2,1,0,-1,-2,-2,-2,-2,-2,-1,
59 0,1,2,3,3,3,3,3,3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1}; 49 0,1,2,3,3,3,3,3,3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1};
60short freearr_y[SIZEOFFREE]= 50short freearr_y[SIZEOFFREE]=
276 return op; 266 return op;
277} 267}
278 268
279/* 269/*
280 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. 270 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
281 * Some error messages. 271 * Some error messages.
282 * The result of the dump is stored in the static global errmsg array. 272 * The result of the dump is stored in the static global errmsg array.
283 */ 273 */
284 274
285void dump_object2(object *op) { 275void dump_object2(object *op) {
286errmsg[0] = 0; 276errmsg[0] = 0;
379 * Used only by the patch command, but not all that useful. 369 * Used only by the patch command, but not all that useful.
380 * Enables features like "patch <name-of-other-player> food 999" 370 * Enables features like "patch <name-of-other-player> food 999"
381 */ 371 */
382 372
383object *find_object_name(const char *str) { 373object *find_object_name(const char *str) {
384 const char *name=add_string(str); 374 const char *name = shstr::find (str);
385 object *op; 375 object *op;
386 for(op=objects;op!=NULL;op=op->next) 376 for(op=objects;op!=NULL;op=op->next)
387 if(op->name==name) 377 if(&op->name == name)
388 break; 378 break;
389 free_string(name); 379
390 return op; 380 return op;
391} 381}
392 382
393void free_all_object_data(void) { 383void free_all_object_data ()
394#ifdef MEMORY_DEBUG 384{
395 object *op, *next; 385 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects);
396
397 for (op=free_objects; op!=NULL; ) {
398 next=op->next;
399 free(op);
400 nrofallocobjects--;
401 nroffreeobjects--;
402 op=next;
403 }
404#endif
405 LOG(llevDebug,"%d allocated objects, %d free objects, STARMAX=%d\n",
406 nrofallocobjects, nroffreeobjects,STARTMAX);
407} 386}
408 387
409/* 388/*
410 * Returns the object which this object marks as being the owner. 389 * Returns the object which this object marks as being the owner.
411 * A id-scheme is used to avoid pointing to objects which have been 390 * A id-scheme is used to avoid pointing to objects which have been
438 op->owner->refcount--; 417 op->owner->refcount--;
439 418
440 op->owner = NULL; 419 op->owner = NULL;
441 op->ownercount = 0; 420 op->ownercount = 0;
442} 421}
443
444
445 422
446/* 423/*
447 * Sets the owner and sets the skill and exp pointers to owner's current 424 * Sets the owner and sets the skill and exp pointers to owner's current
448 * skill and experience objects. 425 * skill and experience objects.
449 */ 426 */
496 } 473 }
497 set_owner(op, owner); 474 set_owner(op, owner);
498 475
499} 476}
500 477
501/*
502 * Resets vital variables in an object
503 */
504
505void reset_object(object *op) {
506
507 op->name = NULL;
508 op->name_pl = NULL;
509 op->title = NULL;
510 op->race = NULL;
511 op->slaying = NULL;
512 op->skill = NULL;
513 op->msg = NULL;
514 op->materialname = NULL;
515 op->lore = NULL;
516 clear_object(op);
517}
518
519/* Zero the key_values on op, decrementing the shared-string 478/* Zero the key_values on op, decrementing the shared-string
520 * refcounts and freeing the links. 479 * refcounts and freeing the links.
521 */ 480 */
522static void free_key_values(object * op) { 481static void free_key_values(object * op)
523 key_value * i; 482{
524 key_value * next = NULL; 483 for (key_value *i = op->key_values; i != 0; )
525 484 {
526 if (op->key_values == NULL) return; 485 key_value *next = i->next;
486 delete i;
487 i = next;
527 488 }
528 for (i = op->key_values; i != NULL; i = next) { 489
529 /* Store next *first*. */
530 next = i->next;
531
532 if (i->key) FREE_AND_CLEAR_STR(i->key);
533 if (i->value) FREE_AND_CLEAR_STR(i->value);
534 i->next = NULL;
535 free(i);
536 }
537
538 op->key_values = NULL; 490 op->key_values = 0;
539} 491}
540 492
493void object::clear ()
494{
495 attachable_base::clear ();
496
497 free_key_values (this);
498
499 name = 0;
500 name_pl = 0;
501 title = 0;
502 race = 0;
503 slaying = 0;
504 skill = 0;
505 msg = 0;
506 lore = 0;
507 custom_name = 0;
508 materialname = 0;
509
510 memset (static_cast<object_pod *>(this), 0, sizeof (object_pod));
511
512 SET_FLAG (this, FLAG_REMOVED);
513}
514
515void object::clone (object *destination)
516{
517 *(object_copy *)destination = *(object_copy *)this;
518 *(object_pod *)destination = *(object_pod *)this;
519
520 if (self || cb)
521 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination));
522}
541 523
542/* 524/*
543 * clear_object() frees everything allocated by an object, and also 525 * clear_object() frees everything allocated by an object, and also
544 * clears all variables and flags to default settings. 526 * clears all variables and flags to default settings.
545 */ 527 */
546 528
547void clear_object(object *op) { 529void clear_object (object *op)
530{
548 op->clear (); 531 op->clear ();
549 532
550 event *evt;
551 event *evt2;
552
553 /* redo this to be simpler/more efficient. Was also seeing
554 * crashes in the old code. Move this to the top - am
555 * seeing periodic crashes in this code, and would like to have
556 * as much info available as possible (eg, object name).
557 */
558 for (evt = op->events; evt; evt=evt2) {
559 evt2 = evt->next;
560
561 if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
562 if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
563 if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
564
565 free(evt);
566 }
567 op->events = NULL;
568
569 free_key_values(op);
570
571 /* the memset will clear all these values for us, but we need
572 * to reduce the refcount on them.
573 */
574 if (op->name!=NULL) FREE_AND_CLEAR_STR(op->name);
575 if (op->name_pl!=NULL) FREE_AND_CLEAR_STR(op->name_pl);
576 if (op->title != NULL) FREE_AND_CLEAR_STR(op->title);
577 if (op->race!=NULL) FREE_AND_CLEAR_STR(op->race);
578 if (op->slaying!=NULL) FREE_AND_CLEAR_STR(op->slaying);
579 if (op->skill!=NULL) FREE_AND_CLEAR_STR(op->skill);
580 if (op->msg!=NULL) FREE_AND_CLEAR_STR(op->msg);
581 if (op->lore!=NULL) FREE_AND_CLEAR_STR(op->lore);
582 if (op->materialname!= NULL) FREE_AND_CLEAR_STR(op->materialname);
583
584
585 memset((void*)op, 0, sizeof (object_special));
586 /* Below here, we clear things that are not done by the memset,
587 * or set default values that are not zero.
588 */
589 /* This is more or less true */
590 SET_FLAG(op, FLAG_REMOVED);
591
592
593 op->contr = NULL; 533 op->contr = NULL;
594 op->below=NULL; 534 op->below = NULL;
595 op->above=NULL; 535 op->above = NULL;
596 op->inv=NULL; 536 op->inv = NULL;
597 op->events=NULL;
598 op->container=NULL; 537 op->container=NULL;
599 op->env=NULL; 538 op->env=NULL;
600 op->more=NULL; 539 op->more=NULL;
601 op->head=NULL; 540 op->head=NULL;
602 op->map=NULL; 541 op->map=NULL;
603 op->refcount=0; 542 op->refcount=0;
604 op->active_next = NULL; 543 op->active_next = NULL;
605 op->active_prev = NULL; 544 op->active_prev = NULL;
606 /* What is not cleared is next, prev, and count */ 545 /* What is not cleared is next, prev, and count */
607 546
608 op->expmul=1.0; 547 op->expmul = 1.0;
609 op->face = blank_face; 548 op->face = blank_face;
610 op->attacked_by_count = (tag_t) -1; 549 op->attacked_by_count = -1;
550
611 if (settings.casting_time) 551 if (settings.casting_time)
612 op->casting_time = -1; 552 op->casting_time = -1;
613
614} 553}
615 554
616/* 555/*
617 * copy object first frees everything allocated by the second object, 556 * copy object first frees everything allocated by the second object,
618 * and then copies the contends of the first object into the second 557 * and then copies the contends of the first object into the second
620 * data that is malloc'd needs to be re-malloc/copied. Otherwise, 559 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
621 * if the first object is freed, the pointers in the new object 560 * if the first object is freed, the pointers in the new object
622 * will point at garbage. 561 * will point at garbage.
623 */ 562 */
624 563
625void copy_object(object *op2, object *op) { 564void copy_object (object *op2, object *op)
626 int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); 565{
627 event *evt, *evt2, *evt_new; 566 bool is_freed = QUERY_FLAG (op, FLAG_FREED);
567 bool is_removed = QUERY_FLAG (op, FLAG_REMOVED);
628 568
629 op->clear ();
630
631 /* Decrement the refcounts, but don't bother zeroing the fields;
632 they'll be overwritten by memcpy. */
633 if(op->name!=NULL) free_string(op->name);
634 if(op->name_pl!=NULL) free_string(op->name_pl);
635 if(op->title!=NULL) free_string(op->title);
636 if(op->race!=NULL) free_string(op->race);
637 if(op->slaying!=NULL) free_string(op->slaying);
638 if(op->skill!=NULL) free_string(op->skill);
639 if(op->msg!=NULL) free_string(op->msg);
640 if(op->lore!=NULL) free_string(op->lore);
641 if(op->materialname != NULL) free_string(op->materialname);
642 if(op->custom_name != NULL) free_string(op->custom_name);
643
644 /* Basically, same code as from clear_object() */
645 for (evt = op->events; evt; evt=evt2) {
646 evt2 = evt->next;
647
648 if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
649 if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
650 if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
651
652 free(evt);
653 }
654 op->events = NULL;
655
656 free_key_values(op);
657
658 *(object_special *)op = *(object_special *)op2;
659 op2->clone (op); 569 op2->clone (op);
660 570
661 if(is_freed) SET_FLAG(op,FLAG_FREED); 571 if (is_freed) SET_FLAG (op, FLAG_FREED);
662 if(is_removed) SET_FLAG(op,FLAG_REMOVED); 572 if (is_removed) SET_FLAG (op, FLAG_REMOVED);
663 if(op->name!=NULL) add_refcount(op->name);
664 if(op->name_pl!=NULL) add_refcount(op->name_pl);
665 if(op->title!=NULL) add_refcount(op->title);
666 if(op->race!=NULL) add_refcount(op->race);
667 if(op->slaying!=NULL) add_refcount(op->slaying);
668 if(op->skill!=NULL) add_refcount(op->skill);
669 if(op->lore!=NULL) add_refcount(op->lore);
670 if(op->msg!=NULL) add_refcount(op->msg);
671 if(op->custom_name!=NULL) add_refcount(op->custom_name);
672 if (op->materialname != NULL) add_refcount(op->materialname);
673 573
674 if((op2->speed<0) && !editor) 574 if (op2->speed < 0)
675 op->speed_left=op2->speed_left-RANDOM()%200/100.0; 575 op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0;
676 576
677 /* Copy over event information */
678 evt2 = NULL;
679 for (evt = op2->events; evt; evt=evt->next) {
680 evt_new = (event *) malloc(sizeof(event));
681 memcpy(evt_new, evt, sizeof(event));
682 if (evt_new->hook) add_refcount(evt_new->hook);
683 if (evt_new->plugin) add_refcount(evt_new->plugin);
684 if (evt_new->options) add_refcount(evt_new->options);
685 evt_new->next = NULL;
686
687 /* Try to be a little clever here, and store away the
688 * last event we copied, so that its simpler to update the
689 * pointer.
690 */
691 if (evt2)
692 evt2->next = evt_new;
693 else
694 op->events = evt_new;
695
696 evt2 = evt_new;
697 }
698 /* Copy over key_values, if any. */ 577 /* Copy over key_values, if any. */
699 if (op2->key_values != NULL) { 578 if (op2->key_values != NULL)
579 {
700 key_value * tail = NULL; 580 key_value *tail = NULL;
701 key_value * i; 581 key_value *i;
702 582
703 op->key_values = NULL; 583 op->key_values = NULL;
704 584
705 for (i = op2->key_values; i != NULL; i = i->next) { 585 for (i = op2->key_values; i != NULL; i = i->next)
706 key_value * new_link = (key_value *) malloc(sizeof(key_value)); 586 {
587 key_value *new_link = new key_value;
707 588
708 new_link->next = NULL; 589 new_link->next = NULL;
709 new_link->key = add_refcount(i->key); 590 new_link->key = i->key;
710 if (i->value)
711 new_link->value = add_refcount(i->value); 591 new_link->value = i->value;
712 else
713 new_link->value = NULL;
714 592
715 /* Try and be clever here, too. */ 593 /* Try and be clever here, too. */
716 if (op->key_values == NULL) { 594 if (op->key_values == NULL)
595 {
717 op->key_values = new_link; 596 op->key_values = new_link;
718 tail = new_link; 597 tail = new_link;
598 }
719 } else { 599 else
600 {
720 tail->next = new_link; 601 tail->next = new_link;
721 tail = new_link; 602 tail = new_link;
722 } 603 }
723 } 604 }
724 } 605 }
725 606
726 update_ob_speed(op); 607 update_ob_speed (op);
727}
728
729/*
730 * expand_objects() allocates more objects for the list of unused objects.
731 * It is called from get_object() if the unused list is empty.
732 */
733
734void expand_objects(void) {
735 int i;
736 object *obj;
737 obj = (object *) CALLOC(OBJ_EXPAND,sizeof(object));
738
739 if(obj==NULL)
740 fatal(OUT_OF_MEMORY);
741 free_objects=obj;
742 obj[0].prev=NULL;
743 obj[0].next= &obj[1],
744 SET_FLAG(&(obj[0]), FLAG_REMOVED);
745 SET_FLAG(&(obj[0]), FLAG_FREED);
746
747 for(i=1;i<OBJ_EXPAND-1;i++) {
748 obj[i].next= &obj[i+1],
749 obj[i].prev= &obj[i-1],
750 SET_FLAG(&(obj[i]), FLAG_REMOVED);
751 SET_FLAG(&(obj[i]), FLAG_FREED);
752 }
753 obj[OBJ_EXPAND-1].prev= &obj[OBJ_EXPAND-2],
754 obj[OBJ_EXPAND-1].next=NULL,
755 SET_FLAG(&(obj[OBJ_EXPAND-1]), FLAG_REMOVED);
756 SET_FLAG(&(obj[OBJ_EXPAND-1]), FLAG_FREED);
757
758 nrofallocobjects += OBJ_EXPAND;
759 nroffreeobjects += OBJ_EXPAND;
760} 608}
761 609
762/* 610/*
763 * get_object() grabs an object from the list of unused objects, makes 611 * get_object() grabs an object from the list of unused objects, makes
764 * sure it is initialised, and returns it. 612 * sure it is initialised, and returns it.
765 * If there are no free objects, expand_objects() is called to get more. 613 * If there are no free objects, expand_objects() is called to get more.
766 */ 614 */
767 615
768object *get_object(void) { 616object *get_object ()
617{
618 object *op = new object;
619
620 op->count = ++ob_count;
621
622 op->active_next = 0;
623 op->active_prev = 0;
624
625 op->next = objects;
626 op->prev = 0;
627
628 if (objects)
629 objects->prev = op;
630
769 object *op; 631 objects = op;
770 632
771 if(free_objects==NULL) {
772 expand_objects();
773 }
774 op=free_objects;
775#ifdef MEMORY_DEBUG
776 /* The idea is hopefully by doing a realloc, the memory
777 * debugging program will now use the current stack trace to
778 * report leaks.
779 */
780 op = realloc(op, sizeof(object));
781 SET_FLAG(op, FLAG_REMOVED); 633 SET_FLAG (op, FLAG_REMOVED);
782 SET_FLAG(op, FLAG_FREED);
783#endif
784 634
785 if(!QUERY_FLAG(op,FLAG_FREED)) { 635 op->expmul = 1.0;
786 LOG(llevError,"Fatal: Getting busy object.\n"); 636 op->face = blank_face;
787 } 637 op->attacked_by_count = -1;
788 free_objects=op->next; 638
789 if(free_objects!=NULL)
790 free_objects->prev=NULL;
791 op->count= ++ob_count;
792 op->name=NULL;
793 op->name_pl=NULL;
794 op->title=NULL;
795 op->race=NULL;
796 op->slaying=NULL;
797 op->skill = NULL;
798 op->lore=NULL;
799 op->msg=NULL;
800 op->materialname=NULL;
801 op->next=objects;
802 op->prev=NULL;
803 op->active_next = NULL;
804 op->active_prev = NULL;
805 if(objects!=NULL)
806 objects->prev=op;
807 objects=op;
808 clear_object(op);
809 SET_FLAG(op,FLAG_REMOVED);
810 nroffreeobjects--;
811 return op; 639 return op;
812} 640}
813 641
814/* 642/*
815 * If an object with the IS_TURNABLE() flag needs to be turned due 643 * If an object with the IS_TURNABLE() flag needs to be turned due
836 /* No reason putting the archetypes objects on the speed list, 664 /* No reason putting the archetypes objects on the speed list,
837 * since they never really need to be updated. 665 * since they never really need to be updated.
838 */ 666 */
839 667
840 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) { 668 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
841 LOG(llevError,"Object %s is freed but has speed.\n", op->name); 669 LOG(llevError,"Object %s is freed but has speed.\n", &op->name);
842#ifdef MANY_CORES 670#ifdef MANY_CORES
843 abort(); 671 abort();
844#else 672#else
845 op->speed = 0; 673 op->speed = 0;
846#endif 674#endif
930 * UP_OBJ_FACE: only the objects face has changed. 758 * UP_OBJ_FACE: only the objects face has changed.
931 */ 759 */
932 760
933void update_object(object *op, int action) { 761void update_object(object *op, int action) {
934 int update_now=0, flags; 762 int update_now=0, flags;
935 MoveType move_on, move_off, move_block, move_slow; 763 MoveType move_on, move_off, move_block, move_slow;
936 764
937 if (op == NULL) { 765 if (op == NULL) {
938 /* this should never happen */ 766 /* this should never happen */
939 LOG(llevDebug,"update_object() called for NULL object.\n"); 767 LOG(llevDebug,"update_object() called for NULL object.\n");
940 return; 768 return;
1023 * 851 *
1024 * If free_inventory is set, free inventory as well. Else drop items in 852 * If free_inventory is set, free inventory as well. Else drop items in
1025 * inventory to the ground. 853 * inventory to the ground.
1026 */ 854 */
1027 855
856void
1028void free_object(object *ob) { 857free_object (object * ob)
858{
1029 free_object2(ob, 0); 859 free_object2 (ob, 0);
1030} 860}
861
862void
1031void free_object2(object *ob, int free_inventory) { 863free_object2 (object * ob, int free_inventory)
864{
1032 object *tmp,*op; 865 object *tmp, *op;
1033 866
1034 ob->clear ();
1035
1036 if (!QUERY_FLAG(ob,FLAG_REMOVED)) { 867 if (!QUERY_FLAG (ob, FLAG_REMOVED))
868 {
1037 LOG(llevDebug,"Free object called with non removed object\n"); 869 LOG (llevDebug, "Free object called with non removed object\n");
1038 dump_object(ob); 870 dump_object (ob);
1039#ifdef MANY_CORES 871#ifdef MANY_CORES
1040 abort(); 872 abort ();
1041#endif 873#endif
1042 } 874 }
875
1043 if(QUERY_FLAG(ob,FLAG_FRIENDLY)) { 876 if (QUERY_FLAG (ob, FLAG_FRIENDLY))
877 {
1044 LOG(llevMonster,"Warning: tried to free friendly object.\n"); 878 LOG (llevMonster, "Warning: tried to free friendly object.\n");
1045 remove_friendly_object(ob); 879 remove_friendly_object (ob);
1046 } 880 }
881
1047 if(QUERY_FLAG(ob,FLAG_FREED)) { 882 if (QUERY_FLAG (ob, FLAG_FREED))
883 {
1048 dump_object(ob); 884 dump_object (ob);
1049 LOG(llevError,"Trying to free freed object.\n%s\n",errmsg); 885 LOG (llevError, "Trying to free freed object.\n%s\n", errmsg);
1050 return; 886 return;
1051 } 887 }
888
1052 if(ob->more!=NULL) { 889 if (ob->more != NULL)
890 {
1053 free_object2(ob->more, free_inventory); 891 free_object2 (ob->more, free_inventory);
1054 ob->more=NULL; 892 ob->more = NULL;
1055 } 893 }
894
1056 if (ob->inv) { 895 if (ob->inv)
896 {
1057 /* Only if the space blocks everything do we not process - 897 /* Only if the space blocks everything do we not process -
1058 * if some form of movemnt is allowed, let objects 898 * if some form of movemnt is allowed, let objects
1059 * drop on that space. 899 * drop on that space.
1060 */ 900 */
1061 if (free_inventory || ob->map==NULL || ob->map->in_memory!=MAP_IN_MEMORY || 901 if (free_inventory || ob->map == NULL
902 || ob->map->in_memory != MAP_IN_MEMORY
1062 (GET_MAP_MOVE_BLOCK(ob->map, ob->x, ob->y) == MOVE_ALL)) 903 || (GET_MAP_MOVE_BLOCK (ob->map, ob->x, ob->y) == MOVE_ALL))
1063 { 904 {
1064 op=ob->inv; 905 op = ob->inv;
1065 while(op!=NULL) { 906
907 while (op != NULL)
908 {
1066 tmp=op->below; 909 tmp = op->below;
1067 remove_ob(op); 910 remove_ob (op);
1068 free_object2(op, free_inventory); 911 free_object2 (op, free_inventory);
1069 op=tmp; 912 op = tmp;
1070 }
1071 }
1072 else { /* Put objects in inventory onto this space */
1073 op=ob->inv;
1074 while(op!=NULL) {
1075 tmp=op->below;
1076 remove_ob(op);
1077 if(QUERY_FLAG(op,FLAG_STARTEQUIP)||QUERY_FLAG(op,FLAG_NO_DROP) ||
1078 op->type==RUNE || op->type==TRAP || QUERY_FLAG(op,FLAG_IS_A_TEMPLATE))
1079 free_object(op);
1080 else {
1081 op->x=ob->x;
1082 op->y=ob->y;
1083 insert_ob_in_map(op,ob->map,NULL,0); /* Insert in same map as the envir */
1084 }
1085 op=tmp;
1086 }
1087 }
1088 } 913 }
914 }
915 else
916 { /* Put objects in inventory onto this space */
917 op = ob->inv;
918
919 while (op != NULL)
920 {
921 tmp = op->below;
922 remove_ob (op);
923
924 if (QUERY_FLAG (op, FLAG_STARTEQUIP)
925 || QUERY_FLAG (op, FLAG_NO_DROP) || op->type == RUNE
926 || op->type == TRAP || QUERY_FLAG (op, FLAG_IS_A_TEMPLATE))
927 free_object (op);
928 else
929 {
930 op->x = ob->x;
931 op->y = ob->y;
932 insert_ob_in_map (op, ob->map, NULL, 0); /* Insert in same map as the envir */
933 }
934
935 op = tmp;
936 }
937 }
938 }
939
1089 /* Remove object from the active list */ 940 /* Remove object from the active list */
1090 ob->speed = 0; 941 ob->speed = 0;
1091 update_ob_speed(ob); 942 update_ob_speed (ob);
1092 943
1093 SET_FLAG(ob, FLAG_FREED); 944 SET_FLAG (ob, FLAG_FREED);
1094 ob->count = 0; 945 ob->count = 0;
1095 946
1096 /* Remove this object from the list of used objects */ 947 /* Remove this object from the list of used objects */
1097 if(ob->prev==NULL) { 948 if (ob->prev == NULL)
949 {
1098 objects=ob->next; 950 objects = ob->next;
951
1099 if(objects!=NULL) 952 if (objects != NULL)
1100 objects->prev=NULL; 953 objects->prev = NULL;
954 }
955 else
1101 } 956 {
1102 else {
1103 ob->prev->next=ob->next; 957 ob->prev->next = ob->next;
958
1104 if(ob->next!=NULL) 959 if (ob->next != NULL)
1105 ob->next->prev=ob->prev; 960 ob->next->prev = ob->prev;
1106 }
1107
1108 if(ob->name!=NULL) FREE_AND_CLEAR_STR(ob->name);
1109 if(ob->name_pl!=NULL) FREE_AND_CLEAR_STR(ob->name_pl);
1110 if(ob->title!=NULL) FREE_AND_CLEAR_STR(ob->title);
1111 if(ob->race!=NULL) FREE_AND_CLEAR_STR(ob->race);
1112 if(ob->slaying!=NULL) FREE_AND_CLEAR_STR(ob->slaying);
1113 if(ob->skill!=NULL) FREE_AND_CLEAR_STR(ob->skill);
1114 if(ob->lore!=NULL) FREE_AND_CLEAR_STR(ob->lore);
1115 if(ob->msg!=NULL) FREE_AND_CLEAR_STR(ob->msg);
1116 if(ob->materialname!=NULL) FREE_AND_CLEAR_STR(ob->materialname);
1117
1118 961 }
1119 /* Why aren't events freed? */ 962
1120 free_key_values(ob); 963 free_key_values (ob);
1121 964
1122#if 0 /* MEMORY_DEBUG*/
1123 /* This is a nice idea. Unfortunately, a lot of the code in crossfire
1124 * presumes the freed_object will stick around for at least a little
1125 * bit
1126 */
1127 /* this is necessary so that memory debugging programs will
1128 * be able to accurately report source of malloc. If we recycle
1129 * objects, then some other area may be doing the get_object
1130 * and not freeing it, but the original one that malloc'd the
1131 * object will get the blame.
1132 */
1133 free(ob);
1134#else
1135
1136 /* Now link it with the free_objects list: */ 965 /* Now link it with the free_objects list: */
1137 ob->prev=NULL; 966 ob->prev = 0;
1138 ob->next=free_objects; 967 ob->next = 0;
1139 if(free_objects!=NULL)
1140 free_objects->prev=ob;
1141 free_objects=ob;
1142 nroffreeobjects++;
1143#endif
1144}
1145 968
1146/* 969 delete ob;
1147 * count_free() returns the number of objects on the list of free objects.
1148 */
1149
1150int count_free(void) {
1151 int i=0;
1152 object *tmp=free_objects;
1153 while(tmp!=NULL)
1154 tmp=tmp->next, i++;
1155 return i;
1156}
1157
1158/*
1159 * count_used() returns the number of objects on the list of used objects.
1160 */
1161
1162int count_used(void) {
1163 int i=0;
1164 object *tmp=objects;
1165 while(tmp!=NULL)
1166 tmp=tmp->next, i++;
1167 return i;
1168}
1169
1170/*
1171 * count_active() returns the number of objects on the list of active objects.
1172 */
1173
1174int count_active(void) {
1175 int i=0;
1176 object *tmp=active_objects;
1177 while(tmp!=NULL)
1178 tmp=tmp->active_next, i++;
1179 return i;
1180} 970}
1181 971
1182/* 972/*
1183 * sub_weight() recursively (outwards) subtracts a number from the 973 * sub_weight() recursively (outwards) subtracts a number from the
1184 * weight of an object (and what is carried by it's environment(s)). 974 * weight of an object (and what is carried by it's environment(s)).
1262 /* we set up values so that it could be inserted into 1052 /* we set up values so that it could be inserted into
1263 * the map, but we don't actually do that - it is up 1053 * the map, but we don't actually do that - it is up
1264 * to the caller to decide what we want to do. 1054 * to the caller to decide what we want to do.
1265 */ 1055 */
1266 op->x=op->env->x,op->y=op->env->y; 1056 op->x=op->env->x,op->y=op->env->y;
1267 op->ox=op->x,op->oy=op->y;
1268 op->map=op->env->map; 1057 op->map=op->env->map;
1269 op->above=NULL,op->below=NULL; 1058 op->above=NULL,op->below=NULL;
1270 op->env=NULL; 1059 op->env=NULL;
1271 return; 1060 return;
1272 } 1061 }
1347 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) { 1136 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) {
1348 1137
1349 move_apply(tmp, op, NULL); 1138 move_apply(tmp, op, NULL);
1350 if (was_destroyed (op, tag)) { 1139 if (was_destroyed (op, tag)) {
1351 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " 1140 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed "
1352 "leaving object\n", tmp->name, tmp->arch->name); 1141 "leaving object\n", &tmp->name, &tmp->arch->name);
1353 } 1142 }
1354 } 1143 }
1355 1144
1356 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ 1145 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1357 1146
1483 * we set the map, that doesn't work if the location is within 1272 * we set the map, that doesn't work if the location is within
1484 * a map and this is straddling an edge. So only if coordinate 1273 * a map and this is straddling an edge. So only if coordinate
1485 * is clear wrong do we normalize it. 1274 * is clear wrong do we normalize it.
1486 */ 1275 */
1487 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) { 1276 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) {
1488 /* Debugging information so you can see the last coordinates this object had */
1489 more->ox = more->x;
1490 more->oy = more->y;
1491 more->map = get_map_from_coord(m, &more->x, &more->y); 1277 more->map = get_map_from_coord(m, &more->x, &more->y);
1492 } else if (!more->map) { 1278 } else if (!more->map) {
1493 /* For backwards compatibility - when not dealing with tiled maps, 1279 /* For backwards compatibility - when not dealing with tiled maps,
1494 * more->map should always point to the parent. 1280 * more->map should always point to the parent.
1495 */ 1281 */
1501 LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n"); 1287 LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n");
1502 return NULL; 1288 return NULL;
1503 } 1289 }
1504 } 1290 }
1505 CLEAR_FLAG(op,FLAG_REMOVED); 1291 CLEAR_FLAG(op,FLAG_REMOVED);
1506
1507 /* Debugging information so you can see the last coordinates this object had */
1508 op->ox=op->x;
1509 op->oy=op->y;
1510 1292
1511 /* Ideally, the caller figures this out. However, it complicates a lot 1293 /* Ideally, the caller figures this out. However, it complicates a lot
1512 * of areas of callers (eg, anything that uses find_free_spot would now 1294 * of areas of callers (eg, anything that uses find_free_spot would now
1513 * need extra work 1295 * need extra work
1514 */ 1296 */
1582 * looks like instead of lots of conditions here. 1364 * looks like instead of lots of conditions here.
1583 * makes things faster, and effectively the same result. 1365 * makes things faster, and effectively the same result.
1584 */ 1366 */
1585 1367
1586 /* Have object 'fall below' other objects that block view. 1368 /* Have object 'fall below' other objects that block view.
1587 * Unless those objects are exits, type 66 1369 * Unless those objects are exits, type 66
1588 * If INS_ON_TOP is used, don't do this processing 1370 * If INS_ON_TOP is used, don't do this processing
1589 * Need to find the object that in fact blocks view, otherwise 1371 * Need to find the object that in fact blocks view, otherwise
1590 * stacking is a bit odd. 1372 * stacking is a bit odd.
1591 */ 1373 */
1592 if (!(flag & INS_ON_TOP) && 1374 if (!(flag & INS_ON_TOP) &&
1593 (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) && 1375 (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) &&
1594 (op->face && !op->face->visibility)) { 1376 (op->face && !op->face->visibility)) {
1595 for (last=top; last != floor; last=last->below) 1377 for (last=top; last != floor; last=last->below)
1596 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break; 1378 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break;
1597 /* Check to see if we found the object that blocks view, 1379 /* Check to see if we found the object that blocks view,
1598 * and make sure we have a below pointer for it so that 1380 * and make sure we have a below pointer for it so that
1599 * we can get inserted below this one, which requires we 1381 * we can get inserted below this one, which requires we
1600 * set top to the object below us. 1382 * set top to the object below us.
1601 */ 1383 */
1630 1412
1631 /* If we have a floor, we know the player, if any, will be above 1413 /* If we have a floor, we know the player, if any, will be above
1632 * it, so save a few ticks and start from there. 1414 * it, so save a few ticks and start from there.
1633 */ 1415 */
1634 if (!(flag & INS_MAP_LOAD)) 1416 if (!(flag & INS_MAP_LOAD))
1635 for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) { 1417 for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) {
1636 if (tmp->type == PLAYER) 1418 if (tmp->type == PLAYER)
1637 tmp->contr->socket.update_look=1; 1419 tmp->contr->socket.update_look=1;
1638 } 1420 }
1639 1421
1640 /* If this object glows, it may affect lighting conditions that are 1422 /* If this object glows, it may affect lighting conditions that are
1641 * visible to others on this map. But update_all_los is really 1423 * visible to others on this map. But update_all_los is really
1642 * an inefficient way to do this, as it means los for all players 1424 * an inefficient way to do this, as it means los for all players
1643 * on the map will get recalculated. The players could very well 1425 * on the map will get recalculated. The players could very well
1715 object *newob; 1497 object *newob;
1716 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0); 1498 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1717 1499
1718 if(orig_ob->nrof<nr) { 1500 if(orig_ob->nrof<nr) {
1719 sprintf(errmsg,"There are only %d %ss.", 1501 sprintf(errmsg,"There are only %d %ss.",
1720 orig_ob->nrof?orig_ob->nrof:1, orig_ob->name); 1502 orig_ob->nrof?orig_ob->nrof:1, &orig_ob->name);
1721 return NULL; 1503 return NULL;
1722 } 1504 }
1723 newob = object_create_clone(orig_ob); 1505 newob = object_create_clone(orig_ob);
1724 if((orig_ob->nrof-=nr)<1) { 1506 if((orig_ob->nrof-=nr)<1) {
1725 if ( ! is_removed) 1507 if ( ! is_removed)
1872 "Warning: Tried to insert object wrong part of multipart object.\n"); 1654 "Warning: Tried to insert object wrong part of multipart object.\n");
1873 where = where->head; 1655 where = where->head;
1874 } 1656 }
1875 if (op->more) { 1657 if (op->more) {
1876 LOG(llevError, "Tried to insert multipart object %s (%d)\n", 1658 LOG(llevError, "Tried to insert multipart object %s (%d)\n",
1877 op->name, op->count); 1659 &op->name, op->count);
1878 return op; 1660 return op;
1879 } 1661 }
1880 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL); 1662 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL);
1881 CLEAR_FLAG(op, FLAG_REMOVED); 1663 CLEAR_FLAG(op, FLAG_REMOVED);
1882 if(op->nrof) { 1664 if(op->nrof) {
1916 op->map=NULL; 1698 op->map=NULL;
1917 op->env=where; 1699 op->env=where;
1918 op->above=NULL; 1700 op->above=NULL;
1919 op->below=NULL; 1701 op->below=NULL;
1920 op->x=0,op->y=0; 1702 op->x=0,op->y=0;
1921 op->ox=0,op->oy=0;
1922 1703
1923 /* reset the light list and los of the players on the map */ 1704 /* reset the light list and los of the players on the map */
1924 if((op->glow_radius!=0)&&where->map) 1705 if((op->glow_radius!=0)&&where->map)
1925 { 1706 {
1926#ifdef DEBUG_LIGHTS 1707#ifdef DEBUG_LIGHTS
1968{ 1749{
1969 object *tmp; 1750 object *tmp;
1970 tag_t tag; 1751 tag_t tag;
1971 mapstruct *m=op->map; 1752 mapstruct *m=op->map;
1972 int x=op->x, y=op->y; 1753 int x=op->x, y=op->y;
1973 MoveType move_on, move_slow, move_block; 1754 MoveType move_on, move_slow, move_block;
1974 1755
1975 if(QUERY_FLAG(op,FLAG_NO_APPLY)) 1756 if(QUERY_FLAG(op,FLAG_NO_APPLY))
1976 return 0; 1757 return 0;
1977 1758
1978 tag = op->count; 1759 tag = op->count;
2296int find_dir(mapstruct *m, int x, int y, object *exclude) { 2077int find_dir(mapstruct *m, int x, int y, object *exclude) {
2297 int i,max=SIZEOFFREE, mflags; 2078 int i,max=SIZEOFFREE, mflags;
2298 sint16 nx, ny; 2079 sint16 nx, ny;
2299 object *tmp; 2080 object *tmp;
2300 mapstruct *mp; 2081 mapstruct *mp;
2301 MoveType blocked, move_type; 2082 MoveType blocked, move_type;
2302 2083
2303 if (exclude && exclude->head) { 2084 if (exclude && exclude->head) {
2304 exclude = exclude->head; 2085 exclude = exclude->head;
2305 move_type = exclude->move_type; 2086 move_type = exclude->move_type;
2306 } else { 2087 } else {
2598 fclose(tempfile); 2379 fclose(tempfile);
2599 2380
2600 op=get_object(); 2381 op=get_object();
2601 2382
2602 object_thawer thawer (filename); 2383 object_thawer thawer (filename);
2384
2603 if (thawer) 2385 if (thawer)
2604 load_object(thawer,op,LO_NEWFILE,0); 2386 load_object(thawer,op,0);
2387
2605 LOG(llevDebug," load str completed, object=%s\n",op->name); 2388 LOG(llevDebug," load str completed, object=%s\n", &op->name);
2606 CLEAR_FLAG(op,FLAG_REMOVED); 2389 CLEAR_FLAG(op,FLAG_REMOVED);
2607 2390
2608 return op; 2391 return op;
2609} 2392}
2610 2393
2649 */ 2432 */
2650const char * get_ob_key_value(const object * op, const char * const key) { 2433const char * get_ob_key_value(const object * op, const char * const key) {
2651 key_value * link; 2434 key_value * link;
2652 const char * canonical_key; 2435 const char * canonical_key;
2653 2436
2654 canonical_key = find_string(key); 2437 canonical_key = shstr::find (key);
2655 2438
2656 if (canonical_key == NULL) { 2439 if (canonical_key == NULL) {
2657 /* 1. There being a field named key on any object 2440 /* 1. There being a field named key on any object
2658 * implies there'd be a shared string to find. 2441 * implies there'd be a shared string to find.
2659 * 2. Since there isn't, no object has this field. 2442 * 2. Since there isn't, no object has this field.
2682 * Unless add_key is TRUE, it won't add fields, only change the value of existing 2465 * Unless add_key is TRUE, it won't add fields, only change the value of existing
2683 * keys. 2466 * keys.
2684 * 2467 *
2685 * Returns TRUE on success. 2468 * Returns TRUE on success.
2686 */ 2469 */
2687int set_ob_key_value_s(object * op, const char * canonical_key, const char * value, int add_key) { 2470int set_ob_key_value_s(object * op, const shstr &canonical_key, const char * value, int add_key) {
2688 key_value * field = NULL, *last=NULL; 2471 key_value * field = NULL, *last=NULL;
2689 2472
2690 for (field=op->key_values; field != NULL; field=field->next) { 2473 for (field=op->key_values; field != NULL; field=field->next) {
2691 if (field->key != canonical_key) { 2474 if (field->key != canonical_key) {
2692 last = field; 2475 last = field;
2693 continue; 2476 continue;
2694 } 2477 }
2695 2478
2696 if (field->value) FREE_AND_CLEAR_STR(field->value);
2697 if (value) 2479 if (value)
2698 field->value = add_string(value); 2480 field->value = value;
2699 else { 2481 else {
2700 /* Basically, if the archetype has this key set, 2482 /* Basically, if the archetype has this key set,
2701 * we need to store the null value so when we save 2483 * we need to store the null value so when we save
2702 * it, we save the empty value so that when we load, 2484 * it, we save the empty value so that when we load,
2703 * we get this value back again. 2485 * we get this value back again.
2704 */ 2486 */
2705 if (get_ob_key_link(&op->arch->clone, canonical_key)) 2487 if (get_ob_key_link (&op->arch->clone, canonical_key))
2706 field->value = NULL; 2488 field->value = 0;
2707 else { 2489 else
2708 /* Delete this link */ 2490 {
2709 if (field->key) FREE_AND_CLEAR_STR(field->key);
2710 if (field->value) FREE_AND_CLEAR_STR(field->value);
2711 if (last) last->next = field->next; 2491 if (last) last->next = field->next;
2712 else op->key_values = field->next; 2492 else op->key_values = field->next;
2493
2713 free(field); 2494 delete field;
2714 } 2495 }
2715 } 2496 }
2716 return TRUE; 2497 return TRUE;
2717 } 2498 }
2718 /* IF we get here, key doesn't exist */ 2499 /* IF we get here, key doesn't exist */
2719 2500
2728 * be here. If user wants to store empty strings, 2509 * be here. If user wants to store empty strings,
2729 * should pass in "" 2510 * should pass in ""
2730 */ 2511 */
2731 if (value == NULL) return TRUE; 2512 if (value == NULL) return TRUE;
2732 2513
2733 field = (key_value *) malloc(sizeof(key_value)); 2514 field = new key_value;
2734 2515
2735 field->key = add_refcount(canonical_key); 2516 field->key = canonical_key;
2736 field->value = add_string(value); 2517 field->value = value;
2737 /* Usual prepend-addition. */ 2518 /* Usual prepend-addition. */
2738 field->next = op->key_values; 2519 field->next = op->key_values;
2739 op->key_values = field; 2520 op->key_values = field;
2740 2521
2741 return TRUE; 2522 return TRUE;
2748 * and not add new ones. 2529 * and not add new ones.
2749 * In general, should be little reason FALSE is ever passed in for add_key 2530 * In general, should be little reason FALSE is ever passed in for add_key
2750 * 2531 *
2751 * Returns TRUE on success. 2532 * Returns TRUE on success.
2752 */ 2533 */
2753int set_ob_key_value(object * op, const char * key, const char * value, int add_key) { 2534int set_ob_key_value(object * op, const char *key, const char * value, int add_key)
2754 const char * canonical_key = NULL; 2535{
2755 int floating_ref = FALSE; 2536 shstr key_ (key);
2756 int ret;
2757
2758 /* HACK This mess is to make sure set_ob_value() passes a shared string
2759 * to get_ob_key_link(), without leaving a leaked refcount.
2760 */
2761
2762 canonical_key = find_string(key);
2763 if (canonical_key == NULL) {
2764 canonical_key = add_string(key);
2765 floating_ref = TRUE;
2766 }
2767
2768 ret = set_ob_key_value_s(op, canonical_key, value, add_key); 2537 return set_ob_key_value_s (op, key_, value, add_key);
2769
2770 if (floating_ref) {
2771 free_string(canonical_key);
2772 }
2773
2774 return ret;
2775} 2538}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines