2013-10-28 16:41:43 -04:00
|
|
|
|
2013-08-22 02:55:58 -04:00
|
|
|
// ProjectileEntity.cpp
|
|
|
|
|
|
|
|
// Implements the cProjectileEntity class representing the common base class for projectiles, as well as individual projectile types
|
|
|
|
|
|
|
|
#include "Globals.h"
|
2014-04-26 19:45:39 -04:00
|
|
|
|
2014-03-29 10:26:41 -04:00
|
|
|
#include "../Bindings/PluginManager.h"
|
2013-08-22 02:55:58 -04:00
|
|
|
#include "ProjectileEntity.h"
|
2020-04-03 02:57:01 -04:00
|
|
|
#include "../BlockInfo.h"
|
2013-08-22 03:07:12 -04:00
|
|
|
#include "../ClientHandle.h"
|
2013-08-27 13:57:37 -04:00
|
|
|
#include "../LineBlockTracer.h"
|
2013-09-01 16:40:35 -04:00
|
|
|
#include "../BoundingBox.h"
|
|
|
|
#include "../ChunkMap.h"
|
|
|
|
#include "../Chunk.h"
|
2013-08-27 13:57:37 -04:00
|
|
|
|
2014-04-27 20:03:06 -04:00
|
|
|
#include "ArrowEntity.h"
|
|
|
|
#include "ThrownEggEntity.h"
|
|
|
|
#include "ThrownEnderPearlEntity.h"
|
|
|
|
#include "ExpBottleEntity.h"
|
|
|
|
#include "ThrownSnowballEntity.h"
|
|
|
|
#include "FireChargeEntity.h"
|
|
|
|
#include "FireworkEntity.h"
|
|
|
|
#include "GhastFireballEntity.h"
|
2014-06-06 03:16:33 -04:00
|
|
|
#include "WitherSkullEntity.h"
|
2014-07-20 05:56:59 -04:00
|
|
|
#include "SplashPotionEntity.h"
|
2014-07-04 11:49:24 -04:00
|
|
|
#include "Player.h"
|
2014-04-26 19:45:39 -04:00
|
|
|
|
2013-08-27 13:57:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-08-27 13:57:37 -04:00
|
|
|
// cProjectileTracerCallback:
|
|
|
|
|
|
|
|
class cProjectileTracerCallback :
|
|
|
|
public cBlockTracer::cCallbacks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cProjectileTracerCallback(cProjectileEntity * a_Projectile) :
|
2013-09-02 15:56:55 -04:00
|
|
|
m_Projectile(a_Projectile),
|
|
|
|
m_SlowdownCoeff(0.99) // Default slowdown when not in water
|
2013-08-27 13:57:37 -04:00
|
|
|
{
|
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-09-02 15:56:55 -04:00
|
|
|
double GetSlowdownCoeff(void) const { return m_SlowdownCoeff; }
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-08-27 13:57:37 -04:00
|
|
|
protected:
|
|
|
|
cProjectileEntity * m_Projectile;
|
2013-09-02 15:56:55 -04:00
|
|
|
double m_SlowdownCoeff;
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-09-02 15:56:55 -04:00
|
|
|
// cCallbacks overrides:
|
2020-05-08 05:04:07 -04:00
|
|
|
virtual bool OnNextBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, eBlockFace a_EntryFace) override
|
2013-08-27 13:57:37 -04:00
|
|
|
{
|
2013-09-07 15:56:22 -04:00
|
|
|
/*
|
|
|
|
// DEBUG:
|
2018-09-24 16:33:39 -04:00
|
|
|
FLOGD("Hit block {0}:{1} at {2} face {3}, {4} ({5})",
|
2013-09-07 15:56:22 -04:00
|
|
|
a_BlockType, a_BlockMeta,
|
2018-09-24 16:33:39 -04:00
|
|
|
Vector3i{a_BlockX, a_BlockY, a_BlockZ}, a_EntryFace,
|
2014-03-01 14:34:19 -05:00
|
|
|
cBlockInfo::IsSolid(a_BlockType) ? "solid" : "non-solid",
|
2018-09-24 16:33:39 -04:00
|
|
|
ItemToString(cItem(a_BlockType, 1, a_BlockMeta))
|
2013-09-07 15:56:22 -04:00
|
|
|
);
|
|
|
|
*/
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2014-03-01 14:34:19 -05:00
|
|
|
if (cBlockInfo::IsSolid(a_BlockType))
|
2013-08-27 13:57:37 -04:00
|
|
|
{
|
2014-06-22 15:44:01 -04:00
|
|
|
// The projectile hit a solid block, calculate the exact hit coords:
|
2020-05-08 05:04:07 -04:00
|
|
|
cBoundingBox bb(a_BlockPos, a_BlockPos + Vector3i(1, 1, 1)); // Bounding box of the block hit
|
2014-07-07 16:14:15 -04:00
|
|
|
const Vector3d LineStart = m_Projectile->GetPosition(); // Start point for the imaginary line that goes through the block hit
|
|
|
|
const Vector3d LineEnd = LineStart + m_Projectile->GetSpeed(); // End point for the imaginary line that goes through the block hit
|
|
|
|
double LineCoeff = 0; // Used to calculate where along the line an intersection with the bounding box occurs
|
|
|
|
eBlockFace Face; // Face hit
|
2014-06-22 15:44:01 -04:00
|
|
|
|
|
|
|
if (bb.CalcLineIntersection(LineStart, LineEnd, LineCoeff, Face))
|
2013-09-03 03:41:31 -04:00
|
|
|
{
|
2014-07-07 16:14:15 -04:00
|
|
|
Vector3d Intersection = LineStart + m_Projectile->GetSpeed() * LineCoeff; // Point where projectile goes into the hit block
|
2014-06-22 15:44:01 -04:00
|
|
|
|
2020-05-08 05:04:07 -04:00
|
|
|
if (cPluginManager::Get()->CallHookProjectileHitBlock(*m_Projectile, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, Face, Intersection))
|
2014-03-29 11:00:45 -04:00
|
|
|
{
|
2014-03-29 12:05:24 -04:00
|
|
|
return false;
|
2014-03-29 11:00:45 -04:00
|
|
|
}
|
|
|
|
|
2013-09-03 03:41:31 -04:00
|
|
|
m_Projectile->OnHitSolidBlock(Intersection, Face);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD("WEIRD! block tracer reports a hit, but BBox tracer doesn't. Ignoring the hit.");
|
|
|
|
}
|
2013-08-27 13:57:37 -04:00
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-08-27 13:57:37 -04:00
|
|
|
// Convey some special effects from special blocks:
|
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_LAVA:
|
|
|
|
case E_BLOCK_STATIONARY_LAVA:
|
|
|
|
{
|
|
|
|
m_Projectile->StartBurning(30);
|
2013-09-02 15:56:55 -04:00
|
|
|
m_SlowdownCoeff = std::min(m_SlowdownCoeff, 0.9); // Slow down to 0.9* the speed each tick when moving through lava
|
2013-08-27 13:57:37 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case E_BLOCK_WATER:
|
|
|
|
case E_BLOCK_STATIONARY_WATER:
|
|
|
|
{
|
|
|
|
m_Projectile->StopBurning();
|
2013-09-02 15:56:55 -04:00
|
|
|
m_SlowdownCoeff = std::min(m_SlowdownCoeff, 0.8); // Slow down to 0.8* the speed each tick when moving through water
|
2013-08-27 13:57:37 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // switch (a_BlockType)
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-08-27 13:57:37 -04:00
|
|
|
// Continue tracing
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} ;
|
2013-08-22 02:55:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-09-01 16:40:35 -04:00
|
|
|
// cProjectileEntityCollisionCallback:
|
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
class cProjectileEntityCollisionCallback
|
2013-09-01 16:40:35 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
cProjectileEntityCollisionCallback(cProjectileEntity * a_Projectile, const Vector3d & a_Pos, const Vector3d & a_NextPos) :
|
|
|
|
m_Projectile(a_Projectile),
|
|
|
|
m_Pos(a_Pos),
|
|
|
|
m_NextPos(a_NextPos),
|
|
|
|
m_MinCoeff(1),
|
2014-10-20 16:55:07 -04:00
|
|
|
m_HitEntity(nullptr)
|
2013-09-01 16:40:35 -04:00
|
|
|
{
|
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
bool operator () (cEntity & a_Entity)
|
2013-09-01 16:40:35 -04:00
|
|
|
{
|
2013-09-02 15:40:56 -04:00
|
|
|
if (
|
2017-09-11 17:20:49 -04:00
|
|
|
(&a_Entity == m_Projectile) || // Do not check collisions with self
|
|
|
|
(a_Entity.GetUniqueID() == m_Projectile->GetCreatorUniqueID()) // Do not check whoever shot the projectile
|
2013-09-02 15:40:56 -04:00
|
|
|
)
|
2013-09-01 16:40:35 -04:00
|
|
|
{
|
2014-07-20 04:43:07 -04:00
|
|
|
// Don't check creator only for the first 5 ticks so that projectiles can collide with the creator
|
|
|
|
if (m_Projectile->GetTicksAlive() <= 5)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-01 16:40:35 -04:00
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2020-05-03 16:04:33 -04:00
|
|
|
auto EntBox = a_Entity.GetBoundingBox();
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-09-01 16:40:35 -04:00
|
|
|
// Instead of colliding the bounding box with another bounding box in motion, we collide an enlarged bounding box with a hairline.
|
|
|
|
// The results should be good enough for our purposes
|
|
|
|
double LineCoeff;
|
2014-02-04 13:59:05 -05:00
|
|
|
eBlockFace Face;
|
2013-09-01 16:40:35 -04:00
|
|
|
EntBox.Expand(m_Projectile->GetWidth() / 2, m_Projectile->GetHeight() / 2, m_Projectile->GetWidth() / 2);
|
|
|
|
if (!EntBox.CalcLineIntersection(m_Pos, m_NextPos, LineCoeff, Face))
|
|
|
|
{
|
|
|
|
// No intersection whatsoever
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-12 08:38:45 -04:00
|
|
|
if (
|
2017-09-11 17:20:49 -04:00
|
|
|
!a_Entity.IsMob() &&
|
|
|
|
!a_Entity.IsMinecart() &&
|
2016-10-12 08:38:45 -04:00
|
|
|
(
|
2017-09-11 17:20:49 -04:00
|
|
|
!a_Entity.IsPlayer() ||
|
|
|
|
static_cast<cPlayer &>(a_Entity).IsGameModeSpectator()
|
2016-10-12 08:38:45 -04:00
|
|
|
) &&
|
2020-11-06 10:00:59 -05:00
|
|
|
!a_Entity.IsBoat() &&
|
|
|
|
!a_Entity.IsEnderCrystal()
|
2016-10-12 08:38:45 -04:00
|
|
|
)
|
2014-06-22 15:44:01 -04:00
|
|
|
{
|
|
|
|
// Not an entity that interacts with a projectile
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
if (cPluginManager::Get()->CallHookProjectileHitEntity(*m_Projectile, a_Entity))
|
2014-03-29 10:26:41 -04:00
|
|
|
{
|
|
|
|
// A plugin disagreed.
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-09-01 16:40:35 -04:00
|
|
|
if (LineCoeff < m_MinCoeff)
|
|
|
|
{
|
|
|
|
// The entity is closer than anything we've stored so far, replace it as the potential victim
|
|
|
|
m_MinCoeff = LineCoeff;
|
2017-09-11 17:20:49 -04:00
|
|
|
m_HitEntity = &a_Entity;
|
2013-09-01 16:40:35 -04:00
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-09-01 16:40:35 -04:00
|
|
|
// Don't break the enumeration, we want all the entities
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns the nearest entity that was hit, after the enumeration has been completed */
|
2013-09-01 16:40:35 -04:00
|
|
|
cEntity * GetHitEntity(void) const { return m_HitEntity; }
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns the line coeff where the hit was encountered, after the enumeration has been completed */
|
2013-09-01 16:40:35 -04:00
|
|
|
double GetMinCoeff(void) const { return m_MinCoeff; }
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns true if the callback has encountered a true hit */
|
2013-09-01 16:40:35 -04:00
|
|
|
bool HasHit(void) const { return (m_MinCoeff < 1); }
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-09-01 16:40:35 -04:00
|
|
|
protected:
|
|
|
|
cProjectileEntity * m_Projectile;
|
|
|
|
const Vector3d & m_Pos;
|
|
|
|
const Vector3d & m_NextPos;
|
|
|
|
double m_MinCoeff; // The coefficient of the nearest hit on the Pos line
|
|
|
|
|
|
|
|
// Although it's bad(tm) to store entity ptrs from a callback, we can afford it here, because the entire callback
|
|
|
|
// is processed inside the tick thread, so the entities won't be removed in between the calls and the final processing
|
|
|
|
cEntity * m_HitEntity; // The nearest hit entity
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-08-22 02:55:58 -04:00
|
|
|
// cProjectileEntity:
|
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, Vector3d a_Pos, double a_Width, double a_Height):
|
2020-04-13 12:38:06 -04:00
|
|
|
Super(etProjectile, a_Pos, a_Width, a_Height),
|
2013-08-22 16:31:15 -04:00
|
|
|
m_ProjectileKind(a_Kind),
|
2014-07-04 17:07:26 -04:00
|
|
|
m_CreatorData(
|
2015-03-21 10:18:17 -04:00
|
|
|
((a_Creator != nullptr) ? a_Creator->GetUniqueID() : cEntity::INVALID_ID),
|
2015-05-24 07:56:56 -04:00
|
|
|
((a_Creator != nullptr) ? (a_Creator->IsPlayer() ? static_cast<cPlayer *>(a_Creator)->GetName() : "") : ""),
|
2014-10-20 16:55:07 -04:00
|
|
|
((a_Creator != nullptr) ? a_Creator->GetEquippedWeapon().m_Enchantments : cEnchantments())
|
2014-07-04 17:07:26 -04:00
|
|
|
),
|
2013-08-27 13:57:37 -04:00
|
|
|
m_IsInGround(false)
|
2013-08-22 02:55:58 -04:00
|
|
|
{
|
2015-03-30 19:42:32 -04:00
|
|
|
SetGravity(-12.0f);
|
2015-03-31 11:03:35 -04:00
|
|
|
SetAirDrag(0.01f);
|
2013-08-22 02:55:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, Vector3d a_Pos, Vector3d a_Speed, double a_Width, double a_Height):
|
2020-04-18 20:10:14 -04:00
|
|
|
cProjectileEntity(a_Kind, a_Creator, a_Pos, a_Width, a_Height)
|
2013-08-22 02:55:58 -04:00
|
|
|
{
|
|
|
|
SetSpeed(a_Speed);
|
2014-01-16 14:00:49 -05:00
|
|
|
SetYawFromSpeed();
|
2013-09-07 11:14:37 -04:00
|
|
|
SetPitchFromSpeed();
|
2013-08-22 02:55:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
std::unique_ptr<cProjectileEntity> cProjectileEntity::Create(
|
|
|
|
eKind a_Kind,
|
|
|
|
cEntity * a_Creator,
|
|
|
|
Vector3d a_Pos,
|
|
|
|
const cItem * a_Item,
|
|
|
|
const Vector3d * a_Speed
|
|
|
|
)
|
2013-08-22 02:55:58 -04:00
|
|
|
{
|
|
|
|
Vector3d Speed;
|
2014-10-20 16:55:07 -04:00
|
|
|
if (a_Speed != nullptr)
|
2013-08-22 02:55:58 -04:00
|
|
|
{
|
|
|
|
Speed = *a_Speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (a_Kind)
|
|
|
|
{
|
2020-08-01 14:18:03 -04:00
|
|
|
case pkArrow: return std::make_unique<cArrowEntity> (a_Creator, a_Pos, Speed);
|
|
|
|
case pkEgg: return std::make_unique<cThrownEggEntity> (a_Creator, a_Pos, Speed);
|
|
|
|
case pkEnderPearl: return std::make_unique<cThrownEnderPearlEntity>(a_Creator, a_Pos, Speed);
|
|
|
|
case pkSnowball: return std::make_unique<cThrownSnowballEntity> (a_Creator, a_Pos, Speed);
|
|
|
|
case pkGhastFireball: return std::make_unique<cGhastFireballEntity> (a_Creator, a_Pos, Speed);
|
|
|
|
case pkFireCharge: return std::make_unique<cFireChargeEntity> (a_Creator, a_Pos, Speed);
|
|
|
|
case pkExpBottle: return std::make_unique<cExpBottleEntity> (a_Creator, a_Pos, Speed);
|
|
|
|
case pkSplashPotion: return std::make_unique<cSplashPotionEntity> (a_Creator, a_Pos, Speed, *a_Item);
|
|
|
|
case pkWitherSkull: return std::make_unique<cWitherSkullEntity> (a_Creator, a_Pos, Speed);
|
2014-02-26 18:29:14 -05:00
|
|
|
case pkFirework:
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT(a_Item != nullptr);
|
2014-07-13 09:09:08 -04:00
|
|
|
if (a_Item->m_FireworkItem.m_Colours.empty())
|
2014-02-26 18:29:14 -05:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
return nullptr;
|
2014-02-26 18:29:14 -05:00
|
|
|
}
|
|
|
|
|
2020-08-01 14:18:03 -04:00
|
|
|
return std::make_unique<cFireworkEntity>(a_Creator, a_Pos, *a_Item);
|
2014-02-26 18:29:14 -05:00
|
|
|
}
|
2013-08-22 02:55:58 -04:00
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-08-30 12:10:58 -04:00
|
|
|
LOGWARNING("%s: Unknown projectile kind: %d", __FUNCTION__, a_Kind);
|
2014-10-20 16:55:07 -04:00
|
|
|
return nullptr;
|
2013-08-22 02:55:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-07 04:25:34 -04:00
|
|
|
void cProjectileEntity::OnHitSolidBlock(Vector3d a_HitPos, eBlockFace a_HitFace)
|
2013-08-27 13:57:37 -04:00
|
|
|
{
|
2013-09-03 03:41:31 -04:00
|
|
|
// Set the position based on what face was hit:
|
|
|
|
SetPosition(a_HitPos);
|
2013-08-27 13:57:37 -04:00
|
|
|
SetSpeed(0, 0, 0);
|
|
|
|
|
|
|
|
// DEBUG:
|
2018-09-24 16:33:39 -04:00
|
|
|
FLOGD("Projectile {0}: pos {1:.02f}, hit solid block at face {2}",
|
|
|
|
m_UniqueID, a_HitPos, a_HitFace
|
2013-08-27 13:57:37 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
m_IsInGround = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-07 04:25:34 -04:00
|
|
|
void cProjectileEntity::OnHitEntity(cEntity & a_EntityHit, Vector3d a_HitPos)
|
2016-01-11 14:34:41 -05:00
|
|
|
{
|
2016-01-22 13:55:46 -05:00
|
|
|
UNUSED(a_HitPos);
|
2016-01-16 07:16:47 -05:00
|
|
|
|
2016-01-22 13:55:46 -05:00
|
|
|
// If we were created by a player and we hit a pawn, notify attacking player's wolves
|
|
|
|
if (a_EntityHit.IsPawn() && (GetCreatorName() != ""))
|
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
auto EntityHit = static_cast<cPawn *>(&a_EntityHit);
|
|
|
|
m_World->DoWithEntityByID(GetCreatorUniqueID(), [=](cEntity & a_Hitter)
|
2016-01-11 14:34:41 -05:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
static_cast<cPlayer&>(a_Hitter).NotifyNearbyWolves(EntityHit, true);
|
2016-01-11 14:34:41 -05:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-11 17:20:49 -04:00
|
|
|
);
|
2016-01-11 14:34:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-22 16:31:15 -04:00
|
|
|
AString cProjectileEntity::GetMCAClassName(void) const
|
|
|
|
{
|
|
|
|
switch (m_ProjectileKind)
|
|
|
|
{
|
|
|
|
case pkArrow: return "Arrow";
|
|
|
|
case pkSnowball: return "Snowball";
|
|
|
|
case pkEgg: return "Egg";
|
|
|
|
case pkGhastFireball: return "Fireball";
|
|
|
|
case pkFireCharge: return "SmallFireball";
|
2014-07-04 11:49:24 -04:00
|
|
|
case pkEnderPearl: return "ThrownEnderpearl";
|
2013-08-22 16:31:15 -04:00
|
|
|
case pkExpBottle: return "ThrownExpBottle";
|
2014-07-11 21:58:11 -04:00
|
|
|
case pkSplashPotion: return "SplashPotion";
|
2013-08-22 16:31:15 -04:00
|
|
|
case pkWitherSkull: return "WitherSkull";
|
2014-02-26 18:29:14 -05:00
|
|
|
case pkFirework: return "Firework";
|
2013-08-22 16:31:15 -04:00
|
|
|
}
|
2018-02-04 18:07:12 -05:00
|
|
|
UNREACHABLE("Unsupported projectile kind");
|
2013-08-22 16:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-11 16:12:26 -05:00
|
|
|
void cProjectileEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
|
2013-08-27 13:57:37 -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-03-20 18:32:32 -04:00
|
|
|
BroadcastMovementUpdate();
|
2013-08-27 13:57:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-11 16:12:26 -05:00
|
|
|
void cProjectileEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
|
2013-08-27 13:57:37 -04:00
|
|
|
{
|
|
|
|
if (m_IsInGround)
|
|
|
|
{
|
|
|
|
// Already-grounded projectiles don't move at all
|
|
|
|
return;
|
|
|
|
}
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2015-03-30 20:07:19 -04:00
|
|
|
auto DtSec = std::chrono::duration_cast<std::chrono::duration<double>>(a_Dt);
|
|
|
|
|
|
|
|
const Vector3d DeltaSpeed = GetSpeed() * DtSec.count();
|
2014-06-22 15:44:01 -04:00
|
|
|
const Vector3d Pos = GetPosition();
|
2015-03-30 20:07:19 -04:00
|
|
|
const Vector3d NextPos = Pos + DeltaSpeed;
|
2014-06-22 15:44:01 -04:00
|
|
|
|
2013-09-01 16:40:35 -04:00
|
|
|
// Test for entity collisions:
|
|
|
|
cProjectileEntityCollisionCallback EntityCollisionCallback(this, Pos, NextPos);
|
|
|
|
a_Chunk.ForEachEntity(EntityCollisionCallback);
|
|
|
|
if (EntityCollisionCallback.HasHit())
|
2013-08-27 13:57:37 -04:00
|
|
|
{
|
2013-09-01 16:40:35 -04:00
|
|
|
// An entity was hit:
|
|
|
|
Vector3d HitPos = Pos + (NextPos - Pos) * EntityCollisionCallback.GetMinCoeff();
|
2013-09-07 12:53:14 -04:00
|
|
|
|
|
|
|
// DEBUG:
|
2018-09-24 16:33:39 -04:00
|
|
|
FLOGD("Projectile {0} has hit an entity {1} ({2}) at {3:.02f} (coeff {4:.03f})",
|
2013-09-01 16:40:35 -04:00
|
|
|
m_UniqueID,
|
|
|
|
EntityCollisionCallback.GetHitEntity()->GetUniqueID(),
|
|
|
|
EntityCollisionCallback.GetHitEntity()->GetClass(),
|
2018-09-24 16:33:39 -04:00
|
|
|
HitPos,
|
2013-09-01 16:40:35 -04:00
|
|
|
EntityCollisionCallback.GetMinCoeff()
|
|
|
|
);
|
2014-06-22 15:44:01 -04:00
|
|
|
|
2013-09-07 12:53:14 -04:00
|
|
|
OnHitEntity(*(EntityCollisionCallback.GetHitEntity()), HitPos);
|
2016-10-09 11:22:34 -04:00
|
|
|
if (!IsTicking())
|
|
|
|
{
|
|
|
|
return; // We were destroyed by an override of OnHitEntity
|
|
|
|
}
|
2013-08-27 13:57:37 -04:00
|
|
|
}
|
2013-09-01 16:40:35 -04:00
|
|
|
// TODO: Test the entities in the neighboring chunks, too
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2014-06-22 15:44:01 -04:00
|
|
|
// Trace the tick's worth of movement as a line:
|
|
|
|
cProjectileTracerCallback TracerCallback(this);
|
|
|
|
if (!cLineBlockTracer::Trace(*m_World, TracerCallback, Pos, NextPos))
|
|
|
|
{
|
|
|
|
// Something has been hit, abort all other processing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The tracer also checks the blocks for slowdown blocks - water and lava - and stores it for later in its SlowdownCoeff
|
2013-09-01 16:40:35 -04:00
|
|
|
|
|
|
|
// Update the position:
|
|
|
|
SetPosition(NextPos);
|
2016-01-11 14:34:41 -05:00
|
|
|
|
2013-09-02 15:56:55 -04:00
|
|
|
// Add slowdown and gravity effect to the speed:
|
|
|
|
Vector3d NewSpeed(GetSpeed());
|
2015-03-30 20:07:19 -04:00
|
|
|
NewSpeed.y += m_Gravity * DtSec.count();
|
2015-03-31 11:03:35 -04:00
|
|
|
NewSpeed -= NewSpeed * (m_AirDrag * 20.0f) * DtSec.count();
|
2013-09-02 15:56:55 -04:00
|
|
|
SetSpeed(NewSpeed);
|
2014-01-16 14:00:49 -05:00
|
|
|
SetYawFromSpeed();
|
2013-09-07 11:14:37 -04:00
|
|
|
SetPitchFromSpeed();
|
2013-08-27 13:57:37 -04:00
|
|
|
|
2014-04-05 16:25:40 -04:00
|
|
|
/*
|
2018-09-24 16:33:39 -04:00
|
|
|
FLOGD("Projectile {0}: pos {1:.02f}, speed {2:.02f}, rot {{{3:.02f}, {4:.02f}}}",
|
|
|
|
m_UniqueID, GetPos(), GetSpeed(), GetYaw(), GetPitch()
|
2013-08-27 13:57:37 -04:00
|
|
|
);
|
2014-04-05 16:25:40 -04:00
|
|
|
*/
|
2013-08-27 13:57:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-30 12:10:58 -04:00
|
|
|
void cProjectileEntity::SpawnOn(cClientHandle & a_Client)
|
|
|
|
{
|
2020-04-20 15:46:04 -04:00
|
|
|
a_Client.SendSpawnEntity(*this);
|
2013-09-07 11:14:37 -04:00
|
|
|
a_Client.SendEntityMetadata(*this);
|
2013-08-30 12:10:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-15 13:01:55 -04:00
|
|
|
void cProjectileEntity::CollectedBy(cPlayer & a_Dest)
|
2013-11-12 16:43:20 -05:00
|
|
|
{
|
|
|
|
// Overriden in arrow
|
|
|
|
UNUSED(a_Dest);
|
2014-04-27 12:42:31 -04:00
|
|
|
}
|
2014-10-15 13:01:55 -04:00
|
|
|
|