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 "main.h" // For g_materials
33 #include "mapnode.h" // For content_t
35 #define QUANTITY_ITEM_MAX_COUNT 99
37 class ServerActiveObject;
38 class ServerEnvironment;
43 InventoryItem(u16 count);
44 virtual ~InventoryItem();
46 static InventoryItem* deSerialize(std::istream &is);
48 virtual const char* getName() const = 0;
49 // Shall write the name and the parameters
50 virtual void serialize(std::ostream &os) const = 0;
51 // Shall make an exact clone of the item
52 virtual InventoryItem* clone() = 0;
54 // Return the name of the image for this item
55 virtual std::string getBasename() const { return ""; }
56 // Shall return an image of the item (or NULL)
57 virtual video::ITexture * getImage() const { return NULL; }
58 // Shall return an image of the item without embellishments (or NULL)
59 virtual video::ITexture * getImageRaw() const { return getImage(); }
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 ServerActiveObject *user){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() const;
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 An item that is used as a mid-product when crafting.
199 class CraftItem : public InventoryItem
202 CraftItem(std::string subname, u16 count):
208 Implementation interface
210 virtual const char* getName() const
214 virtual void serialize(std::ostream &os) const
222 virtual InventoryItem* clone()
224 return new CraftItem(m_subname, m_count);
227 video::ITexture * getImage() const;
229 std::string getText()
231 std::ostringstream os;
236 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
237 u16 getDropCount() const;
239 virtual bool addableTo(const InventoryItem *other) const
241 if(std::string(other->getName()) != "CraftItem")
243 CraftItem *m = (CraftItem*)other;
244 if(m->m_subname != m_subname)
248 u16 freeSpace() const
250 if(m_count > QUANTITY_ITEM_MAX_COUNT)
252 return QUANTITY_ITEM_MAX_COUNT - m_count;
259 bool isCookable() const;
260 InventoryItem *createCookResult() const;
262 bool use(ServerEnvironment *env, ServerActiveObject *user);
267 std::string getSubName()
272 std::string m_subname;
275 class ToolItem : public InventoryItem
278 ToolItem(std::string toolname, u16 wear):
281 m_toolname = toolname;
285 Implementation interface
287 virtual const char* getName() const
291 virtual void serialize(std::ostream &os) const
299 virtual InventoryItem* clone()
301 return new ToolItem(m_toolname, m_wear);
304 std::string getBasename() const {
305 if(m_toolname == "WPick")
306 return "tool_woodpick.png";
307 else if(m_toolname == "STPick")
308 return "tool_stonepick.png";
309 else if(m_toolname == "SteelPick")
310 return "tool_steelpick.png";
311 else if(m_toolname == "MesePick")
312 return "tool_mesepick.png";
313 else if(m_toolname == "WShovel")
314 return "tool_woodshovel.png";
315 else if(m_toolname == "STShovel")
316 return "tool_stoneshovel.png";
317 else if(m_toolname == "SteelShovel")
318 return "tool_steelshovel.png";
319 else if(m_toolname == "WAxe")
320 return "tool_woodaxe.png";
321 else if(m_toolname == "STAxe")
322 return "tool_stoneaxe.png";
323 else if(m_toolname == "SteelAxe")
324 return "tool_steelaxe.png";
325 else if(m_toolname == "WSword")
326 return "tool_woodsword.png";
327 else if(m_toolname == "STSword")
328 return "tool_stonesword.png";
329 else if(m_toolname == "SteelSword")
330 return "tool_steelsword.png";
335 video::ITexture * getImage() const
337 if(g_texturesource == NULL)
340 std::string basename = getBasename();
343 Calculate a progress value with sane amount of
346 u32 maxprogress = 30;
347 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
349 float value_f = (float)toolprogress / (float)maxprogress;
350 std::ostringstream os;
351 os<<basename<<"^[progressbar"<<value_f;
353 return g_texturesource->getTextureRaw(os.str());
356 video::ITexture * getImageRaw() const
358 if(g_texturesource == NULL)
361 return g_texturesource->getTextureRaw(getBasename());
364 std::string getText()
368 /*std::ostringstream os;
372 for(i=0; i<(65535-m_wear)/d; i++)
378 /*std::ostringstream os;
387 std::string getToolName()
395 // Returns true if weared out
396 bool addWear(u16 add)
398 if(m_wear >= 65535 - add)
410 std::string m_toolname;
417 InventoryList(std::string name, u32 size);
420 void serialize(std::ostream &os) const;
421 void deSerialize(std::istream &is);
423 InventoryList(const InventoryList &other);
424 InventoryList & operator = (const InventoryList &other);
426 const std::string &getName() const;
432 /*bool getDirty(){ return m_dirty; }
433 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
435 // Get pointer to item
436 const InventoryItem * getItem(u32 i) const;
437 InventoryItem * getItem(u32 i);
438 // Returns old item (or NULL). Parameter can be NULL.
439 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
441 void deleteItem(u32 i);
443 // Adds an item to a suitable place. Returns leftover item.
444 // If all went into the list, returns NULL.
445 InventoryItem * addItem(InventoryItem *newitem);
447 // If possible, adds item to given slot.
448 // If cannot be added at all, returns the item back.
449 // If can be added partly, decremented item is returned back.
450 // If can be added fully, NULL is returned.
451 InventoryItem * addItem(u32 i, InventoryItem *newitem);
453 // Checks whether the item could be added to the given slot
454 bool itemFits(const u32 i, const InventoryItem *newitem);
456 // Checks whether there is room for a given item
457 bool roomForItem(const InventoryItem *item);
459 // Checks whether there is room for a given item aftr it has been cooked
460 bool roomForCookedItem(const InventoryItem *item);
462 // Takes some items from a slot.
463 // If there are not enough, takes as many as it can.
464 // Returns NULL if couldn't take any.
465 InventoryItem * takeItem(u32 i, u32 count);
467 // Decrements amount of every material item
468 void decrementMaterials(u16 count);
470 void print(std::ostream &o);
473 core::array<InventoryItem*> m_items;
487 Inventory(const Inventory &other);
488 Inventory & operator = (const Inventory &other);
490 void serialize(std::ostream &os) const;
491 void deSerialize(std::istream &is);
493 InventoryList * addList(const std::string &name, u32 size);
494 InventoryList * getList(const std::string &name);
495 const InventoryList * getList(const std::string &name) const;
496 bool deleteList(const std::string &name);
497 // A shorthand for adding items.
498 // Returns NULL if the item was fully added, leftover otherwise.
499 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
501 InventoryList *list = getList(listname);
504 return list->addItem(newitem);
509 const s32 getListIndex(const std::string &name) const;
511 core::array<InventoryList*> m_lists;
516 struct InventoryContext
518 Player *current_player;
525 struct InventoryAction;
527 class InventoryManager
531 virtual ~InventoryManager(){}
534 Get a pointer to an inventory specified by id.
539 virtual Inventory* getInventory(InventoryContext *c, std::string id)
541 // Used on the server by InventoryAction::apply and other stuff
542 virtual void inventoryModified(InventoryContext *c, std::string id)
544 // Used on the client
545 virtual void inventoryAction(InventoryAction *a)
549 #define IACTION_MOVE 0
551 struct InventoryAction
553 static InventoryAction * deSerialize(std::istream &is);
555 virtual u16 getType() const = 0;
556 virtual void serialize(std::ostream &os) const = 0;
557 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
560 struct IMoveAction : public InventoryAction
562 // count=0 means "everything"
564 std::string from_inv;
565 std::string from_list;
577 IMoveAction(std::istream &is)
581 std::getline(is, ts, ' ');
584 std::getline(is, from_inv, ' ');
586 std::getline(is, from_list, ' ');
588 std::getline(is, ts, ' ');
591 std::getline(is, to_inv, ' ');
593 std::getline(is, to_list, ' ');
595 std::getline(is, ts, ' ');
604 void serialize(std::ostream &os) const
616 void apply(InventoryContext *c, InventoryManager *mgr);
620 Craft checking system
634 enum ItemSpecType type;
635 // Only other one of these is used
643 ItemSpec(enum ItemSpecType a_type, std::string a_name):
649 ItemSpec(enum ItemSpecType a_type, u16 a_num):
656 bool checkItem(const InventoryItem *item) const;
660 items: a pointer to an array of 9 pointers to items
661 specs: a pointer to an array of 9 ItemSpecs
663 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);