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.9 by root, Wed Aug 30 06:06:26 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.9 2006/08/30 06:06:26 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;
287return;
288 //TODO//D#d#
289#if 0
286 char *cp; 290 char *cp;
287/* object *tmp;*/ 291/* object *tmp;*/
288 292
289 if(op->arch!=NULL) { 293 if(op->arch!=NULL) {
290 strcat(errmsg,"arch "); 294 strcat(errmsg,"arch ");
315 for (tmp=op->inv; tmp; tmp=tmp->below) 319 for (tmp=op->inv; tmp; tmp=tmp->below)
316 dump_object2(tmp); 320 dump_object2(tmp);
317#endif 321#endif
318 strcat(errmsg,"end\n"); 322 strcat(errmsg,"end\n");
319 } 323 }
324#endif
320} 325}
321 326
322/* 327/*
323 * Dumps an object. Returns output in the static global errmsg array. 328 * Dumps an object. Returns output in the static global errmsg array.
324 */ 329 */
329 return; 334 return;
330 } 335 }
331 errmsg[0]='\0'; 336 errmsg[0]='\0';
332 dump_object2(op); 337 dump_object2(op);
333} 338}
334
335/* GROS - Dumps an object. Return the result into a string */
336/* Note that no checking is done for the validity of the target string, so */
337/* you need to be sure that you allocated enough space for it. */
338void dump_me(object *op, char *outstr)
339{
340 char *cp;
341
342 if(op==NULL)
343 {
344 strcpy(outstr,"[NULL pointer]");
345 return;
346 }
347 outstr[0]='\0';
348
349 if(op->arch!=NULL)
350 {
351 strcat(outstr,"arch ");
352 strcat(outstr,op->arch->name?op->arch->name:"(null)");
353 strcat(outstr,"\n");
354 if((cp=get_ob_diff(op,&empty_archetype->clone))!=NULL)
355 strcat(outstr,cp);
356 strcat(outstr,"end\n");
357 }
358 else
359 {
360 strcat(outstr,"Object ");
361 if (op->name==NULL)
362 strcat(outstr, "(null)");
363 else
364 strcat(outstr,op->name);
365 strcat(outstr,"\n");
366 strcat(outstr,"end\n");
367 }
368}
369
370/*
371 * This is really verbose...Can be triggered by the P key while in DM mode.
372 * All objects are dumped to stderr (or alternate logfile, if in server-mode)
373 */
374 339
375void dump_all_objects(void) { 340void dump_all_objects(void) {
376 object *op; 341 object *op;
377 for(op=objects;op!=NULL;op=op->next) { 342 for(op=objects;op!=NULL;op=op->next) {
378 dump_object(op); 343 dump_object(op);
414 * Used only by the patch command, but not all that useful. 379 * Used only by the patch command, but not all that useful.
415 * Enables features like "patch <name-of-other-player> food 999" 380 * Enables features like "patch <name-of-other-player> food 999"
416 */ 381 */
417 382
418object *find_object_name(const char *str) { 383object *find_object_name(const char *str) {
419 const char *name=add_string(str); 384 const char *name = shstr::find (str);
420 object *op; 385 object *op;
421 for(op=objects;op!=NULL;op=op->next) 386 for(op=objects;op!=NULL;op=op->next)
422 if(op->name==name) 387 if(&op->name == name)
423 break; 388 break;
424 free_string(name); 389
425 return op; 390 return op;
426} 391}
427 392
428void free_all_object_data(void) { 393void free_all_object_data(void) {
429#ifdef MEMORY_DEBUG 394#ifdef MEMORY_DEBUG
552} 517}
553 518
554/* Zero the key_values on op, decrementing the shared-string 519/* Zero the key_values on op, decrementing the shared-string
555 * refcounts and freeing the links. 520 * refcounts and freeing the links.
556 */ 521 */
557static void free_key_values(object * op) { 522static void free_key_values(object * op)
558 key_value * i; 523{
559 key_value * next = NULL; 524 for (key_value *i = op->key_values; i != 0; )
560 525 {
561 if (op->key_values == NULL) return; 526 key_value *next = i->next;
527 delete i;
528 i = next;
562 529 }
563 for (i = op->key_values; i != NULL; i = next) { 530
564 /* Store next *first*. */
565 next = i->next;
566
567 if (i->key) FREE_AND_CLEAR_STR(i->key);
568 if (i->value) FREE_AND_CLEAR_STR(i->value);
569 i->next = NULL;
570 free(i);
571 }
572
573 op->key_values = NULL; 531 op->key_values = 0;
574} 532}
575
576 533
577/* 534/*
578 * clear_object() frees everything allocated by an object, and also 535 * clear_object() frees everything allocated by an object, and also
579 * clears all variables and flags to default settings. 536 * clears all variables and flags to default settings.
580 */ 537 */
581 538
582void clear_object(object *op) { 539void clear_object(object *op)
540{
583 op->clear (); 541 op->clear ();
584 542
585 event *evt;
586 event *evt2;
587
588 /* redo this to be simpler/more efficient. Was also seeing
589 * crashes in the old code. Move this to the top - am
590 * seeing periodic crashes in this code, and would like to have
591 * as much info available as possible (eg, object name).
592 */
593 for (evt = op->events; evt; evt=evt2) {
594 evt2 = evt->next;
595
596 if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
597 if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
598 if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
599
600 free(evt);
601 }
602 op->events = NULL;
603
604 free_key_values(op); 543 free_key_values (op);
605 544
606 /* 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
607 * to reduce the refcount on them. 546 * to reduce the refcount on them.
608 */ 547 */
609 if (op->name!=NULL) FREE_AND_CLEAR_STR(op->name); 548 op->name = 0;
610 if (op->name_pl!=NULL) FREE_AND_CLEAR_STR(op->name_pl); 549 op->name_pl = 0;
611 if (op->title != NULL) FREE_AND_CLEAR_STR(op->title); 550 op->title = 0;
612 if (op->race!=NULL) FREE_AND_CLEAR_STR(op->race); 551 op->race = 0;
613 if (op->slaying!=NULL) FREE_AND_CLEAR_STR(op->slaying); 552 op->slaying = 0;
614 if (op->skill!=NULL) FREE_AND_CLEAR_STR(op->skill); 553 op->skill = 0;
615 if (op->msg!=NULL) FREE_AND_CLEAR_STR(op->msg); 554 op->msg = 0;
616 if (op->lore!=NULL) FREE_AND_CLEAR_STR(op->lore); 555 op->lore = 0;
617 if (op->materialname!= NULL) FREE_AND_CLEAR_STR(op->materialname); 556 op->materialname = 0;
618 557
619
620 memset((void*)op, 0, sizeof (object_special)); 558 memset((void*)op, 0, sizeof (object_special));
621 /* 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,
622 * or set default values that are not zero. 560 * or set default values that are not zero.
623 */ 561 */
624 /* This is more or less true */ 562 /* This is more or less true */
625 SET_FLAG(op, FLAG_REMOVED); 563 SET_FLAG (op, FLAG_REMOVED);
626 564
627
628 op->contr = NULL; 565 op->contr = NULL;
629 op->below=NULL; 566 op->below = NULL;
630 op->above=NULL; 567 op->above = NULL;
631 op->inv=NULL; 568 op->inv = NULL;
632 op->events=NULL;
633 op->container=NULL; 569 op->container=NULL;
634 op->env=NULL; 570 op->env=NULL;
635 op->more=NULL; 571 op->more=NULL;
636 op->head=NULL; 572 op->head=NULL;
637 op->map=NULL; 573 op->map=NULL;
638 op->refcount=0; 574 op->refcount=0;
639 op->active_next = NULL; 575 op->active_next = NULL;
640 op->active_prev = NULL; 576 op->active_prev = NULL;
641 /* What is not cleared is next, prev, and count */ 577 /* What is not cleared is next, prev, and count */
642 578
643 op->expmul=1.0; 579 op->expmul=1.0;
644 op->face = blank_face; 580 op->face = blank_face;
645 op->attacked_by_count = (tag_t) -1; 581 op->attacked_by_count = (tag_t) -1;
582
646 if (settings.casting_time) 583 if (settings.casting_time)
647 op->casting_time = -1; 584 op->casting_time = -1;
648
649} 585}
650 586
651/* 587/*
652 * copy object first frees everything allocated by the second object, 588 * copy object first frees everything allocated by the second object,
653 * 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
655 * 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,
656 * 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
657 * will point at garbage. 593 * will point at garbage.
658 */ 594 */
659 595
660void copy_object(object *op2, object *op) { 596void copy_object(object *op2, object *op)
597{
661 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);
662 event *evt, *evt2, *evt_new;
663 599
664 op->clear (); 600 op->clear ();
665 601
666 /* Decrement the refcounts, but don't bother zeroing the fields;
667 they'll be overwritten by memcpy. */
668 if(op->name!=NULL) free_string(op->name);
669 if(op->name_pl!=NULL) free_string(op->name_pl);
670 if(op->title!=NULL) free_string(op->title);
671 if(op->race!=NULL) free_string(op->race);
672 if(op->slaying!=NULL) free_string(op->slaying);
673 if(op->skill!=NULL) free_string(op->skill);
674 if(op->msg!=NULL) free_string(op->msg);
675 if(op->lore!=NULL) free_string(op->lore);
676 if(op->materialname != NULL) free_string(op->materialname);
677 if(op->custom_name != NULL) free_string(op->custom_name);
678
679 /* Basically, same code as from clear_object() */
680 for (evt = op->events; evt; evt=evt2) {
681 evt2 = evt->next;
682
683 if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
684 if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
685 if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
686
687 free(evt);
688 }
689 op->events = NULL;
690
691 free_key_values(op); 602 free_key_values (op);
692 603
693 *(object_special *)op = *(object_special *)op2; 604 *(object_special *)op = *(object_special *)op2;
694 op2->clone (op); 605 op2->clone (op);
695 606
696 if(is_freed) SET_FLAG(op,FLAG_FREED); 607 if (is_freed) SET_FLAG(op,FLAG_FREED);
697 if(is_removed) SET_FLAG(op,FLAG_REMOVED); 608 if (is_removed) SET_FLAG(op,FLAG_REMOVED);
698 if(op->name!=NULL) add_refcount(op->name);
699 if(op->name_pl!=NULL) add_refcount(op->name_pl);
700 if(op->title!=NULL) add_refcount(op->title);
701 if(op->race!=NULL) add_refcount(op->race);
702 if(op->slaying!=NULL) add_refcount(op->slaying);
703 if(op->skill!=NULL) add_refcount(op->skill);
704 if(op->lore!=NULL) add_refcount(op->lore);
705 if(op->msg!=NULL) add_refcount(op->msg);
706 if(op->custom_name!=NULL) add_refcount(op->custom_name);
707 if (op->materialname != NULL) add_refcount(op->materialname);
708 609
709 if((op2->speed<0) && !editor) 610 if (op2->speed < 0)
710 op->speed_left=op2->speed_left-RANDOM()%200/100.0; 611 op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0;
711 612
712 /* Copy over event information */
713 evt2 = NULL;
714 for (evt = op2->events; evt; evt=evt->next) {
715 evt_new = (event *) malloc(sizeof(event));
716 memcpy(evt_new, evt, sizeof(event));
717 if (evt_new->hook) add_refcount(evt_new->hook);
718 if (evt_new->plugin) add_refcount(evt_new->plugin);
719 if (evt_new->options) add_refcount(evt_new->options);
720 evt_new->next = NULL;
721
722 /* Try to be a little clever here, and store away the
723 * last event we copied, so that its simpler to update the
724 * pointer.
725 */
726 if (evt2)
727 evt2->next = evt_new;
728 else
729 op->events = evt_new;
730
731 evt2 = evt_new;
732 }
733 /* Copy over key_values, if any. */ 613 /* Copy over key_values, if any. */
734 if (op2->key_values != NULL) { 614 if (op2->key_values != NULL) {
735 key_value * tail = NULL; 615 key_value *tail = NULL;
736 key_value * i; 616 key_value *i;
737 617
738 op->key_values = NULL; 618 op->key_values = NULL;
739 619
740 for (i = op2->key_values; i != NULL; i = i->next) { 620 for (i = op2->key_values; i != NULL; i = i->next)
741 key_value * new_link = (key_value *) malloc(sizeof(key_value)); 621 {
622 key_value *new_link = new key_value;
742 623
743 new_link->next = NULL; 624 new_link->next = NULL;
744 new_link->key = add_refcount(i->key); 625 new_link->key = i->key;
745 if (i->value)
746 new_link->value = add_refcount(i->value); 626 new_link->value = i->value;
747 else
748 new_link->value = NULL;
749 627
750 /* Try and be clever here, too. */ 628 /* Try and be clever here, too. */
751 if (op->key_values == NULL) { 629 if (op->key_values == NULL)
630 {
752 op->key_values = new_link; 631 op->key_values = new_link;
753 tail = new_link; 632 tail = new_link;
754 } else {
755 tail->next = new_link;
756 tail = new_link;
757 } 633 }
634 else
635 {
636 tail->next = new_link;
637 tail = new_link;
638 }
758 } 639 }
759 } 640 }
760 641
761 update_ob_speed(op); 642 update_ob_speed (op);
762} 643}
763 644
764/* 645/*
765 * expand_objects() allocates more objects for the list of unused objects. 646 * expand_objects() allocates more objects for the list of unused objects.
766 * 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.
767 */ 648 */
768 649
769void expand_objects(void) { 650void expand_objects(void) {
770 int i; 651 int i;
771 object *obj; 652 object *obj;
772 obj = (object *) CALLOC(OBJ_EXPAND,sizeof(object)); 653 obj = new object [OBJ_EXPAND];
773 654
774 if(obj==NULL)
775 fatal(OUT_OF_MEMORY);
776 free_objects=obj; 655 free_objects=obj;
777 obj[0].prev=NULL; 656 obj[0].prev=NULL;
778 obj[0].next= &obj[1], 657 obj[0].next= &obj[1],
779 SET_FLAG(&(obj[0]), FLAG_REMOVED); 658 SET_FLAG(&(obj[0]), FLAG_REMOVED);
780 SET_FLAG(&(obj[0]), FLAG_FREED); 659 SET_FLAG(&(obj[0]), FLAG_FREED);
798 * 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
799 * sure it is initialised, and returns it. 678 * sure it is initialised, and returns it.
800 * 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.
801 */ 680 */
802 681
803object *get_object(void) { 682object *get_object(void)
683{
804 object *op; 684 object *op;
805 685
806 if(free_objects==NULL) { 686 if(free_objects==NULL)
807 expand_objects(); 687 expand_objects();
808 } 688
809 op=free_objects; 689 op = free_objects;
810#ifdef MEMORY_DEBUG
811 /* The idea is hopefully by doing a realloc, the memory
812 * debugging program will now use the current stack trace to
813 * report leaks.
814 */
815 op = realloc(op, sizeof(object));
816 SET_FLAG(op, FLAG_REMOVED);
817 SET_FLAG(op, FLAG_FREED);
818#endif
819 690
820 if(!QUERY_FLAG(op,FLAG_FREED)) { 691 if (!QUERY_FLAG (op, FLAG_FREED))
821 LOG(llevError,"Fatal: Getting busy object.\n"); 692 LOG(llevError,"Fatal: Getting busy object.\n");
822 } 693
823 free_objects=op->next; 694 free_objects = op->next;
695
824 if(free_objects!=NULL) 696 if (free_objects != NULL)
825 free_objects->prev=NULL; 697 free_objects->prev = NULL;
698
826 op->count= ++ob_count; 699 op->count= ++ob_count;
827 op->name=NULL; 700 op->name=NULL;
828 op->name_pl=NULL; 701 op->name_pl=NULL;
829 op->title=NULL; 702 op->title=NULL;
830 op->race=NULL; 703 op->race=NULL;
871 /* No reason putting the archetypes objects on the speed list, 744 /* No reason putting the archetypes objects on the speed list,
872 * since they never really need to be updated. 745 * since they never really need to be updated.
873 */ 746 */
874 747
875 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) { 748 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
876 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);
877#ifdef MANY_CORES 750#ifdef MANY_CORES
878 abort(); 751 abort();
879#else 752#else
880 op->speed = 0; 753 op->speed = 0;
881#endif 754#endif
965 * UP_OBJ_FACE: only the objects face has changed. 838 * UP_OBJ_FACE: only the objects face has changed.
966 */ 839 */
967 840
968void update_object(object *op, int action) { 841void update_object(object *op, int action) {
969 int update_now=0, flags; 842 int update_now=0, flags;
970 MoveType move_on, move_off, move_block, move_slow; 843 MoveType move_on, move_off, move_block, move_slow;
971 844
972 if (op == NULL) { 845 if (op == NULL) {
973 /* this should never happen */ 846 /* this should never happen */
974 LOG(llevDebug,"update_object() called for NULL object.\n"); 847 LOG(llevDebug,"update_object() called for NULL object.\n");
975 return; 848 return;
1102 remove_ob(op); 975 remove_ob(op);
1103 free_object2(op, free_inventory); 976 free_object2(op, free_inventory);
1104 op=tmp; 977 op=tmp;
1105 } 978 }
1106 } 979 }
1107 else { /* Put objects in inventory onto this space */ 980 else { /* Put objects in inventory onto this space */
1108 op=ob->inv; 981 op=ob->inv;
1109 while(op!=NULL) { 982 while(op!=NULL) {
1110 tmp=op->below; 983 tmp=op->below;
1111 remove_ob(op); 984 remove_ob(op);
1112 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) ||
1138 ob->prev->next=ob->next; 1011 ob->prev->next=ob->next;
1139 if(ob->next!=NULL) 1012 if(ob->next!=NULL)
1140 ob->next->prev=ob->prev; 1013 ob->next->prev=ob->prev;
1141 } 1014 }
1142 1015
1143 if(ob->name!=NULL) FREE_AND_CLEAR_STR(ob->name); 1016 ob->name = 0;
1144 if(ob->name_pl!=NULL) FREE_AND_CLEAR_STR(ob->name_pl); 1017 ob->name_pl = 0;
1145 if(ob->title!=NULL) FREE_AND_CLEAR_STR(ob->title); 1018 ob->title = 0;
1146 if(ob->race!=NULL) FREE_AND_CLEAR_STR(ob->race); 1019 ob->race = 0;
1147 if(ob->slaying!=NULL) FREE_AND_CLEAR_STR(ob->slaying); 1020 ob->slaying = 0;
1148 if(ob->skill!=NULL) FREE_AND_CLEAR_STR(ob->skill); 1021 ob->skill = 0;
1149 if(ob->lore!=NULL) FREE_AND_CLEAR_STR(ob->lore); 1022 ob->lore = 0;
1150 if(ob->msg!=NULL) FREE_AND_CLEAR_STR(ob->msg); 1023 ob->msg = 0;
1151 if(ob->materialname!=NULL) FREE_AND_CLEAR_STR(ob->materialname); 1024 ob->materialname = 0;
1152 1025
1153
1154 /* Why aren't events freed? */
1155 free_key_values(ob); 1026 free_key_values (ob);
1156
1157#if 0 /* MEMORY_DEBUG*/
1158 /* This is a nice idea. Unfortunately, a lot of the code in crossfire
1159 * presumes the freed_object will stick around for at least a little
1160 * bit
1161 */
1162 /* this is necessary so that memory debugging programs will
1163 * be able to accurately report source of malloc. If we recycle
1164 * objects, then some other area may be doing the get_object
1165 * and not freeing it, but the original one that malloc'd the
1166 * object will get the blame.
1167 */
1168 free(ob);
1169#else
1170 1027
1171 /* Now link it with the free_objects list: */ 1028 /* Now link it with the free_objects list: */
1172 ob->prev=NULL; 1029 ob->prev=NULL;
1173 ob->next=free_objects; 1030 ob->next=free_objects;
1031
1174 if(free_objects!=NULL) 1032 if(free_objects!=NULL)
1175 free_objects->prev=ob; 1033 free_objects->prev=ob;
1034
1176 free_objects=ob; 1035 free_objects=ob;
1177 nroffreeobjects++; 1036 nroffreeobjects++;
1178#endif
1179} 1037}
1180 1038
1181/* 1039/*
1182 * 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.
1183 */ 1041 */
1184 1042
1185int count_free(void) { 1043int count_free ()
1044{
1186 int i=0; 1045 int i=0;
1187 object *tmp=free_objects; 1046 object *tmp=free_objects;
1188 while(tmp!=NULL) 1047 while(tmp!=NULL)
1189 tmp=tmp->next, i++; 1048 tmp=tmp->next, i++;
1190 return i; 1049 return i;
1192 1051
1193/* 1052/*
1194 * 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.
1195 */ 1054 */
1196 1055
1197int count_used(void) { 1056int count_used()
1057{
1198 int i=0; 1058 int i=0;
1199 object *tmp=objects; 1059 object *tmp=objects;
1200 while(tmp!=NULL) 1060 while(tmp!=NULL)
1201 tmp=tmp->next, i++; 1061 tmp=tmp->next, i++;
1202 return i; 1062 return i;
1204 1064
1205/* 1065/*
1206 * 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.
1207 */ 1067 */
1208 1068
1209int count_active(void) { 1069int count_active()
1070{
1210 int i=0; 1071 int i=0;
1211 object *tmp=active_objects; 1072 object *tmp=active_objects;
1212 while(tmp!=NULL) 1073 while(tmp!=NULL)
1213 tmp=tmp->active_next, i++; 1074 tmp=tmp->active_next, i++;
1214 return i; 1075 return i;
1297 /* we set up values so that it could be inserted into 1158 /* we set up values so that it could be inserted into
1298 * 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
1299 * to the caller to decide what we want to do. 1160 * to the caller to decide what we want to do.
1300 */ 1161 */
1301 op->x=op->env->x,op->y=op->env->y; 1162 op->x=op->env->x,op->y=op->env->y;
1302 op->ox=op->x,op->oy=op->y;
1303 op->map=op->env->map; 1163 op->map=op->env->map;
1304 op->above=NULL,op->below=NULL; 1164 op->above=NULL,op->below=NULL;
1305 op->env=NULL; 1165 op->env=NULL;
1306 return; 1166 return;
1307 } 1167 }
1382 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) { 1242 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) {
1383 1243
1384 move_apply(tmp, op, NULL); 1244 move_apply(tmp, op, NULL);
1385 if (was_destroyed (op, tag)) { 1245 if (was_destroyed (op, tag)) {
1386 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " 1246 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed "
1387 "leaving object\n", tmp->name, tmp->arch->name); 1247 "leaving object\n", &tmp->name, &tmp->arch->name);
1388 } 1248 }
1389 } 1249 }
1390 1250
1391 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ 1251 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1392 1252
1518 * 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
1519 * 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
1520 * is clear wrong do we normalize it. 1380 * is clear wrong do we normalize it.
1521 */ 1381 */
1522 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) { 1382 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) {
1523 /* Debugging information so you can see the last coordinates this object had */
1524 more->ox = more->x;
1525 more->oy = more->y;
1526 more->map = get_map_from_coord(m, &more->x, &more->y); 1383 more->map = get_map_from_coord(m, &more->x, &more->y);
1527 } else if (!more->map) { 1384 } else if (!more->map) {
1528 /* For backwards compatibility - when not dealing with tiled maps, 1385 /* For backwards compatibility - when not dealing with tiled maps,
1529 * more->map should always point to the parent. 1386 * more->map should always point to the parent.
1530 */ 1387 */
1536 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");
1537 return NULL; 1394 return NULL;
1538 } 1395 }
1539 } 1396 }
1540 CLEAR_FLAG(op,FLAG_REMOVED); 1397 CLEAR_FLAG(op,FLAG_REMOVED);
1541
1542 /* Debugging information so you can see the last coordinates this object had */
1543 op->ox=op->x;
1544 op->oy=op->y;
1545 1398
1546 /* Ideally, the caller figures this out. However, it complicates a lot 1399 /* Ideally, the caller figures this out. However, it complicates a lot
1547 * 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
1548 * need extra work 1401 * need extra work
1549 */ 1402 */
1617 * looks like instead of lots of conditions here. 1470 * looks like instead of lots of conditions here.
1618 * makes things faster, and effectively the same result. 1471 * makes things faster, and effectively the same result.
1619 */ 1472 */
1620 1473
1621 /* Have object 'fall below' other objects that block view. 1474 /* Have object 'fall below' other objects that block view.
1622 * Unless those objects are exits, type 66 1475 * Unless those objects are exits, type 66
1623 * If INS_ON_TOP is used, don't do this processing 1476 * If INS_ON_TOP is used, don't do this processing
1624 * Need to find the object that in fact blocks view, otherwise 1477 * Need to find the object that in fact blocks view, otherwise
1625 * stacking is a bit odd. 1478 * stacking is a bit odd.
1626 */ 1479 */
1627 if (!(flag & INS_ON_TOP) && 1480 if (!(flag & INS_ON_TOP) &&
1628 (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) &&
1629 (op->face && !op->face->visibility)) { 1482 (op->face && !op->face->visibility)) {
1630 for (last=top; last != floor; last=last->below) 1483 for (last=top; last != floor; last=last->below)
1631 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break; 1484 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break;
1632 /* Check to see if we found the object that blocks view, 1485 /* Check to see if we found the object that blocks view,
1633 * 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
1634 * we can get inserted below this one, which requires we 1487 * we can get inserted below this one, which requires we
1635 * set top to the object below us. 1488 * set top to the object below us.
1636 */ 1489 */
1665 1518
1666 /* 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
1667 * it, so save a few ticks and start from there. 1520 * it, so save a few ticks and start from there.
1668 */ 1521 */
1669 if (!(flag & INS_MAP_LOAD)) 1522 if (!(flag & INS_MAP_LOAD))
1670 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) {
1671 if (tmp->type == PLAYER) 1524 if (tmp->type == PLAYER)
1672 tmp->contr->socket.update_look=1; 1525 tmp->contr->socket.update_look=1;
1673 } 1526 }
1674 1527
1675 /* If this object glows, it may affect lighting conditions that are 1528 /* If this object glows, it may affect lighting conditions that are
1676 * 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
1677 * 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
1678 * on the map will get recalculated. The players could very well 1531 * on the map will get recalculated. The players could very well
1750 object *newob; 1603 object *newob;
1751 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0); 1604 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1752 1605
1753 if(orig_ob->nrof<nr) { 1606 if(orig_ob->nrof<nr) {
1754 sprintf(errmsg,"There are only %d %ss.", 1607 sprintf(errmsg,"There are only %d %ss.",
1755 orig_ob->nrof?orig_ob->nrof:1, orig_ob->name); 1608 orig_ob->nrof?orig_ob->nrof:1, &orig_ob->name);
1756 return NULL; 1609 return NULL;
1757 } 1610 }
1758 newob = object_create_clone(orig_ob); 1611 newob = object_create_clone(orig_ob);
1759 if((orig_ob->nrof-=nr)<1) { 1612 if((orig_ob->nrof-=nr)<1) {
1760 if ( ! is_removed) 1613 if ( ! is_removed)
1907 "Warning: Tried to insert object wrong part of multipart object.\n"); 1760 "Warning: Tried to insert object wrong part of multipart object.\n");
1908 where = where->head; 1761 where = where->head;
1909 } 1762 }
1910 if (op->more) { 1763 if (op->more) {
1911 LOG(llevError, "Tried to insert multipart object %s (%d)\n", 1764 LOG(llevError, "Tried to insert multipart object %s (%d)\n",
1912 op->name, op->count); 1765 &op->name, op->count);
1913 return op; 1766 return op;
1914 } 1767 }
1915 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL); 1768 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL);
1916 CLEAR_FLAG(op, FLAG_REMOVED); 1769 CLEAR_FLAG(op, FLAG_REMOVED);
1917 if(op->nrof) { 1770 if(op->nrof) {
1951 op->map=NULL; 1804 op->map=NULL;
1952 op->env=where; 1805 op->env=where;
1953 op->above=NULL; 1806 op->above=NULL;
1954 op->below=NULL; 1807 op->below=NULL;
1955 op->x=0,op->y=0; 1808 op->x=0,op->y=0;
1956 op->ox=0,op->oy=0;
1957 1809
1958 /* 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 */
1959 if((op->glow_radius!=0)&&where->map) 1811 if((op->glow_radius!=0)&&where->map)
1960 { 1812 {
1961#ifdef DEBUG_LIGHTS 1813#ifdef DEBUG_LIGHTS
2003{ 1855{
2004 object *tmp; 1856 object *tmp;
2005 tag_t tag; 1857 tag_t tag;
2006 mapstruct *m=op->map; 1858 mapstruct *m=op->map;
2007 int x=op->x, y=op->y; 1859 int x=op->x, y=op->y;
2008 MoveType move_on, move_slow, move_block; 1860 MoveType move_on, move_slow, move_block;
2009 1861
2010 if(QUERY_FLAG(op,FLAG_NO_APPLY)) 1862 if(QUERY_FLAG(op,FLAG_NO_APPLY))
2011 return 0; 1863 return 0;
2012 1864
2013 tag = op->count; 1865 tag = op->count;
2331int find_dir(mapstruct *m, int x, int y, object *exclude) { 2183int find_dir(mapstruct *m, int x, int y, object *exclude) {
2332 int i,max=SIZEOFFREE, mflags; 2184 int i,max=SIZEOFFREE, mflags;
2333 sint16 nx, ny; 2185 sint16 nx, ny;
2334 object *tmp; 2186 object *tmp;
2335 mapstruct *mp; 2187 mapstruct *mp;
2336 MoveType blocked, move_type; 2188 MoveType blocked, move_type;
2337 2189
2338 if (exclude && exclude->head) { 2190 if (exclude && exclude->head) {
2339 exclude = exclude->head; 2191 exclude = exclude->head;
2340 move_type = exclude->move_type; 2192 move_type = exclude->move_type;
2341 } else { 2193 } else {
2635 op=get_object(); 2487 op=get_object();
2636 2488
2637 object_thawer thawer (filename); 2489 object_thawer thawer (filename);
2638 if (thawer) 2490 if (thawer)
2639 load_object(thawer,op,LO_NEWFILE,0); 2491 load_object(thawer,op,LO_NEWFILE,0);
2640 LOG(llevDebug," load str completed, object=%s\n",op->name); 2492 LOG(llevDebug," load str completed, object=%s\n", &op->name);
2641 CLEAR_FLAG(op,FLAG_REMOVED); 2493 CLEAR_FLAG(op,FLAG_REMOVED);
2642 2494
2643 return op; 2495 return op;
2644} 2496}
2645 2497
2684 */ 2536 */
2685const 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) {
2686 key_value * link; 2538 key_value * link;
2687 const char * canonical_key; 2539 const char * canonical_key;
2688 2540
2689 canonical_key = find_string(key); 2541 canonical_key = shstr::find (key);
2690 2542
2691 if (canonical_key == NULL) { 2543 if (canonical_key == NULL) {
2692 /* 1. There being a field named key on any object 2544 /* 1. There being a field named key on any object
2693 * implies there'd be a shared string to find. 2545 * implies there'd be a shared string to find.
2694 * 2. Since there isn't, no object has this field. 2546 * 2. Since there isn't, no object has this field.
2717 * 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
2718 * keys. 2570 * keys.
2719 * 2571 *
2720 * Returns TRUE on success. 2572 * Returns TRUE on success.
2721 */ 2573 */
2722int 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) {
2723 key_value * field = NULL, *last=NULL; 2575 key_value * field = NULL, *last=NULL;
2724 2576
2725 for (field=op->key_values; field != NULL; field=field->next) { 2577 for (field=op->key_values; field != NULL; field=field->next) {
2726 if (field->key != canonical_key) { 2578 if (field->key != canonical_key) {
2727 last = field; 2579 last = field;
2728 continue; 2580 continue;
2729 } 2581 }
2730 2582
2731 if (field->value) FREE_AND_CLEAR_STR(field->value);
2732 if (value) 2583 if (value)
2733 field->value = add_string(value); 2584 field->value = value;
2734 else { 2585 else {
2735 /* Basically, if the archetype has this key set, 2586 /* Basically, if the archetype has this key set,
2736 * we need to store the null value so when we save 2587 * we need to store the null value so when we save
2737 * it, we save the empty value so that when we load, 2588 * it, we save the empty value so that when we load,
2738 * we get this value back again. 2589 * we get this value back again.
2739 */ 2590 */
2740 if (get_ob_key_link(&op->arch->clone, canonical_key)) 2591 if (get_ob_key_link (&op->arch->clone, canonical_key))
2741 field->value = NULL; 2592 field->value = 0;
2742 else { 2593 else
2743 /* Delete this link */ 2594 {
2744 if (field->key) FREE_AND_CLEAR_STR(field->key);
2745 if (field->value) FREE_AND_CLEAR_STR(field->value);
2746 if (last) last->next = field->next; 2595 if (last) last->next = field->next;
2747 else op->key_values = field->next; 2596 else op->key_values = field->next;
2597
2748 free(field); 2598 delete field;
2749 } 2599 }
2750 } 2600 }
2751 return TRUE; 2601 return TRUE;
2752 } 2602 }
2753 /* IF we get here, key doesn't exist */ 2603 /* IF we get here, key doesn't exist */
2754 2604
2763 * be here. If user wants to store empty strings, 2613 * be here. If user wants to store empty strings,
2764 * should pass in "" 2614 * should pass in ""
2765 */ 2615 */
2766 if (value == NULL) return TRUE; 2616 if (value == NULL) return TRUE;
2767 2617
2768 field = (key_value *) malloc(sizeof(key_value)); 2618 field = new key_value;
2769 2619
2770 field->key = add_refcount(canonical_key); 2620 field->key = canonical_key;
2771 field->value = add_string(value); 2621 field->value = value;
2772 /* Usual prepend-addition. */ 2622 /* Usual prepend-addition. */
2773 field->next = op->key_values; 2623 field->next = op->key_values;
2774 op->key_values = field; 2624 op->key_values = field;
2775 2625
2776 return TRUE; 2626 return TRUE;
2783 * and not add new ones. 2633 * and not add new ones.
2784 * 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
2785 * 2635 *
2786 * Returns TRUE on success. 2636 * Returns TRUE on success.
2787 */ 2637 */
2788int 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)
2789 const char * canonical_key = NULL; 2639{
2790 int floating_ref = FALSE; 2640 shstr key_ (key);
2791 int ret;
2792
2793 /* HACK This mess is to make sure set_ob_value() passes a shared string
2794 * to get_ob_key_link(), without leaving a leaked refcount.
2795 */
2796
2797 canonical_key = find_string(key);
2798 if (canonical_key == NULL) {
2799 canonical_key = add_string(key);
2800 floating_ref = TRUE;
2801 }
2802
2803 ret = set_ob_key_value_s(op, canonical_key, value, add_key); 2641 return set_ob_key_value_s (op, key_, value, add_key);
2804
2805 if (floating_ref) {
2806 free_string(canonical_key);
2807 }
2808
2809 return ret;
2810} 2642}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines