- infostream<<"Unknown InventoryItem name=\""<<name<<"\""<<std::endl;
- throw SerializationError("Unknown InventoryItem name");
- }
-}
-
-InventoryItem* InventoryItem::deSerialize(const std::string &str,
- IGameDef *gamedef)
-{
- std::istringstream is(str, std::ios_base::binary);
- return deSerialize(is, gamedef);
-}
-
-std::string InventoryItem::getItemString() {
- // Get item string
- std::ostringstream os(std::ios_base::binary);
- serialize(os);
- return os.str();
-}
-
-bool InventoryItem::dropOrPlace(ServerEnvironment *env,
- ServerActiveObject *dropper,
- v3f pos, bool place, s16 count)
-{
- /*
- Ensure that the block is loaded so that the item
- can properly be added to the static list too
- */
- v3s16 blockpos = getNodeBlockPos(floatToInt(pos, BS));
- MapBlock *block = env->getMap().emergeBlock(blockpos, false);
- if(block==NULL)
- {
- infostream<<"InventoryItem::dropOrPlace(): FAIL: block not found: "
- <<blockpos.X<<","<<blockpos.Y<<","<<blockpos.Z
- <<std::endl;
- return false;
- }
-
- /*
- Take specified number of items,
- but limit to getDropCount().
- */
- s16 dropcount = getDropCount();
- if(count < 0 || count > dropcount)
- count = dropcount;
- if(count < 0 || count > getCount())
- count = getCount();
- if(count > 0)
- {
- /*
- Create an ItemSAO
- */
- pos.Y -= BS*0.25; // let it drop a bit
- // Randomize a bit
- //pos.X += BS*0.2*(float)myrand_range(-1000,1000)/1000.0;
- //pos.Z += BS*0.2*(float)myrand_range(-1000,1000)/1000.0;
- // Create object
- ServerActiveObject *obj = new ItemSAO(env, pos, getItemString());
- // Add the object to the environment
- env->addActiveObject(obj);
- infostream<<"Dropped item"<<std::endl;
-
- setCount(getCount() - count);
- }
-
- return getCount() < 1; // delete the item?
-}
-
-/*
- MaterialItem
-*/
-
-MaterialItem::MaterialItem(IGameDef *gamedef, std::string nodename, u16 count):
- InventoryItem(gamedef, count)
-{
- if(nodename == "")
- nodename = "unknown_block";
-
- // Convert directly to the correct name through aliases
- m_nodename = gamedef->ndef()->getAlias(nodename);
-}
-// Legacy constructor
-MaterialItem::MaterialItem(IGameDef *gamedef, content_t content, u16 count):
- InventoryItem(gamedef, count)
-{
- NameIdMapping legacy_nimap;
- content_mapnode_get_name_id_mapping(&legacy_nimap);
- std::string nodename;
- legacy_nimap.getName(content, nodename);
- if(nodename == "")
- nodename = "unknown_block";
- m_nodename = nodename;
-}
-
-#ifndef SERVER
-video::ITexture * MaterialItem::getImage() const
-{
- return m_gamedef->getNodeDefManager()->get(m_nodename).inventory_texture;
-}
-#endif
-
-bool MaterialItem::isCookable() const
-{
- INodeDefManager *ndef = m_gamedef->ndef();
- const ContentFeatures &f = ndef->get(m_nodename);
- return (f.cookresult_item != "");
-}
-
-InventoryItem *MaterialItem::createCookResult() const
-{
- INodeDefManager *ndef = m_gamedef->ndef();
- const ContentFeatures &f = ndef->get(m_nodename);
- std::istringstream is(f.cookresult_item, std::ios::binary);
- return InventoryItem::deSerialize(is, m_gamedef);
-}
-
-float MaterialItem::getCookTime() const
-{
- INodeDefManager *ndef = m_gamedef->ndef();
- const ContentFeatures &f = ndef->get(m_nodename);
- return f.furnace_cooktime;
-}
-
-float MaterialItem::getBurnTime() const
-{
- INodeDefManager *ndef = m_gamedef->ndef();
- const ContentFeatures &f = ndef->get(m_nodename);
- return f.furnace_burntime;
-}
-
-content_t MaterialItem::getMaterial() const
-{
- INodeDefManager *ndef = m_gamedef->ndef();
- content_t id = CONTENT_IGNORE;
- ndef->getId(m_nodename, id);
- return id;
-}
-
-/*
- ToolItem
-*/
-
-ToolItem::ToolItem(IGameDef *gamedef, std::string toolname, u16 wear):
- InventoryItem(gamedef, 1)
-{
- // Convert directly to the correct name through aliases
- m_toolname = gamedef->tdef()->getAlias(toolname);
-
- m_wear = wear;
-}
-
-std::string ToolItem::getImageBasename() const
-{
- return m_gamedef->getToolDefManager()->getImagename(m_toolname);
-}
-
-#ifndef SERVER
-video::ITexture * ToolItem::getImage() const
-{
- ITextureSource *tsrc = m_gamedef->tsrc();
-
- std::string basename = getImageBasename();
-
- /*
- 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;