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 == CONTENT_TORCH)
126 return g_texturecache.get("torch_on_floor");
128 u16 tile = content_tile(m_content, v3s16(1,0,0));
129 return g_tile_contents[tile].getTexture(0);*/
131 if(m_content >= USEFUL_CONTENT_COUNT)
134 return g_irrlicht->getTexture(g_content_inventory_texture_paths[m_content]);
137 std::string getText()
139 std::ostringstream os;
144 virtual bool addableTo(InventoryItem *other)
146 if(std::string(other->getName()) != "MaterialItem")
148 MaterialItem *m = (MaterialItem*)other;
149 if(m->getMaterial() != m_content)
155 if(m_count > QUANTITY_ITEM_MAX_COUNT)
157 return QUANTITY_ITEM_MAX_COUNT - m_count;
170 class MapBlockObjectItem : public InventoryItem
173 MapBlockObjectItem(std::string inventorystring):
176 m_inventorystring = inventorystring;
180 Implementation interface
182 virtual const char* getName() const
186 virtual void serialize(std::ostream &os)
190 size_t t = m_inventorystring.find('|');
191 if(t == std::string::npos)
193 m_inventorystring[t] = '?';
197 os<<m_inventorystring;
200 virtual InventoryItem* clone()
202 return new MapBlockObjectItem(m_inventorystring);
206 video::ITexture * getImage();
208 std::string getText();
213 std::string getInventoryString()
215 return m_inventorystring;
218 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
221 std::string m_inventorystring;
225 An item that is used as a mid-product when crafting.
229 class CraftItem : public InventoryItem
232 CraftItem(std::string subname, u16 count):
238 Implementation interface
240 virtual const char* getName() const
244 virtual void serialize(std::ostream &os)
252 virtual InventoryItem* clone()
254 return new CraftItem(m_subname, m_count);
257 video::ITexture * getImage()
259 std::string basename;
260 if(m_subname == "Stick")
261 basename = porting::getDataPath("stick.png").c_str();
262 // Default to cloud texture
264 basename = tile_texture_path_get(TILE_CLOUD);
266 // Get such a texture
267 return g_irrlicht->getTexture(basename);
268 //return g_irrlicht->getTexture(TextureSpec(finalname, basename, mod));
271 std::string getText()
273 std::ostringstream os;
277 virtual bool addableTo(InventoryItem *other)
279 if(std::string(other->getName()) != "CraftItem")
281 CraftItem *m = (CraftItem*)other;
282 if(m->m_subname != m_subname)
288 if(m_count > QUANTITY_ITEM_MAX_COUNT)
290 return QUANTITY_ITEM_MAX_COUNT - m_count;
295 std::string getSubName()
300 std::string m_subname;
303 class ToolItem : public InventoryItem
306 ToolItem(std::string toolname, u16 wear):
309 m_toolname = toolname;
313 Implementation interface
315 virtual const char* getName() const
319 virtual void serialize(std::ostream &os)
327 virtual InventoryItem* clone()
329 return new ToolItem(m_toolname, m_wear);
332 video::ITexture * getImage()
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
343 basename = tile_texture_path_get(TILE_CLOUD);
346 Calculate some progress value with sane amount of
349 u32 maxprogress = 30;
350 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
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();
357 float value_f = (float)toolprogress / (float)maxprogress;
359 // Get such a texture
360 TextureMod *mod = new ProgressBarTextureMod(value_f);
361 return g_irrlicht->getTexture(TextureSpec(finalname, basename, mod));
364 std::string getText()
368 /*std::ostringstream os;
372 for(i=0; i<(65535-m_wear)/d; i++)
378 /*std::ostringstream os;
387 std::string getToolName()
395 // Returns true if weared out
396 bool addWear(u16 add)
398 if(m_wear >= 65535 - add)
410 std::string m_toolname;
417 InventoryList(std::string name, u32 size);
420 void serialize(std::ostream &os);
421 void deSerialize(std::istream &is);
423 InventoryList(const InventoryList &other);
424 InventoryList & operator = (const InventoryList &other);
426 std::string getName();
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);
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);
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);
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);
452 // Decrements amount of every material item
453 void decrementMaterials(u16 count);
455 void print(std::ostream &o);
458 core::array<InventoryItem*> m_items;
471 Inventory(const Inventory &other);
472 Inventory & operator = (const Inventory &other);
474 void serialize(std::ostream &os);
475 void deSerialize(std::istream &is);
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)
484 InventoryList *list = getList(listname);
487 return list->addItem(newitem);
492 s32 getListIndex(const std::string &name);
494 core::array<InventoryList*> m_lists;
497 #define IACTION_MOVE 0
499 struct InventoryAction
501 static InventoryAction * deSerialize(std::istream &is);
503 virtual u16 getType() const = 0;
504 virtual void serialize(std::ostream &os) = 0;
505 virtual void apply(Inventory *inventory) = 0;
508 struct IMoveAction : public InventoryAction
510 // count=0 means "everything"
512 std::string from_name;
523 IMoveAction(std::istream &is)
527 std::getline(is, ts, ' ');
530 std::getline(is, from_name, ' ');
532 std::getline(is, ts, ' ');
535 std::getline(is, to_name, ' ');
537 std::getline(is, ts, ' ');
546 void serialize(std::ostream &os)
556 void apply(Inventory *inventory);