Cuberite
A lightweight, fast and extensible game server for Minecraft
RedstoneTorchHandler.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 
5 
6 
7 
9 {
10  static bool IsOn(BLOCKTYPE a_Block)
11  {
12  return (a_Block == E_BLOCK_REDSTONE_TORCH_ON);
13  }
14 
16  {
17  switch (a_Meta)
18  {
19  case E_META_TORCH_FLOOR: return { 0, -1, 0 };
20  case E_META_TORCH_EAST: return { -1, 0, 0 };
21  case E_META_TORCH_WEST: return { 1, 0, 0 };
22  case E_META_TORCH_NORTH: return { 0, 0, 1 };
23  case E_META_TORCH_SOUTH: return { 0, 0, -1 };
24  default:
25  {
26  ASSERT(!"Unhandled torch metadata");
27  return { 0, 0, 0 };
28  }
29  }
30  }
31 
32  static PowerLevel GetPowerDeliveredToPosition(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked)
33  {
34  const auto QueryOffset = a_QueryPosition - a_Position;
35 
36  if (
37  !IsOn(a_BlockType) ||
38  (QueryOffset == GetOffsetAttachedTo(a_Chunk.GetMeta(a_Position))) ||
39  (IsLinked && (QueryOffset != OffsetYP))
40  )
41  {
42  return 0;
43  }
44 
45  return 15;
46  }
47 
48  static void Update(cChunk & a_Chunk, cChunk & CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power)
49  {
50  // LOGD("Evaluating torchy the redstone torch (%i %i %i)", a_Position.x, a_Position.y, a_Position.z);
51 
52  auto & Data = DataForChunk(a_Chunk);
53  auto DelayInfo = Data.GetMechanismDelayInfo(a_Position);
54 
55  if (DelayInfo == nullptr)
56  {
57  const bool ShouldBeOn = (Power == 0);
58  if (ShouldBeOn != IsOn(a_BlockType))
59  {
60  Data.m_MechanismDelays[a_Position] = std::make_pair(1, ShouldBeOn);
61  }
62 
63  return;
64  }
65 
66  int DelayTicks;
67  bool ShouldPowerOn;
68  std::tie(DelayTicks, ShouldPowerOn) = *DelayInfo;
69 
70  if (DelayTicks != 0)
71  {
72  return;
73  }
74 
76  Data.m_MechanismDelays.erase(a_Position);
77 
78  for (const auto & Adjacent : RelativeAdjacents)
79  {
80  // Update all adjacents (including linked power positions)
81  // apart from our attachment, which can't possibly need an update:
82  if (Adjacent != GetOffsetAttachedTo(a_Meta))
83  {
84  UpdateAdjustedRelative(a_Chunk, CurrentlyTicking, a_Position, Adjacent);
85  }
86  }
87  }
88 
89  static void ForValidSourcePositions(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback & Callback)
90  {
91  UNUSED(a_Chunk);
92  UNUSED(a_BlockType);
93  Callback(a_Position + GetOffsetAttachedTo(a_Meta));
94  }
95 };
@ E_META_TORCH_WEST
Definition: BlockType.h:937
@ E_META_TORCH_SOUTH
Definition: BlockType.h:938
@ E_META_TORCH_FLOOR
Definition: BlockType.h:940
@ E_META_TORCH_EAST
Definition: BlockType.h:936
@ E_META_TORCH_NORTH
Definition: BlockType.h:939
@ E_BLOCK_REDSTONE_TORCH_ON
Definition: BlockType.h:90
@ E_BLOCK_REDSTONE_TORCH_OFF
Definition: BlockType.h:89
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
constexpr Vector3i OffsetYP
auto & DataForChunk(const cChunk &a_Chunk)
constexpr std::array< Vector3i, 6 > RelativeAdjacents
void UpdateAdjustedRelative(const cChunk &a_Chunk, const cChunk &a_TickingChunk, const Vector3i a_Position, const Vector3i a_Offset)
unsigned char PowerLevel
unsigned char Power(const BlockState Block)
static bool ShouldPowerOn(cChunk &Chunk, const Vector3i a_Position, NIBBLETYPE a_Meta, cIncrementalRedstoneSimulatorChunkData &a_Data)
static void Update(cChunk &a_Chunk, cChunk &CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power)
static bool IsOn(BLOCKTYPE a_Block)
static void ForValidSourcePositions(const cChunk &a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback &Callback)
static Vector3i GetOffsetAttachedTo(const NIBBLETYPE a_Meta)
static PowerLevel GetPowerDeliveredToPosition(const cChunk &a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked)
Definition: Chunk.h:36
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:279
void FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta)
Definition: Chunk.cpp:1296