diff options
author | peterbell10 <peterbell10@live.co.uk> | 2017-09-11 23:20:49 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2017-09-11 23:20:49 +0200 |
commit | e225b7f8262df48ad4d7094bc295add3007b0649 (patch) | |
tree | a42e9afcc88cfe6e9d1258458e3ad42764083d0e /src/World.cpp | |
parent | cBlockArea: change MakeIndex to return size_t (diff) | |
download | cuberite-e225b7f8262df48ad4d7094bc295add3007b0649.tar cuberite-e225b7f8262df48ad4d7094bc295add3007b0649.tar.gz cuberite-e225b7f8262df48ad4d7094bc295add3007b0649.tar.bz2 cuberite-e225b7f8262df48ad4d7094bc295add3007b0649.tar.lz cuberite-e225b7f8262df48ad4d7094bc295add3007b0649.tar.xz cuberite-e225b7f8262df48ad4d7094bc295add3007b0649.tar.zst cuberite-e225b7f8262df48ad4d7094bc295add3007b0649.zip |
Diffstat (limited to '')
-rw-r--r-- | src/World.cpp | 192 |
1 files changed, 61 insertions, 131 deletions
diff --git a/src/World.cpp b/src/World.cpp index e2ac24e71..086596eb3 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -1,4 +1,4 @@ - + #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "World.h" @@ -1122,59 +1122,52 @@ void cWorld::TickMobs(std::chrono::milliseconds a_Dt) { m_ChunkMap->SpawnMobs(Spawner); // do the spawn - for (cMobSpawner::tSpawnedContainer::const_iterator itr2 = Spawner.getSpawned().begin(); itr2 != Spawner.getSpawned().end(); ++itr2) + for (auto & Mob : Spawner.getSpawned()) { - SpawnMobFinalize(std::move(const_cast<std::unique_ptr<cMonster> &>(*itr2))); + SpawnMobFinalize(std::move(Mob)); } } } // for i - AllFamilies[] } // if (Spawning enabled) - class cCallback : public cEntityCallback - { - virtual bool Item(cEntity * a_Entity) override + ForEachEntity([=](cEntity & a_Entity) { - if (!a_Entity->IsMob()) + if (!a_Entity.IsMob()) { return false; } - if (!a_Entity->IsTicking()) + if (!a_Entity.IsTicking()) { return false; } - auto Monster = static_cast<cMonster *>(a_Entity); - ASSERT(Monster->GetParentChunk() != nullptr); // A ticking entity must have a valid parent chunk + auto & Monster = static_cast<cMonster &>(a_Entity); + ASSERT(Monster.GetParentChunk() != nullptr); // A ticking entity must have a valid parent chunk // Tick close mobs - if (Monster->GetParentChunk()->HasAnyClients()) + if (Monster.GetParentChunk()->HasAnyClients()) { - Monster->Tick(m_Dt, *(a_Entity->GetParentChunk())); + Monster.Tick(a_Dt, *(a_Entity.GetParentChunk())); } // Destroy far hostile mobs except if last target was a player - else if ((Monster->GetMobFamily() == cMonster::eFamily::mfHostile) && !Monster->WasLastTargetAPlayer()) + else if ((Monster.GetMobFamily() == cMonster::eFamily::mfHostile) && !Monster.WasLastTargetAPlayer()) { - if (Monster->GetMobType() != eMonsterType::mtWolf) + if (Monster.GetMobType() != eMonsterType::mtWolf) { - Monster->Destroy(true); + Monster.Destroy(true); } else { - auto Wolf = static_cast<cWolf *>(Monster); - if (Wolf->IsAngry()) + auto & Wolf = static_cast<cWolf &>(Monster); + if (Wolf.IsAngry()) { - Monster->Destroy(true); + Monster.Destroy(true); } } } return false; } - public: - std::chrono::milliseconds m_Dt; - } Callback; - - Callback.m_Dt = a_Dt; - ForEachEntity(Callback); + ); } @@ -1326,7 +1319,7 @@ void cWorld::WakeUpSimulatorsInArea(const cCuboid & a_Area) -bool cWorld::ForEachBlockEntityInChunk(int a_ChunkX, int a_ChunkZ, cBlockEntityCallback & a_Callback) +bool cWorld::ForEachBlockEntityInChunk(int a_ChunkX, int a_ChunkZ, cBlockEntityCallback a_Callback) { return m_ChunkMap->ForEachBlockEntityInChunk(a_ChunkX, a_ChunkZ, a_Callback); } @@ -1335,7 +1328,7 @@ bool cWorld::ForEachBlockEntityInChunk(int a_ChunkX, int a_ChunkZ, cBlockEntityC -bool cWorld::ForEachBrewingstandInChunk(int a_ChunkX, int a_ChunkZ, cBrewingstandCallback & a_Callback) +bool cWorld::ForEachBrewingstandInChunk(int a_ChunkX, int a_ChunkZ, cBrewingstandCallback a_Callback) { return m_ChunkMap->ForEachBrewingstandInChunk(a_ChunkX, a_ChunkZ, a_Callback); } @@ -1344,7 +1337,7 @@ bool cWorld::ForEachBrewingstandInChunk(int a_ChunkX, int a_ChunkZ, cBrewingstan -bool cWorld::ForEachChestInChunk(int a_ChunkX, int a_ChunkZ, cChestCallback & a_Callback) +bool cWorld::ForEachChestInChunk(int a_ChunkX, int a_ChunkZ, cChestCallback a_Callback) { return m_ChunkMap->ForEachChestInChunk(a_ChunkX, a_ChunkZ, a_Callback); } @@ -1353,7 +1346,7 @@ bool cWorld::ForEachChestInChunk(int a_ChunkX, int a_ChunkZ, cChestCallback & a_ -bool cWorld::ForEachDispenserInChunk(int a_ChunkX, int a_ChunkZ, cDispenserCallback & a_Callback) +bool cWorld::ForEachDispenserInChunk(int a_ChunkX, int a_ChunkZ, cDispenserCallback a_Callback) { return m_ChunkMap->ForEachDispenserInChunk(a_ChunkX, a_ChunkZ, a_Callback); } @@ -1362,7 +1355,7 @@ bool cWorld::ForEachDispenserInChunk(int a_ChunkX, int a_ChunkZ, cDispenserCallb -bool cWorld::ForEachDropperInChunk(int a_ChunkX, int a_ChunkZ, cDropperCallback & a_Callback) +bool cWorld::ForEachDropperInChunk(int a_ChunkX, int a_ChunkZ, cDropperCallback a_Callback) { return m_ChunkMap->ForEachDropperInChunk(a_ChunkX, a_ChunkZ, a_Callback); } @@ -1371,7 +1364,7 @@ bool cWorld::ForEachDropperInChunk(int a_ChunkX, int a_ChunkZ, cDropperCallback -bool cWorld::ForEachDropSpenserInChunk(int a_ChunkX, int a_ChunkZ, cDropSpenserCallback & a_Callback) +bool cWorld::ForEachDropSpenserInChunk(int a_ChunkX, int a_ChunkZ, cDropSpenserCallback a_Callback) { return m_ChunkMap->ForEachDropSpenserInChunk(a_ChunkX, a_ChunkZ, a_Callback); } @@ -1380,7 +1373,7 @@ bool cWorld::ForEachDropSpenserInChunk(int a_ChunkX, int a_ChunkZ, cDropSpenserC -bool cWorld::ForEachFurnaceInChunk(int a_ChunkX, int a_ChunkZ, cFurnaceCallback & a_Callback) +bool cWorld::ForEachFurnaceInChunk(int a_ChunkX, int a_ChunkZ, cFurnaceCallback a_Callback) { return m_ChunkMap->ForEachFurnaceInChunk(a_ChunkX, a_ChunkZ, a_Callback); } @@ -1435,7 +1428,7 @@ void cWorld::DoExplosionAt(double a_ExplosionSize, double a_BlockX, double a_Blo -bool cWorld::DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBlockEntityCallback & a_Callback) +bool cWorld::DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBlockEntityCallback a_Callback) { return m_ChunkMap->DoWithBlockEntityAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1444,7 +1437,7 @@ bool cWorld::DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBloc -bool cWorld::DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback & a_Callback) +bool cWorld::DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback a_Callback) { return m_ChunkMap->DoWithBeaconAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1453,7 +1446,7 @@ bool cWorld::DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCal -bool cWorld::DoWithBedAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBedCallback & a_Callback) +bool cWorld::DoWithBedAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBedCallback a_Callback) { return m_ChunkMap->DoWithBedAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1462,7 +1455,7 @@ bool cWorld::DoWithBedAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBedCallback -bool cWorld::DoWithBrewingstandAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBrewingstandCallback & a_Callback) +bool cWorld::DoWithBrewingstandAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBrewingstandCallback a_Callback) { return m_ChunkMap->DoWithBrewingstandAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1471,7 +1464,7 @@ bool cWorld::DoWithBrewingstandAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBre -bool cWorld::DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallback & a_Callback) +bool cWorld::DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallback a_Callback) { return m_ChunkMap->DoWithChestAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1480,7 +1473,7 @@ bool cWorld::DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallb -bool cWorld::DoWithDispenserAt(int a_BlockX, int a_BlockY, int a_BlockZ, cDispenserCallback & a_Callback) +bool cWorld::DoWithDispenserAt(int a_BlockX, int a_BlockY, int a_BlockZ, cDispenserCallback a_Callback) { return m_ChunkMap->DoWithDispenserAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1489,7 +1482,7 @@ bool cWorld::DoWithDispenserAt(int a_BlockX, int a_BlockY, int a_BlockZ, cDispen -bool cWorld::DoWithDropperAt(int a_BlockX, int a_BlockY, int a_BlockZ, cDropperCallback & a_Callback) +bool cWorld::DoWithDropperAt(int a_BlockX, int a_BlockY, int a_BlockZ, cDropperCallback a_Callback) { return m_ChunkMap->DoWithDropperAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1498,7 +1491,7 @@ bool cWorld::DoWithDropperAt(int a_BlockX, int a_BlockY, int a_BlockZ, cDropperC -bool cWorld::DoWithDropSpenserAt(int a_BlockX, int a_BlockY, int a_BlockZ, cDropSpenserCallback & a_Callback) +bool cWorld::DoWithDropSpenserAt(int a_BlockX, int a_BlockY, int a_BlockZ, cDropSpenserCallback a_Callback) { return m_ChunkMap->DoWithDropSpenserAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1507,7 +1500,7 @@ bool cWorld::DoWithDropSpenserAt(int a_BlockX, int a_BlockY, int a_BlockZ, cDrop -bool cWorld::DoWithFurnaceAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFurnaceCallback & a_Callback) +bool cWorld::DoWithFurnaceAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFurnaceCallback a_Callback) { return m_ChunkMap->DoWithFurnaceAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1516,7 +1509,7 @@ bool cWorld::DoWithFurnaceAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFurnaceC -bool cWorld::DoWithNoteBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cNoteBlockCallback & a_Callback) +bool cWorld::DoWithNoteBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cNoteBlockCallback a_Callback) { return m_ChunkMap->DoWithNoteBlockAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1525,7 +1518,7 @@ bool cWorld::DoWithNoteBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cNoteBl -bool cWorld::DoWithCommandBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cCommandBlockCallback & a_Callback) +bool cWorld::DoWithCommandBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cCommandBlockCallback a_Callback) { return m_ChunkMap->DoWithCommandBlockAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1534,7 +1527,7 @@ bool cWorld::DoWithCommandBlockAt(int a_BlockX, int a_BlockY, int a_BlockZ, cCom -bool cWorld::DoWithMobHeadAt(int a_BlockX, int a_BlockY, int a_BlockZ, cMobHeadCallback & a_Callback) +bool cWorld::DoWithMobHeadAt(int a_BlockX, int a_BlockY, int a_BlockZ, cMobHeadCallback a_Callback) { return m_ChunkMap->DoWithMobHeadAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1543,7 +1536,7 @@ bool cWorld::DoWithMobHeadAt(int a_BlockX, int a_BlockY, int a_BlockZ, cMobHeadC -bool cWorld::DoWithFlowerPotAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFlowerPotCallback & a_Callback) +bool cWorld::DoWithFlowerPotAt(int a_BlockX, int a_BlockY, int a_BlockZ, cFlowerPotCallback a_Callback) { return m_ChunkMap->DoWithFlowerPotAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); } @@ -1561,7 +1554,7 @@ bool cWorld::GetSignLines(int a_BlockX, int a_BlockY, int a_BlockZ, AString & a_ -bool cWorld::DoWithChunk(int a_ChunkX, int a_ChunkZ, cChunkCallback & a_Callback) +bool cWorld::DoWithChunk(int a_ChunkX, int a_ChunkZ, cChunkCallback a_Callback) { return m_ChunkMap->DoWithChunk(a_ChunkX, a_ChunkZ, a_Callback); } @@ -1570,31 +1563,7 @@ bool cWorld::DoWithChunk(int a_ChunkX, int a_ChunkZ, cChunkCallback & a_Callback -bool cWorld::DoWithChunk(int a_ChunkX, int a_ChunkZ, std::function<bool(cChunk &)> a_Callback) -{ - struct cCallBackWrapper : cChunkCallback - { - cCallBackWrapper(std::function<bool(cChunk &)> a_InnerCallback) : - m_Callback(a_InnerCallback) - { - } - - virtual bool Item(cChunk * a_Chunk) - { - return m_Callback(*a_Chunk); - } - - private: - std::function<bool(cChunk &)> m_Callback; - } callback(a_Callback); - return m_ChunkMap->DoWithChunk(a_ChunkX, a_ChunkZ, callback); -} - - - - - -bool cWorld::DoWithChunkAt(Vector3i a_BlockPos, std::function<bool(cChunk &)> a_Callback) +bool cWorld::DoWithChunkAt(Vector3i a_BlockPos, cChunkCallback a_Callback) { return m_ChunkMap->DoWithChunkAt(a_BlockPos, a_Callback); } @@ -3170,18 +3139,13 @@ bool cWorld::IsPlayerReferencedInWorldOrChunk(cPlayer & a_Player) -bool cWorld::ForEachPlayer(cPlayerListCallback & a_Callback) +bool cWorld::ForEachPlayer(cPlayerListCallback a_Callback) { // Calls the callback for each player in the list cCSLock Lock(m_CSPlayers); - for (cPlayerList::iterator itr = m_Players.begin(), itr2 = itr; itr != m_Players.end(); itr = itr2) + for (auto & Player : m_Players) { - ++itr2; - if (!(*itr)->IsTicking()) - { - continue; - } - if (a_Callback.Item(*itr)) + if (Player->IsTicking() && a_Callback(*Player)) { return false; } @@ -3193,19 +3157,15 @@ bool cWorld::ForEachPlayer(cPlayerListCallback & a_Callback) -bool cWorld::DoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback) +bool cWorld::DoWithPlayer(const AString & a_PlayerName, cPlayerListCallback a_Callback) { // Calls the callback for the specified player in the list cCSLock Lock(m_CSPlayers); - for (cPlayerList::iterator itr = m_Players.begin(); itr != m_Players.end(); ++itr) + for (auto & Player : m_Players) { - if (!(*itr)->IsTicking()) - { - continue; - } - if (NoCaseCompare((*itr)->GetName(), a_PlayerName) == 0) + if (Player->IsTicking() && (NoCaseCompare(Player->GetName(), a_PlayerName) == 0)) { - a_Callback.Item(*itr); + a_Callback(*Player); return true; } } // for itr - m_Players[] @@ -3216,7 +3176,7 @@ bool cWorld::DoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_ -bool cWorld::FindAndDoWithPlayer(const AString & a_PlayerNameHint, cPlayerListCallback & a_Callback) +bool cWorld::FindAndDoWithPlayer(const AString & a_PlayerNameHint, cPlayerListCallback a_Callback) { cPlayer * BestMatch = nullptr; size_t BestRating = 0; @@ -3243,7 +3203,7 @@ bool cWorld::FindAndDoWithPlayer(const AString & a_PlayerNameHint, cPlayerListCa if (BestMatch != nullptr) { - return a_Callback.Item (BestMatch); + return a_Callback(*BestMatch); } return false; } @@ -3252,27 +3212,14 @@ bool cWorld::FindAndDoWithPlayer(const AString & a_PlayerNameHint, cPlayerListCa -bool cWorld::DoWithPlayerByUUID(const cUUID & a_PlayerUUID, cPlayerListCallback & a_Callback) -{ - return DoWithPlayerByUUID(a_PlayerUUID, std::bind(&cPlayerListCallback::Item, &a_Callback, std::placeholders::_1)); -} - - - - - -bool cWorld::DoWithPlayerByUUID(const cUUID & a_PlayerUUID, cLambdaPlayerCallback a_Callback) +bool cWorld::DoWithPlayerByUUID(const cUUID & a_PlayerUUID, cPlayerListCallback a_Callback) { cCSLock Lock(m_CSPlayers); - for (cPlayerList::iterator itr = m_Players.begin(); itr != m_Players.end(); ++itr) + for (auto & Player : m_Players) { - if (!(*itr)->IsTicking()) + if (Player->IsTicking() && (Player->GetUUID() == a_PlayerUUID)) { - continue; - } - if ((*itr)->GetUUID() == a_PlayerUUID) - { - return a_Callback(*itr); + return a_Callback(*Player); } } return false; @@ -3340,7 +3287,7 @@ void cWorld::SendPlayerList(cPlayer * a_DestPlayer) -bool cWorld::ForEachEntity(cEntityCallback & a_Callback) +bool cWorld::ForEachEntity(cEntityCallback a_Callback) { return m_ChunkMap->ForEachEntity(a_Callback); } @@ -3349,7 +3296,7 @@ bool cWorld::ForEachEntity(cEntityCallback & a_Callback) -bool cWorld::ForEachEntityInChunk(int a_ChunkX, int a_ChunkZ, cEntityCallback & a_Callback) +bool cWorld::ForEachEntityInChunk(int a_ChunkX, int a_ChunkZ, cEntityCallback a_Callback) { return m_ChunkMap->ForEachEntityInChunk(a_ChunkX, a_ChunkZ, a_Callback); } @@ -3358,7 +3305,7 @@ bool cWorld::ForEachEntityInChunk(int a_ChunkX, int a_ChunkZ, cEntityCallback & -bool cWorld::ForEachEntityInBox(const cBoundingBox & a_Box, cEntityCallback & a_Callback) +bool cWorld::ForEachEntityInBox(const cBoundingBox & a_Box, cEntityCallback a_Callback) { return m_ChunkMap->ForEachEntityInBox(a_Box, a_Callback); } @@ -3367,16 +3314,7 @@ bool cWorld::ForEachEntityInBox(const cBoundingBox & a_Box, cEntityCallback & a_ -bool cWorld::DoWithEntityByID(UInt32 a_UniqueID, cEntityCallback & a_Callback) -{ - return DoWithEntityByID(a_UniqueID, std::bind(&cEntityCallback::Item, &a_Callback, std::placeholders::_1)); -} - - - - - -bool cWorld::DoWithEntityByID(UInt32 a_UniqueID, cLambdaEntityCallback a_Callback) +bool cWorld::DoWithEntityByID(UInt32 a_UniqueID, cEntityCallback a_Callback) { // First check the entities-to-add: { @@ -3385,7 +3323,7 @@ bool cWorld::DoWithEntityByID(UInt32 a_UniqueID, cLambdaEntityCallback a_Callbac { if (ent->GetUniqueID() == a_UniqueID) { - a_Callback(ent.get()); + a_Callback(*ent); return true; } } // for ent - m_EntitiesToAdd[] @@ -3517,20 +3455,12 @@ bool cWorld::SetSignLines(int a_BlockX, int a_BlockY, int a_BlockZ, const AStrin bool cWorld::SetCommandBlockCommand(int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_Command) { - class cUpdateCommandBlock : public cCommandBlockCallback - { - AString m_Command; - public: - cUpdateCommandBlock(const AString & a_CallbackCommand) : m_Command(a_CallbackCommand) {} - - virtual bool Item(cCommandBlockEntity * a_CommandBlock) override + return DoWithCommandBlockAt(a_BlockX, a_BlockY, a_BlockZ, [&](cCommandBlockEntity & a_CommandBlock) { - a_CommandBlock->SetCommand(m_Command); + a_CommandBlock.SetCommand(a_Command); return false; } - } CmdBlockCB (a_Command); - - return DoWithCommandBlockAt(a_BlockX, a_BlockY, a_BlockZ, CmdBlockCB); + ); } @@ -3625,7 +3555,7 @@ bool cWorld::ForEachChunkInRect(int a_MinChunkX, int a_MaxChunkX, int a_MinChunk -bool cWorld::ForEachLoadedChunk(std::function<bool(int, int)> a_Callback) +bool cWorld::ForEachLoadedChunk(cFunctionRef<bool(int, int)> a_Callback) { return m_ChunkMap->ForEachLoadedChunk(a_Callback); } |