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 "mapblockobject.h"
33 #include "main.h" // For g_materials
34 #include "mapnode.h" // For content_t
36 #define QUANTITY_ITEM_MAX_COUNT 99
38 class ServerActiveObject;
39 class ServerEnvironment;
45 InventoryItem(u16 count);
46 virtual ~InventoryItem();
48 static InventoryItem* deSerialize(std::istream &is);
50 virtual const char* getName() const = 0;
51 // Shall write the name and the parameters
52 virtual void serialize(std::ostream &os) const = 0;
53 // Shall make an exact clone of the item
54 virtual InventoryItem* clone() = 0;
56 // Return the name of the image for this item
57 virtual std::string getBasename() { return ""; }
58 // Shall return an image of the item (or NULL)
59 virtual video::ITexture * getImage() { return NULL; }
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 Player *player){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()
156 return content_features(m_content).inventory_texture;
159 std::string getText()
161 std::ostringstream os;
166 virtual bool addableTo(const InventoryItem *other) const
168 if(std::string(other->getName()) != "MaterialItem")
170 MaterialItem *m = (MaterialItem*)other;
171 if(m->getMaterial() != m_content)
175 u16 freeSpace() const
177 if(m_count > QUANTITY_ITEM_MAX_COUNT)
179 return QUANTITY_ITEM_MAX_COUNT - m_count;
184 bool isCookable() const;
185 InventoryItem *createCookResult() const;
189 content_t getMaterial()
198 class MapBlockObjectItem : public InventoryItem
201 MapBlockObjectItem(std::string inventorystring):
204 m_inventorystring = inventorystring;
208 Implementation interface
210 virtual const char* getName() const
214 virtual void serialize(std::ostream &os) const
216 std::string sane_string(m_inventorystring);
217 str_replace_char(sane_string, '|', '?');
223 virtual InventoryItem* clone()
225 return new MapBlockObjectItem(m_inventorystring);
229 video::ITexture * getImage();
231 std::string getText();
236 std::string getInventoryString()
238 return m_inventorystring;
241 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
244 std::string m_inventorystring;
248 An item that is used as a mid-product when crafting.
252 class CraftItem : public InventoryItem
255 CraftItem(std::string subname, u16 count):
261 Implementation interface
263 virtual const char* getName() const
267 virtual void serialize(std::ostream &os) const
275 virtual InventoryItem* clone()
277 return new CraftItem(m_subname, m_count);
280 video::ITexture * getImage();
282 std::string getText()
284 std::ostringstream os;
289 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
290 u16 getDropCount() const;
292 virtual bool addableTo(const InventoryItem *other) const
294 if(std::string(other->getName()) != "CraftItem")
296 CraftItem *m = (CraftItem*)other;
297 if(m->m_subname != m_subname)
301 u16 freeSpace() const
303 if(m_count > QUANTITY_ITEM_MAX_COUNT)
305 return QUANTITY_ITEM_MAX_COUNT - m_count;
312 bool isCookable() const;
313 InventoryItem *createCookResult() const;
315 bool use(ServerEnvironment *env, Player *player);
320 std::string getSubName()
325 std::string m_subname;
328 class ToolItem : public InventoryItem
331 ToolItem(std::string toolname, u16 wear):
334 m_toolname = toolname;
338 Implementation interface
340 virtual const char* getName() const
344 virtual void serialize(std::ostream &os) const
352 virtual InventoryItem* clone()
354 return new ToolItem(m_toolname, m_wear);
357 std::string getBasename() {
358 if(m_toolname == "WPick")
359 return "tool_woodpick.png";
360 else if(m_toolname == "STPick")
361 return "tool_stonepick.png";
362 else if(m_toolname == "SteelPick")
363 return "tool_steelpick.png";
364 else if(m_toolname == "MesePick")
365 return "tool_mesepick.png";
366 else if(m_toolname == "WShovel")
367 return "tool_woodshovel.png";
368 else if(m_toolname == "STShovel")
369 return "tool_stoneshovel.png";
370 else if(m_toolname == "SteelShovel")
371 return "tool_steelshovel.png";
372 else if(m_toolname == "WAxe")
373 return "tool_woodaxe.png";
374 else if(m_toolname == "STAxe")
375 return "tool_stoneaxe.png";
376 else if(m_toolname == "SteelAxe")
377 return "tool_steelaxe.png";
378 else if(m_toolname == "WSword")
379 return "tool_woodsword.png";
380 else if(m_toolname == "STSword")
381 return "tool_stonesword.png";
382 else if(m_toolname == "SteelSword")
383 return "tool_steelsword.png";
388 video::ITexture * getImage()
390 if(g_texturesource == NULL)
393 std::string basename = getBasename();
396 Calculate a progress value with sane amount of
399 u32 maxprogress = 30;
400 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
402 float value_f = (float)toolprogress / (float)maxprogress;
403 std::ostringstream os;
404 os<<basename<<"^[progressbar"<<value_f;
406 return g_texturesource->getTextureRaw(os.str());
409 std::string getText()
413 /*std::ostringstream os;
417 for(i=0; i<(65535-m_wear)/d; i++)
423 /*std::ostringstream os;
432 std::string getToolName()
440 // Returns true if weared out
441 bool addWear(u16 add)
443 if(m_wear >= 65535 - add)
455 std::string m_toolname;
462 InventoryList(std::string name, u32 size);
465 void serialize(std::ostream &os) const;
466 void deSerialize(std::istream &is);
468 InventoryList(const InventoryList &other);
469 InventoryList & operator = (const InventoryList &other);
471 const std::string &getName() const;
477 /*bool getDirty(){ return m_dirty; }
478 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
480 // Get pointer to item
481 const InventoryItem * getItem(u32 i) const;
482 InventoryItem * getItem(u32 i);
483 // Returns old item (or NULL). Parameter can be NULL.
484 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
486 void deleteItem(u32 i);
488 // Adds an item to a suitable place. Returns leftover item.
489 // If all went into the list, returns NULL.
490 InventoryItem * addItem(InventoryItem *newitem);
492 // If possible, adds item to given slot.
493 // If cannot be added at all, returns the item back.
494 // If can be added partly, decremented item is returned back.
495 // If can be added fully, NULL is returned.
496 InventoryItem * addItem(u32 i, InventoryItem *newitem);
498 // Checks whether the item could be added to the given slot
499 bool itemFits(const u32 i, const InventoryItem *newitem);
501 // Checks whether there is room for a given item
502 bool roomForItem(const InventoryItem *item);
504 // Checks whether there is room for a given item aftr it has been cooked
505 bool roomForCookedItem(const InventoryItem *item);
507 // Takes some items from a slot.
508 // If there are not enough, takes as many as it can.
509 // Returns NULL if couldn't take any.
510 InventoryItem * takeItem(u32 i, u32 count);
512 // Decrements amount of every material item
513 void decrementMaterials(u16 count);
515 void print(std::ostream &o);
518 core::array<InventoryItem*> m_items;
532 Inventory(const Inventory &other);
533 Inventory & operator = (const Inventory &other);
535 void serialize(std::ostream &os) const;
536 void deSerialize(std::istream &is);
538 InventoryList * addList(const std::string &name, u32 size);
539 InventoryList * getList(const std::string &name);
540 const InventoryList * getList(const std::string &name) const;
541 bool deleteList(const std::string &name);
542 // A shorthand for adding items.
543 // Returns NULL if the item was fully added, leftover otherwise.
544 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
546 InventoryList *list = getList(listname);
549 return list->addItem(newitem);
554 const s32 getListIndex(const std::string &name) const;
556 core::array<InventoryList*> m_lists;
561 struct InventoryContext
563 Player *current_player;
570 struct InventoryAction;
572 class InventoryManager
576 virtual ~InventoryManager(){}
579 Get a pointer to an inventory specified by id.
584 virtual Inventory* getInventory(InventoryContext *c, std::string id)
586 // Used on the server by InventoryAction::apply and other stuff
587 virtual void inventoryModified(InventoryContext *c, std::string id)
589 // Used on the client
590 virtual void inventoryAction(InventoryAction *a)
594 #define IACTION_MOVE 0
596 struct InventoryAction
598 static InventoryAction * deSerialize(std::istream &is);
600 virtual u16 getType() const = 0;
601 virtual void serialize(std::ostream &os) const = 0;
602 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
605 struct IMoveAction : public InventoryAction
607 // count=0 means "everything"
609 std::string from_inv;
610 std::string from_list;
622 IMoveAction(std::istream &is)
626 std::getline(is, ts, ' ');
629 std::getline(is, from_inv, ' ');
631 std::getline(is, from_list, ' ');
633 std::getline(is, ts, ' ');
636 std::getline(is, to_inv, ' ');
638 std::getline(is, to_list, ' ');
640 std::getline(is, ts, ' ');
649 void serialize(std::ostream &os) const
661 void apply(InventoryContext *c, InventoryManager *mgr);
665 Craft checking system
679 enum ItemSpecType type;
680 // Only other one of these is used
688 ItemSpec(enum ItemSpecType a_type, std::string a_name):
694 ItemSpec(enum ItemSpecType a_type, u16 a_num):
701 bool checkItem(const InventoryItem *item) const;
705 items: a pointer to an array of 9 pointers to items
706 specs: a pointer to an array of 9 ItemSpecs
708 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);