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() { return ""; }
58 // Shall return an image of the item (or NULL)
59 virtual video::ITexture * getImageRaw() { return NULL; }
60 // Shall return an image to show in the GUI (or NULL)
61 virtual video::ITexture * getImage() { return NULL; }
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()
158 return content_features(m_content).inventory_texture;
162 std::string getText()
164 std::ostringstream os;
169 virtual bool addableTo(const InventoryItem *other) const
171 if(std::string(other->getName()) != "MaterialItem")
173 MaterialItem *m = (MaterialItem*)other;
174 if(m->getMaterial() != m_content)
178 u16 freeSpace() const
180 if(m_count > QUANTITY_ITEM_MAX_COUNT)
182 return QUANTITY_ITEM_MAX_COUNT - m_count;
187 bool isCookable() const;
188 InventoryItem *createCookResult() const;
192 content_t getMaterial()
201 class MapBlockObjectItem : public InventoryItem
204 MapBlockObjectItem(std::string inventorystring):
207 m_inventorystring = inventorystring;
211 Implementation interface
213 virtual const char* getName() const
217 virtual void serialize(std::ostream &os) const
219 std::string sane_string(m_inventorystring);
220 str_replace_char(sane_string, '|', '?');
226 virtual InventoryItem* clone()
228 return new MapBlockObjectItem(m_inventorystring);
232 video::ITexture * getImage();
234 std::string getText();
239 std::string getInventoryString()
241 return m_inventorystring;
244 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
247 std::string m_inventorystring;
251 An item that is used as a mid-product when crafting.
255 class CraftItem : public InventoryItem
258 CraftItem(std::string subname, u16 count):
264 Implementation interface
266 virtual const char* getName() const
270 virtual void serialize(std::ostream &os) const
278 virtual InventoryItem* clone()
280 return new CraftItem(m_subname, m_count);
283 video::ITexture * getImage();
285 std::string getText()
287 std::ostringstream os;
292 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
293 u16 getDropCount() const;
295 virtual bool addableTo(const InventoryItem *other) const
297 if(std::string(other->getName()) != "CraftItem")
299 CraftItem *m = (CraftItem*)other;
300 if(m->m_subname != m_subname)
304 u16 freeSpace() const
306 if(m_count > QUANTITY_ITEM_MAX_COUNT)
308 return QUANTITY_ITEM_MAX_COUNT - m_count;
315 bool isCookable() const;
316 InventoryItem *createCookResult() const;
318 bool use(ServerEnvironment *env, Player *player);
323 std::string getSubName()
328 std::string m_subname;
331 class ToolItem : public InventoryItem
334 ToolItem(std::string toolname, u16 wear):
337 m_toolname = toolname;
341 Implementation interface
343 virtual const char* getName() const
347 virtual void serialize(std::ostream &os) const
355 virtual InventoryItem* clone()
357 return new ToolItem(m_toolname, m_wear);
360 std::string getBasename() {
361 if(m_toolname == "WPick")
362 return "tool_woodpick.png";
363 else if(m_toolname == "STPick")
364 return "tool_stonepick.png";
365 else if(m_toolname == "SteelPick")
366 return "tool_steelpick.png";
367 else if(m_toolname == "MesePick")
368 return "tool_mesepick.png";
369 else if(m_toolname == "WShovel")
370 return "tool_woodshovel.png";
371 else if(m_toolname == "STShovel")
372 return "tool_stoneshovel.png";
373 else if(m_toolname == "SteelShovel")
374 return "tool_steelshovel.png";
375 else if(m_toolname == "WAxe")
376 return "tool_woodaxe.png";
377 else if(m_toolname == "STAxe")
378 return "tool_stoneaxe.png";
379 else if(m_toolname == "SteelAxe")
380 return "tool_steelaxe.png";
381 else if(m_toolname == "WSword")
382 return "tool_woodsword.png";
383 else if(m_toolname == "STSword")
384 return "tool_stonesword.png";
385 else if(m_toolname == "SteelSword")
386 return "tool_steelsword.png";
391 video::ITexture * getImageRaw()
393 if(g_texturesource == NULL)
396 return g_texturesource->getTextureRaw(getBasename());
399 video::ITexture * getImage()
401 if(g_texturesource == NULL)
404 std::string basename = getBasename();
406 return g_texturesource->getTextureRaw(basename);
409 Calculate a progress value with sane amount of
412 u32 maxprogress = 30;
413 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
415 float value_f = (float)toolprogress / (float)maxprogress;
416 std::ostringstream os;
417 os<<basename<<"^[progressbar"<<value_f;
419 return g_texturesource->getTextureRaw(os.str());
422 std::string getText()
426 /*std::ostringstream os;
430 for(i=0; i<(65535-m_wear)/d; i++)
436 /*std::ostringstream os;
445 std::string getToolName()
453 // Returns true if weared out
454 bool addWear(u16 add)
456 if(m_wear >= 65535 - add)
468 std::string m_toolname;
475 InventoryList(std::string name, u32 size);
478 void serialize(std::ostream &os) const;
479 void deSerialize(std::istream &is);
481 InventoryList(const InventoryList &other);
482 InventoryList & operator = (const InventoryList &other);
484 const std::string &getName() const;
490 /*bool getDirty(){ return m_dirty; }
491 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
493 // Get pointer to item
494 const InventoryItem * getItem(u32 i) const;
495 InventoryItem * getItem(u32 i);
496 // Returns old item (or NULL). Parameter can be NULL.
497 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
499 void deleteItem(u32 i);
501 // Adds an item to a suitable place. Returns leftover item.
502 // If all went into the list, returns NULL.
503 InventoryItem * addItem(InventoryItem *newitem);
505 // If possible, adds item to given slot.
506 // If cannot be added at all, returns the item back.
507 // If can be added partly, decremented item is returned back.
508 // If can be added fully, NULL is returned.
509 InventoryItem * addItem(u32 i, InventoryItem *newitem);
511 // Checks whether the item could be added to the given slot
512 bool itemFits(const u32 i, const InventoryItem *newitem);
514 // Checks whether there is room for a given item
515 bool roomForItem(const InventoryItem *item);
517 // Checks whether there is room for a given item aftr it has been cooked
518 bool roomForCookedItem(const InventoryItem *item);
520 // Takes some items from a slot.
521 // If there are not enough, takes as many as it can.
522 // Returns NULL if couldn't take any.
523 InventoryItem * takeItem(u32 i, u32 count);
525 // Decrements amount of every material item
526 void decrementMaterials(u16 count);
528 void print(std::ostream &o);
531 core::array<InventoryItem*> m_items;
545 Inventory(const Inventory &other);
546 Inventory & operator = (const Inventory &other);
548 void serialize(std::ostream &os) const;
549 void deSerialize(std::istream &is);
551 InventoryList * addList(const std::string &name, u32 size);
552 InventoryList * getList(const std::string &name);
553 const InventoryList * getList(const std::string &name) const;
554 bool deleteList(const std::string &name);
555 // A shorthand for adding items.
556 // Returns NULL if the item was fully added, leftover otherwise.
557 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
559 InventoryList *list = getList(listname);
562 return list->addItem(newitem);
567 const s32 getListIndex(const std::string &name) const;
569 core::array<InventoryList*> m_lists;
574 struct InventoryContext
576 Player *current_player;
583 struct InventoryAction;
585 class InventoryManager
589 virtual ~InventoryManager(){}
592 Get a pointer to an inventory specified by id.
597 virtual Inventory* getInventory(InventoryContext *c, std::string id)
599 // Used on the server by InventoryAction::apply and other stuff
600 virtual void inventoryModified(InventoryContext *c, std::string id)
602 // Used on the client
603 virtual void inventoryAction(InventoryAction *a)
607 #define IACTION_MOVE 0
609 struct InventoryAction
611 static InventoryAction * deSerialize(std::istream &is);
613 virtual u16 getType() const = 0;
614 virtual void serialize(std::ostream &os) const = 0;
615 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
618 struct IMoveAction : public InventoryAction
620 // count=0 means "everything"
622 std::string from_inv;
623 std::string from_list;
635 IMoveAction(std::istream &is)
639 std::getline(is, ts, ' ');
642 std::getline(is, from_inv, ' ');
644 std::getline(is, from_list, ' ');
646 std::getline(is, ts, ' ');
649 std::getline(is, to_inv, ' ');
651 std::getline(is, to_list, ' ');
653 std::getline(is, ts, ' ');
662 void serialize(std::ostream &os) const
674 void apply(InventoryContext *c, InventoryManager *mgr);
678 Craft checking system
692 enum ItemSpecType type;
693 // Only other one of these is used
701 ItemSpec(enum ItemSpecType a_type, std::string a_name):
707 ItemSpec(enum ItemSpecType a_type, u16 a_num):
714 bool checkItem(const InventoryItem *item) const;
718 items: a pointer to an array of 9 pointers to items
719 specs: a pointer to an array of 9 ItemSpecs
721 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);