From ecf778bbec2794562bf5e5b8645e2171f7cd081c Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 21 Dec 2014 22:19:22 +0100 Subject: cWorld: Moved initialization into constructor. Fixes CID 71781. --- src/World.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index 69b39f831..cd17cde08 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -279,6 +279,8 @@ cWorld::cWorld(const AString & a_WorldName, eDimension a_Dimension, const AStrin m_WorldAge(0), m_TimeOfDay(0), m_LastTimeUpdate(0), + m_LastSave(0), + m_LastUnload(0), m_SkyDarkness(0), m_GameMode(gmNotSet), m_bEnabledPVP(false), @@ -620,9 +622,6 @@ void cWorld::Start(void) m_ChunkMap = make_unique(this); - m_LastSave = 0; - m_LastUnload = 0; - // preallocate some memory for ticking blocks so we don't need to allocate that often m_BlockTickQueue.reserve(1000); m_BlockTickQueueCopy.reserve(1000); -- cgit v1.2.3 From fbd0cf74bdc3a6c3a2524691da07e4ac4ad7940d Mon Sep 17 00:00:00 2001 From: Jonathan Fabian Date: Sun, 21 Dec 2014 23:02:02 -0500 Subject: Fix compile error on OS X introduced by commit ecf778bbec2794562bf5e5b8645e2171f7cd081c The following error occurs on OS X with the order reversed: `MCServer/src/World.cpp:282:2: error: field 'm_LastSave' will be initialized after field 'm_LastUnload' [-Werror,-Wreorder] m_LastSave(0),`. Reversing the order of initialization fixes this. --- src/World.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index cd17cde08..ae739a2c3 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -279,8 +279,8 @@ cWorld::cWorld(const AString & a_WorldName, eDimension a_Dimension, const AStrin m_WorldAge(0), m_TimeOfDay(0), m_LastTimeUpdate(0), - m_LastSave(0), m_LastUnload(0), + m_LastSave(0), m_SkyDarkness(0), m_GameMode(gmNotSet), m_bEnabledPVP(false), -- cgit v1.2.3 From ccdf03daaf880dd0c89a03b50c11eb083ee1cfb0 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 24 Dec 2014 07:20:17 +0100 Subject: Refactored all player block placing to go through hooks. Fixes #1618. --- src/World.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index ae739a2c3..c08b44f77 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -1463,7 +1463,7 @@ void cWorld::GrowTreeImage(const sSetBlockVector & a_Blocks) sSetBlockVector b2; for (sSetBlockVector::const_iterator itr = a_Blocks.begin(); itr != a_Blocks.end(); ++itr) { - if (itr->BlockType == E_BLOCK_LOG) + if (itr->m_BlockType == E_BLOCK_LOG) { b2.push_back(*itr); } @@ -1478,7 +1478,7 @@ void cWorld::GrowTreeImage(const sSetBlockVector & a_Blocks) // Check that at each log's coord there's an block allowed to be overwritten: for (sSetBlockVector::const_iterator itr = b2.begin(); itr != b2.end(); ++itr) { - switch (itr->BlockType) + switch (itr->m_BlockType) { CASE_TREE_ALLOWED_BLOCKS: { @@ -1926,6 +1926,15 @@ void cWorld::SpawnPrimedTNT(double a_X, double a_Y, double a_Z, int a_FuseTicks, +void cWorld::SetBlocks(const sSetBlockVector & a_Blocks) +{ + m_ChunkMap->SetBlocks(a_Blocks); +} + + + + + void cWorld::ReplaceBlocks(const sSetBlockVector & a_Blocks, BLOCKTYPE a_FilterBlockType) { m_ChunkMap->ReplaceBlocks(a_Blocks, a_FilterBlockType); -- cgit v1.2.3 From 4f75b94c99eeb1642a5ec46144542bfcd18af934 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 11 Jan 2015 01:54:18 +0000 Subject: Created new type cTickTime and rewrote cWorld::TickThread to use it --- src/World.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index c08b44f77..2dd03497b 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -233,8 +233,7 @@ cWorld::cTickThread::cTickThread(cWorld & a_World) : void cWorld::cTickThread::Execute(void) { auto LastTime = std::chrono::steady_clock::now(); - static const auto msPerTick = std::chrono::milliseconds(50); - auto TickTime = std::chrono::steady_clock::duration(50); + auto TickTime = std::chrono::duration_cast(cTickTime(1)); while (!m_ShouldTerminate) { @@ -242,12 +241,12 @@ void cWorld::cTickThread::Execute(void) auto msec = std::chrono::duration_cast(NowTime - LastTime).count(); auto LastTickMsec = std::chrono::duration_cast>(TickTime).count(); m_World.Tick(static_cast(msec), LastTickMsec); - TickTime = std::chrono::steady_clock::now() - NowTime; + TickTime = std::chrono::duration_cast(std::chrono::steady_clock::now() - NowTime); - if (TickTime < msPerTick) + if (TickTime < cTickTime(1)) { - // Stretch tick time until it's at least msPerTick - std::this_thread::sleep_for(msPerTick - TickTime); + // Stretch tick time until it's at least 1 tick + std::this_thread::sleep_for(cTickTime(1) - TickTime); } LastTime = NowTime; -- cgit v1.2.3 From 2a9664d6ca8aa9eb4f554301e4d9b0ec33b465ce Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 11 Jan 2015 21:12:26 +0000 Subject: Initial convertion of a_Dt to std::chrono also refactored cWorld::m_WorldAge and cWorld::m_TimeOfDay --- src/World.cpp | 79 ++++++++++++++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 42 deletions(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index 2dd03497b..cd99f2c2d 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -238,9 +238,8 @@ void cWorld::cTickThread::Execute(void) while (!m_ShouldTerminate) { auto NowTime = std::chrono::steady_clock::now(); - auto msec = std::chrono::duration_cast(NowTime - LastTime).count(); - auto LastTickMsec = std::chrono::duration_cast>(TickTime).count(); - m_World.Tick(static_cast(msec), LastTickMsec); + auto WaitTime = std::chrono::duration_cast(NowTime - LastTime); + m_World.Tick(WaitTime, TickTime); TickTime = std::chrono::duration_cast(std::chrono::steady_clock::now() - NowTime); if (TickTime < cTickTime(1)) @@ -273,8 +272,6 @@ cWorld::cWorld(const AString & a_WorldName, eDimension a_Dimension, const AStrin m_Dimension(a_Dimension), m_IsSpawnExplicitlySet(false), m_IsDaylightCycleEnabled(true), - m_WorldAgeSecs(0), - m_TimeOfDaySecs(0), m_WorldAge(0), m_TimeOfDay(0), m_LastTimeUpdate(0), @@ -617,7 +614,7 @@ void cWorld::Start(void) InitialiseGeneratorDefaults(IniFile); InitialiseAndLoadMobSpawningValues(IniFile); - SetTimeOfDay(IniFile.GetValueSetI("General", "TimeInTicks", m_TimeOfDay)); + SetTimeOfDay(IniFile.GetValueSetI("General", "TimeInTicks", GetTimeOfDay())); m_ChunkMap = make_unique(this); @@ -644,10 +641,10 @@ void cWorld::Start(void) m_TickThread.Start(); // Init of the spawn monster time (as they are supposed to have different spawn rate) - m_LastSpawnMonster.insert(std::map::value_type(cMonster::mfHostile, 0)); - m_LastSpawnMonster.insert(std::map::value_type(cMonster::mfPassive, 0)); - m_LastSpawnMonster.insert(std::map::value_type(cMonster::mfAmbient, 0)); - m_LastSpawnMonster.insert(std::map::value_type(cMonster::mfWater, 0)); + m_LastSpawnMonster.insert(std::map::value_type(cMonster::mfHostile, cTickTimeLong(0))); + m_LastSpawnMonster.insert(std::map::value_type(cMonster::mfPassive, cTickTimeLong(0))); + m_LastSpawnMonster.insert(std::map::value_type(cMonster::mfAmbient, cTickTimeLong(0))); + m_LastSpawnMonster.insert(std::map::value_type(cMonster::mfWater, cTickTimeLong(0))); m_MapManager.LoadMapData(); @@ -838,7 +835,7 @@ void cWorld::Stop(void) IniFile.SetValueB("Mechanics", "UseChatPrefixes", m_bUseChatPrefixes); IniFile.SetValueB("General", "IsDaylightCycleEnabled", m_IsDaylightCycleEnabled); IniFile.SetValueI("General", "Weather", (int)m_Weather); - IniFile.SetValueI("General", "TimeInTicks", m_TimeOfDay); + IniFile.SetValueI("General", "TimeInTicks", GetTimeOfDay()); IniFile.WriteFile(m_IniFileName); m_TickThread.Stop(); @@ -852,7 +849,7 @@ void cWorld::Stop(void) -void cWorld::Tick(float a_Dt, int a_LastTickDurationMSec) +void cWorld::Tick(std::chrono::milliseconds a_Dt, std::chrono::milliseconds a_LastTickDurationMSec) { // Call the plugins cPluginManager::Get()->CallHookWorldTick(*this, a_Dt, a_LastTickDurationMSec); @@ -868,30 +865,27 @@ void cWorld::Tick(float a_Dt, int a_LastTickDurationMSec) SetChunkData(**itr); } // for itr - SetChunkDataQueue[] - m_WorldAgeSecs += (double)a_Dt / 1000.0; - m_WorldAge = (Int64)(m_WorldAgeSecs * 20.0); + m_WorldAge += a_Dt; if (m_IsDaylightCycleEnabled) { // We need sub-tick precision here, that's why we store the time in seconds and calculate ticks off of it - m_TimeOfDaySecs += (double)a_Dt / 1000.0; + m_TimeOfDay += a_Dt; // Wrap time of day each 20 minutes (1200 seconds) - if (m_TimeOfDaySecs > 1200.0) + if (m_TimeOfDay > std::chrono::minutes(20)) { - m_TimeOfDaySecs -= 1200.0; + m_TimeOfDay -= std::chrono::minutes(20); } - m_TimeOfDay = static_cast(m_TimeOfDaySecs * 20.0); - // Updates the sky darkness based on current time of day UpdateSkyDarkness(); // Broadcast time update every 40 ticks (2 seconds) - if (m_LastTimeUpdate < m_WorldAge - 40) + if (m_LastTimeUpdate < m_WorldAge - cTickTime(40)) { BroadcastTimeUpdate(); - m_LastTimeUpdate = m_WorldAge; + m_LastTimeUpdate = std::chrono::duration_cast(m_WorldAge); } } @@ -911,23 +905,23 @@ void cWorld::Tick(float a_Dt, int a_LastTickDurationMSec) m_ChunkMap->Tick(a_Dt); - TickClients(a_Dt); + TickClients(a_Dt.count()); TickQueuedBlocks(); TickQueuedTasks(); TickScheduledTasks(); - GetSimulatorManager()->Simulate(a_Dt); + GetSimulatorManager()->Simulate(a_Dt.count()); - TickWeather(a_Dt); + TickWeather(a_Dt.count()); m_ChunkMap->FastSetQueuedBlocks(); - if (m_WorldAge - m_LastSave > 60 * 5 * 20) // Save each 5 minutes + if (m_WorldAge - m_LastSave > std::chrono::minutes(5)) // Save each 5 minutes { SaveAllChunks(); } - if (m_WorldAge - m_LastUnload > 10 * 20) // Unload every 10 seconds + if (m_WorldAge - m_LastUnload > std::chrono::minutes(5)) // Unload every 10 seconds { UnloadUnusedChunks(); } @@ -973,7 +967,7 @@ void cWorld::TickWeather(float a_Dt) -void cWorld::TickMobs(float a_Dt) +void cWorld::TickMobs(std::chrono::milliseconds a_Dt) { // _X 2013_10_22: This is a quick fix for #283 - the world needs to be locked while ticking mobs cWorld::cLock Lock(*this); @@ -994,7 +988,7 @@ void cWorld::TickMobs(float a_Dt) for (size_t i = 0; i < ARRAYCOUNT(AllFamilies); i++) { cMonster::eFamily Family = AllFamilies[i]; - int SpawnDelay = cMonster::GetSpawnDelay(Family); + cTickTime SpawnDelay = cTickTime(cMonster::GetSpawnDelay(Family)); if ( (m_LastSpawnMonster[Family] > m_WorldAge - SpawnDelay) || // Not reached the needed ticks before the next round MobCensus.IsCapped(Family) @@ -1002,7 +996,7 @@ void cWorld::TickMobs(float a_Dt) { continue; } - m_LastSpawnMonster[Family] = m_WorldAge; + m_LastSpawnMonster[Family] = std::chrono::duration_cast(m_WorldAge); cMobSpawner Spawner(Family, m_AllowedMobs); if (Spawner.CanSpawnAnything()) { @@ -1066,7 +1060,7 @@ void cWorld::TickScheduledTasks(void) // Move all the due tasks from m_ScheduledTasks into Tasks: for (auto itr = m_ScheduledTasks.begin(); itr != m_ScheduledTasks.end();) // Cannot use range-basd for, we're modifying the container { - if ((*itr)->m_TargetTick < WorldAge) + if ((*itr)->m_TargetTick < std::chrono::duration_cast(WorldAge).count()) { auto next = itr; ++next; @@ -1141,7 +1135,7 @@ void cWorld::TickClients(float a_Dt) void cWorld::UpdateSkyDarkness(void) { - int TempTime = (int)m_TimeOfDay; + int TempTime = std::chrono::duration_cast(m_TimeOfDay).count(); if (TempTime <= TIME_SUNSET) { m_SkyDarkness = 0; @@ -1422,14 +1416,15 @@ void cWorld::GrowTreeFromSapling(int a_X, int a_Y, int a_Z, NIBBLETYPE a_Sapling { cNoise Noise(m_Generator.GetSeed()); sSetBlockVector Logs, Other; + auto WorldAge = (int)(std::chrono::duration_cast(m_WorldAge).count() & 0xffffffff); switch (a_SaplingMeta & 0x07) { - case E_META_SAPLING_APPLE: GetAppleTreeImage (a_X, a_Y, a_Z, Noise, (int)(m_WorldAge & 0xffffffff), Logs, Other); break; - case E_META_SAPLING_BIRCH: GetBirchTreeImage (a_X, a_Y, a_Z, Noise, (int)(m_WorldAge & 0xffffffff), Logs, Other); break; - case E_META_SAPLING_CONIFER: GetConiferTreeImage(a_X, a_Y, a_Z, Noise, (int)(m_WorldAge & 0xffffffff), Logs, Other); break; - case E_META_SAPLING_JUNGLE: GetJungleTreeImage (a_X, a_Y, a_Z, Noise, (int)(m_WorldAge & 0xffffffff), Logs, Other); break; - case E_META_SAPLING_ACACIA: GetAcaciaTreeImage (a_X, a_Y, a_Z, Noise, (int)(m_WorldAge & 0xffffffff), Logs, Other); break; - case E_META_SAPLING_DARK_OAK: GetDarkoakTreeImage(a_X, a_Y, a_Z, Noise, (int)(m_WorldAge & 0xffffffff), Logs, Other); break; + case E_META_SAPLING_APPLE: GetAppleTreeImage (a_X, a_Y, a_Z, Noise, WorldAge, Logs, Other); break; + case E_META_SAPLING_BIRCH: GetBirchTreeImage (a_X, a_Y, a_Z, Noise, WorldAge, Logs, Other); break; + case E_META_SAPLING_CONIFER: GetConiferTreeImage(a_X, a_Y, a_Z, Noise, WorldAge, Logs, Other); break; + case E_META_SAPLING_JUNGLE: GetJungleTreeImage (a_X, a_Y, a_Z, Noise, WorldAge, Logs, Other); break; + case E_META_SAPLING_ACACIA: GetAcaciaTreeImage (a_X, a_Y, a_Z, Noise, WorldAge, Logs, Other); break; + case E_META_SAPLING_DARK_OAK: GetDarkoakTreeImage(a_X, a_Y, a_Z, Noise, WorldAge, Logs, Other); break; } Other.insert(Other.begin(), Logs.begin(), Logs.end()); Logs.clear(); @@ -1444,7 +1439,7 @@ void cWorld::GrowTreeByBiome(int a_X, int a_Y, int a_Z) { cNoise Noise(m_Generator.GetSeed()); sSetBlockVector Logs, Other; - GetTreeImageByBiome(a_X, a_Y, a_Z, Noise, (int)(m_WorldAge & 0xffffffff), GetBiomeAt(a_X, a_Z), Logs, Other); + GetTreeImageByBiome(a_X, a_Y, a_Z, Noise, (int)(std::chrono::duration_cast(m_WorldAge).count() & 0xffffffff), GetBiomeAt(a_X, a_Z), Logs, Other); Other.insert(Other.begin(), Logs.begin(), Logs.end()); Logs.clear(); GrowTreeImage(Other); @@ -2405,7 +2400,7 @@ void cWorld::BroadcastTimeUpdate(const cClientHandle * a_Exclude) { continue; } - ch->SendTimeUpdate(m_WorldAge, m_TimeOfDay, m_IsDaylightCycleEnabled); + ch->SendTimeUpdate(std::chrono::duration_cast(m_WorldAge).count(), std::chrono::duration_cast(m_TimeOfDay).count(), m_IsDaylightCycleEnabled); } } @@ -2607,7 +2602,7 @@ bool cWorld::HasChunkAnyClients(int a_ChunkX, int a_ChunkZ) const void cWorld::UnloadUnusedChunks(void) { - m_LastUnload = m_WorldAge; + m_LastUnload = std::chrono::duration_cast(m_WorldAge); m_ChunkMap->UnloadUnusedChunks(); } @@ -3083,7 +3078,7 @@ bool cWorld::ForEachChunkInRect(int a_MinChunkX, int a_MaxChunkX, int a_MinChunk void cWorld::SaveAllChunks(void) { - m_LastSave = m_WorldAge; + m_LastSave = std::chrono::duration_cast(m_WorldAge); m_ChunkMap->SaveAllChunks(); } @@ -3112,7 +3107,7 @@ void cWorld::QueueTask(std::unique_ptr a_Task) void cWorld::ScheduleTask(int a_DelayTicks, cTask * a_Task) { - Int64 TargetTick = a_DelayTicks + m_WorldAge; + Int64 TargetTick = a_DelayTicks + std::chrono::duration_cast(m_WorldAge).count(); // Insert the task into the list of scheduled tasks, ordered by its target tick cCSLock Lock(m_CSScheduledTasks); -- cgit v1.2.3 From 6758c1d2a15c789e232c8a3f7b51f23b66efd6e1 Mon Sep 17 00:00:00 2001 From: worktycho Date: Sat, 17 Jan 2015 22:24:25 +0000 Subject: correct comment to say milliseconds --- src/World.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index cd99f2c2d..46488d58b 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -869,7 +869,7 @@ void cWorld::Tick(std::chrono::milliseconds a_Dt, std::chrono::milliseconds a_La if (m_IsDaylightCycleEnabled) { - // We need sub-tick precision here, that's why we store the time in seconds and calculate ticks off of it + // We need sub-tick precision here, that's why we store the time in milliseconds and calculate ticks off of it m_TimeOfDay += a_Dt; // Wrap time of day each 20 minutes (1200 seconds) -- cgit v1.2.3 From 83ed6a2c1b4da2e4a3467090e56eebb2d3e48a30 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 18 Jan 2015 11:25:16 +0100 Subject: Fixed type conversion warnings. --- src/World.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index 46488d58b..eb76abc2c 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -905,14 +905,14 @@ void cWorld::Tick(std::chrono::milliseconds a_Dt, std::chrono::milliseconds a_La m_ChunkMap->Tick(a_Dt); - TickClients(a_Dt.count()); + TickClients(static_cast(a_Dt.count())); TickQueuedBlocks(); TickQueuedTasks(); TickScheduledTasks(); - GetSimulatorManager()->Simulate(a_Dt.count()); + GetSimulatorManager()->Simulate(static_cast(a_Dt.count())); - TickWeather(a_Dt.count()); + TickWeather(static_cast(a_Dt.count())); m_ChunkMap->FastSetQueuedBlocks(); -- cgit v1.2.3 From a216413a33dce89288bdf5608ec229c5d5438432 Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Fri, 23 Jan 2015 13:51:07 +0300 Subject: Fixed defect #71781 in Coverity list. --- src/World.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index eb76abc2c..24b1a9b40 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -271,6 +271,11 @@ cWorld::cWorld(const AString & a_WorldName, eDimension a_Dimension, const AStrin #endif m_Dimension(a_Dimension), m_IsSpawnExplicitlySet(false), + m_SpawnX(0), + m_SpawnY(0), + m_SpawnZ(0), + m_BroadcastDeathMessages(true), + m_BroadcastAchievementMessages(true), m_IsDaylightCycleEnabled(true), m_WorldAge(0), m_TimeOfDay(0), -- cgit v1.2.3