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"
37 InventoryItem::InventoryItem(u16 count)
42 InventoryItem::~InventoryItem()
46 InventoryItem* InventoryItem::deSerialize(std::istream &is)
48 DSTACK(__FUNCTION_NAME);
50 //is.imbue(std::locale("C"));
53 std::getline(is, name, ' ');
55 if(name == "MaterialItem")
57 // u16 reads directly as a number (u8 doesn't)
63 throw SerializationError("Too large material number");
64 return new MaterialItem(material, count);
66 else if(name == "MBOItem")
68 std::string inventorystring;
69 std::getline(is, inventorystring, '|');
70 return new MapBlockObjectItem(inventorystring);
72 else if(name == "CraftItem")
75 std::getline(is, subname, ' ');
78 return new CraftItem(subname, count);
80 else if(name == "ToolItem")
83 std::getline(is, toolname, ' ');
86 return new ToolItem(toolname, wear);
90 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
91 throw SerializationError("Unknown InventoryItem name");
95 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
101 std::ostringstream os(std::ios_base::binary);
104 ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
112 bool MaterialItem::isCookable()
114 if(m_content == CONTENT_TREE)
118 else if(m_content == CONTENT_COBBLE)
122 else if(m_content == CONTENT_SAND)
129 InventoryItem *MaterialItem::createCookResult()
131 if(m_content == CONTENT_TREE)
133 return new CraftItem("lump_of_coal", 1);
135 else if(m_content == CONTENT_COBBLE)
137 return new MaterialItem(CONTENT_STONE, 1);
139 else if(m_content == CONTENT_SAND)
141 return new MaterialItem(CONTENT_GLASS, 1);
151 video::ITexture * CraftItem::getImage()
153 if(g_texturesource == NULL)
158 if(m_subname == "Stick")
160 else if(m_subname == "lump_of_coal")
161 name = "lump_of_coal.png";
162 else if(m_subname == "lump_of_iron")
163 name = "lump_of_iron.png";
164 else if(m_subname == "steel_ingot")
165 name = "steel_ingot.png";
166 else if(m_subname == "rat")
171 // Get such a texture
172 return g_texturesource->getTextureRaw(name);
176 ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
179 if(m_subname == "rat")
181 ServerActiveObject *obj = new RatSAO(env, id, pos);
187 return InventoryItem::createSAO(env, id, pos);
191 u16 CraftItem::getDropCount()
194 if(m_subname == "rat")
198 return InventoryItem::getDropCount();
201 bool CraftItem::isCookable()
203 if(m_subname == "lump_of_iron")
210 InventoryItem *CraftItem::createCookResult()
212 if(m_subname == "lump_of_iron")
214 return new CraftItem("steel_ingot", 1);
224 video::ITexture * MapBlockObjectItem::getImage()
226 if(m_inventorystring.substr(0,3) == "Rat")
227 return g_texturesource->getTextureRaw("rat.png");
229 if(m_inventorystring.substr(0,4) == "Sign")
230 return g_texturesource->getTextureRaw("sign.png");
235 std::string MapBlockObjectItem::getText()
237 if(m_inventorystring.substr(0,3) == "Rat")
240 if(m_inventorystring.substr(0,4) == "Sign")
246 MapBlockObject * MapBlockObjectItem::createObject
247 (v3f pos, f32 player_yaw, f32 player_pitch)
249 std::istringstream is(m_inventorystring);
251 std::getline(is, name, ' ');
257 else if(name == "Sign")
260 std::getline(is, text, '|');
261 SignObject *obj = new SignObject(NULL, -1, pos);
263 obj->setYaw(-player_yaw);
266 else if(name == "Rat")
268 RatObject *obj = new RatObject(NULL, -1, pos);
271 else if(name == "ItemObj")
274 Now we are an inventory item containing the serialization
275 string of an object that contains the serialization
276 string of an inventory item. Fuck this.
279 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
280 <<"because an item-object should never be inside "
281 <<"an object-item."<<std::endl;
294 InventoryList::InventoryList(std::string name, u32 size)
302 InventoryList::~InventoryList()
304 for(u32 i=0; i<m_items.size(); i++)
311 void InventoryList::clearItems()
313 for(u32 i=0; i<m_items.size(); i++)
321 for(u32 i=0; i<m_size; i++)
323 m_items.push_back(NULL);
329 void InventoryList::serialize(std::ostream &os)
331 //os.imbue(std::locale("C"));
333 for(u32 i=0; i<m_items.size(); i++)
335 InventoryItem *item = m_items[i];
348 os<<"EndInventoryList\n";
351 void InventoryList::deSerialize(std::istream &is)
353 //is.imbue(std::locale("C"));
361 std::getline(is, line, '\n');
363 std::istringstream iss(line);
364 //iss.imbue(std::locale("C"));
367 std::getline(iss, name, ' ');
369 if(name == "EndInventoryList")
373 // This is a temporary backwards compatibility fix
374 else if(name == "end")
378 else if(name == "Item")
380 if(item_i > getSize() - 1)
381 throw SerializationError("too many items");
382 InventoryItem *item = InventoryItem::deSerialize(iss);
383 m_items[item_i++] = item;
385 else if(name == "Empty")
387 if(item_i > getSize() - 1)
388 throw SerializationError("too many items");
389 m_items[item_i++] = NULL;
393 throw SerializationError("Unknown inventory identifier");
398 InventoryList::InventoryList(const InventoryList &other)
401 Do this so that the items get cloned. Otherwise the pointers
402 in the array will just get copied.
407 InventoryList & InventoryList::operator = (const InventoryList &other)
409 m_name = other.m_name;
410 m_size = other.m_size;
412 for(u32 i=0; i<other.m_items.size(); i++)
414 InventoryItem *item = other.m_items[i];
417 m_items[i] = item->clone();
425 std::string InventoryList::getName()
430 u32 InventoryList::getSize()
432 return m_items.size();
435 u32 InventoryList::getUsedSlots()
438 for(u32 i=0; i<m_items.size(); i++)
440 InventoryItem *item = m_items[i];
447 u32 InventoryList::getFreeSlots()
449 return getSize() - getUsedSlots();
452 InventoryItem * InventoryList::getItem(u32 i)
454 if(i > m_items.size() - 1)
459 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
461 assert(i < m_items.size());
463 InventoryItem *olditem = m_items[i];
464 m_items[i] = newitem;
469 void InventoryList::deleteItem(u32 i)
471 assert(i < m_items.size());
472 InventoryItem *item = changeItem(i, NULL);
477 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
483 First try to find if it could be added to some existing items
485 for(u32 i=0; i<m_items.size(); i++)
487 // Ignore empty slots
488 if(m_items[i] == NULL)
491 newitem = addItem(i, newitem);
493 return NULL; // All was eaten
497 Then try to add it to empty slots
499 for(u32 i=0; i<m_items.size(); i++)
501 // Ignore unempty slots
502 if(m_items[i] != NULL)
505 newitem = addItem(i, newitem);
507 return NULL; // All was eaten
514 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
521 // If it is an empty position, it's an easy job.
522 InventoryItem *to_item = m_items[i];
525 m_items[i] = newitem;
529 // If not addable, return the item
530 if(newitem->addableTo(to_item) == false)
533 // If the item fits fully in the slot, add counter and delete it
534 if(newitem->getCount() <= to_item->freeSpace())
536 to_item->add(newitem->getCount());
540 // Else the item does not fit fully. Add all that fits and return
544 u16 freespace = to_item->freeSpace();
545 to_item->add(freespace);
546 newitem->remove(freespace);
551 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
553 // If it is an empty position, it's an easy job.
554 InventoryItem *to_item = m_items[i];
560 // If not addable, return the item
561 if(newitem->addableTo(to_item) == false)
564 // If the item fits fully in the slot, add counter and delete it
565 if(newitem->getCount() <= to_item->freeSpace())
573 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
580 InventoryItem *item = m_items[i];
581 // If it is an empty position, return NULL
585 if(count >= item->getCount())
587 // Get the item by swapping NULL to its place
588 return changeItem(i, NULL);
592 InventoryItem *item2 = item->clone();
594 item2->setCount(count);
601 void InventoryList::decrementMaterials(u16 count)
603 for(u32 i=0; i<m_items.size(); i++)
605 InventoryItem *item = takeItem(i, count);
611 void InventoryList::print(std::ostream &o)
613 o<<"InventoryList:"<<std::endl;
614 for(u32 i=0; i<m_items.size(); i++)
616 InventoryItem *item = m_items[i];
630 Inventory::~Inventory()
635 void Inventory::clear()
637 for(u32 i=0; i<m_lists.size(); i++)
644 Inventory::Inventory()
648 Inventory::Inventory(const Inventory &other)
653 Inventory & Inventory::operator = (const Inventory &other)
656 for(u32 i=0; i<other.m_lists.size(); i++)
658 m_lists.push_back(new InventoryList(*other.m_lists[i]));
663 void Inventory::serialize(std::ostream &os)
665 for(u32 i=0; i<m_lists.size(); i++)
667 InventoryList *list = m_lists[i];
668 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
672 os<<"EndInventory\n";
675 void Inventory::deSerialize(std::istream &is)
682 std::getline(is, line, '\n');
684 std::istringstream iss(line);
687 std::getline(iss, name, ' ');
689 if(name == "EndInventory")
693 // This is a temporary backwards compatibility fix
694 else if(name == "end")
698 else if(name == "List")
700 std::string listname;
703 std::getline(iss, listname, ' ');
706 InventoryList *list = new InventoryList(listname, listsize);
707 list->deSerialize(is);
709 m_lists.push_back(list);
713 throw SerializationError("Unknown inventory identifier");
718 InventoryList * Inventory::addList(const std::string &name, u32 size)
720 s32 i = getListIndex(name);
723 if(m_lists[i]->getSize() != size)
726 m_lists[i] = new InventoryList(name, size);
732 m_lists.push_back(new InventoryList(name, size));
733 return m_lists.getLast();
737 InventoryList * Inventory::getList(const std::string &name)
739 s32 i = getListIndex(name);
745 s32 Inventory::getListIndex(const std::string &name)
747 for(u32 i=0; i<m_lists.size(); i++)
749 if(m_lists[i]->getName() == name)
759 InventoryAction * InventoryAction::deSerialize(std::istream &is)
762 std::getline(is, type, ' ');
764 InventoryAction *a = NULL;
768 a = new IMoveAction(is);
774 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
778 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
779 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
780 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
782 Inventory *inv_from = mgr->getInventory(c, from_inv);
783 Inventory *inv_to = mgr->getInventory(c, to_inv);
785 if(!inv_from || !inv_to)
787 dstream<<__FUNCTION_NAME<<": Operation not allowed "
788 <<"(inventories not found)"<<std::endl;
792 InventoryList *list_from = inv_from->getList(from_list);
793 InventoryList *list_to = inv_to->getList(to_list);
795 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
798 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
801 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
805 If a list doesn't exist or the source item doesn't exist
807 if(!list_from || !list_to)
809 dstream<<__FUNCTION_NAME<<": Operation not allowed "
810 <<"(a list doesn't exist)"
814 if(list_from->getItem(from_i) == NULL)
816 dstream<<__FUNCTION_NAME<<": Operation not allowed "
817 <<"(the source item doesn't exist)"
822 If the source and the destination slots are the same
824 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
826 dstream<<__FUNCTION_NAME<<": Operation not allowed "
827 <<"(source and the destination slots are the same)"<<std::endl;
831 // Take item from source list
832 InventoryItem *item1 = NULL;
834 item1 = list_from->changeItem(from_i, NULL);
836 item1 = list_from->takeItem(from_i, count);
838 // Try to add the item to destination list
839 InventoryItem *olditem = item1;
840 item1 = list_to->addItem(to_i, item1);
842 // If something is returned, the item was not fully added
845 // If olditem is returned, nothing was added.
846 bool nothing_added = (item1 == olditem);
848 // If something else is returned, part of the item was left unadded.
849 // Add the other part back to the source item
850 list_from->addItem(from_i, item1);
852 // If olditem is returned, nothing was added.
856 // Take item from source list
857 item1 = list_from->changeItem(from_i, NULL);
858 // Adding was not possible, swap the items.
859 InventoryItem *item2 = list_to->changeItem(to_i, item1);
860 // Put item from destination list to the source list
861 list_from->changeItem(from_i, item2);
865 mgr->inventoryModified(c, from_inv);
866 if(from_inv != to_inv)
867 mgr->inventoryModified(c, to_inv);
872 Craft checking system
875 bool ItemSpec::checkItem(InventoryItem *item)
877 if(type == ITEM_NONE)
885 // There should be an item
889 std::string itemname = item->getName();
891 if(type == ITEM_MATERIAL)
893 if(itemname != "MaterialItem")
895 MaterialItem *mitem = (MaterialItem*)item;
896 if(mitem->getMaterial() != num)
899 else if(type == ITEM_CRAFT)
901 if(itemname != "CraftItem")
903 CraftItem *mitem = (CraftItem*)item;
904 if(mitem->getSubName() != name)
907 else if(type == ITEM_TOOL)
912 else if(type == ITEM_MBO)
925 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
927 u16 items_min_x = 100;
928 u16 items_max_x = 100;
929 u16 items_min_y = 100;
930 u16 items_max_y = 100;
931 for(u16 y=0; y<3; y++)
932 for(u16 x=0; x<3; x++)
934 if(items[y*3 + x] == NULL)
936 if(items_min_x == 100 || x < items_min_x)
938 if(items_min_y == 100 || y < items_min_y)
940 if(items_max_x == 100 || x > items_max_x)
942 if(items_max_y == 100 || y > items_max_y)
945 // No items at all, just return false
946 if(items_min_x == 100)
949 u16 items_w = items_max_x - items_min_x + 1;
950 u16 items_h = items_max_y - items_min_y + 1;
952 u16 specs_min_x = 100;
953 u16 specs_max_x = 100;
954 u16 specs_min_y = 100;
955 u16 specs_max_y = 100;
956 for(u16 y=0; y<3; y++)
957 for(u16 x=0; x<3; x++)
959 if(specs[y*3 + x].type == ITEM_NONE)
961 if(specs_min_x == 100 || x < specs_min_x)
963 if(specs_min_y == 100 || y < specs_min_y)
965 if(specs_max_x == 100 || x > specs_max_x)
967 if(specs_max_y == 100 || y > specs_max_y)
970 // No specs at all, just return false
971 if(specs_min_x == 100)
974 u16 specs_w = specs_max_x - specs_min_x + 1;
975 u16 specs_h = specs_max_y - specs_min_y + 1;
978 if(items_w != specs_w || items_h != specs_h)
981 for(u16 y=0; y<specs_h; y++)
982 for(u16 x=0; x<specs_w; x++)
984 u16 items_x = items_min_x + x;
985 u16 items_y = items_min_y + y;
986 u16 specs_x = specs_min_x + x;
987 u16 specs_y = specs_min_y + y;
988 InventoryItem *item = items[items_y * 3 + items_x];
989 ItemSpec &spec = specs[specs_y * 3 + specs_x];
991 if(spec.checkItem(item) == false)