From 33cc1f2a50d870b7d264ee5479068f8eee3aa458 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 22 Jun 2014 20:44:01 +0100 Subject: [PATCH 01/15] Fixed multiple issues with projectiles * Fixed arrows not being collectable/not truly hitting a block/not lodging into blocks/not going in far enough * Fixed projectiles not playing their block hit animation owning to being destroyed too quickly --- src/Entities/ArrowEntity.cpp | 33 ++++--------- src/Entities/ProjectileEntity.cpp | 61 ++++++++++++++----------- src/Entities/ThrownEggEntity.cpp | 7 +-- src/Entities/ThrownEggEntity.h | 18 ++++++++ src/Entities/ThrownEnderPearlEntity.cpp | 9 ++-- src/Entities/ThrownEnderPearlEntity.h | 20 +++++++- src/Entities/ThrownSnowballEntity.cpp | 7 +-- src/Entities/ThrownSnowballEntity.h | 18 ++++++++ 8 files changed, 111 insertions(+), 62 deletions(-) diff --git a/src/Entities/ArrowEntity.cpp b/src/Entities/ArrowEntity.cpp index 8d2569125..db9dc781a 100644 --- a/src/Entities/ArrowEntity.cpp +++ b/src/Entities/ArrowEntity.cpp @@ -68,25 +68,16 @@ bool cArrowEntity::CanPickup(const cPlayer & a_Player) const void cArrowEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) { - if (a_HitFace == BLOCK_FACE_NONE) { return; } + Vector3d Hit = a_HitPos; + Hit += GetSpeed() / 700; // Make arrow sink into block a little + + super::OnHitSolidBlock(Hit, a_HitFace); + int X = (int)floor(Hit.x), Y = (int)floor(Hit.y), Z = (int)floor(Hit.z); - super::OnHitSolidBlock(a_HitPos, a_HitFace); - int a_X = (int)a_HitPos.x, a_Y = (int)a_HitPos.y, a_Z = (int)a_HitPos.z; - - switch (a_HitFace) - { - case BLOCK_FACE_XM: // Strangely, bounding boxes / block tracers return the actual block for these two directions, so AddFace not needed - case BLOCK_FACE_YM: - { - break; - } - default: AddFaceDirection(a_X, a_Y, a_Z, a_HitFace, true); - } - - m_HitBlockPos = Vector3i(a_X, a_Y, a_Z); + m_HitBlockPos = Vector3i(X, Y, Z); // Broadcast arrow hit sound - m_World->BroadcastSoundEffect("random.bowhit", (int)GetPosX() * 8, (int)GetPosY() * 8, (int)GetPosZ() * 8, 0.5f, (float)(0.75 + ((float)((GetUniqueID() * 23) % 32)) / 64)); + m_World->BroadcastSoundEffect("random.bowhit", X * 8, Y * 8, Z * 8, 0.5f, (float)(0.75 + ((float)((GetUniqueID() * 23) % 32)) / 64)); } @@ -94,13 +85,7 @@ void cArrowEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFa void cArrowEntity::OnHitEntity(cEntity & a_EntityHit, const Vector3d & a_HitPos) -{ - if (!a_EntityHit.IsMob() && !a_EntityHit.IsMinecart() && !a_EntityHit.IsPlayer() && !a_EntityHit.IsBoat()) - { - // Not an entity that interacts with an arrow - return; - } - +{ int Damage = (int)(GetSpeed().Length() / 20 * m_DamageCoeff + 0.5); if (m_IsCritical) { @@ -165,7 +150,7 @@ void cArrowEntity::Tick(float a_Dt, cChunk & a_Chunk) if (!m_HasTeleported) // Sent a teleport already, don't do again { - if (m_HitGroundTimer > 1000.f) // Send after a second, could be less, but just in case + if (m_HitGroundTimer > 500.f) // Send after half a second, could be less, but just in case { m_World->BroadcastTeleportEntity(*this); m_HasTeleported = true; diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index 95c494569..d45f1d212 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -67,16 +67,17 @@ protected: if (cBlockInfo::IsSolid(a_BlockType)) { - // The projectile hit a solid block - // Calculate the exact hit coords: - cBoundingBox bb(a_BlockX, a_BlockX + 1, a_BlockY, a_BlockY + 1, a_BlockZ, a_BlockZ + 1); - Vector3d Line1 = m_Projectile->GetPosition(); - Vector3d Line2 = Line1 + m_Projectile->GetSpeed(); - double LineCoeff = 0; - eBlockFace Face; - if (bb.CalcLineIntersection(Line1, Line2, LineCoeff, Face)) + // The projectile hit a solid block, calculate the exact hit coords: + cBoundingBox bb(a_BlockX, a_BlockX + 1, a_BlockY, a_BlockY + 1, a_BlockZ, a_BlockZ + 1); // Bounding box of the block hit + 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 + + if (bb.CalcLineIntersection(LineStart, LineEnd, LineCoeff, Face)) { - Vector3d Intersection = Line1 + m_Projectile->GetSpeed() * LineCoeff; + Vector3d Intersection = LineStart + m_Projectile->GetSpeed() * LineCoeff; // Point where projectile goes into the hit block + if (cPluginManager::Get()->CallHookProjectileHitBlock(*m_Projectile, a_BlockX, a_BlockY, a_BlockZ, Face, &Intersection)) { return false; @@ -161,7 +162,12 @@ public: return false; } - // TODO: Some entities don't interact with the projectiles (pickups, falling blocks) + if (!a_Entity->IsMob() && !a_Entity->IsMinecart() && !a_Entity->IsPlayer() && !a_Entity->IsBoat()) + { + // Not an entity that interacts with a projectile + return false; + } + if (cPluginManager::Get()->CallHookProjectileHitEntity(*m_Projectile, *a_Entity)) { // A plugin disagreed. @@ -316,8 +322,9 @@ AString cProjectileEntity::GetMCAClassName(void) const void cProjectileEntity::Tick(float a_Dt, cChunk & a_Chunk) { super::Tick(a_Dt, a_Chunk); - - if (GetProjectileKind() != pkArrow) // See cArrow::Tick + + // TODO: see BroadcastMovementUpdate; RelativeMove packet jerkiness affects projectiles too (cause of sympton described in cArrowEntity::Tick()) + if (GetProjectileKind() != pkArrow) { BroadcastMovementUpdate(); } @@ -335,19 +342,10 @@ void cProjectileEntity::HandlePhysics(float a_Dt, cChunk & a_Chunk) return; } - Vector3d PerTickSpeed = GetSpeed() / 20; - Vector3d Pos = GetPosition(); - - // Trace the tick's worth of movement as a line: - Vector3d NextPos = Pos + PerTickSpeed; - 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 - + const Vector3d PerTickSpeed = GetSpeed() / 20; + const Vector3d Pos = GetPosition(); + const Vector3d NextPos = Pos + PerTickSpeed; + // Test for entity collisions: cProjectileEntityCollisionCallback EntityCollisionCallback(this, Pos, NextPos); a_Chunk.ForEachEntity(EntityCollisionCallback); @@ -363,11 +361,20 @@ void cProjectileEntity::HandlePhysics(float a_Dt, cChunk & a_Chunk) EntityCollisionCallback.GetHitEntity()->GetClass(), HitPos.x, HitPos.y, HitPos.z, EntityCollisionCallback.GetMinCoeff() - ); - + ); + OnHitEntity(*(EntityCollisionCallback.GetHitEntity()), HitPos); } // TODO: Test the entities in the neighboring chunks, too + + // 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 // Update the position: SetPosition(NextPos); diff --git a/src/Entities/ThrownEggEntity.cpp b/src/Entities/ThrownEggEntity.cpp index 224019091..d7eed41e3 100644 --- a/src/Entities/ThrownEggEntity.cpp +++ b/src/Entities/ThrownEggEntity.cpp @@ -8,7 +8,8 @@ cThrownEggEntity::cThrownEggEntity(cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d & a_Speed) : - super(pkEgg, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25) + super(pkEgg, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25), + m_DestroyTimer(-1) { SetSpeed(a_Speed); } @@ -21,7 +22,7 @@ void cThrownEggEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_H { TrySpawnChicken(a_HitPos); - Destroy(); + m_DestroyTimer = 5; } @@ -36,7 +37,7 @@ void cThrownEggEntity::OnHitEntity(cEntity & a_EntityHit, const Vector3d & a_Hit TrySpawnChicken(a_HitPos); a_EntityHit.TakeDamage(dtRangedAttack, this, TotalDamage, 1); - Destroy(true); + m_DestroyTimer = 5; } diff --git a/src/Entities/ThrownEggEntity.h b/src/Entities/ThrownEggEntity.h index 5ba8f051b..894665428 100644 --- a/src/Entities/ThrownEggEntity.h +++ b/src/Entities/ThrownEggEntity.h @@ -30,8 +30,26 @@ protected: // cProjectileEntity overrides: virtual void OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) override; virtual void OnHitEntity (cEntity & a_EntityHit, const Vector3d & a_HitPos) override; + virtual void Tick (float a_Dt, cChunk & a_Chunk) override + { + if (m_DestroyTimer > 0) + { + m_DestroyTimer--; + if (m_DestroyTimer == 0) + { + Destroy(); + return; + } + } + else { super::Tick(a_Dt, a_Chunk); } + } // Randomly decides whether to spawn a chicken where the egg lands. void TrySpawnChicken(const Vector3d & a_HitPos); + +private: + + /** Time in ticks to wait for the hit animation to begin before destroying */ + int m_DestroyTimer; } ; // tolua_export diff --git a/src/Entities/ThrownEnderPearlEntity.cpp b/src/Entities/ThrownEnderPearlEntity.cpp index c37161145..a3ee23389 100644 --- a/src/Entities/ThrownEnderPearlEntity.cpp +++ b/src/Entities/ThrownEnderPearlEntity.cpp @@ -7,7 +7,8 @@ cThrownEnderPearlEntity::cThrownEnderPearlEntity(cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d & a_Speed) : - super(pkEnderPearl, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25) + super(pkEnderPearl, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25), + m_DestroyTimer(-1) { SetSpeed(a_Speed); } @@ -21,7 +22,7 @@ void cThrownEnderPearlEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockF // TODO: Tweak a_HitPos based on block face. TeleportCreator(a_HitPos); - Destroy(); + m_DestroyTimer = 5; } @@ -36,7 +37,7 @@ void cThrownEnderPearlEntity::OnHitEntity(cEntity & a_EntityHit, const Vector3d TeleportCreator(a_HitPos); a_EntityHit.TakeDamage(dtRangedAttack, this, TotalDamage, 1); - Destroy(true); + m_DestroyTimer = 5; } @@ -48,7 +49,7 @@ void cThrownEnderPearlEntity::TeleportCreator(const Vector3d & a_HitPos) // Teleport the creator here, make them take 5 damage: if (m_Creator != NULL) { - m_Creator->TeleportToCoords(a_HitPos.x + 0.5, a_HitPos.y + 1.7, a_HitPos.z + 0.5); + m_Creator->TeleportToCoords(a_HitPos.x, a_HitPos.y + 0.2, a_HitPos.z); m_Creator->TakeDamage(dtEnderPearl, this, 5, 0); } } diff --git a/src/Entities/ThrownEnderPearlEntity.h b/src/Entities/ThrownEnderPearlEntity.h index ddee5babe..bfd9bd70d 100644 --- a/src/Entities/ThrownEnderPearlEntity.h +++ b/src/Entities/ThrownEnderPearlEntity.h @@ -30,8 +30,26 @@ protected: // cProjectileEntity overrides: virtual void OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) override; virtual void OnHitEntity (cEntity & a_EntityHit, const Vector3d & a_HitPos) override; + virtual void Tick (float a_Dt, cChunk & a_Chunk) override + { + if (m_DestroyTimer > 0) + { + m_DestroyTimer--; + if (m_DestroyTimer == 0) + { + Destroy(); + return; + } + } + else { super::Tick(a_Dt, a_Chunk); } + } - // Teleports the creator where the ender pearl lands. + /** Teleports the creator where the ender pearl lands */ void TeleportCreator(const Vector3d & a_HitPos); + +private: + + /** Time in ticks to wait for the hit animation to begin before destroying */ + int m_DestroyTimer; } ; // tolua_export diff --git a/src/Entities/ThrownSnowballEntity.cpp b/src/Entities/ThrownSnowballEntity.cpp index cefc3433c..b82cd56db 100644 --- a/src/Entities/ThrownSnowballEntity.cpp +++ b/src/Entities/ThrownSnowballEntity.cpp @@ -8,7 +8,8 @@ cThrownSnowballEntity::cThrownSnowballEntity(cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d & a_Speed) : - super(pkSnowball, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25) + super(pkSnowball, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25), + m_DestroyTimer(-1) { SetSpeed(a_Speed); } @@ -19,7 +20,7 @@ cThrownSnowballEntity::cThrownSnowballEntity(cEntity * a_Creator, double a_X, do void cThrownSnowballEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) { - Destroy(); + m_DestroyTimer = 5; } @@ -40,5 +41,5 @@ void cThrownSnowballEntity::OnHitEntity(cEntity & a_EntityHit, const Vector3d & // TODO: If entity is Ender Crystal, destroy it a_EntityHit.TakeDamage(dtRangedAttack, this, TotalDamage, 1); - Destroy(true); + m_DestroyTimer = 5; } diff --git a/src/Entities/ThrownSnowballEntity.h b/src/Entities/ThrownSnowballEntity.h index a09512e37..e30971f0a 100644 --- a/src/Entities/ThrownSnowballEntity.h +++ b/src/Entities/ThrownSnowballEntity.h @@ -30,5 +30,23 @@ protected: // cProjectileEntity overrides: virtual void OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) override; virtual void OnHitEntity (cEntity & a_EntityHit, const Vector3d & a_HitPos) override; + virtual void Tick (float a_Dt, cChunk & a_Chunk) override + { + if (m_DestroyTimer > 0) + { + m_DestroyTimer--; + if (m_DestroyTimer == 0) + { + Destroy(); + return; + } + } + else { super::Tick(a_Dt, a_Chunk); } + } + +private: + + /** Time in ticks to wait for the hit animation to begin before destroying */ + int m_DestroyTimer; } ; // tolua_export From 4238b0ebe8d910186d4e160222f337b6e8b33cc8 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 22 Jun 2014 20:44:18 +0100 Subject: [PATCH 02/15] Some Entity.cpp style improvements --- src/Entities/Entity.cpp | 17 ++++++----------- src/Entities/Entity.h | 4 ++-- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index ee7ce06ac..f4e89367b 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -255,8 +255,7 @@ void cEntity::TakeDamage(eDamageType a_DamageType, cEntity * a_Attacker, int a_R void cEntity::SetYawFromSpeed(void) { - const double EPS = 0.0000001; - if ((abs(m_Speed.x) < EPS) && (abs(m_Speed.z) < EPS)) + if ((abs(m_Speed.x) < std::numeric_limits::epsilon()) && (abs(m_Speed.z) < std::numeric_limits::epsilon())) { // atan2() may overflow or is undefined, pick any number SetYaw(0); @@ -1236,7 +1235,7 @@ void cEntity::BroadcastMovementUpdate(const cClientHandle * a_Exclude) if (GetWorld()->GetWorldAge() % 2 == 0) { double SpeedSqr = GetSpeed().SqrLength(); - if (SpeedSqr == 0.0) + if (SpeedSqr < std::numeric_limits::epsilon()) { // Speed is zero, send this to clients once only as well as an absolute position if (!m_bHasSentNoSpeed) @@ -1476,8 +1475,7 @@ void cEntity::SetWidth(double a_Width) void cEntity::AddPosX(double a_AddPosX) { - m_Pos.x += a_AddPosX; - + m_Pos.x += a_AddPosX; } @@ -1485,8 +1483,7 @@ void cEntity::AddPosX(double a_AddPosX) void cEntity::AddPosY(double a_AddPosY) { - m_Pos.y += a_AddPosY; - + m_Pos.y += a_AddPosY; } @@ -1494,8 +1491,7 @@ void cEntity::AddPosY(double a_AddPosY) void cEntity::AddPosZ(double a_AddPosZ) { - m_Pos.z += a_AddPosZ; - + m_Pos.z += a_AddPosZ; } @@ -1505,8 +1501,7 @@ void cEntity::AddPosition(double a_AddPosX, double a_AddPosY, double a_AddPosZ) { m_Pos.x += a_AddPosX; m_Pos.y += a_AddPosY; - m_Pos.z += a_AddPosZ; - + m_Pos.z += a_AddPosZ; } diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h index 2df66e353..f4080f8aa 100644 --- a/src/Entities/Entity.h +++ b/src/Entities/Entity.h @@ -237,9 +237,9 @@ public: void AddPosY (double a_AddPosY); void AddPosZ (double a_AddPosZ); void AddPosition(double a_AddPosX, double a_AddPosY, double a_AddPosZ); - void AddPosition(const Vector3d & a_AddPos) { AddPosition(a_AddPos.x,a_AddPos.y,a_AddPos.z);} + void AddPosition(const Vector3d & a_AddPos) { AddPosition(a_AddPos.x, a_AddPos.y, a_AddPos.z); } void AddSpeed (double a_AddSpeedX, double a_AddSpeedY, double a_AddSpeedZ); - void AddSpeed (const Vector3d & a_AddSpeed) { AddSpeed(a_AddSpeed.x,a_AddSpeed.y,a_AddSpeed.z);} + void AddSpeed (const Vector3d & a_AddSpeed) { AddSpeed(a_AddSpeed.x, a_AddSpeed.y, a_AddSpeed.z); } void AddSpeedX (double a_AddSpeedX); void AddSpeedY (double a_AddSpeedY); void AddSpeedZ (double a_AddSpeedZ); From dad0037f987df594d6a1971af5b29f89c2d27901 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 22 Jun 2014 20:44:55 +0100 Subject: [PATCH 03/15] Bettered zombie and skeleton AI * Fixed potential issues with skylight detection --- src/Mobs/Skeleton.cpp | 5 ++--- src/Mobs/Zombie.cpp | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Mobs/Skeleton.cpp b/src/Mobs/Skeleton.cpp index e3357185d..0641a3d57 100644 --- a/src/Mobs/Skeleton.cpp +++ b/src/Mobs/Skeleton.cpp @@ -49,11 +49,10 @@ void cSkeleton::GetDrops(cItems & a_Drops, cEntity * a_Killer) void cSkeleton::MoveToPosition(const Vector3f & a_Position) { - // If the destination is in the sun and if it is not night AND the skeleton isn't on fire then block the movement. + // If the destination is sufficiently skylight challenged AND the skeleton isn't on fire then block the movement if ( !IsOnFire() && - (m_World->GetTimeOfDay() < 13187) && - (m_World->GetBlockSkyLight((int) a_Position.x, (int) a_Position.y, (int) a_Position.z) == 15) + (m_World->GetBlockSkyLight((int)floor(a_Position.x), (int)floor(a_Position.y), (int)floor(a_Position.z)) - m_World->GetSkyDarkness() > 8) ) { m_bMovingToDestination = false; diff --git a/src/Mobs/Zombie.cpp b/src/Mobs/Zombie.cpp index f19e096ee..725790ed9 100644 --- a/src/Mobs/Zombie.cpp +++ b/src/Mobs/Zombie.cpp @@ -44,11 +44,10 @@ void cZombie::GetDrops(cItems & a_Drops, cEntity * a_Killer) void cZombie::MoveToPosition(const Vector3f & a_Position) { - // If the destination is in the sun and if it is not night AND the zombie isn't on fire then block the movement. + // If the destination is sufficiently skylight challenged AND the skeleton isn't on fire then block the movement if ( !IsOnFire() && - (m_World->GetTimeOfDay() < 13187) && - (m_World->GetBlockSkyLight((int)a_Position.x, (int)a_Position.y, (int)a_Position.z) == 15) + (m_World->GetBlockSkyLight((int)floor(a_Position.x), (int)floor(a_Position.y), (int)floor(a_Position.z)) - m_World->GetSkyDarkness() > 8) ) { m_bMovingToDestination = false; From 536cb62f1c4ee47bf39a04240f9460953f3f8869 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 28 Jun 2014 21:14:10 +0100 Subject: [PATCH 04/15] An unification of code style --- src/Entities/ProjectileEntity.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index d45f1d212..76daca186 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -361,7 +361,7 @@ void cProjectileEntity::HandlePhysics(float a_Dt, cChunk & a_Chunk) EntityCollisionCallback.GetHitEntity()->GetClass(), HitPos.x, HitPos.y, HitPos.z, EntityCollisionCallback.GetMinCoeff() - ); + ); OnHitEntity(*(EntityCollisionCallback.GetHitEntity()), HitPos); } From 428cfb5c21ec5a35252b967eb306d6ba9b8e11b3 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 29 Jun 2014 22:41:31 +0100 Subject: [PATCH 05/15] Suggestions --- src/Entities/ArrowEntity.cpp | 4 +++- src/Entities/Entity.cpp | 5 +++-- src/Vector3.h | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Entities/ArrowEntity.cpp b/src/Entities/ArrowEntity.cpp index db9dc781a..c76c710ef 100644 --- a/src/Entities/ArrowEntity.cpp +++ b/src/Entities/ArrowEntity.cpp @@ -69,7 +69,9 @@ bool cArrowEntity::CanPickup(const cPlayer & a_Player) const void cArrowEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) { Vector3d Hit = a_HitPos; - Hit += GetSpeed() / 700; // Make arrow sink into block a little + Vector3d SinkMovement = GetSpeed() / 800; + SinkMovement.Clamp(0.001, 0.001, 0.001, 0.05, 0.05, 0.05); + Hit += SinkMovement; // Make arrow sink into block a little super::OnHitSolidBlock(Hit, a_HitFace); int X = (int)floor(Hit.x), Y = (int)floor(Hit.y), Z = (int)floor(Hit.z); diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index f4e89367b..1683aa209 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -255,7 +255,8 @@ void cEntity::TakeDamage(eDamageType a_DamageType, cEntity * a_Attacker, int a_R void cEntity::SetYawFromSpeed(void) { - if ((abs(m_Speed.x) < std::numeric_limits::epsilon()) && (abs(m_Speed.z) < std::numeric_limits::epsilon())) + const double EPS = 0.0000001; + if ((abs(m_Speed.x) < EPS) && (abs(m_Speed.z) < EPS)) { // atan2() may overflow or is undefined, pick any number SetYaw(0); @@ -1235,7 +1236,7 @@ void cEntity::BroadcastMovementUpdate(const cClientHandle * a_Exclude) if (GetWorld()->GetWorldAge() % 2 == 0) { double SpeedSqr = GetSpeed().SqrLength(); - if (SpeedSqr < std::numeric_limits::epsilon()) + if (SpeedSqr == 0.0) { // Speed is zero, send this to clients once only as well as an absolute position if (!m_bHasSentNoSpeed) diff --git a/src/Vector3.h b/src/Vector3.h index 5faac1457..762fdfd71 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -274,6 +274,14 @@ public: return (a_X - x) / (a_OtherEnd.x - x); } + /** Clamps each value in the vector to within a specified range */ + inline void Clamp(T a_MinX, T a_MinY, T a_MinZ, T a_MaxX, T a_MaxY, T a_MaxZ) + { + x = Clamp(x, (T)copysign(a_MinX, x), (T)copysign(a_MaxX, x)); + y = Clamp(y, (T)copysign(a_MinY, y), (T)copysign(a_MaxY, y)); + z = Clamp(z, (T)copysign(a_MinZ, z), (T)copysign(a_MaxZ, z)); + } + /** The max difference between two coords for which the coords are assumed equal. */ static const double EPS; @@ -288,6 +296,12 @@ protected: { return (a_Value < 0) ? -a_Value : a_Value; } + + /** Clamp X to the specified range. */ + T Clamp(T a_Value, T a_Min, T a_Max) + { + return (a_Value < a_Min) ? a_Min : ((a_Value > a_Max) ? a_Max : a_Value); + } }; // tolua_end From 85fae0e521d3a2ea4f083ee2bc54ac7fbb357768 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 30 Jun 2014 19:21:21 +0100 Subject: [PATCH 06/15] Implemented Vector3<>::Floor() --- src/Entities/ArrowEntity.cpp | 5 +++-- src/Vector3.h | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Entities/ArrowEntity.cpp b/src/Entities/ArrowEntity.cpp index c76c710ef..2d6683f0a 100644 --- a/src/Entities/ArrowEntity.cpp +++ b/src/Entities/ArrowEntity.cpp @@ -74,8 +74,9 @@ void cArrowEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFa Hit += SinkMovement; // Make arrow sink into block a little super::OnHitSolidBlock(Hit, a_HitFace); - int X = (int)floor(Hit.x), Y = (int)floor(Hit.y), Z = (int)floor(Hit.z); - + Hit.Floor(); + + int X = Hit.x, Y = Hit.y, Z = Hit.z; m_HitBlockPos = Vector3i(X, Y, Z); // Broadcast arrow hit sound diff --git a/src/Vector3.h b/src/Vector3.h index 762fdfd71..b5ddc705a 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -134,6 +134,22 @@ public: z += a_Diff.z; } + /** Runs each value of the vector through std::floor() */ + inline void Floor(void) + { + x = (T)floor(x); + y = (T)floor(y); + z = (T)floor(z); + } + + /** Clamps each value in the vector to within a specified range */ + inline void Clamp(T a_MinX, T a_MinY, T a_MinZ, T a_MaxX, T a_MaxY, T a_MaxZ) + { + x = Clamp(x, (T)copysign(a_MinX, x), (T)copysign(a_MaxX, x)); + y = Clamp(y, (T)copysign(a_MinY, y), (T)copysign(a_MaxY, y)); + z = Clamp(z, (T)copysign(a_MinZ, z), (T)copysign(a_MaxZ, z)); + } + // tolua_end inline bool operator != (const Vector3 & a_Rhs) const @@ -274,14 +290,6 @@ public: return (a_X - x) / (a_OtherEnd.x - x); } - /** Clamps each value in the vector to within a specified range */ - inline void Clamp(T a_MinX, T a_MinY, T a_MinZ, T a_MaxX, T a_MaxY, T a_MaxZ) - { - x = Clamp(x, (T)copysign(a_MinX, x), (T)copysign(a_MaxX, x)); - y = Clamp(y, (T)copysign(a_MinY, y), (T)copysign(a_MaxY, y)); - z = Clamp(z, (T)copysign(a_MinZ, z), (T)copysign(a_MaxZ, z)); - } - /** The max difference between two coords for which the coords are assumed equal. */ static const double EPS; From 284c1c0514168e30338f2ad372b7e7f185dba0c4 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 1 Jul 2014 22:39:37 +0100 Subject: [PATCH 07/15] Vector clamping fixes Thank you, @madmaxoft. --- src/Entities/ArrowEntity.cpp | 12 ++++++++---- src/Vector3.h | 33 ++++++++++++++++----------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/Entities/ArrowEntity.cpp b/src/Entities/ArrowEntity.cpp index 2d6683f0a..7e96a666d 100644 --- a/src/Entities/ArrowEntity.cpp +++ b/src/Entities/ArrowEntity.cpp @@ -69,14 +69,18 @@ bool cArrowEntity::CanPickup(const cPlayer & a_Player) const void cArrowEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) { Vector3d Hit = a_HitPos; - Vector3d SinkMovement = GetSpeed() / 800; - SinkMovement.Clamp(0.001, 0.001, 0.001, 0.05, 0.05, 0.05); + Vector3d SinkMovement = GetSpeed() / 800; // Base value for arrow penetration + SinkMovement = Clamp( // Adjust the movement so that fast arrows don't go through blocks (though in reality they would, in addition to exploding into fragments :P) + SinkMovement, + (SinkMovement * 0.001) / SinkMovement.Length(), + (SinkMovement * 0.05) / SinkMovement.Length() + ); Hit += SinkMovement; // Make arrow sink into block a little super::OnHitSolidBlock(Hit, a_HitFace); - Hit.Floor(); + Vector3i BlockHit = Hit.Floor(); - int X = Hit.x, Y = Hit.y, Z = Hit.z; + int X = BlockHit.x, Y = BlockHit.y, Z = BlockHit.z; m_HitBlockPos = Vector3i(X, Y, Z); // Broadcast arrow hit sound diff --git a/src/Vector3.h b/src/Vector3.h index b5ddc705a..faf7fe43c 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -135,19 +135,13 @@ public: } /** Runs each value of the vector through std::floor() */ - inline void Floor(void) + inline Vector3 Floor(void) const { - x = (T)floor(x); - y = (T)floor(y); - z = (T)floor(z); - } - - /** Clamps each value in the vector to within a specified range */ - inline void Clamp(T a_MinX, T a_MinY, T a_MinZ, T a_MaxX, T a_MaxY, T a_MaxZ) - { - x = Clamp(x, (T)copysign(a_MinX, x), (T)copysign(a_MaxX, x)); - y = Clamp(y, (T)copysign(a_MinY, y), (T)copysign(a_MaxY, y)); - z = Clamp(z, (T)copysign(a_MinZ, z), (T)copysign(a_MaxZ, z)); + return Vector3( + (T)floor(x), + (T)floor(y), + (T)floor(z) + ); } // tolua_end @@ -162,6 +156,16 @@ public: return Equals(a_Rhs); } + inline bool operator > (const Vector3 & a_Rhs) const + { + return (SqrLength() > a_Rhs.SqrLength()); + } + + inline bool operator < (const Vector3 & a_Rhs) const + { + return (SqrLength() < a_Rhs.SqrLength()); + } + inline void operator += (const Vector3 & a_Rhs) { x += a_Rhs.x; @@ -305,11 +309,6 @@ protected: return (a_Value < 0) ? -a_Value : a_Value; } - /** Clamp X to the specified range. */ - T Clamp(T a_Value, T a_Min, T a_Max) - { - return (a_Value < a_Min) ? a_Min : ((a_Value > a_Max) ? a_Max : a_Value); - } }; // tolua_end From 89a26cc786f3673cf7b5a100300d8aa595735cc3 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Wed, 2 Jul 2014 21:07:34 +0100 Subject: [PATCH 08/15] Suggestions --- src/Entities/ArrowEntity.cpp | 16 ++++++++-------- src/Entities/ArrowEntity.h | 6 ++++++ src/WorldStorage/NBTChunkSerializer.cpp | 17 ++++++++--------- src/WorldStorage/WSSAnvil.cpp | 11 +++++++++-- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/Entities/ArrowEntity.cpp b/src/Entities/ArrowEntity.cpp index 7e96a666d..c039b0b3c 100644 --- a/src/Entities/ArrowEntity.cpp +++ b/src/Entities/ArrowEntity.cpp @@ -67,15 +67,15 @@ bool cArrowEntity::CanPickup(const cPlayer & a_Player) const void cArrowEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) -{ +{ + if (GetSpeed().SqrLength() == 0) + { + SetSpeed(GetLookVector().NormalizeCopy() * 0.1); // Ensure that no division by zero happens later + } + Vector3d Hit = a_HitPos; - Vector3d SinkMovement = GetSpeed() / 800; // Base value for arrow penetration - SinkMovement = Clamp( // Adjust the movement so that fast arrows don't go through blocks (though in reality they would, in addition to exploding into fragments :P) - SinkMovement, - (SinkMovement * 0.001) / SinkMovement.Length(), - (SinkMovement * 0.05) / SinkMovement.Length() - ); - Hit += SinkMovement; // Make arrow sink into block a little + Vector3d SinkMovement = (GetSpeed() / 800); + Hit += (SinkMovement * 0.01) / SinkMovement.Length(); // Make arrow sink into block a centimetre so it lodges (but not to far so it goes black clientside) super::OnHitSolidBlock(Hit, a_HitFace); Vector3i BlockHit = Hit.Floor(); diff --git a/src/Entities/ArrowEntity.h b/src/Entities/ArrowEntity.h index 1fe3032ee..99b7bae31 100644 --- a/src/Entities/ArrowEntity.h +++ b/src/Entities/ArrowEntity.h @@ -58,8 +58,14 @@ public: /// Sets the IsCritical flag void SetIsCritical(bool a_IsCritical) { m_IsCritical = a_IsCritical; } + + /** Gets the block arrow is in */ + Vector3i GetBlockHit(void) const { return m_HitBlockPos; } // tolua_end + + /** Sets the block arrow is in */ + void SetBlockHit(const Vector3i & a_BlockHit) { m_HitBlockPos = a_BlockHit; } protected: diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index 8294d4a00..e49042ff7 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -589,20 +589,19 @@ void cNBTChunkSerializer::AddProjectileEntity(cProjectileEntity * a_Projectile) m_Writer.BeginCompound(""); AddBasicEntity(a_Projectile, a_Projectile->GetMCAClassName()); Vector3d Pos = a_Projectile->GetPosition(); - m_Writer.AddShort("xTile", (Int16)floor(Pos.x)); - m_Writer.AddShort("yTile", (Int16)floor(Pos.y)); - m_Writer.AddShort("zTile", (Int16)floor(Pos.z)); - m_Writer.AddShort("inTile", 0); // TODO: Query the block type - m_Writer.AddShort("shake", 0); // TODO: Any shake? - m_Writer.AddByte ("inGround", a_Projectile->IsInGround() ? 1 : 0); + m_Writer.AddByte("inGround", a_Projectile->IsInGround() ? 1 : 0); switch (a_Projectile->GetProjectileKind()) { case cProjectileEntity::pkArrow: { - m_Writer.AddByte("inData", 0); // TODO: Query the block meta (is it needed?) - m_Writer.AddByte("pickup", ((cArrowEntity *)a_Projectile)->GetPickupState()); - m_Writer.AddDouble("damage", ((cArrowEntity *)a_Projectile)->GetDamageCoeff()); + cArrowEntity * Arrow = (cArrowEntity *)a_Projectile; + + m_Writer.AddInt("xTile", (Int16)Arrow->GetBlockHit().x); + m_Writer.AddInt("yTile", (Int16)Arrow->GetBlockHit().y); + m_Writer.AddInt("zTile", (Int16)Arrow->GetBlockHit().z); + m_Writer.AddByte("pickup", Arrow->GetPickupState()); + m_Writer.AddDouble("damage", Arrow->GetDamageCoeff()); break; } case cProjectileEntity::pkGhastFireball: diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 0f84a0eb1..1e9ce80cc 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -1656,6 +1656,15 @@ void cWSSAnvil::LoadArrowFromNBT(cEntityList & a_Entities, const cParsedNBT & a_ Arrow->SetDamageCoeff(a_NBT.GetDouble(DamageIdx)); } + // Load block hit: + int InBlockXIdx = a_NBT.FindChildByName(a_TagIdx, "xTile"); + int InBlockYIdx = a_NBT.FindChildByName(a_TagIdx, "yTile"); + int InBlockZIdx = a_NBT.FindChildByName(a_TagIdx, "zTile"); + if ((InBlockXIdx > 0) && (InBlockYIdx > 0) && (InBlockZIdx > 0)) + { + Arrow->SetBlockHit(Vector3i(a_NBT.GetInt(InBlockXIdx), a_NBT.GetInt(InBlockYIdx), a_NBT.GetInt(InBlockZIdx))); + } + // Store the new arrow in the entities list: a_Entities.push_back(Arrow.release()); } @@ -2481,8 +2490,6 @@ bool cWSSAnvil::LoadProjectileBaseFromNBT(cProjectileEntity & a_Entity, const cP } a_Entity.SetIsInGround(IsInGround); - // TODO: Load inTile, TileCoords - return true; } From f6350662414044896e7971dcc792c83d5eaddbce Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 4 Jul 2014 12:50:40 +0100 Subject: [PATCH 09/15] Eps comparison --- src/Entities/ArrowEntity.cpp | 2 +- src/Entities/ArrowEntity.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Entities/ArrowEntity.cpp b/src/Entities/ArrowEntity.cpp index c039b0b3c..1d539679c 100644 --- a/src/Entities/ArrowEntity.cpp +++ b/src/Entities/ArrowEntity.cpp @@ -68,7 +68,7 @@ bool cArrowEntity::CanPickup(const cPlayer & a_Player) const void cArrowEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) { - if (GetSpeed().SqrLength() == 0) + if (GetSpeed().EqualsEps(Vector3d(0, 0, 0), 0.0000001)) { SetSpeed(GetLookVector().NormalizeCopy() * 0.1); // Ensure that no division by zero happens later } diff --git a/src/Entities/ArrowEntity.h b/src/Entities/ArrowEntity.h index 99b7bae31..76cb24449 100644 --- a/src/Entities/ArrowEntity.h +++ b/src/Entities/ArrowEntity.h @@ -64,7 +64,7 @@ public: // tolua_end - /** Sets the block arrow is in */ + /** Sets the block arrow is in. To be used by the MCA loader only! */ void SetBlockHit(const Vector3i & a_BlockHit) { m_HitBlockPos = a_BlockHit; } protected: From f4e3c01a710a2cc5118807a65f8d27519a19ef37 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 4 Jul 2014 16:49:24 +0100 Subject: [PATCH 10/15] Various fixed * Fixed potential invalid pointer dereferencing, fixes #1117 * Fixed ender pearls not being loaded properly --- src/Entities/ProjectileEntity.cpp | 75 ++++++++++++++++++++++++- src/Entities/ProjectileEntity.h | 34 +++++++++-- src/Entities/ThrownEnderPearlEntity.cpp | 8 ++- src/WorldStorage/NBTChunkSerializer.cpp | 11 ++-- 4 files changed, 111 insertions(+), 17 deletions(-) diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index 76daca186..ddcc0f7fd 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -21,6 +21,7 @@ #include "FireChargeEntity.h" #include "FireworkEntity.h" #include "GhastFireballEntity.h" +#include "Player.h" @@ -215,7 +216,7 @@ protected: cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, double a_X, double a_Y, double a_Z, double a_Width, double a_Height) : super(etProjectile, a_X, a_Y, a_Z, a_Width, a_Height), m_ProjectileKind(a_Kind), - m_Creator(a_Creator), + m_CreatorData(a_Creator->GetUniqueID(), a_Creator->IsPlayer() ? ((cPlayer *)a_Creator)->GetName() : ""), m_IsInGround(false) { } @@ -227,7 +228,7 @@ cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, double a cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, const Vector3d & a_Pos, const Vector3d & a_Speed, double a_Width, double a_Height) : super(etProjectile, a_Pos.x, a_Pos.y, a_Pos.z, a_Width, a_Height), m_ProjectileKind(a_Kind), - m_Creator(a_Creator), + m_CreatorData(a_Creator->GetUniqueID(), a_Creator->IsPlayer() ? ((cPlayer *)a_Creator)->GetName() : ""), m_IsInGround(false) { SetSpeed(a_Speed); @@ -295,6 +296,74 @@ void cProjectileEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_ +cEntity * cProjectileEntity::GetCreator() +{ + if (m_CreatorData.m_Name.empty()) + { + class cProjectileCreatorCallback : public cEntityCallback + { + public: + cProjectileCreatorCallback(void) : + m_Entity(NULL) + { + } + + virtual bool Item(cEntity * a_Entity) override + { + m_Entity = a_Entity; + return true; + } + + cEntity * GetEntity(void) + { + return m_Entity; + } + + private: + + cEntity * m_Entity; + }; + + cProjectileCreatorCallback PCC; + GetWorld()->DoWithEntityByID(m_CreatorData.m_UniqueID, PCC); + return PCC.GetEntity(); + } + else + { + class cProjectileCreatorCallbackForPlayers : public cPlayerListCallback + { + public: + cProjectileCreatorCallbackForPlayers(void) : + m_Entity(NULL) + { + } + + virtual bool Item(cPlayer * a_Entity) override + { + m_Entity = a_Entity; + return true; + } + + cPlayer * GetEntity(void) + { + return m_Entity; + } + + private: + + cPlayer * m_Entity; + }; + + cProjectileCreatorCallbackForPlayers PCCFP; + GetWorld()->FindAndDoWithPlayer(m_CreatorData.m_Name, PCCFP); + return PCCFP.GetEntity(); + } +} + + + + + AString cProjectileEntity::GetMCAClassName(void) const { switch (m_ProjectileKind) @@ -304,7 +373,7 @@ AString cProjectileEntity::GetMCAClassName(void) const case pkEgg: return "Egg"; case pkGhastFireball: return "Fireball"; case pkFireCharge: return "SmallFireball"; - case pkEnderPearl: return "ThrownEnderPearl"; + case pkEnderPearl: return "ThrownEnderpearl"; case pkExpBottle: return "ThrownExpBottle"; case pkSplashPotion: return "ThrownPotion"; case pkWitherSkull: return "WitherSkull"; diff --git a/src/Entities/ProjectileEntity.h b/src/Entities/ProjectileEntity.h index ae06b072f..e2ebe9f27 100644 --- a/src/Entities/ProjectileEntity.h +++ b/src/Entities/ProjectileEntity.h @@ -66,8 +66,15 @@ public: /// Returns the kind of the projectile (fast class identification) eKind GetProjectileKind(void) const { return m_ProjectileKind; } - /// Returns the entity who created this projectile; may be NULL - cEntity * GetCreator(void) { return m_Creator; } + /** Returns the entity who created this projectile through running its Unique ID through cWorld::DoWithEntityByID() + May return NULL; do not store the returned pointer outside the scope of the tick thread! + */ + cEntity * GetCreator(void); + + /** Returns the name of the player that created the projectile + Will be empty for non-player creators + */ + AString GetCreatorName(void) const { return m_CreatorData.m_Name; } /// Returns the string that is used as the entity type (class name) in MCA files AString GetMCAClassName(void) const; @@ -81,10 +88,29 @@ public: void SetIsInGround(bool a_IsInGround) { m_IsInGround = a_IsInGround; } protected: + + /** A structure that stores the Entity ID and Playername of the projectile's creator + Used to migitate invalid pointers caused by the creator being destroyed + */ + struct CreatorData + { + CreatorData(int a_UniqueID, AString & a_Name) : + m_UniqueID(a_UniqueID), + m_Name(a_Name) + { + } + + const int m_UniqueID; + AString m_Name; + }; + + /** The type of projectile I am */ eKind m_ProjectileKind; - /// The entity who has created this projectile; may be NULL (e. g. for dispensers) - cEntity * m_Creator; + /** The structure for containing the entity ID and name who has created this projectile + The ID and/or name may be NULL (e.g. for dispensers/mobs) + */ + CreatorData m_CreatorData; /// True if the projectile has hit the ground and is stuck there bool m_IsInGround; diff --git a/src/Entities/ThrownEnderPearlEntity.cpp b/src/Entities/ThrownEnderPearlEntity.cpp index a3ee23389..96dd41ee6 100644 --- a/src/Entities/ThrownEnderPearlEntity.cpp +++ b/src/Entities/ThrownEnderPearlEntity.cpp @@ -46,10 +46,12 @@ void cThrownEnderPearlEntity::OnHitEntity(cEntity & a_EntityHit, const Vector3d void cThrownEnderPearlEntity::TeleportCreator(const Vector3d & a_HitPos) { + cEntity * Creator = GetCreator(); + // Teleport the creator here, make them take 5 damage: - if (m_Creator != NULL) + if (Creator != NULL) { - m_Creator->TeleportToCoords(a_HitPos.x, a_HitPos.y + 0.2, a_HitPos.z); - m_Creator->TakeDamage(dtEnderPearl, this, 5, 0); + Creator->TeleportToCoords(a_HitPos.x, a_HitPos.y + 0.2, a_HitPos.z); + Creator->TakeDamage(dtEnderPearl, this, 5, 0); } } diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index e49042ff7..2dcae51ce 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -619,14 +619,11 @@ void cNBTChunkSerializer::AddProjectileEntity(cProjectileEntity * a_Projectile) { ASSERT(!"Unsaved projectile entity!"); } - } // switch (ProjectileKind) - cEntity * Creator = a_Projectile->GetCreator(); - if (Creator != NULL) + } // switch (ProjectileKind) + + if (!a_Projectile->GetCreatorName().empty()) { - if (Creator->GetEntityType() == cEntity::etPlayer) - { - m_Writer.AddString("ownerName", ((cPlayer *)Creator)->GetName()); - } + m_Writer.AddString("ownerName", a_Projectile->GetCreatorName()); } m_Writer.EndCompound(); } From 79e558be349c40ed40b5eefefd29f563a570e404 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 4 Jul 2014 17:42:40 +0100 Subject: [PATCH 11/15] Suggestions --- src/Entities/ThrownEggEntity.cpp | 2 +- src/Entities/ThrownEggEntity.h | 5 ++++- src/Entities/ThrownEnderPearlEntity.cpp | 2 +- src/Entities/ThrownEnderPearlEntity.h | 5 ++++- src/Entities/ThrownSnowballEntity.cpp | 2 +- src/Entities/ThrownSnowballEntity.h | 5 ++++- src/Vector3.h | 10 +++++----- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Entities/ThrownEggEntity.cpp b/src/Entities/ThrownEggEntity.cpp index d7eed41e3..456083108 100644 --- a/src/Entities/ThrownEggEntity.cpp +++ b/src/Entities/ThrownEggEntity.cpp @@ -22,7 +22,7 @@ void cThrownEggEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_H { TrySpawnChicken(a_HitPos); - m_DestroyTimer = 5; + m_DestroyTimer = 2; } diff --git a/src/Entities/ThrownEggEntity.h b/src/Entities/ThrownEggEntity.h index 894665428..dc72c279f 100644 --- a/src/Entities/ThrownEggEntity.h +++ b/src/Entities/ThrownEggEntity.h @@ -41,7 +41,10 @@ protected: return; } } - else { super::Tick(a_Dt, a_Chunk); } + else + { + super::Tick(a_Dt, a_Chunk); + } } // Randomly decides whether to spawn a chicken where the egg lands. diff --git a/src/Entities/ThrownEnderPearlEntity.cpp b/src/Entities/ThrownEnderPearlEntity.cpp index 96dd41ee6..aeb727205 100644 --- a/src/Entities/ThrownEnderPearlEntity.cpp +++ b/src/Entities/ThrownEnderPearlEntity.cpp @@ -22,7 +22,7 @@ void cThrownEnderPearlEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockF // TODO: Tweak a_HitPos based on block face. TeleportCreator(a_HitPos); - m_DestroyTimer = 5; + m_DestroyTimer = 2; } diff --git a/src/Entities/ThrownEnderPearlEntity.h b/src/Entities/ThrownEnderPearlEntity.h index bfd9bd70d..1cea5f7d9 100644 --- a/src/Entities/ThrownEnderPearlEntity.h +++ b/src/Entities/ThrownEnderPearlEntity.h @@ -41,7 +41,10 @@ protected: return; } } - else { super::Tick(a_Dt, a_Chunk); } + else + { + super::Tick(a_Dt, a_Chunk); + } } /** Teleports the creator where the ender pearl lands */ diff --git a/src/Entities/ThrownSnowballEntity.cpp b/src/Entities/ThrownSnowballEntity.cpp index b82cd56db..d94e75898 100644 --- a/src/Entities/ThrownSnowballEntity.cpp +++ b/src/Entities/ThrownSnowballEntity.cpp @@ -20,7 +20,7 @@ cThrownSnowballEntity::cThrownSnowballEntity(cEntity * a_Creator, double a_X, do void cThrownSnowballEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) { - m_DestroyTimer = 5; + m_DestroyTimer = 2; } diff --git a/src/Entities/ThrownSnowballEntity.h b/src/Entities/ThrownSnowballEntity.h index e30971f0a..9a8770379 100644 --- a/src/Entities/ThrownSnowballEntity.h +++ b/src/Entities/ThrownSnowballEntity.h @@ -41,7 +41,10 @@ protected: return; } } - else { super::Tick(a_Dt, a_Chunk); } + else + { + super::Tick(a_Dt, a_Chunk); + } } private: diff --git a/src/Vector3.h b/src/Vector3.h index faf7fe43c..f350ede2a 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -135,12 +135,12 @@ public: } /** Runs each value of the vector through std::floor() */ - inline Vector3 Floor(void) const + inline Vector3 Floor(void) const { - return Vector3( - (T)floor(x), - (T)floor(y), - (T)floor(z) + return Vector3i( + (int)floor(x), + (int)floor(y), + (int)floor(z) ); } From 25e986220662247cfeefe4a496da959f409859e9 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 4 Jul 2014 17:49:44 +0100 Subject: [PATCH 12/15] Compile fix --- src/Vector3.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Vector3.h b/src/Vector3.h index f350ede2a..9e855b8af 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -137,7 +137,7 @@ public: /** Runs each value of the vector through std::floor() */ inline Vector3 Floor(void) const { - return Vector3i( + return Vector3( (int)floor(x), (int)floor(y), (int)floor(z) From f4e11d194e9e4a2e85a9f9688312ad08ade45b83 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 4 Jul 2014 22:07:26 +0100 Subject: [PATCH 13/15] Crash and compile fix --- src/Entities/ProjectileEntity.cpp | 11 ++++++++--- src/Entities/ProjectileEntity.h | 2 +- src/Protocol/Protocol17x.cpp | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index ddcc0f7fd..50f62b018 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -216,7 +216,10 @@ protected: cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, double a_X, double a_Y, double a_Z, double a_Width, double a_Height) : super(etProjectile, a_X, a_Y, a_Z, a_Width, a_Height), m_ProjectileKind(a_Kind), - m_CreatorData(a_Creator->GetUniqueID(), a_Creator->IsPlayer() ? ((cPlayer *)a_Creator)->GetName() : ""), + m_CreatorData( + ((a_Creator != NULL) ? a_Creator->GetUniqueID() : -1), + ((a_Creator != NULL) ? (a_Creator->IsPlayer() ? ((cPlayer *)a_Creator)->GetName() : "") : "") + ), m_IsInGround(false) { } @@ -298,7 +301,7 @@ void cProjectileEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_ cEntity * cProjectileEntity::GetCreator() { - if (m_CreatorData.m_Name.empty()) + if (m_CreatorData.m_Name.empty() && (m_CreatorData.m_UniqueID >= 1)) { class cProjectileCreatorCallback : public cEntityCallback { @@ -328,7 +331,7 @@ cEntity * cProjectileEntity::GetCreator() GetWorld()->DoWithEntityByID(m_CreatorData.m_UniqueID, PCC); return PCC.GetEntity(); } - else + else if (!m_CreatorData.m_Name.empty()) { class cProjectileCreatorCallbackForPlayers : public cPlayerListCallback { @@ -358,6 +361,8 @@ cEntity * cProjectileEntity::GetCreator() GetWorld()->FindAndDoWithPlayer(m_CreatorData.m_Name, PCCFP); return PCCFP.GetEntity(); } + + return NULL; } diff --git a/src/Entities/ProjectileEntity.h b/src/Entities/ProjectileEntity.h index e2ebe9f27..84eefb9ee 100644 --- a/src/Entities/ProjectileEntity.h +++ b/src/Entities/ProjectileEntity.h @@ -94,7 +94,7 @@ protected: */ struct CreatorData { - CreatorData(int a_UniqueID, AString & a_Name) : + CreatorData(int a_UniqueID, const AString & a_Name) : m_UniqueID(a_UniqueID), m_Name(a_Name) { diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 02c577dc8..109e5a67b 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -2334,7 +2334,7 @@ void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) for (int loretag = NBT.GetFirstChild(displaytag); loretag >= 0; loretag = NBT.GetNextSibling(loretag)) // Loop through array of strings { - AppendPrintf(Lore, "%s`", NBT.GetString(loretag).c_str()); // Append the lore with a newline, used internally by MCS to display a new line in the client; don't forget to c_str ;) + AppendPrintf(Lore, "%s`", NBT.GetString(loretag).c_str()); // Append the lore with a grave accent/backtick, used internally by MCS to display a new line in the client; don't forget to c_str ;) } a_Item.m_Lore = Lore; From 460d6bd0cbb799a6e68f1bc264f55c3d89eb8206 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 5 Jul 2014 22:59:22 +0100 Subject: [PATCH 14/15] Changed everything to callbacks --- src/Entities/ProjectileEntity.cpp | 72 +------------------------ src/Entities/ProjectileEntity.h | 6 +-- src/Entities/ThrownEnderPearlEntity.cpp | 35 +++++++++--- src/Mobs/Creeper.cpp | 22 +++++++- 4 files changed, 53 insertions(+), 82 deletions(-) diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index 50f62b018..334973833 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -142,7 +142,7 @@ public: { if ( (a_Entity == m_Projectile) || // Do not check collisions with self - (a_Entity == m_Projectile->GetCreator()) // Do not check whoever shot the projectile + (a_Entity->GetUniqueID() == m_Projectile->GetCreatorUniqueID()) // Do not check whoever shot the projectile ) { // TODO: Don't check creator only for the first 5 ticks @@ -299,76 +299,6 @@ void cProjectileEntity::OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_ -cEntity * cProjectileEntity::GetCreator() -{ - if (m_CreatorData.m_Name.empty() && (m_CreatorData.m_UniqueID >= 1)) - { - class cProjectileCreatorCallback : public cEntityCallback - { - public: - cProjectileCreatorCallback(void) : - m_Entity(NULL) - { - } - - virtual bool Item(cEntity * a_Entity) override - { - m_Entity = a_Entity; - return true; - } - - cEntity * GetEntity(void) - { - return m_Entity; - } - - private: - - cEntity * m_Entity; - }; - - cProjectileCreatorCallback PCC; - GetWorld()->DoWithEntityByID(m_CreatorData.m_UniqueID, PCC); - return PCC.GetEntity(); - } - else if (!m_CreatorData.m_Name.empty()) - { - class cProjectileCreatorCallbackForPlayers : public cPlayerListCallback - { - public: - cProjectileCreatorCallbackForPlayers(void) : - m_Entity(NULL) - { - } - - virtual bool Item(cPlayer * a_Entity) override - { - m_Entity = a_Entity; - return true; - } - - cPlayer * GetEntity(void) - { - return m_Entity; - } - - private: - - cPlayer * m_Entity; - }; - - cProjectileCreatorCallbackForPlayers PCCFP; - GetWorld()->FindAndDoWithPlayer(m_CreatorData.m_Name, PCCFP); - return PCCFP.GetEntity(); - } - - return NULL; -} - - - - - AString cProjectileEntity::GetMCAClassName(void) const { switch (m_ProjectileKind) diff --git a/src/Entities/ProjectileEntity.h b/src/Entities/ProjectileEntity.h index 84eefb9ee..7b38169e2 100644 --- a/src/Entities/ProjectileEntity.h +++ b/src/Entities/ProjectileEntity.h @@ -66,10 +66,10 @@ public: /// Returns the kind of the projectile (fast class identification) eKind GetProjectileKind(void) const { return m_ProjectileKind; } - /** Returns the entity who created this projectile through running its Unique ID through cWorld::DoWithEntityByID() - May return NULL; do not store the returned pointer outside the scope of the tick thread! + /** Returns the unique ID of the entity who created this projectile + May return an ID <0 */ - cEntity * GetCreator(void); + int GetCreatorUniqueID(void) { return m_CreatorData.m_UniqueID; } /** Returns the name of the player that created the projectile Will be empty for non-player creators diff --git a/src/Entities/ThrownEnderPearlEntity.cpp b/src/Entities/ThrownEnderPearlEntity.cpp index aeb727205..c7407e6ae 100644 --- a/src/Entities/ThrownEnderPearlEntity.cpp +++ b/src/Entities/ThrownEnderPearlEntity.cpp @@ -1,6 +1,7 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "ThrownEnderPearlEntity.h" +#include "Player.h" @@ -46,12 +47,34 @@ void cThrownEnderPearlEntity::OnHitEntity(cEntity & a_EntityHit, const Vector3d void cThrownEnderPearlEntity::TeleportCreator(const Vector3d & a_HitPos) { - cEntity * Creator = GetCreator(); - - // Teleport the creator here, make them take 5 damage: - if (Creator != NULL) + if (m_CreatorData.m_Name.empty()) { - Creator->TeleportToCoords(a_HitPos.x, a_HitPos.y + 0.2, a_HitPos.z); - Creator->TakeDamage(dtEnderPearl, this, 5, 0); + return; } + + class cProjectileCreatorCallbackForPlayers : public cPlayerListCallback + { + public: + cProjectileCreatorCallbackForPlayers(cEntity * a_Attacker, Vector3i a_HitPos) : + m_Attacker(a_Attacker), + m_HitPos(a_HitPos) + { + } + + virtual bool Item(cPlayer * a_Entity) override + { + // Teleport the creator here, make them take 5 damage: + a_Entity->TeleportToCoords(m_HitPos.x, m_HitPos.y + 0.2, m_HitPos.z); + a_Entity->TakeDamage(dtEnderPearl, m_Attacker, 5, 0); + return true; + } + + private: + + cEntity * m_Attacker; + Vector3i m_HitPos; + }; + + cProjectileCreatorCallbackForPlayers PCCFP(this, a_HitPos); + GetWorld()->FindAndDoWithPlayer(m_CreatorData.m_Name, PCCFP); } diff --git a/src/Mobs/Creeper.cpp b/src/Mobs/Creeper.cpp index a7b97f604..b9041bd5a 100644 --- a/src/Mobs/Creeper.cpp +++ b/src/Mobs/Creeper.cpp @@ -67,9 +67,27 @@ void cCreeper::GetDrops(cItems & a_Drops, cEntity * a_Killer) } AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_GUNPOWDER); - if ((a_Killer != NULL) && (a_Killer->IsProjectile())) + if ((a_Killer != NULL) && a_Killer->IsProjectile() && (((cProjectileEntity *)a_Killer)->GetCreatorUniqueID() >= 0)) { - if (((cMonster *)((cProjectileEntity *)a_Killer)->GetCreator())->GetMobType() == mtSkeleton) + class cProjectileCreatorCallback : public cEntityCallback + { + public: + cProjectileCreatorCallback(void) + { + } + + virtual bool Item(cEntity * a_Entity) override + { + if (a_Entity->IsMob() && ((cMonster *)a_Entity)->GetMobType() == mtSkeleton) + { + return true; + } + return false; + } + }; + + cProjectileCreatorCallback PCC; + if (GetWorld()->DoWithEntityByID(((cProjectileEntity *)a_Killer)->GetCreatorUniqueID(), PCC)) { // 12 music discs. TickRand starts from 0 to 11. Disk IDs start at 2256, so add that. There. AddRandomDropItem(a_Drops, 1, 1, (short)m_World->GetTickRandomNumber(11) + 2256); From 7c7501abc5345ec4eddd030ab4649b05354002c3 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 7 Jul 2014 21:14:15 +0100 Subject: [PATCH 15/15] Added extra space before comments --- src/Entities/ProjectileEntity.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index 334973833..0bb34019e 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -69,15 +69,15 @@ protected: if (cBlockInfo::IsSolid(a_BlockType)) { // The projectile hit a solid block, calculate the exact hit coords: - cBoundingBox bb(a_BlockX, a_BlockX + 1, a_BlockY, a_BlockY + 1, a_BlockZ, a_BlockZ + 1); // Bounding box of the block hit - 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 + cBoundingBox bb(a_BlockX, a_BlockX + 1, a_BlockY, a_BlockY + 1, a_BlockZ, a_BlockZ + 1); // Bounding box of the block hit + 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 if (bb.CalcLineIntersection(LineStart, LineEnd, LineCoeff, Face)) { - Vector3d Intersection = LineStart + m_Projectile->GetSpeed() * LineCoeff; // Point where projectile goes into the hit block + Vector3d Intersection = LineStart + m_Projectile->GetSpeed() * LineCoeff; // Point where projectile goes into the hit block if (cPluginManager::Get()->CallHookProjectileHitBlock(*m_Projectile, a_BlockX, a_BlockY, a_BlockZ, Face, &Intersection)) {