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"
43 InventoryItem::InventoryItem(u16 count)
48 InventoryItem::~InventoryItem()
52 content_t content_translate_from_19_to_internal(content_t c_from)
54 for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
56 if(trans_table_19[i][1] == c_from)
58 return trans_table_19[i][0];
64 InventoryItem* InventoryItem::deSerialize(std::istream &is)
66 DSTACK(__FUNCTION_NAME);
68 //is.imbue(std::locale("C"));
71 std::getline(is, name, ' ');
73 if(name == "MaterialItem")
75 // u16 reads directly as a number (u8 doesn't)
80 // Convert old materials
83 material = content_translate_from_19_to_internal(material);
85 if(material > MAX_CONTENT)
86 throw SerializationError("Too large material number");
87 return new MaterialItem(material, count);
89 else if(name == "MaterialItem2")
95 if(material > MAX_CONTENT)
96 throw SerializationError("Too large material number");
97 return new MaterialItem(material, count);
99 else if(name == "MBOItem")
101 std::string inventorystring;
102 std::getline(is, inventorystring, '|');
103 throw SerializationError("MBOItem not supported anymore");
105 else if(name == "CraftItem")
108 std::getline(is, subname, ' ');
111 return new CraftItem(subname, count);
113 else if(name == "ToolItem")
115 std::string toolname;
116 std::getline(is, toolname, ' ');
119 return new ToolItem(toolname, wear);
123 infostream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
124 throw SerializationError("Unknown InventoryItem name");
128 std::string InventoryItem::getItemString() {
130 std::ostringstream os(std::ios_base::binary);
135 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
140 pos.Y -= BS*0.25; // let it drop a bit
142 pos.X += BS*0.2*(float)myrand_range(-1000,1000)/1000.0;
143 pos.Z += BS*0.2*(float)myrand_range(-1000,1000)/1000.0;
145 ServerActiveObject *obj = new ItemSAO(env, pos, getItemString());
154 video::ITexture * MaterialItem::getImage() const
156 return content_features(m_content).inventory_texture;
160 bool MaterialItem::isCookable() const
162 return item_material_is_cookable(m_content);
165 InventoryItem *MaterialItem::createCookResult() const
167 return item_material_create_cook_result(m_content);
174 std::string ToolItem::getImageBasename() const
176 return tool_get_imagename(m_toolname);
184 video::ITexture * CraftItem::getImage() const
186 if(g_texturesource == NULL)
189 std::string name = item_craft_get_image_name(m_subname);
191 // Get such a texture
192 return g_texturesource->getTextureRaw(name);
196 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
199 ServerActiveObject *obj = item_craft_create_object(m_subname, env, pos);
203 return InventoryItem::createSAO(env, id, pos);
206 u16 CraftItem::getDropCount() const
209 s16 dc = item_craft_get_drop_count(m_subname);
213 return InventoryItem::getDropCount();
216 bool CraftItem::isCookable() const
218 return item_craft_is_cookable(m_subname);
221 InventoryItem *CraftItem::createCookResult() const
223 return item_craft_create_cook_result(m_subname);
226 bool CraftItem::use(ServerEnvironment *env, ServerActiveObject *user)
228 if(!item_craft_is_eatable(m_subname))
231 u16 result_count = getCount() - 1; // Eat one at a time
232 s16 hp_change = item_craft_eat_hp_change(m_subname);
233 s16 hp = user->getHP();
242 setCount(result_count);
250 InventoryList::InventoryList(std::string name, u32 size)
258 InventoryList::~InventoryList()
260 for(u32 i=0; i<m_items.size(); i++)
267 void InventoryList::clearItems()
269 for(u32 i=0; i<m_items.size(); i++)
277 for(u32 i=0; i<m_size; i++)
279 m_items.push_back(NULL);
285 void InventoryList::serialize(std::ostream &os) const
287 //os.imbue(std::locale("C"));
289 for(u32 i=0; i<m_items.size(); i++)
291 InventoryItem *item = m_items[i];
304 os<<"EndInventoryList\n";
307 void InventoryList::deSerialize(std::istream &is)
309 //is.imbue(std::locale("C"));
317 std::getline(is, line, '\n');
319 std::istringstream iss(line);
320 //iss.imbue(std::locale("C"));
323 std::getline(iss, name, ' ');
325 if(name == "EndInventoryList")
329 // This is a temporary backwards compatibility fix
330 else if(name == "end")
334 else if(name == "Item")
336 if(item_i > getSize() - 1)
337 throw SerializationError("too many items");
338 InventoryItem *item = InventoryItem::deSerialize(iss);
339 m_items[item_i++] = item;
341 else if(name == "Empty")
343 if(item_i > getSize() - 1)
344 throw SerializationError("too many items");
345 m_items[item_i++] = NULL;
349 throw SerializationError("Unknown inventory identifier");
354 InventoryList::InventoryList(const InventoryList &other)
357 Do this so that the items get cloned. Otherwise the pointers
358 in the array will just get copied.
363 InventoryList & InventoryList::operator = (const InventoryList &other)
365 m_name = other.m_name;
366 m_size = other.m_size;
368 for(u32 i=0; i<other.m_items.size(); i++)
370 InventoryItem *item = other.m_items[i];
373 m_items[i] = item->clone();
381 const std::string &InventoryList::getName() const
386 u32 InventoryList::getSize()
388 return m_items.size();
391 u32 InventoryList::getUsedSlots()
394 for(u32 i=0; i<m_items.size(); i++)
396 InventoryItem *item = m_items[i];
403 u32 InventoryList::getFreeSlots()
405 return getSize() - getUsedSlots();
408 const InventoryItem * InventoryList::getItem(u32 i) const
410 if(i > m_items.size() - 1)
415 InventoryItem * InventoryList::getItem(u32 i)
417 if(i > m_items.size() - 1)
422 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
424 assert(i < m_items.size());
426 InventoryItem *olditem = m_items[i];
427 m_items[i] = newitem;
432 void InventoryList::deleteItem(u32 i)
434 assert(i < m_items.size());
435 InventoryItem *item = changeItem(i, NULL);
440 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
446 First try to find if it could be added to some existing items
448 for(u32 i=0; i<m_items.size(); i++)
450 // Ignore empty slots
451 if(m_items[i] == NULL)
454 newitem = addItem(i, newitem);
456 return NULL; // All was eaten
460 Then try to add it to empty slots
462 for(u32 i=0; i<m_items.size(); i++)
464 // Ignore unempty slots
465 if(m_items[i] != NULL)
468 newitem = addItem(i, newitem);
470 return NULL; // All was eaten
477 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
484 // If it is an empty position, it's an easy job.
485 InventoryItem *to_item = getItem(i);
488 m_items[i] = newitem;
492 // If not addable, return the item
493 if(newitem->addableTo(to_item) == false)
496 // If the item fits fully in the slot, add counter and delete it
497 if(newitem->getCount() <= to_item->freeSpace())
499 to_item->add(newitem->getCount());
503 // Else the item does not fit fully. Add all that fits and return
507 u16 freespace = to_item->freeSpace();
508 to_item->add(freespace);
509 newitem->remove(freespace);
514 bool InventoryList::itemFits(const u32 i, const InventoryItem *newitem)
516 // If it is an empty position, it's an easy job.
517 const InventoryItem *to_item = getItem(i);
523 // If not addable, fail
524 if(newitem->addableTo(to_item) == false)
527 // If the item fits fully in the slot, pass
528 if(newitem->getCount() <= to_item->freeSpace())
536 bool InventoryList::roomForItem(const InventoryItem *item)
538 for(u32 i=0; i<m_items.size(); i++)
539 if(itemFits(i, item))
544 bool InventoryList::roomForCookedItem(const InventoryItem *item)
548 const InventoryItem *cook = item->createCookResult();
551 bool room = roomForItem(cook);
556 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
563 InventoryItem *item = getItem(i);
564 // If it is an empty position, return NULL
568 if(count >= item->getCount())
570 // Get the item by swapping NULL to its place
571 return changeItem(i, NULL);
575 InventoryItem *item2 = item->clone();
577 item2->setCount(count);
584 void InventoryList::decrementMaterials(u16 count)
586 for(u32 i=0; i<m_items.size(); i++)
588 InventoryItem *item = takeItem(i, count);
594 void InventoryList::print(std::ostream &o)
596 o<<"InventoryList:"<<std::endl;
597 for(u32 i=0; i<m_items.size(); i++)
599 InventoryItem *item = m_items[i];
613 Inventory::~Inventory()
618 void Inventory::clear()
620 for(u32 i=0; i<m_lists.size(); i++)
627 Inventory::Inventory()
631 Inventory::Inventory(const Inventory &other)
636 Inventory & Inventory::operator = (const Inventory &other)
639 for(u32 i=0; i<other.m_lists.size(); i++)
641 m_lists.push_back(new InventoryList(*other.m_lists[i]));
646 void Inventory::serialize(std::ostream &os) const
648 for(u32 i=0; i<m_lists.size(); i++)
650 InventoryList *list = m_lists[i];
651 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
655 os<<"EndInventory\n";
658 void Inventory::deSerialize(std::istream &is)
665 std::getline(is, line, '\n');
667 std::istringstream iss(line);
670 std::getline(iss, name, ' ');
672 if(name == "EndInventory")
676 // This is a temporary backwards compatibility fix
677 else if(name == "end")
681 else if(name == "List")
683 std::string listname;
686 std::getline(iss, listname, ' ');
689 InventoryList *list = new InventoryList(listname, listsize);
690 list->deSerialize(is);
692 m_lists.push_back(list);
696 throw SerializationError("Unknown inventory identifier");
701 InventoryList * Inventory::addList(const std::string &name, u32 size)
703 s32 i = getListIndex(name);
706 if(m_lists[i]->getSize() != size)
709 m_lists[i] = new InventoryList(name, size);
715 m_lists.push_back(new InventoryList(name, size));
716 return m_lists.getLast();
720 InventoryList * Inventory::getList(const std::string &name)
722 s32 i = getListIndex(name);
728 const InventoryList * Inventory::getList(const std::string &name) const
730 s32 i = getListIndex(name);
736 const s32 Inventory::getListIndex(const std::string &name) const
738 for(u32 i=0; i<m_lists.size(); i++)
740 if(m_lists[i]->getName() == name)
750 InventoryAction * InventoryAction::deSerialize(std::istream &is)
753 std::getline(is, type, ' ');
755 InventoryAction *a = NULL;
759 a = new IMoveAction(is);
765 static std::string describeC(const struct InventoryContext *c)
767 if(c->current_player == NULL)
768 return "current_player=NULL";
770 return std::string("current_player=") + c->current_player->getName();
773 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
775 Inventory *inv_from = mgr->getInventory(c, from_inv);
776 Inventory *inv_to = mgr->getInventory(c, to_inv);
779 infostream<<"IMoveAction::apply(): FAIL: source inventory not found: "
780 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
781 <<", to_inv=\""<<to_inv<<"\""<<std::endl;
785 infostream<<"IMoveAction::apply(): FAIL: destination inventory not found: "
786 "context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
787 <<", to_inv=\""<<to_inv<<"\""<<std::endl;
791 InventoryList *list_from = inv_from->getList(from_list);
792 InventoryList *list_to = inv_to->getList(to_list);
795 If a list doesn't exist or the source item doesn't exist
798 infostream<<"IMoveAction::apply(): FAIL: source list not found: "
799 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
800 <<", from_list=\""<<from_list<<"\""<<std::endl;
804 infostream<<"IMoveAction::apply(): FAIL: destination list not found: "
805 <<"context=["<<describeC(c)<<"], to_inv=\""<<to_inv<<"\""
806 <<", to_list=\""<<to_list<<"\""<<std::endl;
809 if(list_from->getItem(from_i) == NULL)
811 infostream<<"IMoveAction::apply(): FAIL: source item not found: "
812 <<"context=["<<describeC(c)<<"], from_inv=\""<<from_inv<<"\""
813 <<", from_list=\""<<from_list<<"\""
814 <<" from_i="<<from_i<<std::endl;
818 If the source and the destination slots are the same
820 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
822 infostream<<"IMoveAction::apply(): FAIL: source and destination slots "
823 <<"are the same: inv=\""<<from_inv<<"\" list=\""<<from_list
824 <<"\" i="<<from_i<<std::endl;
828 // Take item from source list
829 InventoryItem *item1 = NULL;
831 item1 = list_from->changeItem(from_i, NULL);
833 item1 = list_from->takeItem(from_i, count);
835 // Try to add the item to destination list
836 InventoryItem *olditem = item1;
837 item1 = list_to->addItem(to_i, item1);
839 // If something is returned, the item was not fully added
842 // If olditem is returned, nothing was added.
843 bool nothing_added = (item1 == olditem);
845 // If something else is returned, part of the item was left unadded.
846 // Add the other part back to the source item
847 list_from->addItem(from_i, item1);
849 // If olditem is returned, nothing was added.
853 // Take item from source list
854 item1 = list_from->changeItem(from_i, NULL);
855 // Adding was not possible, swap the items.
856 InventoryItem *item2 = list_to->changeItem(to_i, item1);
857 // Put item from destination list to the source list
858 list_from->changeItem(from_i, item2);
862 mgr->inventoryModified(c, from_inv);
863 if(from_inv != to_inv)
864 mgr->inventoryModified(c, to_inv);
866 infostream<<"IMoveAction::apply(): moved at "
867 <<"["<<describeC(c)<<"]"
868 <<" from inv=\""<<from_inv<<"\""
869 <<" list=\""<<from_list<<"\""
871 <<" to inv=\""<<to_inv<<"\""
872 <<" list=\""<<to_list<<"\""
878 Craft checking system
881 bool ItemSpec::checkItem(const InventoryItem *item) const
883 if(type == ITEM_NONE)
891 // There should be an item
895 std::string itemname = item->getName();
897 if(type == ITEM_MATERIAL)
899 if(itemname != "MaterialItem")
901 MaterialItem *mitem = (MaterialItem*)item;
902 if(mitem->getMaterial() != num)
905 else if(type == ITEM_CRAFT)
907 if(itemname != "CraftItem")
909 CraftItem *mitem = (CraftItem*)item;
910 if(mitem->getSubName() != name)
913 else if(type == ITEM_TOOL)
918 else if(type == ITEM_MBO)
931 bool checkItemCombination(InventoryItem const * const *items, const ItemSpec *specs)
933 u16 items_min_x = 100;
934 u16 items_max_x = 100;
935 u16 items_min_y = 100;
936 u16 items_max_y = 100;
937 for(u16 y=0; y<3; y++)
938 for(u16 x=0; x<3; x++)
940 if(items[y*3 + x] == NULL)
942 if(items_min_x == 100 || x < items_min_x)
944 if(items_min_y == 100 || y < items_min_y)
946 if(items_max_x == 100 || x > items_max_x)
948 if(items_max_y == 100 || y > items_max_y)
951 // No items at all, just return false
952 if(items_min_x == 100)
955 u16 items_w = items_max_x - items_min_x + 1;
956 u16 items_h = items_max_y - items_min_y + 1;
958 u16 specs_min_x = 100;
959 u16 specs_max_x = 100;
960 u16 specs_min_y = 100;
961 u16 specs_max_y = 100;
962 for(u16 y=0; y<3; y++)
963 for(u16 x=0; x<3; x++)
965 if(specs[y*3 + x].type == ITEM_NONE)
967 if(specs_min_x == 100 || x < specs_min_x)
969 if(specs_min_y == 100 || y < specs_min_y)
971 if(specs_max_x == 100 || x > specs_max_x)
973 if(specs_max_y == 100 || y > specs_max_y)
976 // No specs at all, just return false
977 if(specs_min_x == 100)
980 u16 specs_w = specs_max_x - specs_min_x + 1;
981 u16 specs_h = specs_max_y - specs_min_y + 1;
984 if(items_w != specs_w || items_h != specs_h)
987 for(u16 y=0; y<specs_h; y++)
988 for(u16 x=0; x<specs_w; x++)
990 u16 items_x = items_min_x + x;
991 u16 items_y = items_min_y + y;
992 u16 specs_x = specs_min_x + x;
993 u16 specs_y = specs_min_y + y;
994 const InventoryItem *item = items[items_y * 3 + items_x];
995 const ItemSpec &spec = specs[specs_y * 3 + specs_x];
997 if(spec.checkItem(item) == false)