diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/Chunk.cpp | 5 | ||||
-rw-r--r-- | source/ClientHandle.cpp | 2 | ||||
-rw-r--r-- | source/DeadlockDetect.cpp | 20 | ||||
-rw-r--r-- | source/DeadlockDetect.h | 5 | ||||
-rw-r--r-- | source/Entities/Player.cpp | 4 | ||||
-rw-r--r-- | source/Entities/Player.h | 7 | ||||
-rw-r--r-- | source/Piston.cpp | 34 | ||||
-rw-r--r-- | source/Server.cpp | 2 | ||||
-rw-r--r-- | source/World.cpp | 24 |
9 files changed, 58 insertions, 45 deletions
diff --git a/source/Chunk.cpp b/source/Chunk.cpp index e17e4bebc..db533f642 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -573,6 +573,11 @@ void cChunk::ProcessQueuedSetBlocks(void) void cChunk::BroadcastPendingBlockChanges(void) { + if (m_PendingSendBlocks.empty()) + { + return; + } + for (cClientHandleList::iterator itr = m_LoadedByClient.begin(), end = m_LoadedByClient.end(); itr != end; ++itr) { (*itr)->SendBlockChanges(m_PosX, m_PosZ, m_PendingSendBlocks); diff --git a/source/ClientHandle.cpp b/source/ClientHandle.cpp index 07d580085..57830f63c 100644 --- a/source/ClientHandle.cpp +++ b/source/ClientHandle.cpp @@ -1502,6 +1502,8 @@ void cClientHandle::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BL void cClientHandle::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) { + ASSERT(!a_Changes.empty()); // We don't want to be sending empty change packets! + m_Protocol->SendBlockChanges(a_ChunkX, a_ChunkZ, a_Changes); } diff --git a/source/DeadlockDetect.cpp b/source/DeadlockDetect.cpp index 960038f81..5af3f973d 100644 --- a/source/DeadlockDetect.cpp +++ b/source/DeadlockDetect.cpp @@ -13,10 +13,10 @@ /// Number of milliseconds per cycle -const int CYCLE_MILLISECONDS = 500; +const int CYCLE_MILLISECONDS = 100; /// When the number of cycles for the same world age hits this value, it is considered a deadlock -const int NUM_CYCLES_LIMIT = 40; // 40 = twenty seconds +const int NUM_CYCLES_LIMIT = 200; // 200 = twenty seconds @@ -60,20 +60,10 @@ bool cDeadlockDetect::Start(void) -void cDeadlockDetect::Stop(void) -{ - m_EvtTerminate.Set(); - super::Stop(); -} - - - - - void cDeadlockDetect::Execute(void) { - // Loop until the event is signalled - while (m_EvtTerminate.Wait(CYCLE_MILLISECONDS) == cEvent::wrTimeout) + // Loop until the signal to terminate: + while (!m_ShouldTerminate) { // Check the world ages: class cChecker : @@ -95,6 +85,8 @@ void cDeadlockDetect::Execute(void) } } Checker(this); cRoot::Get()->ForEachWorld(Checker); + + cSleep::MilliSleep(CYCLE_MILLISECONDS); } // while (should run) } diff --git a/source/DeadlockDetect.h b/source/DeadlockDetect.h index bbd76826a..2559c3fff 100644 --- a/source/DeadlockDetect.h +++ b/source/DeadlockDetect.h @@ -31,9 +31,6 @@ public: /// Starts the detection. Hides cIsThread's Start, because we need some initialization bool Start(void); - /// Stops the detection. Hides cIsThread's Stop, because we need to signal m_EvtTerminate - void Stop(void); - protected: struct sWorldAge { @@ -49,8 +46,6 @@ protected: WorldAges m_WorldAges; - cEvent m_EvtTerminate; - // cIsThread overrides: virtual void Execute(void) override; diff --git a/source/Entities/Player.cpp b/source/Entities/Player.cpp index 8ad071453..3ccb4ca1d 100644 --- a/source/Entities/Player.cpp +++ b/source/Entities/Player.cpp @@ -340,9 +340,9 @@ void cPlayer::SetFoodTickTimer(int a_FoodTickTimer) -void cPlayer::SetFoodExhaustionLevel(double a_FoodSaturationLevel) +void cPlayer::SetFoodExhaustionLevel(double a_FoodExhaustionLevel) { - m_FoodExhaustionLevel = std::max(0.0, std::min(a_FoodSaturationLevel, 4.0)); + m_FoodExhaustionLevel = std::max(0.0, std::min(a_FoodExhaustionLevel, 4.0)); } diff --git a/source/Entities/Player.h b/source/Entities/Player.h index 62595f980..5dcce8421 100644 --- a/source/Entities/Player.h +++ b/source/Entities/Player.h @@ -171,14 +171,17 @@ public: void SetFoodLevel (int a_FoodLevel); void SetFoodSaturationLevel (double a_FoodSaturationLevel); void SetFoodTickTimer (int a_FoodTickTimer); - void SetFoodExhaustionLevel (double a_FoodSaturationLevel); + void SetFoodExhaustionLevel (double a_FoodExhaustionLevel); void SetFoodPoisonedTicksRemaining(int a_FoodPoisonedTicksRemaining); /// Adds to FoodLevel and FoodSaturationLevel, returns true if any food has been consumed, false if player "full" bool Feed(int a_Food, double a_Saturation); /// Adds the specified exhaustion to m_FoodExhaustion. Expects only positive values. - void AddFoodExhaustion(double a_Exhaustion) { m_FoodExhaustionLevel += a_Exhaustion; } + void AddFoodExhaustion(double a_Exhaustion) + { + m_FoodExhaustionLevel += a_Exhaustion; + } /// Starts the food poisoning for the specified amount of ticks; if already foodpoisoned, sets FoodPoisonedTicksRemaining to the larger of the two void FoodPoison(int a_NumTicks); diff --git a/source/Piston.cpp b/source/Piston.cpp index d1d2b96a4..2431bdac2 100644 --- a/source/Piston.cpp +++ b/source/Piston.cpp @@ -20,6 +20,13 @@ extern bool g_BlockPistonBreakable[]; +/// Number of ticks that the piston extending / retracting waits before setting the block +const int PISTON_TICK_DELAY = 5; + + + + + cPiston::cPiston(cWorld * a_World) : m_World(a_World) { @@ -39,16 +46,16 @@ int cPiston::FirstPassthroughBlock(int pistonX, int pistonY, int pistonZ, NIBBLE NIBBLETYPE currMeta; AddDir(pistonX, pistonY, pistonZ, pistonmeta, 1); m_World->GetBlockTypeMeta(pistonX, pistonY, pistonZ, currBlock, currMeta); - if (!CanPush(currBlock, currMeta)) - { - // This block cannot be pushed at all, the piston can't extend - return -1; - } if (CanBreakPush(currBlock, currMeta)) { // This block breaks when pushed, extend up to here return ret; } + if (!CanPush(currBlock, currMeta)) + { + // This block cannot be pushed at all, the piston can't extend + return -1; + } } // There is no space for the blocks to move, piston can't extend return -1; @@ -98,7 +105,7 @@ void cPiston::ExtendPiston(int pistx, int pisty, int pistz) { AddDir(pistx, pisty, pistz, pistonMeta, -1); m_World->GetBlockTypeMeta(pistx, pisty, pistz, currBlock, currBlockMeta); - m_World->QueueSetBlock( oldx, oldy, oldz, currBlock, currBlockMeta, 80); + m_World->QueueSetBlock( oldx, oldy, oldz, currBlock, currBlockMeta, PISTON_TICK_DELAY); oldx = pistx; oldy = pisty; oldz = pistz; @@ -113,14 +120,14 @@ void cPiston::ExtendPiston(int pistx, int pisty, int pistz) m_World->BroadcastBlockAction(pistx, pisty, pistz, 0, pistonMeta, pistonBlock); m_World->BroadcastSoundEffect("tile.piston.out", pistx * 8, pisty * 8, pistz * 8, 0.5f, 0.7f); m_World->FastSetBlock( pistx, pisty, pistz, pistonBlock, pistonMeta | 0x8 ); - m_World->QueueSetBlock(extx, exty, extz, E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0), 80); + m_World->QueueSetBlock(extx, exty, extz, E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0), PISTON_TICK_DELAY); } -void cPiston::RetractPiston( int pistx, int pisty, int pistz ) +void cPiston::RetractPiston(int pistx, int pisty, int pistz) { BLOCKTYPE pistonBlock; NIBBLETYPE pistonMeta; @@ -133,7 +140,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), 80); + m_World->QueueSetBlock(pistx, pisty, pistz, pistonBlock, pistonMeta & ~(8), PISTON_TICK_DELAY); // Check the extension: @@ -155,18 +162,18 @@ void cPiston::RetractPiston( int pistx, int pisty, int pistz ) if (CanPull(tempBlock, tempMeta)) { // Pull the block - m_World->QueueSetBlock(pistx, pisty, pistz, tempBlock, tempMeta, 80); - m_World->QueueSetBlock(tempx, tempy, tempz, E_BLOCK_AIR, 0, 80); + m_World->QueueSetBlock(pistx, pisty, pistz, tempBlock, tempMeta, PISTON_TICK_DELAY); + m_World->QueueSetBlock(tempx, tempy, tempz, E_BLOCK_AIR, 0, PISTON_TICK_DELAY); } else { // Retract without pulling - m_World->QueueSetBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0, 80); + m_World->QueueSetBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0, PISTON_TICK_DELAY); } } else { - m_World->QueueSetBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0, 80); + m_World->QueueSetBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0, PISTON_TICK_DELAY); } } @@ -228,6 +235,7 @@ bool cPiston::CanPush(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { return false; } + case E_BLOCK_STICKY_PISTON: case E_BLOCK_PISTON: { // A piston can only be pushed if retracted: diff --git a/source/Server.cpp b/source/Server.cpp index 9c1e06c81..dd18f8d3d 100644 --- a/source/Server.cpp +++ b/source/Server.cpp @@ -75,7 +75,7 @@ void cServer::cTickThread::Execute(void) { cTimer Timer; - long long msPerTick = 50; // TODO - Put this in server config file + long long msPerTick = 50; long long LastTime = Timer.GetNowTime(); while (!m_ShouldTerminate) diff --git a/source/World.cpp b/source/World.cpp index 8f9b3924f..053eaedc7 100644 --- a/source/World.cpp +++ b/source/World.cpp @@ -12,6 +12,7 @@ #include "Root.h" #include "../iniFile/iniFile.h" #include "ChunkMap.h" +#include "OSSupport/Timer.h" // Simulators: #include "Simulator/SimulatorManager.h" @@ -206,18 +207,25 @@ cWorld::cTickThread::cTickThread(cWorld & a_World) : void cWorld::cTickThread::Execute(void) { - const int ClocksPerTick = CLOCKS_PER_SEC / 20; - clock_t LastTime = clock(); + cTimer Timer; + + long long msPerTick = 50; + long long LastTime = Timer.GetNowTime(); + while (!m_ShouldTerminate) { - clock_t Start = clock(); - m_World.Tick((float)(1000 * (Start - LastTime)) / CLOCKS_PER_SEC); - clock_t Now = clock(); - if (Now - Start < ClocksPerTick) + long long NowTime = Timer.GetNowTime(); + float DeltaTime = (float)(NowTime - LastTime); + m_World.Tick(DeltaTime); + long long TickTime = Timer.GetNowTime() - NowTime; + + if (TickTime < msPerTick) { - cSleep::MilliSleep(1000 * (ClocksPerTick - (Now - Start)) / CLOCKS_PER_SEC); + // Stretch tick time until it's at least msPerTick + cSleep::MilliSleep((unsigned int)(msPerTick - TickTime)); } - LastTime = Start; + + LastTime = NowTime; } } |