Cuberite
A lightweight, fast and extensible game server for Minecraft
RedstoneToggleHandler.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "../../Blocks/BlockButton.h"
5 #include "../../Blocks/BlockLever.h"
6 
7 
8 
9 
10 
12 {
13  static Vector3i GetOffsetAttachedTo(Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta)
14  {
15  switch (a_BlockType)
16  {
17  case E_BLOCK_LEVER:
18  {
19  switch (a_Meta & 0x7)
20  {
21  case 0x0:
22  case 0x7: return { 0, 1, 0 };
23  case 0x1: return { -1, 0, 0 };
24  case 0x2: return { 1, 0, 0 };
25  case 0x3: return { 0, 0, -1 };
26  case 0x4: return { 0, 0, 1 };
27  case 0x5:
28  case 0x6: return { 0, -1, 0 };
29  default:
30  {
31  ASSERT(!"Unhandled lever metadata!");
32  return { 0, 0, 0 };
33  }
34  }
35  }
38  {
39  switch (a_Meta & 0x7)
40  {
41  case 0x0: return { 0, 1, 0 };
42  case 0x1: return { -1, 0, 0 };
43  case 0x2: return { 1, 0, 0 };
44  case 0x3: return { 0, 0, -1 };
45  case 0x4: return { 0, 0, 1 };
46  case 0x5: return { 0, -1, 0 };
47  default:
48  {
49  ASSERT(!"Unhandled button metadata!");
50  return { 0, 0, 0 };
51  }
52  }
53  }
54  default:
55  {
56  ASSERT(!"Unexpected block passed to button/lever handler");
57  return { 0, 0, 0 };
58  }
59  }
60  }
61 
62  static unsigned char GetPowerLevel(BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta)
63  {
64  switch (a_BlockType)
65  {
66  case E_BLOCK_LEVER: return cBlockLeverHandler::IsLeverOn(a_Meta) ? 15 : 0;
68  case E_BLOCK_WOODEN_BUTTON: return cBlockButtonHandler::IsButtonOn(a_Meta) ? 15 : 0;
69  default:
70  {
71  ASSERT(!"Unexpected block passed to button/lever handler");
72  return 0;
73  }
74  }
75  }
76 
77  static PowerLevel GetPowerDeliveredToPosition(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked)
78  {
79  UNUSED(a_QueryBlockType);
80 
81  const auto Meta = a_Chunk.GetMeta(a_Position);
82  const auto QueryOffset = a_QueryPosition - a_Position;
83 
84  if (IsLinked && (QueryOffset != GetOffsetAttachedTo(a_Position, a_BlockType, Meta)))
85  {
86  return 0;
87  }
88 
89  return GetPowerLevel(a_BlockType, Meta);
90  }
91 
92  static void Update(cChunk & a_Chunk, cChunk & CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power)
93  {
94  // LOGD("Evaluating templatio<> the lever/button (%d %d %d)", a_Position.x, a_Position.y, a_Position.z);
95  }
96 
97  static void ForValidSourcePositions(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback & Callback)
98  {
99  UNUSED(a_Chunk);
100  UNUSED(a_Position);
101  UNUSED(a_BlockType);
102  UNUSED(a_Meta);
103  UNUSED(Callback);
104  }
105 };
@ E_BLOCK_WOODEN_BUTTON
Definition: BlockType.h:158
@ E_BLOCK_LEVER
Definition: BlockType.h:83
@ E_BLOCK_STONE_BUTTON
Definition: BlockType.h:91
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
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
unsigned char PowerLevel
unsigned char Power(const BlockState Block)
static Vector3i GetOffsetAttachedTo(Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta)
static PowerLevel GetPowerDeliveredToPosition(const cChunk &a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked)
static unsigned char GetPowerLevel(BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta)
static void Update(cChunk &a_Chunk, cChunk &CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power)
static void ForValidSourcePositions(const cChunk &a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback &Callback)
static bool IsButtonOn(NIBBLETYPE a_Meta)
Extracts the ON bit from metadata and returns if true if it is set.
Definition: BlockButton.h:27
static bool IsLeverOn(NIBBLETYPE a_BlockMeta)
Extracts the ON bit from metadata and returns if true if it is set.
Definition: BlockLever.h:22
Definition: Chunk.h:36
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:279