summaryrefslogtreecommitdiffstats
path: root/src/Blocks/MetaRotator.h
blob: ad8f3618328b068348ff178b0b582e2c26e93bdd (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
115
116
117
118
119
120
// MetaRotator.h

// Provides a mixin for rotations and reflections

#pragma once

// MSVC generates warnings for the templated AssertIfNotMatched parameter conditions, so disable it:
#ifdef _MSC_VER
	#pragma warning(disable: 4127)  // Conditional expression is constant
#endif





/*
Provides a mixin for rotations and reflections following the standard pattern of apply mask then use case.

Usage:
Inherit from this class providing your base class as Base, the BitMask for the direction bits in bitmask and the masked value for the directions in North, East, South, West. There is also an aptional parameter AssertIfNotMatched. Set this if it is invalid for a block to exist in any other state.
*/

template<class Base, NIBBLETYPE BitMask, NIBBLETYPE North, NIBBLETYPE East, NIBBLETYPE South, NIBBLETYPE West, bool AssertIfNotMatched = false>
class cMetaRotator : public Base
{
public:

	cMetaRotator(BLOCKTYPE a_BlockType) :
		Base(a_BlockType)
	{}
	
	virtual ~cMetaRotator() {}

	virtual NIBBLETYPE MetaRotateCCW(NIBBLETYPE a_Meta)  override;
	virtual NIBBLETYPE MetaRotateCW(NIBBLETYPE a_Meta)  override;
	virtual NIBBLETYPE MetaMirrorXY(NIBBLETYPE a_Meta)  override;
	virtual NIBBLETYPE MetaMirrorYZ(NIBBLETYPE a_Meta)  override;
};





template<class Base, NIBBLETYPE BitMask, NIBBLETYPE North, NIBBLETYPE East, NIBBLETYPE South, NIBBLETYPE West, bool AssertIfNotMatched>
NIBBLETYPE cMetaRotator<Base, BitMask, North, East, South, West, AssertIfNotMatched>::MetaRotateCW(NIBBLETYPE a_Meta)
{
	NIBBLETYPE OtherMeta = a_Meta & (~BitMask);
	switch (a_Meta & BitMask)
	{
		case South: return West  | OtherMeta; 
		case West:  return North | OtherMeta; 
		case North: return East  | OtherMeta;
		case East:  return South | OtherMeta;
	}
	if (AssertIfNotMatched)
	{
		ASSERT(!"Invalid Meta value");
	}
	return a_Meta;
}





template<class Base, NIBBLETYPE BitMask, NIBBLETYPE North, NIBBLETYPE East, NIBBLETYPE South, NIBBLETYPE West, bool AssertIfNotMatched>
NIBBLETYPE cMetaRotator<Base, BitMask, North, East, South, West, AssertIfNotMatched>::MetaRotateCCW(NIBBLETYPE a_Meta)
{
	NIBBLETYPE OtherMeta = a_Meta & (~BitMask);
	switch (a_Meta & BitMask)
	{
		case South: return East  | OtherMeta; 
		case East:  return North | OtherMeta;
		case North: return West  | OtherMeta;
		case West:  return South | OtherMeta;
	}
	if (AssertIfNotMatched)
	{
		ASSERT(!"Invalid Meta value");
	}
	return a_Meta;
}





template<class Base, NIBBLETYPE BitMask, NIBBLETYPE North, NIBBLETYPE East, NIBBLETYPE South, NIBBLETYPE West, bool AssertIfNotMatched>
NIBBLETYPE cMetaRotator<Base, BitMask, North, East, South, West, AssertIfNotMatched>::MetaMirrorXY(NIBBLETYPE a_Meta)
{
	NIBBLETYPE OtherMeta = a_Meta & (~BitMask);
	switch (a_Meta & BitMask)
	{
		case South: return North | OtherMeta;
		case North: return South | OtherMeta;
	}
	// Not Facing North or South; No change.
	return a_Meta;
}





template<class Base, NIBBLETYPE BitMask, NIBBLETYPE North, NIBBLETYPE East, NIBBLETYPE South, NIBBLETYPE West, bool AssertIfNotMatched>
NIBBLETYPE cMetaRotator<Base, BitMask, North, East, South, West, AssertIfNotMatched>::MetaMirrorYZ(NIBBLETYPE a_Meta)
{
	NIBBLETYPE OtherMeta = a_Meta & (~BitMask);
	switch (a_Meta & BitMask)
	{
		case West: return East | OtherMeta;
		case East: return West | OtherMeta;
	}
	// Not Facing East or West; No change.
	return a_Meta;
}