2013-09-18 17:17:43 -04:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include "Horse.h"
|
2013-10-08 14:20:49 -04:00
|
|
|
#include "../World.h"
|
2015-11-23 18:39:19 -05:00
|
|
|
#include "../EffectID.h"
|
2013-10-08 14:20:49 -04:00
|
|
|
#include "../Entities/Player.h"
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../UI/HorseWindow.h"
|
2013-09-18 17:17:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-08 14:20:49 -04:00
|
|
|
cHorse::cHorse(int Type, int Color, int Style, int TameTimes) :
|
2020-04-13 12:38:06 -04:00
|
|
|
Super("Horse", mtHorse, "entity.horse.hurt", "entity.horse.death", "entity.horse.ambient", 1.4, 1.6),
|
2017-10-21 12:56:09 -04:00
|
|
|
cEntityWindowOwner(this),
|
2013-10-08 17:21:55 -04:00
|
|
|
m_bHasChest(false),
|
2013-10-08 14:20:49 -04: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 16:33:56 -04:00
|
|
|
m_TameAttemptTimes(0),
|
2015-12-15 15:11:58 -05:00
|
|
|
m_RearTickCount(0),
|
2017-07-02 01:40:59 -04:00
|
|
|
m_MaxSpeed(14.0)
|
2013-09-18 17:17:43 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-21 12:56:09 -04:00
|
|
|
cHorse::~cHorse()
|
|
|
|
{
|
|
|
|
auto Window = GetWindow();
|
|
|
|
if (Window != nullptr)
|
|
|
|
{
|
|
|
|
Window->OwnerDestroyed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-11 16:12:26 -05:00
|
|
|
void cHorse::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
|
2013-10-08 14:20:49 -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;
|
|
|
|
}
|
2013-10-08 14:20:49 -04:00
|
|
|
|
2017-06-13 15:35:30 -04:00
|
|
|
auto & Random = GetRandomProvider();
|
|
|
|
|
2013-10-08 14:20:49 -04:00
|
|
|
if (!m_bIsMouthOpen)
|
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
if (Random.RandBool(0.02))
|
2013-10-08 14:20:49 -04:00
|
|
|
{
|
|
|
|
m_bIsMouthOpen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
if (Random.RandBool(0.10))
|
2013-10-08 14:20:49 -04:00
|
|
|
{
|
|
|
|
m_bIsMouthOpen = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 16:55:07 -04:00
|
|
|
if ((m_Attachee != nullptr) && (!m_bIsTame))
|
2013-10-08 14:20:49 -04:00
|
|
|
{
|
|
|
|
if (m_TameAttemptTimes < m_TimesToTame)
|
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
if (Random.RandBool(0.02))
|
2013-10-08 14:20:49 -04:00
|
|
|
{
|
2018-07-26 19:12:41 -04: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 14:20:49 -04:00
|
|
|
|
2017-09-19 10:12:54 -04:00
|
|
|
m_World->BroadcastSoundEffect("entity.horse.angry", GetPosition(), 1.0f, 1.0f);
|
2013-10-08 14:20:49 -04:00
|
|
|
m_Attachee->Detach();
|
|
|
|
m_bIsRearing = true;
|
|
|
|
}
|
2014-07-17 16:59:02 -04:00
|
|
|
}
|
2013-10-08 14:20:49 -04:00
|
|
|
else
|
|
|
|
{
|
2018-07-24 17:30:49 -04:00
|
|
|
m_World->BroadcastParticleEffect("heart", static_cast<Vector3f>(GetPosition()), Vector3f{}, 0, 5);
|
2013-10-08 14:20:49 -04:00
|
|
|
m_bIsTame = true;
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 00:43:50 -05:00
|
|
|
|
2013-10-11 16:33:56 -04:00
|
|
|
if (m_bIsRearing)
|
2013-10-08 14:20:49 -04:00
|
|
|
{
|
2013-10-11 16:33:56 -04:00
|
|
|
if (m_RearTickCount == 20)
|
|
|
|
{
|
|
|
|
m_bIsRearing = false;
|
2013-10-17 12:41:52 -04:00
|
|
|
m_RearTickCount = 0;
|
2013-10-11 16:33:56 -04:00
|
|
|
}
|
2013-10-18 10:34:01 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_RearTickCount++;
|
|
|
|
}
|
2013-10-08 14:20:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_World->BroadcastEntityMetadata(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHorse::OnRightClicked(cPlayer & a_Player)
|
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
Super::OnRightClicked(a_Player);
|
2015-11-29 13:13:31 -05:00
|
|
|
|
2017-07-02 01:40:59 -04:00
|
|
|
if (m_bIsTame)
|
2013-10-18 10:34:01 -04:00
|
|
|
{
|
2017-10-21 12:56:09 -04:00
|
|
|
if (a_Player.IsCrouched())
|
2013-10-18 10:34:01 -04:00
|
|
|
{
|
2017-10-21 12:56:09 -04: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 10:34:01 -04:00
|
|
|
}
|
2017-07-02 01:40:59 -04:00
|
|
|
else
|
2013-10-18 10:34:01 -04:00
|
|
|
{
|
2017-07-02 01:40:59 -04:00
|
|
|
a_Player.AttachTo(this);
|
2013-10-18 10:34:01 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-02 01:40:59 -04:00
|
|
|
else if (a_Player.GetEquippedItem().IsEmpty())
|
2013-10-18 10:34:01 -04:00
|
|
|
{
|
2017-08-21 04:46:41 -04:00
|
|
|
// Check if leashed / unleashed to player before try to ride
|
|
|
|
if (!m_IsLeashActionJustDone)
|
2013-10-18 10:34:01 -04:00
|
|
|
{
|
2017-08-21 04:46:41 -04:00
|
|
|
if (m_Attachee != nullptr)
|
2013-10-18 10:34:01 -04:00
|
|
|
{
|
2017-08-21 04:46:41 -04:00
|
|
|
if (m_Attachee->GetUniqueID() == a_Player.GetUniqueID())
|
|
|
|
{
|
|
|
|
a_Player.Detach();
|
|
|
|
return;
|
|
|
|
}
|
2013-10-18 10:34:01 -04:00
|
|
|
|
2017-08-21 04:46:41 -04:00
|
|
|
if (m_Attachee->IsPlayer())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Attachee->Detach();
|
2013-10-18 10:34:01 -04:00
|
|
|
}
|
|
|
|
|
2017-08-21 04:46:41 -04:00
|
|
|
m_TameAttemptTimes++;
|
|
|
|
a_Player.AttachTo(this);
|
2013-10-18 10:34:01 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-02 01:40:59 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_bIsRearing = true;
|
|
|
|
m_RearTickCount = 0;
|
2017-09-19 10:12:54 -04:00
|
|
|
m_World->BroadcastSoundEffect("entity.horse.angry", GetPosition(), 1.0f, 0.8f);
|
2017-07-02 01:40:59 -04:00
|
|
|
}
|
2013-10-08 14:20:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-21 12:56:09 -04: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 17:17:43 -04:00
|
|
|
void cHorse::GetDrops(cItems & a_Drops, cEntity * a_Killer)
|
|
|
|
{
|
2017-10-21 12:55:46 -04:00
|
|
|
if (IsBaby())
|
|
|
|
{
|
|
|
|
return; // Babies don't drop items
|
|
|
|
}
|
|
|
|
|
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, 2 + LootingLevel, E_ITEM_LEATHER);
|
2017-10-21 12:56:09 -04:00
|
|
|
if (IsSaddled())
|
2013-10-28 15:28:16 -04:00
|
|
|
{
|
2017-10-21 12:56:09 -04:00
|
|
|
a_Drops.push_back(m_Saddle);
|
|
|
|
}
|
|
|
|
if (!m_Armor.IsEmpty())
|
|
|
|
{
|
|
|
|
a_Drops.push_back(m_Armor);
|
2013-10-28 15:28:16 -04:00
|
|
|
}
|
2013-09-18 17:17:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-15 15:11:58 -05:00
|
|
|
|
2015-12-22 00:43:50 -05:00
|
|
|
void cHorse::InStateIdle(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
|
2015-12-15 15:11:58 -05:00
|
|
|
{
|
|
|
|
// If horse is tame and someone is sitting on it, don't walk around
|
|
|
|
if ((!m_bIsTame) || (m_Attachee == nullptr))
|
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
Super::InStateIdle(a_Dt, a_Chunk);
|
2015-12-15 15:11:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHorse::HandleSpeedFromAttachee(float a_Forward, float a_Sideways)
|
|
|
|
{
|
2017-10-21 12:56:09 -04:00
|
|
|
if ((m_bIsTame) && IsSaddled())
|
2016-03-01 06:58:02 -05:00
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
Super::HandleSpeedFromAttachee(a_Forward * m_MaxSpeed, a_Sideways * m_MaxSpeed);
|
2016-03-01 06:58:02 -05:00
|
|
|
}
|
2015-12-15 15:11:58 -05:00
|
|
|
}
|
2017-10-21 12:56:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHorse::PlayerOpenWindow(cPlayer & a_Player)
|
|
|
|
{
|
|
|
|
auto Window = GetWindow();
|
|
|
|
if (Window == nullptr)
|
|
|
|
{
|
|
|
|
Window = new cHorseWindow(*this);
|
|
|
|
OpenWindow(Window);
|
|
|
|
}
|
|
|
|
|
|
|
|
a_Player.OpenWindow(*Window);
|
|
|
|
}
|