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;
41 InventoryItem(IGameDef *gamedef, u16 count);
42 virtual ~InventoryItem();
44 static InventoryItem* deSerialize(std::istream &is, IGameDef *gamedef);
45 static InventoryItem* deSerialize(const std::string &str,
48 virtual const char* getName() const = 0;
49 // Shall write the name and the parameters
50 virtual void serialize(std::ostream &os) const = 0;
51 // Shall make an exact clone of the item
52 virtual InventoryItem* clone() = 0;
53 // Return the name of the image for this item
54 virtual std::string getImageBasename() const { return ""; }
56 // Shall return an image of the item (or NULL)
57 virtual video::ITexture * getImage() const
59 // Shall return an image of the item without embellishments (or NULL)
60 virtual video::ITexture * getImageRaw() const
61 { return getImage(); }
63 // Shall return a text to show in the GUI
64 virtual std::string getText() { return ""; }
65 // Returns the string used for inventory
66 virtual std::string getItemString();
68 // Shall return false if item is not known and cannot be used
69 virtual bool isKnown() const { return true; }
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)
93 u16 max = getStackMax();
103 void remove(u16 count)
105 assert(m_count >= count);
113 // Maximum size of a stack
114 virtual u16 getStackMax() const {return 1;}
115 // Whether it can be used
116 virtual bool isUsable() const {return false;}
117 // Whether it can be cooked
118 virtual bool isCookable() const {return false;}
119 // Result of cooking (can randomize)
120 virtual InventoryItem *createCookResult() const {return NULL;}
122 virtual float getCookTime() const {return 3.0;}
123 // Whether it can be burned (<0 = cannot be burned)
124 virtual float getBurnTime() const {return -1;}
125 // Gets amount of items that dropping one ItemSAO will decrement
126 // -1 means as many as possible
127 virtual s16 getDropCount() const { return -1; }
128 // Whether this item can point to liquids
129 virtual bool areLiquidsPointable() const { return false; }
131 // Creates an object from the item and places it in the world.
132 // If return value is true, item should be removed.
133 virtual bool dropOrPlace(ServerEnvironment *env,
134 ServerActiveObject *dropper,
135 v3f pos, bool place, s16 count);
137 // Eat, press, activate, whatever.
138 // Called when item is left-clicked while in hand.
139 // If returns true, item shall be deleted.
140 virtual bool use(ServerEnvironment *env,
141 ServerActiveObject *user,
142 const PointedThing& pointed){return false;}
149 class MaterialItem : public InventoryItem
152 MaterialItem(IGameDef *gamedef, std::string nodename, u16 count);
153 // Legacy constructor
154 MaterialItem(IGameDef *gamedef, content_t content, u16 count);
156 Implementation interface
158 virtual const char* getName() const
160 return "MaterialItem";
162 virtual void serialize(std::ostream &os) const
170 virtual InventoryItem* clone()
172 return new MaterialItem(m_gamedef, m_nodename, m_count);
175 video::ITexture * getImage() const;
177 std::string getText()
179 std::ostringstream os;
184 virtual bool addableTo(const InventoryItem *other) const
186 if(std::string(other->getName()) != "MaterialItem")
188 MaterialItem *m = (MaterialItem*)other;
189 if(m->m_nodename != m_nodename)
193 virtual bool isSubsetOf(const InventoryItem *other) const
195 if(std::string(other->getName()) != "MaterialItem")
197 MaterialItem *m = (MaterialItem*)other;
198 if(m->m_nodename != m_nodename)
200 return m_count <= m->m_count;
202 virtual bool removeOther(const InventoryItem *other)
204 if(!other->isSubsetOf(this))
206 MaterialItem *m = (MaterialItem*)other;
207 m_count += m->m_count;
211 u16 getStackMax() const
213 return QUANTITY_ITEM_MAX_COUNT;
219 bool isCookable() const;
220 InventoryItem *createCookResult() const;
221 float getCookTime() const;
222 float getBurnTime() const;
224 Special properties (not part of virtual interface)
226 std::string getNodeName() const
227 { return m_nodename; }
228 content_t getMaterial() const;
230 std::string m_nodename;
234 An item that is used as a mid-product when crafting.
238 class CraftItem : public InventoryItem
241 CraftItem(IGameDef *gamedef, std::string subname, u16 count);
243 Implementation interface
245 virtual const char* getName() const
249 virtual void serialize(std::ostream &os) const
257 virtual InventoryItem* clone()
259 return new CraftItem(m_gamedef, m_subname, m_count);
262 video::ITexture * getImage() const;
264 std::string getText()
266 std::ostringstream os;
271 virtual bool isKnown() const;
273 virtual bool addableTo(const InventoryItem *other) const
275 if(std::string(other->getName()) != "CraftItem")
277 CraftItem *m = (CraftItem*)other;
278 if(m->m_subname != m_subname)
282 virtual bool isSubsetOf(const InventoryItem *other) const
284 if(std::string(other->getName()) != "CraftItem")
286 CraftItem *m = (CraftItem*)other;
287 if(m->m_subname != m_subname)
289 return m_count <= m->m_count;
291 virtual bool removeOther(const InventoryItem *other)
293 if(!other->isSubsetOf(this))
295 CraftItem *m = (CraftItem*)other;
296 m_count += m->m_count;
304 u16 getStackMax() const;
305 bool isUsable() const;
306 bool isCookable() const;
307 InventoryItem *createCookResult() const;
308 float getCookTime() const;
309 float getBurnTime() const;
310 s16 getDropCount() const;
311 bool areLiquidsPointable() const;
313 bool dropOrPlace(ServerEnvironment *env,
314 ServerActiveObject *dropper,
315 v3f pos, bool place, s16 count);
316 bool use(ServerEnvironment *env,
317 ServerActiveObject *user,
318 const PointedThing& pointed);
323 std::string getSubName()
328 std::string m_subname;
331 class ToolItem : public InventoryItem
334 ToolItem(IGameDef *gamedef, std::string toolname, u16 wear):
335 InventoryItem(gamedef, 1)
337 m_toolname = toolname;
341 Implementation interface
343 virtual const char* getName() const
347 virtual void serialize(std::ostream &os) const
355 virtual InventoryItem* clone()
357 return new ToolItem(m_gamedef, m_toolname, m_wear);
360 std::string getImageBasename() const;
362 video::ITexture * getImage() const;
363 video::ITexture * getImageRaw() const;
366 std::string getText()
371 virtual bool isKnown() const;
373 virtual bool isSubsetOf(const InventoryItem *other) const
375 if(std::string(other->getName()) != "ToolItem")
377 ToolItem *m = (ToolItem*)other;
378 if(m->m_toolname != m_toolname)
380 return m_wear <= m->m_wear;
382 virtual bool removeOther(const InventoryItem *other)
384 if(!other->isSubsetOf(this))
386 ToolItem *m = (ToolItem*)other;
394 std::string getToolName()
402 // Returns true if weared out
403 bool addWear(u16 add)
405 if(m_wear >= 65535 - add)
417 std::string m_toolname;
424 InventoryList(std::string name, u32 size);
427 void serialize(std::ostream &os) const;
428 void deSerialize(std::istream &is, IGameDef *gamedef);
430 InventoryList(const InventoryList &other);
431 InventoryList & operator = (const InventoryList &other);
433 const std::string &getName() const;
439 /*bool getDirty(){ return m_dirty; }
440 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
442 // Get pointer to item
443 const InventoryItem * getItem(u32 i) const;
444 InventoryItem * getItem(u32 i);
445 // Returns old item (or NULL). Parameter can be NULL.
446 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
448 void deleteItem(u32 i);
450 // Adds an item to a suitable place. Returns leftover item.
451 // If all went into the list, returns NULL.
452 InventoryItem * addItem(InventoryItem *newitem);
454 // If possible, adds item to given slot.
455 // If cannot be added at all, returns the item back.
456 // If can be added partly, decremented item is returned back.
457 // If can be added fully, NULL is returned.
458 InventoryItem * addItem(u32 i, InventoryItem *newitem);
460 // Checks whether the item could be added to the given slot
461 bool itemFits(const u32 i, const InventoryItem *newitem);
463 // Checks whether there is room for a given item
464 bool roomForItem(const InventoryItem *item);
466 // Checks whether there is room for a given item aftr it has been cooked
467 bool roomForCookedItem(const InventoryItem *item);
469 // Takes some items from a slot.
470 // If there are not enough, takes as many as it can.
471 // Returns NULL if couldn't take any.
472 InventoryItem * takeItem(u32 i, u32 count);
474 // Decrements amount of every material item
475 void decrementMaterials(u16 count);
477 void print(std::ostream &o);
480 core::array<InventoryItem*> m_items;
494 Inventory(const Inventory &other);
495 Inventory & operator = (const Inventory &other);
497 void serialize(std::ostream &os) const;
498 void deSerialize(std::istream &is, IGameDef *gamedef);
500 InventoryList * addList(const std::string &name, u32 size);
501 InventoryList * getList(const std::string &name);
502 const InventoryList * getList(const std::string &name) const;
503 bool deleteList(const std::string &name);
504 // A shorthand for adding items.
505 // Returns NULL if the item was fully added, leftover otherwise.
506 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
508 InventoryList *list = getList(listname);
511 return list->addItem(newitem);
516 const s32 getListIndex(const std::string &name) const;
518 core::array<InventoryList*> m_lists;
523 struct InventoryContext
525 Player *current_player;
532 struct InventoryAction;
534 class InventoryManager
538 virtual ~InventoryManager(){}
541 Get a pointer to an inventory specified by id.
546 virtual Inventory* getInventory(InventoryContext *c, std::string id)
548 // Used on the server by InventoryAction::apply and other stuff
549 virtual void inventoryModified(InventoryContext *c, std::string id)
551 // Used on the client
552 virtual void inventoryAction(InventoryAction *a)
556 #define IACTION_MOVE 0
557 #define IACTION_DROP 1
559 struct InventoryAction
561 static InventoryAction * deSerialize(std::istream &is);
563 virtual u16 getType() const = 0;
564 virtual void serialize(std::ostream &os) const = 0;
565 virtual void apply(InventoryContext *c, InventoryManager *mgr,
566 ServerEnvironment *env) = 0;
569 struct IMoveAction : public InventoryAction
571 // count=0 means "everything"
573 std::string from_inv;
574 std::string from_list;
587 IMoveAction(std::istream &is);
594 void serialize(std::ostream &os) const
606 void apply(InventoryContext *c, InventoryManager *mgr,
607 ServerEnvironment *env);
610 struct IDropAction : public InventoryAction
612 // count=0 means "everything"
614 std::string from_inv;
615 std::string from_list;
624 IDropAction(std::istream &is);
631 void serialize(std::ostream &os) const
640 void apply(InventoryContext *c, InventoryManager *mgr,
641 ServerEnvironment *env);
645 Craft checking system
659 enum ItemSpecType type;
660 // Only other one of these is used
668 ItemSpec(enum ItemSpecType a_type, std::string a_name):
674 ItemSpec(enum ItemSpecType a_type, u16 a_num):
681 bool checkItem(const InventoryItem *item) const;
685 items: a pointer to an array of 9 pointers to items
686 specs: a pointer to an array of 9 ItemSpecs
688 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);
691 items: a pointer to an array of 9 pointers to items
692 specs: a pointer to an array of 9 pointers to items
694 bool checkItemCombination(const InventoryItem * const * items,
695 const InventoryItem * const * specs);