diff options
author | Debucquoy Anthony tonitch <debucquoy.anthony@gmail.com> | 2023-09-26 23:54:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-26 23:54:37 +0200 |
commit | 7db4e20fd7ee7cf1c25d5c2c5e9e1b1cf97d4c97 (patch) | |
tree | 62ce535a26ad0637564de14ad7b75429537162a7 /src/Mobs/AggressiveMonster.cpp | |
parent | Update Core (diff) | |
download | cuberite-7db4e20fd7ee7cf1c25d5c2c5e9e1b1cf97d4c97.tar cuberite-7db4e20fd7ee7cf1c25d5c2c5e9e1b1cf97d4c97.tar.gz cuberite-7db4e20fd7ee7cf1c25d5c2c5e9e1b1cf97d4c97.tar.bz2 cuberite-7db4e20fd7ee7cf1c25d5c2c5e9e1b1cf97d4c97.tar.lz cuberite-7db4e20fd7ee7cf1c25d5c2c5e9e1b1cf97d4c97.tar.xz cuberite-7db4e20fd7ee7cf1c25d5c2c5e9e1b1cf97d4c97.tar.zst cuberite-7db4e20fd7ee7cf1c25d5c2c5e9e1b1cf97d4c97.zip |
Diffstat (limited to 'src/Mobs/AggressiveMonster.cpp')
-rw-r--r-- | src/Mobs/AggressiveMonster.cpp | 61 |
1 files changed, 58 insertions, 3 deletions
diff --git a/src/Mobs/AggressiveMonster.cpp b/src/Mobs/AggressiveMonster.cpp index f7392d92e..c93985b90 100644 --- a/src/Mobs/AggressiveMonster.cpp +++ b/src/Mobs/AggressiveMonster.cpp @@ -3,9 +3,9 @@ #include "AggressiveMonster.h" -#include "../World.h" -#include "../Entities/Player.h" -#include "../LineBlockTracer.h" +#include "LineBlockTracer.h" +#include "World.h" +#include "Entities/Player.h" @@ -46,6 +46,61 @@ void cAggressiveMonster::EventSeePlayer(cPlayer * a_Player, cChunk & a_Chunk) +cMonster * cAggressiveMonster::GetMonsterOfTypeInSight(eMonsterType a_MobType, unsigned int a_SightDistance) +{ + + cMonster * FoundTarget = nullptr; + auto MinimumDistance = static_cast<double>(a_SightDistance * a_SightDistance); + + class cCallback : public cBlockTracer::cCallbacks + { + public: + bool OnNextBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, eBlockFace a_EntryFace) override + { + return a_BlockType != E_BLOCK_AIR; + } + }; + + auto Callbacks = cCallback(); + auto Tracer = cLineBlockTracer(*GetWorld(), Callbacks); + + cEntityCallback Callback = [&](cEntity & a_Entity) + { + if (!a_Entity.IsMob()) + { + return false; + } + + auto & Other = dynamic_cast<cMonster &>(a_Entity); + if (Other.GetMobType() != a_MobType) + { + return false; + } + + Vector3d MyHeadPosition = GetPosition().addedY(GetHeight()); + Vector3d TargetPosition = Other.GetPosition().addedY(Other.GetHeight()); + double TargetDistance = (MyHeadPosition - TargetPosition).SqrLength(); + + if ( + (MinimumDistance > TargetDistance) && + (TargetDistance < (a_SightDistance * a_SightDistance)) + ) + { + FoundTarget = & Other; + return true; + } + return false; + }; + + cBoundingBox CheckZone(GetPosition().addedXZ(-a_SightDistance, -a_SightDistance), GetPosition().addedXZ(a_SightDistance, a_SightDistance)); + m_World->ForEachEntityInBox(CheckZone, Callback); + return FoundTarget; +} + + + + + void cAggressiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { Super::Tick(a_Dt, a_Chunk); |