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 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
131 std::ostringstream os(std::ios_base::binary);
134 ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
142 bool MaterialItem::isCookable()
144 return item_material_is_cookable(m_content);
147 InventoryItem *MaterialItem::createCookResult()
149 return item_material_create_cook_result(m_content);
157 video::ITexture * CraftItem::getImage()
159 if(g_texturesource == NULL)
162 std::string name = item_craft_get_image_name(m_subname);
164 // Get such a texture
165 return g_texturesource->getTextureRaw(name);
169 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
172 ServerActiveObject *obj = item_craft_create_object(m_subname, env, id, pos);
176 return InventoryItem::createSAO(env, id, pos);
179 u16 CraftItem::getDropCount()
182 s16 dc = item_craft_get_drop_count(m_subname);
186 return InventoryItem::getDropCount();
189 bool CraftItem::isCookable()
191 return item_craft_is_cookable(m_subname);
194 InventoryItem *CraftItem::createCookResult()
196 return item_craft_create_cook_result(m_subname);
199 bool CraftItem::use(ServerEnvironment *env, Player *player)
201 if(item_craft_is_eatable(m_subname))
203 s16 hp_change = item_craft_eat_hp_change(m_subname);
204 if(player->hp + hp_change > 20)
207 player->hp += hp_change;
214 MapBlockObjectItem DEPRECATED
218 video::ITexture * MapBlockObjectItem::getImage()
220 if(m_inventorystring.substr(0,3) == "Rat")
221 return g_texturesource->getTextureRaw("rat.png");
223 if(m_inventorystring.substr(0,4) == "Sign")
224 return g_texturesource->getTextureRaw("sign.png");
229 std::string MapBlockObjectItem::getText()
231 if(m_inventorystring.substr(0,3) == "Rat")
234 if(m_inventorystring.substr(0,4) == "Sign")
240 MapBlockObject * MapBlockObjectItem::createObject
241 (v3f pos, f32 player_yaw, f32 player_pitch)
243 std::istringstream is(m_inventorystring);
245 std::getline(is, name, ' ');
251 else if(name == "Sign")
254 std::getline(is, text, '|');
255 SignObject *obj = new SignObject(NULL, -1, pos);
257 obj->setYaw(-player_yaw);
260 else if(name == "Rat")
262 RatObject *obj = new RatObject(NULL, -1, pos);
265 else if(name == "ItemObj")
268 Now we are an inventory item containing the serialization
269 string of an object that contains the serialization
270 string of an inventory item. Fuck this.
273 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
274 <<"because an item-object should never be inside "
275 <<"an object-item."<<std::endl;
288 InventoryList::InventoryList(std::string name, u32 size)
296 InventoryList::~InventoryList()
298 for(u32 i=0; i<m_items.size(); i++)
305 void InventoryList::clearItems()
307 for(u32 i=0; i<m_items.size(); i++)
315 for(u32 i=0; i<m_size; i++)
317 m_items.push_back(NULL);
323 void InventoryList::serialize(std::ostream &os)
325 //os.imbue(std::locale("C"));
327 for(u32 i=0; i<m_items.size(); i++)
329 InventoryItem *item = m_items[i];
342 os<<"EndInventoryList\n";
345 void InventoryList::deSerialize(std::istream &is)
347 //is.imbue(std::locale("C"));
355 std::getline(is, line, '\n');
357 std::istringstream iss(line);
358 //iss.imbue(std::locale("C"));
361 std::getline(iss, name, ' ');
363 if(name == "EndInventoryList")
367 // This is a temporary backwards compatibility fix
368 else if(name == "end")
372 else if(name == "Item")
374 if(item_i > getSize() - 1)
375 throw SerializationError("too many items");
376 InventoryItem *item = InventoryItem::deSerialize(iss);
377 m_items[item_i++] = item;
379 else if(name == "Empty")
381 if(item_i > getSize() - 1)
382 throw SerializationError("too many items");
383 m_items[item_i++] = NULL;
387 throw SerializationError("Unknown inventory identifier");
392 InventoryList::InventoryList(const InventoryList &other)
395 Do this so that the items get cloned. Otherwise the pointers
396 in the array will just get copied.
401 InventoryList & InventoryList::operator = (const InventoryList &other)
403 m_name = other.m_name;
404 m_size = other.m_size;
406 for(u32 i=0; i<other.m_items.size(); i++)
408 InventoryItem *item = other.m_items[i];
411 m_items[i] = item->clone();
419 std::string InventoryList::getName()
424 u32 InventoryList::getSize()
426 return m_items.size();
429 u32 InventoryList::getUsedSlots()
432 for(u32 i=0; i<m_items.size(); i++)
434 InventoryItem *item = m_items[i];
441 u32 InventoryList::getFreeSlots()
443 return getSize() - getUsedSlots();
446 InventoryItem * InventoryList::getItem(u32 i)
448 if(i > m_items.size() - 1)
453 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
455 assert(i < m_items.size());
457 InventoryItem *olditem = m_items[i];
458 m_items[i] = newitem;
463 void InventoryList::deleteItem(u32 i)
465 assert(i < m_items.size());
466 InventoryItem *item = changeItem(i, NULL);
471 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
477 First try to find if it could be added to some existing items
479 for(u32 i=0; i<m_items.size(); i++)
481 // Ignore empty slots
482 if(m_items[i] == NULL)
485 newitem = addItem(i, newitem);
487 return NULL; // All was eaten
491 Then try to add it to empty slots
493 for(u32 i=0; i<m_items.size(); i++)
495 // Ignore unempty slots
496 if(m_items[i] != NULL)
499 newitem = addItem(i, newitem);
501 return NULL; // All was eaten
508 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
515 // If it is an empty position, it's an easy job.
516 InventoryItem *to_item = getItem(i);
519 m_items[i] = newitem;
523 // If not addable, return the item
524 if(newitem->addableTo(to_item) == false)
527 // If the item fits fully in the slot, add counter and delete it
528 if(newitem->getCount() <= to_item->freeSpace())
530 to_item->add(newitem->getCount());
534 // Else the item does not fit fully. Add all that fits and return
538 u16 freespace = to_item->freeSpace();
539 to_item->add(freespace);
540 newitem->remove(freespace);
545 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
547 // If it is an empty position, it's an easy job.
548 InventoryItem *to_item = getItem(i);
554 // If not addable, return the item
555 if(newitem->addableTo(to_item) == false)
558 // If the item fits fully in the slot, add counter and delete it
559 if(newitem->getCount() <= to_item->freeSpace())
567 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
574 InventoryItem *item = getItem(i);
575 // If it is an empty position, return NULL
579 if(count >= item->getCount())
581 // Get the item by swapping NULL to its place
582 return changeItem(i, NULL);
586 InventoryItem *item2 = item->clone();
588 item2->setCount(count);
595 void InventoryList::decrementMaterials(u16 count)
597 for(u32 i=0; i<m_items.size(); i++)
599 InventoryItem *item = takeItem(i, count);
605 void InventoryList::print(std::ostream &o)
607 o<<"InventoryList:"<<std::endl;
608 for(u32 i=0; i<m_items.size(); i++)
610 InventoryItem *item = m_items[i];
624 Inventory::~Inventory()
629 void Inventory::clear()
631 for(u32 i=0; i<m_lists.size(); i++)
638 Inventory::Inventory()
642 Inventory::Inventory(const Inventory &other)
647 Inventory & Inventory::operator = (const Inventory &other)
650 for(u32 i=0; i<other.m_lists.size(); i++)
652 m_lists.push_back(new InventoryList(*other.m_lists[i]));
657 void Inventory::serialize(std::ostream &os)
659 for(u32 i=0; i<m_lists.size(); i++)
661 InventoryList *list = m_lists[i];
662 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
666 os<<"EndInventory\n";
669 void Inventory::deSerialize(std::istream &is)
676 std::getline(is, line, '\n');
678 std::istringstream iss(line);
681 std::getline(iss, name, ' ');
683 if(name == "EndInventory")
687 // This is a temporary backwards compatibility fix
688 else if(name == "end")
692 else if(name == "List")
694 std::string listname;
697 std::getline(iss, listname, ' ');
700 InventoryList *list = new InventoryList(listname, listsize);
701 list->deSerialize(is);
703 m_lists.push_back(list);
707 throw SerializationError("Unknown inventory identifier");
712 InventoryList * Inventory::addList(const std::string &name, u32 size)
714 s32 i = getListIndex(name);
717 if(m_lists[i]->getSize() != size)
720 m_lists[i] = new InventoryList(name, size);
726 m_lists.push_back(new InventoryList(name, size));
727 return m_lists.getLast();
731 InventoryList * Inventory::getList(const std::string &name)
733 s32 i = getListIndex(name);
739 s32 Inventory::getListIndex(const std::string &name)
741 for(u32 i=0; i<m_lists.size(); i++)
743 if(m_lists[i]->getName() == name)
753 InventoryAction * InventoryAction::deSerialize(std::istream &is)
756 std::getline(is, type, ' ');
758 InventoryAction *a = NULL;
762 a = new IMoveAction(is);
768 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
772 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
773 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
774 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
776 Inventory *inv_from = mgr->getInventory(c, from_inv);
777 Inventory *inv_to = mgr->getInventory(c, to_inv);
779 if(!inv_from || !inv_to)
781 dstream<<__FUNCTION_NAME<<": Operation not allowed "
782 <<"(inventories not found)"<<std::endl;
786 InventoryList *list_from = inv_from->getList(from_list);
787 InventoryList *list_to = inv_to->getList(to_list);
789 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
792 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
795 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
799 If a list doesn't exist or the source item doesn't exist
801 if(!list_from || !list_to)
803 dstream<<__FUNCTION_NAME<<": Operation not allowed "
804 <<"(a list doesn't exist)"
808 if(list_from->getItem(from_i) == NULL)
810 dstream<<__FUNCTION_NAME<<": Operation not allowed "
811 <<"(the source item doesn't exist)"
816 If the source and the destination slots are the same
818 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
820 dstream<<__FUNCTION_NAME<<": Operation not allowed "
821 <<"(source and the destination slots are the same)"<<std::endl;
825 // Take item from source list
826 InventoryItem *item1 = NULL;
828 item1 = list_from->changeItem(from_i, NULL);
830 item1 = list_from->takeItem(from_i, count);
832 // Try to add the item to destination list
833 InventoryItem *olditem = item1;
834 item1 = list_to->addItem(to_i, item1);
836 // If something is returned, the item was not fully added
839 // If olditem is returned, nothing was added.
840 bool nothing_added = (item1 == olditem);
842 // If something else is returned, part of the item was left unadded.
843 // Add the other part back to the source item
844 list_from->addItem(from_i, item1);
846 // If olditem is returned, nothing was added.
850 // Take item from source list
851 item1 = list_from->changeItem(from_i, NULL);
852 // Adding was not possible, swap the items.
853 InventoryItem *item2 = list_to->changeItem(to_i, item1);
854 // Put item from destination list to the source list
855 list_from->changeItem(from_i, item2);
859 mgr->inventoryModified(c, from_inv);
860 if(from_inv != to_inv)
861 mgr->inventoryModified(c, to_inv);
866 Craft checking system
869 bool ItemSpec::checkItem(InventoryItem *item)
871 if(type == ITEM_NONE)
879 // There should be an item
883 std::string itemname = item->getName();
885 if(type == ITEM_MATERIAL)
887 if(itemname != "MaterialItem")
889 MaterialItem *mitem = (MaterialItem*)item;
890 if(mitem->getMaterial() != num)
893 else if(type == ITEM_CRAFT)
895 if(itemname != "CraftItem")
897 CraftItem *mitem = (CraftItem*)item;
898 if(mitem->getSubName() != name)
901 else if(type == ITEM_TOOL)
906 else if(type == ITEM_MBO)
919 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
921 u16 items_min_x = 100;
922 u16 items_max_x = 100;
923 u16 items_min_y = 100;
924 u16 items_max_y = 100;
925 for(u16 y=0; y<3; y++)
926 for(u16 x=0; x<3; x++)
928 if(items[y*3 + x] == NULL)
930 if(items_min_x == 100 || x < items_min_x)
932 if(items_min_y == 100 || y < items_min_y)
934 if(items_max_x == 100 || x > items_max_x)
936 if(items_max_y == 100 || y > items_max_y)
939 // No items at all, just return false
940 if(items_min_x == 100)
943 u16 items_w = items_max_x - items_min_x + 1;
944 u16 items_h = items_max_y - items_min_y + 1;
946 u16 specs_min_x = 100;
947 u16 specs_max_x = 100;
948 u16 specs_min_y = 100;
949 u16 specs_max_y = 100;
950 for(u16 y=0; y<3; y++)
951 for(u16 x=0; x<3; x++)
953 if(specs[y*3 + x].type == ITEM_NONE)
955 if(specs_min_x == 100 || x < specs_min_x)
957 if(specs_min_y == 100 || y < specs_min_y)
959 if(specs_max_x == 100 || x > specs_max_x)
961 if(specs_max_y == 100 || y > specs_max_y)
964 // No specs at all, just return false
965 if(specs_min_x == 100)
968 u16 specs_w = specs_max_x - specs_min_x + 1;
969 u16 specs_h = specs_max_y - specs_min_y + 1;
972 if(items_w != specs_w || items_h != specs_h)
975 for(u16 y=0; y<specs_h; y++)
976 for(u16 x=0; x<specs_w; x++)
978 u16 items_x = items_min_x + x;
979 u16 items_y = items_min_y + y;
980 u16 specs_x = specs_min_x + x;
981 u16 specs_y = specs_min_y + y;
982 InventoryItem *item = items[items_y * 3 + items_x];
983 ItemSpec &spec = specs[specs_y * 3 + specs_x];
985 if(spec.checkItem(item) == false)