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.32 by root, Mon Sep 11 23:53:30 2006 UTC vs.
Revision 1.42 by root, Thu Sep 14 01:19:47 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;
37static UUID uuid;
38const uint64 UUID_SKIP = 1<<19;
38 39
39object *objects; /* Pointer to the list of used objects */ 40object *objects; /* Pointer to the list of used objects */
40object *active_objects; /* List of active objects that need to be processed */ 41object *active_objects; /* List of active objects that need to be processed */
41 42
42short freearr_x[SIZEOFFREE] = { 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, 43short freearr_x[SIZEOFFREE] = { 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,
50}; 51};
51int freedir[SIZEOFFREE] = { 52int freedir[SIZEOFFREE] = {
52 0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 2, 2, 3, 4, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8, 53 0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 2, 2, 3, 4, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8,
53 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8 54 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8
54}; 55};
56
57static void
58write_uuid (void)
59{
60 char filename1[MAX_BUF], filename2[MAX_BUF];
61
62 sprintf (filename1, "%s/uuid", settings.localdir);
63 sprintf (filename2, "%s/uuid~", settings.localdir);
64
65 FILE *fp;
66
67 if (!(fp = fopen (filename2, "w")))
68 {
69 LOG (llevError, "ERROR: cannot open %s for writing, unable to write UUID!\n", filename2);
70 return;
71 }
72
73 fprintf (fp, "<1,%llx>\n", (unsigned long long)uuid.seq + UUID_SKIP * 2);
74 fclose (fp);
75 rename (filename2, filename1);
76}
77
78static void
79read_uuid (void)
80{
81 char filename[MAX_BUF];
82
83 sprintf (filename, "%s/uuid", settings.localdir);
84
85 FILE *fp;
86
87 if (!(fp = fopen (filename, "r")))
88 {
89 if (errno == ENOENT)
90 {
91 LOG (llevInfo, "RESET uid to 1\n");
92 uuid.seq = 0;
93 write_uuid ();
94 return;
95 }
96
97 LOG (llevError, "FATAL: cannot open %s for reading!\n", filename);
98 _exit (1);
99 }
100
101 int version;
102 unsigned long long uid;
103 if (2 != fscanf (fp, "<%d,%llx>\n", &version, &uid) || version != 1)
104 {
105 LOG (llevError, "FATAL: error reading uid from %s!\n", filename);
106 _exit (1);
107 }
108
109 uuid.seq = uid;
110 write_uuid ();
111 LOG (llevDebug, "read UID: %lld\n", uid);
112 fclose (fp);
113}
114
115UUID
116gen_uuid ()
117{
118 UUID uid;
119
120 uid.seq = ++uuid.seq;
121
122 if (!(uuid.seq & (UUID_SKIP - 1)))
123 write_uuid ();
124
125 return uid;
126}
127
128void
129init_uuid ()
130{
131 read_uuid ();
132}
55 133
56/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 134/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
57static int 135static int
58compare_ob_value_lists_one (const object *wants, const object *has) 136compare_ob_value_lists_one (const object *wants, const object *has)
59{ 137{
255 { 333 {
256 if (inv->inv) 334 if (inv->inv)
257 sum_weight (inv); 335 sum_weight (inv);
258 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); 336 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1);
259 } 337 }
338
260 if (op->type == CONTAINER && op->stats.Str) 339 if (op->type == CONTAINER && op->stats.Str)
261 sum = (sum * (100 - op->stats.Str)) / 100; 340 sum = (sum * (100 - op->stats.Str)) / 100;
341
262 if (op->carrying != sum) 342 if (op->carrying != sum)
263 op->carrying = sum; 343 op->carrying = sum;
344
264 return sum; 345 return sum;
265} 346}
266 347
267/** 348/**
268 * Return the outermost environment object for a given object. 349 * Return the outermost environment object for a given object.
417 */ 498 */
418 499
419object * 500object *
420find_object_name (const char *str) 501find_object_name (const char *str)
421{ 502{
422 const char *name = shstr::find (str); 503 shstr_cmp str_ (str);
423 object *op; 504 object *op;
424 505
425 for (op = objects; op != NULL; op = op->next) 506 for (op = objects; op != NULL; op = op->next)
426 if (&op->name == name) 507 if (op->name == str_)
427 break; 508 break;
428 509
429 return op; 510 return op;
430} 511}
431 512
837 if (op->more != NULL) 918 if (op->more != NULL)
838 update_object (op->more, action); 919 update_object (op->more, action);
839} 920}
840 921
841static unordered_vector<object *> mortals; 922static unordered_vector<object *> mortals;
842static std::vector<object *> freed;
843 923
844void object::free_mortals () 924void object::free_mortals ()
845{ 925{
846 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) 926 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();)
847 if ((*i)->refcnt) 927 if ((*i)->refcnt)
848 ++i; // further delay freeing 928 ++i; // further delay freeing
849 else 929 else
850 { 930 {
851 freed.push_back (*i);//D
852 //delete *i; 931 delete *i;
853 mortals.erase (i); 932 mortals.erase (i);
854 } 933 }
855 934
935 static int lastmortals = 0;//D
936
856 if (mortals.size())//D 937 if (mortals.size() != lastmortals)//D
938 {
939 lastmortals = mortals.size ();//D
857 LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D 940 LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D
941 }
858} 942}
859 943
860object::object () 944object::object ()
861{ 945{
862 SET_FLAG (this, FLAG_REMOVED); 946 SET_FLAG (this, FLAG_REMOVED);
872} 956}
873 957
874void object::link () 958void object::link ()
875{ 959{
876 count = ++ob_count; 960 count = ++ob_count;
961 uuid = gen_uuid ();
877 962
878 prev = 0; 963 prev = 0;
879 next = objects; 964 next = objects;
880 965
881 if (objects) 966 if (objects)
884 objects = this; 969 objects = this;
885} 970}
886 971
887void object::unlink () 972void object::unlink ()
888{ 973{
889 count = 0; 974 //count = 0;//D
890 975 if (!prev && !next) return;//D
891 /* Remove this object from the list of used objects */
892 if (prev)
893 {
894 prev->next = next;
895 prev = 0;
896 }
897
898 if (next)
899 {
900 next->prev = prev;
901 next = 0;
902 }
903 976
904 if (this == objects) 977 if (this == objects)
905 objects = next; 978 objects = next;
979
980 /* Remove this object from the list of used objects */
981 if (prev) prev->next = next;
982 if (next) next->prev = prev;
983
984 prev = 0;
985 next = 0;
906} 986}
907 987
908object *object::create () 988object *object::create ()
909{ 989{
910 object *op;
911
912 if (freed.empty ())
913 op = new object; 990 object *op = new object;
914 else
915 {
916 // highly annoying, but the only way to get it stable right now
917 op = freed.back ();
918 freed.pop_back ();
919 op->~object ();
920 new ((void *) op) object;
921 }
922
923 op->link (); 991 op->link ();
924 return op; 992 return op;
925} 993}
926 994
927/* 995/*
1223 * This function goes through all objects below and including top, and 1291 * This function goes through all objects below and including top, and
1224 * merges op to the first matching object. 1292 * merges op to the first matching object.
1225 * If top is NULL, it is calculated. 1293 * If top is NULL, it is calculated.
1226 * Returns pointer to object if it succeded in the merge, otherwise NULL 1294 * Returns pointer to object if it succeded in the merge, otherwise NULL
1227 */ 1295 */
1228
1229object * 1296object *
1230merge_ob (object *op, object *top) 1297merge_ob (object *op, object *top)
1231{ 1298{
1232 if (!op->nrof) 1299 if (!op->nrof)
1233 return 0; 1300 return 0;
2654 2721
2655 if (tempfile == NULL) 2722 if (tempfile == NULL)
2656 { 2723 {
2657 LOG (llevError, "Error - Unable to access load object temp file\n"); 2724 LOG (llevError, "Error - Unable to access load object temp file\n");
2658 return NULL; 2725 return NULL;
2659 }; 2726 }
2727
2660 fprintf (tempfile, obstr); 2728 fprintf (tempfile, obstr);
2661 fclose (tempfile); 2729 fclose (tempfile);
2662 2730
2663 op = get_object (); 2731 op = get_object ();
2664 2732
2721 * The returned string is shared. 2789 * The returned string is shared.
2722 */ 2790 */
2723const char * 2791const char *
2724get_ob_key_value (const object *op, const char *const key) 2792get_ob_key_value (const object *op, const char *const key)
2725{ 2793{
2726 key_value * 2794 key_value *link;
2727 link; 2795 shstr_cmp canonical_key (key);
2728 const char *
2729 canonical_key;
2730 2796
2731 canonical_key = shstr::find (key);
2732
2733 if (canonical_key == NULL) 2797 if (!canonical_key)
2734 { 2798 {
2735 /* 1. There being a field named key on any object 2799 /* 1. There being a field named key on any object
2736 * implies there'd be a shared string to find. 2800 * implies there'd be a shared string to find.
2737 * 2. Since there isn't, no object has this field. 2801 * 2. Since there isn't, no object has this field.
2738 * 3. Therefore, *this* object doesn't have this field. 2802 * 3. Therefore, *this* object doesn't have this field.
2739 */ 2803 */
2740 return NULL; 2804 return 0;
2741 } 2805 }
2742 2806
2743 /* This is copied from get_ob_key_link() above - 2807 /* This is copied from get_ob_key_link() above -
2744 * only 4 lines, and saves the function call overhead. 2808 * only 4 lines, and saves the function call overhead.
2745 */ 2809 */
2746 for (link = op->key_values; link != NULL; link = link->next) 2810 for (link = op->key_values; link; link = link->next)
2747 {
2748 if (link->key == canonical_key) 2811 if (link->key == canonical_key)
2749 {
2750 return link->value; 2812 return link->value;
2751 } 2813
2752 }
2753 return NULL; 2814 return 0;
2754} 2815}
2755 2816
2756 2817
2757/* 2818/*
2758 * Updates the canonical_key in op to value. 2819 * Updates the canonical_key in op to value.
2844 shstr key_ (key); 2905 shstr key_ (key);
2845 2906
2846 return set_ob_key_value_s (op, key_, value, add_key); 2907 return set_ob_key_value_s (op, key_, value, add_key);
2847} 2908}
2848 2909
2910object::depth_iterator::depth_iterator (object *container)
2911: iterator_base (container)
2912{
2913 while (item->inv)
2914 item = item->inv;
2915}
2916
2849void 2917void
2850object::deep_iterator::next () 2918object::depth_iterator::next ()
2851{ 2919{
2852 if (item->inv)
2853 item = item->inv;
2854 else if (item->below) 2920 if (item->below)
2921 {
2855 item = item->below; 2922 item = item->below;
2923
2924 while (item->inv)
2925 item = item->inv;
2926 }
2856 else 2927 else
2857 item = item->env->below; 2928 item = item->env;
2858} 2929}
2930
2931// return a suitable string describing an objetc in enough detail to find it
2932const char *
2933object::debug_desc (char *info) const
2934{
2935 char info2[256 * 3];
2936 char *p = info;
2937
2938 p += snprintf (p, 256, "%d=\"%s%s%s\"",
2939 count,
2940 &name,
2941 title ? " " : "",
2942 title ? (const char *)title : "");
2943
2944 if (env)
2945 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2));
2946
2947 if (map)
2948 p += snprintf (p, 256, "(on %s@%d+%d)", map->path, x, y);
2949
2950 return info;
2951}
2952
2953const char *
2954object::debug_desc () const
2955{
2956 static char info[256 * 3];
2957 return debug_desc (info);
2958}
2959

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines