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.208 by root, Mon Apr 21 06:16:01 2008 UTC vs.
Revision 1.212 by root, Mon Apr 21 23:35:24 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
351{ 350{
352 while (op) 351 while (op)
353 { 352 {
354 weight = weight_adjust (op, weight); 353 weight = weight_adjust (op, weight);
355 354
355 if (!weight)
356 return;
357
356 op->carrying += weight; 358 op->carrying += weight;
359
360 if (object *pl = op->visible_to ())
361 esrv_update_item (UPD_WEIGHT, pl, op);
362
357 op = op->env; 363 op = op->env;
358 } 364 }
359} 365}
360 366
361/* 367/*
374 op->update_weight (); 380 op->update_weight ();
375 381
376 sum += op->total_weight (); 382 sum += op->total_weight ();
377 } 383 }
378 384
379 carrying = weight_adjust (this, sum); 385 sum = weight_adjust (this, sum);
386
387 if (sum != carrying)
388 {
389 carrying = sum;
390
391 if (object *pl = visible_to ())
392 esrv_update_item (UPD_WEIGHT, pl, this);
393 }
380} 394}
381 395
382/* 396/*
383 * Used by: Server DM commands: dumpbelow, dump. Some error messages. 397 * Used by: Server DM commands: dumpbelow, dump. Some error messages.
384 */ 398 */
916} 930}
917 931
918void 932void
919object::do_destroy () 933object::do_destroy ()
920{ 934{
935 if (object *pl = visible_to ())
936 esrv_del_item (pl->contr, count);
937
921 attachable::do_destroy (); 938 attachable::do_destroy ();
922 939
923 if (flag [FLAG_IS_LINKED]) 940 if (flag [FLAG_IS_LINKED])
924 remove_button_link (this); 941 remove_button_link (this);
925 942
1477 return where->env->insert (this); 1494 return where->env->insert (this);
1478 else 1495 else
1479 return where->map->insert (this, where->x, where->y, originator, flags); 1496 return where->map->insert (this, where->x, where->y, originator, flags);
1480} 1497}
1481 1498
1482/* 1499// find player who can see this object
1483 * get_split_ob(ob,nr) splits up ob into two parts. The part which
1484 * is returned contains nr objects, and the remaining parts contains
1485 * the rest (or is removed and freed if that number is 0).
1486 * On failure, NULL is returned, and the reason put into the
1487 * global static errmsg array.
1488 */
1489object * 1500object *
1490object::split_nr (sint32 nr) 1501object::visible_to () const
1491{ 1502{
1492 if (nrof < nr)
1493 return 0;
1494 else if (nrof == nr)
1495 {
1496 remove ();
1497 return this;
1498 }
1499 else
1500 {
1501 decrease_nr (nr);
1502
1503 object *op = object_create_clone (this);
1504 op->nrof = nr;
1505 return op;
1506 }
1507}
1508
1509//TODO: remove, but semantics differs from split_nr
1510object *
1511get_split_ob (object *orig_ob, uint32 nr)
1512{
1513 if (orig_ob->nrof < nr)
1514 {
1515 sprintf (errmsg, "There are only %d %ss.", orig_ob->nrof ? orig_ob->nrof : 1, &orig_ob->name);
1516 return 0;
1517 }
1518
1519 return orig_ob->split_nr (nr);
1520}
1521
1522// find player who can see this object
1523static object *
1524visible_to (object *op)
1525{
1526 if (!op->flag [FLAG_REMOVED]) 1503 if (!flag [FLAG_REMOVED])
1527 { 1504 {
1528 // see if we are in a container of sorts 1505 // see if we are in a container of sorts
1529 if (object *env = op->env) 1506 if (env)
1530 { 1507 {
1531 // the player inventory itself is always visible 1508 // the player inventory itself is always visible
1532 if (env->type == PLAYER) 1509 if (env->type == PLAYER)
1533 return env; 1510 return env;
1534 1511
1535 // else a player could have our env open 1512 // else a player could have our env open
1536 env = env->outer_env (); 1513 object *envest = env->outer_env ();
1537 1514
1538 // the player itself is always on a map, so we will find him here 1515 // the player itself is always on a map, so we will find him here
1539 // even if our inv is in a player. 1516 // even if our inv is in a player.
1540 if (env->is_on_map ()) 1517 if (envest->is_on_map ())
1541 if (object *pl = env->ms ().player ()) 1518 if (object *pl = envest->ms ().player ())
1542 if (pl->container == op->env) 1519 if (pl->container == env)
1543 return pl; 1520 return pl;
1544 } 1521 }
1545 else 1522 else
1546 { 1523 {
1547 // maybe there is a player standing on the same mapspace 1524 // maybe there is a player standing on the same mapspace
1525 // this will catch the case where "this" is a player
1548 if (object *pl = env->ms ().player ()) 1526 if (object *pl = ms ().player ())
1549 return pl; 1527 return pl;
1550 } 1528 }
1551 } 1529 }
1552 1530
1553 return 0; 1531 return 0;
1554} 1532}
1555 1533
1556/* 1534/*
1557 * decrease_ob_nr(object, number) decreases a specified number from 1535 * decrease(object, number) decreases a specified number from
1558 * 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
1559 * is subsequently removed and freed. 1537 * is subsequently removed and freed.
1560 * 1538 *
1561 * 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
1562 */ 1540 */
1563bool 1541bool
1564object::decrease_nr (sint32 nr) 1542object::decrease (sint32 nr)
1565{ 1543{
1544 if (!nr)
1545 return true;
1546
1566 nr = min (nr, nrof); 1547 nr = min (nr, nrof);
1567 1548
1568 if (!nr)
1569 return 1;
1570
1571 nrof -= nr; 1549 nrof -= nr;
1572 1550
1573 object *visible = visible_to (this);
1574
1575 if (nrof) 1551 if (nrof)
1576 { 1552 {
1577 adjust_weight (env, -weight * nr); // carrying == 0 1553 adjust_weight (env, -weight * nr); // carrying == 0
1578 1554
1579 if (visible) 1555 if (object *pl = visible_to ())
1580 esrv_send_item (visible, this); 1556 esrv_update_item (UPD_NROF, pl, this);
1581 1557
1582 return 1; 1558 return true;
1583 } 1559 }
1584 else 1560 else
1585 { 1561 {
1586 if (visible)
1587 esrv_del_item (visible->contr, count);
1588
1589 this->destroy (1); 1562 destroy (1);
1590 return 0; 1563 return false;
1591 } 1564 }
1592} 1565}
1593 1566
1594//TODO: remove 1567/*
1568 * split(ob,nr) splits up ob into two parts. The part which
1569 * is returned contains nr objects, and the remaining parts contains
1570 * the rest (or is removed and returned if that number is 0).
1571 * On failure, NULL is returned.
1572 */
1595object * 1573object *
1596decrease_ob_nr (object *op, uint32 i) 1574object::split (sint32 nr)
1597{ 1575{
1598 return op->decrease_nr (i) ? op : 0; 1576 int have = number_of ();
1577
1578 if (have < nr)
1579 return 0;
1580 else if (have == nr)
1581 {
1582 remove ();
1583 return this;
1584 }
1585 else
1586 {
1587 decrease (nr);
1588
1589 object *op = object_create_clone (this);
1590 op->nrof = nr;
1591 return op;
1592 }
1599} 1593}
1600 1594
1601object * 1595object *
1602insert_ob_in_ob (object *op, object *where) 1596insert_ob_in_ob (object *op, object *where)
1603{ 1597{
1644 if (object::can_merge (tmp, op)) 1638 if (object::can_merge (tmp, op))
1645 { 1639 {
1646 /* return the original object and remove inserted object 1640 /* return the original object and remove inserted object
1647 (client needs the original object) */ 1641 (client needs the original object) */
1648 tmp->nrof += op->nrof; 1642 tmp->nrof += op->nrof;
1643 adjust_weight (this, op->total_weight ());
1644
1649 op->destroy (1); 1645 op->destroy (1);
1650 op = tmp; 1646 op = tmp;
1651 goto inserted; 1647 goto inserted;
1652 } 1648 }
1653 1649

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines