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

169 lines
3.5 KiB
C
Raw Normal View History

// BlockFarmland.h
// Declares the cBlcokFarmlandHandler representing the block handler for farmland
#pragma once
#include "BlockHandler.h"
#include "../BlockArea.h"
class cBlockFarmlandHandler :
public cBlockHandler
{
2020-04-13 12:38:06 -04:00
using Super = cBlockHandler;
public:
cBlockFarmlandHandler(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
{
return cItem(E_BLOCK_DIRT, 1, 0);
}
virtual void OnUpdate(
cChunkInterface & a_ChunkInterface,
cWorldInterface & a_WorldInterface,
cBlockPluginInterface & a_PluginInterface,
cChunk & a_Chunk,
const Vector3i a_RelPos
) override
{
auto BlockMeta = a_Chunk.GetMeta(a_RelPos);
2014-09-12 13:07:20 -04:00
if (IsWaterInNear(a_Chunk, a_RelPos))
{
2014-09-12 13:07:20 -04:00
// Water was found, set block meta to 7
a_Chunk.FastSetBlock(a_RelPos, m_BlockType, 7);
return;
}
// Water wasn't found, de-hydrate block:
if (BlockMeta > 0)
{
a_Chunk.FastSetBlock(a_RelPos, E_BLOCK_FARMLAND, --BlockMeta);
return;
}
2014-09-12 13:07:20 -04:00
// Farmland too dry. If nothing is growing on top, turn back to dirt:
auto UpperBlock = cChunkDef::IsValidHeight(a_RelPos.y + 1) ? a_Chunk.GetBlock(a_RelPos.addedY(1)) : E_BLOCK_AIR;
2014-09-12 13:07:20 -04:00
switch (UpperBlock)
{
2017-02-14 05:13:55 -05:00
case E_BLOCK_BEETROOTS:
case E_BLOCK_CROPS:
case E_BLOCK_POTATOES:
case E_BLOCK_CARROTS:
case E_BLOCK_MELON_STEM:
case E_BLOCK_PUMPKIN_STEM:
{
// Produce on top, don't revert
break;
}
default:
{
a_Chunk.SetBlock(a_RelPos, E_BLOCK_DIRT, 0);
break;
}
}
}
virtual void OnNeighborChanged(cChunkInterface & a_ChunkInterface, Vector3i a_BlockPos, eBlockFace a_WhichNeighbor) override
2014-09-12 13:07:20 -04:00
{
// Don't care about any neighbor but the one above us (fix recursion loop in #2213):
if (a_WhichNeighbor != BLOCK_FACE_YP)
{
return;
}
// Don't care about anything if we're at the top of the world:
if (a_BlockPos.y >= cChunkDef::Height)
2014-09-12 13:07:20 -04:00
{
return;
}
// Check whether we should revert to dirt:
auto upperBlock = a_ChunkInterface.GetBlock(a_BlockPos.addedY(1));
if (cBlockInfo::FullyOccupiesVoxel(upperBlock))
2014-09-12 13:07:20 -04:00
{
a_ChunkInterface.SetBlock(a_BlockPos, E_BLOCK_DIRT, 0);
2014-09-12 13:07:20 -04:00
}
}
2014-09-12 13:07:20 -04:00
/** Returns true if there's either a water source block close enough to hydrate the specified position, or it's raining there. */
bool IsWaterInNear(cChunk & a_Chunk, const Vector3i a_RelPos)
2014-09-12 13:07:20 -04:00
{
const auto WorldPos = a_Chunk.RelativeToAbsolute(a_RelPos);
if (a_Chunk.GetWorld()->IsWeatherWetAtXYZ(WorldPos))
2014-09-12 13:07:20 -04:00
{
// Rain hydrates farmland, too
2014-09-12 13:07:20 -04:00
return true;
}
// Search for water in a close proximity:
2017-08-24 05:19:40 -04:00
// Ref.: https://minecraft.gamepedia.com/Farmland#Hydration
2014-09-12 13:07:20 -04:00
// TODO: Rewrite this to use the chunk and its neighbors directly
cBlockArea Area;
if (!Area.Read(*a_Chunk.GetWorld(), WorldPos - Vector3i(4, 0, 4), WorldPos + Vector3i(4, 1, 4)))
2014-09-12 13:07:20 -04:00
{
// Too close to the world edge, cannot check surroundings
return false;
}
size_t NumBlocks = Area.GetBlockCount();
BLOCKTYPE * BlockTypes = Area.GetBlockTypes();
for (size_t i = 0; i < NumBlocks; i++)
{
if (IsBlockWater(BlockTypes[i]))
{
return true;
}
} // for i - BlockTypes[]
return false;
}
virtual bool CanSustainPlant(BLOCKTYPE a_Plant) override
{
return (
2017-02-14 05:13:55 -05:00
(a_Plant == E_BLOCK_BEETROOTS) ||
(a_Plant == E_BLOCK_CROPS) ||
(a_Plant == E_BLOCK_CARROTS) ||
(a_Plant == E_BLOCK_POTATOES) ||
(a_Plant == E_BLOCK_MELON_STEM) ||
(a_Plant == E_BLOCK_PUMPKIN_STEM)
);
}
} ;