2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-08-19 12:45:53 -04:00
|
|
|
#include "BlockPlant.h"
|
2014-03-30 17:13:13 -04:00
|
|
|
#include "../FastRandom.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-14 05:13:55 -05:00
|
|
|
/** Common class that takes care of beetroots, carrots, potatoes and wheat */
|
|
|
|
template <NIBBLETYPE RipeMeta>
|
2013-07-29 07:13:03 -04:00
|
|
|
class cBlockCropsHandler :
|
2015-08-19 12:45:53 -04:00
|
|
|
public cBlockPlant
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-08-19 12:45:53 -04:00
|
|
|
typedef cBlockPlant Super;
|
2017-02-25 04:54:44 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
public:
|
2017-02-25 04:54:44 -05:00
|
|
|
|
|
|
|
cBlockCropsHandler(BLOCKTYPE a_BlockType):
|
|
|
|
Super(a_BlockType, true)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-25 04:54:44 -05:00
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_Meta) override
|
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
auto & rand = GetRandomProvider();
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2017-02-25 04:54:44 -05:00
|
|
|
// If not fully grown, drop the "seed" of whatever is growing:
|
2017-05-09 05:11:06 -04:00
|
|
|
if (a_Meta < RipeMeta)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
switch (m_BlockType)
|
|
|
|
{
|
2017-02-14 05:13:55 -05:00
|
|
|
case E_BLOCK_BEETROOTS: a_Pickups.push_back(cItem(E_ITEM_BEETROOT_SEEDS, 1, 0)); break;
|
|
|
|
case E_BLOCK_CROPS: a_Pickups.push_back(cItem(E_ITEM_SEEDS, 1, 0)); break;
|
|
|
|
case E_BLOCK_CARROTS: a_Pickups.push_back(cItem(E_ITEM_CARROT, 1, 0)); break;
|
|
|
|
case E_BLOCK_POTATOES: a_Pickups.push_back(cItem(E_ITEM_POTATO, 1, 0)); break;
|
2013-07-29 07:13:03 -04:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled block type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-25 04:54:44 -05:00
|
|
|
return;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2017-02-25 04:54:44 -05:00
|
|
|
|
|
|
|
// Fully grown, drop the crop's produce:
|
|
|
|
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
|
|
|
|
a_Pickups.emplace_back(E_ITEM_BEETROOT_SEEDS, SeedCount, 0);
|
|
|
|
char BeetrootCount = 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2); // [1 .. 3] with high preference of 2
|
|
|
|
a_Pickups.emplace_back(E_ITEM_BEETROOT, BeetrootCount, 0);
|
2017-02-25 04:54:44 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case E_BLOCK_CROPS:
|
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
a_Pickups.emplace_back(E_ITEM_WHEAT, 1, 0);
|
|
|
|
a_Pickups.emplace_back(E_ITEM_SEEDS, 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2), 0); // [1 .. 3] with high preference of 2
|
2017-02-25 04:54:44 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case E_BLOCK_CARROTS:
|
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
a_Pickups.emplace_back(E_ITEM_CARROT, 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2), 0); // [1 .. 3] with high preference of 2
|
2017-02-25 04:54:44 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case E_BLOCK_POTATOES:
|
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
a_Pickups.emplace_back(E_ITEM_POTATO, 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2), 0); // [1 .. 3] with high preference of 2
|
|
|
|
if (rand.RandBool(0.05))
|
2017-02-25 04:54:44 -05:00
|
|
|
{
|
|
|
|
// With a 5% chance, drop a poisonous potato as well
|
2017-06-13 15:35:30 -04:00
|
|
|
a_Pickups.emplace_back(E_ITEM_POISONOUS_POTATO, 1, 0);
|
2017-02-25 04:54:44 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled block type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // switch (m_BlockType)
|
2014-07-17 16:50:58 -04:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-02-25 04:54:44 -05:00
|
|
|
|
|
|
|
|
2014-02-02 09:49:37 -05:00
|
|
|
virtual void OnUpdate(cChunkInterface & cChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-12-16 13:31:57 -05:00
|
|
|
NIBBLETYPE Meta = a_Chunk.GetMeta(a_RelX, a_RelY, a_RelZ);
|
2015-08-19 12:45:53 -04:00
|
|
|
|
|
|
|
// Check to see if the plant can grow
|
|
|
|
auto Action = CanGrow(a_Chunk, a_RelX, a_RelY, a_RelZ);
|
|
|
|
|
|
|
|
// If there is still room to grow and the plant can grow, then grow.
|
|
|
|
// Otherwise if the plant needs to die, then dig it up
|
2017-05-09 05:11:06 -04:00
|
|
|
if ((Meta < RipeMeta) && (Action == paGrowth))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-02-03 16:14:52 -05:00
|
|
|
a_Chunk.FastSetBlock(a_RelX, a_RelY, a_RelZ, m_BlockType, ++Meta);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2015-08-19 12:45:53 -04:00
|
|
|
else if (Action == paDeath)
|
2013-10-12 17:25:47 -04:00
|
|
|
{
|
2013-11-30 09:58:27 -05:00
|
|
|
a_Chunk.GetWorld()->DigBlock(a_RelX + a_Chunk.GetPosX() * cChunkDef::Width, a_RelY, a_RelZ + a_Chunk.GetPosZ() * cChunkDef::Width);
|
2013-10-12 17:25:47 -04:00
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
2017-02-25 04:54:44 -05:00
|
|
|
|
|
|
|
|
2014-02-01 08:06:32 -05:00
|
|
|
virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
return ((a_RelY > 0) && (a_Chunk.GetBlock(a_RelX, a_RelY - 1, a_RelZ) == E_BLOCK_FARMLAND));
|
|
|
|
}
|
2015-06-30 10:50:15 -04:00
|
|
|
|
2017-02-25 04:54:44 -05:00
|
|
|
|
|
|
|
|
2015-06-30 10:50:15 -04:00
|
|
|
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
|
|
|
|
{
|
|
|
|
UNUSED(a_Meta);
|
|
|
|
return 7;
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|