doc updates; CMake works reasonably well now.
[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 #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_texture_paths[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 = porting::getDataPath("stick.png").c_str();
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 = porting::getDataPath("tool_wpick.png").c_str();
337                 else if(m_toolname == "STPick")
338                         basename = porting::getDataPath("tool_stpick.png").c_str();
339                 else if(m_toolname == "MesePick")
340                         basename = porting::getDataPath("tool_mesepick.png").c_str();
341                 // Default to cloud texture
342                 else
343                         basename = tile_texture_path_get(TILE_CLOUD);
344                 
345                 /*
346                         Calculate some progress value with sane amount of
347                         maximum states
348                 */
349                 u32 maxprogress = 30;
350                 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
351                 
352                 // Make texture name for the new texture with a progress bar
353                 std::ostringstream os;
354                 os<<basename<<"-toolprogress-"<<toolprogress;
355                 std::string finalname = os.str();
356
357                 float value_f = (float)toolprogress / (float)maxprogress;
358                 
359                 // Get such a texture
360                 TextureMod *mod = new ProgressBarTextureMod(value_f);
361                 return g_irrlicht->getTexture(TextureSpec(finalname, basename, mod));
362         }
363 #endif
364         std::string getText()
365         {
366                 return "";
367                 
368                 /*std::ostringstream os;
369                 u16 f = 4;
370                 u16 d = 65535/f;
371                 u16 i;
372                 for(i=0; i<(65535-m_wear)/d; i++)
373                         os<<'X';
374                 for(; i<f; i++)
375                         os<<'-';
376                 return os.str();*/
377                 
378                 /*std::ostringstream os;
379                 os<<m_toolname;
380                 os<<" ";
381                 os<<(m_wear/655);
382                 return os.str();*/
383         }
384         /*
385                 Special methods
386         */
387         std::string getToolName()
388         {
389                 return m_toolname;
390         }
391         u16 getWear()
392         {
393                 return m_wear;
394         }
395         // Returns true if weared out
396         bool addWear(u16 add)
397         {
398                 if(m_wear >= 65535 - add)
399                 {
400                         m_wear = 65535;
401                         return true;
402                 }
403                 else
404                 {
405                         m_wear += add;
406                         return false;
407                 }
408         }
409 private:
410         std::string m_toolname;
411         u16 m_wear;
412 };
413
414 class InventoryList
415 {
416 public:
417         InventoryList(std::string name, u32 size);
418         ~InventoryList();
419         void clearItems();
420         void serialize(std::ostream &os);
421         void deSerialize(std::istream &is);
422
423         InventoryList(const InventoryList &other);
424         InventoryList & operator = (const InventoryList &other);
425
426         std::string getName();
427         u32 getSize();
428         // Count used slots
429         u32 getUsedSlots();
430         
431         // Get pointer to item
432         InventoryItem * getItem(u32 i);
433         // Returns old item (or NULL). Parameter can be NULL.
434         InventoryItem * changeItem(u32 i, InventoryItem *newitem);
435         // Delete item
436         void deleteItem(u32 i);
437         // Adds an item to a suitable place. Returns leftover item.
438         // If all went into the list, returns NULL.
439         InventoryItem * addItem(InventoryItem *newitem);
440
441         // If possible, adds item to given slot.
442         // If cannot be added at all, returns the item back.
443         // If can be added partly, decremented item is returned back.
444         // If can be added fully, NULL is returned.
445         InventoryItem * addItem(u32 i, InventoryItem *newitem);
446
447         // Takes some items from a slot.
448         // If there are not enough, takes as many as it can.
449         // Returns NULL if couldn't take any.
450         InventoryItem * takeItem(u32 i, u32 count);
451
452         // Decrements amount of every material item
453         void decrementMaterials(u16 count);
454
455         void print(std::ostream &o);
456         
457 private:
458         core::array<InventoryItem*> m_items;
459         u32 m_size;
460         std::string m_name;
461 };
462
463 class Inventory
464 {
465 public:
466         ~Inventory();
467
468         void clear();
469
470         Inventory();
471         Inventory(const Inventory &other);
472         Inventory & operator = (const Inventory &other);
473         
474         void serialize(std::ostream &os);
475         void deSerialize(std::istream &is);
476
477         InventoryList * addList(const std::string &name, u32 size);
478         InventoryList * getList(const std::string &name);
479         bool deleteList(const std::string &name);
480         // A shorthand for adding items.
481         // Returns NULL if the item was fully added, leftover otherwise.
482         InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
483         {
484                 InventoryList *list = getList(listname);
485                 if(list == NULL)
486                         return newitem;
487                 return list->addItem(newitem);
488         }
489         
490 private:
491         // -1 if not found
492         s32 getListIndex(const std::string &name);
493
494         core::array<InventoryList*> m_lists;
495 };
496
497 #define IACTION_MOVE 0
498
499 struct InventoryAction
500 {
501         static InventoryAction * deSerialize(std::istream &is);
502         
503         virtual u16 getType() const = 0;
504         virtual void serialize(std::ostream &os) = 0;
505         virtual void apply(Inventory *inventory) = 0;
506 };
507
508 struct IMoveAction : public InventoryAction
509 {
510         // count=0 means "everything"
511         u16 count;
512         std::string from_name;
513         s16 from_i;
514         std::string to_name;
515         s16 to_i;
516         
517         IMoveAction()
518         {
519                 count = 0;
520                 from_i = -1;
521                 to_i = -1;
522         }
523         IMoveAction(std::istream &is)
524         {
525                 std::string ts;
526
527                 std::getline(is, ts, ' ');
528                 count = stoi(ts);
529
530                 std::getline(is, from_name, ' ');
531
532                 std::getline(is, ts, ' ');
533                 from_i = stoi(ts);
534
535                 std::getline(is, to_name, ' ');
536
537                 std::getline(is, ts, ' ');
538                 to_i = stoi(ts);
539         }
540
541         u16 getType() const
542         {
543                 return IACTION_MOVE;
544         }
545
546         void serialize(std::ostream &os)
547         {
548                 os<<"Move ";
549                 os<<count<<" ";
550                 os<<from_name<<" ";
551                 os<<from_i<<" ";
552                 os<<to_name<<" ";
553                 os<<to_i;
554         }
555
556         void apply(Inventory *inventory);
557 };
558
559 #endif
560