Cuberite
A lightweight, fast and extensible game server for Minecraft
RedstoneDataHelper.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../../Chunk.h"
4 
5 inline auto & DataForChunk(const cChunk & a_Chunk)
6 {
8 }
9 
10 inline void UpdateAdjustedRelative(const cChunk & a_Chunk, const cChunk & a_TickingChunk, const Vector3i a_Position, const Vector3i a_Offset)
11 {
12  const auto PositionToWake = a_Position + a_Offset;
13 
14  if (!cChunkDef::IsValidHeight(PositionToWake))
15  {
16  // If an offset position is not a valid height, its linked offset positions won't be either.
17  return;
18  }
19 
20  auto & ChunkData = DataForChunk(a_TickingChunk);
21 
22  // Schedule the block in the requested direction to update:
23  ChunkData.WakeUp(cIncrementalRedstoneSimulatorChunkData::RebaseRelativePosition(a_Chunk, a_TickingChunk, PositionToWake));
24 
25  // To follow Vanilla behaviour, update all linked positions:
26  for (const auto & LinkedOffset : cSimulator::GetLinkedOffsets(a_Offset))
27  {
28  if (const auto LinkedPositionToWake = a_Position + LinkedOffset; cChunkDef::IsValidHeight(LinkedPositionToWake))
29  {
30  ChunkData.WakeUp(cIncrementalRedstoneSimulatorChunkData::RebaseRelativePosition(a_Chunk, a_TickingChunk, LinkedPositionToWake));
31  }
32  }
33 }
34 
35 template <typename ArrayType>
36 inline void UpdateAdjustedRelatives(const cChunk & a_Chunk, const cChunk & a_TickingChunk, const Vector3i a_Position, const ArrayType & a_Relative)
37 {
38  for (const auto & Offset : a_Relative)
39  {
40  UpdateAdjustedRelative(a_Chunk, a_TickingChunk, a_Position, Offset);
41  }
42 }
43 
44 template <typename ArrayType>
45 inline void InvokeForAdjustedRelatives(ForEachSourceCallback & Callback, const Vector3i Position, const ArrayType & Relative)
46 {
47  for (const auto & Offset : Relative)
48  {
49  Callback(Position + Offset);
50  }
51 }
52 
53 inline constexpr Vector3i OffsetYP{ 0, 1, 0 };
54 
55 inline constexpr Vector3i OffsetYM{ 0, -1, 0 };
56 
57 inline constexpr std::array<Vector3i, 6> RelativeAdjacents
58 {
59  {
60  { 1, 0, 0 },
61  { -1, 0, 0 },
62  { 0, 1, 0 },
63  { 0, -1, 0 },
64  { 0, 0, 1 },
65  { 0, 0, -1 },
66  }
67 };
68 
69 inline constexpr std::array<Vector3i, 4> RelativeLaterals
70 {
71  {
72  { 1, 0, 0 },
73  { -1, 0, 0 },
74  { 0, 0, 1 },
75  { 0, 0, -1 },
76  }
77 };
constexpr Vector3i OffsetYP
auto & DataForChunk(const cChunk &a_Chunk)
constexpr std::array< Vector3i, 6 > RelativeAdjacents
constexpr Vector3i OffsetYM
void InvokeForAdjustedRelatives(ForEachSourceCallback &Callback, const Vector3i Position, const ArrayType &Relative)
constexpr std::array< Vector3i, 4 > RelativeLaterals
void UpdateAdjustedRelatives(const cChunk &a_Chunk, const cChunk &a_TickingChunk, const Vector3i a_Position, const ArrayType &a_Relative)
void UpdateAdjustedRelative(const cChunk &a_Chunk, const cChunk &a_TickingChunk, const Vector3i a_Position, const Vector3i a_Offset)
Definition: Chunk.h:36
cRedstoneSimulatorChunkData * GetRedstoneSimulatorData(void) const
Definition: Chunk.h:416
static bool IsValidHeight(Vector3i a_BlockPosition)
Validates a height-coordinate.
Definition: ChunkDef.h:185
static Vector3i RebaseRelativePosition(const cChunk &From, const cChunk &To, const Vector3i Position)
Adjust From-relative coordinates into To-relative coordinates.
static std::array< Vector3i, 5 > GetLinkedOffsets(Vector3i Offset)
For a given offset from a position, return the offsets that represent the adjacents of the newly offs...
Definition: Simulator.cpp:12