summaryrefslogtreecommitdiffstats
path: root/source/cRedstone.cpp
blob: 1a981e654ef825b0d9ae791424a30493516ed73b (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
317
318
319
320
#include "cRedstone.h"
#include "cPiston.h"
#include "cRoot.h"
#include "cWorld.h"
#include "BlockID.h"
#include <iostream>
#include <vector>
#include <string>

cRedstone::cRedstone( cWorld* a_World )
	:m_World ( a_World )
	,m_Metadata ( 0 )
	,m_Direction ( 0 )
{

}

void cRedstone::ChangeRedstone( int fillx, int filly, int fillz, bool added )
{
char before;
//int tempX;
//int tempY;
//int tempZ;
//int state;
//m_Direction
// 0 = x+
// 1 = x-
// 2 = z+
// 3 = z-
// 4 = y+
// 5 = v-


        if (added) {
                m_Metadata = 15;
        } else {
                m_Metadata = 0;
        }
		before = m_Metadata;

		//printf("etb1\n");
		CalculatetRedstone( fillx, filly, fillz ); //calculate all item centers.
		//printf("etb2\n");

		int Block = (int)m_World->GetBlock( fillx, filly, fillz );
		
		switch (Block)//these blocks won't trigger the normal calculaton because they are affected by the redstone around them. So we check each possible channel around them instead.
		{
			case E_BLOCK_AIR:
			case E_BLOCK_PISTON_EXTENSION:
			case E_BLOCK_PISTON:
			case E_BLOCK_STICKY_PISTON:
				{
					m_Metadata = 0;
					m_Direction = 0;
					CalculatetRedstone( fillx+1, filly, fillz );
					m_Metadata = 0;
					m_Direction = 1;
					CalculatetRedstone( fillx-1, filly, fillz );
					m_Metadata = 0;
					m_Direction = 2;
					CalculatetRedstone( fillx, filly, fillz+1 );
					m_Metadata = 0;
					m_Direction = 3;
					CalculatetRedstone( fillx, filly, fillz-1 );
					break;
				}
			case E_BLOCK_REDSTONE_WIRE: //special case for redstone wire.
				{
					m_Direction = 0;
					CalculatetRedstone( fillx+1, filly, fillz );
					m_Direction = 1;
					CalculatetRedstone( fillx-1, filly, fillz );
					m_Direction = 2;
					CalculatetRedstone( fillx, filly, fillz+1 );
					m_Direction = 3;
					CalculatetRedstone( fillx, filly, fillz-1 );
					break;
 				}
		}

	//printf("done here\n");
}

void cRedstone::CalculatetRedstone( int fillx, int filly, int fillz)
{

	if ( ( (int)m_World->GetBlock( fillx, filly, fillz ) == E_BLOCK_STICKY_PISTON ) || ( (int)m_World->GetBlock( fillx, filly, fillz ) == E_BLOCK_PISTON ) ) {
		char pistonMeta = m_World->GetBlockMeta( fillx, filly, fillz );
		if (m_Metadata > 0) {
			if (pistonMeta < 6) { // only extend if piston is not already extended
				cPiston Piston(m_World);
				Piston.ExtendPiston(fillx, filly, fillz);
			}	
		} else {
			if (pistonMeta > 6) { // only retract if piston is not already retracted
				cPiston Piston(m_World);
				Piston.RetractPiston(fillx, filly, fillz);
			}
		}

	}  else if ( ( (int)m_World->GetBlock( fillx, filly, fillz ) == E_ITEM_LEVER ) || ( (int)m_World->GetBlock( fillx, filly, fillz ) == E_ITEM_STONE_BUTTON ) ) {

		if ( (int)m_World->GetBlockMeta( fillx, filly, fillz) > 6 ) { //button powered
			m_Metadata = 15; //change meta to 15 only if redstone power device in on possition is found.
			if ( ( (int)m_World->GetBlock( fillx-1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx-1, filly, fillz) != m_Metadata ) ) {
				CalculatetRedstone(fillx-1,filly,fillz);
			}
			if ( ( (int)m_World->GetBlock( fillx+1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx+1, filly, fillz) != m_Metadata ) ) {
				CalculatetRedstone(fillx+1,filly,fillz);
			}
			if ( ( (int)m_World->GetBlock( fillx, filly, fillz-1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz-1) != m_Metadata ) ) {
				CalculatetRedstone(fillx,filly,fillz-1);
			}
			if ( ( (int)m_World->GetBlock( fillx, filly, fillz+1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz+1) != m_Metadata ) ) {
				CalculatetRedstone(fillx,filly,fillz+1);
			}
		} else {
			if ( ( (int)m_World->GetBlock( fillx-1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx-1, filly, fillz) != m_Metadata ) ) {
				CalculatetRedstone(fillx-1,filly,fillz);
			}
			if ( ( (int)m_World->GetBlock( fillx+1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx+1, filly, fillz) != m_Metadata ) ) {
				CalculatetRedstone(fillx+1,filly,fillz);
			}
			if ( ( (int)m_World->GetBlock( fillx, filly, fillz-1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz-1) != m_Metadata ) ) {
				CalculatetRedstone(fillx,filly,fillz-1);
			}
			if ( ( (int)m_World->GetBlock( fillx, filly, fillz+1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz+1) != m_Metadata ) ) {
				CalculatetRedstone(fillx,filly,fillz+1);
			}
		}

	} else if ( ( (int)m_World->GetBlock( fillx, filly, fillz ) == E_BLOCK_STONE_PRESSURE_PLATE ) || ( (int)m_World->GetBlock( fillx, filly, fillz ) == E_BLOCK_STONE_BUTTON ) ) {

		if ( (int)m_World->GetBlockMeta( fillx, filly, fillz) == 1 ) { //plate powered
			m_Metadata = 15; //change meta to 15 only if redstone power device in on possition is found.
			if ( ( (int)m_World->GetBlock( fillx-1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx-1, filly, fillz) != m_Metadata ) ) {
				CalculatetRedstone(fillx-1,filly,fillz);
			}
			if ( ( (int)m_World->GetBlock( fillx+1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx+1, filly, fillz) != m_Metadata ) ) {
				CalculatetRedstone(fillx+1,filly,fillz);
			}
			if ( ( (int)m_World->GetBlock( fillx, filly, fillz-1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz-1) != m_Metadata ) ) {
				CalculatetRedstone(fillx,filly,fillz-1);
			}
			if ( ( (int)m_World->GetBlock( fillx, filly, fillz+1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz+1) != m_Metadata ) ) {
				CalculatetRedstone(fillx,filly,fillz+1);
			}

		} else {

			if ( ( (int)m_World->GetBlock( fillx-1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx-1, filly, fillz) != m_Metadata ) ) {
				CalculatetRedstone(fillx-1,filly,fillz);
			}
			if ( ( (int)m_World->GetBlock( fillx+1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx+1, filly, fillz) != m_Metadata ) ) {
				CalculatetRedstone(fillx+1,filly,fillz);
			}
			if ( ( (int)m_World->GetBlock( fillx, filly, fillz-1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz-1) != m_Metadata ) ) {
				CalculatetRedstone(fillx,filly,fillz-1);
			}
			if ( ( (int)m_World->GetBlock( fillx, filly, fillz+1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz+1) != m_Metadata ) ) {
				CalculatetRedstone(fillx,filly,fillz+1);
			}
		}

	} else if ( (int)m_World->GetBlock( fillx, filly, fillz ) == E_BLOCK_REDSTONE_TORCH_ON ) { //If torch is on
		//printf("found torch on\n");
		m_Metadata = 15; //change meta to 15 only if redstone torch in on possition is found.
		if ( ( (int)m_World->GetBlock( fillx-1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx-1, filly, fillz) != m_Metadata ) ) {
			CalculatetRedstone(fillx-1,filly,fillz);
		}
		if ( ( (int)m_World->GetBlock( fillx+1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx+1, filly, fillz) != m_Metadata ) ) {
			CalculatetRedstone(fillx+1,filly,fillz);
		}
		if ( ( (int)m_World->GetBlock( fillx, filly, fillz-1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz-1) != m_Metadata ) ) {
			CalculatetRedstone(fillx,filly,fillz-1);
		}
		if ( ( (int)m_World->GetBlock( fillx, filly, fillz+1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz+1) != m_Metadata ) ) {
			CalculatetRedstone(fillx,filly,fillz+1);
		}
	} else if ( (int)m_World->GetBlock( fillx, filly, fillz ) == E_BLOCK_REDSTONE_TORCH_OFF ) { //if the torch is off
		//printf("found torch off\n");
		// no need to change meta here.
		if ( ( (int)m_World->GetBlock( fillx-1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx-1, filly, fillz) != m_Metadata ) ) {
			CalculatetRedstone(fillx-1,filly,fillz);
		}
		if ( ( (int)m_World->GetBlock( fillx+1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx+1, filly, fillz) != m_Metadata ) ) {
			CalculatetRedstone(fillx+1,filly,fillz);
		}
		if ( ( (int)m_World->GetBlock( fillx, filly, fillz-1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz-1) != m_Metadata ) ) {
			CalculatetRedstone(fillx,filly,fillz-1);
		}
		if ( ( (int)m_World->GetBlock( fillx, filly, fillz+1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz+1) != m_Metadata ) ) {
			CalculatetRedstone(fillx,filly,fillz+1);
		}

	} else if ( (int)m_World->GetBlock( fillx, filly, fillz ) == E_BLOCK_REDSTONE_WIRE ) { //simple fill algorithm for redstone wire.

			if ( (int)m_World->GetBlockMeta( fillx, filly, fillz) != m_Metadata ) {
	        m_World->FastSetBlock( fillx, filly, fillz, (char)E_BLOCK_REDSTONE_WIRE, m_Metadata );
					m_Direction = 0;
					CalculatetRedstone( fillx+1, filly, fillz );
					m_Direction = 1;
					CalculatetRedstone( fillx-1, filly, fillz );
					m_Direction = 2;
					CalculatetRedstone( fillx, filly, fillz+1 );
					m_Direction = 3;
					CalculatetRedstone( fillx, filly, fillz-1 );
			}

	} else { //default, check item for torch attached to it. If meta > 0 then turn that torch off, otherwise turn it on. This change needs to be passed to the next world tick.
			//check for torch to east with meta 1 //turn off
			//check for torch to west with meta 2 //turn off
			//check for torch to south with meta 3 //turn off
			//check for torch to north with meta 4 //turn off
			//check for torch above with meta 5 //turn off
		if ( (int)m_World->GetBlock( fillx, filly, fillz ) != E_BLOCK_AIR ) {

				if ( (int)m_Metadata > 0 ) { //wire powered

					//printf("bird: %i dog: %i \n",(int)m_World->GetBlock( fillx, filly+1, fillz ),(int)m_World->GetBlockMeta( fillx, filly+1, fillz));
					if ( ( (int)m_World->GetBlock( fillx+1, filly, fillz ) == E_BLOCK_REDSTONE_TORCH_ON) && ( (int)m_World->GetBlockMeta( fillx+1, filly, fillz) == 1 ) ) { //east
						m_World->FastSetBlock( fillx+1, filly, fillz, E_BLOCK_REDSTONE_TORCH_OFF,  m_World->GetBlockMeta( fillx+1, filly, fillz) );
						m_World->m_RSList.push_back(fillx+1);
						m_World->m_RSList.push_back(filly);
						m_World->m_RSList.push_back(fillz);
						m_World->m_RSList.push_back(00000);
					}
					if ( ( (int)m_World->GetBlock( fillx-1, filly, fillz ) == E_BLOCK_REDSTONE_TORCH_ON) && ( (int)m_World->GetBlockMeta( fillx-1, filly, fillz) == 2 ) ) { //west
						m_World->FastSetBlock( fillx-1, filly, fillz, E_BLOCK_REDSTONE_TORCH_OFF,  m_World->GetBlockMeta( fillx-1, filly, fillz) );
						m_World->m_RSList.push_back(fillx-1);
						m_World->m_RSList.push_back(filly);
						m_World->m_RSList.push_back(fillz);
						m_World->m_RSList.push_back(00000);
					}
					if ( ( (int)m_World->GetBlock( fillx, filly, fillz+1 ) == E_BLOCK_REDSTONE_TORCH_ON) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz+1) == 3 ) ) { //south
						m_World->FastSetBlock( fillx, filly, fillz+1, E_BLOCK_REDSTONE_TORCH_OFF,  m_World->GetBlockMeta( fillx, filly, fillz+1) );
						m_World->m_RSList.push_back(fillx);
						m_World->m_RSList.push_back(filly);
						m_World->m_RSList.push_back(fillz+1);
						m_World->m_RSList.push_back(00000);
					}
					if ( ( (int)m_World->GetBlock( fillx, filly, fillz-1 ) == E_BLOCK_REDSTONE_TORCH_ON) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz-1) == 4 ) ) {  //north
						m_World->FastSetBlock( fillx, filly, fillz-1, E_BLOCK_REDSTONE_TORCH_OFF,  m_World->GetBlockMeta( fillx, filly, fillz-1) );
						m_World->m_RSList.push_back(fillx);
						m_World->m_RSList.push_back(filly);
						m_World->m_RSList.push_back(fillz-1);
						m_World->m_RSList.push_back(00000);
					}
					if ( ( (int)m_World->GetBlock( fillx, filly+1, fillz ) == E_BLOCK_REDSTONE_TORCH_ON) && ( (int)m_World->GetBlockMeta( fillx, filly+1, fillz) == 5 ) ) { //top
						m_World->FastSetBlock( fillx, filly+1, fillz, E_BLOCK_REDSTONE_TORCH_OFF,  m_World->GetBlockMeta( fillx, filly+1, fillz) );
						m_World->m_RSList.push_back(fillx);
						m_World->m_RSList.push_back(filly+1);
						m_World->m_RSList.push_back(fillz);
						m_World->m_RSList.push_back(00000);
					}

				} else { //wire not powered

					bool BlockPowered = IsBlockPowered( fillx, filly, fillz ); //chck this block for other wire turned on or torch turned on below:
					if (BlockPowered == false) { //if block is not bowered by something else then I need to check for off torches and turn them on.

						//printf("echo: %i cruiser: %i \n",(int)m_World->GetBlock( fillx, filly+1, fillz ),(int)m_World->GetBlockMeta( fillx, filly+1, fillz));
						if ( ( (int)m_World->GetBlock( fillx+1, filly, fillz ) == E_BLOCK_REDSTONE_TORCH_OFF) && ( (int)m_World->GetBlockMeta( fillx+1, filly, fillz) == 1 ) ) { //east
						m_World->FastSetBlock( fillx+1, filly, fillz, E_BLOCK_REDSTONE_TORCH_ON,  m_World->GetBlockMeta( fillx+1, filly, fillz) );
							m_World->m_RSList.push_back(fillx+1);
							m_World->m_RSList.push_back(filly);
							m_World->m_RSList.push_back(fillz);
							m_World->m_RSList.push_back(11111);
						}
						if ( ( (int)m_World->GetBlock( fillx-1, filly, fillz ) == E_BLOCK_REDSTONE_TORCH_OFF) && ( (int)m_World->GetBlockMeta( fillx-1, filly, fillz) == 2 ) ) { //west
						m_World->FastSetBlock( fillx-1, filly, fillz, E_BLOCK_REDSTONE_TORCH_ON,  m_World->GetBlockMeta( fillx-1, filly, fillz) );
							m_World->m_RSList.push_back(fillx-1);
							m_World->m_RSList.push_back(filly);
							m_World->m_RSList.push_back(fillz);
							m_World->m_RSList.push_back(11111);
						}
						if ( ( (int)m_World->GetBlock( fillx, filly, fillz+1 ) == E_BLOCK_REDSTONE_TORCH_OFF) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz+1) == 3 ) ) { //south
						m_World->FastSetBlock( fillx, filly, fillz+1, E_BLOCK_REDSTONE_TORCH_ON,  m_World->GetBlockMeta( fillx, filly, fillz+1) );
							m_World->m_RSList.push_back(fillx);
							m_World->m_RSList.push_back(filly);
							m_World->m_RSList.push_back(fillz+1);
							m_World->m_RSList.push_back(11111);;
						}
						if ( ( (int)m_World->GetBlock( fillx, filly, fillz-1 ) == E_BLOCK_REDSTONE_TORCH_OFF) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz-1) == 4 ) ) {  //north
						m_World->FastSetBlock( fillx, filly, fillz-1, E_BLOCK_REDSTONE_TORCH_ON,  m_World->GetBlockMeta( fillx, filly, fillz-1) );
							m_World->m_RSList.push_back(fillx);
							m_World->m_RSList.push_back(filly);
							m_World->m_RSList.push_back(fillz-1);
							m_World->m_RSList.push_back(11111);
						}
						if ( ( (int)m_World->GetBlock( fillx, filly+1, fillz ) == E_BLOCK_REDSTONE_TORCH_OFF) && ( (int)m_World->GetBlockMeta( fillx, filly+1, fillz) == 5 ) ) { //top
						m_World->FastSetBlock( fillx, filly+1, fillz, E_BLOCK_REDSTONE_TORCH_ON,  m_World->GetBlockMeta( fillx, filly+1, fillz) );
							m_World->m_RSList.push_back(fillx);
							m_World->m_RSList.push_back(filly+1);
							m_World->m_RSList.push_back(fillz);
							m_World->m_RSList.push_back(11111);
						}

					}

				}

		}

	}

}


bool cRedstone::IsBlockPowered( int fillx, int filly, int fillz )
{

	if ( (int)m_World->GetBlock( fillx, filly-1, fillz ) == E_BLOCK_REDSTONE_TORCH_ON) { return true; }
	if ( ( (int)m_World->GetBlock( fillx+1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx+1, filly, fillz) > 0 ) ) { return true; }
	if ( ( (int)m_World->GetBlock( fillx-1, filly, fillz ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx-1, filly, fillz) > 0 ) ) { return true; }
	if ( ( (int)m_World->GetBlock( fillx, filly, fillz+1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz+1) > 0 ) ) { return true; }
	if ( ( (int)m_World->GetBlock( fillx, filly, fillz-1 ) == E_BLOCK_REDSTONE_WIRE) && ( (int)m_World->GetBlockMeta( fillx, filly, fillz-1) > 0 ) ) { return true; }
	return false;
}