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.204 by root, Fri Apr 11 17:01:52 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>
140{ 141{
141 read_uuid (); 142 read_uuid ();
142} 143}
143 144
144/* 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. */
145static int 146static bool
146compare_ob_value_lists_one (const object *wants, const object *has) 147compare_ob_value_lists_one (const object *wants, const object *has)
147{ 148{
148 key_value *wants_field; 149 key_value *wants_field;
149 150
150 /* 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
158 key_value *has_field; 159 key_value *has_field;
159 160
160 /* Look for a field in has with the same key. */ 161 /* Look for a field in has with the same key. */
161 has_field = get_ob_key_link (has, wants_field->key); 162 has_field = get_ob_key_link (has, wants_field->key);
162 163
163 if (has_field == NULL) 164 if (!has_field)
164 {
165 /* No field with that name. */ 165 return 0; /* No field with that name. */
166 return FALSE;
167 }
168 166
169 /* Found the matching field. */ 167 /* Found the matching field. */
170 if (has_field->value != wants_field->value) 168 if (has_field->value != wants_field->value)
171 {
172 /* 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. */
173 return FALSE;
174 }
175 170
176 /* 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. */
177 } 172 }
178 173
179 /* 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. */
180 return TRUE; 175 return 1;
181} 176}
182 177
183/* Returns TRUE if ob1 has the same key_values as ob2. */ 178/* Returns TRUE if ob1 has the same key_values as ob2. */
184static int 179static bool
185compare_ob_value_lists (const object *ob1, const object *ob2) 180compare_ob_value_lists (const object *ob1, const object *ob2)
186{ 181{
187 /* 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,
188 * so we need to run the comparison *twice*. :( 183 * so we need to run the comparison *twice*. :(
189 */ 184 */
210 || ob1->speed != ob2->speed 205 || ob1->speed != ob2->speed
211 || ob1->value != ob2->value 206 || ob1->value != ob2->value
212 || ob1->name != ob2->name) 207 || ob1->name != ob2->name)
213 return 0; 208 return 0;
214 209
215 //TODO: this ain't working well, use nicer and correct overflow check 210 /* Do not merge objects if nrof would overflow. First part checks
216 /* 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
217 * 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
218 * used to store nrof). 213 * nrof values.
219 */ 214 */
220 if (ob1->nrof + ob2->nrof >= 1UL << 31) 215 if (~ob1->nrof < ob2->nrof || ob1->nrof + ob2->nrof > (1UL << 31))
221 return 0; 216 return 0;
222 217
223 /* If the objects have been identified, set the BEEN_APPLIED flag. 218 /* If the objects have been identified, set the BEEN_APPLIED flag.
224 * 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
225 * 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
226 * 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
227 * flags lose any meaning. 222 * flags lose any meaning.
228 */ 223 */
229 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED)) 224 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED))
257 || ob1->move_off != ob2->move_off 252 || ob1->move_off != ob2->move_off
258 || ob1->move_slow != ob2->move_slow 253 || ob1->move_slow != ob2->move_slow
259 || ob1->move_slow_penalty != ob2->move_slow_penalty) 254 || ob1->move_slow_penalty != ob2->move_slow_penalty)
260 return 0; 255 return 0;
261 256
262 /* This is really a spellbook check - really, we should 257 /* This is really a spellbook check - we should in general
263 * check all objects in the inventory. 258 * not merge objects with real inventories, as splitting them
259 * is hard.
264 */ 260 */
265 if (ob1->inv || ob2->inv) 261 if (ob1->inv || ob2->inv)
266 { 262 {
267 if (!(ob1->inv && ob2->inv)) 263 if (!(ob1->inv && ob2->inv))
268 return 0; /* inventories differ in length */ 264 return 0; /* inventories differ in length */
269 265
270 if (ob1->inv->below || ob2->inv->below) 266 if (ob1->inv->below || ob2->inv->below)
271 return 0; /* more than one object in inv */ 267 return 0; /* more than one object in inv */
272 268
273 if (!object::can_merge (ob1->inv, ob2->inv)) 269 if (!object::can_merge (ob1->inv, ob2->inv))
274 return 0; /* inventory objexts differ */ 270 return 0; /* inventory objects differ */
275 271
276 /* 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
277 * if it is valid. 273 * if it is valid.
278 */ 274 */
279 } 275 }
298 if (ob1->level != ob2->level) 294 if (ob1->level != ob2->level)
299 return 0; 295 return 0;
300 break; 296 break;
301 } 297 }
302 298
303 if (ob1->key_values != NULL || ob2->key_values != NULL) 299 if (ob1->key_values || ob2->key_values)
304 { 300 {
305 /* At least one of these has key_values. */ 301 /* At least one of these has key_values. */
306 if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) 302 if ((!ob1->key_values) != (!ob2->key_values))
307 /* One has fields, but the other one doesn't. */ 303 /* One has fields, but the other one doesn't. */
308 return 0; 304 return 0;
309 else if (!compare_ob_value_lists (ob1, ob2)) 305 else if (!compare_ob_value_lists (ob1, ob2))
310 return 0; 306 return 0;
311 } 307 }
331 327
332 /* Everything passes, must be OK. */ 328 /* Everything passes, must be OK. */
333 return 1; 329 return 1;
334} 330}
335 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
336/* 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/*
337 * sum_weight() is a recursive function which calculates the weight 367 * this is a recursive function which calculates the weight
338 * 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
339 * containers are carrying, and sums it up. 369 * containers are carrying, and sums it up.
340 */ 370 */
341long 371void
342sum_weight (object *op) 372object::update_weight ()
343{ 373{
344 long sum; 374 sint32 sum = 0;
345 object *inv;
346 375
347 for (sum = 0, inv = op->inv; inv; inv = inv->below) 376 for (object *op = inv; op; op = op->below)
348 { 377 {
349 if (inv->inv) 378 if (op->inv)
350 sum_weight (inv); 379 op->update_weight ();
351 380
352 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); 381 sum += op->total_weight ();
353 } 382 }
354 383
355 if (op->type == CONTAINER && op->stats.Str) 384 carrying = weight_adjust (this, sum);
356 sum = (sum * (100 - op->stats.Str)) / 100;
357
358 if (op->carrying != sum)
359 op->carrying = sum;
360
361 return sum;
362}
363
364/**
365 * Return the outermost environment object for a given object.
366 */
367
368object *
369object_get_env_recursive (object *op)
370{
371 while (op->env != NULL)
372 op = op->env;
373 return op;
374} 385}
375 386
376/* 387/*
377 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. 388 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
378 * Some error messages. 389 * Some error messages.
868 * if some form of movement is allowed, let objects 879 * if some form of movement is allowed, let objects
869 * drop on that space. 880 * drop on that space.
870 */ 881 */
871 if (!drop_to_ground 882 if (!drop_to_ground
872 || !map 883 || !map
873 || map->in_memory != MAP_IN_MEMORY 884 || map->in_memory != MAP_ACTIVE
874 || map->nodrop 885 || map->nodrop
875 || ms ().move_block == MOVE_ALL) 886 || ms ().move_block == MOVE_ALL)
876 { 887 {
877 while (inv) 888 while (inv)
878 { 889 {
940 freed_map->width = 3; 951 freed_map->width = 3;
941 freed_map->height = 3; 952 freed_map->height = 3;
942 freed_map->nodrop = 1; 953 freed_map->nodrop = 1;
943 954
944 freed_map->alloc (); 955 freed_map->alloc ();
945 freed_map->in_memory = MAP_IN_MEMORY; 956 freed_map->in_memory = MAP_ACTIVE;
946 } 957 }
947 958
948 map = freed_map; 959 map = freed_map;
949 x = 1; 960 x = 1;
950 y = 1; 961 y = 1;
979 play_sound (sound_destroy); 990 play_sound (sound_destroy);
980 else if (flag [FLAG_MONSTER]) 991 else if (flag [FLAG_MONSTER])
981 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
982 993
983 attachable::destroy (); 994 attachable::destroy ();
984}
985
986/*
987 * sub_weight() recursively (outwards) subtracts a number from the
988 * weight of an object (and what is carried by it's environment(s)).
989 */
990void
991sub_weight (object *op, signed long weight)
992{
993 while (op != NULL)
994 {
995 if (op->type == CONTAINER)
996 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
997
998 op->carrying -= weight;
999 op = op->env;
1000 }
1001} 995}
1002 996
1003/* op->remove (): 997/* op->remove ():
1004 * 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
1005 * 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
1484} 1478}
1485 1479
1486object * 1480object *
1487object::insert_at (object *where, object *originator, int flags) 1481object::insert_at (object *where, object *originator, int flags)
1488{ 1482{
1483 if (where->env)
1484 return where->env->insert (this);
1485 else
1489 return where->map->insert (this, where->x, where->y, originator, flags); 1486 return where->map->insert (this, where->x, where->y, originator, flags);
1490} 1487}
1491 1488
1492/* 1489/*
1493 * 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
1494 * is returned contains nr objects, and the remaining parts contains 1491 * is returned contains nr objects, and the remaining parts contains
1514 orig_ob->destroy (1); 1511 orig_ob->destroy (1);
1515 else if (!is_removed) 1512 else if (!is_removed)
1516 { 1513 {
1517 if (orig_ob->env != NULL) 1514 if (orig_ob->env != NULL)
1518 sub_weight (orig_ob->env, orig_ob->weight * nr); 1515 sub_weight (orig_ob->env, orig_ob->weight * nr);
1519 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)
1520 { 1517 {
1521 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.");
1522 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");
1523 return NULL; 1520 return NULL;
1524 } 1521 }
1611 return op; 1608 return op;
1612 else 1609 else
1613 { 1610 {
1614 op->destroy (); 1611 op->destroy ();
1615 return 0; 1612 return 0;
1616 }
1617}
1618
1619/*
1620 * add_weight(object, weight) adds the specified weight to an object,
1621 * and also updates how much the environment(s) is/are carrying.
1622 */
1623void
1624add_weight (object *op, signed long weight)
1625{
1626 while (op != NULL)
1627 {
1628 if (op->type == CONTAINER)
1629 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
1630
1631 op->carrying += weight;
1632 op = op->env;
1633 } 1613 }
1634} 1614}
1635 1615
1636object * 1616object *
1637insert_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