From bc1e236d547479833cc4f8d8218064cbdb9dfc0d Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 18 Nov 2013 22:30:34 +0000 Subject: Redstone Megacommit [SEE DESC] + Updated BlockID - look for yourself * Improved button, repeater, lever, and comparator code -> simplification and splitting of some stuff from the redstone simulator file * Fixed buttons not breaking when in an invalid game state * Fixed QueueSetBlock -> improved (AGAIN) piston code + Rewrote redstone simulator Fixes: #57, #58, #205, and part of #131. Fixes FS issues: 281, 116, and 102 --- source/BlockID.cpp | 29 +- source/Blocks/BlockButton.cpp | 21 +- source/Blocks/BlockButton.h | 36 +- source/Blocks/BlockComparator.cpp | 4 +- source/Blocks/BlockLever.cpp | 13 +- source/Blocks/BlockLever.h | 19 +- source/Blocks/BlockRedstoneRepeater.cpp | 3 +- source/Blocks/BlockRedstoneRepeater.h | 27 + source/Chunk.cpp | 14 +- source/Chunk.h | 3 + source/Items/ItemComparator.h | 4 +- source/Items/ItemRedstoneRepeater.h | 4 +- source/Piston.cpp | 21 +- source/Piston.h | 3 - source/Simulator/RedstoneSimulator.cpp | 1631 +++++++++++++++---------------- source/Simulator/RedstoneSimulator.h | 217 +++- 16 files changed, 1047 insertions(+), 1002 deletions(-) (limited to 'source') diff --git a/source/BlockID.cpp b/source/BlockID.cpp index 4467d7e82..a4a1ab2d8 100644 --- a/source/BlockID.cpp +++ b/source/BlockID.cpp @@ -669,13 +669,13 @@ public: g_BlockTransparent[E_BLOCK_FLOWER_POT] = true; g_BlockTransparent[E_BLOCK_GLASS] = true; g_BlockTransparent[E_BLOCK_GLASS_PANE] = true; - g_BlockTransparent[E_BLOCK_STAINED_GLASS] = true; - g_BlockTransparent[E_BLOCK_STAINED_GLASS_PANE] = true; + g_BlockTransparent[E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE] = true; g_BlockTransparent[E_BLOCK_ICE] = true; g_BlockTransparent[E_BLOCK_IRON_DOOR] = true; g_BlockTransparent[E_BLOCK_LAVA] = true; g_BlockTransparent[E_BLOCK_LEAVES] = true; g_BlockTransparent[E_BLOCK_LEVER] = true; + g_BlockTransparent[E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE] = true; g_BlockTransparent[E_BLOCK_MELON_STEM] = true; g_BlockTransparent[E_BLOCK_NETHER_BRICK_FENCE] = true; g_BlockTransparent[E_BLOCK_NEW_LEAVES] = true; @@ -687,6 +687,8 @@ public: g_BlockTransparent[E_BLOCK_RED_MUSHROOM] = true; g_BlockTransparent[E_BLOCK_SIGN_POST] = true; g_BlockTransparent[E_BLOCK_SNOW] = true; + g_BlockTransparent[E_BLOCK_STAINED_GLASS] = true; + g_BlockTransparent[E_BLOCK_STAINED_GLASS_PANE] = true; g_BlockTransparent[E_BLOCK_STATIONARY_LAVA] = true; g_BlockTransparent[E_BLOCK_STATIONARY_WATER] = true; g_BlockTransparent[E_BLOCK_STONE_PRESSURE_PLATE] = true; @@ -700,7 +702,7 @@ public: // TODO: Any other transparent blocks? - // One hit break blocks + // One hit break blocks: g_BlockOneHitDig[E_BLOCK_ACTIVE_COMPARATOR] = true; g_BlockOneHitDig[E_BLOCK_BIG_FLOWER] = true; g_BlockOneHitDig[E_BLOCK_BROWN_MUSHROOM] = true; @@ -711,7 +713,6 @@ public: g_BlockOneHitDig[E_BLOCK_FLOWER] = true; g_BlockOneHitDig[E_BLOCK_FLOWER_POT] = true; g_BlockOneHitDig[E_BLOCK_INACTIVE_COMPARATOR] = true; - g_BlockOneHitDig[E_BLOCK_LOCKED_CHEST] = true; g_BlockOneHitDig[E_BLOCK_MELON_STEM] = true; g_BlockOneHitDig[E_BLOCK_POTATOES] = true; g_BlockOneHitDig[E_BLOCK_PUMPKIN_STEM] = true; @@ -727,7 +728,7 @@ public: g_BlockOneHitDig[E_BLOCK_TALL_GRASS] = true; g_BlockOneHitDig[E_BLOCK_TORCH] = true; - // Blocks that breaks when pushed by piston + // Blocks that break when pushed by piston: g_BlockPistonBreakable[E_BLOCK_ACTIVE_COMPARATOR] = true; g_BlockPistonBreakable[E_BLOCK_AIR] = true; g_BlockPistonBreakable[E_BLOCK_BED] = true; @@ -765,11 +766,12 @@ public: g_BlockPistonBreakable[E_BLOCK_TORCH] = true; g_BlockPistonBreakable[E_BLOCK_VINES] = true; g_BlockPistonBreakable[E_BLOCK_WATER] = true; + g_BlockPistonBreakable[E_BLOCK_WOODEN_BUTTON] = true; g_BlockPistonBreakable[E_BLOCK_WOODEN_DOOR] = true; g_BlockPistonBreakable[E_BLOCK_WOODEN_PRESSURE_PLATE] = true; - // Blocks that can be snowed over: + // Blocks that cannot be snowed over: g_BlockIsSnowable[E_BLOCK_ACTIVE_COMPARATOR] = false; g_BlockIsSnowable[E_BLOCK_AIR] = false; g_BlockIsSnowable[E_BLOCK_BIG_FLOWER] = false; @@ -785,7 +787,6 @@ public: g_BlockIsSnowable[E_BLOCK_INACTIVE_COMPARATOR] = false; g_BlockIsSnowable[E_BLOCK_LAVA] = false; g_BlockIsSnowable[E_BLOCK_LILY_PAD] = false; - g_BlockIsSnowable[E_BLOCK_LOCKED_CHEST] = false; g_BlockIsSnowable[E_BLOCK_REDSTONE_REPEATER_OFF] = false; g_BlockIsSnowable[E_BLOCK_REDSTONE_REPEATER_ON] = false; g_BlockIsSnowable[E_BLOCK_REDSTONE_TORCH_OFF] = false; @@ -796,6 +797,8 @@ public: g_BlockIsSnowable[E_BLOCK_SAPLING] = false; g_BlockIsSnowable[E_BLOCK_SIGN_POST] = false; g_BlockIsSnowable[E_BLOCK_SNOW] = false; + g_BlockIsSnowable[E_BLOCK_STAINED_GLASS] = false; + g_BlockIsSnowable[E_BLOCK_STAINED_GLASS_PANE] = false; g_BlockIsSnowable[E_BLOCK_STATIONARY_LAVA] = false; g_BlockIsSnowable[E_BLOCK_STATIONARY_WATER] = false; g_BlockIsSnowable[E_BLOCK_TALL_GRASS] = false; @@ -806,7 +809,7 @@ public: g_BlockIsSnowable[E_BLOCK_WATER] = false; - // Blocks that don't drop without a special tool + // Blocks that don't drop without a special tool: g_BlockRequiresSpecialTool[E_BLOCK_BRICK] = true; g_BlockRequiresSpecialTool[E_BLOCK_CAULDRON] = true; g_BlockRequiresSpecialTool[E_BLOCK_COAL_ORE] = true; @@ -841,7 +844,7 @@ public: g_BlockRequiresSpecialTool[E_BLOCK_STONE_SLAB] = true; g_BlockRequiresSpecialTool[E_BLOCK_VINES] = true; - // Nonsolid Blocks: + // Nonsolid blocks: g_BlockIsSolid[E_BLOCK_ACTIVATOR_RAIL] = false; g_BlockIsSolid[E_BLOCK_AIR] = false; g_BlockIsSolid[E_BLOCK_BIG_FLOWER] = false; @@ -860,11 +863,8 @@ public: g_BlockIsSolid[E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE] = false; g_BlockIsSolid[E_BLOCK_MELON_STEM] = false; g_BlockIsSolid[E_BLOCK_NETHER_PORTAL] = false; - g_BlockIsSolid[E_BLOCK_PISTON] = false; g_BlockIsSolid[E_BLOCK_PISTON_EXTENSION] = false; g_BlockIsSolid[E_BLOCK_RAIL] = false; - g_BlockIsSolid[E_BLOCK_REDSTONE_REPEATER_OFF] = false; - g_BlockIsSolid[E_BLOCK_REDSTONE_REPEATER_ON] = false; g_BlockIsSolid[E_BLOCK_REDSTONE_TORCH_OFF] = false; g_BlockIsSolid[E_BLOCK_REDSTONE_TORCH_ON] = false; g_BlockIsSolid[E_BLOCK_REDSTONE_WIRE] = false; @@ -887,7 +887,7 @@ public: g_BlockIsSolid[E_BLOCK_WOODEN_PRESSURE_PLATE] = false; g_BlockIsSolid[E_BLOCK_WOODEN_SLAB] = false; - // Torch placeable + // Torch placeable blocks: g_BlockIsTorchPlaceable[E_BLOCK_BEDROCK] = true; g_BlockIsTorchPlaceable[E_BLOCK_BLOCK_OF_COAL] = true; g_BlockIsTorchPlaceable[E_BLOCK_BLOCK_OF_REDSTONE] = true; @@ -933,7 +933,7 @@ public: g_BlockIsTorchPlaceable[E_BLOCK_NETHER_QUARTZ_ORE] = true; g_BlockIsTorchPlaceable[E_BLOCK_NOTE_BLOCK] = true; g_BlockIsTorchPlaceable[E_BLOCK_OBSIDIAN] = true; - g_BlockIsTorchPlaceable[E_BLOCK_PACKED_ICE] = true; + g_BlockIsTorchPlaceable[E_BLOCK_PACKED_ICE] = true; g_BlockIsTorchPlaceable[E_BLOCK_PLANKS] = true; g_BlockIsTorchPlaceable[E_BLOCK_PUMPKIN] = true; g_BlockIsTorchPlaceable[E_BLOCK_QUARTZ_BLOCK] = true; @@ -955,3 +955,4 @@ public: + diff --git a/source/Blocks/BlockButton.cpp b/source/Blocks/BlockButton.cpp index 1011f9351..19b055b62 100644 --- a/source/Blocks/BlockButton.cpp +++ b/source/Blocks/BlockButton.cpp @@ -17,21 +17,14 @@ cBlockButtonHandler::cBlockButtonHandler(BLOCKTYPE a_BlockType) void cBlockButtonHandler::OnUse(cWorld *a_World, cPlayer *a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) { - // Flip the ON bit on/off. Using XOR bitwise operation to turn it on/off. + // Flip the ON bit on/off using the XOR bitwise operation NIBBLETYPE Meta = ((a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) ^ 0x08) & 0x0f); - a_World->SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, Meta); - - if (Meta & 0x08) - { - a_World->BroadcastSoundEffect("random.click", a_BlockX * 8, a_BlockY * 8, a_BlockZ * 8, 0.5f, 0.6f); - } - else - { - a_World->BroadcastSoundEffect("random.click", a_BlockX * 8, a_BlockY * 8, a_BlockZ * 8, 0.5f, 0.5f); - } - - // Queue a button reset (unpress), with a GetBlock to prevent duplication of buttons (press, break, wait for reset) - a_World->QueueSetBlock(a_BlockX, a_BlockY, a_BlockZ, a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ), ((a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) ^ 0x08) & 0x0f), m_BlockType == E_BLOCK_STONE_BUTTON ? 20 : 25); + + a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, m_BlockType, Meta); + a_World->BroadcastSoundEffect("random.click", a_BlockX * 8, a_BlockY * 8, a_BlockZ * 8, 0.5f, (Meta & 0x08) ? 0.6f : 0.5f); + + // Queue a button reset (unpress) + a_World->QueueSetBlock(a_BlockX, a_BlockY, a_BlockZ, m_BlockType, ((a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) ^ 0x08) & 0x0f), m_BlockType == E_BLOCK_STONE_BUTTON ? 20 : 30); } diff --git a/source/Blocks/BlockButton.h b/source/Blocks/BlockButton.h index e3f655bfa..15649acc0 100644 --- a/source/Blocks/BlockButton.h +++ b/source/Blocks/BlockButton.h @@ -14,11 +14,11 @@ public: virtual void OnUse(cWorld * a_World, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override; - + virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override { // Reset meta to 0 - a_Pickups.push_back(cItem(m_BlockType == E_BLOCK_WOODEN_BUTTON ? E_BLOCK_WOODEN_BUTTON : E_BLOCK_STONE_BUTTON, 1, 0)); + a_Pickups.push_back(cItem(m_BlockType, 1, 0)); } @@ -51,10 +51,10 @@ public: { switch (a_BlockFace) { - case BLOCK_FACE_ZP: { return 0x4; } - case BLOCK_FACE_ZM: { return 0x3; } - case BLOCK_FACE_XP: { return 0x2; } - case BLOCK_FACE_XM: { return 0x1; } + case BLOCK_FACE_ZM: { return 0x4; } + case BLOCK_FACE_ZP: { return 0x3; } + case BLOCK_FACE_XM: { return 0x2; } + case BLOCK_FACE_XP: { return 0x1; } default: { ASSERT(!"Unhandled block face!"); @@ -62,6 +62,30 @@ public: } } } + + inline static NIBBLETYPE BlockMetaDataToBlockFace(NIBBLETYPE a_Meta) + { + switch (a_Meta & 0x7) + { + case 0x1: return BLOCK_FACE_XP; + case 0x2: return BLOCK_FACE_XM; + case 0x3: return BLOCK_FACE_ZP; + case 0x4: return BLOCK_FACE_ZM; + default: + { + ASSERT(!"Unhandled block meta!"); + return BLOCK_FACE_NONE; + } + } + } + + virtual bool CanBeAt(int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override + { + NIBBLETYPE Meta = a_Chunk.GetMeta(a_RelX, a_RelY, a_RelZ); + + AddFaceDirection(a_RelX, a_RelY, a_RelZ, BlockMetaDataToBlockFace(Meta), true); + return (a_RelY > 0) && (g_BlockIsSolid[a_Chunk.GetBlock(a_RelX, a_RelY, a_RelZ)]); + } } ; diff --git a/source/Blocks/BlockComparator.cpp b/source/Blocks/BlockComparator.cpp index b4e5a55d0..8bc0ac5ac 100644 --- a/source/Blocks/BlockComparator.cpp +++ b/source/Blocks/BlockComparator.cpp @@ -1,7 +1,7 @@ #include "Globals.h" #include "BlockComparator.h" -#include "../Simulator/RedstoneSimulator.h" +#include "BlockRedstoneRepeater.h" #include "../Entities/Player.h" @@ -44,7 +44,7 @@ bool cBlockComparatorHandler::GetPlacementBlockTypeMeta( ) { a_BlockType = m_BlockType; - a_BlockMeta = cRedstoneSimulator::RepeaterRotationToMetaData(a_Player->GetRotation()); + a_BlockMeta = cBlockRedstoneRepeaterHandler::RepeaterRotationToMetaData(a_Player->GetRotation()); return true; } diff --git a/source/Blocks/BlockLever.cpp b/source/Blocks/BlockLever.cpp index a9bd6c990..2739fa3a9 100644 --- a/source/Blocks/BlockLever.cpp +++ b/source/Blocks/BlockLever.cpp @@ -19,18 +19,11 @@ cBlockLeverHandler::cBlockLeverHandler(BLOCKTYPE a_BlockType) void cBlockLeverHandler::OnUse(cWorld *a_World, cPlayer *a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) { - // Flip the ON bit on/off. Using XOR bitwise operation to turn it on/off. + // Flip the ON bit on/off using the XOR bitwise operation NIBBLETYPE Meta = ((a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) ^ 0x08) & 0x0f); - a_World->SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, Meta); - if (Meta & 0x08) - { - a_World->BroadcastSoundEffect("random.click", a_BlockX * 8, a_BlockY * 8, a_BlockZ * 8, 0.5f, 0.6f); - } - else - { - a_World->BroadcastSoundEffect("random.click", a_BlockX * 8, a_BlockY * 8, a_BlockZ * 8, 0.5f, 0.5f); - } + a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, m_BlockType, Meta); + a_World->BroadcastSoundEffect("random.click", a_BlockX * 8, a_BlockY * 8, a_BlockZ * 8, 0.5f, (Meta & 0x08) ? 0.6f : 0.5f); } diff --git a/source/Blocks/BlockLever.h b/source/Blocks/BlockLever.h index 5553170e2..fe7ecdf7e 100644 --- a/source/Blocks/BlockLever.h +++ b/source/Blocks/BlockLever.h @@ -1,7 +1,6 @@ #pragma once #include "BlockHandler.h" -#include "../Simulator/RedstoneSimulator.h" @@ -37,11 +36,27 @@ public: ) override { a_BlockType = m_BlockType; - a_BlockMeta = cRedstoneSimulator::LeverDirectionToMetaData(a_BlockFace); + a_BlockMeta = LeverDirectionToMetaData(a_BlockFace); return true; } + inline static NIBBLETYPE LeverDirectionToMetaData(char a_Dir) + { + // Determine lever direction: + switch (a_Dir) + { + case BLOCK_FACE_TOP: return 0x6; + case BLOCK_FACE_EAST: return 0x1; + case BLOCK_FACE_WEST: return 0x2; + case BLOCK_FACE_SOUTH: return 0x3; + case BLOCK_FACE_NORTH: return 0x4; + case BLOCK_FACE_BOTTOM: return 0x0; + default: return 0x6; + } + } + + virtual const char * GetStepSound(void) override { return "step.wood"; diff --git a/source/Blocks/BlockRedstoneRepeater.cpp b/source/Blocks/BlockRedstoneRepeater.cpp index 72ea21012..3ab5bc559 100644 --- a/source/Blocks/BlockRedstoneRepeater.cpp +++ b/source/Blocks/BlockRedstoneRepeater.cpp @@ -1,7 +1,6 @@ #include "Globals.h" #include "BlockRedstoneRepeater.h" -#include "../Simulator/RedstoneSimulator.h" #include "../Entities/Player.h" @@ -42,7 +41,7 @@ bool cBlockRedstoneRepeaterHandler::GetPlacementBlockTypeMeta( ) { a_BlockType = m_BlockType; - a_BlockMeta = cRedstoneSimulator::RepeaterRotationToMetaData(a_Player->GetRotation()); + a_BlockMeta = RepeaterRotationToMetaData(a_Player->GetRotation()); return true; } diff --git a/source/Blocks/BlockRedstoneRepeater.h b/source/Blocks/BlockRedstoneRepeater.h index 958841a34..a61121d3a 100644 --- a/source/Blocks/BlockRedstoneRepeater.h +++ b/source/Blocks/BlockRedstoneRepeater.h @@ -48,6 +48,33 @@ public: { return "step.wood"; } + + + inline static NIBBLETYPE RepeaterRotationToMetaData(double a_Rotation) + { + a_Rotation += 90 + 45; // So its not aligned with axis + if (a_Rotation > 360) + { + a_Rotation -= 360; + } + + if ((a_Rotation >= 0) && (a_Rotation < 90)) + { + return 0x1; + } + else if ((a_Rotation >= 180) && (a_Rotation < 270)) + { + return 0x3; + } + else if ((a_Rotation >= 90) && (a_Rotation < 180)) + { + return 0x2; + } + else + { + return 0x0; + } + } } ; diff --git a/source/Chunk.cpp b/source/Chunk.cpp index 1c937c894..6e83d32ea 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -682,17 +682,17 @@ void cChunk::ProcessQueuedSetBlocks(void) Int64 CurrTick = m_World->GetWorldAge(); for (sSetBlockQueueVector::iterator itr = m_SetBlockQueue.begin(); itr != m_SetBlockQueue.end();) { - if (itr->m_Tick < CurrTick) + if (itr->m_Tick <= CurrTick) { - // Not yet - ++itr; - continue; + // Current world age is bigger than/equal to target world age - delay time reached + SetBlock(itr->m_RelX, itr->m_RelY, itr->m_RelZ, itr->m_BlockType, itr->m_BlockMeta); + itr = m_SetBlockQueue.erase(itr); } else { - // Now is the time to set the block - SetBlock(itr->m_RelX, itr->m_RelY, itr->m_RelZ, itr->m_BlockType, itr->m_BlockMeta); - itr = m_SetBlockQueue.erase(itr); + // Not yet + ++itr; + continue; } } // for itr - m_SetBlockQueue[] } diff --git a/source/Chunk.h b/source/Chunk.h index 63a8f75cd..0d7479347 100644 --- a/source/Chunk.h +++ b/source/Chunk.h @@ -6,6 +6,7 @@ #include "Simulator/FireSimulator.h" #include "Simulator/SandSimulator.h" +#include "Simulator/RedstoneSimulator.h" @@ -338,6 +339,7 @@ public: cFluidSimulatorData * GetWaterSimulatorData(void) { return m_WaterSimulatorData; } cFluidSimulatorData * GetLavaSimulatorData (void) { return m_LavaSimulatorData; } cSandSimulatorChunkData & GetSandSimulatorData (void) { return m_SandSimulatorData; } + cRedstoneSimulatorChunkData & GetRedstoneSimulatorData(void) { return m_RedstoneSimulatorData; } cBlockEntity * GetBlockEntity(int a_BlockX, int a_BlockY, int a_BlockZ); cBlockEntity * GetBlockEntity(const Vector3i & a_BlockPos) { return GetBlockEntity(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z); } @@ -407,6 +409,7 @@ private: cFluidSimulatorData * m_WaterSimulatorData; cFluidSimulatorData * m_LavaSimulatorData; cSandSimulatorChunkData m_SandSimulatorData; + cRedstoneSimulatorChunkData m_RedstoneSimulatorData; // pick up a random block of this chunk diff --git a/source/Items/ItemComparator.h b/source/Items/ItemComparator.h index 53dbd020d..3fbb7603d 100644 --- a/source/Items/ItemComparator.h +++ b/source/Items/ItemComparator.h @@ -2,7 +2,7 @@ #pragma once #include "ItemHandler.h" -#include "../Simulator/RedstoneSimulator.h" +#include "../Blocks/BlockRedstoneRepeater.h" @@ -30,7 +30,7 @@ public: ) override { a_BlockType = E_BLOCK_INACTIVE_COMPARATOR; - a_BlockMeta = cRedstoneSimulator::RepeaterRotationToMetaData(a_Player->GetRotation()); + a_BlockMeta = cBlockRedstoneRepeaterHandler::RepeaterRotationToMetaData(a_Player->GetRotation()); return true; } } ; diff --git a/source/Items/ItemRedstoneRepeater.h b/source/Items/ItemRedstoneRepeater.h index 459070579..f69f24eb8 100644 --- a/source/Items/ItemRedstoneRepeater.h +++ b/source/Items/ItemRedstoneRepeater.h @@ -2,7 +2,7 @@ #pragma once #include "ItemHandler.h" -#include "../Simulator/RedstoneSimulator.h" +#include "../Blocks/BlockRedstoneRepeater.h" @@ -30,7 +30,7 @@ public: ) override { a_BlockType = E_BLOCK_REDSTONE_REPEATER_OFF; - a_BlockMeta = cRedstoneSimulator::RepeaterRotationToMetaData(a_Player->GetRotation()); + a_BlockMeta = cBlockRedstoneRepeaterHandler::RepeaterRotationToMetaData(a_Player->GetRotation()); return true; } } ; diff --git a/source/Piston.cpp b/source/Piston.cpp index 136100922..ed175d3e3 100644 --- a/source/Piston.cpp +++ b/source/Piston.cpp @@ -14,14 +14,8 @@ -extern bool g_BlockPistonBreakable[]; - - - - - /// Number of ticks that the piston extending / retracting waits before setting the block -const int PISTON_TICK_DELAY = 20; +const int PISTON_TICK_DELAY = 6; @@ -120,7 +114,7 @@ void cPiston::ExtendPiston(int pistx, int pisty, int pistz) AddDir(pistx, pisty, pistz, pistonMeta, -1); // "pist" now at piston body, "ext" at future extension - m_World->QueueSetBlock( pistx, pisty, pistz, pistonBlock, pistonMeta | 0x8, PISTON_TICK_DELAY); + m_World->SetBlock( pistx, pisty, pistz, pistonBlock, pistonMeta | 0x8); m_World->QueueSetBlock(extx, exty, extz, E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0), PISTON_TICK_DELAY); } @@ -141,7 +135,7 @@ void cPiston::RetractPiston(int pistx, int pisty, int pistz) m_World->BroadcastBlockAction(pistx, pisty, pistz, 1, pistonMeta & ~(8), pistonBlock); m_World->BroadcastSoundEffect("tile.piston.in", pistx * 8, pisty * 8, pistz * 8, 0.5f, 0.7f); - m_World->QueueSetBlock(pistx, pisty, pistz, pistonBlock, pistonMeta & ~(8), PISTON_TICK_DELAY); + m_World->SetBlock(pistx, pisty, pistz, pistonBlock, pistonMeta & ~(8)); // Check the extension: AddDir(pistx, pisty, pistz, pistonMeta, 1); @@ -199,15 +193,6 @@ bool cPiston::IsSticky(BLOCKTYPE a_BlockType) -bool cPiston::IsStickyExtension(NIBBLETYPE a_ExtMeta) -{ - return ((a_ExtMeta & 0x08) != 0); -} - - - - - bool cPiston::CanPush(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { switch (a_BlockType) diff --git a/source/Piston.h b/source/Piston.h index cc051e454..92ddf6938 100644 --- a/source/Piston.h +++ b/source/Piston.h @@ -63,9 +63,6 @@ public: /// Returns true if the piston (with the specified meta) is extended static bool IsExtended(NIBBLETYPE a_PistonMeta); - - /// Returns true if the extension (with the specified meta) is sticky - static bool IsStickyExtension(NIBBLETYPE a_ExtMeta); /// Returns true if the specified block can be pushed by a piston (and left intact) static bool CanPush(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta); diff --git a/source/Simulator/RedstoneSimulator.cpp b/source/Simulator/RedstoneSimulator.cpp index 8526a888e..29f59fa53 100644 --- a/source/Simulator/RedstoneSimulator.cpp +++ b/source/Simulator/RedstoneSimulator.cpp @@ -3,12 +3,10 @@ #include "RedstoneSimulator.h" #include "../BlockEntities/DropSpenserEntity.h" +#include "../Entities/TNTEntity.h" #include "../Blocks/BlockTorch.h" +#include "../Blocks/BlockDoor.h" #include "../Piston.h" -#include "../World.h" -#include "../BlockID.h" -#include "../Chunk.h" -#include "../Entities/TNTEntity.h" @@ -31,1104 +29,1024 @@ cRedstoneSimulator::~cRedstoneSimulator() -void cRedstoneSimulator::WakeUp(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) +void cRedstoneSimulator::AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) { - if (a_Chunk == NULL) + if ((a_Chunk == NULL) || !a_Chunk->IsValid()) + { + return; + } + else if ((a_BlockY < 0) || (a_BlockY > cChunkDef::Height)) { return; } + int RelX = a_BlockX - a_Chunk->GetPosX() * cChunkDef::Width; int RelZ = a_BlockZ - a_Chunk->GetPosZ() * cChunkDef::Width; - - // Check if any close neighbor is redstone-related: - int MinY = (a_BlockY > 0) ? -1 : 0; - int MaxY = (a_BlockY < cChunkDef::Height - 1) ? 1 : 0; - for (int y = MinY; y <= MaxY; y++) for (int x = -1; x < 2; x++) for (int z = -1; z < 2; z++) + + if (!IsAllowedBlock(a_Chunk->GetBlock(RelX, a_BlockY, RelZ))) { - BLOCKTYPE BlockType; - NIBBLETYPE BlockMeta; - if (!a_Chunk->UnboundedRelGetBlock(RelX + x, a_BlockY + y, RelZ + z, BlockType, BlockMeta)) + return; + } + + // Check for duplicates: + cRedstoneSimulatorChunkData & ChunkData = a_Chunk->GetRedstoneSimulatorData(); + for (cRedstoneSimulatorChunkData::iterator itr = ChunkData.begin(); itr != ChunkData.end(); ++itr) + { + if ((itr->x == RelX) && (itr->y == a_BlockY) && (itr->z == RelZ)) { - continue; + return; } - switch (BlockType) - { - case E_BLOCK_PISTON: - case E_BLOCK_STICKY_PISTON: - case E_BLOCK_DISPENSER: - case E_BLOCK_DROPPER: - case E_BLOCK_REDSTONE_LAMP_OFF: - case E_BLOCK_REDSTONE_LAMP_ON: - case E_BLOCK_REDSTONE_REPEATER_OFF: - case E_BLOCK_REDSTONE_REPEATER_ON: - case E_BLOCK_REDSTONE_TORCH_OFF: - case E_BLOCK_REDSTONE_TORCH_ON: - case E_BLOCK_REDSTONE_WIRE: - case E_BLOCK_LEVER: - case E_BLOCK_STONE_BUTTON: - case E_BLOCK_WOODEN_BUTTON: - case E_BLOCK_TRIPWIRE_HOOK: - { - m_Blocks.push_back(Vector3i(a_BlockX, a_BlockY, a_BlockZ)); - return; - } - } // switch (BlockType) - } // for y, x, z - neighbors + } + + ChunkData.push_back(cCoordWithInt(RelX, a_BlockY, RelZ)); } -void cRedstoneSimulator::Simulate(float a_Dt) +void cRedstoneSimulator::SimulateChunk(float a_Dt, int a_ChunkX, int a_ChunkZ, cChunk * a_Chunk) { - // Toggle torches on/off - while (!m_RefreshTorchesAround.empty()) + cRedstoneSimulatorChunkData & ChunkData = a_Chunk->GetRedstoneSimulatorData(); + if (ChunkData.empty()) { - Vector3i pos = m_RefreshTorchesAround.front(); - m_RefreshTorchesAround.pop_front(); - - RefreshTorchesAround(pos); + return; } - // Set repeaters to correct values, and decrement ticks - for (RepeaterList::iterator itr = m_SetRepeaters.begin(); itr != m_SetRepeaters.end();) + int BaseX = a_Chunk->GetPosX() * cChunkDef::Width; + int BaseZ = a_Chunk->GetPosZ() * cChunkDef::Width; + + for (cRedstoneSimulatorChunkData::const_iterator dataitr = ChunkData.begin(), end = ChunkData.end(); dataitr != end;) { - if (--itr->Ticks > 0) + BLOCKTYPE BlockType = a_Chunk->GetBlock(dataitr->x, dataitr->y, dataitr->z); + if (!IsAllowedBlock(BlockType)) { - // Not yet, move to next item in the list - ++itr; + dataitr = ChunkData.erase(dataitr); continue; } - BLOCKTYPE BlockType; - NIBBLETYPE BlockMeta; - m_World.GetBlockTypeMeta(itr->Position.x, itr->Position.y, itr->Position.z, BlockType, BlockMeta); - if (itr->bPowerOn && (BlockType == E_BLOCK_REDSTONE_REPEATER_OFF)) - { - m_World.FastSetBlock(itr->Position.x, itr->Position.y, itr->Position.z, E_BLOCK_REDSTONE_REPEATER_ON, BlockMeta); - m_Blocks.push_back(itr->Position); - } - else if (!itr->bPowerOn && (BlockType == E_BLOCK_REDSTONE_REPEATER_ON)) + + // Check to see if PoweredBlocks have invalid items (source is air or an unpowered source) + for (PoweredBlocksList::iterator itr = m_PoweredBlocks.begin(); itr != m_PoweredBlocks.end();) { - m_World.FastSetBlock(itr->Position.x, itr->Position.y, itr->Position.z, E_BLOCK_REDSTONE_REPEATER_OFF, BlockMeta); - m_Blocks.push_back(itr->Position); + sPoweredBlocks & Change = *itr; + BLOCKTYPE SourceBlockType = m_World.GetBlock(Change.a_SourcePos); + + if (SourceBlockType != Change.a_SourceBlock) + { + itr = m_PoweredBlocks.erase(itr); + } + else if ( + // Changeable sources + ((SourceBlockType == E_BLOCK_REDSTONE_WIRE) && (m_World.GetBlockMeta(Change.a_SourcePos) == 0)) || + ((SourceBlockType == E_BLOCK_LEVER) && !IsLeverOn(m_World.GetBlockMeta(Change.a_SourcePos))) || + ((SourceBlockType == E_BLOCK_DETECTOR_RAIL) && (m_World.GetBlockMeta(Change.a_SourcePos) & 0x08) == 0x08) || + (((SourceBlockType == E_BLOCK_STONE_BUTTON) || (SourceBlockType == E_BLOCK_WOODEN_BUTTON)) && (!IsButtonOn(m_World.GetBlockMeta(Change.a_SourcePos)))) + ) + { + itr = m_PoweredBlocks.erase(itr); + } + else + { + itr++; + } } - if (itr->bPowerOffNextTime) + // Check to see if LinkedPoweredBlocks have invalid items: source, block powered through, or power destination block has changed + for (LinkedBlocksList::iterator itr = m_LinkedPoweredBlocks.begin(); itr != m_LinkedPoweredBlocks.end();) { - itr->bPowerOn = false; - itr->bPowerOffNextTime = false; - itr->Ticks = 10; // TODO: Look up actual ticks from block metadata - ++itr; + sLinkedPoweredBlocks & Change = *itr; + BLOCKTYPE SourceBlockType = m_World.GetBlock(Change.a_SourcePos); + BLOCKTYPE MiddleBlockType = m_World.GetBlock(Change.a_MiddlePos); + + if (SourceBlockType != Change.a_SourceBlock) + { + itr = m_LinkedPoweredBlocks.erase(itr); + } + else if (MiddleBlockType != Change.a_MiddleBlock) + { + itr = m_LinkedPoweredBlocks.erase(itr); + } + else if ( + // Things that can send power through a block but which depends on meta + ((SourceBlockType == E_BLOCK_REDSTONE_WIRE) && (m_World.GetBlockMeta(Change.a_SourcePos) == 0)) || + ((SourceBlockType == E_BLOCK_LEVER) && !IsLeverOn(m_World.GetBlockMeta(Change.a_SourcePos))) || + (((SourceBlockType == E_BLOCK_STONE_BUTTON) || (SourceBlockType == E_BLOCK_WOODEN_BUTTON)) && (!IsButtonOn(m_World.GetBlockMeta(Change.a_SourcePos)))) + ) + { + itr = m_LinkedPoweredBlocks.erase(itr); + } + else + { + itr++; + } } - else + + // PoweredBlock list was fine, now to the actual handling + int a_X = BaseX + dataitr->x; + int a_Z = BaseZ + dataitr->z; + switch (BlockType) { - itr = m_SetRepeaters.erase(itr); + case E_BLOCK_BLOCK_OF_REDSTONE: HandleRedstoneBlock(a_X, dataitr->y, a_Z); break; + case E_BLOCK_LEVER: HandleRedstoneLever(a_X, dataitr->y, a_Z); break; + case E_BLOCK_TNT: HandleTNT(a_X, dataitr->y, a_Z); break; + case E_BLOCK_REDSTONE_WIRE: HandleRedstoneWire(a_X, dataitr->y, a_Z); break; + + case E_BLOCK_REDSTONE_TORCH_OFF: + case E_BLOCK_REDSTONE_TORCH_ON: + { + HandleRedstoneTorch(a_X, dataitr->y, a_Z, BlockType); + break; + } + case E_BLOCK_STONE_BUTTON: + case E_BLOCK_WOODEN_BUTTON: + { + HandleRedstoneButton(a_X, dataitr->y, a_Z, BlockType); + break; + } + case E_BLOCK_REDSTONE_REPEATER_OFF: + case E_BLOCK_REDSTONE_REPEATER_ON: + { + HandleRedstoneRepeater(a_X, dataitr->y, a_Z, BlockType); + break; + } + case E_BLOCK_PISTON: + case E_BLOCK_STICKY_PISTON: + { + HandlePiston(a_X, dataitr->y, a_Z); + break; + } + case E_BLOCK_REDSTONE_LAMP_OFF: + case E_BLOCK_REDSTONE_LAMP_ON: + { + HandleRedstoneLamp(a_X, dataitr->y, a_Z, BlockType); + break; + } + case E_BLOCK_DISPENSER: + case E_BLOCK_DROPPER: + { + HandleDropSpenser(a_X, dataitr->y, a_Z); + break; + } + case E_BLOCK_WOODEN_DOOR: + case E_BLOCK_IRON_DOOR: + { + HandleDoor(a_X, dataitr->y, a_Z); + break; + } + case E_BLOCK_ACTIVATOR_RAIL: + case E_BLOCK_DETECTOR_RAIL: + case E_BLOCK_POWERED_RAIL: + { + HandleRail(a_X, dataitr->y, a_Z, BlockType); + break; + } } - } - // Handle changed blocks - { - cCSLock Lock(m_CS); - std::swap(m_Blocks, m_BlocksBuffer); - } - for (BlockList::iterator itr = m_BlocksBuffer.begin(); itr != m_BlocksBuffer.end(); ++itr) - { - HandleChange(*itr); + ++dataitr; } - m_BlocksBuffer.clear(); } -void cRedstoneSimulator::RefreshTorchesAround(const Vector3i & a_BlockPos) +void cRedstoneSimulator::HandleRedstoneTorch(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_MyState) { - static Vector3i Surroundings [] = { - Vector3i(-1, 0, 0), - Vector3i(1, 0, 0), - Vector3i(0, 0,-1), - Vector3i(0, 0, 1), - Vector3i(0, 1, 0), // Also toggle torch on top - }; - BLOCKTYPE TargetBlockType = E_BLOCK_REDSTONE_TORCH_ON; - BLOCKTYPE TargetRepeaterType = E_BLOCK_REDSTONE_REPEATER_OFF; - if (IsPowered(a_BlockPos, true)) + static const struct // Define which directions the torch can power { - TargetBlockType = E_BLOCK_REDSTONE_TORCH_OFF; - TargetRepeaterType = E_BLOCK_REDSTONE_REPEATER_ON; - //Make TNT Explode when it gets powered. - if (m_World.GetBlock(a_BlockPos) == E_BLOCK_TNT) + int x, y, z; + } gCrossCoords[] = + { + { 1, 0, 0}, + {-1, 0, 0}, + { 0, 0, 1}, + { 0, 0, -1}, + { 0, 1, 0}, + } ; + + if (a_MyState == E_BLOCK_REDSTONE_TORCH_ON) + { + // Check if the block the torch is on is powered + int X = a_BlockX; int Y = a_BlockY; int Z = a_BlockZ; + AddFaceDirection(X, Y, Z, cBlockTorchHandler::MetaDataToDirection(m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ)), true); // Inverse true to get the block torch is on + + if (AreCoordsPowered(X, Y, Z)) + { + // There was a match, torch goes off + // FastSetBlock so the server doesn't fail an assert -_- + m_World.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_TORCH_OFF, m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ)); + return; + } + + // Torch still on, make all 4(X, Z) + 1(Y) sides powered + for (int i = 0; i < ARRAYCOUNT(gCrossCoords); i++) { - m_World.BroadcastSoundEffect("random.fuse", a_BlockPos.x * 8, a_BlockPos.y * 8, a_BlockPos.z * 8, 0.5f, 0.6f); - m_World.SpawnPrimedTNT(a_BlockPos.x + 0.5, a_BlockPos.y + 0.5, a_BlockPos.z + 0.5, 4); // 4 seconds to boom - m_World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, E_BLOCK_AIR, 0); + BLOCKTYPE Type = m_World.GetBlock(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z); + if (i < ARRAYCOUNT(gCrossCoords) - 1) // Sides of torch, not top (top is last) + { + if ( + ((IsMechanism(Type)) || (Type == E_BLOCK_REDSTONE_WIRE)) && // Is it a mechanism or wire? Not block/other torch etc. + (!Vector3i(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z).Equals(Vector3i(X, Y, Z))) // CAN'T power block is that it is on + ) + { + SetBlockPowered(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_TORCH_ON); + } + } + else + { + // Top side, power whatever is there, including blocks + SetBlockPowered(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_TORCH_ON); + } } - //Turn a redstone lamp on when it gets powered. - if (m_World.GetBlock(a_BlockPos) == E_BLOCK_REDSTONE_LAMP_OFF) + + if (m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) != 0x5) // Is torch standing on ground? If not (i.e. on wall), power block beneath { - m_World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, E_BLOCK_REDSTONE_LAMP_ON, 0); + BLOCKTYPE Type = m_World.GetBlock(a_BlockX, a_BlockY - 1, a_BlockZ); + + if ((IsMechanism(Type)) || (Type == E_BLOCK_REDSTONE_WIRE)) // Still can't make a normal block powered though! + { + SetBlockPowered(a_BlockX, a_BlockY - 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_TORCH_ON); + } } - //if (m_World.GetBlock(a_BlockPos) == E_BLOCK_DIRT) - //{ - // m_World.FastSetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, E_BLOCK_STONE, 0); - //} } else { - //Turn a redstone lamp off when it gets powered. - if (m_World.GetBlock(a_BlockPos) == E_BLOCK_REDSTONE_LAMP_ON) + // Check if the block the torch is on is powered + int X = a_BlockX; int Y = a_BlockY; int Z = a_BlockZ; + AddFaceDirection(X, Y, Z, cBlockTorchHandler::MetaDataToDirection(m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ)), true); // Inverse true to get the block torch is on + + // See if off state torch can be turned on again + if (AreCoordsPowered(X, Y, Z)) { - m_World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, E_BLOCK_REDSTONE_LAMP_OFF, 0); + return; // Something matches, torch still powered } - //if (m_World.GetBlock(a_BlockPos) == E_BLOCK_STONE) - //{ - // m_World.FastSetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, E_BLOCK_DIRT, 0); - //} + + // Block torch on not powered, can be turned on again! + // FastSetBlock so the server doesn't fail an assert -_- + m_World.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_TORCH_ON, m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ)); } + return; +} + + + - for (unsigned int i = 0; i < ARRAYCOUNT(Surroundings); ++i) + +void cRedstoneSimulator::HandleRedstoneBlock(int a_BlockX, int a_BlockY, int a_BlockZ) +{ + static const struct // Define which directions the redstone block can power { - Vector3i TorchPos = a_BlockPos + Surroundings[i]; - BLOCKTYPE BlockType; - NIBBLETYPE BlockMeta; - m_World.GetBlockTypeMeta(TorchPos.x, TorchPos.y, TorchPos.z, BlockType, BlockMeta); - switch (BlockType) - { - case E_BLOCK_REDSTONE_TORCH_ON: - case E_BLOCK_REDSTONE_TORCH_OFF: - { - if (BlockType != TargetBlockType) - { - if (cBlockTorchHandler::IsAttachedTo(TorchPos, BlockMeta, a_BlockPos)) - { - m_World.FastSetBlock(TorchPos.x, TorchPos.y, TorchPos.z, TargetBlockType, BlockMeta); - m_Blocks.push_back(TorchPos); - } - } - break; - } - case E_BLOCK_REDSTONE_REPEATER_ON: - case E_BLOCK_REDSTONE_REPEATER_OFF: - { - if ((BlockType != TargetRepeaterType) && IsRepeaterPointingAway(TorchPos, BlockMeta, a_BlockPos)) - { - SetRepeater(TorchPos, 10, (TargetRepeaterType == E_BLOCK_REDSTONE_REPEATER_ON)); - } - break; - } - } // switch (BlockType) - } // for i - Surroundings[] + int x, y, z; + } gCrossCoords[] = + { + { 0, 0, 0}, // Oh, anomalous redstone. Only block that powers itself + { 1, 0, 0}, + {-1, 0, 0}, + { 0, 0, 1}, + { 0, 0, -1}, + { 0, 1, 0}, + { 0,-1, 0}, + } ; + + for (int i = 0; i < ARRAYCOUNT(gCrossCoords); i++) + { + // Power everything + SetBlockPowered(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_BLOCK_OF_REDSTONE); + } + return; } -void cRedstoneSimulator::HandleChange(const Vector3i & a_BlockPos) +void cRedstoneSimulator::HandleRedstoneLever(int a_BlockX, int a_BlockY, int a_BlockZ) { - std::deque< Vector3i > SpreadStack; - - static const Vector3i Surroundings[] = { - Vector3i(1, 0, 0), - Vector3i(1, 1, 0), - Vector3i(1,-1, 0), - Vector3i(-1, 0, 0), - Vector3i(-1, 1, 0), - Vector3i(-1,-1, 0), - Vector3i(0, 0, 1), - Vector3i(0, 1, 1), - Vector3i(0,-1, 1), - Vector3i(0, 0,-1), - Vector3i(0, 1,-1), - Vector3i(0,-1,-1), - Vector3i(0,-1, 0), - }; - - BLOCKTYPE BlockType; - NIBBLETYPE BlockMeta; - m_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, BlockType, BlockMeta); - - // First check whether torch should be on or off - switch (BlockType) + if (IsLeverOn(m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ))) { - case E_BLOCK_REDSTONE_TORCH_ON: - case E_BLOCK_REDSTONE_TORCH_OFF: + static const struct // Define which directions the redstone lever can power (all sides) { - static const Vector3i Surroundings [] = { - Vector3i(-1, 0, 0), - Vector3i(1, 0, 0), - Vector3i(0, 0,-1), - Vector3i(0, 0, 1), - Vector3i(0,-1, 0), - }; - for (unsigned int i = 0; i < ARRAYCOUNT(Surroundings); ++i) - { - Vector3i pos = a_BlockPos + Surroundings[i]; - BLOCKTYPE OtherBlock = m_World.GetBlock(pos); - if ( - (OtherBlock != E_BLOCK_AIR) && - (OtherBlock != E_BLOCK_REDSTONE_TORCH_ON) && - (OtherBlock != E_BLOCK_REDSTONE_TORCH_OFF) - ) - { - RefreshTorchesAround(pos); - } - } - m_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, BlockType, BlockMeta); - break; - } // case "torches" - - case E_BLOCK_REDSTONE_REPEATER_ON: - case E_BLOCK_REDSTONE_REPEATER_OFF: + int x, y, z; + } gCrossCoords[] = { - // Check if repeater is powered by a 'powered block' (not wires/torch) - Vector3i Direction = GetRepeaterDirection(BlockMeta); - Vector3i pos = a_BlockPos - Direction; // NOTE: It's minus Direction - BLOCKTYPE OtherBlock = m_World.GetBlock(pos); - if ( - (OtherBlock != E_BLOCK_AIR) && - (OtherBlock != E_BLOCK_REDSTONE_TORCH_ON) && - (OtherBlock != E_BLOCK_REDSTONE_TORCH_OFF) && - (OtherBlock != E_BLOCK_REDSTONE_WIRE) - ) - { - RefreshTorchesAround(pos); - } - else - { - SetRepeater(a_BlockPos, 10, IsPowered(a_BlockPos, false)); - } - m_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, BlockType, BlockMeta); - break; + { 1, 0, 0}, + {-1, 0, 0}, + { 0, 0, 1}, + { 0, 0, -1}, + { 0, 1, 0}, + { 0,-1, 0}, + } ; + + for (int i = 0; i < ARRAYCOUNT(gCrossCoords); i++) + { + // Power everything + SetBlockPowered(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_LEVER); } - } // switch (BlockType) - BlockList Sources; - switch (BlockType) + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_XM, E_BLOCK_LEVER); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_XP, E_BLOCK_LEVER); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_YM, E_BLOCK_LEVER); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_YP, E_BLOCK_LEVER); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_ZM, E_BLOCK_LEVER); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_ZP, E_BLOCK_LEVER); + } + return; +} + + + + + +void cRedstoneSimulator::HandleRedstoneButton(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType) +{ + if (IsButtonOn(m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ))) { - case E_BLOCK_REDSTONE_TORCH_ON: + static const struct // Define which directions the redstone button can power (all sides) { - // If torch is still on, use it as a source - Sources.push_back(a_BlockPos); - break; - } - - case E_BLOCK_REDSTONE_REPEATER_ON: + int x, y, z; + } gCrossCoords[] = { - // Repeater only spreads charge right in front, and up to one block up: - static const Vector3i Surroundings [] = { - Vector3i(0, 0, 0), - Vector3i(0, 1, 0), - }; - Vector3i Direction = GetRepeaterDirection(BlockMeta); - for (unsigned int i = 0; i < ARRAYCOUNT(Surroundings); ++i) - { - Vector3i pos = a_BlockPos + Direction + Surroundings[i]; - if (PowerBlock(pos, a_BlockPos, 0xf)) - { - SpreadStack.push_back(pos); - } - } - break; - } // case E_BLOCK_REDSTONE_REPEATER_ON - - case E_BLOCK_LEVER: - { - // Adding lever to the source queue - if (cRedstoneSimulator::IsLeverOn(BlockMeta)) - { - Sources.push_back(a_BlockPos); - } - break; - } // case E_BLOCK_LEVER - } // switch (BlockType) + { 1, 0, 0}, + {-1, 0, 0}, + { 0, 0, 1}, + { 0, 0, -1}, + { 0, 1, 0}, + { 0,-1, 0}, + } ; + + for (int i = 0; i < ARRAYCOUNT(gCrossCoords); i++) + { + // Power everything + SetBlockPowered(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z, a_BlockX, a_BlockY, a_BlockZ, a_BlockType); + } + + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_XM, a_BlockType); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_XP, a_BlockType); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_YM, a_BlockType); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_YP, a_BlockType); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_ZM, a_BlockType); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_ZP, a_BlockType); + } +} + + + + - // Power all blocks legally connected to the sources - if (BlockType != E_BLOCK_REDSTONE_REPEATER_ON) +void cRedstoneSimulator::HandleRedstoneWire(int a_BlockX, int a_BlockY, int a_BlockZ) +{ + static const struct // Define which directions the wire can receive power from + { + int x, y, z; + } gCrossCoords[] = + { + { 1, 0, 0}, + {-1, 0, 0}, + { 0, 0, 1}, + { 0, 0, -1}, + { 1, 1, 0}, // From here to end, check for wire placed on sides of blocks + {-1, 1, 0}, + { 0, 1, 1}, + { 0, 1, -1}, + { 1,-1, 0}, + {-1,-1, 0}, + { 0,-1, 1}, + { 0,-1, -1}, + } ; + + // Check to see if directly beside a power source + if (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) + { + m_World.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, 15); // Maximum power + } + else { - BlockList NewSources = RemoveCurrent(a_BlockPos); - Sources.insert(Sources.end(), NewSources.begin(), NewSources.end()); - while (!Sources.empty()) + NIBBLETYPE MyMeta = m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + int TimesMetaSmaller = 0, TimesFoundAWire = 0; + + for (int i = 0; i < ARRAYCOUNT(gCrossCoords); i++) // Loop through all directions to transfer or receive power { - Vector3i SourcePos = Sources.back(); - Sources.pop_back(); + BLOCKTYPE SurroundType; + NIBBLETYPE SurroundMeta; + m_World.GetBlockTypeMeta(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z, SurroundType, SurroundMeta); - BLOCKTYPE BlockType; - NIBBLETYPE BlockMeta; - m_World.GetBlockTypeMeta(SourcePos.x, SourcePos.y, SourcePos.z, BlockType, BlockMeta); - switch (BlockType) + if (SurroundType == E_BLOCK_REDSTONE_WIRE) { - case E_BLOCK_LEVER: // Treating lever as a torch - case E_BLOCK_REDSTONE_TORCH_OFF: - case E_BLOCK_REDSTONE_TORCH_ON: + TimesFoundAWire++; + + if (SurroundMeta > 1) // Wires of power 1 or 0 cannot transfer power TO ME, don't bother checking { - static Vector3i Surroundings [] = { - Vector3i(-1, 0, 0), - Vector3i(1, 0, 0), - Vector3i(0, 0,-1), - Vector3i(0, 0, 1), - }; - for (unsigned int i = 0; i < ARRAYCOUNT(Surroundings); ++i) + if (SurroundMeta > MyMeta) // Does surrounding wire have a higher power level than self? { - Vector3i OtherPos = SourcePos + Surroundings[i]; - if (PowerBlock(OtherPos, a_BlockPos, 0xf)) - { - SpreadStack.push_back(OtherPos); // Changed, so add to stack - } + m_World.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, SurroundMeta - 1); } - break; } - - case E_BLOCK_REDSTONE_REPEATER_OFF: - case E_BLOCK_REDSTONE_REPEATER_ON: + + if (SurroundMeta < MyMeta) // Go through all surroundings to see if self power is larger than everyone else's { - static Vector3i Surroundings [] = { - Vector3i(0, 0, 0), - Vector3i(0, 1, 0), - }; - Vector3i Direction = GetRepeaterDirection(BlockMeta); - for (unsigned int i = 0; i < ARRAYCOUNT(Surroundings); ++i) - { - Vector3i pos = SourcePos + Direction + Surroundings[i]; - if (PowerBlock(pos, a_BlockPos, 0xf)) - { - SpreadStack.push_back(pos); - } - } - break; + TimesMetaSmaller++; } - } // switch (BlockType) - } // while (Sources[]) - } // if (!repeater_on) - - // Do a floodfill - while (!SpreadStack.empty()) - { - Vector3i pos = SpreadStack.back(); - SpreadStack.pop_back(); - NIBBLETYPE Meta = m_World.GetBlockMeta(pos); + } + } - for (unsigned int i = 0; i < ARRAYCOUNT(Surroundings); ++i) + if (TimesMetaSmaller == TimesFoundAWire) { - Vector3i OtherPos = pos + Surroundings[i]; - if (PowerBlock(OtherPos, pos, Meta - 1)) - { - SpreadStack.push_back(OtherPos); // Changed, so add to stack - } - } + // All surrounding metas were smaller - self must have been a wire that was + // transferring power to other wires around. + // However, self not directly powered anymore, so source must have been removed, + // therefore, self must be set to meta zero + m_World.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, 0); + } } - // Only after a redstone area has been completely simulated the redstone entities can react - while (!m_RefreshPistons.empty()) + if (m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) != 0) // A powered wire { - Vector3i pos = m_RefreshPistons.back(); - m_RefreshPistons.pop_back(); + //SetBlockPowered(a_BlockX, a_BlockY - 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_WIRE); // No matter what, block underneath gets powered - BLOCKTYPE BlockType = m_World.GetBlock(pos); - switch (BlockType) + switch (GetWireDirection(a_BlockX, a_BlockY, a_BlockZ)) { - case E_BLOCK_PISTON: - case E_BLOCK_STICKY_PISTON: + case REDSTONE_NONE: + { + static const struct // Define which directions the redstone wire can power + { + int x, y, z; + } gCrossCoords[] = + { + { 1, 0, 0}, // Power block in front + { 2, 0, 0}, // Power block in front of that (strongly power) + {-1, 0, 0}, + {-2, 0, 0}, + { 0, 0, 1}, + { 0, 0, 2}, + { 0, 0, -1}, + { 0, 0, -2}, + { 0, 1, 0}, + { 0, 2, 0}, + { 0,-1, 0}, + { 0,-2, 0}, + } ; + + for (int i = 0; i < ARRAYCOUNT(gCrossCoords); i++) + { + // Power if block is solid, CURRENTLY all mechanisms are solid + if (g_BlockIsSolid[m_World.GetBlock(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z)]) + { + SetBlockPowered(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_WIRE); + } + } + break; + } + case REDSTONE_X_POS: { - if (IsPowered(pos)) + if (m_World.GetBlock(a_BlockX + 1, a_BlockY, a_BlockZ) != E_BLOCK_REDSTONE_WIRE) { - cPiston Piston(&m_World); - Piston.ExtendPiston(pos.x, pos.y, pos.z); + SetBlockPowered(a_BlockX + 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_WIRE); } - else + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_XP, E_BLOCK_REDSTONE_WIRE); + break; + } + case REDSTONE_X_NEG: + { + if (m_World.GetBlock(a_BlockX - 1, a_BlockY, a_BlockZ) != E_BLOCK_REDSTONE_WIRE) { - cPiston Piston(&m_World); - Piston.RetractPiston(pos.x, pos.y, pos.z); + SetBlockPowered(a_BlockX - 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_WIRE); } + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_XM, E_BLOCK_REDSTONE_WIRE); break; } - } // switch (BlockType) - } // while (m_RefreshPistons[]) - - while (!m_RefreshDropSpensers.empty()) - { - Vector3i pos = m_RefreshDropSpensers.back(); - m_RefreshDropSpensers.pop_back(); - - BLOCKTYPE BlockType = m_World.GetBlock(pos); - if ((BlockType == E_BLOCK_DISPENSER) || (BlockType == E_BLOCK_DROPPER)) - { - class cSetPowerToDropSpenser : - public cDropSpenserCallback + case REDSTONE_Z_POS: { - bool m_IsPowered; - public: - cSetPowerToDropSpenser(bool a_IsPowered) : m_IsPowered(a_IsPowered) {} - - virtual bool Item(cDropSpenserEntity * a_DropSpenser) override + if (m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ + 1) != E_BLOCK_REDSTONE_WIRE) { - a_DropSpenser->SetRedstonePower(m_IsPowered); - return false; + SetBlockPowered(a_BlockX, a_BlockY, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_WIRE); } - } DrSpSP(IsPowered(pos)); - m_World.DoWithDropSpenserAt(pos.x, pos.y, pos.z, DrSpSP); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_ZP, E_BLOCK_REDSTONE_WIRE); + break; + } + case REDSTONE_Z_NEG: + { + if (m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ - 1) != E_BLOCK_REDSTONE_WIRE) + { + SetBlockPowered(a_BlockX, a_BlockY, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_WIRE); + } + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_ZM, E_BLOCK_REDSTONE_WIRE); + break; + } } } + return; } -bool cRedstoneSimulator::PowerBlock(const Vector3i & a_BlockPos, const Vector3i & a_FromBlock, char a_Power) +void cRedstoneSimulator::HandleRedstoneRepeater(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_MyState) { - BLOCKTYPE BlockType; - NIBBLETYPE BlockMeta; - m_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, BlockType, BlockMeta); - switch (BlockType) + NIBBLETYPE a_Meta = m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + if (a_MyState == E_BLOCK_REDSTONE_REPEATER_OFF) { - case E_BLOCK_REDSTONE_WIRE: + if (IsRepeaterPowered(a_BlockX, a_BlockY, a_BlockZ, a_Meta & 0x3)) { - if (BlockMeta < a_Power) + m_World.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_REPEATER_ON, a_Meta); + switch (a_Meta & 0x3) // We only want the direction (bottom) bits { - m_World.SetBlockMeta(a_BlockPos, a_Power); - return true; + case 0x0: + { + SetBlockPowered(a_BlockX, a_BlockY, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_REPEATER_ON); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_ZM, E_BLOCK_REDSTONE_REPEATER_ON); + break; + } + case 0x1: + { + SetBlockPowered(a_BlockX + 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_REPEATER_ON); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_XP, E_BLOCK_REDSTONE_REPEATER_ON); + break; + } + case 0x2: + { + SetBlockPowered(a_BlockX, a_BlockY, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_REPEATER_ON); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_ZP, E_BLOCK_REDSTONE_REPEATER_ON); + break; + } + case 0x3: + { + SetBlockPowered(a_BlockX - 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_REPEATER_ON); + SetDirectionLinkedPowered(a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_XM, E_BLOCK_REDSTONE_REPEATER_ON); + break; + } } - break; } - - case E_BLOCK_PISTON: - case E_BLOCK_STICKY_PISTON: + } + else + { + if (!IsRepeaterPowered(a_BlockX, a_BlockY, a_BlockZ, a_Meta & 0x3)) { - m_RefreshPistons.push_back(a_BlockPos); - break; + m_World.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_REPEATER_OFF, a_Meta); } + } + return; +} - case E_BLOCK_DISPENSER: - case E_BLOCK_DROPPER: - { - m_RefreshDropSpensers.push_back(a_BlockPos); - break; - } - - case E_BLOCK_REDSTONE_REPEATER_OFF: - case E_BLOCK_REDSTONE_REPEATER_ON: - { - if (IsRepeaterPointingAway(a_BlockPos, BlockMeta, a_FromBlock)) - { - SetRepeater(a_BlockPos, 10, true); - } - break; - } - - case E_BLOCK_REDSTONE_LAMP_OFF: - { - m_World.FastSetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, E_BLOCK_REDSTONE_LAMP_ON, 0); - break; - } - case E_BLOCK_TNT: - { - m_World.BroadcastSoundEffect("random.fuse", a_BlockPos.x * 8, a_BlockPos.y * 8, a_BlockPos.z * 8, 0.5f, 0.6f); - m_World.SpawnPrimedTNT(a_BlockPos.x + 0.5, a_BlockPos.y + 0.5, a_BlockPos.z + 0.5, 4); // 4 seconds to boom - m_World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, E_BLOCK_AIR, 0); - break; - } - default: - { - if ( - (BlockType != E_BLOCK_AIR) && - (BlockType != E_BLOCK_REDSTONE_TORCH_ON) && - (BlockType != E_BLOCK_REDSTONE_TORCH_OFF) && - (BlockType != E_BLOCK_LEVER) // Treating lever as a torch, for refreshing - ) - { - if (IsPowered(a_BlockPos, true)) - { - m_RefreshTorchesAround.push_back(a_BlockPos); - } - } - break; - } - } // switch (BlockType) - return false; + +void cRedstoneSimulator::HandlePiston(int a_BlockX, int a_BlockY, int a_BlockZ) +{ + if (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) + { + cPiston Piston(&m_World); + Piston.ExtendPiston(a_BlockX, a_BlockY, a_BlockZ); + } + else + { + cPiston Piston(&m_World); + Piston.RetractPiston(a_BlockX, a_BlockY, a_BlockZ); + } + return; } -int cRedstoneSimulator::UnPowerBlock(const Vector3i & a_BlockPos, const Vector3i & a_FromBlock) +void cRedstoneSimulator::HandleDropSpenser(int a_BlockX, int a_BlockY, int a_BlockZ) { - BLOCKTYPE BlockType; - NIBBLETYPE BlockMeta; - if ((a_BlockPos.y < 0) || (a_BlockPos.y >= cChunkDef::Height)) - { - return 0; - } - m_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, BlockType, BlockMeta); - switch (BlockType) + class cSetPowerToDropSpenser : + public cDropSpenserCallback { - case E_BLOCK_REDSTONE_WIRE: - { - if (BlockMeta > 0) - { - m_World.SetBlockMeta(a_BlockPos, 0); - return 1; - } - break; - } - - case E_BLOCK_PISTON: - case E_BLOCK_STICKY_PISTON: + bool m_IsPowered; + public: + cSetPowerToDropSpenser(bool a_IsPowered) : m_IsPowered(a_IsPowered) {} + + virtual bool Item(cDropSpenserEntity * a_DropSpenser) override { - m_RefreshPistons.push_back(a_BlockPos); - break; + a_DropSpenser->SetRedstonePower(m_IsPowered); + return false; } + } DrSpSP (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)); + + m_World.DoWithDropSpenserAt(a_BlockX, a_BlockY, a_BlockZ, DrSpSP); + return; +} - case E_BLOCK_DISPENSER: - case E_BLOCK_DROPPER: - { - m_RefreshDropSpensers.push_back(a_BlockPos); - break; - } - - case E_BLOCK_REDSTONE_TORCH_ON: - { - return 2; - break; - } - - case E_BLOCK_LEVER: - { - // Check if lever is ON. If it is, report it back as a source - if (cRedstoneSimulator::IsLeverOn(BlockMeta)) - { - return 2; - } - break; - } - - case E_BLOCK_REDSTONE_REPEATER_ON: - { - if ( - IsRepeaterPointingTo(a_BlockPos, BlockMeta, a_FromBlock) || // Repeater is next to wire - IsRepeaterPointingTo(a_BlockPos, BlockMeta, a_FromBlock - Vector3i(0, 1, 0)) // Repeater is below wire - ) - { - return 2; - } - else if (IsRepeaterPointingAway(a_BlockPos, BlockMeta, a_FromBlock)) - { - SetRepeater(a_BlockPos, 10, false); - } - // fall-through: - } - - case E_BLOCK_REDSTONE_REPEATER_OFF: - { - if (IsRepeaterPointingAway(a_BlockPos, BlockMeta, a_FromBlock)) - { - SetRepeater(a_BlockPos, 10, false); - } - break; - } - case E_BLOCK_REDSTONE_LAMP_ON: + + + +void cRedstoneSimulator::HandleRedstoneLamp(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_MyState) +{ + if (a_MyState == E_BLOCK_REDSTONE_LAMP_OFF) + { + if (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) { - m_World.FastSetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, E_BLOCK_REDSTONE_LAMP_OFF, 0); - break; + m_World.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_LAMP_ON, 0); } - - default: + } + else + { + if (!AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) { - if ( - (BlockType != E_BLOCK_AIR) && - (BlockType != E_BLOCK_REDSTONE_TORCH_ON) && - (BlockType != E_BLOCK_REDSTONE_TORCH_OFF) && - (BlockType != E_BLOCK_LEVER) - ) - { - if (!IsPowered(a_BlockPos, true)) - { - m_RefreshTorchesAround.push_back(a_BlockPos); - } - } - break; + m_World.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_REDSTONE_LAMP_OFF, 0); } - } // switch (BlockType) - - return 0; + } + return; } -// Removes current from all powered redstone wires until it reaches an energy source. -// Also returns all energy sources it encountered -cRedstoneSimulator::BlockList cRedstoneSimulator::RemoveCurrent(const Vector3i & a_BlockPos) +void cRedstoneSimulator::HandleTNT(int a_BlockX, int a_BlockY, int a_BlockZ) { + if (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) + { + m_World.BroadcastSoundEffect("random.fuse", a_BlockX * 8, a_BlockY * 8, a_BlockZ * 8, 0.5f, 0.6f); + m_World.SpawnPrimedTNT(a_BlockX + 0.5, a_BlockY + 0.5, a_BlockZ + 0.5, 4); // 4 seconds to boom + m_World.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0); + } + return; +} + + + - std::deque< Vector3i > SpreadStack; - std::deque< Vector3i > FoundSources; - - Vector3i Surroundings[] = { - Vector3i(1, 0, 0), - Vector3i(1, 1, 0), - Vector3i(1,-1, 0), - Vector3i(-1, 0, 0), - Vector3i(-1, 1, 0), - Vector3i(-1,-1, 0), - Vector3i(0, 0, 1), - Vector3i(0, 1, 1), - Vector3i(0,-1, 1), - Vector3i(0, 0,-1), - Vector3i(0, 1,-1), - Vector3i(0,-1,-1), - Vector3i(0,-1, 0), - }; - - BLOCKTYPE BlockType; - NIBBLETYPE BlockMeta; - m_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, BlockType, BlockMeta); - switch (BlockType) +void cRedstoneSimulator::HandleDoor(int a_BlockX, int a_BlockY, int a_BlockZ) +{ + if ((m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) & 0x08) == 0x08) { - case E_BLOCK_REDSTONE_REPEATER_ON: - case E_BLOCK_REDSTONE_REPEATER_OFF: + // Block position is located at top half of door + // Is Y - 1 both within world boundaries, a door block, and the bottom half of a door? + // The bottom half stores the open/closed information + if ( + (a_BlockY - 1 >= 0) && + ((m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ) == E_BLOCK_WOODEN_DOOR) || (m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ) == E_BLOCK_IRON_DOOR)) && + (m_World.GetBlockMeta(a_BlockX, a_BlockY - 1, a_BlockZ & 0x08) == 0) + ) { - // Repeaters only spread to their front front and 0 or 1 block up - static Vector3i Surroundings [] = { - Vector3i(0, 0, 0), - Vector3i(0, 1, 0), - }; - Vector3i Direction = GetRepeaterDirection(BlockMeta); - for (unsigned int i = 0; i < ARRAYCOUNT(Surroundings); ++i) + if ((m_World.GetBlockMeta(a_BlockX, a_BlockY - 1, a_BlockZ) & 0x04) == 0) // Closed door? { - Vector3i pos = a_BlockPos + Direction + Surroundings[i]; - int RetVal = UnPowerBlock(pos, a_BlockPos); - if (RetVal == 1) - { - // Changed, so add to stack - SpreadStack.push_back(pos); - } - else if (RetVal == 2) + if (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) // Powered? If so, toggle open { - FoundSources.push_back(pos); + cBlockDoorHandler::ChangeDoor(&m_World, a_BlockX, a_BlockY, a_BlockZ); } } - break; - } - - case E_BLOCK_REDSTONE_TORCH_OFF: - case E_BLOCK_REDSTONE_TORCH_ON: - case E_BLOCK_LEVER: - { - static Vector3i Surroundings [] = { // Torches only spread on the same level - Vector3i(-1, 0, 0), - Vector3i(1, 0, 0), - Vector3i(0, 0,-1), - Vector3i(0, 0, 1), - }; - - for (unsigned int i = 0; i < ARRAYCOUNT(Surroundings); ++i) + else // Opened door { - Vector3i pos = Vector3i(a_BlockPos) + Surroundings[i]; - int RetVal = UnPowerBlock(pos, a_BlockPos); - if (RetVal == 1) - { - SpreadStack.push_back(pos); // Changed, so add to stack - } - else if (RetVal == 2) + if (!AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) // Unpowered? Close if so { - FoundSources.push_back(pos); + cBlockDoorHandler::ChangeDoor(&m_World, a_BlockX, a_BlockY, a_BlockZ); } } - break; - } - - default: - { - SpreadStack.push_back(a_BlockPos); - break; } - } // switch (BlockType) - - - while (!SpreadStack.empty()) + } + else { - Vector3i pos = SpreadStack.back(); - SpreadStack.pop_back(); - - for (unsigned int i = 0; i < ARRAYCOUNT(Surroundings); ++i) + if ((m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) & 0x04) == 0) // Closed door? { - Vector3i OtherPos = pos + Surroundings[i]; - int RetVal = UnPowerBlock(OtherPos, pos); - if (RetVal == 1) + if (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) // Powered? If so, toggle open { - SpreadStack.push_back(OtherPos); // Changed, so add to stack + cBlockDoorHandler::ChangeDoor(&m_World, a_BlockX, a_BlockY, a_BlockZ); } - else if (RetVal == 2) + } + else // Opened door + { + if (!AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) // Unpowered? Close if so { - FoundSources.push_back(OtherPos); + cBlockDoorHandler::ChangeDoor(&m_World, a_BlockX, a_BlockY, a_BlockZ); } } } - - return FoundSources; + return; } -bool cRedstoneSimulator::IsPowering(const Vector3i & a_PowerPos, const Vector3i & a_BlockPos, eRedstoneDirection a_WireDirection, bool a_bOnlyByWire) +void cRedstoneSimulator::HandleRail(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_MyType) { - BLOCKTYPE PowerBlock; - NIBBLETYPE PowerMeta; - m_World.GetBlockTypeMeta(a_PowerPos.x, a_PowerPos.y, a_PowerPos.z, PowerBlock, PowerMeta); - - // Filter out powering blocks for a_bOnlyByWire - if ( - !a_bOnlyByWire && ( - (PowerBlock == E_BLOCK_REDSTONE_TORCH_ON) || - (PowerBlock == E_BLOCK_LEVER) - ) - ) + switch (a_MyType) { - return true; - } - - switch (PowerBlock) - { - case E_BLOCK_REDSTONE_REPEATER_ON: - { - // A repeater pointing towards block is regarded as wire - if (IsRepeaterPointingTo(a_PowerPos, PowerMeta, a_BlockPos)) - { - return true; - } - break; - } - - case E_BLOCK_REDSTONE_WIRE: + case E_BLOCK_DETECTOR_RAIL: { - if (PowerMeta > 0) + if ((m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) & 0x08) == 0x08) { - if (GetWireDirection(a_PowerPos) == a_WireDirection) + static const struct // Define which directions the rail can power (all sides) { - return true; + int x, y, z; + } gCrossCoords[] = + { + { 1, 0, 0}, + {-1, 0, 0}, + { 0, 0, 1}, + { 0, 0, -1}, + { 0, 1, 0}, + { 0,-1, 0}, + } ; + + for (int i = 0; i < ARRAYCOUNT(gCrossCoords); i++) + { + // Power everything + SetBlockPowered(a_BlockX + gCrossCoords[i].x, a_BlockY + gCrossCoords[i].y, a_BlockZ + gCrossCoords[i].z, a_BlockX, a_BlockY, a_BlockZ, a_MyType); } } break; } - } // switch (PowerBlock) - - return false; -} - - - - - -bool cRedstoneSimulator::IsPowered(const Vector3i & a_BlockPos, bool a_bOnlyByWire /* = false */) -{ - BLOCKTYPE BlockType; - NIBBLETYPE BlockMeta; - m_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, BlockType, BlockMeta); - if ((BlockType == E_BLOCK_REDSTONE_REPEATER_OFF) || (BlockType == E_BLOCK_REDSTONE_REPEATER_ON)) - { - Vector3i Behind = a_BlockPos - GetRepeaterDirection(BlockMeta); - BLOCKTYPE BehindBlock; - NIBBLETYPE BehindMeta; - m_World.GetBlockTypeMeta(Behind.x, Behind.y, Behind.z, BehindBlock, BehindMeta); - switch (BehindBlock) + case E_BLOCK_ACTIVATOR_RAIL: + case E_BLOCK_POWERED_RAIL: { - case E_BLOCK_REDSTONE_TORCH_ON: - case E_BLOCK_LEVER: - { - // _X: TODO: Shouldn't a lever be checked if it is switched on? - return true; - } - case E_BLOCK_REDSTONE_WIRE: + if (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) { - return (BehindMeta > 0); + m_World.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) | 0x08); } - case E_BLOCK_REDSTONE_REPEATER_ON: + else { - return IsRepeaterPointingTo(Behind, BehindMeta, a_BlockPos); + m_World.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) & 0x07); } - } // switch (BehindBlock) - return false; - } - - if (IsPowering(Vector3i(a_BlockPos.x - 1, a_BlockPos.y, a_BlockPos.z), a_BlockPos, REDSTONE_X_POS, a_bOnlyByWire)) - { - return true; - } - if (IsPowering(Vector3i(a_BlockPos.x + 1, a_BlockPos.y, a_BlockPos.z), a_BlockPos, REDSTONE_X_NEG, a_bOnlyByWire)) - { - return true; - } - if (IsPowering(Vector3i(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z - 1), a_BlockPos, REDSTONE_Z_POS, a_bOnlyByWire)) - { - return true; - } - if (IsPowering(Vector3i(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z + 1), a_BlockPos, REDSTONE_Z_NEG, a_bOnlyByWire)) - { - return true; - } - - // Only wires can power the bottom block - BLOCKTYPE PosYType; - NIBBLETYPE PosYMeta; - m_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y + 1, a_BlockPos.z, PosYType, PosYMeta); - if (PosYType == E_BLOCK_REDSTONE_WIRE) - { - return (PosYMeta > 0); + break; + } } - - return false; } -// Believe me, it works!! TODO: Add repeaters and low/high wires -cRedstoneSimulator::eRedstoneDirection cRedstoneSimulator::GetWireDirection(int a_BlockX, int a_BlockY, int a_BlockZ) -{ - int Dir = REDSTONE_NONE; - BLOCKTYPE NegX = m_World.GetBlock(a_BlockX - 1, a_BlockY, a_BlockZ); - if ( - (NegX == E_BLOCK_REDSTONE_WIRE) || - (NegX == E_BLOCK_REDSTONE_TORCH_ON) || - (NegX == E_BLOCK_LEVER) - ) - { - Dir |= (REDSTONE_X_POS); - } - - BLOCKTYPE PosX = m_World.GetBlock(a_BlockX + 1, a_BlockY, a_BlockZ); - if ( - (PosX == E_BLOCK_REDSTONE_WIRE) || - (PosX == E_BLOCK_REDSTONE_TORCH_ON) || - (PosX == E_BLOCK_LEVER) - ) - { - Dir |= (REDSTONE_X_NEG); - } - - BLOCKTYPE NegZ = m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ - 1); - if ( - (NegZ == E_BLOCK_REDSTONE_WIRE) || - (NegZ == E_BLOCK_REDSTONE_TORCH_ON) || - (NegZ == E_BLOCK_LEVER) - ) +bool cRedstoneSimulator::AreCoordsPowered(int a_BlockX, int a_BlockY, int a_BlockZ) +{ + for (PoweredBlocksList::iterator itr = m_PoweredBlocks.begin(); itr != m_PoweredBlocks.end(); ++itr) // Check powered list { - if ((Dir & REDSTONE_X_POS) && !(Dir & REDSTONE_X_NEG)) // corner - { - Dir ^= REDSTONE_X_POS; - Dir |= REDSTONE_X_NEG; - } - if ((Dir & REDSTONE_X_NEG) && !(Dir & REDSTONE_X_POS)) // corner + sPoweredBlocks & Change = *itr; + + if (Change.a_BlockPos.Equals(Vector3i(a_BlockX, a_BlockY, a_BlockZ))) { - Dir ^= REDSTONE_X_NEG; - Dir |= REDSTONE_X_POS; + return true; } - Dir |= REDSTONE_Z_POS; } - - BLOCKTYPE PosZ = m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ + 1); - if ( - (PosZ == E_BLOCK_REDSTONE_WIRE) || - (PosZ == E_BLOCK_REDSTONE_TORCH_ON) || - (PosZ == E_BLOCK_LEVER) - ) + + for (LinkedBlocksList::iterator itr = m_LinkedPoweredBlocks.begin(); itr != m_LinkedPoweredBlocks.end(); ++itr) // Check linked powered list { - if ((Dir & REDSTONE_X_POS) && !(Dir & REDSTONE_X_NEG)) // corner - { - Dir ^= REDSTONE_X_POS; - Dir |= REDSTONE_X_NEG; - } - if ((Dir & REDSTONE_X_NEG) && !(Dir & REDSTONE_X_POS)) // corner + sLinkedPoweredBlocks & Change = *itr; + + if (Change.a_BlockPos.Equals(Vector3i(a_BlockX, a_BlockY, a_BlockZ))) { - Dir ^= REDSTONE_X_NEG; - Dir |= REDSTONE_X_POS; + return true; } - Dir |= REDSTONE_Z_NEG; } - - return (eRedstoneDirection)Dir; + return false; } -bool cRedstoneSimulator::IsRepeaterPointingTo(const Vector3i & a_RepeaterPos, char a_MetaData, const Vector3i & a_BlockPos) +bool cRedstoneSimulator::IsRepeaterPowered(int a_BlockX, int a_BlockY, int a_BlockZ, NIBBLETYPE a_Meta) { - switch (a_MetaData & 0x3) + // Check through powered blocks list + for (PoweredBlocksList::iterator itr = m_PoweredBlocks.begin(); itr != m_PoweredBlocks.end(); ++itr) { - case 0x0: + sPoweredBlocks & Change = *itr; + + switch (a_Meta) { - if ((a_RepeaterPos - a_BlockPos).Equals(Vector3i(0, 0, 1))) + case 0x0: { - return true; + // Flip the coords to check the back of the repeater + if (Change.a_SourcePos.Equals(Vector3i(a_BlockX, a_BlockY, a_BlockZ + 1))) { return true; } + break; } - break; - } - - case 0x1: - { - if ((a_RepeaterPos - a_BlockPos).Equals(Vector3i(-1, 0, 0))) + case 0x1: { - return true; + if (Change.a_SourcePos.Equals(Vector3i(a_BlockX - 1, a_BlockY, a_BlockZ))) { return true; } + break; } - break; - } - - case 0x2: - { - if ((a_RepeaterPos - a_BlockPos).Equals(Vector3i(0, 0,-1))) + case 0x2: + { + if (Change.a_SourcePos.Equals(Vector3i(a_BlockX, a_BlockY, a_BlockZ - 1))) { return true; } + break; + } + case 0x3: { - return true; + if (Change.a_SourcePos.Equals(Vector3i(a_BlockX + 1, a_BlockY, a_BlockZ))) { return true; } + break; } - break; } - - case 0x3: + } + + // Check linked powered list, 'middle' blocks + for (LinkedBlocksList::iterator itr = m_LinkedPoweredBlocks.begin(); itr != m_LinkedPoweredBlocks.end(); ++itr) + { + sLinkedPoweredBlocks & Change = *itr; + + switch (a_Meta) { - if ((a_RepeaterPos - a_BlockPos).Equals(Vector3i(1, 0, 0))) + case 0x0: { - return true; + if (Change.a_MiddlePos.Equals(Vector3i(a_BlockX, a_BlockY, a_BlockZ + 1))) { return true; } + break; + } + case 0x1: + { + if (Change.a_MiddlePos.Equals(Vector3i(a_BlockX - 1, a_BlockY, a_BlockZ))) { return true; } + break; + } + case 0x2: + { + if (Change.a_MiddlePos.Equals(Vector3i(a_BlockX, a_BlockY, a_BlockZ - 1))) { return true; } + break; + } + case 0x3: + { + if (Change.a_MiddlePos.Equals(Vector3i(a_BlockX + 1, a_BlockY, a_BlockZ))) { return true; } + break; } - break; } } - return false; + return false; // Couldn't find power source behind repeater } -bool cRedstoneSimulator::IsRepeaterPointingAway(const Vector3i & a_RepeaterPos, char a_MetaData, const Vector3i & a_BlockPos) +void cRedstoneSimulator::SetDirectionLinkedPowered(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Direction, BLOCKTYPE a_SourceType) { - switch (a_MetaData & 0x3) + switch (a_Direction) { - case 0x0: + case BLOCK_FACE_XM: { - if ((a_RepeaterPos - a_BlockPos).Equals(Vector3i(0, 0,-1))) - { - return true; - } + BLOCKTYPE MiddleBlock = m_World.GetBlock(a_BlockX - 1, a_BlockY, a_BlockZ); + if (!g_BlockIsSolid[MiddleBlock]) { return; } + + SetBlockLinkedPowered(a_BlockX - 2, a_BlockY, a_BlockZ, a_BlockX - 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX - 1, a_BlockY + 1, a_BlockZ, a_BlockX - 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX - 1, a_BlockY - 1, a_BlockZ, a_BlockX - 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX - 1, a_BlockY, a_BlockZ + 1, a_BlockX - 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX - 1, a_BlockY, a_BlockZ - 1, a_BlockX - 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); break; } - - case 0x1: + case BLOCK_FACE_XP: { - if ((a_RepeaterPos - a_BlockPos).Equals(Vector3i(1, 0, 0))) - { - return true; - } + BLOCKTYPE MiddleBlock = m_World.GetBlock(a_BlockX + 1, a_BlockY, a_BlockZ); + if (!g_BlockIsSolid[MiddleBlock]) { return; } + + SetBlockLinkedPowered(a_BlockX + 2, a_BlockY, a_BlockZ, a_BlockX + 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX + 1, a_BlockY + 1, a_BlockZ, a_BlockX + 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX + 1, a_BlockY - 1, a_BlockZ, a_BlockX + 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX + 1, a_BlockY, a_BlockZ + 1, a_BlockX + 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX + 1, a_BlockY, a_BlockZ - 1, a_BlockX + 1, a_BlockY, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); break; } - - case 0x2: + case BLOCK_FACE_YM: { - if ((a_RepeaterPos - a_BlockPos).Equals(Vector3i(0, 0, 1))) - { - return true; - } + BLOCKTYPE MiddleBlock = m_World.GetBlock(a_BlockX, a_BlockY - 1, a_BlockZ); + if (!g_BlockIsSolid[MiddleBlock]) { return; } + + SetBlockLinkedPowered(a_BlockX, a_BlockY - 2, a_BlockZ, a_BlockX, a_BlockY - 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX + 1, a_BlockY - 1, a_BlockZ, a_BlockX, a_BlockY - 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX - 1, a_BlockY - 1, a_BlockZ, a_BlockX, a_BlockY - 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX, a_BlockY - 1, a_BlockZ + 1, a_BlockX, a_BlockY - 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX, a_BlockY - 1, a_BlockZ - 1, a_BlockX, a_BlockY - 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); break; } - - case 0x3: + case BLOCK_FACE_YP: { - if ((a_RepeaterPos - a_BlockPos).Equals(Vector3i(-1, 0, 0))) - { - return true; - } + BLOCKTYPE MiddleBlock = m_World.GetBlock(a_BlockX, a_BlockY + 1, a_BlockZ); + if (!g_BlockIsSolid[MiddleBlock]) { return; } + + SetBlockLinkedPowered(a_BlockX, a_BlockY + 2, a_BlockZ, a_BlockX, a_BlockY + 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX + 1, a_BlockY + 1, a_BlockZ, a_BlockX, a_BlockY + 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX - 1, a_BlockY + 1, a_BlockZ, a_BlockX, a_BlockY + 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX, a_BlockY + 1, a_BlockZ + 1, a_BlockX, a_BlockY + 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX, a_BlockY + 1, a_BlockZ - 1, a_BlockX, a_BlockY + 1, a_BlockZ, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + break; + } + case BLOCK_FACE_ZM: + { + BLOCKTYPE MiddleBlock = m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ - 1); + if (!g_BlockIsSolid[MiddleBlock]) { return; } + + SetBlockLinkedPowered(a_BlockX, a_BlockY, a_BlockZ - 2, a_BlockX, a_BlockY, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX + 1, a_BlockY, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX - 1, a_BlockY, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX, a_BlockY + 1, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX, a_BlockY - 1, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ - 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + break; + } + case BLOCK_FACE_ZP: + { + BLOCKTYPE MiddleBlock = m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ + 1); + if (!g_BlockIsSolid[MiddleBlock]) { return; } + + SetBlockLinkedPowered(a_BlockX, a_BlockY, a_BlockZ + 2, a_BlockX, a_BlockY, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX + 1, a_BlockY, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX - 1, a_BlockY, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX, a_BlockY + 1, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + SetBlockLinkedPowered(a_BlockX, a_BlockY - 1, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ + 1, a_BlockX, a_BlockY, a_BlockZ, a_SourceType, MiddleBlock); + break; + } + default: + { + ASSERT(!"Unhandled face direction when attempting to set blocks as linked powered!"); break; } } - return false; + return; } -NIBBLETYPE cRedstoneSimulator::RepeaterRotationToMetaData(double a_Rotation) +void cRedstoneSimulator::SetBlockPowered(int a_BlockX, int a_BlockY, int a_BlockZ, int a_SourceX, int a_SourceY, int a_SourceZ, BLOCKTYPE a_SourceBlock) { - a_Rotation += 90 + 45; // So its not aligned with axis - if (a_Rotation > 360) - { - a_Rotation -= 360; - } - - if ((a_Rotation >= 0) && (a_Rotation < 90)) - { - return 0x1; - } - else if ((a_Rotation >= 180) && (a_Rotation < 270)) - { - return 0x3; - } - else if ((a_Rotation >= 90) && (a_Rotation < 180)) - { - return 0x2; - } - else - { - return 0x0; - } + if (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) { return; } // Check for duplicates + + sPoweredBlocks RC; + RC.a_BlockPos = Vector3i(a_BlockX, a_BlockY, a_BlockZ); + RC.a_SourcePos = Vector3i(a_SourceX, a_SourceY, a_SourceZ); + RC.a_SourceBlock = a_SourceBlock; + m_PoweredBlocks.push_back(RC); + return; } -Vector3i cRedstoneSimulator::GetRepeaterDirection(NIBBLETYPE a_MetaData) +void cRedstoneSimulator::SetBlockLinkedPowered(int a_BlockX, int a_BlockY, int a_BlockZ, + int a_MiddleX, int a_MiddleY, int a_MiddleZ, + int a_SourceX, int a_SourceY, int a_SourceZ, + BLOCKTYPE a_SourceBlock, BLOCKTYPE a_MiddleBlock + ) { - switch (a_MetaData & 0x3) - { - case 0x0: return Vector3i(0, 0,-1); - case 0x1: return Vector3i(1, 0, 0); - case 0x2: return Vector3i(0, 0, 1); - case 0x3: return Vector3i(-1, 0, 0); - } - return Vector3i(); + if (AreCoordsPowered(a_BlockX, a_BlockY, a_BlockZ)) { return; } // Check for duplicates + + sLinkedPoweredBlocks RC; + RC.a_BlockPos = Vector3i(a_BlockX, a_BlockY, a_BlockZ); + RC.a_MiddlePos = Vector3i(a_MiddleX, a_MiddleY, a_MiddleZ); + RC.a_SourcePos = Vector3i(a_SourceX, a_SourceY, a_SourceZ); + RC.a_SourceBlock = a_SourceBlock; + RC.a_MiddleBlock = a_MiddleBlock; + m_LinkedPoweredBlocks.push_back(RC); + return; } -NIBBLETYPE cRedstoneSimulator::LeverDirectionToMetaData(char a_Dir) +cRedstoneSimulator::eRedstoneDirection cRedstoneSimulator::GetWireDirection(int a_BlockX, int a_BlockY, int a_BlockZ) { - // Determine lever direction: - switch (a_Dir) + int Dir = REDSTONE_NONE; + + BLOCKTYPE NegX = m_World.GetBlock(a_BlockX - 1, a_BlockY, a_BlockZ); + if (IsPotentialSource(NegX)) { - case BLOCK_FACE_TOP: return 0x6; - case BLOCK_FACE_EAST: return 0x1; - case BLOCK_FACE_WEST: return 0x2; - case BLOCK_FACE_SOUTH: return 0x3; - case BLOCK_FACE_NORTH: return 0x4; - case BLOCK_FACE_BOTTOM: return 0x0; - default: return 0x6; + Dir |= (REDSTONE_X_POS); } -} - - - - - -bool cRedstoneSimulator::IsLeverOn(cWorld * a_World, const Vector3i & a_BlockPos) -{ - // Extract the metadata and ask the lower level: - return IsLeverOn(a_World->GetBlockMeta(a_BlockPos)); + + BLOCKTYPE PosX = m_World.GetBlock(a_BlockX + 1, a_BlockY, a_BlockZ); + if (IsPotentialSource(PosX)) + { + Dir |= (REDSTONE_X_NEG); + } + + BLOCKTYPE NegZ = m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ - 1); + if (IsPotentialSource(NegZ)) + { + if ((Dir & REDSTONE_X_POS) && !(Dir & REDSTONE_X_NEG)) // corner + { + Dir ^= REDSTONE_X_POS; + Dir |= REDSTONE_X_NEG; + } + if ((Dir & REDSTONE_X_NEG) && !(Dir & REDSTONE_X_POS)) // corner + { + Dir ^= REDSTONE_X_NEG; + Dir |= REDSTONE_X_POS; + } + Dir |= REDSTONE_Z_POS; + } + + BLOCKTYPE PosZ = m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ + 1); + if (IsPotentialSource(PosZ)) + { + if ((Dir & REDSTONE_X_POS) && !(Dir & REDSTONE_X_NEG)) // corner + { + Dir ^= REDSTONE_X_POS; + Dir |= REDSTONE_X_NEG; + } + if ((Dir & REDSTONE_X_NEG) && !(Dir & REDSTONE_X_POS)) // corner + { + Dir ^= REDSTONE_X_NEG; + Dir |= REDSTONE_X_POS; + } + Dir |= REDSTONE_Z_NEG; + } + return (eRedstoneDirection)Dir; } @@ -1145,32 +1063,9 @@ bool cRedstoneSimulator::IsLeverOn(NIBBLETYPE a_BlockMeta) -void cRedstoneSimulator::SetRepeater(const Vector3i & a_Position, int a_Ticks, bool a_bPowerOn) +bool cRedstoneSimulator::IsButtonOn(NIBBLETYPE a_BlockMeta) { - for (RepeaterList::iterator itr = m_SetRepeaters.begin(); itr != m_SetRepeaters.end(); ++itr) - { - sRepeaterChange & Change = *itr; - if (Change.Position.Equals(a_Position)) - { - if (Change.bPowerOn && !a_bPowerOn) - { - Change.bPowerOffNextTime = true; - } - if (a_bPowerOn) - { - Change.bPowerOffNextTime = false; - } - Change.bPowerOn |= a_bPowerOn; - return; - } - } - - sRepeaterChange RC; - RC.Position = a_Position; - RC.Ticks = a_Ticks; - RC.bPowerOn = a_bPowerOn; - RC.bPowerOffNextTime = false; - m_SetRepeaters.push_back(RC); + return IsLeverOn(a_BlockMeta); } diff --git a/source/Simulator/RedstoneSimulator.h b/source/Simulator/RedstoneSimulator.h index c0d5795c7..d68c6daeb 100644 --- a/source/Simulator/RedstoneSimulator.h +++ b/source/Simulator/RedstoneSimulator.h @@ -3,6 +3,9 @@ #include "Simulator.h" +/// Per-chunk data for the simulator, specified individual chunks to simulate; 'Data' is not used +typedef cCoordWithIntList cRedstoneSimulatorChunkData; + @@ -12,13 +15,13 @@ class cRedstoneSimulator : { typedef cSimulator super; public: + cRedstoneSimulator(cWorld & a_World); ~cRedstoneSimulator(); - virtual void Simulate( float a_Dt ) override; - virtual bool IsAllowedBlock( BLOCKTYPE a_BlockType ) override { return true; } - - virtual void WakeUp(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) override; + virtual void Simulate(float a_Dt) override {}; // Not used in this simulator + virtual void SimulateChunk(float a_Dt, int a_ChunkX, int a_ChunkZ, cChunk * a_Chunk) override; + virtual bool IsAllowedBlock( BLOCKTYPE a_BlockType ) override { return IsRedstone(a_BlockType); } enum eRedstoneDirection { @@ -31,56 +34,166 @@ public: eRedstoneDirection GetWireDirection(int a_BlockX, int a_BlockY, int a_BlockZ); eRedstoneDirection GetWireDirection(const Vector3i & a_Pos) { return GetWireDirection(a_Pos.x, a_Pos.y, a_Pos.z); } - static bool IsRepeaterPointingTo (const Vector3i & a_RepeaterPos, char a_MetaData, const Vector3i & a_BlockPos); - static bool IsRepeaterPointingAway(const Vector3i & a_RepeaterPos, char a_MetaData, const Vector3i & a_BlockPos); - static NIBBLETYPE RepeaterRotationToMetaData(double a_Rotation); - static Vector3i GetRepeaterDirection(NIBBLETYPE a_MetaData); - static NIBBLETYPE LeverDirectionToMetaData(char a_Dir); - static bool IsLeverOn(cWorld * a_World, const Vector3i & a_BlockPos); - static bool IsLeverOn(NIBBLETYPE a_BlockMeta); - - private: - struct sRepeaterChange + + struct sPoweredBlocks // Define structure of the directly powered blocks list { - Vector3i Position; - int Ticks; - bool bPowerOn; - bool bPowerOffNextTime; + Vector3i a_BlockPos; // Position of powered block + Vector3i a_SourcePos; // Position of source powering the block at a_BlockPos + BLOCKTYPE a_SourceBlock; // The source block type (for pistons pushing away sources and replacing with non source etc.) }; - typedef std::deque BlockList; - - typedef std::deque< sRepeaterChange > RepeaterList; - RepeaterList m_SetRepeaters; - - void SetRepeater(const Vector3i & a_Position, int a_Ticks, bool a_bPowerOn); - - virtual void AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) override {} - - void HandleChange( const Vector3i & a_BlockPos ); - BlockList RemoveCurrent( const Vector3i & a_BlockPos ); - - bool PowerBlock( const Vector3i & a_BlockPos, const Vector3i & a_FromBlock, char a_Power ); - int UnPowerBlock( const Vector3i & a_BlockPos, const Vector3i & a_FromBlock ); - - bool IsPowered( const Vector3i & a_BlockPos, bool a_bOnlyByWire = false ); - bool IsPowering( const Vector3i & a_PowerPos, const Vector3i & a_BlockPos, eRedstoneDirection a_WireDirection, bool a_bOnlyByWire ); - - BlockList m_Blocks; - BlockList m_BlocksBuffer; - - BlockList m_RefreshPistons; - BlockList m_RefreshDropSpensers; - - BlockList m_RefreshTorchesAround; + struct sLinkedPoweredBlocks // Define structure of the indirectly powered blocks list (i.e. repeaters powering through a block to the block at the other side) + { + Vector3i a_BlockPos; + Vector3i a_MiddlePos; + Vector3i a_SourcePos; + BLOCKTYPE a_SourceBlock; + BLOCKTYPE a_MiddleBlock; + }; - void RefreshTorchesAround( const Vector3i & a_BlockPos ); - - // TODO: The entire simulator is synchronized, no need to lock data structures; remove this - cCriticalSection m_CS; -}; - - - - + typedef std::vector PoweredBlocksList; + typedef std::vector LinkedBlocksList; + + PoweredBlocksList m_PoweredBlocks; + LinkedBlocksList m_LinkedPoweredBlocks; + + virtual void AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) override; + + // We want a_MyState for devices needing a full FastSetBlock (as opposed to meta) because with our simulation model, we cannot keep setting the block if it is already set correctly + // In addition to being non-performant, it would stop the player from actually breaking said device + + /* ====== SOURCES ====== */ + ///Handles the redstone torch + void HandleRedstoneTorch(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_MyState); + ///Handles the redstone block + void HandleRedstoneBlock(int a_BlockX, int a_BlockY, int a_BlockZ); + ///Handles levers + void HandleRedstoneLever(int a_BlockX, int a_BlockY, int a_BlockZ); + ///Handles buttons + void HandleRedstoneButton(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType); + /* ==================== */ + + /* ====== CARRIERS ====== */ + ///Handles redstone wire + void HandleRedstoneWire(int a_BlockX, int a_BlockY, int a_BlockZ); + ///Handles repeaters + void HandleRedstoneRepeater(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_MyState); + /* ====================== */ + + /* ====== DEVICES ====== */ + ///Handles pistons + void HandlePiston(int a_BlockX, int a_BlockY, int a_BlockZ); + ///Handles dispensers and droppers + void HandleDropSpenser(int a_BlockX, int a_BlockY, int a_BlockZ); + ///Handles TNT (exploding) + void HandleTNT(int a_BlockX, int a_BlockY, int a_BlockZ); + ///Handles redstone lamps + void HandleRedstoneLamp(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_MyState); + ///Handles doords + void HandleDoor(int a_BlockX, int a_BlockY, int a_BlockZ); + ///Handles activator, detector, and powered rails + void HandleRail(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_MyType); + /* ===================== */ + + /* ====== Helper functions ====== */ + void SetBlockPowered(int a_BlockX, int a_BlockY, int a_BlockZ, int a_SourceX, int a_SourceY, int a_SourceZ, BLOCKTYPE a_SourceBlock); + void SetBlockLinkedPowered(int a_BlockX, int a_BlockY, int a_BlockZ, int a_MiddleX, int a_MiddleY, int a_MiddleZ, int a_SourceX, int a_SourceY, int a_SourceZ, BLOCKTYPE a_SourceBlock, BLOCKTYPE a_MiddeBlock); + void SetDirectionLinkedPowered(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Direction, BLOCKTYPE a_SourceType); + + bool AreCoordsPowered(int a_BlockX, int a_BlockY, int a_BlockZ); + bool IsRepeaterPowered(int a_BlockX, int a_BlockY, int a_BlockZ, NIBBLETYPE a_Meta); + + bool IsLeverOn(NIBBLETYPE a_BlockMeta); + bool IsButtonOn(NIBBLETYPE a_BlockMeta); + /* ============================== */ + + inline static bool IsMechanism(BLOCKTYPE Block) + { + switch (Block) + { + case E_BLOCK_PISTON: + case E_BLOCK_STICKY_PISTON: + case E_BLOCK_DISPENSER: + case E_BLOCK_DROPPER: + case E_BLOCK_TNT: + case E_BLOCK_REDSTONE_LAMP_OFF: + case E_BLOCK_REDSTONE_LAMP_ON: + case E_BLOCK_WOODEN_DOOR: + case E_BLOCK_IRON_DOOR: + case E_BLOCK_REDSTONE_REPEATER_OFF: + case E_BLOCK_REDSTONE_REPEATER_ON: + case E_BLOCK_POWERED_RAIL: + { + return true; + } + default: return false; + } + } + + inline static bool IsPotentialSource(BLOCKTYPE Block) + { + switch (Block) + { + case E_BLOCK_WOODEN_BUTTON: + case E_BLOCK_STONE_BUTTON: + case E_BLOCK_REDSTONE_WIRE: + case E_BLOCK_REDSTONE_TORCH_OFF: + case E_BLOCK_REDSTONE_TORCH_ON: + case E_BLOCK_LEVER: + case E_BLOCK_REDSTONE_REPEATER_ON: + case E_BLOCK_REDSTONE_REPEATER_OFF: + case E_BLOCK_BLOCK_OF_REDSTONE: + case E_BLOCK_ACTIVE_COMPARATOR: + case E_BLOCK_INACTIVE_COMPARATOR: + { + return true; + } + default: return false; + } + } + + inline static bool IsRedstone(BLOCKTYPE Block) + { + switch (Block) + { + // All redstone devices, please alpha sort + case E_BLOCK_ACTIVATOR_RAIL: + case E_BLOCK_ACTIVE_COMPARATOR: + case E_BLOCK_BLOCK_OF_REDSTONE: + case E_BLOCK_DETECTOR_RAIL: + case E_BLOCK_DISPENSER: + case E_BLOCK_DAYLIGHT_SENSOR: + case E_BLOCK_DROPPER: + case E_BLOCK_FENCE_GATE: + case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE: + case E_BLOCK_HOPPER: + case E_BLOCK_INACTIVE_COMPARATOR: + case E_BLOCK_IRON_DOOR: + case E_BLOCK_LEVER: + case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE: + case E_BLOCK_NOTE_BLOCK: + case E_BLOCK_REDSTONE_LAMP_OFF: + case E_BLOCK_REDSTONE_LAMP_ON: + case E_BLOCK_REDSTONE_REPEATER_OFF: + case E_BLOCK_REDSTONE_REPEATER_ON: + case E_BLOCK_REDSTONE_TORCH_OFF: + case E_BLOCK_REDSTONE_TORCH_ON: + case E_BLOCK_REDSTONE_WIRE: + case E_BLOCK_STICKY_PISTON: + case E_BLOCK_STONE_BUTTON: + case E_BLOCK_STONE_PRESSURE_PLATE: + case E_BLOCK_TNT: + case E_BLOCK_TRAPDOOR: + case E_BLOCK_TRIPWIRE_HOOK: + case E_BLOCK_WOODEN_BUTTON: + case E_BLOCK_WOODEN_DOOR: + case E_BLOCK_WOODEN_PRESSURE_PLATE: + case E_BLOCK_PISTON: + { + return true; + } + default: return false; + } + } +}; \ No newline at end of file -- cgit v1.2.3