Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
+GNU Lesser General Public License for more details.
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "inventory.h"
#include "serialization.h"
-#include "utility.h"
#include "debug.h"
#include <sstream>
#include "log.h"
#include "strfnd.h"
#include "content_mapnode.h" // For loading legacy MaterialItems
#include "nameidmapping.h" // For loading legacy MaterialItems
+#include "util/serialize.h"
+#include "util/string.h"
/*
ItemStack
count = 1;
// Then read wear
fnd.skip_over(" ");
+ name = itemdef->getAlias(name);
wear = stoi(trim(fnd.next("")));
}
else
{
m_name = name;
m_size = size;
+ m_width = 0;
m_itemdef = itemdef;
clearItems();
//m_dirty = false;
m_size = newsize;
}
+void InventoryList::setWidth(u32 newwidth)
+{
+ m_width = newwidth;
+}
+
+void InventoryList::setName(const std::string &name)
+{
+ m_name = name;
+}
+
void InventoryList::serialize(std::ostream &os) const
{
//os.imbue(std::locale("C"));
+ os<<"Width "<<m_width<<"\n";
+
for(u32 i=0; i<m_items.size(); i++)
{
const ItemStack &item = m_items[i];
clearItems();
u32 item_i = 0;
+ m_width = 0;
for(;;)
{
{
break;
}
+ else if(name == "Width")
+ {
+ iss >> m_width;
+ if (iss.fail())
+ throw SerializationError("incorrect width property");
+ }
else if(name == "Item")
{
if(item_i > getSize() - 1)
throw SerializationError("too many items");
m_items[item_i++].clear();
}
- else
- {
- throw SerializationError("Unknown inventory identifier");
- }
}
}
{
m_items = other.m_items;
m_size = other.m_size;
+ m_width = other.m_width;
m_name = other.m_name;
m_itemdef = other.m_itemdef;
//setDirty(true);
return m_items.size();
}
+u32 InventoryList::getWidth() const
+{
+ return m_width;
+}
+
u32 InventoryList::getUsedSlots() const
{
u32 num = 0;
return m_items[i].peekItem(peekcount);
}
+void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count)
+{
+ if(this == dest && i == dest_i)
+ return;
+
+ // Take item from source list
+ ItemStack item1;
+ if(count == 0)
+ item1 = changeItem(i, ItemStack());
+ else
+ item1 = takeItem(i, count);
+
+ if(item1.empty())
+ return;
+
+ // Try to add the item to destination list
+ u32 oldcount = item1.count;
+ item1 = dest->addItem(dest_i, item1);
+
+ // If something is returned, the item was not fully added
+ if(!item1.empty())
+ {
+ // If olditem is returned, nothing was added.
+ bool nothing_added = (item1.count == oldcount);
+
+ // If something else is returned, part of the item was left unadded.
+ // Add the other part back to the source item
+ addItem(i, item1);
+
+ // If olditem is returned, nothing was added.
+ // Swap the items
+ if(nothing_added)
+ {
+ // Take item from source list
+ item1 = changeItem(i, ItemStack());
+ // Adding was not possible, swap the items.
+ ItemStack item2 = dest->changeItem(dest_i, item1);
+ // Put item from destination list to the source list
+ changeItem(i, item2);
+ }
+ }
+}
+
/*
Inventory
*/
m_lists.clear();
}
+void Inventory::clearContents()
+{
+ for(u32 i=0; i<m_lists.size(); i++)
+ {
+ InventoryList *list = m_lists[i];
+ for(u32 j=0; j<list->getSize(); j++)
+ {
+ list->deleteItem(j);
+ }
+ }
+}
+
Inventory::Inventory(IItemDefManager *itemdef)
{
m_itemdef = itemdef;
m_lists.push_back(list);
}
- else
- {
- throw SerializationError("Unknown inventory identifier");
- }
}
}
return m_lists[i];
}
+std::vector<const InventoryList*> Inventory::getLists()
+{
+ std::vector<const InventoryList*> lists;
+ for(u32 i=0; i<m_lists.size(); i++)
+ {
+ InventoryList *list = m_lists[i];
+ lists.push_back(list);
+ }
+ return lists;
+}
+
bool Inventory::deleteList(const std::string &name)
{
s32 i = getListIndex(name);