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