2013-07-29 11:13:03 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "BlockHandler.h"
|
2014-03-30 21:13:13 +00:00
|
|
|
#include "../FastRandom.h"
|
2015-07-02 22:16:05 +00:00
|
|
|
#include "../BlockInfo.h"
|
2014-09-26 17:13:19 +00:00
|
|
|
#include "Root.h"
|
|
|
|
#include "Bindings/PluginManager.h"
|
2013-07-29 11:13:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-30 14:50:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Handler used for all types of dirt and grass */
|
2013-07-29 11:13:03 +00:00
|
|
|
class cBlockDirtHandler :
|
|
|
|
public cBlockHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cBlockDirtHandler(BLOCKTYPE a_BlockType)
|
|
|
|
: cBlockHandler(a_BlockType)
|
|
|
|
{
|
|
|
|
}
|
2016-02-05 21:45:45 +00:00
|
|
|
|
2013-07-29 11:13:03 +00:00
|
|
|
virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override
|
|
|
|
{
|
2014-09-30 18:31:27 +00:00
|
|
|
if (a_BlockMeta == E_META_DIRT_COARSE)
|
|
|
|
{
|
|
|
|
// Drop the coarse block (dirt, meta 1)
|
2014-09-30 18:59:59 +00:00
|
|
|
a_Pickups.Add(E_BLOCK_DIRT, 1, E_META_DIRT_COARSE);
|
2014-09-30 18:31:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-30 18:59:59 +00:00
|
|
|
a_Pickups.Add(E_BLOCK_DIRT, 1, E_META_DIRT_NORMAL);
|
2014-09-30 18:31:27 +00:00
|
|
|
}
|
2013-07-29 11:13:03 +00:00
|
|
|
}
|
2016-02-05 21:45:45 +00:00
|
|
|
|
2014-02-02 14:49:37 +00: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 11:13:03 +00:00
|
|
|
{
|
|
|
|
if (m_BlockType != E_BLOCK_GRASS)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-03-05 19:29:57 +00:00
|
|
|
|
|
|
|
// Make sure that there is enough light at the source block to spread
|
|
|
|
if (!a_Chunk.GetWorld()->IsChunkLighted(a_Chunk.GetPosX(), a_Chunk.GetPosZ()))
|
|
|
|
{
|
|
|
|
a_Chunk.GetWorld()->QueueLightChunk(a_Chunk.GetPosX(), a_Chunk.GetPosZ());
|
|
|
|
return;
|
|
|
|
}
|
2015-06-22 17:29:38 +00:00
|
|
|
else if ((a_RelY < cChunkDef::Height - 1))
|
2015-03-05 19:29:57 +00:00
|
|
|
{
|
2015-07-02 22:16:05 +00:00
|
|
|
BLOCKTYPE above = a_Chunk.GetBlock(a_RelX, a_RelY + 1, a_RelZ);
|
2016-02-05 21:45:45 +00:00
|
|
|
|
2015-07-03 19:05:02 +00:00
|
|
|
// Grass turns back to dirt when the block above is not transparent
|
2015-07-02 22:16:05 +00:00
|
|
|
if (!cBlockInfo::IsTransparent(above))
|
2015-06-22 17:29:38 +00:00
|
|
|
{
|
|
|
|
a_Chunk.FastSetBlock(a_RelX, a_RelY, a_RelZ, E_BLOCK_DIRT, E_META_DIRT_NORMAL);
|
|
|
|
return;
|
|
|
|
}
|
2016-02-05 21:45:45 +00:00
|
|
|
|
2015-07-03 19:05:02 +00:00
|
|
|
NIBBLETYPE light = std::max(a_Chunk.GetBlockLight(a_RelX, a_RelY + 1, a_RelZ), a_Chunk.GetTimeAlteredLight(a_Chunk.GetSkyLight(a_RelX, a_RelY + 1, a_RelZ)));
|
2015-03-05 19:29:57 +00:00
|
|
|
// Source block is not bright enough to spread
|
2015-06-22 17:29:38 +00:00
|
|
|
if (light < 9)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-02-05 21:45:45 +00:00
|
|
|
|
2015-03-05 19:29:57 +00:00
|
|
|
}
|
|
|
|
|
2013-11-30 12:48:22 +00:00
|
|
|
// Grass spreads to adjacent dirt blocks:
|
2014-03-30 21:13:13 +00:00
|
|
|
cFastRandom rand;
|
2013-07-29 11:13:03 +00:00
|
|
|
for (int i = 0; i < 2; i++) // Pick two blocks to grow to
|
|
|
|
{
|
2015-03-14 00:59:53 +00:00
|
|
|
int OfsX = rand.NextInt(3) - 1; // [-1 .. 1]
|
|
|
|
int OfsY = rand.NextInt(5) - 3; // [-3 .. 1]
|
|
|
|
int OfsZ = rand.NextInt(3) - 1; // [-1 .. 1]
|
2016-02-05 21:45:45 +00:00
|
|
|
|
2013-07-29 11:13:03 +00:00
|
|
|
BLOCKTYPE DestBlock;
|
|
|
|
NIBBLETYPE DestMeta;
|
2015-11-10 22:06:29 +00:00
|
|
|
if (!cChunkDef::IsValidHeight(a_RelY + OfsY))
|
2013-07-29 11:13:03 +00:00
|
|
|
{
|
|
|
|
// Y Coord out of range
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-30 14:58:27 +00:00
|
|
|
int BlockX = a_RelX + OfsX;
|
|
|
|
int BlockY = a_RelY + OfsY;
|
|
|
|
int BlockZ = a_RelZ + OfsZ;
|
|
|
|
cChunk * Chunk = a_Chunk.GetRelNeighborChunkAdjustCoords(BlockX, BlockZ);
|
2014-10-20 20:55:07 +00:00
|
|
|
if (Chunk == nullptr)
|
2013-07-29 11:13:03 +00:00
|
|
|
{
|
2014-04-06 19:45:55 +00:00
|
|
|
// Unloaded chunk
|
2013-11-30 14:58:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Chunk->GetBlockTypeMeta(BlockX, BlockY, BlockZ, DestBlock, DestMeta);
|
|
|
|
if ((DestBlock != E_BLOCK_DIRT) || (DestMeta != E_META_DIRT_NORMAL))
|
|
|
|
{
|
|
|
|
// Not a regular dirt block
|
2013-07-29 11:13:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-02 22:16:05 +00:00
|
|
|
BLOCKTYPE above = a_Chunk.GetBlock(BlockX, BlockY + 1, BlockZ);
|
2015-06-22 17:29:38 +00:00
|
|
|
NIBBLETYPE light = std::max(a_Chunk.GetBlockLight(BlockX, BlockY + 1, BlockZ), a_Chunk.GetTimeAlteredLight(a_Chunk.GetSkyLight(BlockX, BlockY + 1, BlockZ)));
|
|
|
|
// Grass does not spread to blocks with a light level less than 5
|
2015-07-02 22:19:23 +00:00
|
|
|
if ((light > 4) && cBlockInfo::IsTransparent(above))
|
2013-07-29 11:13:03 +00:00
|
|
|
{
|
2014-10-15 17:01:55 +00:00
|
|
|
if (!cRoot::Get()->GetPluginManager()->CallHookBlockSpread(*Chunk->GetWorld(), Chunk->GetPosX() * cChunkDef::Width + BlockX, BlockY, Chunk->GetPosZ() * cChunkDef::Width + BlockZ, ssGrassSpread))
|
2014-03-16 15:06:03 +00:00
|
|
|
{
|
|
|
|
Chunk->FastSetBlock(BlockX, BlockY, BlockZ, E_BLOCK_GRASS, 0);
|
|
|
|
}
|
2013-07-29 11:13:03 +00:00
|
|
|
}
|
|
|
|
} // for i - repeat twice
|
|
|
|
}
|
2015-06-30 14:50:15 +00:00
|
|
|
|
|
|
|
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
|
|
|
|
{
|
|
|
|
UNUSED(a_Meta);
|
|
|
|
switch (m_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_DIRT: return 10;
|
|
|
|
case E_BLOCK_GRASS: return 1;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled blocktype in dirt handler!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 11:13:03 +00:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|