]> git.lizzy.rs Git - minetest.git/blob - src/inventory.h
minecraft-like crafting
[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 #define QUANTITY_ITEM_MAX_COUNT 99
37
38 class InventoryItem
39 {
40 public:
41         InventoryItem(u16 count);
42         virtual ~InventoryItem();
43         
44         static InventoryItem* deSerialize(std::istream &is);
45         
46         virtual const char* getName() const = 0;
47         // Shall write the name and the parameters
48         virtual void serialize(std::ostream &os) = 0;
49         // Shall make an exact clone of the item
50         virtual InventoryItem* clone() = 0;
51 #ifndef SERVER
52         // Shall return an image to show in the GUI (or NULL)
53         virtual video::ITexture * getImage() { return NULL; }
54 #endif
55         // Shall return a text to show in the GUI
56         virtual std::string getText() { return ""; }
57
58         // Shall return true if the item can be add()ed to the other
59         virtual bool addableTo(InventoryItem *other)
60         {
61                 return false;
62         }
63         
64         /*
65                 Quantity methods
66         */
67         u16 getCount()
68         {
69                 return m_count;
70         }
71         void setCount(u16 count)
72         {
73                 m_count = count;
74         }
75         virtual u16 freeSpace()
76         {
77                 return 0;
78         }
79         void add(u16 count)
80         {
81                 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
82                 m_count += count;
83         }
84         void remove(u16 count)
85         {
86                 assert(m_count >= count);
87                 m_count -= count;
88         }
89
90 protected:
91         u16 m_count;
92 };
93
94 class MaterialItem : public InventoryItem
95 {
96 public:
97         MaterialItem(u8 content, u16 count):
98                 InventoryItem(count)
99         {
100                 m_content = content;
101         }
102         /*
103                 Implementation interface
104         */
105         virtual const char* getName() const
106         {
107                 return "MaterialItem";
108         }
109         virtual void serialize(std::ostream &os)
110         {
111                 //os.imbue(std::locale("C"));
112                 os<<getName();
113                 os<<" ";
114                 os<<(unsigned int)m_content;
115                 os<<" ";
116                 os<<m_count;
117         }
118         virtual InventoryItem* clone()
119         {
120                 return new MaterialItem(m_content, m_count);
121         }
122 #ifndef SERVER
123         video::ITexture * getImage()
124         {
125                 /*if(m_content == CONTENT_TORCH)
126                         return g_texturecache.get("torch_on_floor");
127
128                 u16 tile = content_tile(m_content, v3s16(1,0,0));
129                 return g_tile_contents[tile].getTexture(0);*/
130                 
131                 if(m_content >= USEFUL_CONTENT_COUNT)
132                         return NULL;
133                         
134                 return g_irrlicht->getTexture(g_content_inventory_textures[m_content]);
135         }
136 #endif
137         std::string getText()
138         {
139                 std::ostringstream os;
140                 os<<m_count;
141                 return os.str();
142         }
143
144         virtual bool addableTo(InventoryItem *other)
145         {
146                 if(std::string(other->getName()) != "MaterialItem")
147                         return false;
148                 MaterialItem *m = (MaterialItem*)other;
149                 if(m->getMaterial() != m_content)
150                         return false;
151                 return true;
152         }
153         u16 freeSpace()
154         {
155                 if(m_count > QUANTITY_ITEM_MAX_COUNT)
156                         return 0;
157                 return QUANTITY_ITEM_MAX_COUNT - m_count;
158         }
159         /*
160                 Special methods
161         */
162         u8 getMaterial()
163         {
164                 return m_content;
165         }
166 private:
167         u8 m_content;
168 };
169
170 class MapBlockObjectItem : public InventoryItem
171 {
172 public:
173         MapBlockObjectItem(std::string inventorystring):
174                 InventoryItem(1)
175         {
176                 m_inventorystring = inventorystring;
177         }
178         
179         /*
180                 Implementation interface
181         */
182         virtual const char* getName() const
183         {
184                 return "MBOItem";
185         }
186         virtual void serialize(std::ostream &os)
187         {
188                 for(;;)
189                 {
190                         size_t t = m_inventorystring.find('|');
191                         if(t == std::string::npos)
192                                 break;
193                         m_inventorystring[t] = '?';
194                 }
195                 os<<getName();
196                 os<<" ";
197                 os<<m_inventorystring;
198                 os<<"|";
199         }
200         virtual InventoryItem* clone()
201         {
202                 return new MapBlockObjectItem(m_inventorystring);
203         }
204
205 #ifndef SERVER
206         video::ITexture * getImage();
207 #endif
208         std::string getText();
209
210         /*
211                 Special methods
212         */
213         std::string getInventoryString()
214         {
215                 return m_inventorystring;
216         }
217
218         MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
219
220 private:
221         std::string m_inventorystring;
222 };
223
224 /*
225         An item that is used as a mid-product when crafting.
226         Subnames:
227         - Stick
228 */
229 class CraftItem : public InventoryItem
230 {
231 public:
232         CraftItem(std::string subname, u16 count):
233                 InventoryItem(count)
234         {
235                 m_subname = subname;
236         }
237         /*
238                 Implementation interface
239         */
240         virtual const char* getName() const
241         {
242                 return "CraftItem";
243         }
244         virtual void serialize(std::ostream &os)
245         {
246                 os<<getName();
247                 os<<" ";
248                 os<<m_subname;
249                 os<<" ";
250                 os<<m_count;
251         }
252         virtual InventoryItem* clone()
253         {
254                 return new CraftItem(m_subname, m_count);
255         }
256 #ifndef SERVER
257         video::ITexture * getImage()
258         {
259                 std::string basename;
260                 if(m_subname == "Stick")
261                         basename = "../data/stick.png";
262                 // Default to cloud texture
263                 else
264                         basename = tile_texture_path_get(TILE_CLOUD);
265                 
266                 // Get such a texture
267                 return g_irrlicht->getTexture(basename);
268                 //return g_irrlicht->getTexture(TextureSpec(finalname, basename, mod));
269         }
270 #endif
271         std::string getText()
272         {
273                 std::ostringstream os;
274                 os<<m_count;
275                 return os.str();
276         }
277         virtual bool addableTo(InventoryItem *other)
278         {
279                 if(std::string(other->getName()) != "CraftItem")
280                         return false;
281                 CraftItem *m = (CraftItem*)other;
282                 if(m->m_subname != m_subname)
283                         return false;
284                 return true;
285         }
286         u16 freeSpace()
287         {
288                 if(m_count > QUANTITY_ITEM_MAX_COUNT)
289                         return 0;
290                 return QUANTITY_ITEM_MAX_COUNT - m_count;
291         }
292         /*
293                 Special methods
294         */
295         std::string getSubName()
296         {
297                 return m_subname;
298         }
299 private:
300         std::string m_subname;
301 };
302
303 class ToolItem : public InventoryItem
304 {
305 public:
306         ToolItem(std::string toolname, u16 wear):
307                 InventoryItem(1)
308         {
309                 m_toolname = toolname;
310                 m_wear = wear;
311         }
312         /*
313                 Implementation interface
314         */
315         virtual const char* getName() const
316         {
317                 return "ToolItem";
318         }
319         virtual void serialize(std::ostream &os)
320         {
321                 os<<getName();
322                 os<<" ";
323                 os<<m_toolname;
324                 os<<" ";
325                 os<<m_wear;
326         }
327         virtual InventoryItem* clone()
328         {
329                 return new ToolItem(m_toolname, m_wear);
330         }
331 #ifndef SERVER
332         video::ITexture * getImage()
333         {
334                 std::string basename;
335                 if(m_toolname == "WPick")
336                         basename = "../data/tool_wpick.png";
337                 else if(m_toolname == "STPick")
338                         basename = "../data/tool_stpick.png";
339                 // Default to cloud texture
340                 else
341                         basename = tile_texture_path_get(TILE_CLOUD);
342                 
343                 /*
344                         Calculate some progress value with sane amount of
345                         maximum states
346                 */
347                 u32 maxprogress = 30;
348                 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
349                 
350                 // Make texture name for the new texture with a progress bar
351                 std::ostringstream os;
352                 os<<basename<<"-toolprogress-"<<toolprogress;
353                 std::string finalname = os.str();
354
355                 float value_f = (float)toolprogress / (float)maxprogress;
356                 
357                 // Get such a texture
358                 TextureMod *mod = new ProgressBarTextureMod(value_f);
359                 return g_irrlicht->getTexture(TextureSpec(finalname, basename, mod));
360         }
361 #endif
362         std::string getText()
363         {
364                 return "";
365                 
366                 /*std::ostringstream os;
367                 u16 f = 4;
368                 u16 d = 65535/f;
369                 u16 i;
370                 for(i=0; i<(65535-m_wear)/d; i++)
371                         os<<'X';
372                 for(; i<f; i++)
373                         os<<'-';
374                 return os.str();*/
375                 
376                 /*std::ostringstream os;
377                 os<<m_toolname;
378                 os<<" ";
379                 os<<(m_wear/655);
380                 return os.str();*/
381         }
382         /*
383                 Special methods
384         */
385         std::string getToolName()
386         {
387                 return m_toolname;
388         }
389         u16 getWear()
390         {
391                 return m_wear;
392         }
393         // Returns true if weared out
394         bool addWear(u16 add)
395         {
396                 if(m_wear >= 65535 - add)
397                 {
398                         m_wear = 65535;
399                         return true;
400                 }
401                 else
402                 {
403                         m_wear += add;
404                         return false;
405                 }
406         }
407 private:
408         std::string m_toolname;
409         u16 m_wear;
410 };
411
412 class InventoryList
413 {
414 public:
415         InventoryList(std::string name, u32 size);
416         ~InventoryList();
417         void clearItems();
418         void serialize(std::ostream &os);
419         void deSerialize(std::istream &is);
420
421         InventoryList(const InventoryList &other);
422         InventoryList & operator = (const InventoryList &other);
423
424         std::string getName();
425         u32 getSize();
426         // Count used slots
427         u32 getUsedSlots();
428         
429         // Get pointer to item
430         InventoryItem * getItem(u32 i);
431         // Returns old item (or NULL). Parameter can be NULL.
432         InventoryItem * changeItem(u32 i, InventoryItem *newitem);
433         // Delete item
434         void deleteItem(u32 i);
435         // Adds an item to a suitable place. Returns leftover item.
436         // If all went into the list, returns NULL.
437         InventoryItem * addItem(InventoryItem *newitem);
438
439         // If possible, adds item to given slot.
440         // If cannot be added at all, returns the item back.
441         // If can be added partly, decremented item is returned back.
442         // If can be added fully, NULL is returned.
443         InventoryItem * addItem(u32 i, InventoryItem *newitem);
444
445         // Takes some items from a slot.
446         // If there are not enough, takes as many as it can.
447         // Returns NULL if couldn't take any.
448         InventoryItem * takeItem(u32 i, u32 count);
449
450         // Decrements amount of every material item
451         void decrementMaterials(u16 count);
452
453         void print(std::ostream &o);
454         
455 private:
456         core::array<InventoryItem*> m_items;
457         u32 m_size;
458         std::string m_name;
459 };
460
461 class Inventory
462 {
463 public:
464         ~Inventory();
465
466         void clear();
467
468         Inventory();
469         Inventory(const Inventory &other);
470         Inventory & operator = (const Inventory &other);
471         
472         void serialize(std::ostream &os);
473         void deSerialize(std::istream &is);
474
475         InventoryList * addList(const std::string &name, u32 size);
476         InventoryList * getList(const std::string &name);
477         bool deleteList(const std::string &name);
478         // A shorthand for adding items.
479         // Returns NULL if the item was fully added, leftover otherwise.
480         InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
481         {
482                 InventoryList *list = getList(listname);
483                 if(list == NULL)
484                         return newitem;
485                 return list->addItem(newitem);
486         }
487         
488 private:
489         // -1 if not found
490         s32 getListIndex(const std::string &name);
491
492         core::array<InventoryList*> m_lists;
493 };
494
495 #define IACTION_MOVE 0
496
497 struct InventoryAction
498 {
499         static InventoryAction * deSerialize(std::istream &is);
500         
501         virtual u16 getType() const = 0;
502         virtual void serialize(std::ostream &os) = 0;
503         virtual void apply(Inventory *inventory) = 0;
504 };
505
506 struct IMoveAction : public InventoryAction
507 {
508         // count=0 means "everything"
509         u16 count;
510         std::string from_name;
511         s16 from_i;
512         std::string to_name;
513         s16 to_i;
514         
515         IMoveAction()
516         {
517                 count = 0;
518                 from_i = -1;
519                 to_i = -1;
520         }
521         IMoveAction(std::istream &is)
522         {
523                 std::string ts;
524
525                 std::getline(is, ts, ' ');
526                 count = stoi(ts);
527
528                 std::getline(is, from_name, ' ');
529
530                 std::getline(is, ts, ' ');
531                 from_i = stoi(ts);
532
533                 std::getline(is, to_name, ' ');
534
535                 std::getline(is, ts, ' ');
536                 to_i = stoi(ts);
537         }
538
539         u16 getType() const
540         {
541                 return IACTION_MOVE;
542         }
543
544         void serialize(std::ostream &os)
545         {
546                 os<<"Move ";
547                 os<<count<<" ";
548                 os<<from_name<<" ";
549                 os<<from_i<<" ";
550                 os<<to_name<<" ";
551                 os<<to_i;
552         }
553
554         void apply(Inventory *inventory);
555 };
556
557 #endif
558