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