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"
25 #include "main.h" // For tsrc, g_toolmanager
26 #include "serverobject.h"
27 #include "content_mapnode.h"
28 #include "content_inventory.h"
29 #include "content_sao.h"
40 InventoryItem::InventoryItem(IGameDef *gamedef, u16 count):
47 InventoryItem::~InventoryItem()
51 content_t content_translate_from_19_to_internal(content_t c_from)
53 for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
55 if(trans_table_19[i][1] == c_from)
57 return trans_table_19[i][0];
63 InventoryItem* InventoryItem::deSerialize(std::istream &is, IGameDef *gamedef)
65 DSTACK(__FUNCTION_NAME);
67 //is.imbue(std::locale("C"));
70 std::getline(is, name, ' ');
72 if(name == "MaterialItem")
74 // u16 reads directly as a number (u8 doesn't)
79 // Convert old materials
82 material = content_translate_from_19_to_internal(material);
84 if(material > MAX_CONTENT)
85 throw SerializationError("Too large material number");
86 return new MaterialItem(gamedef, material, count);
88 else if(name == "MaterialItem2")
94 if(material > MAX_CONTENT)
95 throw SerializationError("Too large material number");
96 return new MaterialItem(gamedef, material, count);
98 else if(name == "MBOItem")
100 std::string inventorystring;
101 std::getline(is, inventorystring, '|');
102 throw SerializationError("MBOItem not supported anymore");
104 else if(name == "CraftItem")
107 std::getline(is, subname, ' ');
110 return new CraftItem(gamedef, subname, count);
112 else if(name == "ToolItem")
114 std::string toolname;
115 std::getline(is, toolname, ' ');
118 return new ToolItem(gamedef, toolname, wear);
122 infostream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
123 throw SerializationError("Unknown InventoryItem name");
127 std::string InventoryItem::getItemString() {
129 std::ostringstream os(std::ios_base::binary);
134 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
139 pos.Y -= BS*0.25; // let it drop a bit
141 pos.X += BS*0.2*(float)myrand_range(-1000,1000)/1000.0;
142 pos.Z += BS*0.2*(float)myrand_range(-1000,1000)/1000.0;
144 ServerActiveObject *obj = new ItemSAO(env, pos, getItemString());
153 video::ITexture * MaterialItem::getImage(ITextureSource *tsrc) const
155 return m_gamedef->getNodeDefManager()->get(m_content).inventory_texture;
159 bool MaterialItem::isCookable() const
161 INodeDefManager *ndef = m_gamedef->ndef();
162 const ContentFeatures &f = ndef->get(m_content);
163 return (f.cookresult_item != "");
166 InventoryItem *MaterialItem::createCookResult() const
168 INodeDefManager *ndef = m_gamedef->ndef();
169 const ContentFeatures &f = ndef->get(m_content);
170 std::istringstream is(f.cookresult_item, std::ios::binary);
171 return InventoryItem::deSerialize(is, m_gamedef);
174 float MaterialItem::getCookTime() const
176 INodeDefManager *ndef = m_gamedef->ndef();
177 const ContentFeatures &f = ndef->get(m_content);
178 return f.furnace_cooktime;
181 float MaterialItem::getBurnTime() const
183 INodeDefManager *ndef = m_gamedef->ndef();
184 const ContentFeatures &f = ndef->get(m_content);
185 return f.furnace_burntime;
192 std::string ToolItem::getImageBasename() const
194 return m_gamedef->getToolDefManager()->getImagename(m_toolname);
198 video::ITexture * ToolItem::getImage(ITextureSource *tsrc) const
203 std::string basename = getImageBasename();
206 Calculate a progress value with sane amount of
209 u32 maxprogress = 30;
210 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
212 float value_f = (float)toolprogress / (float)maxprogress;
213 std::ostringstream os;
214 os<<basename<<"^[progressbar"<<value_f;
216 return tsrc->getTextureRaw(os.str());
219 video::ITexture * ToolItem::getImageRaw(ITextureSource *tsrc) const
224 return tsrc->getTextureRaw(getImageBasename());
233 video::ITexture * CraftItem::getImage(ITextureSource *tsrc) const
238 std::string name = item_craft_get_image_name(m_subname, m_gamedef);
240 // Get such a texture
241 return tsrc->getTextureRaw(name);
245 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
248 ServerActiveObject *obj = item_craft_create_object(m_subname, env, pos);
252 return InventoryItem::createSAO(env, id, pos);
255 u16 CraftItem::getDropCount() const
258 s16 dc = item_craft_get_drop_count(m_subname, m_gamedef);
262 return InventoryItem::getDropCount();
265 bool CraftItem::isCookable() const
267 return item_craft_is_cookable(m_subname, m_gamedef);
270 InventoryItem *CraftItem::createCookResult() const
272 return item_craft_create_cook_result(m_subname, m_gamedef);
275 float CraftItem::getCookTime() const
280 float CraftItem::getBurnTime() const
282 if(m_subname == "lump_of_coal")
287 bool CraftItem::use(ServerEnvironment *env, ServerActiveObject *user)
289 if(!item_craft_is_eatable(m_subname, m_gamedef))
292 u16 result_count = getCount() - 1; // Eat one at a time
293 s16 hp_change = item_craft_eat_hp_change(m_subname, m_gamedef);
294 s16 hp = user->getHP();
303 setCount(result_count);
311 InventoryList::InventoryList(std::string name, u32 size)
319 InventoryList::~InventoryList()
321 for(u32 i=0; i<m_items.size(); i++)
328 void InventoryList::clearItems()
330 for(u32 i=0; i<m_items.size(); i++)
338 for(u32 i=0; i<m_size; i++)
340 m_items.push_back(NULL);
346 void InventoryList::serialize(std::ostream &os) const
348 //os.imbue(std::locale("C"));
350 for(u32 i=0; i<m_items.size(); i++)
352 InventoryItem *item = m_items[i];
365 os<<"EndInventoryList\n";
368 void InventoryList::deSerialize(std::istream &is, IGameDef *gamedef)
370 //is.imbue(std::locale("C"));
378 std::getline(is, line, '\n');
380 std::istringstream iss(line);
381 //iss.imbue(std::locale("C"));
384 std::getline(iss, name, ' ');
386 if(name == "EndInventoryList")
390 // This is a temporary backwards compatibility fix
391 else if(name == "end")
395 else if(name == "Item")
397 if(item_i > getSize() - 1)
398 throw SerializationError("too many items");
399 InventoryItem *item = InventoryItem::deSerialize(iss, gamedef);
400 m_items[item_i++] = item;
402 else if(name == "Empty")
404 if(item_i > getSize() - 1)
405 throw SerializationError("too many items");
406 m_items[item_i++] = NULL;
410 throw SerializationError("Unknown inventory identifier");
415 InventoryList::InventoryList(const InventoryList &other)
418 Do this so that the items get cloned. Otherwise the pointers
419 in the array will just get copied.
424 InventoryList & InventoryList::operator = (const InventoryList &other)
426 m_name = other.m_name;
427 m_size = other.m_size;
429 for(u32 i=0; i<other.m_items.size(); i++)
431 InventoryItem *item = other.m_items[i];
434 m_items[i] = item->clone();
442 const std::string &InventoryList::getName() const
447 u32 InventoryList::getSize()
449 return m_items.size();
452 u32 InventoryList::getUsedSlots()
455 for(u32 i=0; i<m_items.size(); i++)
457 InventoryItem *item = m_items[i];
464 u32 InventoryList::getFreeSlots()
466 return getSize() - getUsedSlots();
469 const InventoryItem * InventoryList::getItem(u32 i) const
471 if(i > m_items.size() - 1)
476 InventoryItem * InventoryList::getItem(u32 i)
478 if(i > m_items.size() - 1)
483 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
485 assert(i < m_items.size());
487 InventoryItem *olditem = m_items[i];
488 m_items[i] = newitem;
493 void InventoryList::deleteItem(u32 i)
495 assert(i < m_items.size());
496 InventoryItem *item = changeItem(i, NULL);
501 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
507 First try to find if it could be added to some existing items
509 for(u32 i=0; i<m_items.size(); i++)
511 // Ignore empty slots
512 if(m_items[i] == NULL)
515 newitem = addItem(i, newitem);
517 return NULL; // All was eaten
521 Then try to add it to empty slots
523 for(u32 i=0; i<m_items.size(); i++)
525 // Ignore unempty slots
526 if(m_items[i] != NULL)
529 newitem = addItem(i, newitem);
531 return NULL; // All was eaten
538 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
545 // If it is an empty position, it's an easy job.
546 InventoryItem *to_item = getItem(i);
549 m_items[i] = newitem;
553 // If not addable, return the item
554 if(newitem->addableTo(to_item) == false)
557 // If the item fits fully in the slot, add counter and delete it
558 if(newitem->getCount() <= to_item->freeSpace())
560 to_item->add(newitem->getCount());
564 // Else the item does not fit fully. Add all that fits and return
568 u16 freespace = to_item->freeSpace();
569 to_item->add(freespace);
570 newitem->remove(freespace);
575 bool InventoryList::itemFits(const u32 i, const InventoryItem *newitem)
577 // If it is an empty position, it's an easy job.
578 const InventoryItem *to_item = getItem(i);
584 // If not addable, fail
585 if(newitem->addableTo(to_item) == false)
588 // If the item fits fully in the slot, pass
589 if(newitem->getCount() <= to_item->freeSpace())
597 bool InventoryList::roomForItem(const InventoryItem *item)
599 for(u32 i=0; i<m_items.size(); i++)
600 if(itemFits(i, item))
605 bool InventoryList::roomForCookedItem(const InventoryItem *item)
609 const InventoryItem *cook = item->createCookResult();
612 bool room = roomForItem(cook);
617 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
624 InventoryItem *item = getItem(i);
625 // If it is an empty position, return NULL
629 if(count >= item->getCount())
631 // Get the item by swapping NULL to its place
632 return changeItem(i, NULL);
636 InventoryItem *item2 = item->clone();
638 item2->setCount(count);
645 void InventoryList::decrementMaterials(u16 count)
647 for(u32 i=0; i<m_items.size(); i++)
649 InventoryItem *item = takeItem(i, count);
655 void InventoryList::print(std::ostream &o)
657 o<<"InventoryList:"<<std::endl;
658 for(u32 i=0; i<m_items.size(); i++)
660 InventoryItem *item = m_items[i];
674 Inventory::~Inventory()
679 void Inventory::clear()
681 for(u32 i=0; i<m_lists.size(); i++)
688 Inventory::Inventory()
692 Inventory::Inventory(const Inventory &other)
697 Inventory & Inventory::operator = (const Inventory &other)
700 for(u32 i=0; i<other.m_lists.size(); i++)
702 m_lists.push_back(new InventoryList(*other.m_lists[i]));
707 void Inventory::serialize(std::ostream &os) const
709 for(u32 i=0; i<m_lists.size(); i++)
711 InventoryList *list = m_lists[i];
712 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
716 os<<"EndInventory\n";
719 void Inventory::deSerialize(std::istream &is, IGameDef *gamedef)
726 std::getline(is, line, '\n');
728 std::istringstream iss(line);
731 std::getline(iss, name, ' ');
733 if(name == "EndInventory")
737 // This is a temporary backwards compatibility fix
738 else if(name == "end")
742 else if(name == "List")
744 std::string listname;
747 std::getline(iss, listname, ' ');
750 InventoryList *list = new InventoryList(listname, listsize);
751 list->deSerialize(is, gamedef);
753 m_lists.push_back(list);
757 throw SerializationError("Unknown inventory identifier");
762 InventoryList * Inventory::addList(const std::string &name, u32 size)
764 s32 i = getListIndex(name);
767 if(m_lists[i]->getSize() != size)
770 m_lists[i] = new InventoryList(name, size);
776 m_lists.push_back(new InventoryList(name, size));
777 return m_lists.getLast();
781 InventoryList * Inventory::getList(const std::string &name)
783 s32 i = getListIndex(name);
789 const InventoryList * Inventory::getList(const std::string &name) const
791 s32 i = getListIndex(name);
797 const s32 Inventory::getListIndex(const std::string &name) const
799 for(u32 i=0; i<m_lists.size(); i++)
801 if(m_lists[i]->getName() == name)
811 InventoryAction * InventoryAction::deSerialize(std::istream &is)
814 std::getline(is, type, ' ');
816 InventoryAction *a = NULL;
820 a = new IMoveAction(is);
826 static std::string describeC(const struct InventoryContext *c)
828 if(c->current_player == NULL)
829 return "current_player=NULL";
831 return std::string("current_player=") + c->current_player->getName();
834 IMoveAction::IMoveAction(std::istream &is)
838 std::getline(is, ts, ' ');
841 std::getline(is, from_inv, ' ');
843 std::getline(is, from_list, ' ');
845 std::getline(is, ts, ' ');
848 std::getline(is, to_inv, ' ');
850 std::getline(is, to_list, ' ');
852 std::getline(is, ts, ' ');
856 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
858 Inventory *inv_from = mgr->getInventory(c, from_inv);
859 Inventory *inv_to = mgr->getInventory(c, to_inv);
862 infostream<<"IMoveAction::apply(): FAIL: source inventory not found: "
863 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
864 <<", to_inv=\""<<to_inv<<"\""<<std::endl;
868 infostream<<"IMoveAction::apply(): FAIL: destination inventory not found: "
869 "context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
870 <<", to_inv=\""<<to_inv<<"\""<<std::endl;
874 InventoryList *list_from = inv_from->getList(from_list);
875 InventoryList *list_to = inv_to->getList(to_list);
878 If a list doesn't exist or the source item doesn't exist
881 infostream<<"IMoveAction::apply(): FAIL: source list not found: "
882 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
883 <<", from_list=\""<<from_list<<"\""<<std::endl;
887 infostream<<"IMoveAction::apply(): FAIL: destination list not found: "
888 <<"context=["<<describeC(c)<<"], to_inv=\""<<to_inv<<"\""
889 <<", to_list=\""<<to_list<<"\""<<std::endl;
892 if(list_from->getItem(from_i) == NULL)
894 infostream<<"IMoveAction::apply(): FAIL: source item not found: "
895 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
896 <<", from_list=\""<<from_list<<"\""
897 <<" from_i="<<from_i<<std::endl;
901 If the source and the destination slots are the same
903 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
905 infostream<<"IMoveAction::apply(): FAIL: source and destination slots "
906 <<"are the same: inv=\""<<from_inv<<"\" list=\""<<from_list
907 <<"\" i="<<from_i<<std::endl;
911 // Take item from source list
912 InventoryItem *item1 = NULL;
914 item1 = list_from->changeItem(from_i, NULL);
916 item1 = list_from->takeItem(from_i, count);
918 // Try to add the item to destination list
919 InventoryItem *olditem = item1;
920 item1 = list_to->addItem(to_i, item1);
922 // If something is returned, the item was not fully added
925 // If olditem is returned, nothing was added.
926 bool nothing_added = (item1 == olditem);
928 // If something else is returned, part of the item was left unadded.
929 // Add the other part back to the source item
930 list_from->addItem(from_i, item1);
932 // If olditem is returned, nothing was added.
936 // Take item from source list
937 item1 = list_from->changeItem(from_i, NULL);
938 // Adding was not possible, swap the items.
939 InventoryItem *item2 = list_to->changeItem(to_i, item1);
940 // Put item from destination list to the source list
941 list_from->changeItem(from_i, item2);
945 mgr->inventoryModified(c, from_inv);
946 if(from_inv != to_inv)
947 mgr->inventoryModified(c, to_inv);
949 infostream<<"IMoveAction::apply(): moved at "
950 <<"["<<describeC(c)<<"]"
951 <<" from inv=\""<<from_inv<<"\""
952 <<" list=\""<<from_list<<"\""
954 <<" to inv=\""<<to_inv<<"\""
955 <<" list=\""<<to_list<<"\""
961 Craft checking system
964 bool ItemSpec::checkItem(const InventoryItem *item) const
966 if(type == ITEM_NONE)
974 // There should be an item
978 std::string itemname = item->getName();
980 if(type == ITEM_MATERIAL)
982 if(itemname != "MaterialItem")
984 MaterialItem *mitem = (MaterialItem*)item;
985 if(mitem->getMaterial() != num)
988 else if(type == ITEM_CRAFT)
990 if(itemname != "CraftItem")
992 CraftItem *mitem = (CraftItem*)item;
993 if(mitem->getSubName() != name)
996 else if(type == ITEM_TOOL)
1001 else if(type == ITEM_MBO)
1003 // Not supported yet
1008 // Not supported yet
1014 bool checkItemCombination(InventoryItem const * const *items, const ItemSpec *specs)
1016 u16 items_min_x = 100;
1017 u16 items_max_x = 100;
1018 u16 items_min_y = 100;
1019 u16 items_max_y = 100;
1020 for(u16 y=0; y<3; y++)
1021 for(u16 x=0; x<3; x++)
1023 if(items[y*3 + x] == NULL)
1025 if(items_min_x == 100 || x < items_min_x)
1027 if(items_min_y == 100 || y < items_min_y)
1029 if(items_max_x == 100 || x > items_max_x)
1031 if(items_max_y == 100 || y > items_max_y)
1034 // No items at all, just return false
1035 if(items_min_x == 100)
1038 u16 items_w = items_max_x - items_min_x + 1;
1039 u16 items_h = items_max_y - items_min_y + 1;
1041 u16 specs_min_x = 100;
1042 u16 specs_max_x = 100;
1043 u16 specs_min_y = 100;
1044 u16 specs_max_y = 100;
1045 for(u16 y=0; y<3; y++)
1046 for(u16 x=0; x<3; x++)
1048 if(specs[y*3 + x].type == ITEM_NONE)
1050 if(specs_min_x == 100 || x < specs_min_x)
1052 if(specs_min_y == 100 || y < specs_min_y)
1054 if(specs_max_x == 100 || x > specs_max_x)
1056 if(specs_max_y == 100 || y > specs_max_y)
1059 // No specs at all, just return false
1060 if(specs_min_x == 100)
1063 u16 specs_w = specs_max_x - specs_min_x + 1;
1064 u16 specs_h = specs_max_y - specs_min_y + 1;
1067 if(items_w != specs_w || items_h != specs_h)
1070 for(u16 y=0; y<specs_h; y++)
1071 for(u16 x=0; x<specs_w; x++)
1073 u16 items_x = items_min_x + x;
1074 u16 items_y = items_min_y + y;
1075 u16 specs_x = specs_min_x + x;
1076 u16 specs_y = specs_min_y + y;
1077 const InventoryItem *item = items[items_y * 3 + items_x];
1078 const ItemSpec &spec = specs[specs_y * 3 + specs_x];
1080 if(spec.checkItem(item) == false)