diff options
Diffstat (limited to '')
-rw-r--r-- | src/Entities/Pawn.cpp | 78 |
1 files changed, 78 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); +} |