]> git.lizzy.rs Git - minetest.git/blob - src/inventory.h
better graphics, zlib to work on vc++
[minetest.git] / src / inventory.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #ifndef INVENTORY_HEADER
25 #define INVENTORY_HEADER
26
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include "common_irrlicht.h"
31 #include "debug.h"
32 #include "mapblockobject.h"
33 // For g_materials
34 #include "main.h"
35
36 class InventoryItem
37 {
38 public:
39         InventoryItem();
40         virtual ~InventoryItem();
41         
42         static InventoryItem* deSerialize(std::istream &is);
43         
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 ""; }
53
54 private:
55 };
56
57 #define MATERIAL_ITEM_MAX_COUNT 99
58
59 class MaterialItem : public InventoryItem
60 {
61 public:
62         MaterialItem(u8 content, u16 count)
63         {
64                 m_content = content;
65                 m_count = count;
66         }
67         /*
68                 Implementation interface
69         */
70         virtual const char* getName() const
71         {
72                 return "MaterialItem";
73         }
74         virtual void serialize(std::ostream &os)
75         {
76                 //os.imbue(std::locale("C"));
77                 os<<getName();
78                 os<<" ";
79                 os<<(unsigned int)m_content;
80                 os<<" ";
81                 os<<m_count;
82         }
83         virtual InventoryItem* clone()
84         {
85                 return new MaterialItem(m_content, m_count);
86         }
87         video::ITexture * getImage()
88         {
89                 /*if(m_content == CONTENT_TORCH)
90                         return g_texturecache.get("torch_on_floor");
91
92                 u16 tile = content_tile(m_content, v3s16(1,0,0));
93                 return g_tile_contents[tile].getTexture(0);*/
94                 
95                 if(m_content >= USEFUL_CONTENT_COUNT)
96                         return NULL;
97                         
98                 return g_texturecache.get(g_content_inventory_textures[m_content]);
99         }
100         std::string getText()
101         {
102                 std::ostringstream os;
103                 os<<m_count;
104                 return os.str();
105         }
106         /*
107                 Special methods
108         */
109         u8 getMaterial()
110         {
111                 return m_content;
112         }
113         u16 getCount()
114         {
115                 return m_count;
116         }
117         u16 freeSpace()
118         {
119                 if(m_count > MATERIAL_ITEM_MAX_COUNT)
120                         return 0;
121                 return MATERIAL_ITEM_MAX_COUNT - m_count;
122         }
123         void add(u16 count)
124         {
125                 assert(m_count + count <= MATERIAL_ITEM_MAX_COUNT);
126                 m_count += count;
127         }
128         void remove(u16 count)
129         {
130                 assert(m_count >= count);
131                 m_count -= count;
132         }
133 private:
134         u8 m_content;
135         u16 m_count;
136 };
137
138 class MapBlockObjectItem : public InventoryItem
139 {
140 public:
141         /*MapBlockObjectItem(MapBlockObject *obj)
142         {
143                 m_inventorystring = obj->getInventoryString();
144         }*/
145         MapBlockObjectItem(std::string inventorystring)
146         {
147                 m_inventorystring = inventorystring;
148         }
149         
150         /*
151                 Implementation interface
152         */
153         virtual const char* getName() const
154         {
155                 return "MBOItem";
156         }
157         virtual void serialize(std::ostream &os)
158         {
159                 for(;;)
160                 {
161                         size_t t = m_inventorystring.find('|');
162                         if(t == std::string::npos)
163                                 break;
164                         m_inventorystring[t] = '?';
165                 }
166                 os<<getName();
167                 os<<" ";
168                 os<<m_inventorystring;
169                 os<<"|";
170         }
171         virtual InventoryItem* clone()
172         {
173                 return new MapBlockObjectItem(m_inventorystring);
174         }
175
176         video::ITexture * getImage();
177         std::string getText();
178
179         /*
180                 Special methods
181         */
182         std::string getInventoryString()
183         {
184                 return m_inventorystring;
185         }
186
187         MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
188
189 private:
190         std::string m_inventorystring;
191 };
192
193 //SUGGESTION: Split into ClientInventory and ServerInventory
194 class Inventory
195 {
196 public:
197         Inventory(u32 size);
198         ~Inventory();
199         void clearItems();
200         void serialize(std::ostream &os);
201         void deSerialize(std::istream &is);
202
203         Inventory & operator = (Inventory &other);
204
205         u32 getSize();
206         u32 getUsedSlots();
207         
208         InventoryItem * getItem(u32 i);
209         // Returns old item (or NULL). Parameter can be NULL.
210         InventoryItem * changeItem(u32 i, InventoryItem *newitem);
211         void deleteItem(u32 i);
212         // Adds an item to a suitable place. Returns false if failed.
213         bool addItem(InventoryItem *newitem);
214
215         void print(std::ostream &o);
216         
217 private:
218         core::array<InventoryItem*> m_items;
219         u32 m_size;
220 };
221
222 #endif
223