3 Copyright (C) 2010 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.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
24 #include "inventory.h"
25 #include "serialization.h"
30 #include "serverobject.h"
31 #include "content_mapnode.h"
32 #include "content_inventory.h"
38 InventoryItem::InventoryItem(u16 count)
43 InventoryItem::~InventoryItem()
47 InventoryItem* InventoryItem::deSerialize(std::istream &is)
49 DSTACK(__FUNCTION_NAME);
51 //is.imbue(std::locale("C"));
54 std::getline(is, name, ' ');
56 if(name == "MaterialItem")
58 // u16 reads directly as a number (u8 doesn't)
64 throw SerializationError("Too large material number");
65 return new MaterialItem(material, count);
67 else if(name == "MBOItem")
69 std::string inventorystring;
70 std::getline(is, inventorystring, '|');
71 return new MapBlockObjectItem(inventorystring);
73 else if(name == "CraftItem")
76 std::getline(is, subname, ' ');
79 return new CraftItem(subname, count);
81 else if(name == "ToolItem")
84 std::getline(is, toolname, ' ');
87 return new ToolItem(toolname, wear);
91 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
92 throw SerializationError("Unknown InventoryItem name");
96 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
102 std::ostringstream os(std::ios_base::binary);
105 ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
113 bool MaterialItem::isCookable()
115 return item_material_is_cookable(m_content);
118 InventoryItem *MaterialItem::createCookResult()
120 return item_material_create_cook_result(m_content);
128 video::ITexture * CraftItem::getImage()
130 if(g_texturesource == NULL)
133 std::string name = item_craft_get_image_name(m_subname);
135 // Get such a texture
136 return g_texturesource->getTextureRaw(name);
140 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
143 ServerActiveObject *obj = item_craft_create_object(m_subname, env, id, pos);
147 return InventoryItem::createSAO(env, id, pos);
150 u16 CraftItem::getDropCount()
153 s16 dc = item_craft_get_drop_count(m_subname);
157 return InventoryItem::getDropCount();
160 bool CraftItem::isCookable()
162 return item_craft_is_cookable(m_subname);
165 InventoryItem *CraftItem::createCookResult()
167 return item_craft_create_cook_result(m_subname);
171 MapBlockObjectItem DEPRECATED
175 video::ITexture * MapBlockObjectItem::getImage()
177 if(m_inventorystring.substr(0,3) == "Rat")
178 return g_texturesource->getTextureRaw("rat.png");
180 if(m_inventorystring.substr(0,4) == "Sign")
181 return g_texturesource->getTextureRaw("sign.png");
186 std::string MapBlockObjectItem::getText()
188 if(m_inventorystring.substr(0,3) == "Rat")
191 if(m_inventorystring.substr(0,4) == "Sign")
197 MapBlockObject * MapBlockObjectItem::createObject
198 (v3f pos, f32 player_yaw, f32 player_pitch)
200 std::istringstream is(m_inventorystring);
202 std::getline(is, name, ' ');
208 else if(name == "Sign")
211 std::getline(is, text, '|');
212 SignObject *obj = new SignObject(NULL, -1, pos);
214 obj->setYaw(-player_yaw);
217 else if(name == "Rat")
219 RatObject *obj = new RatObject(NULL, -1, pos);
222 else if(name == "ItemObj")
225 Now we are an inventory item containing the serialization
226 string of an object that contains the serialization
227 string of an inventory item. Fuck this.
230 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
231 <<"because an item-object should never be inside "
232 <<"an object-item."<<std::endl;
245 InventoryList::InventoryList(std::string name, u32 size)
253 InventoryList::~InventoryList()
255 for(u32 i=0; i<m_items.size(); i++)
262 void InventoryList::clearItems()
264 for(u32 i=0; i<m_items.size(); i++)
272 for(u32 i=0; i<m_size; i++)
274 m_items.push_back(NULL);
280 void InventoryList::serialize(std::ostream &os)
282 //os.imbue(std::locale("C"));
284 for(u32 i=0; i<m_items.size(); i++)
286 InventoryItem *item = m_items[i];
299 os<<"EndInventoryList\n";
302 void InventoryList::deSerialize(std::istream &is)
304 //is.imbue(std::locale("C"));
312 std::getline(is, line, '\n');
314 std::istringstream iss(line);
315 //iss.imbue(std::locale("C"));
318 std::getline(iss, name, ' ');
320 if(name == "EndInventoryList")
324 // This is a temporary backwards compatibility fix
325 else if(name == "end")
329 else if(name == "Item")
331 if(item_i > getSize() - 1)
332 throw SerializationError("too many items");
333 InventoryItem *item = InventoryItem::deSerialize(iss);
334 m_items[item_i++] = item;
336 else if(name == "Empty")
338 if(item_i > getSize() - 1)
339 throw SerializationError("too many items");
340 m_items[item_i++] = NULL;
344 throw SerializationError("Unknown inventory identifier");
349 InventoryList::InventoryList(const InventoryList &other)
352 Do this so that the items get cloned. Otherwise the pointers
353 in the array will just get copied.
358 InventoryList & InventoryList::operator = (const InventoryList &other)
360 m_name = other.m_name;
361 m_size = other.m_size;
363 for(u32 i=0; i<other.m_items.size(); i++)
365 InventoryItem *item = other.m_items[i];
368 m_items[i] = item->clone();
376 std::string InventoryList::getName()
381 u32 InventoryList::getSize()
383 return m_items.size();
386 u32 InventoryList::getUsedSlots()
389 for(u32 i=0; i<m_items.size(); i++)
391 InventoryItem *item = m_items[i];
398 u32 InventoryList::getFreeSlots()
400 return getSize() - getUsedSlots();
403 InventoryItem * InventoryList::getItem(u32 i)
405 if(i > m_items.size() - 1)
410 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
412 assert(i < m_items.size());
414 InventoryItem *olditem = m_items[i];
415 m_items[i] = newitem;
420 void InventoryList::deleteItem(u32 i)
422 assert(i < m_items.size());
423 InventoryItem *item = changeItem(i, NULL);
428 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
434 First try to find if it could be added to some existing items
436 for(u32 i=0; i<m_items.size(); i++)
438 // Ignore empty slots
439 if(m_items[i] == NULL)
442 newitem = addItem(i, newitem);
444 return NULL; // All was eaten
448 Then try to add it to empty slots
450 for(u32 i=0; i<m_items.size(); i++)
452 // Ignore unempty slots
453 if(m_items[i] != NULL)
456 newitem = addItem(i, newitem);
458 return NULL; // All was eaten
465 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
472 // If it is an empty position, it's an easy job.
473 InventoryItem *to_item = getItem(i);
476 m_items[i] = newitem;
480 // If not addable, return the item
481 if(newitem->addableTo(to_item) == false)
484 // If the item fits fully in the slot, add counter and delete it
485 if(newitem->getCount() <= to_item->freeSpace())
487 to_item->add(newitem->getCount());
491 // Else the item does not fit fully. Add all that fits and return
495 u16 freespace = to_item->freeSpace();
496 to_item->add(freespace);
497 newitem->remove(freespace);
502 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
504 // If it is an empty position, it's an easy job.
505 InventoryItem *to_item = getItem(i);
511 // If not addable, return the item
512 if(newitem->addableTo(to_item) == false)
515 // If the item fits fully in the slot, add counter and delete it
516 if(newitem->getCount() <= to_item->freeSpace())
524 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
531 InventoryItem *item = getItem(i);
532 // If it is an empty position, return NULL
536 if(count >= item->getCount())
538 // Get the item by swapping NULL to its place
539 return changeItem(i, NULL);
543 InventoryItem *item2 = item->clone();
545 item2->setCount(count);
552 void InventoryList::decrementMaterials(u16 count)
554 for(u32 i=0; i<m_items.size(); i++)
556 InventoryItem *item = takeItem(i, count);
562 void InventoryList::print(std::ostream &o)
564 o<<"InventoryList:"<<std::endl;
565 for(u32 i=0; i<m_items.size(); i++)
567 InventoryItem *item = m_items[i];
581 Inventory::~Inventory()
586 void Inventory::clear()
588 for(u32 i=0; i<m_lists.size(); i++)
595 Inventory::Inventory()
599 Inventory::Inventory(const Inventory &other)
604 Inventory & Inventory::operator = (const Inventory &other)
607 for(u32 i=0; i<other.m_lists.size(); i++)
609 m_lists.push_back(new InventoryList(*other.m_lists[i]));
614 void Inventory::serialize(std::ostream &os)
616 for(u32 i=0; i<m_lists.size(); i++)
618 InventoryList *list = m_lists[i];
619 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
623 os<<"EndInventory\n";
626 void Inventory::deSerialize(std::istream &is)
633 std::getline(is, line, '\n');
635 std::istringstream iss(line);
638 std::getline(iss, name, ' ');
640 if(name == "EndInventory")
644 // This is a temporary backwards compatibility fix
645 else if(name == "end")
649 else if(name == "List")
651 std::string listname;
654 std::getline(iss, listname, ' ');
657 InventoryList *list = new InventoryList(listname, listsize);
658 list->deSerialize(is);
660 m_lists.push_back(list);
664 throw SerializationError("Unknown inventory identifier");
669 InventoryList * Inventory::addList(const std::string &name, u32 size)
671 s32 i = getListIndex(name);
674 if(m_lists[i]->getSize() != size)
677 m_lists[i] = new InventoryList(name, size);
683 m_lists.push_back(new InventoryList(name, size));
684 return m_lists.getLast();
688 InventoryList * Inventory::getList(const std::string &name)
690 s32 i = getListIndex(name);
696 s32 Inventory::getListIndex(const std::string &name)
698 for(u32 i=0; i<m_lists.size(); i++)
700 if(m_lists[i]->getName() == name)
710 InventoryAction * InventoryAction::deSerialize(std::istream &is)
713 std::getline(is, type, ' ');
715 InventoryAction *a = NULL;
719 a = new IMoveAction(is);
725 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
729 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
730 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
731 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
733 Inventory *inv_from = mgr->getInventory(c, from_inv);
734 Inventory *inv_to = mgr->getInventory(c, to_inv);
736 if(!inv_from || !inv_to)
738 dstream<<__FUNCTION_NAME<<": Operation not allowed "
739 <<"(inventories not found)"<<std::endl;
743 InventoryList *list_from = inv_from->getList(from_list);
744 InventoryList *list_to = inv_to->getList(to_list);
746 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
749 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
752 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
756 If a list doesn't exist or the source item doesn't exist
758 if(!list_from || !list_to)
760 dstream<<__FUNCTION_NAME<<": Operation not allowed "
761 <<"(a list doesn't exist)"
765 if(list_from->getItem(from_i) == NULL)
767 dstream<<__FUNCTION_NAME<<": Operation not allowed "
768 <<"(the source item doesn't exist)"
773 If the source and the destination slots are the same
775 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
777 dstream<<__FUNCTION_NAME<<": Operation not allowed "
778 <<"(source and the destination slots are the same)"<<std::endl;
782 // Take item from source list
783 InventoryItem *item1 = NULL;
785 item1 = list_from->changeItem(from_i, NULL);
787 item1 = list_from->takeItem(from_i, count);
789 // Try to add the item to destination list
790 InventoryItem *olditem = item1;
791 item1 = list_to->addItem(to_i, item1);
793 // If something is returned, the item was not fully added
796 // If olditem is returned, nothing was added.
797 bool nothing_added = (item1 == olditem);
799 // If something else is returned, part of the item was left unadded.
800 // Add the other part back to the source item
801 list_from->addItem(from_i, item1);
803 // If olditem is returned, nothing was added.
807 // Take item from source list
808 item1 = list_from->changeItem(from_i, NULL);
809 // Adding was not possible, swap the items.
810 InventoryItem *item2 = list_to->changeItem(to_i, item1);
811 // Put item from destination list to the source list
812 list_from->changeItem(from_i, item2);
816 mgr->inventoryModified(c, from_inv);
817 if(from_inv != to_inv)
818 mgr->inventoryModified(c, to_inv);
823 Craft checking system
826 bool ItemSpec::checkItem(InventoryItem *item)
828 if(type == ITEM_NONE)
836 // There should be an item
840 std::string itemname = item->getName();
842 if(type == ITEM_MATERIAL)
844 if(itemname != "MaterialItem")
846 MaterialItem *mitem = (MaterialItem*)item;
847 if(mitem->getMaterial() != num)
850 else if(type == ITEM_CRAFT)
852 if(itemname != "CraftItem")
854 CraftItem *mitem = (CraftItem*)item;
855 if(mitem->getSubName() != name)
858 else if(type == ITEM_TOOL)
863 else if(type == ITEM_MBO)
876 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
878 u16 items_min_x = 100;
879 u16 items_max_x = 100;
880 u16 items_min_y = 100;
881 u16 items_max_y = 100;
882 for(u16 y=0; y<3; y++)
883 for(u16 x=0; x<3; x++)
885 if(items[y*3 + x] == NULL)
887 if(items_min_x == 100 || x < items_min_x)
889 if(items_min_y == 100 || y < items_min_y)
891 if(items_max_x == 100 || x > items_max_x)
893 if(items_max_y == 100 || y > items_max_y)
896 // No items at all, just return false
897 if(items_min_x == 100)
900 u16 items_w = items_max_x - items_min_x + 1;
901 u16 items_h = items_max_y - items_min_y + 1;
903 u16 specs_min_x = 100;
904 u16 specs_max_x = 100;
905 u16 specs_min_y = 100;
906 u16 specs_max_y = 100;
907 for(u16 y=0; y<3; y++)
908 for(u16 x=0; x<3; x++)
910 if(specs[y*3 + x].type == ITEM_NONE)
912 if(specs_min_x == 100 || x < specs_min_x)
914 if(specs_min_y == 100 || y < specs_min_y)
916 if(specs_max_x == 100 || x > specs_max_x)
918 if(specs_max_y == 100 || y > specs_max_y)
921 // No specs at all, just return false
922 if(specs_min_x == 100)
925 u16 specs_w = specs_max_x - specs_min_x + 1;
926 u16 specs_h = specs_max_y - specs_min_y + 1;
929 if(items_w != specs_w || items_h != specs_h)
932 for(u16 y=0; y<specs_h; y++)
933 for(u16 x=0; x<specs_w; x++)
935 u16 items_x = items_min_x + x;
936 u16 items_y = items_min_y + y;
937 u16 specs_x = specs_min_x + x;
938 u16 specs_y = specs_min_y + y;
939 InventoryItem *item = items[items_y * 3 + items_x];
940 ItemSpec &spec = specs[specs_y * 3 + specs_x];
942 if(spec.checkItem(item) == false)