2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "RedstoneHandler.h"
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../../Blocks/BlockTripwireHook.h"
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cTripwireHookHandler : public cRedstoneHandler
|
|
|
|
{
|
|
|
|
typedef cRedstoneHandler super;
|
|
|
|
public:
|
|
|
|
|
2017-09-07 04:25:34 -04:00
|
|
|
virtual unsigned char GetPowerDeliveredToPosition(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType) const override
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
|
|
|
UNUSED(a_QueryBlockType);
|
|
|
|
UNUSED(a_QueryPosition);
|
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
return (GetPowerLevel(a_World, a_Position, a_BlockType, a_Meta) == 15) ? 15 : 0;
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
|
|
|
|
2017-09-07 04:25:34 -04:00
|
|
|
virtual unsigned char GetPowerLevel(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) const override
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
|
|
|
UNUSED(a_BlockType);
|
|
|
|
|
|
|
|
bool FoundActivated = false;
|
|
|
|
auto Position = a_Position;
|
|
|
|
eBlockFace FaceToGoTowards = cBlockTripwireHookHandler::MetadataToDirection(a_Meta);
|
|
|
|
|
|
|
|
for (int i = 0; i < 40; ++i) // Tripwires can be connected up to 40 blocks
|
|
|
|
{
|
|
|
|
BLOCKTYPE Type;
|
|
|
|
NIBBLETYPE Meta;
|
|
|
|
|
|
|
|
AddFaceDirection(Position.x, Position.y, Position.z, FaceToGoTowards);
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.GetBlockTypeMeta(Position.x, Position.y, Position.z, Type, Meta);
|
2015-06-26 18:24:51 -04:00
|
|
|
|
|
|
|
if (Type == E_BLOCK_TRIPWIRE)
|
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
if (!a_World.ForEachEntityInBox(cBoundingBox(Vector3d(0.5, 0, 0.5) + Position, 0.5, 0.5), [](cEntity &) { return true; }))
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
|
|
|
FoundActivated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Type == E_BLOCK_TRIPWIRE_HOOK)
|
|
|
|
{
|
|
|
|
if (ReverseBlockFace(cBlockTripwireHookHandler::MetadataToDirection(Meta)) == FaceToGoTowards)
|
|
|
|
{
|
|
|
|
// Other hook facing in opposite direction - circuit completed!
|
|
|
|
return FoundActivated ? 15 : 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Tripwire hook not connected at all
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Tripwire hook not connected at all
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-07 04:25:34 -04:00
|
|
|
virtual cVector3iArray Update(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) const override
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
|
|
|
// LOGD("Evaluating hooky the tripwire hook (%d %d %d)", a_Position.x, a_Position.y, a_Position.z);
|
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
auto Power = GetPowerLevel(a_World, a_Position, a_BlockType, a_Meta);
|
2015-06-26 18:24:51 -04:00
|
|
|
NIBBLETYPE Meta;
|
|
|
|
if (Power == 0)
|
|
|
|
{
|
|
|
|
Meta = (a_Meta & 0x3);
|
|
|
|
}
|
|
|
|
else if (Power == 1)
|
|
|
|
{
|
|
|
|
// Connected but not activated, AND away the highest bit
|
|
|
|
Meta = (a_Meta & 0x7) | 0x4;
|
|
|
|
}
|
|
|
|
else if (Power == 15)
|
|
|
|
{
|
|
|
|
// Connected and activated, set the 3rd and 4th highest bits
|
|
|
|
Meta = (a_Meta | 0xC);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT(!"Unexpected tripwire hook power level!");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Meta != a_Meta)
|
|
|
|
{
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.SetBlockMeta(a_Position, Meta);
|
2015-06-26 18:24:51 -04:00
|
|
|
return GetAdjustedRelatives(a_Position, GetRelativeAdjacents());
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-06-26 18:24:51 -04:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-09-07 04:25:34 -04:00
|
|
|
virtual cVector3iArray GetValidSourcePositions(cWorld & a_World, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) const override
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2017-07-14 22:09:55 -04:00
|
|
|
UNUSED(a_World);
|
2015-06-26 18:24:51 -04:00
|
|
|
UNUSED(a_BlockType);
|
|
|
|
UNUSED(a_Meta);
|
|
|
|
UNUSED(a_Position);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|