diff options
author | KingCol13 <48412633+KingCol13@users.noreply.github.com> | 2021-07-10 22:05:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-10 22:05:00 +0200 |
commit | 1d1fa914012125126d1a3656edc428859696f54f (patch) | |
tree | d0190a09b6f519ce528e6d8e25d99141a3faa29f /src | |
parent | Symmetry in MultiVersionProtocol to fix a crash (#5261) (diff) | |
download | cuberite-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/ClientHandle.cpp | 3 | ||||
-rw-r--r-- | src/Entities/Pawn.cpp | 78 | ||||
-rw-r--r-- | src/Entities/Pawn.h | 15 | ||||
-rw-r--r-- | src/Items/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/Items/ItemChorusFruit.h | 44 | ||||
-rw-r--r-- | src/Items/ItemHandler.cpp | 3 |
6 files changed, 142 insertions, 2 deletions
diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 9dc112011..e5f001116 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -1756,7 +1756,8 @@ void cClientHandle::HandleUseItem(bool a_UsedMainHand) if ( ItemHandler->IsFood() && (m_Player->IsSatiated() || m_Player->IsGameModeCreative()) && // Only non-creative or hungry players can eat - (HeldItem.m_ItemType != E_ITEM_GOLDEN_APPLE) // Golden apple is a special case, it is used instead of eaten + (HeldItem.m_ItemType != E_ITEM_GOLDEN_APPLE) && // Golden apple is a special case, it is used instead of eaten + (HeldItem.m_ItemType != E_ITEM_CHORUS_FRUIT) // Chorus fruit is a special case, it is used instead of eaten ) { // The player is satiated or in creative, and trying to eat 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; diff --git a/src/Items/CMakeLists.txt b/src/Items/CMakeLists.txt index 3f182dd76..0dc7f8365 100644 --- a/src/Items/CMakeLists.txt +++ b/src/Items/CMakeLists.txt @@ -15,6 +15,7 @@ target_sources( ItemBucket.h ItemButton.h ItemChest.h + ItemChorusFruit.h ItemCloth.h ItemComparator.h ItemCookedFish.h diff --git a/src/Items/ItemChorusFruit.h b/src/Items/ItemChorusFruit.h new file mode 100644 index 000000000..261088411 --- /dev/null +++ b/src/Items/ItemChorusFruit.h @@ -0,0 +1,44 @@ + +#pragma once + +#include "ItemFood.h" +#include "../Entities/Pawn.h" + + + + + +class cItemChorusFruitHandler: + public cItemFoodHandler +{ + using Super = cItemFoodHandler; + +public: + + cItemChorusFruitHandler(): + Super(E_ITEM_CHORUS_FRUIT, FoodInfo(4, 2.4)) + { + } + + virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) override + { + cItemHandler::EatItem(a_Player, a_Item); + + if (!a_Player->IsGameModeCreative()) + { + a_Player->GetInventory().RemoveOneEquippedItem(); + } + + // Attempt to find a teleport destination + Vector3d Destination; + cWorld * World = a_Player->GetWorld(); + if (cPawn::FindTeleportDestination(*World, 2, 16, Destination, a_Player->GetPosition(), 8)) + { + // Broadcast sound effect to _pre-teleport_ location, then teleport player. + World->BroadcastSoundEffect("item.chorus_fruit.teleport", a_Player->GetPosition(), 1, 1); + a_Player->TeleportToCoords(Destination.x, Destination.y, Destination.z); + } + return true; + } + +}; diff --git a/src/Items/ItemHandler.cpp b/src/Items/ItemHandler.cpp index 1c6300353..63f950e26 100644 --- a/src/Items/ItemHandler.cpp +++ b/src/Items/ItemHandler.cpp @@ -20,6 +20,7 @@ #include "ItemBucket.h" #include "ItemButton.h" #include "ItemChest.h" +#include "ItemChorusFruit.h" #include "ItemCloth.h" #include "ItemComparator.h" #include "ItemCookedFish.h" @@ -354,7 +355,6 @@ cItemHandler * cItemHandler::CreateItemHandler(int a_ItemType) case E_ITEM_BAKED_POTATO: return new cItemFoodHandler(a_ItemType, FoodInfo(5, 6)); case E_ITEM_BEETROOT: return new cItemFoodHandler(a_ItemType, FoodInfo(1, 1.2)); case E_ITEM_BREAD: return new cItemFoodHandler(a_ItemType, FoodInfo(5, 6)); - case E_ITEM_CHORUS_FRUIT: return new cItemFoodHandler(a_ItemType, FoodInfo(4, 2.4)); case E_ITEM_COOKED_CHICKEN: return new cItemFoodHandler(a_ItemType, FoodInfo(6, 7.2)); case E_ITEM_COOKED_MUTTON: return new cItemFoodHandler(a_ItemType, FoodInfo(6, 9.6)); case E_ITEM_COOKED_PORKCHOP: return new cItemFoodHandler(a_ItemType, FoodInfo(8, 12.8)); @@ -371,6 +371,7 @@ cItemHandler * cItemHandler::CreateItemHandler(int a_ItemType) case E_ITEM_STEAK: return new cItemFoodHandler(a_ItemType, FoodInfo(8, 12.8)); // Special-case food with their own handler + case E_ITEM_CHORUS_FRUIT: return new cItemChorusFruitHandler(); case E_ITEM_COOKED_FISH: return new cItemCookedFishHandler(); case E_ITEM_GOLDEN_APPLE: return new cItemGoldenAppleHandler(); case E_ITEM_POISONOUS_POTATO: return new cItemPoisonousPotatoHandler(); |