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)
121 else if(m_content == CONTENT_SAND)
128 InventoryItem *MaterialItem::createCookResult()
130 if(m_content == CONTENT_TREE)
132 return new CraftItem("lump_of_coal", 1);
134 else if(m_content == CONTENT_COBBLE)
136 return new MaterialItem(CONTENT_STONE, 1);
138 else if(m_content == CONTENT_SAND)
140 return new MaterialItem(CONTENT_GLASS, 1);
150 video::ITexture * CraftItem::getImage()
152 if(g_texturesource == NULL)
157 if(m_subname == "Stick")
159 else if(m_subname == "lump_of_coal")
160 name = "lump_of_coal.png";
161 else if(m_subname == "lump_of_iron")
162 name = "lump_of_iron.png";
163 else if(m_subname == "steel_ingot")
164 name = "steel_ingot.png";
165 else if(m_subname == "rat")
170 // Get such a texture
171 return g_texturesource->getTextureRaw(name);
175 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
178 if(m_subname == "rat")
180 ServerActiveObject *obj = new RatSAO(env, id, pos);
186 return InventoryItem::createSAO(env, id, pos);
190 u16 CraftItem::getDropCount()
193 if(m_subname == "rat")
197 return InventoryItem::getDropCount();
200 bool CraftItem::isCookable()
202 if(m_subname == "lump_of_iron")
209 InventoryItem *CraftItem::createCookResult()
211 if(m_subname == "lump_of_iron")
213 return new CraftItem("steel_ingot", 1);
223 video::ITexture * MapBlockObjectItem::getImage()
225 if(m_inventorystring.substr(0,3) == "Rat")
226 return g_texturesource->getTextureRaw("rat.png");
228 if(m_inventorystring.substr(0,4) == "Sign")
229 return g_texturesource->getTextureRaw("sign.png");
234 std::string MapBlockObjectItem::getText()
236 if(m_inventorystring.substr(0,3) == "Rat")
239 if(m_inventorystring.substr(0,4) == "Sign")
245 MapBlockObject * MapBlockObjectItem::createObject
246 (v3f pos, f32 player_yaw, f32 player_pitch)
248 std::istringstream is(m_inventorystring);
250 std::getline(is, name, ' ');
256 else if(name == "Sign")
259 std::getline(is, text, '|');
260 SignObject *obj = new SignObject(NULL, -1, pos);
262 obj->setYaw(-player_yaw);
265 else if(name == "Rat")
267 RatObject *obj = new RatObject(NULL, -1, pos);
270 else if(name == "ItemObj")
273 Now we are an inventory item containing the serialization
274 string of an object that contains the serialization
275 string of an inventory item. Fuck this.
278 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
279 <<"because an item-object should never be inside "
280 <<"an object-item."<<std::endl;
293 InventoryList::InventoryList(std::string name, u32 size)
301 InventoryList::~InventoryList()
303 for(u32 i=0; i<m_items.size(); i++)
310 void InventoryList::clearItems()
312 for(u32 i=0; i<m_items.size(); i++)
320 for(u32 i=0; i<m_size; i++)
322 m_items.push_back(NULL);
328 void InventoryList::serialize(std::ostream &os)
330 //os.imbue(std::locale("C"));
332 for(u32 i=0; i<m_items.size(); i++)
334 InventoryItem *item = m_items[i];
347 os<<"EndInventoryList\n";
350 void InventoryList::deSerialize(std::istream &is)
352 //is.imbue(std::locale("C"));
360 std::getline(is, line, '\n');
362 std::istringstream iss(line);
363 //iss.imbue(std::locale("C"));
366 std::getline(iss, name, ' ');
368 if(name == "EndInventoryList")
372 // This is a temporary backwards compatibility fix
373 else if(name == "end")
377 else if(name == "Item")
379 if(item_i > getSize() - 1)
380 throw SerializationError("too many items");
381 InventoryItem *item = InventoryItem::deSerialize(iss);
382 m_items[item_i++] = item;
384 else if(name == "Empty")
386 if(item_i > getSize() - 1)
387 throw SerializationError("too many items");
388 m_items[item_i++] = NULL;
392 throw SerializationError("Unknown inventory identifier");
397 InventoryList::InventoryList(const InventoryList &other)
400 Do this so that the items get cloned. Otherwise the pointers
401 in the array will just get copied.
406 InventoryList & InventoryList::operator = (const InventoryList &other)
408 m_name = other.m_name;
409 m_size = other.m_size;
411 for(u32 i=0; i<other.m_items.size(); i++)
413 InventoryItem *item = other.m_items[i];
416 m_items[i] = item->clone();
424 std::string InventoryList::getName()
429 u32 InventoryList::getSize()
431 return m_items.size();
434 u32 InventoryList::getUsedSlots()
437 for(u32 i=0; i<m_items.size(); i++)
439 InventoryItem *item = m_items[i];
446 u32 InventoryList::getFreeSlots()
448 return getSize() - getUsedSlots();
451 InventoryItem * InventoryList::getItem(u32 i)
453 if(i > m_items.size() - 1)
458 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
460 assert(i < m_items.size());
462 InventoryItem *olditem = m_items[i];
463 m_items[i] = newitem;
468 void InventoryList::deleteItem(u32 i)
470 assert(i < m_items.size());
471 InventoryItem *item = changeItem(i, NULL);
476 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
482 First try to find if it could be added to some existing items
484 for(u32 i=0; i<m_items.size(); i++)
486 // Ignore empty slots
487 if(m_items[i] == NULL)
490 newitem = addItem(i, newitem);
492 return NULL; // All was eaten
496 Then try to add it to empty slots
498 for(u32 i=0; i<m_items.size(); i++)
500 // Ignore unempty slots
501 if(m_items[i] != NULL)
504 newitem = addItem(i, newitem);
506 return NULL; // All was eaten
513 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
520 // If it is an empty position, it's an easy job.
521 InventoryItem *to_item = m_items[i];
524 m_items[i] = newitem;
528 // If not addable, return the item
529 if(newitem->addableTo(to_item) == false)
532 // If the item fits fully in the slot, add counter and delete it
533 if(newitem->getCount() <= to_item->freeSpace())
535 to_item->add(newitem->getCount());
539 // Else the item does not fit fully. Add all that fits and return
543 u16 freespace = to_item->freeSpace();
544 to_item->add(freespace);
545 newitem->remove(freespace);
550 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
552 // If it is an empty position, it's an easy job.
553 InventoryItem *to_item = m_items[i];
559 // If not addable, return the item
560 if(newitem->addableTo(to_item) == false)
563 // If the item fits fully in the slot, add counter and delete it
564 if(newitem->getCount() <= to_item->freeSpace())
572 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
579 InventoryItem *item = m_items[i];
580 // If it is an empty position, return NULL
584 if(count >= item->getCount())
586 // Get the item by swapping NULL to its place
587 return changeItem(i, NULL);
591 InventoryItem *item2 = item->clone();
593 item2->setCount(count);
600 void InventoryList::decrementMaterials(u16 count)
602 for(u32 i=0; i<m_items.size(); i++)
604 InventoryItem *item = takeItem(i, count);
610 void InventoryList::print(std::ostream &o)
612 o<<"InventoryList:"<<std::endl;
613 for(u32 i=0; i<m_items.size(); i++)
615 InventoryItem *item = m_items[i];
629 Inventory::~Inventory()
634 void Inventory::clear()
636 for(u32 i=0; i<m_lists.size(); i++)
643 Inventory::Inventory()
647 Inventory::Inventory(const Inventory &other)
652 Inventory & Inventory::operator = (const Inventory &other)
655 for(u32 i=0; i<other.m_lists.size(); i++)
657 m_lists.push_back(new InventoryList(*other.m_lists[i]));
662 void Inventory::serialize(std::ostream &os)
664 for(u32 i=0; i<m_lists.size(); i++)
666 InventoryList *list = m_lists[i];
667 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
671 os<<"EndInventory\n";
674 void Inventory::deSerialize(std::istream &is)
681 std::getline(is, line, '\n');
683 std::istringstream iss(line);
686 std::getline(iss, name, ' ');
688 if(name == "EndInventory")
692 // This is a temporary backwards compatibility fix
693 else if(name == "end")
697 else if(name == "List")
699 std::string listname;
702 std::getline(iss, listname, ' ');
705 InventoryList *list = new InventoryList(listname, listsize);
706 list->deSerialize(is);
708 m_lists.push_back(list);
712 throw SerializationError("Unknown inventory identifier");
717 InventoryList * Inventory::addList(const std::string &name, u32 size)
719 s32 i = getListIndex(name);
722 if(m_lists[i]->getSize() != size)
725 m_lists[i] = new InventoryList(name, size);
731 m_lists.push_back(new InventoryList(name, size));
732 return m_lists.getLast();
736 InventoryList * Inventory::getList(const std::string &name)
738 s32 i = getListIndex(name);
744 s32 Inventory::getListIndex(const std::string &name)
746 for(u32 i=0; i<m_lists.size(); i++)
748 if(m_lists[i]->getName() == name)
758 InventoryAction * InventoryAction::deSerialize(std::istream &is)
761 std::getline(is, type, ' ');
763 InventoryAction *a = NULL;
767 a = new IMoveAction(is);
773 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
777 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
778 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
779 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
781 Inventory *inv_from = mgr->getInventory(c, from_inv);
782 Inventory *inv_to = mgr->getInventory(c, to_inv);
784 if(!inv_from || !inv_to)
786 dstream<<__FUNCTION_NAME<<": Operation not allowed "
787 <<"(inventories not found)"<<std::endl;
791 InventoryList *list_from = inv_from->getList(from_list);
792 InventoryList *list_to = inv_to->getList(to_list);
794 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
797 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
800 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
804 If a list doesn't exist or the source item doesn't exist
806 if(!list_from || !list_to)
808 dstream<<__FUNCTION_NAME<<": Operation not allowed "
809 <<"(a list doesn't exist)"
813 if(list_from->getItem(from_i) == NULL)
815 dstream<<__FUNCTION_NAME<<": Operation not allowed "
816 <<"(the source item doesn't exist)"
821 If the source and the destination slots are the same
823 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
825 dstream<<__FUNCTION_NAME<<": Operation not allowed "
826 <<"(source and the destination slots are the same)"<<std::endl;
830 // Take item from source list
831 InventoryItem *item1 = NULL;
833 item1 = list_from->changeItem(from_i, NULL);
835 item1 = list_from->takeItem(from_i, count);
837 // Try to add the item to destination list
838 InventoryItem *olditem = item1;
839 item1 = list_to->addItem(to_i, item1);
841 // If something is returned, the item was not fully added
844 // If olditem is returned, nothing was added.
845 bool nothing_added = (item1 == olditem);
847 // If something else is returned, part of the item was left unadded.
848 // Add the other part back to the source item
849 list_from->addItem(from_i, item1);
851 // If olditem is returned, nothing was added.
855 // Take item from source list
856 item1 = list_from->changeItem(from_i, NULL);
857 // Adding was not possible, swap the items.
858 InventoryItem *item2 = list_to->changeItem(to_i, item1);
859 // Put item from destination list to the source list
860 list_from->changeItem(from_i, item2);
864 mgr->inventoryModified(c, from_inv);
865 if(from_inv != to_inv)
866 mgr->inventoryModified(c, to_inv);
871 Craft checking system
874 bool ItemSpec::checkItem(InventoryItem *item)
876 if(type == ITEM_NONE)
884 // There should be an item
888 std::string itemname = item->getName();
890 if(type == ITEM_MATERIAL)
892 if(itemname != "MaterialItem")
894 MaterialItem *mitem = (MaterialItem*)item;
895 if(mitem->getMaterial() != num)
898 else if(type == ITEM_CRAFT)
900 if(itemname != "CraftItem")
902 CraftItem *mitem = (CraftItem*)item;
903 if(mitem->getSubName() != name)
906 else if(type == ITEM_TOOL)
911 else if(type == ITEM_MBO)
924 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
926 u16 items_min_x = 100;
927 u16 items_max_x = 100;
928 u16 items_min_y = 100;
929 u16 items_max_y = 100;
930 for(u16 y=0; y<3; y++)
931 for(u16 x=0; x<3; x++)
933 if(items[y*3 + x] == NULL)
935 if(items_min_x == 100 || x < items_min_x)
937 if(items_min_y == 100 || y < items_min_y)
939 if(items_max_x == 100 || x > items_max_x)
941 if(items_max_y == 100 || y > items_max_y)
944 // No items at all, just return false
945 if(items_min_x == 100)
948 u16 items_w = items_max_x - items_min_x + 1;
949 u16 items_h = items_max_y - items_min_y + 1;
951 u16 specs_min_x = 100;
952 u16 specs_max_x = 100;
953 u16 specs_min_y = 100;
954 u16 specs_max_y = 100;
955 for(u16 y=0; y<3; y++)
956 for(u16 x=0; x<3; x++)
958 if(specs[y*3 + x].type == ITEM_NONE)
960 if(specs_min_x == 100 || x < specs_min_x)
962 if(specs_min_y == 100 || y < specs_min_y)
964 if(specs_max_x == 100 || x > specs_max_x)
966 if(specs_max_y == 100 || y > specs_max_y)
969 // No specs at all, just return false
970 if(specs_min_x == 100)
973 u16 specs_w = specs_max_x - specs_min_x + 1;
974 u16 specs_h = specs_max_y - specs_min_y + 1;
977 if(items_w != specs_w || items_h != specs_h)
980 for(u16 y=0; y<specs_h; y++)
981 for(u16 x=0; x<specs_w; x++)
983 u16 items_x = items_min_x + x;
984 u16 items_y = items_min_y + y;
985 u16 specs_x = specs_min_x + x;
986 u16 specs_y = specs_min_y + y;
987 InventoryItem *item = items[items_y * 3 + items_x];
988 ItemSpec &spec = specs[specs_y * 3 + specs_x];
990 if(spec.checkItem(item) == false)