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);
94 // Whether it can be cooked
95 virtual bool isCookable(){return false;}
97 virtual float getCookTime(){return 3.0;}
99 virtual InventoryItem *createCookResult(){return NULL;}
105 class MaterialItem : public InventoryItem
108 MaterialItem(u8 content, u16 count):
114 Implementation interface
116 virtual const char* getName() const
118 return "MaterialItem";
120 virtual void serialize(std::ostream &os)
122 //os.imbue(std::locale("C"));
125 os<<(unsigned int)m_content;
129 virtual InventoryItem* clone()
131 return new MaterialItem(m_content, m_count);
134 video::ITexture * getImage()
136 return content_features(m_content).inventory_texture;
140 std::string getText()
142 std::ostringstream os;
147 virtual bool addableTo(InventoryItem *other)
149 if(std::string(other->getName()) != "MaterialItem")
151 MaterialItem *m = (MaterialItem*)other;
152 if(m->getMaterial() != m_content)
158 if(m_count > QUANTITY_ITEM_MAX_COUNT)
160 return QUANTITY_ITEM_MAX_COUNT - m_count;
166 InventoryItem *createCookResult();
178 class MapBlockObjectItem : public InventoryItem
181 MapBlockObjectItem(std::string inventorystring):
184 m_inventorystring = inventorystring;
188 Implementation interface
190 virtual const char* getName() const
194 virtual void serialize(std::ostream &os)
198 size_t t = m_inventorystring.find('|');
199 if(t == std::string::npos)
201 m_inventorystring[t] = '?';
205 os<<m_inventorystring;
208 virtual InventoryItem* clone()
210 return new MapBlockObjectItem(m_inventorystring);
214 video::ITexture * getImage();
216 std::string getText();
221 std::string getInventoryString()
223 return m_inventorystring;
226 MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
229 std::string m_inventorystring;
233 An item that is used as a mid-product when crafting.
237 class CraftItem : public InventoryItem
240 CraftItem(std::string subname, u16 count):
246 Implementation interface
248 virtual const char* getName() const
252 virtual void serialize(std::ostream &os)
260 virtual InventoryItem* clone()
262 return new CraftItem(m_subname, m_count);
265 video::ITexture * getImage()
267 if(g_texturesource == NULL)
272 if(m_subname == "Stick")
274 else if(m_subname == "lump_of_coal")
275 name = "lump_of_coal.png";
276 else if(m_subname == "lump_of_iron")
277 name = "lump_of_iron.png";
278 else if(m_subname == "steel_ingot")
279 name = "steel_ingot.png";
283 // Get such a texture
284 //return g_irrlicht->getTexture(name);
285 return g_texturesource->getTextureRaw(name);
288 std::string getText()
290 std::ostringstream os;
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;
313 InventoryItem *createCookResult();
317 std::string getSubName()
322 std::string m_subname;
325 class ToolItem : public InventoryItem
328 ToolItem(std::string toolname, u16 wear):
331 m_toolname = toolname;
335 Implementation interface
337 virtual const char* getName() const
341 virtual void serialize(std::ostream &os)
349 virtual InventoryItem* clone()
351 return new ToolItem(m_toolname, m_wear);
354 video::ITexture * getImage()
356 if(g_texturesource == NULL)
359 std::string basename;
360 if(m_toolname == "WPick")
361 basename = "tool_woodpick.png";
362 else if(m_toolname == "STPick")
363 basename = "tool_stonepick.png";
364 else if(m_toolname == "SteelPick")
365 basename = "tool_steelpick.png";
366 else if(m_toolname == "MesePick")
367 basename = "tool_mesepick.png";
368 else if(m_toolname == "WShovel")
369 basename = "tool_woodshovel.png";
370 else if(m_toolname == "STShovel")
371 basename = "tool_stoneshovel.png";
372 else if(m_toolname == "SteelShovel")
373 basename = "tool_steelshovel.png";
374 else if(m_toolname == "WAxe")
375 basename = "tool_woodaxe.png";
376 else if(m_toolname == "STAxe")
377 basename = "tool_stoneaxe.png";
378 else if(m_toolname == "SteelAxe")
379 basename = "tool_steelaxe.png";
381 basename = "cloud.png";
384 Calculate a progress value with sane amount of
387 u32 maxprogress = 30;
388 u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
390 float value_f = (float)toolprogress / (float)maxprogress;
391 std::ostringstream os;
392 os<<basename<<"^[progressbar"<<value_f;
394 return g_texturesource->getTextureRaw(os.str());
397 spec.addTid(g_irrlicht->getTextureId(basename));
398 spec.addTid(g_irrlicht->getTextureId(os.str()));
399 return g_irrlicht->getTexture(spec);*/
402 std::string getText()
406 /*std::ostringstream os;
410 for(i=0; i<(65535-m_wear)/d; i++)
416 /*std::ostringstream os;
425 std::string getToolName()
433 // Returns true if weared out
434 bool addWear(u16 add)
436 if(m_wear >= 65535 - add)
448 std::string m_toolname;
455 InventoryList(std::string name, u32 size);
458 void serialize(std::ostream &os);
459 void deSerialize(std::istream &is);
461 InventoryList(const InventoryList &other);
462 InventoryList & operator = (const InventoryList &other);
464 std::string getName();
470 // Get pointer to item
471 InventoryItem * getItem(u32 i);
472 // Returns old item (or NULL). Parameter can be NULL.
473 InventoryItem * changeItem(u32 i, InventoryItem *newitem);
475 void deleteItem(u32 i);
477 // Adds an item to a suitable place. Returns leftover item.
478 // If all went into the list, returns NULL.
479 InventoryItem * addItem(InventoryItem *newitem);
481 // If possible, adds item to given slot.
482 // If cannot be added at all, returns the item back.
483 // If can be added partly, decremented item is returned back.
484 // If can be added fully, NULL is returned.
485 InventoryItem * addItem(u32 i, InventoryItem *newitem);
487 // Checks whether the item could be added to the given slot
488 bool itemFits(u32 i, InventoryItem *newitem);
490 // Takes some items from a slot.
491 // If there are not enough, takes as many as it can.
492 // Returns NULL if couldn't take any.
493 InventoryItem * takeItem(u32 i, u32 count);
495 // Decrements amount of every material item
496 void decrementMaterials(u16 count);
498 void print(std::ostream &o);
501 core::array<InventoryItem*> m_items;
514 Inventory(const Inventory &other);
515 Inventory & operator = (const Inventory &other);
517 void serialize(std::ostream &os);
518 void deSerialize(std::istream &is);
520 InventoryList * addList(const std::string &name, u32 size);
521 InventoryList * getList(const std::string &name);
522 bool deleteList(const std::string &name);
523 // A shorthand for adding items.
524 // Returns NULL if the item was fully added, leftover otherwise.
525 InventoryItem * addItem(const std::string &listname, InventoryItem *newitem)
527 InventoryList *list = getList(listname);
530 return list->addItem(newitem);
535 s32 getListIndex(const std::string &name);
537 core::array<InventoryList*> m_lists;
542 struct InventoryContext
544 Player *current_player;
551 class InventoryAction;
553 class InventoryManager
557 virtual ~InventoryManager(){}
560 Get a pointer to an inventory specified by id.
565 virtual Inventory* getInventory(InventoryContext *c, std::string id)
567 // Used on the server by InventoryAction::apply and other stuff
568 virtual void inventoryModified(InventoryContext *c, std::string id)
570 // Used on the client
571 virtual void inventoryAction(InventoryAction *a)
575 #define IACTION_MOVE 0
577 struct InventoryAction
579 static InventoryAction * deSerialize(std::istream &is);
581 virtual u16 getType() const = 0;
582 virtual void serialize(std::ostream &os) = 0;
583 virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
586 struct IMoveAction : public InventoryAction
588 // count=0 means "everything"
590 std::string from_inv;
591 std::string from_list;
603 IMoveAction(std::istream &is)
607 std::getline(is, ts, ' ');
610 std::getline(is, from_inv, ' ');
612 std::getline(is, from_list, ' ');
614 std::getline(is, ts, ' ');
617 std::getline(is, to_inv, ' ');
619 std::getline(is, to_list, ' ');
621 std::getline(is, ts, ' ');
630 void serialize(std::ostream &os)
642 void apply(InventoryContext *c, InventoryManager *mgr);
646 Craft checking system
660 enum ItemSpecType type;
661 // Only other one of these is used
669 ItemSpec(enum ItemSpecType a_type, std::string a_name):
675 ItemSpec(enum ItemSpecType a_type, u16 a_num):
682 bool checkItem(InventoryItem *item);
686 items: a pointer to an array of 9 pointers to items
687 specs: a pointer to an array of 9 ItemSpecs
689 bool checkItemCombination(InventoryItem **items, ItemSpec *specs);