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"
33 #include "content_sao.h"
36 #include "mapnode_contentfeatures.h"
42 InventoryItem::InventoryItem(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)
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(material, count);
88 else if(name == "MaterialItem2")
94 if(material > MAX_CONTENT)
95 throw SerializationError("Too large material number");
96 return new MaterialItem(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(subname, count);
112 else if(name == "ToolItem")
114 std::string toolname;
115 std::getline(is, toolname, ' ');
118 return new ToolItem(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() const
155 return content_features(m_content).inventory_texture;
159 bool MaterialItem::isCookable() const
161 return item_material_is_cookable(m_content);
164 InventoryItem *MaterialItem::createCookResult() const
166 return item_material_create_cook_result(m_content);
174 video::ITexture * CraftItem::getImage() const
176 if(g_texturesource == NULL)
179 std::string name = item_craft_get_image_name(m_subname);
181 // Get such a texture
182 return g_texturesource->getTextureRaw(name);
186 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
189 ServerActiveObject *obj = item_craft_create_object(m_subname, env, pos);
193 return InventoryItem::createSAO(env, id, pos);
196 u16 CraftItem::getDropCount() const
199 s16 dc = item_craft_get_drop_count(m_subname);
203 return InventoryItem::getDropCount();
206 bool CraftItem::isCookable() const
208 return item_craft_is_cookable(m_subname);
211 InventoryItem *CraftItem::createCookResult() const
213 return item_craft_create_cook_result(m_subname);
216 bool CraftItem::use(ServerEnvironment *env, ServerActiveObject *user)
218 if(!item_craft_is_eatable(m_subname))
221 u16 result_count = getCount() - 1; // Eat one at a time
222 s16 hp_change = item_craft_eat_hp_change(m_subname);
223 s16 hp = user->getHP();
232 setCount(result_count);
240 InventoryList::InventoryList(std::string name, u32 size)
248 InventoryList::~InventoryList()
250 for(u32 i=0; i<m_items.size(); i++)
257 void InventoryList::clearItems()
259 for(u32 i=0; i<m_items.size(); i++)
267 for(u32 i=0; i<m_size; i++)
269 m_items.push_back(NULL);
275 void InventoryList::serialize(std::ostream &os) const
277 //os.imbue(std::locale("C"));
279 for(u32 i=0; i<m_items.size(); i++)
281 InventoryItem *item = m_items[i];
294 os<<"EndInventoryList\n";
297 void InventoryList::deSerialize(std::istream &is)
299 //is.imbue(std::locale("C"));
307 std::getline(is, line, '\n');
309 std::istringstream iss(line);
310 //iss.imbue(std::locale("C"));
313 std::getline(iss, name, ' ');
315 if(name == "EndInventoryList")
319 // This is a temporary backwards compatibility fix
320 else if(name == "end")
324 else if(name == "Item")
326 if(item_i > getSize() - 1)
327 throw SerializationError("too many items");
328 InventoryItem *item = InventoryItem::deSerialize(iss);
329 m_items[item_i++] = item;
331 else if(name == "Empty")
333 if(item_i > getSize() - 1)
334 throw SerializationError("too many items");
335 m_items[item_i++] = NULL;
339 throw SerializationError("Unknown inventory identifier");
344 InventoryList::InventoryList(const InventoryList &other)
347 Do this so that the items get cloned. Otherwise the pointers
348 in the array will just get copied.
353 InventoryList & InventoryList::operator = (const InventoryList &other)
355 m_name = other.m_name;
356 m_size = other.m_size;
358 for(u32 i=0; i<other.m_items.size(); i++)
360 InventoryItem *item = other.m_items[i];
363 m_items[i] = item->clone();
371 const std::string &InventoryList::getName() const
376 u32 InventoryList::getSize()
378 return m_items.size();
381 u32 InventoryList::getUsedSlots()
384 for(u32 i=0; i<m_items.size(); i++)
386 InventoryItem *item = m_items[i];
393 u32 InventoryList::getFreeSlots()
395 return getSize() - getUsedSlots();
398 const InventoryItem * InventoryList::getItem(u32 i) const
400 if(i > m_items.size() - 1)
405 InventoryItem * InventoryList::getItem(u32 i)
407 if(i > m_items.size() - 1)
412 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
414 assert(i < m_items.size());
416 InventoryItem *olditem = m_items[i];
417 m_items[i] = newitem;
422 void InventoryList::deleteItem(u32 i)
424 assert(i < m_items.size());
425 InventoryItem *item = changeItem(i, NULL);
430 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
436 First try to find if it could be added to some existing items
438 for(u32 i=0; i<m_items.size(); i++)
440 // Ignore empty slots
441 if(m_items[i] == NULL)
444 newitem = addItem(i, newitem);
446 return NULL; // All was eaten
450 Then try to add it to empty slots
452 for(u32 i=0; i<m_items.size(); i++)
454 // Ignore unempty slots
455 if(m_items[i] != NULL)
458 newitem = addItem(i, newitem);
460 return NULL; // All was eaten
467 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
474 // If it is an empty position, it's an easy job.
475 InventoryItem *to_item = getItem(i);
478 m_items[i] = newitem;
482 // If not addable, return the item
483 if(newitem->addableTo(to_item) == false)
486 // If the item fits fully in the slot, add counter and delete it
487 if(newitem->getCount() <= to_item->freeSpace())
489 to_item->add(newitem->getCount());
493 // Else the item does not fit fully. Add all that fits and return
497 u16 freespace = to_item->freeSpace();
498 to_item->add(freespace);
499 newitem->remove(freespace);
504 bool InventoryList::itemFits(const u32 i, const InventoryItem *newitem)
506 // If it is an empty position, it's an easy job.
507 const InventoryItem *to_item = getItem(i);
513 // If not addable, fail
514 if(newitem->addableTo(to_item) == false)
517 // If the item fits fully in the slot, pass
518 if(newitem->getCount() <= to_item->freeSpace())
526 bool InventoryList::roomForItem(const InventoryItem *item)
528 for(u32 i=0; i<m_items.size(); i++)
529 if(itemFits(i, item))
534 bool InventoryList::roomForCookedItem(const InventoryItem *item)
538 const InventoryItem *cook = item->createCookResult();
541 bool room = roomForItem(cook);
546 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
553 InventoryItem *item = getItem(i);
554 // If it is an empty position, return NULL
558 if(count >= item->getCount())
560 // Get the item by swapping NULL to its place
561 return changeItem(i, NULL);
565 InventoryItem *item2 = item->clone();
567 item2->setCount(count);
574 void InventoryList::decrementMaterials(u16 count)
576 for(u32 i=0; i<m_items.size(); i++)
578 InventoryItem *item = takeItem(i, count);
584 void InventoryList::print(std::ostream &o)
586 o<<"InventoryList:"<<std::endl;
587 for(u32 i=0; i<m_items.size(); i++)
589 InventoryItem *item = m_items[i];
603 Inventory::~Inventory()
608 void Inventory::clear()
610 for(u32 i=0; i<m_lists.size(); i++)
617 Inventory::Inventory()
621 Inventory::Inventory(const Inventory &other)
626 Inventory & Inventory::operator = (const Inventory &other)
629 for(u32 i=0; i<other.m_lists.size(); i++)
631 m_lists.push_back(new InventoryList(*other.m_lists[i]));
636 void Inventory::serialize(std::ostream &os) const
638 for(u32 i=0; i<m_lists.size(); i++)
640 InventoryList *list = m_lists[i];
641 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
645 os<<"EndInventory\n";
648 void Inventory::deSerialize(std::istream &is)
655 std::getline(is, line, '\n');
657 std::istringstream iss(line);
660 std::getline(iss, name, ' ');
662 if(name == "EndInventory")
666 // This is a temporary backwards compatibility fix
667 else if(name == "end")
671 else if(name == "List")
673 std::string listname;
676 std::getline(iss, listname, ' ');
679 InventoryList *list = new InventoryList(listname, listsize);
680 list->deSerialize(is);
682 m_lists.push_back(list);
686 throw SerializationError("Unknown inventory identifier");
691 InventoryList * Inventory::addList(const std::string &name, u32 size)
693 s32 i = getListIndex(name);
696 if(m_lists[i]->getSize() != size)
699 m_lists[i] = new InventoryList(name, size);
705 m_lists.push_back(new InventoryList(name, size));
706 return m_lists.getLast();
710 InventoryList * Inventory::getList(const std::string &name)
712 s32 i = getListIndex(name);
718 const InventoryList * Inventory::getList(const std::string &name) const
720 s32 i = getListIndex(name);
726 const s32 Inventory::getListIndex(const std::string &name) const
728 for(u32 i=0; i<m_lists.size(); i++)
730 if(m_lists[i]->getName() == name)
740 InventoryAction * InventoryAction::deSerialize(std::istream &is)
743 std::getline(is, type, ' ');
745 InventoryAction *a = NULL;
749 a = new IMoveAction(is);
755 static std::string describeC(const struct InventoryContext *c)
757 if(c->current_player == NULL)
758 return "current_player=NULL";
760 return std::string("current_player=") + c->current_player->getName();
763 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
765 Inventory *inv_from = mgr->getInventory(c, from_inv);
766 Inventory *inv_to = mgr->getInventory(c, to_inv);
769 infostream<<"IMoveAction::apply(): FAIL: source inventory not found: "
770 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
771 <<", to_inv=\""<<to_inv<<"\""<<std::endl;
775 infostream<<"IMoveAction::apply(): FAIL: destination inventory not found: "
776 "context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
777 <<", to_inv=\""<<to_inv<<"\""<<std::endl;
781 InventoryList *list_from = inv_from->getList(from_list);
782 InventoryList *list_to = inv_to->getList(to_list);
785 If a list doesn't exist or the source item doesn't exist
788 infostream<<"IMoveAction::apply(): FAIL: source list not found: "
789 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
790 <<", from_list=\""<<from_list<<"\""<<std::endl;
794 infostream<<"IMoveAction::apply(): FAIL: destination list not found: "
795 <<"context=["<<describeC(c)<<"], to_inv=\""<<to_inv<<"\""
796 <<", to_list=\""<<to_list<<"\""<<std::endl;
799 if(list_from->getItem(from_i) == NULL)
801 infostream<<"IMoveAction::apply(): FAIL: source item not found: "
802 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
803 <<", from_list=\""<<from_list<<"\""
804 <<" from_i="<<from_i<<std::endl;
808 If the source and the destination slots are the same
810 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
812 infostream<<"IMoveAction::apply(): FAIL: source and destination slots "
813 <<"are the same: inv=\""<<from_inv<<"\" list=\""<<from_list
814 <<"\" i="<<from_i<<std::endl;
818 // Take item from source list
819 InventoryItem *item1 = NULL;
821 item1 = list_from->changeItem(from_i, NULL);
823 item1 = list_from->takeItem(from_i, count);
825 // Try to add the item to destination list
826 InventoryItem *olditem = item1;
827 item1 = list_to->addItem(to_i, item1);
829 // If something is returned, the item was not fully added
832 // If olditem is returned, nothing was added.
833 bool nothing_added = (item1 == olditem);
835 // If something else is returned, part of the item was left unadded.
836 // Add the other part back to the source item
837 list_from->addItem(from_i, item1);
839 // If olditem is returned, nothing was added.
843 // Take item from source list
844 item1 = list_from->changeItem(from_i, NULL);
845 // Adding was not possible, swap the items.
846 InventoryItem *item2 = list_to->changeItem(to_i, item1);
847 // Put item from destination list to the source list
848 list_from->changeItem(from_i, item2);
852 mgr->inventoryModified(c, from_inv);
853 if(from_inv != to_inv)
854 mgr->inventoryModified(c, to_inv);
856 infostream<<"IMoveAction::apply(): moved at "
857 <<"["<<describeC(c)<<"]"
858 <<" from inv=\""<<from_inv<<"\""
859 <<" list=\""<<from_list<<"\""
861 <<" to inv=\""<<to_inv<<"\""
862 <<" list=\""<<to_list<<"\""
868 Craft checking system
871 bool ItemSpec::checkItem(const InventoryItem *item) const
873 if(type == ITEM_NONE)
881 // There should be an item
885 std::string itemname = item->getName();
887 if(type == ITEM_MATERIAL)
889 if(itemname != "MaterialItem")
891 MaterialItem *mitem = (MaterialItem*)item;
892 if(mitem->getMaterial() != num)
895 else if(type == ITEM_CRAFT)
897 if(itemname != "CraftItem")
899 CraftItem *mitem = (CraftItem*)item;
900 if(mitem->getSubName() != name)
903 else if(type == ITEM_TOOL)
908 else if(type == ITEM_MBO)
921 bool checkItemCombination(InventoryItem const * const *items, const ItemSpec *specs)
923 u16 items_min_x = 100;
924 u16 items_max_x = 100;
925 u16 items_min_y = 100;
926 u16 items_max_y = 100;
927 for(u16 y=0; y<3; y++)
928 for(u16 x=0; x<3; x++)
930 if(items[y*3 + x] == NULL)
932 if(items_min_x == 100 || x < items_min_x)
934 if(items_min_y == 100 || y < items_min_y)
936 if(items_max_x == 100 || x > items_max_x)
938 if(items_max_y == 100 || y > items_max_y)
941 // No items at all, just return false
942 if(items_min_x == 100)
945 u16 items_w = items_max_x - items_min_x + 1;
946 u16 items_h = items_max_y - items_min_y + 1;
948 u16 specs_min_x = 100;
949 u16 specs_max_x = 100;
950 u16 specs_min_y = 100;
951 u16 specs_max_y = 100;
952 for(u16 y=0; y<3; y++)
953 for(u16 x=0; x<3; x++)
955 if(specs[y*3 + x].type == ITEM_NONE)
957 if(specs_min_x == 100 || x < specs_min_x)
959 if(specs_min_y == 100 || y < specs_min_y)
961 if(specs_max_x == 100 || x > specs_max_x)
963 if(specs_max_y == 100 || y > specs_max_y)
966 // No specs at all, just return false
967 if(specs_min_x == 100)
970 u16 specs_w = specs_max_x - specs_min_x + 1;
971 u16 specs_h = specs_max_y - specs_min_y + 1;
974 if(items_w != specs_w || items_h != specs_h)
977 for(u16 y=0; y<specs_h; y++)
978 for(u16 x=0; x<specs_w; x++)
980 u16 items_x = items_min_x + x;
981 u16 items_y = items_min_y + y;
982 u16 specs_x = specs_min_x + x;
983 u16 specs_y = specs_min_y + y;
984 const InventoryItem *item = items[items_y * 3 + items_x];
985 const ItemSpec &spec = specs[specs_y * 3 + specs_x];
987 if(spec.checkItem(item) == false)