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"
40 InventoryItem::InventoryItem(u16 count)
45 InventoryItem::~InventoryItem()
49 content_t content_translate_from_19_to_internal(content_t c_from)
51 for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
53 if(trans_table_19[i][1] == c_from)
55 return trans_table_19[i][0];
61 InventoryItem* InventoryItem::deSerialize(std::istream &is)
63 DSTACK(__FUNCTION_NAME);
65 //is.imbue(std::locale("C"));
68 std::getline(is, name, ' ');
70 if(name == "MaterialItem")
72 // u16 reads directly as a number (u8 doesn't)
77 // Convert old materials
80 material = content_translate_from_19_to_internal(material);
82 if(material > MAX_CONTENT)
83 throw SerializationError("Too large material number");
84 return new MaterialItem(material, count);
86 else if(name == "MaterialItem2")
92 if(material > MAX_CONTENT)
93 throw SerializationError("Too large material number");
94 return new MaterialItem(material, count);
96 else if(name == "MBOItem")
98 std::string inventorystring;
99 std::getline(is, inventorystring, '|');
100 return new MapBlockObjectItem(inventorystring);
102 else if(name == "CraftItem")
105 std::getline(is, subname, ' ');
108 return new CraftItem(subname, count);
110 else if(name == "ToolItem")
112 std::string toolname;
113 std::getline(is, toolname, ' ');
116 return new ToolItem(toolname, wear);
120 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
121 throw SerializationError("Unknown InventoryItem name");
125 std::string InventoryItem::getItemString() {
127 std::ostringstream os(std::ios_base::binary);
132 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
138 ServerActiveObject *obj = new ItemSAO(env, 0, pos, getItemString());
146 bool MaterialItem::isCookable() const
148 return item_material_is_cookable(m_content);
151 InventoryItem *MaterialItem::createCookResult() const
153 return item_material_create_cook_result(m_content);
161 video::ITexture * CraftItem::getImage() const
163 if(g_texturesource == NULL)
166 std::string name = item_craft_get_image_name(m_subname);
168 // Get such a texture
169 return g_texturesource->getTextureRaw(name);
173 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
176 ServerActiveObject *obj = item_craft_create_object(m_subname, env, id, pos);
180 return InventoryItem::createSAO(env, id, pos);
183 u16 CraftItem::getDropCount() const
186 s16 dc = item_craft_get_drop_count(m_subname);
190 return InventoryItem::getDropCount();
193 bool CraftItem::isCookable() const
195 return item_craft_is_cookable(m_subname);
198 InventoryItem *CraftItem::createCookResult() const
200 return item_craft_create_cook_result(m_subname);
203 bool CraftItem::use(ServerEnvironment *env, Player *player)
205 if(item_craft_is_eatable(m_subname))
207 u16 result_count = getCount() - 1; // Eat one at a time
208 s16 hp_change = item_craft_eat_hp_change(m_subname);
209 if(player->hp + hp_change > 20)
212 player->hp += hp_change;
217 setCount(result_count);
223 MapBlockObjectItem DEPRECATED
227 video::ITexture * MapBlockObjectItem::getImage() const
229 if(m_inventorystring.substr(0,3) == "Rat")
230 return g_texturesource->getTextureRaw("rat.png");
232 if(m_inventorystring.substr(0,4) == "Sign")
233 return g_texturesource->getTextureRaw("sign.png");
238 std::string MapBlockObjectItem::getText()
240 if(m_inventorystring.substr(0,3) == "Rat")
243 if(m_inventorystring.substr(0,4) == "Sign")
249 MapBlockObject * MapBlockObjectItem::createObject
250 (v3f pos, f32 player_yaw, f32 player_pitch)
252 std::istringstream is(m_inventorystring);
254 std::getline(is, name, ' ');
260 else if(name == "Sign")
263 std::getline(is, text, '|');
264 SignObject *obj = new SignObject(NULL, -1, pos);
266 obj->setYaw(-player_yaw);
269 else if(name == "Rat")
271 RatObject *obj = new RatObject(NULL, -1, pos);
274 else if(name == "ItemObj")
277 Now we are an inventory item containing the serialization
278 string of an object that contains the serialization
279 string of an inventory item. Fuck this.
282 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
283 <<"because an item-object should never be inside "
284 <<"an object-item."<<std::endl;
297 InventoryList::InventoryList(std::string name, u32 size)
305 InventoryList::~InventoryList()
307 for(u32 i=0; i<m_items.size(); i++)
314 void InventoryList::clearItems()
316 for(u32 i=0; i<m_items.size(); i++)
324 for(u32 i=0; i<m_size; i++)
326 m_items.push_back(NULL);
332 void InventoryList::serialize(std::ostream &os) const
334 //os.imbue(std::locale("C"));
336 for(u32 i=0; i<m_items.size(); i++)
338 InventoryItem *item = m_items[i];
351 os<<"EndInventoryList\n";
354 void InventoryList::deSerialize(std::istream &is)
356 //is.imbue(std::locale("C"));
364 std::getline(is, line, '\n');
366 std::istringstream iss(line);
367 //iss.imbue(std::locale("C"));
370 std::getline(iss, name, ' ');
372 if(name == "EndInventoryList")
376 // This is a temporary backwards compatibility fix
377 else if(name == "end")
381 else if(name == "Item")
383 if(item_i > getSize() - 1)
384 throw SerializationError("too many items");
385 InventoryItem *item = InventoryItem::deSerialize(iss);
386 m_items[item_i++] = item;
388 else if(name == "Empty")
390 if(item_i > getSize() - 1)
391 throw SerializationError("too many items");
392 m_items[item_i++] = NULL;
396 throw SerializationError("Unknown inventory identifier");
401 InventoryList::InventoryList(const InventoryList &other)
404 Do this so that the items get cloned. Otherwise the pointers
405 in the array will just get copied.
410 InventoryList & InventoryList::operator = (const InventoryList &other)
412 m_name = other.m_name;
413 m_size = other.m_size;
415 for(u32 i=0; i<other.m_items.size(); i++)
417 InventoryItem *item = other.m_items[i];
420 m_items[i] = item->clone();
428 const std::string &InventoryList::getName() const
433 u32 InventoryList::getSize()
435 return m_items.size();
438 u32 InventoryList::getUsedSlots()
441 for(u32 i=0; i<m_items.size(); i++)
443 InventoryItem *item = m_items[i];
450 u32 InventoryList::getFreeSlots()
452 return getSize() - getUsedSlots();
455 const InventoryItem * InventoryList::getItem(u32 i) const
457 if(i > m_items.size() - 1)
462 InventoryItem * InventoryList::getItem(u32 i)
464 if(i > m_items.size() - 1)
469 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
471 assert(i < m_items.size());
473 InventoryItem *olditem = m_items[i];
474 m_items[i] = newitem;
479 void InventoryList::deleteItem(u32 i)
481 assert(i < m_items.size());
482 InventoryItem *item = changeItem(i, NULL);
487 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
493 First try to find if it could be added to some existing items
495 for(u32 i=0; i<m_items.size(); i++)
497 // Ignore empty slots
498 if(m_items[i] == NULL)
501 newitem = addItem(i, newitem);
503 return NULL; // All was eaten
507 Then try to add it to empty slots
509 for(u32 i=0; i<m_items.size(); i++)
511 // Ignore unempty slots
512 if(m_items[i] != NULL)
515 newitem = addItem(i, newitem);
517 return NULL; // All was eaten
524 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
531 // If it is an empty position, it's an easy job.
532 InventoryItem *to_item = getItem(i);
535 m_items[i] = newitem;
539 // If not addable, return the item
540 if(newitem->addableTo(to_item) == false)
543 // If the item fits fully in the slot, add counter and delete it
544 if(newitem->getCount() <= to_item->freeSpace())
546 to_item->add(newitem->getCount());
550 // Else the item does not fit fully. Add all that fits and return
554 u16 freespace = to_item->freeSpace();
555 to_item->add(freespace);
556 newitem->remove(freespace);
561 bool InventoryList::itemFits(const u32 i, const InventoryItem *newitem)
563 // If it is an empty position, it's an easy job.
564 const InventoryItem *to_item = getItem(i);
570 // If not addable, fail
571 if(newitem->addableTo(to_item) == false)
574 // If the item fits fully in the slot, pass
575 if(newitem->getCount() <= to_item->freeSpace())
583 bool InventoryList::roomForItem(const InventoryItem *item)
585 for(u32 i=0; i<m_items.size(); i++)
586 if(itemFits(i, item))
591 bool InventoryList::roomForCookedItem(const InventoryItem *item)
595 const InventoryItem *cook = item->createCookResult();
598 bool room = roomForItem(cook);
603 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
610 InventoryItem *item = getItem(i);
611 // If it is an empty position, return NULL
615 if(count >= item->getCount())
617 // Get the item by swapping NULL to its place
618 return changeItem(i, NULL);
622 InventoryItem *item2 = item->clone();
624 item2->setCount(count);
631 void InventoryList::decrementMaterials(u16 count)
633 for(u32 i=0; i<m_items.size(); i++)
635 InventoryItem *item = takeItem(i, count);
641 void InventoryList::print(std::ostream &o)
643 o<<"InventoryList:"<<std::endl;
644 for(u32 i=0; i<m_items.size(); i++)
646 InventoryItem *item = m_items[i];
660 Inventory::~Inventory()
665 void Inventory::clear()
667 for(u32 i=0; i<m_lists.size(); i++)
674 Inventory::Inventory()
678 Inventory::Inventory(const Inventory &other)
683 Inventory & Inventory::operator = (const Inventory &other)
686 for(u32 i=0; i<other.m_lists.size(); i++)
688 m_lists.push_back(new InventoryList(*other.m_lists[i]));
693 void Inventory::serialize(std::ostream &os) const
695 for(u32 i=0; i<m_lists.size(); i++)
697 InventoryList *list = m_lists[i];
698 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
702 os<<"EndInventory\n";
705 void Inventory::deSerialize(std::istream &is)
712 std::getline(is, line, '\n');
714 std::istringstream iss(line);
717 std::getline(iss, name, ' ');
719 if(name == "EndInventory")
723 // This is a temporary backwards compatibility fix
724 else if(name == "end")
728 else if(name == "List")
730 std::string listname;
733 std::getline(iss, listname, ' ');
736 InventoryList *list = new InventoryList(listname, listsize);
737 list->deSerialize(is);
739 m_lists.push_back(list);
743 throw SerializationError("Unknown inventory identifier");
748 InventoryList * Inventory::addList(const std::string &name, u32 size)
750 s32 i = getListIndex(name);
753 if(m_lists[i]->getSize() != size)
756 m_lists[i] = new InventoryList(name, size);
762 m_lists.push_back(new InventoryList(name, size));
763 return m_lists.getLast();
767 InventoryList * Inventory::getList(const std::string &name)
769 s32 i = getListIndex(name);
775 const InventoryList * Inventory::getList(const std::string &name) const
777 s32 i = getListIndex(name);
783 const s32 Inventory::getListIndex(const std::string &name) const
785 for(u32 i=0; i<m_lists.size(); i++)
787 if(m_lists[i]->getName() == name)
797 InventoryAction * InventoryAction::deSerialize(std::istream &is)
800 std::getline(is, type, ' ');
802 InventoryAction *a = NULL;
806 a = new IMoveAction(is);
812 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
816 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
817 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
818 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
820 Inventory *inv_from = mgr->getInventory(c, from_inv);
821 Inventory *inv_to = mgr->getInventory(c, to_inv);
823 if(!inv_from || !inv_to)
825 dstream<<__FUNCTION_NAME<<": Operation not allowed "
826 <<"(inventories not found)"<<std::endl;
830 InventoryList *list_from = inv_from->getList(from_list);
831 InventoryList *list_to = inv_to->getList(to_list);
833 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
836 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
839 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
843 If a list doesn't exist or the source item doesn't exist
845 if(!list_from || !list_to)
847 dstream<<__FUNCTION_NAME<<": Operation not allowed "
848 <<"(a list doesn't exist)"
852 if(list_from->getItem(from_i) == NULL)
854 dstream<<__FUNCTION_NAME<<": Operation not allowed "
855 <<"(the source item doesn't exist)"
860 If the source and the destination slots are the same
862 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
864 dstream<<__FUNCTION_NAME<<": Operation not allowed "
865 <<"(source and the destination slots are the same)"<<std::endl;
869 // Take item from source list
870 InventoryItem *item1 = NULL;
872 item1 = list_from->changeItem(from_i, NULL);
874 item1 = list_from->takeItem(from_i, count);
876 // Try to add the item to destination list
877 InventoryItem *olditem = item1;
878 item1 = list_to->addItem(to_i, item1);
880 // If something is returned, the item was not fully added
883 // If olditem is returned, nothing was added.
884 bool nothing_added = (item1 == olditem);
886 // If something else is returned, part of the item was left unadded.
887 // Add the other part back to the source item
888 list_from->addItem(from_i, item1);
890 // If olditem is returned, nothing was added.
894 // Take item from source list
895 item1 = list_from->changeItem(from_i, NULL);
896 // Adding was not possible, swap the items.
897 InventoryItem *item2 = list_to->changeItem(to_i, item1);
898 // Put item from destination list to the source list
899 list_from->changeItem(from_i, item2);
903 mgr->inventoryModified(c, from_inv);
904 if(from_inv != to_inv)
905 mgr->inventoryModified(c, to_inv);
910 Craft checking system
913 bool ItemSpec::checkItem(const InventoryItem *item) const
915 if(type == ITEM_NONE)
923 // There should be an item
927 std::string itemname = item->getName();
929 if(type == ITEM_MATERIAL)
931 if(itemname != "MaterialItem")
933 MaterialItem *mitem = (MaterialItem*)item;
934 if(mitem->getMaterial() != num)
937 else if(type == ITEM_CRAFT)
939 if(itemname != "CraftItem")
941 CraftItem *mitem = (CraftItem*)item;
942 if(mitem->getSubName() != name)
945 else if(type == ITEM_TOOL)
950 else if(type == ITEM_MBO)
963 bool checkItemCombination(InventoryItem const * const *items, const ItemSpec *specs)
965 u16 items_min_x = 100;
966 u16 items_max_x = 100;
967 u16 items_min_y = 100;
968 u16 items_max_y = 100;
969 for(u16 y=0; y<3; y++)
970 for(u16 x=0; x<3; x++)
972 if(items[y*3 + x] == NULL)
974 if(items_min_x == 100 || x < items_min_x)
976 if(items_min_y == 100 || y < items_min_y)
978 if(items_max_x == 100 || x > items_max_x)
980 if(items_max_y == 100 || y > items_max_y)
983 // No items at all, just return false
984 if(items_min_x == 100)
987 u16 items_w = items_max_x - items_min_x + 1;
988 u16 items_h = items_max_y - items_min_y + 1;
990 u16 specs_min_x = 100;
991 u16 specs_max_x = 100;
992 u16 specs_min_y = 100;
993 u16 specs_max_y = 100;
994 for(u16 y=0; y<3; y++)
995 for(u16 x=0; x<3; x++)
997 if(specs[y*3 + x].type == ITEM_NONE)
999 if(specs_min_x == 100 || x < specs_min_x)
1001 if(specs_min_y == 100 || y < specs_min_y)
1003 if(specs_max_x == 100 || x > specs_max_x)
1005 if(specs_max_y == 100 || y > specs_max_y)
1008 // No specs at all, just return false
1009 if(specs_min_x == 100)
1012 u16 specs_w = specs_max_x - specs_min_x + 1;
1013 u16 specs_h = specs_max_y - specs_min_y + 1;
1016 if(items_w != specs_w || items_h != specs_h)
1019 for(u16 y=0; y<specs_h; y++)
1020 for(u16 x=0; x<specs_w; x++)
1022 u16 items_x = items_min_x + x;
1023 u16 items_y = items_min_y + y;
1024 u16 specs_x = specs_min_x + x;
1025 u16 specs_y = specs_min_y + y;
1026 const InventoryItem *item = items[items_y * 3 + items_x];
1027 const ItemSpec &spec = specs[specs_y * 3 + specs_x];
1029 if(spec.checkItem(item) == false)