2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2021-01-04 21:13:02 -05:00
|
|
|
#include "Chunk.h"
|
2012-09-23 16:53:08 -04:00
|
|
|
#include "Enderman.h"
|
2014-05-29 07:34:38 -04:00
|
|
|
#include "../Entities/Player.h"
|
2017-05-11 08:34:36 -04:00
|
|
|
#include "../LineBlockTracer.h"
|
2014-05-29 07:34:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-04 06:03:37 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-05-29 07:34:38 -04:00
|
|
|
// cPlayerLookCheck
|
2017-09-11 17:20:49 -04:00
|
|
|
class cPlayerLookCheck
|
2014-05-29 07:34:38 -04:00
|
|
|
{
|
|
|
|
public:
|
2020-12-18 15:49:36 -05:00
|
|
|
|
|
|
|
cPlayerLookCheck(Vector3d a_EndermanHeadPosition, int a_SightDistance) :
|
2014-10-20 16:55:07 -04:00
|
|
|
m_Player(nullptr),
|
2020-12-18 15:49:36 -05:00
|
|
|
m_EndermanHeadPosition(a_EndermanHeadPosition),
|
2014-08-01 17:05:40 -04:00
|
|
|
m_SightDistance(a_SightDistance)
|
2014-05-29 07:34:38 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
bool operator () (cPlayer & a_Player)
|
2014-05-29 07:34:38 -04:00
|
|
|
{
|
2016-10-12 08:38:45 -04:00
|
|
|
// Don't check players who cannot be targeted
|
2017-09-11 17:20:49 -04:00
|
|
|
if (!a_Player.CanMobsTarget())
|
2014-05-29 07:34:38 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2020-12-18 15:49:36 -05:00
|
|
|
const auto PlayerHeadPosition = a_Player.GetPosition().addedY(a_Player.GetHeight());
|
|
|
|
const auto Direction = m_EndermanHeadPosition - PlayerHeadPosition;
|
|
|
|
|
|
|
|
// Don't check players who are more than SightDistance (64) blocks away:
|
2014-08-01 17:05:40 -04:00
|
|
|
if (Direction.Length() > m_SightDistance)
|
2014-05-29 07:34:38 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2020-12-18 15:49:36 -05:00
|
|
|
// Don't check if the player has a pumpkin on his head:
|
2017-09-11 17:20:49 -04:00
|
|
|
if (a_Player.GetEquippedHelmet().m_ItemType == E_BLOCK_PUMPKIN)
|
2014-05-29 07:34:38 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:49:36 -05:00
|
|
|
const auto LookVector = a_Player.GetLookVector(); // Note: ||LookVector|| is always 1.
|
|
|
|
const auto Cosine = Direction.Dot(LookVector) / Direction.Length(); // a.b / (||a|| * ||b||)
|
|
|
|
|
|
|
|
// If the player's crosshair is within 5 degrees of the enderman, it counts as looking:
|
|
|
|
if ((Cosine < std::cos(0.09)) || (Cosine > std::cos(0))) // 0.09 rad ~ 5 degrees
|
2014-05-29 07:34:38 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2017-05-11 08:34:36 -04:00
|
|
|
// TODO: Check if endermen are angered through water in Vanilla
|
2020-12-18 15:49:36 -05:00
|
|
|
if (!cLineBlockTracer::LineOfSightTrace(*a_Player.GetWorld(), m_EndermanHeadPosition, PlayerHeadPosition, cLineBlockTracer::losAirWater))
|
2014-06-04 04:19:55 -04:00
|
|
|
{
|
|
|
|
// No direct line of sight
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
m_Player = &a_Player;
|
2014-05-29 07:34:38 -04:00
|
|
|
return true;
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2014-06-04 04:19:55 -04:00
|
|
|
cPlayer * GetPlayer(void) const { return m_Player; }
|
|
|
|
|
2014-05-29 07:34:38 -04:00
|
|
|
protected:
|
2020-12-18 15:49:36 -05:00
|
|
|
|
2014-05-29 07:34:38 -04:00
|
|
|
cPlayer * m_Player;
|
2020-12-18 15:49:36 -05:00
|
|
|
Vector3d m_EndermanHeadPosition;
|
2014-08-01 17:05:40 -04:00
|
|
|
int m_SightDistance;
|
2014-05-29 07:34:38 -04:00
|
|
|
} ;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 04:39:13 -05:00
|
|
|
cEnderman::cEnderman(void) :
|
2021-04-06 11:09:16 -04:00
|
|
|
Super("Enderman", mtEnderman, "entity.endermen.hurt", "entity.endermen.death", "entity.endermen.ambient", 0.6f, 2.9f),
|
2013-10-09 16:02:59 -04:00
|
|
|
m_bIsScreaming(false),
|
2018-05-06 13:07:34 -04:00
|
|
|
m_CarriedBlock(E_BLOCK_AIR),
|
|
|
|
m_CarriedMeta(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-01 06:39:56 -04:00
|
|
|
void cEnderman::GetDrops(cItems & a_Drops, cEntity * a_Killer)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-05-19 14:32:10 -04:00
|
|
|
unsigned int LootingLevel = 0;
|
2014-10-20 16:55:07 -04:00
|
|
|
if (a_Killer != nullptr)
|
2014-02-23 13:44:58 -05:00
|
|
|
{
|
|
|
|
LootingLevel = a_Killer->GetEquippedWeapon().m_Enchantments.GetLevel(cEnchantments::enchLooting);
|
|
|
|
}
|
|
|
|
AddRandomDropItem(a_Drops, 0, 1 + LootingLevel, E_ITEM_ENDER_PEARL);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-26 17:24:36 -04:00
|
|
|
|
2016-01-17 09:09:25 -05:00
|
|
|
void cEnderman::CheckEventSeePlayer(cChunk & a_Chunk)
|
2014-08-03 15:31:04 -04:00
|
|
|
{
|
2016-02-01 15:49:34 -05:00
|
|
|
if (GetTarget() != nullptr)
|
2014-05-29 07:34:38 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:49:36 -05:00
|
|
|
cPlayerLookCheck Callback(GetPosition().addedY(GetHeight()), m_SightDistance);
|
2014-06-04 04:19:55 -04:00
|
|
|
if (m_World->ForEachPlayer(Callback))
|
2014-05-29 07:34:38 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-01-17 09:09:25 -05:00
|
|
|
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT(Callback.GetPlayer() != nullptr);
|
2014-05-29 07:34:38 -04:00
|
|
|
|
2020-12-18 15:48:32 -05:00
|
|
|
// Target the player:
|
|
|
|
cAggressiveMonster::EventSeePlayer(Callback.GetPlayer(), a_Chunk);
|
2016-10-12 08:38:45 -04:00
|
|
|
m_bIsScreaming = true;
|
|
|
|
GetWorld()->BroadcastEntityMetadata(*this);
|
2014-05-29 07:34:38 -04:00
|
|
|
}
|
2014-08-01 17:05:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-04 04:19:55 -04:00
|
|
|
void cEnderman::EventLosePlayer()
|
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
Super::EventLosePlayer();
|
2014-06-04 04:19:55 -04:00
|
|
|
m_bIsScreaming = false;
|
|
|
|
GetWorld()->BroadcastEntityMetadata(*this);
|
2014-06-04 09:16:24 -04:00
|
|
|
}
|
2014-08-01 17:05:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-11 16:12:26 -05:00
|
|
|
void cEnderman::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
|
2014-09-28 16:56:41 -04:00
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
Super::Tick(a_Dt, a_Chunk);
|
2016-09-03 07:31:27 -04:00
|
|
|
if (!IsTicking())
|
|
|
|
{
|
|
|
|
// The base class tick destroyed us
|
|
|
|
return;
|
|
|
|
}
|
2014-09-28 16:56:41 -04:00
|
|
|
|
2021-01-04 21:13:02 -05:00
|
|
|
PREPARE_REL_AND_CHUNK(GetPosition().Floor(), a_Chunk);
|
|
|
|
if (!RelSuccess)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take damage when wet:
|
|
|
|
if (IsInWater() || Chunk->IsWeatherWetAt(Rel))
|
2014-09-28 16:56:41 -04:00
|
|
|
{
|
|
|
|
EventLosePlayer();
|
2020-09-17 14:06:41 -04:00
|
|
|
TakeDamage(dtEnvironment, nullptr, 1, 0);
|
2014-09-28 22:27:53 -04:00
|
|
|
// TODO teleport to a safe location
|
2014-09-28 16:56:41 -04:00
|
|
|
}
|
2017-06-28 04:56:50 -04:00
|
|
|
}
|