3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
24 #ifndef INVENTORY_HEADER
25 #define INVENTORY_HEADER
30 #include "common_irrlicht.h"
32 #include "mapblockobject.h"
36 #define QUANTITY_ITEM_MAX_COUNT 99
41 InventoryItem(u16 count);
42 virtual ~InventoryItem();
44 static InventoryItem* deSerialize(std::istream &is);
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;
52 // Shall return an image to show in the GUI (or NULL)
53 virtual video::ITexture * getImage() { return NULL; }
55 // Shall return a text to show in the GUI
56 virtual std::string getText() { return ""; }
58 // Shall return true if the item can be add()ed to the other
59 virtual bool addableTo(InventoryItem *other)
71 void setCount(u16 count)
75 virtual u16 freeSpace()
81 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
84 void remove(u16 count)
86 assert(m_count >= count);
94 class MaterialItem : public InventoryItem
97 MaterialItem(u8 content, u16 count):
103 Implementation interface
105 virtual const char* getName() const
107 return "MaterialItem";
109 virtual void serialize(std::ostream &os)
111 //os.imbue(std::locale("C"));
114 os<<(unsigned int)m_content;
118 virtual InventoryItem* clone()
120 return new MaterialItem(m_content, m_count);
123 video::ITexture * getImage()
125 if(m_content >= USEFUL_CONTENT_COUNT)
128 return g_irrlicht->getTexture(g_content_inventory_texture_paths[m_content]);
131 std::string getText()
133 std::ostringstream os;
138 virtual bool addableTo(InventoryItem *other)
140 if(std::string(other->getName()) != "MaterialItem")
142 MaterialItem *m = (MaterialItem*)other;
143 if(m->getMaterial() != m_content)
149 if(m_count > QUANTITY_ITEM_MAX_COUNT)
151 return QUANTITY_ITEM_MAX_COUNT - m_count;
164 class MapBlockObjectItem : public InventoryItem
167 MapBlockObjectItem(std::string inventorystring):
170 m_inventorystring = inventorystring;
174 Implementation interface
176 virtual const char* getName() const
180 virtual void serialize(std::ostream &os)
184 size_t t = m_inventorystring.find('|');
185 if(t == std::string::npos)
187 m_inventorystring[t] = '?';
191 os<<m_inventorystring;
194 virtual InventoryItem* clone()
196 return new MapBlockObjectItem(m_inventorystring);
200 video::ITexture * getImage();
202 std::string getText();
207 std::string getInventoryString()
209 return m_inventorystring;
212 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
215 std::string m_inventorystring;
219 An item that is used as a mid-product when crafting.
223 class CraftItem : public InventoryItem
226 CraftItem(std::string subname, u16 count):
232 Implementation interface
234 virtual const char* getName() const
238 virtual void serialize(std::ostream &os)
246 virtual InventoryItem* clone()
248 return new CraftItem(m_subname, m_count);
251 video::ITexture * getImage()
253 std::string basename;
255 if(m_subname == "Stick")
256 basename = porting::getDataPath("stick.png");
257 else if(m_subname == "lump_of_coal")
258 basename = porting::getDataPath("lump_of_coal.png");
259 else if(m_subname == "lump_of_iron")
260 basename = porting::getDataPath("lump_of_iron.png");
262 basename = porting::getDataPath("cloud.png[[mod:crack3");
264 // Get such a texture
265 return g_irrlicht->getTexture(basename);
268 std::string getText()
270 std::ostringstream os;
274 virtual bool addableTo(InventoryItem *other)
276 if(std::string(other->getName()) != "CraftItem")
278 CraftItem *m = (CraftItem*)other;
279 if(m->m_subname != m_subname)
285 if(m_count > QUANTITY_ITEM_MAX_COUNT)
287 return QUANTITY_ITEM_MAX_COUNT - m_count;
292 std::string getSubName()
297 std::string m_subname;
300 class ToolItem : public InventoryItem
303 ToolItem(std::string toolname, u16 wear):
306 m_toolname = toolname;
310 Implementation interface
312 virtual const char* getName() const
316 virtual void serialize(std::ostream &os)
324 virtual InventoryItem* clone()
326 return new ToolItem(m_toolname, m_wear);
329 video::ITexture * getImage()
331 std::string basename;
332 if(m_toolname == "WPick")
333 basename = porting::getDataPath("tool_wpick.png").c_str();
334 else if(m_toolname == "STPick")
335 basename = porting::getDataPath("tool_stpick.png").c_str();
336 else if(m_toolname == "MesePick")
337 basename = porting::getDataPath("tool_mesepick.png").c_str();
338 // Default to cloud texture
340 basename = porting::getDataPath("cloud.png").c_str();
341 //basename = tile_texture_path_get(TILE_CLOUD);
344 Calculate some progress value with sane amount of
347 u32 maxprogress = 30;
348 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
350 // Make texture name for the new texture with a progress bar
351 float value_f = (float)toolprogress / (float)maxprogress;
352 std::ostringstream os;
353 os<<basename<<"[[mod:progressbar"<<value_f;
354 return g_irrlicht->getTexture(os.str());
356 /*// Make texture name for the new texture with a progress bar
357 std::ostringstream os;
358 os<<basename<<"-toolprogress-"<<toolprogress;
359 std::string finalname = os.str();
361 float value_f = (float)toolprogress / (float)maxprogress;
363 // Get such a texture
364 TextureMod *mod = new ProgressBarTextureMod(value_f);
365 return g_irrlicht->getTexture(TextureSpec(finalname, basename, mod));*/
368 std::string getText()
372 /*std::ostringstream os;
376 for(i=0; i<(65535-m_wear)/d; i++)
382 /*std::ostringstream os;
391 std::string getToolName()
399 // Returns true if weared out
400 bool addWear(u16 add)
402 if(m_wear >= 65535 - add)
414 std::string m_toolname;
421 InventoryList(std::string name, u32 size);
424 void serialize(std::ostream &os);
425 void deSerialize(std::istream &is);
427 InventoryList(const InventoryList &other);
428 InventoryList & operator = (const InventoryList &other);
430 std::string getName();
435 // Get pointer to item
436 InventoryItem * getItem(u32 i);
437 // Returns old item (or NULL). Parameter can be NULL.
438 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
440 void deleteItem(u32 i);
441 // Adds an item to a suitable place. Returns leftover item.
442 // If all went into the list, returns NULL.
443 InventoryItem * addItem(InventoryItem *newitem);
445 // If possible, adds item to given slot.
446 // If cannot be added at all, returns the item back.
447 // If can be added partly, decremented item is returned back.
448 // If can be added fully, NULL is returned.
449 InventoryItem * addItem(u32 i, InventoryItem *newitem);
451 // Takes some items from a slot.
452 // If there are not enough, takes as many as it can.
453 // Returns NULL if couldn't take any.
454 InventoryItem * takeItem(u32 i, u32 count);
456 // Decrements amount of every material item
457 void decrementMaterials(u16 count);
459 void print(std::ostream &o);
462 core::array<InventoryItem*> m_items;
475 Inventory(const Inventory &other);
476 Inventory & operator = (const Inventory &other);
478 void serialize(std::ostream &os);
479 void deSerialize(std::istream &is);
481 InventoryList * addList(const std::string &name, u32 size);
482 InventoryList * getList(const std::string &name);
483 bool deleteList(const std::string &name);
484 // A shorthand for adding items.
485 // Returns NULL if the item was fully added, leftover otherwise.
486 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
488 InventoryList *list = getList(listname);
491 return list->addItem(newitem);
496 s32 getListIndex(const std::string &name);
498 core::array<InventoryList*> m_lists;
501 #define IACTION_MOVE 0
503 struct InventoryAction
505 static InventoryAction * deSerialize(std::istream &is);
507 virtual u16 getType() const = 0;
508 virtual void serialize(std::ostream &os) = 0;
509 virtual void apply(Inventory *inventory) = 0;
512 struct IMoveAction : public InventoryAction
514 // count=0 means "everything"
516 std::string from_name;
527 IMoveAction(std::istream &is)
531 std::getline(is, ts, ' ');
534 std::getline(is, from_name, ' ');
536 std::getline(is, ts, ' ');
539 std::getline(is, to_name, ' ');
541 std::getline(is, ts, ' ');
550 void serialize(std::ostream &os)
560 void apply(Inventory *inventory);