1
0
Fork 0
cuberite-2a/src/Mobs/Enderman.cpp

143 lines
2.7 KiB
C++
Raw Normal View History

#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Enderman.h"
#include "../Entities/Player.h"
2014-06-04 08:19:55 +00:00
#include "../Tracer.h"
///////////////////////////////////////////////////////////////////////////
// cPlayerLookCheck
class cPlayerLookCheck :
public cPlayerListCallback
{
public:
cPlayerLookCheck(Vector3d a_EndermanPos) :
2014-05-29 12:00:12 +00:00
m_Player(NULL),
m_EndermanPos(a_EndermanPos)
{
}
virtual bool Item(cPlayer * a_Player) override
{
// Don't check players who are in creative gamemode.
if (a_Player->IsGameModeCreative())
{
return false;
}
Vector3d Direction = m_EndermanPos - a_Player->GetPosition();
// Don't check players who are more then 64 blocks away.
if (Direction.SqrLength() > 64)
{
return false;
}
// Don't check if the player has a pumpkin on his head.
if (a_Player->GetEquippedHelmet().m_ItemType == E_BLOCK_PUMPKIN)
{
return false;
}
Direction.Normalize();
Vector3d LookVector = a_Player->GetLookVector();
LookVector.Normalize();
if ((Direction - LookVector).SqrLength() > 0.02)
{
return false;
}
2014-06-04 08:19:55 +00:00
cTracer LineOfSight(a_Player->GetWorld());
if (LineOfSight.Trace(m_EndermanPos, Direction, (int)Direction.Length()))
{
// No direct line of sight
return false;
}
m_Player = a_Player;
return true;
}
2014-06-04 08:19:55 +00:00
cPlayer * GetPlayer(void) const { return m_Player; }
protected:
cPlayer * m_Player;
Vector3d m_EndermanPos;
} ;
cEnderman::cEnderman(void) :
2013-10-20 08:23:30 +00:00
super("Enderman", mtEnderman, "mob.endermen.hit", "mob.endermen.death", 0.5, 2.9),
m_bIsScreaming(false),
CarriedBlock(E_BLOCK_AIR),
CarriedMeta(0)
{
}
void cEnderman::GetDrops(cItems & a_Drops, cEntity * a_Killer)
{
int LootingLevel = 0;
if (a_Killer != NULL)
{
LootingLevel = a_Killer->GetEquippedWeapon().m_Enchantments.GetLevel(cEnchantments::enchLooting);
}
AddRandomDropItem(a_Drops, 0, 1 + LootingLevel, E_ITEM_ENDER_PEARL);
}
2014-06-04 08:19:55 +00:00
void cEnderman::CheckEventSeePlayer()
{
if (m_Target != NULL)
{
return;
}
cPlayerLookCheck Callback(GetPosition());
2014-06-04 08:19:55 +00:00
if (m_World->ForEachPlayer(Callback))
{
return;
}
2014-06-04 08:19:55 +00:00
ASSERT(Callback.GetPlayer() != NULL);
2014-06-04 08:19:55 +00:00
int CX, CZ;
cChunkDef::BlockToChunk(POSX_TOINT, POSZ_TOINT, CX, CZ);
if (!GetWorld()->IsChunkLighted(CX, CZ))
{
2014-06-04 08:19:55 +00:00
GetWorld()->QueueLightChunk(CX, CZ);
return;
}
2014-06-04 08:19:55 +00:00
if ((GetWorld()->GetBlockSkyLight(POSX_TOINT, POSY_TOINT, POSZ_TOINT) - GetWorld()->GetSkyDarkness() < 15) && !Callback.GetPlayer()->IsGameModeCreative())
{
super::EventSeePlayer(Callback.GetPlayer());
m_EMState = CHASING;
m_bIsScreaming = true;
GetWorld()->BroadcastEntityMetadata(*this);
}
}
2014-06-04 08:19:55 +00:00
void cEnderman::EventLosePlayer()
{
super::EventLosePlayer();
m_bIsScreaming = false;
GetWorld()->BroadcastEntityMetadata(*this);
}