summaryrefslogtreecommitdiffstats
path: root/source/Piston.cpp
blob: f83eb016d6fd5c9a7d33baabcb3e7d2d84229ed3 (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
#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "Piston.h"
#include "ChunkDef.h"
#include "Pickup.h"
#include "Item.h"
#include "Root.h"
#include "ClientHandle.h"
#include "World.h"
#include "BlockID.h"
#include "Server.h"
#include "Blocks/BlockHandler.h"





extern bool g_BlockPistonBreakable[];




//Replace AddDir (...) with the switch cases that, sets coords in direction of piston face
#define AddDir( x, y, z, dir, amount ) \
	switch (dir) \
	{ \
		case 0: (y)-=(amount); break; \
		case 1: (y)+=(amount); break; \
		case 2: (z)-=(amount); break; \
		case 3: (z)+=(amount); break; \
		case 4: (x)-=(amount); break; \
		case 5: (x)+=(amount); break; \
	}





cPiston::cPiston(cWorld * a_World)
	: m_World(a_World)
{

}





unsigned short cPiston::FirstPassthroughBlock(int pistonX, int pistonY, int pistonZ, char pistonmeta)
{
	unsigned short ret;
	pistonmeta &= 7;
	if (pistonmeta >= 6)
	{
		// Just in case, it shouldn't happen but if it would, it'd cause inf loop
		LOGD("cPiston::FirstPassthroughBlock - piston has invalid meta data!\n");
		return 9001;
	}
	BLOCKTYPE currBlock;
	NIBBLETYPE currMeta;
	for (ret = 0; ret < 24; ret++)  //Push up to 24 blocks
	{
		AddDir( pistonX, pistonY, pistonZ, pistonmeta, 1) //Set the coords one further from the piston direction
		m_World->GetBlockTypeMeta(pistonX, pistonY, pistonZ, currBlock, currMeta);
		if ((currBlock == E_BLOCK_BEDROCK) ||
			(currBlock == E_BLOCK_OBSIDIAN) ||
			(currBlock == E_BLOCK_PISTON_EXTENSION) ||
			( (currMeta & 0x8) != 0x0 ) || //Seems to include signs as well
			(currBlock == E_BLOCK_CHEST) ||
			(currBlock == E_BLOCK_DISPENSER) ||
			(currBlock == E_BLOCK_DROPPER) ||
			(currBlock == E_BLOCK_FURNACE) ||
			(currBlock == E_BLOCK_LIT_FURNACE) ||
			(currBlock == E_BLOCK_HOPPER) ||
			(currBlock == E_BLOCK_JUKEBOX) ||
			(currBlock == E_BLOCK_NOTE_BLOCK))
		{
			return 9001;
		}
		if (g_BlockPistonBreakable[currBlock]) //If it's a breakable block (air, torch, etc.) then the line of blocks can be pushed
		{
			return ret;
		}
	}
	return 9001; //There is no space for the blocks to move within 24 spaces, piston can't push
}





void cPiston::ExtendPiston( int pistx, int pisty, int pistz )
{
	BLOCKTYPE pistonBlock;
	NIBBLETYPE pistonMeta;
	m_World->GetBlockTypeMeta(pistx, pisty, pistz, pistonBlock, pistonMeta);
	
	char isSticky = (char)(pistonBlock == E_BLOCK_STICKY_PISTON) * 8;
	if ( (pistonMeta & 0x8) != 0x0 )
	{
		// Piston already extended, bail out
		return;
	}
	
	unsigned short dist = FirstPassthroughBlock(pistx, pisty, pistz, pistonMeta);
	if (dist > 9000) return; // FirstPassthroughBlock says piston can't push anything, bail out

	AddDir(pistx, pisty, pistz, pistonMeta & 7, dist + 1) //Get the coords of the air / breakable block in the line, dist+1 because of coords
	BLOCKTYPE currBlock;
	NIBBLETYPE currMeta;
	m_World->GetBlockTypeMeta(pistx, pisty, pistz, currBlock, currMeta);
	if (currBlock != E_BLOCK_AIR)
	{
		cBlockHandler * Handler = BlockHandler(currBlock);
		if (Handler->DoesDropOnUnsuitable())
		{
			Handler->DropBlock(m_World, NULL, pistx, pisty, pistz); //If block is breakable, drop it
		}
	}
	int oldx = pistx, oldy = pisty, oldz = pistz; //Make a second set of coord vars along with the originals
	NIBBLETYPE currBlockMeta;
	if (dist != 0) //Check for single block being pushed - for loop doesn't catch it in time and breaks stuffz
	{
		for (int i = dist + 1; i > 1; i--) //Decrement from the dropped breakable block to two further than the piston (one further will be future extension)
		{
			AddDir(pistx, pisty, pistz, pistonMeta & 7, -1) //Move one set of coords one back from breakable dropped block
			m_World->GetBlockTypeMeta(pistx, pisty, pistz, currBlock, currBlockMeta); //Get the block
			m_World->SetServerBlock( oldx, oldy, oldz, currBlock, currBlockMeta); //Set the block at the location of the original coords
			oldx = pistx; //Shift the selectors down a block and repeat
			oldy = pisty;
			oldz = pistz;
		}
	}

	int extx = pistx;
	int exty = pisty;
	int extz = pistz;

	AddDir(pistx, pisty, pistz, pistonMeta & 7, -1) //Move back one block to the piston base

	if (pistonBlock == E_BLOCK_STICKY_PISTON)
	{
		m_World->BroadcastBlockAction(pistx, pisty, pistz, 0, pistonMeta, E_BLOCK_STICKY_PISTON);
	}
	else
	{
		m_World->BroadcastBlockAction(pistx, pisty, pistz, 0, pistonMeta, E_BLOCK_PISTON);
	}

	m_World->BroadcastSoundEffect("tile.piston.out", pistx * 8, pisty * 8, pistz * 8, 0.5f, 0.7f);
	m_World->FastSetBlock( pistx, pisty, pistz, pistonBlock, pistonMeta | 0x8 ); //Set the base
	m_World->SetServerBlock(extx, exty, extz, E_BLOCK_PISTON_EXTENSION, isSticky + pistonMeta); //Set the arm
}





void cPiston::RetractPiston( int pistx, int pisty, int pistz )
{
	BLOCKTYPE pistonBlock;
	NIBBLETYPE pistonMeta;
	m_World->GetBlockTypeMeta(pistx, pisty, pistz, pistonBlock, pistonMeta);
	if (pistonMeta <= 6)
	{
		// Already retracted, bail out
		return;
	}
	
	if (pistonBlock == E_BLOCK_STICKY_PISTON)
	{
		m_World->BroadcastBlockAction(pistx, pisty, pistz, 1, pistonMeta & ~(8), E_BLOCK_STICKY_PISTON);
	}
	else
	{
		m_World->BroadcastBlockAction(pistx, pisty, pistz, 1, pistonMeta & ~(8), E_BLOCK_PISTON);
	}

	m_World->BroadcastSoundEffect("tile.piston.in", pistx * 8, pisty * 8, pistz * 8, 0.5f, 0.7f);
	m_World->SetServerBlock(pistx, pisty, pistz, pistonBlock, pistonMeta & ~(8)); //Set the base

	AddDir(pistx, pisty, pistz, pistonMeta & 7, 1) //Move forwards to the extension coord
	if (m_World->GetBlock(pistx, pisty, pistz) != E_BLOCK_PISTON_EXTENSION)
	{
		LOGD("%s: Piston without an extension?", __FUNCTION__);
		return;
	}
	
	if (pistonBlock == E_BLOCK_STICKY_PISTON)
	{
		int tempx = pistx, tempy = pisty, tempz = pistz;
		AddDir( tempx, tempy, tempz, pistonMeta & 7, 1 ) //Move forward to the block being pulled
		BLOCKTYPE tempblock;
		NIBBLETYPE tempmeta;
		m_World->GetBlockTypeMeta(tempx, tempy, tempz, tempblock, tempmeta); //Check for pullable-ness
		if (
			(tempblock == E_BLOCK_OBSIDIAN) || 
			(tempblock == E_BLOCK_BEDROCK) || 
			(tempblock == E_BLOCK_PISTON_EXTENSION) ||
			(g_BlockPistonBreakable[tempblock]) ||
			( (tempmeta & 0x8) != 0x0 ) || //Seems to include signs as well
			(tempblock == E_BLOCK_CHEST) ||
			(tempblock == E_BLOCK_DISPENSER) ||
			(tempblock == E_BLOCK_DROPPER) ||
			(tempblock == E_BLOCK_FURNACE) ||
			(tempblock == E_BLOCK_LIT_FURNACE) ||
			(tempblock == E_BLOCK_HOPPER) ||
			(tempblock == E_BLOCK_JUKEBOX) ||
			(tempblock == E_BLOCK_NOTE_BLOCK)
		)
		{
			// These cannot be moved by the sticky piston, bail out
			m_World->SetServerBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0);
			return;
		}

		m_World->SetServerBlock(pistx, pisty, pistz, tempblock, tempmeta);
		m_World->SetServerBlock(tempx, tempy, tempz, E_BLOCK_AIR, 0);
	}
	else
	{
		m_World->SetServerBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0);
	}
}