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");
92 std::string MapBlockObjectItem::getText()
94 if(m_inventorystring.substr(0,3) == "Rat")
97 if(m_inventorystring.substr(0,4) == "Sign")
103 MapBlockObject * MapBlockObjectItem::createObject
104 (v3f pos, f32 player_yaw, f32 player_pitch)
106 std::istringstream is(m_inventorystring);
108 std::getline(is, name, ' ');
114 else if(name == "Sign")
117 std::getline(is, text, '|');
118 SignObject *obj = new SignObject(NULL, -1, pos);
120 obj->setYaw(-player_yaw);
123 else if(name == "Rat")
125 RatObject *obj = new RatObject(NULL, -1, pos);
138 Inventory::Inventory(u32 size)
144 Inventory::~Inventory()
146 for(u32 i=0; i<m_items.size(); i++)
152 void Inventory::clearItems()
155 for(u32 i=0; i<m_size; i++)
157 m_items.push_back(NULL);
161 void Inventory::serialize(std::ostream &os)
163 //os.imbue(std::locale("C"));
165 for(u32 i=0; i<m_items.size(); i++)
167 InventoryItem *item = m_items[i];
183 void Inventory::deSerialize(std::istream &is)
185 //is.imbue(std::locale("C"));
193 std::getline(is, line, '\n');
195 std::istringstream iss(line);
196 //iss.imbue(std::locale("C"));
199 std::getline(iss, name, ' ');
205 else if(name == "Item")
207 if(item_i > getSize() - 1)
208 throw SerializationError("too many items");
209 InventoryItem *item = InventoryItem::deSerialize(iss);
210 m_items[item_i++] = item;
212 else if(name == "Empty")
214 if(item_i > getSize() - 1)
215 throw SerializationError("too many items");
216 m_items[item_i++] = NULL;
220 throw SerializationError("Unknown inventory identifier");
225 Inventory & Inventory::operator = (Inventory &other)
227 m_size = other.m_size;
229 for(u32 i=0; i<other.m_items.size(); i++)
231 InventoryItem *item = other.m_items[i];
234 m_items[i] = item->clone();
241 u32 Inventory::getSize()
243 return m_items.size();
246 u32 Inventory::getUsedSlots()
249 for(u32 i=0; i<m_items.size(); i++)
251 InventoryItem *item = m_items[i];
258 InventoryItem * Inventory::getItem(u32 i)
260 if(i > m_items.size() - 1)
265 InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem)
267 assert(i < m_items.size());
269 InventoryItem *olditem = m_items[i];
270 m_items[i] = newitem;
274 void Inventory::deleteItem(u32 i)
276 assert(i < m_items.size());
277 InventoryItem *item = changeItem(i, NULL);
282 bool Inventory::addItem(InventoryItem *newitem)
284 // If it is a MaterialItem, try to find an already existing one
285 // and just increment the counter
286 if(std::string("MaterialItem") == newitem->getName())
288 u8 material = ((MaterialItem*)newitem)->getMaterial();
289 u8 count = ((MaterialItem*)newitem)->getCount();
290 for(u32 i=0; i<m_items.size(); i++)
292 InventoryItem *item2 = m_items[i];
295 if(std::string("MaterialItem") != item2->getName())
297 // Found one. Check if it is of the right material and has
299 MaterialItem *mitem2 = (MaterialItem*)item2;
300 if(mitem2->getMaterial() != material)
302 //TODO: Add all that can be added and add remaining part
304 if(mitem2->freeSpace() < count)
306 // Add to the counter
308 // Dump the parameter
313 // Else find an empty position
314 for(u32 i=0; i<m_items.size(); i++)
316 InventoryItem *item = m_items[i];
319 m_items[i] = newitem;
326 void Inventory::print(std::ostream &o)
328 o<<"Player inventory:"<<std::endl;
329 for(u32 i=0; i<m_items.size(); i++)
331 InventoryItem *item = m_items[i];