summaryrefslogtreecommitdiffstats
path: root/src/BlockEntities/FurnaceEntity.cpp
diff options
context:
space:
mode:
authorTiger Wang <ziwei.tiger@hotmail.co.uk>2014-10-03 22:32:41 +0200
committerTiger Wang <ziwei.tiger@hotmail.co.uk>2014-10-03 22:32:41 +0200
commitb5a2c6667ac0845d17a709cc436afb570079a9a7 (patch)
treec9c94d48d811534e0e89d30ba88fb446a5681487 /src/BlockEntities/FurnaceEntity.cpp
parentFix spelling of PRISMRAINE (diff)
downloadcuberite-b5a2c6667ac0845d17a709cc436afb570079a9a7.tar
cuberite-b5a2c6667ac0845d17a709cc436afb570079a9a7.tar.gz
cuberite-b5a2c6667ac0845d17a709cc436afb570079a9a7.tar.bz2
cuberite-b5a2c6667ac0845d17a709cc436afb570079a9a7.tar.lz
cuberite-b5a2c6667ac0845d17a709cc436afb570079a9a7.tar.xz
cuberite-b5a2c6667ac0845d17a709cc436afb570079a9a7.tar.zst
cuberite-b5a2c6667ac0845d17a709cc436afb570079a9a7.zip
Diffstat (limited to 'src/BlockEntities/FurnaceEntity.cpp')
-rw-r--r--src/BlockEntities/FurnaceEntity.cpp141
1 files changed, 47 insertions, 94 deletions
diff --git a/src/BlockEntities/FurnaceEntity.cpp b/src/BlockEntities/FurnaceEntity.cpp
index 4452fc00a..222b27637 100644
--- a/src/BlockEntities/FurnaceEntity.cpp
+++ b/src/BlockEntities/FurnaceEntity.cpp
@@ -5,6 +5,7 @@
#include "../UI/Window.h"
#include "../Entities/Player.h"
#include "../Root.h"
+#include "Chunk.h"
@@ -13,8 +14,9 @@
enum
{
- PROGRESSBAR_SMELTING = 0,
- PROGRESSBAR_FUEL = 1,
+ PROGRESSBAR_FUEL = 0,
+ PROGRESSBAR_SMELTING = 2,
+ PROGRESSBAR_SMELTING_CONFIRM = 3,
} ;
@@ -23,7 +25,6 @@ enum
cFurnaceEntity::cFurnaceEntity(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, cWorld * a_World) :
super(E_BLOCK_FURNACE, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, a_World),
- m_BlockType(a_BlockType),
m_BlockMeta(a_BlockMeta),
m_CurrentRecipe(NULL),
m_IsCooking((a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ) == E_BLOCK_LIT_FURNACE)),
@@ -31,8 +32,7 @@ cFurnaceEntity::cFurnaceEntity(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTY
m_TimeCooked(0),
m_FuelBurnTime(0),
m_TimeBurned(0),
- m_LastProgressFuel(0),
- m_LastProgressCook(0)
+ m_IsDestroyed(false)
{
m_Contents.AddListener(*this);
}
@@ -57,27 +57,28 @@ cFurnaceEntity::~cFurnaceEntity()
void cFurnaceEntity::UsedBy(cPlayer * a_Player)
{
- if (GetWindow() == NULL)
+ cWindow * Window = GetWindow();
+ if (Window == NULL)
{
OpenWindow(new cFurnaceWindow(m_PosX, m_PosY, m_PosZ, this));
+ Window = GetWindow();
}
- cWindow * Window = GetWindow();
+
if (Window != NULL)
{
if (a_Player->GetWindow() != Window)
{
a_Player->OpenWindow(Window);
- BroadcastProgress(PROGRESSBAR_FUEL, (short)m_LastProgressFuel);
- BroadcastProgress(PROGRESSBAR_SMELTING, (short)m_LastProgressCook);
}
}
+
+ UpdateProgressBars(true);
}
-/// 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 cFurnaceEntity::ContinueCooking(void)
{
UpdateInput();
@@ -92,14 +93,16 @@ bool cFurnaceEntity::ContinueCooking(void)
bool cFurnaceEntity::Tick(float a_Dt, cChunk & a_Chunk)
{
UNUSED(a_Dt);
- UNUSED(a_Chunk);
+
if (m_FuelBurnTime <= 0)
{
- // No fuel is burning, reset progressbars and bail out
- if ((m_LastProgressCook > 0) || (m_LastProgressFuel > 0))
- {
- UpdateProgressBars();
- }
+ // Cooked time decreases twice as fast when ran out of fuel
+ m_TimeCooked -= 2;
+ m_TimeCooked = std::max(m_TimeCooked, 0);
+
+ // Reset progressbars, block type, and bail out
+ a_Chunk.FastSetBlock(GetRelX(), m_PosY, GetRelZ(), E_BLOCK_FURNACE, m_BlockMeta);
+ UpdateProgressBars();
return false;
}
@@ -139,12 +142,12 @@ void cFurnaceEntity::SendTo(cClientHandle & a_Client)
-void cFurnaceEntity::BroadcastProgress(int a_ProgressbarID, short a_Value)
+void cFurnaceEntity::BroadcastProgress(short a_ProgressbarID, short a_Value)
{
cWindow * Window = GetWindow();
if (Window != NULL)
{
- Window->BroadcastProgress(a_ProgressbarID, a_Value);
+ Window->SetProperty(a_ProgressbarID, a_Value);
}
}
@@ -152,7 +155,6 @@ void cFurnaceEntity::BroadcastProgress(int a_ProgressbarID, short a_Value)
-/// One item finished cooking
void cFurnaceEntity::FinishOne()
{
m_TimeCooked = 0;
@@ -166,8 +168,6 @@ void cFurnaceEntity::FinishOne()
m_Contents.ChangeSlotCount(fsOutput, m_CurrentRecipe->Out->m_ItemCount);
}
m_Contents.ChangeSlotCount(fsInput, -m_CurrentRecipe->In->m_ItemCount);
-
- UpdateIsCooking();
}
@@ -181,8 +181,7 @@ void cFurnaceEntity::BurnNewFuel(void)
if (NewTime == 0)
{
// The item in the fuel slot is not suitable
- m_FuelBurnTime = 0;
- m_TimeBurned = 0;
+ SetBurnTimes(0, 0);
SetIsCooking(false);
return;
}
@@ -194,8 +193,7 @@ void cFurnaceEntity::BurnNewFuel(void)
}
// Burn one new fuel:
- m_FuelBurnTime = NewTime;
- m_TimeBurned = 0;
+ SetBurnTimes(NewTime, 0);
SetIsCooking(true);
if (m_Contents.GetSlot(fsFuel).m_ItemType == E_ITEM_LAVA_BUCKET)
{
@@ -214,33 +212,19 @@ void cFurnaceEntity::BurnNewFuel(void)
void cFurnaceEntity::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum)
{
super::OnSlotChanged(a_ItemGrid, a_SlotNum);
-
- if (m_World == NULL)
+
+ if (m_IsDestroyed)
{
- // The furnace isn't initialized yet, do no processing
return;
}
ASSERT(a_ItemGrid == &m_Contents);
switch (a_SlotNum)
{
- case fsInput:
- {
- UpdateInput();
- break;
- }
-
- case fsFuel:
- {
- UpdateFuel();
- break;
- }
-
- case fsOutput:
- {
- UpdateOutput();
- break;
- }
+ case fsInput: UpdateInput(); break;
+ case fsFuel: UpdateFuel(); break;
+ case fsOutput: UpdateOutput(); break;
+ default: ASSERT(!"Invalid furnace slot update!"); break;
}
}
@@ -249,7 +233,6 @@ void cFurnaceEntity::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum)
-/// Updates the current recipe, based on the current input
void cFurnaceEntity::UpdateInput(void)
{
if (!m_Contents.GetSlot(fsInput).IsEqual(m_LastInput))
@@ -263,8 +246,8 @@ void cFurnaceEntity::UpdateInput(void)
m_CurrentRecipe = FR->GetRecipeFrom(m_Contents.GetSlot(fsInput));
if (!CanCookInputToOutput())
{
- // This input cannot be cooked
- m_NeedCookTime = 0;
+ // This input cannot be cooked, reset cook counter immediately
+ SetCookTimes(0, 0);
SetIsCooking(false);
}
else
@@ -284,7 +267,6 @@ void cFurnaceEntity::UpdateInput(void)
-/// Called when the fuel slot changes or when the fuel is spent, burns another piece of fuel if appropriate
void cFurnaceEntity::UpdateFuel(void)
{
if (m_FuelBurnTime > m_TimeBurned)
@@ -301,14 +283,12 @@ void cFurnaceEntity::UpdateFuel(void)
-/// Called when the output slot changes; starts burning if space became available
void cFurnaceEntity::UpdateOutput(void)
{
if (!CanCookInputToOutput())
{
// Cannot cook anymore:
- m_TimeCooked = 0;
- m_NeedCookTime = 0;
+ SetCookTimes(0, 0);
SetIsCooking(false);
return;
}
@@ -324,30 +304,6 @@ void cFurnaceEntity::UpdateOutput(void)
-/// Updates the m_IsCooking, based on the input slot, output slot and m_FuelBurnTime / m_TimeBurned
-void cFurnaceEntity::UpdateIsCooking(void)
-{
- if (
- !CanCookInputToOutput() || // Cannot cook this
- (m_FuelBurnTime <= 0) || // No fuel
- (m_TimeBurned >= m_FuelBurnTime) // Fuel burnt out
- )
- {
- // Reset everything
- SetIsCooking(false);
- m_TimeCooked = 0;
- m_NeedCookTime = 0;
- return;
- }
-
- SetIsCooking(true);
-}
-
-
-
-
-
-/// Returns true if the input can be cooked into output and the item counts allow for another cooking operation
bool cFurnaceEntity::CanCookInputToOutput(void) const
{
if (m_CurrentRecipe == NULL)
@@ -382,25 +338,20 @@ bool cFurnaceEntity::CanCookInputToOutput(void) const
-/// Broadcasts progressbar updates, if needed
-void cFurnaceEntity::UpdateProgressBars(void)
+void cFurnaceEntity::UpdateProgressBars(bool a_ForceUpdate)
{
// In order to preserve bandwidth, an update is sent only every 10th tick
- // That's why the comparisons use the division by eight
-
- int CurFuel = (m_FuelBurnTime > 0) ? (200 - 200 * m_TimeBurned / m_FuelBurnTime) : 0;
- if ((CurFuel / 8) != (m_LastProgressFuel / 8))
+ if (!a_ForceUpdate && (m_World->GetWorldAge() % 10 != 0))
{
- BroadcastProgress(PROGRESSBAR_FUEL, (short)CurFuel);
- m_LastProgressFuel = CurFuel;
+ return;
}
- int CurCook = (m_NeedCookTime > 0) ? (200 * m_TimeCooked / m_NeedCookTime) : 0;
- if ((CurCook / 8) != (m_LastProgressCook / 8))
- {
- BroadcastProgress(PROGRESSBAR_SMELTING, (short)CurCook);
- m_LastProgressCook = CurCook;
- }
+ int CurFuel = (m_FuelBurnTime > 0) ? 200 - (200 * m_TimeBurned / m_FuelBurnTime) : 0;
+ BroadcastProgress(PROGRESSBAR_FUEL, (short)CurFuel);
+
+ int CurCook = (m_NeedCookTime > 0) ? (200 * m_TimeCooked / m_NeedCookTime) : 0;
+ BroadcastProgress(PROGRESSBAR_SMELTING_CONFIRM, 200); // Post 1.8, Mojang requires a random packet with an ID of three and value of 200. Wat. Wat. Wat.
+ BroadcastProgress(PROGRESSBAR_SMELTING, (short)CurCook);
}
@@ -413,11 +364,13 @@ void cFurnaceEntity::SetIsCooking(bool a_IsCooking)
{
return;
}
-
m_IsCooking = a_IsCooking;
-
- // Light or extinguish the furnace:
- m_World->FastSetBlock(m_PosX, m_PosY, m_PosZ, m_IsCooking ? E_BLOCK_LIT_FURNACE : E_BLOCK_FURNACE, m_BlockMeta);
+
+ // Only light the furnace as it is extinguished only when the fuel runs out, not when cooking stops - handled in this::Tick()
+ if (m_IsCooking)
+ {
+ m_World->FastSetBlock(m_PosX, m_PosY, m_PosZ, E_BLOCK_LIT_FURNACE, m_BlockMeta);
+ }
}