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"
36 #define QUANTITY_ITEM_MAX_COUNT 99
38 class ServerActiveObject;
39 class ServerEnvironment;
44 InventoryItem(u16 count);
45 virtual ~InventoryItem();
47 static InventoryItem* deSerialize(std::istream &is);
49 virtual const char* getName() const = 0;
50 // Shall write the name and the parameters
51 virtual void serialize(std::ostream &os) = 0;
52 // Shall make an exact clone of the item
53 virtual InventoryItem* clone() = 0;
55 // Shall return an image to show in the GUI (or NULL)
56 virtual video::ITexture * getImage() { return NULL; }
58 // Shall return a text to show in the GUI
59 virtual std::string getText() { return ""; }
60 // Creates an object from the item, to be placed in the world.
61 virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
62 // Gets amount of items that dropping one SAO will decrement
63 virtual u16 getDropCount(){ return getCount(); }
69 // Shall return true if the item can be add()ed to the other
70 virtual bool addableTo(InventoryItem *other)
79 void setCount(u16 count)
83 // This should return something else for stackable items
84 virtual u16 freeSpace()
90 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
93 void remove(u16 count)
95 assert(m_count >= count);
102 // Whether it can be cooked
103 virtual bool isCookable(){return false;}
105 virtual float getCookTime(){return 3.0;}
107 virtual InventoryItem *createCookResult(){return NULL;}
113 class MaterialItem : public InventoryItem
116 MaterialItem(u8 content, u16 count):
122 Implementation interface
124 virtual const char* getName() const
126 return "MaterialItem";
128 virtual void serialize(std::ostream &os)
130 //os.imbue(std::locale("C"));
133 os<<(unsigned int)m_content;
137 virtual InventoryItem* clone()
139 return new MaterialItem(m_content, m_count);
142 video::ITexture * getImage()
144 return content_features(m_content).inventory_texture;
148 std::string getText()
150 std::ostringstream os;
155 virtual bool addableTo(InventoryItem *other)
157 if(std::string(other->getName()) != "MaterialItem")
159 MaterialItem *m = (MaterialItem*)other;
160 if(m->getMaterial() != m_content)
166 if(m_count > QUANTITY_ITEM_MAX_COUNT)
168 return QUANTITY_ITEM_MAX_COUNT - m_count;
174 InventoryItem *createCookResult();
187 class MapBlockObjectItem : public InventoryItem
190 MapBlockObjectItem(std::string inventorystring):
193 m_inventorystring = inventorystring;
197 Implementation interface
199 virtual const char* getName() const
203 virtual void serialize(std::ostream &os)
207 size_t t = m_inventorystring.find('|');
208 if(t == std::string::npos)
210 m_inventorystring[t] = '?';
214 os<<m_inventorystring;
217 virtual InventoryItem* clone()
219 return new MapBlockObjectItem(m_inventorystring);
223 video::ITexture * getImage();
225 std::string getText();
230 std::string getInventoryString()
232 return m_inventorystring;
235 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
238 std::string m_inventorystring;
242 An item that is used as a mid-product when crafting.
246 class CraftItem : public InventoryItem
249 CraftItem(std::string subname, u16 count):
255 Implementation interface
257 virtual const char* getName() const
261 virtual void serialize(std::ostream &os)
269 virtual InventoryItem* clone()
271 return new CraftItem(m_subname, m_count);
274 video::ITexture * getImage();
276 std::string getText()
278 std::ostringstream os;
283 ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
286 virtual bool addableTo(InventoryItem *other)
288 if(std::string(other->getName()) != "CraftItem")
290 CraftItem *m = (CraftItem*)other;
291 if(m->m_subname != m_subname)
297 if(m_count > QUANTITY_ITEM_MAX_COUNT)
299 return QUANTITY_ITEM_MAX_COUNT - m_count;
305 InventoryItem *createCookResult();
309 std::string getSubName()
314 std::string m_subname;
317 class ToolItem : public InventoryItem
320 ToolItem(std::string toolname, u16 wear):
323 m_toolname = toolname;
327 Implementation interface
329 virtual const char* getName() const
333 virtual void serialize(std::ostream &os)
341 virtual InventoryItem* clone()
343 return new ToolItem(m_toolname, m_wear);
346 video::ITexture * getImage()
348 if(g_texturesource == NULL)
351 std::string basename;
352 if(m_toolname == "WPick")
353 basename = "tool_woodpick.png";
354 else if(m_toolname == "STPick")
355 basename = "tool_stonepick.png";
356 else if(m_toolname == "SteelPick")
357 basename = "tool_steelpick.png";
358 else if(m_toolname == "MesePick")
359 basename = "tool_mesepick.png";
360 else if(m_toolname == "WShovel")
361 basename = "tool_woodshovel.png";
362 else if(m_toolname == "STShovel")
363 basename = "tool_stoneshovel.png";
364 else if(m_toolname == "SteelShovel")
365 basename = "tool_steelshovel.png";
366 else if(m_toolname == "WAxe")
367 basename = "tool_woodaxe.png";
368 else if(m_toolname == "STAxe")
369 basename = "tool_stoneaxe.png";
370 else if(m_toolname == "SteelAxe")
371 basename = "tool_steelaxe.png";
372 else if(m_toolname == "WSword")
373 basename = "tool_woodsword.png";
374 else if(m_toolname == "STSword")
375 basename = "tool_stonesword.png";
376 else if(m_toolname == "SteelSword")
377 basename = "tool_steelsword.png";
379 basename = "cloud.png";
382 Calculate a progress value with sane amount of
385 u32 maxprogress = 30;
386 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
388 float value_f = (float)toolprogress / (float)maxprogress;
389 std::ostringstream os;
390 os<<basename<<"^[progressbar"<<value_f;
392 return g_texturesource->getTextureRaw(os.str());
395 std::string getText()
399 /*std::ostringstream os;
403 for(i=0; i<(65535-m_wear)/d; i++)
409 /*std::ostringstream os;
418 std::string getToolName()
426 // Returns true if weared out
427 bool addWear(u16 add)
429 if(m_wear >= 65535 - add)
441 std::string m_toolname;
448 InventoryList(std::string name, u32 size);
451 void serialize(std::ostream &os);
452 void deSerialize(std::istream &is);
454 InventoryList(const InventoryList &other);
455 InventoryList & operator = (const InventoryList &other);
457 std::string getName();
463 /*bool getDirty(){ return m_dirty; }
464 void setDirty(bool dirty=true){ m_dirty = dirty; }*/
466 // Get pointer to item
467 InventoryItem * getItem(u32 i);
468 // Returns old item (or NULL). Parameter can be NULL.
469 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
471 void deleteItem(u32 i);
473 // Adds an item to a suitable place. Returns leftover item.
474 // If all went into the list, returns NULL.
475 InventoryItem * addItem(InventoryItem *newitem);
477 // If possible, adds item to given slot.
478 // If cannot be added at all, returns the item back.
479 // If can be added partly, decremented item is returned back.
480 // If can be added fully, NULL is returned.
481 InventoryItem * addItem(u32 i, InventoryItem *newitem);
483 // Checks whether the item could be added to the given slot
484 bool itemFits(u32 i, InventoryItem *newitem);
486 // Takes some items from a slot.
487 // If there are not enough, takes as many as it can.
488 // Returns NULL if couldn't take any.
489 InventoryItem * takeItem(u32 i, u32 count);
491 // Decrements amount of every material item
492 void decrementMaterials(u16 count);
494 void print(std::ostream &o);
497 core::array<InventoryItem*> m_items;
511 Inventory(const Inventory &other);
512 Inventory & operator = (const Inventory &other);
514 void serialize(std::ostream &os);
515 void deSerialize(std::istream &is);
517 InventoryList * addList(const std::string &name, u32 size);
518 InventoryList * getList(const std::string &name);
519 bool deleteList(const std::string &name);
520 // A shorthand for adding items.
521 // Returns NULL if the item was fully added, leftover otherwise.
522 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
524 InventoryList *list = getList(listname);
527 return list->addItem(newitem);
532 s32 getListIndex(const std::string &name);
534 core::array<InventoryList*> m_lists;
539 struct InventoryContext
541 Player *current_player;
548 class InventoryAction;
550 class InventoryManager
554 virtual ~InventoryManager(){}
557 Get a pointer to an inventory specified by id.
562 virtual Inventory* getInventory(InventoryContext *c, std::string id)
564 // Used on the server by InventoryAction::apply and other stuff
565 virtual void inventoryModified(InventoryContext *c, std::string id)
567 // Used on the client
568 virtual void inventoryAction(InventoryAction *a)
572 #define IACTION_MOVE 0
574 struct InventoryAction
576 static InventoryAction * deSerialize(std::istream &is);
578 virtual u16 getType() const = 0;
579 virtual void serialize(std::ostream &os) = 0;
580 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
583 struct IMoveAction : public InventoryAction
585 // count=0 means "everything"
587 std::string from_inv;
588 std::string from_list;
600 IMoveAction(std::istream &is)
604 std::getline(is, ts, ' ');
607 std::getline(is, from_inv, ' ');
609 std::getline(is, from_list, ' ');
611 std::getline(is, ts, ' ');
614 std::getline(is, to_inv, ' ');
616 std::getline(is, to_list, ' ');
618 std::getline(is, ts, ' ');
627 void serialize(std::ostream &os)
639 void apply(InventoryContext *c, InventoryManager *mgr);
643 Craft checking system
657 enum ItemSpecType type;
658 // Only other one of these is used
666 ItemSpec(enum ItemSpecType a_type, std::string a_name):
672 ItemSpec(enum ItemSpecType a_type, u16 a_num):
679 bool checkItem(InventoryItem *item);
683 items: a pointer to an array of 9 pointers to items
684 specs: a pointer to an array of 9 ItemSpecs
686 bool checkItemCombination(InventoryItem **items, ItemSpec *specs);