X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Finventory.cpp;h=069355affeeee45fd8235c7ca9e766efadc2c0e2;hb=2e41a5e304d9c35ece851b8a65482bca8784b582;hp=3899c9394186cdf3e5cac86ed81d115c64618c88;hpb=4ec61b0ccdedecf54cd697d0b1cfa16aaf92bf82;p=dragonfireclient.git 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