1
0
cuberite-2a/src/Blocks/BlockCrops.h

130 lines
3.0 KiB
C
Raw Normal View History

#pragma once
#include "BlockPlant.h"
#include "../FastRandom.h"
2017-02-14 05:13:55 -05:00
/** Common class that takes care of beetroots, carrots, potatoes and wheat */
template <NIBBLETYPE RipeMeta>
class cBlockCropsHandler:
public cBlockPlant<true>
{
2020-04-13 12:38:06 -04:00
using Super = cBlockPlant<true>;
public:
cBlockCropsHandler(BLOCKTYPE a_BlockType):
2020-04-13 12:38:06 -04:00
Super(a_BlockType)
{
}
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override
{
2017-06-13 15:35:30 -04:00
auto & rand = GetRandomProvider();
// If not fully grown, drop the "seed" of whatever is growing:
if (a_BlockMeta < RipeMeta)
{
switch (m_BlockType)
{
case E_BLOCK_BEETROOTS: return cItem(E_ITEM_BEETROOT_SEEDS, 1, 0); break;
case E_BLOCK_CROPS: return cItem(E_ITEM_SEEDS, 1, 0); break;
case E_BLOCK_CARROTS: return cItem(E_ITEM_CARROT, 1, 0); break;
case E_BLOCK_POTATOES: return cItem(E_ITEM_POTATO, 1, 0); break;
}
ASSERT(!"Unhandled block type");
return {};
}
// Fully grown, drop the crop's produce:
cItems res;
switch (m_BlockType)
{
case E_BLOCK_BEETROOTS:
{
2017-06-13 15:35:30 -04:00
char SeedCount = 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2); // [1 .. 3] with high preference of 2
res.Add(E_ITEM_BEETROOT_SEEDS, SeedCount, 0);
2017-06-13 15:35:30 -04:00
char BeetrootCount = 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2); // [1 .. 3] with high preference of 2
res.Add(E_ITEM_BEETROOT, BeetrootCount, 0);
break;
}
case E_BLOCK_CROPS:
{
res.Add(E_ITEM_WHEAT, 1, 0);
res.Add(E_ITEM_SEEDS, 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2), 0); // [1 .. 3] with high preference of 2
break;
}
case E_BLOCK_CARROTS:
{
res.Add(E_ITEM_CARROT, 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2), 0); // [1 .. 3] with high preference of 2
break;
}
case E_BLOCK_POTATOES:
{
res.Add(E_ITEM_POTATO, 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2), 0); // [1 .. 3] with high preference of 2
2017-06-13 15:35:30 -04:00
if (rand.RandBool(0.05))
{
// With a 5% chance, drop a poisonous potato as well
res.emplace_back(E_ITEM_POISONOUS_POTATO, 1, 0);
}
break;
}
default:
{
ASSERT(!"Unhandled block type");
break;
}
} // switch (m_BlockType)
return res;
2014-07-17 16:50:58 -04:00
}
2016-02-05 16:45:45 -05:00
virtual int Grow(cChunk & a_Chunk, Vector3i a_RelPos, int a_NumStages = 1) override
{
auto oldMeta = a_Chunk.GetMeta(a_RelPos);
if (oldMeta >= RipeMeta)
{
// Already ripe
return 0;
}
auto newMeta = std::min<int>(oldMeta + a_NumStages, RipeMeta);
ASSERT(newMeta > oldMeta);
a_Chunk.GetWorld()->SetBlock(a_Chunk.RelativeToAbsolute(a_RelPos), m_BlockType, static_cast<NIBBLETYPE>(newMeta));
return newMeta - oldMeta;
}
virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, const Vector3i a_RelPos, const cChunk & a_Chunk) override
{
return ((a_RelPos.y > 0) && (a_Chunk.GetBlock(a_RelPos.addedY(-1)) == E_BLOCK_FARMLAND));
}
2015-06-30 10:50:15 -04:00
2015-06-30 10:50:15 -04:00
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
{
UNUSED(a_Meta);
return 7;
}
} ;