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 InventoryItem* InventoryItem::deSerialize(std::istream &is)
51 DSTACK(__FUNCTION_NAME);
53 //is.imbue(std::locale("C"));
56 std::getline(is, name, ' ');
58 if(name == "MaterialItem")
60 // u16 reads directly as a number (u8 doesn't)
65 if(material > MAX_CONTENT)
66 throw SerializationError("Too large material number");
67 return new MaterialItem(material, count);
69 else if(name == "MBOItem")
71 std::string inventorystring;
72 std::getline(is, inventorystring, '|');
73 return new MapBlockObjectItem(inventorystring);
75 else if(name == "CraftItem")
78 std::getline(is, subname, ' ');
81 return new CraftItem(subname, count);
83 else if(name == "ToolItem")
86 std::getline(is, toolname, ' ');
89 return new ToolItem(toolname, wear);
93 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
94 throw SerializationError("Unknown InventoryItem name");
98 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
104 std::ostringstream os(std::ios_base::binary);
107 ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
115 bool MaterialItem::isCookable()
117 return item_material_is_cookable(m_content);
120 InventoryItem *MaterialItem::createCookResult()
122 return item_material_create_cook_result(m_content);
130 video::ITexture * CraftItem::getImage()
132 if(g_texturesource == NULL)
135 std::string name = item_craft_get_image_name(m_subname);
137 // Get such a texture
138 return g_texturesource->getTextureRaw(name);
142 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
145 ServerActiveObject *obj = item_craft_create_object(m_subname, env, id, pos);
149 return InventoryItem::createSAO(env, id, pos);
152 u16 CraftItem::getDropCount()
155 s16 dc = item_craft_get_drop_count(m_subname);
159 return InventoryItem::getDropCount();
162 bool CraftItem::isCookable()
164 return item_craft_is_cookable(m_subname);
167 InventoryItem *CraftItem::createCookResult()
169 return item_craft_create_cook_result(m_subname);
172 bool CraftItem::use(ServerEnvironment *env, Player *player)
174 if(item_craft_is_eatable(m_subname))
176 s16 hp_change = item_craft_eat_hp_change(m_subname);
177 if(player->hp + hp_change > 20)
180 player->hp += hp_change;
187 MapBlockObjectItem DEPRECATED
191 video::ITexture * MapBlockObjectItem::getImage()
193 if(m_inventorystring.substr(0,3) == "Rat")
194 return g_texturesource->getTextureRaw("rat.png");
196 if(m_inventorystring.substr(0,4) == "Sign")
197 return g_texturesource->getTextureRaw("sign.png");
202 std::string MapBlockObjectItem::getText()
204 if(m_inventorystring.substr(0,3) == "Rat")
207 if(m_inventorystring.substr(0,4) == "Sign")
213 MapBlockObject * MapBlockObjectItem::createObject
214 (v3f pos, f32 player_yaw, f32 player_pitch)
216 std::istringstream is(m_inventorystring);
218 std::getline(is, name, ' ');
224 else if(name == "Sign")
227 std::getline(is, text, '|');
228 SignObject *obj = new SignObject(NULL, -1, pos);
230 obj->setYaw(-player_yaw);
233 else if(name == "Rat")
235 RatObject *obj = new RatObject(NULL, -1, pos);
238 else if(name == "ItemObj")
241 Now we are an inventory item containing the serialization
242 string of an object that contains the serialization
243 string of an inventory item. Fuck this.
246 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
247 <<"because an item-object should never be inside "
248 <<"an object-item."<<std::endl;
261 InventoryList::InventoryList(std::string name, u32 size)
269 InventoryList::~InventoryList()
271 for(u32 i=0; i<m_items.size(); i++)
278 void InventoryList::clearItems()
280 for(u32 i=0; i<m_items.size(); i++)
288 for(u32 i=0; i<m_size; i++)
290 m_items.push_back(NULL);
296 void InventoryList::serialize(std::ostream &os)
298 //os.imbue(std::locale("C"));
300 for(u32 i=0; i<m_items.size(); i++)
302 InventoryItem *item = m_items[i];
315 os<<"EndInventoryList\n";
318 void InventoryList::deSerialize(std::istream &is)
320 //is.imbue(std::locale("C"));
328 std::getline(is, line, '\n');
330 std::istringstream iss(line);
331 //iss.imbue(std::locale("C"));
334 std::getline(iss, name, ' ');
336 if(name == "EndInventoryList")
340 // This is a temporary backwards compatibility fix
341 else if(name == "end")
345 else if(name == "Item")
347 if(item_i > getSize() - 1)
348 throw SerializationError("too many items");
349 InventoryItem *item = InventoryItem::deSerialize(iss);
350 m_items[item_i++] = item;
352 else if(name == "Empty")
354 if(item_i > getSize() - 1)
355 throw SerializationError("too many items");
356 m_items[item_i++] = NULL;
360 throw SerializationError("Unknown inventory identifier");
365 InventoryList::InventoryList(const InventoryList &other)
368 Do this so that the items get cloned. Otherwise the pointers
369 in the array will just get copied.
374 InventoryList & InventoryList::operator = (const InventoryList &other)
376 m_name = other.m_name;
377 m_size = other.m_size;
379 for(u32 i=0; i<other.m_items.size(); i++)
381 InventoryItem *item = other.m_items[i];
384 m_items[i] = item->clone();
392 std::string InventoryList::getName()
397 u32 InventoryList::getSize()
399 return m_items.size();
402 u32 InventoryList::getUsedSlots()
405 for(u32 i=0; i<m_items.size(); i++)
407 InventoryItem *item = m_items[i];
414 u32 InventoryList::getFreeSlots()
416 return getSize() - getUsedSlots();
419 InventoryItem * InventoryList::getItem(u32 i)
421 if(i > m_items.size() - 1)
426 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
428 assert(i < m_items.size());
430 InventoryItem *olditem = m_items[i];
431 m_items[i] = newitem;
436 void InventoryList::deleteItem(u32 i)
438 assert(i < m_items.size());
439 InventoryItem *item = changeItem(i, NULL);
444 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
450 First try to find if it could be added to some existing items
452 for(u32 i=0; i<m_items.size(); i++)
454 // Ignore empty slots
455 if(m_items[i] == NULL)
458 newitem = addItem(i, newitem);
460 return NULL; // All was eaten
464 Then try to add it to empty slots
466 for(u32 i=0; i<m_items.size(); i++)
468 // Ignore unempty slots
469 if(m_items[i] != NULL)
472 newitem = addItem(i, newitem);
474 return NULL; // All was eaten
481 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
488 // If it is an empty position, it's an easy job.
489 InventoryItem *to_item = getItem(i);
492 m_items[i] = newitem;
496 // If not addable, return the item
497 if(newitem->addableTo(to_item) == false)
500 // If the item fits fully in the slot, add counter and delete it
501 if(newitem->getCount() <= to_item->freeSpace())
503 to_item->add(newitem->getCount());
507 // Else the item does not fit fully. Add all that fits and return
511 u16 freespace = to_item->freeSpace();
512 to_item->add(freespace);
513 newitem->remove(freespace);
518 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
520 // If it is an empty position, it's an easy job.
521 InventoryItem *to_item = getItem(i);
527 // If not addable, return the item
528 if(newitem->addableTo(to_item) == false)
531 // If the item fits fully in the slot, add counter and delete it
532 if(newitem->getCount() <= to_item->freeSpace())
540 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
547 InventoryItem *item = getItem(i);
548 // If it is an empty position, return NULL
552 if(count >= item->getCount())
554 // Get the item by swapping NULL to its place
555 return changeItem(i, NULL);
559 InventoryItem *item2 = item->clone();
561 item2->setCount(count);
568 void InventoryList::decrementMaterials(u16 count)
570 for(u32 i=0; i<m_items.size(); i++)
572 InventoryItem *item = takeItem(i, count);
578 void InventoryList::print(std::ostream &o)
580 o<<"InventoryList:"<<std::endl;
581 for(u32 i=0; i<m_items.size(); i++)
583 InventoryItem *item = m_items[i];
597 Inventory::~Inventory()
602 void Inventory::clear()
604 for(u32 i=0; i<m_lists.size(); i++)
611 Inventory::Inventory()
615 Inventory::Inventory(const Inventory &other)
620 Inventory & Inventory::operator = (const Inventory &other)
623 for(u32 i=0; i<other.m_lists.size(); i++)
625 m_lists.push_back(new InventoryList(*other.m_lists[i]));
630 void Inventory::serialize(std::ostream &os)
632 for(u32 i=0; i<m_lists.size(); i++)
634 InventoryList *list = m_lists[i];
635 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
639 os<<"EndInventory\n";
642 void Inventory::deSerialize(std::istream &is)
649 std::getline(is, line, '\n');
651 std::istringstream iss(line);
654 std::getline(iss, name, ' ');
656 if(name == "EndInventory")
660 // This is a temporary backwards compatibility fix
661 else if(name == "end")
665 else if(name == "List")
667 std::string listname;
670 std::getline(iss, listname, ' ');
673 InventoryList *list = new InventoryList(listname, listsize);
674 list->deSerialize(is);
676 m_lists.push_back(list);
680 throw SerializationError("Unknown inventory identifier");
685 InventoryList * Inventory::addList(const std::string &name, u32 size)
687 s32 i = getListIndex(name);
690 if(m_lists[i]->getSize() != size)
693 m_lists[i] = new InventoryList(name, size);
699 m_lists.push_back(new InventoryList(name, size));
700 return m_lists.getLast();
704 InventoryList * Inventory::getList(const std::string &name)
706 s32 i = getListIndex(name);
712 s32 Inventory::getListIndex(const std::string &name)
714 for(u32 i=0; i<m_lists.size(); i++)
716 if(m_lists[i]->getName() == name)
726 InventoryAction * InventoryAction::deSerialize(std::istream &is)
729 std::getline(is, type, ' ');
731 InventoryAction *a = NULL;
735 a = new IMoveAction(is);
741 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
745 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
746 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
747 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
749 Inventory *inv_from = mgr->getInventory(c, from_inv);
750 Inventory *inv_to = mgr->getInventory(c, to_inv);
752 if(!inv_from || !inv_to)
754 dstream<<__FUNCTION_NAME<<": Operation not allowed "
755 <<"(inventories not found)"<<std::endl;
759 InventoryList *list_from = inv_from->getList(from_list);
760 InventoryList *list_to = inv_to->getList(to_list);
762 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
765 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
768 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
772 If a list doesn't exist or the source item doesn't exist
774 if(!list_from || !list_to)
776 dstream<<__FUNCTION_NAME<<": Operation not allowed "
777 <<"(a list doesn't exist)"
781 if(list_from->getItem(from_i) == NULL)
783 dstream<<__FUNCTION_NAME<<": Operation not allowed "
784 <<"(the source item doesn't exist)"
789 If the source and the destination slots are the same
791 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
793 dstream<<__FUNCTION_NAME<<": Operation not allowed "
794 <<"(source and the destination slots are the same)"<<std::endl;
798 // Take item from source list
799 InventoryItem *item1 = NULL;
801 item1 = list_from->changeItem(from_i, NULL);
803 item1 = list_from->takeItem(from_i, count);
805 // Try to add the item to destination list
806 InventoryItem *olditem = item1;
807 item1 = list_to->addItem(to_i, item1);
809 // If something is returned, the item was not fully added
812 // If olditem is returned, nothing was added.
813 bool nothing_added = (item1 == olditem);
815 // If something else is returned, part of the item was left unadded.
816 // Add the other part back to the source item
817 list_from->addItem(from_i, item1);
819 // If olditem is returned, nothing was added.
823 // Take item from source list
824 item1 = list_from->changeItem(from_i, NULL);
825 // Adding was not possible, swap the items.
826 InventoryItem *item2 = list_to->changeItem(to_i, item1);
827 // Put item from destination list to the source list
828 list_from->changeItem(from_i, item2);
832 mgr->inventoryModified(c, from_inv);
833 if(from_inv != to_inv)
834 mgr->inventoryModified(c, to_inv);
839 Craft checking system
842 bool ItemSpec::checkItem(InventoryItem *item)
844 if(type == ITEM_NONE)
852 // There should be an item
856 std::string itemname = item->getName();
858 if(type == ITEM_MATERIAL)
860 if(itemname != "MaterialItem")
862 MaterialItem *mitem = (MaterialItem*)item;
863 if(mitem->getMaterial() != num)
866 else if(type == ITEM_CRAFT)
868 if(itemname != "CraftItem")
870 CraftItem *mitem = (CraftItem*)item;
871 if(mitem->getSubName() != name)
874 else if(type == ITEM_TOOL)
879 else if(type == ITEM_MBO)
892 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
894 u16 items_min_x = 100;
895 u16 items_max_x = 100;
896 u16 items_min_y = 100;
897 u16 items_max_y = 100;
898 for(u16 y=0; y<3; y++)
899 for(u16 x=0; x<3; x++)
901 if(items[y*3 + x] == NULL)
903 if(items_min_x == 100 || x < items_min_x)
905 if(items_min_y == 100 || y < items_min_y)
907 if(items_max_x == 100 || x > items_max_x)
909 if(items_max_y == 100 || y > items_max_y)
912 // No items at all, just return false
913 if(items_min_x == 100)
916 u16 items_w = items_max_x - items_min_x + 1;
917 u16 items_h = items_max_y - items_min_y + 1;
919 u16 specs_min_x = 100;
920 u16 specs_max_x = 100;
921 u16 specs_min_y = 100;
922 u16 specs_max_y = 100;
923 for(u16 y=0; y<3; y++)
924 for(u16 x=0; x<3; x++)
926 if(specs[y*3 + x].type == ITEM_NONE)
928 if(specs_min_x == 100 || x < specs_min_x)
930 if(specs_min_y == 100 || y < specs_min_y)
932 if(specs_max_x == 100 || x > specs_max_x)
934 if(specs_max_y == 100 || y > specs_max_y)
937 // No specs at all, just return false
938 if(specs_min_x == 100)
941 u16 specs_w = specs_max_x - specs_min_x + 1;
942 u16 specs_h = specs_max_y - specs_min_y + 1;
945 if(items_w != specs_w || items_h != specs_h)
948 for(u16 y=0; y<specs_h; y++)
949 for(u16 x=0; x<specs_w; x++)
951 u16 items_x = items_min_x + x;
952 u16 items_y = items_min_y + y;
953 u16 specs_x = specs_min_x + x;
954 u16 specs_y = specs_min_y + y;
955 InventoryItem *item = items[items_y * 3 + items_x];
956 ItemSpec &spec = specs[specs_y * 3 + specs_x];
958 if(spec.checkItem(item) == false)