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.30 by root, Mon Sep 11 20:28:37 2006 UTC vs.
Revision 1.38 by root, Wed Sep 13 01:09:24 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
623/* 625/*
624 * 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
625 * 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.
626 * 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.
627 */ 629 */
628
629void 630void
630update_ob_speed (object *op) 631update_ob_speed (object *op)
631{ 632{
632 extern int arch_init; 633 extern int arch_init;
633 634
642 abort (); 643 abort ();
643#else 644#else
644 op->speed = 0; 645 op->speed = 0;
645#endif 646#endif
646 } 647 }
648
647 if (arch_init) 649 if (arch_init)
648 {
649 return; 650 return;
650 } 651
651 if (FABS (op->speed) > MIN_ACTIVE_SPEED) 652 if (FABS (op->speed) > MIN_ACTIVE_SPEED)
652 { 653 {
653 /* If already on active list, don't do anything */ 654 /* If already on active list, don't do anything */
654 if (op->active_next || op->active_prev || op == active_objects) 655 if (op->active_next || op->active_prev || op == active_objects)
655 return; 656 return;
656 657
657 /* process_events() expects us to insert the object at the beginning 658 /* process_events() expects us to insert the object at the beginning
658 * of the list. */ 659 * of the list. */
659 op->active_next = active_objects; 660 op->active_next = active_objects;
661
660 if (op->active_next != NULL) 662 if (op->active_next != NULL)
661 op->active_next->active_prev = op; 663 op->active_next->active_prev = op;
664
662 active_objects = op; 665 active_objects = op;
663 } 666 }
664 else 667 else
665 { 668 {
666 /* If not on the active list, nothing needs to be done */ 669 /* If not on the active list, nothing needs to be done */
668 return; 671 return;
669 672
670 if (op->active_prev == NULL) 673 if (op->active_prev == NULL)
671 { 674 {
672 active_objects = op->active_next; 675 active_objects = op->active_next;
676
673 if (op->active_next != NULL) 677 if (op->active_next != NULL)
674 op->active_next->active_prev = NULL; 678 op->active_next->active_prev = NULL;
675 } 679 }
676 else 680 else
677 { 681 {
678 op->active_prev->active_next = op->active_next; 682 op->active_prev->active_next = op->active_next;
683
679 if (op->active_next) 684 if (op->active_next)
680 op->active_next->active_prev = op->active_prev; 685 op->active_next->active_prev = op->active_prev;
681 } 686 }
687
682 op->active_next = NULL; 688 op->active_next = NULL;
683 op->active_prev = NULL; 689 op->active_prev = NULL;
684 } 690 }
685} 691}
686 692
833 if (op->more != NULL) 839 if (op->more != NULL)
834 update_object (op->more, action); 840 update_object (op->more, action);
835} 841}
836 842
837static unordered_vector<object *> mortals; 843static unordered_vector<object *> mortals;
838static std::vector<object *> freed; 844static std::vector<object *, slice_allocator <object *> > freed;
839 845
840void object::free_mortals () 846void object::free_mortals ()
841{ 847{
842 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) 848 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();)
843 if ((*i)->refcnt) 849 if ((*i)->refcnt)
847 freed.push_back (*i);//D 853 freed.push_back (*i);//D
848 //delete *i; 854 //delete *i;
849 mortals.erase (i); 855 mortals.erase (i);
850 } 856 }
851 857
852 if (mortals.size())//D 858 if (mortals.size() && 0)//D
853 LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D 859 LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D
854} 860}
855 861
856object::object () 862object::object ()
857{ 863{
882 888
883void object::unlink () 889void object::unlink ()
884{ 890{
885 count = 0; 891 count = 0;
886 892
893 if (this == objects)
894 objects = next;
895
887 /* Remove this object from the list of used objects */ 896 /* Remove this object from the list of used objects */
888 if (prev) 897 if (prev)
889 { 898 {
890 prev->next = next; 899 prev->next = next;
891 prev = 0; 900 prev = 0;
894 if (next) 903 if (next)
895 { 904 {
896 next->prev = prev; 905 next->prev = prev;
897 next = 0; 906 next = 0;
898 } 907 }
899
900 if (this == objects)
901 objects = next;
902} 908}
903 909
904object *object::create () 910object *object::create ()
905{ 911{
906 object * 912 object *op;
907 op;
908 913
909 if (freed.empty ()) 914 if (freed.empty ())
910 op = new object; 915 op = new object;
911 else 916 else
912 { 917 {
934void object::free (bool free_inventory) 939void object::free (bool free_inventory)
935{ 940{
936 if (QUERY_FLAG (this, FLAG_FREED)) 941 if (QUERY_FLAG (this, FLAG_FREED))
937 return; 942 return;
938 943
944 if (QUERY_FLAG (this, FLAG_FRIENDLY))
945 remove_friendly_object (this);
946
939 if (!QUERY_FLAG (this, FLAG_REMOVED)) 947 if (!QUERY_FLAG (this, FLAG_REMOVED))
940 remove_ob (this); 948 remove_ob (this);
941
942 if (QUERY_FLAG (this, FLAG_FRIENDLY))
943 remove_friendly_object (this);
944 949
945 SET_FLAG (this, FLAG_FREED); 950 SET_FLAG (this, FLAG_FREED);
946 951
947 if (more) 952 if (more)
948 { 953 {
954 { 959 {
955 /* Only if the space blocks everything do we not process - 960 /* Only if the space blocks everything do we not process -
956 * if some form of movement is allowed, let objects 961 * if some form of movement is allowed, let objects
957 * drop on that space. 962 * drop on that space.
958 */ 963 */
959 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)
960 { 965 {
961 object *op = inv; 966 object *op = inv;
962 967
963 while (op) 968 while (op)
964 { 969 {
1220 * This function goes through all objects below and including top, and 1225 * This function goes through all objects below and including top, and
1221 * merges op to the first matching object. 1226 * merges op to the first matching object.
1222 * If top is NULL, it is calculated. 1227 * If top is NULL, it is calculated.
1223 * 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
1224 */ 1229 */
1225
1226object * 1230object *
1227merge_ob (object *op, object *top) 1231merge_ob (object *op, object *top)
1228{ 1232{
1229 if (!op->nrof) 1233 if (!op->nrof)
1230 return 0; 1234 return 0;
2718 * The returned string is shared. 2722 * The returned string is shared.
2719 */ 2723 */
2720const char * 2724const char *
2721get_ob_key_value (const object *op, const char *const key) 2725get_ob_key_value (const object *op, const char *const key)
2722{ 2726{
2723 key_value * 2727 key_value *link;
2724 link; 2728 shstr_cmp canonical_key (key);
2725 const char *
2726 canonical_key;
2727 2729
2728 canonical_key = shstr::find (key);
2729
2730 if (canonical_key == NULL) 2730 if (!canonical_key)
2731 { 2731 {
2732 /* 1. There being a field named key on any object 2732 /* 1. There being a field named key on any object
2733 * implies there'd be a shared string to find. 2733 * implies there'd be a shared string to find.
2734 * 2. Since there isn't, no object has this field. 2734 * 2. Since there isn't, no object has this field.
2735 * 3. Therefore, *this* object doesn't have this field. 2735 * 3. Therefore, *this* object doesn't have this field.
2736 */ 2736 */
2737 return NULL; 2737 return 0;
2738 } 2738 }
2739 2739
2740 /* This is copied from get_ob_key_link() above - 2740 /* This is copied from get_ob_key_link() above -
2741 * only 4 lines, and saves the function call overhead. 2741 * only 4 lines, and saves the function call overhead.
2742 */ 2742 */
2743 for (link = op->key_values; link != NULL; link = link->next) 2743 for (link = op->key_values; link; link = link->next)
2744 {
2745 if (link->key == canonical_key) 2744 if (link->key == canonical_key)
2746 {
2747 return link->value; 2745 return link->value;
2748 } 2746
2749 }
2750 return NULL; 2747 return 0;
2751} 2748}
2752 2749
2753 2750
2754/* 2751/*
2755 * Updates the canonical_key in op to value. 2752 * Updates the canonical_key in op to value.
2840{ 2837{
2841 shstr key_ (key); 2838 shstr key_ (key);
2842 2839
2843 return set_ob_key_value_s (op, key_, value, add_key); 2840 return set_ob_key_value_s (op, key_, value, add_key);
2844} 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