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");
85 if(m_inventorystring.substr(0,4) == "Sign")
86 return g_device->getVideoDriver()->getTexture("../data/sign.png");
90 std::string MapBlockObjectItem::getText()
92 if(m_inventorystring.substr(0,3) == "Rat")
95 if(m_inventorystring.substr(0,4) == "Sign")
101 MapBlockObject * MapBlockObjectItem::createObject
102 (v3f pos, f32 player_yaw, f32 player_pitch)
104 std::istringstream is(m_inventorystring);
106 std::getline(is, name, ' ');
112 else if(name == "Sign")
115 std::getline(is, text, '|');
116 SignObject *obj = new SignObject(NULL, -1, pos);
118 obj->setYaw(-player_yaw);
121 else if(name == "Rat")
123 RatObject *obj = new RatObject(NULL, -1, pos);
136 Inventory::Inventory(u32 size)
142 Inventory::~Inventory()
144 for(u32 i=0; i<m_items.size(); i++)
150 void Inventory::clearItems()
153 for(u32 i=0; i<m_size; i++)
155 m_items.push_back(NULL);
159 void Inventory::serialize(std::ostream &os)
161 //os.imbue(std::locale("C"));
163 for(u32 i=0; i<m_items.size(); i++)
165 InventoryItem *item = m_items[i];
181 void Inventory::deSerialize(std::istream &is)
183 //is.imbue(std::locale("C"));
191 std::getline(is, line, '\n');
193 std::istringstream iss(line);
194 //iss.imbue(std::locale("C"));
197 std::getline(iss, name, ' ');
203 else if(name == "Item")
205 if(item_i > getSize() - 1)
206 throw SerializationError("too many items");
207 InventoryItem *item = InventoryItem::deSerialize(iss);
208 m_items[item_i++] = item;
210 else if(name == "Empty")
212 if(item_i > getSize() - 1)
213 throw SerializationError("too many items");
214 m_items[item_i++] = NULL;
218 throw SerializationError("Unknown inventory identifier");
223 Inventory & Inventory::operator = (Inventory &other)
225 m_size = other.m_size;
227 for(u32 i=0; i<other.m_items.size(); i++)
229 InventoryItem *item = other.m_items[i];
232 m_items[i] = item->clone();
239 u32 Inventory::getSize()
241 return m_items.size();
244 u32 Inventory::getUsedSlots()
247 for(u32 i=0; i<m_items.size(); i++)
249 InventoryItem *item = m_items[i];
256 InventoryItem * Inventory::getItem(u32 i)
258 if(i > m_items.size() - 1)
263 InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem)
265 assert(i < m_items.size());
267 InventoryItem *olditem = m_items[i];
268 m_items[i] = newitem;
272 void Inventory::deleteItem(u32 i)
274 assert(i < m_items.size());
275 InventoryItem *item = changeItem(i, NULL);
280 bool Inventory::addItem(InventoryItem *newitem)
282 // If it is a MaterialItem, try to find an already existing one
283 // and just increment the counter
284 if(std::string("MaterialItem") == newitem->getName())
286 u8 material = ((MaterialItem*)newitem)->getMaterial();
287 u8 count = ((MaterialItem*)newitem)->getCount();
288 for(u32 i=0; i<m_items.size(); i++)
290 InventoryItem *item2 = m_items[i];
293 if(std::string("MaterialItem") != item2->getName())
295 // Found one. Check if it is of the right material and has
297 MaterialItem *mitem2 = (MaterialItem*)item2;
298 if(mitem2->getMaterial() != material)
300 //TODO: Add all that can be added and add remaining part
302 if(mitem2->freeSpace() < count)
304 // Add to the counter
306 // Dump the parameter
311 // Else find an empty position
312 for(u32 i=0; i<m_items.size(); i++)
314 InventoryItem *item = m_items[i];
317 m_items[i] = newitem;
324 void Inventory::print(std::ostream &o)
326 o<<"Player inventory:"<<std::endl;
327 for(u32 i=0; i<m_items.size(); i++)
329 InventoryItem *item = m_items[i];