A more robust format for node metadata

stable-0.2
Perttu Ahola 2011-04-05 10:59:48 +03:00
parent d1d57cf5c3
commit d6d811f97b
3 changed files with 59 additions and 11 deletions

View File

@ -90,6 +90,19 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is)
} }
} }
/*
MaterialItem
*/
InventoryItem *MaterialItem::createCookResult()
{
if(m_content == CONTENT_TREE)
{
return new CraftItem("lump_of_coal", 1);
}
return NULL;
}
/* /*
MapBlockObjectItem MapBlockObjectItem
*/ */
@ -313,6 +326,11 @@ u32 InventoryList::getUsedSlots()
return num; return num;
} }
u32 InventoryList::getFreeSlots()
{
return getSize() - getUsedSlots();
}
InventoryItem * InventoryList::getItem(u32 i) InventoryItem * InventoryList::getItem(u32 i)
{ {
if(i > m_items.size() - 1) if(i > m_items.size() - 1)

View File

@ -72,6 +72,7 @@ public:
{ {
m_count = count; m_count = count;
} }
// This should return something else for stackable items
virtual u16 freeSpace() virtual u16 freeSpace()
{ {
return 0; return 0;
@ -87,6 +88,14 @@ public:
m_count -= count; m_count -= count;
} }
/*
Other properties
*/
// Time of cooking
virtual float getCookTime(){return 3.0;}
// Result of cooking
virtual InventoryItem *createCookResult(){return NULL;}
protected: protected:
u16 m_count; u16 m_count;
}; };
@ -148,6 +157,10 @@ public:
return 0; return 0;
return QUANTITY_ITEM_MAX_COUNT - m_count; return QUANTITY_ITEM_MAX_COUNT - m_count;
} }
/*
Other properties
*/
InventoryItem *createCookResult();
/* /*
Special methods Special methods
*/ */
@ -428,6 +441,7 @@ public:
u32 getSize(); u32 getSize();
// Count used slots // Count used slots
u32 getUsedSlots(); u32 getUsedSlots();
u32 getFreeSlots();
// Get pointer to item // Get pointer to item
InventoryItem * getItem(u32 i); InventoryItem * getItem(u32 i);

View File

@ -177,7 +177,7 @@ FurnaceNodeMetadata::FurnaceNodeMetadata()
m_inventory = new Inventory(); m_inventory = new Inventory();
m_inventory->addList("fuel", 1); m_inventory->addList("fuel", 1);
m_inventory->addList("src", 1); m_inventory->addList("src", 1);
m_inventory->addList("dst", 1); m_inventory->addList("dst", 4);
m_step_accumulator = 0; m_step_accumulator = 0;
m_fuel_totaltime = 0; m_fuel_totaltime = 0;
@ -202,12 +202,15 @@ NodeMetadata* FurnaceNodeMetadata::clone()
NodeMetadata* FurnaceNodeMetadata::create(std::istream &is) NodeMetadata* FurnaceNodeMetadata::create(std::istream &is)
{ {
FurnaceNodeMetadata *d = new FurnaceNodeMetadata(); FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
d->m_inventory->deSerialize(is); d->m_inventory->deSerialize(is);
int temp; int temp;
is>>temp; is>>temp;
d->m_fuel_totaltime = (float)temp/10; d->m_fuel_totaltime = (float)temp/10;
is>>temp; is>>temp;
d->m_fuel_time = (float)temp/10; d->m_fuel_time = (float)temp/10;
return d; return d;
} }
void FurnaceNodeMetadata::serializeBody(std::ostream &os) void FurnaceNodeMetadata::serializeBody(std::ostream &os)
@ -261,8 +264,9 @@ bool FurnaceNodeMetadata::step(float dtime)
assert(src_list); assert(src_list);
InventoryItem *src_item = src_list->getItem(0); InventoryItem *src_item = src_list->getItem(0);
if(ItemSpec(ITEM_MATERIAL, CONTENT_TREE).checkItem(src_item) // Start only if there are free slots in dst, so that it can
&& dst_list->itemFits(0, new CraftItem("lump_of_coal", 1))) // accomodate any result item
if(dst_list->getFreeSlots() > 0)
{ {
m_src_totaltime = 3; m_src_totaltime = 3;
} }
@ -278,15 +282,13 @@ bool FurnaceNodeMetadata::step(float dtime)
m_fuel_time += dtime; m_fuel_time += dtime;
m_src_time += dtime; m_src_time += dtime;
if(m_src_time >= m_src_totaltime && m_src_totaltime > 0.001) if(m_src_time >= m_src_totaltime && m_src_totaltime > 0.001)
{
if(ItemSpec(ITEM_MATERIAL, CONTENT_TREE).checkItem(src_item))
{ {
src_list->decrementMaterials(1); src_list->decrementMaterials(1);
dst_list->addItem(0, new CraftItem("lump_of_coal", 1)); InventoryItem *cookresult = src_item->createCookResult();
dst_list->addItem(cookresult);
m_src_time = 0; m_src_time = 0;
m_src_totaltime = 0; m_src_totaltime = 0;
} }
}
return true; return true;
} }
@ -340,6 +342,10 @@ void NodeMetadataList::serialize(std::ostream &os)
{ {
u8 buf[6]; u8 buf[6];
u16 version = 1;
writeU16(buf, version);
os.write((char*)buf, 2);
u16 count = m_data.size(); u16 count = m_data.size();
writeU16(buf, count); writeU16(buf, count);
os.write((char*)buf, 2); os.write((char*)buf, 2);
@ -365,6 +371,16 @@ void NodeMetadataList::deSerialize(std::istream &is)
u8 buf[6]; u8 buf[6];
is.read((char*)buf, 2);
u16 version = readU16(buf);
if(version > 1)
{
dstream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
<<std::endl;
throw SerializationError("NodeMetadataList::deSerialize");
}
is.read((char*)buf, 2); is.read((char*)buf, 2);
u16 count = readU16(buf); u16 count = readU16(buf);