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 // Returns the string used for inventory
62 virtual std::string getItemString();
63 // Creates an object from the item, to be placed in the world.
64 virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
65 // Gets amount of items that dropping one SAO will decrement
66 virtual u16 getDropCount() const { return getCount(); }
72 // Shall return true if the item can be add()ed to the other
73 virtual bool addableTo(const InventoryItem *other) const
82 void setCount(u16 count)
86 // This should return something else for stackable items
87 virtual u16 freeSpace() const
93 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
96 void remove(u16 count)
98 assert(m_count >= count);
106 // Whether it can be cooked
107 virtual bool isCookable() const {return false;}
109 virtual float getCookTime(){return 3.0;}
110 // Result of cooking (can randomize)
111 virtual InventoryItem *createCookResult() const {return NULL;}
113 // Eat, press, activate, whatever.
114 // Called when item is right-clicked when lying on ground.
115 // If returns true, item shall be deleted.
116 virtual bool use(ServerEnvironment *env,
117 Player *player){return false;}
123 class MaterialItem : public InventoryItem
126 MaterialItem(content_t content, u16 count):
132 Implementation interface
134 virtual const char* getName() const
136 return "MaterialItem";
138 virtual void serialize(std::ostream &os) const
140 //os.imbue(std::locale("C"));
143 os<<(unsigned int)m_content;
147 virtual InventoryItem* clone()
149 return new MaterialItem(m_content, m_count);
152 video::ITexture * getImage()
154 return content_features(m_content).inventory_texture;
158 std::string getText()
160 std::ostringstream os;
165 virtual bool addableTo(const InventoryItem *other) const
167 if(std::string(other->getName()) != "MaterialItem")
169 MaterialItem *m = (MaterialItem*)other;
170 if(m->getMaterial() != m_content)
174 u16 freeSpace() const
176 if(m_count > QUANTITY_ITEM_MAX_COUNT)
178 return QUANTITY_ITEM_MAX_COUNT - m_count;
183 bool isCookable() const;
184 InventoryItem *createCookResult() const;
188 content_t getMaterial()
197 class MapBlockObjectItem : public InventoryItem
200 MapBlockObjectItem(std::string inventorystring):
203 m_inventorystring = inventorystring;
207 Implementation interface
209 virtual const char* getName() const
213 virtual void serialize(std::ostream &os) const
215 std::string sane_string(m_inventorystring);
216 str_replace_char(sane_string, '|', '?');
222 virtual InventoryItem* clone()
224 return new MapBlockObjectItem(m_inventorystring);
228 video::ITexture * getImage();
230 std::string getText();
235 std::string getInventoryString()
237 return m_inventorystring;
240 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
243 std::string m_inventorystring;
247 An item that is used as a mid-product when crafting.
251 class CraftItem : public InventoryItem
254 CraftItem(std::string subname, u16 count):
260 Implementation interface
262 virtual const char* getName() const
266 virtual void serialize(std::ostream &os) const
274 virtual InventoryItem* clone()
276 return new CraftItem(m_subname, m_count);
279 video::ITexture * getImage();
281 std::string getText()
283 std::ostringstream os;
288 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
289 u16 getDropCount() const;
291 virtual bool addableTo(const InventoryItem *other) const
293 if(std::string(other->getName()) != "CraftItem")
295 CraftItem *m = (CraftItem*)other;
296 if(m->m_subname != m_subname)
300 u16 freeSpace() const
302 if(m_count > QUANTITY_ITEM_MAX_COUNT)
304 return QUANTITY_ITEM_MAX_COUNT - m_count;
311 bool isCookable() const;
312 InventoryItem *createCookResult() const;
314 bool use(ServerEnvironment *env, Player *player);
319 std::string getSubName()
324 std::string m_subname;
327 class ToolItem : public InventoryItem
330 ToolItem(std::string toolname, u16 wear):
333 m_toolname = toolname;
337 Implementation interface
339 virtual const char* getName() const
343 virtual void serialize(std::ostream &os) const
351 virtual InventoryItem* clone()
353 return new ToolItem(m_toolname, m_wear);
356 video::ITexture * getImage()
358 if(g_texturesource == NULL)
361 std::string basename;
362 if(m_toolname == "WPick")
363 basename = "tool_woodpick.png";
364 else if(m_toolname == "STPick")
365 basename = "tool_stonepick.png";
366 else if(m_toolname == "SteelPick")
367 basename = "tool_steelpick.png";
368 else if(m_toolname == "MesePick")
369 basename = "tool_mesepick.png";
370 else if(m_toolname == "WShovel")
371 basename = "tool_woodshovel.png";
372 else if(m_toolname == "STShovel")
373 basename = "tool_stoneshovel.png";
374 else if(m_toolname == "SteelShovel")
375 basename = "tool_steelshovel.png";
376 else if(m_toolname == "WAxe")
377 basename = "tool_woodaxe.png";
378 else if(m_toolname == "STAxe")
379 basename = "tool_stoneaxe.png";
380 else if(m_toolname == "SteelAxe")
381 basename = "tool_steelaxe.png";
382 else if(m_toolname == "WSword")
383 basename = "tool_woodsword.png";
384 else if(m_toolname == "STSword")
385 basename = "tool_stonesword.png";
386 else if(m_toolname == "SteelSword")
387 basename = "tool_steelsword.png";
389 basename = "cloud.png";
392 Calculate a progress value with sane amount of
395 u32 maxprogress = 30;
396 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
398 float value_f = (float)toolprogress / (float)maxprogress;
399 std::ostringstream os;
400 os<<basename<<"^[progressbar"<<value_f;
402 return g_texturesource->getTextureRaw(os.str());
405 std::string getText()
409 /*std::ostringstream os;
413 for(i=0; i<(65535-m_wear)/d; i++)
419 /*std::ostringstream os;
428 std::string getToolName()
436 // Returns true if weared out
437 bool addWear(u16 add)
439 if(m_wear >= 65535 - add)
451 std::string m_toolname;
458 InventoryList(std::string name, u32 size);
461 void serialize(std::ostream &os) const;
462 void deSerialize(std::istream &is);
464 InventoryList(const InventoryList &other);
465 InventoryList & operator = (const InventoryList &other);
467 const std::string &getName() const;
473 /*bool getDirty(){ return m_dirty; }
474 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
476 // Get pointer to item
477 const InventoryItem * getItem(u32 i) const;
478 InventoryItem * getItem(u32 i);
479 // Returns old item (or NULL). Parameter can be NULL.
480 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
482 void deleteItem(u32 i);
484 // Adds an item to a suitable place. Returns leftover item.
485 // If all went into the list, returns NULL.
486 InventoryItem * addItem(InventoryItem *newitem);
488 // If possible, adds item to given slot.
489 // If cannot be added at all, returns the item back.
490 // If can be added partly, decremented item is returned back.
491 // If can be added fully, NULL is returned.
492 InventoryItem * addItem(u32 i, InventoryItem *newitem);
494 // Checks whether the item could be added to the given slot
495 bool itemFits(const u32 i, const InventoryItem *newitem);
497 // Checks whether there is room for a given item
498 bool roomForItem(const InventoryItem *item);
500 // Checks whether there is room for a given item aftr it has been cooked
501 bool roomForCookedItem(const InventoryItem *item);
503 // Takes some items from a slot.
504 // If there are not enough, takes as many as it can.
505 // Returns NULL if couldn't take any.
506 InventoryItem * takeItem(u32 i, u32 count);
508 // Decrements amount of every material item
509 void decrementMaterials(u16 count);
511 void print(std::ostream &o);
514 core::array<InventoryItem*> m_items;
528 Inventory(const Inventory &other);
529 Inventory & operator = (const Inventory &other);
531 void serialize(std::ostream &os) const;
532 void deSerialize(std::istream &is);
534 InventoryList * addList(const std::string &name, u32 size);
535 InventoryList * getList(const std::string &name);
536 const InventoryList * getList(const std::string &name) const;
537 bool deleteList(const std::string &name);
538 // A shorthand for adding items.
539 // Returns NULL if the item was fully added, leftover otherwise.
540 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
542 InventoryList *list = getList(listname);
545 return list->addItem(newitem);
550 const s32 getListIndex(const std::string &name) const;
552 core::array<InventoryList*> m_lists;
557 struct InventoryContext
559 Player *current_player;
566 struct InventoryAction;
568 class InventoryManager
572 virtual ~InventoryManager(){}
575 Get a pointer to an inventory specified by id.
580 virtual Inventory* getInventory(InventoryContext *c, std::string id)
582 // Used on the server by InventoryAction::apply and other stuff
583 virtual void inventoryModified(InventoryContext *c, std::string id)
585 // Used on the client
586 virtual void inventoryAction(InventoryAction *a)
590 #define IACTION_MOVE 0
592 struct InventoryAction
594 static InventoryAction * deSerialize(std::istream &is);
596 virtual u16 getType() const = 0;
597 virtual void serialize(std::ostream &os) const = 0;
598 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
601 struct IMoveAction : public InventoryAction
603 // count=0 means "everything"
605 std::string from_inv;
606 std::string from_list;
618 IMoveAction(std::istream &is)
622 std::getline(is, ts, ' ');
625 std::getline(is, from_inv, ' ');
627 std::getline(is, from_list, ' ');
629 std::getline(is, ts, ' ');
632 std::getline(is, to_inv, ' ');
634 std::getline(is, to_list, ' ');
636 std::getline(is, ts, ' ');
645 void serialize(std::ostream &os) const
657 void apply(InventoryContext *c, InventoryManager *mgr);
661 Craft checking system
675 enum ItemSpecType type;
676 // Only other one of these is used
684 ItemSpec(enum ItemSpecType a_type, std::string a_name):
690 ItemSpec(enum ItemSpecType a_type, u16 a_num):
697 bool checkItem(const InventoryItem *item) const;
701 items: a pointer to an array of 9 pointers to items
702 specs: a pointer to an array of 9 ItemSpecs
704 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);