From 47c0b48bfd5df90cf889574c5634542d2aaa8873 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 18 Dec 2020 20:48:32 +0000 Subject: Monsters: improve targeting * Replace DoWithNearestPlayer with bounding box search (avoid iterating through all players in world). * Do line-of-sight checks from eye-to-eye. + Added LOS and LOS lost timer to target lost checks, in addition to distance. --- src/Mobs/AggressiveMonster.cpp | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'src/Mobs/AggressiveMonster.cpp') diff --git a/src/Mobs/AggressiveMonster.cpp b/src/Mobs/AggressiveMonster.cpp index a904ac2da..17912a090 100644 --- a/src/Mobs/AggressiveMonster.cpp +++ b/src/Mobs/AggressiveMonster.cpp @@ -38,11 +38,6 @@ void cAggressiveMonster::InStateChasing(std::chrono::milliseconds a_Dt, cChunk & void cAggressiveMonster::EventSeePlayer(cPlayer * a_Player, cChunk & a_Chunk) { - if (!a_Player->CanMobsTarget()) - { - return; - } - Super::EventSeePlayer(a_Player, a_Chunk); m_EMState = CHASING; } @@ -60,27 +55,25 @@ void cAggressiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) return; } + // Set or clear m_Target depending on rules for this Monster: if (m_EMState == CHASING) { - CheckEventLostPlayer(); + CheckEventLostPlayer(a_Dt); } else { CheckEventSeePlayer(a_Chunk); } - auto target = GetTarget(); - if (target == nullptr) - { - return; - } - - // TODO: Currently all mobs see through lava, but only Nether-native mobs should be able to. - Vector3d MyHeadPosition = GetPosition() + Vector3d(0, GetHeight(), 0); - Vector3d TargetPosition = target->GetPosition() + Vector3d(0, target->GetHeight(), 0); if ( + (GetTarget() != nullptr) && TargetIsInRange() && - cLineBlockTracer::LineOfSightTrace(*GetWorld(), MyHeadPosition, TargetPosition, cLineBlockTracer::losAirWaterLava) && + cLineBlockTracer::LineOfSightTrace( + *GetWorld(), + GetPosition().addedY(GetHeight()), + GetTarget()->GetPosition().addedY(GetTarget()->GetHeight()), + cLineBlockTracer::losAirWaterLava // TODO: Currently all mobs see through lava, but only Nether-native mobs should be able to. + ) && (GetHealth() > 0.0) ) { -- cgit v1.2.3