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
60 DSTACK(FUNCTION_NAME);
65 // Check how many parts of the itemstring are needed
71 if (!metadata.empty())
74 os<<serializeJsonStringIfNeeded(name);
81 metadata.serialize(os);
85 void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
87 DSTACK(FUNCTION_NAME);
92 name = deSerializeJsonStringIfNeeded(is);
96 std::getline(is, tmp, ' ');
98 throw SerializationError("Unexpected text after item name");
100 if(name == "MaterialItem")
102 // Obsoleted on 2011-07-30
108 // Convert old materials
110 material = content_translate_from_19_to_internal(material);
112 throw SerializationError("Too large material number");
113 // Convert old id to name
114 NameIdMapping legacy_nimap;
115 content_mapnode_get_name_id_mapping(&legacy_nimap);
116 legacy_nimap.getName(material, name);
118 name = "unknown_block";
120 name = itemdef->getAlias(name);
121 count = materialcount;
123 else if(name == "MaterialItem2")
125 // Obsoleted on 2011-11-16
132 throw SerializationError("Too large material number");
133 // Convert old id to name
134 NameIdMapping legacy_nimap;
135 content_mapnode_get_name_id_mapping(&legacy_nimap);
136 legacy_nimap.getName(material, name);
138 name = "unknown_block";
140 name = itemdef->getAlias(name);
141 count = materialcount;
143 else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
144 || name == "craft" || name == "CraftItem")
146 // Obsoleted on 2012-01-07
149 std::getline(is, all, '\n');
150 // First attempt to read inside ""
153 // If didn't skip to end, we have ""s
155 name = fnd.next("\"");
156 } else { // No luck, just read a word then
158 name = fnd.next(" ");
162 name = itemdef->getAlias(name);
163 count = stoi(trim(fnd.next("")));
167 else if(name == "MBOItem")
169 // Obsoleted on 2011-10-14
170 throw SerializationError("MBOItem not supported anymore");
172 else if(name == "tool" || name == "ToolItem")
174 // Obsoleted on 2012-01-07
177 std::getline(is, all, '\n');
178 // First attempt to read inside ""
181 // If didn't skip to end, we have ""s
183 name = fnd.next("\"");
184 } else { // No luck, just read a word then
186 name = fnd.next(" ");
192 name = itemdef->getAlias(name);
193 wear = stoi(trim(fnd.next("")));
197 do // This loop is just to allow "break;"
201 // Apply item aliases
203 name = itemdef->getAlias(name);
206 std::string count_str;
207 std::getline(is, count_str, ' ');
208 if (count_str.empty()) {
213 count = stoi(count_str);
216 std::string wear_str;
217 std::getline(is, wear_str, ' ');
221 wear = stoi(wear_str);
224 metadata.deSerialize(is);
226 // In case fields are added after metadata, skip space here:
227 //std::getline(is, tmp, ' ');
229 // throw SerializationError("Unexpected text after metadata");
234 if (name.empty() || count == 0)
236 else if (itemdef && itemdef->get(name).type == ITEM_TOOL)
240 void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
242 std::istringstream is(str, std::ios::binary);
243 deSerialize(is, itemdef);
246 std::string ItemStack::getItemString() const
248 std::ostringstream os(std::ios::binary);
254 ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
256 // If the item is empty or the position invalid, bail out
259 // nothing can be added trivially
261 // If this is an empty item, it's an easy job.
264 const u16 stackMax = newitem.getStackMax(itemdef);
268 // If the item fits fully, delete it
269 if (count <= stackMax) {
271 } else { // Else the item does not fit fully. Return the rest.
273 newitem.remove(count);
276 // If item name or metadata differs, bail out
277 else if (name != newitem.name
278 || metadata != newitem.metadata)
282 // If the item fits fully, add counter and delete it
283 else if(newitem.count <= freeSpace(itemdef))
288 // Else the item does not fit fully. Add all that fits and return
292 u16 freespace = freeSpace(itemdef);
294 newitem.remove(freespace);
300 bool ItemStack::itemFits(ItemStack newitem,
302 IItemDefManager *itemdef) const
305 // If the item is empty or the position invalid, bail out
308 // nothing can be added trivially
310 // If this is an empty item, it's an easy job.
313 const u16 stackMax = newitem.getStackMax(itemdef);
315 // If the item fits fully, delete it
316 if (newitem.count <= stackMax) {
318 } else { // Else the item does not fit fully. Return the rest.
319 newitem.remove(stackMax);
322 // If item name or metadata differs, bail out
323 else if (name != newitem.name
324 || metadata != newitem.metadata)
328 // If the item fits fully, delete it
329 else if(newitem.count <= freeSpace(itemdef))
333 // Else the item does not fit fully. Return the rest.
336 u16 freespace = freeSpace(itemdef);
337 newitem.remove(freespace);
342 return newitem.empty();
345 ItemStack ItemStack::takeItem(u32 takecount)
347 if(takecount == 0 || count == 0)
350 ItemStack result = *this;
351 if(takecount >= count)
360 result.count = takecount;
365 ItemStack ItemStack::peekItem(u32 peekcount) const
367 if(peekcount == 0 || count == 0)
370 ItemStack result = *this;
371 if(peekcount < count)
372 result.count = peekcount;
380 InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
388 void InventoryList::clearItems()
392 for (u32 i=0; i < m_size; i++) {
393 m_items.emplace_back();
399 void InventoryList::setSize(u32 newsize)
401 if(newsize != m_items.size())
402 m_items.resize(newsize);
406 void InventoryList::setWidth(u32 newwidth)
411 void InventoryList::setName(const std::string &name)
416 void InventoryList::serialize(std::ostream &os) const
418 //os.imbue(std::locale("C"));
420 os<<"Width "<<m_width<<"\n";
422 for (const auto &item : m_items) {
432 os<<"EndInventoryList\n";
435 void InventoryList::deSerialize(std::istream &is)
437 //is.imbue(std::locale("C"));
446 std::getline(is, line, '\n');
448 std::istringstream iss(line);
449 //iss.imbue(std::locale("C"));
452 std::getline(iss, name, ' ');
454 if (name == "EndInventoryList") {
458 // This is a temporary backwards compatibility fix
463 if (name == "Width") {
466 throw SerializationError("incorrect width property");
468 else if(name == "Item")
470 if(item_i > getSize() - 1)
471 throw SerializationError("too many items");
473 item.deSerialize(iss, m_itemdef);
474 m_items[item_i++] = item;
476 else if(name == "Empty")
478 if(item_i > getSize() - 1)
479 throw SerializationError("too many items");
480 m_items[item_i++].clear();
485 InventoryList::InventoryList(const InventoryList &other)
490 InventoryList & InventoryList::operator = (const InventoryList &other)
492 m_items = other.m_items;
493 m_size = other.m_size;
494 m_width = other.m_width;
495 m_name = other.m_name;
496 m_itemdef = other.m_itemdef;
502 bool InventoryList::operator == (const InventoryList &other) const
504 if(m_size != other.m_size)
506 if(m_width != other.m_width)
508 if(m_name != other.m_name)
510 for(u32 i=0; i<m_items.size(); i++)
512 ItemStack s1 = m_items[i];
513 ItemStack s2 = other.m_items[i];
514 if(s1.name != s2.name || s1.wear!= s2.wear || s1.count != s2.count ||
515 s1.metadata != s2.metadata)
522 const std::string &InventoryList::getName() const
527 u32 InventoryList::getSize() const
529 return m_items.size();
532 u32 InventoryList::getWidth() const
537 u32 InventoryList::getUsedSlots() const
540 for (const auto &m_item : m_items) {
547 u32 InventoryList::getFreeSlots() const
549 return getSize() - getUsedSlots();
552 const ItemStack& InventoryList::getItem(u32 i) const
554 assert(i < m_size); // Pre-condition
558 ItemStack& InventoryList::getItem(u32 i)
560 assert(i < m_size); // Pre-condition
564 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
566 if(i >= m_items.size())
569 ItemStack olditem = m_items[i];
570 m_items[i] = newitem;
575 void InventoryList::deleteItem(u32 i)
577 assert(i < m_items.size()); // Pre-condition
581 ItemStack InventoryList::addItem(const ItemStack &newitem_)
583 ItemStack newitem = newitem_;
589 First try to find if it could be added to some existing items
591 for(u32 i=0; i<m_items.size(); i++)
593 // Ignore empty slots
594 if(m_items[i].empty())
597 newitem = addItem(i, newitem);
599 return newitem; // All was eaten
603 Then try to add it to empty slots
605 for(u32 i=0; i<m_items.size(); i++)
607 // Ignore unempty slots
608 if(!m_items[i].empty())
611 newitem = addItem(i, newitem);
613 return newitem; // All was eaten
620 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
622 if(i >= m_items.size())
625 ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
626 //if(leftover != newitem)
631 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
632 ItemStack *restitem) const
634 if(i >= m_items.size())
641 return m_items[i].itemFits(newitem, restitem, m_itemdef);
644 bool InventoryList::roomForItem(const ItemStack &item_) const
646 ItemStack item = item_;
648 for(u32 i=0; i<m_items.size(); i++)
650 if(itemFits(i, item, &leftover))
657 bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
659 u32 count = item.count;
663 for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
666 if (i->name == item.name && (!match_meta || (i->metadata == item.metadata))) {
667 if (i->count >= count)
676 ItemStack InventoryList::removeItem(const ItemStack &item)
679 for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
680 if (i->name == item.name) {
681 u32 still_to_remove = item.count - removed.count;
682 removed.addItem(i->takeItem(still_to_remove), m_itemdef);
683 if (removed.count == item.count)
690 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
692 if(i >= m_items.size())
695 ItemStack taken = m_items[i].takeItem(takecount);
701 void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
703 // Take item from source list
706 item1 = changeItem(i, ItemStack());
708 item1 = takeItem(i, count);
713 // Try to add the item to destination list
714 u32 dest_size = dest->getSize();
715 // First try all the non-empty slots
716 for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
717 if (!m_items[dest_i].empty()) {
718 item1 = dest->addItem(dest_i, item1);
719 if (item1.empty()) return;
723 // Then try all the empty ones
724 for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
725 if (m_items[dest_i].empty()) {
726 item1 = dest->addItem(dest_i, item1);
727 if (item1.empty()) return;
731 // If we reach this, the item was not fully added
732 // Add the remaining part back to the source item
736 u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
737 u32 count, bool swap_if_needed, bool *did_swap)
739 if(this == dest && i == dest_i)
742 // Take item from source list
745 item1 = changeItem(i, ItemStack());
747 item1 = takeItem(i, count);
752 // Try to add the item to destination list
753 u32 oldcount = item1.count;
754 item1 = dest->addItem(dest_i, item1);
756 // If something is returned, the item was not fully added
759 // If olditem is returned, nothing was added.
760 bool nothing_added = (item1.count == oldcount);
762 // If something else is returned, part of the item was left unadded.
763 // Add the other part back to the source item
766 // If olditem is returned, nothing was added.
768 if (nothing_added && swap_if_needed) {
769 // Tell that we swapped
770 if (did_swap != NULL) {
773 // Take item from source list
774 item1 = changeItem(i, ItemStack());
775 // Adding was not possible, swap the items.
776 ItemStack item2 = dest->changeItem(dest_i, item1);
777 // Put item from destination list to the source list
778 changeItem(i, item2);
781 return (oldcount - item1.count);
788 Inventory::~Inventory()
793 void Inventory::clear()
796 for (auto &m_list : m_lists) {
802 void Inventory::clearContents()
805 for (InventoryList *list : m_lists) {
806 for (u32 j=0; j<list->getSize(); j++) {
812 Inventory::Inventory(IItemDefManager *itemdef)
818 Inventory::Inventory(const Inventory &other)
824 Inventory & Inventory::operator = (const Inventory &other)
826 // Gracefully handle self assignment
831 m_itemdef = other.m_itemdef;
832 for (InventoryList *list : other.m_lists) {
833 m_lists.push_back(new InventoryList(*list));
839 bool Inventory::operator == (const Inventory &other) const
841 if(m_lists.size() != other.m_lists.size())
844 for(u32 i=0; i<m_lists.size(); i++)
846 if(*m_lists[i] != *other.m_lists[i])
852 void Inventory::serialize(std::ostream &os) const
854 for (InventoryList *list : m_lists) {
855 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
859 os<<"EndInventory\n";
862 void Inventory::deSerialize(std::istream &is)
869 std::getline(is, line, '\n');
871 std::istringstream iss(line);
874 std::getline(iss, name, ' ');
876 if (name == "EndInventory") {
880 // This is a temporary backwards compatibility fix
885 if (name == "List") {
886 std::string listname;
889 std::getline(iss, listname, ' ');
892 InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
893 list->deSerialize(is);
895 m_lists.push_back(list);
899 throw SerializationError("invalid inventory specifier: " + name);
904 InventoryList * Inventory::addList(const std::string &name, u32 size)
907 s32 i = getListIndex(name);
910 if(m_lists[i]->getSize() != size)
913 m_lists[i] = new InventoryList(name, size, m_itemdef);
919 //don't create list with invalid name
920 if (name.find(' ') != std::string::npos) return NULL;
922 InventoryList *list = new InventoryList(name, size, m_itemdef);
923 m_lists.push_back(list);
927 InventoryList * Inventory::getList(const std::string &name)
929 s32 i = getListIndex(name);
935 std::vector<const InventoryList*> Inventory::getLists()
937 std::vector<const InventoryList*> lists;
938 for (auto list : m_lists) {
939 lists.push_back(list);
944 bool Inventory::deleteList(const std::string &name)
946 s32 i = getListIndex(name);
951 m_lists.erase(m_lists.begin() + i);
955 const InventoryList * Inventory::getList(const std::string &name) const
957 s32 i = getListIndex(name);
963 const s32 Inventory::getListIndex(const std::string &name) const
965 for(u32 i=0; i<m_lists.size(); i++)
967 if(m_lists[i]->getName() == name)