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 video::ITexture * MapBlockObjectItem::getImage()
99 if(m_inventorystring.substr(0,3) == "Rat")
100 //return g_device->getVideoDriver()->getTexture(porting::getDataPath("rat.png").c_str());
101 return g_irrlicht->getTexture(porting::getDataPath("rat.png").c_str());
103 if(m_inventorystring.substr(0,4) == "Sign")
104 //return g_device->getVideoDriver()->getTexture(porting::getDataPath("sign.png").c_str());
105 return g_irrlicht->getTexture(porting::getDataPath("sign.png").c_str());
110 std::string MapBlockObjectItem::getText()
112 if(m_inventorystring.substr(0,3) == "Rat")
115 if(m_inventorystring.substr(0,4) == "Sign")
121 MapBlockObject * MapBlockObjectItem::createObject
122 (v3f pos, f32 player_yaw, f32 player_pitch)
124 std::istringstream is(m_inventorystring);
126 std::getline(is, name, ' ');
132 else if(name == "Sign")
135 std::getline(is, text, '|');
136 SignObject *obj = new SignObject(NULL, -1, pos);
138 obj->setYaw(-player_yaw);
141 else if(name == "Rat")
143 RatObject *obj = new RatObject(NULL, -1, pos);
146 else if(name == "ItemObj")
149 Now we are an inventory item containing the serialization
150 string of an object that contains the serialization
151 string of an inventory item. Fuck this.
154 dstream<<__FUNCTION_NAME<<": WARNING: Ignoring ItemObj "
155 <<"because an item-object should never be inside "
156 <<"an object-item."<<std::endl;
169 InventoryList::InventoryList(std::string name, u32 size)
176 InventoryList::~InventoryList()
178 for(u32 i=0; i<m_items.size(); i++)
185 void InventoryList::clearItems()
187 for(u32 i=0; i<m_items.size(); i++)
195 for(u32 i=0; i<m_size; i++)
197 m_items.push_back(NULL);
201 void InventoryList::serialize(std::ostream &os)
203 //os.imbue(std::locale("C"));
205 for(u32 i=0; i<m_items.size(); i++)
207 InventoryItem *item = m_items[i];
223 void InventoryList::deSerialize(std::istream &is)
225 //is.imbue(std::locale("C"));
233 std::getline(is, line, '\n');
235 std::istringstream iss(line);
236 //iss.imbue(std::locale("C"));
239 std::getline(iss, name, ' ');
245 else if(name == "Item")
247 if(item_i > getSize() - 1)
248 throw SerializationError("too many items");
249 InventoryItem *item = InventoryItem::deSerialize(iss);
250 m_items[item_i++] = item;
252 else if(name == "Empty")
254 if(item_i > getSize() - 1)
255 throw SerializationError("too many items");
256 m_items[item_i++] = NULL;
260 throw SerializationError("Unknown inventory identifier");
265 InventoryList::InventoryList(const InventoryList &other)
268 Do this so that the items get cloned. Otherwise the pointers
269 in the array will just get copied.
274 InventoryList & InventoryList::operator = (const InventoryList &other)
276 m_name = other.m_name;
277 m_size = other.m_size;
279 for(u32 i=0; i<other.m_items.size(); i++)
281 InventoryItem *item = other.m_items[i];
284 m_items[i] = item->clone();
291 std::string InventoryList::getName()
296 u32 InventoryList::getSize()
298 return m_items.size();
301 u32 InventoryList::getUsedSlots()
304 for(u32 i=0; i<m_items.size(); i++)
306 InventoryItem *item = m_items[i];
313 InventoryItem * InventoryList::getItem(u32 i)
315 if(i > m_items.size() - 1)
320 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
322 assert(i < m_items.size());
324 InventoryItem *olditem = m_items[i];
325 m_items[i] = newitem;
329 void InventoryList::deleteItem(u32 i)
331 assert(i < m_items.size());
332 InventoryItem *item = changeItem(i, NULL);
337 InventoryItem * InventoryList::addItem(InventoryItem *newitem)
340 First try to find if it could be added to some existing items
342 for(u32 i=0; i<m_items.size(); i++)
344 // Ignore empty slots
345 if(m_items[i] == NULL)
348 newitem = addItem(i, newitem);
350 return NULL; // All was eaten
354 Then try to add it to empty slots
356 for(u32 i=0; i<m_items.size(); i++)
358 // Ignore unempty slots
359 if(m_items[i] != NULL)
362 newitem = addItem(i, newitem);
364 return NULL; // All was eaten
371 InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
373 // If it is an empty position, it's an easy job.
374 InventoryItem *to_item = m_items[i];
377 m_items[i] = newitem;
381 // If not addable, return the item
382 if(newitem->addableTo(to_item) == false)
385 // If the item fits fully in the slot, add counter and delete it
386 if(newitem->getCount() <= to_item->freeSpace())
388 to_item->add(newitem->getCount());
392 // Else the item does not fit fully. Add all that fits and return
396 u16 freespace = to_item->freeSpace();
397 to_item->add(freespace);
398 newitem->remove(freespace);
403 InventoryItem * InventoryList::takeItem(u32 i, u32 count)
408 InventoryItem *item = m_items[i];
409 // If it is an empty position, return NULL
413 if(count >= item->getCount())
415 // Get the item by swapping NULL to its place
416 return changeItem(i, NULL);
420 InventoryItem *item2 = item->clone();
422 item2->setCount(count);
429 void InventoryList::decrementMaterials(u16 count)
431 for(u32 i=0; i<m_items.size(); i++)
433 InventoryItem *item = takeItem(i, count);
439 void InventoryList::print(std::ostream &o)
441 o<<"InventoryList:"<<std::endl;
442 for(u32 i=0; i<m_items.size(); i++)
444 InventoryItem *item = m_items[i];
458 Inventory::~Inventory()
463 void Inventory::clear()
465 for(u32 i=0; i<m_lists.size(); i++)
472 Inventory::Inventory()
476 Inventory::Inventory(const Inventory &other)
481 Inventory & Inventory::operator = (const Inventory &other)
484 for(u32 i=0; i<other.m_lists.size(); i++)
486 m_lists.push_back(new InventoryList(*other.m_lists[i]));
491 void Inventory::serialize(std::ostream &os)
493 for(u32 i=0; i<m_lists.size(); i++)
495 InventoryList *list = m_lists[i];
496 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
503 void Inventory::deSerialize(std::istream &is)
510 std::getline(is, line, '\n');
512 std::istringstream iss(line);
515 std::getline(iss, name, ' ');
521 else if(name == "List")
523 std::string listname;
526 std::getline(iss, listname, ' ');
529 InventoryList *list = new InventoryList(listname, listsize);
530 list->deSerialize(is);
532 m_lists.push_back(list);
536 throw SerializationError("Unknown inventory identifier");
541 InventoryList * Inventory::addList(const std::string &name, u32 size)
543 s32 i = getListIndex(name);
546 if(m_lists[i]->getSize() != size)
549 m_lists[i] = new InventoryList(name, size);
555 m_lists.push_back(new InventoryList(name, size));
556 return m_lists.getLast();
560 InventoryList * Inventory::getList(const std::string &name)
562 s32 i = getListIndex(name);
568 s32 Inventory::getListIndex(const std::string &name)
570 for(u32 i=0; i<m_lists.size(); i++)
572 if(m_lists[i]->getName() == name)
582 InventoryAction * InventoryAction::deSerialize(std::istream &is)
585 std::getline(is, type, ' ');
587 InventoryAction *a = NULL;
591 a = new IMoveAction(is);
597 void IMoveAction::apply(Inventory *inventory)
599 /*dstream<<"from_name="<<from_name<<" to_name="<<to_name<<std::endl;
600 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
601 InventoryList *list_from = inventory->getList(from_name);
602 InventoryList *list_to = inventory->getList(to_name);
603 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
606 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
609 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
613 If a list doesn't exist or the source item doesn't exist
614 or the source and the destination slots are the same
616 if(!list_from || !list_to || list_from->getItem(from_i) == NULL
617 || (list_from == list_to && from_i == to_i))
619 dstream<<__FUNCTION_NAME<<": Operation not allowed"<<std::endl;
623 // Take item from source list
624 InventoryItem *item1 = NULL;
626 item1 = list_from->changeItem(from_i, NULL);
628 item1 = list_from->takeItem(from_i, count);
630 // Try to add the item to destination list
631 InventoryItem *olditem = item1;
632 item1 = list_to->addItem(to_i, item1);
634 // If nothing is returned, the item was fully added
638 // If olditem is returned, nothing was added.
639 bool nothing_added = (item1 == olditem);
641 // If something else is returned, part of the item was left unadded.
642 // Add the other part back to the source item
643 list_from->addItem(from_i, item1);
645 // If olditem is returned, nothing was added.
649 // Take item from source list
650 item1 = list_from->changeItem(from_i, NULL);
651 // Adding was not possible, swap the items.
652 InventoryItem *item2 = list_to->changeItem(to_i, item1);
653 // Put item from destination list to the source list
654 list_from->changeItem(from_i, item2);