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
41 InventoryItem(u16 count);
42 virtual ~InventoryItem();
44 static InventoryItem* deSerialize(std::istream &is);
46 virtual const char* getName() const = 0;
47 // Shall write the name and the parameters
48 virtual void serialize(std::ostream &os) = 0;
49 // Shall make an exact clone of the item
50 virtual InventoryItem* clone() = 0;
52 // Shall return an image to show in the GUI (or NULL)
53 virtual video::ITexture * getImage() { return NULL; }
55 // Shall return a text to show in the GUI
56 virtual std::string getText() { return ""; }
58 // Shall return true if the item can be add()ed to the other
59 virtual bool addableTo(InventoryItem *other)
71 void setCount(u16 count)
75 // This should return something else for stackable items
76 virtual u16 freeSpace()
82 assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
85 void remove(u16 count)
87 assert(m_count >= count);
95 virtual float getCookTime(){return 3.0;}
97 virtual InventoryItem *createCookResult(){return NULL;}
103 class MaterialItem : public InventoryItem
106 MaterialItem(u8 content, u16 count):
112 Implementation interface
114 virtual const char* getName() const
116 return "MaterialItem";
118 virtual void serialize(std::ostream &os)
120 //os.imbue(std::locale("C"));
123 os<<(unsigned int)m_content;
127 virtual InventoryItem* clone()
129 return new MaterialItem(m_content, m_count);
132 video::ITexture * getImage()
134 return content_features(m_content).inventory_texture;
138 std::string getText()
140 std::ostringstream os;
145 virtual bool addableTo(InventoryItem *other)
147 if(std::string(other->getName()) != "MaterialItem")
149 MaterialItem *m = (MaterialItem*)other;
150 if(m->getMaterial() != m_content)
156 if(m_count > QUANTITY_ITEM_MAX_COUNT)
158 return QUANTITY_ITEM_MAX_COUNT - m_count;
163 InventoryItem *createCookResult();
175 class MapBlockObjectItem : public InventoryItem
178 MapBlockObjectItem(std::string inventorystring):
181 m_inventorystring = inventorystring;
185 Implementation interface
187 virtual const char* getName() const
191 virtual void serialize(std::ostream &os)
195 size_t t = m_inventorystring.find('|');
196 if(t == std::string::npos)
198 m_inventorystring[t] = '?';
202 os<<m_inventorystring;
205 virtual InventoryItem* clone()
207 return new MapBlockObjectItem(m_inventorystring);
211 video::ITexture * getImage();
213 std::string getText();
218 std::string getInventoryString()
220 return m_inventorystring;
223 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
226 std::string m_inventorystring;
230 An item that is used as a mid-product when crafting.
234 class CraftItem : public InventoryItem
237 CraftItem(std::string subname, u16 count):
243 Implementation interface
245 virtual const char* getName() const
249 virtual void serialize(std::ostream &os)
257 virtual InventoryItem* clone()
259 return new CraftItem(m_subname, m_count);
262 video::ITexture * getImage()
264 if(g_texturesource == NULL)
269 if(m_subname == "Stick")
271 else if(m_subname == "lump_of_coal")
272 name = "lump_of_coal.png";
273 else if(m_subname == "lump_of_iron")
274 name = "lump_of_iron.png";
278 // Get such a texture
279 //return g_irrlicht->getTexture(name);
280 return g_texturesource->getTextureRaw(name);
283 std::string getText()
285 std::ostringstream os;
289 virtual bool addableTo(InventoryItem *other)
291 if(std::string(other->getName()) != "CraftItem")
293 CraftItem *m = (CraftItem*)other;
294 if(m->m_subname != m_subname)
300 if(m_count > QUANTITY_ITEM_MAX_COUNT)
302 return QUANTITY_ITEM_MAX_COUNT - m_count;
307 std::string getSubName()
312 std::string m_subname;
315 class ToolItem : public InventoryItem
318 ToolItem(std::string toolname, u16 wear):
321 m_toolname = toolname;
325 Implementation interface
327 virtual const char* getName() const
331 virtual void serialize(std::ostream &os)
339 virtual InventoryItem* clone()
341 return new ToolItem(m_toolname, m_wear);
344 video::ITexture * getImage()
346 if(g_texturesource == NULL)
349 std::string basename;
350 if(m_toolname == "WPick")
351 basename = "tool_wpick.png";
352 else if(m_toolname == "STPick")
353 basename = "tool_stpick.png";
354 else if(m_toolname == "MesePick")
355 basename = "tool_mesepick.png";
357 basename = "cloud.png";
360 Calculate a progress value with sane amount of
363 u32 maxprogress = 30;
364 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
366 float value_f = (float)toolprogress / (float)maxprogress;
367 std::ostringstream os;
368 os<<basename<<"^[progressbar"<<value_f;
370 return g_texturesource->getTextureRaw(os.str());
373 spec.addTid(g_irrlicht->getTextureId(basename));
374 spec.addTid(g_irrlicht->getTextureId(os.str()));
375 return g_irrlicht->getTexture(spec);*/
378 std::string getText()
382 /*std::ostringstream os;
386 for(i=0; i<(65535-m_wear)/d; i++)
392 /*std::ostringstream os;
401 std::string getToolName()
409 // Returns true if weared out
410 bool addWear(u16 add)
412 if(m_wear >= 65535 - add)
424 std::string m_toolname;
431 InventoryList(std::string name, u32 size);
434 void serialize(std::ostream &os);
435 void deSerialize(std::istream &is);
437 InventoryList(const InventoryList &other);
438 InventoryList & operator = (const InventoryList &other);
440 std::string getName();
446 // Get pointer to item
447 InventoryItem * getItem(u32 i);
448 // Returns old item (or NULL). Parameter can be NULL.
449 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
451 void deleteItem(u32 i);
453 // Adds an item to a suitable place. Returns leftover item.
454 // If all went into the list, returns NULL.
455 InventoryItem * addItem(InventoryItem *newitem);
457 // If possible, adds item to given slot.
458 // If cannot be added at all, returns the item back.
459 // If can be added partly, decremented item is returned back.
460 // If can be added fully, NULL is returned.
461 InventoryItem * addItem(u32 i, InventoryItem *newitem);
463 // Checks whether the item could be added to the given slot
464 bool itemFits(u32 i, InventoryItem *newitem);
466 // Takes some items from a slot.
467 // If there are not enough, takes as many as it can.
468 // Returns NULL if couldn't take any.
469 InventoryItem * takeItem(u32 i, u32 count);
471 // Decrements amount of every material item
472 void decrementMaterials(u16 count);
474 void print(std::ostream &o);
477 core::array<InventoryItem*> m_items;
490 Inventory(const Inventory &other);
491 Inventory & operator = (const Inventory &other);
493 void serialize(std::ostream &os);
494 void deSerialize(std::istream &is);
496 InventoryList * addList(const std::string &name, u32 size);
497 InventoryList * getList(const std::string &name);
498 bool deleteList(const std::string &name);
499 // A shorthand for adding items.
500 // Returns NULL if the item was fully added, leftover otherwise.
501 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
503 InventoryList *list = getList(listname);
506 return list->addItem(newitem);
511 s32 getListIndex(const std::string &name);
513 core::array<InventoryList*> m_lists;
518 struct InventoryContext
520 Player *current_player;
527 class InventoryAction;
529 class InventoryManager
533 virtual ~InventoryManager(){}
536 Get a pointer to an inventory specified by id.
541 virtual Inventory* getInventory(InventoryContext *c, std::string id)
543 // Used on the server by InventoryAction::apply and other stuff
544 virtual void inventoryModified(InventoryContext *c, std::string id)
546 // Used on the client
547 virtual void inventoryAction(InventoryAction *a)
551 #define IACTION_MOVE 0
553 struct InventoryAction
555 static InventoryAction * deSerialize(std::istream &is);
557 virtual u16 getType() const = 0;
558 virtual void serialize(std::ostream &os) = 0;
559 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
562 struct IMoveAction : public InventoryAction
564 // count=0 means "everything"
566 std::string from_inv;
567 std::string from_list;
579 IMoveAction(std::istream &is)
583 std::getline(is, ts, ' ');
586 std::getline(is, from_inv, ' ');
588 std::getline(is, from_list, ' ');
590 std::getline(is, ts, ' ');
593 std::getline(is, to_inv, ' ');
595 std::getline(is, to_list, ' ');
597 std::getline(is, ts, ' ');
606 void serialize(std::ostream &os)
618 void apply(InventoryContext *c, InventoryManager *mgr);
622 Craft checking system
636 enum ItemSpecType type;
637 // Only other one of these is used
645 ItemSpec(enum ItemSpecType a_type, std::string a_name):
651 ItemSpec(enum ItemSpecType a_type, u16 a_num):
658 bool checkItem(InventoryItem *item);
662 items: a pointer to an array of 9 pointers to items
663 specs: a pointer to an array of 9 ItemSpecs
665 bool checkItemCombination(InventoryItem **items, ItemSpec *specs);