From 1a0bfd8d28c68d3ee60352ef67c58da946c43cea Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Mon, 28 May 2012 16:47:57 +0000 Subject: Chests and Furnaces are now saved properly into Anvil scheme. git-svn-id: http://mc-server.googlecode.com/svn/trunk@516 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/WSSAnvil.cpp | 101 ++++++++++++++++++++++++++++++++++++++++---- source/WSSAnvil.h | 13 +----- source/cChestEntity.cpp | 12 +++++- source/cChestEntity.h | 2 +- source/cChunk.cpp | 2 +- source/cFurnaceEntity.cpp | 105 +++++++++++++++++++++++++++++++++++----------- source/cFurnaceEntity.h | 28 +++++++++---- source/cItem.cpp | 2 +- source/cItem.h | 2 +- 9 files changed, 210 insertions(+), 57 deletions(-) diff --git a/source/WSSAnvil.cpp b/source/WSSAnvil.cpp index abb31d050..56f8dac7a 100644 --- a/source/WSSAnvil.cpp +++ b/source/WSSAnvil.cpp @@ -9,6 +9,7 @@ #include "zlib.h" #include "BlockID.h" #include "cChestEntity.h" +#include "cFurnaceEntity.h" #include "cItem.h" #include "StringCompression.h" #include "cEntity.h" @@ -55,7 +56,7 @@ public: { if (m_IsTagOpen) { - m_Writer.EndCompound(); + m_Writer.EndList(); } } @@ -90,7 +91,7 @@ protected: } - void AddItem(cItem * a_Item, int a_Slot) + void AddItem(const cItem * a_Item, int a_Slot) { m_Writer.BeginCompound(""); m_Writer.AddShort("id", (short)(a_Item->m_ItemID)); @@ -104,11 +105,11 @@ protected: void AddChestEntity(cChestEntity * a_Entity) { m_Writer.BeginCompound(""); - AddBasicTileEntity(a_Entity, "chest"); + AddBasicTileEntity(a_Entity, "Chest"); m_Writer.BeginList("Items", TAG_Compound); for (int i = 0; i < cChestEntity::c_ChestHeight * cChestEntity::c_ChestWidth; i++) { - cItem * Item = a_Entity->GetSlot(i); + const cItem * Item = a_Entity->GetSlot(i); if ((Item == NULL) || Item->IsEmpty()) { continue; @@ -118,6 +119,21 @@ protected: m_Writer.EndList(); m_Writer.EndCompound(); } + + + void AddFurnaceEntity(cFurnaceEntity * a_Furnace) + { + m_Writer.BeginCompound(""); + AddBasicTileEntity(a_Furnace, "Furnace"); + m_Writer.BeginList("Items", TAG_Compound); + AddItem(&a_Furnace->GetSlot(0), 0); + AddItem(&a_Furnace->GetSlot(1), 1); + AddItem(&a_Furnace->GetSlot(2), 2); + m_Writer.EndList(); + m_Writer.AddShort("BurnTime", (Int16)(a_Furnace->GetTimeToBurn() / 50.0)); + m_Writer.AddShort("CookTime", (Int16)(a_Furnace->GetTimeCooked() / 50.0)); + m_Writer.EndCompound(); + } virtual bool LightIsValid(bool a_IsLightValid) override @@ -139,20 +155,21 @@ protected: { if (!m_HasHadBlockEntity) { - m_Writer.EndCompound(); - m_Writer.BeginCompound("TileEntities"); + m_Writer.EndList(); + m_Writer.BeginList("TileEntities", TAG_Compound); } } else { - m_Writer.BeginCompound("TileEntities"); + m_Writer.BeginList("TileEntities", TAG_Compound); } m_IsTagOpen = true; // Add tile-entity into NBT: switch (a_Entity->GetBlockType()) { - case E_BLOCK_CHEST: AddChestEntity((cChestEntity *)a_Entity); break; + case E_BLOCK_CHEST: AddChestEntity ((cChestEntity *) a_Entity); break; + case E_BLOCK_FURNACE: AddFurnaceEntity((cFurnaceEntity *)a_Entity); break; default: { ASSERT(!"Unhandled block entity saved into Anvil"); @@ -598,6 +615,10 @@ void cWSSAnvil::LoadBlockEntitiesFromNBT(cBlockEntityList & a_BlockEntities, con { LoadChestFromNBT(a_BlockEntities, a_NBT, Child); } + else if (strncmp(a_NBT.GetData(sID), "Furnace", a_NBT.GetDataLength(sID)) == 0) + { + LoadFurnaceFromNBT(a_BlockEntities, a_NBT, Child); + } // TODO: Other block entities } // for Child - tag children } @@ -655,6 +676,70 @@ void cWSSAnvil::LoadChestFromNBT(cBlockEntityList & a_BlockEntities, const cPars +void cWSSAnvil::LoadFurnaceFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx) +{ + ASSERT(a_NBT.GetType(a_TagIdx) == TAG_Compound); + int x, y, z; + if (!GetBlockEntityNBTPos(a_NBT, a_TagIdx, x, y, z)) + { + return; + } + int Items = a_NBT.FindChildByName(a_TagIdx, "Items"); + if ((Items < 0) || (a_NBT.GetType(Items) != TAG_List)) + { + return; // Make it an empty furnace - the chunk loader will provide an empty cFurnaceEntity for this + } + std::auto_ptr Furnace(new cFurnaceEntity(x, y, z, m_World)); + for (int Child = a_NBT.GetFirstChild(Items); Child != -1; Child = a_NBT.GetNextSibling(Child)) + { + int Slot = a_NBT.FindChildByName(Child, "Slot"); + if ((Slot < 0) || (a_NBT.GetType(Slot) != TAG_Byte)) + { + continue; + } + cItem Item; + int ID = a_NBT.FindChildByName(Child, "id"); + if ((ID < 0) || (a_NBT.GetType(ID) != TAG_Short)) + { + continue; + } + Item.m_ItemID = (ENUM_ITEM_ID)(a_NBT.GetShort(ID)); + int Damage = a_NBT.FindChildByName(Child, "Damage"); + if ((Damage < 0) || (a_NBT.GetType(Damage) != TAG_Short)) + { + continue; + } + Item.m_ItemHealth = a_NBT.GetShort(Damage); + int Count = a_NBT.FindChildByName(Child, "Count"); + if ((Count < 0) || (a_NBT.GetType(Count) != TAG_Byte)) + { + continue; + } + Item.m_ItemCount = a_NBT.GetByte(Count); + Furnace->SetSlot(a_NBT.GetByte(Slot), Item); + } // for itr - ItemDefs[] + int BurnTime = a_NBT.FindChildByName(a_TagIdx, "BurnTime"); + if (BurnTime >= 0) + { + Int16 bt = a_NBT.GetShort(BurnTime); + // Anvil doesn't store the time that the fuel can burn. We simply "reset" the current value to be the 100% + Furnace->SetBurnTimes((float)(bt * 50.0), (float)(bt * 50.0)); + } + int CookTime = a_NBT.FindChildByName(a_TagIdx, "CookTime"); + if (CookTime >= 0) + { + Int16 ct = a_NBT.GetShort(CookTime); + // Anvil doesn't store the time that an item takes to cook. We simply use the default - 10 seconds + Furnace->SetCookTimes(10000.0, (float)(ct * 50.0)); + } + Furnace->ContinueCooking(); + a_BlockEntities.push_back(Furnace.release()); +} + + + + + bool cWSSAnvil::GetBlockEntityNBTPos(const cParsedNBT & a_NBT, int a_TagIdx, int & a_X, int & a_Y, int & a_Z) { int x = a_NBT.FindChildByName(a_TagIdx, "x"); diff --git a/source/WSSAnvil.h b/source/WSSAnvil.h index c9f08aeee..f8beea524 100644 --- a/source/WSSAnvil.h +++ b/source/WSSAnvil.h @@ -31,16 +31,6 @@ enum -// fwd: "NBT.h" -class cNBTTag; -class cNBTList; -class cNBTCompound; -class cNBTByteArray; - - - - - class cWSSAnvil : public cWSSchema { @@ -118,7 +108,8 @@ protected: /// Loads the chunk's BlockEntities from NBT data (a_Tag is the Level\\TileEntities list tag; may be -1) void LoadBlockEntitiesFromNBT(cBlockEntityList & a_BlockEntitites, const cParsedNBT & a_NBT, int a_Tag); - void LoadChestFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); + void LoadChestFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); + void LoadFurnaceFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); /// Helper function for extracting the X, Y, and Z int subtags of a NBT compound; returns true if successful bool GetBlockEntityNBTPos(const cParsedNBT & a_NBT, int a_TagIdx, int & a_X, int & a_Y, int & a_Z); diff --git a/source/cChestEntity.cpp b/source/cChestEntity.cpp index e1976836b..7a0de425a 100644 --- a/source/cChestEntity.cpp +++ b/source/cChestEntity.cpp @@ -73,7 +73,7 @@ void cChestEntity::Destroy() -cItem * cChestEntity::GetSlot( int a_Slot ) +const cItem * cChestEntity::GetSlot( int a_Slot ) const { if( a_Slot > -1 && a_Slot < c_ChestHeight*c_ChestWidth ) { @@ -161,7 +161,7 @@ void cChestEntity::SaveToJson( Json::Value& a_Value ) for(unsigned int i = 0; i < NumSlots; i++) { Json::Value Slot; - cItem* Item = GetSlot( i ); + const cItem * Item = GetSlot( i ); if( Item ) Item->GetJson( Slot ); AllSlots.append( Slot ); } @@ -210,6 +210,14 @@ void cChestEntity::UsedBy( cPlayer * a_Player ) ChestOpen.m_Byte1 = (char)1; ChestOpen.m_Byte2 = (char)1; m_World->BroadcastToChunkOfBlock(m_PosX, m_PosY, m_PosZ, &ChestOpen); + + // This is rather a hack + // Instead of marking the chunk as dirty upon chest contents change, we mark it dirty now + // We cannot properly detect contents change, but such a change doesn't happen without a player opening the chest first. + // The few false positives aren't much to worry about + int ChunkX, ChunkY = 0, ChunkZ; + cChunkDef::BlockToChunk(m_PosX, m_PosY, m_PosZ, ChunkX, ChunkZ); + m_World->MarkChunkDirty(ChunkX, ChunkY, ChunkZ); } diff --git a/source/cChestEntity.h b/source/cChestEntity.h index 323eb0f8a..2ae87d59f 100644 --- a/source/cChestEntity.h +++ b/source/cChestEntity.h @@ -34,7 +34,7 @@ public: void HandleData( cNBTData* a_NBTData ); - cItem * GetSlot( int a_Slot ); + const cItem * GetSlot( int a_Slot ) const; void SetSlot( int a_Slot, cItem & a_Item ); OBSOLETE bool LoadFromFile(cFile & a_File); // deprecated format diff --git a/source/cChunk.cpp b/source/cChunk.cpp index 0ef2ab7b0..90d94a1ce 100644 --- a/source/cChunk.cpp +++ b/source/cChunk.cpp @@ -531,7 +531,7 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) { if ((*itr)->GetBlockType() == E_BLOCK_FURNACE) { - ((cFurnaceEntity *)(*itr))->Tick( a_Dt ); + m_IsDirty = ((cFurnaceEntity *)(*itr))->Tick( a_Dt ) | m_IsDirty; } } } diff --git a/source/cFurnaceEntity.cpp b/source/cFurnaceEntity.cpp index 389d4af54..4e831cdff 100644 --- a/source/cFurnaceEntity.cpp +++ b/source/cFurnaceEntity.cpp @@ -102,13 +102,27 @@ void cFurnaceEntity::UsedBy( cPlayer * a_Player ) bool cFurnaceEntity::Tick( float a_Dt ) { - //LOG("Time left: %0.1f Time burned: %0.1f Burn time: %0.1f", m_CookTime - m_TimeCooked, m_TimeBurned, m_BurnTime ); - if( m_CookingItem && ( (m_TimeBurned < m_BurnTime) || (m_TimeCooked + a_Dt >= m_CookTime) ) ) + /* + // DEBUG: + Int16 BurnTime = (Int16)(GetTimeToBurn() / 50.0); + Int16 CookTime = (Int16)(GetTimeCooked() / 50.0); + LOGD("Furnace: BurnTime %d, CookTime %d", BurnTime, CookTime); + */ + + if (m_BurnTime <= 0) { - if( m_CookingItem->Equals( m_Items[2] ) || m_Items[2].IsEmpty() ) + // There is no fuel and no flame, no need to tick at all + return false; + } + + // DEBUG: LOGD("Furnace: Left: %0.1f Burned: %0.1f Burn time: %0.1f", m_CookTime - m_TimeCooked, m_TimeBurned, m_BurnTime ); + + if ((m_CookingItem != NULL) && ((m_TimeBurned < m_BurnTime) || (m_TimeCooked + a_Dt >= m_CookTime))) + { + if (m_CookingItem->Equals(m_Items[2]) || m_Items[2].IsEmpty()) { m_TimeCooked += a_Dt; - if( m_TimeCooked >= m_CookTime ) + if ( m_TimeCooked >= m_CookTime ) { m_Items[0].m_ItemCount--; if( m_Items[0].IsEmpty() ) m_Items[0].Empty(); @@ -117,26 +131,26 @@ bool cFurnaceEntity::Tick( float a_Dt ) m_Items[2].m_ItemID = m_CookingItem->m_ItemID; m_Items[2].m_ItemCount += m_CookingItem->m_ItemCount; delete m_CookingItem; - m_CookingItem = 0; + m_CookingItem = NULL; - cWindow* Window = GetWindow(); - if( Window ) + cWindow * Window = GetWindow(); + if (Window != NULL) { Window->BroadcastWholeWindow(); } - m_TimeCooked = 0.f; + m_TimeCooked -= m_CookTime; StartCooking(); } - cWindow* Window = GetWindow(); - if( Window ) + cWindow * Window = GetWindow(); + if (Window != NULL) { cPacket_InventoryProgressBar Progress; Progress.m_ProgressBar = 0; Progress.m_WindowID = (char)Window->GetWindowID(); - Progress.m_Value = (short)( m_TimeCooked * (180.f/m_CookTime) ); - if( Progress.m_Value > 180 ) Progress.m_Value = 180; - if( Progress.m_Value < 0 ) Progress.m_Value = 0; + Progress.m_Value = (short)( m_TimeCooked * (180.f / m_CookTime) ); + if (Progress.m_Value > 180) Progress.m_Value = 180; + if (Progress.m_Value < 0) Progress.m_Value = 0; Window->Broadcast(Progress); } } @@ -144,42 +158,42 @@ bool cFurnaceEntity::Tick( float a_Dt ) m_TimeBurned += a_Dt; - cWindow* Window = GetWindow(); - if( m_TimeBurned >= m_BurnTime ) + cWindow * Window = GetWindow(); + if (m_TimeBurned >= m_BurnTime) { m_TimeBurned -= m_BurnTime; m_BurnTime = 0; - if( StartCooking() && Window ) + if (StartCooking() && (Window != NULL)) { Window->BroadcastWholeWindow(); } } - if( Window ) + if (Window != NULL) { cPacket_InventoryProgressBar Progress; Progress.m_WindowID = (char)Window->GetWindowID(); Progress.m_ProgressBar = 1; - if ( m_BurnTime > 0.f ) + if (m_BurnTime > 0.f) { - Progress.m_Value = (short)( m_TimeBurned * (150.f / m_BurnTime) ); - if ( Progress.m_Value > 150 ) Progress.m_Value = 150; - if ( Progress.m_Value < 0 ) Progress.m_Value = 0; + Progress.m_Value = 250 - (short)( m_TimeBurned * (250.f / m_BurnTime) ); + if (Progress.m_Value > 250) Progress.m_Value = 250; + if (Progress.m_Value < 0) Progress.m_Value = 0; } else { Progress.m_Value = 0; } - Window->Broadcast( Progress ); + Window->Broadcast(Progress); } - return ((m_CookingItem != 0) || (m_TimeBurned < m_BurnTime)) && m_BurnTime > 0.f; // Keep on ticking, if there's more to cook, or if it's cooking + return ((m_CookingItem != NULL) || (m_TimeBurned < m_BurnTime)) && (m_BurnTime > 0.0); // Keep on ticking, if there's more to cook, or if it's cooking } -bool cFurnaceEntity::StartCooking() +bool cFurnaceEntity::StartCooking(void) { cFurnaceRecipe* FR = cRoot::Get()->GetFurnaceRecipe(); float BurnTime = FR->GetBurnTime( m_Items[1] ); @@ -217,6 +231,33 @@ bool cFurnaceEntity::StartCooking() +bool cFurnaceEntity::ContinueCooking(void) +{ + cFurnaceRecipe * FR = cRoot::Get()->GetFurnaceRecipe(); + float BurnTime = FR->GetBurnTime( m_Items[1] ); + if( (m_TimeBurned < m_BurnTime) || BurnTime > 0.f ) // burnable material + { + const cFurnaceRecipe::Recipe * R = FR->GetRecipeFrom( m_Items[0] ); + if (R != NULL) // cook able ingredient + { + if (m_Items[2].Equals(*R->Out) || m_Items[2].IsEmpty()) + { + // good to go + if (m_CookingItem == NULL) // Only cook new item if not already cooking + { + m_CookingItem = new cItem( *R->Out ); // Resulting item + } + return true; + } + } + } + return false; +} + + + + + void cFurnaceEntity::ResetCookTimer() { delete m_CookingItem; @@ -229,6 +270,20 @@ void cFurnaceEntity::ResetCookTimer() +void cFurnaceEntity::SetSlot(int a_Slot, const cItem & a_Item) +{ + if ((a_Slot < 0) || (a_Slot >= 3)) + { + ASSERT(!"Furnace: slot number out of range"); + return; + } + m_Items[a_Slot] = a_Item; +} + + + + + #define READ(File, Var) \ if (File.Read(&Var, sizeof(Var)) != sizeof(Var)) \ { \ @@ -321,7 +376,7 @@ void cFurnaceEntity::SaveToJson( Json::Value& a_Value ) for(unsigned int i = 0; i < 3; i++) { Json::Value Slot; - m_Items[ i ].GetJson( Slot ); + m_Items[ i ].GetJson( Slot ); AllSlots.append( Slot ); } a_Value["Slots"] = AllSlots; diff --git a/source/cFurnaceEntity.h b/source/cFurnaceEntity.h index 21cdb1e38..253990000 100644 --- a/source/cFurnaceEntity.h +++ b/source/cFurnaceEntity.h @@ -4,6 +4,7 @@ #include "cBlockEntity.h" #include "cWindowOwner.h" #include "FileDefine.h" +#include "cItem.h" @@ -16,8 +17,6 @@ namespace Json class cClientHandle; class cServer; -class cItem; -class cNBTData; @@ -36,23 +35,38 @@ public: bool LoadFromJson(const Json::Value& a_Value ); virtual void SaveToJson(Json::Value& a_Value ) override; + // Returns true if there's any change, forcing the chunk to go dirty. bool Tick( float a_Dt ); virtual void UsedBy( cPlayer * a_Player ) override; bool StartCooking(); + + /// Restarts cooking. Used after the furnace is loaded from storage to set up the internal variables so that cooking continues, if it was active. Returns true if cooking. + bool ContinueCooking(void); void ResetCookTimer(); + const cItem & GetSlot(int i) const { return m_Items[i]; } + + void SetSlot(int a_Slot, const cItem & a_Item); + + float GetTimeCooked(void) const {return m_TimeCooked; } + float GetTimeToBurn(void) const {return m_BurnTime - m_TimeBurned; } + + void SetBurnTimes(float a_BurnTime, float a_TimeBurned) {m_BurnTime = a_BurnTime; m_TimeBurned = 0; } + void SetCookTimes(float a_CookTime, float a_TimeCooked) {m_CookTime = a_CookTime; m_TimeCooked = a_TimeCooked; } + private: cItem * m_Items; cItem * m_CookingItem; - float m_CookTime; - float m_TimeCooked; - - float m_BurnTime; - float m_TimeBurned; + + // All timers are in 1 ms + float m_CookTime; // Amount of time needed to fully cook current item + float m_TimeCooked; // Amount of time that the current item has been cooking + float m_BurnTime; // Amount of time that the current fuel can burn (in total); zero if no fuel burning + float m_TimeBurned; // Amount of time that the current fuel has been burning }; diff --git a/source/cItem.cpp b/source/cItem.cpp index 2015bd751..acb51611a 100644 --- a/source/cItem.cpp +++ b/source/cItem.cpp @@ -8,7 +8,7 @@ -void cItem::GetJson( Json::Value & a_OutValue ) +void cItem::GetJson( Json::Value & a_OutValue ) const { a_OutValue["ID"] = m_ItemID; if( m_ItemID > 0 ) diff --git a/source/cItem.h b/source/cItem.h index ba88a1940..a0832ef03 100644 --- a/source/cItem.h +++ b/source/cItem.h @@ -85,7 +85,7 @@ public: inline bool HasDuration() { return GetMaxDuration() > 0; } - void GetJson( Json::Value & a_OutValue ); //tolua_export + void GetJson( Json::Value & a_OutValue ) const; //tolua_export void FromJson( const Json::Value & a_Value ); //tolua_export static bool IsEnchantable(ENUM_ITEM_ID item); -- cgit v1.2.3