1
0
Fork 0

At long last... Piston animations!

* Fixes #3198
* Fixes #57 (again lol)
This commit is contained in:
Tiger Wang 2018-07-24 00:35:20 +01:00
parent 74f5160332
commit b8ab03bc6b
4 changed files with 117 additions and 86 deletions

View File

@ -214,9 +214,31 @@ bool cBlockPistonHandler::CanPushBlock(
void cBlockPistonHandler::ExtendPiston(Vector3i a_BlockPos, cWorld & a_World) void cBlockPistonHandler::ExtendPiston(Vector3i a_BlockPos, cWorld & a_World)
{ {
{
// Broadcast block action first. Will do nothing if piston cannot in fact push
BLOCKTYPE pistonBlock; BLOCKTYPE pistonBlock;
NIBBLETYPE pistonMeta; NIBBLETYPE pistonMeta;
a_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta); a_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
a_World.BroadcastBlockAction(a_BlockPos, PistonExtendAction, pistonMeta, pistonBlock);
}
// Client expects the server to "play" the animation before setting the final blocks
// However, we don't confuse animation with the underlying state of the world, so emulate by delaying 1 tick
// (Probably why vanilla has so many dupe glitches with sand and pistons lolol)
a_World.ScheduleTask(1, [a_BlockPos](cWorld & World)
{
BLOCKTYPE pistonBlock;
NIBBLETYPE pistonMeta;
World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
if ((pistonBlock != E_BLOCK_PISTON) && !IsSticky(pistonBlock))
{
// Ensure we operate on a piston to avoid spurious behaviour
// Note that the scheduled task may result in the block type of a_BlockPos changing
return;
}
if (IsExtended(pistonMeta)) if (IsExtended(pistonMeta))
{ {
@ -225,26 +247,26 @@ void cBlockPistonHandler::ExtendPiston(Vector3i a_BlockPos, cWorld & a_World)
} }
Vector3i pushDir = MetadataToOffset(pistonMeta); Vector3i pushDir = MetadataToOffset(pistonMeta);
Vector3iSet blocksPushed; Vector3iSet blocksPushed;
if (!CanPushBlock(a_BlockPos + pushDir, a_World, true, blocksPushed, pushDir)) if (!CanPushBlock(a_BlockPos + pushDir, World, true, blocksPushed, pushDir))
{ {
// Can't push anything, bail out // Can't push anything, bail out
return; return;
} }
PushBlocks(blocksPushed, World, pushDir);
a_World.BroadcastBlockAction(a_BlockPos, 0, pistonMeta, pistonBlock);
a_World.BroadcastSoundEffect("block.piston.extend", a_BlockPos, 0.5f, 0.7f);
PushBlocks(blocksPushed, a_World, pushDir);
// Set the extension and the piston base correctly // Set the extension and the piston base correctly
Vector3i extensionPos = a_BlockPos + pushDir; Vector3i extensionPos = a_BlockPos + pushDir;
a_World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta | 0x8); World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta | 0x8);
a_World.SetBlock( World.SetBlock(
extensionPos.x, extensionPos.y, extensionPos.z, extensionPos.x, extensionPos.y, extensionPos.z,
E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0) E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0)
); );
// Play sound effect only if extended successfully
World.BroadcastSoundEffect("block.piston.extend", a_BlockPos, 0.5f, 0.7f);
}
);
} }
@ -252,10 +274,26 @@ void cBlockPistonHandler::ExtendPiston(Vector3i a_BlockPos, cWorld & a_World)
void cBlockPistonHandler::RetractPiston(Vector3i a_BlockPos, cWorld & a_World) void cBlockPistonHandler::RetractPiston(Vector3i a_BlockPos, cWorld & a_World)
{
{ {
BLOCKTYPE pistonBlock; BLOCKTYPE pistonBlock;
NIBBLETYPE pistonMeta; NIBBLETYPE pistonMeta;
a_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta); a_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
a_World.BroadcastBlockAction(a_BlockPos, PistonRetractAction, pistonMeta, pistonBlock);
}
a_World.ScheduleTask(1, [a_BlockPos](cWorld & World)
{
BLOCKTYPE pistonBlock;
NIBBLETYPE pistonMeta;
World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
if ((pistonBlock != E_BLOCK_PISTON) && !IsSticky(pistonBlock))
{
// Ensure we operate on a piston to avoid spurious behaviour
// Note that the scheduled task may result in the block type of a_BlockPos changing
return;
}
if (!IsExtended(pistonMeta)) if (!IsExtended(pistonMeta))
{ {
@ -267,18 +305,18 @@ void cBlockPistonHandler::RetractPiston(Vector3i a_BlockPos, cWorld & a_World)
// Check the extension: // Check the extension:
Vector3i extensionPos = a_BlockPos + pushDir; Vector3i extensionPos = a_BlockPos + pushDir;
if (a_World.GetBlock(extensionPos) != E_BLOCK_PISTON_EXTENSION) if (World.GetBlock(extensionPos) != E_BLOCK_PISTON_EXTENSION)
{ {
LOGD("%s: Piston without an extension - still extending, or just in an invalid state?", __FUNCTION__); LOGD("%s: Piston without an extension - still extending, or just in an invalid state?", __FUNCTION__);
return; return;
} }
// Remove Extension // Remove extension, update base state
a_World.SetBlock(extensionPos.x, extensionPos.y, extensionPos.z, E_BLOCK_AIR, 0); World.SetBlock(extensionPos.x, extensionPos.y, extensionPos.z, E_BLOCK_AIR, 0);
World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta & ~(8));
a_World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta & ~(8)); // (Retraction is always successful, but play in the task for consistency)
a_World.BroadcastBlockAction(a_BlockPos, 1, pistonMeta & ~(8), pistonBlock); World.BroadcastSoundEffect("block.piston.contract", a_BlockPos, 0.5f, 0.7f);
a_World.BroadcastSoundEffect("block.piston.contract", a_BlockPos, 0.5f, 0.7f);
if (!IsSticky(pistonBlock)) if (!IsSticky(pistonBlock))
{ {
@ -287,18 +325,20 @@ void cBlockPistonHandler::RetractPiston(Vector3i a_BlockPos, cWorld & a_World)
} }
// Get the block to pull // Get the block to pull
a_BlockPos += pushDir * 2; Vector3i AdjustedPosition = a_BlockPos + pushDir * 2;
// Try to "push" the pulling block in the opposite direction // Try to "push" the pulling block in the opposite direction
pushDir *= -1; pushDir *= -1;
Vector3iSet pushedBlocks; Vector3iSet pushedBlocks;
if (!CanPushBlock(a_BlockPos, a_World, false, pushedBlocks, pushDir)) if (!CanPushBlock(AdjustedPosition, World, false, pushedBlocks, pushDir))
{ {
// Not pushable, bail out // Not pushable, bail out
return; return;
} }
PushBlocks(pushedBlocks, a_World, pushDir); PushBlocks(pushedBlocks, World, pushDir);
}
);
} }

View File

@ -100,6 +100,12 @@ private:
typedef std::unordered_set<Vector3i, VectorHasher<int>> Vector3iSet; typedef std::unordered_set<Vector3i, VectorHasher<int>> Vector3iSet;
/** Piston extension block action */
static const Byte PistonExtendAction = 0U;
/** Piston retraction block action */
static const Byte PistonRetractAction = 1U;
/** Returns true if the piston (specified by blocktype) is a sticky piston */ /** Returns true if the piston (specified by blocktype) is a sticky piston */
static inline bool IsSticky(BLOCKTYPE a_BlockType) { return (a_BlockType == E_BLOCK_STICKY_PISTON); } static inline bool IsSticky(BLOCKTYPE a_BlockType) { return (a_BlockType == E_BLOCK_STICKY_PISTON); }

View File

@ -36,26 +36,13 @@ public:
virtual cVector3iArray Update(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) const override virtual cVector3iArray Update(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) const override
{ {
// LOGD("Evaluating pisty the piston (%d %d %d)", a_Position.x, a_Position.y, a_Position.z); // LOGD("Evaluating pisty the piston (%d %d %d)", a_Position.x, a_Position.y, a_Position.z);
auto Data = static_cast<cIncrementalRedstoneSimulator *>(a_World.GetRedstoneSimulator())->GetChunkData();
auto DelayInfo = Data->GetMechanismDelayInfo(a_Position);
// Delay is used here to prevent an infinite loop (#3168)
if (DelayInfo == nullptr)
{
bool ShouldBeExtended = (a_PoweringData.PowerLevel != 0); bool ShouldBeExtended = (a_PoweringData.PowerLevel != 0);
if (ShouldBeExtended != cBlockPistonHandler::IsExtended(a_Meta)) if (ShouldBeExtended == cBlockPistonHandler::IsExtended(a_Meta))
{ {
Data->m_MechanismDelays[a_Position] = std::make_pair(1, ShouldBeExtended); return {};
} }
}
else
{
int DelayTicks;
bool ShouldBeExtended;
std::tie(DelayTicks, ShouldBeExtended) = *DelayInfo;
if (DelayTicks == 0)
{
if (ShouldBeExtended) if (ShouldBeExtended)
{ {
cBlockPistonHandler::ExtendPiston(a_Position, a_World); cBlockPistonHandler::ExtendPiston(a_Position, a_World);
@ -65,9 +52,8 @@ public:
cBlockPistonHandler::RetractPiston(a_Position, a_World); cBlockPistonHandler::RetractPiston(a_Position, a_World);
} }
Data->m_MechanismDelays.erase(a_Position); // It is necessary to delay after a signal to prevent an infinite loop (#3168)
} // However, that is present as a side effect of the implementation of piston animation in Blocks\BlockPiston.cpp
}
return {}; return {};
} }

View File

@ -60,7 +60,6 @@ public:
/** Structure storing position of mechanism + it's delay ticks (countdown) & if to power on */ /** Structure storing position of mechanism + it's delay ticks (countdown) & if to power on */
std::unordered_map<Vector3i, std::pair<int, bool>, VectorHasher<int>> m_MechanismDelays; std::unordered_map<Vector3i, std::pair<int, bool>, VectorHasher<int>> m_MechanismDelays;
std::unordered_map<Vector3i, bool, VectorHasher<int>> m_UpdateOncePositions;
private: private: