1
0
Fork 0
cuberite-2a/src/Entities/Pawn.cpp

492 lines
14 KiB
C++
Raw Normal View History

2017-09-19 08:34:08 +00:00
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Pawn.h"
2015-11-10 13:02:07 +00:00
#include "Player.h"
#include "../World.h"
2014-06-13 10:47:01 +00:00
#include "../Bindings/PluginManager.h"
#include "BoundingBox.h"
2015-11-10 13:02:07 +00:00
#include "../Blocks/BlockHandler.h"
#include "EffectID.h"
#include "../Mobs/Monster.h"
2014-09-01 18:12:56 +00:00
cPawn::cPawn(eEntityType a_EntityType, double a_Width, double a_Height) :
2015-11-10 13:02:07 +00:00
super(a_EntityType, 0, 0, 0, a_Width, a_Height),
m_EntityEffects(tEffectMap()),
m_LastGroundHeight(0),
m_bTouchGround(false)
{
SetGravity(-32.0f);
SetAirDrag(0.02f);
}
cPawn::~cPawn()
{
ASSERT(m_TargetingMe.size() == 0);
}
void cPawn::Destroyed()
{
StopEveryoneFromTargetingMe();
super::Destroyed();
}
void cPawn::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
std::vector<cEntityEffect *> EffectsToTick;
// Iterate through this entity's applied effects
for (tEffectMap::iterator iter = m_EntityEffects.begin(); iter != m_EntityEffects.end();)
{
// Copies values to prevent pesky wrong accesses and erasures
cEntityEffect::eType EffectType = iter->first;
2017-08-01 17:51:43 +00:00
cEntityEffect * Effect = iter->second.get();
// Iterates (must be called before any possible erasure)
++iter;
// Remove effect if duration has elapsed
if (Effect->GetDuration() - Effect->GetTicks() <= 0)
{
RemoveEntityEffect(EffectType);
}
// Call OnTick later to make sure the iterator won't be invalid
else
{
EffectsToTick.push_back(Effect);
}
// TODO: Check for discrepancies between client and server effect values
}
for (auto * Effect : EffectsToTick)
{
Effect->OnTick(*this);
}
// Spectators cannot push entities around
if ((!IsPlayer()) || (!static_cast<cPlayer *>(this)->IsGameModeSpectator()))
{
m_World->ForEachEntityInBox(cBoundingBox(GetPosition(), GetWidth(), GetHeight()), [=](cEntity & a_Entity)
{
if (a_Entity.GetUniqueID() == GetUniqueID())
{
return false;
}
// we only push other mobs, boats and minecarts
if ((a_Entity.GetEntityType() != etMonster) && (a_Entity.GetEntityType() != etMinecart) && (a_Entity.GetEntityType() != etBoat))
{
return false;
}
// do not push a boat / minecart you're sitting in
if (IsAttachedTo(&a_Entity))
{
return false;
}
Vector3d v3Delta = a_Entity.GetPosition() - GetPosition();
v3Delta.y = 0.0; // we only push sideways
v3Delta *= 1.0 / (v3Delta.Length() + 0.01); // we push harder if we're close
// QUESTION: is there an additional multiplier for this? current shoving seems a bit weak
a_Entity.AddSpeed(v3Delta);
return false;
}
);
2016-10-12 12:38:45 +00:00
}
2015-11-10 13:02:07 +00:00
super::Tick(a_Dt, a_Chunk);
if (!IsTicking())
{
// The base class tick destroyed us
return;
}
2015-11-10 13:02:07 +00:00
HandleFalling();
}
void cPawn::KilledBy(TakeDamageInfo & a_TDI)
{
ClearEntityEffects();
super::KilledBy(a_TDI);
}
bool cPawn::IsFireproof(void) const
{
return super::IsFireproof() || HasEntityEffect(cEntityEffect::effFireResistance);
}
2017-08-01 17:51:43 +00:00
bool cPawn::IsInvisible() const
{
return HasEntityEffect(cEntityEffect::effInvisibility);
}
void cPawn::HandleAir(void)
{
if (IsSubmerged() && HasEntityEffect(cEntityEffect::effWaterBreathing))
{
// Prevent the oxygen from decreasing
return;
}
super::HandleAir();
}
void cPawn::AddEntityEffect(cEntityEffect::eType a_EffectType, int a_Duration, short a_Intensity, double a_DistanceModifier)
{
2014-06-13 10:47:01 +00:00
// Check if the plugins allow the addition:
if (cPluginManager::Get()->CallHookEntityAddEffect(*this, a_EffectType, a_Duration, a_Intensity, a_DistanceModifier))
2014-06-13 10:47:01 +00:00
{
// A plugin disallows the addition, bail out.
return;
}
2014-06-13 10:47:01 +00:00
// No need to add empty effects:
if (a_EffectType == cEntityEffect::effNoEffect)
{
return;
}
2015-05-24 11:56:56 +00:00
a_Duration = static_cast<int>(a_Duration * a_DistanceModifier);
// If we already have the effect, we have to deactivate it or else it will act cumulatively
auto ExistingEffect = m_EntityEffects.find(a_EffectType);
if (ExistingEffect != m_EntityEffects.end())
{
ExistingEffect->second->OnDeactivate(*this);
}
2017-08-01 17:51:43 +00:00
auto Res = m_EntityEffects.emplace(a_EffectType, cEntityEffect::CreateEntityEffect(a_EffectType, a_Duration, a_Intensity, a_DistanceModifier));
m_World->BroadcastEntityEffect(*this, a_EffectType, a_Intensity, static_cast<short>(a_Duration));
2017-08-01 17:51:43 +00:00
cEntityEffect * Effect = Res.first->second.get();
Effect->OnActivate(*this);
}
void cPawn::RemoveEntityEffect(cEntityEffect::eType a_EffectType)
{
m_World->BroadcastRemoveEntityEffect(*this, a_EffectType);
2017-08-01 17:51:43 +00:00
auto itr = m_EntityEffects.find(a_EffectType);
if (itr != m_EntityEffects.end())
{
// Erase from effect map before calling OnDeactivate to allow metadata broadcasts (e.g. for invisibility effect)
auto Effect = std::move(itr->second);
m_EntityEffects.erase(itr);
Effect->OnDeactivate(*this);
}
}
bool cPawn::HasEntityEffect(cEntityEffect::eType a_EffectType) const
{
return m_EntityEffects.find(a_EffectType) != m_EntityEffects.end();
}
void cPawn::ClearEntityEffects()
{
// Iterate through this entity's applied effects
for (tEffectMap::iterator iter = m_EntityEffects.begin(); iter != m_EntityEffects.end();)
{
// Copy values to prevent pesky wrong erasures
cEntityEffect::eType EffectType = iter->first;
// Iterates (must be called before any possible erasure)
++iter;
// Remove effect
RemoveEntityEffect(EffectType);
}
}
2014-09-01 18:12:56 +00:00
2015-11-10 13:02:07 +00:00
void cPawn::NoLongerTargetingMe(cMonster * a_Monster)
{
ASSERT(IsTicking()); // Our destroy override is supposed to clear all targets before we're destroyed.
for (auto i = m_TargetingMe.begin(); i != m_TargetingMe.end(); ++i)
{
cMonster * Monster = *i;
if (Monster == a_Monster)
{
ASSERT(Monster->GetTarget() != this); // The monster is notifying us it is no longer targeting us, assert if that's a lie
m_TargetingMe.erase(i);
return;
}
}
ASSERT(false); // If this happens, something is wrong. Perhaps the monster never called TargetingMe() or called NoLongerTargetingMe() twice.
}
void cPawn::TargetingMe(cMonster * a_Monster)
{
ASSERT(IsTicking());
ASSERT(m_TargetingMe.size() < 10000);
ASSERT(a_Monster->GetTarget() == this);
m_TargetingMe.push_back(a_Monster);
}
2015-11-10 13:02:07 +00:00
void cPawn::HandleFalling(void)
{
/* Not pretty looking, and is more suited to wherever server-sided collision detection is implemented.
The following condition sets on-ground-ness if
The player isn't swimming or flying (client hardcoded conditions) and
they're on a block (Y is exact) - ensure any they could be standing on (including on the edges) is solid or
they're on a slab (Y significand is 0.5) - ditto with slab check
they're on a snow layer (Y divisible by 0.125) - ditto with snow layer check
*/
static const auto HalfWidth = GetWidth() / 2;
static const auto EPS = 0.0001;
/* Since swimming is decided in a tick and is asynchronous to this, we have to check for dampeners ourselves.
The behaviour as of 1.8.9 is the following:
- Landing in water alleviates all fall damage
- Passing through any liquid (water + lava) and cobwebs "slows" the player down,
i.e. resets the fall distance to that block, but only after checking for fall damage
(this means that plummeting into lava will still kill the player via fall damage, although cobwebs
will slow players down enough to have multiple updates that keep them alive)
- Slime blocks reverse falling velocity, unless it's a crouching player, in which case they act as standard blocks.
They also reset the topmost point of the damage calculation with each bounce,
so if the block is removed while the player is bouncing or crouches after a bounce, the last bounce's zenith is considered as fall damage.
With this in mind, we first check the block at the player's feet, then the one below that (because fences),
and decide which behaviour we want to go with.
*/
BLOCKTYPE BlockAtFoot = (cChunkDef::IsValidHeight(POSY_TOINT)) ? GetWorld()->GetBlock(POS_TOINT) : E_BLOCK_AIR;
/* We initialize these with what the foot is really IN, because for sampling we will move down with the epsilon above */
bool IsFootInWater = IsBlockWater(BlockAtFoot);
bool IsFootInLiquid = IsFootInWater || IsBlockLava(BlockAtFoot) || (BlockAtFoot == E_BLOCK_COBWEB); // okay so cobweb is not _technically_ a liquid...
bool IsFootOnSlimeBlock = false;
/* The "cross" we sample around to account for the player width/girth */
static const struct
{
int x, z;
} CrossSampleCoords[] =
{
{ 0, 0 },
{ 1, 0 },
{ -1, 0 },
{ 0, 1 },
{ 0, -1 },
};
/* The blocks we're interested in relative to the player to account for larger than 1 blocks.
This can be extended to do additional checks in case there are blocks that are represented as one block
in memory but have a hitbox larger than 1 (like fences) */
static const struct
{
int x, y, z;
} BlockSampleOffsets[] =
{
2016-06-01 22:46:24 +00:00
{ 0, 0, 0 }, // TODO: something went wrong here (offset 0?)
{ 0, -1, 0 }, // Potentially causes mis-detection (IsFootInWater) when player stands on block diagonal to water (i.e. on side of pool)
2015-11-10 13:02:07 +00:00
};
/* Here's the rough outline of how this mechanism works:
We take the player's pointlike position (sole of feet), and expand it into a crosslike shape.
If any of the five points hit a block, we consider the player to be "on" (or "in") the ground. */
bool OnGround = false;
for (size_t i = 0; i < ARRAYCOUNT(CrossSampleCoords); i++)
{
/* We calculate from the player's position, one of the cross-offsets above, and we move it down slightly so it's beyond inaccuracy.
The added advantage of this method is that if the player is simply standing on the floor,
the point will move into the next block, and the floor() will retrieve that instead of air. */
Vector3d CrossTestPosition = GetPosition() + Vector3d(CrossSampleCoords[i].x * HalfWidth, -EPS, CrossSampleCoords[i].z * HalfWidth);
/* We go through the blocks that we consider "relevant" */
for (size_t j = 0; j < ARRAYCOUNT(BlockSampleOffsets); j++)
{
Vector3i BlockTestPosition = CrossTestPosition.Floor() + Vector3i(BlockSampleOffsets[j].x, BlockSampleOffsets[j].y, BlockSampleOffsets[j].z);
if (!cChunkDef::IsValidHeight(BlockTestPosition.y))
{
continue;
}
BLOCKTYPE Block = GetWorld()->GetBlock(BlockTestPosition);
NIBBLETYPE BlockMeta = GetWorld()->GetBlockMeta(BlockTestPosition);
/* we do the cross-shaped sampling to check for water / liquids, but only on our level because water blocks are never bigger than unit voxels */
if (j == 0)
{
IsFootInWater |= IsBlockWater(Block);
IsFootInLiquid |= IsFootInWater || IsBlockLava(Block) || (Block == E_BLOCK_COBWEB); // okay so cobweb is not _technically_ a liquid...
IsFootOnSlimeBlock |= (Block == E_BLOCK_SLIME_BLOCK);
}
/* If the block is solid, and the blockhandler confirms the block to be inside, we're officially on the ground. */
if ((cBlockInfo::IsSolid(Block)) && (cBlockInfo::GetHandler(Block)->IsInsideBlock(CrossTestPosition - BlockTestPosition, Block, BlockMeta)))
{
OnGround = true;
}
}
}
/* So here's the use of the rules above: */
/* 1. Falling in water absorbs all fall damage */
bool FallDamageAbsorbed = IsFootInWater;
/* 2. Falling in liquid (lava, water, cobweb) or hitting a slime block resets the "fall zenith".
Note: Even though the pawn bounces back with no damage after hitting the slime block,
the "fall zenith" will continue to increase back up when flying upwards - which is good */
bool ShouldBounceOnSlime = true;
2016-06-01 22:46:24 +00:00
if (IsPlayer())
2015-11-10 13:02:07 +00:00
{
2016-06-01 22:46:24 +00:00
auto Player = static_cast<cPlayer *>(this);
2015-11-10 13:02:07 +00:00
/* 3. If the player is flying or climbing, absorb fall damage */
2016-06-01 22:46:24 +00:00
FallDamageAbsorbed |= Player->IsFlying() || Player->IsClimbing();
2015-11-10 13:02:07 +00:00
/* 4. If the player is about to bounce on a slime block and is not crouching, absorb all fall damage */
ShouldBounceOnSlime = !Player->IsCrouched();
FallDamageAbsorbed |= (IsFootOnSlimeBlock && ShouldBounceOnSlime);
}
else
{
/* 5. Bouncing on a slime block absorbs all fall damage */
FallDamageAbsorbed |= IsFootOnSlimeBlock;
}
/* If the player is not crouching or is not a player, shoot them back up.
NOTE: this will only work in some cases; should be done in HandlePhysics() */
if (IsFootOnSlimeBlock && ShouldBounceOnSlime)
{
2016-06-01 22:46:24 +00:00
// TODO: doesn't work too well, causes dissatisfactory experience for players on slime blocks - SetSpeedY(-GetSpeedY());
2015-11-10 13:02:07 +00:00
}
2016-06-01 22:46:24 +00:00
// TODO: put player speed into GetSpeedY, and use that.
// If flying, climbing, swimming, or going up...
if (FallDamageAbsorbed || ((GetPosition() - m_LastPosition).y > 0))
{
// ...update the ground height to have the highest position of the player (i.e. jumping up adds to the eventual fall damage)
m_LastGroundHeight = GetPosY();
}
if (OnGround)
2015-11-10 13:02:07 +00:00
{
auto Damage = static_cast<int>(m_LastGroundHeight - GetPosY() - 3.0);
if ((Damage > 0) && !FallDamageAbsorbed)
{
TakeDamage(dtFalling, nullptr, Damage, Damage, 0);
// Fall particles
2016-07-21 07:47:42 +00:00
// TODO: Re-enable this when effects in 1.9 aren't broken (right now this uses the wrong effect ID in 1.9 and the right one in 1.8)
1.9 / 1.9.2 / 1.9.3 / 1.9.4 protocol support (#3135) * Semistable update to 15w31a I'm going through snapshots in a sequential order since it should make things easier, and since protocol version history is written. * Update to 15w34b protocol Also, fix an issue with the Entity Equipment packet from the past version. Clients are able to connect and do stuff! * Partially update to 15w35e Chunk data doesn't work, but the client joins. I'm waiting to do chunk data because chunk data has an incomplete format until 15w36d. * Add '/blk' debug command This command lets one see what block they are looking at, and makes figuring out what's supposed to be where in a highly broken chunk possible. * Fix CRLF normalization in CheckBasicStyle.lua Normally, this doesn't cause an issue, but when running from cygwin, it detects the CR as whitespace and creates thousands of violations for every single line. Lua, when run on windows, will normalize automatically, but when run via cygwin, it won't. The bug was simply that gsub was returning a replaced version, but not changing the parameter, so the replaced version was ignored. * Update to 15w40b This includes chunk serialization. Fully functional chunk serialization for 1.9. I'm not completely happy with the chunk serialization as-is (correct use of palettes would be great), but cuberite also doesn't skip sending empty chunks so this performance optimization should probably come later. The creation of a full buffer is suboptimal, but it's the easiest way to implement this code. * Write long-by-long rather than creating a buffer This is a bit faster and should be equivalent. However, the code still doesn't look too good. * Update to 15w41a protocol This includes the new set passengers packet, which works off of the ridden entity, not the rider. That means, among other things, that information about the previously ridden vehicle is needed when detaching. So a new method with that info was added. * Update to 15w45a * 15w51b protocol * Update to 1.9.0 protocol Closes #3067. There are still a few things that need to be worked out (picking up items, effects, particles, and most importantly inventory), but in general this should work. I'll make a few more changes tomorrow to get the rest of the protocol set up, along with 1.9.1/1.9.2 (which did make a few changes). Chunks, however, _are_ working, along with most other parts of the game (placing/breaking blocks). * Fix item pickup packet not working That was a silly mistake, but at least it was an easy one. * 1.9.2 protocol support * Fix version info found in server list ping Thus, the client reports that it can connect rather than saying that the server is out of date. This required creating separate classes for 1.9.1 and 1.9.2, unfortunately. * Fix build errors generated by clang These didn't happen in MSVC. * Add protocol19x.cpp and protocol19x.h to CMakeLists * Ignore warnings in protocol19x that are ignored in protocol18x * Document BLOCK_FACE and DIG_STATUS constants * Fix BLOCK_FACE links and add separate section for DIG_STATUS * Fix bat animation and object spawning The causes of both of these are explained in #3135, but the gist is that both were typos. * Implement Use Item packet This means that buckets, bows, fishing rods, and several other similar items now work when not looking at a block. * Handle DIG_STATUS_SWAP_ITEM_IN_HAND * Add support for spawn eggs and potions The items are transformed from the 1.9 version to the 1.8 version when reading and transformed back when sending. * Remove spammy potion debug logging * Fix wolf collar color metadata The wrong type was being used, causing several clientside issues (including the screen going black). * Fix 1.9 chunk sending in the nether The nether and the end don't send skylight. * Fix clang build errors * Fix water bottles becoming mundane potions This happened because the can become splash potion bit got set incorrectly. Water bottles and mundane potions are only differentiated by the fact that water bottles have a metadata of 0, so setting that bit made it a mundane potion. Also add missing break statements to the read item NBT switch, which would otherwise break items with custom names and also cause incorrect "Unimplemented NBT data when parsing!" logging. * Copy Protocol18x as Protocol19x Aditionally, method and class names have been swapped to clean up other diffs. This commit is only added to make the following diffs more readable; it doesn't make any other changes (beyond class names). * Make thrown potions use the correct appearence This was caused by potions now using metadata. * Add missing api doc for cSplashPotionEntity::GetItem * Fix compile error in SplashPotionEntity.cpp * Fix fix of cSplashPotionEntity API doc * Temporarilly disable fall damage particles These were causing issues in 1.9 due to the changed effect ID. * Properly send a kick packet when connecting with an invalid version This means that the client no longer waits on the server screen with no indication whatsoever. However, right now the server list ping isn't implemented for unknown versions, so it'll only load "Old" on the ping. I also added a GetVarIntSize method to cByteBuffer. This helps clean up part of the code here (and I think it could clean up other parts), but it may make sense for it to be moved elsewhere (or declared in a different way). * Handle server list pings from unrecognized versions This isn't the cleanest way of writing it (it feels odd to use ProtocolRecognizer to send packets, and the addition of m_InPingForUnrecognizedVersion feels like the wrong technique), but it works and I can't think of a better way (apart from creating a full separate protocol class to handle only the ping... which would be worse). * Use cPacketizer for the disconnect packet This also should fix clang build errors. * Add 1.9.3 / 1.9.4 support * Fix incorrect indentation in APIDesc
2016-05-14 19:12:42 +00:00
// int ParticleSize = static_cast<int>((std::min(15, Damage) - 1.f) * ((50.f - 20.f) / (15.f - 1.f)) + 20.f);
// GetWorld()->BroadcastSoundParticleEffect(EffectID::PARTICLE_FALL_PARTICLES, POSX_TOINT, POSY_TOINT - 1, POSZ_TOINT, ParticleSize);
2015-11-10 13:02:07 +00:00
}
m_bTouchGround = true;
m_LastGroundHeight = GetPosY();
}
else
{
m_bTouchGround = false;
}
/* Note: it is currently possible to fall through lava and still die from fall damage
because of the client skipping an update about the lava block. This can only be resolved by
somehow integrating these above checks into the tracer in HandlePhysics. */
}
void cPawn::StopEveryoneFromTargetingMe()
{
std::vector<cMonster*>::iterator i = m_TargetingMe.begin();
while (i != m_TargetingMe.end())
{
cMonster * Monster = *i;
ASSERT(Monster->GetTarget() == this);
Monster->UnsafeUnsetTarget();
i = m_TargetingMe.erase(i);
}
ASSERT(m_TargetingMe.size() == 0);
}
std::map<cEntityEffect::eType, cEntityEffect *> cPawn::GetEntityEffects()
{
2017-08-01 17:51:43 +00:00
std::map<cEntityEffect::eType, cEntityEffect *> Effects;
for (auto & Effect : m_EntityEffects)
{
Effects.insert({ Effect.first, Effect.second.get() });
}
return Effects;
}
2017-08-01 17:51:43 +00:00
cEntityEffect * cPawn::GetEntityEffect(cEntityEffect::eType a_EffectType)
{
2017-08-01 17:51:43 +00:00
auto itr = m_EntityEffects.find(a_EffectType);
return (itr != m_EntityEffects.end()) ? itr->second.get() : nullptr;
}