summaryrefslogtreecommitdiffstats
path: root/source/Blocks/BlockRail.h
blob: 24947c0bc15c45fcb52cdd1952ab3726e1619a79 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

#pragma once

#include "BlockEntity.h"
#include "../World.h"





enum ENUM_RAIL_DIRECTIONS
{
	E_RAIL_NORTH_SOUTH = 0,
	E_RAIL_EAST_WEST = 1,
	E_RAIL_ASCEND_EAST = 2,
	E_RAIL_ASCEND_WEST = 3,
	E_RAIL_ASCEND_NORTH = 4,
	E_RAIL_ASCEND_SOUTH = 5,
	E_RAIL_CURVED_SOUTH_EAST = 6,
	E_RAIL_CURVED_SOUTH_WEST = 7,
	E_RAIL_CURVED_NORTH_WEST = 8,
	E_RAIL_CURVED_NORTH_EAST = 9
};

enum ENUM_PURE
{
	E_PURE_UPDOWN = 0,
	E_PURE_DOWN = 1,
	E_PURE_NONE = 2
};





class cBlockRailHandler :
	public cBlockHandler
{
public:
	cBlockRailHandler(BLOCKTYPE a_BlockType)
		: cBlockHandler(a_BlockType)
	{
	}
	
	virtual void PlaceBlock(cWorld * a_World, cPlayer * a_Player, NIBBLETYPE a_BlockMeta, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Dir) override
	{
		a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, m_BlockType, FindMeta(a_World, a_BlockX, a_BlockY, a_BlockZ));
		OnPlacedByPlayer(a_World, a_Player, a_BlockX, a_BlockY, a_BlockZ, a_Dir);
		NeighborChanged(a_World, a_BlockX - 1, a_BlockY + 1, a_BlockZ);
		NeighborChanged(a_World, a_BlockX + 1, a_BlockY + 1, a_BlockZ);
		NeighborChanged(a_World, a_BlockX, a_BlockY + 1, a_BlockZ - 1);
		NeighborChanged(a_World, a_BlockX, a_BlockY + 1, a_BlockZ + 1);
		NeighborChanged(a_World, a_BlockX - 1, a_BlockY - 1, a_BlockZ);
		NeighborChanged(a_World, a_BlockX + 1, a_BlockY - 1, a_BlockZ);
		NeighborChanged(a_World, a_BlockX, a_BlockY - 1, a_BlockZ - 1);
		NeighborChanged(a_World, a_BlockX, a_BlockY - 1, a_BlockZ + 1);
	}

	virtual void OnNeighborChanged(cWorld *a_World, int a_BlockX, int a_BlockY, int a_BlockZ) override
	{
		char Meta = a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
		if(IsUnstable(a_World, a_BlockX, a_BlockY, a_BlockZ) && Meta != FindMeta(a_World, a_BlockX, a_BlockY, a_BlockZ))
			a_World->FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, m_BlockType, FindMeta(a_World, a_BlockX, a_BlockY, a_BlockZ));
	}

	virtual bool CanBeAt(cWorld *a_World, int a_BlockX, int a_BlockY, int a_BlockZ) override
	{
		if(!g_BlockIsSolid[a_World->GetBlock(a_BlockX, a_BlockY - 1, a_BlockZ)])
			return false;
		char Meta = a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
		switch(Meta)
		{
			case E_RAIL_ASCEND_EAST:
			{
				if(!g_BlockIsSolid[a_World->GetBlock(a_BlockX + 1, a_BlockY, a_BlockZ)])
					return false;
				break;
			}
			case E_RAIL_ASCEND_WEST:
			{
				if(!g_BlockIsSolid[a_World->GetBlock(a_BlockX - 1, a_BlockY, a_BlockZ)])
					return false;
				break;
			}
			case E_RAIL_ASCEND_NORTH:
			{
				if(!g_BlockIsSolid[a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ - 1)])
					return false;
				break;
			}
			case E_RAIL_ASCEND_SOUTH:
			{
				if(!g_BlockIsSolid[a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ + 1)])
					return false;
				break;
			}
		}
		return true;
	}

	char FindMeta(cWorld *a_World, int a_BlockX, int a_BlockY, int a_BlockZ)
	{
		char Meta = 0;
		char RailsCnt = 0;
		bool Neighbors[8]; // 0 - EAST, 1 - WEST, 2 - NORTH, 3 - SOUTH, 4 - EAST UP, 5 - WEST UP, 6 - NORTH UP, 7 - SOUTH UP
		memset(Neighbors, false, sizeof(Neighbors));
		if(IsUnstable(a_World, a_BlockX + 1, a_BlockY, a_BlockZ) || !IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_EAST, E_PURE_DOWN))
			Neighbors[0] = true;
		if(IsUnstable(a_World, a_BlockX - 1, a_BlockY, a_BlockZ) || !IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_WEST, E_PURE_DOWN))
			Neighbors[1] = true;
		if(IsUnstable(a_World, a_BlockX, a_BlockY, a_BlockZ - 1) || !IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_NORTH, E_PURE_DOWN))
			Neighbors[2] = true;
		if(IsUnstable(a_World, a_BlockX, a_BlockY, a_BlockZ + 1) || !IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_SOUTH, E_PURE_DOWN))
			Neighbors[3] = true;
		if(IsUnstable(a_World, a_BlockX + 1, a_BlockY + 1, a_BlockZ) || !IsNotConnected(a_World, a_BlockX, a_BlockY + 1, a_BlockZ, BLOCK_FACE_EAST, E_PURE_NONE))
			Neighbors[4] = true;
		if(IsUnstable(a_World, a_BlockX - 1, a_BlockY + 1, a_BlockZ) || !IsNotConnected(a_World, a_BlockX, a_BlockY + 1, a_BlockZ, BLOCK_FACE_WEST, E_PURE_NONE))
			Neighbors[5] = true;
		if(IsUnstable(a_World, a_BlockX, a_BlockY + 1, a_BlockZ - 1) || !IsNotConnected(a_World, a_BlockX, a_BlockY + 1, a_BlockZ, BLOCK_FACE_NORTH, E_PURE_NONE))
			Neighbors[6] = true;
		if(IsUnstable(a_World, a_BlockX, a_BlockY + 1, a_BlockZ + 1) || !IsNotConnected(a_World, a_BlockX, a_BlockY + 1, a_BlockZ, BLOCK_FACE_SOUTH, E_PURE_NONE))
			Neighbors[7] = true;
		if(IsUnstable(a_World, a_BlockX + 1, a_BlockY - 1, a_BlockZ) || !IsNotConnected(a_World, a_BlockX, a_BlockY - 1, a_BlockZ, BLOCK_FACE_EAST))
			Neighbors[0] = true;
		if(IsUnstable(a_World, a_BlockX - 1, a_BlockY - 1, a_BlockZ) || !IsNotConnected(a_World, a_BlockX, a_BlockY - 1, a_BlockZ, BLOCK_FACE_WEST))
			Neighbors[1] = true;
		if(IsUnstable(a_World, a_BlockX, a_BlockY - 1, a_BlockZ - 1) || !IsNotConnected(a_World, a_BlockX, a_BlockY - 1, a_BlockZ, BLOCK_FACE_NORTH))
			Neighbors[2] = true;
		if(IsUnstable(a_World, a_BlockX, a_BlockY - 1, a_BlockZ + 1) || !IsNotConnected(a_World, a_BlockX, a_BlockY - 1, a_BlockZ, BLOCK_FACE_SOUTH))
			Neighbors[3] = true;
		for(int i = 0; i < 8; i++)
		{
			if(Neighbors[i])
			{
				RailsCnt++;
			}
		}
		if(RailsCnt == 1)
		{
			if(Neighbors[7]) Meta = E_RAIL_ASCEND_SOUTH;
			else if(Neighbors[6]) Meta = E_RAIL_ASCEND_NORTH;
			else if(Neighbors[5]) Meta = E_RAIL_ASCEND_WEST;
			else if(Neighbors[4]) Meta = E_RAIL_ASCEND_EAST;
			else if(Neighbors[0] || Neighbors[1]) Meta = E_RAIL_EAST_WEST;
			else if(Neighbors[2] || Neighbors[3]) Meta = E_RAIL_NORTH_SOUTH;
			return Meta;
		}
		for(int i = 0; i < 4; i++)
		{
			if(Neighbors[i+4])
			{
				Neighbors[i] = true;
			}
		}
		if(RailsCnt > 1)
		{
			if(Neighbors[3] && Neighbors[0]) Meta = E_RAIL_CURVED_SOUTH_EAST;
			else if(Neighbors[3] && Neighbors[1]) Meta = E_RAIL_CURVED_SOUTH_WEST;
			else if(Neighbors[2] && Neighbors[0]) Meta = E_RAIL_CURVED_NORTH_EAST;
			else if(Neighbors[2] && Neighbors[1]) Meta = E_RAIL_CURVED_NORTH_WEST;
			else if(Neighbors[7] && Neighbors[2]) Meta = E_RAIL_ASCEND_SOUTH;
			else if(Neighbors[3] && Neighbors[6]) Meta = E_RAIL_ASCEND_NORTH;
			else if(Neighbors[5] && Neighbors[0]) Meta = E_RAIL_ASCEND_WEST;
			else if(Neighbors[4] && Neighbors[1]) Meta = E_RAIL_ASCEND_EAST;
			else if(Neighbors[0] && Neighbors[1]) Meta = E_RAIL_EAST_WEST;
			else if(Neighbors[2] && Neighbors[3]) Meta = E_RAIL_NORTH_SOUTH;
		}
		return Meta;
	}

	bool IsUnstable(cWorld *a_World, int a_BlockX, int a_BlockY, int a_BlockZ)
	{
		if(a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ) != E_BLOCK_RAIL) return false;
		char Meta = a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
		switch(Meta)
		{
			case E_RAIL_NORTH_SOUTH:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_NORTH, E_PURE_DOWN) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_SOUTH, E_PURE_DOWN)) return true;
				break;
			}
			case E_RAIL_EAST_WEST:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_EAST, E_PURE_DOWN) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_WEST, E_PURE_DOWN)) return true;
				break;
			}
			case E_RAIL_ASCEND_EAST:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY + 1, a_BlockZ, BLOCK_FACE_EAST) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_WEST)) return true;
				break;
			}
			case E_RAIL_ASCEND_WEST:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_EAST) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY + 1, a_BlockZ, BLOCK_FACE_WEST)) return true;
				break;
			}
			case E_RAIL_ASCEND_NORTH:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY + 1, a_BlockZ, BLOCK_FACE_NORTH) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_SOUTH)) return true;
				break;
			}
			case E_RAIL_ASCEND_SOUTH:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_NORTH) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY + 1, a_BlockZ, BLOCK_FACE_SOUTH)) return true;
				break;
			}
			case E_RAIL_CURVED_SOUTH_EAST:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_SOUTH) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_EAST)) return true;
				break;
			}
			case E_RAIL_CURVED_SOUTH_WEST:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_SOUTH) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_WEST)) return true;
				break;
			}
			case E_RAIL_CURVED_NORTH_WEST:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_NORTH) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_WEST)) return true;
				break;
			}
			case E_RAIL_CURVED_NORTH_EAST:
			{
				if(IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_NORTH) || 
				   IsNotConnected(a_World, a_BlockX, a_BlockY, a_BlockZ, BLOCK_FACE_EAST)) return true;
				break;
			}
		}
		return false;
	}

	bool IsNotConnected(cWorld *a_World, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Direction, char a_Pure = 0)
	{
		AddDirection(a_BlockX, a_BlockY, a_BlockZ, a_Direction, false);
		char Meta;
		if(a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ) != E_BLOCK_RAIL)
		{
			if((a_World->GetBlock(a_BlockX, a_BlockY + 1, a_BlockZ) != E_BLOCK_RAIL) || (a_Pure != E_PURE_UPDOWN))
			{
				if((a_World->GetBlock(a_BlockX, a_BlockY - 1, a_BlockZ) != E_BLOCK_RAIL) || (a_Pure == E_PURE_NONE))
				{
					return true;
				}
				else
				{
					Meta = a_World->GetBlockMeta(a_BlockX, a_BlockY - 1, a_BlockZ);
				}
			}
			else
			{
				Meta = a_World->GetBlockMeta(a_BlockX, a_BlockY + 1, a_BlockZ);
			}
		}
		else
		{
			Meta = a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
		}
		switch(a_Direction)
		{
			case BLOCK_FACE_NORTH:
			{
				if(Meta == E_RAIL_NORTH_SOUTH ||
				   Meta == E_RAIL_ASCEND_NORTH ||
				   Meta == E_RAIL_ASCEND_SOUTH ||
				   Meta == E_RAIL_CURVED_SOUTH_EAST ||
				   Meta == E_RAIL_CURVED_SOUTH_WEST)
				   return false;
				break;
			}
			case BLOCK_FACE_SOUTH:
			{
				if(Meta == E_RAIL_NORTH_SOUTH ||
				   Meta == E_RAIL_ASCEND_NORTH ||
				   Meta == E_RAIL_ASCEND_SOUTH ||
				   Meta == E_RAIL_CURVED_NORTH_EAST ||
				   Meta == E_RAIL_CURVED_NORTH_WEST)
				   return false;
				break;
			}
			case BLOCK_FACE_EAST:
			{
				if(Meta == E_RAIL_EAST_WEST ||
				   Meta == E_RAIL_ASCEND_EAST ||
				   Meta == E_RAIL_ASCEND_WEST ||
				   Meta == E_RAIL_CURVED_SOUTH_WEST ||
				   Meta == E_RAIL_CURVED_NORTH_WEST)
				   return false;
				break;
			}
			case BLOCK_FACE_WEST:
			{
				if(Meta == E_RAIL_EAST_WEST ||
				   Meta == E_RAIL_ASCEND_EAST ||
				   Meta == E_RAIL_ASCEND_WEST ||
				   Meta == E_RAIL_CURVED_SOUTH_EAST ||
				   Meta == E_RAIL_CURVED_NORTH_EAST)
				   return false;
				break;
			}
		}
		return true;
	}
} ;