1
0

Implement fortune for ores, glowstone and sea lanterns (#4897)

* Implemented fortune for ores, glowstone and sea lanterns (but nothing organic or flint).

* Cleanup printf

* Stopped playing golf, gave the Camels a FirstHump and moved the FortuneDropMult comment. Thanks for the review :).

* Got rid of FortuneDropMult and replaced with Peter's massive optimization/simplification.

* Fixed default lapis max droprate (8 -> 9).

* Clamp max drops for non-redstone ores to 10.

* Comment justifying the clamp.
This commit is contained in:
KingCol13 2020-09-20 19:06:28 +01:00 committed by GitHub
parent 80fa364fd7
commit b6b7fb1a65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 19 deletions

View File

@ -6,7 +6,6 @@
class cBlockGlowstoneHandler : class cBlockGlowstoneHandler :
public cBlockHandler public cBlockHandler
{ {
@ -25,8 +24,10 @@ private:
} }
else else
{ {
// TODO: Handle the Fortune enchantment here unsigned int DropNum = GetRandomProvider().RandInt<char>(2, 4 + ToolFortuneLevel(a_Tool));
return cItem(E_ITEM_GLOWSTONE_DUST, GetRandomProvider().RandInt<char>(2, 4), 0); // cap the dropnum to the max amount of 4
DropNum = std::min<unsigned int>(DropNum, 4);
return cItem(E_ITEM_GLOWSTONE_DUST, DropNum, 0);
} }
} }

View File

@ -649,6 +649,22 @@ bool cBlockHandler::ToolHasSilkTouch(const cItem * a_Tool)
unsigned int cBlockHandler::ToolFortuneLevel(const cItem * a_Tool)
{
if (a_Tool != nullptr)
{
return a_Tool->m_Enchantments.GetLevel(cEnchantments::enchFortune);
}
else // Not a tool
{
return 0;
}
}
const cBlockHandler & cBlockHandler::For(BLOCKTYPE a_BlockType) const cBlockHandler & cBlockHandler::For(BLOCKTYPE a_BlockType)
{ {
// Switch on the block type, as an enumeration // Switch on the block type, as an enumeration

View File

@ -230,6 +230,10 @@ public:
Helper used in many ConvertToPickups() implementations. */ Helper used in many ConvertToPickups() implementations. */
static bool ToolHasSilkTouch(const cItem * a_Tool); static bool ToolHasSilkTouch(const cItem * a_Tool);
/** Returns the fortune level of a tool, if it is a valid tool.
Can be used in ConvertToPickups() implementations. */
static unsigned int ToolFortuneLevel(const cItem * a_Tool);
// Gets the blockhandler for the given block type. // Gets the blockhandler for the given block type.
static const cBlockHandler & For(BLOCKTYPE a_BlockType); static const cBlockHandler & For(BLOCKTYPE a_BlockType);

View File

@ -3,10 +3,6 @@
#include "BlockHandler.h" #include "BlockHandler.h"
class cBlockOreHandler: class cBlockOreHandler:
public cBlockHandler public cBlockHandler
{ {
@ -31,17 +27,20 @@ private:
} }
} }
// TODO: Handle the Fortune enchantment here
auto & random = GetRandomProvider(); auto & random = GetRandomProvider();
const unsigned int FortuneLevel = ToolFortuneLevel(a_Tool);
// Clamp to 10 to prevent server crash if thing are mined with extremely high level fortune pick
const unsigned int DropMult = std::clamp<unsigned int>(FloorC(random.RandReal(FortuneLevel + 2.0)), 1, 10);
switch (m_BlockType) switch (m_BlockType)
{ {
case E_BLOCK_LAPIS_ORE: return cItem(E_ITEM_DYE, random.RandInt<char>(4, 8), 4); case E_BLOCK_LAPIS_ORE: return cItem(E_ITEM_DYE, DropMult * random.RandInt<char>(4, 9), 4);
case E_BLOCK_REDSTONE_ORE: return cItem(E_ITEM_REDSTONE_DUST, random.RandInt<char>(4, 5), 0); case E_BLOCK_REDSTONE_ORE: // handled by next case (glowing redstone), no dropMult
case E_BLOCK_REDSTONE_ORE_GLOWING: return cItem(E_ITEM_REDSTONE_DUST, random.RandInt<char>(4, 5), 0); case E_BLOCK_REDSTONE_ORE_GLOWING: return cItem(E_ITEM_REDSTONE_DUST, random.RandInt<char>(4, 5 + FortuneLevel), 0);
case E_BLOCK_DIAMOND_ORE: return cItem(E_ITEM_DIAMOND); case E_BLOCK_DIAMOND_ORE: return cItem(E_ITEM_DIAMOND, DropMult);
case E_BLOCK_EMERALD_ORE: return cItem(E_ITEM_EMERALD); case E_BLOCK_EMERALD_ORE: return cItem(E_ITEM_EMERALD, DropMult);
case E_BLOCK_COAL_ORE: return cItem(E_ITEM_COAL); case E_BLOCK_COAL_ORE: return cItem(E_ITEM_COAL, DropMult);
case E_BLOCK_NETHER_QUARTZ_ORE: return cItem(E_ITEM_NETHER_QUARTZ); case E_BLOCK_NETHER_QUARTZ_ORE: return cItem(E_ITEM_NETHER_QUARTZ, DropMult);
case E_BLOCK_CLAY: return cItem(E_ITEM_CLAY, 4); case E_BLOCK_CLAY: return cItem(E_ITEM_CLAY, 4);
default: default:
{ {

View File

@ -6,7 +6,6 @@
class cBlockSeaLanternHandler : class cBlockSeaLanternHandler :
public cBlockHandler public cBlockHandler
{ {
@ -20,8 +19,18 @@ private:
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) const override virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) const override
{ {
// Reset meta to 0 // Drop self only when using silk-touch:
// TODO: Handle the Fortune enchantment if (ToolHasSilkTouch(a_Tool))
return cItem(E_ITEM_PRISMARINE_CRYSTALS, GetRandomProvider().RandInt<char>(2, 3), 0); {
return cItem(E_BLOCK_SEA_LANTERN, 1, 0);
}
else
{
unsigned int DropNum = GetRandomProvider().RandInt<char>(2, 3 + ToolFortuneLevel(a_Tool));
// cap the dropnum to the max amount of 5
DropNum = std::min<unsigned int>(DropNum, 5);
// Reset meta to 0
return cItem(E_ITEM_PRISMARINE_CRYSTALS, DropNum, 0);
}
} }
} ; } ;