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;
50 // Shall return an image to show in the GUI (or NULL)
51 virtual video::ITexture * getImage() { return NULL; }
53 // Shall return a text to show in the GUI
54 virtual std::string getText() { return ""; }
59 #define MATERIAL_ITEM_MAX_COUNT 99
61 class MaterialItem : public InventoryItem
64 MaterialItem(u8 content, u16 count)
70 Implementation interface
72 virtual const char* getName() const
74 return "MaterialItem";
76 virtual void serialize(std::ostream &os)
78 //os.imbue(std::locale("C"));
81 os<<(unsigned int)m_content;
85 virtual InventoryItem* clone()
87 return new MaterialItem(m_content, m_count);
90 video::ITexture * getImage()
92 /*if(m_content == CONTENT_TORCH)
93 return g_texturecache.get("torch_on_floor");
95 u16 tile = content_tile(m_content, v3s16(1,0,0));
96 return g_tile_contents[tile].getTexture(0);*/
98 if(m_content >= USEFUL_CONTENT_COUNT)
101 return g_irrlicht->getTexture(g_content_inventory_textures[m_content]);
104 std::string getText()
106 std::ostringstream os;
123 if(m_count > MATERIAL_ITEM_MAX_COUNT)
125 return MATERIAL_ITEM_MAX_COUNT - m_count;
129 assert(m_count + count <= MATERIAL_ITEM_MAX_COUNT);
132 void remove(u16 count)
134 assert(m_count >= count);
142 class MapBlockObjectItem : public InventoryItem
145 /*MapBlockObjectItem(MapBlockObject *obj)
147 m_inventorystring = obj->getInventoryString();
149 MapBlockObjectItem(std::string inventorystring)
151 m_inventorystring = inventorystring;
155 Implementation interface
157 virtual const char* getName() const
161 virtual void serialize(std::ostream &os)
165 size_t t = m_inventorystring.find('|');
166 if(t == std::string::npos)
168 m_inventorystring[t] = '?';
172 os<<m_inventorystring;
175 virtual InventoryItem* clone()
177 return new MapBlockObjectItem(m_inventorystring);
181 video::ITexture * getImage();
183 std::string getText();
188 std::string getInventoryString()
190 return m_inventorystring;
193 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
196 std::string m_inventorystring;
199 class ToolItem : public InventoryItem
202 ToolItem(std::string toolname, u16 wear)
204 m_toolname = toolname;
208 Implementation interface
210 virtual const char* getName() const
214 virtual void serialize(std::ostream &os)
222 virtual InventoryItem* clone()
224 return new ToolItem(m_toolname, m_wear);
227 video::ITexture * getImage()
229 if(m_toolname == "WPick")
230 return g_irrlicht->getTexture("../data/tool_wpick.png");
231 if(m_toolname == "STPick")
232 return g_irrlicht->getTexture("../data/tool_stpick.png");
233 // Default to cloud texture
234 return g_irrlicht->getTexture(tile_texture_path_get(TILE_CLOUD));
237 std::string getText()
239 std::ostringstream os;
243 for(i=0; i<(65535-m_wear)/d; i++)
249 /*std::ostringstream os;
258 std::string getToolName()
267 std::string m_toolname;
274 InventoryList(std::string name, u32 size);
277 void serialize(std::ostream &os);
278 void deSerialize(std::istream &is);
280 InventoryList(const InventoryList &other);
281 InventoryList & operator = (const InventoryList &other);
283 std::string getName();
288 // Get pointer to item
289 InventoryItem * getItem(u32 i);
290 // Returns old item (or NULL). Parameter can be NULL.
291 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
293 void deleteItem(u32 i);
294 // Adds an item to a suitable place. Returns false if failed.
295 bool addItem(InventoryItem *newitem);
296 // If possible, adds item to given slot. Returns true on success.
297 // Fails when slot is populated by a different kind of item.
298 bool addItem(u32 i, InventoryItem *newitem);
300 // Decrements amount of every material item
301 void decrementMaterials(u16 count);
303 void print(std::ostream &o);
306 core::array<InventoryItem*> m_items;
319 Inventory(const Inventory &other);
320 Inventory & operator = (const Inventory &other);
322 void serialize(std::ostream &os);
323 void deSerialize(std::istream &is);
325 InventoryList * addList(const std::string &name, u32 size);
326 InventoryList * getList(const std::string &name);
327 bool deleteList(const std::string &name);
328 // A shorthand for adding items
329 bool addItem(const std::string &listname, InventoryItem *newitem)
331 InventoryList *list = getList(listname);
334 return list->addItem(newitem);
339 s32 getListIndex(const std::string &name);
341 core::array<InventoryList*> m_lists;
344 #define IACTION_MOVE 0
346 struct InventoryAction
348 static InventoryAction * deSerialize(std::istream &is);
350 virtual u16 getType() const = 0;
351 virtual void serialize(std::ostream &os) = 0;
352 virtual void apply(Inventory *inventory) = 0;
355 struct IMoveAction : public InventoryAction
358 std::string from_name;
369 IMoveAction(std::istream &is)
373 std::getline(is, ts, ' ');
376 std::getline(is, from_name, ' ');
378 std::getline(is, ts, ' ');
381 std::getline(is, to_name, ' ');
383 std::getline(is, ts, ' ');
392 void serialize(std::ostream &os)
402 void apply(Inventory *inventory);