summaryrefslogtreecommitdiffstats
path: root/src/Entities
diff options
context:
space:
mode:
authorKingCol13 <48412633+KingCol13@users.noreply.github.com>2021-07-10 22:05:00 +0200
committerGitHub <noreply@github.com>2021-07-10 22:05:00 +0200
commit1d1fa914012125126d1a3656edc428859696f54f (patch)
treed0190a09b6f519ce528e6d8e25d99141a3faa29f /src/Entities
parentSymmetry in MultiVersionProtocol to fix a crash (#5261) (diff)
downloadcuberite-1d1fa914012125126d1a3656edc428859696f54f.tar
cuberite-1d1fa914012125126d1a3656edc428859696f54f.tar.gz
cuberite-1d1fa914012125126d1a3656edc428859696f54f.tar.bz2
cuberite-1d1fa914012125126d1a3656edc428859696f54f.tar.lz
cuberite-1d1fa914012125126d1a3656edc428859696f54f.tar.xz
cuberite-1d1fa914012125126d1a3656edc428859696f54f.tar.zst
cuberite-1d1fa914012125126d1a3656edc428859696f54f.zip
Diffstat (limited to '')
-rw-r--r--src/Entities/Pawn.cpp78
-rw-r--r--src/Entities/Pawn.h15
2 files changed, 93 insertions, 0 deletions
diff --git a/src/Entities/Pawn.cpp b/src/Entities/Pawn.cpp
index 5476db468..ed26a080b 100644
--- a/src/Entities/Pawn.cpp
+++ b/src/Entities/Pawn.cpp
@@ -547,3 +547,81 @@ bool cPawn::DeductTotem(const eDamageType a_DamageType)
}
return false;
}
+
+
+
+
+
+bool cPawn::FindTeleportDestination(cWorld & a_World, const int a_HeightRequired, const unsigned int a_NumTries, Vector3d & a_Destination, const Vector3i a_MinBoxCorner, const Vector3i a_MaxBoxCorner)
+{
+ /*
+ Algorithm:
+ Choose random destination.
+ Seek downwards, regardless of distance until the block is made of movement-blocking material: https://minecraft.fandom.com/wiki/Materials
+ Succeeds if no liquid or solid blocks prevents from standing at destination.
+ */
+ auto & Random = GetRandomProvider();
+
+ for (unsigned int i = 0; i < a_NumTries; i++)
+ {
+ const int DestX = Random.RandInt(a_MinBoxCorner.x, a_MaxBoxCorner.x);
+ int DestY = Random.RandInt(a_MinBoxCorner.y, a_MaxBoxCorner.y);
+ const int DestZ = Random.RandInt(a_MinBoxCorner.z, a_MaxBoxCorner.z);
+
+ // Seek downwards from initial destination until we find a solid block or go into the void
+ BLOCKTYPE DestBlock = a_World.GetBlock({DestX, DestY, DestZ});
+ while ((DestY >= 0) && !cBlockInfo::IsSolid(DestBlock))
+ {
+ DestBlock = a_World.GetBlock({DestX, DestY, DestZ});
+ DestY--;
+ }
+
+ // Couldn't find a solid block so move to next attempt
+ if (DestY < 0)
+ {
+ continue;
+ }
+
+ // Succeed if blocks above destination are empty
+ bool Success = true;
+ for (int j = 1; j <= a_HeightRequired; j++)
+ {
+ BLOCKTYPE TestBlock = a_World.GetBlock({DestX, DestY + j, DestZ});
+ if (cBlockInfo::IsSolid(TestBlock) || IsBlockLiquid(TestBlock))
+ {
+ Success = false;
+ break;
+ }
+ }
+
+ if (!Success)
+ {
+ continue;
+ }
+
+ // Offsets for entity to be centred and standing on solid block
+ a_Destination = Vector3d(DestX + 0.5, DestY + 1, DestZ + 0.5);
+ return true;
+ }
+ return false;
+}
+
+
+
+
+
+bool cPawn::FindTeleportDestination(cWorld & a_World, const int a_HeightRequired, const unsigned int a_NumTries, Vector3d & a_Destination, const cBoundingBox a_BoundingBox)
+{
+ return FindTeleportDestination(a_World, a_HeightRequired, a_NumTries, a_Destination, a_BoundingBox.GetMin(), a_BoundingBox.GetMax());
+}
+
+
+
+
+
+bool cPawn::FindTeleportDestination(cWorld & a_World, const int a_HeightRequired, const unsigned int a_NumTries, Vector3d & a_Destination, Vector3i a_Centre, const int a_HalfCubeWidth)
+{
+ Vector3i MinCorner(a_Centre.x - a_HalfCubeWidth, a_Centre.y - a_HalfCubeWidth, a_Centre.z - a_HalfCubeWidth);
+ Vector3i MaxCorner(a_Centre.x + a_HalfCubeWidth, a_Centre.y + a_HalfCubeWidth, a_Centre.z + a_HalfCubeWidth);
+ return FindTeleportDestination(a_World, a_HeightRequired, a_NumTries, a_Destination, MinCorner, MaxCorner);
+}
diff --git a/src/Entities/Pawn.h b/src/Entities/Pawn.h
index 706c51e49..222e8f3b8 100644
--- a/src/Entities/Pawn.h
+++ b/src/Entities/Pawn.h
@@ -70,6 +70,21 @@ public:
/** Returns the entity effect, if it is currently applied or nullptr if not. */
cEntityEffect * GetEntityEffect(cEntityEffect::eType a_EffectType) const;
+ // tolua_begin
+
+ static bool FindTeleportDestination(cWorld & a_World, const int a_HeightRequired, const unsigned int a_NumTries, Vector3d & a_Destination, const Vector3i a_MinBoxCorner, const Vector3i a_MaxBoxCorner);
+
+ static bool FindTeleportDestination(cWorld & a_World, const int a_HeightRequired, const unsigned int a_NumTries, Vector3d & a_Destination, const cBoundingBox a_BoundingBox);
+
+ /** Used by enderman and chorus fruit.
+ Checks for valid destinations in a cube of length 2 * a_HalfCubeWidth centred at a_Centre.
+ Returns true and places destination in a_Destination if successful.
+ Returns false if destination could be found after a_NumTries attempts.
+ Details at: https://minecraft.fandom.com/wiki/Enderman#Teleportation. */
+ static bool FindTeleportDestination(cWorld & a_World, const int a_HeightRequired, const unsigned int a_NumTries, Vector3d & a_Destination, Vector3i a_Centre, const int a_HalfCubeWidth);
+
+ // tolua_end
+
protected:
typedef std::map<cEntityEffect::eType, std::unique_ptr<cEntityEffect>> tEffectMap;