/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-/*
-(c) 2010 Perttu Ahola <celeron55@gmail.com>
-*/
-
#ifndef INVENTORY_HEADER
#define INVENTORY_HEADER
#include <string>
#include "common_irrlicht.h"
#include "debug.h"
-#include "mapblockobject.h"
-#include "main.h" // For g_materials
#include "mapnode.h" // For content_t
#define QUANTITY_ITEM_MAX_COUNT 99
class ServerActiveObject;
class ServerEnvironment;
-class Player;
+class ITextureSource;
+class IGameDef;
class InventoryItem
{
public:
- InventoryItem(u16 count);
+ InventoryItem(IGameDef *gamedef, u16 count);
virtual ~InventoryItem();
- static InventoryItem* deSerialize(std::istream &is);
+ static InventoryItem* deSerialize(std::istream &is, IGameDef *gamedef);
virtual const char* getName() const = 0;
// Shall write the name and the parameters
virtual void serialize(std::ostream &os) const = 0;
// Shall make an exact clone of the item
virtual InventoryItem* clone() = 0;
-#ifndef SERVER
// Return the name of the image for this item
- virtual std::string getBasename() { return ""; }
+ virtual std::string getImageBasename() const { return ""; }
+#ifndef SERVER
// Shall return an image of the item (or NULL)
- virtual video::ITexture * getImage() { return NULL; }
+ virtual video::ITexture * getImage() const
+ { return NULL; }
+ // Shall return an image of the item without embellishments (or NULL)
+ virtual video::ITexture * getImageRaw() const
+ { return getImage(); }
#endif
// Shall return a text to show in the GUI
virtual std::string getText() { return ""; }
Quantity methods
*/
- // Shall return true if the item can be add()ed to the other
+ // Return true if the item can be add()ed to the other
virtual bool addableTo(const InventoryItem *other) const
- {
- return false;
- }
+ { return false; }
+ // Return true if the other item contains this item
+ virtual bool isSubsetOf(const InventoryItem *other) const
+ { return false; }
+ // Remove the other item from this one if possible and return true
+ // Return false if not possible
+ virtual bool removeOther(const InventoryItem *other)
+ { return false; }
u16 getCount() const
- {
- return m_count;
- }
+ { return m_count; }
void setCount(u16 count)
- {
- m_count = count;
- }
+ { m_count = count; }
+
// This should return something else for stackable items
virtual u16 freeSpace() const
- {
- return 0;
- }
+ { return 0; }
+
void add(u16 count)
{
assert(m_count + count <= QUANTITY_ITEM_MAX_COUNT);
// Whether it can be cooked
virtual bool isCookable() const {return false;}
- // Time of cooking
- virtual float getCookTime(){return 3.0;}
// Result of cooking (can randomize)
virtual InventoryItem *createCookResult() const {return NULL;}
+ // Time of cooking
+ virtual float getCookTime() const {return 3.0;}
+ // Whether it can be burned (<0 = cannot be burned)
+ virtual float getBurnTime() const {return -1;}
// Eat, press, activate, whatever.
// Called when item is right-clicked when lying on ground.
// If returns true, item shall be deleted.
virtual bool use(ServerEnvironment *env,
- Player *player){return false;}
+ ServerActiveObject *user){return false;}
protected:
+ IGameDef *m_gamedef;
u16 m_count;
};
class MaterialItem : public InventoryItem
{
public:
- MaterialItem(content_t content, u16 count):
- InventoryItem(count)
- {
- m_content = content;
- }
+ MaterialItem(IGameDef *gamedef, std::string nodename, u16 count);
+ // Legacy constructor
+ MaterialItem(IGameDef *gamedef, content_t content, u16 count);
/*
Implementation interface
*/
}
virtual void serialize(std::ostream &os) const
{
- //os.imbue(std::locale("C"));
- os<<"MaterialItem2";
- os<<" ";
- os<<(unsigned int)m_content;
- os<<" ";
+ os<<"NodeItem";
+ os<<" \"";
+ os<<m_nodename;
+ os<<"\" ";
os<<m_count;
}
virtual InventoryItem* clone()
{
- return new MaterialItem(m_content, m_count);
+ return new MaterialItem(m_gamedef, m_nodename, m_count);
}
#ifndef SERVER
- video::ITexture * getImage()
- {
- return content_features(m_content).inventory_texture;
- }
+ video::ITexture * getImage() const;
#endif
std::string getText()
{
if(std::string(other->getName()) != "MaterialItem")
return false;
MaterialItem *m = (MaterialItem*)other;
- if(m->getMaterial() != m_content)
+ if(m->m_nodename != m_nodename)
return false;
return true;
}
+ virtual bool isSubsetOf(const InventoryItem *other) const
+ {
+ if(std::string(other->getName()) != "MaterialItem")
+ return false;
+ MaterialItem *m = (MaterialItem*)other;
+ if(m->m_nodename != m_nodename)
+ return false;
+ return m_count <= m->m_count;
+ }
+ virtual bool removeOther(const InventoryItem *other)
+ {
+ if(!other->isSubsetOf(this))
+ return false;
+ MaterialItem *m = (MaterialItem*)other;
+ m_count += m->m_count;
+ return true;
+ }
+
u16 freeSpace() const
{
if(m_count > QUANTITY_ITEM_MAX_COUNT)
*/
bool isCookable() const;
InventoryItem *createCookResult() const;
+ float getCookTime() const;
+ float getBurnTime() const;
/*
- Special methods
- */
- content_t getMaterial()
- {
- return m_content;
- }
-private:
- content_t m_content;
-};
-
-//TODO: Remove
-class MapBlockObjectItem : public InventoryItem
-{
-public:
- MapBlockObjectItem(std::string inventorystring):
- InventoryItem(1)
- {
- m_inventorystring = inventorystring;
- }
-
- /*
- Implementation interface
- */
- virtual const char* getName() const
- {
- return "MBOItem";
- }
- virtual void serialize(std::ostream &os) const
- {
- std::string sane_string(m_inventorystring);
- str_replace_char(sane_string, '|', '?');
- os<<getName();
- os<<" ";
- os<<sane_string;
- os<<"|";
- }
- virtual InventoryItem* clone()
- {
- return new MapBlockObjectItem(m_inventorystring);
- }
-
-#ifndef SERVER
- video::ITexture * getImage();
-#endif
- std::string getText();
-
- /*
- Special methods
+ Special properties (not part of virtual interface)
*/
- std::string getInventoryString()
- {
- return m_inventorystring;
- }
-
- MapBlockObject * createObject(v3f pos, f32 player_yaw, f32 player_pitch);
-
+ std::string getNodeName() const
+ { return m_nodename; }
+ content_t getMaterial() const;
private:
- std::string m_inventorystring;
+ std::string m_nodename;
};
/*
class CraftItem : public InventoryItem
{
public:
- CraftItem(std::string subname, u16 count):
- InventoryItem(count)
+ CraftItem(IGameDef *gamedef, std::string subname, u16 count):
+ InventoryItem(gamedef, count)
{
m_subname = subname;
}
virtual void serialize(std::ostream &os) const
{
os<<getName();
- os<<" ";
+ os<<" \"";
os<<m_subname;
- os<<" ";
+ os<<"\" ";
os<<m_count;
}
virtual InventoryItem* clone()
{
- return new CraftItem(m_subname, m_count);
+ return new CraftItem(m_gamedef, m_subname, m_count);
}
#ifndef SERVER
- video::ITexture * getImage();
+ video::ITexture * getImage() const;
#endif
std::string getText()
{
return false;
return true;
}
+ virtual bool isSubsetOf(const InventoryItem *other) const
+ {
+ if(std::string(other->getName()) != "CraftItem")
+ return false;
+ CraftItem *m = (CraftItem*)other;
+ if(m->m_subname != m_subname)
+ return false;
+ return m_count <= m->m_count;
+ }
+ virtual bool removeOther(const InventoryItem *other)
+ {
+ if(!other->isSubsetOf(this))
+ return false;
+ CraftItem *m = (CraftItem*)other;
+ m_count += m->m_count;
+ return true;
+ }
+
u16 freeSpace() const
{
if(m_count > QUANTITY_ITEM_MAX_COUNT)
bool isCookable() const;
InventoryItem *createCookResult() const;
+ float getCookTime() const;
+ float getBurnTime() const;
- bool use(ServerEnvironment *env, Player *player);
+ bool use(ServerEnvironment *env, ServerActiveObject *user);
/*
Special methods
class ToolItem : public InventoryItem
{
public:
- ToolItem(std::string toolname, u16 wear):
- InventoryItem(1)
+ ToolItem(IGameDef *gamedef, std::string toolname, u16 wear):
+ InventoryItem(gamedef, 1)
{
m_toolname = toolname;
m_wear = wear;
virtual void serialize(std::ostream &os) const
{
os<<getName();
- os<<" ";
+ os<<" \"";
os<<m_toolname;
- os<<" ";
+ os<<"\" ";
os<<m_wear;
}
virtual InventoryItem* clone()
{
- return new ToolItem(m_toolname, m_wear);
+ return new ToolItem(m_gamedef, m_toolname, m_wear);
}
-#ifndef SERVER
- std::string getBasename() {
- if(m_toolname == "WPick")
- return "tool_woodpick.png";
- else if(m_toolname == "STPick")
- return "tool_stonepick.png";
- else if(m_toolname == "SteelPick")
- return "tool_steelpick.png";
- else if(m_toolname == "MesePick")
- return "tool_mesepick.png";
- else if(m_toolname == "WShovel")
- return "tool_woodshovel.png";
- else if(m_toolname == "STShovel")
- return "tool_stoneshovel.png";
- else if(m_toolname == "SteelShovel")
- return "tool_steelshovel.png";
- else if(m_toolname == "WAxe")
- return "tool_woodaxe.png";
- else if(m_toolname == "STAxe")
- return "tool_stoneaxe.png";
- else if(m_toolname == "SteelAxe")
- return "tool_steelaxe.png";
- else if(m_toolname == "WSword")
- return "tool_woodsword.png";
- else if(m_toolname == "STSword")
- return "tool_stonesword.png";
- else if(m_toolname == "SteelSword")
- return "tool_steelsword.png";
- else
- return "cloud.png";
-}
-
- video::ITexture * getImage()
- {
- if(g_texturesource == NULL)
- return NULL;
-
- std::string basename = getBasename();
-
- /*
- Calculate a progress value with sane amount of
- maximum states
- */
- u32 maxprogress = 30;
- u32 toolprogress = (65535-m_wear)/(65535/maxprogress);
-
- float value_f = (float)toolprogress / (float)maxprogress;
- std::ostringstream os;
- os<<basename<<"^[progressbar"<<value_f;
- return g_texturesource->getTextureRaw(os.str());
- }
+ std::string getImageBasename() const;
+#ifndef SERVER
+ video::ITexture * getImage() const;
+ video::ITexture * getImageRaw() const;
#endif
+
std::string getText()
{
return "";
-
- /*std::ostringstream os;
- u16 f = 4;
- u16 d = 65535/f;
- u16 i;
- for(i=0; i<(65535-m_wear)/d; i++)
- os<<'X';
- for(; i<f; i++)
- os<<'-';
- return os.str();*/
-
- /*std::ostringstream os;
- os<<m_toolname;
- os<<" ";
- os<<(m_wear/655);
- return os.str();*/
}
+
+ virtual bool isSubsetOf(const InventoryItem *other) const
+ {
+ if(std::string(other->getName()) != "ToolItem")
+ return false;
+ ToolItem *m = (ToolItem*)other;
+ if(m->m_toolname != m_toolname)
+ return false;
+ return m_wear <= m->m_wear;
+ }
+ virtual bool removeOther(const InventoryItem *other)
+ {
+ if(!other->isSubsetOf(this))
+ return false;
+ ToolItem *m = (ToolItem*)other;
+ m_wear -= m->m_wear;
+ return true;
+ }
+
/*
Special methods
*/
~InventoryList();
void clearItems();
void serialize(std::ostream &os) const;
- void deSerialize(std::istream &is);
+ void deSerialize(std::istream &is, IGameDef *gamedef);
InventoryList(const InventoryList &other);
InventoryList & operator = (const InventoryList &other);
Inventory & operator = (const Inventory &other);
void serialize(std::ostream &os) const;
- void deSerialize(std::istream &is);
+ void deSerialize(std::istream &is, IGameDef *gamedef);
InventoryList * addList(const std::string &name, u32 size);
InventoryList * getList(const std::string &name);
from_i = -1;
to_i = -1;
}
- IMoveAction(std::istream &is)
- {
- std::string ts;
-
- std::getline(is, ts, ' ');
- count = stoi(ts);
-
- std::getline(is, from_inv, ' ');
-
- std::getline(is, from_list, ' ');
-
- std::getline(is, ts, ' ');
- from_i = stoi(ts);
-
- std::getline(is, to_inv, ' ');
-
- std::getline(is, to_list, ' ');
-
- std::getline(is, ts, ' ');
- to_i = stoi(ts);
- }
+
+ IMoveAction(std::istream &is);
u16 getType() const
{
*/
bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);
+/*
+ items: a pointer to an array of 9 pointers to items
+ specs: a pointer to an array of 9 pointers to items
+*/
+bool checkItemCombination(const InventoryItem * const * items,
+ const InventoryItem * const * specs);
+
#endif