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(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
40 if(trans_table_19[i][1] == c_from)
42 return trans_table_19[i][0];
48 // If the string contains spaces, quotes or control characters, encodes as JSON.
49 // Else returns the string unmodified.
50 static std::string serializeJsonStringIfNeeded(const std::string &s)
52 for(size_t i = 0; i < s.size(); ++i)
54 if(s[i] <= 0x1f || s[i] >= 0x7f || s[i] == ' ' || s[i] == '\"')
55 return serializeJsonString(s);
60 // Parses a string serialized by serializeJsonStringIfNeeded.
61 static std::string deSerializeJsonStringIfNeeded(std::istream &is)
63 std::ostringstream tmp_os;
64 bool expect_initial_quote = true;
66 bool was_backslash = false;
72 if(expect_initial_quote && c == '"')
81 was_backslash = false;
85 break; // Found end of string
100 expect_initial_quote = false;
104 std::istringstream tmp_is(tmp_os.str(), std::ios::binary);
105 return deSerializeJsonString(tmp_is);
112 ItemStack::ItemStack(std::string name_, u16 count_,
113 u16 wear_, std::string metadata_,
114 IItemDefManager *itemdef)
116 name = itemdef->getAlias(name_);
119 metadata = metadata_;
121 if(name.empty() || count == 0)
123 else if(itemdef->get(name).type == ITEM_TOOL)
127 void ItemStack::serialize(std::ostream &os) const
129 DSTACK(FUNCTION_NAME);
134 // Check how many parts of the itemstring are needed
143 os<<serializeJsonStringIfNeeded(name);
149 os<<" "<<serializeJsonStringIfNeeded(metadata);
152 void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
154 DSTACK(FUNCTION_NAME);
159 name = deSerializeJsonStringIfNeeded(is);
163 std::getline(is, tmp, ' ');
165 throw SerializationError("Unexpected text after item name");
167 if(name == "MaterialItem")
169 // Obsoleted on 2011-07-30
175 // Convert old materials
177 material = content_translate_from_19_to_internal(material);
179 throw SerializationError("Too large material number");
180 // Convert old id to name
181 NameIdMapping legacy_nimap;
182 content_mapnode_get_name_id_mapping(&legacy_nimap);
183 legacy_nimap.getName(material, name);
185 name = "unknown_block";
187 name = itemdef->getAlias(name);
188 count = materialcount;
190 else if(name == "MaterialItem2")
192 // Obsoleted on 2011-11-16
199 throw SerializationError("Too large material number");
200 // Convert old id to name
201 NameIdMapping legacy_nimap;
202 content_mapnode_get_name_id_mapping(&legacy_nimap);
203 legacy_nimap.getName(material, name);
205 name = "unknown_block";
207 name = itemdef->getAlias(name);
208 count = materialcount;
210 else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
211 || name == "craft" || name == "CraftItem")
213 // Obsoleted on 2012-01-07
216 std::getline(is, all, '\n');
217 // First attempt to read inside ""
220 // If didn't skip to end, we have ""s
222 name = fnd.next("\"");
223 } else { // No luck, just read a word then
225 name = fnd.next(" ");
229 name = itemdef->getAlias(name);
230 count = stoi(trim(fnd.next("")));
234 else if(name == "MBOItem")
236 // Obsoleted on 2011-10-14
237 throw SerializationError("MBOItem not supported anymore");
239 else if(name == "tool" || name == "ToolItem")
241 // Obsoleted on 2012-01-07
244 std::getline(is, all, '\n');
245 // First attempt to read inside ""
248 // If didn't skip to end, we have ""s
250 name = fnd.next("\"");
251 } else { // No luck, just read a word then
253 name = fnd.next(" ");
259 name = itemdef->getAlias(name);
260 wear = stoi(trim(fnd.next("")));
264 do // This loop is just to allow "break;"
268 // Apply item aliases
270 name = itemdef->getAlias(name);
273 std::string count_str;
274 std::getline(is, count_str, ' ');
275 if(count_str.empty())
281 count = stoi(count_str);
284 std::string wear_str;
285 std::getline(is, wear_str, ' ');
289 wear = stoi(wear_str);
292 metadata = deSerializeJsonStringIfNeeded(is);
294 // In case fields are added after metadata, skip space here:
295 //std::getline(is, tmp, ' ');
297 // throw SerializationError("Unexpected text after metadata");
302 if (name.empty() || count == 0)
304 else if (itemdef && itemdef->get(name).type == ITEM_TOOL)
308 void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
310 std::istringstream is(str, std::ios::binary);
311 deSerialize(is, itemdef);
314 std::string ItemStack::getItemString() const
316 std::ostringstream os(std::ios::binary);
322 ItemStack ItemStack::addItem(const ItemStack &newitem_,
323 IItemDefManager *itemdef)
325 ItemStack newitem = newitem_;
327 // If the item is empty or the position invalid, bail out
330 // nothing can be added trivially
332 // If this is an empty item, it's an easy job.
338 // If item name or metadata differs, bail out
339 else if (name != newitem.name
340 || metadata != newitem.metadata)
344 // If the item fits fully, add counter and delete it
345 else if(newitem.count <= freeSpace(itemdef))
350 // Else the item does not fit fully. Add all that fits and return
354 u16 freespace = freeSpace(itemdef);
356 newitem.remove(freespace);
362 bool ItemStack::itemFits(const ItemStack &newitem_,
364 IItemDefManager *itemdef) const
366 ItemStack newitem = newitem_;
368 // If the item is empty or the position invalid, bail out
371 // nothing can be added trivially
373 // If this is an empty item, it's an easy job.
378 // If item name or metadata differs, bail out
379 else if (name != newitem.name
380 || metadata != newitem.metadata)
384 // If the item fits fully, delete it
385 else if(newitem.count <= freeSpace(itemdef))
389 // Else the item does not fit fully. Return the rest.
393 u16 freespace = freeSpace(itemdef);
394 newitem.remove(freespace);
399 return newitem.empty();
402 ItemStack ItemStack::takeItem(u32 takecount)
404 if(takecount == 0 || count == 0)
407 ItemStack result = *this;
408 if(takecount >= count)
417 result.count = takecount;
422 ItemStack ItemStack::peekItem(u32 peekcount) const
424 if(peekcount == 0 || count == 0)
427 ItemStack result = *this;
428 if(peekcount < count)
429 result.count = peekcount;
437 InventoryList::InventoryList(std::string name, u32 size, IItemDefManager *itemdef)
447 InventoryList::~InventoryList()
451 void InventoryList::clearItems()
455 for(u32 i=0; i<m_size; i++)
457 m_items.push_back(ItemStack());
463 void InventoryList::setSize(u32 newsize)
465 if(newsize != m_items.size())
466 m_items.resize(newsize);
470 void InventoryList::setWidth(u32 newwidth)
475 void InventoryList::setName(const std::string &name)
480 void InventoryList::serialize(std::ostream &os) const
482 //os.imbue(std::locale("C"));
484 os<<"Width "<<m_width<<"\n";
486 for(u32 i=0; i<m_items.size(); i++)
488 const ItemStack &item = m_items[i];
501 os<<"EndInventoryList\n";
504 void InventoryList::deSerialize(std::istream &is)
506 //is.imbue(std::locale("C"));
515 std::getline(is, line, '\n');
517 std::istringstream iss(line);
518 //iss.imbue(std::locale("C"));
521 std::getline(iss, name, ' ');
523 if(name == "EndInventoryList")
527 // This is a temporary backwards compatibility fix
528 else if(name == "end")
532 else if(name == "Width")
536 throw SerializationError("incorrect width property");
538 else if(name == "Item")
540 if(item_i > getSize() - 1)
541 throw SerializationError("too many items");
543 item.deSerialize(iss, m_itemdef);
544 m_items[item_i++] = item;
546 else if(name == "Empty")
548 if(item_i > getSize() - 1)
549 throw SerializationError("too many items");
550 m_items[item_i++].clear();
555 InventoryList::InventoryList(const InventoryList &other)
560 InventoryList & InventoryList::operator = (const InventoryList &other)
562 m_items = other.m_items;
563 m_size = other.m_size;
564 m_width = other.m_width;
565 m_name = other.m_name;
566 m_itemdef = other.m_itemdef;
572 bool InventoryList::operator == (const InventoryList &other) const
574 if(m_size != other.m_size)
576 if(m_width != other.m_width)
578 if(m_name != other.m_name)
580 for(u32 i=0; i<m_items.size(); i++)
582 ItemStack s1 = m_items[i];
583 ItemStack s2 = other.m_items[i];
584 if(s1.name != s2.name || s1.wear!= s2.wear || s1.count != s2.count ||
585 s1.metadata != s2.metadata)
592 const std::string &InventoryList::getName() const
597 u32 InventoryList::getSize() const
599 return m_items.size();
602 u32 InventoryList::getWidth() const
607 u32 InventoryList::getUsedSlots() const
610 for(u32 i=0; i<m_items.size(); i++)
612 if(!m_items[i].empty())
618 u32 InventoryList::getFreeSlots() const
620 return getSize() - getUsedSlots();
623 const ItemStack& InventoryList::getItem(u32 i) const
625 assert(i < m_size); // Pre-condition
629 ItemStack& InventoryList::getItem(u32 i)
631 assert(i < m_size); // Pre-condition
635 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
637 if(i >= m_items.size())
640 ItemStack olditem = m_items[i];
641 m_items[i] = newitem;
646 void InventoryList::deleteItem(u32 i)
648 assert(i < m_items.size()); // Pre-condition
652 ItemStack InventoryList::addItem(const ItemStack &newitem_)
654 ItemStack newitem = newitem_;
660 First try to find if it could be added to some existing items
662 for(u32 i=0; i<m_items.size(); i++)
664 // Ignore empty slots
665 if(m_items[i].empty())
668 newitem = addItem(i, newitem);
670 return newitem; // All was eaten
674 Then try to add it to empty slots
676 for(u32 i=0; i<m_items.size(); i++)
678 // Ignore unempty slots
679 if(!m_items[i].empty())
682 newitem = addItem(i, newitem);
684 return newitem; // All was eaten
691 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
693 if(i >= m_items.size())
696 ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
697 //if(leftover != newitem)
702 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
703 ItemStack *restitem) const
705 if(i >= m_items.size())
712 return m_items[i].itemFits(newitem, restitem, m_itemdef);
715 bool InventoryList::roomForItem(const ItemStack &item_) const
717 ItemStack item = item_;
719 for(u32 i=0; i<m_items.size(); i++)
721 if(itemFits(i, item, &leftover))
728 bool InventoryList::containsItem(const ItemStack &item) const
730 u32 count = item.count;
733 for(std::vector<ItemStack>::const_reverse_iterator
734 i = m_items.rbegin();
735 i != m_items.rend(); ++i)
739 if(i->name == item.name)
741 if(i->count >= count)
750 ItemStack InventoryList::removeItem(const ItemStack &item)
753 for(std::vector<ItemStack>::reverse_iterator
754 i = m_items.rbegin();
755 i != m_items.rend(); ++i)
757 if(i->name == item.name)
759 u32 still_to_remove = item.count - removed.count;
760 removed.addItem(i->takeItem(still_to_remove), m_itemdef);
761 if(removed.count == item.count)
768 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
770 if(i >= m_items.size())
773 ItemStack taken = m_items[i].takeItem(takecount);
779 ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const
781 if(i >= m_items.size())
784 return m_items[i].peekItem(peekcount);
787 void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
789 // Take item from source list
792 item1 = changeItem(i, ItemStack());
794 item1 = takeItem(i, count);
799 // Try to add the item to destination list
800 u32 dest_size = dest->getSize();
801 // First try all the non-empty slots
802 for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
803 if (!m_items[dest_i].empty()) {
804 item1 = dest->addItem(dest_i, item1);
805 if (item1.empty()) return;
809 // Then try all the empty ones
810 for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
811 if (m_items[dest_i].empty()) {
812 item1 = dest->addItem(dest_i, item1);
813 if (item1.empty()) return;
817 // If we reach this, the item was not fully added
818 // Add the remaining part back to the source item
822 u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
823 u32 count, bool swap_if_needed, bool *did_swap)
825 if(this == dest && i == dest_i)
828 // Take item from source list
831 item1 = changeItem(i, ItemStack());
833 item1 = takeItem(i, count);
838 // Try to add the item to destination list
839 u32 oldcount = item1.count;
840 item1 = dest->addItem(dest_i, item1);
842 // If something is returned, the item was not fully added
845 // If olditem is returned, nothing was added.
846 bool nothing_added = (item1.count == oldcount);
848 // If something else is returned, part of the item was left unadded.
849 // Add the other part back to the source item
852 // If olditem is returned, nothing was added.
854 if (nothing_added && swap_if_needed) {
855 // Tell that we swapped
856 if (did_swap != NULL) {
859 // Take item from source list
860 item1 = changeItem(i, ItemStack());
861 // Adding was not possible, swap the items.
862 ItemStack item2 = dest->changeItem(dest_i, item1);
863 // Put item from destination list to the source list
864 changeItem(i, item2);
867 return (oldcount - item1.count);
874 Inventory::~Inventory()
879 void Inventory::clear()
882 for(u32 i=0; i<m_lists.size(); i++)
889 void Inventory::clearContents()
892 for(u32 i=0; i<m_lists.size(); i++)
894 InventoryList *list = m_lists[i];
895 for(u32 j=0; j<list->getSize(); j++)
902 Inventory::Inventory(IItemDefManager *itemdef)
908 Inventory::Inventory(const Inventory &other)
914 Inventory & Inventory::operator = (const Inventory &other)
916 // Gracefully handle self assignment
921 m_itemdef = other.m_itemdef;
922 for(u32 i=0; i<other.m_lists.size(); i++)
924 m_lists.push_back(new InventoryList(*other.m_lists[i]));
930 bool Inventory::operator == (const Inventory &other) const
932 if(m_lists.size() != other.m_lists.size())
935 for(u32 i=0; i<m_lists.size(); i++)
937 if(*m_lists[i] != *other.m_lists[i])
943 void Inventory::serialize(std::ostream &os) const
945 for(u32 i=0; i<m_lists.size(); i++)
947 InventoryList *list = m_lists[i];
948 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
952 os<<"EndInventory\n";
955 void Inventory::deSerialize(std::istream &is)
962 std::getline(is, line, '\n');
964 std::istringstream iss(line);
967 std::getline(iss, name, ' ');
969 if(name == "EndInventory")
973 // This is a temporary backwards compatibility fix
974 else if(name == "end")
978 else if(name == "List")
980 std::string listname;
983 std::getline(iss, listname, ' ');
986 InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
987 list->deSerialize(is);
989 m_lists.push_back(list);
993 throw SerializationError("invalid inventory specifier: " + name);
998 InventoryList * Inventory::addList(const std::string &name, u32 size)
1001 s32 i = getListIndex(name);
1004 if(m_lists[i]->getSize() != size)
1007 m_lists[i] = new InventoryList(name, size, m_itemdef);
1013 //don't create list with invalid name
1014 if (name.find(" ") != std::string::npos) return NULL;
1016 InventoryList *list = new InventoryList(name, size, m_itemdef);
1017 m_lists.push_back(list);
1022 InventoryList * Inventory::getList(const std::string &name)
1024 s32 i = getListIndex(name);
1030 std::vector<const InventoryList*> Inventory::getLists()
1032 std::vector<const InventoryList*> lists;
1033 for(u32 i=0; i<m_lists.size(); i++)
1035 InventoryList *list = m_lists[i];
1036 lists.push_back(list);
1041 bool Inventory::deleteList(const std::string &name)
1043 s32 i = getListIndex(name);
1048 m_lists.erase(m_lists.begin() + i);
1052 const InventoryList * Inventory::getList(const std::string &name) const
1054 s32 i = getListIndex(name);
1060 const s32 Inventory::getListIndex(const std::string &name) const
1062 for(u32 i=0; i<m_lists.size(); i++)
1064 if(m_lists[i]->getName() == name)