From de5b1401f813985300c33c6b3166b115f9a10dd3 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 8 Feb 2015 16:35:10 +0000 Subject: Comparators --- src/Simulator/IncrementalRedstoneSimulator.h | 58 ++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator.h index 43f0e89d0..65e3ff4af 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator.h @@ -114,73 +114,98 @@ private: // 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_RelBlockX, int a_RelBlockY, int a_RelBlockZ, BLOCKTYPE a_MyState); + /** Handles the redstone block */ void HandleRedstoneBlock(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles levers */ void HandleRedstoneLever(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles buttons */ void HandleRedstoneButton(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles daylight sensors */ void HandleDaylightSensor(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles pressure plates */ void HandlePressurePlate(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, BLOCKTYPE a_MyType); + /** Handles tripwire hooks Performs correct meta and power setting for self by going in the direction it faces and looking for a continous line of tripwire bounded by another oppositely facing hook If this line is complete, it verifies that at least on wire reports an entity is on top (via its meta), and performs its task */ void HandleTripwireHook(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles trapped chests */ void HandleTrappedChest(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); - /* ==================== */ - /* ====== CARRIERS ====== */ + /** Handles redstone wire */ void HandleRedstoneWire(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles repeaters */ void HandleRedstoneRepeater(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, BLOCKTYPE a_MyState); - /* ====================== */ - /* ====== DEVICES ====== */ + /** Handles comparators */ + void HandleRedstoneComparator(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + + /** Handles pistons */ void HandlePiston(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles dispensers and droppers */ void HandleDropSpenser(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles TNT (exploding) */ void HandleTNT(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles redstone lamps */ void HandleRedstoneLamp(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, BLOCKTYPE a_MyState); + /** Handles doords */ void HandleDoor(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles command blocks */ void HandleCommandBlock(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles activator, detector, and powered rails */ void HandleRail(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, BLOCKTYPE a_MyType); + /** Handles trapdoors */ void HandleTrapdoor(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles fence gates */ void HandleFenceGate(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles noteblocks */ void HandleNoteBlock(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Handles tripwires */ void HandleTripwire(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); - /* ===================== */ - /* ====== Helper functions ====== */ + /** Marks a block as powered */ - void SetBlockPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, unsigned char a_PowerLevel = MAX_POWER_LEVEL); + void SetBlockPowered(Vector3i a_RelBlockPosition, Vector3i a_RelSourcePosition, unsigned char a_PowerLevel = MAX_POWER_LEVEL); + void SetBlockPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, unsigned char a_PowerLevel = MAX_POWER_LEVEL) { SetBlockPowered(Vector3i(a_RelBlockX, a_RelBlockY, a_RelBlockZ), Vector3i(a_RelSourceX, a_RelSourceY, a_RelSourceZ), a_PowerLevel); } + /** Marks a block as being powered through another block */ void SetBlockLinkedPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, int a_RelMiddleX, int a_RelMiddleY, int a_RelMiddleZ, int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, BLOCKTYPE a_MiddeBlock, unsigned char a_PowerLevel = MAX_POWER_LEVEL); + /** Marks a block as simulated, who should not be simulated further unless their power state changes, to accomodate a player manually toggling the block without triggering the simulator toggling it back */ void SetPlayerToggleableBlockAsSimulated(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, bool WasLastStatePowered); + /** Marks the second block in a direction as linked powered */ void SetDirectionLinkedPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, char a_Direction, unsigned char a_PowerLevel = MAX_POWER_LEVEL); + /** Marks all blocks immediately surrounding a coordinate as powered */ void SetAllDirsAsPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, unsigned char a_PowerLevel = MAX_POWER_LEVEL); + /** Queues a repeater to be powered or unpowered and returns if the m_RepeatersDelayList iterators were invalidated */ bool QueueRepeaterPowerChange(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, NIBBLETYPE a_Meta, bool ShouldPowerOn); + /** Removes a block from the Powered and LinkedPowered lists Used for variable sources such as tripwire hooks, daylight sensors, and trapped chests */ @@ -189,31 +214,39 @@ private: /** Returns if a coordinate is powered or linked powered */ bool AreCoordsPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ) { return AreCoordsDirectlyPowered(a_RelBlockX, a_RelBlockY, a_RelBlockZ, m_Chunk) || AreCoordsLinkedPowered(a_RelBlockX, a_RelBlockY, a_RelBlockZ); } + /** Returns if a coordinate is in the directly powered blocks list */ bool AreCoordsDirectlyPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, cChunk * a_Chunk); + /** Returns if a coordinate is in the indirectly powered blocks list */ bool AreCoordsLinkedPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + /** Returns if a coordinate was marked as simulated (for blocks toggleable by players) */ bool AreCoordsSimulated(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, bool IsCurrentStatePowered); + /** Returns if a repeater is powered by testing for power sources behind the repeater */ bool IsRepeaterPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, NIBBLETYPE a_Meta); + /** Returns if a repeater is locked */ bool IsRepeaterLocked(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, NIBBLETYPE a_Meta); + /** Returns if a piston is powered */ bool IsPistonPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, NIBBLETYPE a_Meta); + /** Returns if a wire is powered The only diffence between this and a normal AreCoordsPowered is that this function checks for a wire powering another wire */ - bool IsWirePowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, unsigned char & a_PowerLevel); + unsigned char IsWirePowered(Vector3i a_RelBlockPosition); + /** Handles delayed updates to repeaters **/ void HandleRedstoneRepeaterDelays(void); /** Returns if lever metadata marks it as emitting power */ bool IsLeverOn(NIBBLETYPE a_BlockMeta); + /** Returns if button metadata marks it as emitting power */ bool IsButtonOn(NIBBLETYPE a_BlockMeta) { return IsLeverOn(a_BlockMeta); } - /* ============================== */ - /* ====== Misc Functions ====== */ + /** Returns if a block is viable to be the MiddleBlock of a SetLinkedPowered operation */ inline static bool IsViableMiddleBlock(BLOCKTYPE Block) { return cBlockInfo::FullyOccupiesVoxel(Block); } @@ -227,6 +260,7 @@ private: case E_BLOCK_ACACIA_DOOR: case E_BLOCK_ACACIA_FENCE_GATE: case E_BLOCK_ACTIVATOR_RAIL: + case E_BLOCK_ACTIVE_COMPARATOR: case E_BLOCK_BIRCH_DOOR: case E_BLOCK_BIRCH_FENCE_GATE: case E_BLOCK_COMMAND_BLOCK: @@ -236,6 +270,7 @@ private: case E_BLOCK_DROPPER: case E_BLOCK_FENCE_GATE: case E_BLOCK_HOPPER: + case E_BLOCK_INACTIVE_COMPARATOR: case E_BLOCK_IRON_DOOR: case E_BLOCK_IRON_TRAPDOOR: case E_BLOCK_JUNGLE_DOOR: @@ -276,6 +311,7 @@ private: case E_BLOCK_REDSTONE_REPEATER_ON: case E_BLOCK_BLOCK_OF_REDSTONE: case E_BLOCK_ACTIVE_COMPARATOR: + case E_BLOCK_INACTIVE_COMPARATOR: case E_BLOCK_HEAVY_WEIGHTED_PRESSURE_PLATE: case E_BLOCK_LIGHT_WEIGHTED_PRESSURE_PLATE: case E_BLOCK_STONE_PRESSURE_PLATE: -- cgit v1.2.3 From bd484750856586bd2354be2aa6852a63fd102c1b Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 20 Mar 2015 22:30:20 +0000 Subject: Provides improvements to redstone wire Intermediary commit that fixes #1763. --- src/Simulator/IncrementalRedstoneSimulator.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator.h index 65e3ff4af..903dbe82a 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator.h @@ -22,7 +22,8 @@ class cIncrementalRedstoneSimulator : public: cIncrementalRedstoneSimulator(cWorld & a_World) - : cRedstoneSimulator(a_World) + : cRedstoneSimulator(a_World), + m_Chunk(nullptr) { } @@ -209,7 +210,7 @@ private: /** Removes a block from the Powered and LinkedPowered lists Used for variable sources such as tripwire hooks, daylight sensors, and trapped chests */ - void SetSourceUnpowered(int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, cChunk * a_Chunk, bool a_IsFirstCall = true); + void SetSourceUnpowered(int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, cChunk * a_Chunk); void SetInvalidMiddleBlock(int a_RelMiddleX, int a_RelMiddleY, int a_RelMiddleZ, cChunk * a_Chunk, bool a_IsFirstCall = true); /** Returns if a coordinate is powered or linked powered */ @@ -384,14 +385,14 @@ private: } } - inline static bool AreCoordsOnChunkBoundary(int a_BlockX, int a_BlockY, int a_BlockZ) + inline static Vector3i GetCoordinateAdjacentChunk(const Vector3i & a_BlockPos) { - return ( // Are we on a chunk boundary? +- 2 because of LinkedPowered blocks - ((a_BlockX % cChunkDef::Width) <= 1) || - ((a_BlockX % cChunkDef::Width) >= 14) || - ((a_BlockZ % cChunkDef::Width) <= 1) || - ((a_BlockZ % cChunkDef::Width) >= 14) - ); + // Are we on a chunk boundary? +- 2 because of LinkedPowered blocks + if ((a_BlockPos.x % cChunkDef::Width) <= 1) { return{ -2, 0, 0 }; } + if ((a_BlockPos.x % cChunkDef::Width) >= 14) { return{ 2, 0, 0 }; } + if ((a_BlockPos.z % cChunkDef::Width) <= 1) { return{ 0, 0, -2 }; } + if ((a_BlockPos.z % cChunkDef::Width) >= 14) { return{ 0, 0, 2 }; } + return { 0, 0, 0 }; } inline static Vector3i AdjustRelativeCoords(const Vector3i & a_RelPosition) -- cgit v1.2.3 From 84534dfeb288dd12d998f3f192ee162a1def5367 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 26 Apr 2015 00:38:41 +0100 Subject: Redstone wire and trapdoor fixes * Fixes #1887 * Fixes #1763 * Fixes #1083 --- src/Simulator/IncrementalRedstoneSimulator.h | 69 +++++++++++++++++----------- 1 file changed, 42 insertions(+), 27 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator.h index 903dbe82a..c383b6a0b 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator.h @@ -3,6 +3,7 @@ #include "RedstoneSimulator.h" #include "BlockEntities/RedstonePoweredEntity.h" +#include class cWorld; class cChunk; @@ -37,15 +38,14 @@ public: virtual bool IsAllowedBlock(BLOCKTYPE a_BlockType) override { return IsRedstone(a_BlockType); } virtual void WakeUp(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) override; - enum eRedstoneDirection + enum eRedstoneWireDirectionBitfieldPositions { - REDSTONE_NONE = 0, - REDSTONE_X_POS = 0x1, - REDSTONE_X_NEG = 0x2, - REDSTONE_Z_POS = 0x4, - REDSTONE_Z_NEG = 0x8, + eWbpXP = 0, + eWbpXN = 1, + eWbpZP = 2, + eWbpZN = 3, }; - eRedstoneDirection GetWireDirection(int a_BlockX, int a_BlockY, int a_BlockZ); + std::bitset<4> GetWireDirection(int a_BlockX, int a_BlockY, int a_BlockZ); private: @@ -108,8 +108,9 @@ private: SimulatedPlayerToggleableList * m_SimulatedPlayerToggleableBlocks; RepeatersDelayList * m_RepeatersDelayList; - virtual void AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) override { RedstoneAddBlock(a_BlockX, a_BlockY, a_BlockZ, a_Chunk); } - void RedstoneAddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk, cChunk * a_OtherChunk = nullptr); + virtual void AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) override; + + void AddBlock(const Vector3i & a_BlockPosition, cChunk * a_Chunk, cChunk * a_OtherChunk = nullptr); cChunk * m_Chunk; // 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 @@ -192,6 +193,9 @@ private: void SetBlockPowered(Vector3i a_RelBlockPosition, Vector3i a_RelSourcePosition, unsigned char a_PowerLevel = MAX_POWER_LEVEL); void SetBlockPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, unsigned char a_PowerLevel = MAX_POWER_LEVEL) { SetBlockPowered(Vector3i(a_RelBlockX, a_RelBlockY, a_RelBlockZ), Vector3i(a_RelSourceX, a_RelSourceY, a_RelSourceZ), a_PowerLevel); } + /** Recursively searches for a wire path and powers everything that should be powered */ + void FindAndPowerBorderingWires(std::vector> & a_PotentialWireList, const std::pair & a_Entry); + /** Marks a block as being powered through another block */ void SetBlockLinkedPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, int a_RelMiddleX, int a_RelMiddleY, int a_RelMiddleZ, int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, BLOCKTYPE a_MiddeBlock, unsigned char a_PowerLevel = MAX_POWER_LEVEL); @@ -214,13 +218,13 @@ private: void SetInvalidMiddleBlock(int a_RelMiddleX, int a_RelMiddleY, int a_RelMiddleZ, cChunk * a_Chunk, bool a_IsFirstCall = true); /** Returns if a coordinate is powered or linked powered */ - bool AreCoordsPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ) { return AreCoordsDirectlyPowered(a_RelBlockX, a_RelBlockY, a_RelBlockZ, m_Chunk) || AreCoordsLinkedPowered(a_RelBlockX, a_RelBlockY, a_RelBlockZ); } + bool AreCoordsPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ) { return AreCoordsDirectlyPowered(a_RelBlockX, a_RelBlockY, a_RelBlockZ, m_Chunk) || AreCoordsLinkedPowered(a_RelBlockX, a_RelBlockY, a_RelBlockZ, m_Chunk); } /** Returns if a coordinate is in the directly powered blocks list */ - bool AreCoordsDirectlyPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, cChunk * a_Chunk); + static bool AreCoordsDirectlyPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, cChunk * a_Chunk); /** Returns if a coordinate is in the indirectly powered blocks list */ - bool AreCoordsLinkedPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ); + static bool AreCoordsLinkedPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, cChunk * a_Chunk); /** Returns if a coordinate was marked as simulated (for blocks toggleable by players) */ bool AreCoordsSimulated(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, bool IsCurrentStatePowered); @@ -236,7 +240,7 @@ private: /** Returns if a wire is powered The only diffence between this and a normal AreCoordsPowered is that this function checks for a wire powering another wire */ - unsigned char IsWirePowered(Vector3i a_RelBlockPosition); + static unsigned char IsWirePowered(Vector3i a_RelBlockPosition, cChunk * a_Chunk); /** Handles delayed updates to repeaters **/ void HandleRedstoneRepeaterDelays(void); @@ -249,14 +253,14 @@ private: /** Returns if a block is viable to be the MiddleBlock of a SetLinkedPowered operation */ - inline static bool IsViableMiddleBlock(BLOCKTYPE Block) { return cBlockInfo::FullyOccupiesVoxel(Block); } + inline static bool IsViableMiddleBlock(BLOCKTYPE a_Block) { return cBlockInfo::FullyOccupiesVoxel(a_Block); } /** Returns if a block is a mechanism (something that accepts power and does something) Used by torches to determine if they power a block whilst not standing on the ground */ - inline static bool IsMechanism(BLOCKTYPE Block) + inline static bool IsMechanism(BLOCKTYPE a_Block) { - switch (Block) + switch (a_Block) { case E_BLOCK_ACACIA_DOOR: case E_BLOCK_ACACIA_FENCE_GATE: @@ -298,9 +302,9 @@ private: } /** Returns if a block has the potential to output power */ - inline static bool IsPotentialSource(BLOCKTYPE Block) + inline static bool IsPotentialSource(BLOCKTYPE a_Block) { - switch (Block) + switch (a_Block) { case E_BLOCK_DETECTOR_RAIL: case E_BLOCK_DAYLIGHT_SENSOR: @@ -326,9 +330,9 @@ private: } /** Returns if a block is any sort of redstone device */ - inline static bool IsRedstone(BLOCKTYPE Block) + inline static bool IsRedstone(BLOCKTYPE a_Block) { - switch (Block) + switch (a_Block) { // All redstone devices, please alpha sort case E_BLOCK_ACACIA_DOOR: @@ -385,16 +389,27 @@ private: } } - inline static Vector3i GetCoordinateAdjacentChunk(const Vector3i & a_BlockPos) + inline static bool DoesIgnorePlayerToggle(BLOCKTYPE a_Block) { - // Are we on a chunk boundary? +- 2 because of LinkedPowered blocks - if ((a_BlockPos.x % cChunkDef::Width) <= 1) { return{ -2, 0, 0 }; } - if ((a_BlockPos.x % cChunkDef::Width) >= 14) { return{ 2, 0, 0 }; } - if ((a_BlockPos.z % cChunkDef::Width) <= 1) { return{ 0, 0, -2 }; } - if ((a_BlockPos.z % cChunkDef::Width) >= 14) { return{ 0, 0, 2 }; } - return { 0, 0, 0 }; + switch (a_Block) + { + case E_BLOCK_ACACIA_FENCE_GATE: + case E_BLOCK_BIRCH_FENCE_GATE: + case E_BLOCK_DARK_OAK_FENCE_GATE: + case E_BLOCK_FENCE_GATE: + case E_BLOCK_JUNGLE_FENCE_GATE: + case E_BLOCK_SPRUCE_FENCE_GATE: + case E_BLOCK_IRON_TRAPDOOR: + case E_BLOCK_TRAPDOOR: + { + return true; + } + default: return false; + } } + inline static std::vector GetAdjacentChunks(const Vector3i & a_RelBlockPosition, cChunk * a_Chunk); + inline static Vector3i AdjustRelativeCoords(const Vector3i & a_RelPosition) { if ( -- cgit v1.2.3 From bef7cce5ede018a4b0bc0f03cbd7a3a87f066d4c Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 1 May 2015 22:55:43 +0100 Subject: Improved wires and repeaters * Fixed repeater cross-chunk power detection + Re-implemented horizontal wire cut-off checking --- src/Simulator/IncrementalRedstoneSimulator.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/Simulator/IncrementalRedstoneSimulator.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator.h index c383b6a0b..5133f7caf 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator.h @@ -196,6 +196,8 @@ private: /** Recursively searches for a wire path and powers everything that should be powered */ void FindAndPowerBorderingWires(std::vector> & a_PotentialWireList, const std::pair & a_Entry); + void PowerBorderingWires(std::vector> & a_PotentialWireList, const std::pair & a_Entry, const Vector3i & a_AdjustedPos, cChunk * a_NeighbourChunk, unsigned char a_MyPower); + /** Marks a block as being powered through another block */ void SetBlockLinkedPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, int a_RelMiddleX, int a_RelMiddleY, int a_RelMiddleZ, int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, BLOCKTYPE a_MiddeBlock, unsigned char a_PowerLevel = MAX_POWER_LEVEL); -- cgit v1.2.3 From 6a831c0b6fdf984f0cdf1d30995c9e602bdd41a5 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 26 May 2015 23:30:50 +0100 Subject: Improved link power behaviour --- src/Simulator/IncrementalRedstoneSimulator.h | 34 +++------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator.h index 5133f7caf..934a0eab9 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator.h @@ -214,10 +214,10 @@ private: bool QueueRepeaterPowerChange(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, NIBBLETYPE a_Meta, bool ShouldPowerOn); /** Removes a block from the Powered and LinkedPowered lists - Used for variable sources such as tripwire hooks, daylight sensors, and trapped chests + Recursively removes all blocks powered by the given one */ void SetSourceUnpowered(int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, cChunk * a_Chunk); - void SetInvalidMiddleBlock(int a_RelMiddleX, int a_RelMiddleY, int a_RelMiddleZ, cChunk * a_Chunk, bool a_IsFirstCall = true); + void SetInvalidMiddleBlock(int a_RelMiddleX, int a_RelMiddleY, int a_RelMiddleZ, cChunk * a_Chunk); /** Returns if a coordinate is powered or linked powered */ bool AreCoordsPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ) { return AreCoordsDirectlyPowered(a_RelBlockX, a_RelBlockY, a_RelBlockZ, m_Chunk) || AreCoordsLinkedPowered(a_RelBlockX, a_RelBlockY, a_RelBlockZ, m_Chunk); } @@ -414,35 +414,7 @@ private: inline static Vector3i AdjustRelativeCoords(const Vector3i & a_RelPosition) { - if ( - (a_RelPosition.x >= 0) && (a_RelPosition.x < cChunkDef::Width) && - (a_RelPosition.z >= 0) && (a_RelPosition.z < cChunkDef::Width) - ) - { - return a_RelPosition; - } - - Vector3i RelPos = a_RelPosition; - - // Request for a different chunk, calculate chunk offset: - while (RelPos.x >= cChunkDef::Width) - { - RelPos.x -= cChunkDef::Width; - } - while (RelPos.x < 0) - { - RelPos.x += cChunkDef::Width; - } - while (RelPos.z >= cChunkDef::Width) - { - RelPos.z -= cChunkDef::Width; - } - while (RelPos.z < 0) - { - RelPos.z += cChunkDef::Width; - } - - return RelPos; + return Vector3i((a_RelPosition.x % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width, a_RelPosition.y, (a_RelPosition.z % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width); } }; -- cgit v1.2.3 From 171c59a01b07b7513cf7a281bbcb141e8182d274 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 2 Jun 2015 13:29:52 +0100 Subject: Changed appropriate containers to unordered_map Thanks to @worktycho for guidance! * Potential speed improvements --- src/Simulator/IncrementalRedstoneSimulator.h | 31 +++++++--------------------- 1 file changed, 7 insertions(+), 24 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator.h index 934a0eab9..c4be70e97 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator.h @@ -3,7 +3,7 @@ #include "RedstoneSimulator.h" #include "BlockEntities/RedstonePoweredEntity.h" -#include +#include class cWorld; class cChunk; @@ -38,15 +38,6 @@ public: virtual bool IsAllowedBlock(BLOCKTYPE a_BlockType) override { return IsRedstone(a_BlockType); } virtual void WakeUp(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) override; - enum eRedstoneWireDirectionBitfieldPositions - { - eWbpXP = 0, - eWbpXN = 1, - eWbpZP = 2, - eWbpZN = 3, - }; - std::bitset<4> GetWireDirection(int a_BlockX, int a_BlockY, int a_BlockZ); - private: #define MAX_POWER_LEVEL 15 @@ -66,15 +57,8 @@ private: unsigned char a_PowerLevel; }; - struct sSimulatedPlayerToggleableList // Define structure of the list containing simulate-on-update blocks (such as trapdoors that respond once to a block update, and can be toggled by a player) - { - Vector3i a_RelBlockPos; - bool WasLastStatePowered; // Was the last state powered or not? Determines whether a source update has happened and if I should resimulate - }; - struct sRepeatersDelayList // Define structure of list containing repeaters' delay states { - Vector3i a_RelBlockPos; unsigned char a_DelayTicks; // For how many ticks should the repeater delay unsigned char a_ElapsedTicks; // How much of the previous has been elapsed? bool ShouldPowerOn; // What happens when the delay time is fulfilled? @@ -85,20 +69,19 @@ private: { public: /// Per-chunk data for the simulator, specified individual chunks to simulate - cCoordWithBlockAndBoolVector m_ChunkData; - cCoordWithBlockAndBoolVector m_QueuedChunkData; + std::unordered_map, Vector3i> m_ChunkData; std::vector m_PoweredBlocks; std::vector m_LinkedBlocks; - std::vector m_SimulatedPlayerToggleableBlocks; - std::vector m_RepeatersDelayList; + std::unordered_map m_SimulatedPlayerToggleableBlocks; + std::unordered_map m_RepeatersDelayList; }; public: typedef std::vector PoweredBlocksList; typedef std::vector LinkedBlocksList; - typedef std::vector SimulatedPlayerToggleableList; - typedef std::vector RepeatersDelayList; + typedef std::unordered_map SimulatedPlayerToggleableList; + typedef std::unordered_map RepeatersDelayList; private: @@ -211,7 +194,7 @@ private: void SetAllDirsAsPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, unsigned char a_PowerLevel = MAX_POWER_LEVEL); /** Queues a repeater to be powered or unpowered and returns if the m_RepeatersDelayList iterators were invalidated */ - bool QueueRepeaterPowerChange(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, NIBBLETYPE a_Meta, bool ShouldPowerOn); + void QueueRepeaterPowerChange(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, NIBBLETYPE a_Meta, bool ShouldPowerOn); /** Removes a block from the Powered and LinkedPowered lists Recursively removes all blocks powered by the given one -- cgit v1.2.3 From ed6c37febc3e5f6bd491317946aae11db73c2d04 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 6 Jun 2015 12:30:08 +0100 Subject: Use emplace to construct structures --- src/Simulator/IncrementalRedstoneSimulator.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator.h index c4be70e97..b0d3ad7af 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator.h @@ -44,9 +44,16 @@ private: struct sPoweredBlocks // Define structure of the directly powered blocks list { - Vector3i a_BlockPos; // Position of powered block - Vector3i a_SourcePos; // Position of source powering the block at a_BlockPos - unsigned char a_PowerLevel; + sPoweredBlocks(Vector3i a_BlockPos, Vector3i a_SourcePos, unsigned char a_PowerLevel) : + m_BlockPos(a_BlockPos), + m_SourcePos(a_SourcePos), + m_PowerLevel(a_PowerLevel) + { + } + + Vector3i m_BlockPos; // Position of powered block + Vector3i m_SourcePos; // Position of source powering the block at a_BlockPos + unsigned char m_PowerLevel; }; struct sLinkedPoweredBlocks // Define structure of the indirectly powered blocks list (i.e. repeaters powering through a block to the block at the other side) @@ -177,9 +184,14 @@ private: void SetBlockPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, unsigned char a_PowerLevel = MAX_POWER_LEVEL) { SetBlockPowered(Vector3i(a_RelBlockX, a_RelBlockY, a_RelBlockZ), Vector3i(a_RelSourceX, a_RelSourceY, a_RelSourceZ), a_PowerLevel); } /** Recursively searches for a wire path and powers everything that should be powered */ - void FindAndPowerBorderingWires(std::vector> & a_PotentialWireList, const std::pair & a_Entry); + void FindAndPowerBorderingWires(std::vector> & a_PotentialWireList, const Vector3i & a_EntryRelBlockPosition, cChunk * a_EntryChunk); - void PowerBorderingWires(std::vector> & a_PotentialWireList, const std::pair & a_Entry, const Vector3i & a_AdjustedPos, cChunk * a_NeighbourChunk, unsigned char a_MyPower); + /** Powers a specified wire block position with the specified source wire position + Checks are performed to ensure one wire does not power the same location more than once + a_EntryChunk will be the chunk which the source resides, and a_NeighbourChunk will be that which the to-be-powered wire resides + a_PotentialWireList is updated to include the new powered wire so that FindAndPowerBorderingWires can continue the redstone wire line tracing process + */ + void PowerBorderingWires(std::vector> & a_PotentialWireList, const Vector3i & a_EntryRelSourcePosition, cChunk * a_EntryChunk, const Vector3i & a_AdjustedPos, cChunk * a_NeighbourChunk, unsigned char a_MyPower); /** Marks a block as being powered through another block */ void SetBlockLinkedPowered(int a_RelBlockX, int a_RelBlockY, int a_RelBlockZ, int a_RelMiddleX, int a_RelMiddleY, int a_RelMiddleZ, int a_RelSourceX, int a_RelSourceY, int a_RelSourceZ, BLOCKTYPE a_MiddeBlock, unsigned char a_PowerLevel = MAX_POWER_LEVEL); @@ -397,7 +409,11 @@ private: inline static Vector3i AdjustRelativeCoords(const Vector3i & a_RelPosition) { - return Vector3i((a_RelPosition.x % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width, a_RelPosition.y, (a_RelPosition.z % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width); + return { + (a_RelPosition.x % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width, + a_RelPosition.y, + (a_RelPosition.z % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width + }; } }; -- cgit v1.2.3 From f44d123ba8ca14c3df90afff85a0674a4463cfa2 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 7 Jun 2015 11:52:14 +0100 Subject: Vector hasher is now a separate class --- src/Simulator/IncrementalRedstoneSimulator.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator.h index b0d3ad7af..9fbefae73 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator.h @@ -76,19 +76,21 @@ private: { public: /// Per-chunk data for the simulator, specified individual chunks to simulate - std::unordered_map, Vector3i> m_ChunkData; + + /** test */ + std::unordered_map, VectorHasher> m_ChunkData; std::vector m_PoweredBlocks; std::vector m_LinkedBlocks; - std::unordered_map m_SimulatedPlayerToggleableBlocks; - std::unordered_map m_RepeatersDelayList; + std::unordered_map> m_SimulatedPlayerToggleableBlocks; + std::unordered_map> m_RepeatersDelayList; }; public: typedef std::vector PoweredBlocksList; typedef std::vector LinkedBlocksList; - typedef std::unordered_map SimulatedPlayerToggleableList; - typedef std::unordered_map RepeatersDelayList; + typedef std::unordered_map> SimulatedPlayerToggleableList; + typedef std::unordered_map> RepeatersDelayList; private: -- cgit v1.2.3 From 061866616d44c127871e95eb8dd6c713bcd3a2c8 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 7 Jun 2015 12:03:28 +0100 Subject: Added moar comments --- src/Simulator/IncrementalRedstoneSimulator.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src/Simulator/IncrementalRedstoneSimulator.h') diff --git a/src/Simulator/IncrementalRedstoneSimulator.h b/src/Simulator/IncrementalRedstoneSimulator.h index 9fbefae73..e4250c0b9 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.h +++ b/src/Simulator/IncrementalRedstoneSimulator.h @@ -411,11 +411,7 @@ private: inline static Vector3i AdjustRelativeCoords(const Vector3i & a_RelPosition) { - return { - (a_RelPosition.x % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width, - a_RelPosition.y, - (a_RelPosition.z % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width - }; + return { (a_RelPosition.x % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width, a_RelPosition.y, (a_RelPosition.z % cChunkDef::Width + cChunkDef::Width) % cChunkDef::Width }; } }; -- cgit v1.2.3