]> git.lizzy.rs Git - minetest.git/blob - src/inventory.h
59ff89ed8bbd4c82dc49995ed287134f367d85a6
[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 #ifndef SERVER
88         video::ITexture * getImage()
89         {
90                 /*if(m_content == CONTENT_TORCH)
91                         return g_texturecache.get("torch_on_floor");
92
93                 u16 tile = content_tile(m_content, v3s16(1,0,0));
94                 return g_tile_contents[tile].getTexture(0);*/
95                 
96                 if(m_content >= USEFUL_CONTENT_COUNT)
97                         return NULL;
98                         
99                 return g_irrlicht->getTexture(g_content_inventory_textures[m_content]);
100         }
101 #endif
102         std::string getText()
103         {
104                 std::ostringstream os;
105                 os<<m_count;
106                 return os.str();
107         }
108         /*
109                 Special methods
110         */
111         u8 getMaterial()
112         {
113                 return m_content;
114         }
115         u16 getCount()
116         {
117                 return m_count;
118         }
119         u16 freeSpace()
120         {
121                 if(m_count > MATERIAL_ITEM_MAX_COUNT)
122                         return 0;
123                 return MATERIAL_ITEM_MAX_COUNT - m_count;
124         }
125         void add(u16 count)
126         {
127                 assert(m_count + count <= MATERIAL_ITEM_MAX_COUNT);
128                 m_count += count;
129         }
130         void remove(u16 count)
131         {
132                 assert(m_count >= count);
133                 m_count -= count;
134         }
135 private:
136         u8 m_content;
137         u16 m_count;
138 };
139
140 class MapBlockObjectItem : public InventoryItem
141 {
142 public:
143         /*MapBlockObjectItem(MapBlockObject *obj)
144         {
145                 m_inventorystring = obj->getInventoryString();
146         }*/
147         MapBlockObjectItem(std::string inventorystring)
148         {
149                 m_inventorystring = inventorystring;
150         }
151         
152         /*
153                 Implementation interface
154         */
155         virtual const char* getName() const
156         {
157                 return "MBOItem";
158         }
159         virtual void serialize(std::ostream &os)
160         {
161                 for(;;)
162                 {
163                         size_t t = m_inventorystring.find('|');
164                         if(t == std::string::npos)
165                                 break;
166                         m_inventorystring[t] = '?';
167                 }
168                 os<<getName();
169                 os<<" ";
170                 os<<m_inventorystring;
171                 os<<"|";
172         }
173         virtual InventoryItem* clone()
174         {
175                 return new MapBlockObjectItem(m_inventorystring);
176         }
177
178         video::ITexture * getImage();
179         std::string getText();
180
181         /*
182                 Special methods
183         */
184         std::string getInventoryString()
185         {
186                 return m_inventorystring;
187         }
188
189         MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
190
191 private:
192         std::string m_inventorystring;
193 };
194
195 //SUGGESTION: Split into ClientInventory and ServerInventory
196 class Inventory
197 {
198 public:
199         Inventory(u32 size);
200         ~Inventory();
201         void clearItems();
202         void serialize(std::ostream &os);
203         void deSerialize(std::istream &is);
204
205         Inventory & operator = (Inventory &other);
206
207         u32 getSize();
208         u32 getUsedSlots();
209         
210         InventoryItem * getItem(u32 i);
211         // Returns old item (or NULL). Parameter can be NULL.
212         InventoryItem * changeItem(u32 i, InventoryItem *newitem);
213         void deleteItem(u32 i);
214         // Adds an item to a suitable place. Returns false if failed.
215         bool addItem(InventoryItem *newitem);
216
217         void print(std::ostream &o);
218         
219 private:
220         core::array<InventoryItem*> m_items;
221         u32 m_size;
222 };
223
224 #endif
225