3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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"
28 #include "content_mapnode.h" // For loading legacy MaterialItems
29 #include "nameidmapping.h" // For loading legacy MaterialItems
35 static content_t content_translate_from_19_to_internal(content_t c_from)
37 for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
39 if(trans_table_19[i][1] == c_from)
41 return trans_table_19[i][0];
47 // If the string contains spaces, quotes or control characters, encodes as JSON.
48 // Else returns the string unmodified.
49 static std::string serializeJsonStringIfNeeded(const std::string &s)
51 for(size_t i = 0; i < s.size(); ++i)
53 if(s[i] <= 0x1f || s[i] >= 0x7f || s[i] == ' ' || s[i] == '\"')
54 return serializeJsonString(s);
59 // Parses a string serialized by serializeJsonStringIfNeeded.
60 static std::string deSerializeJsonStringIfNeeded(std::istream &is)
62 std::ostringstream tmp_os;
63 bool expect_initial_quote = true;
65 bool was_backslash = false;
71 if(expect_initial_quote && c == '"')
80 was_backslash = false;
84 break; // Found end of string
99 expect_initial_quote = false;
103 std::istringstream tmp_is(tmp_os.str(), std::ios::binary);
104 return deSerializeJsonString(tmp_is);
111 ItemStack::ItemStack(std::string name_, u16 count_,
112 u16 wear_, std::string metadata_,
113 IItemDefManager *itemdef)
115 name = itemdef->getAlias(name_);
118 metadata = metadata_;
120 if(name.empty() || count == 0)
122 else if(itemdef->get(name).type == ITEM_TOOL)
126 void ItemStack::serialize(std::ostream &os) const
128 DSTACK(__FUNCTION_NAME);
133 // Check how many parts of the itemstring are needed
142 os<<serializeJsonStringIfNeeded(name);
148 os<<" "<<serializeJsonStringIfNeeded(metadata);
151 void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
153 DSTACK(__FUNCTION_NAME);
158 name = deSerializeJsonStringIfNeeded(is);
162 std::getline(is, tmp, ' ');
164 throw SerializationError("Unexpected text after item name");
166 if(name == "MaterialItem")
168 // Obsoleted on 2011-07-30
174 // Convert old materials
176 material = content_translate_from_19_to_internal(material);
177 if(material > MAX_CONTENT)
178 throw SerializationError("Too large material number");
179 // Convert old id to name
180 NameIdMapping legacy_nimap;
181 content_mapnode_get_name_id_mapping(&legacy_nimap);
182 legacy_nimap.getName(material, name);
184 name = "unknown_block";
185 name = itemdef->getAlias(name);
186 count = materialcount;
188 else if(name == "MaterialItem2")
190 // Obsoleted on 2011-11-16
196 if(material > MAX_CONTENT)
197 throw SerializationError("Too large material number");
198 // Convert old id to name
199 NameIdMapping legacy_nimap;
200 content_mapnode_get_name_id_mapping(&legacy_nimap);
201 legacy_nimap.getName(material, name);
203 name = "unknown_block";
204 name = itemdef->getAlias(name);
205 count = materialcount;
207 else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
208 || name == "craft" || name == "CraftItem")
210 // Obsoleted on 2012-01-07
213 std::getline(is, all, '\n');
214 // First attempt to read inside ""
217 // If didn't skip to end, we have ""s
219 name = fnd.next("\"");
220 } else { // No luck, just read a word then
222 name = fnd.next(" ");
225 name = itemdef->getAlias(name);
226 count = stoi(trim(fnd.next("")));
230 else if(name == "MBOItem")
232 // Obsoleted on 2011-10-14
233 throw SerializationError("MBOItem not supported anymore");
235 else if(name == "tool" || name == "ToolItem")
237 // Obsoleted on 2012-01-07
240 std::getline(is, all, '\n');
241 // First attempt to read inside ""
244 // If didn't skip to end, we have ""s
246 name = fnd.next("\"");
247 } else { // No luck, just read a word then
249 name = fnd.next(" ");
254 name = itemdef->getAlias(name);
255 wear = stoi(trim(fnd.next("")));
259 do // This loop is just to allow "break;"
263 // Apply item aliases
264 name = itemdef->getAlias(name);
267 std::string count_str;
268 std::getline(is, count_str, ' ');
269 if(count_str.empty())
275 count = stoi(count_str);
278 std::string wear_str;
279 std::getline(is, wear_str, ' ');
283 wear = stoi(wear_str);
286 metadata = deSerializeJsonStringIfNeeded(is);
288 // In case fields are added after metadata, skip space here:
289 //std::getline(is, tmp, ' ');
291 // throw SerializationError("Unexpected text after metadata");
296 if(name.empty() || count == 0)
298 else if(itemdef->get(name).type == ITEM_TOOL)
302 void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
304 std::istringstream is(str, std::ios::binary);
305 deSerialize(is, itemdef);
308 std::string ItemStack::getItemString() const
311 std::ostringstream os(std::ios::binary);
316 ItemStack ItemStack::addItem(const ItemStack &newitem_,
317 IItemDefManager *itemdef)
319 ItemStack newitem = newitem_;
321 // If the item is empty or the position invalid, bail out
324 // nothing can be added trivially
326 // If this is an empty item, it's an easy job.
332 // If item name differs, bail out
333 else if(name != newitem.name)
337 // If the item fits fully, add counter and delete it
338 else if(newitem.count <= freeSpace(itemdef))
343 // Else the item does not fit fully. Add all that fits and return
347 u16 freespace = freeSpace(itemdef);
349 newitem.remove(freespace);
355 bool ItemStack::itemFits(const ItemStack &newitem_,
357 IItemDefManager *itemdef) const
359 ItemStack newitem = newitem_;
361 // If the item is empty or the position invalid, bail out
364 // nothing can be added trivially
366 // If this is an empty item, it's an easy job.
371 // If item name differs, bail out
372 else if(name != newitem.name)
376 // If the item fits fully, delete it
377 else if(newitem.count <= freeSpace(itemdef))
381 // Else the item does not fit fully. Return the rest.
385 u16 freespace = freeSpace(itemdef);
386 newitem.remove(freespace);
391 return newitem.empty();
394 ItemStack ItemStack::takeItem(u32 takecount)
396 if(takecount == 0 || count == 0)
399 ItemStack result = *this;
400 if(takecount >= count)
409 result.count = takecount;
414 ItemStack ItemStack::peekItem(u32 peekcount) const
416 if(peekcount == 0 || count == 0)
419 ItemStack result = *this;
420 if(peekcount < count)
421 result.count = peekcount;
429 InventoryList::InventoryList(std::string name, u32 size, IItemDefManager *itemdef)
438 InventoryList::~InventoryList()
442 void InventoryList::clearItems()
446 for(u32 i=0; i<m_size; i++)
448 m_items.push_back(ItemStack());
454 void InventoryList::setSize(u32 newsize)
456 if(newsize != m_items.size())
457 m_items.resize(newsize);
461 void InventoryList::setName(const std::string &name)
466 void InventoryList::serialize(std::ostream &os) const
468 //os.imbue(std::locale("C"));
470 for(u32 i=0; i<m_items.size(); i++)
472 const ItemStack &item = m_items[i];
485 os<<"EndInventoryList\n";
488 void InventoryList::deSerialize(std::istream &is)
490 //is.imbue(std::locale("C"));
498 std::getline(is, line, '\n');
500 std::istringstream iss(line);
501 //iss.imbue(std::locale("C"));
504 std::getline(iss, name, ' ');
506 if(name == "EndInventoryList")
510 // This is a temporary backwards compatibility fix
511 else if(name == "end")
515 else if(name == "Item")
517 if(item_i > getSize() - 1)
518 throw SerializationError("too many items");
520 item.deSerialize(iss, m_itemdef);
521 m_items[item_i++] = item;
523 else if(name == "Empty")
525 if(item_i > getSize() - 1)
526 throw SerializationError("too many items");
527 m_items[item_i++].clear();
531 throw SerializationError("Unknown inventory identifier");
536 InventoryList::InventoryList(const InventoryList &other)
541 InventoryList & InventoryList::operator = (const InventoryList &other)
543 m_items = other.m_items;
544 m_size = other.m_size;
545 m_name = other.m_name;
546 m_itemdef = other.m_itemdef;
552 const std::string &InventoryList::getName() const
557 u32 InventoryList::getSize() const
559 return m_items.size();
562 u32 InventoryList::getUsedSlots() const
565 for(u32 i=0; i<m_items.size(); i++)
567 if(!m_items[i].empty())
573 u32 InventoryList::getFreeSlots() const
575 return getSize() - getUsedSlots();
578 const ItemStack& InventoryList::getItem(u32 i) const
584 ItemStack& InventoryList::getItem(u32 i)
590 ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
592 if(i >= m_items.size())
595 ItemStack olditem = m_items[i];
596 m_items[i] = newitem;
601 void InventoryList::deleteItem(u32 i)
603 assert(i < m_items.size());
607 ItemStack InventoryList::addItem(const ItemStack &newitem_)
609 ItemStack newitem = newitem_;
615 First try to find if it could be added to some existing items
617 for(u32 i=0; i<m_items.size(); i++)
619 // Ignore empty slots
620 if(m_items[i].empty())
623 newitem = addItem(i, newitem);
625 return newitem; // All was eaten
629 Then try to add it to empty slots
631 for(u32 i=0; i<m_items.size(); i++)
633 // Ignore unempty slots
634 if(!m_items[i].empty())
637 newitem = addItem(i, newitem);
639 return newitem; // All was eaten
646 ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
648 if(i >= m_items.size())
651 ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
652 //if(leftover != newitem)
657 bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
658 ItemStack *restitem) const
660 if(i >= m_items.size())
667 return m_items[i].itemFits(newitem, restitem, m_itemdef);
670 bool InventoryList::roomForItem(const ItemStack &item_) const
672 ItemStack item = item_;
674 for(u32 i=0; i<m_items.size(); i++)
676 if(itemFits(i, item, &leftover))
683 bool InventoryList::containsItem(const ItemStack &item) const
685 u32 count = item.count;
688 for(std::vector<ItemStack>::const_reverse_iterator
689 i = m_items.rbegin();
690 i != m_items.rend(); i++)
694 if(i->name == item.name)
696 if(i->count >= count)
705 ItemStack InventoryList::removeItem(const ItemStack &item)
708 for(std::vector<ItemStack>::reverse_iterator
709 i = m_items.rbegin();
710 i != m_items.rend(); i++)
712 if(i->name == item.name)
714 u32 still_to_remove = item.count - removed.count;
715 removed.addItem(i->takeItem(still_to_remove), m_itemdef);
716 if(removed.count == item.count)
723 ItemStack InventoryList::takeItem(u32 i, u32 takecount)
725 if(i >= m_items.size())
728 ItemStack taken = m_items[i].takeItem(takecount);
734 ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const
736 if(i >= m_items.size())
739 return m_items[i].peekItem(peekcount);
742 void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count)
744 if(this == dest && i == dest_i)
747 // Take item from source list
750 item1 = changeItem(i, ItemStack());
752 item1 = takeItem(i, count);
757 // Try to add the item to destination list
758 u32 oldcount = item1.count;
759 item1 = dest->addItem(dest_i, item1);
761 // If something is returned, the item was not fully added
764 // If olditem is returned, nothing was added.
765 bool nothing_added = (item1.count == oldcount);
767 // If something else is returned, part of the item was left unadded.
768 // Add the other part back to the source item
771 // If olditem is returned, nothing was added.
775 // Take item from source list
776 item1 = changeItem(i, ItemStack());
777 // Adding was not possible, swap the items.
778 ItemStack item2 = dest->changeItem(dest_i, item1);
779 // Put item from destination list to the source list
780 changeItem(i, item2);
789 Inventory::~Inventory()
794 void Inventory::clear()
796 for(u32 i=0; i<m_lists.size(); i++)
803 void Inventory::clearContents()
805 for(u32 i=0; i<m_lists.size(); i++)
807 InventoryList *list = m_lists[i];
808 for(u32 j=0; j<list->getSize(); j++)
815 Inventory::Inventory(IItemDefManager *itemdef)
820 Inventory::Inventory(const Inventory &other)
825 Inventory & Inventory::operator = (const Inventory &other)
827 // Gracefully handle self assignment
831 m_itemdef = other.m_itemdef;
832 for(u32 i=0; i<other.m_lists.size(); i++)
834 m_lists.push_back(new InventoryList(*other.m_lists[i]));
840 void Inventory::serialize(std::ostream &os) const
842 for(u32 i=0; i<m_lists.size(); i++)
844 InventoryList *list = m_lists[i];
845 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
849 os<<"EndInventory\n";
852 void Inventory::deSerialize(std::istream &is)
859 std::getline(is, line, '\n');
861 std::istringstream iss(line);
864 std::getline(iss, name, ' ');
866 if(name == "EndInventory")
870 // This is a temporary backwards compatibility fix
871 else if(name == "end")
875 else if(name == "List")
877 std::string listname;
880 std::getline(iss, listname, ' ');
883 InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
884 list->deSerialize(is);
886 m_lists.push_back(list);
890 throw SerializationError("Unknown inventory identifier");
895 InventoryList * Inventory::addList(const std::string &name, u32 size)
897 s32 i = getListIndex(name);
900 if(m_lists[i]->getSize() != size)
903 m_lists[i] = new InventoryList(name, size, m_itemdef);
909 InventoryList *list = new InventoryList(name, size, m_itemdef);
910 m_lists.push_back(list);
915 InventoryList * Inventory::getList(const std::string &name)
917 s32 i = getListIndex(name);
923 std::vector<const InventoryList*> Inventory::getLists()
925 std::vector<const InventoryList*> lists;
926 for(u32 i=0; i<m_lists.size(); i++)
928 InventoryList *list = m_lists[i];
929 lists.push_back(list);
934 bool Inventory::deleteList(const std::string &name)
936 s32 i = getListIndex(name);
940 m_lists.erase(m_lists.begin() + i);
944 const InventoryList * Inventory::getList(const std::string &name) const
946 s32 i = getListIndex(name);
952 const s32 Inventory::getListIndex(const std::string &name) const
954 for(u32 i=0; i<m_lists.size(); i++)
956 if(m_lists[i]->getName() == name)