From e4b2502896febbbf1d53fee1970e973bc6afde04 Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 2 Mar 2014 16:01:37 +0100 Subject: Add Trapdoor Functions to cWorld and fix Trapdoor Redstone Bugs --- src/Blocks/BlockTrapdoor.h | 4 ++- src/Simulator/IncrementalRedstoneSimulator.cpp | 8 +++--- src/World.cpp | 37 ++++++++++++++++++++++++++ src/World.h | 6 +++++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/Blocks/BlockTrapdoor.h b/src/Blocks/BlockTrapdoor.h index 08fc28327..251044afd 100644 --- a/src/Blocks/BlockTrapdoor.h +++ b/src/Blocks/BlockTrapdoor.h @@ -36,8 +36,10 @@ public: { // Flip the ON bit on/off using the XOR bitwise operation NIBBLETYPE Meta = (a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) ^ 0x04); - a_ChunkInterface.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, Meta); + + cWorld * World = (cWorld *) &a_WorldInterface; + World->BroadcastSoundParticleEffect(1003, a_BlockX, a_BlockY, a_BlockZ, 0, a_Player->GetClientHandle()); } virtual bool GetPlacementBlockTypeMeta( diff --git a/src/Simulator/IncrementalRedstoneSimulator.cpp b/src/Simulator/IncrementalRedstoneSimulator.cpp index 91de9e0cc..74b30f920 100644 --- a/src/Simulator/IncrementalRedstoneSimulator.cpp +++ b/src/Simulator/IncrementalRedstoneSimulator.cpp @@ -937,17 +937,15 @@ void cIncrementalRedstoneSimulator::HandleTrapdoor(int a_BlockX, int a_BlockY, i { if (!AreCoordsSimulated(a_BlockX, a_BlockY, a_BlockZ, true)) { - m_World.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) | 0x4); - m_World.BroadcastSoundParticleEffect(1003, a_BlockX, a_BlockY, a_BlockZ, 0); + m_World.SetTrapdoorOpen(a_BlockX, a_BlockY, a_BlockZ, true); SetPlayerToggleableBlockAsSimulated(a_BlockX, a_BlockY, a_BlockZ, true); - } + } } else { if (!AreCoordsSimulated(a_BlockX, a_BlockY, a_BlockZ, false)) { - m_World.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, m_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) & 0xB); // Take into account that the fourth bit is needed for trapdoors too - m_World.BroadcastSoundParticleEffect(1003, a_BlockX, a_BlockY, a_BlockZ, 0); + m_World.SetTrapdoorOpen(a_BlockX, a_BlockY, a_BlockZ, false); SetPlayerToggleableBlockAsSimulated(a_BlockX, a_BlockY, a_BlockZ, false); } } diff --git a/src/World.cpp b/src/World.cpp index ffdae2a37..ca72d2e20 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -2647,6 +2647,43 @@ bool cWorld::SetCommandBlockCommand(int a_BlockX, int a_BlockY, int a_BlockZ, co +bool cWorld::IsTrapdoorOpen(int a_BlockX, int a_BlockY, int a_BlockZ) +{ + if (GetBlock(a_BlockX, a_BlockY, a_BlockZ) != E_BLOCK_TRAPDOOR) + { + return false; + } + + NIBBLETYPE Meta = GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + return (Meta & 0x4) > 0; +} + + + + + +bool cWorld::SetTrapdoorOpen(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_Open) +{ + if (GetBlock(a_BlockX, a_BlockY, a_BlockZ) != E_BLOCK_TRAPDOOR) + { + return false; + } + + NIBBLETYPE Meta = GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + bool IsOpen = (Meta & 0x4) > 0; + if (a_Open != IsOpen) + { + SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, Meta ^ 0x4); + BroadcastSoundParticleEffect(1003, a_BlockX, a_BlockY, a_BlockZ, 0); + return true; + } + return false; +} + + + + + void cWorld::RegenerateChunk(int a_ChunkX, int a_ChunkZ) { m_ChunkMap->MarkChunkRegenerating(a_ChunkX, a_ChunkZ); diff --git a/src/World.h b/src/World.h index 4b74f7aba..ec6805dcf 100644 --- a/src/World.h +++ b/src/World.h @@ -342,6 +342,12 @@ public: /** Sets the command block command. Returns true if command changed. */ bool SetCommandBlockCommand(int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_Command); // tolua_export + /** Is the trapdoor open? */ + bool IsTrapdoorOpen(int a_BlockX, int a_BlockY, int a_BlockZ); // tolua_export + + /** Set the state of a trapdoor */ + bool SetTrapdoorOpen(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_Open); // tolua_export + /** Regenerate the given chunk: */ void RegenerateChunk(int a_ChunkX, int a_ChunkZ); // tolua_export -- cgit v1.2.3 From 5e427ee82592bd411a6b7ae0e9f1bd574ff68eb3 Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 2 Mar 2014 16:16:22 +0100 Subject: More documentation (thanks to madmaxoft) and use GetBlockTypeMeta --- src/World.cpp | 12 ++++++++---- src/World.h | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/World.cpp b/src/World.cpp index ca72d2e20..6c9ea7453 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -2649,12 +2649,14 @@ bool cWorld::SetCommandBlockCommand(int a_BlockX, int a_BlockY, int a_BlockZ, co bool cWorld::IsTrapdoorOpen(int a_BlockX, int a_BlockY, int a_BlockZ) { - if (GetBlock(a_BlockX, a_BlockY, a_BlockZ) != E_BLOCK_TRAPDOOR) + BLOCKTYPE Block; + NIBBLETYPE Meta; + GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, Block, Meta); + if (Block != E_BLOCK_TRAPDOOR) { return false; } - NIBBLETYPE Meta = GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); return (Meta & 0x4) > 0; } @@ -2664,12 +2666,14 @@ bool cWorld::IsTrapdoorOpen(int a_BlockX, int a_BlockY, int a_BlockZ) bool cWorld::SetTrapdoorOpen(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_Open) { - if (GetBlock(a_BlockX, a_BlockY, a_BlockZ) != E_BLOCK_TRAPDOOR) + BLOCKTYPE Block; + NIBBLETYPE Meta; + GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, Block, Meta); + if (Block != E_BLOCK_TRAPDOOR) { return false; } - NIBBLETYPE Meta = GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); bool IsOpen = (Meta & 0x4) > 0; if (a_Open != IsOpen) { diff --git a/src/World.h b/src/World.h index ec6805dcf..25b42888c 100644 --- a/src/World.h +++ b/src/World.h @@ -342,10 +342,10 @@ public: /** Sets the command block command. Returns true if command changed. */ bool SetCommandBlockCommand(int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_Command); // tolua_export - /** Is the trapdoor open? */ + /** Is the trapdoor open? Returns false if there is no trapdoor at the specified coords. */ bool IsTrapdoorOpen(int a_BlockX, int a_BlockY, int a_BlockZ); // tolua_export - /** Set the state of a trapdoor */ + /** Set the state of a trapdoor. Returns true if the trapdoor was update, false if there was no trapdoor at those coords. */ bool SetTrapdoorOpen(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_Open); // tolua_export /** Regenerate the given chunk: */ -- cgit v1.2.3 From 1d67345989ac1e55bd0ed18baaf80becfbf37e74 Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 2 Mar 2014 21:04:01 +0100 Subject: Add cancelling to WeatherChanging event. --- src/World.cpp | 53 ++++++++++++++++++++++++++++++++++++++++------------- src/World.h | 3 +++ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/World.cpp b/src/World.cpp index 6c9ea7453..88cc19559 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -307,25 +307,52 @@ void cWorld::CastThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) +int cWorld::GetDefaultWeatherInterval(eWeather a_Weather) +{ + switch (a_Weather) + { + case eWeather_Sunny: + { + return 14400 + (m_TickRand.randInt() % 4800); // 12 - 16 minutes + } + case eWeather_Rain: + { + return 9600 + (m_TickRand.randInt() % 7200); // 8 - 14 minutes + } + case eWeather_ThunderStorm: + { + return 2400 + (m_TickRand.randInt() % 4800); // 2 - 6 minutes + } + default: + { + LOGWARNING("Missing default weather interval for weather %d.", a_Weather); + return 1200; + } + } // switch (Weather) +} + + + + + void cWorld::SetWeather(eWeather a_NewWeather) { // Do the plugins agree? Do they want a different weather? - cRoot::Get()->GetPluginManager()->CallHookWeatherChanging(*this, a_NewWeather); + if (cRoot::Get()->GetPluginManager()->CallHookWeatherChanging(*this, a_NewWeather)) + { + m_WeatherInterval = GetDefaultWeatherInterval(m_Weather); + return; + } // Set new period for the selected weather: - switch (a_NewWeather) + m_WeatherInterval = GetDefaultWeatherInterval(a_NewWeather); + + // The weather can't be found: + if (m_WeatherInterval == 1200) { - case eWeather_Sunny: m_WeatherInterval = 14400 + (m_TickRand.randInt() % 4800); break; // 12 - 16 minutes - case eWeather_Rain: m_WeatherInterval = 9600 + (m_TickRand.randInt() % 7200); break; // 8 - 14 minutes - case eWeather_ThunderStorm: m_WeatherInterval = 2400 + (m_TickRand.randInt() % 4800); break; // 2 - 6 minutes - default: - { - LOGWARNING("Requested unknown weather %d, setting sunny for a minute instead.", a_NewWeather); - a_NewWeather = eWeather_Sunny; - m_WeatherInterval = 1200; - break; - } - } // switch (NewWeather) + return; + } + m_Weather = a_NewWeather; BroadcastWeather(m_Weather); diff --git a/src/World.h b/src/World.h index 25b42888c..27f1482e5 100644 --- a/src/World.h +++ b/src/World.h @@ -139,6 +139,9 @@ public: BroadcastTimeUpdate(); } + /** Returns the default weather interval for the specific weather type */ + int GetDefaultWeatherInterval(eWeather a_Weather); + /** Returns the current game mode. Partly OBSOLETE, you should use IsGameModeXXX() functions wherever applicable */ eGameMode GetGameMode(void) const { return m_GameMode; } -- cgit v1.2.3