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 // Shall return an image to show in the GUI (or NULL)
57 virtual video::ITexture * getImage() { return NULL; }
59 // Shall return a text to show in the GUI
60 virtual std::string getText() { return ""; }
61 // Creates an object from the item, to be placed in the world.
62 virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
63 // Gets amount of items that dropping one SAO will decrement
64 virtual u16 getDropCount() const { return getCount(); }
70 // Shall return true if the item can be add()ed to the other
71 virtual bool addableTo(const InventoryItem *other) const
80 void setCount(u16 count)
84 // This should return something else for stackable items
85 virtual u16 freeSpace() const
91 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
94 void remove(u16 count)
96 assert(m_count >= count);
104 // Whether it can be cooked
105 virtual bool isCookable() const {return false;}
107 virtual float getCookTime(){return 3.0;}
108 // Result of cooking (can randomize)
109 virtual InventoryItem *createCookResult() const {return NULL;}
111 // Eat, press, activate, whatever.
112 // Called when item is right-clicked when lying on ground.
113 // If returns true, item shall be deleted.
114 virtual bool use(ServerEnvironment *env,
115 Player *player){return false;}
121 class MaterialItem : public InventoryItem
124 MaterialItem(content_t content, u16 count):
130 Implementation interface
132 virtual const char* getName() const
134 return "MaterialItem";
136 virtual void serialize(std::ostream &os) const
138 //os.imbue(std::locale("C"));
141 os<<(unsigned int)m_content;
145 virtual InventoryItem* clone()
147 return new MaterialItem(m_content, m_count);
150 video::ITexture * getImage()
152 return content_features(m_content).inventory_texture;
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 class MapBlockObjectItem : public InventoryItem
198 MapBlockObjectItem(std::string inventorystring):
201 m_inventorystring = inventorystring;
205 Implementation interface
207 virtual const char* getName() const
211 virtual void serialize(std::ostream &os) const
213 std::string sane_string(m_inventorystring);
214 str_replace_char(sane_string, '|', '?');
220 virtual InventoryItem* clone()
222 return new MapBlockObjectItem(m_inventorystring);
226 video::ITexture * getImage();
228 std::string getText();
233 std::string getInventoryString()
235 return m_inventorystring;
238 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
241 std::string m_inventorystring;
245 An item that is used as a mid-product when crafting.
249 class CraftItem : public InventoryItem
252 CraftItem(std::string subname, u16 count):
258 Implementation interface
260 virtual const char* getName() const
264 virtual void serialize(std::ostream &os) const
272 virtual InventoryItem* clone()
274 return new CraftItem(m_subname, m_count);
277 video::ITexture * getImage();
279 std::string getText()
281 std::ostringstream os;
286 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
287 u16 getDropCount() const;
289 virtual bool addableTo(const InventoryItem *other) const
291 if(std::string(other->getName()) != "CraftItem")
293 CraftItem *m = (CraftItem*)other;
294 if(m->m_subname != m_subname)
298 u16 freeSpace() const
300 if(m_count > QUANTITY_ITEM_MAX_COUNT)
302 return QUANTITY_ITEM_MAX_COUNT - m_count;
309 bool isCookable() const;
310 InventoryItem *createCookResult() const;
312 bool use(ServerEnvironment *env, Player *player);
317 std::string getSubName()
322 std::string m_subname;
325 class ToolItem : public InventoryItem
328 ToolItem(std::string toolname, u16 wear):
331 m_toolname = toolname;
335 Implementation interface
337 virtual const char* getName() const
341 virtual void serialize(std::ostream &os) const
349 virtual InventoryItem* clone()
351 return new ToolItem(m_toolname, m_wear);
354 video::ITexture * getImage()
356 if(g_texturesource == NULL)
359 std::string basename;
360 if(m_toolname == "WPick")
361 basename = "tool_woodpick.png";
362 else if(m_toolname == "STPick")
363 basename = "tool_stonepick.png";
364 else if(m_toolname == "SteelPick")
365 basename = "tool_steelpick.png";
366 else if(m_toolname == "MesePick")
367 basename = "tool_mesepick.png";
368 else if(m_toolname == "WShovel")
369 basename = "tool_woodshovel.png";
370 else if(m_toolname == "STShovel")
371 basename = "tool_stoneshovel.png";
372 else if(m_toolname == "SteelShovel")
373 basename = "tool_steelshovel.png";
374 else if(m_toolname == "WAxe")
375 basename = "tool_woodaxe.png";
376 else if(m_toolname == "STAxe")
377 basename = "tool_stoneaxe.png";
378 else if(m_toolname == "SteelAxe")
379 basename = "tool_steelaxe.png";
380 else if(m_toolname == "WSword")
381 basename = "tool_woodsword.png";
382 else if(m_toolname == "STSword")
383 basename = "tool_stonesword.png";
384 else if(m_toolname == "SteelSword")
385 basename = "tool_steelsword.png";
387 basename = "cloud.png";
390 Calculate a progress value with sane amount of
393 u32 maxprogress = 30;
394 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
396 float value_f = (float)toolprogress / (float)maxprogress;
397 std::ostringstream os;
398 os<<basename<<"^[progressbar"<<value_f;
400 return g_texturesource->getTextureRaw(os.str());
403 std::string getText()
407 /*std::ostringstream os;
411 for(i=0; i<(65535-m_wear)/d; i++)
417 /*std::ostringstream os;
426 std::string getToolName()
434 // Returns true if weared out
435 bool addWear(u16 add)
437 if(m_wear >= 65535 - add)
449 std::string m_toolname;
456 InventoryList(std::string name, u32 size);
459 void serialize(std::ostream &os) const;
460 void deSerialize(std::istream &is);
462 InventoryList(const InventoryList &other);
463 InventoryList & operator = (const InventoryList &other);
465 const std::string &getName() const;
471 /*bool getDirty(){ return m_dirty; }
472 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
474 // Get pointer to item
475 const InventoryItem * getItem(u32 i) const;
476 InventoryItem * getItem(u32 i);
477 // Returns old item (or NULL). Parameter can be NULL.
478 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
480 void deleteItem(u32 i);
482 // Adds an item to a suitable place. Returns leftover item.
483 // If all went into the list, returns NULL.
484 InventoryItem * addItem(InventoryItem *newitem);
486 // If possible, adds item to given slot.
487 // If cannot be added at all, returns the item back.
488 // If can be added partly, decremented item is returned back.
489 // If can be added fully, NULL is returned.
490 InventoryItem * addItem(u32 i, InventoryItem *newitem);
492 // Checks whether the item could be added to the given slot
493 bool itemFits(u32 i, InventoryItem *newitem);
495 // Takes some items from a slot.
496 // If there are not enough, takes as many as it can.
497 // Returns NULL if couldn't take any.
498 InventoryItem * takeItem(u32 i, u32 count);
500 // Decrements amount of every material item
501 void decrementMaterials(u16 count);
503 void print(std::ostream &o);
506 core::array<InventoryItem*> m_items;
520 Inventory(const Inventory &other);
521 Inventory & operator = (const Inventory &other);
523 void serialize(std::ostream &os) const;
524 void deSerialize(std::istream &is);
526 InventoryList * addList(const std::string &name, u32 size);
527 InventoryList * getList(const std::string &name);
528 const InventoryList * getList(const std::string &name) const;
529 bool deleteList(const std::string &name);
530 // A shorthand for adding items.
531 // Returns NULL if the item was fully added, leftover otherwise.
532 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
534 InventoryList *list = getList(listname);
537 return list->addItem(newitem);
542 const s32 getListIndex(const std::string &name) const;
544 core::array<InventoryList*> m_lists;
549 struct InventoryContext
551 Player *current_player;
558 struct InventoryAction;
560 class InventoryManager
564 virtual ~InventoryManager(){}
567 Get a pointer to an inventory specified by id.
572 virtual Inventory* getInventory(InventoryContext *c, std::string id)
574 // Used on the server by InventoryAction::apply and other stuff
575 virtual void inventoryModified(InventoryContext *c, std::string id)
577 // Used on the client
578 virtual void inventoryAction(InventoryAction *a)
582 #define IACTION_MOVE 0
584 struct InventoryAction
586 static InventoryAction * deSerialize(std::istream &is);
588 virtual u16 getType() const = 0;
589 virtual void serialize(std::ostream &os) const = 0;
590 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
593 struct IMoveAction : public InventoryAction
595 // count=0 means "everything"
597 std::string from_inv;
598 std::string from_list;
610 IMoveAction(std::istream &is)
614 std::getline(is, ts, ' ');
617 std::getline(is, from_inv, ' ');
619 std::getline(is, from_list, ' ');
621 std::getline(is, ts, ' ');
624 std::getline(is, to_inv, ' ');
626 std::getline(is, to_list, ' ');
628 std::getline(is, ts, ' ');
637 void serialize(std::ostream &os) const
649 void apply(InventoryContext *c, InventoryManager *mgr);
653 Craft checking system
667 enum ItemSpecType type;
668 // Only other one of these is used
676 ItemSpec(enum ItemSpecType a_type, std::string a_name):
682 ItemSpec(enum ItemSpecType a_type, u16 a_num):
689 bool checkItem(const InventoryItem *item) const;
693 items: a pointer to an array of 9 pointers to items
694 specs: a pointer to an array of 9 ItemSpecs
696 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);