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)
284 InventoryList::~InventoryList()
286 for(u32 i=0; i<m_items.size(); i++)
293 void InventoryList::clearItems()
295 for(u32 i=0; i<m_items.size(); i++)
303 for(u32 i=0; i<m_size; i++)
305 m_items.push_back(NULL);
311 void InventoryList::serialize(std::ostream &os)
313 //os.imbue(std::locale("C"));
315 for(u32 i=0; i<m_items.size(); i++)
317 InventoryItem *item = m_items[i];
330 os<<"EndInventoryList\n";
333 void InventoryList::deSerialize(std::istream &is)
335 //is.imbue(std::locale("C"));
343 std::getline(is, line, '\n');
345 std::istringstream iss(line);
346 //iss.imbue(std::locale("C"));
349 std::getline(iss, name, ' ');
351 if(name == "EndInventoryList")
355 // This is a temporary backwards compatibility fix
356 else if(name == "end")
360 else if(name == "Item")
362 if(item_i > getSize() - 1)
363 throw SerializationError("too many items");
364 InventoryItem *item = InventoryItem::deSerialize(iss);
365 m_items[item_i++] = item;
367 else if(name == "Empty")
369 if(item_i > getSize() - 1)
370 throw SerializationError("too many items");
371 m_items[item_i++] = NULL;
375 throw SerializationError("Unknown inventory identifier");
380 InventoryList::InventoryList(const InventoryList &other)
383 Do this so that the items get cloned. Otherwise the pointers
384 in the array will just get copied.
389 InventoryList & InventoryList::operator = (const InventoryList &other)
391 m_name = other.m_name;
392 m_size = other.m_size;
394 for(u32 i=0; i<other.m_items.size(); i++)
396 InventoryItem *item = other.m_items[i];
399 m_items[i] = item->clone();
407 std::string InventoryList::getName()
412 u32 InventoryList::getSize()
414 return m_items.size();
417 u32 InventoryList::getUsedSlots()
420 for(u32 i=0; i<m_items.size(); i++)
422 InventoryItem *item = m_items[i];
429 u32 InventoryList::getFreeSlots()
431 return getSize() - getUsedSlots();
434 InventoryItem * InventoryList::getItem(u32 i)
436 if(i > m_items.size() - 1)
441 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
443 assert(i < m_items.size());
445 InventoryItem *olditem = m_items[i];
446 m_items[i] = newitem;
451 void InventoryList::deleteItem(u32 i)
453 assert(i < m_items.size());
454 InventoryItem *item = changeItem(i, NULL);
459 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
465 First try to find if it could be added to some existing items
467 for(u32 i=0; i<m_items.size(); i++)
469 // Ignore empty slots
470 if(m_items[i] == NULL)
473 newitem = addItem(i, newitem);
475 return NULL; // All was eaten
479 Then try to add it to empty slots
481 for(u32 i=0; i<m_items.size(); i++)
483 // Ignore unempty slots
484 if(m_items[i] != NULL)
487 newitem = addItem(i, newitem);
489 return NULL; // All was eaten
496 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
503 // If it is an empty position, it's an easy job.
504 InventoryItem *to_item = m_items[i];
507 m_items[i] = newitem;
511 // If not addable, return the item
512 if(newitem->addableTo(to_item) == false)
515 // If the item fits fully in the slot, add counter and delete it
516 if(newitem->getCount() <= to_item->freeSpace())
518 to_item->add(newitem->getCount());
522 // Else the item does not fit fully. Add all that fits and return
526 u16 freespace = to_item->freeSpace();
527 to_item->add(freespace);
528 newitem->remove(freespace);
533 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
535 // If it is an empty position, it's an easy job.
536 InventoryItem *to_item = m_items[i];
542 // If not addable, return the item
543 if(newitem->addableTo(to_item) == false)
546 // If the item fits fully in the slot, add counter and delete it
547 if(newitem->getCount() <= to_item->freeSpace())
555 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
562 InventoryItem *item = m_items[i];
563 // If it is an empty position, return NULL
567 if(count >= item->getCount())
569 // Get the item by swapping NULL to its place
570 return changeItem(i, NULL);
574 InventoryItem *item2 = item->clone();
576 item2->setCount(count);
583 void InventoryList::decrementMaterials(u16 count)
585 for(u32 i=0; i<m_items.size(); i++)
587 InventoryItem *item = takeItem(i, count);
593 void InventoryList::print(std::ostream &o)
595 o<<"InventoryList:"<<std::endl;
596 for(u32 i=0; i<m_items.size(); i++)
598 InventoryItem *item = m_items[i];
612 Inventory::~Inventory()
617 void Inventory::clear()
619 for(u32 i=0; i<m_lists.size(); i++)
626 Inventory::Inventory()
630 Inventory::Inventory(const Inventory &other)
635 Inventory & Inventory::operator = (const Inventory &other)
638 for(u32 i=0; i<other.m_lists.size(); i++)
640 m_lists.push_back(new InventoryList(*other.m_lists[i]));
645 void Inventory::serialize(std::ostream &os)
647 for(u32 i=0; i<m_lists.size(); i++)
649 InventoryList *list = m_lists[i];
650 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
654 os<<"EndInventory\n";
657 void Inventory::deSerialize(std::istream &is)
664 std::getline(is, line, '\n');
666 std::istringstream iss(line);
669 std::getline(iss, name, ' ');
671 if(name == "EndInventory")
675 // This is a temporary backwards compatibility fix
676 else if(name == "end")
680 else if(name == "List")
682 std::string listname;
685 std::getline(iss, listname, ' ');
688 InventoryList *list = new InventoryList(listname, listsize);
689 list->deSerialize(is);
691 m_lists.push_back(list);
695 throw SerializationError("Unknown inventory identifier");
700 InventoryList * Inventory::addList(const std::string &name, u32 size)
702 s32 i = getListIndex(name);
705 if(m_lists[i]->getSize() != size)
708 m_lists[i] = new InventoryList(name, size);
714 m_lists.push_back(new InventoryList(name, size));
715 return m_lists.getLast();
719 InventoryList * Inventory::getList(const std::string &name)
721 s32 i = getListIndex(name);
727 s32 Inventory::getListIndex(const std::string &name)
729 for(u32 i=0; i<m_lists.size(); i++)
731 if(m_lists[i]->getName() == name)
741 InventoryAction * InventoryAction::deSerialize(std::istream &is)
744 std::getline(is, type, ' ');
746 InventoryAction *a = NULL;
750 a = new IMoveAction(is);
756 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
760 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
761 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
762 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
764 Inventory *inv_from = mgr->getInventory(c, from_inv);
765 Inventory *inv_to = mgr->getInventory(c, to_inv);
767 if(!inv_from || !inv_to)
769 dstream<<__FUNCTION_NAME<<": Operation not allowed "
770 <<"(inventories not found)"<<std::endl;
774 InventoryList *list_from = inv_from->getList(from_list);
775 InventoryList *list_to = inv_to->getList(to_list);
777 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
780 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
783 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
787 If a list doesn't exist or the source item doesn't exist
789 if(!list_from || !list_to)
791 dstream<<__FUNCTION_NAME<<": Operation not allowed "
792 <<"(a list doesn't exist)"
796 if(list_from->getItem(from_i) == NULL)
798 dstream<<__FUNCTION_NAME<<": Operation not allowed "
799 <<"(the source item doesn't exist)"
804 If the source and the destination slots are the same
806 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
808 dstream<<__FUNCTION_NAME<<": Operation not allowed "
809 <<"(source and the destination slots are the same)"<<std::endl;
813 // Take item from source list
814 InventoryItem *item1 = NULL;
816 item1 = list_from->changeItem(from_i, NULL);
818 item1 = list_from->takeItem(from_i, count);
820 // Try to add the item to destination list
821 InventoryItem *olditem = item1;
822 item1 = list_to->addItem(to_i, item1);
824 // If something is returned, the item was not fully added
827 // If olditem is returned, nothing was added.
828 bool nothing_added = (item1 == olditem);
830 // If something else is returned, part of the item was left unadded.
831 // Add the other part back to the source item
832 list_from->addItem(from_i, item1);
834 // If olditem is returned, nothing was added.
838 // Take item from source list
839 item1 = list_from->changeItem(from_i, NULL);
840 // Adding was not possible, swap the items.
841 InventoryItem *item2 = list_to->changeItem(to_i, item1);
842 // Put item from destination list to the source list
843 list_from->changeItem(from_i, item2);
847 mgr->inventoryModified(c, from_inv);
848 if(from_inv != to_inv)
849 mgr->inventoryModified(c, to_inv);
854 Craft checking system
857 bool ItemSpec::checkItem(InventoryItem *item)
859 if(type == ITEM_NONE)
867 // There should be an item
871 std::string itemname = item->getName();
873 if(type == ITEM_MATERIAL)
875 if(itemname != "MaterialItem")
877 MaterialItem *mitem = (MaterialItem*)item;
878 if(mitem->getMaterial() != num)
881 else if(type == ITEM_CRAFT)
883 if(itemname != "CraftItem")
885 CraftItem *mitem = (CraftItem*)item;
886 if(mitem->getSubName() != name)
889 else if(type == ITEM_TOOL)
894 else if(type == ITEM_MBO)
907 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
909 u16 items_min_x = 100;
910 u16 items_max_x = 100;
911 u16 items_min_y = 100;
912 u16 items_max_y = 100;
913 for(u16 y=0; y<3; y++)
914 for(u16 x=0; x<3; x++)
916 if(items[y*3 + x] == NULL)
918 if(items_min_x == 100 || x < items_min_x)
920 if(items_min_y == 100 || y < items_min_y)
922 if(items_max_x == 100 || x > items_max_x)
924 if(items_max_y == 100 || y > items_max_y)
927 // No items at all, just return false
928 if(items_min_x == 100)
931 u16 items_w = items_max_x - items_min_x + 1;
932 u16 items_h = items_max_y - items_min_y + 1;
934 u16 specs_min_x = 100;
935 u16 specs_max_x = 100;
936 u16 specs_min_y = 100;
937 u16 specs_max_y = 100;
938 for(u16 y=0; y<3; y++)
939 for(u16 x=0; x<3; x++)
941 if(specs[y*3 + x].type == ITEM_NONE)
943 if(specs_min_x == 100 || x < specs_min_x)
945 if(specs_min_y == 100 || y < specs_min_y)
947 if(specs_max_x == 100 || x > specs_max_x)
949 if(specs_max_y == 100 || y > specs_max_y)
952 // No specs at all, just return false
953 if(specs_min_x == 100)
956 u16 specs_w = specs_max_x - specs_min_x + 1;
957 u16 specs_h = specs_max_y - specs_min_y + 1;
960 if(items_w != specs_w || items_h != specs_h)
963 for(u16 y=0; y<specs_h; y++)
964 for(u16 x=0; x<specs_w; x++)
966 u16 items_x = items_min_x + x;
967 u16 items_y = items_min_y + y;
968 u16 specs_x = specs_min_x + x;
969 u16 specs_y = specs_min_y + y;
970 InventoryItem *item = items[items_y * 3 + items_x];
971 ItemSpec &spec = specs[specs_y * 3 + specs_x];
973 if(spec.checkItem(item) == false)