From 5b62c4c3145c08b093521e42c565922fa85de4ad Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 26 Jun 2015 17:24:51 -0500 Subject: Reorganised the redstone simulator -> Many thanks to @worktycho for the idea, and @Haxi52 for the implementation plan! * Uses classes and inheritance now * Speed should be improved --- src/Blocks/BlockButton.h | 26 ++++++++++++++------- src/Blocks/BlockComparator.h | 47 ++++++++++++++++++++------------------ src/Blocks/BlockLever.h | 6 +++++ src/Blocks/BlockPiston.h | 2 +- src/Blocks/BlockRail.h | 7 +++--- src/Blocks/BlockRedstoneRepeater.h | 36 +++++++++++++++++++++++++++++ src/Blocks/ChunkInterface.cpp | 10 -------- src/Blocks/ChunkInterface.h | 2 -- 8 files changed, 90 insertions(+), 46 deletions(-) (limited to 'src/Blocks') diff --git a/src/Blocks/BlockButton.h b/src/Blocks/BlockButton.h index 7f6c3e56f..354d563fd 100644 --- a/src/Blocks/BlockButton.h +++ b/src/Blocks/BlockButton.h @@ -38,14 +38,18 @@ public: a_WorldInterface.GetBroadcastManager().BroadcastSoundEffect("random.click", x, y, z, 0.5f, 0.6f); // Queue a button reset (unpress) - int delay = (m_BlockType == E_BLOCK_STONE_BUTTON) ? 20 : 30; - - a_ChunkInterface.QueueSetBlock(a_BlockX, a_BlockY, a_BlockZ, m_BlockType, (a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) & 0x07), delay, m_BlockType, a_WorldInterface); - - a_Player->GetWorld()->ScheduleTask(delay, [x, y, z](cWorld & a_World) - { - a_World.BroadcastSoundEffect("random.click", x, y, z, 0.5f, 0.5f); - }); + auto TickDelay = (m_BlockType == E_BLOCK_STONE_BUTTON) ? 20 : 30; + a_Player->GetWorld()->ScheduleTask(TickDelay, [x, y, z, a_BlockX, a_BlockY, a_BlockZ, this](cWorld & a_World) + { + if (a_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ) == m_BlockType) + { + // Block hasn't change in the meantime; set its meta + a_World.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, a_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) & 0x07); + a_World.WakeUpSimulators(a_BlockX, a_BlockY, a_BlockZ); + a_World.BroadcastSoundEffect("random.click", x, y, z, 0.5f, 0.5f); + } + } + ); return true; } @@ -129,6 +133,12 @@ public: UNUSED(a_Meta); return 0; } + + /** Extracts the ON bit from metadata and returns if true if it is set */ + static bool IsButtonOn(NIBBLETYPE a_BlockMeta) + { + return ((a_BlockMeta & 0x8) == 0x8); + } } ; diff --git a/src/Blocks/BlockComparator.h b/src/Blocks/BlockComparator.h index d4ec08dff..8f1822299 100644 --- a/src/Blocks/BlockComparator.h +++ b/src/Blocks/BlockComparator.h @@ -60,16 +60,17 @@ public: return true; } - inline static Vector3i GetSideCoordinate(int a_BlockX, int a_BlockY, int a_BlockZ, NIBBLETYPE a_Meta, bool a_bInverse) + inline static Vector3i GetSideCoordinate(const Vector3i & a_Position, NIBBLETYPE a_Meta, bool a_bInverse) { + auto Position = a_Position; if (!a_bInverse) { switch (a_Meta) { - case 0x0: a_BlockX++; break; - case 0x1: a_BlockZ--; break; - case 0x2: a_BlockX--; break; - case 0x3: a_BlockZ++; break; + case 0x0: Position.x++; break; + case 0x1: Position.z--; break; + case 0x2: Position.x--; break; + case 0x3: Position.z++; break; default: { LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta); @@ -82,10 +83,10 @@ public: { switch (a_Meta) { - case 0x0: a_BlockX--; break; - case 0x1: a_BlockZ++; break; - case 0x2: a_BlockX++; break; - case 0x3: a_BlockZ--; break; + case 0x0: Position.x--; break; + case 0x1: Position.z++; break; + case 0x2: Position.x++; break; + case 0x3: Position.z--; break; default: { LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta); @@ -95,17 +96,18 @@ public: } } - return Vector3i(a_BlockX, a_BlockY, a_BlockZ); + return Position; } - inline static Vector3i GetRearCoordinate(int a_BlockX, int a_BlockY, int a_BlockZ, NIBBLETYPE a_Meta) + inline static Vector3i GetRearCoordinate(const Vector3i & a_Position, NIBBLETYPE a_Meta) { + auto Position = a_Position; switch (a_Meta) { - case 0x0: a_BlockZ++; break; - case 0x1: a_BlockX--; break; - case 0x2: a_BlockZ--; break; - case 0x3: a_BlockX++; break; + case 0x0: Position.z++; break; + case 0x1: Position.x--; break; + case 0x2: Position.z--; break; + case 0x3: Position.x++; break; default: { LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta); @@ -114,17 +116,18 @@ public: } } - return Vector3i(a_BlockX, a_BlockY, a_BlockZ); + return Position; } - inline static Vector3i GetFrontCoordinate(int a_BlockX, int a_BlockY, int a_BlockZ, NIBBLETYPE a_Meta) + inline static Vector3i GetFrontCoordinate(const Vector3i & a_Position, NIBBLETYPE a_Meta) { + auto Position = a_Position; switch (a_Meta) { - case 0x0: a_BlockZ--; break; - case 0x1: a_BlockX++; break; - case 0x2: a_BlockZ++; break; - case 0x3: a_BlockX--; break; + case 0x0: Position.z--; break; + case 0x1: Position.x++; break; + case 0x2: Position.z++; break; + case 0x3: Position.x--; break; default: { LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta); @@ -133,7 +136,7 @@ public: } } - return Vector3i(a_BlockX, a_BlockY, a_BlockZ); + return Position; } virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override diff --git a/src/Blocks/BlockLever.h b/src/Blocks/BlockLever.h index 85fd2f28d..a3bbd54f5 100644 --- a/src/Blocks/BlockLever.h +++ b/src/Blocks/BlockLever.h @@ -156,6 +156,12 @@ public: UNUSED(a_Meta); return 0; } + + /** Extracts the ON bit from metadata and returns if true if it is set */ + static bool IsLeverOn(NIBBLETYPE a_BlockMeta) + { + return ((a_BlockMeta & 0x8) == 0x8); + } } ; diff --git a/src/Blocks/BlockPiston.h b/src/Blocks/BlockPiston.h index e0066e8ab..6a384b85d 100644 --- a/src/Blocks/BlockPiston.h +++ b/src/Blocks/BlockPiston.h @@ -65,7 +65,7 @@ public: static eBlockFace MetaDataToDirection(NIBBLETYPE a_MetaData) { - switch (a_MetaData) + switch (a_MetaData & 0x7) // We only want the bottom three bits (4th controls extended-ness)) { case 0x0: return BLOCK_FACE_YM; case 0x1: return BLOCK_FACE_YP; diff --git a/src/Blocks/BlockRail.h b/src/Blocks/BlockRail.h index b603bb1a7..734ee93c7 100644 --- a/src/Blocks/BlockRail.h +++ b/src/Blocks/BlockRail.h @@ -73,10 +73,11 @@ public: virtual void OnNeighborChanged(cChunkInterface & a_ChunkInterface, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_WhichNeighbor) override { - NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); - if (IsUnstable(a_ChunkInterface, a_BlockX, a_BlockY, a_BlockZ) && (Meta != FindMeta(a_ChunkInterface, a_BlockX, a_BlockY, a_BlockZ))) + auto Meta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + auto NewMeta = FindMeta(a_ChunkInterface, a_BlockX, a_BlockY, a_BlockZ); + if (IsUnstable(a_ChunkInterface, a_BlockX, a_BlockY, a_BlockZ) && (Meta != NewMeta)) { - a_ChunkInterface.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, m_BlockType, FindMeta(a_ChunkInterface, a_BlockX, a_BlockY, a_BlockZ)); + a_ChunkInterface.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, m_BlockType, (m_BlockType == E_BLOCK_RAIL) ? NewMeta : NewMeta | (Meta & 0x08)); } } diff --git a/src/Blocks/BlockRedstoneRepeater.h b/src/Blocks/BlockRedstoneRepeater.h index 5f413e10a..033ba82de 100644 --- a/src/Blocks/BlockRedstoneRepeater.h +++ b/src/Blocks/BlockRedstoneRepeater.h @@ -110,6 +110,42 @@ public: UNUSED(a_Meta); return 11; } + + + inline static Vector3i GetRearCoordinateOffset(NIBBLETYPE a_Meta) + { + switch (a_Meta & 0x3) // We only want the direction (bottom) bits + { + case 0x0: return {0, 0, 1}; + case 0x1: return {-1, 0, 0}; + case 0x2: return {0, 0, -1}; + case 0x3: return {1, 0, 0}; + default: + { + LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta); + ASSERT(!"Unknown metadata while determining orientation of repeater!"); + return {0, 0, 0}; + } + } + } + + + inline static Vector3i GetFrontCoordinateOffset(NIBBLETYPE a_Meta) + { + switch (a_Meta & 0x3) // We only want the direction (bottom) bits + { + case 0x0: return {0, 0, -1}; + case 0x1: return {1, 0, 0}; + case 0x2: return {0, 0, 1}; + case 0x3: return {-1, 0, 0}; + default: + { + LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta); + ASSERT(!"Unknown metadata while determining orientation of repeater!"); + return {0, 0, 0}; + } + } + } } ; diff --git a/src/Blocks/ChunkInterface.cpp b/src/Blocks/ChunkInterface.cpp index d8a7fc919..7fa2981e9 100644 --- a/src/Blocks/ChunkInterface.cpp +++ b/src/Blocks/ChunkInterface.cpp @@ -72,16 +72,6 @@ void cChunkInterface::SetBlockMeta(int a_BlockX, int a_BlockY, int a_BlockZ, NIB -void cChunkInterface::QueueSetBlock(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, int a_TickDelay, BLOCKTYPE a_PreviousBlockType, cWorldInterface & a_WorldInterface) -{ - m_ChunkMap->QueueSetBlock(a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta, a_WorldInterface.GetWorldAge() + a_TickDelay, a_PreviousBlockType); -} - - - - - - /** Sets the block at the specified coords to the specified value. The replacement doesn't trigger block updates. The replaced blocks aren't checked for block entities (block entity is leaked if it exists at this block) diff --git a/src/Blocks/ChunkInterface.h b/src/Blocks/ChunkInterface.h index 7b5f0d7fc..ee9bf9da7 100644 --- a/src/Blocks/ChunkInterface.h +++ b/src/Blocks/ChunkInterface.h @@ -29,8 +29,6 @@ public: void SetBlockMeta(int a_BlockX, int a_BlockY, int a_BlockZ, NIBBLETYPE a_MetaData); - void QueueSetBlock(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, int a_TickDelay, BLOCKTYPE a_PreviousBlockType, cWorldInterface & a_WorldInterface); - /** Sets the block at the specified coords to the specified value. The replacement doesn't trigger block updates. The replaced blocks aren't checked for block entities (block entity is leaked if it exists at this block) -- cgit v1.2.3