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);
45 virtual const char* getName() const = 0;
46 // Shall write the name and the parameters
47 virtual void serialize(std::ostream &os) const = 0;
48 // Shall make an exact clone of the item
49 virtual InventoryItem* clone() = 0;
50 // Return the name of the image for this item
51 virtual std::string getImageBasename() const { return ""; }
53 // Shall return an image of the item (or NULL)
54 virtual video::ITexture * getImage() const
56 // Shall return an image of the item without embellishments (or NULL)
57 virtual video::ITexture * getImageRaw() const
58 { return getImage(); }
60 // Shall return a text to show in the GUI
61 virtual std::string getText() { return ""; }
62 // Returns the string used for inventory
63 virtual std::string getItemString();
64 // Creates an object from the item, to be placed in the world.
65 virtual ServerActiveObject* createSAO(ServerEnvironment *env, v3f pos);
66 // Gets amount of items that dropping one SAO will decrement
67 virtual u16 getDropCount() const { return getCount(); }
73 // Return true if the item can be add()ed to the other
74 virtual bool addableTo(const InventoryItem *other) const
76 // Return true if the other item contains this item
77 virtual bool isSubsetOf(const InventoryItem *other) const
79 // Remove the other item from this one if possible and return true
80 // Return false if not possible
81 virtual bool removeOther(const InventoryItem *other)
86 void setCount(u16 count)
89 // This should return something else for stackable items
90 virtual u16 freeSpace() const
95 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
98 void remove(u16 count)
100 assert(m_count >= count);
108 // Whether it can be cooked
109 virtual bool isCookable() const {return false;}
110 // Result of cooking (can randomize)
111 virtual InventoryItem *createCookResult() const {return NULL;}
113 virtual float getCookTime() const {return 3.0;}
114 // Whether it can be burned (<0 = cannot be burned)
115 virtual float getBurnTime() const {return -1;}
117 // Eat, press, activate, whatever.
118 // Called when item is right-clicked when lying on ground.
119 // If returns true, item shall be deleted.
120 virtual bool use(ServerEnvironment *env,
121 ServerActiveObject *user){return false;}
128 class MaterialItem : public InventoryItem
131 MaterialItem(IGameDef *gamedef, std::string nodename, u16 count);
132 // Legacy constructor
133 MaterialItem(IGameDef *gamedef, content_t content, u16 count);
135 Implementation interface
137 virtual const char* getName() const
139 return "MaterialItem";
141 virtual void serialize(std::ostream &os) const
149 virtual InventoryItem* clone()
151 return new MaterialItem(m_gamedef, m_nodename, m_count);
154 video::ITexture * getImage() const;
156 std::string getText()
158 std::ostringstream os;
163 virtual bool addableTo(const InventoryItem *other) const
165 if(std::string(other->getName()) != "MaterialItem")
167 MaterialItem *m = (MaterialItem*)other;
168 if(m->m_nodename != m_nodename)
172 virtual bool isSubsetOf(const InventoryItem *other) const
174 if(std::string(other->getName()) != "MaterialItem")
176 MaterialItem *m = (MaterialItem*)other;
177 if(m->m_nodename != m_nodename)
179 return m_count <= m->m_count;
181 virtual bool removeOther(const InventoryItem *other)
183 if(!other->isSubsetOf(this))
185 MaterialItem *m = (MaterialItem*)other;
186 m_count += m->m_count;
190 u16 freeSpace() const
192 if(m_count > QUANTITY_ITEM_MAX_COUNT)
194 return QUANTITY_ITEM_MAX_COUNT - m_count;
199 bool isCookable() const;
200 InventoryItem *createCookResult() const;
201 float getCookTime() const;
202 float getBurnTime() const;
204 Special properties (not part of virtual interface)
206 std::string getNodeName() const
207 { return m_nodename; }
208 content_t getMaterial() const;
210 std::string m_nodename;
214 An item that is used as a mid-product when crafting.
218 class CraftItem : public InventoryItem
221 CraftItem(IGameDef *gamedef, std::string subname, u16 count):
222 InventoryItem(gamedef, count)
227 Implementation interface
229 virtual const char* getName() const
233 virtual void serialize(std::ostream &os) const
241 virtual InventoryItem* clone()
243 return new CraftItem(m_gamedef, m_subname, m_count);
246 video::ITexture * getImage() const;
248 std::string getText()
250 std::ostringstream os;
255 ServerActiveObject* createSAO(ServerEnvironment *env, v3f pos);
256 u16 getDropCount() const;
258 virtual bool addableTo(const InventoryItem *other) const
260 if(std::string(other->getName()) != "CraftItem")
262 CraftItem *m = (CraftItem*)other;
263 if(m->m_subname != m_subname)
267 virtual bool isSubsetOf(const InventoryItem *other) const
269 if(std::string(other->getName()) != "CraftItem")
271 CraftItem *m = (CraftItem*)other;
272 if(m->m_subname != m_subname)
274 return m_count <= m->m_count;
276 virtual bool removeOther(const InventoryItem *other)
278 if(!other->isSubsetOf(this))
280 CraftItem *m = (CraftItem*)other;
281 m_count += m->m_count;
285 u16 freeSpace() const
287 if(m_count > QUANTITY_ITEM_MAX_COUNT)
289 return QUANTITY_ITEM_MAX_COUNT - m_count;
296 bool isCookable() const;
297 InventoryItem *createCookResult() const;
298 float getCookTime() const;
299 float getBurnTime() const;
301 bool use(ServerEnvironment *env, ServerActiveObject *user);
306 std::string getSubName()
311 std::string m_subname;
314 class ToolItem : public InventoryItem
317 ToolItem(IGameDef *gamedef, std::string toolname, u16 wear):
318 InventoryItem(gamedef, 1)
320 m_toolname = toolname;
324 Implementation interface
326 virtual const char* getName() const
330 virtual void serialize(std::ostream &os) const
338 virtual InventoryItem* clone()
340 return new ToolItem(m_gamedef, m_toolname, m_wear);
343 std::string getImageBasename() const;
345 video::ITexture * getImage() const;
346 video::ITexture * getImageRaw() const;
349 std::string getText()
354 virtual bool isSubsetOf(const InventoryItem *other) const
356 if(std::string(other->getName()) != "ToolItem")
358 ToolItem *m = (ToolItem*)other;
359 if(m->m_toolname != m_toolname)
361 return m_wear <= m->m_wear;
363 virtual bool removeOther(const InventoryItem *other)
365 if(!other->isSubsetOf(this))
367 ToolItem *m = (ToolItem*)other;
375 std::string getToolName()
383 // Returns true if weared out
384 bool addWear(u16 add)
386 if(m_wear >= 65535 - add)
398 std::string m_toolname;
405 InventoryList(std::string name, u32 size);
408 void serialize(std::ostream &os) const;
409 void deSerialize(std::istream &is, IGameDef *gamedef);
411 InventoryList(const InventoryList &other);
412 InventoryList & operator = (const InventoryList &other);
414 const std::string &getName() const;
420 /*bool getDirty(){ return m_dirty; }
421 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
423 // Get pointer to item
424 const InventoryItem * getItem(u32 i) const;
425 InventoryItem * getItem(u32 i);
426 // Returns old item (or NULL). Parameter can be NULL.
427 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
429 void deleteItem(u32 i);
431 // Adds an item to a suitable place. Returns leftover item.
432 // If all went into the list, returns NULL.
433 InventoryItem * addItem(InventoryItem *newitem);
435 // If possible, adds item to given slot.
436 // If cannot be added at all, returns the item back.
437 // If can be added partly, decremented item is returned back.
438 // If can be added fully, NULL is returned.
439 InventoryItem * addItem(u32 i, InventoryItem *newitem);
441 // Checks whether the item could be added to the given slot
442 bool itemFits(const u32 i, const InventoryItem *newitem);
444 // Checks whether there is room for a given item
445 bool roomForItem(const InventoryItem *item);
447 // Checks whether there is room for a given item aftr it has been cooked
448 bool roomForCookedItem(const InventoryItem *item);
450 // Takes some items from a slot.
451 // If there are not enough, takes as many as it can.
452 // Returns NULL if couldn't take any.
453 InventoryItem * takeItem(u32 i, u32 count);
455 // Decrements amount of every material item
456 void decrementMaterials(u16 count);
458 void print(std::ostream &o);
461 core::array<InventoryItem*> m_items;
475 Inventory(const Inventory &other);
476 Inventory & operator = (const Inventory &other);
478 void serialize(std::ostream &os) const;
479 void deSerialize(std::istream &is, IGameDef *gamedef);
481 InventoryList * addList(const std::string &name, u32 size);
482 InventoryList * getList(const std::string &name);
483 const InventoryList * getList(const std::string &name) const;
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 const s32 getListIndex(const std::string &name) const;
499 core::array<InventoryList*> m_lists;
504 struct InventoryContext
506 Player *current_player;
513 struct 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
538 #define IACTION_DROP 1
540 struct InventoryAction
542 static InventoryAction * deSerialize(std::istream &is);
544 virtual u16 getType() const = 0;
545 virtual void serialize(std::ostream &os) const = 0;
546 virtual void apply(InventoryContext *c, InventoryManager *mgr,
547 ServerEnvironment *env) = 0;
550 struct IMoveAction : public InventoryAction
552 // count=0 means "everything"
554 std::string from_inv;
555 std::string from_list;
568 IMoveAction(std::istream &is);
575 void serialize(std::ostream &os) const
587 void apply(InventoryContext *c, InventoryManager *mgr,
588 ServerEnvironment *env);
591 struct IDropAction : public InventoryAction
593 // count=0 means "everything"
595 std::string from_inv;
596 std::string from_list;
605 IDropAction(std::istream &is);
612 void serialize(std::ostream &os) const
621 void apply(InventoryContext *c, InventoryManager *mgr,
622 ServerEnvironment *env);
626 Craft checking system
640 enum ItemSpecType type;
641 // Only other one of these is used
649 ItemSpec(enum ItemSpecType a_type, std::string a_name):
655 ItemSpec(enum ItemSpecType a_type, u16 a_num):
662 bool checkItem(const InventoryItem *item) const;
666 items: a pointer to an array of 9 pointers to items
667 specs: a pointer to an array of 9 ItemSpecs
669 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);
672 items: a pointer to an array of 9 pointers to items
673 specs: a pointer to an array of 9 pointers to items
675 bool checkItemCombination(const InventoryItem * const * items,
676 const InventoryItem * const * specs);