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.227 by root, Sat May 3 09:04:17 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 (client_visible () && !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 if (!pl->container || this == pl->container)
364 return pl;
365 }
366 }
367
368 return 0;
369}
370
336// adjust weight per container type ("of holding") 371// adjust weight per container type ("of holding")
337static sint32 372static sint32
338weight_adjust (object *op, sint32 weight) 373weight_adjust (object *op, sint32 weight)
339{ 374{
340 return op->type == CONTAINER 375 return op->type == CONTAINER
351{ 386{
352 while (op) 387 while (op)
353 { 388 {
354 weight = weight_adjust (op, weight); 389 weight = weight_adjust (op, weight);
355 390
391 if (!weight)
392 return;
393
356 op->carrying += weight; 394 op->carrying += weight;
395
396 if (object *pl = op->visible_to ())
397 if (pl != op) // player is handled lazily
398 esrv_update_item (UPD_WEIGHT, pl, op);
399
357 op = op->env; 400 op = op->env;
358 } 401 }
359} 402}
360 403
361/* 404/*
374 op->update_weight (); 417 op->update_weight ();
375 418
376 sum += op->total_weight (); 419 sum += op->total_weight ();
377 } 420 }
378 421
379 carrying = weight_adjust (this, sum); 422 sum = weight_adjust (this, sum);
423
424 if (sum != carrying)
425 {
426 carrying = sum;
427
428 if (object *pl = visible_to ())
429 if (pl != this) // player is handled lazily
430 esrv_update_item (UPD_WEIGHT, pl, this);
431 }
380} 432}
381 433
382/* 434/*
383 * Used by: Server DM commands: dumpbelow, dump. Some error messages. 435 * Used by: Server DM commands: dumpbelow, dump. Some error messages.
384 */ 436 */
551 } 603 }
552 604
553 op->key_values = 0; 605 op->key_values = 0;
554} 606}
555 607
556object & 608/*
557object::operator =(const object &src) 609 * copy_to first frees everything allocated by the dst object,
610 * and then copies the contents of itself into the second
611 * object, allocating what needs to be allocated. Basically, any
612 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
613 * if the first object is freed, the pointers in the new object
614 * will point at garbage.
615 */
616void
617object::copy_to (object *dst)
558{ 618{
559 bool is_freed = flag [FLAG_FREED]; 619 dst->remove ();
560 bool is_removed = flag [FLAG_REMOVED];
561
562 *(object_copy *)this = src; 620 *(object_copy *)dst = *this;
563
564 flag [FLAG_FREED] = is_freed;
565 flag [FLAG_REMOVED] = is_removed; 621 dst->flag [FLAG_REMOVED] = true;
566 622
567 /* Copy over key_values, if any. */ 623 /* Copy over key_values, if any. */
568 if (src.key_values) 624 if (key_values)
569 { 625 {
570 key_value *tail = 0; 626 key_value *tail = 0;
571 key_values = 0; 627 dst->key_values = 0;
572 628
573 for (key_value *i = src.key_values; i; i = i->next) 629 for (key_value *i = key_values; i; i = i->next)
574 { 630 {
575 key_value *new_link = new key_value; 631 key_value *new_link = new key_value;
576 632
577 new_link->next = 0; 633 new_link->next = 0;
578 new_link->key = i->key; 634 new_link->key = i->key;
579 new_link->value = i->value; 635 new_link->value = i->value;
580 636
581 /* Try and be clever here, too. */ 637 /* Try and be clever here, too. */
582 if (!key_values) 638 if (!dst->key_values)
583 { 639 {
584 key_values = new_link; 640 dst->key_values = new_link;
585 tail = new_link; 641 tail = new_link;
586 } 642 }
587 else 643 else
588 { 644 {
589 tail->next = new_link; 645 tail->next = new_link;
590 tail = new_link; 646 tail = new_link;
591 } 647 }
592 } 648 }
593 } 649 }
594}
595
596/*
597 * copy_to first frees everything allocated by the dst object,
598 * and then copies the contents of itself into the second
599 * object, allocating what needs to be allocated. Basically, any
600 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
601 * if the first object is freed, the pointers in the new object
602 * will point at garbage.
603 */
604void
605object::copy_to (object *dst)
606{
607 *dst = *this;
608 650
609 if (speed < 0) 651 if (speed < 0)
610 dst->speed_left -= rndm (); 652 dst->speed_left -= rndm ();
611 653
612 dst->set_speed (dst->speed); 654 dst->set_speed (dst->speed);
634object * 676object *
635object::clone () 677object::clone ()
636{ 678{
637 object *neu = create (); 679 object *neu = create ();
638 copy_to (neu); 680 copy_to (neu);
681 neu->map = map; // not copied by copy_to
639 return neu; 682 return neu;
640} 683}
641 684
642/* 685/*
643 * If an object with the IS_TURNABLE() flag needs to be turned due 686 * If an object with the IS_TURNABLE() flag needs to be turned due
694 * UP_OBJ_FACE: only the objects face has changed. 737 * UP_OBJ_FACE: only the objects face has changed.
695 */ 738 */
696void 739void
697update_object (object *op, int action) 740update_object (object *op, int action)
698{ 741{
699 if (op == NULL) 742 if (!op)
700 { 743 {
701 /* this should never happen */ 744 /* this should never happen */
702 LOG (llevDebug, "update_object() called for NULL object.\n"); 745 LOG (llevError | logBacktrace, "update_object() called for NULL object.\n");
703 return; 746 return;
704 } 747 }
705 748
706 if (op->env) 749 if (!op->is_on_map ())
707 { 750 {
708 /* Animation is currently handled by client, so nothing 751 /* Animation is currently handled by client, so nothing
709 * to do in this case. 752 * to do in this case.
710 */ 753 */
711 return; 754 return;
712 } 755 }
713
714 /* If the map is saving, don't do anything as everything is
715 * going to get freed anyways.
716 */
717 if (!op->map || op->map->in_memory == MAP_SAVING)
718 return;
719 756
720 /* make sure the object is within map boundaries */ 757 /* make sure the object is within map boundaries */
721 if (op->x < 0 || op->x >= op->map->width || op->y < 0 || op->y >= op->map->height) 758 if (op->x < 0 || op->x >= op->map->width || op->y < 0 || op->y >= op->map->height)
722 { 759 {
723 LOG (llevError, "update_object() called for object out of map!\n"); 760 LOG (llevError, "update_object() called for object out of map!\n");
866object::destroy_inv (bool drop_to_ground) 903object::destroy_inv (bool drop_to_ground)
867{ 904{
868 // need to check first, because the checks below might segfault 905 // need to check first, because the checks below might segfault
869 // as we might be on an invalid mapspace and crossfire code 906 // as we might be on an invalid mapspace and crossfire code
870 // is too buggy to ensure that the inventory is empty. 907 // is too buggy to ensure that the inventory is empty.
871 // corollary: if you create arrows etc. with stuff in tis inventory, 908 // corollary: if you create arrows etc. with stuff in its inventory,
872 // cf will crash below with off-map x and y 909 // cf will crash below with off-map x and y
873 if (!inv) 910 if (!inv)
874 return; 911 return;
875 912
876 /* Only if the space blocks everything do we not process - 913 /* Only if the space blocks everything do we not process -
883 || map->nodrop 920 || map->nodrop
884 || ms ().move_block == MOVE_ALL) 921 || ms ().move_block == MOVE_ALL)
885 { 922 {
886 while (inv) 923 while (inv)
887 { 924 {
888 inv->destroy_inv (drop_to_ground); 925 inv->destroy_inv (false);
889 inv->destroy (); 926 inv->destroy ();
890 } 927 }
891 } 928 }
892 else 929 else
893 { /* Put objects in inventory onto this space */ 930 { /* Put objects in inventory onto this space */
899 || op->flag [FLAG_NO_DROP] 936 || op->flag [FLAG_NO_DROP]
900 || op->type == RUNE 937 || op->type == RUNE
901 || op->type == TRAP 938 || op->type == TRAP
902 || op->flag [FLAG_IS_A_TEMPLATE] 939 || op->flag [FLAG_IS_A_TEMPLATE]
903 || op->flag [FLAG_DESTROY_ON_DEATH]) 940 || op->flag [FLAG_DESTROY_ON_DEATH])
904 op->destroy (); 941 op->destroy (true);
905 else 942 else
906 map->insert (op, x, y); 943 map->insert (op, x, y);
907 } 944 }
908 } 945 }
909} 946}
913 object *op = new object; 950 object *op = new object;
914 op->link (); 951 op->link ();
915 return op; 952 return op;
916} 953}
917 954
955static struct freed_map : maptile
956{
957 freed_map ()
958 {
959 path = "<freed objects map>";
960 name = "/internal/freed_objects_map";
961 width = 3;
962 height = 3;
963 nodrop = 1;
964
965 alloc ();
966 in_memory = MAP_ACTIVE;
967 }
968} freed_map; // freed objects are moved here to avoid crashes
969
918void 970void
919object::do_destroy () 971object::do_destroy ()
920{ 972{
921 attachable::do_destroy ();
922
923 if (flag [FLAG_IS_LINKED]) 973 if (flag [FLAG_IS_LINKED])
924 remove_button_link (this); 974 remove_button_link (this);
925 975
926 if (flag [FLAG_FRIENDLY]) 976 if (flag [FLAG_FRIENDLY])
927 remove_friendly_object (this); 977 remove_friendly_object (this);
928 978
929 if (!flag [FLAG_REMOVED])
930 remove (); 979 remove ();
931 980
932 destroy_inv (true); 981 attachable::do_destroy ();
933 982
934 deactivate (); 983 deactivate ();
935 unlink (); 984 unlink ();
936 985
937 flag [FLAG_FREED] = 1; 986 flag [FLAG_FREED] = 1;
938 987
939 // hack to ensure that freed objects still have a valid map 988 // hack to ensure that freed objects still have a valid map
940 {
941 static maptile *freed_map; // freed objects are moved here to avoid crashes
942
943 if (!freed_map)
944 {
945 freed_map = new maptile;
946
947 freed_map->path = "<freed objects map>";
948 freed_map->name = "/internal/freed_objects_map";
949 freed_map->width = 3;
950 freed_map->height = 3;
951 freed_map->nodrop = 1;
952
953 freed_map->alloc ();
954 freed_map->in_memory = MAP_ACTIVE;
955 }
956
957 map = freed_map; 989 map = &freed_map;
958 x = 1; 990 x = 1;
959 y = 1; 991 y = 1;
960 }
961 992
962 if (more) 993 if (more)
963 { 994 {
964 more->destroy (); 995 more->destroy ();
965 more = 0; 996 more = 0;
978object::destroy (bool destroy_inventory) 1009object::destroy (bool destroy_inventory)
979{ 1010{
980 if (destroyed ()) 1011 if (destroyed ())
981 return; 1012 return;
982 1013
1014 if (!is_head () && !head->destroyed ())
1015 {
1016 LOG (llevError | logBacktrace, "tried to destroy the tail of an object");
1017 head->destroy (destroy_inventory);
1018 return;
1019 }
1020
983 if (destroy_inventory) 1021 destroy_inv (!destroy_inventory);
984 destroy_inv (false);
985 1022
986 if (is_head ()) 1023 if (is_head ())
987 if (sound_destroy) 1024 if (sound_destroy)
988 play_sound (sound_destroy); 1025 play_sound (sound_destroy);
989 else if (flag [FLAG_MONSTER]) 1026 else if (flag [FLAG_MONSTER])
1003object::do_remove () 1040object::do_remove ()
1004{ 1041{
1005 object *tmp, *last = 0; 1042 object *tmp, *last = 0;
1006 object *otmp; 1043 object *otmp;
1007 1044
1008 if (QUERY_FLAG (this, FLAG_REMOVED)) 1045 if (flag [FLAG_REMOVED])
1009 return; 1046 return;
1010 1047
1011 SET_FLAG (this, FLAG_REMOVED);
1012 INVOKE_OBJECT (REMOVE, this); 1048 INVOKE_OBJECT (REMOVE, this);
1049
1050 flag [FLAG_REMOVED] = true;
1013 1051
1014 if (more) 1052 if (more)
1015 more->remove (); 1053 more->remove ();
1016 1054
1017 /* 1055 /*
1018 * In this case, the object to be removed is in someones 1056 * In this case, the object to be removed is in someones
1019 * inventory. 1057 * inventory.
1020 */ 1058 */
1021 if (env) 1059 if (env)
1022 { 1060 {
1061 flag [FLAG_REMOVED] = false; // hack around the issue of visible_to checking flag_removed
1062 if (object *pl = visible_to ())
1063 esrv_del_item (pl->contr, count);
1064 flag [FLAG_REMOVED] = true; // hack around the issue of visible_to checking flag_removed
1065
1023 adjust_weight (env, -total_weight ()); 1066 adjust_weight (env, -total_weight ());
1024 1067
1025 *(above ? &above->below : &env->inv) = below; 1068 *(above ? &above->below : &env->inv) = below;
1026 1069
1027 if (below) 1070 if (below)
1045 if ((otmp = in_player ()) && otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER)) 1088 if ((otmp = in_player ()) && otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER))
1046 otmp->update_stats (); 1089 otmp->update_stats ();
1047 } 1090 }
1048 else if (map) 1091 else if (map)
1049 { 1092 {
1050 if (type == PLAYER)
1051 {
1052 // leaving a spot always closes any open container on the ground
1053 if (container && !container->env)
1054 // this causes spurious floorbox updates, but it ensures
1055 // that the CLOSE event is being sent.
1056 close_container ();
1057
1058 --map->players;
1059 map->touch ();
1060 }
1061
1062 map->dirty = true; 1093 map->dirty = true;
1063 mapspace &ms = this->ms (); 1094 mapspace &ms = this->ms ();
1095
1096 if (object *pl = ms.player ())
1097 {
1098 if (type == PLAYER) // this == pl(!)
1099 {
1100 // leaving a spot always closes any open container on the ground
1101 if (container && !container->env)
1102 // this causes spurious floorbox updates, but it ensures
1103 // that the CLOSE event is being sent.
1104 close_container ();
1105
1106 --map->players;
1107 map->touch ();
1108 }
1109 else if (pl->container == this)
1110 {
1111 // removing a container should close it
1112 close_container ();
1113 }
1114
1115 esrv_del_item (pl->contr, count);
1116 }
1064 1117
1065 /* link the object above us */ 1118 /* link the object above us */
1066 if (above) 1119 if (above)
1067 above->below = below; 1120 above->below = below;
1068 else 1121 else
1098 * removed (most likely destroyed), update the player view 1151 * removed (most likely destroyed), update the player view
1099 * appropriately. 1152 * appropriately.
1100 */ 1153 */
1101 pl->close_container (); 1154 pl->close_container ();
1102 1155
1156 //TODO: the floorbox prev/next might need updating
1157 //esrv_del_item (pl->contr, count);
1158 //TODO: update floorbox to preserve ordering
1159 if (pl->contr->ns)
1103 pl->contr->ns->floorbox_update (); 1160 pl->contr->ns->floorbox_update ();
1104 } 1161 }
1105 1162
1106 for (tmp = ms.bot; tmp; tmp = tmp->above) 1163 for (tmp = ms.bot; tmp; tmp = tmp->above)
1107 { 1164 {
1108 /* No point updating the players look faces if he is the object 1165 /* No point updating the players look faces if he is the object
1152 if (!top) 1209 if (!top)
1153 for (top = op; top && top->above; top = top->above) 1210 for (top = op; top && top->above; top = top->above)
1154 ; 1211 ;
1155 1212
1156 for (; top; top = top->below) 1213 for (; top; top = top->below)
1157 {
1158 if (top == op)
1159 continue;
1160
1161 if (object::can_merge (op, top)) 1214 if (object::can_merge (op, top))
1162 { 1215 {
1163 top->nrof += op->nrof; 1216 top->nrof += op->nrof;
1164 1217
1165/* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/ 1218 if (object *pl = top->visible_to ())
1166 op->weight = 0; /* Don't want any adjustements now */ 1219 esrv_update_item (UPD_NROF, pl, top);
1220
1221 op->weight = 0; // cancel the addition above
1222 op->carrying = 0; // must be 0 already
1223
1167 op->destroy (); 1224 op->destroy (1);
1225
1168 return top; 1226 return top;
1169 } 1227 }
1170 }
1171 1228
1172 return 0; 1229 return 0;
1173} 1230}
1174 1231
1175void 1232void
1234object * 1291object *
1235insert_ob_in_map (object *op, maptile *m, object *originator, int flag) 1292insert_ob_in_map (object *op, maptile *m, object *originator, int flag)
1236{ 1293{
1237 assert (!op->flag [FLAG_FREED]); 1294 assert (!op->flag [FLAG_FREED]);
1238 1295
1239 object *top, *floor = NULL;
1240
1241 op->remove (); 1296 op->remove ();
1242 1297
1243 /* Ideally, the caller figures this out. However, it complicates a lot 1298 /* 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 1299 * of areas of callers (eg, anything that uses find_free_spot would now
1245 * need extra work 1300 * need extra work
1246 */ 1301 */
1247 if (!xy_normalise (m, op->x, op->y)) 1302 if (!xy_normalise (m, op->x, op->y))
1248 { 1303 {
1249 op->destroy (); 1304 op->destroy (1);
1250 return 0; 1305 return 0;
1251 } 1306 }
1252 1307
1253 if (object *more = op->more) 1308 if (object *more = op->more)
1254 if (!insert_ob_in_map (more, m, originator, flag)) 1309 if (!insert_ob_in_map (more, m, originator, flag))
1263 */ 1318 */
1264 if (op->nrof && !(flag & INS_NO_MERGE)) 1319 if (op->nrof && !(flag & INS_NO_MERGE))
1265 for (object *tmp = ms.bot; tmp; tmp = tmp->above) 1320 for (object *tmp = ms.bot; tmp; tmp = tmp->above)
1266 if (object::can_merge (op, tmp)) 1321 if (object::can_merge (op, tmp))
1267 { 1322 {
1323 // TODO: we atcually want to update tmp, not op,
1324 // but some caller surely breaks when we return tmp
1325 // from here :/
1268 op->nrof += tmp->nrof; 1326 op->nrof += tmp->nrof;
1269 tmp->destroy (1); 1327 tmp->destroy (1);
1270 } 1328 }
1271 1329
1272 CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */ 1330 CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */
1294 /* since *below* originator, no need to update top */ 1352 /* since *below* originator, no need to update top */
1295 originator->below = op; 1353 originator->below = op;
1296 } 1354 }
1297 else 1355 else
1298 { 1356 {
1357 object *top, *floor = NULL;
1358
1299 top = ms.bot; 1359 top = ms.bot;
1300 1360
1301 /* If there are other objects, then */ 1361 /* If there are other objects, then */
1302 if (top) 1362 if (top)
1303 { 1363 {
1402 } 1462 }
1403 1463
1404 op->map->dirty = true; 1464 op->map->dirty = true;
1405 1465
1406 if (object *pl = ms.player ()) 1466 if (object *pl = ms.player ())
1467 //TODO: the floorbox prev/next might need updating
1468 //esrv_send_item (pl, op);
1469 //TODO: update floorbox to preserve ordering
1470 if (pl->contr->ns)
1407 pl->contr->ns->floorbox_update (); 1471 pl->contr->ns->floorbox_update ();
1408 1472
1409 /* If this object glows, it may affect lighting conditions that are 1473 /* If this object glows, it may affect lighting conditions that are
1410 * visible to others on this map. But update_all_los is really 1474 * 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 1475 * 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 1476 * on the map will get recalculated. The players could very well
1477 return where->env->insert (this); 1541 return where->env->insert (this);
1478 else 1542 else
1479 return where->map->insert (this, where->x, where->y, originator, flags); 1543 return where->map->insert (this, where->x, where->y, originator, flags);
1480} 1544}
1481 1545
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/* 1546/*
1517 * decrease(object, number) decreases a specified number from 1547 * decrease(object, number) decreases a specified number from
1518 * the amount of an object. If the amount reaches 0, the object 1548 * the amount of an object. If the amount reaches 0, the object
1519 * is subsequently removed and freed. 1549 * is subsequently removed and freed.
1520 * 1550 *
1521 * Return value: 'op' if something is left, NULL if the amount reached 0 1551 * Return value: 'op' if something is left, NULL if the amount reached 0
1522 */ 1552 */
1523bool 1553bool
1524object::decrease (sint32 nr) 1554object::decrease (sint32 nr)
1525{ 1555{
1556 if (!nr)
1557 return true;
1558
1526 nr = min (nr, nrof); 1559 nr = min (nr, nrof);
1527 1560
1528 if (!nr)
1529 return 1;
1530
1531 nrof -= nr; 1561 nrof -= nr;
1532 1562
1533 object *visible = visible_to (this);
1534
1535 if (nrof) 1563 if (nrof)
1536 { 1564 {
1537 adjust_weight (env, -weight * nr); // carrying == 0 1565 adjust_weight (env, -weight * nr); // carrying == 0
1538 1566
1539 if (visible) 1567 if (object *pl = visible_to ())
1540 esrv_send_item (visible, this); 1568 esrv_update_item (UPD_NROF, pl, this);
1541 1569
1542 return 1; 1570 return true;
1543 } 1571 }
1544 else 1572 else
1545 { 1573 {
1546 if (visible)
1547 esrv_del_item (visible->contr, count);
1548
1549 this->destroy (1); 1574 destroy (1);
1550 return 0; 1575 return false;
1551 } 1576 }
1552} 1577}
1553 1578
1554/* 1579/*
1555 * split(ob,nr) splits up ob into two parts. The part which 1580 * split(ob,nr) splits up ob into two parts. The part which
1558 * On failure, NULL is returned. 1583 * On failure, NULL is returned.
1559 */ 1584 */
1560object * 1585object *
1561object::split (sint32 nr) 1586object::split (sint32 nr)
1562{ 1587{
1588 int have = number_of ();
1589
1563 if (nrof < nr) 1590 if (have < nr)
1564 return 0; 1591 return 0;
1565 else if (nrof == nr) 1592 else if (have == nr)
1566 { 1593 {
1567 remove (); 1594 remove ();
1568 return this; 1595 return this;
1569 } 1596 }
1570 else 1597 else
1623 if (object::can_merge (tmp, op)) 1650 if (object::can_merge (tmp, op))
1624 { 1651 {
1625 /* return the original object and remove inserted object 1652 /* return the original object and remove inserted object
1626 (client needs the original object) */ 1653 (client needs the original object) */
1627 tmp->nrof += op->nrof; 1654 tmp->nrof += op->nrof;
1655
1656 if (object *pl = tmp->visible_to ())
1657 esrv_update_item (UPD_NROF, pl, tmp);
1658
1628 adjust_weight (this, op->total_weight ()); 1659 adjust_weight (this, op->total_weight ());
1629 1660
1630 op->destroy (1); 1661 op->destroy (1);
1631 op = tmp; 1662 op = tmp;
1632 goto inserted; 1663 goto inserted;
1646 1677
1647 inv = op; 1678 inv = op;
1648 1679
1649 op->flag [FLAG_REMOVED] = 0; 1680 op->flag [FLAG_REMOVED] = 0;
1650 1681
1682 if (object *pl = op->visible_to ())
1683 esrv_send_item (pl, op);
1684
1651 adjust_weight (this, op->total_weight ()); 1685 adjust_weight (this, op->total_weight ());
1652 1686
1653inserted: 1687inserted:
1654 /* reset the light list and los of the players on the map */ 1688 /* reset the light list and los of the players on the map */
1655 if (op->glow_radius && map && map->darkness) 1689 if (op->glow_radius && map && map->darkness)
1656 update_all_los (map, x, y); 1690 update_all_los (map, x, y);
1657 1691
1658 if (object *otmp = in_player ()) 1692 // if this is a player's inventory, update stats
1659 if (otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER)) 1693 if (type == PLAYER && !flag [FLAG_NO_FIX_PLAYER])
1660 otmp->update_stats (); 1694 update_stats ();
1661 1695
1662 INVOKE_OBJECT (INSERT, this); 1696 INVOKE_OBJECT (INSERT, this);
1663 1697
1664 return op; 1698 return op;
1665} 1699}
2289 * create clone from object to another 2323 * create clone from object to another
2290 */ 2324 */
2291object * 2325object *
2292object_create_clone (object *asrc) 2326object_create_clone (object *asrc)
2293{ 2327{
2294 object *dst = 0, *tmp, *src, *prev, *item; 2328 object *dst = 0;
2295 2329
2296 if (!asrc) 2330 if (!asrc)
2297 return 0; 2331 return 0;
2298 2332
2299 src = asrc->head_ (); 2333 object *src = asrc->head_ ();
2300 2334
2301 prev = 0; 2335 object *prev = 0;
2302 for (object *part = src; part; part = part->more) 2336 for (object *part = src; part; part = part->more)
2303 { 2337 {
2304 tmp = part->clone (); 2338 object *tmp = part->clone ();
2339
2305 tmp->x -= src->x; 2340 tmp->x -= src->x;
2306 tmp->y -= src->y; 2341 tmp->y -= src->y;
2307 2342
2308 if (!part->head) 2343 if (!part->head)
2309 { 2344 {
2319 prev->more = tmp; 2354 prev->more = tmp;
2320 2355
2321 prev = tmp; 2356 prev = tmp;
2322 } 2357 }
2323 2358
2324 for (item = src->inv; item; item = item->below) 2359 for (object *item = src->inv; item; item = item->below)
2325 insert_ob_in_ob (object_create_clone (item), dst); 2360 insert_ob_in_ob (object_create_clone (item), dst);
2326 2361
2327 return dst; 2362 return dst;
2328} 2363}
2329 2364
2543 &name, 2578 &name,
2544 title ? "\",title:\"" : "", 2579 title ? "\",title:\"" : "",
2545 title ? (const char *)title : "", 2580 title ? (const char *)title : "",
2546 flag_desc (flagdesc, 512), type); 2581 flag_desc (flagdesc, 512), type);
2547 2582
2548 if (!this->flag[FLAG_REMOVED] && env) 2583 if (!flag[FLAG_REMOVED] && env)
2549 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); 2584 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2));
2550 2585
2551 if (map) 2586 if (map)
2552 p += snprintf (p, 256, "(on %s@%d+%d)", &map->path, x, y); 2587 p += snprintf (p, 256, "(on %s@%d+%d)", &map->path, x, y);
2553 2588
2583object::open_container (object *new_container) 2618object::open_container (object *new_container)
2584{ 2619{
2585 if (container == new_container) 2620 if (container == new_container)
2586 return; 2621 return;
2587 2622
2588 if (object *old_container = container) 2623 object *old_container = container;
2624
2625 if (old_container)
2589 { 2626 {
2590 if (INVOKE_OBJECT (CLOSE, old_container, ARG_OBJECT (this))) 2627 if (INVOKE_OBJECT (CLOSE, old_container, ARG_OBJECT (this)))
2591 return; 2628 return;
2592 2629
2593#if 0 2630#if 0
2595 if (object *closer = old_container->inv) 2632 if (object *closer = old_container->inv)
2596 if (closer->type == CLOSE_CON) 2633 if (closer->type == CLOSE_CON)
2597 closer->destroy (); 2634 closer->destroy ();
2598#endif 2635#endif
2599 2636
2637 // make sure the container is available
2638 esrv_send_item (this, old_container);
2639
2600 old_container->flag [FLAG_APPLIED] = 0; 2640 old_container->flag [FLAG_APPLIED] = false;
2601 container = 0; 2641 container = 0;
2602 2642
2643 // client needs item update to make it work, client bug requires this to be separate
2603 esrv_update_item (UPD_FLAGS, this, old_container); 2644 esrv_update_item (UPD_FLAGS, this, old_container);
2645
2604 new_draw_info_format (NDI_UNIQUE, 0, this, "You close %s.", query_name (old_container)); 2646 new_draw_info_format (NDI_UNIQUE, 0, this, "You close %s.", query_name (old_container));
2605 play_sound (sound_find ("chest_close")); 2647 play_sound (sound_find ("chest_close"));
2606 } 2648 }
2607 2649
2608 if (new_container) 2650 if (new_container)
2621 } 2663 }
2622#endif 2664#endif
2623 2665
2624 new_draw_info_format (NDI_UNIQUE, 0, this, "You open %s.", query_name (new_container)); 2666 new_draw_info_format (NDI_UNIQUE, 0, this, "You open %s.", query_name (new_container));
2625 2667
2668 // make sure the container is available, client bug requires this to be separate
2669 esrv_send_item (this, new_container);
2670
2626 new_container->flag [FLAG_APPLIED] = 1; 2671 new_container->flag [FLAG_APPLIED] = true;
2627 container = new_container; 2672 container = new_container;
2628 2673
2674 // client needs flag change
2629 esrv_update_item (UPD_FLAGS, this, new_container); 2675 esrv_update_item (UPD_FLAGS, this, new_container);
2630 esrv_send_inventory (this, new_container); 2676 esrv_send_inventory (this, new_container);
2631 play_sound (sound_find ("chest_open")); 2677 play_sound (sound_find ("chest_open"));
2632 } 2678 }
2679// else if (!old_container->env && contr && contr->ns)
2680// contr->ns->floorbox_reset ();
2633} 2681}
2634 2682
2635object * 2683object *
2636object::force_find (const shstr name) 2684object::force_find (const shstr name)
2637{ 2685{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines