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.200 by root, Sun Jan 13 08:55:41 2008 UTC vs.
Revision 1.205 by root, Sun Apr 13 01:34:08 2008 UTC

33#include <sproto.h> 33#include <sproto.h>
34#include <loader.h> 34#include <loader.h>
35 35
36#include <bitset> 36#include <bitset>
37 37
38int nrofallocobjects = 0; 38UUID UUID::cur;
39static UUID uuid; 39static uint64_t seq_next_save;
40const uint64 UUID_SKIP = 1<<19; 40static const uint64 UUID_GAP = 1<<19;
41 41
42objectvec objects; 42objectvec objects;
43activevec actives; 43activevec actives;
44 44
45short 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, 45short freearr_x[SIZEOFFREE] = {
46 0,
47 0, 1, 1, 1, 0, -1, -1, -1,
48 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1,
46 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1 49 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1
47}; 50};
48short freearr_y[SIZEOFFREE] = { 0, -1, -1, 0, 1, 1, 1, 0, -1, -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, 51short freearr_y[SIZEOFFREE] = {
52 0,
53 -1, -1, 0, 1, 1, 1, 0, -1,
54 -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2,
49 -3, -3, -3, -3, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3 55 -3, -3, -3, -3, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3
50}; 56};
51int maxfree[SIZEOFFREE] = { 0, 9, 10, 13, 14, 17, 18, 21, 22, 25, 26, 27, 30, 31, 32, 33, 36, 37, 39, 39, 42, 43, 44, 45, 57int maxfree[SIZEOFFREE] = {
58 0,
59 9, 10, 13, 14, 17, 18, 21, 22,
60 25, 26, 27, 30, 31, 32, 33, 36, 37, 39, 39, 42, 43, 44, 45, 48,
52 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49 61 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49
53}; 62};
54int freedir[SIZEOFFREE] = { 63int freedir[SIZEOFFREE] = {
55 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, 64 0,
65 1, 2, 3, 4, 5, 6, 7, 8,
66 1, 2, 2, 2, 3, 4, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8,
56 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8 67 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8
57}; 68};
58 69
59static void 70static void
60write_uuid (void) 71write_uuid (uval64 skip, bool sync)
61{ 72{
62 char filename1[MAX_BUF], filename2[MAX_BUF]; 73 CALL_BEGIN (2);
63 74 CALL_ARG_SV (newSVval64 (skip));
64 sprintf (filename1, "%s/uuid", settings.localdir); 75 CALL_ARG_SV (boolSV (sync));
65 sprintf (filename2, "%s/uuid~", settings.localdir); 76 CALL_CALL ("cf::write_uuid", G_DISCARD);
66 77 CALL_END;
67 FILE *fp;
68
69 if (!(fp = fopen (filename2, "w")))
70 {
71 LOG (llevError, "ERROR: cannot open %s for writing, unable to write UUID!\n", filename2);
72 return;
73 }
74
75 fprintf (fp, "<1,%llx>\n", (unsigned long long)uuid.seq + UUID_SKIP * 2);
76 fclose (fp);
77 rename (filename2, filename1);
78} 78}
79 79
80static void 80static void
81read_uuid (void) 81read_uuid (void)
82{ 82{
83 char filename[MAX_BUF]; 83 char filename[MAX_BUF];
84 84
85 sprintf (filename, "%s/uuid", settings.localdir); 85 sprintf (filename, "%s/uuid", settings.localdir);
86 86
87 seq_next_save = 0;
88
87 FILE *fp; 89 FILE *fp;
88 90
89 if (!(fp = fopen (filename, "r"))) 91 if (!(fp = fopen (filename, "r")))
90 { 92 {
91 if (errno == ENOENT) 93 if (errno == ENOENT)
92 { 94 {
93 LOG (llevInfo, "RESET uid to 1\n"); 95 LOG (llevInfo, "RESET uid to 1\n");
94 uuid.seq = 0; 96 UUID::cur.seq = 0;
95 write_uuid (); 97 write_uuid (UUID_GAP, true);
96 return; 98 return;
97 } 99 }
98 100
99 LOG (llevError, "FATAL: cannot open %s for reading!\n", filename); 101 LOG (llevError, "FATAL: cannot open %s for reading!\n", filename);
100 _exit (1); 102 _exit (1);
101 } 103 }
102 104
103 int version; 105 UUID::BUF buf;
104 unsigned long long uid; 106 buf[0] = 0;
105 if (2 != fscanf (fp, "<%d,%llx>\n", &version, &uid) || version != 1) 107 fgets (buf, sizeof (buf), fp);
108
109 if (!UUID::cur.parse (buf))
106 { 110 {
107 LOG (llevError, "FATAL: error reading uid from %s!\n", filename); 111 LOG (llevError, "FATAL: error reading uid from %s (%s)!\n", filename, buf);
108 _exit (1); 112 _exit (1);
109 } 113 }
110 114
111 uuid.seq = uid; 115 LOG (llevDebug, "read UUID: %s\n", UUID::cur.c_str ());
112 write_uuid (); 116
113 LOG (llevDebug, "read UID: %" PRId64 "\n", uid); 117 write_uuid (UUID_GAP, true);
114 fclose (fp); 118 fclose (fp);
115} 119}
116 120
117UUID 121UUID
118gen_uuid () 122UUID::gen ()
119{ 123{
120 UUID uid; 124 UUID uid;
121 125
122 uid.seq = ++uuid.seq; 126 uid.seq = ++cur.seq;
123 127
124 if (!(uuid.seq & (UUID_SKIP - 1))) 128 if (expect_false (cur.seq >= seq_next_save))
125 write_uuid (); 129 {
130 seq_next_save = UUID::cur.seq + (UUID_GAP >> 1);
131 write_uuid (UUID_GAP, false);
132 }
133
126 134
127 return uid; 135 return uid;
128} 136}
129 137
130void 138void
131init_uuid () 139UUID::init ()
132{ 140{
133 read_uuid (); 141 read_uuid ();
134} 142}
135 143
136/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 144/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
137static int 145static bool
138compare_ob_value_lists_one (const object *wants, const object *has) 146compare_ob_value_lists_one (const object *wants, const object *has)
139{ 147{
140 key_value *wants_field; 148 key_value *wants_field;
141 149
142 /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both 150 /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both
150 key_value *has_field; 158 key_value *has_field;
151 159
152 /* Look for a field in has with the same key. */ 160 /* Look for a field in has with the same key. */
153 has_field = get_ob_key_link (has, wants_field->key); 161 has_field = get_ob_key_link (has, wants_field->key);
154 162
155 if (has_field == NULL) 163 if (!has_field)
156 {
157 /* No field with that name. */ 164 return 0; /* No field with that name. */
158 return FALSE;
159 }
160 165
161 /* Found the matching field. */ 166 /* Found the matching field. */
162 if (has_field->value != wants_field->value) 167 if (has_field->value != wants_field->value)
163 {
164 /* Values don't match, so this half of the comparison is false. */ 168 return 0; /* Values don't match, so this half of the comparison is false. */
165 return FALSE;
166 }
167 169
168 /* If we get here, we found a match. Now for the next field in wants. */ 170 /* If we get here, we found a match. Now for the next field in wants. */
169 } 171 }
170 172
171 /* If we get here, every field in wants has a matching field in has. */ 173 /* If we get here, every field in wants has a matching field in has. */
172 return TRUE; 174 return 1;
173} 175}
174 176
175/* Returns TRUE if ob1 has the same key_values as ob2. */ 177/* Returns TRUE if ob1 has the same key_values as ob2. */
176static int 178static bool
177compare_ob_value_lists (const object *ob1, const object *ob2) 179compare_ob_value_lists (const object *ob1, const object *ob2)
178{ 180{
179 /* However, there may be fields in has which aren't partnered in wants, 181 /* However, there may be fields in has which aren't partnered in wants,
180 * so we need to run the comparison *twice*. :( 182 * so we need to run the comparison *twice*. :(
181 */ 183 */
202 || ob1->speed != ob2->speed 204 || ob1->speed != ob2->speed
203 || ob1->value != ob2->value 205 || ob1->value != ob2->value
204 || ob1->name != ob2->name) 206 || ob1->name != ob2->name)
205 return 0; 207 return 0;
206 208
207 //TODO: this ain't working well, use nicer and correct overflow check 209 /* Do not merge objects if nrof would overflow. First part checks
208 /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that 210 * for unsigned overflow (2c), second part checks wether the result
209 * value could not be stored in a sint32 (which unfortunately sometimes is 211 * would fit into a 32 bit signed int, which is often used to hold
210 * used to store nrof). 212 * nrof values.
211 */ 213 */
212 if (ob1->nrof + ob2->nrof >= 1UL << 31) 214 if (~ob1->nrof < ob2->nrof || ob1->nrof + ob2->nrof > (1UL << 31))
213 return 0; 215 return 0;
214 216
215 /* If the objects have been identified, set the BEEN_APPLIED flag. 217 /* If the objects have been identified, set the BEEN_APPLIED flag.
216 * This is to the comparison of the flags below will be OK. We 218 * This is to the comparison of the flags below will be OK. We
217 * just can't ignore the been applied or identified flags, as they 219 * just can't ignore the been applied or identified flags, as they
218 * are not equal - just if it has been identified, the been_applied 220 * are not equal - just if it has been identified, the been_applied
219 * flags lose any meaning. 221 * flags lose any meaning.
220 */ 222 */
221 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED)) 223 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED))
249 || ob1->move_off != ob2->move_off 251 || ob1->move_off != ob2->move_off
250 || ob1->move_slow != ob2->move_slow 252 || ob1->move_slow != ob2->move_slow
251 || ob1->move_slow_penalty != ob2->move_slow_penalty) 253 || ob1->move_slow_penalty != ob2->move_slow_penalty)
252 return 0; 254 return 0;
253 255
254 /* This is really a spellbook check - really, we should 256 /* This is really a spellbook check - we should in general
255 * check all objects in the inventory. 257 * not merge objects with real inventories, as splitting them
258 * is hard.
256 */ 259 */
257 if (ob1->inv || ob2->inv) 260 if (ob1->inv || ob2->inv)
258 { 261 {
259 if (!(ob1->inv && ob2->inv)) 262 if (!(ob1->inv && ob2->inv))
260 return 0; /* inventories differ in length */ 263 return 0; /* inventories differ in length */
261 264
262 if (ob1->inv->below || ob2->inv->below) 265 if (ob1->inv->below || ob2->inv->below)
263 return 0; /* more than one object in inv */ 266 return 0; /* more than one object in inv */
264 267
265 if (!object::can_merge (ob1->inv, ob2->inv)) 268 if (!object::can_merge (ob1->inv, ob2->inv))
266 return 0; /* inventory objexts differ */ 269 return 0; /* inventory objects differ */
267 270
268 /* inventory ok - still need to check rest of this object to see 271 /* inventory ok - still need to check rest of this object to see
269 * if it is valid. 272 * if it is valid.
270 */ 273 */
271 } 274 }
290 if (ob1->level != ob2->level) 293 if (ob1->level != ob2->level)
291 return 0; 294 return 0;
292 break; 295 break;
293 } 296 }
294 297
295 if (ob1->key_values != NULL || ob2->key_values != NULL) 298 if (ob1->key_values || ob2->key_values)
296 { 299 {
297 /* At least one of these has key_values. */ 300 /* At least one of these has key_values. */
298 if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) 301 if ((!ob1->key_values) != (!ob2->key_values))
299 /* One has fields, but the other one doesn't. */ 302 /* One has fields, but the other one doesn't. */
300 return 0; 303 return 0;
301 else if (!compare_ob_value_lists (ob1, ob2)) 304 else if (!compare_ob_value_lists (ob1, ob2))
302 return 0; 305 return 0;
303 } 306 }
427 for_all_objects (op) 430 for_all_objects (op)
428 if (op->name == str_) 431 if (op->name == str_)
429 break; 432 break;
430 433
431 return op; 434 return op;
432}
433
434void
435free_all_object_data ()
436{
437 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects);
438} 435}
439 436
440/* 437/*
441 * Sets the owner and sets the skill and exp pointers to owner's current 438 * Sets the owner and sets the skill and exp pointers to owner's current
442 * skill and experience objects. 439 * skill and experience objects.
603 600
604void 601void
605object::instantiate () 602object::instantiate ()
606{ 603{
607 if (!uuid.seq) // HACK 604 if (!uuid.seq) // HACK
608 uuid = gen_uuid (); 605 uuid = UUID::gen ();
609 606
610 speed_left = -0.1f; 607 speed_left = -0.1f;
611 /* copy the body_info to the body_used - this is only really 608 /* copy the body_info to the body_used - this is only really
612 * need for monsters, but doesn't hurt to do it for everything. 609 * need for monsters, but doesn't hurt to do it for everything.
613 * by doing so, when a monster is created, it has good starting 610 * by doing so, when a monster is created, it has good starting
772static int object_count; 769static int object_count;
773 770
774void object::link () 771void object::link ()
775{ 772{
776 assert (!index);//D 773 assert (!index);//D
777 uuid = gen_uuid (); 774 uuid = UUID::gen ();
778 count = ++object_count; 775 count = ++object_count;
779 776
780 refcnt_inc (); 777 refcnt_inc ();
781 objects.insert (this); 778 objects.insert (this);
782} 779}
935 932
936 freed_map->path = "<freed objects map>"; 933 freed_map->path = "<freed objects map>";
937 freed_map->name = "/internal/freed_objects_map"; 934 freed_map->name = "/internal/freed_objects_map";
938 freed_map->width = 3; 935 freed_map->width = 3;
939 freed_map->height = 3; 936 freed_map->height = 3;
937 freed_map->nodrop = 1;
940 938
941 freed_map->alloc (); 939 freed_map->alloc ();
942 freed_map->in_memory = MAP_IN_MEMORY; 940 freed_map->in_memory = MAP_IN_MEMORY;
943 } 941 }
944 942
1481} 1479}
1482 1480
1483object * 1481object *
1484object::insert_at (object *where, object *originator, int flags) 1482object::insert_at (object *where, object *originator, int flags)
1485{ 1483{
1484 if (where->env)
1485 return where->env->insert (this);
1486 else
1486 return where->map->insert (this, where->x, where->y, originator, flags); 1487 return where->map->insert (this, where->x, where->y, originator, flags);
1487} 1488}
1488 1489
1489/* 1490/*
1490 * get_split_ob(ob,nr) splits up ob into two parts. The part which 1491 * get_split_ob(ob,nr) splits up ob into two parts. The part which
1491 * is returned contains nr objects, and the remaining parts contains 1492 * is returned contains nr objects, and the remaining parts contains
2613{ 2614{
2614 char flagdesc[512]; 2615 char flagdesc[512];
2615 char info2[256 * 4]; 2616 char info2[256 * 4];
2616 char *p = info; 2617 char *p = info;
2617 2618
2618 p += snprintf (p, 512, "{cnt:%d,uuid:<1.%" PRIx64 ">,name:\"%s\"%s%s,flags:[%s],type:%d}", 2619 p += snprintf (p, 512, "{cnt:%d,uuid:%s,name:\"%s\"%s%s,flags:[%s],type:%d}",
2619 count, uuid.seq, 2620 count,
2621 uuid.c_str (),
2620 &name, 2622 &name,
2621 title ? "\",title:\"" : "", 2623 title ? "\",title:\"" : "",
2622 title ? (const char *)title : "", 2624 title ? (const char *)title : "",
2623 flag_desc (flagdesc, 512), type); 2625 flag_desc (flagdesc, 512), type);
2624 2626

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines