2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "FurnaceEntity.h"
|
2014-12-13 09:06:55 -05:00
|
|
|
#include "../UI/FurnaceWindow.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Player.h"
|
2013-05-28 15:12:47 -04:00
|
|
|
#include "../Root.h"
|
2014-10-16 14:08:22 -04:00
|
|
|
#include "../Chunk.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2014-10-03 16:32:41 -04:00
|
|
|
PROGRESSBAR_FUEL = 0,
|
|
|
|
PROGRESSBAR_SMELTING = 2,
|
|
|
|
PROGRESSBAR_SMELTING_CONFIRM = 3,
|
2012-09-20 09:25:54 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 07:41:44 -04:00
|
|
|
cFurnaceEntity::cFurnaceEntity(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, cWorld * a_World) :
|
2014-10-18 14:54:34 -04:00
|
|
|
super(a_BlockType, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, a_World),
|
2013-06-20 07:41:44 -04:00
|
|
|
m_BlockMeta(a_BlockMeta),
|
2014-10-20 16:55:07 -04:00
|
|
|
m_CurrentRecipe(nullptr),
|
2014-10-16 14:08:22 -04:00
|
|
|
m_IsDestroyed(false),
|
2014-10-18 14:54:34 -04:00
|
|
|
m_IsCooking(a_BlockType == E_BLOCK_LIT_FURNACE),
|
2013-06-16 16:24:07 -04:00
|
|
|
m_NeedCookTime(0),
|
|
|
|
m_TimeCooked(0),
|
|
|
|
m_FuelBurnTime(0),
|
2014-10-16 14:08:22 -04:00
|
|
|
m_TimeBurned(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
m_Contents.AddListener(*this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
cFurnaceEntity::~cFurnaceEntity()
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
// Tell window its owner is destroyed
|
|
|
|
cWindow * Window = GetWindow();
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
Window->OwnerDestroyed();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
void cFurnaceEntity::UsedBy(cPlayer * a_Player)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-10-03 16:32:41 -04:00
|
|
|
cWindow * Window = GetWindow();
|
2014-10-16 15:21:33 -04:00
|
|
|
if (Window == nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-20 09:25:54 -04:00
|
|
|
OpenWindow(new cFurnaceWindow(m_PosX, m_PosY, m_PosZ, this));
|
2014-10-03 16:32:41 -04:00
|
|
|
Window = GetWindow();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-03 16:32:41 -04:00
|
|
|
|
2014-10-16 15:21:33 -04:00
|
|
|
if (Window != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
if (a_Player->GetWindow() != Window)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
a_Player->OpenWindow(Window);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-03 16:32:41 -04:00
|
|
|
|
|
|
|
UpdateProgressBars(true);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
bool cFurnaceEntity::ContinueCooking(void)
|
|
|
|
{
|
|
|
|
UpdateInput();
|
|
|
|
UpdateFuel();
|
|
|
|
return m_IsCooking;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-28 14:50:44 -04:00
|
|
|
bool cFurnaceEntity::Tick(float a_Dt, cChunk & a_Chunk)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-02-24 14:29:59 -05:00
|
|
|
UNUSED(a_Dt);
|
2014-10-03 16:32:41 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
if (m_FuelBurnTime <= 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-10-18 11:12:12 -04:00
|
|
|
// If a furnace is out of fuel, the progress bar reverses at twice the speed of cooking.
|
|
|
|
m_TimeCooked = std::max((m_TimeCooked - 2), 0);
|
2014-10-03 16:32:41 -04:00
|
|
|
|
|
|
|
// Reset progressbars, block type, and bail out
|
2014-10-18 14:54:34 -04:00
|
|
|
m_BlockType = E_BLOCK_FURNACE;
|
2014-10-03 16:32:41 -04:00
|
|
|
a_Chunk.FastSetBlock(GetRelX(), m_PosY, GetRelZ(), E_BLOCK_FURNACE, m_BlockMeta);
|
|
|
|
UpdateProgressBars();
|
2012-06-14 09:06:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
if (m_IsCooking)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
m_TimeCooked++;
|
|
|
|
if (m_TimeCooked >= m_NeedCookTime)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
// Finished smelting one item
|
2014-02-24 14:29:59 -05:00
|
|
|
FinishOne();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
m_TimeBurned++;
|
|
|
|
if (m_TimeBurned >= m_FuelBurnTime)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
// The current fuel has been exhausted, use another one, if possible
|
|
|
|
BurnNewFuel();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
UpdateProgressBars();
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
void cFurnaceEntity::SendTo(cClientHandle & a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
// Nothing needs to be sent
|
|
|
|
UNUSED(a_Client);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-03 16:32:41 -04:00
|
|
|
void cFurnaceEntity::BroadcastProgress(short a_ProgressbarID, short a_Value)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
cWindow * Window = GetWindow();
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-10-03 16:32:41 -04:00
|
|
|
Window->SetProperty(a_ProgressbarID, a_Value);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-24 14:29:59 -05:00
|
|
|
void cFurnaceEntity::FinishOne()
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
m_TimeCooked = 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
if (m_Contents.GetSlot(fsOutput).IsEmpty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
m_Contents.SetSlot(fsOutput, *m_CurrentRecipe->Out);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Contents.ChangeSlotCount(fsOutput, m_CurrentRecipe->Out->m_ItemCount);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-06-16 16:24:07 -04:00
|
|
|
m_Contents.ChangeSlotCount(fsInput, -m_CurrentRecipe->In->m_ItemCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
|
|
|
|
void cFurnaceEntity::BurnNewFuel(void)
|
|
|
|
{
|
|
|
|
cFurnaceRecipe * FR = cRoot::Get()->GetFurnaceRecipe();
|
|
|
|
int NewTime = FR->GetBurnTime(m_Contents.GetSlot(fsFuel));
|
|
|
|
if (NewTime == 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
// The item in the fuel slot is not suitable
|
2014-10-03 16:32:41 -04:00
|
|
|
SetBurnTimes(0, 0);
|
2013-06-20 07:41:44 -04:00
|
|
|
SetIsCooking(false);
|
2013-06-16 16:24:07 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
// Is the input and output ready for cooking?
|
|
|
|
if (!CanCookInputToOutput())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Burn one new fuel:
|
2014-10-03 16:32:41 -04:00
|
|
|
SetBurnTimes(NewTime, 0);
|
2013-06-20 07:41:44 -04:00
|
|
|
SetIsCooking(true);
|
2013-06-16 16:24:07 -04:00
|
|
|
if (m_Contents.GetSlot(fsFuel).m_ItemType == E_ITEM_LAVA_BUCKET)
|
|
|
|
{
|
|
|
|
m_Contents.SetSlot(fsFuel, cItem(E_ITEM_BUCKET));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Contents.ChangeSlotCount(fsFuel, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cFurnaceEntity::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum)
|
|
|
|
{
|
|
|
|
super::OnSlotChanged(a_ItemGrid, a_SlotNum);
|
2014-10-03 16:32:41 -04:00
|
|
|
|
|
|
|
if (m_IsDestroyed)
|
2013-06-16 16:24:07 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
ASSERT(a_ItemGrid == &m_Contents);
|
|
|
|
switch (a_SlotNum)
|
|
|
|
{
|
2014-10-21 12:12:40 -04:00
|
|
|
case fsInput: UpdateInput(); break;
|
|
|
|
case fsFuel: UpdateFuel(); break;
|
2014-10-03 16:32:41 -04:00
|
|
|
case fsOutput: UpdateOutput(); break;
|
|
|
|
default: ASSERT(!"Invalid furnace slot update!"); break;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-06-16 16:24:07 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cFurnaceEntity::UpdateInput(void)
|
|
|
|
{
|
2014-01-16 14:00:49 -05:00
|
|
|
if (!m_Contents.GetSlot(fsInput).IsEqual(m_LastInput))
|
2013-06-16 16:24:07 -04:00
|
|
|
{
|
|
|
|
// The input is different from what we had before, reset the cooking time
|
|
|
|
m_TimeCooked = 0;
|
|
|
|
}
|
|
|
|
m_LastInput = m_Contents.GetSlot(fsInput);
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
cFurnaceRecipe * FR = cRoot::Get()->GetFurnaceRecipe();
|
|
|
|
m_CurrentRecipe = FR->GetRecipeFrom(m_Contents.GetSlot(fsInput));
|
|
|
|
if (!CanCookInputToOutput())
|
|
|
|
{
|
2014-10-03 16:32:41 -04:00
|
|
|
// This input cannot be cooked, reset cook counter immediately
|
|
|
|
SetCookTimes(0, 0);
|
2013-06-20 07:41:44 -04:00
|
|
|
SetIsCooking(false);
|
2013-06-16 16:24:07 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_NeedCookTime = m_CurrentRecipe->CookTime;
|
2013-06-20 07:41:44 -04:00
|
|
|
SetIsCooking(true);
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
// Start burning new fuel if there's no flame now:
|
|
|
|
if (GetFuelBurnTimeLeft() <= 0)
|
|
|
|
{
|
|
|
|
BurnNewFuel();
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
void cFurnaceEntity::UpdateFuel(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
if (m_FuelBurnTime > m_TimeBurned)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
// The current fuel is still burning, don't modify anything:
|
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
// The current fuel is spent, try to burn some more:
|
|
|
|
BurnNewFuel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
void cFurnaceEntity::UpdateOutput(void)
|
|
|
|
{
|
|
|
|
if (!CanCookInputToOutput())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
// Cannot cook anymore:
|
2014-10-03 16:32:41 -04:00
|
|
|
SetCookTimes(0, 0);
|
2013-06-20 07:41:44 -04:00
|
|
|
SetIsCooking(false);
|
2013-06-16 16:24:07 -04:00
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
// No need to burn new fuel, the Tick() function will take care of that
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
// Can cook, start cooking if not already underway:
|
|
|
|
m_NeedCookTime = m_CurrentRecipe->CookTime;
|
2013-06-20 07:41:44 -04:00
|
|
|
SetIsCooking(m_FuelBurnTime > 0);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
bool cFurnaceEntity::CanCookInputToOutput(void) const
|
2012-09-20 09:25:54 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_CurrentRecipe == nullptr)
|
2012-09-20 09:25:54 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
// This input cannot be cooked
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2014-04-18 15:09:44 -04:00
|
|
|
const cItem & Slot = m_Contents.GetSlot(fsOutput);
|
|
|
|
if (Slot.IsEmpty())
|
2013-06-16 16:24:07 -04:00
|
|
|
{
|
|
|
|
// The output is empty, can cook
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-18 15:09:44 -04:00
|
|
|
if (!Slot.IsEqual(*m_CurrentRecipe->Out))
|
2013-06-16 16:24:07 -04:00
|
|
|
{
|
|
|
|
// The output slot is blocked with something that cannot be stacked with the recipe's output
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2014-04-18 15:09:44 -04:00
|
|
|
if (Slot.IsFullStack())
|
2013-06-16 16:24:07 -04:00
|
|
|
{
|
|
|
|
// Cannot add any more items to the output slot
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-03 16:32:41 -04:00
|
|
|
void cFurnaceEntity::UpdateProgressBars(bool a_ForceUpdate)
|
2013-06-16 16:24:07 -04:00
|
|
|
{
|
|
|
|
// In order to preserve bandwidth, an update is sent only every 10th tick
|
2014-10-03 16:32:41 -04:00
|
|
|
if (!a_ForceUpdate && (m_World->GetWorldAge() % 10 != 0))
|
2013-06-16 16:24:07 -04:00
|
|
|
{
|
2014-10-03 16:32:41 -04:00
|
|
|
return;
|
2013-06-16 16:24:07 -04:00
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2014-10-03 16:32:41 -04:00
|
|
|
int CurFuel = (m_FuelBurnTime > 0) ? 200 - (200 * m_TimeBurned / m_FuelBurnTime) : 0;
|
2014-10-16 14:08:22 -04:00
|
|
|
BroadcastProgress(PROGRESSBAR_FUEL, static_cast<short>(CurFuel));
|
2014-10-03 16:32:41 -04:00
|
|
|
|
2014-10-21 16:00:31 -04:00
|
|
|
int CurCook = (m_NeedCookTime > 0) ? (200 * m_TimeCooked / m_NeedCookTime) : 0;
|
2014-10-03 16:32:41 -04:00
|
|
|
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.
|
2014-10-16 14:08:22 -04:00
|
|
|
BroadcastProgress(PROGRESSBAR_SMELTING, static_cast<short>(CurCook));
|
2012-09-20 09:25:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 07:41:44 -04:00
|
|
|
|
|
|
|
void cFurnaceEntity::SetIsCooking(bool a_IsCooking)
|
|
|
|
{
|
|
|
|
if (a_IsCooking == m_IsCooking)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_IsCooking = a_IsCooking;
|
2014-10-03 16:32:41 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2014-10-18 14:54:34 -04:00
|
|
|
m_BlockType = E_BLOCK_LIT_FURNACE;
|
2014-10-03 16:32:41 -04:00
|
|
|
m_World->FastSetBlock(m_PosX, m_PosY, m_PosZ, E_BLOCK_LIT_FURNACE, m_BlockMeta);
|
|
|
|
}
|
2013-06-20 07:41:44 -04:00
|
|
|
}
|