summaryrefslogtreecommitdiffstats
path: root/src/Simulator/IncrementalRedstoneSimulator/RedstoneHandler.h
blob: fec38b14d58a8d24493d024ca8b1cba80dba14b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#pragma once

#include "../../Chunk.h"
#include "RedstoneSimulatorChunkData.h"





class cRedstoneHandler
{
public:

	cRedstoneHandler() = default;
	DISALLOW_COPY_AND_ASSIGN(cRedstoneHandler);

	using SourceCallback = cFunctionRef<void(Vector3i)>;

	virtual unsigned char GetPowerDeliveredToPosition(cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType) const = 0;
	virtual void Update(cChunk & a_Chunk, cChunk & CurrentlyTicking, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, PoweringData a_PoweringData) const = 0;
	virtual void ForValidSourcePositions(cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, SourceCallback Callback) const = 0;

	// Force a virtual destructor
	virtual ~cRedstoneHandler() {}

protected:

	template <class Container>
	static Container StaticAppend(const Container & a_Lhs, const Container & a_Rhs)
	{
		Container ToReturn = a_Lhs;
		std::copy(a_Rhs.begin(), a_Rhs.end(), std::back_inserter(ToReturn));
		return ToReturn;
	}

	inline static Vector3i OffsetYP{ 0, 1, 0 };

	inline static Vector3i OffsetYM{ 0, -1, 0 };

	static cVector3iArray GetAdjustedRelatives(Vector3i a_Position, cVector3iArray a_Relatives)
	{
		for (auto & Entry : a_Relatives)
		{
			Entry += a_Position;
		}
		return a_Relatives;
	}

	inline static cIncrementalRedstoneSimulatorChunkData & DataForChunk(cChunk & a_Chunk)
	{
		return *static_cast<cIncrementalRedstoneSimulatorChunkData *>(a_Chunk.GetRedstoneSimulatorData());
	}

	template <typename... ArrayTypes>
	static void UpdateAdjustedRelatives(cChunk & From, cChunk & To, const Vector3i Position)
	{
		DataForChunk(To).WakeUp(cIncrementalRedstoneSimulatorChunkData::RebaseRelativePosition(From, To, Position));
	}

	template <typename ArrayType, typename... ArrayTypes>
	static void UpdateAdjustedRelatives(cChunk & From, cChunk & To, const Vector3i Position, const ArrayType & Relative, const ArrayTypes &... Relatives)
	{
		for (const auto Offset : Relative)
		{
			DataForChunk(To).GetActiveBlocks().push(cIncrementalRedstoneSimulatorChunkData::RebaseRelativePosition(From, To, Position + Offset));
		}

		UpdateAdjustedRelatives(From, To, Position, Relatives...);
	}

	template <typename ArrayType, typename... ArrayTypes>
	static void InvokeForAdjustedRelatives(SourceCallback Callback, Vector3i Position, const ArrayType & Relative, const ArrayTypes &... Relatives)
	{
		for (const auto Offset : Relative)
		{
			Callback(Position + Offset);
		}

		InvokeForAdjustedRelatives(Callback, Position, Relatives...);
	}

	inline static std::array<Vector3i, 6> RelativeAdjacents
	{
		{
			{ 1, 0, 0 },
			{ -1, 0, 0 },
			{ 0, 1, 0 },
			{ 0, -1, 0 },
			{ 0, 0, 1 },
			{ 0, 0, -1 },
		}
	};

	inline static std::array<Vector3i, 4> RelativeLaterals
	{
		{
			{ 1, 0, 0 },
			{ -1, 0, 0 },
			{ 0, 0, 1 },
			{ 0, 0, -1 },
		}
	};

private:

	static void UpdateAdjustedRelatives(cVector3iArray &, Vector3i)
	{
	}

	static void InvokeForAdjustedRelatives(SourceCallback, Vector3i)
	{
	}
};