diff options
-rw-r--r-- | src/Entities/Minecart.cpp | 305 | ||||
-rw-r--r-- | src/Entities/Minecart.h | 7 |
2 files changed, 242 insertions, 70 deletions
diff --git a/src/Entities/Minecart.cpp b/src/Entities/Minecart.cpp index 7f3fea5ec..ad63f848e 100644 --- a/src/Entities/Minecart.cpp +++ b/src/Entities/Minecart.cpp @@ -19,6 +19,70 @@ +class cMinecartCollisionCallback : + public cEntityCallback +{ +public: + cMinecartCollisionCallback(Vector3d a_Pos, double a_Height, double a_Width, int a_UniqueID, int a_AttacheeUniqueID) : + m_Pos(a_Pos), + m_Height(a_Height), + m_Width(a_Width), + m_DoesInteserct(false), + m_CollidedEntityPos(0, 0, 0), + m_UniqueID(a_UniqueID), + m_AttacheeUniqueID(a_AttacheeUniqueID) + { + } + + virtual bool Item(cEntity * a_Entity) override + { + ASSERT(a_Entity != NULL); + + if (!a_Entity->IsPlayer() && !a_Entity->IsMob() && !a_Entity->IsMinecart() && !a_Entity->IsBoat()) + { + return false; + } + else if ((a_Entity->GetUniqueID() == m_UniqueID) || (a_Entity->GetUniqueID() == m_AttacheeUniqueID)) + { + return false; + } + + cBoundingBox bbEntity(a_Entity->GetPosition(), a_Entity->GetWidth() / 2, a_Entity->GetHeight()); + cBoundingBox bbMinecart(Vector3d(m_Pos.x, floor(m_Pos.y), m_Pos.z), m_Width / 2, m_Height); + + if (bbEntity.DoesIntersect(bbMinecart)) + { + m_CollidedEntityPos = a_Entity->GetPosition(); + m_DoesInteserct = true; + return true; + } + return false; + } + + bool FoundIntersection(void) const + { + return m_DoesInteserct; + } + + Vector3d GetCollidedEntityPosition(void) const + { + return m_CollidedEntityPos; + } + +protected: + bool m_DoesInteserct; + + Vector3d m_CollidedEntityPos; + + Vector3d m_Pos; + double m_Height, m_Width; + int m_UniqueID; + int m_AttacheeUniqueID; +}; + + + + cMinecart::cMinecart(ePayload a_Payload, double a_X, double a_Y, double a_Z) : super(etMinecart, a_X, a_Y, a_Z, 0.98, 0.7), @@ -30,7 +94,7 @@ cMinecart::cMinecart(ePayload a_Payload, double a_X, double a_Y, double a_Z) : SetMass(20.f); SetMaxHealth(6); SetHealth(6); - SetWidth(1.2); + SetWidth(1); SetHeight(0.9); } @@ -153,7 +217,9 @@ void cMinecart::HandleRailPhysics(NIBBLETYPE a_RailMeta, float a_Dt) SetSpeedY(0); // Don't move vertically as on ground SetSpeedX(0); // Correct diagonal movement from curved rails - if (TestBlockCollision(a_RailMeta)) return; + // Execute both the entity and block collision checks + bool BlckCol = TestBlockCollision(a_RailMeta), EntCol = TestEntityCollision(a_RailMeta); + if (EntCol || BlckCol) return; if (GetSpeedZ() != 0) // Don't do anything if cart is stationary { @@ -177,7 +243,8 @@ void cMinecart::HandleRailPhysics(NIBBLETYPE a_RailMeta, float a_Dt) SetSpeedY(0); SetSpeedZ(0); - if (TestBlockCollision(a_RailMeta)) return; + bool BlckCol = TestBlockCollision(a_RailMeta), EntCol = TestEntityCollision(a_RailMeta); + if (EntCol || BlckCol) return; if (GetSpeedX() != 0) { @@ -281,24 +348,11 @@ void cMinecart::HandleRailPhysics(NIBBLETYPE a_RailMeta, float a_Dt) SetRotation(315); // Set correct rotation server side SetPosY(floor(GetPosY()) + 0.55); // Levitate dat cart - if (TestBlockCollision(a_RailMeta)) return; + TestBlockCollision(a_RailMeta); + TestEntityCollision(a_RailMeta); + + // SnapToRail handles turning - if (GetSpeedZ() > 0) // Cart moving south - { - int OldX = (int)floor(GetPosX()); - AddSpeedX(-GetSpeedZ() + 0.5); // See below - AddPosX(-GetSpeedZ() * (a_Dt / 1000)); // Diagonally move southwest (which will make cart hit a southwest rail) - // If we are already at southwest rail, set Z speed to zero as we can be moving so fast, MCS doesn't tick fast enough to active the handle for the rail... - // ...and so we derail unexpectedly. - if (GetPosX() <= OldX - 1) SetSpeedZ(0); - } - else if (GetSpeedX() > 0) // Cart moving east - { - int OldZ = (int)floor(GetPosZ()); - AddSpeedZ(-GetSpeedX() + 0.5); - AddPosZ(-GetSpeedX() * (a_Dt / 1000)); // Diagonally move northeast - if (GetPosZ() <= OldZ - 1) SetSpeedX(0); - } break; } case E_META_RAIL_CURVED_ZM_XP: // Curved NORTH EAST @@ -306,22 +360,9 @@ void cMinecart::HandleRailPhysics(NIBBLETYPE a_RailMeta, float a_Dt) SetRotation(225); SetPosY(floor(GetPosY()) + 0.55); - if (TestBlockCollision(a_RailMeta)) return; + TestBlockCollision(a_RailMeta); + TestEntityCollision(a_RailMeta); - if (GetSpeedZ() > 0) - { - int OldX = (int)floor(GetPosX()); - AddSpeedX(GetSpeedZ() - 0.5); - AddPosX(GetSpeedZ() * (a_Dt / 1000)); - if (GetPosX() >= OldX + 1) SetSpeedZ(0); - } - else if (GetSpeedX() < 0) - { - int OldZ = (int)floor(GetPosZ()); - AddSpeedZ(GetSpeedX() + 0.5); - AddPosZ(GetSpeedX() * (a_Dt / 1000)); - if (GetPosZ() <= OldZ - 1) SetSpeedX(0); - } break; } case E_META_RAIL_CURVED_ZP_XM: // Curved SOUTH WEST @@ -329,22 +370,9 @@ void cMinecart::HandleRailPhysics(NIBBLETYPE a_RailMeta, float a_Dt) SetRotation(135); SetPosY(floor(GetPosY()) + 0.55); - if (TestBlockCollision(a_RailMeta)) return; + TestBlockCollision(a_RailMeta); + TestEntityCollision(a_RailMeta); - if (GetSpeedZ() < 0) - { - int OldX = (int)floor(GetPosX()); - AddSpeedX(GetSpeedZ() + 0.5); - AddPosX(GetSpeedZ() * (a_Dt / 1000)); - if (GetPosX() <= OldX - 1) SetSpeedZ(0); - } - else if (GetSpeedX() > 0) - { - int OldZ = (int)floor(GetPosZ()); - AddSpeedZ(GetSpeedX() - 0.5); - AddPosZ(GetSpeedX() * (a_Dt / 1000)); - if (GetPosZ() >= OldZ + 1) SetSpeedX(0); - } break; } case E_META_RAIL_CURVED_ZP_XP: // Curved SOUTH EAST @@ -352,22 +380,9 @@ void cMinecart::HandleRailPhysics(NIBBLETYPE a_RailMeta, float a_Dt) SetRotation(45); SetPosY(floor(GetPosY()) + 0.55); - if (TestBlockCollision(a_RailMeta)) return; + TestBlockCollision(a_RailMeta); + TestEntityCollision(a_RailMeta); - if (GetSpeedZ() < 0) - { - int OldX = (int)floor(GetPosX()); - AddSpeedX(-GetSpeedZ() - 0.5); - AddPosX(-GetSpeedZ() * (a_Dt / 1000)); - if (GetPosX() >= OldX + 1) SetSpeedZ(0); - } - else if (GetSpeedX() < 0) - { - int OldZ = (int)floor(GetPosZ()); - AddSpeedZ(-GetSpeedX() - 0.5); - AddPosZ(-GetSpeedX() * (a_Dt / 1000)); - if (GetPosZ() >= OldZ + 1) SetSpeedX(0); - } break; } default: @@ -481,6 +496,102 @@ void cMinecart::SnapToRail(NIBBLETYPE a_RailMeta) SetPosX(floor(GetPosX()) + 0.5); break; } + case E_META_RAIL_CURVED_ZM_XM: + { + if (GetPosZ() < floor(GetPosZ()) + 0.5) + { + if (GetSpeedX() > 0) + { + SetSpeedZ(-GetSpeedX() * 0.7); + } + + SetSpeedX(0); + SetPosX(floor(GetPosX()) + 0.5); + } + else + { + if (GetSpeedZ() > 0) + { + SetSpeedX(-GetSpeedZ() * 0.7); + } + + SetSpeedZ(0); + SetPosZ(floor(GetPosZ()) + 0.5); + } + break; + } + case E_META_RAIL_CURVED_ZM_XP: + { + if (GetPosZ() < floor(GetPosZ()) + 0.5) + { + if (GetSpeedX() < 0) + { + SetSpeedZ(GetSpeedX() * 0.7); + } + + SetSpeedX(0); + SetPosX(floor(GetPosX()) + 0.5); + } + else + { + if (GetSpeedZ() > 0) + { + SetSpeedX(GetSpeedZ() * 0.7); + } + + SetSpeedZ(0); + SetPosZ(floor(GetPosZ()) + 0.5); + } + break; + } + case E_META_RAIL_CURVED_ZP_XM: + { + if (GetPosZ() < floor(GetPosZ()) + 0.5) + { + if (GetSpeedZ() < 0) + { + SetSpeedX(GetSpeedZ() * 0.7); + } + + SetSpeedZ(0); + SetPosZ(floor(GetPosZ()) + 0.5); + } + else + { + if (GetSpeedX() > 0) + { + SetSpeedZ(GetSpeedX() * 0.7); + } + + SetSpeedX(0); + SetPosX(floor(GetPosX()) + 0.5); + } + break; + } + case E_META_RAIL_CURVED_ZP_XP: + { + if (GetPosZ() < floor(GetPosZ()) + 0.5) + { + if (GetSpeedZ() < 0) + { + SetSpeedX(-GetSpeedZ() * 0.7); + } + + SetSpeedZ(0); + SetPosZ(floor(GetPosZ()) + 0.5); + } + else + { + if (GetSpeedX() < 0) + { + SetSpeedZ(-GetSpeedX() * 0.7); + } + + SetSpeedX(0); + SetPosX(floor(GetPosX()) + 0.5); + } + break; + } default: break; } } @@ -512,7 +623,7 @@ bool cMinecart::TestBlockCollision(NIBBLETYPE a_RailMeta) } } } - else + else if (GetSpeedZ() < 0) { BLOCKTYPE Block = m_World->GetBlock((int)floor(GetPosX()), (int)floor(GetPosY()), (int)floor(GetPosZ()) - 1); if (!IsBlockRail(Block) && g_BlockIsSolid[Block]) @@ -548,7 +659,7 @@ bool cMinecart::TestBlockCollision(NIBBLETYPE a_RailMeta) } } } - else + else if (GetSpeedX() < 0) { BLOCKTYPE Block = m_World->GetBlock((int)floor(GetPosX()) - 1, (int)floor(GetPosY()), (int)floor(GetPosZ())); if (!IsBlockRail(Block) && g_BlockIsSolid[Block]) @@ -596,10 +707,68 @@ bool cMinecart::TestBlockCollision(NIBBLETYPE a_RailMeta) +bool cMinecart::TestEntityCollision(NIBBLETYPE a_RailMeta) +{ + switch (a_RailMeta) + { + case E_META_RAIL_ZM_ZP: + { + cMinecartCollisionCallback MinecartCollisionCallback(GetPosition(), GetHeight(), GetWidth(), GetUniqueID(), ((m_Attachee == NULL) ? -1 : m_Attachee->GetUniqueID())); + m_World->ForEachEntity(MinecartCollisionCallback); + + if (MinecartCollisionCallback.FoundIntersection()) + { + if (MinecartCollisionCallback.GetCollidedEntityPosition().z >= GetPosZ()) + { + if (((-GetSpeedZ()) * 0.4) < 0.01) + { + AddSpeedZ(-4); + } + else + { + SetSpeedZ((-GetSpeedZ()) * 0.4); + } + } + else + { + if ((GetSpeedZ() * 0.4) < 0.01) + { + AddSpeedZ(4); + } + else + { + SetSpeedZ(GetSpeedZ() * 0.4); + } + } + return true; + } + break; + } + case E_META_RAIL_XM_XP: + { + + break; + } + case E_META_RAIL_CURVED_ZM_XM: + case E_META_RAIL_CURVED_ZM_XP: + case E_META_RAIL_CURVED_ZP_XM: + case E_META_RAIL_CURVED_ZP_XP: + { + + break; + } + default: break; + } + return false; +} + + + + void cMinecart::DoTakeDamage(TakeDamageInfo & TDI) { - if (TDI.Attacker->IsPlayer() && ((cPlayer *)TDI.Attacker)->IsGameModeCreative()) + if ((TDI.Attacker != NULL) && TDI.Attacker->IsPlayer() && ((cPlayer *)TDI.Attacker)->IsGameModeCreative()) { Destroy(); TDI.FinalDamage = GetMaxHealth(); // Instant hit for creative diff --git a/src/Entities/Minecart.h b/src/Entities/Minecart.h index 1ebddfdda..1c3ea3220 100644 --- a/src/Entities/Minecart.h +++ b/src/Entities/Minecart.h @@ -79,10 +79,13 @@ protected: */ void HandleDetectorRailPhysics(NIBBLETYPE a_RailMeta, float a_Dt); - /** Snaps a minecart to a rail's axis, resetting its speed */ + /** Snaps a mincecart to a rail's axis, resetting its speed + For curved rails, it changes the cart's direction as well as snapping it to axis */ void SnapToRail(NIBBLETYPE a_RailMeta); - /** Tests is a solid block is in front of a cart, and stops the cart (and returns true) if so; returns false if no obstruction*/ + /** Tests if a solid block is in front of a cart, and stops the cart (and returns true) if so; returns false if no obstruction */ bool TestBlockCollision(NIBBLETYPE a_RailMeta); + /** Tests if this mincecart's bounding box is intersecting another entity's bounding box (collision) and pushes mincecart away */ + bool TestEntityCollision(NIBBLETYPE a_RailMeta); } ; |