2014-04-26 19:45:39 -04:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include "Player.h"
|
2014-04-27 20:03:06 -04:00
|
|
|
#include "ArrowEntity.h"
|
2014-04-26 19:45:39 -04:00
|
|
|
#include "../Chunk.h"
|
2020-05-03 16:05:32 -04:00
|
|
|
#include "../Blocks/BlockButton.h"
|
2020-04-18 20:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
cArrowEntity::cArrowEntity(cEntity * a_Creator, Vector3d a_Pos, Vector3d a_Speed):
|
2020-04-18 20:10:14 -04:00
|
|
|
Super(pkArrow, a_Creator, a_Pos, a_Speed, 0.5, 0.5),
|
2014-04-27 20:03:06 -04:00
|
|
|
m_PickupState(psNoPickup),
|
|
|
|
m_DamageCoeff(2),
|
|
|
|
m_IsCritical(false),
|
|
|
|
m_Timer(0),
|
2020-04-18 20:10:14 -04:00
|
|
|
m_bIsCollected(false)
|
2014-04-26 19:45:39 -04:00
|
|
|
{
|
|
|
|
SetMass(0.1);
|
2015-03-30 19:42:32 -04:00
|
|
|
SetGravity(-20.0f);
|
2020-04-18 20:10:14 -04:00
|
|
|
|
2018-09-24 16:33:39 -04:00
|
|
|
FLOGD("Created arrow {0} with speed {1:.02f} and rot {{{2:.02f}, {3:.02f}}}",
|
|
|
|
m_UniqueID, GetSpeed(), GetYaw(), GetPitch()
|
2014-06-16 16:57:13 -04:00
|
|
|
);
|
2014-04-26 19:45:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cArrowEntity::cArrowEntity(cPlayer & a_Player, double a_Force) :
|
2020-04-18 20:10:14 -04:00
|
|
|
cArrowEntity(&a_Player, a_Player.GetThrowStartPos(), a_Player.GetThrowSpeed(a_Force * 1.5 * 20))
|
2014-04-26 19:45:39 -04:00
|
|
|
{
|
2020-04-18 20:10:14 -04:00
|
|
|
m_IsCritical = a_Force >= 1;
|
|
|
|
m_PickupState = a_Player.IsGameModeCreative() ? psInCreative : psInSurvivalOrCreative;
|
2014-04-26 19:45:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cArrowEntity::CanPickup(const cPlayer & a_Player) const
|
|
|
|
{
|
|
|
|
switch (m_PickupState)
|
|
|
|
{
|
|
|
|
case psNoPickup: return false;
|
|
|
|
case psInSurvivalOrCreative: return (a_Player.IsGameModeSurvival() || a_Player.IsGameModeCreative());
|
|
|
|
case psInCreative: return a_Player.IsGameModeCreative();
|
|
|
|
}
|
2018-02-04 18:07:12 -05:00
|
|
|
UNREACHABLE("Unsupported arrow pickup state");
|
2014-04-26 19:45:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-19 11:57:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-07 04:25:34 -04:00
|
|
|
void cArrowEntity::OnHitSolidBlock(Vector3d a_HitPos, eBlockFace a_HitFace)
|
2014-07-17 16:50:58 -04:00
|
|
|
{
|
2020-04-18 11:58:22 -04:00
|
|
|
// Save the direction we're going in before Super resets it
|
|
|
|
auto Speed = GetSpeed();
|
|
|
|
Speed.Normalize();
|
2014-06-22 15:44:01 -04:00
|
|
|
|
2020-04-18 11:58:22 -04:00
|
|
|
/*
|
|
|
|
The line tracer returns the arrow hit position located on the face of a block;
|
|
|
|
if the arrow hit a block at -5, 95, -5 then a_HitPos would start off as -4, 95.5, -4.1,
|
|
|
|
i.e. it collided with the X face.
|
|
|
|
|
2020-04-18 20:10:14 -04:00
|
|
|
First we subtract a bit of speed vector so it doesn't appear black clientside.
|
|
|
|
|
2020-04-18 11:58:22 -04:00
|
|
|
Then we add a bit of speed vector to make a_HitPos -4.0001, 95.5, -4.1
|
|
|
|
and floor to get exactly -5, 95, -5 which is stored in m_HitBlockPos.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 20:10:14 -04:00
|
|
|
// Shift the arrow's position slightly back so that less than 50% of the hitbox
|
|
|
|
// is in the block. Otherwise the arrow can appear black
|
|
|
|
Super::OnHitSolidBlock(a_HitPos - (Speed / 100), a_HitFace);
|
|
|
|
|
2020-04-18 11:58:22 -04:00
|
|
|
// Nudge into the block a tiny bit according to its direction of travel
|
|
|
|
// Floor to give the coordinates of the block it crashed into
|
2020-04-18 20:10:14 -04:00
|
|
|
m_HitBlockPos = (a_HitPos + (Speed / 100000)).Floor();
|
2015-05-28 10:45:47 -04:00
|
|
|
|
2014-04-26 19:45:39 -04:00
|
|
|
// Broadcast arrow hit sound
|
2020-04-18 11:58:22 -04:00
|
|
|
m_World->BroadcastSoundEffect("entity.arrow.hit", m_HitBlockPos, 0.5f, static_cast<float>(0.75 + (static_cast<float>((GetUniqueID() * 23) % 32)) / 64));
|
2014-08-19 06:38:15 -04:00
|
|
|
|
2020-05-03 16:05:32 -04:00
|
|
|
// Trigger any buttons that were hit
|
|
|
|
// Wooden buttons will be depressed by the arrow
|
|
|
|
cBlockButtonHandler::OnArrowHit(*m_World, m_HitBlockPos, a_HitFace);
|
|
|
|
|
2020-04-18 11:58:22 -04:00
|
|
|
if ((m_World->GetBlock(m_HitBlockPos) == E_BLOCK_TNT) && IsOnFire())
|
2014-08-19 06:38:15 -04:00
|
|
|
{
|
2020-04-18 11:58:22 -04:00
|
|
|
m_World->SetBlock(m_HitBlockPos, E_BLOCK_AIR, 0);
|
|
|
|
m_World->SpawnPrimedTNT(m_HitBlockPos);
|
2014-08-19 06:38:15 -04:00
|
|
|
}
|
2014-04-26 19:45:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-07 04:25:34 -04:00
|
|
|
void cArrowEntity::OnHitEntity(cEntity & a_EntityHit, Vector3d a_HitPos)
|
2014-07-17 16:50:58 -04:00
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
Super::OnHitEntity(a_EntityHit, a_HitPos);
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2015-07-29 11:04:03 -04:00
|
|
|
int Damage = static_cast<int>(GetSpeed().Length() / 20 * m_DamageCoeff + 0.5);
|
2014-04-26 19:45:39 -04:00
|
|
|
if (m_IsCritical)
|
|
|
|
{
|
|
|
|
Damage += m_World->GetTickRandomNumber(Damage / 2 + 2);
|
|
|
|
}
|
2014-08-19 06:38:15 -04:00
|
|
|
|
2015-05-19 14:32:10 -04:00
|
|
|
unsigned int PowerLevel = m_CreatorData.m_Enchantments.GetLevel(cEnchantments::enchPower);
|
2014-08-19 06:38:15 -04:00
|
|
|
if (PowerLevel > 0)
|
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
int ExtraDamage = static_cast<int>(ceil(0.25 * (PowerLevel + 1)));
|
2014-09-01 08:29:13 -04:00
|
|
|
Damage += ExtraDamage;
|
2014-08-19 06:38:15 -04:00
|
|
|
}
|
2014-08-19 10:08:17 -04:00
|
|
|
|
2020-03-19 13:13:41 -04:00
|
|
|
double Knockback = 10;
|
|
|
|
|
2015-05-19 14:32:10 -04:00
|
|
|
unsigned int PunchLevel = m_CreatorData.m_Enchantments.GetLevel(cEnchantments::enchPunch);
|
2020-03-19 13:13:41 -04:00
|
|
|
unsigned int PunchLevelMultiplier = 8;
|
|
|
|
|
|
|
|
Knockback += PunchLevelMultiplier * PunchLevel;
|
|
|
|
a_EntityHit.TakeDamage(dtRangedAttack, GetCreatorUniqueID(), Damage, Knockback);
|
2015-05-28 10:45:47 -04:00
|
|
|
|
2018-01-14 13:44:45 -05:00
|
|
|
if (IsOnFire() && !a_EntityHit.IsInWater())
|
2014-08-19 06:38:15 -04:00
|
|
|
{
|
|
|
|
a_EntityHit.StartBurning(100);
|
|
|
|
}
|
|
|
|
|
2014-04-26 19:45:39 -04:00
|
|
|
// Broadcast successful hit sound
|
2020-03-03 19:05:04 -05:00
|
|
|
GetWorld()->BroadcastSoundEffect("entity.arrow.hit", GetPosition(), 0.5, static_cast<float>(0.75 + (static_cast<float>((GetUniqueID() * 23) % 32)) / 64));
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2014-04-26 19:45:39 -04:00
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-15 13:01:55 -04:00
|
|
|
void cArrowEntity::CollectedBy(cPlayer & a_Dest)
|
2014-04-26 19:45:39 -04:00
|
|
|
{
|
2014-10-15 13:01:55 -04:00
|
|
|
if (m_IsInGround && !m_bIsCollected && CanPickup(a_Dest))
|
2014-04-26 19:45:39 -04:00
|
|
|
{
|
2014-06-26 12:42:28 -04:00
|
|
|
// Do not add the arrow to the inventory when the player is in creative:
|
2014-10-15 13:01:55 -04:00
|
|
|
if (!a_Dest.IsGameModeCreative())
|
2014-04-26 19:45:39 -04:00
|
|
|
{
|
2020-03-22 06:17:04 -04:00
|
|
|
int NumAdded = a_Dest.GetInventory().AddItem(cItem(E_ITEM_ARROW));
|
2014-06-16 16:57:13 -04:00
|
|
|
if (NumAdded == 0)
|
|
|
|
{
|
|
|
|
// No space in the inventory
|
|
|
|
return;
|
|
|
|
}
|
2014-04-26 19:45:39 -04:00
|
|
|
}
|
2014-06-26 12:42:28 -04:00
|
|
|
|
2016-12-15 14:21:43 -05:00
|
|
|
GetWorld()->BroadcastCollectEntity(*this, a_Dest, 1);
|
2020-04-03 11:49:12 -04:00
|
|
|
GetWorld()->BroadcastSoundEffect("entity.item.pickup", GetPosition(), 0.3f, (1.2f + (static_cast<float>((GetUniqueID() * 23) % 32)) / 64));
|
2014-06-16 16:57:13 -04:00
|
|
|
m_bIsCollected = true;
|
2014-04-26 19:45:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-11 16:12:26 -05:00
|
|
|
void cArrowEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
|
2014-04-26 19:45:39 -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;
|
|
|
|
}
|
2015-01-16 08:13:23 -05:00
|
|
|
m_Timer += a_Dt;
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2014-04-26 19:45:39 -04:00
|
|
|
if (m_bIsCollected)
|
|
|
|
{
|
2015-01-16 08:13:23 -05:00
|
|
|
if (m_Timer > std::chrono::milliseconds(500))
|
2014-04-26 19:45:39 -04:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-01-16 08:13:23 -05:00
|
|
|
else if (m_Timer > std::chrono::minutes(5))
|
2014-04-26 19:45:39 -04:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2014-04-26 19:45:39 -04:00
|
|
|
if (m_IsInGround)
|
2015-03-21 09:07:16 -04:00
|
|
|
{
|
2020-04-18 20:10:14 -04:00
|
|
|
if (m_World->GetBlock(m_HitBlockPos) == E_BLOCK_AIR) // Block attached to was destroyed?
|
2014-04-26 19:45:39 -04:00
|
|
|
{
|
2014-07-17 16:15:34 -04:00
|
|
|
m_IsInGround = false; // Yes, begin simulating physics again
|
2014-04-26 19:45:39 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-27 12:42:31 -04:00
|
|
|
}
|
2020-08-08 17:41:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cArrowEntity::DoesPreventBlockPlacement(void) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|