From 2e41a5e304d9c35ece851b8a65482bca8784b582 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 22 Dec 2010 11:29:06 +0200 Subject: just savin' --- src/inventory.cpp | 179 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 164 insertions(+), 15 deletions(-) (limited to 'src/inventory.cpp') diff --git a/src/inventory.cpp b/src/inventory.cpp index 3899c9394..069355aff 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -136,30 +136,39 @@ MapBlockObject * MapBlockObjectItem::createObject Inventory */ -Inventory::Inventory(u32 size) +InventoryList::InventoryList(std::string name, u32 size) { + m_name = name; m_size = size; clearItems(); } -Inventory::~Inventory() +InventoryList::~InventoryList() { for(u32 i=0; i m_items.size() - 1) return NULL; return m_items[i]; } -InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem) +InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem) { assert(i < m_items.size()); @@ -272,7 +296,7 @@ InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem) return olditem; } -void Inventory::deleteItem(u32 i) +void InventoryList::deleteItem(u32 i) { assert(i < m_items.size()); InventoryItem *item = changeItem(i, NULL); @@ -280,7 +304,7 @@ void Inventory::deleteItem(u32 i) delete item; } -bool Inventory::addItem(InventoryItem *newitem) +bool InventoryList::addItem(InventoryItem *newitem) { // If it is a MaterialItem, try to find an already existing one // and just increment the counter @@ -324,9 +348,9 @@ bool Inventory::addItem(InventoryItem *newitem) return false; } -void Inventory::print(std::ostream &o) +void InventoryList::print(std::ostream &o) { - o<<"Player inventory:"<getName()<<" "<getSize()<<"\n"; + list->serialize(os); + } + + os<<"end\n"; +} + +void Inventory::deSerialize(std::istream &is) +{ + clear(); + + for(;;) + { + std::string line; + std::getline(is, line, '\n'); + + std::istringstream iss(line); + + std::string name; + std::getline(iss, name, ' '); + + if(name == "end") + { + break; + } + else if(name == "List") + { + std::string listname; + u32 listsize; + + std::getline(iss, listname, ' '); + iss>>listsize; + + InventoryList *list = new InventoryList(listname, listsize); + list->deSerialize(is); + + m_lists.push_back(list); + } + else + { + throw SerializationError("Unknown inventory identifier"); + } + } +} + +InventoryList * Inventory::addList(const std::string &name, u32 size) +{ + s32 i = getListIndex(name); + if(i != -1) + { + if(m_lists[i]->getSize() != size) + { + delete m_lists[i]; + m_lists[i] = new InventoryList(name, size); + } + return m_lists[i]; + } + else + { + m_lists.push_back(new InventoryList(name, size)); + return m_lists.getLast(); + } +} + +InventoryList * Inventory::getList(const std::string &name) +{ + s32 i = getListIndex(name); + if(i == -1) + return NULL; + return m_lists[i]; +} + +s32 Inventory::getListIndex(const std::string &name) +{ + for(u32 i=0; igetName() == name) + return i; + } + return -1; +} + //END -- cgit v1.2.3