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.29 by root, Mon Sep 11 12:38:36 2006 UTC vs.
Revision 1.37 by root, Tue Sep 12 20:55:40 2006 UTC

29#include <stdio.h> 29#include <stdio.h>
30#include <sys/types.h> 30#include <sys/types.h>
31#include <sys/uio.h> 31#include <sys/uio.h>
32#include <object.h> 32#include <object.h>
33#include <funcpoint.h> 33#include <funcpoint.h>
34#include <skills.h>
35#include <loader.h> 34#include <loader.h>
36 35
37int nrofallocobjects = 0; 36int nrofallocobjects = 0;
38 37
39object *objects; /* Pointer to the list of used objects */ 38object *objects; /* Pointer to the list of used objects */
255 { 254 {
256 if (inv->inv) 255 if (inv->inv)
257 sum_weight (inv); 256 sum_weight (inv);
258 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); 257 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1);
259 } 258 }
259
260 if (op->type == CONTAINER && op->stats.Str) 260 if (op->type == CONTAINER && op->stats.Str)
261 sum = (sum * (100 - op->stats.Str)) / 100; 261 sum = (sum * (100 - op->stats.Str)) / 100;
262
262 if (op->carrying != sum) 263 if (op->carrying != sum)
263 op->carrying = sum; 264 op->carrying = sum;
265
264 return sum; 266 return sum;
265} 267}
266 268
267/** 269/**
268 * Return the outermost environment object for a given object. 270 * Return the outermost environment object for a given object.
417 */ 419 */
418 420
419object * 421object *
420find_object_name (const char *str) 422find_object_name (const char *str)
421{ 423{
422 const char *name = shstr::find (str); 424 shstr_cmp str_ (str);
423 object *op; 425 object *op;
424 426
425 for (op = objects; op != NULL; op = op->next) 427 for (op = objects; op != NULL; op = op->next)
426 if (&op->name == name) 428 if (op->name == str_)
427 break; 429 break;
428 430
429 return op; 431 return op;
430} 432}
431 433
443 * Changed 2004-02-12 - if the player is setting at the play again 445 * Changed 2004-02-12 - if the player is setting at the play again
444 * prompt, he is removed, and we don't want to treat him as an owner of 446 * prompt, he is removed, and we don't want to treat him as an owner of
445 * anything, so check removed flag. I don't expect that this should break 447 * anything, so check removed flag. I don't expect that this should break
446 * anything - once an object is removed, it is basically dead anyways. 448 * anything - once an object is removed, it is basically dead anyways.
447 */ 449 */
448
449object * 450object *
450get_owner (object *op) 451object::get_owner ()
451{ 452{
452 if (op->owner == NULL) 453 if (!owner
453 return NULL; 454 || QUERY_FLAG (owner, FLAG_FREED)
455 || QUERY_FLAG (owner, FLAG_REMOVED))
456 owner = 0;
454 457
455 if (!QUERY_FLAG (op->owner, FLAG_FREED) && !QUERY_FLAG (op->owner, FLAG_REMOVED) && op->owner->count == op->ownercount)
456 return op->owner; 458 return owner;
457
458 op->owner = NULL;
459 op->ownercount = 0;
460 return NULL;
461}
462
463void
464clear_owner (object *op)
465{
466 if (!op)
467 return;
468
469 if (op->owner && op->ownercount == op->owner->count)
470 op->owner->refcount--;
471
472 op->owner = NULL;
473 op->ownercount = 0;
474} 459}
475 460
476/* 461/*
477 * Sets the owner and sets the skill and exp pointers to owner's current 462 * Sets the owner and sets the skill and exp pointers to owner's current
478 * skill and experience objects. 463 * skill and experience objects.
479 */ 464 */
480void 465void
481set_owner (object *op, object *owner) 466object::set_owner (object *owner)
482{ 467{
483 if (owner == NULL || op == NULL) 468 if (!owner)
484 return; 469 return;
485 470
486 /* next line added to allow objects which own objects */ 471 /* next line added to allow objects which own objects */
487 /* Add a check for ownercounts in here, as I got into an endless loop 472 /* Add a check for ownercounts in here, as I got into an endless loop
488 * with the fireball owning a poison cloud which then owned the 473 * with the fireball owning a poison cloud which then owned the
489 * fireball. I believe that was caused by one of the objects getting 474 * fireball. I believe that was caused by one of the objects getting
490 * freed and then another object replacing it. Since the ownercounts 475 * freed and then another object replacing it. Since the ownercounts
491 * didn't match, this check is valid and I believe that cause is valid. 476 * didn't match, this check is valid and I believe that cause is valid.
492 */ 477 */
493 while (owner->owner && owner != owner->owner && owner->ownercount == owner->owner->count) 478 while (owner->owner)
494 owner = owner->owner; 479 owner = owner->owner;
495 480
496 /* IF the owner still has an owner, we did not resolve to a final owner.
497 * so lets not add to that.
498 */
499 if (owner->owner)
500 return;
501
502 op->owner = owner; 481 this->owner = owner;
503
504 op->ownercount = owner->count;
505 owner->refcount++;
506}
507
508/* Set the owner to clone's current owner and set the skill and experience
509 * objects to clone's objects (typically those objects that where the owner's
510 * current skill and experience objects at the time when clone's owner was
511 * set - not the owner's current skill and experience objects).
512 *
513 * Use this function if player created an object (e.g. fire bullet, swarm
514 * spell), and this object creates further objects whose kills should be
515 * accounted for the player's original skill, even if player has changed
516 * skills meanwhile.
517 */
518void
519copy_owner (object *op, object *clone)
520{
521 object *owner = get_owner (clone);
522
523 if (owner == NULL)
524 {
525 /* players don't have owners - they own themselves. Update
526 * as appropriate.
527 */
528 if (clone->type == PLAYER)
529 owner = clone;
530 else
531 return;
532 }
533
534 set_owner (op, owner);
535} 482}
536 483
537/* Zero the key_values on op, decrementing the shared-string 484/* Zero the key_values on op, decrementing the shared-string
538 * refcounts and freeing the links. 485 * refcounts and freeing the links.
539 */ 486 */
555{ 502{
556 attachable_base::clear (); 503 attachable_base::clear ();
557 504
558 free_key_values (this); 505 free_key_values (this);
559 506
560 clear_owner (this); 507 owner = 0;
561
562 name = 0; 508 name = 0;
563 name_pl = 0; 509 name_pl = 0;
564 title = 0; 510 title = 0;
565 race = 0; 511 race = 0;
566 slaying = 0; 512 slaying = 0;
567 skill = 0; 513 skill = 0;
568 msg = 0; 514 msg = 0;
569 lore = 0; 515 lore = 0;
570 custom_name = 0; 516 custom_name = 0;
571 materialname = 0; 517 materialname = 0;
518 contr = 0;
519 below = 0;
520 above = 0;
521 inv = 0;
522 container = 0;
523 env = 0;
524 more = 0;
525 head = 0;
526 map = 0;
527 active_next = 0;
528 active_prev = 0;
572 529
573 memset (static_cast < object_pod * >(this), 0, sizeof (object_pod)); 530 memset (static_cast<object_pod *>(this), 0, sizeof (object_pod));
574 531
575 SET_FLAG (this, FLAG_REMOVED); 532 SET_FLAG (this, FLAG_REMOVED);
533
534 /* What is not cleared is next, prev, and count */
535
536 expmul = 1.0;
537 face = blank_face;
538 attacked_by_count = -1;
539
540 if (settings.casting_time)
541 casting_time = -1;
576} 542}
577 543
578void object::clone (object *destination) 544void object::clone (object *destination)
579{ 545{
580 *(object_copy *) destination = *this; 546 *(object_copy *)destination = *this;
581 *(object_pod *) destination = *this; 547 *(object_pod *)destination = *this;
582 548
583 if (self || cb) 549 if (self || cb)
584 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination)); 550 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination));
585}
586
587/*
588 * clear_object() frees everything allocated by an object, and also
589 * clears all variables and flags to default settings.
590 */
591
592void
593clear_object (object *op)
594{
595 op->clear ();
596
597 op->contr = NULL;
598 op->below = NULL;
599 op->above = NULL;
600 op->inv = NULL;
601 op->container = NULL;
602 op->env = NULL;
603 op->more = NULL;
604 op->head = NULL;
605 op->map = NULL;
606 op->refcount = 0;
607 op->active_next = NULL;
608 op->active_prev = NULL;
609 /* What is not cleared is next, prev, and count */
610
611 op->expmul = 1.0;
612 op->face = blank_face;
613 op->attacked_by_count = -1;
614
615 if (settings.casting_time)
616 op->casting_time = -1;
617} 551}
618 552
619/* 553/*
620 * copy object first frees everything allocated by the second object, 554 * copy object first frees everything allocated by the second object,
621 * and then copies the contends of the first object into the second 555 * and then copies the contends of the first object into the second
622 * object, allocating what needs to be allocated. Basically, any 556 * object, allocating what needs to be allocated. Basically, any
623 * data that is malloc'd needs to be re-malloc/copied. Otherwise, 557 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
624 * if the first object is freed, the pointers in the new object 558 * if the first object is freed, the pointers in the new object
625 * will point at garbage. 559 * will point at garbage.
626 */ 560 */
627
628void 561void
629copy_object (object *op2, object *op) 562copy_object (object *op2, object *op)
630{ 563{
631 bool is_freed = QUERY_FLAG (op, FLAG_FREED); 564 bool is_freed = QUERY_FLAG (op, FLAG_FREED);
632 bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); 565 bool is_removed = QUERY_FLAG (op, FLAG_REMOVED);
692/* 625/*
693 * Updates the speed of an object. If the speed changes from 0 to another 626 * Updates the speed of an object. If the speed changes from 0 to another
694 * value, or vice versa, then add/remove the object from the active list. 627 * value, or vice versa, then add/remove the object from the active list.
695 * This function needs to be called whenever the speed of an object changes. 628 * This function needs to be called whenever the speed of an object changes.
696 */ 629 */
697
698void 630void
699update_ob_speed (object *op) 631update_ob_speed (object *op)
700{ 632{
701 extern int arch_init; 633 extern int arch_init;
702 634
711 abort (); 643 abort ();
712#else 644#else
713 op->speed = 0; 645 op->speed = 0;
714#endif 646#endif
715 } 647 }
648
716 if (arch_init) 649 if (arch_init)
717 {
718 return; 650 return;
719 } 651
720 if (FABS (op->speed) > MIN_ACTIVE_SPEED) 652 if (FABS (op->speed) > MIN_ACTIVE_SPEED)
721 { 653 {
722 /* If already on active list, don't do anything */ 654 /* If already on active list, don't do anything */
723 if (op->active_next || op->active_prev || op == active_objects) 655 if (op->active_next || op->active_prev || op == active_objects)
724 return; 656 return;
725 657
726 /* process_events() expects us to insert the object at the beginning 658 /* process_events() expects us to insert the object at the beginning
727 * of the list. */ 659 * of the list. */
728 op->active_next = active_objects; 660 op->active_next = active_objects;
661
729 if (op->active_next != NULL) 662 if (op->active_next != NULL)
730 op->active_next->active_prev = op; 663 op->active_next->active_prev = op;
664
731 active_objects = op; 665 active_objects = op;
732 } 666 }
733 else 667 else
734 { 668 {
735 /* If not on the active list, nothing needs to be done */ 669 /* If not on the active list, nothing needs to be done */
737 return; 671 return;
738 672
739 if (op->active_prev == NULL) 673 if (op->active_prev == NULL)
740 { 674 {
741 active_objects = op->active_next; 675 active_objects = op->active_next;
676
742 if (op->active_next != NULL) 677 if (op->active_next != NULL)
743 op->active_next->active_prev = NULL; 678 op->active_next->active_prev = NULL;
744 } 679 }
745 else 680 else
746 { 681 {
747 op->active_prev->active_next = op->active_next; 682 op->active_prev->active_next = op->active_next;
683
748 if (op->active_next) 684 if (op->active_next)
749 op->active_next->active_prev = op->active_prev; 685 op->active_next->active_prev = op->active_prev;
750 } 686 }
687
751 op->active_next = NULL; 688 op->active_next = NULL;
752 op->active_prev = NULL; 689 op->active_prev = NULL;
753 } 690 }
754} 691}
755 692
901 838
902 if (op->more != NULL) 839 if (op->more != NULL)
903 update_object (op->more, action); 840 update_object (op->more, action);
904} 841}
905 842
906static unordered_vector < object *>mortals; 843static unordered_vector<object *> mortals;
907static 844static std::vector<object *, slice_allocator <object *> > freed;
908std::vector < object *>
909 freed;
910 845
911void object::free_mortals () 846void object::free_mortals ()
912{ 847{
913 for (unordered_vector < object *>::iterator i = mortals.begin (); i != mortals.end ();) 848 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();)
914 if (!(*i)->refcount) 849 if ((*i)->refcnt)
850 ++i; // further delay freeing
851 else
915 { 852 {
916 freed.push_back (*i); 853 freed.push_back (*i);//D
854 //delete *i;
917 mortals.erase (i); 855 mortals.erase (i);
918 } 856 }
919 else 857
920 ++i; 858 if (mortals.size() && 0)//D
859 LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D
921} 860}
922 861
923object::object () 862object::object ()
924{ 863{
925 SET_FLAG (this, FLAG_REMOVED); 864 SET_FLAG (this, FLAG_REMOVED);
968 objects = next; 907 objects = next;
969} 908}
970 909
971object *object::create () 910object *object::create ()
972{ 911{
973 object * 912 object *op;
974 op;
975 913
976 if (freed.empty ()) 914 if (freed.empty ())
977 op = new object; 915 op = new object;
978
979 else 916 else
980 { 917 {
981 // highly annoying, but the only way to get it stable right now 918 // highly annoying, but the only way to get it stable right now
982 op = freed.back (); 919 op = freed.back ();
983 freed.pop_back (); 920 freed.pop_back ();
1002void object::free (bool free_inventory) 939void object::free (bool free_inventory)
1003{ 940{
1004 if (QUERY_FLAG (this, FLAG_FREED)) 941 if (QUERY_FLAG (this, FLAG_FREED))
1005 return; 942 return;
1006 943
944 if (QUERY_FLAG (this, FLAG_FRIENDLY))
945 remove_friendly_object (this);
946
1007 if (!QUERY_FLAG (this, FLAG_REMOVED)) 947 if (!QUERY_FLAG (this, FLAG_REMOVED))
1008 remove_ob (this); 948 remove_ob (this);
1009
1010 if (QUERY_FLAG (this, FLAG_FRIENDLY))
1011 remove_friendly_object (this);
1012 949
1013 SET_FLAG (this, FLAG_FREED); 950 SET_FLAG (this, FLAG_FREED);
1014 951
1015 if (more) 952 if (more)
1016 { 953 {
1022 { 959 {
1023 /* Only if the space blocks everything do we not process - 960 /* Only if the space blocks everything do we not process -
1024 * if some form of movement is allowed, let objects 961 * if some form of movement is allowed, let objects
1025 * drop on that space. 962 * drop on that space.
1026 */ 963 */
1027 if (free_inventory || !map || map->in_memory != MAP_IN_MEMORY || (GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)) 964 if (free_inventory || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)
1028 { 965 {
1029 object *op = inv; 966 object *op = inv;
1030 967
1031 while (op) 968 while (op)
1032 { 969 {
1058 op = tmp; 995 op = tmp;
1059 } 996 }
1060 } 997 }
1061 } 998 }
1062 999
1063 clear_owner (this); 1000 owner = 0;
1064 1001
1065 /* Remove object from the active list */ 1002 /* Remove object from the active list */
1066 speed = 0; 1003 speed = 0;
1067 update_ob_speed (this); 1004 update_ob_speed (this);
1068 1005
1288 * This function goes through all objects below and including top, and 1225 * This function goes through all objects below and including top, and
1289 * merges op to the first matching object. 1226 * merges op to the first matching object.
1290 * If top is NULL, it is calculated. 1227 * If top is NULL, it is calculated.
1291 * Returns pointer to object if it succeded in the merge, otherwise NULL 1228 * Returns pointer to object if it succeded in the merge, otherwise NULL
1292 */ 1229 */
1293
1294object * 1230object *
1295merge_ob (object *op, object *top) 1231merge_ob (object *op, object *top)
1296{ 1232{
1297 if (!op->nrof) 1233 if (!op->nrof)
1298 return 0; 1234 return 0;
2786 * The returned string is shared. 2722 * The returned string is shared.
2787 */ 2723 */
2788const char * 2724const char *
2789get_ob_key_value (const object *op, const char *const key) 2725get_ob_key_value (const object *op, const char *const key)
2790{ 2726{
2791 key_value * 2727 key_value *link;
2792 link; 2728 shstr_cmp canonical_key (key);
2793 const char *
2794 canonical_key;
2795 2729
2796 canonical_key = shstr::find (key);
2797
2798 if (canonical_key == NULL) 2730 if (!canonical_key)
2799 { 2731 {
2800 /* 1. There being a field named key on any object 2732 /* 1. There being a field named key on any object
2801 * implies there'd be a shared string to find. 2733 * implies there'd be a shared string to find.
2802 * 2. Since there isn't, no object has this field. 2734 * 2. Since there isn't, no object has this field.
2803 * 3. Therefore, *this* object doesn't have this field. 2735 * 3. Therefore, *this* object doesn't have this field.
2804 */ 2736 */
2805 return NULL; 2737 return 0;
2806 } 2738 }
2807 2739
2808 /* This is copied from get_ob_key_link() above - 2740 /* This is copied from get_ob_key_link() above -
2809 * only 4 lines, and saves the function call overhead. 2741 * only 4 lines, and saves the function call overhead.
2810 */ 2742 */
2811 for (link = op->key_values; link != NULL; link = link->next) 2743 for (link = op->key_values; link; link = link->next)
2812 {
2813 if (link->key == canonical_key) 2744 if (link->key == canonical_key)
2814 {
2815 return link->value; 2745 return link->value;
2816 } 2746
2817 }
2818 return NULL; 2747 return 0;
2819} 2748}
2820 2749
2821 2750
2822/* 2751/*
2823 * Updates the canonical_key in op to value. 2752 * Updates the canonical_key in op to value.
2908{ 2837{
2909 shstr key_ (key); 2838 shstr key_ (key);
2910 2839
2911 return set_ob_key_value_s (op, key_, value, add_key); 2840 return set_ob_key_value_s (op, key_, value, add_key);
2912} 2841}
2842
2843object::depth_iterator::depth_iterator (object *container)
2844: iterator_base (container)
2845{
2846 while (item->inv)
2847 item = item->inv;
2848}
2849
2850void
2851object::depth_iterator::next ()
2852{
2853 if (item->below)
2854 {
2855 item = item->below;
2856
2857 while (item->inv)
2858 item = item->inv;
2859 }
2860 else
2861 item = item->env;
2862}
2863
2864// return a suitable string describing an objetc in enough detail to find it
2865const char *
2866object::debug_desc (char *info) const
2867{
2868 char info2[256 * 3];
2869 char *p = info;
2870
2871 p += snprintf (p, 256, "%d=\"%s%s%s\"",
2872 count,
2873 &name,
2874 title ? " " : "",
2875 title ? (const char *)title : "");
2876
2877 if (env)
2878 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2));
2879
2880 if (map)
2881 p += snprintf (p, 256, "(on %s@%d+%d)", map->path, x, y);
2882
2883 return info;
2884}
2885
2886const char *
2887object::debug_desc () const
2888{
2889 static char info[256 * 3];
2890 return debug_desc (info);
2891}
2892

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines