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"
35 InventoryItem::InventoryItem(u16 count)
40 InventoryItem::~InventoryItem()
44 InventoryItem* InventoryItem::deSerialize(std::istream &is)
46 DSTACK(__FUNCTION_NAME);
48 //is.imbue(std::locale("C"));
51 std::getline(is, name, ' ');
53 if(name == "MaterialItem")
55 // u16 reads directly as a number (u8 doesn't)
61 throw SerializationError("Too large material number");
62 return new MaterialItem(material, count);
64 else if(name == "MBOItem")
66 std::string inventorystring;
67 std::getline(is, inventorystring, '|');
68 return new MapBlockObjectItem(inventorystring);
70 else if(name == "CraftItem")
73 std::getline(is, subname, ' ');
76 return new CraftItem(subname, count);
78 else if(name == "ToolItem")
81 std::getline(is, toolname, ' ');
84 return new ToolItem(toolname, wear);
88 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
89 throw SerializationError("Unknown InventoryItem name");
97 InventoryItem *MaterialItem::createCookResult()
99 if(m_content == CONTENT_TREE)
101 return new CraftItem("lump_of_coal", 1);
110 video::ITexture * MapBlockObjectItem::getImage()
112 if(m_inventorystring.substr(0,3) == "Rat")
113 return g_texturesource->getTextureRaw("rat.png");
115 if(m_inventorystring.substr(0,4) == "Sign")
116 return g_texturesource->getTextureRaw("sign.png");
121 std::string MapBlockObjectItem::getText()
123 if(m_inventorystring.substr(0,3) == "Rat")
126 if(m_inventorystring.substr(0,4) == "Sign")
132 MapBlockObject * MapBlockObjectItem::createObject
133 (v3f pos, f32 player_yaw, f32 player_pitch)
135 std::istringstream is(m_inventorystring);
137 std::getline(is, name, ' ');
143 else if(name == "Sign")
146 std::getline(is, text, '|');
147 SignObject *obj = new SignObject(NULL, -1, pos);
149 obj->setYaw(-player_yaw);
152 else if(name == "Rat")
154 RatObject *obj = new RatObject(NULL, -1, pos);
157 else if(name == "ItemObj")
160 Now we are an inventory item containing the serialization
161 string of an object that contains the serialization
162 string of an inventory item. Fuck this.
165 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
166 <<"because an item-object should never be inside "
167 <<"an object-item."<<std::endl;
180 InventoryList::InventoryList(std::string name, u32 size)
187 InventoryList::~InventoryList()
189 for(u32 i=0; i<m_items.size(); i++)
196 void InventoryList::clearItems()
198 for(u32 i=0; i<m_items.size(); i++)
206 for(u32 i=0; i<m_size; i++)
208 m_items.push_back(NULL);
212 void InventoryList::serialize(std::ostream &os)
214 //os.imbue(std::locale("C"));
216 for(u32 i=0; i<m_items.size(); i++)
218 InventoryItem *item = m_items[i];
231 os<<"EndInventoryList\n";
234 void InventoryList::deSerialize(std::istream &is)
236 //is.imbue(std::locale("C"));
244 std::getline(is, line, '\n');
246 std::istringstream iss(line);
247 //iss.imbue(std::locale("C"));
250 std::getline(iss, name, ' ');
252 if(name == "EndInventoryList")
256 // This is a temporary backwards compatibility fix
257 else if(name == "end")
261 else if(name == "Item")
263 if(item_i > getSize() - 1)
264 throw SerializationError("too many items");
265 InventoryItem *item = InventoryItem::deSerialize(iss);
266 m_items[item_i++] = item;
268 else if(name == "Empty")
270 if(item_i > getSize() - 1)
271 throw SerializationError("too many items");
272 m_items[item_i++] = NULL;
276 throw SerializationError("Unknown inventory identifier");
281 InventoryList::InventoryList(const InventoryList &other)
284 Do this so that the items get cloned. Otherwise the pointers
285 in the array will just get copied.
290 InventoryList & InventoryList::operator = (const InventoryList &other)
292 m_name = other.m_name;
293 m_size = other.m_size;
295 for(u32 i=0; i<other.m_items.size(); i++)
297 InventoryItem *item = other.m_items[i];
300 m_items[i] = item->clone();
307 std::string InventoryList::getName()
312 u32 InventoryList::getSize()
314 return m_items.size();
317 u32 InventoryList::getUsedSlots()
320 for(u32 i=0; i<m_items.size(); i++)
322 InventoryItem *item = m_items[i];
329 u32 InventoryList::getFreeSlots()
331 return getSize() - getUsedSlots();
334 InventoryItem * InventoryList::getItem(u32 i)
336 if(i > m_items.size() - 1)
341 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
343 assert(i < m_items.size());
345 InventoryItem *olditem = m_items[i];
346 m_items[i] = newitem;
350 void InventoryList::deleteItem(u32 i)
352 assert(i < m_items.size());
353 InventoryItem *item = changeItem(i, NULL);
358 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
361 First try to find if it could be added to some existing items
363 for(u32 i=0; i<m_items.size(); i++)
365 // Ignore empty slots
366 if(m_items[i] == NULL)
369 newitem = addItem(i, newitem);
371 return NULL; // All was eaten
375 Then try to add it to empty slots
377 for(u32 i=0; i<m_items.size(); i++)
379 // Ignore unempty slots
380 if(m_items[i] != NULL)
383 newitem = addItem(i, newitem);
385 return NULL; // All was eaten
392 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
394 // If it is an empty position, it's an easy job.
395 InventoryItem *to_item = m_items[i];
398 m_items[i] = newitem;
402 // If not addable, return the item
403 if(newitem->addableTo(to_item) == false)
406 // If the item fits fully in the slot, add counter and delete it
407 if(newitem->getCount() <= to_item->freeSpace())
409 to_item->add(newitem->getCount());
413 // Else the item does not fit fully. Add all that fits and return
417 u16 freespace = to_item->freeSpace();
418 to_item->add(freespace);
419 newitem->remove(freespace);
424 bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
426 // If it is an empty position, it's an easy job.
427 InventoryItem *to_item = m_items[i];
433 // If not addable, return the item
434 if(newitem->addableTo(to_item) == false)
437 // If the item fits fully in the slot, add counter and delete it
438 if(newitem->getCount() <= to_item->freeSpace())
446 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
451 InventoryItem *item = m_items[i];
452 // If it is an empty position, return NULL
456 if(count >= item->getCount())
458 // Get the item by swapping NULL to its place
459 return changeItem(i, NULL);
463 InventoryItem *item2 = item->clone();
465 item2->setCount(count);
472 void InventoryList::decrementMaterials(u16 count)
474 for(u32 i=0; i<m_items.size(); i++)
476 InventoryItem *item = takeItem(i, count);
482 void InventoryList::print(std::ostream &o)
484 o<<"InventoryList:"<<std::endl;
485 for(u32 i=0; i<m_items.size(); i++)
487 InventoryItem *item = m_items[i];
501 Inventory::~Inventory()
506 void Inventory::clear()
508 for(u32 i=0; i<m_lists.size(); i++)
515 Inventory::Inventory()
519 Inventory::Inventory(const Inventory &other)
524 Inventory & Inventory::operator = (const Inventory &other)
527 for(u32 i=0; i<other.m_lists.size(); i++)
529 m_lists.push_back(new InventoryList(*other.m_lists[i]));
534 void Inventory::serialize(std::ostream &os)
536 for(u32 i=0; i<m_lists.size(); i++)
538 InventoryList *list = m_lists[i];
539 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
543 os<<"EndInventory\n";
546 void Inventory::deSerialize(std::istream &is)
553 std::getline(is, line, '\n');
555 std::istringstream iss(line);
558 std::getline(iss, name, ' ');
560 if(name == "EndInventory")
564 // This is a temporary backwards compatibility fix
565 else if(name == "end")
569 else if(name == "List")
571 std::string listname;
574 std::getline(iss, listname, ' ');
577 InventoryList *list = new InventoryList(listname, listsize);
578 list->deSerialize(is);
580 m_lists.push_back(list);
584 throw SerializationError("Unknown inventory identifier");
589 InventoryList * Inventory::addList(const std::string &name, u32 size)
591 s32 i = getListIndex(name);
594 if(m_lists[i]->getSize() != size)
597 m_lists[i] = new InventoryList(name, size);
603 m_lists.push_back(new InventoryList(name, size));
604 return m_lists.getLast();
608 InventoryList * Inventory::getList(const std::string &name)
610 s32 i = getListIndex(name);
616 s32 Inventory::getListIndex(const std::string &name)
618 for(u32 i=0; i<m_lists.size(); i++)
620 if(m_lists[i]->getName() == name)
630 InventoryAction * InventoryAction::deSerialize(std::istream &is)
633 std::getline(is, type, ' ');
635 InventoryAction *a = NULL;
639 a = new IMoveAction(is);
645 void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
649 /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
650 dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
651 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
653 Inventory *inv_from = mgr->getInventory(c, from_inv);
654 Inventory *inv_to = mgr->getInventory(c, to_inv);
656 if(!inv_from || !inv_to)
658 dstream<<__FUNCTION_NAME<<": Operation not allowed "
659 <<"(inventories not found)"<<std::endl;
663 InventoryList *list_from = inv_from->getList(from_list);
664 InventoryList *list_to = inv_to->getList(to_list);
666 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
669 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
672 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
676 If a list doesn't exist or the source item doesn't exist
678 if(!list_from || !list_to)
680 dstream<<__FUNCTION_NAME<<": Operation not allowed "
681 <<"(a list doesn't exist)"
685 if(list_from->getItem(from_i) == NULL)
687 dstream<<__FUNCTION_NAME<<": Operation not allowed "
688 <<"(the source item doesn't exist)"
693 If the source and the destination slots are the same
695 if(inv_from == inv_to && list_from == list_to && from_i == to_i)
697 dstream<<__FUNCTION_NAME<<": Operation not allowed "
698 <<"(source and the destination slots are the same)"<<std::endl;
702 // Take item from source list
703 InventoryItem *item1 = NULL;
705 item1 = list_from->changeItem(from_i, NULL);
707 item1 = list_from->takeItem(from_i, count);
709 // Try to add the item to destination list
710 InventoryItem *olditem = item1;
711 item1 = list_to->addItem(to_i, item1);
713 // If something is returned, the item was not fully added
716 // If olditem is returned, nothing was added.
717 bool nothing_added = (item1 == olditem);
719 // If something else is returned, part of the item was left unadded.
720 // Add the other part back to the source item
721 list_from->addItem(from_i, item1);
723 // If olditem is returned, nothing was added.
727 // Take item from source list
728 item1 = list_from->changeItem(from_i, NULL);
729 // Adding was not possible, swap the items.
730 InventoryItem *item2 = list_to->changeItem(to_i, item1);
731 // Put item from destination list to the source list
732 list_from->changeItem(from_i, item2);
736 mgr->inventoryModified(c, from_inv);
737 if(from_inv != to_inv)
738 mgr->inventoryModified(c, to_inv);
743 Craft checking system
746 bool ItemSpec::checkItem(InventoryItem *item)
748 if(type == ITEM_NONE)
756 // There should be an item
760 std::string itemname = item->getName();
762 if(type == ITEM_MATERIAL)
764 if(itemname != "MaterialItem")
766 MaterialItem *mitem = (MaterialItem*)item;
767 if(mitem->getMaterial() != num)
770 else if(type == ITEM_CRAFT)
772 if(itemname != "CraftItem")
774 CraftItem *mitem = (CraftItem*)item;
775 if(mitem->getSubName() != name)
778 else if(type == ITEM_TOOL)
783 else if(type == ITEM_MBO)
796 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
798 u16 items_min_x = 100;
799 u16 items_max_x = 100;
800 u16 items_min_y = 100;
801 u16 items_max_y = 100;
802 for(u16 y=0; y<3; y++)
803 for(u16 x=0; x<3; x++)
805 if(items[y*3 + x] == NULL)
807 if(items_min_x == 100 || x < items_min_x)
809 if(items_min_y == 100 || y < items_min_y)
811 if(items_max_x == 100 || x > items_max_x)
813 if(items_max_y == 100 || y > items_max_y)
816 // No items at all, just return false
817 if(items_min_x == 100)
820 u16 items_w = items_max_x - items_min_x + 1;
821 u16 items_h = items_max_y - items_min_y + 1;
823 u16 specs_min_x = 100;
824 u16 specs_max_x = 100;
825 u16 specs_min_y = 100;
826 u16 specs_max_y = 100;
827 for(u16 y=0; y<3; y++)
828 for(u16 x=0; x<3; x++)
830 if(specs[y*3 + x].type == ITEM_NONE)
832 if(specs_min_x == 100 || x < specs_min_x)
834 if(specs_min_y == 100 || y < specs_min_y)
836 if(specs_max_x == 100 || x > specs_max_x)
838 if(specs_max_y == 100 || y > specs_max_y)
841 // No specs at all, just return false
842 if(specs_min_x == 100)
845 u16 specs_w = specs_max_x - specs_min_x + 1;
846 u16 specs_h = specs_max_y - specs_min_y + 1;
849 if(items_w != specs_w || items_h != specs_h)
852 for(u16 y=0; y<specs_h; y++)
853 for(u16 x=0; x<specs_w; x++)
855 u16 items_x = items_min_x + x;
856 u16 items_y = items_min_y + y;
857 u16 specs_x = specs_min_x + x;
858 u16 specs_y = specs_min_y + y;
859 InventoryItem *item = items[items_y * 3 + items_x];
860 ItemSpec &spec = specs[specs_y * 3 + specs_x];
862 if(spec.checkItem(item) == false)