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 #ifndef INVENTORY_HEADER
25 #define INVENTORY_HEADER
30 #include "common_irrlicht.h"
32 #include "mapblockobject.h"
40 virtual ~InventoryItem();
42 static InventoryItem* deSerialize(std::istream &is);
44 virtual const char* getName() const = 0;
45 // Shall write the name and the parameters
46 virtual void serialize(std::ostream &os) = 0;
47 // Shall make an exact clone of the item
48 virtual InventoryItem* clone() = 0;
49 // Shall return an image to show in the GUI (or NULL)
50 virtual video::ITexture * getImage() { return NULL; }
51 // Shall return a text to show in the GUI
52 virtual std::string getText() { return ""; }
57 #define MATERIAL_ITEM_MAX_COUNT 99
59 class MaterialItem : public InventoryItem
62 MaterialItem(u8 material, u16 count)
64 m_material = material;
68 Implementation interface
70 virtual const char* getName() const
72 return "MaterialItem";
74 virtual void serialize(std::ostream &os)
76 //os.imbue(std::locale("C"));
79 os<<(unsigned int)m_material;
83 virtual InventoryItem* clone()
85 return new MaterialItem(m_material, m_count);
87 video::ITexture * getImage()
89 u16 tile = content_tile(m_material, v3s16(1,0,0));
90 return g_tile_materials[tile].getTexture(0);
94 std::ostringstream os;
111 if(m_count > MATERIAL_ITEM_MAX_COUNT)
113 return MATERIAL_ITEM_MAX_COUNT - m_count;
117 assert(m_count + count <= MATERIAL_ITEM_MAX_COUNT);
120 void remove(u16 count)
122 assert(m_count >= count);
130 class MapBlockObjectItem : public InventoryItem
133 /*MapBlockObjectItem(MapBlockObject *obj)
135 m_inventorystring = obj->getInventoryString();
137 MapBlockObjectItem(std::string inventorystring)
139 m_inventorystring = inventorystring;
143 Implementation interface
145 virtual const char* getName() const
149 virtual void serialize(std::ostream &os)
153 size_t t = m_inventorystring.find('|');
154 if(t == std::string::npos)
156 m_inventorystring[t] = '?';
160 os<<m_inventorystring;
163 virtual InventoryItem* clone()
165 return new MapBlockObjectItem(m_inventorystring);
168 video::ITexture * getImage();
169 std::string getText();
174 std::string getInventoryString()
176 return m_inventorystring;
179 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
182 std::string m_inventorystring;
185 //SUGGESTION: Split into ClientInventory and ServerInventory
192 void serialize(std::ostream &os);
193 void deSerialize(std::istream &is);
195 Inventory & operator = (Inventory &other);
200 InventoryItem * getItem(u32 i);
201 // Returns old item (or NULL). Parameter can be NULL.
202 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
203 void deleteItem(u32 i);
204 // Adds an item to a suitable place. Returns false if failed.
205 bool addItem(InventoryItem *newitem);
207 void print(std::ostream &o);
210 core::array<InventoryItem*> m_items;