2 (c) 2010 Perttu Ahola <celeron55@gmail.com>
5 #ifndef INVENTORY_HEADER
6 #define INVENTORY_HEADER
11 #include "common_irrlicht.h"
13 #include "mapblockobject.h"
21 virtual ~InventoryItem();
23 static InventoryItem* deSerialize(std::istream &is);
25 virtual const char* getName() const = 0;
26 // Shall write the name and the parameters
27 virtual void serialize(std::ostream &os) = 0;
28 // Shall make an exact clone of the item
29 virtual InventoryItem* clone() = 0;
30 // Shall return an image to show in the GUI (or NULL)
31 virtual video::ITexture * getImage() { return NULL; }
32 // Shall return a text to show in the GUI
33 virtual std::string getText() { return ""; }
38 #define MATERIAL_ITEM_MAX_COUNT 99
40 class MaterialItem : public InventoryItem
43 MaterialItem(u8 material, u16 count)
45 m_material = material;
49 Implementation interface
51 virtual const char* getName() const
53 return "MaterialItem";
55 virtual void serialize(std::ostream &os)
57 //os.imbue(std::locale("C"));
60 os<<(unsigned int)m_material;
64 virtual InventoryItem* clone()
66 return new MaterialItem(m_material, m_count);
68 video::ITexture * getImage()
70 return g_materials[m_material].getTexture(0);
74 std::ostringstream os;
91 if(m_count > MATERIAL_ITEM_MAX_COUNT)
93 return MATERIAL_ITEM_MAX_COUNT - m_count;
97 assert(m_count + count <= MATERIAL_ITEM_MAX_COUNT);
100 void remove(u16 count)
102 assert(m_count >= count);
110 class MapBlockObjectItem : public InventoryItem
113 /*MapBlockObjectItem(MapBlockObject *obj)
115 m_inventorystring = obj->getInventoryString();
117 MapBlockObjectItem(std::string inventorystring)
119 m_inventorystring = inventorystring;
123 Implementation interface
125 virtual const char* getName() const
129 virtual void serialize(std::ostream &os)
133 size_t t = m_inventorystring.find('|');
134 if(t == std::string::npos)
136 m_inventorystring[t] = '?';
140 os<<m_inventorystring;
143 virtual InventoryItem* clone()
145 return new MapBlockObjectItem(m_inventorystring);
148 video::ITexture * getImage();
149 std::string getText();
154 std::string getInventoryString()
156 return m_inventorystring;
159 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
162 std::string m_inventorystring;
165 //SUGGESTION: Split into ClientInventory and ServerInventory
172 void serialize(std::ostream &os);
173 void deSerialize(std::istream &is);
175 Inventory & operator = (Inventory &other);
180 InventoryItem * getItem(u32 i);
181 // Returns old item (or NULL). Parameter can be NULL.
182 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
183 void deleteItem(u32 i);
184 // Adds an item to a suitable place. Returns false if failed.
185 bool addItem(InventoryItem *newitem);
187 void print(std::ostream &o);
190 core::array<InventoryItem*> m_items;