]> git.lizzy.rs Git - minetest.git/blob - src/inventory.h
plant amount fix and ravine amount setting
[minetest.git] / src / inventory.h
1 /*
2 (c) 2010 Perttu Ahola <celeron55@gmail.com>
3 */
4
5 #ifndef INVENTORY_HEADER
6 #define INVENTORY_HEADER
7
8 #include <iostream>
9 #include <sstream>
10 #include <string>
11 #include "common_irrlicht.h"
12 #include "debug.h"
13 #include "mapblockobject.h"
14 // For g_materials
15 #include "main.h"
16
17 class InventoryItem
18 {
19 public:
20         InventoryItem();
21         virtual ~InventoryItem();
22         
23         static InventoryItem* deSerialize(std::istream &is);
24         
25         virtual const char* getName() const = 0;
26         // Shall write the name and the parameters
27         virtual void serialize(std::ostream &os) = 0;
28         // Shall make an exact clone of the item
29         virtual InventoryItem* clone() = 0;
30         // Shall return an image to show in the GUI (or NULL)
31         virtual video::ITexture * getImage() { return NULL; }
32         // Shall return a text to show in the GUI
33         virtual std::string getText() { return ""; }
34
35 private:
36 };
37
38 #define MATERIAL_ITEM_MAX_COUNT 99
39
40 class MaterialItem : public InventoryItem
41 {
42 public:
43         MaterialItem(u8 material, u16 count)
44         {
45                 m_material = material;
46                 m_count = count;
47         }
48         /*
49                 Implementation interface
50         */
51         virtual const char* getName() const
52         {
53                 return "MaterialItem";
54         }
55         virtual void serialize(std::ostream &os)
56         {
57                 //os.imbue(std::locale("C"));
58                 os<<getName();
59                 os<<" ";
60                 os<<(unsigned int)m_material;
61                 os<<" ";
62                 os<<m_count;
63         }
64         virtual InventoryItem* clone()
65         {
66                 return new MaterialItem(m_material, m_count);
67         }
68         video::ITexture * getImage()
69         {
70                 return g_materials[m_material].getTexture(0);
71         }
72         std::string getText()
73         {
74                 std::ostringstream os;
75                 os<<m_count;
76                 return os.str();
77         }
78         /*
79                 Special methods
80         */
81         u8 getMaterial()
82         {
83                 return m_material;
84         }
85         u16 getCount()
86         {
87                 return m_count;
88         }
89         u16 freeSpace()
90         {
91                 if(m_count > MATERIAL_ITEM_MAX_COUNT)
92                         return 0;
93                 return MATERIAL_ITEM_MAX_COUNT - m_count;
94         }
95         void add(u16 count)
96         {
97                 assert(m_count + count <= MATERIAL_ITEM_MAX_COUNT);
98                 m_count += count;
99         }
100         void remove(u16 count)
101         {
102                 assert(m_count >= count);
103                 m_count -= count;
104         }
105 private:
106         u8 m_material;
107         u16 m_count;
108 };
109
110 class MapBlockObjectItem : public InventoryItem
111 {
112 public:
113         /*MapBlockObjectItem(MapBlockObject *obj)
114         {
115                 m_inventorystring = obj->getInventoryString();
116         }*/
117         MapBlockObjectItem(std::string inventorystring)
118         {
119                 m_inventorystring = inventorystring;
120         }
121         
122         /*
123                 Implementation interface
124         */
125         virtual const char* getName() const
126         {
127                 return "MBOItem";
128         }
129         virtual void serialize(std::ostream &os)
130         {
131                 for(;;)
132                 {
133                         size_t t = m_inventorystring.find('|');
134                         if(t == std::string::npos)
135                                 break;
136                         m_inventorystring[t] = '?';
137                 }
138                 os<<getName();
139                 os<<" ";
140                 os<<m_inventorystring;
141                 os<<"|";
142         }
143         virtual InventoryItem* clone()
144         {
145                 return new MapBlockObjectItem(m_inventorystring);
146         }
147
148         video::ITexture * getImage();
149         std::string getText();
150
151         /*
152                 Special methods
153         */
154         std::string getInventoryString()
155         {
156                 return m_inventorystring;
157         }
158
159         MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
160
161 private:
162         std::string m_inventorystring;
163 };
164
165 //SUGGESTION: Split into ClientInventory and ServerInventory
166 class Inventory
167 {
168 public:
169         Inventory(u32 size);
170         ~Inventory();
171         void clearItems();
172         void serialize(std::ostream &os);
173         void deSerialize(std::istream &is);
174
175         Inventory & operator = (Inventory &other);
176
177         u32 getSize();
178         u32 getUsedSlots();
179         
180         InventoryItem * getItem(u32 i);
181         // Returns old item (or NULL). Parameter can be NULL.
182         InventoryItem * changeItem(u32 i, InventoryItem *newitem);
183         void deleteItem(u32 i);
184         // Adds an item to a suitable place. Returns false if failed.
185         bool addItem(InventoryItem *newitem);
186
187         void print(std::ostream &o);
188         
189 private:
190         core::array<InventoryItem*> m_items;
191         u32 m_size;
192 };
193
194 #endif
195