Fix stuff in inventory.{cpp,h}

This commit is contained in:
Perttu Ahola 2011-11-28 14:55:24 +02:00
parent 7bb4b7911b
commit e880cc40e9
2 changed files with 25 additions and 3 deletions

@ -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() { std::string InventoryItem::getItemString() {
// Get item string // Get item string
std::ostringstream os(std::ios_base::binary); std::ostringstream os(std::ios_base::binary);
@ -535,21 +542,22 @@ u32 InventoryList::getFreeSlots()
const InventoryItem * InventoryList::getItem(u32 i) const const InventoryItem * InventoryList::getItem(u32 i) const
{ {
if(i > m_items.size() - 1) if(i >= m_items.size())
return NULL; return NULL;
return m_items[i]; return m_items[i];
} }
InventoryItem * InventoryList::getItem(u32 i) InventoryItem * InventoryList::getItem(u32 i)
{ {
if(i > m_items.size() - 1) if(i >= m_items.size())
return NULL; return NULL;
return m_items[i]; return m_items[i];
} }
InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem) InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
{ {
assert(i < m_items.size()); if(i >= m_items.size())
return newitem;
InventoryItem *olditem = m_items[i]; InventoryItem *olditem = m_items[i];
m_items[i] = newitem; m_items[i] = newitem;
@ -606,6 +614,8 @@ InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
{ {
if(newitem == NULL) if(newitem == NULL)
return NULL; return NULL;
if(i >= m_items.size())
return newitem;
//setDirty(true); //setDirty(true);
@ -853,6 +863,16 @@ InventoryList * Inventory::getList(const std::string &name)
return m_lists[i]; 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 const InventoryList * Inventory::getList(const std::string &name) const
{ {
s32 i = getListIndex(name); s32 i = getListIndex(name);

@ -41,6 +41,8 @@ public:
virtual ~InventoryItem(); virtual ~InventoryItem();
static InventoryItem* deSerialize(std::istream &is, IGameDef *gamedef); static InventoryItem* deSerialize(std::istream &is, IGameDef *gamedef);
static InventoryItem* deSerialize(const std::string &str,
IGameDef *gamedef);
virtual const char* getName() const = 0; virtual const char* getName() const = 0;
// Shall write the name and the parameters // Shall write the name and the parameters