if (item1.empty())
return;
- // Try to add the item to destination list
- u32 dest_size = dest->getSize();
- // First try all the non-empty slots
- for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
- if (!m_items[dest_i].empty()) {
- item1 = dest->addItem(dest_i, item1);
- if (item1.empty()) return;
- }
- }
+ ItemStack leftover;
+ leftover = dest->addItem(item1);
- // Then try all the empty ones
- for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
- if (m_items[dest_i].empty()) {
- item1 = dest->addItem(dest_i, item1);
- if (item1.empty()) return;
- }
+ if (!leftover.empty()) {
+ // Add the remaining part back to the source item
+ addItem(i, leftover);
}
-
- // If we reach this, the item was not fully added
- // Add the remaining part back to the source item
- addItem(i, item1);
}
u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
void Inventory::clear()
{
- m_dirty = true;
for (auto &m_list : m_lists) {
delete m_list;
}
m_lists.clear();
+ setModified();
}
Inventory::Inventory(IItemDefManager *itemdef)
{
- m_dirty = false;
m_itemdef = itemdef;
+ setModified();
}
Inventory::Inventory(const Inventory &other)
// Gracefully handle self assignment
if(this != &other)
{
- m_dirty = true;
clear();
m_itemdef = other.m_itemdef;
for (InventoryList *list : other.m_lists) {
m_lists.push_back(new InventoryList(*list));
}
+ setModified();
}
return *this;
}
void Inventory::serialize(std::ostream &os, bool incremental) const
{
+ //std::cout << "Serialize " << (int)incremental << ", n=" << m_lists.size() << std::endl;
for (const InventoryList *list : m_lists) {
if (!incremental || list->checkModified()) {
os << "List " << list->getName() << " " << list->getSize() << "\n";
delete list;
list = nullptr;
- m_dirty = true;
+ setModified();
}
m_lists.erase(std::remove(m_lists.begin(), m_lists.end(),
nullptr), m_lists.end());
InventoryList * Inventory::addList(const std::string &name, u32 size)
{
- m_dirty = true;
+ setModified();
s32 i = getListIndex(name);
if(i != -1)
{
s32 i = getListIndex(name);
if(i == -1)
return false;
- m_dirty = true;
+
+ setModified();
delete m_lists[i];
m_lists.erase(m_lists.begin() + i);
return true;