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 Inventory::Inventory(u32 size)
145 Inventory::~Inventory()
147 for(u32 i=0; i<m_items.size(); i++)
153 void Inventory::clearItems()
156 for(u32 i=0; i<m_size; i++)
158 m_items.push_back(NULL);
162 void Inventory::serialize(std::ostream &os)
164 //os.imbue(std::locale("C"));
166 for(u32 i=0; i<m_items.size(); i++)
168 InventoryItem *item = m_items[i];
184 void Inventory::deSerialize(std::istream &is)
186 //is.imbue(std::locale("C"));
194 std::getline(is, line, '\n');
196 std::istringstream iss(line);
197 //iss.imbue(std::locale("C"));
200 std::getline(iss, name, ' ');
206 else if(name == "Item")
208 if(item_i > getSize() - 1)
209 throw SerializationError("too many items");
210 InventoryItem *item = InventoryItem::deSerialize(iss);
211 m_items[item_i++] = item;
213 else if(name == "Empty")
215 if(item_i > getSize() - 1)
216 throw SerializationError("too many items");
217 m_items[item_i++] = NULL;
221 throw SerializationError("Unknown inventory identifier");
226 Inventory & Inventory::operator = (Inventory &other)
228 m_size = other.m_size;
230 for(u32 i=0; i<other.m_items.size(); i++)
232 InventoryItem *item = other.m_items[i];
235 m_items[i] = item->clone();
242 u32 Inventory::getSize()
244 return m_items.size();
247 u32 Inventory::getUsedSlots()
250 for(u32 i=0; i<m_items.size(); i++)
252 InventoryItem *item = m_items[i];
259 InventoryItem * Inventory::getItem(u32 i)
261 if(i > m_items.size() - 1)
266 InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem)
268 assert(i < m_items.size());
270 InventoryItem *olditem = m_items[i];
271 m_items[i] = newitem;
275 void Inventory::deleteItem(u32 i)
277 assert(i < m_items.size());
278 InventoryItem *item = changeItem(i, NULL);
283 bool Inventory::addItem(InventoryItem *newitem)
285 // If it is a MaterialItem, try to find an already existing one
286 // and just increment the counter
287 if(std::string("MaterialItem") == newitem->getName())
289 u8 material = ((MaterialItem*)newitem)->getMaterial();
290 u8 count = ((MaterialItem*)newitem)->getCount();
291 for(u32 i=0; i<m_items.size(); i++)
293 InventoryItem *item2 = m_items[i];
296 if(std::string("MaterialItem") != item2->getName())
298 // Found one. Check if it is of the right material and has
300 MaterialItem *mitem2 = (MaterialItem*)item2;
301 if(mitem2->getMaterial() != material)
303 //TODO: Add all that can be added and add remaining part
305 if(mitem2->freeSpace() < count)
307 // Add to the counter
309 // Dump the parameter
314 // Else find an empty position
315 for(u32 i=0; i<m_items.size(); i++)
317 InventoryItem *item = m_items[i];
320 m_items[i] = newitem;
327 void Inventory::print(std::ostream &o)
329 o<<"Player inventory:"<<std::endl;
330 for(u32 i=0; i<m_items.size(); i++)
332 InventoryItem *item = m_items[i];