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()
39 InventoryItem::~InventoryItem()
43 InventoryItem* InventoryItem::deSerialize(std::istream &is)
45 DSTACK(__FUNCTION_NAME);
47 //is.imbue(std::locale("C"));
50 std::getline(is, name, ' ');
52 if(name == "MaterialItem")
54 // u16 reads directly as a number (u8 doesn't)
60 throw SerializationError("Too large material number");
61 return new MaterialItem(material, count);
63 else if(name == "MBOItem")
65 std::string inventorystring;
66 std::getline(is, inventorystring, '|');
67 return new MapBlockObjectItem(inventorystring);
71 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
72 throw SerializationError("Unknown InventoryItem name");
80 video::ITexture * MapBlockObjectItem::getImage()
82 if(m_inventorystring.substr(0,3) == "Rat")
83 //return g_device->getVideoDriver()->getTexture("../data/rat.png");
84 return g_irrlicht->getTexture("../data/rat.png");
86 if(m_inventorystring.substr(0,4) == "Sign")
87 //return g_device->getVideoDriver()->getTexture("../data/sign.png");
88 return g_irrlicht->getTexture("../data/sign.png");
93 std::string MapBlockObjectItem::getText()
95 if(m_inventorystring.substr(0,3) == "Rat")
98 if(m_inventorystring.substr(0,4) == "Sign")
104 MapBlockObject * MapBlockObjectItem::createObject
105 (v3f pos, f32 player_yaw, f32 player_pitch)
107 std::istringstream is(m_inventorystring);
109 std::getline(is, name, ' ');
115 else if(name == "Sign")
118 std::getline(is, text, '|');
119 SignObject *obj = new SignObject(NULL, -1, pos);
121 obj->setYaw(-player_yaw);
124 else if(name == "Rat")
126 RatObject *obj = new RatObject(NULL, -1, pos);
139 InventoryList::InventoryList(std::string name, u32 size)
146 InventoryList::~InventoryList()
148 for(u32 i=0; i<m_items.size(); i++)
155 void InventoryList::clearItems()
157 for(u32 i=0; i<m_items.size(); i++)
165 for(u32 i=0; i<m_size; i++)
167 m_items.push_back(NULL);
171 void InventoryList::serialize(std::ostream &os)
173 //os.imbue(std::locale("C"));
175 for(u32 i=0; i<m_items.size(); i++)
177 InventoryItem *item = m_items[i];
193 void InventoryList::deSerialize(std::istream &is)
195 //is.imbue(std::locale("C"));
203 std::getline(is, line, '\n');
205 std::istringstream iss(line);
206 //iss.imbue(std::locale("C"));
209 std::getline(iss, name, ' ');
215 else if(name == "Item")
217 if(item_i > getSize() - 1)
218 throw SerializationError("too many items");
219 InventoryItem *item = InventoryItem::deSerialize(iss);
220 m_items[item_i++] = item;
222 else if(name == "Empty")
224 if(item_i > getSize() - 1)
225 throw SerializationError("too many items");
226 m_items[item_i++] = NULL;
230 throw SerializationError("Unknown inventory identifier");
235 InventoryList::InventoryList(const InventoryList &other)
238 Do this so that the items get cloned. Otherwise the pointers
239 in the array will just get copied.
244 InventoryList & InventoryList::operator = (const InventoryList &other)
246 m_name = other.m_name;
247 m_size = other.m_size;
249 for(u32 i=0; i<other.m_items.size(); i++)
251 InventoryItem *item = other.m_items[i];
254 m_items[i] = item->clone();
261 std::string InventoryList::getName()
266 u32 InventoryList::getSize()
268 return m_items.size();
271 u32 InventoryList::getUsedSlots()
274 for(u32 i=0; i<m_items.size(); i++)
276 InventoryItem *item = m_items[i];
283 InventoryItem * InventoryList::getItem(u32 i)
285 if(i > m_items.size() - 1)
290 InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
292 assert(i < m_items.size());
294 InventoryItem *olditem = m_items[i];
295 m_items[i] = newitem;
299 void InventoryList::deleteItem(u32 i)
301 assert(i < m_items.size());
302 InventoryItem *item = changeItem(i, NULL);
307 bool InventoryList::addItem(InventoryItem *newitem)
309 // If it is a MaterialItem, try to find an already existing one
310 // and just increment the counter
311 if(std::string("MaterialItem") == newitem->getName())
313 u8 material = ((MaterialItem*)newitem)->getMaterial();
314 u8 count = ((MaterialItem*)newitem)->getCount();
315 for(u32 i=0; i<m_items.size(); i++)
317 InventoryItem *item2 = m_items[i];
320 if(std::string("MaterialItem") != item2->getName())
322 // Found one. Check if it is of the right material and has
324 MaterialItem *mitem2 = (MaterialItem*)item2;
325 if(mitem2->getMaterial() != material)
327 //TODO: Add all that can be added and add remaining part
329 if(mitem2->freeSpace() < count)
331 // Add to the counter
333 // Dump the parameter
338 // Else find an empty position
339 for(u32 i=0; i<m_items.size(); i++)
341 InventoryItem *item = m_items[i];
344 m_items[i] = newitem;
351 bool InventoryList::addItem(u32 i, InventoryItem *newitem)
353 // If it is an empty position, it's an easy job.
354 InventoryItem *item = m_items[i];
357 m_items[i] = newitem;
361 // If it is a material item, try to
362 if(std::string("MaterialItem") == newitem->getName())
364 u8 material = ((MaterialItem*)newitem)->getMaterial();
365 u8 count = ((MaterialItem*)newitem)->getCount();
366 InventoryItem *item2 = m_items[i];
369 && std::string("MaterialItem") == item2->getName())
371 // Check if it is of the right material and has free space
372 MaterialItem *mitem2 = (MaterialItem*)item2;
373 if(mitem2->getMaterial() == material
374 && mitem2->freeSpace() >= count)
376 // Add to the counter
378 // Dump the parameter
389 void InventoryList::decrementMaterials(u16 count)
391 for(u32 i=0; i<m_items.size(); i++)
393 InventoryItem *item = m_items[i];
396 if(std::string("MaterialItem") == item->getName())
398 MaterialItem *mitem = (MaterialItem*)item;
399 if(mitem->getCount() < count)
401 dstream<<__FUNCTION_NAME<<": decrementMaterials():"
402 <<" too small material count"<<std::endl;
404 else if(mitem->getCount() == count)
416 void InventoryList::print(std::ostream &o)
418 o<<"InventoryList:"<<std::endl;
419 for(u32 i=0; i<m_items.size(); i++)
421 InventoryItem *item = m_items[i];
435 Inventory::~Inventory()
440 void Inventory::clear()
442 for(u32 i=0; i<m_lists.size(); i++)
449 Inventory::Inventory()
453 Inventory::Inventory(const Inventory &other)
458 Inventory & Inventory::operator = (const Inventory &other)
461 for(u32 i=0; i<other.m_lists.size(); i++)
463 m_lists.push_back(new InventoryList(*other.m_lists[i]));
468 void Inventory::serialize(std::ostream &os)
470 for(u32 i=0; i<m_lists.size(); i++)
472 InventoryList *list = m_lists[i];
473 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
480 void Inventory::deSerialize(std::istream &is)
487 std::getline(is, line, '\n');
489 std::istringstream iss(line);
492 std::getline(iss, name, ' ');
498 else if(name == "List")
500 std::string listname;
503 std::getline(iss, listname, ' ');
506 InventoryList *list = new InventoryList(listname, listsize);
507 list->deSerialize(is);
509 m_lists.push_back(list);
513 throw SerializationError("Unknown inventory identifier");
518 InventoryList * Inventory::addList(const std::string &name, u32 size)
520 s32 i = getListIndex(name);
523 if(m_lists[i]->getSize() != size)
526 m_lists[i] = new InventoryList(name, size);
532 m_lists.push_back(new InventoryList(name, size));
533 return m_lists.getLast();
537 InventoryList * Inventory::getList(const std::string &name)
539 s32 i = getListIndex(name);
545 s32 Inventory::getListIndex(const std::string &name)
547 for(u32 i=0; i<m_lists.size(); i++)
549 if(m_lists[i]->getName() == name)
559 InventoryAction * InventoryAction::deSerialize(std::istream &is)
562 std::getline(is, type, ' ');
564 InventoryAction *a = NULL;
568 a = new IMoveAction(is);
574 void IMoveAction::apply(Inventory *inventory)
576 /*dstream<<"from_name="<<from_name<<" to_name="<<to_name<<std::endl;
577 dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
578 InventoryList *list_from = inventory->getList(from_name);
579 InventoryList *list_to = inventory->getList(to_name);
580 /*dstream<<"list_from="<<list_from<<" list_to="<<list_to
583 dstream<<" list_from->getItem(from_i)="<<list_from->getItem(from_i)
586 dstream<<" list_to->getItem(to_i)="<<list_to->getItem(to_i)
589 if(!list_from || !list_to || list_from->getItem(from_i) == NULL
590 || (list_from == list_to && from_i == to_i))
592 dstream<<__FUNCTION_NAME<<": Operation not allowed"<<std::endl;
596 // Take item from source list
597 InventoryItem *item1 = list_from->changeItem(from_i, NULL);
598 // Try to add the item to destination list
599 if(list_to->addItem(to_i, item1))
604 // Adding was not possible, switch it.
605 // Switch it to the destination list
606 InventoryItem *item2 = list_to->changeItem(to_i, item1);
607 // Put item from destination list to the source list
608 list_from->changeItem(from_i, item2);