From 496c337cdfa593654018c171f6a74c28272265b5 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Fri, 1 Sep 2017 12:04:50 +0100 Subject: Replace ItemCallbacks with lambdas (#3948) --- src/Mobs/Creeper.cpp | 15 ++++------- src/Mobs/Enderman.cpp | 17 ++++++------- src/Mobs/Ocelot.cpp | 61 ++++++++++++++++++--------------------------- src/Mobs/PassiveMonster.cpp | 49 ++++++++++++------------------------ src/Mobs/Wither.cpp | 19 ++++---------- src/Mobs/Wolf.cpp | 45 +++++++++++++-------------------- 6 files changed, 75 insertions(+), 131 deletions(-) (limited to 'src/Mobs') diff --git a/src/Mobs/Creeper.cpp b/src/Mobs/Creeper.cpp index 84b68cf7c..adab8c5aa 100644 --- a/src/Mobs/Creeper.cpp +++ b/src/Mobs/Creeper.cpp @@ -81,21 +81,16 @@ void cCreeper::GetDrops(cItems & a_Drops, cEntity * a_Killer) a_Killer->IsProjectile() && ((reinterpret_cast(a_Killer))->GetCreatorUniqueID() != cEntity::INVALID_ID)) { - class cProjectileCreatorCallback : public cEntityCallback - { - public: - cProjectileCreatorCallback(void) {} - - virtual bool Item(cEntity * a_Entity) override + auto ProjectileCreatorCallback = [](cEntity & a_Entity) { - if (a_Entity->IsMob() && ((reinterpret_cast(a_Entity))->GetMobType() == mtSkeleton)) + if (a_Entity.IsMob() && ((static_cast(a_Entity)).GetMobType() == mtSkeleton)) { return true; } return false; - } - } PCC; - if (GetWorld()->DoWithEntityByID((reinterpret_cast(a_Killer))->GetCreatorUniqueID(), PCC)) + }; + + if (GetWorld()->DoWithEntityByID(static_cast(a_Killer)->GetCreatorUniqueID(), ProjectileCreatorCallback)) { AddRandomDropItem(a_Drops, 1, 1, static_cast(m_World->GetTickRandomNumber(11) + E_ITEM_FIRST_DISC)); } diff --git a/src/Mobs/Enderman.cpp b/src/Mobs/Enderman.cpp index 5cfe0d4cd..000496df0 100644 --- a/src/Mobs/Enderman.cpp +++ b/src/Mobs/Enderman.cpp @@ -10,8 +10,7 @@ //////////////////////////////////////////////////////////////////////////////// // cPlayerLookCheck -class cPlayerLookCheck : - public cPlayerListCallback +class cPlayerLookCheck { public: cPlayerLookCheck(Vector3d a_EndermanPos, int a_SightDistance) : @@ -21,29 +20,29 @@ public: { } - virtual bool Item(cPlayer * a_Player) override + bool operator () (cPlayer & a_Player) { // Don't check players who cannot be targeted - if (!a_Player->CanMobsTarget()) + if (!a_Player.CanMobsTarget()) { return false; } // Don't check players who are more than SightDistance (64) blocks away - auto Direction = m_EndermanPos - a_Player->GetPosition(); + auto Direction = m_EndermanPos - a_Player.GetPosition(); if (Direction.Length() > m_SightDistance) { return false; } // Don't check if the player has a pumpkin on his head - if (a_Player->GetEquippedHelmet().m_ItemType == E_BLOCK_PUMPKIN) + if (a_Player.GetEquippedHelmet().m_ItemType == E_BLOCK_PUMPKIN) { return false; } // If the player's crosshair is within 5 degrees of the enderman, it counts as looking - auto LookVector = a_Player->GetLookVector(); + auto LookVector = a_Player.GetLookVector(); auto dot = Direction.Dot(LookVector); if (dot <= cos(0.09)) // 0.09 rad ~ 5 degrees { @@ -51,13 +50,13 @@ public: } // TODO: Check if endermen are angered through water in Vanilla - if (!cLineBlockTracer::LineOfSightTrace(*a_Player->GetWorld(), m_EndermanPos, a_Player->GetPosition(), cLineBlockTracer::losAirWater)) + if (!cLineBlockTracer::LineOfSightTrace(*a_Player.GetWorld(), m_EndermanPos, a_Player.GetPosition(), cLineBlockTracer::losAirWater)) { // No direct line of sight return false; } - m_Player = a_Player; + m_Player = &a_Player; return true; } diff --git a/src/Mobs/Ocelot.cpp b/src/Mobs/Ocelot.cpp index e5004a1d1..50dd249c0 100644 --- a/src/Mobs/Ocelot.cpp +++ b/src/Mobs/Ocelot.cpp @@ -90,30 +90,25 @@ void cOcelot::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) void cOcelot::TickFollowPlayer() { - class cCallback : - public cPlayerListCallback + Vector3d OwnerPos; + bool OwnerFlying = false; + auto Callback = [&](cPlayer & a_Player) { - virtual bool Item(cPlayer * a_Player) override - { - OwnerPos = a_Player->GetPosition(); - OwnerFlying = a_Player->IsFlying(); - return true; - } - public: - Vector3d OwnerPos; - bool OwnerFlying; - } Callback; + OwnerPos = a_Player.GetPosition(); + OwnerFlying = a_Player.IsFlying(); + return true; + }; if (m_World->DoWithPlayerByUUID(m_OwnerUUID, Callback)) { // The player is present in the world, follow him: - double Distance = (Callback.OwnerPos - GetPosition()).Length(); + double Distance = (OwnerPos - GetPosition()).Length(); if (Distance > 12) { - if (!Callback.OwnerFlying) + if (!OwnerFlying) { - Callback.OwnerPos.y = FindFirstNonAirBlockPosition(Callback.OwnerPos.x, Callback.OwnerPos.z); - TeleportToCoords(Callback.OwnerPos.x, Callback.OwnerPos.y, Callback.OwnerPos.z); + OwnerPos.y = FindFirstNonAirBlockPosition(OwnerPos.x, OwnerPos.z); + TeleportToCoords(OwnerPos.x, OwnerPos.y, OwnerPos.z); } } if (Distance < 2) @@ -122,9 +117,9 @@ void cOcelot::TickFollowPlayer() } else { - if (!Callback.OwnerFlying) + if (!OwnerFlying) { - MoveToPosition(Callback.OwnerPos); + MoveToPosition(OwnerPos); } } } @@ -205,27 +200,19 @@ void cOcelot::SpawnOn(cClientHandle & a_ClientHandle) -class cFindSittingCat : - public cEntityCallback -{ - virtual bool Item(cEntity * a_Entity) override - { - return ( - (a_Entity->GetEntityType() == cEntity::etMonster) && - (static_cast(a_Entity)->GetMobType() == eMonsterType::mtOcelot) && - (static_cast(a_Entity)->IsSitting()) - ); - } -}; - - - - - bool cOcelot::IsCatSittingOnBlock(cWorld * a_World, Vector3d a_BlockPosition) { - cFindSittingCat FindSittingCat; - return a_World->ForEachEntityInBox(cBoundingBox(Vector3d(a_BlockPosition.x, a_BlockPosition.y + 1, a_BlockPosition.z), 1), FindSittingCat); + return a_World->ForEachEntityInBox( + cBoundingBox(Vector3d(a_BlockPosition.x, a_BlockPosition.y + 1, a_BlockPosition.z), 1), + [=](cEntity & a_Entity) + { + return ( + (a_Entity.GetEntityType() == cEntity::etMonster) && + (static_cast(a_Entity).GetMobType() == eMonsterType::mtOcelot) && + (static_cast(a_Entity).IsSitting()) + ); + } + ); } diff --git a/src/Mobs/PassiveMonster.cpp b/src/Mobs/PassiveMonster.cpp index a2089e13f..c9345662d 100644 --- a/src/Mobs/PassiveMonster.cpp +++ b/src/Mobs/PassiveMonster.cpp @@ -109,23 +109,18 @@ void cPassiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) Vector3f Pos = (GetPosition() + m_LovePartner->GetPosition()) * 0.5; UInt32 BabyID = m_World->SpawnMob(Pos.x, Pos.y, Pos.z, GetMobType(), true); - class cBabyInheritCallback : - public cEntityCallback - { - public: - cPassiveMonster * Baby; - cBabyInheritCallback() : Baby(nullptr) { } - virtual bool Item(cEntity * a_Entity) override + cPassiveMonster * Baby = nullptr; + + m_World->DoWithEntityByID(BabyID, [&](cEntity & a_Entity) { - Baby = static_cast(a_Entity); + Baby = static_cast(&a_Entity); return true; } - } Callback; + ); - m_World->DoWithEntityByID(BabyID, Callback); - if (Callback.Baby != nullptr) + if (Baby != nullptr) { - Callback.Baby->InheritFromParents(this, m_LovePartner); + Baby->InheritFromParents(this, m_LovePartner); } m_World->SpawnExperienceOrb(Pos.x, Pos.y, Pos.z, GetRandomProvider().RandInt(1, 6)); @@ -159,49 +154,37 @@ void cPassiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { if (m_LovePartner == nullptr) { - class LookForLover : public cEntityCallback - { - public: - cEntity * m_Me; - - LookForLover(cEntity * a_Me) : - m_Me(a_Me) - { - } - - virtual bool Item(cEntity * a_Entity) override + m_World->ForEachEntityInBox(cBoundingBox(GetPosition(), 8, 8), [=](cEntity & a_Entity) { // If the entity is not a monster, don't breed with it // Also, do not self-breed - if ((a_Entity->GetEntityType() != etMonster) || (a_Entity == m_Me)) + if ((a_Entity.GetEntityType() != etMonster) || (&a_Entity == this)) { return false; } - cPassiveMonster * Me = static_cast(m_Me); - cPassiveMonster * PotentialPartner = static_cast(a_Entity); + auto & Me = static_cast(*this); + auto & PotentialPartner = static_cast(a_Entity); // If the potential partner is not of the same species, don't breed with it - if (PotentialPartner->GetMobType() != Me->GetMobType()) + if (PotentialPartner.GetMobType() != Me.GetMobType()) { return false; } // If the potential partner is not in love // Or they already have a mate, do not breed with them - if ((!PotentialPartner->IsInLove()) || (PotentialPartner->GetPartner() != nullptr)) + if ((!PotentialPartner.IsInLove()) || (PotentialPartner.GetPartner() != nullptr)) { return false; } // All conditions met, let's breed! - PotentialPartner->EngageLoveMode(Me); - Me->EngageLoveMode(PotentialPartner); + PotentialPartner.EngageLoveMode(&Me); + Me.EngageLoveMode(&PotentialPartner); return true; } - } Callback(this); - - m_World->ForEachEntityInBox(cBoundingBox(GetPosition(), 8, 8, -4), Callback); + ); } m_LoveTimer--; diff --git a/src/Mobs/Wither.cpp b/src/Mobs/Wither.cpp index dd85d7d2b..2cf564fca 100644 --- a/src/Mobs/Wither.cpp +++ b/src/Mobs/Wither.cpp @@ -101,28 +101,19 @@ void cWither::KilledBy(TakeDamageInfo & a_TDI) { super::KilledBy(a_TDI); - class cPlayerCallback : public cPlayerListCallback - { - Vector3f m_Pos; - - virtual bool Item(cPlayer * a_Player) + Vector3d Pos = GetPosition(); + m_World->ForEachPlayer([=](cPlayer & a_Player) { // TODO 2014-05-21 xdot: Vanilla minecraft uses an AABB check instead of a radius one - double Dist = (a_Player->GetPosition() - m_Pos).Length(); + double Dist = (a_Player.GetPosition() - Pos).Length(); if (Dist < 50.0) { // If player is close, award achievement - a_Player->AwardAchievement(achKillWither); + a_Player.AwardAchievement(achKillWither); } return false; } - - public: - cPlayerCallback(const Vector3f & a_Pos) : m_Pos(a_Pos) {} - - } PlayerCallback(GetPosition()); - - m_World->ForEachPlayer(PlayerCallback); + ); } diff --git a/src/Mobs/Wolf.cpp b/src/Mobs/Wolf.cpp index f3b859c76..45df3dd05 100644 --- a/src/Mobs/Wolf.cpp +++ b/src/Mobs/Wolf.cpp @@ -80,19 +80,13 @@ void cWolf::NotifyAlliesOfFight(cPawn * a_Opponent) return; } m_NotificationCooldown = 15; - class cCallback : public cPlayerListCallback - { - virtual bool Item(cPlayer * a_Player) override + + m_World->DoWithPlayerByUUID(m_OwnerUUID, [=](cPlayer & a_Player) { - a_Player->NotifyNearbyWolves(m_Opponent, false); + a_Player.NotifyNearbyWolves(a_Opponent, false); return false; } - public: - cPawn * m_Opponent; - } Callback; - - Callback.m_Opponent = a_Opponent; - m_World->DoWithPlayerByUUID(m_OwnerUUID, Callback); + ); } bool cWolf::Attack(std::chrono::milliseconds a_Dt) @@ -347,30 +341,25 @@ void cWolf::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) void cWolf::TickFollowPlayer() { - class cCallback : - public cPlayerListCallback + Vector3d OwnerPos; + bool OwnerFlying; + auto Callback = [&](cPlayer & a_Player) { - virtual bool Item(cPlayer * a_Player) override - { - OwnerPos = a_Player->GetPosition(); - OwnerFlying = a_Player->IsFlying(); - return true; - } - public: - Vector3d OwnerPos; - bool OwnerFlying; - } Callback; + OwnerPos = a_Player.GetPosition(); + OwnerFlying = a_Player.IsFlying(); + return true; + }; if (m_World->DoWithPlayerByUUID(m_OwnerUUID, Callback)) { // The player is present in the world, follow him: - double Distance = (Callback.OwnerPos - GetPosition()).Length(); + double Distance = (OwnerPos - GetPosition()).Length(); if (Distance > 20) { - if (!Callback.OwnerFlying) + if (!OwnerFlying) { - Callback.OwnerPos.y = FindFirstNonAirBlockPosition(Callback.OwnerPos.x, Callback.OwnerPos.z); - TeleportToCoords(Callback.OwnerPos.x, Callback.OwnerPos.y, Callback.OwnerPos.z); + OwnerPos.y = FindFirstNonAirBlockPosition(OwnerPos.x, OwnerPos.z); + TeleportToCoords(OwnerPos.x, OwnerPos.y, OwnerPos.z); SetTarget(nullptr); } } @@ -385,9 +374,9 @@ void cWolf::TickFollowPlayer() { if (GetTarget() == nullptr) { - if (!Callback.OwnerFlying) + if (!OwnerFlying) { - MoveToPosition(Callback.OwnerPos); + MoveToPosition(OwnerPos); } } } -- cgit v1.2.3