From 3de176cc587c4e0601c3c3f5a049e30db6bd2c17 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 22 Dec 2010 16:30:23 +0200 Subject: crafting system! --- src/inventory.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) (limited to 'src/inventory.cpp') diff --git a/src/inventory.cpp b/src/inventory.cpp index 069355aff..073477dde 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -348,6 +348,71 @@ bool InventoryList::addItem(InventoryItem *newitem) return false; } +bool InventoryList::addItem(u32 i, InventoryItem *newitem) +{ + // If it is an empty position, it's an easy job. + InventoryItem *item = m_items[i]; + if(item == NULL) + { + m_items[i] = newitem; + return true; + } + + // If it is a material item, try to + if(std::string("MaterialItem") == newitem->getName()) + { + u8 material = ((MaterialItem*)newitem)->getMaterial(); + u8 count = ((MaterialItem*)newitem)->getCount(); + InventoryItem *item2 = m_items[i]; + + if(item2 != NULL + && std::string("MaterialItem") == item2->getName()) + { + // Check if it is of the right material and has free space + MaterialItem *mitem2 = (MaterialItem*)item2; + if(mitem2->getMaterial() == material + && mitem2->freeSpace() >= count) + { + // Add to the counter + mitem2->add(count); + // Dump the parameter + delete newitem; + // Done + return true; + } + } + } + + return false; +} + +void InventoryList::decrementMaterials(u16 count) +{ + for(u32 i=0; igetName()) + { + MaterialItem *mitem = (MaterialItem*)item; + if(mitem->getCount() < count) + { + dstream<<__FUNCTION_NAME<<": decrementMaterials():" + <<" too small material count"<getCount() == count) + { + deleteItem(i); + } + else + { + mitem->remove(1); + } + } + } +} + void InventoryList::print(std::ostream &o) { o<<"InventoryList:"<getItem(from_i)="<getItem(from_i) + <getItem(to_i)="<getItem(to_i) + <getItem(from_i) == NULL + || (list_from == list_to && from_i == to_i)) + { + dstream<<__FUNCTION_NAME<<": Operation not allowed"<changeItem(from_i, NULL); + // Try to add the item to destination list + if(list_to->addItem(to_i, item1)) + { + // Done. + return; + } + // Adding was not possible, switch it. + // Switch it to the destination list + InventoryItem *item2 = list_to->changeItem(to_i, item1); + // Put item from destination list to the source list + list_from->changeItem(from_i, item2); +} //END -- cgit v1.2.3