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() const { return ""; }
58 // Shall return an image of the item (or NULL)
59 virtual video::ITexture * getImage() const { return NULL; }
60 // Shall return an image of the item without embellishments (or NULL)
61 virtual video::ITexture * getImageRaw() const { 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();
67 // Creates an object from the item, to be placed in the world.
68 virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
69 // Gets amount of items that dropping one SAO will decrement
70 virtual u16 getDropCount() const { return getCount(); }
76 // Shall return true if the item can be add()ed to the other
77 virtual bool addableTo(const InventoryItem *other) const
86 void setCount(u16 count)
90 // This should return something else for stackable items
91 virtual u16 freeSpace() const
97 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
100 void remove(u16 count)
102 assert(m_count >= count);
110 // Whether it can be cooked
111 virtual bool isCookable() const {return false;}
113 virtual float getCookTime(){return 3.0;}
114 // Result of cooking (can randomize)
115 virtual InventoryItem *createCookResult() const {return NULL;}
117 // Eat, press, activate, whatever.
118 // Called when item is right-clicked when lying on ground.
119 // If returns true, item shall be deleted.
120 virtual bool use(ServerEnvironment *env,
121 Player *player){return false;}
127 class MaterialItem : public InventoryItem
130 MaterialItem(content_t content, u16 count):
136 Implementation interface
138 virtual const char* getName() const
140 return "MaterialItem";
142 virtual void serialize(std::ostream &os) const
144 //os.imbue(std::locale("C"));
147 os<<(unsigned int)m_content;
151 virtual InventoryItem* clone()
153 return new MaterialItem(m_content, m_count);
156 video::ITexture * getImage() const
158 return content_features(m_content).inventory_texture;
161 std::string getText()
163 std::ostringstream os;
168 virtual bool addableTo(const InventoryItem *other) const
170 if(std::string(other->getName()) != "MaterialItem")
172 MaterialItem *m = (MaterialItem*)other;
173 if(m->getMaterial() != m_content)
177 u16 freeSpace() const
179 if(m_count > QUANTITY_ITEM_MAX_COUNT)
181 return QUANTITY_ITEM_MAX_COUNT - m_count;
186 bool isCookable() const;
187 InventoryItem *createCookResult() const;
191 content_t getMaterial()
200 class MapBlockObjectItem : public InventoryItem
203 MapBlockObjectItem(std::string inventorystring):
206 m_inventorystring = inventorystring;
210 Implementation interface
212 virtual const char* getName() const
216 virtual void serialize(std::ostream &os) const
218 std::string sane_string(m_inventorystring);
219 str_replace_char(sane_string, '|', '?');
225 virtual InventoryItem* clone()
227 return new MapBlockObjectItem(m_inventorystring);
231 video::ITexture * getImage() const;
233 std::string getText();
238 std::string getInventoryString()
240 return m_inventorystring;
243 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
246 std::string m_inventorystring;
250 An item that is used as a mid-product when crafting.
254 class CraftItem : public InventoryItem
257 CraftItem(std::string subname, u16 count):
263 Implementation interface
265 virtual const char* getName() const
269 virtual void serialize(std::ostream &os) const
277 virtual InventoryItem* clone()
279 return new CraftItem(m_subname, m_count);
282 video::ITexture * getImage() const;
284 std::string getText()
286 std::ostringstream os;
291 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
292 u16 getDropCount() const;
294 virtual bool addableTo(const InventoryItem *other) const
296 if(std::string(other->getName()) != "CraftItem")
298 CraftItem *m = (CraftItem*)other;
299 if(m->m_subname != m_subname)
303 u16 freeSpace() const
305 if(m_count > QUANTITY_ITEM_MAX_COUNT)
307 return QUANTITY_ITEM_MAX_COUNT - m_count;
314 bool isCookable() const;
315 InventoryItem *createCookResult() const;
317 bool use(ServerEnvironment *env, Player *player);
322 std::string getSubName()
327 std::string m_subname;
330 class ToolItem : public InventoryItem
333 ToolItem(std::string toolname, u16 wear):
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_toolname, m_wear);
359 std::string getBasename() const {
360 if(m_toolname == "WPick")
361 return "tool_woodpick.png";
362 else if(m_toolname == "STPick")
363 return "tool_stonepick.png";
364 else if(m_toolname == "SteelPick")
365 return "tool_steelpick.png";
366 else if(m_toolname == "MesePick")
367 return "tool_mesepick.png";
368 else if(m_toolname == "WShovel")
369 return "tool_woodshovel.png";
370 else if(m_toolname == "STShovel")
371 return "tool_stoneshovel.png";
372 else if(m_toolname == "SteelShovel")
373 return "tool_steelshovel.png";
374 else if(m_toolname == "WAxe")
375 return "tool_woodaxe.png";
376 else if(m_toolname == "STAxe")
377 return "tool_stoneaxe.png";
378 else if(m_toolname == "SteelAxe")
379 return "tool_steelaxe.png";
380 else if(m_toolname == "WSword")
381 return "tool_woodsword.png";
382 else if(m_toolname == "STSword")
383 return "tool_stonesword.png";
384 else if(m_toolname == "SteelSword")
385 return "tool_steelsword.png";
390 video::ITexture * getImage() const
392 if(g_texturesource == NULL)
395 std::string basename = getBasename();
398 Calculate a progress value with sane amount of
401 u32 maxprogress = 30;
402 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
404 float value_f = (float)toolprogress / (float)maxprogress;
405 std::ostringstream os;
406 os<<basename<<"^[progressbar"<<value_f;
408 return g_texturesource->getTextureRaw(os.str());
411 video::ITexture * getImageRaw() const
413 if(g_texturesource == NULL)
416 return g_texturesource->getTextureRaw(getBasename());
419 std::string getText()
423 /*std::ostringstream os;
427 for(i=0; i<(65535-m_wear)/d; i++)
433 /*std::ostringstream os;
442 std::string getToolName()
450 // Returns true if weared out
451 bool addWear(u16 add)
453 if(m_wear >= 65535 - add)
465 std::string m_toolname;
472 InventoryList(std::string name, u32 size);
475 void serialize(std::ostream &os) const;
476 void deSerialize(std::istream &is);
478 InventoryList(const InventoryList &other);
479 InventoryList & operator = (const InventoryList &other);
481 const std::string &getName() const;
487 /*bool getDirty(){ return m_dirty; }
488 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
490 // Get pointer to item
491 const InventoryItem * getItem(u32 i) const;
492 InventoryItem * getItem(u32 i);
493 // Returns old item (or NULL). Parameter can be NULL.
494 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
496 void deleteItem(u32 i);
498 // Adds an item to a suitable place. Returns leftover item.
499 // If all went into the list, returns NULL.
500 InventoryItem * addItem(InventoryItem *newitem);
502 // If possible, adds item to given slot.
503 // If cannot be added at all, returns the item back.
504 // If can be added partly, decremented item is returned back.
505 // If can be added fully, NULL is returned.
506 InventoryItem * addItem(u32 i, InventoryItem *newitem);
508 // Checks whether the item could be added to the given slot
509 bool itemFits(const u32 i, const InventoryItem *newitem);
511 // Checks whether there is room for a given item
512 bool roomForItem(const InventoryItem *item);
514 // Checks whether there is room for a given item aftr it has been cooked
515 bool roomForCookedItem(const InventoryItem *item);
517 // Takes some items from a slot.
518 // If there are not enough, takes as many as it can.
519 // Returns NULL if couldn't take any.
520 InventoryItem * takeItem(u32 i, u32 count);
522 // Decrements amount of every material item
523 void decrementMaterials(u16 count);
525 void print(std::ostream &o);
528 core::array<InventoryItem*> m_items;
542 Inventory(const Inventory &other);
543 Inventory & operator = (const Inventory &other);
545 void serialize(std::ostream &os) const;
546 void deSerialize(std::istream &is);
548 InventoryList * addList(const std::string &name, u32 size);
549 InventoryList * getList(const std::string &name);
550 const InventoryList * getList(const std::string &name) const;
551 bool deleteList(const std::string &name);
552 // A shorthand for adding items.
553 // Returns NULL if the item was fully added, leftover otherwise.
554 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
556 InventoryList *list = getList(listname);
559 return list->addItem(newitem);
564 const s32 getListIndex(const std::string &name) const;
566 core::array<InventoryList*> m_lists;
571 struct InventoryContext
573 Player *current_player;
580 struct InventoryAction;
582 class InventoryManager
586 virtual ~InventoryManager(){}
589 Get a pointer to an inventory specified by id.
594 virtual Inventory* getInventory(InventoryContext *c, std::string id)
596 // Used on the server by InventoryAction::apply and other stuff
597 virtual void inventoryModified(InventoryContext *c, std::string id)
599 // Used on the client
600 virtual void inventoryAction(InventoryAction *a)
604 #define IACTION_MOVE 0
606 struct InventoryAction
608 static InventoryAction * deSerialize(std::istream &is);
610 virtual u16 getType() const = 0;
611 virtual void serialize(std::ostream &os) const = 0;
612 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
615 struct IMoveAction : public InventoryAction
617 // count=0 means "everything"
619 std::string from_inv;
620 std::string from_list;
632 IMoveAction(std::istream &is)
636 std::getline(is, ts, ' ');
639 std::getline(is, from_inv, ' ');
641 std::getline(is, from_list, ' ');
643 std::getline(is, ts, ' ');
646 std::getline(is, to_inv, ' ');
648 std::getline(is, to_list, ' ');
650 std::getline(is, ts, ' ');
659 void serialize(std::ostream &os) const
671 void apply(InventoryContext *c, InventoryManager *mgr);
675 Craft checking system
689 enum ItemSpecType type;
690 // Only other one of these is used
698 ItemSpec(enum ItemSpecType a_type, std::string a_name):
704 ItemSpec(enum ItemSpecType a_type, u16 a_num):
711 bool checkItem(const InventoryItem *item) const;
715 items: a pointer to an array of 9 pointers to items
716 specs: a pointer to an array of 9 ItemSpecs
718 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);