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.210 by root, Mon Apr 21 07:01:40 2008 UTC vs.
Revision 1.214 by root, Tue Apr 22 07:01:46 2008 UTC

24#include <global.h> 24#include <global.h>
25#include <stdio.h> 25#include <stdio.h>
26#include <sys/types.h> 26#include <sys/types.h>
27#include <sys/uio.h> 27#include <sys/uio.h>
28#include <object.h> 28#include <object.h>
29#include <funcpoint.h>
30#include <sproto.h> 29#include <sproto.h>
31#include <loader.h> 30#include <loader.h>
32 31
33#include <bitset> 32#include <bitset>
34 33
331 330
332 /* Everything passes, must be OK. */ 331 /* Everything passes, must be OK. */
333 return 1; 332 return 1;
334} 333}
335 334
335// find player who can see this object
336object *
337object::visible_to () const
338{
339 if (!flag [FLAG_REMOVED])
340 {
341 // see if we are in a container of sorts
342 if (env)
343 {
344 // the player inventory itself is always visible
345 if (env->type == PLAYER)
346 return env;
347
348 // else a player could have our env open
349 object *envest = env->outer_env ();
350
351 // the player itself is always on a map, so we will find him here
352 // even if our inv is in a player.
353 if (envest->is_on_map ())
354 if (object *pl = envest->ms ().player ())
355 if (pl->container == env)
356 return pl;
357 }
358 else
359 {
360 // maybe there is a player standing on the same mapspace
361 // this will catch the case where "this" is a player
362 if (object *pl = ms ().player ())
363 return pl;
364 }
365 }
366
367 return 0;
368}
369
336// adjust weight per container type ("of holding") 370// adjust weight per container type ("of holding")
337static sint32 371static sint32
338weight_adjust (object *op, sint32 weight) 372weight_adjust (object *op, sint32 weight)
339{ 373{
340 return op->type == CONTAINER 374 return op->type == CONTAINER
351{ 385{
352 while (op) 386 while (op)
353 { 387 {
354 weight = weight_adjust (op, weight); 388 weight = weight_adjust (op, weight);
355 389
390 if (!weight)
391 return;
392
356 op->carrying += weight; 393 op->carrying += weight;
394
395 if (object *pl = op->visible_to ())
396 esrv_update_item (UPD_WEIGHT, pl, op);
397
357 op = op->env; 398 op = op->env;
358 } 399 }
359} 400}
360 401
361/* 402/*
374 op->update_weight (); 415 op->update_weight ();
375 416
376 sum += op->total_weight (); 417 sum += op->total_weight ();
377 } 418 }
378 419
379 carrying = weight_adjust (this, sum); 420 sum = weight_adjust (this, sum);
421
422 if (sum != carrying)
423 {
424 carrying = sum;
425
426 if (object *pl = visible_to ())
427 esrv_update_item (UPD_WEIGHT, pl, this);
428 }
380} 429}
381 430
382/* 431/*
383 * Used by: Server DM commands: dumpbelow, dump. Some error messages. 432 * Used by: Server DM commands: dumpbelow, dump. Some error messages.
384 */ 433 */
916} 965}
917 966
918void 967void
919object::do_destroy () 968object::do_destroy ()
920{ 969{
921 attachable::do_destroy ();
922
923 if (flag [FLAG_IS_LINKED]) 970 if (flag [FLAG_IS_LINKED])
924 remove_button_link (this); 971 remove_button_link (this);
925 972
926 if (flag [FLAG_FRIENDLY]) 973 if (flag [FLAG_FRIENDLY])
927 remove_friendly_object (this); 974 remove_friendly_object (this);
928 975
929 if (!flag [FLAG_REMOVED])
930 remove (); 976 remove ();
977
978 attachable::do_destroy ();
931 979
932 destroy_inv (true); 980 destroy_inv (true);
933 981
934 deactivate (); 982 deactivate ();
935 unlink (); 983 unlink ();
1003object::do_remove () 1051object::do_remove ()
1004{ 1052{
1005 object *tmp, *last = 0; 1053 object *tmp, *last = 0;
1006 object *otmp; 1054 object *otmp;
1007 1055
1008 if (QUERY_FLAG (this, FLAG_REMOVED)) 1056 if (flag [FLAG_REMOVED])
1009 return; 1057 return;
1010 1058
1011 SET_FLAG (this, FLAG_REMOVED);
1012 INVOKE_OBJECT (REMOVE, this); 1059 INVOKE_OBJECT (REMOVE, this);
1060
1061 if (object *pl = visible_to ())
1062 esrv_del_item (pl->contr, count);
1063
1064 flag [FLAG_REMOVED] = true;
1013 1065
1014 if (more) 1066 if (more)
1015 more->remove (); 1067 more->remove ();
1016 1068
1017 /* 1069 /*
1152 if (!top) 1204 if (!top)
1153 for (top = op; top && top->above; top = top->above) 1205 for (top = op; top && top->above; top = top->above)
1154 ; 1206 ;
1155 1207
1156 for (; top; top = top->below) 1208 for (; top; top = top->below)
1157 {
1158 if (top == op)
1159 continue;
1160
1161 if (object::can_merge (op, top)) 1209 if (object::can_merge (op, top))
1162 { 1210 {
1163 top->nrof += op->nrof; 1211 top->nrof += op->nrof;
1164 1212
1165/* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/ 1213 if (object *pl = top->visible_to ())
1166 op->weight = 0; /* Don't want any adjustements now */ 1214 esrv_update_item (UPD_NROF, pl, top);
1215
1216 op->weight = 0; // cancel the addition above
1217 op->carrying = 0; // must be 0 already
1218
1167 op->destroy (); 1219 op->destroy (1);
1220
1168 return top; 1221 return top;
1169 } 1222 }
1170 }
1171 1223
1172 return 0; 1224 return 0;
1173} 1225}
1174 1226
1175void 1227void
1477 return where->env->insert (this); 1529 return where->env->insert (this);
1478 else 1530 else
1479 return where->map->insert (this, where->x, where->y, originator, flags); 1531 return where->map->insert (this, where->x, where->y, originator, flags);
1480} 1532}
1481 1533
1482// find player who can see this object
1483static object *
1484visible_to (object *op)
1485{
1486 if (!op->flag [FLAG_REMOVED])
1487 {
1488 // see if we are in a container of sorts
1489 if (object *env = op->env)
1490 {
1491 // the player inventory itself is always visible
1492 if (env->type == PLAYER)
1493 return env;
1494
1495 // else a player could have our env open
1496 env = env->outer_env ();
1497
1498 // the player itself is always on a map, so we will find him here
1499 // even if our inv is in a player.
1500 if (env->is_on_map ())
1501 if (object *pl = env->ms ().player ())
1502 if (pl->container == op->env)
1503 return pl;
1504 }
1505 else
1506 {
1507 // maybe there is a player standing on the same mapspace
1508 if (object *pl = env->ms ().player ())
1509 return pl;
1510 }
1511 }
1512
1513 return 0;
1514}
1515
1516/* 1534/*
1517 * decrease(object, number) decreases a specified number from 1535 * decrease(object, number) decreases a specified number from
1518 * the amount of an object. If the amount reaches 0, the object 1536 * the amount of an object. If the amount reaches 0, the object
1519 * is subsequently removed and freed. 1537 * is subsequently removed and freed.
1520 * 1538 *
1521 * Return value: 'op' if something is left, NULL if the amount reached 0 1539 * Return value: 'op' if something is left, NULL if the amount reached 0
1522 */ 1540 */
1523bool 1541bool
1524object::decrease (sint32 nr) 1542object::decrease (sint32 nr)
1525{ 1543{
1544 if (!nr)
1545 return true;
1546
1526 nr = min (nr, nrof); 1547 nr = min (nr, nrof);
1527 1548
1528 if (!nr)
1529 return 1;
1530
1531 nrof -= nr; 1549 nrof -= nr;
1532 1550
1533 object *visible = visible_to (this);
1534
1535 if (nrof) 1551 if (nrof)
1536 { 1552 {
1537 adjust_weight (env, -weight * nr); // carrying == 0 1553 adjust_weight (env, -weight * nr); // carrying == 0
1538 1554
1539 if (visible) 1555 if (object *pl = visible_to ())
1540 esrv_send_item (visible, this); 1556 esrv_update_item (UPD_NROF, pl, this);
1541 1557
1542 return 1; 1558 return true;
1543 } 1559 }
1544 else 1560 else
1545 { 1561 {
1546 if (visible)
1547 esrv_del_item (visible->contr, count);
1548
1549 this->destroy (1); 1562 destroy (1);
1550 return 0; 1563 return false;
1551 } 1564 }
1552} 1565}
1553 1566
1554/* 1567/*
1555 * split(ob,nr) splits up ob into two parts. The part which 1568 * split(ob,nr) splits up ob into two parts. The part which
1558 * On failure, NULL is returned. 1571 * On failure, NULL is returned.
1559 */ 1572 */
1560object * 1573object *
1561object::split (sint32 nr) 1574object::split (sint32 nr)
1562{ 1575{
1576 int have = number_of ();
1577
1563 if (nrof < nr) 1578 if (have < nr)
1564 return 0; 1579 return 0;
1565 else if (nrof == nr) 1580 else if (have == nr)
1566 { 1581 {
1567 remove (); 1582 remove ();
1568 return this; 1583 return this;
1569 } 1584 }
1570 else 1585 else
1623 if (object::can_merge (tmp, op)) 1638 if (object::can_merge (tmp, op))
1624 { 1639 {
1625 /* return the original object and remove inserted object 1640 /* return the original object and remove inserted object
1626 (client needs the original object) */ 1641 (client needs the original object) */
1627 tmp->nrof += op->nrof; 1642 tmp->nrof += op->nrof;
1643
1644 if (object *pl = tmp->visible_to ())
1645 esrv_update_item (UPD_NROF, pl, tmp);
1646
1628 adjust_weight (this, op->total_weight ()); 1647 adjust_weight (this, op->total_weight ());
1629 1648
1630 op->destroy (1); 1649 op->destroy (1);
1631 op = tmp; 1650 op = tmp;
1632 goto inserted; 1651 goto inserted;
1646 1665
1647 inv = op; 1666 inv = op;
1648 1667
1649 op->flag [FLAG_REMOVED] = 0; 1668 op->flag [FLAG_REMOVED] = 0;
1650 1669
1670 if (object *pl = op->visible_to ())
1671 esrv_send_item (pl, op);
1672
1651 adjust_weight (this, op->total_weight ()); 1673 adjust_weight (this, op->total_weight ());
1652 1674
1653inserted: 1675inserted:
1654 /* reset the light list and los of the players on the map */ 1676 /* reset the light list and los of the players on the map */
1655 if (op->glow_radius && map && map->darkness) 1677 if (op->glow_radius && map && map->darkness)
1656 update_all_los (map, x, y); 1678 update_all_los (map, x, y);
1657 1679
1658 if (object *otmp = in_player ()) 1680 // if this is a player's inventory, update stats
1659 if (otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER)) 1681 if (type == PLAYER && !flag [FLAG_NO_FIX_PLAYER])
1660 otmp->update_stats (); 1682 update_stats ();
1661 1683
1662 INVOKE_OBJECT (INSERT, this); 1684 INVOKE_OBJECT (INSERT, this);
1663 1685
1664 return op; 1686 return op;
1665} 1687}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines