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 return content_features(m_content).inventory_texture;
129 std::string getText()
131 std::ostringstream os;
136 virtual bool addableTo(InventoryItem *other)
138 if(std::string(other->getName()) != "MaterialItem")
140 MaterialItem *m = (MaterialItem*)other;
141 if(m->getMaterial() != m_content)
147 if(m_count > QUANTITY_ITEM_MAX_COUNT)
149 return QUANTITY_ITEM_MAX_COUNT - m_count;
162 class MapBlockObjectItem : public InventoryItem
165 MapBlockObjectItem(std::string inventorystring):
168 m_inventorystring = inventorystring;
172 Implementation interface
174 virtual const char* getName() const
178 virtual void serialize(std::ostream &os)
182 size_t t = m_inventorystring.find('|');
183 if(t == std::string::npos)
185 m_inventorystring[t] = '?';
189 os<<m_inventorystring;
192 virtual InventoryItem* clone()
194 return new MapBlockObjectItem(m_inventorystring);
198 video::ITexture * getImage();
200 std::string getText();
205 std::string getInventoryString()
207 return m_inventorystring;
210 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
213 std::string m_inventorystring;
217 An item that is used as a mid-product when crafting.
221 class CraftItem : public InventoryItem
224 CraftItem(std::string subname, u16 count):
230 Implementation interface
232 virtual const char* getName() const
236 virtual void serialize(std::ostream &os)
244 virtual InventoryItem* clone()
246 return new CraftItem(m_subname, m_count);
249 video::ITexture * getImage()
251 if(g_texturesource == NULL)
256 if(m_subname == "Stick")
258 else if(m_subname == "lump_of_coal")
259 name = "lump_of_coal.png";
260 else if(m_subname == "lump_of_iron")
261 name = "lump_of_iron.png";
265 // Get such a texture
266 //return g_irrlicht->getTexture(name);
267 return g_texturesource->getTextureRaw(name);
270 std::string getText()
272 std::ostringstream os;
276 virtual bool addableTo(InventoryItem *other)
278 if(std::string(other->getName()) != "CraftItem")
280 CraftItem *m = (CraftItem*)other;
281 if(m->m_subname != m_subname)
287 if(m_count > QUANTITY_ITEM_MAX_COUNT)
289 return QUANTITY_ITEM_MAX_COUNT - m_count;
294 std::string getSubName()
299 std::string m_subname;
302 class ToolItem : public InventoryItem
305 ToolItem(std::string toolname, u16 wear):
308 m_toolname = toolname;
312 Implementation interface
314 virtual const char* getName() const
318 virtual void serialize(std::ostream &os)
326 virtual InventoryItem* clone()
328 return new ToolItem(m_toolname, m_wear);
331 video::ITexture * getImage()
333 if(g_texturesource == NULL)
336 std::string basename;
337 if(m_toolname == "WPick")
338 basename = "tool_wpick.png";
339 else if(m_toolname == "STPick")
340 basename = "tool_stpick.png";
341 else if(m_toolname == "MesePick")
342 basename = "tool_mesepick.png";
344 basename = "cloud.png";
347 Calculate a progress value with sane amount of
350 u32 maxprogress = 30;
351 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
353 float value_f = (float)toolprogress / (float)maxprogress;
354 std::ostringstream os;
355 os<<basename<<"^[progressbar"<<value_f;
357 return g_texturesource->getTextureRaw(os.str());
360 spec.addTid(g_irrlicht->getTextureId(basename));
361 spec.addTid(g_irrlicht->getTextureId(os.str()));
362 return g_irrlicht->getTexture(spec);*/
365 std::string getText()
369 /*std::ostringstream os;
373 for(i=0; i<(65535-m_wear)/d; i++)
379 /*std::ostringstream os;
388 std::string getToolName()
396 // Returns true if weared out
397 bool addWear(u16 add)
399 if(m_wear >= 65535 - add)
411 std::string m_toolname;
418 InventoryList(std::string name, u32 size);
421 void serialize(std::ostream &os);
422 void deSerialize(std::istream &is);
424 InventoryList(const InventoryList &other);
425 InventoryList & operator = (const InventoryList &other);
427 std::string getName();
432 // Get pointer to item
433 InventoryItem * getItem(u32 i);
434 // Returns old item (or NULL). Parameter can be NULL.
435 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
437 void deleteItem(u32 i);
439 // Adds an item to a suitable place. Returns leftover item.
440 // If all went into the list, returns NULL.
441 InventoryItem * addItem(InventoryItem *newitem);
443 // If possible, adds item to given slot.
444 // If cannot be added at all, returns the item back.
445 // If can be added partly, decremented item is returned back.
446 // If can be added fully, NULL is returned.
447 InventoryItem * addItem(u32 i, InventoryItem *newitem);
449 // Checks whether the item could be added to the given slot
450 bool itemFits(u32 i, InventoryItem *newitem);
452 // Takes some items from a slot.
453 // If there are not enough, takes as many as it can.
454 // Returns NULL if couldn't take any.
455 InventoryItem * takeItem(u32 i, u32 count);
457 // Decrements amount of every material item
458 void decrementMaterials(u16 count);
460 void print(std::ostream &o);
463 core::array<InventoryItem*> m_items;
476 Inventory(const Inventory &other);
477 Inventory & operator = (const Inventory &other);
479 void serialize(std::ostream &os);
480 void deSerialize(std::istream &is);
482 InventoryList * addList(const std::string &name, u32 size);
483 InventoryList * getList(const std::string &name);
484 bool deleteList(const std::string &name);
485 // A shorthand for adding items.
486 // Returns NULL if the item was fully added, leftover otherwise.
487 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
489 InventoryList *list = getList(listname);
492 return list->addItem(newitem);
497 s32 getListIndex(const std::string &name);
499 core::array<InventoryList*> m_lists;
504 struct InventoryContext
506 Player *current_player;
513 class InventoryAction;
515 class InventoryManager
519 virtual ~InventoryManager(){}
522 Get a pointer to an inventory specified by id.
527 virtual Inventory* getInventory(InventoryContext *c, std::string id)
529 // Used on the server by InventoryAction::apply and other stuff
530 virtual void inventoryModified(InventoryContext *c, std::string id)
532 // Used on the client
533 virtual void inventoryAction(InventoryAction *a)
537 #define IACTION_MOVE 0
539 struct InventoryAction
541 static InventoryAction * deSerialize(std::istream &is);
543 virtual u16 getType() const = 0;
544 virtual void serialize(std::ostream &os) = 0;
545 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
548 struct IMoveAction : public InventoryAction
550 // count=0 means "everything"
552 std::string from_inv;
553 std::string from_list;
565 IMoveAction(std::istream &is)
569 std::getline(is, ts, ' ');
572 std::getline(is, from_inv, ' ');
574 std::getline(is, from_list, ' ');
576 std::getline(is, ts, ' ');
579 std::getline(is, to_inv, ' ');
581 std::getline(is, to_list, ' ');
583 std::getline(is, ts, ' ');
592 void serialize(std::ostream &os)
604 void apply(InventoryContext *c, InventoryManager *mgr);
608 Craft checking system
622 enum ItemSpecType type;
623 // Only other one of these is used
631 ItemSpec(enum ItemSpecType a_type, std::string a_name):
637 ItemSpec(enum ItemSpecType a_type, u16 a_num):
644 bool checkItem(InventoryItem *item);
648 items: a pointer to an array of 9 pointers to items
649 specs: a pointer to an array of 9 ItemSpecs
651 bool checkItemCombination(InventoryItem **items, ItemSpec *specs);