3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "inventory.h"
21 #include "serialization.h"
26 #include "util/strfnd.h"
27 #include "content_mapnode.h" // For loading legacy MaterialItems
28 #include "nameidmapping.h" // For loading legacy MaterialItems
29 #include "util/serialize.h"
30 #include "util/string.h"
36 static content_t content_translate_from_19_to_internal(content_t c_from)
38 for (const auto &tt : trans_table_19) {
46 ItemStack::ItemStack(const std::string &name_, u16 count_,
47 u16 wear_, IItemDefManager *itemdef) :
48 name(itemdef->getAlias(name_)),
52 if (name.empty() || count == 0)
54 else if (itemdef->get(name).type == ITEM_TOOL)
58 void ItemStack::serialize(std::ostream &os) const
63 // Check how many parts of the itemstring are needed
69 if (!metadata.empty())
72 os<<serializeJsonStringIfNeeded(name);
79 metadata.serialize(os);
83 void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
88 name = deSerializeJsonStringIfNeeded(is);
92 std::getline(is, tmp, ' ');
94 throw SerializationError("Unexpected text after item name");
96 if(name == "MaterialItem")
98 // Obsoleted on 2011-07-30
104 // Convert old materials
106 material = content_translate_from_19_to_internal(material);
108 throw SerializationError("Too large material number");
109 // Convert old id to name
110 NameIdMapping legacy_nimap;
111 content_mapnode_get_name_id_mapping(&legacy_nimap);
112 legacy_nimap.getName(material, name);
114 name = "unknown_block";
116 name = itemdef->getAlias(name);
117 count = materialcount;
119 else if(name == "MaterialItem2")
121 // Obsoleted on 2011-11-16
128 throw SerializationError("Too large material number");
129 // Convert old id to name
130 NameIdMapping legacy_nimap;
131 content_mapnode_get_name_id_mapping(&legacy_nimap);
132 legacy_nimap.getName(material, name);
134 name = "unknown_block";
136 name = itemdef->getAlias(name);
137 count = materialcount;
139 else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
140 || name == "craft" || name == "CraftItem")
142 // Obsoleted on 2012-01-07
145 std::getline(is, all, '\n');
146 // First attempt to read inside ""
149 // If didn't skip to end, we have ""s
151 name = fnd.next("\"");
152 } else { // No luck, just read a word then
154 name = fnd.next(" ");
158 name = itemdef->getAlias(name);
159 count = stoi(trim(fnd.next("")));
163 else if(name == "MBOItem")
165 // Obsoleted on 2011-10-14
166 throw SerializationError("MBOItem not supported anymore");
168 else if(name == "tool" || name == "ToolItem")
170 // Obsoleted on 2012-01-07
173 std::getline(is, all, '\n');
174 // First attempt to read inside ""
177 // If didn't skip to end, we have ""s
179 name = fnd.next("\"");
180 } else { // No luck, just read a word then
182 name = fnd.next(" ");
188 name = itemdef->getAlias(name);
189 wear = stoi(trim(fnd.next("")));
193 do // This loop is just to allow "break;"
197 // Apply item aliases
199 name = itemdef->getAlias(name);
202 std::string count_str;
203 std::getline(is, count_str, ' ');
204 if (count_str.empty()) {
209 count = stoi(count_str);
212 std::string wear_str;
213 std::getline(is, wear_str, ' ');
217 wear = stoi(wear_str);
220 metadata.deSerialize(is);
222 // In case fields are added after metadata, skip space here:
223 //std::getline(is, tmp, ' ');
225 // throw SerializationError("Unexpected text after metadata");
230 if (name.empty() || count == 0)
232 else if (itemdef && itemdef->get(name).type == ITEM_TOOL)
236 void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
238 std::istringstream is(str, std::ios::binary);
239 deSerialize(is, itemdef);
242 std::string ItemStack::getItemString() const
244 std::ostringstream os(std::ios::binary);
250 ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
252 // If the item is empty or the position invalid, bail out
255 // nothing can be added trivially
257 // If this is an empty item, it's an easy job.
263 // If item name or metadata differs, bail out
264 else if (name != newitem.name
265 || metadata != newitem.metadata)
269 // If the item fits fully, add counter and delete it
270 else if(newitem.count <= freeSpace(itemdef))
275 // Else the item does not fit fully. Add all that fits and return
279 u16 freespace = freeSpace(itemdef);
281 newitem.remove(freespace);
287 bool ItemStack::itemFits(ItemStack newitem,
289 IItemDefManager *itemdef) const
292 // If the item is empty or the position invalid, bail out
295 // nothing can be added trivially
297 // If this is an empty item, it's an easy job.
302 // If item name or metadata differs, bail out
303 else if (name != newitem.name
304 || metadata != newitem.metadata)
308 // If the item fits fully, delete it
309 else if(newitem.count <= freeSpace(itemdef))
313 // Else the item does not fit fully. Return the rest.
316 u16 freespace = freeSpace(itemdef);
317 newitem.remove(freespace);
323 return newitem.empty();
326 ItemStack ItemStack::takeItem(u32 takecount)
328 if(takecount == 0 || count == 0)
331 ItemStack result = *this;
332 if(takecount >= count)
341 result.count = takecount;
346 ItemStack ItemStack::peekItem(u32 peekcount) const
348 if(peekcount == 0 || count == 0)
351 ItemStack result = *this;
352 if(peekcount < count)
353 result.count = peekcount;
361 InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
369 void InventoryList::clearItems()
373 for (u32 i=0; i < m_size; i++) {
374 m_items.emplace_back();
380 void InventoryList::setSize(u32 newsize)
382 if(newsize != m_items.size())
383 m_items.resize(newsize);
387 void InventoryList::setWidth(u32 newwidth)
392 void InventoryList::setName(const std::string &name)
397 void InventoryList::serialize(std::ostream &os) const
399 //os.imbue(std::locale("C"));
401 os<<"Width "<<m_width<<"\n";
403 for (const auto &item : m_items) {
413 os<<"EndInventoryList\n";
416 void InventoryList::deSerialize(std::istream &is)
418 //is.imbue(std::locale("C"));
426 std::getline(is, line, '\n');
428 std::istringstream iss(line);
429 //iss.imbue(std::locale("C"));
432 std::getline(iss, name, ' ');
434 if (name == "EndInventoryList")
437 // This is a temporary backwards compatibility fix
441 if (name == "Width") {
444 throw SerializationError("incorrect width property");
446 else if(name == "Item")
448 if(item_i > getSize() - 1)
449 throw SerializationError("too many items");
451 item.deSerialize(iss, m_itemdef);
452 m_items[item_i++] = item;
454 else if(name == "Empty")
456 if(item_i > getSize() - 1)
457 throw SerializationError("too many items");
458 m_items[item_i++].clear();
462 // Contents given to deSerialize() were not terminated properly: throw error.
464 std::ostringstream ss;
465 ss << "Malformatted inventory list. list="
466 << m_name << ", read " << item_i << " of " << getSize()
467 << " ItemStacks." << std::endl;
468 throw SerializationError(ss.str());
471 InventoryList::InventoryList(const InventoryList &other)
476 InventoryList & InventoryList::operator = (const InventoryList &other)
478 m_items = other.m_items;
479 m_size = other.m_size;
480 m_width = other.m_width;
481 m_name = other.m_name;
482 m_itemdef = other.m_itemdef;
488 bool InventoryList::operator == (const InventoryList &other) const
490 if(m_size != other.m_size)
492 if(m_width != other.m_width)
494 if(m_name != other.m_name)
496 for (u32 i = 0; i < m_items.size(); i++)
497 if (m_items[i] != other.m_items[i])
503 const std::string &InventoryList::getName() const
508 u32 InventoryList::getSize() const
510 return m_items.size();
513 u32 InventoryList::getWidth() const
518 u32 InventoryList::getUsedSlots() const
521 for (const auto &m_item : m_items) {
528 u32 InventoryList::getFreeSlots() const
530 return getSize() - getUsedSlots();
533 const ItemStack& InventoryList::getItem(u32 i) const
535 assert(i < m_size); // Pre-condition
539 ItemStack& InventoryList::getItem(u32 i)
541 assert(i < m_size); // Pre-condition
545 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
547 if(i >= m_items.size())
550 ItemStack olditem = m_items[i];
551 m_items[i] = newitem;
556 void InventoryList::deleteItem(u32 i)
558 assert(i < m_items.size()); // Pre-condition
562 ItemStack InventoryList::addItem(const ItemStack &newitem_)
564 ItemStack newitem = newitem_;
570 First try to find if it could be added to some existing items
572 for(u32 i=0; i<m_items.size(); i++)
574 // Ignore empty slots
575 if(m_items[i].empty())
578 newitem = addItem(i, newitem);
580 return newitem; // All was eaten
584 Then try to add it to empty slots
586 for(u32 i=0; i<m_items.size(); i++)
588 // Ignore unempty slots
589 if(!m_items[i].empty())
592 newitem = addItem(i, newitem);
594 return newitem; // All was eaten
601 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
603 if(i >= m_items.size())
606 ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
607 //if(leftover != newitem)
612 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
613 ItemStack *restitem) const
615 if(i >= m_items.size())
622 return m_items[i].itemFits(newitem, restitem, m_itemdef);
625 bool InventoryList::roomForItem(const ItemStack &item_) const
627 ItemStack item = item_;
629 for(u32 i=0; i<m_items.size(); i++)
631 if(itemFits(i, item, &leftover))
638 bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
640 u32 count = item.count;
644 for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
647 if (i->name == item.name && (!match_meta || (i->metadata == item.metadata))) {
648 if (i->count >= count)
657 ItemStack InventoryList::removeItem(const ItemStack &item)
660 for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
661 if (i->name == item.name) {
662 u32 still_to_remove = item.count - removed.count;
663 removed.addItem(i->takeItem(still_to_remove), m_itemdef);
664 if (removed.count == item.count)
671 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
673 if(i >= m_items.size())
676 ItemStack taken = m_items[i].takeItem(takecount);
682 void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
684 // Take item from source list
687 item1 = changeItem(i, ItemStack());
689 item1 = takeItem(i, count);
694 // Try to add the item to destination list
695 u32 dest_size = dest->getSize();
696 // First try all the non-empty slots
697 for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
698 if (!m_items[dest_i].empty()) {
699 item1 = dest->addItem(dest_i, item1);
700 if (item1.empty()) return;
704 // Then try all the empty ones
705 for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
706 if (m_items[dest_i].empty()) {
707 item1 = dest->addItem(dest_i, item1);
708 if (item1.empty()) return;
712 // If we reach this, the item was not fully added
713 // Add the remaining part back to the source item
717 u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
718 u32 count, bool swap_if_needed, bool *did_swap)
720 if(this == dest && i == dest_i)
723 // Take item from source list
726 item1 = changeItem(i, ItemStack());
728 item1 = takeItem(i, count);
733 // Try to add the item to destination list
734 u32 oldcount = item1.count;
735 item1 = dest->addItem(dest_i, item1);
737 // If something is returned, the item was not fully added
740 // If olditem is returned, nothing was added.
741 bool nothing_added = (item1.count == oldcount);
743 // If something else is returned, part of the item was left unadded.
744 // Add the other part back to the source item
747 // If olditem is returned, nothing was added.
749 if (nothing_added && swap_if_needed) {
750 // Tell that we swapped
751 if (did_swap != NULL) {
754 // Take item from source list
755 item1 = changeItem(i, ItemStack());
756 // Adding was not possible, swap the items.
757 ItemStack item2 = dest->changeItem(dest_i, item1);
758 // Put item from destination list to the source list
759 changeItem(i, item2);
762 return (oldcount - item1.count);
769 Inventory::~Inventory()
774 void Inventory::clear()
777 for (auto &m_list : m_lists) {
783 void Inventory::clearContents()
786 for (InventoryList *list : m_lists) {
787 for (u32 j=0; j<list->getSize(); j++) {
793 Inventory::Inventory(IItemDefManager *itemdef)
799 Inventory::Inventory(const Inventory &other)
805 Inventory & Inventory::operator = (const Inventory &other)
807 // Gracefully handle self assignment
812 m_itemdef = other.m_itemdef;
813 for (InventoryList *list : other.m_lists) {
814 m_lists.push_back(new InventoryList(*list));
820 bool Inventory::operator == (const Inventory &other) const
822 if(m_lists.size() != other.m_lists.size())
825 for(u32 i=0; i<m_lists.size(); i++)
827 if(*m_lists[i] != *other.m_lists[i])
833 void Inventory::serialize(std::ostream &os) const
835 for (InventoryList *list : m_lists) {
836 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
840 os<<"EndInventory\n";
843 void Inventory::deSerialize(std::istream &is)
849 std::getline(is, line, '\n');
851 std::istringstream iss(line);
854 std::getline(iss, name, ' ');
856 if (name == "EndInventory")
859 // This is a temporary backwards compatibility fix
863 if (name == "List") {
864 std::string listname;
867 std::getline(iss, listname, ' ');
870 InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
871 list->deSerialize(is);
873 m_lists.push_back(list);
877 throw SerializationError("invalid inventory specifier: " + name);
881 // Contents given to deSerialize() were not terminated properly: throw error.
883 std::ostringstream ss;
884 ss << "Malformatted inventory (damaged?). "
885 << m_lists.size() << " lists read." << std::endl;
886 throw SerializationError(ss.str());
889 InventoryList * Inventory::addList(const std::string &name, u32 size)
892 s32 i = getListIndex(name);
895 if(m_lists[i]->getSize() != size)
898 m_lists[i] = new InventoryList(name, size, m_itemdef);
904 //don't create list with invalid name
905 if (name.find(' ') != std::string::npos) return NULL;
907 InventoryList *list = new InventoryList(name, size, m_itemdef);
908 m_lists.push_back(list);
912 InventoryList * Inventory::getList(const std::string &name)
914 s32 i = getListIndex(name);
920 std::vector<const InventoryList*> Inventory::getLists()
922 std::vector<const InventoryList*> lists;
923 for (auto list : m_lists) {
924 lists.push_back(list);
929 bool Inventory::deleteList(const std::string &name)
931 s32 i = getListIndex(name);
936 m_lists.erase(m_lists.begin() + i);
940 const InventoryList * Inventory::getList(const std::string &name) const
942 s32 i = getListIndex(name);
948 const s32 Inventory::getListIndex(const std::string &name) const
950 for(u32 i=0; i<m_lists.size(); i++)
952 if(m_lists[i]->getName() == name)