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;
44 InventoryItem(u16 count);
45 virtual ~InventoryItem();
47 static InventoryItem* deSerialize(std::istream &is);
49 virtual const char* getName() const = 0;
50 // Shall write the name and the parameters
51 virtual void serialize(std::ostream &os) const = 0;
52 // Shall make an exact clone of the item
53 virtual InventoryItem* clone() = 0;
55 // Return the name of the image for this item
56 virtual std::string getBasename() const { return ""; }
57 // Shall return an image of the item (or NULL)
58 virtual video::ITexture * getImage() const { return NULL; }
59 // Shall return an image of the item without embellishments (or NULL)
60 virtual video::ITexture * getImageRaw() const { 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, u16 id, v3f pos);
68 // Gets amount of items that dropping one SAO will decrement
69 virtual u16 getDropCount() const { return getCount(); }
75 // Shall return true if the item can be add()ed to the other
76 virtual bool addableTo(const InventoryItem *other) const
85 void setCount(u16 count)
89 // This should return something else for stackable items
90 virtual u16 freeSpace() const
96 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
99 void remove(u16 count)
101 assert(m_count >= count);
109 // Whether it can be cooked
110 virtual bool isCookable() const {return false;}
112 virtual float getCookTime(){return 3.0;}
113 // Result of cooking (can randomize)
114 virtual InventoryItem *createCookResult() const {return NULL;}
116 // Eat, press, activate, whatever.
117 // Called when item is right-clicked when lying on ground.
118 // If returns true, item shall be deleted.
119 virtual bool use(ServerEnvironment *env,
120 Player *player){return false;}
126 class MaterialItem : public InventoryItem
129 MaterialItem(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
143 //os.imbue(std::locale("C"));
146 os<<(unsigned int)m_content;
150 virtual InventoryItem* clone()
152 return new MaterialItem(m_content, m_count);
155 video::ITexture * getImage() const
157 return content_features(m_content).inventory_texture;
160 std::string getText()
162 std::ostringstream os;
167 virtual bool addableTo(const InventoryItem *other) const
169 if(std::string(other->getName()) != "MaterialItem")
171 MaterialItem *m = (MaterialItem*)other;
172 if(m->getMaterial() != m_content)
176 u16 freeSpace() const
178 if(m_count > QUANTITY_ITEM_MAX_COUNT)
180 return QUANTITY_ITEM_MAX_COUNT - m_count;
185 bool isCookable() const;
186 InventoryItem *createCookResult() const;
190 content_t getMaterial()
199 An item that is used as a mid-product when crafting.
203 class CraftItem : public InventoryItem
206 CraftItem(std::string subname, u16 count):
212 Implementation interface
214 virtual const char* getName() const
218 virtual void serialize(std::ostream &os) const
226 virtual InventoryItem* clone()
228 return new CraftItem(m_subname, m_count);
231 video::ITexture * getImage() const;
233 std::string getText()
235 std::ostringstream os;
240 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
241 u16 getDropCount() const;
243 virtual bool addableTo(const InventoryItem *other) const
245 if(std::string(other->getName()) != "CraftItem")
247 CraftItem *m = (CraftItem*)other;
248 if(m->m_subname != m_subname)
252 u16 freeSpace() const
254 if(m_count > QUANTITY_ITEM_MAX_COUNT)
256 return QUANTITY_ITEM_MAX_COUNT - m_count;
263 bool isCookable() const;
264 InventoryItem *createCookResult() const;
266 bool use(ServerEnvironment *env, Player *player);
271 std::string getSubName()
276 std::string m_subname;
279 class ToolItem : public InventoryItem
282 ToolItem(std::string toolname, u16 wear):
285 m_toolname = toolname;
289 Implementation interface
291 virtual const char* getName() const
295 virtual void serialize(std::ostream &os) const
303 virtual InventoryItem* clone()
305 return new ToolItem(m_toolname, m_wear);
308 std::string getBasename() const {
309 if(m_toolname == "WPick")
310 return "tool_woodpick.png";
311 else if(m_toolname == "STPick")
312 return "tool_stonepick.png";
313 else if(m_toolname == "SteelPick")
314 return "tool_steelpick.png";
315 else if(m_toolname == "MesePick")
316 return "tool_mesepick.png";
317 else if(m_toolname == "WShovel")
318 return "tool_woodshovel.png";
319 else if(m_toolname == "STShovel")
320 return "tool_stoneshovel.png";
321 else if(m_toolname == "SteelShovel")
322 return "tool_steelshovel.png";
323 else if(m_toolname == "WAxe")
324 return "tool_woodaxe.png";
325 else if(m_toolname == "STAxe")
326 return "tool_stoneaxe.png";
327 else if(m_toolname == "SteelAxe")
328 return "tool_steelaxe.png";
329 else if(m_toolname == "WSword")
330 return "tool_woodsword.png";
331 else if(m_toolname == "STSword")
332 return "tool_stonesword.png";
333 else if(m_toolname == "SteelSword")
334 return "tool_steelsword.png";
339 video::ITexture * getImage() const
341 if(g_texturesource == NULL)
344 std::string basename = getBasename();
347 Calculate a progress value with sane amount of
350 u32 maxprogress = 30;
351 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
353 float value_f = (float)toolprogress / (float)maxprogress;
354 std::ostringstream os;
355 os<<basename<<"^[progressbar"<<value_f;
357 return g_texturesource->getTextureRaw(os.str());
360 video::ITexture * getImageRaw() const
362 if(g_texturesource == NULL)
365 return g_texturesource->getTextureRaw(getBasename());
368 std::string getText()
372 /*std::ostringstream os;
376 for(i=0; i<(65535-m_wear)/d; i++)
382 /*std::ostringstream os;
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);
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);
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
555 struct InventoryAction
557 static InventoryAction * deSerialize(std::istream &is);
559 virtual u16 getType() const = 0;
560 virtual void serialize(std::ostream &os) const = 0;
561 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
564 struct IMoveAction : public InventoryAction
566 // count=0 means "everything"
568 std::string from_inv;
569 std::string from_list;
581 IMoveAction(std::istream &is)
585 std::getline(is, ts, ' ');
588 std::getline(is, from_inv, ' ');
590 std::getline(is, from_list, ' ');
592 std::getline(is, ts, ' ');
595 std::getline(is, to_inv, ' ');
597 std::getline(is, to_list, ' ');
599 std::getline(is, ts, ' ');
608 void serialize(std::ostream &os) const
620 void apply(InventoryContext *c, InventoryManager *mgr);
624 Craft checking system
638 enum ItemSpecType type;
639 // Only other one of these is used
647 ItemSpec(enum ItemSpecType a_type, std::string a_name):
653 ItemSpec(enum ItemSpecType a_type, u16 a_num):
660 bool checkItem(const InventoryItem *item) const;
664 items: a pointer to an array of 9 pointers to items
665 specs: a pointer to an array of 9 ItemSpecs
667 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);