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"
36 InventoryItem::InventoryItem(u16 count)
41 InventoryItem::~InventoryItem()
45 InventoryItem* InventoryItem::deSerialize(std::istream &is)
47 DSTACK(__FUNCTION_NAME);
49 //is.imbue(std::locale("C"));
52 std::getline(is, name, ' ');
54 if(name == "MaterialItem")
56 // u16 reads directly as a number (u8 doesn't)
62 throw SerializationError("Too large material number");
63 return new MaterialItem(material, count);
65 else if(name == "MBOItem")
67 std::string inventorystring;
68 std::getline(is, inventorystring, '|');
69 return new MapBlockObjectItem(inventorystring);
71 else if(name == "CraftItem")
74 std::getline(is, subname, ' ');
77 return new CraftItem(subname, count);
79 else if(name == "ToolItem")
82 std::getline(is, toolname, ' ');
85 return new ToolItem(toolname, wear);
89 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
90 throw SerializationError("Unknown InventoryItem name");
94 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
100 std::ostringstream os(std::ios_base::binary);
103 ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
111 bool MaterialItem::isCookable()
113 if(m_content == CONTENT_TREE)
117 else if(m_content == CONTENT_COBBLE)
124 InventoryItem *MaterialItem::createCookResult()
126 if(m_content == CONTENT_TREE)
128 return new CraftItem("lump_of_coal", 1);
130 else if(m_content == CONTENT_COBBLE)
132 return new MaterialItem(CONTENT_STONE, 1);
142 video::ITexture * CraftItem::getImage()
144 if(g_texturesource == NULL)
149 if(m_subname == "Stick")
151 else if(m_subname == "lump_of_coal")
152 name = "lump_of_coal.png";
153 else if(m_subname == "lump_of_iron")
154 name = "lump_of_iron.png";
155 else if(m_subname == "steel_ingot")
156 name = "steel_ingot.png";
157 else if(m_subname == "rat")
162 // Get such a texture
163 return g_texturesource->getTextureRaw(name);
167 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
170 if(m_subname == "rat")
172 ServerActiveObject *obj = new RatSAO(env, id, pos);
178 return InventoryItem::createSAO(env, id, pos);
182 u16 CraftItem::getDropCount()
185 if(m_subname == "rat")
189 return InventoryItem::getDropCount();
192 bool CraftItem::isCookable()
194 if(m_subname == "lump_of_iron")
201 InventoryItem *CraftItem::createCookResult()
203 if(m_subname == "lump_of_iron")
205 return new CraftItem("steel_ingot", 1);
215 video::ITexture * MapBlockObjectItem::getImage()
217 if(m_inventorystring.substr(0,3) == "Rat")
218 return g_texturesource->getTextureRaw("rat.png");
220 if(m_inventorystring.substr(0,4) == "Sign")
221 return g_texturesource->getTextureRaw("sign.png");
226 std::string MapBlockObjectItem::getText()
228 if(m_inventorystring.substr(0,3) == "Rat")
231 if(m_inventorystring.substr(0,4) == "Sign")
237 MapBlockObject * MapBlockObjectItem::createObject
238 (v3f pos, f32 player_yaw, f32 player_pitch)
240 std::istringstream is(m_inventorystring);
242 std::getline(is, name, ' ');
248 else if(name == "Sign")
251 std::getline(is, text, '|');
252 SignObject *obj = new SignObject(NULL, -1, pos);
254 obj->setYaw(-player_yaw);
257 else if(name == "Rat")
259 RatObject *obj = new RatObject(NULL, -1, pos);
262 else if(name == "ItemObj")
265 Now we are an inventory item containing the serialization
266 string of an object that contains the serialization
267 string of an inventory item. Fuck this.
270 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
271 <<"because an item-object should never be inside "
272 <<"an object-item."<<std::endl;
285 InventoryList::InventoryList(std::string name, u32 size)
293 InventoryList::~InventoryList()
295 for(u32 i=0; i<m_items.size(); i++)
302 void InventoryList::clearItems()
304 for(u32 i=0; i<m_items.size(); i++)
312 for(u32 i=0; i<m_size; i++)
314 m_items.push_back(NULL);
320 void InventoryList::serialize(std::ostream &os)
322 //os.imbue(std::locale("C"));
324 for(u32 i=0; i<m_items.size(); i++)
326 InventoryItem *item = m_items[i];
339 os<<"EndInventoryList\n";
342 void InventoryList::deSerialize(std::istream &is)
344 //is.imbue(std::locale("C"));
352 std::getline(is, line, '\n');
354 std::istringstream iss(line);
355 //iss.imbue(std::locale("C"));
358 std::getline(iss, name, ' ');
360 if(name == "EndInventoryList")
364 // This is a temporary backwards compatibility fix
365 else if(name == "end")
369 else if(name == "Item")
371 if(item_i > getSize() - 1)
372 throw SerializationError("too many items");
373 InventoryItem *item = InventoryItem::deSerialize(iss);
374 m_items[item_i++] = item;
376 else if(name == "Empty")
378 if(item_i > getSize() - 1)
379 throw SerializationError("too many items");
380 m_items[item_i++] = NULL;
384 throw SerializationError("Unknown inventory identifier");
389 InventoryList::InventoryList(const InventoryList &other)
392 Do this so that the items get cloned. Otherwise the pointers
393 in the array will just get copied.
398 InventoryList & InventoryList::operator = (const InventoryList &other)
400 m_name = other.m_name;
401 m_size = other.m_size;
403 for(u32 i=0; i<other.m_items.size(); i++)
405 InventoryItem *item = other.m_items[i];
408 m_items[i] = item->clone();
416 std::string InventoryList::getName()
421 u32 InventoryList::getSize()
423 return m_items.size();
426 u32 InventoryList::getUsedSlots()
429 for(u32 i=0; i<m_items.size(); i++)
431 InventoryItem *item = m_items[i];
438 u32 InventoryList::getFreeSlots()
440 return getSize() - getUsedSlots();
443 InventoryItem * InventoryList::getItem(u32 i)
445 if(i > m_items.size() - 1)
450 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
452 assert(i < m_items.size());
454 InventoryItem *olditem = m_items[i];
455 m_items[i] = newitem;
460 void InventoryList::deleteItem(u32 i)
462 assert(i < m_items.size());
463 InventoryItem *item = changeItem(i, NULL);
468 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
474 First try to find if it could be added to some existing items
476 for(u32 i=0; i<m_items.size(); i++)
478 // Ignore empty slots
479 if(m_items[i] == NULL)
482 newitem = addItem(i, newitem);
484 return NULL; // All was eaten
488 Then try to add it to empty slots
490 for(u32 i=0; i<m_items.size(); i++)
492 // Ignore unempty slots
493 if(m_items[i] != NULL)
496 newitem = addItem(i, newitem);
498 return NULL; // All was eaten
505 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
512 // If it is an empty position, it's an easy job.
513 InventoryItem *to_item = m_items[i];
516 m_items[i] = newitem;
520 // If not addable, return the item
521 if(newitem->addableTo(to_item) == false)
524 // If the item fits fully in the slot, add counter and delete it
525 if(newitem->getCount() <= to_item->freeSpace())
527 to_item->add(newitem->getCount());
531 // Else the item does not fit fully. Add all that fits and return
535 u16 freespace = to_item->freeSpace();
536 to_item->add(freespace);
537 newitem->remove(freespace);
542 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
544 // If it is an empty position, it's an easy job.
545 InventoryItem *to_item = m_items[i];
551 // If not addable, return the item
552 if(newitem->addableTo(to_item) == false)
555 // If the item fits fully in the slot, add counter and delete it
556 if(newitem->getCount() <= to_item->freeSpace())
564 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
571 InventoryItem *item = m_items[i];
572 // If it is an empty position, return NULL
576 if(count >= item->getCount())
578 // Get the item by swapping NULL to its place
579 return changeItem(i, NULL);
583 InventoryItem *item2 = item->clone();
585 item2->setCount(count);
592 void InventoryList::decrementMaterials(u16 count)
594 for(u32 i=0; i<m_items.size(); i++)
596 InventoryItem *item = takeItem(i, count);
602 void InventoryList::print(std::ostream &o)
604 o<<"InventoryList:"<<std::endl;
605 for(u32 i=0; i<m_items.size(); i++)
607 InventoryItem *item = m_items[i];
621 Inventory::~Inventory()
626 void Inventory::clear()
628 for(u32 i=0; i<m_lists.size(); i++)
635 Inventory::Inventory()
639 Inventory::Inventory(const Inventory &other)
644 Inventory & Inventory::operator = (const Inventory &other)
647 for(u32 i=0; i<other.m_lists.size(); i++)
649 m_lists.push_back(new InventoryList(*other.m_lists[i]));
654 void Inventory::serialize(std::ostream &os)
656 for(u32 i=0; i<m_lists.size(); i++)
658 InventoryList *list = m_lists[i];
659 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
663 os<<"EndInventory\n";
666 void Inventory::deSerialize(std::istream &is)
673 std::getline(is, line, '\n');
675 std::istringstream iss(line);
678 std::getline(iss, name, ' ');
680 if(name == "EndInventory")
684 // This is a temporary backwards compatibility fix
685 else if(name == "end")
689 else if(name == "List")
691 std::string listname;
694 std::getline(iss, listname, ' ');
697 InventoryList *list = new InventoryList(listname, listsize);
698 list->deSerialize(is);
700 m_lists.push_back(list);
704 throw SerializationError("Unknown inventory identifier");
709 InventoryList * Inventory::addList(const std::string &name, u32 size)
711 s32 i = getListIndex(name);
714 if(m_lists[i]->getSize() != size)
717 m_lists[i] = new InventoryList(name, size);
723 m_lists.push_back(new InventoryList(name, size));
724 return m_lists.getLast();
728 InventoryList * Inventory::getList(const std::string &name)
730 s32 i = getListIndex(name);
736 s32 Inventory::getListIndex(const std::string &name)
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 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
769 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
770 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
771 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
773 Inventory *inv_from = mgr->getInventory(c, from_inv);
774 Inventory *inv_to = mgr->getInventory(c, to_inv);
776 if(!inv_from || !inv_to)
778 dstream<<__FUNCTION_NAME<<": Operation not allowed "
779 <<"(inventories not found)"<<std::endl;
783 InventoryList *list_from = inv_from->getList(from_list);
784 InventoryList *list_to = inv_to->getList(to_list);
786 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
789 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
792 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
796 If a list doesn't exist or the source item doesn't exist
798 if(!list_from || !list_to)
800 dstream<<__FUNCTION_NAME<<": Operation not allowed "
801 <<"(a list doesn't exist)"
805 if(list_from->getItem(from_i) == NULL)
807 dstream<<__FUNCTION_NAME<<": Operation not allowed "
808 <<"(the source item doesn't exist)"
813 If the source and the destination slots are the same
815 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
817 dstream<<__FUNCTION_NAME<<": Operation not allowed "
818 <<"(source and the destination slots are the same)"<<std::endl;
822 // Take item from source list
823 InventoryItem *item1 = NULL;
825 item1 = list_from->changeItem(from_i, NULL);
827 item1 = list_from->takeItem(from_i, count);
829 // Try to add the item to destination list
830 InventoryItem *olditem = item1;
831 item1 = list_to->addItem(to_i, item1);
833 // If something is returned, the item was not fully added
836 // If olditem is returned, nothing was added.
837 bool nothing_added = (item1 == olditem);
839 // If something else is returned, part of the item was left unadded.
840 // Add the other part back to the source item
841 list_from->addItem(from_i, item1);
843 // If olditem is returned, nothing was added.
847 // Take item from source list
848 item1 = list_from->changeItem(from_i, NULL);
849 // Adding was not possible, swap the items.
850 InventoryItem *item2 = list_to->changeItem(to_i, item1);
851 // Put item from destination list to the source list
852 list_from->changeItem(from_i, item2);
856 mgr->inventoryModified(c, from_inv);
857 if(from_inv != to_inv)
858 mgr->inventoryModified(c, to_inv);
863 Craft checking system
866 bool ItemSpec::checkItem(InventoryItem *item)
868 if(type == ITEM_NONE)
876 // There should be an item
880 std::string itemname = item->getName();
882 if(type == ITEM_MATERIAL)
884 if(itemname != "MaterialItem")
886 MaterialItem *mitem = (MaterialItem*)item;
887 if(mitem->getMaterial() != num)
890 else if(type == ITEM_CRAFT)
892 if(itemname != "CraftItem")
894 CraftItem *mitem = (CraftItem*)item;
895 if(mitem->getSubName() != name)
898 else if(type == ITEM_TOOL)
903 else if(type == ITEM_MBO)
916 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
918 u16 items_min_x = 100;
919 u16 items_max_x = 100;
920 u16 items_min_y = 100;
921 u16 items_max_y = 100;
922 for(u16 y=0; y<3; y++)
923 for(u16 x=0; x<3; x++)
925 if(items[y*3 + x] == NULL)
927 if(items_min_x == 100 || x < items_min_x)
929 if(items_min_y == 100 || y < items_min_y)
931 if(items_max_x == 100 || x > items_max_x)
933 if(items_max_y == 100 || y > items_max_y)
936 // No items at all, just return false
937 if(items_min_x == 100)
940 u16 items_w = items_max_x - items_min_x + 1;
941 u16 items_h = items_max_y - items_min_y + 1;
943 u16 specs_min_x = 100;
944 u16 specs_max_x = 100;
945 u16 specs_min_y = 100;
946 u16 specs_max_y = 100;
947 for(u16 y=0; y<3; y++)
948 for(u16 x=0; x<3; x++)
950 if(specs[y*3 + x].type == ITEM_NONE)
952 if(specs_min_x == 100 || x < specs_min_x)
954 if(specs_min_y == 100 || y < specs_min_y)
956 if(specs_max_x == 100 || x > specs_max_x)
958 if(specs_max_y == 100 || y > specs_max_y)
961 // No specs at all, just return false
962 if(specs_min_x == 100)
965 u16 specs_w = specs_max_x - specs_min_x + 1;
966 u16 specs_h = specs_max_y - specs_min_y + 1;
969 if(items_w != specs_w || items_h != specs_h)
972 for(u16 y=0; y<specs_h; y++)
973 for(u16 x=0; x<specs_w; x++)
975 u16 items_x = items_min_x + x;
976 u16 items_y = items_min_y + y;
977 u16 specs_x = specs_min_x + x;
978 u16 specs_y = specs_min_y + y;
979 InventoryItem *item = items[items_y * 3 + items_x];
980 ItemSpec &spec = specs[specs_y * 3 + specs_x];
982 if(spec.checkItem(item) == false)