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_irrlicht->getTexture(name);
164 return g_texturesource->getTextureRaw(name);
168 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
171 if(m_subname == "rat")
173 ServerActiveObject *obj = new RatSAO(env, id, pos);
179 return InventoryItem::createSAO(env, id, pos);
183 u16 CraftItem::getDropCount()
186 if(m_subname == "rat")
190 return InventoryItem::getDropCount();
193 bool CraftItem::isCookable()
195 if(m_subname == "lump_of_iron")
202 InventoryItem *CraftItem::createCookResult()
204 if(m_subname == "lump_of_iron")
206 return new CraftItem("steel_ingot", 1);
216 video::ITexture * MapBlockObjectItem::getImage()
218 if(m_inventorystring.substr(0,3) == "Rat")
219 return g_texturesource->getTextureRaw("rat.png");
221 if(m_inventorystring.substr(0,4) == "Sign")
222 return g_texturesource->getTextureRaw("sign.png");
227 std::string MapBlockObjectItem::getText()
229 if(m_inventorystring.substr(0,3) == "Rat")
232 if(m_inventorystring.substr(0,4) == "Sign")
238 MapBlockObject * MapBlockObjectItem::createObject
239 (v3f pos, f32 player_yaw, f32 player_pitch)
241 std::istringstream is(m_inventorystring);
243 std::getline(is, name, ' ');
249 else if(name == "Sign")
252 std::getline(is, text, '|');
253 SignObject *obj = new SignObject(NULL, -1, pos);
255 obj->setYaw(-player_yaw);
258 else if(name == "Rat")
260 RatObject *obj = new RatObject(NULL, -1, pos);
263 else if(name == "ItemObj")
266 Now we are an inventory item containing the serialization
267 string of an object that contains the serialization
268 string of an inventory item. Fuck this.
271 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
272 <<"because an item-object should never be inside "
273 <<"an object-item."<<std::endl;
286 InventoryList::InventoryList(std::string name, u32 size)
294 InventoryList::~InventoryList()
296 for(u32 i=0; i<m_items.size(); i++)
303 void InventoryList::clearItems()
305 for(u32 i=0; i<m_items.size(); i++)
313 for(u32 i=0; i<m_size; i++)
315 m_items.push_back(NULL);
321 void InventoryList::serialize(std::ostream &os)
323 //os.imbue(std::locale("C"));
325 for(u32 i=0; i<m_items.size(); i++)
327 InventoryItem *item = m_items[i];
340 os<<"EndInventoryList\n";
343 void InventoryList::deSerialize(std::istream &is)
345 //is.imbue(std::locale("C"));
353 std::getline(is, line, '\n');
355 std::istringstream iss(line);
356 //iss.imbue(std::locale("C"));
359 std::getline(iss, name, ' ');
361 if(name == "EndInventoryList")
365 // This is a temporary backwards compatibility fix
366 else if(name == "end")
370 else if(name == "Item")
372 if(item_i > getSize() - 1)
373 throw SerializationError("too many items");
374 InventoryItem *item = InventoryItem::deSerialize(iss);
375 m_items[item_i++] = item;
377 else if(name == "Empty")
379 if(item_i > getSize() - 1)
380 throw SerializationError("too many items");
381 m_items[item_i++] = NULL;
385 throw SerializationError("Unknown inventory identifier");
390 InventoryList::InventoryList(const InventoryList &other)
393 Do this so that the items get cloned. Otherwise the pointers
394 in the array will just get copied.
399 InventoryList & InventoryList::operator = (const InventoryList &other)
401 m_name = other.m_name;
402 m_size = other.m_size;
404 for(u32 i=0; i<other.m_items.size(); i++)
406 InventoryItem *item = other.m_items[i];
409 m_items[i] = item->clone();
417 std::string InventoryList::getName()
422 u32 InventoryList::getSize()
424 return m_items.size();
427 u32 InventoryList::getUsedSlots()
430 for(u32 i=0; i<m_items.size(); i++)
432 InventoryItem *item = m_items[i];
439 u32 InventoryList::getFreeSlots()
441 return getSize() - getUsedSlots();
444 InventoryItem * InventoryList::getItem(u32 i)
446 if(i > m_items.size() - 1)
451 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
453 assert(i < m_items.size());
455 InventoryItem *olditem = m_items[i];
456 m_items[i] = newitem;
461 void InventoryList::deleteItem(u32 i)
463 assert(i < m_items.size());
464 InventoryItem *item = changeItem(i, NULL);
469 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
475 First try to find if it could be added to some existing items
477 for(u32 i=0; i<m_items.size(); i++)
479 // Ignore empty slots
480 if(m_items[i] == NULL)
483 newitem = addItem(i, newitem);
485 return NULL; // All was eaten
489 Then try to add it to empty slots
491 for(u32 i=0; i<m_items.size(); i++)
493 // Ignore unempty slots
494 if(m_items[i] != NULL)
497 newitem = addItem(i, newitem);
499 return NULL; // All was eaten
506 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
513 // If it is an empty position, it's an easy job.
514 InventoryItem *to_item = m_items[i];
517 m_items[i] = newitem;
521 // If not addable, return the item
522 if(newitem->addableTo(to_item) == false)
525 // If the item fits fully in the slot, add counter and delete it
526 if(newitem->getCount() <= to_item->freeSpace())
528 to_item->add(newitem->getCount());
532 // Else the item does not fit fully. Add all that fits and return
536 u16 freespace = to_item->freeSpace();
537 to_item->add(freespace);
538 newitem->remove(freespace);
543 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
545 // If it is an empty position, it's an easy job.
546 InventoryItem *to_item = m_items[i];
552 // If not addable, return the item
553 if(newitem->addableTo(to_item) == false)
556 // If the item fits fully in the slot, add counter and delete it
557 if(newitem->getCount() <= to_item->freeSpace())
565 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
572 InventoryItem *item = m_items[i];
573 // If it is an empty position, return NULL
577 if(count >= item->getCount())
579 // Get the item by swapping NULL to its place
580 return changeItem(i, NULL);
584 InventoryItem *item2 = item->clone();
586 item2->setCount(count);
593 void InventoryList::decrementMaterials(u16 count)
595 for(u32 i=0; i<m_items.size(); i++)
597 InventoryItem *item = takeItem(i, count);
603 void InventoryList::print(std::ostream &o)
605 o<<"InventoryList:"<<std::endl;
606 for(u32 i=0; i<m_items.size(); i++)
608 InventoryItem *item = m_items[i];
622 Inventory::~Inventory()
627 void Inventory::clear()
629 for(u32 i=0; i<m_lists.size(); i++)
636 Inventory::Inventory()
640 Inventory::Inventory(const Inventory &other)
645 Inventory & Inventory::operator = (const Inventory &other)
648 for(u32 i=0; i<other.m_lists.size(); i++)
650 m_lists.push_back(new InventoryList(*other.m_lists[i]));
655 void Inventory::serialize(std::ostream &os)
657 for(u32 i=0; i<m_lists.size(); i++)
659 InventoryList *list = m_lists[i];
660 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
664 os<<"EndInventory\n";
667 void Inventory::deSerialize(std::istream &is)
674 std::getline(is, line, '\n');
676 std::istringstream iss(line);
679 std::getline(iss, name, ' ');
681 if(name == "EndInventory")
685 // This is a temporary backwards compatibility fix
686 else if(name == "end")
690 else if(name == "List")
692 std::string listname;
695 std::getline(iss, listname, ' ');
698 InventoryList *list = new InventoryList(listname, listsize);
699 list->deSerialize(is);
701 m_lists.push_back(list);
705 throw SerializationError("Unknown inventory identifier");
710 InventoryList * Inventory::addList(const std::string &name, u32 size)
712 s32 i = getListIndex(name);
715 if(m_lists[i]->getSize() != size)
718 m_lists[i] = new InventoryList(name, size);
724 m_lists.push_back(new InventoryList(name, size));
725 return m_lists.getLast();
729 InventoryList * Inventory::getList(const std::string &name)
731 s32 i = getListIndex(name);
737 s32 Inventory::getListIndex(const std::string &name)
739 for(u32 i=0; i<m_lists.size(); i++)
741 if(m_lists[i]->getName() == name)
751 InventoryAction * InventoryAction::deSerialize(std::istream &is)
754 std::getline(is, type, ' ');
756 InventoryAction *a = NULL;
760 a = new IMoveAction(is);
766 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
770 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
771 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
772 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
774 Inventory *inv_from = mgr->getInventory(c, from_inv);
775 Inventory *inv_to = mgr->getInventory(c, to_inv);
777 if(!inv_from || !inv_to)
779 dstream<<__FUNCTION_NAME<<": Operation not allowed "
780 <<"(inventories not found)"<<std::endl;
784 InventoryList *list_from = inv_from->getList(from_list);
785 InventoryList *list_to = inv_to->getList(to_list);
787 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
790 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
793 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
797 If a list doesn't exist or the source item doesn't exist
799 if(!list_from || !list_to)
801 dstream<<__FUNCTION_NAME<<": Operation not allowed "
802 <<"(a list doesn't exist)"
806 if(list_from->getItem(from_i) == NULL)
808 dstream<<__FUNCTION_NAME<<": Operation not allowed "
809 <<"(the source item doesn't exist)"
814 If the source and the destination slots are the same
816 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
818 dstream<<__FUNCTION_NAME<<": Operation not allowed "
819 <<"(source and the destination slots are the same)"<<std::endl;
823 // Take item from source list
824 InventoryItem *item1 = NULL;
826 item1 = list_from->changeItem(from_i, NULL);
828 item1 = list_from->takeItem(from_i, count);
830 // Try to add the item to destination list
831 InventoryItem *olditem = item1;
832 item1 = list_to->addItem(to_i, item1);
834 // If something is returned, the item was not fully added
837 // If olditem is returned, nothing was added.
838 bool nothing_added = (item1 == olditem);
840 // If something else is returned, part of the item was left unadded.
841 // Add the other part back to the source item
842 list_from->addItem(from_i, item1);
844 // If olditem is returned, nothing was added.
848 // Take item from source list
849 item1 = list_from->changeItem(from_i, NULL);
850 // Adding was not possible, swap the items.
851 InventoryItem *item2 = list_to->changeItem(to_i, item1);
852 // Put item from destination list to the source list
853 list_from->changeItem(from_i, item2);
857 mgr->inventoryModified(c, from_inv);
858 if(from_inv != to_inv)
859 mgr->inventoryModified(c, to_inv);
864 Craft checking system
867 bool ItemSpec::checkItem(InventoryItem *item)
869 if(type == ITEM_NONE)
877 // There should be an item
881 std::string itemname = item->getName();
883 if(type == ITEM_MATERIAL)
885 if(itemname != "MaterialItem")
887 MaterialItem *mitem = (MaterialItem*)item;
888 if(mitem->getMaterial() != num)
891 else if(type == ITEM_CRAFT)
893 if(itemname != "CraftItem")
895 CraftItem *mitem = (CraftItem*)item;
896 if(mitem->getSubName() != name)
899 else if(type == ITEM_TOOL)
904 else if(type == ITEM_MBO)
917 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
919 u16 items_min_x = 100;
920 u16 items_max_x = 100;
921 u16 items_min_y = 100;
922 u16 items_max_y = 100;
923 for(u16 y=0; y<3; y++)
924 for(u16 x=0; x<3; x++)
926 if(items[y*3 + x] == NULL)
928 if(items_min_x == 100 || x < items_min_x)
930 if(items_min_y == 100 || y < items_min_y)
932 if(items_max_x == 100 || x > items_max_x)
934 if(items_max_y == 100 || y > items_max_y)
937 // No items at all, just return false
938 if(items_min_x == 100)
941 u16 items_w = items_max_x - items_min_x + 1;
942 u16 items_h = items_max_y - items_min_y + 1;
944 u16 specs_min_x = 100;
945 u16 specs_max_x = 100;
946 u16 specs_min_y = 100;
947 u16 specs_max_y = 100;
948 for(u16 y=0; y<3; y++)
949 for(u16 x=0; x<3; x++)
951 if(specs[y*3 + x].type == ITEM_NONE)
953 if(specs_min_x == 100 || x < specs_min_x)
955 if(specs_min_y == 100 || y < specs_min_y)
957 if(specs_max_x == 100 || x > specs_max_x)
959 if(specs_max_y == 100 || y > specs_max_y)
962 // No specs at all, just return false
963 if(specs_min_x == 100)
966 u16 specs_w = specs_max_x - specs_min_x + 1;
967 u16 specs_h = specs_max_y - specs_min_y + 1;
970 if(items_w != specs_w || items_h != specs_h)
973 for(u16 y=0; y<specs_h; y++)
974 for(u16 x=0; x<specs_w; x++)
976 u16 items_x = items_min_x + x;
977 u16 items_y = items_min_y + y;
978 u16 specs_x = specs_min_x + x;
979 u16 specs_y = specs_min_y + y;
980 InventoryItem *item = items[items_y * 3 + items_x];
981 ItemSpec &spec = specs[specs_y * 3 + specs_x];
983 if(spec.checkItem(item) == false)