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 ItemStack::ItemStack(const std::string &name_, u16 count_,
49 u16 wear_, IItemDefManager *itemdef) :
50 name(itemdef->getAlias(name_)),
54 if (name.empty() || count == 0)
56 else if (itemdef->get(name).type == ITEM_TOOL)
60 void ItemStack::serialize(std::ostream &os) const
62 DSTACK(FUNCTION_NAME);
67 // Check how many parts of the itemstring are needed
73 if (!metadata.empty())
76 os<<serializeJsonStringIfNeeded(name);
83 metadata.serialize(os);
87 void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
89 DSTACK(FUNCTION_NAME);
94 name = deSerializeJsonStringIfNeeded(is);
98 std::getline(is, tmp, ' ');
100 throw SerializationError("Unexpected text after item name");
102 if(name == "MaterialItem")
104 // Obsoleted on 2011-07-30
110 // Convert old materials
112 material = content_translate_from_19_to_internal(material);
114 throw SerializationError("Too large material number");
115 // Convert old id to name
116 NameIdMapping legacy_nimap;
117 content_mapnode_get_name_id_mapping(&legacy_nimap);
118 legacy_nimap.getName(material, name);
120 name = "unknown_block";
122 name = itemdef->getAlias(name);
123 count = materialcount;
125 else if(name == "MaterialItem2")
127 // Obsoleted on 2011-11-16
134 throw SerializationError("Too large material number");
135 // Convert old id to name
136 NameIdMapping legacy_nimap;
137 content_mapnode_get_name_id_mapping(&legacy_nimap);
138 legacy_nimap.getName(material, name);
140 name = "unknown_block";
142 name = itemdef->getAlias(name);
143 count = materialcount;
145 else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
146 || name == "craft" || name == "CraftItem")
148 // Obsoleted on 2012-01-07
151 std::getline(is, all, '\n');
152 // First attempt to read inside ""
155 // If didn't skip to end, we have ""s
157 name = fnd.next("\"");
158 } else { // No luck, just read a word then
160 name = fnd.next(" ");
164 name = itemdef->getAlias(name);
165 count = stoi(trim(fnd.next("")));
169 else if(name == "MBOItem")
171 // Obsoleted on 2011-10-14
172 throw SerializationError("MBOItem not supported anymore");
174 else if(name == "tool" || name == "ToolItem")
176 // Obsoleted on 2012-01-07
179 std::getline(is, all, '\n');
180 // First attempt to read inside ""
183 // If didn't skip to end, we have ""s
185 name = fnd.next("\"");
186 } else { // No luck, just read a word then
188 name = fnd.next(" ");
194 name = itemdef->getAlias(name);
195 wear = stoi(trim(fnd.next("")));
199 do // This loop is just to allow "break;"
203 // Apply item aliases
205 name = itemdef->getAlias(name);
208 std::string count_str;
209 std::getline(is, count_str, ' ');
210 if(count_str.empty())
216 count = stoi(count_str);
219 std::string wear_str;
220 std::getline(is, wear_str, ' ');
224 wear = stoi(wear_str);
227 metadata.deSerialize(is);
229 // In case fields are added after metadata, skip space here:
230 //std::getline(is, tmp, ' ');
232 // throw SerializationError("Unexpected text after metadata");
237 if (name.empty() || count == 0)
239 else if (itemdef && itemdef->get(name).type == ITEM_TOOL)
243 void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
245 std::istringstream is(str, std::ios::binary);
246 deSerialize(is, itemdef);
249 std::string ItemStack::getItemString() const
251 std::ostringstream os(std::ios::binary);
257 ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
259 // If the item is empty or the position invalid, bail out
262 // nothing can be added trivially
264 // If this is an empty item, it's an easy job.
267 const u16 stackMax = newitem.getStackMax(itemdef);
271 // If the item fits fully, delete it
272 if (count <= stackMax) {
274 } else { // Else the item does not fit fully. Return the rest.
276 newitem.remove(count);
279 // If item name or metadata differs, bail out
280 else if (name != newitem.name
281 || metadata != newitem.metadata)
285 // If the item fits fully, add counter and delete it
286 else if(newitem.count <= freeSpace(itemdef))
291 // Else the item does not fit fully. Add all that fits and return
295 u16 freespace = freeSpace(itemdef);
297 newitem.remove(freespace);
303 bool ItemStack::itemFits(ItemStack newitem,
305 IItemDefManager *itemdef) const
308 // If the item is empty or the position invalid, bail out
311 // nothing can be added trivially
313 // If this is an empty item, it's an easy job.
316 const u16 stackMax = newitem.getStackMax(itemdef);
318 // If the item fits fully, delete it
319 if (newitem.count <= stackMax) {
321 } else { // Else the item does not fit fully. Return the rest.
322 newitem.remove(stackMax);
325 // If item name or metadata differs, bail out
326 else if (name != newitem.name
327 || metadata != newitem.metadata)
331 // If the item fits fully, delete it
332 else if(newitem.count <= freeSpace(itemdef))
336 // Else the item does not fit fully. Return the rest.
339 u16 freespace = freeSpace(itemdef);
340 newitem.remove(freespace);
345 return newitem.empty();
348 ItemStack ItemStack::takeItem(u32 takecount)
350 if(takecount == 0 || count == 0)
353 ItemStack result = *this;
354 if(takecount >= count)
363 result.count = takecount;
368 ItemStack ItemStack::peekItem(u32 peekcount) const
370 if(peekcount == 0 || count == 0)
373 ItemStack result = *this;
374 if(peekcount < count)
375 result.count = peekcount;
383 InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
391 InventoryList::~InventoryList()
395 void InventoryList::clearItems()
399 for(u32 i=0; i<m_size; i++)
401 m_items.push_back(ItemStack());
407 void InventoryList::setSize(u32 newsize)
409 if(newsize != m_items.size())
410 m_items.resize(newsize);
414 void InventoryList::setWidth(u32 newwidth)
419 void InventoryList::setName(const std::string &name)
424 void InventoryList::serialize(std::ostream &os) const
426 //os.imbue(std::locale("C"));
428 os<<"Width "<<m_width<<"\n";
430 for(u32 i=0; i<m_items.size(); i++)
432 const ItemStack &item = m_items[i];
445 os<<"EndInventoryList\n";
448 void InventoryList::deSerialize(std::istream &is)
450 //is.imbue(std::locale("C"));
459 std::getline(is, line, '\n');
461 std::istringstream iss(line);
462 //iss.imbue(std::locale("C"));
465 std::getline(iss, name, ' ');
467 if(name == "EndInventoryList")
471 // This is a temporary backwards compatibility fix
472 else if(name == "end")
476 else if(name == "Width")
480 throw SerializationError("incorrect width property");
482 else if(name == "Item")
484 if(item_i > getSize() - 1)
485 throw SerializationError("too many items");
487 item.deSerialize(iss, m_itemdef);
488 m_items[item_i++] = item;
490 else if(name == "Empty")
492 if(item_i > getSize() - 1)
493 throw SerializationError("too many items");
494 m_items[item_i++].clear();
499 InventoryList::InventoryList(const InventoryList &other)
504 InventoryList & InventoryList::operator = (const InventoryList &other)
506 m_items = other.m_items;
507 m_size = other.m_size;
508 m_width = other.m_width;
509 m_name = other.m_name;
510 m_itemdef = other.m_itemdef;
516 bool InventoryList::operator == (const InventoryList &other) const
518 if(m_size != other.m_size)
520 if(m_width != other.m_width)
522 if(m_name != other.m_name)
524 for(u32 i=0; i<m_items.size(); i++)
526 ItemStack s1 = m_items[i];
527 ItemStack s2 = other.m_items[i];
528 if(s1.name != s2.name || s1.wear!= s2.wear || s1.count != s2.count ||
529 s1.metadata != s2.metadata)
536 const std::string &InventoryList::getName() const
541 u32 InventoryList::getSize() const
543 return m_items.size();
546 u32 InventoryList::getWidth() const
551 u32 InventoryList::getUsedSlots() const
554 for(u32 i=0; i<m_items.size(); i++)
556 if(!m_items[i].empty())
562 u32 InventoryList::getFreeSlots() const
564 return getSize() - getUsedSlots();
567 const ItemStack& InventoryList::getItem(u32 i) const
569 assert(i < m_size); // Pre-condition
573 ItemStack& InventoryList::getItem(u32 i)
575 assert(i < m_size); // Pre-condition
579 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
581 if(i >= m_items.size())
584 ItemStack olditem = m_items[i];
585 m_items[i] = newitem;
590 void InventoryList::deleteItem(u32 i)
592 assert(i < m_items.size()); // Pre-condition
596 ItemStack InventoryList::addItem(const ItemStack &newitem_)
598 ItemStack newitem = newitem_;
604 First try to find if it could be added to some existing items
606 for(u32 i=0; i<m_items.size(); i++)
608 // Ignore empty slots
609 if(m_items[i].empty())
612 newitem = addItem(i, newitem);
614 return newitem; // All was eaten
618 Then try to add it to empty slots
620 for(u32 i=0; i<m_items.size(); i++)
622 // Ignore unempty slots
623 if(!m_items[i].empty())
626 newitem = addItem(i, newitem);
628 return newitem; // All was eaten
635 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
637 if(i >= m_items.size())
640 ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
641 //if(leftover != newitem)
646 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
647 ItemStack *restitem) const
649 if(i >= m_items.size())
656 return m_items[i].itemFits(newitem, restitem, m_itemdef);
659 bool InventoryList::roomForItem(const ItemStack &item_) const
661 ItemStack item = item_;
663 for(u32 i=0; i<m_items.size(); i++)
665 if(itemFits(i, item, &leftover))
672 bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
674 u32 count = item.count;
677 for(std::vector<ItemStack>::const_reverse_iterator
678 i = m_items.rbegin();
679 i != m_items.rend(); ++i)
683 if (i->name == item.name
684 && (!match_meta || (i->metadata == item.metadata))) {
685 if (i->count >= count)
694 ItemStack InventoryList::removeItem(const ItemStack &item)
697 for(std::vector<ItemStack>::reverse_iterator
698 i = m_items.rbegin();
699 i != m_items.rend(); ++i)
701 if(i->name == item.name)
703 u32 still_to_remove = item.count - removed.count;
704 removed.addItem(i->takeItem(still_to_remove), m_itemdef);
705 if(removed.count == item.count)
712 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
714 if(i >= m_items.size())
717 ItemStack taken = m_items[i].takeItem(takecount);
723 void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
725 // Take item from source list
728 item1 = changeItem(i, ItemStack());
730 item1 = takeItem(i, count);
735 // Try to add the item to destination list
736 u32 dest_size = dest->getSize();
737 // First try all the non-empty slots
738 for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
739 if (!m_items[dest_i].empty()) {
740 item1 = dest->addItem(dest_i, item1);
741 if (item1.empty()) return;
745 // Then try all the empty ones
746 for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
747 if (m_items[dest_i].empty()) {
748 item1 = dest->addItem(dest_i, item1);
749 if (item1.empty()) return;
753 // If we reach this, the item was not fully added
754 // Add the remaining part back to the source item
758 u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
759 u32 count, bool swap_if_needed, bool *did_swap)
761 if(this == dest && i == dest_i)
764 // Take item from source list
767 item1 = changeItem(i, ItemStack());
769 item1 = takeItem(i, count);
774 // Try to add the item to destination list
775 u32 oldcount = item1.count;
776 item1 = dest->addItem(dest_i, item1);
778 // If something is returned, the item was not fully added
781 // If olditem is returned, nothing was added.
782 bool nothing_added = (item1.count == oldcount);
784 // If something else is returned, part of the item was left unadded.
785 // Add the other part back to the source item
788 // If olditem is returned, nothing was added.
790 if (nothing_added && swap_if_needed) {
791 // Tell that we swapped
792 if (did_swap != NULL) {
795 // Take item from source list
796 item1 = changeItem(i, ItemStack());
797 // Adding was not possible, swap the items.
798 ItemStack item2 = dest->changeItem(dest_i, item1);
799 // Put item from destination list to the source list
800 changeItem(i, item2);
803 return (oldcount - item1.count);
810 Inventory::~Inventory()
815 void Inventory::clear()
818 for(u32 i=0; i<m_lists.size(); i++)
825 void Inventory::clearContents()
828 for(u32 i=0; i<m_lists.size(); i++)
830 InventoryList *list = m_lists[i];
831 for(u32 j=0; j<list->getSize(); j++)
838 Inventory::Inventory(IItemDefManager *itemdef)
844 Inventory::Inventory(const Inventory &other)
850 Inventory & Inventory::operator = (const Inventory &other)
852 // Gracefully handle self assignment
857 m_itemdef = other.m_itemdef;
858 for(u32 i=0; i<other.m_lists.size(); i++)
860 m_lists.push_back(new InventoryList(*other.m_lists[i]));
866 bool Inventory::operator == (const Inventory &other) const
868 if(m_lists.size() != other.m_lists.size())
871 for(u32 i=0; i<m_lists.size(); i++)
873 if(*m_lists[i] != *other.m_lists[i])
879 void Inventory::serialize(std::ostream &os) const
881 for(u32 i=0; i<m_lists.size(); i++)
883 InventoryList *list = m_lists[i];
884 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
888 os<<"EndInventory\n";
891 void Inventory::deSerialize(std::istream &is)
898 std::getline(is, line, '\n');
900 std::istringstream iss(line);
903 std::getline(iss, name, ' ');
905 if(name == "EndInventory")
909 // This is a temporary backwards compatibility fix
910 else if(name == "end")
914 else if(name == "List")
916 std::string listname;
919 std::getline(iss, listname, ' ');
922 InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
923 list->deSerialize(is);
925 m_lists.push_back(list);
929 throw SerializationError("invalid inventory specifier: " + name);
934 InventoryList * Inventory::addList(const std::string &name, u32 size)
937 s32 i = getListIndex(name);
940 if(m_lists[i]->getSize() != size)
943 m_lists[i] = new InventoryList(name, size, m_itemdef);
949 //don't create list with invalid name
950 if (name.find(" ") != std::string::npos) return NULL;
952 InventoryList *list = new InventoryList(name, size, m_itemdef);
953 m_lists.push_back(list);
958 InventoryList * Inventory::getList(const std::string &name)
960 s32 i = getListIndex(name);
966 std::vector<const InventoryList*> Inventory::getLists()
968 std::vector<const InventoryList*> lists;
969 for(u32 i=0; i<m_lists.size(); i++)
971 InventoryList *list = m_lists[i];
972 lists.push_back(list);
977 bool Inventory::deleteList(const std::string &name)
979 s32 i = getListIndex(name);
984 m_lists.erase(m_lists.begin() + i);
988 const InventoryList * Inventory::getList(const std::string &name) const
990 s32 i = getListIndex(name);
996 const s32 Inventory::getListIndex(const std::string &name) const
998 for(u32 i=0; i<m_lists.size(); i++)
1000 if(m_lists[i]->getName() == name)