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(){ return getCount(); }
70 // Shall return true if the item can be add()ed to the other
71 virtual bool addableTo(InventoryItem *other)
80 void setCount(u16 count)
84 // This should return something else for stackable items
85 virtual u16 freeSpace()
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(){return false;}
107 virtual float getCookTime(){return 3.0;}
108 // Result of cooking (can randomize)
109 virtual InventoryItem *createCookResult(){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(InventoryItem *other)
165 if(std::string(other->getName()) != "MaterialItem")
167 MaterialItem *m = (MaterialItem*)other;
168 if(m->getMaterial() != m_content)
174 if(m_count > QUANTITY_ITEM_MAX_COUNT)
176 return QUANTITY_ITEM_MAX_COUNT - m_count;
182 InventoryItem *createCookResult();
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);
294 virtual bool addableTo(InventoryItem *other)
296 if(std::string(other->getName()) != "CraftItem")
298 CraftItem *m = (CraftItem*)other;
299 if(m->m_subname != m_subname)
305 if(m_count > QUANTITY_ITEM_MAX_COUNT)
307 return QUANTITY_ITEM_MAX_COUNT - m_count;
315 InventoryItem *createCookResult();
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 std::string getName();
476 /*bool getDirty(){ return m_dirty; }
477 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
479 // Get pointer to item
480 InventoryItem * getItem(u32 i);
481 // Returns old item (or NULL). Parameter can be NULL.
482 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
484 void deleteItem(u32 i);
486 // Adds an item to a suitable place. Returns leftover item.
487 // If all went into the list, returns NULL.
488 InventoryItem * addItem(InventoryItem *newitem);
490 // If possible, adds item to given slot.
491 // If cannot be added at all, returns the item back.
492 // If can be added partly, decremented item is returned back.
493 // If can be added fully, NULL is returned.
494 InventoryItem * addItem(u32 i, InventoryItem *newitem);
496 // Checks whether the item could be added to the given slot
497 bool itemFits(u32 i, InventoryItem *newitem);
499 // Takes some items from a slot.
500 // If there are not enough, takes as many as it can.
501 // Returns NULL if couldn't take any.
502 InventoryItem * takeItem(u32 i, u32 count);
504 // Decrements amount of every material item
505 void decrementMaterials(u16 count);
507 void print(std::ostream &o);
510 core::array<InventoryItem*> m_items;
524 Inventory(const Inventory &other);
525 Inventory & operator = (const Inventory &other);
527 void serialize(std::ostream &os);
528 void deSerialize(std::istream &is);
530 InventoryList * addList(const std::string &name, u32 size);
531 InventoryList * getList(const std::string &name);
532 bool deleteList(const std::string &name);
533 // A shorthand for adding items.
534 // Returns NULL if the item was fully added, leftover otherwise.
535 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
537 InventoryList *list = getList(listname);
540 return list->addItem(newitem);
545 s32 getListIndex(const std::string &name);
547 core::array<InventoryList*> m_lists;
552 struct InventoryContext
554 Player *current_player;
561 class InventoryAction;
563 class InventoryManager
567 virtual ~InventoryManager(){}
570 Get a pointer to an inventory specified by id.
575 virtual Inventory* getInventory(InventoryContext *c, std::string id)
577 // Used on the server by InventoryAction::apply and other stuff
578 virtual void inventoryModified(InventoryContext *c, std::string id)
580 // Used on the client
581 virtual void inventoryAction(InventoryAction *a)
585 #define IACTION_MOVE 0
587 struct InventoryAction
589 static InventoryAction * deSerialize(std::istream &is);
591 virtual u16 getType() const = 0;
592 virtual void serialize(std::ostream &os) = 0;
593 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
596 struct IMoveAction : public InventoryAction
598 // count=0 means "everything"
600 std::string from_inv;
601 std::string from_list;
613 IMoveAction(std::istream &is)
617 std::getline(is, ts, ' ');
620 std::getline(is, from_inv, ' ');
622 std::getline(is, from_list, ' ');
624 std::getline(is, ts, ' ');
627 std::getline(is, to_inv, ' ');
629 std::getline(is, to_list, ' ');
631 std::getline(is, ts, ' ');
640 void serialize(std::ostream &os)
652 void apply(InventoryContext *c, InventoryManager *mgr);
656 Craft checking system
670 enum ItemSpecType type;
671 // Only other one of these is used
679 ItemSpec(enum ItemSpecType a_type, std::string a_name):
685 ItemSpec(enum ItemSpecType a_type, u16 a_num):
692 bool checkItem(InventoryItem *item);
696 items: a pointer to an array of 9 pointers to items
697 specs: a pointer to an array of 9 ItemSpecs
699 bool checkItemCombination(InventoryItem **items, ItemSpec *specs);