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.215 by root, Tue Apr 22 07:28:05 2008 UTC vs.
Revision 1.227 by root, Sat May 3 09:04:17 2008 UTC

334 334
335// find player who can see this object 335// find player who can see this object
336object * 336object *
337object::visible_to () const 337object::visible_to () const
338{ 338{
339 if (!flag [FLAG_REMOVED]) 339 if (client_visible () && !flag [FLAG_REMOVED])
340 { 340 {
341 // see if we are in a container of sorts 341 // see if we are in a container of sorts
342 if (env) 342 if (env)
343 { 343 {
344 // the player inventory itself is always visible 344 // the player inventory itself is always visible
358 else 358 else
359 { 359 {
360 // maybe there is a player standing on the same mapspace 360 // maybe there is a player standing on the same mapspace
361 // this will catch the case where "this" is a player 361 // this will catch the case where "this" is a player
362 if (object *pl = ms ().player ()) 362 if (object *pl = ms ().player ())
363 if (!pl->container || this == pl->container)
363 return pl; 364 return pl;
364 } 365 }
365 } 366 }
366 367
367 return 0; 368 return 0;
368} 369}
602 } 603 }
603 604
604 op->key_values = 0; 605 op->key_values = 0;
605} 606}
606 607
607object & 608/*
608object::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)
609{ 618{
610 bool is_freed = flag [FLAG_FREED]; 619 dst->remove ();
611 bool is_removed = flag [FLAG_REMOVED];
612
613 *(object_copy *)this = src; 620 *(object_copy *)dst = *this;
614
615 flag [FLAG_FREED] = is_freed;
616 flag [FLAG_REMOVED] = is_removed; 621 dst->flag [FLAG_REMOVED] = true;
617 622
618 /* Copy over key_values, if any. */ 623 /* Copy over key_values, if any. */
619 if (src.key_values) 624 if (key_values)
620 { 625 {
621 key_value *tail = 0; 626 key_value *tail = 0;
622 key_values = 0; 627 dst->key_values = 0;
623 628
624 for (key_value *i = src.key_values; i; i = i->next) 629 for (key_value *i = key_values; i; i = i->next)
625 { 630 {
626 key_value *new_link = new key_value; 631 key_value *new_link = new key_value;
627 632
628 new_link->next = 0; 633 new_link->next = 0;
629 new_link->key = i->key; 634 new_link->key = i->key;
630 new_link->value = i->value; 635 new_link->value = i->value;
631 636
632 /* Try and be clever here, too. */ 637 /* Try and be clever here, too. */
633 if (!key_values) 638 if (!dst->key_values)
634 { 639 {
635 key_values = new_link; 640 dst->key_values = new_link;
636 tail = new_link; 641 tail = new_link;
637 } 642 }
638 else 643 else
639 { 644 {
640 tail->next = new_link; 645 tail->next = new_link;
641 tail = new_link; 646 tail = new_link;
642 } 647 }
643 } 648 }
644 } 649 }
645}
646
647/*
648 * copy_to first frees everything allocated by the dst object,
649 * and then copies the contents of itself into the second
650 * object, allocating what needs to be allocated. Basically, any
651 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
652 * if the first object is freed, the pointers in the new object
653 * will point at garbage.
654 */
655void
656object::copy_to (object *dst)
657{
658 *dst = *this;
659 650
660 if (speed < 0) 651 if (speed < 0)
661 dst->speed_left -= rndm (); 652 dst->speed_left -= rndm ();
662 653
663 dst->set_speed (dst->speed); 654 dst->set_speed (dst->speed);
685object * 676object *
686object::clone () 677object::clone ()
687{ 678{
688 object *neu = create (); 679 object *neu = create ();
689 copy_to (neu); 680 copy_to (neu);
681 neu->map = map; // not copied by copy_to
690 return neu; 682 return neu;
691} 683}
692 684
693/* 685/*
694 * 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
745 * UP_OBJ_FACE: only the objects face has changed. 737 * UP_OBJ_FACE: only the objects face has changed.
746 */ 738 */
747void 739void
748update_object (object *op, int action) 740update_object (object *op, int action)
749{ 741{
750 if (op == NULL) 742 if (!op)
751 { 743 {
752 /* this should never happen */ 744 /* this should never happen */
753 LOG (llevDebug, "update_object() called for NULL object.\n"); 745 LOG (llevError | logBacktrace, "update_object() called for NULL object.\n");
754 return; 746 return;
755 } 747 }
756 748
757 if (op->env) 749 if (!op->is_on_map ())
758 { 750 {
759 /* Animation is currently handled by client, so nothing 751 /* Animation is currently handled by client, so nothing
760 * to do in this case. 752 * to do in this case.
761 */ 753 */
762 return; 754 return;
763 } 755 }
764
765 /* If the map is saving, don't do anything as everything is
766 * going to get freed anyways.
767 */
768 if (!op->map || op->map->in_memory == MAP_SAVING)
769 return;
770 756
771 /* make sure the object is within map boundaries */ 757 /* make sure the object is within map boundaries */
772 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)
773 { 759 {
774 LOG (llevError, "update_object() called for object out of map!\n"); 760 LOG (llevError, "update_object() called for object out of map!\n");
917object::destroy_inv (bool drop_to_ground) 903object::destroy_inv (bool drop_to_ground)
918{ 904{
919 // need to check first, because the checks below might segfault 905 // need to check first, because the checks below might segfault
920 // as we might be on an invalid mapspace and crossfire code 906 // as we might be on an invalid mapspace and crossfire code
921 // is too buggy to ensure that the inventory is empty. 907 // is too buggy to ensure that the inventory is empty.
922 // corollary: if you create arrows etc. with stuff in tis inventory, 908 // corollary: if you create arrows etc. with stuff in its inventory,
923 // cf will crash below with off-map x and y 909 // cf will crash below with off-map x and y
924 if (!inv) 910 if (!inv)
925 return; 911 return;
926 912
927 /* Only if the space blocks everything do we not process - 913 /* Only if the space blocks everything do we not process -
934 || map->nodrop 920 || map->nodrop
935 || ms ().move_block == MOVE_ALL) 921 || ms ().move_block == MOVE_ALL)
936 { 922 {
937 while (inv) 923 while (inv)
938 { 924 {
939 inv->destroy_inv (drop_to_ground); 925 inv->destroy_inv (false);
940 inv->destroy (); 926 inv->destroy ();
941 } 927 }
942 } 928 }
943 else 929 else
944 { /* Put objects in inventory onto this space */ 930 { /* Put objects in inventory onto this space */
950 || op->flag [FLAG_NO_DROP] 936 || op->flag [FLAG_NO_DROP]
951 || op->type == RUNE 937 || op->type == RUNE
952 || op->type == TRAP 938 || op->type == TRAP
953 || op->flag [FLAG_IS_A_TEMPLATE] 939 || op->flag [FLAG_IS_A_TEMPLATE]
954 || op->flag [FLAG_DESTROY_ON_DEATH]) 940 || op->flag [FLAG_DESTROY_ON_DEATH])
955 op->destroy (); 941 op->destroy (true);
956 else 942 else
957 map->insert (op, x, y); 943 map->insert (op, x, y);
958 } 944 }
959 } 945 }
960} 946}
964 object *op = new object; 950 object *op = new object;
965 op->link (); 951 op->link ();
966 return op; 952 return op;
967} 953}
968 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
969void 970void
970object::do_destroy () 971object::do_destroy ()
971{ 972{
972 if (flag [FLAG_IS_LINKED]) 973 if (flag [FLAG_IS_LINKED])
973 remove_button_link (this); 974 remove_button_link (this);
977 978
978 remove (); 979 remove ();
979 980
980 attachable::do_destroy (); 981 attachable::do_destroy ();
981 982
982 destroy_inv (true);
983
984 deactivate (); 983 deactivate ();
985 unlink (); 984 unlink ();
986 985
987 flag [FLAG_FREED] = 1; 986 flag [FLAG_FREED] = 1;
988 987
989 // hack to ensure that freed objects still have a valid map 988 // hack to ensure that freed objects still have a valid map
990 {
991 static maptile *freed_map; // freed objects are moved here to avoid crashes
992
993 if (!freed_map)
994 {
995 freed_map = new maptile;
996
997 freed_map->path = "<freed objects map>";
998 freed_map->name = "/internal/freed_objects_map";
999 freed_map->width = 3;
1000 freed_map->height = 3;
1001 freed_map->nodrop = 1;
1002
1003 freed_map->alloc ();
1004 freed_map->in_memory = MAP_ACTIVE;
1005 }
1006
1007 map = freed_map; 989 map = &freed_map;
1008 x = 1; 990 x = 1;
1009 y = 1; 991 y = 1;
1010 }
1011 992
1012 if (more) 993 if (more)
1013 { 994 {
1014 more->destroy (); 995 more->destroy ();
1015 more = 0; 996 more = 0;
1028object::destroy (bool destroy_inventory) 1009object::destroy (bool destroy_inventory)
1029{ 1010{
1030 if (destroyed ()) 1011 if (destroyed ())
1031 return; 1012 return;
1032 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
1033 if (destroy_inventory) 1021 destroy_inv (!destroy_inventory);
1034 destroy_inv (false);
1035 1022
1036 if (is_head ()) 1023 if (is_head ())
1037 if (sound_destroy) 1024 if (sound_destroy)
1038 play_sound (sound_destroy); 1025 play_sound (sound_destroy);
1039 else if (flag [FLAG_MONSTER]) 1026 else if (flag [FLAG_MONSTER])
1058 if (flag [FLAG_REMOVED]) 1045 if (flag [FLAG_REMOVED])
1059 return; 1046 return;
1060 1047
1061 INVOKE_OBJECT (REMOVE, this); 1048 INVOKE_OBJECT (REMOVE, this);
1062 1049
1063 if (object *pl = visible_to ())
1064 esrv_del_item (pl->contr, count);
1065
1066 flag [FLAG_REMOVED] = true; 1050 flag [FLAG_REMOVED] = true;
1067 1051
1068 if (more) 1052 if (more)
1069 more->remove (); 1053 more->remove ();
1070 1054
1072 * In this case, the object to be removed is in someones 1056 * In this case, the object to be removed is in someones
1073 * inventory. 1057 * inventory.
1074 */ 1058 */
1075 if (env) 1059 if (env)
1076 { 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
1077 adjust_weight (env, -total_weight ()); 1066 adjust_weight (env, -total_weight ());
1078 1067
1079 *(above ? &above->below : &env->inv) = below; 1068 *(above ? &above->below : &env->inv) = below;
1080 1069
1081 if (below) 1070 if (below)
1099 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))
1100 otmp->update_stats (); 1089 otmp->update_stats ();
1101 } 1090 }
1102 else if (map) 1091 else if (map)
1103 { 1092 {
1104 if (type == PLAYER)
1105 {
1106 // leaving a spot always closes any open container on the ground
1107 if (container && !container->env)
1108 // this causes spurious floorbox updates, but it ensures
1109 // that the CLOSE event is being sent.
1110 close_container ();
1111
1112 --map->players;
1113 map->touch ();
1114 }
1115
1116 map->dirty = true; 1093 map->dirty = true;
1117 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 }
1118 1117
1119 /* link the object above us */ 1118 /* link the object above us */
1120 if (above) 1119 if (above)
1121 above->below = below; 1120 above->below = below;
1122 else 1121 else
1152 * removed (most likely destroyed), update the player view 1151 * removed (most likely destroyed), update the player view
1153 * appropriately. 1152 * appropriately.
1154 */ 1153 */
1155 pl->close_container (); 1154 pl->close_container ();
1156 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)
1157 pl->contr->ns->floorbox_update (); 1160 pl->contr->ns->floorbox_update ();
1158 } 1161 }
1159 1162
1160 for (tmp = ms.bot; tmp; tmp = tmp->above) 1163 for (tmp = ms.bot; tmp; tmp = tmp->above)
1161 { 1164 {
1162 /* 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
1288object * 1291object *
1289insert_ob_in_map (object *op, maptile *m, object *originator, int flag) 1292insert_ob_in_map (object *op, maptile *m, object *originator, int flag)
1290{ 1293{
1291 assert (!op->flag [FLAG_FREED]); 1294 assert (!op->flag [FLAG_FREED]);
1292 1295
1293 object *top, *floor = NULL;
1294
1295 op->remove (); 1296 op->remove ();
1296 1297
1297 /* Ideally, the caller figures this out. However, it complicates a lot 1298 /* Ideally, the caller figures this out. However, it complicates a lot
1298 * 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
1299 * need extra work 1300 * need extra work
1300 */ 1301 */
1301 if (!xy_normalise (m, op->x, op->y)) 1302 if (!xy_normalise (m, op->x, op->y))
1302 { 1303 {
1303 op->destroy (); 1304 op->destroy (1);
1304 return 0; 1305 return 0;
1305 } 1306 }
1306 1307
1307 if (object *more = op->more) 1308 if (object *more = op->more)
1308 if (!insert_ob_in_map (more, m, originator, flag)) 1309 if (!insert_ob_in_map (more, m, originator, flag))
1317 */ 1318 */
1318 if (op->nrof && !(flag & INS_NO_MERGE)) 1319 if (op->nrof && !(flag & INS_NO_MERGE))
1319 for (object *tmp = ms.bot; tmp; tmp = tmp->above) 1320 for (object *tmp = ms.bot; tmp; tmp = tmp->above)
1320 if (object::can_merge (op, tmp)) 1321 if (object::can_merge (op, tmp))
1321 { 1322 {
1323 // TODO: we atcually want to update tmp, not op,
1324 // but some caller surely breaks when we return tmp
1325 // from here :/
1322 op->nrof += tmp->nrof; 1326 op->nrof += tmp->nrof;
1323 tmp->destroy (1); 1327 tmp->destroy (1);
1324 } 1328 }
1325 1329
1326 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 */
1348 /* since *below* originator, no need to update top */ 1352 /* since *below* originator, no need to update top */
1349 originator->below = op; 1353 originator->below = op;
1350 } 1354 }
1351 else 1355 else
1352 { 1356 {
1357 object *top, *floor = NULL;
1358
1353 top = ms.bot; 1359 top = ms.bot;
1354 1360
1355 /* If there are other objects, then */ 1361 /* If there are other objects, then */
1356 if (top) 1362 if (top)
1357 { 1363 {
1456 } 1462 }
1457 1463
1458 op->map->dirty = true; 1464 op->map->dirty = true;
1459 1465
1460 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)
1461 pl->contr->ns->floorbox_update (); 1471 pl->contr->ns->floorbox_update ();
1462 1472
1463 /* If this object glows, it may affect lighting conditions that are 1473 /* If this object glows, it may affect lighting conditions that are
1464 * 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
1465 * 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
1466 * on the map will get recalculated. The players could very well 1476 * on the map will get recalculated. The players could very well
2313 * create clone from object to another 2323 * create clone from object to another
2314 */ 2324 */
2315object * 2325object *
2316object_create_clone (object *asrc) 2326object_create_clone (object *asrc)
2317{ 2327{
2318 object *dst = 0, *tmp, *src, *prev, *item; 2328 object *dst = 0;
2319 2329
2320 if (!asrc) 2330 if (!asrc)
2321 return 0; 2331 return 0;
2322 2332
2323 src = asrc->head_ (); 2333 object *src = asrc->head_ ();
2324 2334
2325 prev = 0; 2335 object *prev = 0;
2326 for (object *part = src; part; part = part->more) 2336 for (object *part = src; part; part = part->more)
2327 { 2337 {
2328 tmp = part->clone (); 2338 object *tmp = part->clone ();
2339
2329 tmp->x -= src->x; 2340 tmp->x -= src->x;
2330 tmp->y -= src->y; 2341 tmp->y -= src->y;
2331 2342
2332 if (!part->head) 2343 if (!part->head)
2333 { 2344 {
2343 prev->more = tmp; 2354 prev->more = tmp;
2344 2355
2345 prev = tmp; 2356 prev = tmp;
2346 } 2357 }
2347 2358
2348 for (item = src->inv; item; item = item->below) 2359 for (object *item = src->inv; item; item = item->below)
2349 insert_ob_in_ob (object_create_clone (item), dst); 2360 insert_ob_in_ob (object_create_clone (item), dst);
2350 2361
2351 return dst; 2362 return dst;
2352} 2363}
2353 2364
2567 &name, 2578 &name,
2568 title ? "\",title:\"" : "", 2579 title ? "\",title:\"" : "",
2569 title ? (const char *)title : "", 2580 title ? (const char *)title : "",
2570 flag_desc (flagdesc, 512), type); 2581 flag_desc (flagdesc, 512), type);
2571 2582
2572 if (!this->flag[FLAG_REMOVED] && env) 2583 if (!flag[FLAG_REMOVED] && env)
2573 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); 2584 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2));
2574 2585
2575 if (map) 2586 if (map)
2576 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);
2577 2588
2607object::open_container (object *new_container) 2618object::open_container (object *new_container)
2608{ 2619{
2609 if (container == new_container) 2620 if (container == new_container)
2610 return; 2621 return;
2611 2622
2612 if (object *old_container = container) 2623 object *old_container = container;
2624
2625 if (old_container)
2613 { 2626 {
2614 if (INVOKE_OBJECT (CLOSE, old_container, ARG_OBJECT (this))) 2627 if (INVOKE_OBJECT (CLOSE, old_container, ARG_OBJECT (this)))
2615 return; 2628 return;
2616 2629
2617#if 0 2630#if 0
2619 if (object *closer = old_container->inv) 2632 if (object *closer = old_container->inv)
2620 if (closer->type == CLOSE_CON) 2633 if (closer->type == CLOSE_CON)
2621 closer->destroy (); 2634 closer->destroy ();
2622#endif 2635#endif
2623 2636
2637 // make sure the container is available
2638 esrv_send_item (this, old_container);
2639
2624 old_container->flag [FLAG_APPLIED] = 0; 2640 old_container->flag [FLAG_APPLIED] = false;
2625 container = 0; 2641 container = 0;
2626 2642
2643 // client needs item update to make it work, client bug requires this to be separate
2627 esrv_update_item (UPD_FLAGS, this, old_container); 2644 esrv_update_item (UPD_FLAGS, this, old_container);
2645
2628 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));
2629 play_sound (sound_find ("chest_close")); 2647 play_sound (sound_find ("chest_close"));
2630 } 2648 }
2631 2649
2632 if (new_container) 2650 if (new_container)
2645 } 2663 }
2646#endif 2664#endif
2647 2665
2648 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));
2649 2667
2668 // make sure the container is available, client bug requires this to be separate
2669 esrv_send_item (this, new_container);
2670
2650 new_container->flag [FLAG_APPLIED] = 1; 2671 new_container->flag [FLAG_APPLIED] = true;
2651 container = new_container; 2672 container = new_container;
2652 2673
2674 // client needs flag change
2653 esrv_update_item (UPD_FLAGS, this, new_container); 2675 esrv_update_item (UPD_FLAGS, this, new_container);
2654 esrv_send_inventory (this, new_container); 2676 esrv_send_inventory (this, new_container);
2655 play_sound (sound_find ("chest_open")); 2677 play_sound (sound_find ("chest_open"));
2656 } 2678 }
2679// else if (!old_container->env && contr && contr->ns)
2680// contr->ns->floorbox_reset ();
2657} 2681}
2658 2682
2659object * 2683object *
2660object::force_find (const shstr name) 2684object::force_find (const shstr name)
2661{ 2685{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines