+ if (move_somewhere) {
+ s16 old_to_i = to_i;
+ u16 old_count = count;
+ caused_by_move_somewhere = true;
+ move_somewhere = false;
+
+ infostream << "IMoveAction::apply(): moving item somewhere"
+ << " msom=" << move_somewhere
+ << " count=" << count
+ << " from inv=\"" << from_inv.dump() << "\""
+ << " list=\"" << from_list << "\""
+ << " i=" << from_i
+ << " to inv=\"" << to_inv.dump() << "\""
+ << " list=\"" << to_list << "\""
+ << std::endl;
+
+ // Try to add the item to destination list
+ s16 dest_size = list_to->getSize();
+ // First try all the non-empty slots
+ for (s16 dest_i = 0; dest_i < dest_size && count > 0; dest_i++) {
+ if (!list_to->getItem(dest_i).empty()) {
+ to_i = dest_i;
+ apply(mgr, player, gamedef);
+ count -= move_count;
+ }
+ }
+
+ // Then try all the empty ones
+ for (s16 dest_i = 0; dest_i < dest_size && count > 0; dest_i++) {
+ if (list_to->getItem(dest_i).empty()) {
+ to_i = dest_i;
+ apply(mgr, player, gamedef);
+ count -= move_count;
+ }
+ }
+
+ to_i = old_to_i;
+ count = old_count;
+ caused_by_move_somewhere = false;
+ move_somewhere = true;
+ return;
+ }
+
+ if ((u16)to_i > list_to->getSize()) {
+ infostream << "IMoveAction::apply(): FAIL: destination index out of bounds: "
+ << "to_i=" << to_i
+ << ", size=" << list_to->getSize() << std::endl;
+ return;
+ }