summaryrefslogtreecommitdiffstats
path: root/source/Blocks/BlockHandler.cpp
blob: 5134c1103855e97e7ffd2acf588323757f838e76 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458

#include "Globals.h"
#include "BlockHandler.h"
#include "../Item.h"
#include "../World.h"
#include "../Root.h"
#include "../PluginManager.h"
#include "BlockBed.h"
#include "BlockBrewingStand.h"
#include "BlockCactus.h"
#include "BlockCarpet.h"
#include "BlockCauldron.h"
#include "BlockChest.h"
#include "BlockCloth.h"
#include "BlockCobWeb.h"
#include "BlockCrops.h"
#include "BlockDeadBush.h"
#include "BlockDirt.h"
#include "BlockDoor.h"
#include "BlockDropSpenser.h"
#include "BlockEnderchest.h"
#include "BlockEntity.h"
#include "BlockFarmland.h"
#include "BlockFenceGate.h"
#include "BlockFire.h"
#include "BlockFlower.h"
#include "BlockFlowerPot.h"
#include "BlockFluid.h"
#include "BlockFurnace.h"
#include "BlockGlass.h"
#include "BlockGlowstone.h"
#include "BlockGravel.h"
#include "BlockHopper.h"
#include "BlockIce.h"
#include "BlockLadder.h"
#include "BlockLeaves.h"
#include "BlockLever.h"
#include "BlockMelon.h"
#include "BlockMushroom.h"
#include "BlockMycelium.h"
#include "BlockNote.h"
#include "BlockOre.h"
#include "BlockPiston.h"
#include "BlockRail.h"
#include "BlockRedstone.h"
#include "BlockRedstoneRepeater.h"
#include "BlockRedstoneTorch.h"
#include "BlockSand.h"
#include "BlockSapling.h"
#include "BlockSign.h"
#include "BlockSlab.h"
#include "BlockSnow.h"
#include "BlockStairs.h"
#include "BlockStems.h"
#include "BlockStone.h"
#include "BlockSugarcane.h"
#include "BlockTallGrass.h"
#include "BlockTorch.h"
#include "BlockVine.h"
#include "BlockWood.h"
#include "BlockWorkbench.h"





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





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

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





cBlockHandler * cBlockHandler::CreateBlockHandler(BLOCKTYPE a_BlockType)
{
	switch(a_BlockType)
	{
		// Block handlers, alphabetically sorted:
		case E_BLOCK_ACTIVATOR_RAIL:        return new cBlockRailHandler            (a_BlockType);
		case E_BLOCK_BED:                   return new cBlockBedHandler             (a_BlockType);
		case E_BLOCK_BIRCH_WOOD_STAIRS:     return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_BREWING_STAND:         return new cBlockBrewingStandHandler    (a_BlockType);
		case E_BLOCK_BRICK_STAIRS:          return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_BROWN_MUSHROOM:        return new cBlockMushroomHandler        (a_BlockType);
		case E_BLOCK_CACTUS:                return new cBlockCactusHandler          (a_BlockType);
		case E_BLOCK_CARROTS:               return new cBlockCropsHandler           (a_BlockType);
		case E_BLOCK_CARPET:                return new cBlockCarpetHandler          (a_BlockType);
		case E_BLOCK_CAULDRON:              return new cBlockCauldronHandler        (a_BlockType);
		case E_BLOCK_CHEST:                 return new cBlockChestHandler           (a_BlockType);
		case E_BLOCK_COAL_ORE:              return new cBlockOreHandler             (a_BlockType);
		case E_BLOCK_COBBLESTONE:           return new cBlockStoneHandler           (a_BlockType);
		case E_BLOCK_COBBLESTONE_STAIRS:    return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_COBWEB:                return new cBlockCobWebHandler          (a_BlockType);
		case E_BLOCK_CROPS:                 return new cBlockCropsHandler           (a_BlockType);
		case E_BLOCK_DEAD_BUSH:             return new cBlockDeadBushHandler        (a_BlockType);
		case E_BLOCK_DETECTOR_RAIL:         return new cBlockRailHandler            (a_BlockType);
		case E_BLOCK_DIAMOND_ORE:           return new cBlockOreHandler             (a_BlockType);
		case E_BLOCK_DIRT:                  return new cBlockDirtHandler            (a_BlockType);
		case E_BLOCK_DISPENSER:             return new cBlockDropSpenserHandler     (a_BlockType);
		case E_BLOCK_DOUBLE_STONE_SLAB:     return new cBlockDoubleSlabHandler      (a_BlockType);
		case E_BLOCK_DOUBLE_WOODEN_SLAB:    return new cBlockDoubleSlabHandler      (a_BlockType);
		case E_BLOCK_DROPPER:               return new cBlockDropSpenserHandler     (a_BlockType);
		case E_BLOCK_EMERALD_ORE:           return new cBlockOreHandler             (a_BlockType);
		case E_BLOCK_ENDER_CHEST:           return new cBlockEnderchestHandler      (a_BlockType);
		case E_BLOCK_FARMLAND:              return new cBlockFarmlandHandler;
		case E_BLOCK_FENCE_GATE:            return new cBlockFenceGateHandler       (a_BlockType);
		case E_BLOCK_FIRE:                  return new cBlockFireHandler            (a_BlockType);
		case E_BLOCK_FLOWER_POT:            return new cBlockFlowerPotHandler       (a_BlockType);
		case E_BLOCK_FURNACE:               return new cBlockFurnaceHandler         (a_BlockType);
		case E_BLOCK_GLOWSTONE:             return new cBlockGlowstoneHandler       (a_BlockType);
		case E_BLOCK_GOLD_ORE:              return new cBlockOreHandler             (a_BlockType);
		case E_BLOCK_GLASS:                 return new cBlockGlassHandler           (a_BlockType);
		case E_BLOCK_GRASS:                 return new cBlockDirtHandler            (a_BlockType);
		case E_BLOCK_GRAVEL:                return new cBlockGravelHandler          (a_BlockType);
		case E_BLOCK_HOPPER:                return new cBlockHopperHandler          (a_BlockType);
		case E_BLOCK_ICE:                   return new cBlockIceHandler             (a_BlockType);
		case E_BLOCK_IRON_DOOR:             return new cBlockDoorHandler            (a_BlockType);
		case E_BLOCK_IRON_ORE:              return new cBlockOreHandler             (a_BlockType);
		case E_BLOCK_JUKEBOX:               return new cBlockEntityHandler          (a_BlockType);
		case E_BLOCK_JUNGLE_WOOD_STAIRS:    return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_LADDER:                return new cBlockLadderHandler          (a_BlockType);
		case E_BLOCK_LEVER:                 return new cBlockLeverHandler           (a_BlockType);
		case E_BLOCK_LAPIS_ORE:             return new cBlockOreHandler             (a_BlockType);
		case E_BLOCK_LAVA:                  return new cBlockFluidHandler           (a_BlockType);
		case E_BLOCK_LEAVES:                return new cBlockLeavesHandler          (a_BlockType);
		case E_BLOCK_LIT_FURNACE:           return new cBlockFurnaceHandler         (a_BlockType);
		case E_BLOCK_LOG:                   return new cBlockWoodHandler            (a_BlockType);
		case E_BLOCK_MELON:                 return new cBlockMelonHandler           (a_BlockType);
		case E_BLOCK_MELON_STEM:            return new cBlockStemsHandler           (a_BlockType);
		case E_BLOCK_MYCELIUM:              return new cBlockMyceliumHandler        (a_BlockType);
		case E_BLOCK_NETHER_BRICK_STAIRS:   return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_NOTE_BLOCK:            return new cBlockNoteHandler            (a_BlockType);
		case E_BLOCK_PISTON:                return new cBlockPistonHandler          (a_BlockType);
		case E_BLOCK_PISTON_EXTENSION:      return new cBlockPistonHeadHandler      ();
		case E_BLOCK_PLANKS:                return new cBlockWoodHandler            (a_BlockType);
		case E_BLOCK_PUMPKIN_STEM:          return new cBlockStemsHandler           (a_BlockType);
		case E_BLOCK_QUARTZ_STAIR:          return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_RAIL:                  return new cBlockRailHandler            (a_BlockType);
		case E_BLOCK_POTATOES:              return new cBlockCropsHandler           (a_BlockType);
		case E_BLOCK_POWERED_RAIL:          return new cBlockRailHandler            (a_BlockType);
		case E_BLOCK_REDSTONE_ORE:          return new cBlockOreHandler             (a_BlockType);
		case E_BLOCK_REDSTONE_ORE_GLOWING:  return new cBlockOreHandler             (a_BlockType);
		case E_BLOCK_REDSTONE_REPEATER_OFF: return new cBlockRedstoneRepeaterHandler(a_BlockType);
		case E_BLOCK_REDSTONE_REPEATER_ON:  return new cBlockRedstoneRepeaterHandler(a_BlockType);
		case E_BLOCK_REDSTONE_TORCH_OFF:    return new cBlockRedstoneTorchHandler   (a_BlockType);
		case E_BLOCK_REDSTONE_TORCH_ON:     return new cBlockRedstoneTorchHandler   (a_BlockType);
		case E_BLOCK_REDSTONE_WIRE:	        return new cBlockRedstoneHandler        (a_BlockType);
		case E_BLOCK_RED_MUSHROOM:          return new cBlockMushroomHandler        (a_BlockType);
		case E_BLOCK_RED_ROSE:              return new cBlockFlowerHandler          (a_BlockType);
		case E_BLOCK_SAND:                  return new cBlockSandHandler            (a_BlockType);
		case E_BLOCK_SANDSTONE_STAIRS:      return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_SAPLING:               return new cBlockSaplingHandler         (a_BlockType);
		case E_BLOCK_SIGN_POST:             return new cBlockSignHandler            (a_BlockType);
		case E_BLOCK_SNOW:                  return new cBlockSnowHandler            (a_BlockType);
		case E_BLOCK_SPRUCE_WOOD_STAIRS:    return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_STATIONARY_LAVA:       return new cBlockFluidHandler           (a_BlockType);
		case E_BLOCK_STATIONARY_WATER:      return new cBlockFluidHandler           (a_BlockType);
		case E_BLOCK_STICKY_PISTON:         return new cBlockPistonHandler          (a_BlockType);
		case E_BLOCK_STONE:                 return new cBlockStoneHandler           (a_BlockType);
		case E_BLOCK_STONE_BRICK_STAIRS:    return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_STONE_SLAB:            return new cBlockSlabHandler            (a_BlockType);
		case E_BLOCK_SUGARCANE:             return new cBlockSugarcaneHandler       (a_BlockType);
		case E_BLOCK_TALL_GRASS:            return new cBlockTallGrassHandler       (a_BlockType);
		case E_BLOCK_TORCH:                 return new cBlockTorchHandler           (a_BlockType);
		case E_BLOCK_VINES:                 return new cBlockVineHandler            (a_BlockType);
		case E_BLOCK_WALLSIGN:              return new cBlockSignHandler            (a_BlockType);
		case E_BLOCK_WATER:                 return new cBlockFluidHandler           (a_BlockType);
		case E_BLOCK_WOODEN_DOOR:           return new cBlockDoorHandler            (a_BlockType);
		case E_BLOCK_WOODEN_SLAB:           return new cBlockSlabHandler            (a_BlockType);
		case E_BLOCK_WOODEN_STAIRS:         return new cBlockStairsHandler          (a_BlockType);
		case E_BLOCK_WOOL:                  return new cBlockClothHandler           (a_BlockType);
		case E_BLOCK_WORKBENCH:             return new cBlockWorkbenchHandler       (a_BlockType);
		case E_BLOCK_YELLOW_FLOWER:         return new cBlockFlowerHandler          (a_BlockType);
			
		default: return new cBlockHandler(a_BlockType);
	}
}





void cBlockHandler::Deinit()
{
	for (int i = 0; i < 256; i++)
	{
		delete m_BlockHandler[i];
	}
	memset(m_BlockHandler, 0, sizeof(m_BlockHandler));  // Don't leave any dangling pointers around, just in case
	m_HandlerInitialized = false;
}





cBlockHandler::cBlockHandler(BLOCKTYPE a_BlockType)
{
	m_BlockType = a_BlockType;
}





bool cBlockHandler::GetPlacementBlockTypeMeta(
	cWorld * a_World, cPlayer * a_Player,
	int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, 
	int a_CursorX, int a_CursorY, int a_CursorZ,
	BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
)
{
	// By default, all blocks can be placed and the meta is copied over from the item's damage value:
	a_BlockType = m_BlockType;
	a_BlockMeta = (NIBBLETYPE)(a_Player->GetEquippedItem().m_ItemDamage & 0x0f);
	return true;
}





void cBlockHandler::OnUpdate(cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ)
{
}





void cBlockHandler::OnPlacedByPlayer(cWorld * a_World, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
}





void cBlockHandler::OnDestroyedByPlayer(cWorld *a_World, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ)
{
}





void cBlockHandler::OnPlaced(cWorld *a_World, int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
	// Notify the neighbors
	NeighborChanged(a_World, a_BlockX - 1, a_BlockY, a_BlockZ);
	NeighborChanged(a_World, a_BlockX + 1, a_BlockY, a_BlockZ);
	NeighborChanged(a_World, a_BlockX, a_BlockY - 1, a_BlockZ);
	NeighborChanged(a_World, a_BlockX, a_BlockY + 1, a_BlockZ);
	NeighborChanged(a_World, a_BlockX, a_BlockY, a_BlockZ - 1);
	NeighborChanged(a_World, a_BlockX, a_BlockY, a_BlockZ + 1);
}





void cBlockHandler::OnDestroyed(cWorld *a_World, int a_BlockX, int a_BlockY, int a_BlockZ)
{
	// Notify the neighbors
	NeighborChanged(a_World, a_BlockX - 1, a_BlockY, a_BlockZ);
	NeighborChanged(a_World, a_BlockX + 1, a_BlockY, a_BlockZ);
	NeighborChanged(a_World, a_BlockX, a_BlockY - 1, a_BlockZ);
	NeighborChanged(a_World, a_BlockX, a_BlockY + 1, a_BlockZ);
	NeighborChanged(a_World, a_BlockX, a_BlockY, a_BlockZ - 1);
	NeighborChanged(a_World, a_BlockX, a_BlockY, a_BlockZ + 1);
}





void cBlockHandler::NeighborChanged(cWorld *a_World, int a_BlockX, int a_BlockY, int a_BlockZ)
{
	if ((a_BlockY >= 0) && (a_BlockY < cChunkDef::Height))
	{
		GetBlockHandler(a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ))->OnNeighborChanged(a_World, a_BlockX, a_BlockY, a_BlockZ);
	}
}





void cBlockHandler::OnNeighborChanged(cWorld *a_World, int a_BlockX, int a_BlockY, int a_BlockZ)
{
}





void cBlockHandler::OnDigging(cWorld *a_World, cPlayer *a_Player, int a_BlockX, int a_BlockY, int a_BlockZ)
{
}





void cBlockHandler::OnUse(cWorld *a_World, cPlayer *a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
{
}





void cBlockHandler::ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta)
{
	// Setting the meta to a_BlockMeta keeps most textures. The few other blocks have to override this.
	a_Pickups.push_back(cItem(m_BlockType, 1, a_BlockMeta));
}





void cBlockHandler::DropBlock(cWorld * a_World, cEntity * a_Digger, int a_BlockX, int a_BlockY, int a_BlockZ)
{
	cItems Pickups;
	NIBBLETYPE Meta = a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
	ConvertToPickups(Pickups, Meta);
	
	// Allow plugins to modify the pickups:
	cRoot::Get()->GetPluginManager()->CallHookBlockToPickups(a_World, a_Digger, a_BlockX, a_BlockY, a_BlockZ, m_BlockType, Meta, Pickups);
	
	if (!Pickups.empty())
	{
		a_World->SpawnItemPickups(Pickups, a_BlockX, a_BlockY, a_BlockZ);
	}
}





const char * cBlockHandler::GetStepSound()
{
	return "step.stone";
}





bool cBlockHandler::CanBeAt(int a_BlockX, int a_BlockY, int a_BlockZ, const cChunk & a_Chunk)
{
	return true;
}





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





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





bool cBlockHandler::DoesIgnoreBuildCollision(void)
{
	return (m_BlockType == E_BLOCK_AIR);
}





bool cBlockHandler::DoesAllowBlockOnTop(void)
{
	return true;
}





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





bool cBlockHandler::DoesDropOnUnsuitable(void)
{
	return true;
}





void cBlockHandler::Check(int a_RelX, int a_RelY, int a_RelZ, cChunk & a_Chunk)
{
	if (!CanBeAt(a_RelX, a_RelY, a_RelZ, a_Chunk))
	{
		if (DoesDropOnUnsuitable())
		{
			int BlockX = a_RelX + a_Chunk.GetPosX() * cChunkDef::Width;
			int BlockZ = a_RelZ + a_Chunk.GetPosZ() * cChunkDef::Width;
			DropBlock(a_Chunk.GetWorld(), NULL, BlockX, a_RelY, BlockZ);
		}
		
		a_Chunk.SetBlock(a_RelX, a_RelY, a_RelZ, E_BLOCK_AIR, 0);
	}
	else
	{
		// Wake up the simulators for this block:
		int BlockX = a_RelX + a_Chunk.GetPosX() * cChunkDef::Width;
		int BlockZ = a_RelZ + a_Chunk.GetPosZ() * cChunkDef::Width;
		a_Chunk.GetWorld()->GetSimulatorManager()->WakeUp(BlockX, a_RelY, BlockZ, &a_Chunk);
	}
}