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 void InventoryList::print(std::ostream &o)
353 o<<"InventoryList:"<<std::endl;
354 for(u32 i=0; i<m_items.size(); i++)
356 InventoryItem *item = m_items[i];
370 Inventory::~Inventory()
375 void Inventory::clear()
377 for(u32 i=0; i<m_lists.size(); i++)
384 Inventory::Inventory()
388 Inventory::Inventory(const Inventory &other)
393 Inventory & Inventory::operator = (const Inventory &other)
396 for(u32 i=0; i<other.m_lists.size(); i++)
398 m_lists.push_back(new InventoryList(*other.m_lists[i]));
403 void Inventory::serialize(std::ostream &os)
405 for(u32 i=0; i<m_lists.size(); i++)
407 InventoryList *list = m_lists[i];
408 os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
415 void Inventory::deSerialize(std::istream &is)
422 std::getline(is, line, '\n');
424 std::istringstream iss(line);
427 std::getline(iss, name, ' ');
433 else if(name == "List")
435 std::string listname;
438 std::getline(iss, listname, ' ');
441 InventoryList *list = new InventoryList(listname, listsize);
442 list->deSerialize(is);
444 m_lists.push_back(list);
448 throw SerializationError("Unknown inventory identifier");
453 InventoryList * Inventory::addList(const std::string &name, u32 size)
455 s32 i = getListIndex(name);
458 if(m_lists[i]->getSize() != size)
461 m_lists[i] = new InventoryList(name, size);
467 m_lists.push_back(new InventoryList(name, size));
468 return m_lists.getLast();
472 InventoryList * Inventory::getList(const std::string &name)
474 s32 i = getListIndex(name);
480 s32 Inventory::getListIndex(const std::string &name)
482 for(u32 i=0; i<m_lists.size(); i++)
484 if(m_lists[i]->getName() == name)