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 return item_material_is_cookable(m_content, m_gamedef);
164 InventoryItem *MaterialItem::createCookResult() const
166 return item_material_create_cook_result(m_content, m_gamedef);
173 std::string ToolItem::getImageBasename() const
175 return m_gamedef->getToolDefManager()->getImagename(m_toolname);
179 video::ITexture * ToolItem::getImage(ITextureSource *tsrc) const
184 std::string basename = getImageBasename();
187 Calculate a progress value with sane amount of
190 u32 maxprogress = 30;
191 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
193 float value_f = (float)toolprogress / (float)maxprogress;
194 std::ostringstream os;
195 os<<basename<<"^[progressbar"<<value_f;
197 return tsrc->getTextureRaw(os.str());
200 video::ITexture * ToolItem::getImageRaw(ITextureSource *tsrc) const
205 return tsrc->getTextureRaw(getImageBasename());
214 video::ITexture * CraftItem::getImage(ITextureSource *tsrc) const
219 std::string name = item_craft_get_image_name(m_subname, m_gamedef);
221 // Get such a texture
222 return tsrc->getTextureRaw(name);
226 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
229 ServerActiveObject *obj = item_craft_create_object(m_subname, env, pos);
233 return InventoryItem::createSAO(env, id, pos);
236 u16 CraftItem::getDropCount() const
239 s16 dc = item_craft_get_drop_count(m_subname, m_gamedef);
243 return InventoryItem::getDropCount();
246 bool CraftItem::isCookable() const
248 return item_craft_is_cookable(m_subname, m_gamedef);
251 InventoryItem *CraftItem::createCookResult() const
253 return item_craft_create_cook_result(m_subname, m_gamedef);
256 bool CraftItem::use(ServerEnvironment *env, ServerActiveObject *user)
258 if(!item_craft_is_eatable(m_subname, m_gamedef))
261 u16 result_count = getCount() - 1; // Eat one at a time
262 s16 hp_change = item_craft_eat_hp_change(m_subname, m_gamedef);
263 s16 hp = user->getHP();
272 setCount(result_count);
280 InventoryList::InventoryList(std::string name, u32 size)
288 InventoryList::~InventoryList()
290 for(u32 i=0; i<m_items.size(); i++)
297 void InventoryList::clearItems()
299 for(u32 i=0; i<m_items.size(); i++)
307 for(u32 i=0; i<m_size; i++)
309 m_items.push_back(NULL);
315 void InventoryList::serialize(std::ostream &os) const
317 //os.imbue(std::locale("C"));
319 for(u32 i=0; i<m_items.size(); i++)
321 InventoryItem *item = m_items[i];
334 os<<"EndInventoryList\n";
337 void InventoryList::deSerialize(std::istream &is, IGameDef *gamedef)
339 //is.imbue(std::locale("C"));
347 std::getline(is, line, '\n');
349 std::istringstream iss(line);
350 //iss.imbue(std::locale("C"));
353 std::getline(iss, name, ' ');
355 if(name == "EndInventoryList")
359 // This is a temporary backwards compatibility fix
360 else if(name == "end")
364 else if(name == "Item")
366 if(item_i > getSize() - 1)
367 throw SerializationError("too many items");
368 InventoryItem *item = InventoryItem::deSerialize(iss, gamedef);
369 m_items[item_i++] = item;
371 else if(name == "Empty")
373 if(item_i > getSize() - 1)
374 throw SerializationError("too many items");
375 m_items[item_i++] = NULL;
379 throw SerializationError("Unknown inventory identifier");
384 InventoryList::InventoryList(const InventoryList &other)
387 Do this so that the items get cloned. Otherwise the pointers
388 in the array will just get copied.
393 InventoryList & InventoryList::operator = (const InventoryList &other)
395 m_name = other.m_name;
396 m_size = other.m_size;
398 for(u32 i=0; i<other.m_items.size(); i++)
400 InventoryItem *item = other.m_items[i];
403 m_items[i] = item->clone();
411 const std::string &InventoryList::getName() const
416 u32 InventoryList::getSize()
418 return m_items.size();
421 u32 InventoryList::getUsedSlots()
424 for(u32 i=0; i<m_items.size(); i++)
426 InventoryItem *item = m_items[i];
433 u32 InventoryList::getFreeSlots()
435 return getSize() - getUsedSlots();
438 const InventoryItem * InventoryList::getItem(u32 i) const
440 if(i > m_items.size() - 1)
445 InventoryItem * InventoryList::getItem(u32 i)
447 if(i > m_items.size() - 1)
452 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
454 assert(i < m_items.size());
456 InventoryItem *olditem = m_items[i];
457 m_items[i] = newitem;
462 void InventoryList::deleteItem(u32 i)
464 assert(i < m_items.size());
465 InventoryItem *item = changeItem(i, NULL);
470 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
476 First try to find if it could be added to some existing items
478 for(u32 i=0; i<m_items.size(); i++)
480 // Ignore empty slots
481 if(m_items[i] == NULL)
484 newitem = addItem(i, newitem);
486 return NULL; // All was eaten
490 Then try to add it to empty slots
492 for(u32 i=0; i<m_items.size(); i++)
494 // Ignore unempty slots
495 if(m_items[i] != NULL)
498 newitem = addItem(i, newitem);
500 return NULL; // All was eaten
507 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
514 // If it is an empty position, it's an easy job.
515 InventoryItem *to_item = getItem(i);
518 m_items[i] = newitem;
522 // If not addable, return the item
523 if(newitem->addableTo(to_item) == false)
526 // If the item fits fully in the slot, add counter and delete it
527 if(newitem->getCount() <= to_item->freeSpace())
529 to_item->add(newitem->getCount());
533 // Else the item does not fit fully. Add all that fits and return
537 u16 freespace = to_item->freeSpace();
538 to_item->add(freespace);
539 newitem->remove(freespace);
544 bool InventoryList::itemFits(const u32 i, const InventoryItem *newitem)
546 // If it is an empty position, it's an easy job.
547 const InventoryItem *to_item = getItem(i);
553 // If not addable, fail
554 if(newitem->addableTo(to_item) == false)
557 // If the item fits fully in the slot, pass
558 if(newitem->getCount() <= to_item->freeSpace())
566 bool InventoryList::roomForItem(const InventoryItem *item)
568 for(u32 i=0; i<m_items.size(); i++)
569 if(itemFits(i, item))
574 bool InventoryList::roomForCookedItem(const InventoryItem *item)
578 const InventoryItem *cook = item->createCookResult();
581 bool room = roomForItem(cook);
586 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
593 InventoryItem *item = getItem(i);
594 // If it is an empty position, return NULL
598 if(count >= item->getCount())
600 // Get the item by swapping NULL to its place
601 return changeItem(i, NULL);
605 InventoryItem *item2 = item->clone();
607 item2->setCount(count);
614 void InventoryList::decrementMaterials(u16 count)
616 for(u32 i=0; i<m_items.size(); i++)
618 InventoryItem *item = takeItem(i, count);
624 void InventoryList::print(std::ostream &o)
626 o<<"InventoryList:"<<std::endl;
627 for(u32 i=0; i<m_items.size(); i++)
629 InventoryItem *item = m_items[i];
643 Inventory::~Inventory()
648 void Inventory::clear()
650 for(u32 i=0; i<m_lists.size(); i++)
657 Inventory::Inventory()
661 Inventory::Inventory(const Inventory &other)
666 Inventory & Inventory::operator = (const Inventory &other)
669 for(u32 i=0; i<other.m_lists.size(); i++)
671 m_lists.push_back(new InventoryList(*other.m_lists[i]));
676 void Inventory::serialize(std::ostream &os) const
678 for(u32 i=0; i<m_lists.size(); i++)
680 InventoryList *list = m_lists[i];
681 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
685 os<<"EndInventory\n";
688 void Inventory::deSerialize(std::istream &is, IGameDef *gamedef)
695 std::getline(is, line, '\n');
697 std::istringstream iss(line);
700 std::getline(iss, name, ' ');
702 if(name == "EndInventory")
706 // This is a temporary backwards compatibility fix
707 else if(name == "end")
711 else if(name == "List")
713 std::string listname;
716 std::getline(iss, listname, ' ');
719 InventoryList *list = new InventoryList(listname, listsize);
720 list->deSerialize(is, gamedef);
722 m_lists.push_back(list);
726 throw SerializationError("Unknown inventory identifier");
731 InventoryList * Inventory::addList(const std::string &name, u32 size)
733 s32 i = getListIndex(name);
736 if(m_lists[i]->getSize() != size)
739 m_lists[i] = new InventoryList(name, size);
745 m_lists.push_back(new InventoryList(name, size));
746 return m_lists.getLast();
750 InventoryList * Inventory::getList(const std::string &name)
752 s32 i = getListIndex(name);
758 const InventoryList * Inventory::getList(const std::string &name) const
760 s32 i = getListIndex(name);
766 const s32 Inventory::getListIndex(const std::string &name) const
768 for(u32 i=0; i<m_lists.size(); i++)
770 if(m_lists[i]->getName() == name)
780 InventoryAction * InventoryAction::deSerialize(std::istream &is)
783 std::getline(is, type, ' ');
785 InventoryAction *a = NULL;
789 a = new IMoveAction(is);
795 static std::string describeC(const struct InventoryContext *c)
797 if(c->current_player == NULL)
798 return "current_player=NULL";
800 return std::string("current_player=") + c->current_player->getName();
803 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
805 Inventory *inv_from = mgr->getInventory(c, from_inv);
806 Inventory *inv_to = mgr->getInventory(c, to_inv);
809 infostream<<"IMoveAction::apply(): FAIL: source inventory not found: "
810 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
811 <<", to_inv=\""<<to_inv<<"\""<<std::endl;
815 infostream<<"IMoveAction::apply(): FAIL: destination inventory not found: "
816 "context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
817 <<", to_inv=\""<<to_inv<<"\""<<std::endl;
821 InventoryList *list_from = inv_from->getList(from_list);
822 InventoryList *list_to = inv_to->getList(to_list);
825 If a list doesn't exist or the source item doesn't exist
828 infostream<<"IMoveAction::apply(): FAIL: source list not found: "
829 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
830 <<", from_list=\""<<from_list<<"\""<<std::endl;
834 infostream<<"IMoveAction::apply(): FAIL: destination list not found: "
835 <<"context=["<<describeC(c)<<"], to_inv=\""<<to_inv<<"\""
836 <<", to_list=\""<<to_list<<"\""<<std::endl;
839 if(list_from->getItem(from_i) == NULL)
841 infostream<<"IMoveAction::apply(): FAIL: source item not found: "
842 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
843 <<", from_list=\""<<from_list<<"\""
844 <<" from_i="<<from_i<<std::endl;
848 If the source and the destination slots are the same
850 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
852 infostream<<"IMoveAction::apply(): FAIL: source and destination slots "
853 <<"are the same: inv=\""<<from_inv<<"\" list=\""<<from_list
854 <<"\" i="<<from_i<<std::endl;
858 // Take item from source list
859 InventoryItem *item1 = NULL;
861 item1 = list_from->changeItem(from_i, NULL);
863 item1 = list_from->takeItem(from_i, count);
865 // Try to add the item to destination list
866 InventoryItem *olditem = item1;
867 item1 = list_to->addItem(to_i, item1);
869 // If something is returned, the item was not fully added
872 // If olditem is returned, nothing was added.
873 bool nothing_added = (item1 == olditem);
875 // If something else is returned, part of the item was left unadded.
876 // Add the other part back to the source item
877 list_from->addItem(from_i, item1);
879 // If olditem is returned, nothing was added.
883 // Take item from source list
884 item1 = list_from->changeItem(from_i, NULL);
885 // Adding was not possible, swap the items.
886 InventoryItem *item2 = list_to->changeItem(to_i, item1);
887 // Put item from destination list to the source list
888 list_from->changeItem(from_i, item2);
892 mgr->inventoryModified(c, from_inv);
893 if(from_inv != to_inv)
894 mgr->inventoryModified(c, to_inv);
896 infostream<<"IMoveAction::apply(): moved at "
897 <<"["<<describeC(c)<<"]"
898 <<" from inv=\""<<from_inv<<"\""
899 <<" list=\""<<from_list<<"\""
901 <<" to inv=\""<<to_inv<<"\""
902 <<" list=\""<<to_list<<"\""
908 Craft checking system
911 bool ItemSpec::checkItem(const InventoryItem *item) const
913 if(type == ITEM_NONE)
921 // There should be an item
925 std::string itemname = item->getName();
927 if(type == ITEM_MATERIAL)
929 if(itemname != "MaterialItem")
931 MaterialItem *mitem = (MaterialItem*)item;
932 if(mitem->getMaterial() != num)
935 else if(type == ITEM_CRAFT)
937 if(itemname != "CraftItem")
939 CraftItem *mitem = (CraftItem*)item;
940 if(mitem->getSubName() != name)
943 else if(type == ITEM_TOOL)
948 else if(type == ITEM_MBO)
961 bool checkItemCombination(InventoryItem const * const *items, const ItemSpec *specs)
963 u16 items_min_x = 100;
964 u16 items_max_x = 100;
965 u16 items_min_y = 100;
966 u16 items_max_y = 100;
967 for(u16 y=0; y<3; y++)
968 for(u16 x=0; x<3; x++)
970 if(items[y*3 + x] == NULL)
972 if(items_min_x == 100 || x < items_min_x)
974 if(items_min_y == 100 || y < items_min_y)
976 if(items_max_x == 100 || x > items_max_x)
978 if(items_max_y == 100 || y > items_max_y)
981 // No items at all, just return false
982 if(items_min_x == 100)
985 u16 items_w = items_max_x - items_min_x + 1;
986 u16 items_h = items_max_y - items_min_y + 1;
988 u16 specs_min_x = 100;
989 u16 specs_max_x = 100;
990 u16 specs_min_y = 100;
991 u16 specs_max_y = 100;
992 for(u16 y=0; y<3; y++)
993 for(u16 x=0; x<3; x++)
995 if(specs[y*3 + x].type == ITEM_NONE)
997 if(specs_min_x == 100 || x < specs_min_x)
999 if(specs_min_y == 100 || y < specs_min_y)
1001 if(specs_max_x == 100 || x > specs_max_x)
1003 if(specs_max_y == 100 || y > specs_max_y)
1006 // No specs at all, just return false
1007 if(specs_min_x == 100)
1010 u16 specs_w = specs_max_x - specs_min_x + 1;
1011 u16 specs_h = specs_max_y - specs_min_y + 1;
1014 if(items_w != specs_w || items_h != specs_h)
1017 for(u16 y=0; y<specs_h; y++)
1018 for(u16 x=0; x<specs_w; x++)
1020 u16 items_x = items_min_x + x;
1021 u16 items_y = items_min_y + y;
1022 u16 specs_x = specs_min_x + x;
1023 u16 specs_y = specs_min_y + y;
1024 const InventoryItem *item = items[items_y * 3 + items_x];
1025 const ItemSpec &spec = specs[specs_y * 3 + specs_x];
1027 if(spec.checkItem(item) == false)