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 "main.h" // For g_materials
33 #include "mapnode.h" // For content_t
35 #define QUANTITY_ITEM_MAX_COUNT 99
37 class ServerActiveObject;
38 class ServerEnvironment;
43 InventoryItem(u16 count);
44 virtual ~InventoryItem();
46 static InventoryItem* deSerialize(std::istream &is);
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;
54 // Return the name of the image for this item
55 virtual std::string getImageBasename() const { return ""; }
56 // Shall return an image of the item (or NULL)
57 virtual video::ITexture * getImage() const { return NULL; }
58 // Shall return an image of the item without embellishments (or NULL)
59 virtual video::ITexture * getImageRaw() const { return getImage(); }
61 // Shall return a text to show in the GUI
62 virtual std::string getText() { return ""; }
63 // Returns the string used for inventory
64 virtual std::string getItemString();
65 // Creates an object from the item, to be placed in the world.
66 virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
67 // Gets amount of items that dropping one SAO will decrement
68 virtual u16 getDropCount() const { return getCount(); }
74 // Shall return true if the item can be add()ed to the other
75 virtual bool addableTo(const InventoryItem *other) const
84 void setCount(u16 count)
88 // This should return something else for stackable items
89 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;}
111 virtual float getCookTime(){return 3.0;}
112 // Result of cooking (can randomize)
113 virtual InventoryItem *createCookResult() const {return NULL;}
115 // Eat, press, activate, whatever.
116 // Called when item is right-clicked when lying on ground.
117 // If returns true, item shall be deleted.
118 virtual bool use(ServerEnvironment *env,
119 ServerActiveObject *user){return false;}
125 class MaterialItem : public InventoryItem
128 MaterialItem(content_t content, u16 count):
134 Implementation interface
136 virtual const char* getName() const
138 return "MaterialItem";
140 virtual void serialize(std::ostream &os) const
142 //os.imbue(std::locale("C"));
145 os<<(unsigned int)m_content;
149 virtual InventoryItem* clone()
151 return new MaterialItem(m_content, 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->getMaterial() != m_content)
172 u16 freeSpace() const
174 if(m_count > QUANTITY_ITEM_MAX_COUNT)
176 return QUANTITY_ITEM_MAX_COUNT - m_count;
181 bool isCookable() const;
182 InventoryItem *createCookResult() const;
186 content_t getMaterial()
195 An item that is used as a mid-product when crafting.
199 class CraftItem : public InventoryItem
202 CraftItem(std::string subname, u16 count):
208 Implementation interface
210 virtual const char* getName() const
214 virtual void serialize(std::ostream &os) const
222 virtual InventoryItem* clone()
224 return new CraftItem(m_subname, m_count);
227 video::ITexture * getImage() const;
229 std::string getText()
231 std::ostringstream os;
236 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
237 u16 getDropCount() const;
239 virtual bool addableTo(const InventoryItem *other) const
241 if(std::string(other->getName()) != "CraftItem")
243 CraftItem *m = (CraftItem*)other;
244 if(m->m_subname != m_subname)
248 u16 freeSpace() const
250 if(m_count > QUANTITY_ITEM_MAX_COUNT)
252 return QUANTITY_ITEM_MAX_COUNT - m_count;
259 bool isCookable() const;
260 InventoryItem *createCookResult() const;
262 bool use(ServerEnvironment *env, ServerActiveObject *user);
267 std::string getSubName()
272 std::string m_subname;
275 class ToolItem : public InventoryItem
278 ToolItem(std::string toolname, u16 wear):
281 m_toolname = toolname;
285 Implementation interface
287 virtual const char* getName() const
291 virtual void serialize(std::ostream &os) const
299 virtual InventoryItem* clone()
301 return new ToolItem(m_toolname, m_wear);
304 std::string getImageBasename() const;
307 video::ITexture * getImage() const
309 if(g_texturesource == NULL)
312 std::string basename = getImageBasename();
315 Calculate a progress value with sane amount of
318 u32 maxprogress = 30;
319 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
321 float value_f = (float)toolprogress / (float)maxprogress;
322 std::ostringstream os;
323 os<<basename<<"^[progressbar"<<value_f;
325 return g_texturesource->getTextureRaw(os.str());
328 video::ITexture * getImageRaw() const
330 if(g_texturesource == NULL)
333 return g_texturesource->getTextureRaw(getImageBasename());
336 std::string getText()
340 /*std::ostringstream os;
344 for(i=0; i<(65535-m_wear)/d; i++)
350 /*std::ostringstream os;
359 std::string getToolName()
367 // Returns true if weared out
368 bool addWear(u16 add)
370 if(m_wear >= 65535 - add)
382 std::string m_toolname;
389 InventoryList(std::string name, u32 size);
392 void serialize(std::ostream &os) const;
393 void deSerialize(std::istream &is);
395 InventoryList(const InventoryList &other);
396 InventoryList & operator = (const InventoryList &other);
398 const std::string &getName() const;
404 /*bool getDirty(){ return m_dirty; }
405 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
407 // Get pointer to item
408 const InventoryItem * getItem(u32 i) const;
409 InventoryItem * getItem(u32 i);
410 // Returns old item (or NULL). Parameter can be NULL.
411 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
413 void deleteItem(u32 i);
415 // Adds an item to a suitable place. Returns leftover item.
416 // If all went into the list, returns NULL.
417 InventoryItem * addItem(InventoryItem *newitem);
419 // If possible, adds item to given slot.
420 // If cannot be added at all, returns the item back.
421 // If can be added partly, decremented item is returned back.
422 // If can be added fully, NULL is returned.
423 InventoryItem * addItem(u32 i, InventoryItem *newitem);
425 // Checks whether the item could be added to the given slot
426 bool itemFits(const u32 i, const InventoryItem *newitem);
428 // Checks whether there is room for a given item
429 bool roomForItem(const InventoryItem *item);
431 // Checks whether there is room for a given item aftr it has been cooked
432 bool roomForCookedItem(const InventoryItem *item);
434 // Takes some items from a slot.
435 // If there are not enough, takes as many as it can.
436 // Returns NULL if couldn't take any.
437 InventoryItem * takeItem(u32 i, u32 count);
439 // Decrements amount of every material item
440 void decrementMaterials(u16 count);
442 void print(std::ostream &o);
445 core::array<InventoryItem*> m_items;
459 Inventory(const Inventory &other);
460 Inventory & operator = (const Inventory &other);
462 void serialize(std::ostream &os) const;
463 void deSerialize(std::istream &is);
465 InventoryList * addList(const std::string &name, u32 size);
466 InventoryList * getList(const std::string &name);
467 const InventoryList * getList(const std::string &name) const;
468 bool deleteList(const std::string &name);
469 // A shorthand for adding items.
470 // Returns NULL if the item was fully added, leftover otherwise.
471 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
473 InventoryList *list = getList(listname);
476 return list->addItem(newitem);
481 const s32 getListIndex(const std::string &name) const;
483 core::array<InventoryList*> m_lists;
488 struct InventoryContext
490 Player *current_player;
497 struct InventoryAction;
499 class InventoryManager
503 virtual ~InventoryManager(){}
506 Get a pointer to an inventory specified by id.
511 virtual Inventory* getInventory(InventoryContext *c, std::string id)
513 // Used on the server by InventoryAction::apply and other stuff
514 virtual void inventoryModified(InventoryContext *c, std::string id)
516 // Used on the client
517 virtual void inventoryAction(InventoryAction *a)
521 #define IACTION_MOVE 0
523 struct InventoryAction
525 static InventoryAction * deSerialize(std::istream &is);
527 virtual u16 getType() const = 0;
528 virtual void serialize(std::ostream &os) const = 0;
529 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
532 struct IMoveAction : public InventoryAction
534 // count=0 means "everything"
536 std::string from_inv;
537 std::string from_list;
549 IMoveAction(std::istream &is)
553 std::getline(is, ts, ' ');
556 std::getline(is, from_inv, ' ');
558 std::getline(is, from_list, ' ');
560 std::getline(is, ts, ' ');
563 std::getline(is, to_inv, ' ');
565 std::getline(is, to_list, ' ');
567 std::getline(is, ts, ' ');
576 void serialize(std::ostream &os) const
588 void apply(InventoryContext *c, InventoryManager *mgr);
592 Craft checking system
606 enum ItemSpecType type;
607 // Only other one of these is used
615 ItemSpec(enum ItemSpecType a_type, std::string a_name):
621 ItemSpec(enum ItemSpecType a_type, u16 a_num):
628 bool checkItem(const InventoryItem *item) const;
632 items: a pointer to an array of 9 pointers to items
633 specs: a pointer to an array of 9 ItemSpecs
635 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);