2 (c) 2010 Perttu Ahola <celeron55@gmail.com>
6 #include "serialization.h"
16 InventoryItem::InventoryItem()
20 InventoryItem::~InventoryItem()
24 InventoryItem* InventoryItem::deSerialize(std::istream &is)
26 DSTACK(__FUNCTION_NAME);
28 //is.imbue(std::locale("C"));
31 std::getline(is, name, ' ');
33 if(name == "MaterialItem")
35 // u16 reads directly as a number (u8 doesn't)
41 throw SerializationError("Too large material number");
42 return new MaterialItem(material, count);
44 else if(name == "MBOItem")
46 std::string inventorystring;
47 std::getline(is, inventorystring, '|');
48 return new MapBlockObjectItem(inventorystring);
52 dstream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
53 throw SerializationError("Unknown InventoryItem name");
61 video::ITexture * MapBlockObjectItem::getImage()
63 if(m_inventorystring.substr(0,3) == "Rat")
64 return g_device->getVideoDriver()->getTexture("../data/rat.png");
66 if(m_inventorystring.substr(0,4) == "Sign")
67 return g_device->getVideoDriver()->getTexture("../data/sign.png");
71 std::string MapBlockObjectItem::getText()
73 if(m_inventorystring.substr(0,3) == "Rat")
76 if(m_inventorystring.substr(0,4) == "Sign")
82 MapBlockObject * MapBlockObjectItem::createObject
83 (v3f pos, f32 player_yaw, f32 player_pitch)
85 std::istringstream is(m_inventorystring);
87 std::getline(is, name, ' ');
93 else if(name == "Sign")
96 std::getline(is, text, '|');
97 SignObject *obj = new SignObject(NULL, -1, pos);
99 obj->setYaw(-player_yaw);
102 else if(name == "Rat")
104 RatObject *obj = new RatObject(NULL, -1, pos);
117 Inventory::Inventory(u32 size)
123 Inventory::~Inventory()
125 for(u32 i=0; i<m_items.size(); i++)
131 void Inventory::clearItems()
134 for(u32 i=0; i<m_size; i++)
136 m_items.push_back(NULL);
140 void Inventory::serialize(std::ostream &os)
142 //os.imbue(std::locale("C"));
144 for(u32 i=0; i<m_items.size(); i++)
146 InventoryItem *item = m_items[i];
162 void Inventory::deSerialize(std::istream &is)
164 //is.imbue(std::locale("C"));
172 std::getline(is, line, '\n');
174 std::istringstream iss(line);
175 //iss.imbue(std::locale("C"));
178 std::getline(iss, name, ' ');
184 else if(name == "Item")
186 if(item_i > getSize() - 1)
187 throw SerializationError("too many items");
188 InventoryItem *item = InventoryItem::deSerialize(iss);
189 m_items[item_i++] = item;
191 else if(name == "Empty")
193 if(item_i > getSize() - 1)
194 throw SerializationError("too many items");
195 m_items[item_i++] = NULL;
199 throw SerializationError("Unknown inventory identifier");
204 Inventory & Inventory::operator = (Inventory &other)
206 m_size = other.m_size;
208 for(u32 i=0; i<other.m_items.size(); i++)
210 InventoryItem *item = other.m_items[i];
213 m_items[i] = item->clone();
220 u32 Inventory::getSize()
222 return m_items.size();
225 u32 Inventory::getUsedSlots()
228 for(u32 i=0; i<m_items.size(); i++)
230 InventoryItem *item = m_items[i];
237 InventoryItem * Inventory::getItem(u32 i)
239 if(i > m_items.size() - 1)
244 InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem)
246 assert(i < m_items.size());
248 InventoryItem *olditem = m_items[i];
249 m_items[i] = newitem;
253 void Inventory::deleteItem(u32 i)
255 assert(i < m_items.size());
256 InventoryItem *item = changeItem(i, NULL);
261 bool Inventory::addItem(InventoryItem *newitem)
263 // If it is a MaterialItem, try to find an already existing one
264 // and just increment the counter
265 if(std::string("MaterialItem") == newitem->getName())
267 u8 material = ((MaterialItem*)newitem)->getMaterial();
268 u8 count = ((MaterialItem*)newitem)->getCount();
269 for(u32 i=0; i<m_items.size(); i++)
271 InventoryItem *item2 = m_items[i];
274 if(std::string("MaterialItem") != item2->getName())
276 // Found one. Check if it is of the right material and has
278 MaterialItem *mitem2 = (MaterialItem*)item2;
279 if(mitem2->getMaterial() != material)
281 //TODO: Add all that can be added and add remaining part
283 if(mitem2->freeSpace() < count)
285 // Add to the counter
287 // Dump the parameter
292 // Else find an empty position
293 for(u32 i=0; i<m_items.size(); i++)
295 InventoryItem *item = m_items[i];
298 m_items[i] = newitem;
305 void Inventory::print(std::ostream &o)
307 o<<"Player inventory:"<<std::endl;
308 for(u32 i=0; i<m_items.size(); i++)
310 InventoryItem *item = m_items[i];