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) = 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)
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)
215 size_t t = m_inventorystring.find('|');
216 if(t == std::string::npos)
218 m_inventorystring[t] = '?';
222 os<<m_inventorystring;
225 virtual InventoryItem* clone()
227 return new MapBlockObjectItem(m_inventorystring);
231 video::ITexture * getImage();
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)
277 virtual InventoryItem* clone()
279 return new CraftItem(m_subname, m_count);
282 video::ITexture * getImage();
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)
354 virtual InventoryItem* clone()
356 return new ToolItem(m_toolname, m_wear);
359 video::ITexture * getImage()
361 if(g_texturesource == NULL)
364 std::string basename;
365 if(m_toolname == "WPick")
366 basename = "tool_woodpick.png";
367 else if(m_toolname == "STPick")
368 basename = "tool_stonepick.png";
369 else if(m_toolname == "SteelPick")
370 basename = "tool_steelpick.png";
371 else if(m_toolname == "MesePick")
372 basename = "tool_mesepick.png";
373 else if(m_toolname == "WShovel")
374 basename = "tool_woodshovel.png";
375 else if(m_toolname == "STShovel")
376 basename = "tool_stoneshovel.png";
377 else if(m_toolname == "SteelShovel")
378 basename = "tool_steelshovel.png";
379 else if(m_toolname == "WAxe")
380 basename = "tool_woodaxe.png";
381 else if(m_toolname == "STAxe")
382 basename = "tool_stoneaxe.png";
383 else if(m_toolname == "SteelAxe")
384 basename = "tool_steelaxe.png";
385 else if(m_toolname == "WSword")
386 basename = "tool_woodsword.png";
387 else if(m_toolname == "STSword")
388 basename = "tool_stonesword.png";
389 else if(m_toolname == "SteelSword")
390 basename = "tool_steelsword.png";
392 basename = "cloud.png";
395 Calculate a progress value with sane amount of
398 u32 maxprogress = 30;
399 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
401 float value_f = (float)toolprogress / (float)maxprogress;
402 std::ostringstream os;
403 os<<basename<<"^[progressbar"<<value_f;
405 return g_texturesource->getTextureRaw(os.str());
408 std::string getText()
412 /*std::ostringstream os;
416 for(i=0; i<(65535-m_wear)/d; i++)
422 /*std::ostringstream os;
431 std::string getToolName()
439 // Returns true if weared out
440 bool addWear(u16 add)
442 if(m_wear >= 65535 - add)
454 std::string m_toolname;
461 InventoryList(std::string name, u32 size);
464 void serialize(std::ostream &os);
465 void deSerialize(std::istream &is);
467 InventoryList(const InventoryList &other);
468 InventoryList & operator = (const InventoryList &other);
470 const std::string &getName() const;
476 /*bool getDirty(){ return m_dirty; }
477 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
479 // Get pointer to item
480 const InventoryItem * getItem(u32 i) const;
481 InventoryItem * getItem(u32 i);
482 // Returns old item (or NULL). Parameter can be NULL.
483 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
485 void deleteItem(u32 i);
487 // Adds an item to a suitable place. Returns leftover item.
488 // If all went into the list, returns NULL.
489 InventoryItem * addItem(InventoryItem *newitem);
491 // If possible, adds item to given slot.
492 // If cannot be added at all, returns the item back.
493 // If can be added partly, decremented item is returned back.
494 // If can be added fully, NULL is returned.
495 InventoryItem * addItem(u32 i, InventoryItem *newitem);
497 // Checks whether the item could be added to the given slot
498 bool itemFits(u32 i, InventoryItem *newitem);
500 // Takes some items from a slot.
501 // If there are not enough, takes as many as it can.
502 // Returns NULL if couldn't take any.
503 InventoryItem * takeItem(u32 i, u32 count);
505 // Decrements amount of every material item
506 void decrementMaterials(u16 count);
508 void print(std::ostream &o);
511 core::array<InventoryItem*> m_items;
525 Inventory(const Inventory &other);
526 Inventory & operator = (const Inventory &other);
528 void serialize(std::ostream &os);
529 void deSerialize(std::istream &is);
531 InventoryList * addList(const std::string &name, u32 size);
532 InventoryList * getList(const std::string &name);
533 const InventoryList * getList(const std::string &name) const;
534 bool deleteList(const std::string &name);
535 // A shorthand for adding items.
536 // Returns NULL if the item was fully added, leftover otherwise.
537 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
539 InventoryList *list = getList(listname);
542 return list->addItem(newitem);
547 const s32 getListIndex(const std::string &name) const;
549 core::array<InventoryList*> m_lists;
554 struct InventoryContext
556 Player *current_player;
563 struct InventoryAction;
565 class InventoryManager
569 virtual ~InventoryManager(){}
572 Get a pointer to an inventory specified by id.
577 virtual Inventory* getInventory(InventoryContext *c, std::string id)
579 // Used on the server by InventoryAction::apply and other stuff
580 virtual void inventoryModified(InventoryContext *c, std::string id)
582 // Used on the client
583 virtual void inventoryAction(InventoryAction *a)
587 #define IACTION_MOVE 0
589 struct InventoryAction
591 static InventoryAction * deSerialize(std::istream &is);
593 virtual u16 getType() const = 0;
594 virtual void serialize(std::ostream &os) = 0;
595 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
598 struct IMoveAction : public InventoryAction
600 // count=0 means "everything"
602 std::string from_inv;
603 std::string from_list;
615 IMoveAction(std::istream &is)
619 std::getline(is, ts, ' ');
622 std::getline(is, from_inv, ' ');
624 std::getline(is, from_list, ' ');
626 std::getline(is, ts, ' ');
629 std::getline(is, to_inv, ' ');
631 std::getline(is, to_list, ' ');
633 std::getline(is, ts, ' ');
642 void serialize(std::ostream &os)
654 void apply(InventoryContext *c, InventoryManager *mgr);
658 Craft checking system
672 enum ItemSpecType type;
673 // Only other one of these is used
681 ItemSpec(enum ItemSpecType a_type, std::string a_name):
687 ItemSpec(enum ItemSpecType a_type, u16 a_num):
694 bool checkItem(const InventoryItem *item) const;
698 items: a pointer to an array of 9 pointers to items
699 specs: a pointer to an array of 9 ItemSpecs
701 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);