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.203 by root, Fri Apr 11 13:59:05 2008 UTC vs.
Revision 1.207 by root, Sun Apr 20 23:25:09 2008 UTC

22 */ 22 */
23 23
24/* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects. 24/* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects.
25 sub/add_weight will transcend the environment updating the carrying 25 sub/add_weight will transcend the environment updating the carrying
26 variable. */ 26 variable. */
27
27#include <global.h> 28#include <global.h>
28#include <stdio.h> 29#include <stdio.h>
29#include <sys/types.h> 30#include <sys/types.h>
30#include <sys/uio.h> 31#include <sys/uio.h>
31#include <object.h> 32#include <object.h>
34#include <loader.h> 35#include <loader.h>
35 36
36#include <bitset> 37#include <bitset>
37 38
38UUID UUID::cur; 39UUID UUID::cur;
40static uint64_t seq_next_save;
39static const uint64 UUID_SKIP = 1<<19; 41static const uint64 UUID_GAP = 1<<19;
40 42
41objectvec objects; 43objectvec objects;
42activevec actives; 44activevec actives;
43 45
44short freearr_x[SIZEOFFREE] = { 46short freearr_x[SIZEOFFREE] = {
81{ 83{
82 char filename[MAX_BUF]; 84 char filename[MAX_BUF];
83 85
84 sprintf (filename, "%s/uuid", settings.localdir); 86 sprintf (filename, "%s/uuid", settings.localdir);
85 87
88 seq_next_save = 0;
89
86 FILE *fp; 90 FILE *fp;
87 91
88 if (!(fp = fopen (filename, "r"))) 92 if (!(fp = fopen (filename, "r")))
89 { 93 {
90 if (errno == ENOENT) 94 if (errno == ENOENT)
91 { 95 {
92 LOG (llevInfo, "RESET uid to 1\n"); 96 LOG (llevInfo, "RESET uid to 1\n");
93 UUID::cur.seq = 0; 97 UUID::cur.seq = 0;
94 write_uuid (UUID_SKIP, true); 98 write_uuid (UUID_GAP, true);
95 return; 99 return;
96 } 100 }
97 101
98 LOG (llevError, "FATAL: cannot open %s for reading!\n", filename); 102 LOG (llevError, "FATAL: cannot open %s for reading!\n", filename);
99 _exit (1); 103 _exit (1);
109 _exit (1); 113 _exit (1);
110 } 114 }
111 115
112 LOG (llevDebug, "read UUID: %s\n", UUID::cur.c_str ()); 116 LOG (llevDebug, "read UUID: %s\n", UUID::cur.c_str ());
113 117
114 write_uuid (UUID_SKIP, true); 118 write_uuid (UUID_GAP, true);
115 fclose (fp); 119 fclose (fp);
116} 120}
117 121
118UUID 122UUID
119UUID::gen () 123UUID::gen ()
120{ 124{
121 UUID uid; 125 UUID uid;
122 126
123 uid.seq = ++cur.seq; 127 uid.seq = ++cur.seq;
124 128
125 if (expect_false (!(cur.seq & (UUID_SKIP - 1)))) 129 if (expect_false (cur.seq >= seq_next_save))
130 {
131 seq_next_save = UUID::cur.seq + (UUID_GAP >> 1);
126 write_uuid (UUID_SKIP, false); 132 write_uuid (UUID_GAP, false);
133 }
134
127 135
128 return uid; 136 return uid;
129} 137}
130 138
131void 139void
133{ 141{
134 read_uuid (); 142 read_uuid ();
135} 143}
136 144
137/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 145/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
138static int 146static bool
139compare_ob_value_lists_one (const object *wants, const object *has) 147compare_ob_value_lists_one (const object *wants, const object *has)
140{ 148{
141 key_value *wants_field; 149 key_value *wants_field;
142 150
143 /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both 151 /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both
151 key_value *has_field; 159 key_value *has_field;
152 160
153 /* Look for a field in has with the same key. */ 161 /* Look for a field in has with the same key. */
154 has_field = get_ob_key_link (has, wants_field->key); 162 has_field = get_ob_key_link (has, wants_field->key);
155 163
156 if (has_field == NULL) 164 if (!has_field)
157 {
158 /* No field with that name. */ 165 return 0; /* No field with that name. */
159 return FALSE;
160 }
161 166
162 /* Found the matching field. */ 167 /* Found the matching field. */
163 if (has_field->value != wants_field->value) 168 if (has_field->value != wants_field->value)
164 {
165 /* Values don't match, so this half of the comparison is false. */ 169 return 0; /* Values don't match, so this half of the comparison is false. */
166 return FALSE;
167 }
168 170
169 /* If we get here, we found a match. Now for the next field in wants. */ 171 /* If we get here, we found a match. Now for the next field in wants. */
170 } 172 }
171 173
172 /* If we get here, every field in wants has a matching field in has. */ 174 /* If we get here, every field in wants has a matching field in has. */
173 return TRUE; 175 return 1;
174} 176}
175 177
176/* Returns TRUE if ob1 has the same key_values as ob2. */ 178/* Returns TRUE if ob1 has the same key_values as ob2. */
177static int 179static bool
178compare_ob_value_lists (const object *ob1, const object *ob2) 180compare_ob_value_lists (const object *ob1, const object *ob2)
179{ 181{
180 /* However, there may be fields in has which aren't partnered in wants, 182 /* However, there may be fields in has which aren't partnered in wants,
181 * so we need to run the comparison *twice*. :( 183 * so we need to run the comparison *twice*. :(
182 */ 184 */
203 || ob1->speed != ob2->speed 205 || ob1->speed != ob2->speed
204 || ob1->value != ob2->value 206 || ob1->value != ob2->value
205 || ob1->name != ob2->name) 207 || ob1->name != ob2->name)
206 return 0; 208 return 0;
207 209
208 //TODO: this ain't working well, use nicer and correct overflow check 210 /* Do not merge objects if nrof would overflow. First part checks
209 /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that 211 * for unsigned overflow (2c), second part checks wether the result
210 * value could not be stored in a sint32 (which unfortunately sometimes is 212 * would fit into a 32 bit signed int, which is often used to hold
211 * used to store nrof). 213 * nrof values.
212 */ 214 */
213 if (ob1->nrof + ob2->nrof >= 1UL << 31) 215 if (~ob1->nrof < ob2->nrof || ob1->nrof + ob2->nrof > (1UL << 31))
214 return 0; 216 return 0;
215 217
216 /* If the objects have been identified, set the BEEN_APPLIED flag. 218 /* If the objects have been identified, set the BEEN_APPLIED flag.
217 * This is to the comparison of the flags below will be OK. We 219 * This is to the comparison of the flags below will be OK. We
218 * just can't ignore the been applied or identified flags, as they 220 * just can't ignore the been applied or identified flags, as they
219 * are not equal - just if it has been identified, the been_applied 221 * are not equal - just if it has been identified, the been_applied
220 * flags lose any meaning. 222 * flags lose any meaning.
221 */ 223 */
222 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED)) 224 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED))
250 || ob1->move_off != ob2->move_off 252 || ob1->move_off != ob2->move_off
251 || ob1->move_slow != ob2->move_slow 253 || ob1->move_slow != ob2->move_slow
252 || ob1->move_slow_penalty != ob2->move_slow_penalty) 254 || ob1->move_slow_penalty != ob2->move_slow_penalty)
253 return 0; 255 return 0;
254 256
255 /* This is really a spellbook check - really, we should 257 /* This is really a spellbook check - we should in general
256 * check all objects in the inventory. 258 * not merge objects with real inventories, as splitting them
259 * is hard.
257 */ 260 */
258 if (ob1->inv || ob2->inv) 261 if (ob1->inv || ob2->inv)
259 { 262 {
260 if (!(ob1->inv && ob2->inv)) 263 if (!(ob1->inv && ob2->inv))
261 return 0; /* inventories differ in length */ 264 return 0; /* inventories differ in length */
262 265
263 if (ob1->inv->below || ob2->inv->below) 266 if (ob1->inv->below || ob2->inv->below)
264 return 0; /* more than one object in inv */ 267 return 0; /* more than one object in inv */
265 268
266 if (!object::can_merge (ob1->inv, ob2->inv)) 269 if (!object::can_merge (ob1->inv, ob2->inv))
267 return 0; /* inventory objexts differ */ 270 return 0; /* inventory objects differ */
268 271
269 /* inventory ok - still need to check rest of this object to see 272 /* inventory ok - still need to check rest of this object to see
270 * if it is valid. 273 * if it is valid.
271 */ 274 */
272 } 275 }
291 if (ob1->level != ob2->level) 294 if (ob1->level != ob2->level)
292 return 0; 295 return 0;
293 break; 296 break;
294 } 297 }
295 298
296 if (ob1->key_values != NULL || ob2->key_values != NULL) 299 if (ob1->key_values || ob2->key_values)
297 { 300 {
298 /* At least one of these has key_values. */ 301 /* At least one of these has key_values. */
299 if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) 302 if ((!ob1->key_values) != (!ob2->key_values))
300 /* One has fields, but the other one doesn't. */ 303 /* One has fields, but the other one doesn't. */
301 return 0; 304 return 0;
302 else if (!compare_ob_value_lists (ob1, ob2)) 305 else if (!compare_ob_value_lists (ob1, ob2))
303 return 0; 306 return 0;
304 } 307 }
324 327
325 /* Everything passes, must be OK. */ 328 /* Everything passes, must be OK. */
326 return 1; 329 return 1;
327} 330}
328 331
332static sint32
333weight_adjust (object *op, sint32 weight)
334{
335 return op->type == CONTAINER
336 ? lerp (weight, 0, 100, 0, 100 - op->stats.Str)
337 : weight;
338}
339
329/* 340/*
341 * add_weight(object, weight) adds the specified weight to an object,
342 * and also updates how much the environment(s) is/are carrying.
343 */
344static void
345add_weight (object *op, sint32 weight)
346{
347 while (op)
348 {
349 weight = weight_adjust (op, weight);
350
351 op->carrying += weight;
352 op = op->env;
353 }
354}
355
356/*
357 * sub_weight() recursively (outwards) subtracts a number from the
358 * weight of an object (and what is carried by it's environment(s)).
359 */
360static void
361sub_weight (object *op, long weight)
362{
363 add_weight (op, -weight);
364}
365
366/*
330 * sum_weight() is a recursive function which calculates the weight 367 * this is a recursive function which calculates the weight
331 * an object is carrying. It goes through in figures out how much 368 * an object is carrying. It goes through op and figures out how much
332 * containers are carrying, and sums it up. 369 * containers are carrying, and sums it up.
333 */ 370 */
334long 371void
335sum_weight (object *op) 372object::update_weight ()
336{ 373{
337 long sum; 374 sint32 sum = 0;
338 object *inv;
339 375
340 for (sum = 0, inv = op->inv; inv; inv = inv->below) 376 for (object *op = inv; op; op = op->below)
341 { 377 {
342 if (inv->inv) 378 if (op->inv)
343 sum_weight (inv); 379 op->update_weight ();
344 380
345 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); 381 sum += op->total_weight ();
346 } 382 }
347 383
348 if (op->type == CONTAINER && op->stats.Str) 384 carrying = weight_adjust (this, sum);
349 sum = (sum * (100 - op->stats.Str)) / 100;
350
351 if (op->carrying != sum)
352 op->carrying = sum;
353
354 return sum;
355}
356
357/**
358 * Return the outermost environment object for a given object.
359 */
360
361object *
362object_get_env_recursive (object *op)
363{
364 while (op->env != NULL)
365 op = op->env;
366 return op;
367} 385}
368 386
369/* 387/*
370 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. 388 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
371 * Some error messages. 389 * Some error messages.
861 * if some form of movement is allowed, let objects 879 * if some form of movement is allowed, let objects
862 * drop on that space. 880 * drop on that space.
863 */ 881 */
864 if (!drop_to_ground 882 if (!drop_to_ground
865 || !map 883 || !map
866 || map->in_memory != MAP_IN_MEMORY 884 || map->in_memory != MAP_ACTIVE
867 || map->nodrop 885 || map->nodrop
868 || ms ().move_block == MOVE_ALL) 886 || ms ().move_block == MOVE_ALL)
869 { 887 {
870 while (inv) 888 while (inv)
871 { 889 {
933 freed_map->width = 3; 951 freed_map->width = 3;
934 freed_map->height = 3; 952 freed_map->height = 3;
935 freed_map->nodrop = 1; 953 freed_map->nodrop = 1;
936 954
937 freed_map->alloc (); 955 freed_map->alloc ();
938 freed_map->in_memory = MAP_IN_MEMORY; 956 freed_map->in_memory = MAP_ACTIVE;
939 } 957 }
940 958
941 map = freed_map; 959 map = freed_map;
942 x = 1; 960 x = 1;
943 y = 1; 961 y = 1;
972 play_sound (sound_destroy); 990 play_sound (sound_destroy);
973 else if (flag [FLAG_MONSTER]) 991 else if (flag [FLAG_MONSTER])
974 play_sound (sound_find ("monster_destroy")); // quick hack, too lazy to create a generic mechanism 992 play_sound (sound_find ("monster_destroy")); // quick hack, too lazy to create a generic mechanism
975 993
976 attachable::destroy (); 994 attachable::destroy ();
977}
978
979/*
980 * sub_weight() recursively (outwards) subtracts a number from the
981 * weight of an object (and what is carried by it's environment(s)).
982 */
983void
984sub_weight (object *op, signed long weight)
985{
986 while (op != NULL)
987 {
988 if (op->type == CONTAINER)
989 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
990
991 op->carrying -= weight;
992 op = op->env;
993 }
994} 995}
995 996
996/* op->remove (): 997/* op->remove ():
997 * This function removes the object op from the linked list of objects 998 * This function removes the object op from the linked list of objects
998 * which it is currently tied to. When this function is done, the 999 * which it is currently tied to. When this function is done, the
1477} 1478}
1478 1479
1479object * 1480object *
1480object::insert_at (object *where, object *originator, int flags) 1481object::insert_at (object *where, object *originator, int flags)
1481{ 1482{
1483 if (where->env)
1484 return where->env->insert (this);
1485 else
1482 return where->map->insert (this, where->x, where->y, originator, flags); 1486 return where->map->insert (this, where->x, where->y, originator, flags);
1483} 1487}
1484 1488
1485/* 1489/*
1486 * get_split_ob(ob,nr) splits up ob into two parts. The part which 1490 * get_split_ob(ob,nr) splits up ob into two parts. The part which
1487 * is returned contains nr objects, and the remaining parts contains 1491 * is returned contains nr objects, and the remaining parts contains
1507 orig_ob->destroy (1); 1511 orig_ob->destroy (1);
1508 else if (!is_removed) 1512 else if (!is_removed)
1509 { 1513 {
1510 if (orig_ob->env != NULL) 1514 if (orig_ob->env != NULL)
1511 sub_weight (orig_ob->env, orig_ob->weight * nr); 1515 sub_weight (orig_ob->env, orig_ob->weight * nr);
1512 if (orig_ob->env == NULL && orig_ob->map->in_memory != MAP_IN_MEMORY) 1516 if (orig_ob->env == NULL && orig_ob->map->in_memory != MAP_ACTIVE)
1513 { 1517 {
1514 strcpy (errmsg, "Tried to split object whose map is not in memory."); 1518 strcpy (errmsg, "Tried to split object whose map is not in memory.");
1515 LOG (llevDebug, "Error, Tried to split object whose map is not in memory.\n"); 1519 LOG (llevDebug, "Error, Tried to split object whose map is not in memory.\n");
1516 return NULL; 1520 return NULL;
1517 } 1521 }
1604 return op; 1608 return op;
1605 else 1609 else
1606 { 1610 {
1607 op->destroy (); 1611 op->destroy ();
1608 return 0; 1612 return 0;
1609 }
1610}
1611
1612/*
1613 * add_weight(object, weight) adds the specified weight to an object,
1614 * and also updates how much the environment(s) is/are carrying.
1615 */
1616void
1617add_weight (object *op, signed long weight)
1618{
1619 while (op != NULL)
1620 {
1621 if (op->type == CONTAINER)
1622 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
1623
1624 op->carrying += weight;
1625 op = op->env;
1626 } 1613 }
1627} 1614}
1628 1615
1629object * 1616object *
1630insert_ob_in_ob (object *op, object *where) 1617insert_ob_in_ob (object *op, object *where)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines