]> git.lizzy.rs Git - dragonfireclient.git/blob - src/inventory.h
in before messing with face drawing orientation
[dragonfireclient.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                 u16 tile = content_tile(m_material, v3s16(1,0,0));
90                 return g_tile_materials[tile].getTexture(0);
91         }
92         std::string getText()
93         {
94                 std::ostringstream os;
95                 os<<m_count;
96                 return os.str();
97         }
98         /*
99                 Special methods
100         */
101         u8 getMaterial()
102         {
103                 return m_material;
104         }
105         u16 getCount()
106         {
107                 return m_count;
108         }
109         u16 freeSpace()
110         {
111                 if(m_count > MATERIAL_ITEM_MAX_COUNT)
112                         return 0;
113                 return MATERIAL_ITEM_MAX_COUNT - m_count;
114         }
115         void add(u16 count)
116         {
117                 assert(m_count + count <= MATERIAL_ITEM_MAX_COUNT);
118                 m_count += count;
119         }
120         void remove(u16 count)
121         {
122                 assert(m_count >= count);
123                 m_count -= count;
124         }
125 private:
126         u8 m_material;
127         u16 m_count;
128 };
129
130 class MapBlockObjectItem : public InventoryItem
131 {
132 public:
133         /*MapBlockObjectItem(MapBlockObject *obj)
134         {
135                 m_inventorystring = obj->getInventoryString();
136         }*/
137         MapBlockObjectItem(std::string inventorystring)
138         {
139                 m_inventorystring = inventorystring;
140         }
141         
142         /*
143                 Implementation interface
144         */
145         virtual const char* getName() const
146         {
147                 return "MBOItem";
148         }
149         virtual void serialize(std::ostream &os)
150         {
151                 for(;;)
152                 {
153                         size_t t = m_inventorystring.find('|');
154                         if(t == std::string::npos)
155                                 break;
156                         m_inventorystring[t] = '?';
157                 }
158                 os<<getName();
159                 os<<" ";
160                 os<<m_inventorystring;
161                 os<<"|";
162         }
163         virtual InventoryItem* clone()
164         {
165                 return new MapBlockObjectItem(m_inventorystring);
166         }
167
168         video::ITexture * getImage();
169         std::string getText();
170
171         /*
172                 Special methods
173         */
174         std::string getInventoryString()
175         {
176                 return m_inventorystring;
177         }
178
179         MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
180
181 private:
182         std::string m_inventorystring;
183 };
184
185 //SUGGESTION: Split into ClientInventory and ServerInventory
186 class Inventory
187 {
188 public:
189         Inventory(u32 size);
190         ~Inventory();
191         void clearItems();
192         void serialize(std::ostream &os);
193         void deSerialize(std::istream &is);
194
195         Inventory & operator = (Inventory &other);
196
197         u32 getSize();
198         u32 getUsedSlots();
199         
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);
206
207         void print(std::ostream &o);
208         
209 private:
210         core::array<InventoryItem*> m_items;
211         u32 m_size;
212 };
213
214 #endif
215