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();
72 // Return true if the item can be add()ed to the other
73 virtual bool addableTo(const InventoryItem *other) const
75 // Return true if the other item contains this item
76 virtual bool isSubsetOf(const InventoryItem *other) const
78 // Remove the other item from this one if possible and return true
79 // Return false if not possible
80 virtual bool removeOther(const InventoryItem *other)
85 void setCount(u16 count)
90 u16 max = getStackMax();
100 void remove(u16 count)
102 assert(m_count >= count);
110 // Maximum size of a stack
111 virtual u16 getStackMax() const {return 1;}
112 // Whether it can be used
113 virtual bool isUsable() const {return false;}
114 // Whether it can be cooked
115 virtual bool isCookable() const {return false;}
116 // Result of cooking (can randomize)
117 virtual InventoryItem *createCookResult() const {return NULL;}
119 virtual float getCookTime() const {return 3.0;}
120 // Whether it can be burned (<0 = cannot be burned)
121 virtual float getBurnTime() const {return -1;}
122 // Gets amount of items that dropping one ItemSAO will decrement
123 // -1 means as many as possible
124 virtual s16 getDropCount() const { return -1; }
125 // Whether this item can point to liquids
126 virtual bool areLiquidsPointable() const { return false; }
128 // Creates an object from the item and places it in the world.
129 // If return value is true, item should be removed.
130 virtual bool dropOrPlace(ServerEnvironment *env,
131 ServerActiveObject *dropper,
132 v3f pos, bool place, s16 count);
134 // Eat, press, activate, whatever.
135 // Called when item is left-clicked while in hand.
136 // If returns true, item shall be deleted.
137 virtual bool use(ServerEnvironment *env,
138 ServerActiveObject *user,
139 const PointedThing& pointed){return false;}
146 class MaterialItem : public InventoryItem
149 MaterialItem(IGameDef *gamedef, std::string nodename, u16 count);
150 // Legacy constructor
151 MaterialItem(IGameDef *gamedef, content_t content, u16 count);
153 Implementation interface
155 virtual const char* getName() const
157 return "MaterialItem";
159 virtual void serialize(std::ostream &os) const
167 virtual InventoryItem* clone()
169 return new MaterialItem(m_gamedef, m_nodename, m_count);
172 video::ITexture * getImage() const;
174 std::string getText()
176 std::ostringstream os;
181 virtual bool addableTo(const InventoryItem *other) const
183 if(std::string(other->getName()) != "MaterialItem")
185 MaterialItem *m = (MaterialItem*)other;
186 if(m->m_nodename != m_nodename)
190 virtual bool isSubsetOf(const InventoryItem *other) const
192 if(std::string(other->getName()) != "MaterialItem")
194 MaterialItem *m = (MaterialItem*)other;
195 if(m->m_nodename != m_nodename)
197 return m_count <= m->m_count;
199 virtual bool removeOther(const InventoryItem *other)
201 if(!other->isSubsetOf(this))
203 MaterialItem *m = (MaterialItem*)other;
204 m_count += m->m_count;
208 u16 getStackMax() const
210 return QUANTITY_ITEM_MAX_COUNT;
216 bool isCookable() const;
217 InventoryItem *createCookResult() const;
218 float getCookTime() const;
219 float getBurnTime() const;
221 Special properties (not part of virtual interface)
223 std::string getNodeName() const
224 { return m_nodename; }
225 content_t getMaterial() const;
227 std::string m_nodename;
231 An item that is used as a mid-product when crafting.
235 class CraftItem : public InventoryItem
238 CraftItem(IGameDef *gamedef, std::string subname, u16 count):
239 InventoryItem(gamedef, count)
244 Implementation interface
246 virtual const char* getName() const
250 virtual void serialize(std::ostream &os) const
258 virtual InventoryItem* clone()
260 return new CraftItem(m_gamedef, m_subname, m_count);
263 video::ITexture * getImage() const;
265 std::string getText()
267 std::ostringstream os;
272 virtual bool addableTo(const InventoryItem *other) const
274 if(std::string(other->getName()) != "CraftItem")
276 CraftItem *m = (CraftItem*)other;
277 if(m->m_subname != m_subname)
281 virtual bool isSubsetOf(const InventoryItem *other) const
283 if(std::string(other->getName()) != "CraftItem")
285 CraftItem *m = (CraftItem*)other;
286 if(m->m_subname != m_subname)
288 return m_count <= m->m_count;
290 virtual bool removeOther(const InventoryItem *other)
292 if(!other->isSubsetOf(this))
294 CraftItem *m = (CraftItem*)other;
295 m_count += m->m_count;
303 u16 getStackMax() const;
304 bool isUsable() const;
305 bool isCookable() const;
306 InventoryItem *createCookResult() const;
307 float getCookTime() const;
308 float getBurnTime() const;
309 s16 getDropCount() const;
310 bool areLiquidsPointable() const;
312 bool dropOrPlace(ServerEnvironment *env,
313 ServerActiveObject *dropper,
314 v3f pos, bool place, s16 count);
315 bool use(ServerEnvironment *env,
316 ServerActiveObject *user,
317 const PointedThing& pointed);
322 std::string getSubName()
327 std::string m_subname;
330 class ToolItem : public InventoryItem
333 ToolItem(IGameDef *gamedef, std::string toolname, u16 wear):
334 InventoryItem(gamedef, 1)
336 m_toolname = toolname;
340 Implementation interface
342 virtual const char* getName() const
346 virtual void serialize(std::ostream &os) const
354 virtual InventoryItem* clone()
356 return new ToolItem(m_gamedef, m_toolname, m_wear);
359 std::string getImageBasename() const;
361 video::ITexture * getImage() const;
362 video::ITexture * getImageRaw() const;
365 std::string getText()
370 virtual bool isSubsetOf(const InventoryItem *other) const
372 if(std::string(other->getName()) != "ToolItem")
374 ToolItem *m = (ToolItem*)other;
375 if(m->m_toolname != m_toolname)
377 return m_wear <= m->m_wear;
379 virtual bool removeOther(const InventoryItem *other)
381 if(!other->isSubsetOf(this))
383 ToolItem *m = (ToolItem*)other;
391 std::string getToolName()
399 // Returns true if weared out
400 bool addWear(u16 add)
402 if(m_wear >= 65535 - add)
414 std::string m_toolname;
421 InventoryList(std::string name, u32 size);
424 void serialize(std::ostream &os) const;
425 void deSerialize(std::istream &is, IGameDef *gamedef);
427 InventoryList(const InventoryList &other);
428 InventoryList & operator = (const InventoryList &other);
430 const std::string &getName() const;
436 /*bool getDirty(){ return m_dirty; }
437 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
439 // Get pointer to item
440 const InventoryItem * getItem(u32 i) const;
441 InventoryItem * getItem(u32 i);
442 // Returns old item (or NULL). Parameter can be NULL.
443 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
445 void deleteItem(u32 i);
447 // Adds an item to a suitable place. Returns leftover item.
448 // If all went into the list, returns NULL.
449 InventoryItem * addItem(InventoryItem *newitem);
451 // If possible, adds item to given slot.
452 // If cannot be added at all, returns the item back.
453 // If can be added partly, decremented item is returned back.
454 // If can be added fully, NULL is returned.
455 InventoryItem * addItem(u32 i, InventoryItem *newitem);
457 // Checks whether the item could be added to the given slot
458 bool itemFits(const u32 i, const InventoryItem *newitem);
460 // Checks whether there is room for a given item
461 bool roomForItem(const InventoryItem *item);
463 // Checks whether there is room for a given item aftr it has been cooked
464 bool roomForCookedItem(const InventoryItem *item);
466 // Takes some items from a slot.
467 // If there are not enough, takes as many as it can.
468 // Returns NULL if couldn't take any.
469 InventoryItem * takeItem(u32 i, u32 count);
471 // Decrements amount of every material item
472 void decrementMaterials(u16 count);
474 void print(std::ostream &o);
477 core::array<InventoryItem*> m_items;
491 Inventory(const Inventory &other);
492 Inventory & operator = (const Inventory &other);
494 void serialize(std::ostream &os) const;
495 void deSerialize(std::istream &is, IGameDef *gamedef);
497 InventoryList * addList(const std::string &name, u32 size);
498 InventoryList * getList(const std::string &name);
499 const InventoryList * getList(const std::string &name) const;
500 bool deleteList(const std::string &name);
501 // A shorthand for adding items.
502 // Returns NULL if the item was fully added, leftover otherwise.
503 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
505 InventoryList *list = getList(listname);
508 return list->addItem(newitem);
513 const s32 getListIndex(const std::string &name) const;
515 core::array<InventoryList*> m_lists;
520 struct InventoryContext
522 Player *current_player;
529 struct InventoryAction;
531 class InventoryManager
535 virtual ~InventoryManager(){}
538 Get a pointer to an inventory specified by id.
543 virtual Inventory* getInventory(InventoryContext *c, std::string id)
545 // Used on the server by InventoryAction::apply and other stuff
546 virtual void inventoryModified(InventoryContext *c, std::string id)
548 // Used on the client
549 virtual void inventoryAction(InventoryAction *a)
553 #define IACTION_MOVE 0
554 #define IACTION_DROP 1
556 struct InventoryAction
558 static InventoryAction * deSerialize(std::istream &is);
560 virtual u16 getType() const = 0;
561 virtual void serialize(std::ostream &os) const = 0;
562 virtual void apply(InventoryContext *c, InventoryManager *mgr,
563 ServerEnvironment *env) = 0;
566 struct IMoveAction : public InventoryAction
568 // count=0 means "everything"
570 std::string from_inv;
571 std::string from_list;
584 IMoveAction(std::istream &is);
591 void serialize(std::ostream &os) const
603 void apply(InventoryContext *c, InventoryManager *mgr,
604 ServerEnvironment *env);
607 struct IDropAction : public InventoryAction
609 // count=0 means "everything"
611 std::string from_inv;
612 std::string from_list;
621 IDropAction(std::istream &is);
628 void serialize(std::ostream &os) const
637 void apply(InventoryContext *c, InventoryManager *mgr,
638 ServerEnvironment *env);
642 Craft checking system
656 enum ItemSpecType type;
657 // Only other one of these is used
665 ItemSpec(enum ItemSpecType a_type, std::string a_name):
671 ItemSpec(enum ItemSpecType a_type, u16 a_num):
678 bool checkItem(const InventoryItem *item) const;
682 items: a pointer to an array of 9 pointers to items
683 specs: a pointer to an array of 9 ItemSpecs
685 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);
688 items: a pointer to an array of 9 pointers to items
689 specs: a pointer to an array of 9 pointers to items
691 bool checkItemCombination(const InventoryItem * const * items,
692 const InventoryItem * const * specs);