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

251 lines
4.8 KiB
C++
Raw Normal View History

#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Wolf.h"
#include "../World.h"
#include "../Entities/Player.h"
2014-02-20 19:41:53 +00:00
#include "../Items/ItemHandler.h"
#include "Broadcaster.h"
cWolf::cWolf(void) :
2013-10-20 08:23:30 +00:00
super("Wolf", mtWolf, "mob.wolf.hurt", "mob.wolf.death", 0.6, 0.8),
2013-11-10 20:24:36 +00:00
m_IsSitting(false),
m_IsTame(false),
2013-11-10 20:24:36 +00:00
m_IsBegging(false),
m_IsAngry(false),
2013-11-12 15:39:59 +00:00
m_OwnerName(""),
m_CollarColor(E_META_DYE_ORANGE)
{
m_RelativeWalkSpeed = 2;
}
2014-04-25 22:32:30 +00:00
bool cWolf::DoTakeDamage(TakeDamageInfo & a_TDI)
{
2014-04-25 22:32:30 +00:00
if (super::DoTakeDamage(a_TDI))
{
return false;
}
2013-11-10 20:24:36 +00:00
if (!m_IsTame)
{
2013-11-10 20:24:36 +00:00
m_IsAngry = true;
}
m_World->BroadcastEntityMetadata(*this); // Broadcast health and possibly angry face
2014-04-25 22:32:30 +00:00
return true;
}
fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'master' into cavespider-attack fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack
2015-11-08 12:44:17 +00:00
bool cWolf::Attack(std::chrono::milliseconds a_Dt)
{
UNUSED(a_Dt);
2014-10-20 20:55:07 +00:00
if ((m_Target != nullptr) && (m_Target->IsPlayer()))
{
2015-05-24 11:56:56 +00:00
if (static_cast<cPlayer *>(m_Target)->GetName() != m_OwnerName)
{
fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'master' into cavespider-attack fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack
2015-11-08 12:44:17 +00:00
return super::Attack(a_Dt);
}
}
else
{
fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'master' into cavespider-attack fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack
2015-11-08 12:44:17 +00:00
return super::Attack(a_Dt);
}
fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'master' into cavespider-attack fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style fix cavespider poisoning even if attack is in cooldown make attack function more responsive fix cavespider poisoning even if attack is in cooldown make attack function more responsive Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack code style Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'master' into cavespider-attack Merge branch 'cavespider-attack' of github.com:Gargaj/cuberite into cavespider-attack
2015-11-08 12:44:17 +00:00
return false;
}
void cWolf::OnRightClicked(cPlayer & a_Player)
{
2013-11-10 20:55:32 +00:00
if (!IsTame() && !IsAngry())
{
2014-08-03 20:03:48 +00:00
// If the player is holding a bone, try to tame the wolf:
if (a_Player.GetEquippedItem().m_ItemType == E_ITEM_BONE)
{
if (!a_Player.IsGameModeCreative())
{
a_Player.GetInventory().RemoveOneEquippedItem();
}
if (m_World->GetTickRandomNumber(7) == 0)
{
2014-08-03 20:03:48 +00:00
// Taming succeeded
SetMaxHealth(20);
SetIsTame(true);
2014-08-03 20:03:48 +00:00
SetOwner(a_Player.GetName(), a_Player.GetUUID());
m_World->BroadcastEntityStatus(*this, esWolfTamed);
m_World->GetBroadcaster().BroadcastParticleEffect("heart", static_cast<Vector3f>(GetPosition()), Vector3f{}, 0, 5);
}
else
{
2014-08-03 20:03:48 +00:00
// Taming failed
m_World->BroadcastEntityStatus(*this, esWolfTaming);
m_World->GetBroadcaster().BroadcastParticleEffect("smoke", static_cast<Vector3f>(GetPosition()), Vector3f{}, 0, 5);
}
}
}
else if (IsTame())
{
2014-08-03 20:03:48 +00:00
// Feed the wolf, restoring its health, or dye its collar:
2014-02-20 19:41:53 +00:00
switch (a_Player.GetEquippedItem().m_ItemType)
{
2014-02-20 19:41:53 +00:00
case E_ITEM_RAW_BEEF:
case E_ITEM_STEAK:
case E_ITEM_RAW_PORKCHOP:
case E_ITEM_COOKED_PORKCHOP:
case E_ITEM_RAW_CHICKEN:
case E_ITEM_COOKED_CHICKEN:
case E_ITEM_ROTTEN_FLESH:
{
2014-02-20 19:41:53 +00:00
if (m_Health < m_MaxHealth)
{
2014-02-20 19:41:53 +00:00
Heal(ItemHandler(a_Player.GetEquippedItem().m_ItemType)->GetFoodInfo().FoodLevel);
if (!a_Player.IsGameModeCreative())
{
a_Player.GetInventory().RemoveOneEquippedItem();
}
}
2014-02-20 19:41:53 +00:00
break;
}
case E_ITEM_DYE:
{
if (a_Player.GetName() == m_OwnerName) // Is the player the owner of the dog?
2014-02-20 19:41:53 +00:00
{
SetCollarColor(a_Player.GetEquippedItem().m_ItemDamage);
2014-02-20 19:41:53 +00:00
if (!a_Player.IsGameModeCreative())
{
a_Player.GetInventory().RemoveOneEquippedItem();
}
}
break;
}
2014-02-20 19:41:53 +00:00
default:
{
if (a_Player.GetName() == m_OwnerName) // Is the player the owner of the dog?
2014-02-20 19:41:53 +00:00
{
SetIsSitting(!IsSitting());
}
}
}
}
2015-04-29 16:24:14 +00:00
m_World->BroadcastEntityMetadata(*this);
}
void cWolf::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
if (!IsAngry())
{
cMonster::Tick(a_Dt, a_Chunk);
2013-11-10 20:55:32 +00:00
}
else
{
super::Tick(a_Dt, a_Chunk);
}
2015-05-24 11:56:56 +00:00
cPlayer * a_Closest_Player = m_World->FindClosestPlayer(GetPosition(), static_cast<float>(m_SightDistance));
2014-10-20 20:55:07 +00:00
if (a_Closest_Player != nullptr)
{
switch (a_Closest_Player->GetEquippedItem().m_ItemType)
{
case E_ITEM_BONE:
case E_ITEM_RAW_BEEF:
case E_ITEM_STEAK:
case E_ITEM_RAW_CHICKEN:
case E_ITEM_COOKED_CHICKEN:
case E_ITEM_ROTTEN_FLESH:
2014-02-20 19:41:53 +00:00
case E_ITEM_RAW_PORKCHOP:
case E_ITEM_COOKED_PORKCHOP:
{
if (!IsBegging())
{
SetIsBegging(true);
m_World->BroadcastEntityMetadata(*this);
}
2014-02-16 13:47:55 +00:00
m_FinalDestination = a_Closest_Player->GetPosition(); // So that we will look at a player holding food
2014-02-16 17:08:49 +00:00
// Don't move to the player if the wolf is sitting.
2014-02-16 17:08:49 +00:00
if (!IsSitting())
{
2014-02-16 17:08:49 +00:00
MoveToPosition(a_Closest_Player->GetPosition());
}
2014-02-16 13:47:55 +00:00
break;
}
default:
{
if (IsBegging())
{
SetIsBegging(false);
m_World->BroadcastEntityMetadata(*this);
}
}
}
}
2013-11-12 15:39:59 +00:00
2014-02-16 13:47:55 +00:00
if (IsTame() && !IsSitting())
2013-11-12 15:39:59 +00:00
{
TickFollowPlayer();
}
2014-02-16 17:08:49 +00:00
else if (IsSitting())
{
2015-04-29 16:24:14 +00:00
StopMovingToPosition();
2014-02-16 17:08:49 +00:00
}
2013-11-12 15:39:59 +00:00
}
void cWolf::TickFollowPlayer()
{
class cCallback :
public cPlayerListCallback
{
2013-11-12 15:39:59 +00:00
virtual bool Item(cPlayer * a_Player) override
{
2013-11-12 15:39:59 +00:00
OwnerPos = a_Player->GetPosition();
return false;
}
public:
Vector3d OwnerPos;
2013-11-10 20:55:32 +00:00
} Callback;
2014-02-16 13:47:55 +00:00
2013-11-12 15:39:59 +00:00
if (m_World->DoWithPlayer(m_OwnerName, Callback))
{
// The player is present in the world, follow him:
2013-11-12 15:39:59 +00:00
double Distance = (Callback.OwnerPos - GetPosition()).Length();
if (Distance > 20)
2013-11-12 15:39:59 +00:00
{
2014-02-16 17:08:49 +00:00
Callback.OwnerPos.y = FindFirstNonAirBlockPosition(Callback.OwnerPos.x, Callback.OwnerPos.z);
TeleportToCoords(Callback.OwnerPos.x, Callback.OwnerPos.y, Callback.OwnerPos.z);
2013-11-12 15:39:59 +00:00
}
else
{
2014-02-16 13:47:55 +00:00
MoveToPosition(Callback.OwnerPos);
}
}
2013-11-10 20:55:32 +00:00
}