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()
126 //return g_irrlicht->getTexture(content_features(m_content).inventory_texture);
130 std::string getText()
132 std::ostringstream os;
137 virtual bool addableTo(InventoryItem *other)
139 if(std::string(other->getName()) != "MaterialItem")
141 MaterialItem *m = (MaterialItem*)other;
142 if(m->getMaterial() != m_content)
148 if(m_count > QUANTITY_ITEM_MAX_COUNT)
150 return QUANTITY_ITEM_MAX_COUNT - m_count;
163 class MapBlockObjectItem : public InventoryItem
166 MapBlockObjectItem(std::string inventorystring):
169 m_inventorystring = inventorystring;
173 Implementation interface
175 virtual const char* getName() const
179 virtual void serialize(std::ostream &os)
183 size_t t = m_inventorystring.find('|');
184 if(t == std::string::npos)
186 m_inventorystring[t] = '?';
190 os<<m_inventorystring;
193 virtual InventoryItem* clone()
195 return new MapBlockObjectItem(m_inventorystring);
199 video::ITexture * getImage();
201 std::string getText();
206 std::string getInventoryString()
208 return m_inventorystring;
211 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
214 std::string m_inventorystring;
218 An item that is used as a mid-product when crafting.
222 class CraftItem : public InventoryItem
225 CraftItem(std::string subname, u16 count):
231 Implementation interface
233 virtual const char* getName() const
237 virtual void serialize(std::ostream &os)
245 virtual InventoryItem* clone()
247 return new CraftItem(m_subname, m_count);
250 video::ITexture * getImage()
254 if(m_subname == "Stick")
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";
263 // Get such a texture
264 //return g_irrlicht->getTexture(name);
269 std::string getText()
271 std::ostringstream os;
275 virtual bool addableTo(InventoryItem *other)
277 if(std::string(other->getName()) != "CraftItem")
279 CraftItem *m = (CraftItem*)other;
280 if(m->m_subname != m_subname)
286 if(m_count > QUANTITY_ITEM_MAX_COUNT)
288 return QUANTITY_ITEM_MAX_COUNT - m_count;
293 std::string getSubName()
298 std::string m_subname;
301 class ToolItem : public InventoryItem
304 ToolItem(std::string toolname, u16 wear):
307 m_toolname = toolname;
311 Implementation interface
313 virtual const char* getName() const
317 virtual void serialize(std::ostream &os)
325 virtual InventoryItem* clone()
327 return new ToolItem(m_toolname, m_wear);
330 video::ITexture * getImage()
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";
340 basename = "cloud.png";
343 Calculate a progress value with sane amount of
346 u32 maxprogress = 30;
347 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
349 float value_f = (float)toolprogress / (float)maxprogress;
350 std::ostringstream os;
351 os<<"[progressbar"<<value_f;
354 spec.addTid(g_irrlicht->getTextureId(basename));
355 spec.addTid(g_irrlicht->getTextureId(os.str()));
356 return g_irrlicht->getTexture(spec);*/
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());*/
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();
371 float value_f = (float)toolprogress / (float)maxprogress;
373 // Get such a texture
374 TextureMod *mod = new ProgressBarTextureMod(value_f);
375 return g_irrlicht->getTexture(TextureSpec(finalname, basename, mod));*/
378 std::string getText()
382 /*std::ostringstream os;
386 for(i=0; i<(65535-m_wear)/d; i++)
392 /*std::ostringstream os;
401 std::string getToolName()
409 // Returns true if weared out
410 bool addWear(u16 add)
412 if(m_wear >= 65535 - add)
424 std::string m_toolname;
431 InventoryList(std::string name, u32 size);
434 void serialize(std::ostream &os);
435 void deSerialize(std::istream &is);
437 InventoryList(const InventoryList &other);
438 InventoryList & operator = (const InventoryList &other);
440 std::string getName();
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);
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);
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);
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);
466 // Decrements amount of every material item
467 void decrementMaterials(u16 count);
469 void print(std::ostream &o);
472 core::array<InventoryItem*> m_items;
485 Inventory(const Inventory &other);
486 Inventory & operator = (const Inventory &other);
488 void serialize(std::ostream &os);
489 void deSerialize(std::istream &is);
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)
498 InventoryList *list = getList(listname);
501 return list->addItem(newitem);
506 s32 getListIndex(const std::string &name);
508 core::array<InventoryList*> m_lists;
511 #define IACTION_MOVE 0
513 struct InventoryAction
515 static InventoryAction * deSerialize(std::istream &is);
517 virtual u16 getType() const = 0;
518 virtual void serialize(std::ostream &os) = 0;
519 virtual void apply(Inventory *inventory) = 0;
522 struct IMoveAction : public InventoryAction
524 // count=0 means "everything"
526 std::string from_name;
537 IMoveAction(std::istream &is)
541 std::getline(is, ts, ' ');
544 std::getline(is, from_name, ' ');
546 std::getline(is, ts, ' ');
549 std::getline(is, to_name, ' ');
551 std::getline(is, ts, ' ');
560 void serialize(std::ostream &os)
570 void apply(Inventory *inventory);