2013-09-22 14:34:42 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "BlockHandler.h"
|
2020-04-03 02:57:01 -04:00
|
|
|
#include "../BlockInfo.h"
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../Chunk.h"
|
2019-10-16 04:06:34 -04:00
|
|
|
#include "Mixins.h"
|
2020-05-03 16:05:32 -04:00
|
|
|
#include "ChunkInterface.h"
|
2013-09-22 14:34:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-05 06:27:14 -04:00
|
|
|
class cBlockButtonHandler final :
|
2019-10-16 04:06:34 -04:00
|
|
|
public cClearMetaOnDrop<cMetaRotator<cBlockHandler, 0x07, 0x04, 0x01, 0x03, 0x02, true>>
|
2013-09-22 14:34:42 -04:00
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
using Super = cClearMetaOnDrop<cMetaRotator<cBlockHandler, 0x07, 0x04, 0x01, 0x03, 0x02, true>>;
|
2019-10-16 04:06:34 -04:00
|
|
|
|
2013-09-22 14:34:42 -04:00
|
|
|
public:
|
2019-10-16 04:06:34 -04:00
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
using Super::Super;
|
|
|
|
|
|
|
|
/** Extracts the ON bit from metadata and returns if true if it is set */
|
|
|
|
static bool IsButtonOn(NIBBLETYPE a_Meta)
|
2013-11-29 17:25:07 -05:00
|
|
|
{
|
2020-09-20 09:50:52 -04:00
|
|
|
return (a_Meta & 0x08) == 0x08;
|
2013-11-29 17:25:07 -05:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
/** Event handler for an arrow striking a block.
|
|
|
|
Performs appropriate handling if the arrow intersected a wooden button. */
|
|
|
|
static void OnArrowHit(cWorld & a_World, const Vector3i a_Position, const eBlockFace a_HitFace)
|
|
|
|
{
|
|
|
|
BLOCKTYPE Type;
|
|
|
|
NIBBLETYPE Meta;
|
|
|
|
const auto Pos = AddFaceDirection(a_Position, a_HitFace);
|
2019-10-16 04:06:34 -04:00
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
if (
|
|
|
|
!a_World.GetBlockTypeMeta(Pos, Type, Meta) ||
|
|
|
|
IsButtonOn(Meta) ||
|
|
|
|
!IsButtonPressedByArrow(a_World, Pos, Type, Meta)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Bail if we're not specifically a wooden button, or it's already on
|
|
|
|
// or if the arrow didn't intersect. It is very important that nothing is
|
|
|
|
// done if the button is depressed, since the release task will already be queued
|
|
|
|
return;
|
|
|
|
}
|
2019-10-16 04:06:34 -04:00
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
a_World.SetBlockMeta(Pos, Meta | 0x08);
|
|
|
|
a_World.WakeUpSimulators(Pos);
|
2019-10-16 04:06:34 -04:00
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
// sound name is ok to be wood, because only wood gets triggered by arrow
|
|
|
|
a_World.GetBroadcastManager().BroadcastSoundEffect("block.wood_button.click_on", Pos, 0.5f, 0.6f);
|
|
|
|
|
|
|
|
// Queue a button reset
|
|
|
|
QueueButtonRelease(a_World, Pos, Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-10-16 04:06:34 -04:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
virtual bool OnUse(
|
|
|
|
cChunkInterface & a_ChunkInterface,
|
|
|
|
cWorldInterface & a_WorldInterface,
|
|
|
|
cPlayer & a_Player,
|
|
|
|
const Vector3i a_BlockPos,
|
|
|
|
eBlockFace a_BlockFace,
|
|
|
|
const Vector3i a_CursorPos
|
2020-09-20 09:50:52 -04:00
|
|
|
) const override
|
2013-11-29 17:25:07 -05:00
|
|
|
{
|
2020-04-21 16:19:22 -04:00
|
|
|
NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(a_BlockPos);
|
2015-06-22 22:00:15 -04:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
// If button is already on, do nothing:
|
2020-05-03 16:05:32 -04:00
|
|
|
if (IsButtonOn(Meta))
|
2015-06-22 22:00:15 -04:00
|
|
|
{
|
2015-12-01 17:12:44 -05:00
|
|
|
return false;
|
2015-06-22 22:00:15 -04:00
|
|
|
}
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
// Set the ON bit to on
|
2015-06-22 22:00:15 -04:00
|
|
|
Meta |= 0x08;
|
2013-11-29 17:25:07 -05:00
|
|
|
|
2020-05-03 16:05:32 -04:00
|
|
|
const auto SoundToPlay = (m_BlockType == E_BLOCK_STONE_BUTTON) ? "block.stone_button.click_on" : "block.wood_button.click_on";
|
|
|
|
|
2020-08-26 16:45:13 -04:00
|
|
|
a_ChunkInterface.SetBlockMeta(a_BlockPos, Meta);
|
2020-08-08 13:22:16 -04:00
|
|
|
a_WorldInterface.WakeUpSimulators(a_BlockPos);
|
2020-05-03 16:05:32 -04:00
|
|
|
a_WorldInterface.GetBroadcastManager().BroadcastSoundEffect(SoundToPlay, a_BlockPos, 0.5f, 0.6f, a_Player.GetClientHandle());
|
2013-11-29 17:25:07 -05:00
|
|
|
|
|
|
|
// Queue a button reset (unpress)
|
2020-05-03 16:05:32 -04:00
|
|
|
QueueButtonRelease(*a_Player.GetWorld(), a_BlockPos, m_BlockType);
|
2015-12-01 17:12:44 -05:00
|
|
|
|
|
|
|
return true;
|
2013-11-29 17:25:07 -05:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
virtual bool IsUseable(void) const override
|
2013-09-22 14:34:42 -04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-22 14:34:42 -04:00
|
|
|
virtual bool GetPlacementBlockTypeMeta(
|
2020-04-21 16:19:22 -04:00
|
|
|
cChunkInterface & a_ChunkInterface,
|
|
|
|
cPlayer & a_Player,
|
|
|
|
const Vector3i a_PlacedBlockPos,
|
|
|
|
eBlockFace a_ClickedBlockFace,
|
|
|
|
const Vector3i a_CursorPos,
|
2013-09-22 14:34:42 -04:00
|
|
|
BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
|
2020-09-20 09:50:52 -04:00
|
|
|
) const override
|
2013-09-22 14:34:42 -04:00
|
|
|
{
|
|
|
|
a_BlockType = m_BlockType;
|
2020-04-21 16:19:22 -04:00
|
|
|
a_BlockMeta = BlockFaceToMetaData(a_ClickedBlockFace);
|
2013-09-22 14:34:42 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Converts the block face of the neighbor to which the button is attached, to the block meta for this button. */
|
2014-02-04 13:59:05 -05:00
|
|
|
inline static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_BlockFace)
|
2013-09-22 14:34:42 -04:00
|
|
|
{
|
|
|
|
switch (a_BlockFace)
|
|
|
|
{
|
2015-03-25 16:12:02 -04:00
|
|
|
case BLOCK_FACE_YP: return 0x5;
|
2013-11-29 17:25:07 -05:00
|
|
|
case BLOCK_FACE_ZM: return 0x4;
|
|
|
|
case BLOCK_FACE_ZP: return 0x3;
|
|
|
|
case BLOCK_FACE_XM: return 0x2;
|
|
|
|
case BLOCK_FACE_XP: return 0x1;
|
2015-03-09 18:13:55 -04:00
|
|
|
case BLOCK_FACE_YM: return 0x0;
|
2015-05-19 06:50:59 -04:00
|
|
|
case BLOCK_FACE_NONE:
|
2013-09-22 14:34:42 -04:00
|
|
|
{
|
2021-02-20 11:24:13 -05:00
|
|
|
break;
|
2013-09-22 14:34:42 -04:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 18:07:12 -05:00
|
|
|
UNREACHABLE("Unsupported block face");
|
2013-09-22 14:34:42 -04:00
|
|
|
}
|
2013-11-18 17:30:34 -05:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Converts the block meta of this button into a block face of the neighbor to which the button is attached. */
|
2014-02-04 13:59:05 -05:00
|
|
|
inline static eBlockFace BlockMetaDataToBlockFace(NIBBLETYPE a_Meta)
|
2013-11-18 17:30:34 -05:00
|
|
|
{
|
|
|
|
switch (a_Meta & 0x7)
|
|
|
|
{
|
2015-03-25 16:12:02 -04:00
|
|
|
case 0x0: return BLOCK_FACE_YM;
|
2013-11-18 17:30:34 -05:00
|
|
|
case 0x1: return BLOCK_FACE_XP;
|
|
|
|
case 0x2: return BLOCK_FACE_XM;
|
|
|
|
case 0x3: return BLOCK_FACE_ZP;
|
|
|
|
case 0x4: return BLOCK_FACE_ZM;
|
2015-03-25 16:12:02 -04:00
|
|
|
case 0x5: return BLOCK_FACE_YP;
|
2013-11-18 17:30:34 -05:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled block meta!");
|
2014-02-08 19:57:22 -05:00
|
|
|
return BLOCK_FACE_NONE;
|
2013-11-18 17:30:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-21 17:44:18 -05:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, const Vector3i a_RelPos, const cChunk & a_Chunk) const override
|
2020-04-21 16:19:22 -04:00
|
|
|
{
|
|
|
|
auto Meta = a_Chunk.GetMeta(a_RelPos);
|
|
|
|
auto SupportRelPos = AddFaceDirection(a_RelPos, BlockMetaDataToBlockFace(Meta), true);
|
|
|
|
if (!cChunkDef::IsValidHeight(SupportRelPos.y))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BLOCKTYPE SupportBlockType;
|
|
|
|
a_Chunk.UnboundedRelGetBlockType(SupportRelPos, SupportBlockType);
|
|
|
|
|
|
|
|
return cBlockInfo::FullyOccupiesVoxel(SupportBlockType);
|
2013-11-18 17:30:34 -05:00
|
|
|
}
|
2015-06-30 10:50:15 -04:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
|
2015-06-30 10:50:15 -04:00
|
|
|
{
|
|
|
|
UNUSED(a_Meta);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-26 18:24:51 -04:00
|
|
|
|
2020-05-03 16:05:32 -04:00
|
|
|
/** Schedules a recurring event at appropriate intervals to release a button at a given position.
|
|
|
|
The given block type is checked when the task is executed to ensure the position still contains a button. */
|
|
|
|
static void QueueButtonRelease(cWorld & a_ButtonWorld, const Vector3i a_Position, const BLOCKTYPE a_BlockType)
|
2015-06-26 18:24:51 -04:00
|
|
|
{
|
2020-05-03 16:05:32 -04:00
|
|
|
const auto TickDelay = (a_BlockType == E_BLOCK_STONE_BUTTON) ? 20 : 30;
|
|
|
|
a_ButtonWorld.ScheduleTask(
|
|
|
|
TickDelay,
|
|
|
|
[a_Position, a_BlockType](cWorld & a_World)
|
|
|
|
{
|
|
|
|
BLOCKTYPE Type;
|
|
|
|
NIBBLETYPE Meta;
|
|
|
|
|
|
|
|
if (
|
|
|
|
!a_World.GetBlockTypeMeta(a_Position, Type, Meta) ||
|
|
|
|
(Type != a_BlockType) || !IsButtonOn(Meta)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Total failure or block changed, bail
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsButtonPressedByArrow(a_World, a_Position, Type, Meta))
|
|
|
|
{
|
|
|
|
// Try again in a little while
|
|
|
|
QueueButtonRelease(a_World, a_Position, a_BlockType);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block hasn't change in the meantime; release it
|
|
|
|
const auto SoundToPlayOnRelease = (Type == E_BLOCK_STONE_BUTTON) ? "block.stone_button.click_off" : "block.wood_button.click_off";
|
|
|
|
|
2020-08-26 16:45:13 -04:00
|
|
|
a_World.SetBlockMeta(a_Position, Meta & 0x07);
|
2020-08-08 13:22:16 -04:00
|
|
|
a_World.WakeUpSimulators(a_Position);
|
2020-05-03 16:05:32 -04:00
|
|
|
a_World.BroadcastSoundEffect(SoundToPlayOnRelease, a_Position, 0.5f, 0.5f);
|
|
|
|
}
|
|
|
|
);
|
2015-06-26 18:24:51 -04:00
|
|
|
}
|
2013-09-22 14:34:42 -04:00
|
|
|
|
2020-05-03 16:05:32 -04:00
|
|
|
/** Returns true if an arrow was found in the wooden button */
|
|
|
|
static bool IsButtonPressedByArrow(cWorld & a_World, const Vector3i a_ButtonPosition, const BLOCKTYPE a_BlockType, const NIBBLETYPE a_Meta)
|
|
|
|
{
|
|
|
|
if (a_BlockType != E_BLOCK_WOODEN_BUTTON)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto FaceOffset = GetButtonOffsetOnBlock(a_Meta);
|
|
|
|
const bool FoundArrow = !a_World.ForEachEntityInBox(
|
|
|
|
cBoundingBox(FaceOffset + a_ButtonPosition, 0.2, 0.2),
|
|
|
|
[](cEntity & a_Entity)
|
|
|
|
{
|
|
|
|
return a_Entity.IsArrow();
|
|
|
|
}
|
|
|
|
);
|
2013-09-22 14:34:42 -04:00
|
|
|
|
2020-05-03 16:05:32 -04:00
|
|
|
return FoundArrow;
|
|
|
|
}
|
2013-09-22 14:34:42 -04:00
|
|
|
|
2020-05-03 16:05:32 -04:00
|
|
|
/** Returns an offset to the integer world coordinates of a button.
|
|
|
|
Applying this offset yields the centre of the button's bounding box,
|
|
|
|
in terms of the position within the block the button with given meta occupies.
|
2013-09-22 14:34:42 -04:00
|
|
|
|
2020-05-03 16:05:32 -04:00
|
|
|
TODO: this is only approximate, return the exact bbox instead. */
|
|
|
|
static Vector3d GetButtonOffsetOnBlock(NIBBLETYPE a_Meta)
|
|
|
|
{
|
|
|
|
switch (BlockMetaDataToBlockFace(a_Meta))
|
|
|
|
{
|
|
|
|
case BLOCK_FACE_YM: return { 0.5, 1, 0.5 };
|
|
|
|
case BLOCK_FACE_XP: return { 0, 0.5, 0.5 };
|
|
|
|
case BLOCK_FACE_XM: return { 1, 0.5, 0.5 };
|
|
|
|
case BLOCK_FACE_ZP: return { 0.5, 0.5, 0 };
|
|
|
|
case BLOCK_FACE_ZM: return { 0.5, 0.5, 1 };
|
|
|
|
case BLOCK_FACE_YP: return { 0.5, 0, 0.5 };
|
|
|
|
case BLOCK_FACE_NONE:
|
|
|
|
{
|
2021-02-20 11:24:13 -05:00
|
|
|
break;
|
2020-05-03 16:05:32 -04:00
|
|
|
}
|
|
|
|
}
|
2021-02-20 11:24:13 -05:00
|
|
|
UNREACHABLE("Unhandled block face!");
|
2020-05-03 16:05:32 -04:00
|
|
|
}
|
|
|
|
} ;
|