From 1d1fa914012125126d1a3656edc428859696f54f Mon Sep 17 00:00:00 2001 From: KingCol13 <48412633+KingCol13@users.noreply.github.com> Date: Sat, 10 Jul 2021 21:05:00 +0100 Subject: Chorus fruit teleport (#5259) * Outline function for teleporting. * Created new handler for chorus fruit. * Fixed AttemptTeleport failing. * Better names, working sound effect. * Corrected naming. * Remove stray LOGD. * Offset teleport to middle of block. * Style Fixes Co-authored-by: x12xx12x <44411062+12xx12@users.noreply.github.com> * Style Fixes 2 Co-authored-by: x12xx12x <44411062+12xx12@users.noreply.github.com> * Move FindTeleportDestination to static cPawn method. * cBoundingBox interface. * Cleanup includes. * Maybe exported to API? * Change a_World to reference, add to APIDesc. Co-authored-by: x12xx12x <44411062+12xx12@users.noreply.github.com> --- src/Entities/Pawn.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'src/Entities/Pawn.cpp') 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); +} -- cgit v1.2.3