Fix stuff in inventory.{cpp,h}
authorPerttu Ahola <celeron55@gmail.com>
Mon, 28 Nov 2011 12:55:24 +0000 (14:55 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:57 +0000 (19:13 +0200)
src/inventory.cpp
src/inventory.h

index f3b81090be892e561dca924cb4f6a9dcd6b109e4..4e897d9ff343e26827cf96462c913453784465be 100644 (file)
@@ -168,6 +168,13 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is, IGameDef *gamedef)
        }
 }
 
+InventoryItem* InventoryItem::deSerialize(const std::string &str,
+               IGameDef *gamedef)
+{
+       std::istringstream is(str, std::ios_base::binary);
+       return deSerialize(is, gamedef);
+}
+
 std::string InventoryItem::getItemString() {
        // Get item string
        std::ostringstream os(std::ios_base::binary);
@@ -535,21 +542,22 @@ u32 InventoryList::getFreeSlots()
 
 const InventoryItem * InventoryList::getItem(u32 i) const
 {
-       if(i > m_items.size() - 1)
+       if(i >= m_items.size())
                return NULL;
        return m_items[i];
 }
 
 InventoryItem * InventoryList::getItem(u32 i)
 {
-       if(i > m_items.size() - 1)
+       if(i >= m_items.size())
                return NULL;
        return m_items[i];
 }
 
 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
 {
-       assert(i < m_items.size());
+       if(i >= m_items.size())
+               return newitem;
 
        InventoryItem *olditem = m_items[i];
        m_items[i] = newitem;
@@ -606,6 +614,8 @@ InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
 {
        if(newitem == NULL)
                return NULL;
+       if(i >= m_items.size())
+               return newitem;
        
        //setDirty(true);
        
@@ -853,6 +863,16 @@ InventoryList * Inventory::getList(const std::string &name)
        return m_lists[i];
 }
 
+bool Inventory::deleteList(const std::string &name)
+{
+       s32 i = getListIndex(name);
+       if(i == -1)
+               return false;
+       delete m_lists[i];
+       m_lists.erase(i);
+       return true;
+}
+
 const InventoryList * Inventory::getList(const std::string &name) const
 {
        s32 i = getListIndex(name);
index 441b2ca4d09035bc1f28b208bc47a6ff451c230d..6253ea50e659a180a93b134fbe12da935f31768c 100644 (file)
@@ -41,6 +41,8 @@ public:
        virtual ~InventoryItem();
        
        static InventoryItem* deSerialize(std::istream &is, IGameDef *gamedef);
+       static InventoryItem* deSerialize(const std::string &str,
+                       IGameDef *gamedef);
        
        virtual const char* getName() const = 0;
        // Shall write the name and the parameters