2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "BlockHandler.h"
|
2020-04-03 02:57:01 -04:00
|
|
|
#include "../BlockInfo.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Player.h"
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../Chunk.h"
|
2019-10-16 04:06:34 -04:00
|
|
|
#include "Mixins.h"
|
2014-09-26 13:13:19 -04:00
|
|
|
#include "ChunkInterface.h"
|
2015-04-07 11:51:38 -04:00
|
|
|
#include "BlockSlab.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cBlockDoorHandler :
|
2020-04-10 19:20:51 -04:00
|
|
|
public cYawRotator<cBlockHandler, 0x03, 0x03, 0x00, 0x01, 0x02>
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
using Super = cYawRotator<cBlockHandler, 0x03, 0x03, 0x00, 0x01, 0x02>;
|
2019-10-16 04:06:34 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
public:
|
2019-10-16 04:06:34 -04:00
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
using Super::Super;
|
|
|
|
|
|
|
|
/** Returns true if door can be placed on the specified block type. */
|
|
|
|
static bool CanBeOn(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
|
|
|
{
|
|
|
|
// Vanilla refuses to place doors on transparent blocks, except top-half slabs and other doors
|
|
|
|
// We need to keep the door compatible with itself, otherwise the top half drops while the bottom half stays
|
|
|
|
|
|
|
|
// Doors can be placed on upside-down slabs
|
|
|
|
if (cBlockSlabHandler::IsAnySlabType(a_BlockType) && ((a_BlockMeta & 0x08) != 0))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Doors can also be placed on other doors
|
|
|
|
else if (IsDoorBlockType(a_BlockType))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Doors can not be placed on transparent blocks, but on any other block
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return !cBlockInfo::IsTransparent(a_BlockType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool CanReplaceBlock(BLOCKTYPE a_BlockType)
|
|
|
|
{
|
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_AIR:
|
|
|
|
case E_BLOCK_TALL_GRASS:
|
|
|
|
case E_BLOCK_WATER:
|
|
|
|
case E_BLOCK_STATIONARY_WATER:
|
|
|
|
case E_BLOCK_LAVA:
|
|
|
|
case E_BLOCK_STATIONARY_LAVA:
|
|
|
|
case E_BLOCK_SNOW:
|
|
|
|
case E_BLOCK_FIRE:
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a vector pointing one block in the direction the door is facing (where the outside is). */
|
|
|
|
inline static Vector3i GetRelativeDirectionToOutside(NIBBLETYPE a_BlockMeta)
|
|
|
|
{
|
|
|
|
switch (a_BlockMeta & 0x03)
|
|
|
|
{
|
|
|
|
case 0: return Vector3i(-1, 0, 0); // Facing West / XM
|
|
|
|
case 1: return Vector3i(0, 0, -1); // Facing North / ZM
|
|
|
|
case 2: return Vector3i(1, 0, 0); // Facing East / XP
|
|
|
|
default: return Vector3i(0, 0, 1); // Facing South / ZP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns true if the specified blocktype is any kind of door */
|
|
|
|
inline static bool IsDoorBlockType(BLOCKTYPE a_Block)
|
|
|
|
{
|
|
|
|
switch (a_Block)
|
|
|
|
{
|
|
|
|
case E_BLOCK_ACACIA_DOOR:
|
|
|
|
case E_BLOCK_BIRCH_DOOR:
|
|
|
|
case E_BLOCK_DARK_OAK_DOOR:
|
|
|
|
case E_BLOCK_IRON_DOOR:
|
|
|
|
case E_BLOCK_JUNGLE_DOOR:
|
|
|
|
case E_BLOCK_SPRUCE_DOOR:
|
|
|
|
case E_BLOCK_OAK_DOOR:
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 17:04:57 -04:00
|
|
|
/** Returns true iff the door at the specified coords is open.
|
|
|
|
The coords may point to either the top part or the bottom part of the door. */
|
|
|
|
static bool IsOpen(cChunkInterface & a_ChunkInterface, const Vector3i a_BlockPos)
|
|
|
|
{
|
|
|
|
const auto Meta = GetCompleteDoorMeta(a_ChunkInterface, a_BlockPos);
|
|
|
|
return (Meta & 0x04) != 0;
|
|
|
|
}
|
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
/** Sets the door to the specified state. If the door is already in that state, does nothing. */
|
|
|
|
static void SetOpen(cChunkInterface & a_ChunkInterface, const Vector3i a_BlockPos, bool a_Open)
|
|
|
|
{
|
|
|
|
BLOCKTYPE Block = a_ChunkInterface.GetBlock(a_BlockPos);
|
|
|
|
if (!IsDoorBlockType(Block))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NIBBLETYPE Meta = GetCompleteDoorMeta(a_ChunkInterface, a_BlockPos);
|
|
|
|
bool IsOpened = ((Meta & 0x04) != 0);
|
|
|
|
if (IsOpened == a_Open)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the door
|
|
|
|
NIBBLETYPE NewMeta = (Meta & 0x07) ^ 0x04; // Flip the "IsOpen" bit (0x04)
|
|
|
|
if ((Meta & 0x08) == 0)
|
|
|
|
{
|
|
|
|
// The block is the bottom part of the door
|
|
|
|
a_ChunkInterface.SetBlockMeta(a_BlockPos, NewMeta);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The block is the top part of the door, set the meta to the corresponding top part
|
|
|
|
if (a_BlockPos.y > 0)
|
|
|
|
{
|
|
|
|
a_ChunkInterface.SetBlockMeta(a_BlockPos.addedY(-1), NewMeta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
virtual void OnBroken(
|
|
|
|
cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
|
|
|
|
Vector3i a_BlockPos,
|
|
|
|
BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta
|
2020-09-20 09:50:52 -04:00
|
|
|
) const override;
|
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;
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
virtual void OnCancelRightClick(
|
|
|
|
cChunkInterface & a_ChunkInterface,
|
|
|
|
cWorldInterface & a_WorldInterface,
|
|
|
|
cPlayer & a_Player,
|
|
|
|
const Vector3i a_BlockPos,
|
|
|
|
eBlockFace a_BlockFace
|
2020-09-20 09:50:52 -04:00
|
|
|
) const override;
|
2014-05-29 13:21:56 -04:00
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
virtual NIBBLETYPE MetaRotateCCW(NIBBLETYPE a_Meta) const override;
|
|
|
|
virtual NIBBLETYPE MetaRotateCW(NIBBLETYPE a_Meta) const override;
|
|
|
|
virtual NIBBLETYPE MetaMirrorXY(NIBBLETYPE a_Meta) const override;
|
|
|
|
virtual NIBBLETYPE MetaMirrorYZ(NIBBLETYPE a_Meta) const override;
|
2014-05-29 13:21:56 -04:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -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-07-29 07:13:03 -04:00
|
|
|
BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
|
2020-09-20 09:50:52 -04:00
|
|
|
) const override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// If clicking a bottom face, place the door one block lower:
|
2020-04-21 16:19:22 -04:00
|
|
|
auto PlacedPos = a_PlacedBlockPos;
|
|
|
|
if (a_ClickedBlockFace == BLOCK_FACE_BOTTOM)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2020-04-21 16:19:22 -04:00
|
|
|
PlacedPos.y--;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2020-04-21 16:19:22 -04:00
|
|
|
!CanReplaceBlock(a_ChunkInterface.GetBlock(PlacedPos)) ||
|
|
|
|
!CanReplaceBlock(a_ChunkInterface.GetBlock(PlacedPos.addedY(1)))
|
2013-07-29 07:13:03 -04:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
return Super::GetPlacementBlockTypeMeta(a_ChunkInterface, a_Player, PlacedPos, a_ClickedBlockFace, a_CursorPos, a_BlockType, a_BlockMeta);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
virtual cBoundingBox GetPlacementCollisionBox(BLOCKTYPE a_XM, BLOCKTYPE a_XP, BLOCKTYPE a_YM, BLOCKTYPE a_YP, BLOCKTYPE a_ZM, BLOCKTYPE a_ZP) const override;
|
2017-07-28 12:59:21 -04:00
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-23 11:06:27 -04:00
|
|
|
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, const cEntity * a_Digger, const cItem * a_Tool) const override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-09-27 08:19:45 -04:00
|
|
|
switch (m_BlockType)
|
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
case E_BLOCK_OAK_DOOR: return cItem(E_ITEM_WOODEN_DOOR);
|
|
|
|
case E_BLOCK_ACACIA_DOOR: return cItem(E_ITEM_ACACIA_DOOR);
|
|
|
|
case E_BLOCK_BIRCH_DOOR: return cItem(E_ITEM_BIRCH_DOOR);
|
|
|
|
case E_BLOCK_DARK_OAK_DOOR: return cItem(E_ITEM_DARK_OAK_DOOR);
|
|
|
|
case E_BLOCK_JUNGLE_DOOR: return cItem(E_ITEM_JUNGLE_DOOR);
|
|
|
|
case E_BLOCK_SPRUCE_DOOR: return cItem(E_ITEM_SPRUCE_DOOR);
|
|
|
|
case E_BLOCK_IRON_DOOR: return cItem(E_ITEM_IRON_DOOR);
|
2014-09-27 09:11:35 -04:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled door type!");
|
2019-10-16 04:06:34 -04:00
|
|
|
return {};
|
2014-09-27 09:11:35 -04:00
|
|
|
}
|
2014-09-27 08:19:45 -04:00
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-20 09:50:52 -04:00
|
|
|
virtual bool IsUseable(void) const override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2020-04-21 16:19:22 -04:00
|
|
|
return ((a_RelPos.y > 0) && CanBeOn(a_Chunk.GetBlock(a_RelPos.addedY(-1)), a_Chunk.GetMeta(a_RelPos.addedY(-1))));
|
2014-12-24 01:20:17 -05:00
|
|
|
}
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-09 15:35:46 -04:00
|
|
|
/** Returns the complete meta composed from the both parts of the door as (TopMeta << 4) | BottomMeta
|
|
|
|
The coords may point to either part of the door.
|
|
|
|
The returned value has bit 3 (0x08) set iff the coords point to the top part of the door.
|
2020-04-21 16:19:22 -04:00
|
|
|
Fails silently for (invalid) doors on the world's top and bottom. */
|
|
|
|
static NIBBLETYPE GetCompleteDoorMeta(cChunkInterface & a_ChunkInterface, const Vector3i a_BlockPos)
|
2014-05-29 13:21:56 -04:00
|
|
|
{
|
2020-04-21 16:19:22 -04:00
|
|
|
NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(a_BlockPos);
|
2014-05-29 13:21:56 -04:00
|
|
|
|
2014-06-09 15:35:46 -04:00
|
|
|
if ((Meta & 0x08) != 0)
|
2014-05-29 13:21:56 -04:00
|
|
|
{
|
2014-06-09 15:35:46 -04:00
|
|
|
// The coords are pointing at the top part of the door
|
2020-04-21 16:19:22 -04:00
|
|
|
if (a_BlockPos.y > 0)
|
2014-06-09 15:35:46 -04:00
|
|
|
{
|
2020-04-21 16:19:22 -04:00
|
|
|
NIBBLETYPE DownMeta = a_ChunkInterface.GetBlockMeta(a_BlockPos.addedY(-1));
|
2015-05-24 07:56:56 -04:00
|
|
|
return static_cast<NIBBLETYPE>((DownMeta & 0x07) | 0x08 | (Meta << 4));
|
2014-06-09 15:35:46 -04:00
|
|
|
}
|
|
|
|
// This is the top part of the door at the bottommost layer of the world, there's no bottom:
|
2015-05-24 07:56:56 -04:00
|
|
|
return static_cast<NIBBLETYPE>(0x08 | (Meta << 4));
|
2014-05-30 16:22:42 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-09 15:35:46 -04:00
|
|
|
// The coords are pointing at the bottom part of the door
|
2020-04-21 16:19:22 -04:00
|
|
|
if (a_BlockPos.y < cChunkDef::Height - 1)
|
2014-06-09 15:35:46 -04:00
|
|
|
{
|
2020-04-21 16:19:22 -04:00
|
|
|
NIBBLETYPE UpMeta = a_ChunkInterface.GetBlockMeta(a_BlockPos.addedY(1));
|
2015-05-24 07:56:56 -04:00
|
|
|
return static_cast<NIBBLETYPE>(Meta | (UpMeta << 4));
|
2014-06-09 15:35:46 -04:00
|
|
|
}
|
|
|
|
// This is the bottom part of the door at the topmost layer of the world, there's no top:
|
|
|
|
return Meta;
|
2014-05-29 13:21:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-29 13:21:56 -04:00
|
|
|
/** Changes the door at the specified coords from open to close or vice versa */
|
2020-04-21 16:19:22 -04:00
|
|
|
static void ChangeDoor(cChunkInterface & a_ChunkInterface, const Vector3i a_BlockPos)
|
2013-09-09 04:49:52 -04:00
|
|
|
{
|
2020-04-21 16:19:22 -04:00
|
|
|
SetOpen(a_ChunkInterface, a_BlockPos, !IsOpen(a_ChunkInterface, a_BlockPos));
|
2013-09-09 04:49:52 -04:00
|
|
|
}
|
2015-06-30 10:50:15 -04:00
|
|
|
|
2019-10-11 05:02:53 -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);
|
|
|
|
switch (m_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_OAK_DOOR: return 13;
|
|
|
|
case E_BLOCK_SPRUCE_DOOR: return 34;
|
|
|
|
case E_BLOCK_BIRCH_DOOR: return 2;
|
|
|
|
case E_BLOCK_JUNGLE_DOOR: return 10;
|
|
|
|
case E_BLOCK_DARK_OAK_DOOR: return 26;
|
|
|
|
case E_BLOCK_ACACIA_DOOR: return 15;
|
|
|
|
case E_BLOCK_IRON_DOOR: return 6;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled blocktype in door handler!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|