base stuff for item->object conversion
[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 #ifndef SERVER
50         // Shall return an image to show in the GUI (or NULL)
51         virtual video::ITexture * getImage() { return NULL; }
52 #endif
53         // Shall return a text to show in the GUI
54         virtual std::string getText() { return ""; }
55
56 private:
57 };
58
59 #define MATERIAL_ITEM_MAX_COUNT 99
60
61 class MaterialItem : public InventoryItem
62 {
63 public:
64         MaterialItem(u8 content, u16 count)
65         {
66                 m_content = content;
67                 m_count = count;
68         }
69         /*
70                 Implementation interface
71         */
72         virtual const char* getName() const
73         {
74                 return "MaterialItem";
75         }
76         virtual void serialize(std::ostream &os)
77         {
78                 //os.imbue(std::locale("C"));
79                 os<<getName();
80                 os<<" ";
81                 os<<(unsigned int)m_content;
82                 os<<" ";
83                 os<<m_count;
84         }
85         virtual InventoryItem* clone()
86         {
87                 return new MaterialItem(m_content, m_count);
88         }
89 #ifndef SERVER
90         video::ITexture * getImage()
91         {
92                 /*if(m_content == CONTENT_TORCH)
93                         return g_texturecache.get("torch_on_floor");
94
95                 u16 tile = content_tile(m_content, v3s16(1,0,0));
96                 return g_tile_contents[tile].getTexture(0);*/
97                 
98                 if(m_content >= USEFUL_CONTENT_COUNT)
99                         return NULL;
100                         
101                 return g_irrlicht->getTexture(g_content_inventory_textures[m_content]);
102         }
103 #endif
104         std::string getText()
105         {
106                 std::ostringstream os;
107                 os<<m_count;
108                 return os.str();
109         }
110         /*
111                 Special methods
112         */
113         u8 getMaterial()
114         {
115                 return m_content;
116         }
117         u16 getCount()
118         {
119                 return m_count;
120         }
121         u16 freeSpace()
122         {
123                 if(m_count > MATERIAL_ITEM_MAX_COUNT)
124                         return 0;
125                 return MATERIAL_ITEM_MAX_COUNT - m_count;
126         }
127         void add(u16 count)
128         {
129                 assert(m_count + count <= MATERIAL_ITEM_MAX_COUNT);
130                 m_count += count;
131         }
132         void remove(u16 count)
133         {
134                 assert(m_count >= count);
135                 m_count -= count;
136         }
137 private:
138         u8 m_content;
139         u16 m_count;
140 };
141
142 class MapBlockObjectItem : public InventoryItem
143 {
144 public:
145         /*MapBlockObjectItem(MapBlockObject *obj)
146         {
147                 m_inventorystring = obj->getInventoryString();
148         }*/
149         MapBlockObjectItem(std::string inventorystring)
150         {
151                 m_inventorystring = inventorystring;
152         }
153         
154         /*
155                 Implementation interface
156         */
157         virtual const char* getName() const
158         {
159                 return "MBOItem";
160         }
161         virtual void serialize(std::ostream &os)
162         {
163                 for(;;)
164                 {
165                         size_t t = m_inventorystring.find('|');
166                         if(t == std::string::npos)
167                                 break;
168                         m_inventorystring[t] = '?';
169                 }
170                 os<<getName();
171                 os<<" ";
172                 os<<m_inventorystring;
173                 os<<"|";
174         }
175         virtual InventoryItem* clone()
176         {
177                 return new MapBlockObjectItem(m_inventorystring);
178         }
179
180 #ifndef SERVER
181         video::ITexture * getImage();
182 #endif
183         std::string getText();
184
185         /*
186                 Special methods
187         */
188         std::string getInventoryString()
189         {
190                 return m_inventorystring;
191         }
192
193         MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
194
195 private:
196         std::string m_inventorystring;
197 };
198
199 class ToolItem : public InventoryItem
200 {
201 public:
202         ToolItem(std::string toolname, u16 wear)
203         {
204                 m_toolname = toolname;
205                 m_wear = wear;
206         }
207         /*
208                 Implementation interface
209         */
210         virtual const char* getName() const
211         {
212                 return "ToolItem";
213         }
214         virtual void serialize(std::ostream &os)
215         {
216                 os<<getName();
217                 os<<" ";
218                 os<<m_toolname;
219                 os<<" ";
220                 os<<m_wear;
221         }
222         virtual InventoryItem* clone()
223         {
224                 return new ToolItem(m_toolname, m_wear);
225         }
226 #ifndef SERVER
227         video::ITexture * getImage()
228         {
229                 if(m_toolname == "WPick")
230                         return g_irrlicht->getTexture("../data/tool_wpick.png");
231                 if(m_toolname == "STPick")
232                         return g_irrlicht->getTexture("../data/tool_stpick.png");
233                 // Default to cloud texture
234                 return g_irrlicht->getTexture(tile_texture_path_get(TILE_CLOUD));
235         }
236 #endif
237         std::string getText()
238         {
239                 std::ostringstream os;
240                 u16 f = 4;
241                 u16 d = 65535/f;
242                 u16 i;
243                 for(i=0; i<(65535-m_wear)/d; i++)
244                         os<<'X';
245                 for(; i<f; i++)
246                         os<<'-';
247                 return os.str();
248                 
249                 /*std::ostringstream os;
250                 os<<m_toolname;
251                 os<<" ";
252                 os<<(m_wear/655);
253                 return os.str();*/
254         }
255         /*
256                 Special methods
257         */
258         std::string getToolName()
259         {
260                 return m_toolname;
261         }
262         u16 getWear()
263         {
264                 return m_wear;
265         }
266 private:
267         std::string m_toolname;
268         u16 m_wear;
269 };
270
271 class InventoryList
272 {
273 public:
274         InventoryList(std::string name, u32 size);
275         ~InventoryList();
276         void clearItems();
277         void serialize(std::ostream &os);
278         void deSerialize(std::istream &is);
279
280         InventoryList(const InventoryList &other);
281         InventoryList & operator = (const InventoryList &other);
282
283         std::string getName();
284         u32 getSize();
285         // Count used slots
286         u32 getUsedSlots();
287         
288         // Get pointer to item
289         InventoryItem * getItem(u32 i);
290         // Returns old item (or NULL). Parameter can be NULL.
291         InventoryItem * changeItem(u32 i, InventoryItem *newitem);
292         // Delete item
293         void deleteItem(u32 i);
294         // Adds an item to a suitable place. Returns false if failed.
295         bool addItem(InventoryItem *newitem);
296         // If possible, adds item to given slot. Returns true on success.
297         // Fails when slot is populated by a different kind of item.
298         bool addItem(u32 i, InventoryItem *newitem);
299
300         // Decrements amount of every material item
301         void decrementMaterials(u16 count);
302
303         void print(std::ostream &o);
304         
305 private:
306         core::array<InventoryItem*> m_items;
307         u32 m_size;
308         std::string m_name;
309 };
310
311 class Inventory
312 {
313 public:
314         ~Inventory();
315
316         void clear();
317
318         Inventory();
319         Inventory(const Inventory &other);
320         Inventory & operator = (const Inventory &other);
321         
322         void serialize(std::ostream &os);
323         void deSerialize(std::istream &is);
324
325         InventoryList * addList(const std::string &name, u32 size);
326         InventoryList * getList(const std::string &name);
327         bool deleteList(const std::string &name);
328         // A shorthand for adding items
329         bool addItem(const std::string &listname, InventoryItem *newitem)
330         {
331                 InventoryList *list = getList(listname);
332                 if(list == NULL)
333                         return false;
334                 return list->addItem(newitem);
335         }
336         
337 private:
338         // -1 if not found
339         s32 getListIndex(const std::string &name);
340
341         core::array<InventoryList*> m_lists;
342 };
343
344 #define IACTION_MOVE 0
345
346 struct InventoryAction
347 {
348         static InventoryAction * deSerialize(std::istream &is);
349         
350         virtual u16 getType() const = 0;
351         virtual void serialize(std::ostream &os) = 0;
352         virtual void apply(Inventory *inventory) = 0;
353 };
354
355 struct IMoveAction : public InventoryAction
356 {
357         u16 count;
358         std::string from_name;
359         s16 from_i;
360         std::string to_name;
361         s16 to_i;
362         
363         IMoveAction()
364         {
365                 count = 0;
366                 from_i = -1;
367                 to_i = -1;
368         }
369         IMoveAction(std::istream &is)
370         {
371                 std::string ts;
372
373                 std::getline(is, ts, ' ');
374                 count = stoi(ts);
375
376                 std::getline(is, from_name, ' ');
377
378                 std::getline(is, ts, ' ');
379                 from_i = stoi(ts);
380
381                 std::getline(is, to_name, ' ');
382
383                 std::getline(is, ts, ' ');
384                 to_i = stoi(ts);
385         }
386
387         u16 getType() const
388         {
389                 return IACTION_MOVE;
390         }
391
392         void serialize(std::ostream &os)
393         {
394                 os<<"Move ";
395                 os<<count<<" ";
396                 os<<from_name<<" ";
397                 os<<from_i<<" ";
398                 os<<to_name<<" ";
399                 os<<to_i;
400         }
401
402         void apply(Inventory *inventory);
403 };
404
405 #endif
406