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"
36 InventoryItem::InventoryItem(u16 count)
41 InventoryItem::~InventoryItem()
45 InventoryItem* InventoryItem::deSerialize(std::istream &is)
47 DSTACK(__FUNCTION_NAME);
49 //is.imbue(std::locale("C"));
52 std::getline(is, name, ' ');
54 if(name == "MaterialItem")
56 // u16 reads directly as a number (u8 doesn't)
62 throw SerializationError("Too large material number");
63 return new MaterialItem(material, count);
65 else if(name == "MBOItem")
67 std::string inventorystring;
68 std::getline(is, inventorystring, '|');
69 return new MapBlockObjectItem(inventorystring);
71 else if(name == "CraftItem")
74 std::getline(is, subname, ' ');
77 return new CraftItem(subname, count);
79 else if(name == "ToolItem")
82 std::getline(is, toolname, ' ');
85 return new ToolItem(toolname, wear);
89 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
90 throw SerializationError("Unknown InventoryItem name");
94 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
100 std::ostringstream os(std::ios_base::binary);
103 ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
111 bool MaterialItem::isCookable()
113 if(m_content == CONTENT_TREE)
117 else if(m_content == CONTENT_COBBLE)
121 else if(m_content == CONTENT_SAND)
128 InventoryItem *MaterialItem::createCookResult()
130 if(m_content == CONTENT_TREE)
132 return new CraftItem("lump_of_coal", 1);
134 else if(m_content == CONTENT_COBBLE)
136 return new MaterialItem(CONTENT_STONE, 1);
138 else if(m_content == CONTENT_SAND)
140 return new MaterialItem(CONTENT_GLASS, 1);
150 video::ITexture * CraftItem::getImage()
152 if(g_texturesource == NULL)
157 if(m_subname == "Stick")
159 else if(m_subname == "paper")
161 else if(m_subname == "book")
163 else if(m_subname == "lump_of_coal")
164 name = "lump_of_coal.png";
165 else if(m_subname == "lump_of_iron")
166 name = "lump_of_iron.png";
167 else if(m_subname == "lump_of_clay")
168 name = "lump_of_clay.png";
169 else if(m_subname == "steel_ingot")
170 name = "steel_ingot.png";
171 else if(m_subname == "clay_brick")
172 name = "clay_brick.png";
173 else if(m_subname == "rat")
178 // Get such a texture
179 return g_texturesource->getTextureRaw(name);
183 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
186 if(m_subname == "rat")
188 ServerActiveObject *obj = new RatSAO(env, id, pos);
194 return InventoryItem::createSAO(env, id, pos);
198 u16 CraftItem::getDropCount()
201 if(m_subname == "rat")
205 return InventoryItem::getDropCount();
208 bool CraftItem::isCookable()
210 if(m_subname == "lump_of_iron" || m_subname == "lump_of_clay")
217 InventoryItem *CraftItem::createCookResult()
219 if(m_subname == "lump_of_iron")
221 return new CraftItem("steel_ingot", 1);
223 else if(m_subname == "lump_of_clay")
224 return new CraftItem("clay_brick", 1);
233 video::ITexture * MapBlockObjectItem::getImage()
235 if(m_inventorystring.substr(0,3) == "Rat")
236 return g_texturesource->getTextureRaw("rat.png");
238 if(m_inventorystring.substr(0,4) == "Sign")
239 return g_texturesource->getTextureRaw("sign.png");
244 std::string MapBlockObjectItem::getText()
246 if(m_inventorystring.substr(0,3) == "Rat")
249 if(m_inventorystring.substr(0,4) == "Sign")
255 MapBlockObject * MapBlockObjectItem::createObject
256 (v3f pos, f32 player_yaw, f32 player_pitch)
258 std::istringstream is(m_inventorystring);
260 std::getline(is, name, ' ');
266 else if(name == "Sign")
269 std::getline(is, text, '|');
270 SignObject *obj = new SignObject(NULL, -1, pos);
272 obj->setYaw(-player_yaw);
275 else if(name == "Rat")
277 RatObject *obj = new RatObject(NULL, -1, pos);
280 else if(name == "ItemObj")
283 Now we are an inventory item containing the serialization
284 string of an object that contains the serialization
285 string of an inventory item. Fuck this.
288 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
289 <<"because an item-object should never be inside "
290 <<"an object-item."<<std::endl;
303 InventoryList::InventoryList(std::string name, u32 size)
311 InventoryList::~InventoryList()
313 for(u32 i=0; i<m_items.size(); i++)
320 void InventoryList::clearItems()
322 for(u32 i=0; i<m_items.size(); i++)
330 for(u32 i=0; i<m_size; i++)
332 m_items.push_back(NULL);
338 void InventoryList::serialize(std::ostream &os)
340 //os.imbue(std::locale("C"));
342 for(u32 i=0; i<m_items.size(); i++)
344 InventoryItem *item = m_items[i];
357 os<<"EndInventoryList\n";
360 void InventoryList::deSerialize(std::istream &is)
362 //is.imbue(std::locale("C"));
370 std::getline(is, line, '\n');
372 std::istringstream iss(line);
373 //iss.imbue(std::locale("C"));
376 std::getline(iss, name, ' ');
378 if(name == "EndInventoryList")
382 // This is a temporary backwards compatibility fix
383 else if(name == "end")
387 else if(name == "Item")
389 if(item_i > getSize() - 1)
390 throw SerializationError("too many items");
391 InventoryItem *item = InventoryItem::deSerialize(iss);
392 m_items[item_i++] = item;
394 else if(name == "Empty")
396 if(item_i > getSize() - 1)
397 throw SerializationError("too many items");
398 m_items[item_i++] = NULL;
402 throw SerializationError("Unknown inventory identifier");
407 InventoryList::InventoryList(const InventoryList &other)
410 Do this so that the items get cloned. Otherwise the pointers
411 in the array will just get copied.
416 InventoryList & InventoryList::operator = (const InventoryList &other)
418 m_name = other.m_name;
419 m_size = other.m_size;
421 for(u32 i=0; i<other.m_items.size(); i++)
423 InventoryItem *item = other.m_items[i];
426 m_items[i] = item->clone();
434 std::string InventoryList::getName()
439 u32 InventoryList::getSize()
441 return m_items.size();
444 u32 InventoryList::getUsedSlots()
447 for(u32 i=0; i<m_items.size(); i++)
449 InventoryItem *item = m_items[i];
456 u32 InventoryList::getFreeSlots()
458 return getSize() - getUsedSlots();
461 InventoryItem * InventoryList::getItem(u32 i)
463 if(i > m_items.size() - 1)
468 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
470 assert(i < m_items.size());
472 InventoryItem *olditem = m_items[i];
473 m_items[i] = newitem;
478 void InventoryList::deleteItem(u32 i)
480 assert(i < m_items.size());
481 InventoryItem *item = changeItem(i, NULL);
486 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
492 First try to find if it could be added to some existing items
494 for(u32 i=0; i<m_items.size(); i++)
496 // Ignore empty slots
497 if(m_items[i] == NULL)
500 newitem = addItem(i, newitem);
502 return NULL; // All was eaten
506 Then try to add it to empty slots
508 for(u32 i=0; i<m_items.size(); i++)
510 // Ignore unempty slots
511 if(m_items[i] != NULL)
514 newitem = addItem(i, newitem);
516 return NULL; // All was eaten
523 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
530 // If it is an empty position, it's an easy job.
531 InventoryItem *to_item = m_items[i];
534 m_items[i] = newitem;
538 // If not addable, return the item
539 if(newitem->addableTo(to_item) == false)
542 // If the item fits fully in the slot, add counter and delete it
543 if(newitem->getCount() <= to_item->freeSpace())
545 to_item->add(newitem->getCount());
549 // Else the item does not fit fully. Add all that fits and return
553 u16 freespace = to_item->freeSpace();
554 to_item->add(freespace);
555 newitem->remove(freespace);
560 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
562 // If it is an empty position, it's an easy job.
563 InventoryItem *to_item = m_items[i];
569 // If not addable, return the item
570 if(newitem->addableTo(to_item) == false)
573 // If the item fits fully in the slot, add counter and delete it
574 if(newitem->getCount() <= to_item->freeSpace())
582 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
589 InventoryItem *item = m_items[i];
590 // If it is an empty position, return NULL
594 if(count >= item->getCount())
596 // Get the item by swapping NULL to its place
597 return changeItem(i, NULL);
601 InventoryItem *item2 = item->clone();
603 item2->setCount(count);
610 void InventoryList::decrementMaterials(u16 count)
612 for(u32 i=0; i<m_items.size(); i++)
614 InventoryItem *item = takeItem(i, count);
620 void InventoryList::print(std::ostream &o)
622 o<<"InventoryList:"<<std::endl;
623 for(u32 i=0; i<m_items.size(); i++)
625 InventoryItem *item = m_items[i];
639 Inventory::~Inventory()
644 void Inventory::clear()
646 for(u32 i=0; i<m_lists.size(); i++)
653 Inventory::Inventory()
657 Inventory::Inventory(const Inventory &other)
662 Inventory & Inventory::operator = (const Inventory &other)
665 for(u32 i=0; i<other.m_lists.size(); i++)
667 m_lists.push_back(new InventoryList(*other.m_lists[i]));
672 void Inventory::serialize(std::ostream &os)
674 for(u32 i=0; i<m_lists.size(); i++)
676 InventoryList *list = m_lists[i];
677 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
681 os<<"EndInventory\n";
684 void Inventory::deSerialize(std::istream &is)
691 std::getline(is, line, '\n');
693 std::istringstream iss(line);
696 std::getline(iss, name, ' ');
698 if(name == "EndInventory")
702 // This is a temporary backwards compatibility fix
703 else if(name == "end")
707 else if(name == "List")
709 std::string listname;
712 std::getline(iss, listname, ' ');
715 InventoryList *list = new InventoryList(listname, listsize);
716 list->deSerialize(is);
718 m_lists.push_back(list);
722 throw SerializationError("Unknown inventory identifier");
727 InventoryList * Inventory::addList(const std::string &name, u32 size)
729 s32 i = getListIndex(name);
732 if(m_lists[i]->getSize() != size)
735 m_lists[i] = new InventoryList(name, size);
741 m_lists.push_back(new InventoryList(name, size));
742 return m_lists.getLast();
746 InventoryList * Inventory::getList(const std::string &name)
748 s32 i = getListIndex(name);
754 s32 Inventory::getListIndex(const std::string &name)
756 for(u32 i=0; i<m_lists.size(); i++)
758 if(m_lists[i]->getName() == name)
768 InventoryAction * InventoryAction::deSerialize(std::istream &is)
771 std::getline(is, type, ' ');
773 InventoryAction *a = NULL;
777 a = new IMoveAction(is);
783 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
787 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
788 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
789 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
791 Inventory *inv_from = mgr->getInventory(c, from_inv);
792 Inventory *inv_to = mgr->getInventory(c, to_inv);
794 if(!inv_from || !inv_to)
796 dstream<<__FUNCTION_NAME<<": Operation not allowed "
797 <<"(inventories not found)"<<std::endl;
801 InventoryList *list_from = inv_from->getList(from_list);
802 InventoryList *list_to = inv_to->getList(to_list);
804 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
807 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
810 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
814 If a list doesn't exist or the source item doesn't exist
816 if(!list_from || !list_to)
818 dstream<<__FUNCTION_NAME<<": Operation not allowed "
819 <<"(a list doesn't exist)"
823 if(list_from->getItem(from_i) == NULL)
825 dstream<<__FUNCTION_NAME<<": Operation not allowed "
826 <<"(the source item doesn't exist)"
831 If the source and the destination slots are the same
833 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
835 dstream<<__FUNCTION_NAME<<": Operation not allowed "
836 <<"(source and the destination slots are the same)"<<std::endl;
840 // Take item from source list
841 InventoryItem *item1 = NULL;
843 item1 = list_from->changeItem(from_i, NULL);
845 item1 = list_from->takeItem(from_i, count);
847 // Try to add the item to destination list
848 InventoryItem *olditem = item1;
849 item1 = list_to->addItem(to_i, item1);
851 // If something is returned, the item was not fully added
854 // If olditem is returned, nothing was added.
855 bool nothing_added = (item1 == olditem);
857 // If something else is returned, part of the item was left unadded.
858 // Add the other part back to the source item
859 list_from->addItem(from_i, item1);
861 // If olditem is returned, nothing was added.
865 // Take item from source list
866 item1 = list_from->changeItem(from_i, NULL);
867 // Adding was not possible, swap the items.
868 InventoryItem *item2 = list_to->changeItem(to_i, item1);
869 // Put item from destination list to the source list
870 list_from->changeItem(from_i, item2);
874 mgr->inventoryModified(c, from_inv);
875 if(from_inv != to_inv)
876 mgr->inventoryModified(c, to_inv);
881 Craft checking system
884 bool ItemSpec::checkItem(InventoryItem *item)
886 if(type == ITEM_NONE)
894 // There should be an item
898 std::string itemname = item->getName();
900 if(type == ITEM_MATERIAL)
902 if(itemname != "MaterialItem")
904 MaterialItem *mitem = (MaterialItem*)item;
905 if(mitem->getMaterial() != num)
908 else if(type == ITEM_CRAFT)
910 if(itemname != "CraftItem")
912 CraftItem *mitem = (CraftItem*)item;
913 if(mitem->getSubName() != name)
916 else if(type == ITEM_TOOL)
921 else if(type == ITEM_MBO)
934 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
936 u16 items_min_x = 100;
937 u16 items_max_x = 100;
938 u16 items_min_y = 100;
939 u16 items_max_y = 100;
940 for(u16 y=0; y<3; y++)
941 for(u16 x=0; x<3; x++)
943 if(items[y*3 + x] == NULL)
945 if(items_min_x == 100 || x < items_min_x)
947 if(items_min_y == 100 || y < items_min_y)
949 if(items_max_x == 100 || x > items_max_x)
951 if(items_max_y == 100 || y > items_max_y)
954 // No items at all, just return false
955 if(items_min_x == 100)
958 u16 items_w = items_max_x - items_min_x + 1;
959 u16 items_h = items_max_y - items_min_y + 1;
961 u16 specs_min_x = 100;
962 u16 specs_max_x = 100;
963 u16 specs_min_y = 100;
964 u16 specs_max_y = 100;
965 for(u16 y=0; y<3; y++)
966 for(u16 x=0; x<3; x++)
968 if(specs[y*3 + x].type == ITEM_NONE)
970 if(specs_min_x == 100 || x < specs_min_x)
972 if(specs_min_y == 100 || y < specs_min_y)
974 if(specs_max_x == 100 || x > specs_max_x)
976 if(specs_max_y == 100 || y > specs_max_y)
979 // No specs at all, just return false
980 if(specs_min_x == 100)
983 u16 specs_w = specs_max_x - specs_min_x + 1;
984 u16 specs_h = specs_max_y - specs_min_y + 1;
987 if(items_w != specs_w || items_h != specs_h)
990 for(u16 y=0; y<specs_h; y++)
991 for(u16 x=0; x<specs_w; x++)
993 u16 items_x = items_min_x + x;
994 u16 items_y = items_min_y + y;
995 u16 specs_x = specs_min_x + x;
996 u16 specs_y = specs_min_y + y;
997 InventoryItem *item = items[items_y * 3 + items_x];
998 ItemSpec &spec = specs[specs_y * 3 + specs_x];
1000 if(spec.checkItem(item) == false)