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 == "lump_of_clay")
164 name = "lump_of_clay.png";
165 else if(m_subname == "steel_ingot")
166 name = "steel_ingot.png";
167 else if(m_subname == "clay_brick")
168 name = "clay_brick.png";
169 else if(m_subname == "rat")
174 // Get such a texture
175 return g_texturesource->getTextureRaw(name);
179 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
182 if(m_subname == "rat")
184 ServerActiveObject *obj = new RatSAO(env, id, pos);
190 return InventoryItem::createSAO(env, id, pos);
194 u16 CraftItem::getDropCount()
197 if(m_subname == "rat")
201 return InventoryItem::getDropCount();
204 bool CraftItem::isCookable()
206 if(m_subname == "lump_of_iron" || m_subname == "lump_of_clay")
213 InventoryItem *CraftItem::createCookResult()
215 if(m_subname == "lump_of_iron")
217 return new CraftItem("steel_ingot", 1);
219 else if(m_subname == "lump_of_clay")
220 return new CraftItem("clay_brick", 1);
229 video::ITexture * MapBlockObjectItem::getImage()
231 if(m_inventorystring.substr(0,3) == "Rat")
232 return g_texturesource->getTextureRaw("rat.png");
234 if(m_inventorystring.substr(0,4) == "Sign")
235 return g_texturesource->getTextureRaw("sign.png");
240 std::string MapBlockObjectItem::getText()
242 if(m_inventorystring.substr(0,3) == "Rat")
245 if(m_inventorystring.substr(0,4) == "Sign")
251 MapBlockObject * MapBlockObjectItem::createObject
252 (v3f pos, f32 player_yaw, f32 player_pitch)
254 std::istringstream is(m_inventorystring);
256 std::getline(is, name, ' ');
262 else if(name == "Sign")
265 std::getline(is, text, '|');
266 SignObject *obj = new SignObject(NULL, -1, pos);
268 obj->setYaw(-player_yaw);
271 else if(name == "Rat")
273 RatObject *obj = new RatObject(NULL, -1, pos);
276 else if(name == "ItemObj")
279 Now we are an inventory item containing the serialization
280 string of an object that contains the serialization
281 string of an inventory item. Fuck this.
284 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
285 <<"because an item-object should never be inside "
286 <<"an object-item."<<std::endl;
299 InventoryList::InventoryList(std::string name, u32 size)
307 InventoryList::~InventoryList()
309 for(u32 i=0; i<m_items.size(); i++)
316 void InventoryList::clearItems()
318 for(u32 i=0; i<m_items.size(); i++)
326 for(u32 i=0; i<m_size; i++)
328 m_items.push_back(NULL);
334 void InventoryList::serialize(std::ostream &os)
336 //os.imbue(std::locale("C"));
338 for(u32 i=0; i<m_items.size(); i++)
340 InventoryItem *item = m_items[i];
353 os<<"EndInventoryList\n";
356 void InventoryList::deSerialize(std::istream &is)
358 //is.imbue(std::locale("C"));
366 std::getline(is, line, '\n');
368 std::istringstream iss(line);
369 //iss.imbue(std::locale("C"));
372 std::getline(iss, name, ' ');
374 if(name == "EndInventoryList")
378 // This is a temporary backwards compatibility fix
379 else if(name == "end")
383 else if(name == "Item")
385 if(item_i > getSize() - 1)
386 throw SerializationError("too many items");
387 InventoryItem *item = InventoryItem::deSerialize(iss);
388 m_items[item_i++] = item;
390 else if(name == "Empty")
392 if(item_i > getSize() - 1)
393 throw SerializationError("too many items");
394 m_items[item_i++] = NULL;
398 throw SerializationError("Unknown inventory identifier");
403 InventoryList::InventoryList(const InventoryList &other)
406 Do this so that the items get cloned. Otherwise the pointers
407 in the array will just get copied.
412 InventoryList & InventoryList::operator = (const InventoryList &other)
414 m_name = other.m_name;
415 m_size = other.m_size;
417 for(u32 i=0; i<other.m_items.size(); i++)
419 InventoryItem *item = other.m_items[i];
422 m_items[i] = item->clone();
430 std::string InventoryList::getName()
435 u32 InventoryList::getSize()
437 return m_items.size();
440 u32 InventoryList::getUsedSlots()
443 for(u32 i=0; i<m_items.size(); i++)
445 InventoryItem *item = m_items[i];
452 u32 InventoryList::getFreeSlots()
454 return getSize() - getUsedSlots();
457 InventoryItem * InventoryList::getItem(u32 i)
459 if(i > m_items.size() - 1)
464 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
466 assert(i < m_items.size());
468 InventoryItem *olditem = m_items[i];
469 m_items[i] = newitem;
474 void InventoryList::deleteItem(u32 i)
476 assert(i < m_items.size());
477 InventoryItem *item = changeItem(i, NULL);
482 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
488 First try to find if it could be added to some existing items
490 for(u32 i=0; i<m_items.size(); i++)
492 // Ignore empty slots
493 if(m_items[i] == NULL)
496 newitem = addItem(i, newitem);
498 return NULL; // All was eaten
502 Then try to add it to empty slots
504 for(u32 i=0; i<m_items.size(); i++)
506 // Ignore unempty slots
507 if(m_items[i] != NULL)
510 newitem = addItem(i, newitem);
512 return NULL; // All was eaten
519 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
526 // If it is an empty position, it's an easy job.
527 InventoryItem *to_item = m_items[i];
530 m_items[i] = newitem;
534 // If not addable, return the item
535 if(newitem->addableTo(to_item) == false)
538 // If the item fits fully in the slot, add counter and delete it
539 if(newitem->getCount() <= to_item->freeSpace())
541 to_item->add(newitem->getCount());
545 // Else the item does not fit fully. Add all that fits and return
549 u16 freespace = to_item->freeSpace();
550 to_item->add(freespace);
551 newitem->remove(freespace);
556 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
558 // If it is an empty position, it's an easy job.
559 InventoryItem *to_item = m_items[i];
565 // If not addable, return the item
566 if(newitem->addableTo(to_item) == false)
569 // If the item fits fully in the slot, add counter and delete it
570 if(newitem->getCount() <= to_item->freeSpace())
578 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
585 InventoryItem *item = m_items[i];
586 // If it is an empty position, return NULL
590 if(count >= item->getCount())
592 // Get the item by swapping NULL to its place
593 return changeItem(i, NULL);
597 InventoryItem *item2 = item->clone();
599 item2->setCount(count);
606 void InventoryList::decrementMaterials(u16 count)
608 for(u32 i=0; i<m_items.size(); i++)
610 InventoryItem *item = takeItem(i, count);
616 void InventoryList::print(std::ostream &o)
618 o<<"InventoryList:"<<std::endl;
619 for(u32 i=0; i<m_items.size(); i++)
621 InventoryItem *item = m_items[i];
635 Inventory::~Inventory()
640 void Inventory::clear()
642 for(u32 i=0; i<m_lists.size(); i++)
649 Inventory::Inventory()
653 Inventory::Inventory(const Inventory &other)
658 Inventory & Inventory::operator = (const Inventory &other)
661 for(u32 i=0; i<other.m_lists.size(); i++)
663 m_lists.push_back(new InventoryList(*other.m_lists[i]));
668 void Inventory::serialize(std::ostream &os)
670 for(u32 i=0; i<m_lists.size(); i++)
672 InventoryList *list = m_lists[i];
673 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
677 os<<"EndInventory\n";
680 void Inventory::deSerialize(std::istream &is)
687 std::getline(is, line, '\n');
689 std::istringstream iss(line);
692 std::getline(iss, name, ' ');
694 if(name == "EndInventory")
698 // This is a temporary backwards compatibility fix
699 else if(name == "end")
703 else if(name == "List")
705 std::string listname;
708 std::getline(iss, listname, ' ');
711 InventoryList *list = new InventoryList(listname, listsize);
712 list->deSerialize(is);
714 m_lists.push_back(list);
718 throw SerializationError("Unknown inventory identifier");
723 InventoryList * Inventory::addList(const std::string &name, u32 size)
725 s32 i = getListIndex(name);
728 if(m_lists[i]->getSize() != size)
731 m_lists[i] = new InventoryList(name, size);
737 m_lists.push_back(new InventoryList(name, size));
738 return m_lists.getLast();
742 InventoryList * Inventory::getList(const std::string &name)
744 s32 i = getListIndex(name);
750 s32 Inventory::getListIndex(const std::string &name)
752 for(u32 i=0; i<m_lists.size(); i++)
754 if(m_lists[i]->getName() == name)
764 InventoryAction * InventoryAction::deSerialize(std::istream &is)
767 std::getline(is, type, ' ');
769 InventoryAction *a = NULL;
773 a = new IMoveAction(is);
779 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
783 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
784 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
785 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
787 Inventory *inv_from = mgr->getInventory(c, from_inv);
788 Inventory *inv_to = mgr->getInventory(c, to_inv);
790 if(!inv_from || !inv_to)
792 dstream<<__FUNCTION_NAME<<": Operation not allowed "
793 <<"(inventories not found)"<<std::endl;
797 InventoryList *list_from = inv_from->getList(from_list);
798 InventoryList *list_to = inv_to->getList(to_list);
800 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
803 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
806 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
810 If a list doesn't exist or the source item doesn't exist
812 if(!list_from || !list_to)
814 dstream<<__FUNCTION_NAME<<": Operation not allowed "
815 <<"(a list doesn't exist)"
819 if(list_from->getItem(from_i) == NULL)
821 dstream<<__FUNCTION_NAME<<": Operation not allowed "
822 <<"(the source item doesn't exist)"
827 If the source and the destination slots are the same
829 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
831 dstream<<__FUNCTION_NAME<<": Operation not allowed "
832 <<"(source and the destination slots are the same)"<<std::endl;
836 // Take item from source list
837 InventoryItem *item1 = NULL;
839 item1 = list_from->changeItem(from_i, NULL);
841 item1 = list_from->takeItem(from_i, count);
843 // Try to add the item to destination list
844 InventoryItem *olditem = item1;
845 item1 = list_to->addItem(to_i, item1);
847 // If something is returned, the item was not fully added
850 // If olditem is returned, nothing was added.
851 bool nothing_added = (item1 == olditem);
853 // If something else is returned, part of the item was left unadded.
854 // Add the other part back to the source item
855 list_from->addItem(from_i, item1);
857 // If olditem is returned, nothing was added.
861 // Take item from source list
862 item1 = list_from->changeItem(from_i, NULL);
863 // Adding was not possible, swap the items.
864 InventoryItem *item2 = list_to->changeItem(to_i, item1);
865 // Put item from destination list to the source list
866 list_from->changeItem(from_i, item2);
870 mgr->inventoryModified(c, from_inv);
871 if(from_inv != to_inv)
872 mgr->inventoryModified(c, to_inv);
877 Craft checking system
880 bool ItemSpec::checkItem(InventoryItem *item)
882 if(type == ITEM_NONE)
890 // There should be an item
894 std::string itemname = item->getName();
896 if(type == ITEM_MATERIAL)
898 if(itemname != "MaterialItem")
900 MaterialItem *mitem = (MaterialItem*)item;
901 if(mitem->getMaterial() != num)
904 else if(type == ITEM_CRAFT)
906 if(itemname != "CraftItem")
908 CraftItem *mitem = (CraftItem*)item;
909 if(mitem->getSubName() != name)
912 else if(type == ITEM_TOOL)
917 else if(type == ITEM_MBO)
930 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
932 u16 items_min_x = 100;
933 u16 items_max_x = 100;
934 u16 items_min_y = 100;
935 u16 items_max_y = 100;
936 for(u16 y=0; y<3; y++)
937 for(u16 x=0; x<3; x++)
939 if(items[y*3 + x] == NULL)
941 if(items_min_x == 100 || x < items_min_x)
943 if(items_min_y == 100 || y < items_min_y)
945 if(items_max_x == 100 || x > items_max_x)
947 if(items_max_y == 100 || y > items_max_y)
950 // No items at all, just return false
951 if(items_min_x == 100)
954 u16 items_w = items_max_x - items_min_x + 1;
955 u16 items_h = items_max_y - items_min_y + 1;
957 u16 specs_min_x = 100;
958 u16 specs_max_x = 100;
959 u16 specs_min_y = 100;
960 u16 specs_max_y = 100;
961 for(u16 y=0; y<3; y++)
962 for(u16 x=0; x<3; x++)
964 if(specs[y*3 + x].type == ITEM_NONE)
966 if(specs_min_x == 100 || x < specs_min_x)
968 if(specs_min_y == 100 || y < specs_min_y)
970 if(specs_max_x == 100 || x > specs_max_x)
972 if(specs_max_y == 100 || y > specs_max_y)
975 // No specs at all, just return false
976 if(specs_min_x == 100)
979 u16 specs_w = specs_max_x - specs_min_x + 1;
980 u16 specs_h = specs_max_y - specs_min_y + 1;
983 if(items_w != specs_w || items_h != specs_h)
986 for(u16 y=0; y<specs_h; y++)
987 for(u16 x=0; x<specs_w; x++)
989 u16 items_x = items_min_x + x;
990 u16 items_y = items_min_y + y;
991 u16 specs_x = specs_min_x + x;
992 u16 specs_y = specs_min_y + y;
993 InventoryItem *item = items[items_y * 3 + items_x];
994 ItemSpec &spec = specs[specs_y * 3 + specs_x];
996 if(spec.checkItem(item) == false)