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"
39 InventoryItem::InventoryItem(u16 count)
44 InventoryItem::~InventoryItem()
48 InventoryItem* InventoryItem::deSerialize(std::istream &is)
50 DSTACK(__FUNCTION_NAME);
52 //is.imbue(std::locale("C"));
55 std::getline(is, name, ' ');
57 if(name == "MaterialItem")
59 // u16 reads directly as a number (u8 doesn't)
65 throw SerializationError("Too large material number");
66 return new MaterialItem(material, count);
68 else if(name == "MBOItem")
70 std::string inventorystring;
71 std::getline(is, inventorystring, '|');
72 return new MapBlockObjectItem(inventorystring);
74 else if(name == "CraftItem")
77 std::getline(is, subname, ' ');
80 return new CraftItem(subname, count);
82 else if(name == "ToolItem")
85 std::getline(is, toolname, ' ');
88 return new ToolItem(toolname, wear);
92 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
93 throw SerializationError("Unknown InventoryItem name");
97 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
103 std::ostringstream os(std::ios_base::binary);
106 ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
114 bool MaterialItem::isCookable()
116 return item_material_is_cookable(m_content);
119 InventoryItem *MaterialItem::createCookResult()
121 return item_material_create_cook_result(m_content);
129 video::ITexture * CraftItem::getImage()
131 if(g_texturesource == NULL)
134 std::string name = item_craft_get_image_name(m_subname);
136 // Get such a texture
137 return g_texturesource->getTextureRaw(name);
141 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
144 ServerActiveObject *obj = item_craft_create_object(m_subname, env, id, pos);
148 return InventoryItem::createSAO(env, id, pos);
151 u16 CraftItem::getDropCount()
154 s16 dc = item_craft_get_drop_count(m_subname);
158 return InventoryItem::getDropCount();
161 bool CraftItem::isCookable()
163 return item_craft_is_cookable(m_subname);
166 InventoryItem *CraftItem::createCookResult()
168 return item_craft_create_cook_result(m_subname);
172 MapBlockObjectItem DEPRECATED
176 video::ITexture * MapBlockObjectItem::getImage()
178 if(m_inventorystring.substr(0,3) == "Rat")
179 return g_texturesource->getTextureRaw("rat.png");
181 if(m_inventorystring.substr(0,4) == "Sign")
182 return g_texturesource->getTextureRaw("sign.png");
187 std::string MapBlockObjectItem::getText()
189 if(m_inventorystring.substr(0,3) == "Rat")
192 if(m_inventorystring.substr(0,4) == "Sign")
198 MapBlockObject * MapBlockObjectItem::createObject
199 (v3f pos, f32 player_yaw, f32 player_pitch)
201 std::istringstream is(m_inventorystring);
203 std::getline(is, name, ' ');
209 else if(name == "Sign")
212 std::getline(is, text, '|');
213 SignObject *obj = new SignObject(NULL, -1, pos);
215 obj->setYaw(-player_yaw);
218 else if(name == "Rat")
220 RatObject *obj = new RatObject(NULL, -1, pos);
223 else if(name == "ItemObj")
226 Now we are an inventory item containing the serialization
227 string of an object that contains the serialization
228 string of an inventory item. Fuck this.
231 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
232 <<"because an item-object should never be inside "
233 <<"an object-item."<<std::endl;
246 InventoryList::InventoryList(std::string name, u32 size)
254 InventoryList::~InventoryList()
256 for(u32 i=0; i<m_items.size(); i++)
263 void InventoryList::clearItems()
265 for(u32 i=0; i<m_items.size(); i++)
273 for(u32 i=0; i<m_size; i++)
275 m_items.push_back(NULL);
281 void InventoryList::serialize(std::ostream &os)
283 //os.imbue(std::locale("C"));
285 for(u32 i=0; i<m_items.size(); i++)
287 InventoryItem *item = m_items[i];
300 os<<"EndInventoryList\n";
303 void InventoryList::deSerialize(std::istream &is)
305 //is.imbue(std::locale("C"));
313 std::getline(is, line, '\n');
315 std::istringstream iss(line);
316 //iss.imbue(std::locale("C"));
319 std::getline(iss, name, ' ');
321 if(name == "EndInventoryList")
325 // This is a temporary backwards compatibility fix
326 else if(name == "end")
330 else if(name == "Item")
332 if(item_i > getSize() - 1)
333 throw SerializationError("too many items");
334 InventoryItem *item = InventoryItem::deSerialize(iss);
335 m_items[item_i++] = item;
337 else if(name == "Empty")
339 if(item_i > getSize() - 1)
340 throw SerializationError("too many items");
341 m_items[item_i++] = NULL;
345 throw SerializationError("Unknown inventory identifier");
350 InventoryList::InventoryList(const InventoryList &other)
353 Do this so that the items get cloned. Otherwise the pointers
354 in the array will just get copied.
359 InventoryList & InventoryList::operator = (const InventoryList &other)
361 m_name = other.m_name;
362 m_size = other.m_size;
364 for(u32 i=0; i<other.m_items.size(); i++)
366 InventoryItem *item = other.m_items[i];
369 m_items[i] = item->clone();
377 std::string InventoryList::getName()
382 u32 InventoryList::getSize()
384 return m_items.size();
387 u32 InventoryList::getUsedSlots()
390 for(u32 i=0; i<m_items.size(); i++)
392 InventoryItem *item = m_items[i];
399 u32 InventoryList::getFreeSlots()
401 return getSize() - getUsedSlots();
404 InventoryItem * InventoryList::getItem(u32 i)
406 if(i > m_items.size() - 1)
411 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
413 assert(i < m_items.size());
415 InventoryItem *olditem = m_items[i];
416 m_items[i] = newitem;
421 void InventoryList::deleteItem(u32 i)
423 assert(i < m_items.size());
424 InventoryItem *item = changeItem(i, NULL);
429 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
435 First try to find if it could be added to some existing items
437 for(u32 i=0; i<m_items.size(); i++)
439 // Ignore empty slots
440 if(m_items[i] == NULL)
443 newitem = addItem(i, newitem);
445 return NULL; // All was eaten
449 Then try to add it to empty slots
451 for(u32 i=0; i<m_items.size(); i++)
453 // Ignore unempty slots
454 if(m_items[i] != NULL)
457 newitem = addItem(i, newitem);
459 return NULL; // All was eaten
466 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
473 // If it is an empty position, it's an easy job.
474 InventoryItem *to_item = getItem(i);
477 m_items[i] = newitem;
481 // If not addable, return the item
482 if(newitem->addableTo(to_item) == false)
485 // If the item fits fully in the slot, add counter and delete it
486 if(newitem->getCount() <= to_item->freeSpace())
488 to_item->add(newitem->getCount());
492 // Else the item does not fit fully. Add all that fits and return
496 u16 freespace = to_item->freeSpace();
497 to_item->add(freespace);
498 newitem->remove(freespace);
503 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
505 // If it is an empty position, it's an easy job.
506 InventoryItem *to_item = getItem(i);
512 // If not addable, return the item
513 if(newitem->addableTo(to_item) == false)
516 // If the item fits fully in the slot, add counter and delete it
517 if(newitem->getCount() <= to_item->freeSpace())
525 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
532 InventoryItem *item = getItem(i);
533 // If it is an empty position, return NULL
537 if(count >= item->getCount())
539 // Get the item by swapping NULL to its place
540 return changeItem(i, NULL);
544 InventoryItem *item2 = item->clone();
546 item2->setCount(count);
553 void InventoryList::decrementMaterials(u16 count)
555 for(u32 i=0; i<m_items.size(); i++)
557 InventoryItem *item = takeItem(i, count);
563 void InventoryList::print(std::ostream &o)
565 o<<"InventoryList:"<<std::endl;
566 for(u32 i=0; i<m_items.size(); i++)
568 InventoryItem *item = m_items[i];
582 Inventory::~Inventory()
587 void Inventory::clear()
589 for(u32 i=0; i<m_lists.size(); i++)
596 Inventory::Inventory()
600 Inventory::Inventory(const Inventory &other)
605 Inventory & Inventory::operator = (const Inventory &other)
608 for(u32 i=0; i<other.m_lists.size(); i++)
610 m_lists.push_back(new InventoryList(*other.m_lists[i]));
615 void Inventory::serialize(std::ostream &os)
617 for(u32 i=0; i<m_lists.size(); i++)
619 InventoryList *list = m_lists[i];
620 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
624 os<<"EndInventory\n";
627 void Inventory::deSerialize(std::istream &is)
634 std::getline(is, line, '\n');
636 std::istringstream iss(line);
639 std::getline(iss, name, ' ');
641 if(name == "EndInventory")
645 // This is a temporary backwards compatibility fix
646 else if(name == "end")
650 else if(name == "List")
652 std::string listname;
655 std::getline(iss, listname, ' ');
658 InventoryList *list = new InventoryList(listname, listsize);
659 list->deSerialize(is);
661 m_lists.push_back(list);
665 throw SerializationError("Unknown inventory identifier");
670 InventoryList * Inventory::addList(const std::string &name, u32 size)
672 s32 i = getListIndex(name);
675 if(m_lists[i]->getSize() != size)
678 m_lists[i] = new InventoryList(name, size);
684 m_lists.push_back(new InventoryList(name, size));
685 return m_lists.getLast();
689 InventoryList * Inventory::getList(const std::string &name)
691 s32 i = getListIndex(name);
697 s32 Inventory::getListIndex(const std::string &name)
699 for(u32 i=0; i<m_lists.size(); i++)
701 if(m_lists[i]->getName() == name)
711 InventoryAction * InventoryAction::deSerialize(std::istream &is)
714 std::getline(is, type, ' ');
716 InventoryAction *a = NULL;
720 a = new IMoveAction(is);
726 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
730 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
731 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
732 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
734 Inventory *inv_from = mgr->getInventory(c, from_inv);
735 Inventory *inv_to = mgr->getInventory(c, to_inv);
737 if(!inv_from || !inv_to)
739 dstream<<__FUNCTION_NAME<<": Operation not allowed "
740 <<"(inventories not found)"<<std::endl;
744 InventoryList *list_from = inv_from->getList(from_list);
745 InventoryList *list_to = inv_to->getList(to_list);
747 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
750 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
753 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
757 If a list doesn't exist or the source item doesn't exist
759 if(!list_from || !list_to)
761 dstream<<__FUNCTION_NAME<<": Operation not allowed "
762 <<"(a list doesn't exist)"
766 if(list_from->getItem(from_i) == NULL)
768 dstream<<__FUNCTION_NAME<<": Operation not allowed "
769 <<"(the source item doesn't exist)"
774 If the source and the destination slots are the same
776 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
778 dstream<<__FUNCTION_NAME<<": Operation not allowed "
779 <<"(source and the destination slots are the same)"<<std::endl;
783 // Take item from source list
784 InventoryItem *item1 = NULL;
786 item1 = list_from->changeItem(from_i, NULL);
788 item1 = list_from->takeItem(from_i, count);
790 // Try to add the item to destination list
791 InventoryItem *olditem = item1;
792 item1 = list_to->addItem(to_i, item1);
794 // If something is returned, the item was not fully added
797 // If olditem is returned, nothing was added.
798 bool nothing_added = (item1 == olditem);
800 // If something else is returned, part of the item was left unadded.
801 // Add the other part back to the source item
802 list_from->addItem(from_i, item1);
804 // If olditem is returned, nothing was added.
808 // Take item from source list
809 item1 = list_from->changeItem(from_i, NULL);
810 // Adding was not possible, swap the items.
811 InventoryItem *item2 = list_to->changeItem(to_i, item1);
812 // Put item from destination list to the source list
813 list_from->changeItem(from_i, item2);
817 mgr->inventoryModified(c, from_inv);
818 if(from_inv != to_inv)
819 mgr->inventoryModified(c, to_inv);
824 Craft checking system
827 bool ItemSpec::checkItem(InventoryItem *item)
829 if(type == ITEM_NONE)
837 // There should be an item
841 std::string itemname = item->getName();
843 if(type == ITEM_MATERIAL)
845 if(itemname != "MaterialItem")
847 MaterialItem *mitem = (MaterialItem*)item;
848 if(mitem->getMaterial() != num)
851 else if(type == ITEM_CRAFT)
853 if(itemname != "CraftItem")
855 CraftItem *mitem = (CraftItem*)item;
856 if(mitem->getSubName() != name)
859 else if(type == ITEM_TOOL)
864 else if(type == ITEM_MBO)
877 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
879 u16 items_min_x = 100;
880 u16 items_max_x = 100;
881 u16 items_min_y = 100;
882 u16 items_max_y = 100;
883 for(u16 y=0; y<3; y++)
884 for(u16 x=0; x<3; x++)
886 if(items[y*3 + x] == NULL)
888 if(items_min_x == 100 || x < items_min_x)
890 if(items_min_y == 100 || y < items_min_y)
892 if(items_max_x == 100 || x > items_max_x)
894 if(items_max_y == 100 || y > items_max_y)
897 // No items at all, just return false
898 if(items_min_x == 100)
901 u16 items_w = items_max_x - items_min_x + 1;
902 u16 items_h = items_max_y - items_min_y + 1;
904 u16 specs_min_x = 100;
905 u16 specs_max_x = 100;
906 u16 specs_min_y = 100;
907 u16 specs_max_y = 100;
908 for(u16 y=0; y<3; y++)
909 for(u16 x=0; x<3; x++)
911 if(specs[y*3 + x].type == ITEM_NONE)
913 if(specs_min_x == 100 || x < specs_min_x)
915 if(specs_min_y == 100 || y < specs_min_y)
917 if(specs_max_x == 100 || x > specs_max_x)
919 if(specs_max_y == 100 || y > specs_max_y)
922 // No specs at all, just return false
923 if(specs_min_x == 100)
926 u16 specs_w = specs_max_x - specs_min_x + 1;
927 u16 specs_h = specs_max_y - specs_min_y + 1;
930 if(items_w != specs_w || items_h != specs_h)
933 for(u16 y=0; y<specs_h; y++)
934 for(u16 x=0; x<specs_w; x++)
936 u16 items_x = items_min_x + x;
937 u16 items_y = items_min_y + y;
938 u16 specs_x = specs_min_x + x;
939 u16 specs_y = specs_min_y + y;
940 InventoryItem *item = items[items_y * 3 + items_x];
941 ItemSpec &spec = specs[specs_y * 3 + specs_x];
943 if(spec.checkItem(item) == false)