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 bool CraftItem::isCookable()
185 if(m_subname == "lump_of_iron")
192 InventoryItem *CraftItem::createCookResult()
194 if(m_subname == "lump_of_iron")
196 return new CraftItem("steel_ingot", 1);
206 video::ITexture * MapBlockObjectItem::getImage()
208 if(m_inventorystring.substr(0,3) == "Rat")
209 return g_texturesource->getTextureRaw("rat.png");
211 if(m_inventorystring.substr(0,4) == "Sign")
212 return g_texturesource->getTextureRaw("sign.png");
217 std::string MapBlockObjectItem::getText()
219 if(m_inventorystring.substr(0,3) == "Rat")
222 if(m_inventorystring.substr(0,4) == "Sign")
228 MapBlockObject * MapBlockObjectItem::createObject
229 (v3f pos, f32 player_yaw, f32 player_pitch)
231 std::istringstream is(m_inventorystring);
233 std::getline(is, name, ' ');
239 else if(name == "Sign")
242 std::getline(is, text, '|');
243 SignObject *obj = new SignObject(NULL, -1, pos);
245 obj->setYaw(-player_yaw);
248 else if(name == "Rat")
250 RatObject *obj = new RatObject(NULL, -1, pos);
253 else if(name == "ItemObj")
256 Now we are an inventory item containing the serialization
257 string of an object that contains the serialization
258 string of an inventory item. Fuck this.
261 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
262 <<"because an item-object should never be inside "
263 <<"an object-item."<<std::endl;
276 InventoryList::InventoryList(std::string name, u32 size)
283 InventoryList::~InventoryList()
285 for(u32 i=0; i<m_items.size(); i++)
292 void InventoryList::clearItems()
294 for(u32 i=0; i<m_items.size(); i++)
302 for(u32 i=0; i<m_size; i++)
304 m_items.push_back(NULL);
308 void InventoryList::serialize(std::ostream &os)
310 //os.imbue(std::locale("C"));
312 for(u32 i=0; i<m_items.size(); i++)
314 InventoryItem *item = m_items[i];
327 os<<"EndInventoryList\n";
330 void InventoryList::deSerialize(std::istream &is)
332 //is.imbue(std::locale("C"));
340 std::getline(is, line, '\n');
342 std::istringstream iss(line);
343 //iss.imbue(std::locale("C"));
346 std::getline(iss, name, ' ');
348 if(name == "EndInventoryList")
352 // This is a temporary backwards compatibility fix
353 else if(name == "end")
357 else if(name == "Item")
359 if(item_i > getSize() - 1)
360 throw SerializationError("too many items");
361 InventoryItem *item = InventoryItem::deSerialize(iss);
362 m_items[item_i++] = item;
364 else if(name == "Empty")
366 if(item_i > getSize() - 1)
367 throw SerializationError("too many items");
368 m_items[item_i++] = NULL;
372 throw SerializationError("Unknown inventory identifier");
377 InventoryList::InventoryList(const InventoryList &other)
380 Do this so that the items get cloned. Otherwise the pointers
381 in the array will just get copied.
386 InventoryList & InventoryList::operator = (const InventoryList &other)
388 m_name = other.m_name;
389 m_size = other.m_size;
391 for(u32 i=0; i<other.m_items.size(); i++)
393 InventoryItem *item = other.m_items[i];
396 m_items[i] = item->clone();
403 std::string InventoryList::getName()
408 u32 InventoryList::getSize()
410 return m_items.size();
413 u32 InventoryList::getUsedSlots()
416 for(u32 i=0; i<m_items.size(); i++)
418 InventoryItem *item = m_items[i];
425 u32 InventoryList::getFreeSlots()
427 return getSize() - getUsedSlots();
430 InventoryItem * InventoryList::getItem(u32 i)
432 if(i > m_items.size() - 1)
437 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
439 assert(i < m_items.size());
441 InventoryItem *olditem = m_items[i];
442 m_items[i] = newitem;
446 void InventoryList::deleteItem(u32 i)
448 assert(i < m_items.size());
449 InventoryItem *item = changeItem(i, NULL);
454 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
460 First try to find if it could be added to some existing items
462 for(u32 i=0; i<m_items.size(); i++)
464 // Ignore empty slots
465 if(m_items[i] == NULL)
468 newitem = addItem(i, newitem);
470 return NULL; // All was eaten
474 Then try to add it to empty slots
476 for(u32 i=0; i<m_items.size(); i++)
478 // Ignore unempty slots
479 if(m_items[i] != NULL)
482 newitem = addItem(i, newitem);
484 return NULL; // All was eaten
491 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
496 // If it is an empty position, it's an easy job.
497 InventoryItem *to_item = m_items[i];
500 m_items[i] = newitem;
504 // If not addable, return the item
505 if(newitem->addableTo(to_item) == false)
508 // If the item fits fully in the slot, add counter and delete it
509 if(newitem->getCount() <= to_item->freeSpace())
511 to_item->add(newitem->getCount());
515 // Else the item does not fit fully. Add all that fits and return
519 u16 freespace = to_item->freeSpace();
520 to_item->add(freespace);
521 newitem->remove(freespace);
526 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
528 // If it is an empty position, it's an easy job.
529 InventoryItem *to_item = m_items[i];
535 // If not addable, return the item
536 if(newitem->addableTo(to_item) == false)
539 // If the item fits fully in the slot, add counter and delete it
540 if(newitem->getCount() <= to_item->freeSpace())
548 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
553 InventoryItem *item = m_items[i];
554 // If it is an empty position, return NULL
558 if(count >= item->getCount())
560 // Get the item by swapping NULL to its place
561 return changeItem(i, NULL);
565 InventoryItem *item2 = item->clone();
567 item2->setCount(count);
574 void InventoryList::decrementMaterials(u16 count)
576 for(u32 i=0; i<m_items.size(); i++)
578 InventoryItem *item = takeItem(i, count);
584 void InventoryList::print(std::ostream &o)
586 o<<"InventoryList:"<<std::endl;
587 for(u32 i=0; i<m_items.size(); i++)
589 InventoryItem *item = m_items[i];
603 Inventory::~Inventory()
608 void Inventory::clear()
610 for(u32 i=0; i<m_lists.size(); i++)
617 Inventory::Inventory()
621 Inventory::Inventory(const Inventory &other)
626 Inventory & Inventory::operator = (const Inventory &other)
629 for(u32 i=0; i<other.m_lists.size(); i++)
631 m_lists.push_back(new InventoryList(*other.m_lists[i]));
636 void Inventory::serialize(std::ostream &os)
638 for(u32 i=0; i<m_lists.size(); i++)
640 InventoryList *list = m_lists[i];
641 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
645 os<<"EndInventory\n";
648 void Inventory::deSerialize(std::istream &is)
655 std::getline(is, line, '\n');
657 std::istringstream iss(line);
660 std::getline(iss, name, ' ');
662 if(name == "EndInventory")
666 // This is a temporary backwards compatibility fix
667 else if(name == "end")
671 else if(name == "List")
673 std::string listname;
676 std::getline(iss, listname, ' ');
679 InventoryList *list = new InventoryList(listname, listsize);
680 list->deSerialize(is);
682 m_lists.push_back(list);
686 throw SerializationError("Unknown inventory identifier");
691 InventoryList * Inventory::addList(const std::string &name, u32 size)
693 s32 i = getListIndex(name);
696 if(m_lists[i]->getSize() != size)
699 m_lists[i] = new InventoryList(name, size);
705 m_lists.push_back(new InventoryList(name, size));
706 return m_lists.getLast();
710 InventoryList * Inventory::getList(const std::string &name)
712 s32 i = getListIndex(name);
718 s32 Inventory::getListIndex(const std::string &name)
720 for(u32 i=0; i<m_lists.size(); i++)
722 if(m_lists[i]->getName() == name)
732 InventoryAction * InventoryAction::deSerialize(std::istream &is)
735 std::getline(is, type, ' ');
737 InventoryAction *a = NULL;
741 a = new IMoveAction(is);
747 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
751 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
752 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
753 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
755 Inventory *inv_from = mgr->getInventory(c, from_inv);
756 Inventory *inv_to = mgr->getInventory(c, to_inv);
758 if(!inv_from || !inv_to)
760 dstream<<__FUNCTION_NAME<<": Operation not allowed "
761 <<"(inventories not found)"<<std::endl;
765 InventoryList *list_from = inv_from->getList(from_list);
766 InventoryList *list_to = inv_to->getList(to_list);
768 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
771 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
774 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
778 If a list doesn't exist or the source item doesn't exist
780 if(!list_from || !list_to)
782 dstream<<__FUNCTION_NAME<<": Operation not allowed "
783 <<"(a list doesn't exist)"
787 if(list_from->getItem(from_i) == NULL)
789 dstream<<__FUNCTION_NAME<<": Operation not allowed "
790 <<"(the source item doesn't exist)"
795 If the source and the destination slots are the same
797 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
799 dstream<<__FUNCTION_NAME<<": Operation not allowed "
800 <<"(source and the destination slots are the same)"<<std::endl;
804 // Take item from source list
805 InventoryItem *item1 = NULL;
807 item1 = list_from->changeItem(from_i, NULL);
809 item1 = list_from->takeItem(from_i, count);
811 // Try to add the item to destination list
812 InventoryItem *olditem = item1;
813 item1 = list_to->addItem(to_i, item1);
815 // If something is returned, the item was not fully added
818 // If olditem is returned, nothing was added.
819 bool nothing_added = (item1 == olditem);
821 // If something else is returned, part of the item was left unadded.
822 // Add the other part back to the source item
823 list_from->addItem(from_i, item1);
825 // If olditem is returned, nothing was added.
829 // Take item from source list
830 item1 = list_from->changeItem(from_i, NULL);
831 // Adding was not possible, swap the items.
832 InventoryItem *item2 = list_to->changeItem(to_i, item1);
833 // Put item from destination list to the source list
834 list_from->changeItem(from_i, item2);
838 mgr->inventoryModified(c, from_inv);
839 if(from_inv != to_inv)
840 mgr->inventoryModified(c, to_inv);
845 Craft checking system
848 bool ItemSpec::checkItem(InventoryItem *item)
850 if(type == ITEM_NONE)
858 // There should be an item
862 std::string itemname = item->getName();
864 if(type == ITEM_MATERIAL)
866 if(itemname != "MaterialItem")
868 MaterialItem *mitem = (MaterialItem*)item;
869 if(mitem->getMaterial() != num)
872 else if(type == ITEM_CRAFT)
874 if(itemname != "CraftItem")
876 CraftItem *mitem = (CraftItem*)item;
877 if(mitem->getSubName() != name)
880 else if(type == ITEM_TOOL)
885 else if(type == ITEM_MBO)
898 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
900 u16 items_min_x = 100;
901 u16 items_max_x = 100;
902 u16 items_min_y = 100;
903 u16 items_max_y = 100;
904 for(u16 y=0; y<3; y++)
905 for(u16 x=0; x<3; x++)
907 if(items[y*3 + x] == NULL)
909 if(items_min_x == 100 || x < items_min_x)
911 if(items_min_y == 100 || y < items_min_y)
913 if(items_max_x == 100 || x > items_max_x)
915 if(items_max_y == 100 || y > items_max_y)
918 // No items at all, just return false
919 if(items_min_x == 100)
922 u16 items_w = items_max_x - items_min_x + 1;
923 u16 items_h = items_max_y - items_min_y + 1;
925 u16 specs_min_x = 100;
926 u16 specs_max_x = 100;
927 u16 specs_min_y = 100;
928 u16 specs_max_y = 100;
929 for(u16 y=0; y<3; y++)
930 for(u16 x=0; x<3; x++)
932 if(specs[y*3 + x].type == ITEM_NONE)
934 if(specs_min_x == 100 || x < specs_min_x)
936 if(specs_min_y == 100 || y < specs_min_y)
938 if(specs_max_x == 100 || x > specs_max_x)
940 if(specs_max_y == 100 || y > specs_max_y)
943 // No specs at all, just return false
944 if(specs_min_x == 100)
947 u16 specs_w = specs_max_x - specs_min_x + 1;
948 u16 specs_h = specs_max_y - specs_min_y + 1;
951 if(items_w != specs_w || items_h != specs_h)
954 for(u16 y=0; y<specs_h; y++)
955 for(u16 x=0; x<specs_w; x++)
957 u16 items_x = items_min_x + x;
958 u16 items_y = items_min_y + y;
959 u16 specs_x = specs_min_x + x;
960 u16 specs_y = specs_min_y + y;
961 InventoryItem *item = items[items_y * 3 + items_x];
962 ItemSpec &spec = specs[specs_y * 3 + specs_x];
964 if(spec.checkItem(item) == false)