Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockTorch.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "BlockHandler.h"
4 #include "BlockSlab.h"
5 #include "BlockStairs.h"
6 #include "../Chunk.h"
7 #include "BlockType.h"
8 #include "ChunkInterface.h"
9 #include "Defines.h"
10 #include "Mixins.h"
11 
12 
13 
14 
15 
17  public cMetaRotator<cBlockHandler, 0x7, 0x4, 0x1, 0x3, 0x2>
18 {
20 
21 public:
22 
23  using Super::Super;
24 
25 
27  static bool CanBePlacedOn(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, eBlockFace a_BlockFace)
28  {
29  // upside down slabs
30  if (cBlockSlabHandler::IsAnySlabType(a_BlockType))
31  {
32  return (a_BlockFace == BLOCK_FACE_YP) && (a_BlockMeta & E_META_WOODEN_SLAB_UPSIDE_DOWN);
33  }
34 
35  // stairs (top and sides)
36  if (cBlockStairsHandler::IsAnyStairType(a_BlockType))
37  {
38  switch (a_BlockFace)
39  {
41  return (a_BlockMeta & E_BLOCK_STAIRS_UPSIDE_DOWN);
43  return ((a_BlockMeta & 0b11) == E_BLOCK_STAIRS_XP);
45  return ((a_BlockMeta & 0b11) == E_BLOCK_STAIRS_XM);
47  return ((a_BlockMeta & 0b11) == E_BLOCK_STAIRS_ZP);
49  return ((a_BlockMeta & 0b11) == E_BLOCK_STAIRS_ZM);
50  default:
51  {
52  return false;
53  }
54  }
55  }
56 
57  switch (a_BlockType)
58  {
60  case E_BLOCK_SOULSAND:
61  {
62  // Exceptional vanilla behaviour
63  return true;
64  }
65  case E_BLOCK_GLASS:
67  case E_BLOCK_FENCE:
75  {
76  // Torches can only be placed on top of these blocks
77  return (a_BlockFace == BLOCK_FACE_YP);
78  }
79  default:
80  {
81  if (cBlockInfo::FullyOccupiesVoxel(a_BlockType))
82  {
83  // Torches can be placed on all sides of full blocks except the bottom
84  return (a_BlockFace != BLOCK_FACE_YM);
85  }
86  return false;
87  }
88  }
89  }
90 
91 protected:
92 
94 
95 private:
96 
98  inline static eBlockFace MetaDataToBlockFace(NIBBLETYPE a_MetaData)
99  {
100  switch (a_MetaData)
101  {
102  case 0: return BLOCK_FACE_TOP; // By default, the torches stand on the ground
103  case E_META_TORCH_FLOOR: return BLOCK_FACE_TOP;
104  case E_META_TORCH_EAST: return BLOCK_FACE_EAST;
105  case E_META_TORCH_WEST: return BLOCK_FACE_WEST;
108  default:
109  {
110  ASSERT(!"Unhandled torch metadata");
111  break;
112  }
113  }
114  return BLOCK_FACE_TOP;
115  }
116 
117 
118 
119 
120 
121  virtual bool CanBeAt(const cChunk & a_Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta) const override
122  {
123  auto Face = MetaDataToBlockFace(a_Meta);
124  auto NeighborRelPos = AddFaceDirection(a_Position, Face, true);
125  BLOCKTYPE NeighborBlockType;
126  NIBBLETYPE NeighborBlockMeta;
127  if (!a_Chunk.UnboundedRelGetBlock(NeighborRelPos, NeighborBlockType, NeighborBlockMeta))
128  {
129  // Neighbor in an unloaded chunk, bail out without changint this.
130  return false;
131  }
132 
133  return CanBePlacedOn(NeighborBlockType, NeighborBlockMeta, Face);
134  }
135 
136 
137 
138 
139 
140  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
141  {
142  UNUSED(a_Meta);
143  return 0;
144  }
145 } ;
146 
147 
148 
149 
150 
151 class cBlockTorchHandler final :
152  public cClearMetaOnDrop<cBlockTorchBaseHandler>
153 {
155 
156 public:
157 
158  using Super::Super;
159 };
160 
161 
162 
163 
164 
167 {
169 
170 public:
171 
172  using Super::Super;
173 
174 private:
175 
176  virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem * const a_Tool) const override
177  {
178  // Always drop the ON torch, meta 0:
179  return { E_BLOCK_REDSTONE_TORCH_ON };
180  }
181 
182  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
183  {
184  UNUSED(a_Meta);
185  return 0;
186  }
187 };
@ E_META_WOODEN_SLAB_UPSIDE_DOWN
Definition: BlockType.h:989
@ E_BLOCK_STAIRS_UPSIDE_DOWN
Definition: BlockType.h:902
@ E_META_TORCH_WEST
Definition: BlockType.h:937
@ E_BLOCK_STAIRS_ZP
Definition: BlockType.h:900
@ E_META_TORCH_SOUTH
Definition: BlockType.h:938
@ E_BLOCK_STAIRS_ZM
Definition: BlockType.h:901
@ E_META_TORCH_FLOOR
Definition: BlockType.h:940
@ E_META_TORCH_EAST
Definition: BlockType.h:936
@ E_BLOCK_STAIRS_XM
Definition: BlockType.h:899
@ E_META_TORCH_NORTH
Definition: BlockType.h:939
@ E_BLOCK_STAIRS_XP
Definition: BlockType.h:898
@ E_BLOCK_STAINED_GLASS
Definition: BlockType.h:110
@ E_BLOCK_REDSTONE_TORCH_ON
Definition: BlockType.h:90
@ E_BLOCK_DARK_OAK_FENCE
Definition: BlockType.h:210
@ E_BLOCK_END_PORTAL_FRAME
Definition: BlockType.h:135
@ E_BLOCK_FENCE
Definition: BlockType.h:100
@ E_BLOCK_SOULSAND
Definition: BlockType.h:103
@ E_BLOCK_SPRUCE_FENCE
Definition: BlockType.h:207
@ E_BLOCK_ACACIA_FENCE
Definition: BlockType.h:211
@ E_BLOCK_GLASS
Definition: BlockType.h:30
@ E_BLOCK_COBBLESTONE_WALL
Definition: BlockType.h:154
@ E_BLOCK_NETHER_BRICK_FENCE
Definition: BlockType.h:128
@ E_BLOCK_BIRCH_FENCE
Definition: BlockType.h:208
@ E_BLOCK_JUNGLE_FENCE
Definition: BlockType.h:209
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse)
Modifies the specified coords so that they point to the block adjacent to the one specified through i...
Definition: Defines.cpp:378
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
@ BLOCK_FACE_XP
Definition: Defines.h:41
@ BLOCK_FACE_EAST
Definition: Defines.h:53
@ BLOCK_FACE_YP
Definition: Defines.h:43
@ BLOCK_FACE_YM
Definition: Defines.h:42
@ BLOCK_FACE_SOUTH
Definition: Defines.h:51
@ BLOCK_FACE_TOP
Definition: Defines.h:49
@ BLOCK_FACE_WEST
Definition: Defines.h:52
@ BLOCK_FACE_ZM
Definition: Defines.h:44
@ BLOCK_FACE_ZP
Definition: Defines.h:45
@ BLOCK_FACE_NORTH
Definition: Defines.h:50
@ BLOCK_FACE_XM
Definition: Defines.h:40
Byte ColourID
Definition: Globals.h:162
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
static bool FullyOccupiesVoxel(BLOCKTYPE Block)
Does this block fully occupy its voxel - is it a 'full' block?
Definition: BlockInfo.cpp:606
static bool IsAnySlabType(BLOCKTYPE a_BlockType)
Returns true if the specified blocktype is one of the slabs handled by this handler.
Definition: BlockSlab.h:30
static bool IsAnyStairType(BLOCKTYPE a_Block)
Definition: BlockStairs.h:19
cMetaRotator< cBlockHandler, 0x7, 0x4, 0x1, 0x3, 0x2 > Super
Definition: BlockTorch.h:19
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
Returns the base colour ID of the block, as will be represented on a map, as per documentation: https...
Definition: BlockTorch.h:140
static eBlockFace MetaDataToBlockFace(NIBBLETYPE a_MetaData)
Converts the torch block's meta to the block face of the neighbor to which the torch is attached.
Definition: BlockTorch.h:98
virtual bool CanBeAt(const cChunk &a_Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta) const override
Checks if the block can stay at the specified relative coords in the chunk.
Definition: BlockTorch.h:121
static bool CanBePlacedOn(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, eBlockFace a_BlockFace)
Returns true if the torch can be placed on the specified block's face.
Definition: BlockTorch.h:27
~cBlockTorchBaseHandler()=default
virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem *const a_Tool) const override
Returns the pickups that would result if the block was mined by a_Digger using a_Tool.
Definition: BlockTorch.h:176
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
Returns the base colour ID of the block, as will be represented on a map, as per documentation: https...
Definition: BlockTorch.h:182
Mixin to clear the block's meta value when converting to a pickup.
Definition: Mixins.h:31
Mixin for rotations and reflections following the standard pattern of "apply mask,...
Definition: Mixins.h:62
Definition: Chunk.h:36
bool UnboundedRelGetBlock(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Same as GetBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in ...
Definition: Chunk.cpp:1008
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215