3 Copyright (C) 2010-2011 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.
20 #ifndef INVENTORY_HEADER
21 #define INVENTORY_HEADER
26 #include "common_irrlicht.h"
28 #include "mapnode.h" // For content_t
30 #define QUANTITY_ITEM_MAX_COUNT 99
32 class ServerActiveObject;
33 class ServerEnvironment;
40 InventoryItem(IGameDef *gamedef, u16 count);
41 virtual ~InventoryItem();
43 static InventoryItem* deSerialize(std::istream &is, IGameDef *gamedef);
44 static InventoryItem* deSerialize(const std::string &str,
47 virtual const char* getName() const = 0;
48 // Shall write the name and the parameters
49 virtual void serialize(std::ostream &os) const = 0;
50 // Shall make an exact clone of the item
51 virtual InventoryItem* clone() = 0;
52 // Return the name of the image for this item
53 virtual std::string getImageBasename() const { return ""; }
55 // Shall return an image of the item (or NULL)
56 virtual video::ITexture * getImage() const
58 // Shall return an image of the item without embellishments (or NULL)
59 virtual video::ITexture * getImageRaw() const
60 { return getImage(); }
62 // Shall return a text to show in the GUI
63 virtual std::string getText() { return ""; }
64 // Returns the string used for inventory
65 virtual std::string getItemString();
66 // Creates an object from the item, to be placed in the world.
67 virtual ServerActiveObject* createSAO(ServerEnvironment *env, v3f pos);
68 // Gets amount of items that dropping one SAO will decrement
69 virtual u16 getDropCount() const { return getCount(); }
75 // Return true if the item can be add()ed to the other
76 virtual bool addableTo(const InventoryItem *other) const
78 // Return true if the other item contains this item
79 virtual bool isSubsetOf(const InventoryItem *other) const
81 // Remove the other item from this one if possible and return true
82 // Return false if not possible
83 virtual bool removeOther(const InventoryItem *other)
88 void setCount(u16 count)
91 // This should return something else for stackable items
92 virtual u16 freeSpace() const
97 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
100 void remove(u16 count)
102 assert(m_count >= count);
110 // Whether it can be cooked
111 virtual bool isCookable() const {return false;}
112 // Result of cooking (can randomize)
113 virtual InventoryItem *createCookResult() const {return NULL;}
115 virtual float getCookTime() const {return 3.0;}
116 // Whether it can be burned (<0 = cannot be burned)
117 virtual float getBurnTime() const {return -1;}
119 // Eat, press, activate, whatever.
120 // Called when item is right-clicked when lying on ground.
121 // If returns true, item shall be deleted.
122 virtual bool use(ServerEnvironment *env,
123 ServerActiveObject *user){return false;}
130 class MaterialItem : public InventoryItem
133 MaterialItem(IGameDef *gamedef, std::string nodename, u16 count);
134 // Legacy constructor
135 MaterialItem(IGameDef *gamedef, content_t content, u16 count);
137 Implementation interface
139 virtual const char* getName() const
141 return "MaterialItem";
143 virtual void serialize(std::ostream &os) const
151 virtual InventoryItem* clone()
153 return new MaterialItem(m_gamedef, m_nodename, m_count);
156 video::ITexture * getImage() const;
158 std::string getText()
160 std::ostringstream os;
165 virtual bool addableTo(const InventoryItem *other) const
167 if(std::string(other->getName()) != "MaterialItem")
169 MaterialItem *m = (MaterialItem*)other;
170 if(m->m_nodename != m_nodename)
174 virtual bool isSubsetOf(const InventoryItem *other) const
176 if(std::string(other->getName()) != "MaterialItem")
178 MaterialItem *m = (MaterialItem*)other;
179 if(m->m_nodename != m_nodename)
181 return m_count <= m->m_count;
183 virtual bool removeOther(const InventoryItem *other)
185 if(!other->isSubsetOf(this))
187 MaterialItem *m = (MaterialItem*)other;
188 m_count += m->m_count;
192 u16 freeSpace() const
194 if(m_count > QUANTITY_ITEM_MAX_COUNT)
196 return QUANTITY_ITEM_MAX_COUNT - m_count;
201 bool isCookable() const;
202 InventoryItem *createCookResult() const;
203 float getCookTime() const;
204 float getBurnTime() const;
206 Special properties (not part of virtual interface)
208 std::string getNodeName() const
209 { return m_nodename; }
210 content_t getMaterial() const;
212 std::string m_nodename;
216 An item that is used as a mid-product when crafting.
220 class CraftItem : public InventoryItem
223 CraftItem(IGameDef *gamedef, std::string subname, u16 count):
224 InventoryItem(gamedef, count)
229 Implementation interface
231 virtual const char* getName() const
235 virtual void serialize(std::ostream &os) const
243 virtual InventoryItem* clone()
245 return new CraftItem(m_gamedef, m_subname, m_count);
248 video::ITexture * getImage() const;
250 std::string getText()
252 std::ostringstream os;
257 ServerActiveObject* createSAO(ServerEnvironment *env, v3f pos);
258 u16 getDropCount() const;
260 virtual bool addableTo(const InventoryItem *other) const
262 if(std::string(other->getName()) != "CraftItem")
264 CraftItem *m = (CraftItem*)other;
265 if(m->m_subname != m_subname)
269 virtual bool isSubsetOf(const InventoryItem *other) const
271 if(std::string(other->getName()) != "CraftItem")
273 CraftItem *m = (CraftItem*)other;
274 if(m->m_subname != m_subname)
276 return m_count <= m->m_count;
278 virtual bool removeOther(const InventoryItem *other)
280 if(!other->isSubsetOf(this))
282 CraftItem *m = (CraftItem*)other;
283 m_count += m->m_count;
287 u16 freeSpace() const
289 if(m_count > QUANTITY_ITEM_MAX_COUNT)
291 return QUANTITY_ITEM_MAX_COUNT - m_count;
298 bool isCookable() const;
299 InventoryItem *createCookResult() const;
300 float getCookTime() const;
301 float getBurnTime() const;
303 bool use(ServerEnvironment *env, ServerActiveObject *user);
308 std::string getSubName()
313 std::string m_subname;
316 class ToolItem : public InventoryItem
319 ToolItem(IGameDef *gamedef, std::string toolname, u16 wear):
320 InventoryItem(gamedef, 1)
322 m_toolname = toolname;
326 Implementation interface
328 virtual const char* getName() const
332 virtual void serialize(std::ostream &os) const
340 virtual InventoryItem* clone()
342 return new ToolItem(m_gamedef, m_toolname, m_wear);
345 std::string getImageBasename() const;
347 video::ITexture * getImage() const;
348 video::ITexture * getImageRaw() const;
351 std::string getText()
356 virtual bool isSubsetOf(const InventoryItem *other) const
358 if(std::string(other->getName()) != "ToolItem")
360 ToolItem *m = (ToolItem*)other;
361 if(m->m_toolname != m_toolname)
363 return m_wear <= m->m_wear;
365 virtual bool removeOther(const InventoryItem *other)
367 if(!other->isSubsetOf(this))
369 ToolItem *m = (ToolItem*)other;
377 std::string getToolName()
385 // Returns true if weared out
386 bool addWear(u16 add)
388 if(m_wear >= 65535 - add)
400 std::string m_toolname;
407 InventoryList(std::string name, u32 size);
410 void serialize(std::ostream &os) const;
411 void deSerialize(std::istream &is, IGameDef *gamedef);
413 InventoryList(const InventoryList &other);
414 InventoryList & operator = (const InventoryList &other);
416 const std::string &getName() const;
422 /*bool getDirty(){ return m_dirty; }
423 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
425 // Get pointer to item
426 const InventoryItem * getItem(u32 i) const;
427 InventoryItem * getItem(u32 i);
428 // Returns old item (or NULL). Parameter can be NULL.
429 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
431 void deleteItem(u32 i);
433 // Adds an item to a suitable place. Returns leftover item.
434 // If all went into the list, returns NULL.
435 InventoryItem * addItem(InventoryItem *newitem);
437 // If possible, adds item to given slot.
438 // If cannot be added at all, returns the item back.
439 // If can be added partly, decremented item is returned back.
440 // If can be added fully, NULL is returned.
441 InventoryItem * addItem(u32 i, InventoryItem *newitem);
443 // Checks whether the item could be added to the given slot
444 bool itemFits(const u32 i, const InventoryItem *newitem);
446 // Checks whether there is room for a given item
447 bool roomForItem(const InventoryItem *item);
449 // Checks whether there is room for a given item aftr it has been cooked
450 bool roomForCookedItem(const InventoryItem *item);
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;
477 Inventory(const Inventory &other);
478 Inventory & operator = (const Inventory &other);
480 void serialize(std::ostream &os) const;
481 void deSerialize(std::istream &is, IGameDef *gamedef);
483 InventoryList * addList(const std::string &name, u32 size);
484 InventoryList * getList(const std::string &name);
485 const InventoryList * getList(const std::string &name) const;
486 bool deleteList(const std::string &name);
487 // A shorthand for adding items.
488 // Returns NULL if the item was fully added, leftover otherwise.
489 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
491 InventoryList *list = getList(listname);
494 return list->addItem(newitem);
499 const s32 getListIndex(const std::string &name) const;
501 core::array<InventoryList*> m_lists;
506 struct InventoryContext
508 Player *current_player;
515 struct InventoryAction;
517 class InventoryManager
521 virtual ~InventoryManager(){}
524 Get a pointer to an inventory specified by id.
529 virtual Inventory* getInventory(InventoryContext *c, std::string id)
531 // Used on the server by InventoryAction::apply and other stuff
532 virtual void inventoryModified(InventoryContext *c, std::string id)
534 // Used on the client
535 virtual void inventoryAction(InventoryAction *a)
539 #define IACTION_MOVE 0
540 #define IACTION_DROP 1
542 struct InventoryAction
544 static InventoryAction * deSerialize(std::istream &is);
546 virtual u16 getType() const = 0;
547 virtual void serialize(std::ostream &os) const = 0;
548 virtual void apply(InventoryContext *c, InventoryManager *mgr,
549 ServerEnvironment *env) = 0;
552 struct IMoveAction : public InventoryAction
554 // count=0 means "everything"
556 std::string from_inv;
557 std::string from_list;
570 IMoveAction(std::istream &is);
577 void serialize(std::ostream &os) const
589 void apply(InventoryContext *c, InventoryManager *mgr,
590 ServerEnvironment *env);
593 struct IDropAction : public InventoryAction
595 // count=0 means "everything"
597 std::string from_inv;
598 std::string from_list;
607 IDropAction(std::istream &is);
614 void serialize(std::ostream &os) const
623 void apply(InventoryContext *c, InventoryManager *mgr,
624 ServerEnvironment *env);
628 Craft checking system
642 enum ItemSpecType type;
643 // Only other one of these is used
651 ItemSpec(enum ItemSpecType a_type, std::string a_name):
657 ItemSpec(enum ItemSpecType a_type, u16 a_num):
664 bool checkItem(const InventoryItem *item) const;
668 items: a pointer to an array of 9 pointers to items
669 specs: a pointer to an array of 9 ItemSpecs
671 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);
674 items: a pointer to an array of 9 pointers to items
675 specs: a pointer to an array of 9 pointers to items
677 bool checkItemCombination(const InventoryItem * const * items,
678 const InventoryItem * const * specs);