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.209 by root, Mon Apr 21 06:35:26 2008 UTC vs.
Revision 1.219 by root, Wed Apr 23 21:09:10 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 if (pl != op) // player is handled lazily
397 esrv_update_item (UPD_WEIGHT, pl, op);
398
357 op = op->env; 399 op = op->env;
358 } 400 }
359} 401}
360 402
361/* 403/*
374 op->update_weight (); 416 op->update_weight ();
375 417
376 sum += op->total_weight (); 418 sum += op->total_weight ();
377 } 419 }
378 420
379 carrying = weight_adjust (this, sum); 421 sum = weight_adjust (this, sum);
422
423 if (sum != carrying)
424 {
425 carrying = sum;
426
427 if (object *pl = visible_to ())
428 if (pl != this) // player is handled lazily
429 esrv_update_item (UPD_WEIGHT, pl, this);
430 }
380} 431}
381 432
382/* 433/*
383 * Used by: Server DM commands: dumpbelow, dump. Some error messages. 434 * Used by: Server DM commands: dumpbelow, dump. Some error messages.
384 */ 435 */
866object::destroy_inv (bool drop_to_ground) 917object::destroy_inv (bool drop_to_ground)
867{ 918{
868 // need to check first, because the checks below might segfault 919 // need to check first, because the checks below might segfault
869 // as we might be on an invalid mapspace and crossfire code 920 // as we might be on an invalid mapspace and crossfire code
870 // is too buggy to ensure that the inventory is empty. 921 // is too buggy to ensure that the inventory is empty.
871 // corollary: if you create arrows etc. with stuff in tis inventory, 922 // corollary: if you create arrows etc. with stuff in its inventory,
872 // cf will crash below with off-map x and y 923 // cf will crash below with off-map x and y
873 if (!inv) 924 if (!inv)
874 return; 925 return;
875 926
876 /* Only if the space blocks everything do we not process - 927 /* Only if the space blocks everything do we not process -
883 || map->nodrop 934 || map->nodrop
884 || ms ().move_block == MOVE_ALL) 935 || ms ().move_block == MOVE_ALL)
885 { 936 {
886 while (inv) 937 while (inv)
887 { 938 {
888 inv->destroy_inv (drop_to_ground); 939 inv->destroy_inv (false);
889 inv->destroy (); 940 inv->destroy ();
890 } 941 }
891 } 942 }
892 else 943 else
893 { /* Put objects in inventory onto this space */ 944 { /* Put objects in inventory onto this space */
899 || op->flag [FLAG_NO_DROP] 950 || op->flag [FLAG_NO_DROP]
900 || op->type == RUNE 951 || op->type == RUNE
901 || op->type == TRAP 952 || op->type == TRAP
902 || op->flag [FLAG_IS_A_TEMPLATE] 953 || op->flag [FLAG_IS_A_TEMPLATE]
903 || op->flag [FLAG_DESTROY_ON_DEATH]) 954 || op->flag [FLAG_DESTROY_ON_DEATH])
904 op->destroy (); 955 op->destroy (true);
905 else 956 else
906 map->insert (op, x, y); 957 map->insert (op, x, y);
907 } 958 }
908 } 959 }
909} 960}
916} 967}
917 968
918void 969void
919object::do_destroy () 970object::do_destroy ()
920{ 971{
921 attachable::do_destroy ();
922
923 if (flag [FLAG_IS_LINKED]) 972 if (flag [FLAG_IS_LINKED])
924 remove_button_link (this); 973 remove_button_link (this);
925 974
926 if (flag [FLAG_FRIENDLY]) 975 if (flag [FLAG_FRIENDLY])
927 remove_friendly_object (this); 976 remove_friendly_object (this);
928 977
929 if (!flag [FLAG_REMOVED])
930 remove (); 978 remove ();
931 979
932 destroy_inv (true); 980 attachable::do_destroy ();
933 981
934 deactivate (); 982 deactivate ();
935 unlink (); 983 unlink ();
936 984
937 flag [FLAG_FREED] = 1; 985 flag [FLAG_FREED] = 1;
978object::destroy (bool destroy_inventory) 1026object::destroy (bool destroy_inventory)
979{ 1027{
980 if (destroyed ()) 1028 if (destroyed ())
981 return; 1029 return;
982 1030
1031 if (!is_head () && !head->destroyed ())
1032 {
1033 LOG (llevError | logBacktrace, "tried to destroy the tail of an object");
1034 head->destroy (destroy_inventory);
1035 }
1036
983 if (destroy_inventory) 1037 destroy_inv (!destroy_inventory);
984 destroy_inv (false);
985 1038
986 if (is_head ()) 1039 if (is_head ())
987 if (sound_destroy) 1040 if (sound_destroy)
988 play_sound (sound_destroy); 1041 play_sound (sound_destroy);
989 else if (flag [FLAG_MONSTER]) 1042 else if (flag [FLAG_MONSTER])
1003object::do_remove () 1056object::do_remove ()
1004{ 1057{
1005 object *tmp, *last = 0; 1058 object *tmp, *last = 0;
1006 object *otmp; 1059 object *otmp;
1007 1060
1008 if (QUERY_FLAG (this, FLAG_REMOVED)) 1061 if (flag [FLAG_REMOVED])
1009 return; 1062 return;
1010 1063
1011 SET_FLAG (this, FLAG_REMOVED);
1012 INVOKE_OBJECT (REMOVE, this); 1064 INVOKE_OBJECT (REMOVE, this);
1065
1066 if (object *pl = visible_to ())
1067 esrv_del_item (pl->contr, count);
1068
1069 flag [FLAG_REMOVED] = true;
1013 1070
1014 if (more) 1071 if (more)
1015 more->remove (); 1072 more->remove ();
1016 1073
1017 /* 1074 /*
1098 * removed (most likely destroyed), update the player view 1155 * removed (most likely destroyed), update the player view
1099 * appropriately. 1156 * appropriately.
1100 */ 1157 */
1101 pl->close_container (); 1158 pl->close_container ();
1102 1159
1103 pl->contr->ns->floorbox_update (); 1160 //TODO: the floorbox prev/next might need updating
1161 esrv_del_item (pl->contr, count);
1104 } 1162 }
1105 1163
1106 for (tmp = ms.bot; tmp; tmp = tmp->above) 1164 for (tmp = ms.bot; tmp; tmp = tmp->above)
1107 { 1165 {
1108 /* No point updating the players look faces if he is the object 1166 /* No point updating the players look faces if he is the object
1152 if (!top) 1210 if (!top)
1153 for (top = op; top && top->above; top = top->above) 1211 for (top = op; top && top->above; top = top->above)
1154 ; 1212 ;
1155 1213
1156 for (; top; top = top->below) 1214 for (; top; top = top->below)
1157 {
1158 if (top == op)
1159 continue;
1160
1161 if (object::can_merge (op, top)) 1215 if (object::can_merge (op, top))
1162 { 1216 {
1163 top->nrof += op->nrof; 1217 top->nrof += op->nrof;
1164 1218
1165/* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/ 1219 if (object *pl = top->visible_to ())
1166 op->weight = 0; /* Don't want any adjustements now */ 1220 esrv_update_item (UPD_NROF, pl, top);
1221
1222 op->weight = 0; // cancel the addition above
1223 op->carrying = 0; // must be 0 already
1224
1167 op->destroy (); 1225 op->destroy (1);
1226
1168 return top; 1227 return top;
1169 } 1228 }
1170 }
1171 1229
1172 return 0; 1230 return 0;
1173} 1231}
1174 1232
1175void 1233void
1234object * 1292object *
1235insert_ob_in_map (object *op, maptile *m, object *originator, int flag) 1293insert_ob_in_map (object *op, maptile *m, object *originator, int flag)
1236{ 1294{
1237 assert (!op->flag [FLAG_FREED]); 1295 assert (!op->flag [FLAG_FREED]);
1238 1296
1239 object *top, *floor = NULL;
1240
1241 op->remove (); 1297 op->remove ();
1242 1298
1243 /* Ideally, the caller figures this out. However, it complicates a lot 1299 /* Ideally, the caller figures this out. However, it complicates a lot
1244 * of areas of callers (eg, anything that uses find_free_spot would now 1300 * of areas of callers (eg, anything that uses find_free_spot would now
1245 * need extra work 1301 * need extra work
1246 */ 1302 */
1247 if (!xy_normalise (m, op->x, op->y)) 1303 if (!xy_normalise (m, op->x, op->y))
1248 { 1304 {
1249 op->destroy (); 1305 op->destroy (1);
1250 return 0; 1306 return 0;
1251 } 1307 }
1252 1308
1253 if (object *more = op->more) 1309 if (object *more = op->more)
1254 if (!insert_ob_in_map (more, m, originator, flag)) 1310 if (!insert_ob_in_map (more, m, originator, flag))
1263 */ 1319 */
1264 if (op->nrof && !(flag & INS_NO_MERGE)) 1320 if (op->nrof && !(flag & INS_NO_MERGE))
1265 for (object *tmp = ms.bot; tmp; tmp = tmp->above) 1321 for (object *tmp = ms.bot; tmp; tmp = tmp->above)
1266 if (object::can_merge (op, tmp)) 1322 if (object::can_merge (op, tmp))
1267 { 1323 {
1324 // TODO: we atcually want to update tmp, not op,
1325 // but some caller surely breaks when we return tmp
1326 // from here :/
1268 op->nrof += tmp->nrof; 1327 op->nrof += tmp->nrof;
1269 tmp->destroy (1); 1328 tmp->destroy (1);
1270 } 1329 }
1271 1330
1272 CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */ 1331 CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */
1294 /* since *below* originator, no need to update top */ 1353 /* since *below* originator, no need to update top */
1295 originator->below = op; 1354 originator->below = op;
1296 } 1355 }
1297 else 1356 else
1298 { 1357 {
1358 object *top, *floor = NULL;
1359
1299 top = ms.bot; 1360 top = ms.bot;
1300 1361
1301 /* If there are other objects, then */ 1362 /* If there are other objects, then */
1302 if (top) 1363 if (top)
1303 { 1364 {
1402 } 1463 }
1403 1464
1404 op->map->dirty = true; 1465 op->map->dirty = true;
1405 1466
1406 if (object *pl = ms.player ()) 1467 if (object *pl = ms.player ())
1407 pl->contr->ns->floorbox_update (); 1468 //TODO: the floorbox prev/next might need updating
1469 esrv_send_item (pl, op);
1408 1470
1409 /* If this object glows, it may affect lighting conditions that are 1471 /* If this object glows, it may affect lighting conditions that are
1410 * visible to others on this map. But update_all_los is really 1472 * visible to others on this map. But update_all_los is really
1411 * an inefficient way to do this, as it means los for all players 1473 * an inefficient way to do this, as it means los for all players
1412 * on the map will get recalculated. The players could very well 1474 * on the map will get recalculated. The players could very well
1477 return where->env->insert (this); 1539 return where->env->insert (this);
1478 else 1540 else
1479 return where->map->insert (this, where->x, where->y, originator, flags); 1541 return where->map->insert (this, where->x, where->y, originator, flags);
1480} 1542}
1481 1543
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/* 1544/*
1517 * decrease(object, number) decreases a specified number from 1545 * decrease(object, number) decreases a specified number from
1518 * the amount of an object. If the amount reaches 0, the object 1546 * the amount of an object. If the amount reaches 0, the object
1519 * is subsequently removed and freed. 1547 * is subsequently removed and freed.
1520 * 1548 *
1521 * Return value: 'op' if something is left, NULL if the amount reached 0 1549 * Return value: 'op' if something is left, NULL if the amount reached 0
1522 */ 1550 */
1523bool 1551bool
1524object::decrease (sint32 nr) 1552object::decrease (sint32 nr)
1525{ 1553{
1554 if (!nr)
1555 return true;
1556
1526 nr = min (nr, nrof); 1557 nr = min (nr, nrof);
1527 1558
1528 if (!nr)
1529 return 1;
1530
1531 nrof -= nr; 1559 nrof -= nr;
1532 1560
1533 object *visible = visible_to (this);
1534
1535 if (nrof) 1561 if (nrof)
1536 { 1562 {
1537 adjust_weight (env, -weight * nr); // carrying == 0 1563 adjust_weight (env, -weight * nr); // carrying == 0
1538 1564
1539 if (visible) 1565 if (object *pl = visible_to ())
1540 esrv_send_item (visible, this); 1566 esrv_update_item (UPD_NROF, pl, this);
1541 1567
1542 return 1; 1568 return true;
1543 } 1569 }
1544 else 1570 else
1545 { 1571 {
1546 if (visible)
1547 esrv_del_item (visible->contr, count);
1548
1549 this->destroy (1); 1572 destroy (1);
1550 return 0; 1573 return false;
1551 } 1574 }
1552} 1575}
1553 1576
1554/* 1577/*
1555 * get_split_ob(ob,nr) splits up ob into two parts. The part which 1578 * split(ob,nr) splits up ob into two parts. The part which
1556 * is returned contains nr objects, and the remaining parts contains 1579 * is returned contains nr objects, and the remaining parts contains
1557 * the rest (or is removed and freed if that number is 0). 1580 * the rest (or is removed and returned if that number is 0).
1558 * On failure, NULL is returned, and the reason put into the 1581 * On failure, NULL is returned.
1559 * global static errmsg array.
1560 */ 1582 */
1561object * 1583object *
1562object::split (sint32 nr) 1584object::split (sint32 nr)
1563{ 1585{
1586 int have = number_of ();
1587
1564 if (nrof < nr) 1588 if (have < nr)
1565 return 0; 1589 return 0;
1566 else if (nrof == nr) 1590 else if (have == nr)
1567 { 1591 {
1568 remove (); 1592 remove ();
1569 return this; 1593 return this;
1570 } 1594 }
1571 else 1595 else
1574 1598
1575 object *op = object_create_clone (this); 1599 object *op = object_create_clone (this);
1576 op->nrof = nr; 1600 op->nrof = nr;
1577 return op; 1601 return op;
1578 } 1602 }
1579}
1580
1581//TODO: remove, but semantics differs from split_nr
1582object *
1583get_split_ob (object *orig_ob, uint32 nr)
1584{
1585 if (orig_ob->nrof < nr)
1586 {
1587 sprintf (errmsg, "There are only %d %ss.", orig_ob->nrof ? orig_ob->nrof : 1, &orig_ob->name);
1588 return 0;
1589 }
1590
1591 return orig_ob->split (nr);
1592} 1603}
1593 1604
1594object * 1605object *
1595insert_ob_in_ob (object *op, object *where) 1606insert_ob_in_ob (object *op, object *where)
1596{ 1607{
1637 if (object::can_merge (tmp, op)) 1648 if (object::can_merge (tmp, op))
1638 { 1649 {
1639 /* return the original object and remove inserted object 1650 /* return the original object and remove inserted object
1640 (client needs the original object) */ 1651 (client needs the original object) */
1641 tmp->nrof += op->nrof; 1652 tmp->nrof += op->nrof;
1653
1654 if (object *pl = tmp->visible_to ())
1655 esrv_update_item (UPD_NROF, pl, tmp);
1656
1657 adjust_weight (this, op->total_weight ());
1658
1642 op->destroy (1); 1659 op->destroy (1);
1643 op = tmp; 1660 op = tmp;
1644 goto inserted; 1661 goto inserted;
1645 } 1662 }
1646 1663
1658 1675
1659 inv = op; 1676 inv = op;
1660 1677
1661 op->flag [FLAG_REMOVED] = 0; 1678 op->flag [FLAG_REMOVED] = 0;
1662 1679
1680 if (object *pl = op->visible_to ())
1681 esrv_send_item (pl, op);
1682
1663 adjust_weight (this, op->total_weight ()); 1683 adjust_weight (this, op->total_weight ());
1664 1684
1665inserted: 1685inserted:
1666 /* reset the light list and los of the players on the map */ 1686 /* reset the light list and los of the players on the map */
1667 if (op->glow_radius && map && map->darkness) 1687 if (op->glow_radius && map && map->darkness)
1668 update_all_los (map, x, y); 1688 update_all_los (map, x, y);
1669 1689
1670 if (object *otmp = in_player ()) 1690 // if this is a player's inventory, update stats
1671 if (otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER)) 1691 if (type == PLAYER && !flag [FLAG_NO_FIX_PLAYER])
1672 otmp->update_stats (); 1692 update_stats ();
1673 1693
1674 INVOKE_OBJECT (INSERT, this); 1694 INVOKE_OBJECT (INSERT, this);
1675 1695
1676 return op; 1696 return op;
1677} 1697}
2555 &name, 2575 &name,
2556 title ? "\",title:\"" : "", 2576 title ? "\",title:\"" : "",
2557 title ? (const char *)title : "", 2577 title ? (const char *)title : "",
2558 flag_desc (flagdesc, 512), type); 2578 flag_desc (flagdesc, 512), type);
2559 2579
2560 if (!this->flag[FLAG_REMOVED] && env) 2580 if (!flag[FLAG_REMOVED] && env)
2561 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); 2581 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2));
2562 2582
2563 if (map) 2583 if (map)
2564 p += snprintf (p, 256, "(on %s@%d+%d)", &map->path, x, y); 2584 p += snprintf (p, 256, "(on %s@%d+%d)", &map->path, x, y);
2565 2585
2607 if (object *closer = old_container->inv) 2627 if (object *closer = old_container->inv)
2608 if (closer->type == CLOSE_CON) 2628 if (closer->type == CLOSE_CON)
2609 closer->destroy (); 2629 closer->destroy ();
2610#endif 2630#endif
2611 2631
2612 old_container->flag [FLAG_APPLIED] = 0; 2632 old_container->flag [FLAG_APPLIED] = false;
2613 container = 0; 2633 container = 0;
2614 2634
2615 esrv_update_item (UPD_FLAGS, this, old_container); 2635 esrv_update_item (UPD_FLAGS, this, old_container);
2616 new_draw_info_format (NDI_UNIQUE, 0, this, "You close %s.", query_name (old_container)); 2636 new_draw_info_format (NDI_UNIQUE, 0, this, "You close %s.", query_name (old_container));
2617 play_sound (sound_find ("chest_close")); 2637 play_sound (sound_find ("chest_close"));
2633 } 2653 }
2634#endif 2654#endif
2635 2655
2636 new_draw_info_format (NDI_UNIQUE, 0, this, "You open %s.", query_name (new_container)); 2656 new_draw_info_format (NDI_UNIQUE, 0, this, "You open %s.", query_name (new_container));
2637 2657
2638 new_container->flag [FLAG_APPLIED] = 1; 2658 new_container->flag [FLAG_APPLIED] = true;
2639 container = new_container; 2659 container = new_container;
2640 2660
2641 esrv_update_item (UPD_FLAGS, this, new_container); 2661 esrv_update_item (UPD_FLAGS, this, new_container);
2642 esrv_send_inventory (this, new_container); 2662 esrv_send_inventory (this, new_container);
2643 play_sound (sound_find ("chest_open")); 2663 play_sound (sound_find ("chest_open"));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines