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.12 by elmex, Sun Sep 3 14:33:47 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.12 2006/09/03 14:33:47 elmex 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
50int nrofallocobjects = STARTMAX; /* How many OBs allocated (free + used) */ 50int nrofallocobjects = STARTMAX; /* How many OBs allocated (free + used) */
51#endif 51#endif
52 52
53object *objects; /* Pointer to the list of used objects */ 53object *objects; /* Pointer to the list of used objects */
54object *free_objects; /* Pointer to the list of unused objects */ 54object *free_objects; /* Pointer to the list of unused objects */
55object *active_objects; /* List of active objects that need to be processed */ 55object *active_objects; /* List of active objects that need to be processed */
56 56
57short freearr_x[SIZEOFFREE]= 57short 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, 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,
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}; 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};
60short freearr_y[SIZEOFFREE]= 60short freearr_y[SIZEOFFREE]=
276 return op; 276 return op;
277} 277}
278 278
279/* 279/*
280 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. 280 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
281 * Some error messages. 281 * Some error messages.
282 * The result of the dump is stored in the static global errmsg array. 282 * The result of the dump is stored in the static global errmsg array.
283 */ 283 */
284 284
285void dump_object2(object *op) { 285void dump_object2(object *op) {
286errmsg[0] = 0; 286errmsg[0] = 0;
379 * Used only by the patch command, but not all that useful. 379 * Used only by the patch command, but not all that useful.
380 * Enables features like "patch <name-of-other-player> food 999" 380 * Enables features like "patch <name-of-other-player> food 999"
381 */ 381 */
382 382
383object *find_object_name(const char *str) { 383object *find_object_name(const char *str) {
384 const char *name=add_string(str); 384 const char *name = shstr::find (str);
385 object *op; 385 object *op;
386 for(op=objects;op!=NULL;op=op->next) 386 for(op=objects;op!=NULL;op=op->next)
387 if(op->name==name) 387 if(&op->name == name)
388 break; 388 break;
389 free_string(name); 389
390 return op; 390 return op;
391} 391}
392 392
393void free_all_object_data(void) { 393void free_all_object_data(void) {
394#ifdef MEMORY_DEBUG 394#ifdef MEMORY_DEBUG
517} 517}
518 518
519/* Zero the key_values on op, decrementing the shared-string 519/* Zero the key_values on op, decrementing the shared-string
520 * refcounts and freeing the links. 520 * refcounts and freeing the links.
521 */ 521 */
522static void free_key_values(object * op) { 522static void free_key_values(object * op)
523 key_value * i; 523{
524 key_value * next = NULL; 524 for (key_value *i = op->key_values; i != 0; )
525 525 {
526 if (op->key_values == NULL) return; 526 key_value *next = i->next;
527 delete i;
528 i = next;
527 529 }
528 for (i = op->key_values; i != NULL; i = next) { 530
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; 531 op->key_values = 0;
539} 532}
540
541 533
542/* 534/*
543 * clear_object() frees everything allocated by an object, and also 535 * clear_object() frees everything allocated by an object, and also
544 * clears all variables and flags to default settings. 536 * clears all variables and flags to default settings.
545 */ 537 */
546 538
547void clear_object(object *op) { 539void clear_object(object *op)
540{
548 op->clear (); 541 op->clear ();
549 542
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); 543 free_key_values (op);
570 544
571 /* the memset will clear all these values for us, but we need 545 /* the memset will clear all these values for us, but we need
572 * to reduce the refcount on them. 546 * to reduce the refcount on them.
573 */ 547 */
574 if (op->name!=NULL) FREE_AND_CLEAR_STR(op->name); 548 op->name = 0;
575 if (op->name_pl!=NULL) FREE_AND_CLEAR_STR(op->name_pl); 549 op->name_pl = 0;
576 if (op->title != NULL) FREE_AND_CLEAR_STR(op->title); 550 op->title = 0;
577 if (op->race!=NULL) FREE_AND_CLEAR_STR(op->race); 551 op->race = 0;
578 if (op->slaying!=NULL) FREE_AND_CLEAR_STR(op->slaying); 552 op->slaying = 0;
579 if (op->skill!=NULL) FREE_AND_CLEAR_STR(op->skill); 553 op->skill = 0;
580 if (op->msg!=NULL) FREE_AND_CLEAR_STR(op->msg); 554 op->msg = 0;
581 if (op->lore!=NULL) FREE_AND_CLEAR_STR(op->lore); 555 op->lore = 0;
582 if (op->materialname!= NULL) FREE_AND_CLEAR_STR(op->materialname); 556 op->materialname = 0;
583 557
584
585 memset((void*)op, 0, sizeof (object_special)); 558 memset((void*)op, 0, sizeof (object_special));
586 /* Below here, we clear things that are not done by the memset, 559 /* Below here, we clear things that are not done by the memset,
587 * or set default values that are not zero. 560 * or set default values that are not zero.
588 */ 561 */
589 /* This is more or less true */ 562 /* This is more or less true */
590 SET_FLAG(op, FLAG_REMOVED); 563 SET_FLAG (op, FLAG_REMOVED);
591 564
592
593 op->contr = NULL; 565 op->contr = NULL;
594 op->below=NULL; 566 op->below = NULL;
595 op->above=NULL; 567 op->above = NULL;
596 op->inv=NULL; 568 op->inv = NULL;
597 op->events=NULL;
598 op->container=NULL; 569 op->container=NULL;
599 op->env=NULL; 570 op->env=NULL;
600 op->more=NULL; 571 op->more=NULL;
601 op->head=NULL; 572 op->head=NULL;
602 op->map=NULL; 573 op->map=NULL;
603 op->refcount=0; 574 op->refcount=0;
604 op->active_next = NULL; 575 op->active_next = NULL;
605 op->active_prev = NULL; 576 op->active_prev = NULL;
606 /* What is not cleared is next, prev, and count */ 577 /* What is not cleared is next, prev, and count */
607 578
608 op->expmul=1.0; 579 op->expmul=1.0;
609 op->face = blank_face; 580 op->face = blank_face;
610 op->attacked_by_count = (tag_t) -1; 581 op->attacked_by_count = (tag_t) -1;
582
611 if (settings.casting_time) 583 if (settings.casting_time)
612 op->casting_time = -1; 584 op->casting_time = -1;
613
614} 585}
615 586
616/* 587/*
617 * copy object first frees everything allocated by the second object, 588 * copy object first frees everything allocated by the second object,
618 * and then copies the contends of the first object into the second 589 * 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, 591 * 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 592 * if the first object is freed, the pointers in the new object
622 * will point at garbage. 593 * will point at garbage.
623 */ 594 */
624 595
625void copy_object(object *op2, object *op) { 596void copy_object(object *op2, object *op)
597{
626 int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); 598 int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED);
627 event *evt, *evt2, *evt_new;
628 599
629 op->clear (); 600 op->clear ();
630 601
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); 602 free_key_values (op);
657 603
658 *(object_special *)op = *(object_special *)op2; 604 *(object_special *)op = *(object_special *)op2;
659 op2->clone (op); 605 op2->clone (op);
660 606
661 if(is_freed) SET_FLAG(op,FLAG_FREED); 607 if (is_freed) SET_FLAG(op,FLAG_FREED);
662 if(is_removed) SET_FLAG(op,FLAG_REMOVED); 608 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 609
674 if((op2->speed<0) && !editor) 610 if (op2->speed < 0)
675 op->speed_left=op2->speed_left-RANDOM()%200/100.0; 611 op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0;
676 612
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. */ 613 /* Copy over key_values, if any. */
699 if (op2->key_values != NULL) { 614 if (op2->key_values != NULL) {
700 key_value * tail = NULL; 615 key_value *tail = NULL;
701 key_value * i; 616 key_value *i;
702 617
703 op->key_values = NULL; 618 op->key_values = NULL;
704 619
705 for (i = op2->key_values; i != NULL; i = i->next) { 620 for (i = op2->key_values; i != NULL; i = i->next)
706 key_value * new_link = (key_value *) malloc(sizeof(key_value)); 621 {
622 key_value *new_link = new key_value;
707 623
708 new_link->next = NULL; 624 new_link->next = NULL;
709 new_link->key = add_refcount(i->key); 625 new_link->key = i->key;
710 if (i->value)
711 new_link->value = add_refcount(i->value); 626 new_link->value = i->value;
712 else
713 new_link->value = NULL;
714 627
715 /* Try and be clever here, too. */ 628 /* Try and be clever here, too. */
716 if (op->key_values == NULL) { 629 if (op->key_values == NULL)
630 {
717 op->key_values = new_link; 631 op->key_values = new_link;
718 tail = new_link; 632 tail = new_link;
719 } else {
720 tail->next = new_link;
721 tail = new_link;
722 } 633 }
634 else
635 {
636 tail->next = new_link;
637 tail = new_link;
638 }
723 } 639 }
724 } 640 }
725 641
726 update_ob_speed(op); 642 update_ob_speed (op);
727} 643}
728 644
729/* 645/*
730 * expand_objects() allocates more objects for the list of unused objects. 646 * expand_objects() allocates more objects for the list of unused objects.
731 * It is called from get_object() if the unused list is empty. 647 * It is called from get_object() if the unused list is empty.
732 */ 648 */
733 649
734void expand_objects(void) { 650void expand_objects(void) {
735 int i; 651 int i;
736 object *obj; 652 object *obj;
737 obj = (object *) CALLOC(OBJ_EXPAND,sizeof(object)); 653 obj = new object [OBJ_EXPAND];
738 654
739 if(obj==NULL)
740 fatal(OUT_OF_MEMORY);
741 free_objects=obj; 655 free_objects=obj;
742 obj[0].prev=NULL; 656 obj[0].prev=NULL;
743 obj[0].next= &obj[1], 657 obj[0].next= &obj[1],
744 SET_FLAG(&(obj[0]), FLAG_REMOVED); 658 SET_FLAG(&(obj[0]), FLAG_REMOVED);
745 SET_FLAG(&(obj[0]), FLAG_FREED); 659 SET_FLAG(&(obj[0]), FLAG_FREED);
763 * get_object() grabs an object from the list of unused objects, makes 677 * get_object() grabs an object from the list of unused objects, makes
764 * sure it is initialised, and returns it. 678 * sure it is initialised, and returns it.
765 * If there are no free objects, expand_objects() is called to get more. 679 * If there are no free objects, expand_objects() is called to get more.
766 */ 680 */
767 681
768object *get_object(void) { 682object *get_object(void)
683{
769 object *op; 684 object *op;
770 685
771 if(free_objects==NULL) { 686 if(free_objects==NULL)
772 expand_objects(); 687 expand_objects();
773 } 688
774 op=free_objects; 689 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);
782 SET_FLAG(op, FLAG_FREED);
783#endif
784 690
785 if(!QUERY_FLAG(op,FLAG_FREED)) { 691 if (!QUERY_FLAG (op, FLAG_FREED))
786 LOG(llevError,"Fatal: Getting busy object.\n"); 692 LOG(llevError,"Fatal: Getting busy object.\n");
787 } 693
788 free_objects=op->next; 694 free_objects = op->next;
695
789 if(free_objects!=NULL) 696 if (free_objects != NULL)
790 free_objects->prev=NULL; 697 free_objects->prev = NULL;
698
791 op->count= ++ob_count; 699 op->count= ++ob_count;
792 op->name=NULL; 700 op->name=NULL;
793 op->name_pl=NULL; 701 op->name_pl=NULL;
794 op->title=NULL; 702 op->title=NULL;
795 op->race=NULL; 703 op->race=NULL;
836 /* No reason putting the archetypes objects on the speed list, 744 /* No reason putting the archetypes objects on the speed list,
837 * since they never really need to be updated. 745 * since they never really need to be updated.
838 */ 746 */
839 747
840 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) { 748 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
841 LOG(llevError,"Object %s is freed but has speed.\n", op->name); 749 LOG(llevError,"Object %s is freed but has speed.\n", &op->name);
842#ifdef MANY_CORES 750#ifdef MANY_CORES
843 abort(); 751 abort();
844#else 752#else
845 op->speed = 0; 753 op->speed = 0;
846#endif 754#endif
930 * UP_OBJ_FACE: only the objects face has changed. 838 * UP_OBJ_FACE: only the objects face has changed.
931 */ 839 */
932 840
933void update_object(object *op, int action) { 841void update_object(object *op, int action) {
934 int update_now=0, flags; 842 int update_now=0, flags;
935 MoveType move_on, move_off, move_block, move_slow; 843 MoveType move_on, move_off, move_block, move_slow;
936 844
937 if (op == NULL) { 845 if (op == NULL) {
938 /* this should never happen */ 846 /* this should never happen */
939 LOG(llevDebug,"update_object() called for NULL object.\n"); 847 LOG(llevDebug,"update_object() called for NULL object.\n");
940 return; 848 return;
1067 remove_ob(op); 975 remove_ob(op);
1068 free_object2(op, free_inventory); 976 free_object2(op, free_inventory);
1069 op=tmp; 977 op=tmp;
1070 } 978 }
1071 } 979 }
1072 else { /* Put objects in inventory onto this space */ 980 else { /* Put objects in inventory onto this space */
1073 op=ob->inv; 981 op=ob->inv;
1074 while(op!=NULL) { 982 while(op!=NULL) {
1075 tmp=op->below; 983 tmp=op->below;
1076 remove_ob(op); 984 remove_ob(op);
1077 if(QUERY_FLAG(op,FLAG_STARTEQUIP)||QUERY_FLAG(op,FLAG_NO_DROP) || 985 if(QUERY_FLAG(op,FLAG_STARTEQUIP)||QUERY_FLAG(op,FLAG_NO_DROP) ||
1103 ob->prev->next=ob->next; 1011 ob->prev->next=ob->next;
1104 if(ob->next!=NULL) 1012 if(ob->next!=NULL)
1105 ob->next->prev=ob->prev; 1013 ob->next->prev=ob->prev;
1106 } 1014 }
1107 1015
1108 if(ob->name!=NULL) FREE_AND_CLEAR_STR(ob->name); 1016 ob->name = 0;
1109 if(ob->name_pl!=NULL) FREE_AND_CLEAR_STR(ob->name_pl); 1017 ob->name_pl = 0;
1110 if(ob->title!=NULL) FREE_AND_CLEAR_STR(ob->title); 1018 ob->title = 0;
1111 if(ob->race!=NULL) FREE_AND_CLEAR_STR(ob->race); 1019 ob->race = 0;
1112 if(ob->slaying!=NULL) FREE_AND_CLEAR_STR(ob->slaying); 1020 ob->slaying = 0;
1113 if(ob->skill!=NULL) FREE_AND_CLEAR_STR(ob->skill); 1021 ob->skill = 0;
1114 if(ob->lore!=NULL) FREE_AND_CLEAR_STR(ob->lore); 1022 ob->lore = 0;
1115 if(ob->msg!=NULL) FREE_AND_CLEAR_STR(ob->msg); 1023 ob->msg = 0;
1116 if(ob->materialname!=NULL) FREE_AND_CLEAR_STR(ob->materialname); 1024 ob->materialname = 0;
1117 1025
1118
1119 /* Why aren't events freed? */
1120 free_key_values(ob); 1026 free_key_values (ob);
1121
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 1027
1136 /* Now link it with the free_objects list: */ 1028 /* Now link it with the free_objects list: */
1137 ob->prev=NULL; 1029 ob->prev=NULL;
1138 ob->next=free_objects; 1030 ob->next=free_objects;
1031
1139 if(free_objects!=NULL) 1032 if(free_objects!=NULL)
1140 free_objects->prev=ob; 1033 free_objects->prev=ob;
1034
1141 free_objects=ob; 1035 free_objects=ob;
1142 nroffreeobjects++; 1036 nroffreeobjects++;
1143#endif
1144} 1037}
1145 1038
1146/* 1039/*
1147 * count_free() returns the number of objects on the list of free objects. 1040 * count_free() returns the number of objects on the list of free objects.
1148 */ 1041 */
1149 1042
1150int count_free(void) { 1043int count_free ()
1044{
1151 int i=0; 1045 int i=0;
1152 object *tmp=free_objects; 1046 object *tmp=free_objects;
1153 while(tmp!=NULL) 1047 while(tmp!=NULL)
1154 tmp=tmp->next, i++; 1048 tmp=tmp->next, i++;
1155 return i; 1049 return i;
1157 1051
1158/* 1052/*
1159 * count_used() returns the number of objects on the list of used objects. 1053 * count_used() returns the number of objects on the list of used objects.
1160 */ 1054 */
1161 1055
1162int count_used(void) { 1056int count_used()
1057{
1163 int i=0; 1058 int i=0;
1164 object *tmp=objects; 1059 object *tmp=objects;
1165 while(tmp!=NULL) 1060 while(tmp!=NULL)
1166 tmp=tmp->next, i++; 1061 tmp=tmp->next, i++;
1167 return i; 1062 return i;
1169 1064
1170/* 1065/*
1171 * count_active() returns the number of objects on the list of active objects. 1066 * count_active() returns the number of objects on the list of active objects.
1172 */ 1067 */
1173 1068
1174int count_active(void) { 1069int count_active()
1070{
1175 int i=0; 1071 int i=0;
1176 object *tmp=active_objects; 1072 object *tmp=active_objects;
1177 while(tmp!=NULL) 1073 while(tmp!=NULL)
1178 tmp=tmp->active_next, i++; 1074 tmp=tmp->active_next, i++;
1179 return i; 1075 return i;
1262 /* we set up values so that it could be inserted into 1158 /* we set up values so that it could be inserted into
1263 * the map, but we don't actually do that - it is up 1159 * the map, but we don't actually do that - it is up
1264 * to the caller to decide what we want to do. 1160 * to the caller to decide what we want to do.
1265 */ 1161 */
1266 op->x=op->env->x,op->y=op->env->y; 1162 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; 1163 op->map=op->env->map;
1269 op->above=NULL,op->below=NULL; 1164 op->above=NULL,op->below=NULL;
1270 op->env=NULL; 1165 op->env=NULL;
1271 return; 1166 return;
1272 } 1167 }
1347 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) { 1242 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) {
1348 1243
1349 move_apply(tmp, op, NULL); 1244 move_apply(tmp, op, NULL);
1350 if (was_destroyed (op, tag)) { 1245 if (was_destroyed (op, tag)) {
1351 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " 1246 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed "
1352 "leaving object\n", tmp->name, tmp->arch->name); 1247 "leaving object\n", &tmp->name, &tmp->arch->name);
1353 } 1248 }
1354 } 1249 }
1355 1250
1356 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ 1251 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1357 1252
1483 * we set the map, that doesn't work if the location is within 1378 * 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 1379 * a map and this is straddling an edge. So only if coordinate
1485 * is clear wrong do we normalize it. 1380 * is clear wrong do we normalize it.
1486 */ 1381 */
1487 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) { 1382 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); 1383 more->map = get_map_from_coord(m, &more->x, &more->y);
1492 } else if (!more->map) { 1384 } else if (!more->map) {
1493 /* For backwards compatibility - when not dealing with tiled maps, 1385 /* For backwards compatibility - when not dealing with tiled maps,
1494 * more->map should always point to the parent. 1386 * more->map should always point to the parent.
1495 */ 1387 */
1501 LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n"); 1393 LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n");
1502 return NULL; 1394 return NULL;
1503 } 1395 }
1504 } 1396 }
1505 CLEAR_FLAG(op,FLAG_REMOVED); 1397 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 1398
1511 /* Ideally, the caller figures this out. However, it complicates a lot 1399 /* 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 1400 * of areas of callers (eg, anything that uses find_free_spot would now
1513 * need extra work 1401 * need extra work
1514 */ 1402 */
1582 * looks like instead of lots of conditions here. 1470 * looks like instead of lots of conditions here.
1583 * makes things faster, and effectively the same result. 1471 * makes things faster, and effectively the same result.
1584 */ 1472 */
1585 1473
1586 /* Have object 'fall below' other objects that block view. 1474 /* Have object 'fall below' other objects that block view.
1587 * Unless those objects are exits, type 66 1475 * Unless those objects are exits, type 66
1588 * If INS_ON_TOP is used, don't do this processing 1476 * If INS_ON_TOP is used, don't do this processing
1589 * Need to find the object that in fact blocks view, otherwise 1477 * Need to find the object that in fact blocks view, otherwise
1590 * stacking is a bit odd. 1478 * stacking is a bit odd.
1591 */ 1479 */
1592 if (!(flag & INS_ON_TOP) && 1480 if (!(flag & INS_ON_TOP) &&
1593 (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) && 1481 (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) &&
1594 (op->face && !op->face->visibility)) { 1482 (op->face && !op->face->visibility)) {
1595 for (last=top; last != floor; last=last->below) 1483 for (last=top; last != floor; last=last->below)
1596 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break; 1484 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break;
1597 /* Check to see if we found the object that blocks view, 1485 /* Check to see if we found the object that blocks view,
1598 * and make sure we have a below pointer for it so that 1486 * and make sure we have a below pointer for it so that
1599 * we can get inserted below this one, which requires we 1487 * we can get inserted below this one, which requires we
1600 * set top to the object below us. 1488 * set top to the object below us.
1601 */ 1489 */
1630 1518
1631 /* If we have a floor, we know the player, if any, will be above 1519 /* 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. 1520 * it, so save a few ticks and start from there.
1633 */ 1521 */
1634 if (!(flag & INS_MAP_LOAD)) 1522 if (!(flag & INS_MAP_LOAD))
1635 for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) { 1523 for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) {
1636 if (tmp->type == PLAYER) 1524 if (tmp->type == PLAYER)
1637 tmp->contr->socket.update_look=1; 1525 tmp->contr->socket.update_look=1;
1638 } 1526 }
1639 1527
1640 /* If this object glows, it may affect lighting conditions that are 1528 /* If this object glows, it may affect lighting conditions that are
1641 * visible to others on this map. But update_all_los is really 1529 * 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 1530 * 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 1531 * on the map will get recalculated. The players could very well
1715 object *newob; 1603 object *newob;
1716 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0); 1604 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1717 1605
1718 if(orig_ob->nrof<nr) { 1606 if(orig_ob->nrof<nr) {
1719 sprintf(errmsg,"There are only %d %ss.", 1607 sprintf(errmsg,"There are only %d %ss.",
1720 orig_ob->nrof?orig_ob->nrof:1, orig_ob->name); 1608 orig_ob->nrof?orig_ob->nrof:1, &orig_ob->name);
1721 return NULL; 1609 return NULL;
1722 } 1610 }
1723 newob = object_create_clone(orig_ob); 1611 newob = object_create_clone(orig_ob);
1724 if((orig_ob->nrof-=nr)<1) { 1612 if((orig_ob->nrof-=nr)<1) {
1725 if ( ! is_removed) 1613 if ( ! is_removed)
1872 "Warning: Tried to insert object wrong part of multipart object.\n"); 1760 "Warning: Tried to insert object wrong part of multipart object.\n");
1873 where = where->head; 1761 where = where->head;
1874 } 1762 }
1875 if (op->more) { 1763 if (op->more) {
1876 LOG(llevError, "Tried to insert multipart object %s (%d)\n", 1764 LOG(llevError, "Tried to insert multipart object %s (%d)\n",
1877 op->name, op->count); 1765 &op->name, op->count);
1878 return op; 1766 return op;
1879 } 1767 }
1880 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL); 1768 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL);
1881 CLEAR_FLAG(op, FLAG_REMOVED); 1769 CLEAR_FLAG(op, FLAG_REMOVED);
1882 if(op->nrof) { 1770 if(op->nrof) {
1916 op->map=NULL; 1804 op->map=NULL;
1917 op->env=where; 1805 op->env=where;
1918 op->above=NULL; 1806 op->above=NULL;
1919 op->below=NULL; 1807 op->below=NULL;
1920 op->x=0,op->y=0; 1808 op->x=0,op->y=0;
1921 op->ox=0,op->oy=0;
1922 1809
1923 /* reset the light list and los of the players on the map */ 1810 /* reset the light list and los of the players on the map */
1924 if((op->glow_radius!=0)&&where->map) 1811 if((op->glow_radius!=0)&&where->map)
1925 { 1812 {
1926#ifdef DEBUG_LIGHTS 1813#ifdef DEBUG_LIGHTS
1968{ 1855{
1969 object *tmp; 1856 object *tmp;
1970 tag_t tag; 1857 tag_t tag;
1971 mapstruct *m=op->map; 1858 mapstruct *m=op->map;
1972 int x=op->x, y=op->y; 1859 int x=op->x, y=op->y;
1973 MoveType move_on, move_slow, move_block; 1860 MoveType move_on, move_slow, move_block;
1974 1861
1975 if(QUERY_FLAG(op,FLAG_NO_APPLY)) 1862 if(QUERY_FLAG(op,FLAG_NO_APPLY))
1976 return 0; 1863 return 0;
1977 1864
1978 tag = op->count; 1865 tag = op->count;
2296int find_dir(mapstruct *m, int x, int y, object *exclude) { 2183int find_dir(mapstruct *m, int x, int y, object *exclude) {
2297 int i,max=SIZEOFFREE, mflags; 2184 int i,max=SIZEOFFREE, mflags;
2298 sint16 nx, ny; 2185 sint16 nx, ny;
2299 object *tmp; 2186 object *tmp;
2300 mapstruct *mp; 2187 mapstruct *mp;
2301 MoveType blocked, move_type; 2188 MoveType blocked, move_type;
2302 2189
2303 if (exclude && exclude->head) { 2190 if (exclude && exclude->head) {
2304 exclude = exclude->head; 2191 exclude = exclude->head;
2305 move_type = exclude->move_type; 2192 move_type = exclude->move_type;
2306 } else { 2193 } else {
2600 op=get_object(); 2487 op=get_object();
2601 2488
2602 object_thawer thawer (filename); 2489 object_thawer thawer (filename);
2603 if (thawer) 2490 if (thawer)
2604 load_object(thawer,op,LO_NEWFILE,0); 2491 load_object(thawer,op,LO_NEWFILE,0);
2605 LOG(llevDebug," load str completed, object=%s\n",op->name); 2492 LOG(llevDebug," load str completed, object=%s\n", &op->name);
2606 CLEAR_FLAG(op,FLAG_REMOVED); 2493 CLEAR_FLAG(op,FLAG_REMOVED);
2607 2494
2608 return op; 2495 return op;
2609} 2496}
2610 2497
2649 */ 2536 */
2650const char * get_ob_key_value(const object * op, const char * const key) { 2537const char * get_ob_key_value(const object * op, const char * const key) {
2651 key_value * link; 2538 key_value * link;
2652 const char * canonical_key; 2539 const char * canonical_key;
2653 2540
2654 canonical_key = find_string(key); 2541 canonical_key = shstr::find (key);
2655 2542
2656 if (canonical_key == NULL) { 2543 if (canonical_key == NULL) {
2657 /* 1. There being a field named key on any object 2544 /* 1. There being a field named key on any object
2658 * implies there'd be a shared string to find. 2545 * implies there'd be a shared string to find.
2659 * 2. Since there isn't, no object has this field. 2546 * 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 2569 * Unless add_key is TRUE, it won't add fields, only change the value of existing
2683 * keys. 2570 * keys.
2684 * 2571 *
2685 * Returns TRUE on success. 2572 * Returns TRUE on success.
2686 */ 2573 */
2687int set_ob_key_value_s(object * op, const char * canonical_key, const char * value, int add_key) { 2574int set_ob_key_value_s(object * op, const shstr &canonical_key, const char * value, int add_key) {
2688 key_value * field = NULL, *last=NULL; 2575 key_value * field = NULL, *last=NULL;
2689 2576
2690 for (field=op->key_values; field != NULL; field=field->next) { 2577 for (field=op->key_values; field != NULL; field=field->next) {
2691 if (field->key != canonical_key) { 2578 if (field->key != canonical_key) {
2692 last = field; 2579 last = field;
2693 continue; 2580 continue;
2694 } 2581 }
2695 2582
2696 if (field->value) FREE_AND_CLEAR_STR(field->value);
2697 if (value) 2583 if (value)
2698 field->value = add_string(value); 2584 field->value = value;
2699 else { 2585 else {
2700 /* Basically, if the archetype has this key set, 2586 /* Basically, if the archetype has this key set,
2701 * we need to store the null value so when we save 2587 * we need to store the null value so when we save
2702 * it, we save the empty value so that when we load, 2588 * it, we save the empty value so that when we load,
2703 * we get this value back again. 2589 * we get this value back again.
2704 */ 2590 */
2705 if (get_ob_key_link(&op->arch->clone, canonical_key)) 2591 if (get_ob_key_link (&op->arch->clone, canonical_key))
2706 field->value = NULL; 2592 field->value = 0;
2707 else { 2593 else
2708 /* Delete this link */ 2594 {
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; 2595 if (last) last->next = field->next;
2712 else op->key_values = field->next; 2596 else op->key_values = field->next;
2597
2713 free(field); 2598 delete field;
2714 } 2599 }
2715 } 2600 }
2716 return TRUE; 2601 return TRUE;
2717 } 2602 }
2718 /* IF we get here, key doesn't exist */ 2603 /* IF we get here, key doesn't exist */
2719 2604
2728 * be here. If user wants to store empty strings, 2613 * be here. If user wants to store empty strings,
2729 * should pass in "" 2614 * should pass in ""
2730 */ 2615 */
2731 if (value == NULL) return TRUE; 2616 if (value == NULL) return TRUE;
2732 2617
2733 field = (key_value *) malloc(sizeof(key_value)); 2618 field = new key_value;
2734 2619
2735 field->key = add_refcount(canonical_key); 2620 field->key = canonical_key;
2736 field->value = add_string(value); 2621 field->value = value;
2737 /* Usual prepend-addition. */ 2622 /* Usual prepend-addition. */
2738 field->next = op->key_values; 2623 field->next = op->key_values;
2739 op->key_values = field; 2624 op->key_values = field;
2740 2625
2741 return TRUE; 2626 return TRUE;
2748 * and not add new ones. 2633 * and not add new ones.
2749 * In general, should be little reason FALSE is ever passed in for add_key 2634 * In general, should be little reason FALSE is ever passed in for add_key
2750 * 2635 *
2751 * Returns TRUE on success. 2636 * Returns TRUE on success.
2752 */ 2637 */
2753int set_ob_key_value(object * op, const char * key, const char * value, int add_key) { 2638int set_ob_key_value(object * op, const char *key, const char * value, int add_key)
2754 const char * canonical_key = NULL; 2639{
2755 int floating_ref = FALSE; 2640 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); 2641 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} 2642}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines