Files
cuberite-2a/src/Mobs/Horse.cpp
T

306 lines
5.4 KiB
C++
Raw Normal View History

2013-09-18 22:17:43 +01:00
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Horse.h"
2013-10-08 19:20:49 +01:00
#include "../World.h"
2015-11-23 23:39:19 +00:00
#include "../EffectID.h"
2013-10-08 19:20:49 +01:00
#include "../Entities/Player.h"
#include "../UI/HorseWindow.h"
2013-09-18 22:17:43 +01:00
2013-10-08 19:20:49 +01:00
cHorse::cHorse(int Type, int Color, int Style, int TameTimes) :
2020-04-13 18:38:06 +02:00
Super("Horse", mtHorse, "entity.horse.hurt", "entity.horse.death", "entity.horse.ambient", 1.4, 1.6),
2017-10-21 17:56:09 +01:00
cEntityWindowOwner(this),
2013-10-08 22:21:55 +01:00
m_bHasChest(false),
2013-10-08 19:20:49 +01:00
m_bIsEating(false),
m_bIsRearing(false),
m_bIsMouthOpen(false),
m_bIsTame(false),
m_Type(Type),
m_Color(Color),
m_Style(Style),
m_TimesToTame(TameTimes),
2013-10-11 21:33:56 +01:00
m_TameAttemptTimes(0),
2015-12-15 21:11:58 +01:00
m_RearTickCount(0),
2017-07-02 07:40:59 +02:00
m_MaxSpeed(14.0)
2013-09-18 22:17:43 +01:00
{
}
2017-10-21 17:56:09 +01:00
cHorse::~cHorse()
{
auto Window = GetWindow();
if (Window != nullptr)
{
Window->OwnerDestroyed();
}
}
2015-01-11 21:12:26 +00:00
void cHorse::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
2013-10-08 19:20:49 +01:00
{
2020-04-13 18:38:06 +02:00
Super::Tick(a_Dt, a_Chunk);
if (!IsTicking())
{
// The base class tick destroyed us
return;
}
2013-10-08 19:20:49 +01:00
2017-06-13 20:35:30 +01:00
auto & Random = GetRandomProvider();
2013-10-08 19:20:49 +01:00
if (!m_bIsMouthOpen)
{
2017-06-13 20:35:30 +01:00
if (Random.RandBool(0.02))
2013-10-08 19:20:49 +01:00
{
m_bIsMouthOpen = true;
}
}
else
{
2017-06-13 20:35:30 +01:00
if (Random.RandBool(0.10))
2013-10-08 19:20:49 +01:00
{
m_bIsMouthOpen = false;
}
}
2014-10-20 21:55:07 +01:00
if ((m_Attachee != nullptr) && (!m_bIsTame))
2013-10-08 19:20:49 +01:00
{
if (m_TameAttemptTimes < m_TimesToTame)
{
2017-06-13 20:35:30 +01:00
if (Random.RandBool(0.02))
2013-10-08 19:20:49 +01:00
{
m_World->BroadcastSoundParticleEffect(EffectID::PARTICLE_SMOKE, GetPosition().Floor(), int(SmokeDirection::SOUTH_EAST));
m_World->BroadcastSoundParticleEffect(EffectID::PARTICLE_SMOKE, GetPosition().Floor(), int(SmokeDirection::SOUTH_WEST));
m_World->BroadcastSoundParticleEffect(EffectID::PARTICLE_SMOKE, GetPosition().Floor(), int(SmokeDirection::NORTH_EAST));
m_World->BroadcastSoundParticleEffect(EffectID::PARTICLE_SMOKE, GetPosition().Floor(), int(SmokeDirection::NORTH_WEST));
2013-10-08 19:20:49 +01:00
m_World->BroadcastSoundEffect("entity.horse.angry", GetPosition(), 1.0f, 1.0f);
2013-10-08 19:20:49 +01:00
m_Attachee->Detach();
m_bIsRearing = true;
}
2014-07-17 22:59:02 +02:00
}
2013-10-08 19:20:49 +01:00
else
{
2018-07-24 22:30:49 +01:00
m_World->BroadcastParticleEffect("heart", static_cast<Vector3f>(GetPosition()), Vector3f{}, 0, 5);
2013-10-08 19:20:49 +01:00
m_bIsTame = true;
}
}
2015-12-22 07:43:50 +02:00
2013-10-11 21:33:56 +01:00
if (m_bIsRearing)
2013-10-08 19:20:49 +01:00
{
2013-10-11 21:33:56 +01:00
if (m_RearTickCount == 20)
{
m_bIsRearing = false;
2013-10-17 18:41:52 +02:00
m_RearTickCount = 0;
2013-10-11 21:33:56 +01:00
}
2013-10-18 16:34:01 +02:00
else
{
m_RearTickCount++;
}
2013-10-08 19:20:49 +01:00
}
m_World->BroadcastEntityMetadata(*this);
}
void cHorse::OnRightClicked(cPlayer & a_Player)
{
2020-04-13 18:38:06 +02:00
Super::OnRightClicked(a_Player);
2015-11-29 19:13:31 +01:00
2017-07-02 07:40:59 +02:00
if (m_bIsTame)
2013-10-18 16:34:01 +02:00
{
2017-10-21 17:56:09 +01:00
if (a_Player.IsCrouched())
2013-10-18 16:34:01 +02:00
{
2017-10-21 17:56:09 +01:00
PlayerOpenWindow(a_Player);
return;
}
auto EquipedItemType = a_Player.GetEquippedItem().m_ItemType;
if (
!IsSaddled() &&
(
(EquipedItemType == E_ITEM_SADDLE) ||
ItemCategory::IsHorseArmor(EquipedItemType)
)
)
{
// Player is holding a horse inventory item, open the window:
PlayerOpenWindow(a_Player);
2013-10-18 16:34:01 +02:00
}
2017-07-02 07:40:59 +02:00
else
2013-10-18 16:34:01 +02:00
{
2017-07-02 07:40:59 +02:00
a_Player.AttachTo(this);
2013-10-18 16:34:01 +02:00
}
}
2017-07-02 07:40:59 +02:00
else if (a_Player.GetEquippedItem().IsEmpty())
2013-10-18 16:34:01 +02:00
{
2017-08-21 10:46:41 +02:00
// Check if leashed / unleashed to player before try to ride
if (!m_IsLeashActionJustDone)
2013-10-18 16:34:01 +02:00
{
2017-08-21 10:46:41 +02:00
if (m_Attachee != nullptr)
2013-10-18 16:34:01 +02:00
{
2017-08-21 10:46:41 +02:00
if (m_Attachee->GetUniqueID() == a_Player.GetUniqueID())
{
a_Player.Detach();
return;
}
2013-10-18 16:34:01 +02:00
2017-08-21 10:46:41 +02:00
if (m_Attachee->IsPlayer())
{
return;
}
m_Attachee->Detach();
2013-10-18 16:34:01 +02:00
}
2017-08-21 10:46:41 +02:00
m_TameAttemptTimes++;
a_Player.AttachTo(this);
2013-10-18 16:34:01 +02:00
}
}
2017-07-02 07:40:59 +02:00
else
{
m_bIsRearing = true;
m_RearTickCount = 0;
m_World->BroadcastSoundEffect("entity.horse.angry", GetPosition(), 1.0f, 0.8f);
2017-07-02 07:40:59 +02:00
}
2013-10-08 19:20:49 +01:00
}
2017-10-21 17:56:09 +01:00
void cHorse::SetHorseSaddle(cItem a_Saddle)
{
if (a_Saddle.m_ItemType == E_ITEM_SADDLE)
{
m_World->BroadcastSoundEffect("entity.horse.saddle", GetPosition(), 1.0f, 0.8f);
}
else if (!a_Saddle.IsEmpty())
{
return; // Invalid item
}
m_Saddle = std::move(a_Saddle);
m_World->BroadcastEntityMetadata(*this);
}
void cHorse::SetHorseArmor(cItem a_Armor)
{
if (ItemCategory::IsHorseArmor(a_Armor.m_ItemType))
{
m_World->BroadcastSoundEffect("entity.horse.armor", GetPosition(), 1.0f, 0.8f);
}
else if (!a_Armor.IsEmpty())
{
return; // Invalid item
}
m_Armor = std::move(a_Armor);
m_World->BroadcastEntityMetadata(*this);
}
int cHorse::GetHorseArmour(void) const
{
switch (m_Armor.m_ItemType)
{
case E_ITEM_EMPTY: return 0;
case E_ITEM_IRON_HORSE_ARMOR: return 1;
case E_ITEM_GOLD_HORSE_ARMOR: return 2;
case E_ITEM_DIAMOND_HORSE_ARMOR: return 3;
default:
{
LOGWARN("cHorse::GetHorseArmour: Invalid armour item (%d)", m_Armor.m_ItemType);
return 0;
}
}
}
2013-09-18 22:17:43 +01:00
void cHorse::GetDrops(cItems & a_Drops, cEntity * a_Killer)
{
2017-10-21 18:55:46 +02:00
if (IsBaby())
{
return; // Babies don't drop items
}
2015-05-19 19:32:10 +01:00
unsigned int LootingLevel = 0;
2014-10-20 21:55:07 +01:00
if (a_Killer != nullptr)
{
LootingLevel = a_Killer->GetEquippedWeapon().m_Enchantments.GetLevel(cEnchantments::enchLooting);
}
AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_LEATHER);
2017-10-21 17:56:09 +01:00
if (IsSaddled())
{
2017-10-21 17:56:09 +01:00
a_Drops.push_back(m_Saddle);
}
if (!m_Armor.IsEmpty())
{
a_Drops.push_back(m_Armor);
}
2013-09-18 22:17:43 +01:00
}
2015-12-15 21:11:58 +01:00
2015-12-22 07:43:50 +02:00
void cHorse::InStateIdle(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
2015-12-15 21:11:58 +01:00
{
// If horse is tame and someone is sitting on it, don't walk around
if ((!m_bIsTame) || (m_Attachee == nullptr))
{
2020-04-13 18:38:06 +02:00
Super::InStateIdle(a_Dt, a_Chunk);
2015-12-15 21:11:58 +01:00
}
}
void cHorse::HandleSpeedFromAttachee(float a_Forward, float a_Sideways)
{
2017-10-21 17:56:09 +01:00
if ((m_bIsTame) && IsSaddled())
2016-03-01 12:58:02 +01:00
{
2020-04-13 18:38:06 +02:00
Super::HandleSpeedFromAttachee(a_Forward * m_MaxSpeed, a_Sideways * m_MaxSpeed);
2016-03-01 12:58:02 +01:00
}
2015-12-15 21:11:58 +01:00
}
2017-10-21 17:56:09 +01:00
void cHorse::PlayerOpenWindow(cPlayer & a_Player)
{
auto Window = GetWindow();
if (Window == nullptr)
{
Window = new cHorseWindow(*this);
OpenWindow(Window);
}
a_Player.OpenWindow(*Window);
}