summaryrefslogtreecommitdiffstats
path: root/source/blocks/Block.cpp
blob: 2fa1ba7185c42dbd9ac05461b7ed07f6d875862a (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
#include "Globals.h"
#include "Block.h"
#include "../cItem.h"
#include "../cWorld.h"
#include "BlockDoor.h"
#include "BlockFire.h"
#include "BlockRedstone.h"
#include "BlockRedstoneTorch.h"
#include "BlockRedstoneRepeater.h"
#include "BlockPiston.h"
#include "BlockWorkbench.h"
#include "BlockEntity.h"
#include "BlockVine.h"
#include "BlockTallGrass.h"
#include "BlockSnow.h"
#include "BlockCloth.h"
#include "BlockSlab.h"
#include "BlockDirt.h"
#include "BlockTorch.h"
#include "BlockWood.h"
#include "BlockLeaves.h"
#include "BlockSapling.h"
#include "BlockFluid.h"
#include "BlockChest.h"
#include "BlockFurnace.h"
#include "BlockDispenser.h"
#include "BlockStairs.h"
#include "BlockLadder.h"
#include "BlockSign.h"
#include "BlockCrops.h"
#include "BlockSugarcane.h"
#include "BlockFlower.h"
#include "BlockMushroom.h"
#include "BlockCactus.h"
#include "BlockStems.h"
#include "BlockGlowstone.h"
#include "BlockStone.h"
#include "BlockMelon.h"
#include "BlockIce.h"
#include "BlockOre.h"
#include "BlockNote.h"

bool cBlockHandler::m_HandlerInitialized = false;
cBlockHandler *cBlockHandler::m_BlockHandler[256];

cBlockHandler *cBlockHandler::GetBlockHandler(BLOCKTYPE a_BlockID)
{
	if(!m_HandlerInitialized)
	{	//We have to initialize
		memset(m_BlockHandler, 0, sizeof(m_BlockHandler));
		m_HandlerInitialized = true;
	}
	if(m_BlockHandler[a_BlockID])
		return m_BlockHandler[a_BlockID];

	return m_BlockHandler[a_BlockID] = CreateBlockHandler(a_BlockID);
}

cBlockHandler *cBlockHandler::CreateBlockHandler(BLOCKTYPE a_BlockID)
{
	switch(a_BlockID)
	{
	case E_BLOCK_WOODEN_DOOR:
	case E_BLOCK_IRON_DOOR:
		return new cBlockDoorHandler(a_BlockID);
	case E_BLOCK_FIRE:
		return new cBlockFireHandler(a_BlockID);
	case E_BLOCK_REDSTONE_TORCH_ON:
	case E_BLOCK_REDSTONE_TORCH_OFF:
		return new cBlockRedstoneTorchHandler(a_BlockID);
	case E_BLOCK_REDSTONE_WIRE:	
		return new cBlockRedstoneHandler(a_BlockID);
	case  E_BLOCK_PISTON:
	case E_BLOCK_STICKY_PISTON:
		return new cBlockPistonHandler(a_BlockID);
	case E_BLOCK_REDSTONE_REPEATER_ON:
	case E_BLOCK_REDSTONE_REPEATER_OFF:
		return new cBlockRedstoneRepeaterHandler(a_BlockID);
	case E_BLOCK_WORKBENCH:
		return new cBlockWorkbenchHandler(a_BlockID);
	case E_BLOCK_SNOW:
		return new cBlockSnowHandler(a_BlockID);
	case E_BLOCK_TALL_GRASS:
		return new cBlockTallGrassHandler(a_BlockID);
	case E_BLOCK_VINES:
		return new cBlockVineHandler(a_BlockID);
	case ::E_BLOCK_WOOL:
		return new cBlockClothHandler(a_BlockID);
	case E_BLOCK_WOODEN_SLAB:
	case E_BLOCK_STONE_SLAB:
	case E_BLOCK_DOUBLE_WOODEN_SLAB:
	case E_BLOCK_DOUBLE_STONE_SLAB:
		return new cBlockSlabHandler(a_BlockID);
	case E_BLOCK_LOG:
	case E_BLOCK_PLANKS:
		return new cBlockWoodHandler(a_BlockID);
	case E_BLOCK_TORCH:
		return new cBlockTorchHandler(a_BlockID);
	case E_BLOCK_DIRT:
	case E_BLOCK_GRASS:
		return new cBlockDirtHandler(a_BlockID);
	case E_BLOCK_LEAVES:
		return new cBlockLeavesHandler(a_BlockID);
	case E_BLOCK_SAPLING:
		return new cBlockSaplingHandler(a_BlockID);
	case E_BLOCK_WATER:
	case E_BLOCK_STATIONARY_WATER:
	case E_BLOCK_STATIONARY_LAVA:
	case E_BLOCK_LAVA:
		return new cBlockFluidHandler(a_BlockID);
	case E_BLOCK_DISPENSER:
		return new cBlockDispenserHandler(a_BlockID);
	case E_BLOCK_FURNACE:
	case E_BLOCK_LIT_FURNACE:
		return new cBlockFurnaceHandler(a_BlockID);
	case E_BLOCK_CHEST:
		return new cBlockChestHandler(a_BlockID);
	case E_BLOCK_ICE:
		return new cBlockIceHandler(a_BlockID);
	case E_BLOCK_LADDER:
		return new cBlockLadderHandler(a_BlockID);
	case E_BLOCK_COBBLESTONE_STAIRS:
	case E_BLOCK_BRICK_STAIRS:
	case E_BLOCK_STONE_BRICK_STAIRS:
	case E_BLOCK_NETHER_BRICK_STAIRS:
	case E_BLOCK_WOODEN_STAIRS:
		return new cBlockStairsHandler(a_BlockID);
	case E_BLOCK_SIGN_POST:
	case E_BLOCK_WALLSIGN:
		return new cBlockSignHandler(a_BlockID);
	case E_BLOCK_CROPS:
		return new cBlockCropsHandler(a_BlockID);
	case E_BLOCK_SUGARCANE:
		return new cBlockSugarcaneHandler(a_BlockID);
	case E_BLOCK_YELLOW_FLOWER:
	case E_BLOCK_RED_ROSE:
		return new cBlockFlowerHandler(a_BlockID);
	case E_BLOCK_BROWN_MUSHROOM:
	case E_BLOCK_RED_MUSHROOM:
		return new cBlockMushroomHandler(a_BlockID);
	case E_BLOCK_CACTUS:
		return new cBlockCactusHandler(a_BlockID);
	case E_BLOCK_MELON_STEM:
	case E_BLOCK_PUMPKIN_STEM:
		return new cBlockStemsHandler(a_BlockID);
	case E_BLOCK_GLOWSTONE:
		return new cBlockGlowstoneHandler(a_BlockID);
	case E_BLOCK_DIAMOND_ORE:
	case E_BLOCK_GOLD_ORE:
	case E_BLOCK_REDSTONE_ORE:
	case E_BLOCK_REDSTONE_ORE_GLOWING:
	case E_BLOCK_EMERALD_ORE:
	case E_BLOCK_IRON_ORE:
	case E_BLOCK_LAPIS_ORE:
	case E_BLOCK_COAL_ORE:
		return new cBlockOreHandler(a_BlockID);
	case E_BLOCK_STONE:
	case E_BLOCK_COBBLESTONE:
		return new cBlockStoneHandler(a_BlockID);
	case E_BLOCK_MELON:
		return new cBlockMelonHandler(a_BlockID);
	case E_BLOCK_NOTE_BLOCK:
		return new cBlockNoteHandler(a_BlockID);
	default:		
		return new cBlockHandler(a_BlockID);
		break;
	}
}

void cBlockHandler::Deinit()
{
	for(int i = 0; i < 256; i++)
	{
		delete m_BlockHandler[i];
	}
}

cBlockHandler::cBlockHandler(BLOCKTYPE a_BlockID)
{
	m_BlockID = a_BlockID;
}

void cBlockHandler::OnUpdate(cWorld *a_World, int a_X, int a_Y, int a_Z)
{

}

void cBlockHandler::OnPlacedByPlayer(cWorld *a_World, cPlayer * a_Player, int a_X, int a_Y, int a_Z, int a_Dir)
{
}

void cBlockHandler::OnDestroyedByPlayer(cWorld *a_World, cPlayer * a_Player, int a_X, int a_Y, int a_Z)
{
}

void cBlockHandler::OnPlaced(cWorld *a_World, int a_X, int a_Y, int a_Z, int a_Dir)
{
	//Notify the neighbors
	NeighborChanged(a_World, a_X - 1, a_Y, a_Z);
	NeighborChanged(a_World, a_X + 1, a_Y, a_Z);
	NeighborChanged(a_World, a_X, a_Y - 1, a_Z);
	NeighborChanged(a_World, a_X, a_Y + 1, a_Z);
	NeighborChanged(a_World, a_X, a_Y, a_Z - 1);
	NeighborChanged(a_World, a_X, a_Y, a_Z + 1);
}

void cBlockHandler::OnDestroyed(cWorld *a_World, int a_X, int a_Y, int a_Z)
{
	//Notify the neighbors
	NeighborChanged(a_World, a_X - 1, a_Y, a_Z);
	NeighborChanged(a_World, a_X + 1, a_Y, a_Z);
	NeighborChanged(a_World, a_X, a_Y - 1, a_Z);
	NeighborChanged(a_World, a_X, a_Y + 1, a_Z);
	NeighborChanged(a_World, a_X, a_Y, a_Z - 1);
	NeighborChanged(a_World, a_X, a_Y, a_Z + 1);
}

void cBlockHandler::NeighborChanged(cWorld *a_World, int a_X, int a_Y, int a_Z)
{
	GetBlockHandler(a_World->GetBlock(a_X, a_Y, a_Z))->OnNeighborChanged(a_World, a_X, a_Y, a_Z);
}

void cBlockHandler::OnNeighborChanged(cWorld *a_World, int a_X, int a_Y, int a_Z)
{

}

void cBlockHandler::OnDigging(cWorld *a_World, cPlayer *a_Player, int a_X, int a_Y, int a_Z)
{

}

void cBlockHandler::OnUse(cWorld *a_World, cPlayer *a_Player, int a_X, int a_Y, int a_Z)
{

}

void cBlockHandler::PlaceBlock(cWorld *a_World, cPlayer *a_Player, NIBBLETYPE a_BlockMeta, int a_X, int a_Y, int a_Z, char a_Dir)
{
	a_World->SetBlock(a_X, a_Y, a_Z, m_BlockID, a_BlockMeta);
	OnPlacedByPlayer(a_World, a_Player, a_X, a_Y, a_Z, a_Dir);
}

char cBlockHandler::GetDropCount()
{
	return 1;
}

int cBlockHandler::GetDropID()
{
	return m_BlockID;
}

NIBBLETYPE cBlockHandler::GetDropMeta(NIBBLETYPE a_BlockMeta)
{
	return a_BlockMeta;		//This keeps most textures. The few other blocks have to override this
}

void cBlockHandler::DropBlock(cWorld *a_World, int a_X, int a_Y, int a_Z)
{
	cItems Drops;
	NIBBLETYPE Meta = a_World->GetBlockMeta(a_X, a_Y, a_Z);
	char DropCount = GetDropCount();
	int DropItem = GetDropID();
	if(DropCount > 0 && DropItem != E_ITEM_EMPTY)
	{
		Drops.push_back(cItem((ENUM_ITEM_ID)DropItem, DropCount, GetDropMeta(Meta)));
		a_World->SpawnItemPickups(Drops, a_X, a_Y, a_Z);
	}
}

bool cBlockHandler::CanBePlacedAt(cWorld *a_World, int a_X, int a_Y, int a_Z, char a_Dir)
{
	return CanBeAt(a_World, a_X, a_Y, a_Z);
}

bool cBlockHandler::CanBeAt(cWorld *a_World, int a_X, int a_Y, int a_Z)
{
	return true;
}

bool cBlockHandler::IsUseable()
{
	return false;
}

bool cBlockHandler::IsClickedThrough()
{
	return false;
}

bool cBlockHandler::IgnoreBuildCollision()
{
	return m_BlockID == E_BLOCK_AIR;
}

bool cBlockHandler::NeedsRandomTicks()
{
	return false;
}

bool cBlockHandler::AllowBlockOnTop()
{
	return true;
}

bool cBlockHandler::CanBePlacedOnSide()
{
	return true;
}

bool cBlockHandler::DropOnUnsuitable()
{
	return true;
}