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.36 by root, Tue Sep 12 19:20:06 2006 UTC vs.
Revision 1.42 by root, Thu Sep 14 01:19:47 2006 UTC

32#include <object.h> 32#include <object.h>
33#include <funcpoint.h> 33#include <funcpoint.h>
34#include <loader.h> 34#include <loader.h>
35 35
36int nrofallocobjects = 0; 36int nrofallocobjects = 0;
37static UUID uuid;
38const uint64 UUID_SKIP = 1<<19;
37 39
38object *objects; /* Pointer to the list of used objects */ 40object *objects; /* Pointer to the list of used objects */
39object *active_objects; /* List of active objects that need to be processed */ 41object *active_objects; /* List of active objects that need to be processed */
40 42
41short 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,
49}; 51};
50int freedir[SIZEOFFREE] = { 52int freedir[SIZEOFFREE] = {
51 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,
52 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
53}; 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}
54 133
55/* 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. */
56static int 135static int
57compare_ob_value_lists_one (const object *wants, const object *has) 136compare_ob_value_lists_one (const object *wants, const object *has)
58{ 137{
254 { 333 {
255 if (inv->inv) 334 if (inv->inv)
256 sum_weight (inv); 335 sum_weight (inv);
257 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); 336 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1);
258 } 337 }
338
259 if (op->type == CONTAINER && op->stats.Str) 339 if (op->type == CONTAINER && op->stats.Str)
260 sum = (sum * (100 - op->stats.Str)) / 100; 340 sum = (sum * (100 - op->stats.Str)) / 100;
341
261 if (op->carrying != sum) 342 if (op->carrying != sum)
262 op->carrying = sum; 343 op->carrying = sum;
344
263 return sum; 345 return sum;
264} 346}
265 347
266/** 348/**
267 * Return the outermost environment object for a given object. 349 * Return the outermost environment object for a given object.
836 if (op->more != NULL) 918 if (op->more != NULL)
837 update_object (op->more, action); 919 update_object (op->more, action);
838} 920}
839 921
840static unordered_vector<object *> mortals; 922static unordered_vector<object *> mortals;
841static std::vector<object *> freed;
842 923
843void object::free_mortals () 924void object::free_mortals ()
844{ 925{
845 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) 926 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();)
846 if ((*i)->refcnt) 927 if ((*i)->refcnt)
847 ++i; // further delay freeing 928 ++i; // further delay freeing
848 else 929 else
849 { 930 {
850 freed.push_back (*i);//D
851 //delete *i; 931 delete *i;
852 mortals.erase (i); 932 mortals.erase (i);
853 } 933 }
854 934
855 if (mortals.size() && 0)//D 935 static int lastmortals = 0;//D
936
937 if (mortals.size() != lastmortals)//D
938 {
939 lastmortals = mortals.size ();//D
856 LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D 940 LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D
941 }
857} 942}
858 943
859object::object () 944object::object ()
860{ 945{
861 SET_FLAG (this, FLAG_REMOVED); 946 SET_FLAG (this, FLAG_REMOVED);
871} 956}
872 957
873void object::link () 958void object::link ()
874{ 959{
875 count = ++ob_count; 960 count = ++ob_count;
961 uuid = gen_uuid ();
876 962
877 prev = 0; 963 prev = 0;
878 next = objects; 964 next = objects;
879 965
880 if (objects) 966 if (objects)
883 objects = this; 969 objects = this;
884} 970}
885 971
886void object::unlink () 972void object::unlink ()
887{ 973{
888 count = 0; 974 //count = 0;//D
889 975 if (!prev && !next) return;//D
890 /* Remove this object from the list of used objects */
891 if (prev)
892 {
893 prev->next = next;
894 prev = 0;
895 }
896
897 if (next)
898 {
899 next->prev = prev;
900 next = 0;
901 }
902 976
903 if (this == objects) 977 if (this == objects)
904 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;
905} 986}
906 987
907object *object::create () 988object *object::create ()
908{ 989{
909 object *op;
910
911 if (freed.empty ())
912 op = new object; 990 object *op = new object;
913 else
914 {
915 // highly annoying, but the only way to get it stable right now
916 op = freed.back ();
917 freed.pop_back ();
918 op->~object ();
919 new ((void *) op) object;
920 }
921
922 op->link (); 991 op->link ();
923 return op; 992 return op;
924} 993}
925 994
926/* 995/*
1222 * This function goes through all objects below and including top, and 1291 * This function goes through all objects below and including top, and
1223 * merges op to the first matching object. 1292 * merges op to the first matching object.
1224 * If top is NULL, it is calculated. 1293 * If top is NULL, it is calculated.
1225 * 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
1226 */ 1295 */
1227
1228object * 1296object *
1229merge_ob (object *op, object *top) 1297merge_ob (object *op, object *top)
1230{ 1298{
1231 if (!op->nrof) 1299 if (!op->nrof)
1232 return 0; 1300 return 0;
2653 2721
2654 if (tempfile == NULL) 2722 if (tempfile == NULL)
2655 { 2723 {
2656 LOG (llevError, "Error - Unable to access load object temp file\n"); 2724 LOG (llevError, "Error - Unable to access load object temp file\n");
2657 return NULL; 2725 return NULL;
2658 }; 2726 }
2727
2659 fprintf (tempfile, obstr); 2728 fprintf (tempfile, obstr);
2660 fclose (tempfile); 2729 fclose (tempfile);
2661 2730
2662 op = get_object (); 2731 op = get_object ();
2663 2732

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines