summaryrefslogtreecommitdiffstats
path: root/src/BlockEntities/DispenserEntity.cpp
blob: 59a9a997ba59724042160229166ad10765d5ba0d (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

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "DispenserEntity.h"
#include "../Chunk.h"
#include "../BlockInfo.h"
#include "../Defines.h"
#include "../World.h"
#include "../Entities/Boat.h"
#include "../Entities/ProjectileEntity.h"
#include "../Simulator/FluidSimulator.h"
#include "../Items/ItemSpawnEgg.h"
#include "../Items/ItemDye.h"



cDispenserEntity::cDispenserEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World) :
	Super(a_BlockType, a_BlockMeta, a_Pos, a_World)
{
	ASSERT(a_BlockType == E_BLOCK_DISPENSER);
}





void cDispenserEntity::DropSpenseFromSlot(cChunk & a_Chunk, int a_SlotNum)
{
	Vector3i DispRelCoord(GetRelPos());
	auto Meta = a_Chunk.GetMeta(DispRelCoord);
	AddDropSpenserDir(DispRelCoord, Meta);
	auto DispChunk = a_Chunk.GetRelNeighborChunkAdjustCoords(DispRelCoord);
	if (DispChunk == nullptr)
	{
		// Would dispense into / interact with a non-loaded chunk, ignore the tick
		return;
	}

	BLOCKTYPE DispBlock = DispChunk->GetBlock(DispRelCoord);
	auto DispAbsCoord = DispChunk->RelativeToAbsolute(DispRelCoord);

	// Dispense the item:
	const cItem & SlotItem = m_Contents.GetSlot(a_SlotNum);
	if (ItemCategory::IsMinecart(SlotItem.m_ItemType) &&
		IsBlockRail(DispBlock))  // only actually place the minecart if there are rails!
	{
		if (m_World->SpawnMinecart(
				DispAbsCoord.x + 0.5,
				DispAbsCoord.y + 0.5,
				DispAbsCoord.z + 0.5,
				SlotItem.m_ItemType
			) != cEntity::INVALID_ID)
		{
			m_Contents.ChangeSlotCount(a_SlotNum, -1);
		}
		return;
	}
	switch (SlotItem.m_ItemType)
	{
		case E_ITEM_BUCKET:
		{
			LOGD(
				"Dispensing empty bucket in slot %d; DispBlock is \"%s\" (%d).",
				a_SlotNum,
				ItemTypeToString(DispBlock).c_str(),
				DispBlock
			);
			switch (DispBlock)
			{
				case E_BLOCK_STATIONARY_WATER:
				case E_BLOCK_WATER:
				{
					if (ScoopUpLiquid(a_SlotNum, E_ITEM_WATER_BUCKET))
					{
						DispChunk->SetBlock(DispRelCoord, E_BLOCK_AIR, 0);
					}
					break;
				}
				case E_BLOCK_STATIONARY_LAVA:
				case E_BLOCK_LAVA:
				{
					if (ScoopUpLiquid(a_SlotNum, E_ITEM_LAVA_BUCKET))
					{
						DispChunk->SetBlock(DispRelCoord, E_BLOCK_AIR, 0);
					}
					break;
				}
				default:
				{
					DropFromSlot(a_Chunk, a_SlotNum);
					break;
				}
			}
			break;
		}  // E_ITEM_BUCKET

		case E_ITEM_WATER_BUCKET:
		{
			LOGD(
				"Dispensing water bucket in slot %d; DispBlock is \"%s\" (%d).",
				a_SlotNum,
				ItemTypeToString(DispBlock).c_str(),
				DispBlock
			);
			if (EmptyLiquidBucket(DispBlock, a_SlotNum))
			{
				DispChunk->SetBlock(DispRelCoord, E_BLOCK_WATER, 0);
			}
			else
			{
				DropFromSlot(a_Chunk, a_SlotNum);
			}
			break;
		}

		case E_ITEM_LAVA_BUCKET:
		{
			LOGD(
				"Dispensing lava bucket in slot %d; DispBlock is \"%s\" (%d).",
				a_SlotNum,
				ItemTypeToString(DispBlock).c_str(),
				DispBlock
			);
			if (EmptyLiquidBucket(DispBlock, a_SlotNum))
			{
				DispChunk->SetBlock(DispRelCoord, E_BLOCK_LAVA, 0);
			}
			else
			{
				DropFromSlot(a_Chunk, a_SlotNum);
			}
			break;
		}

		case E_ITEM_SPAWN_EGG:
		{
			double MobX = 0.5 + DispAbsCoord.x;
			double MobZ = 0.5 + DispAbsCoord.z;
			auto MonsterType =
				cItemSpawnEggHandler::ItemDamageToMonsterType(m_Contents.GetSlot(a_SlotNum).m_ItemDamage);
			if (m_World->SpawnMob(MobX, DispAbsCoord.y, MobZ, MonsterType, false) != cEntity::INVALID_ID)
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_BLOCK_TNT:
		{
			// Spawn a primed TNT entity, if space allows:
			if (!cBlockInfo::IsSolid(DispBlock))
			{
				m_World->SpawnPrimedTNT(
					Vector3d(0.5, 0.5, 0.5) + DispAbsCoord,
					80,
					0
				);  // 80 ticks fuse, no initial velocity
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_ITEM_FLINT_AND_STEEL:
		{
			// Spawn fire if the block in front is air.
			if (DispBlock == E_BLOCK_AIR)
			{
				DispChunk->SetBlock(DispRelCoord, E_BLOCK_FIRE, 0);

				bool ItemBroke = m_Contents.DamageItem(a_SlotNum, 1);

				if (ItemBroke)
				{
					m_Contents.ChangeSlotCount(a_SlotNum, -1);
				}
			}
			break;
		}

		case E_ITEM_FIRE_CHARGE:
		{
			if (SpawnProjectileFromDispenser(
					DispAbsCoord,
					cProjectileEntity::pkFireCharge,
					GetShootVector(Meta) * 20
				) != cEntity::INVALID_ID)
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_ITEM_ARROW:
		{
			if (SpawnProjectileFromDispenser(
					DispAbsCoord,
					cProjectileEntity::pkArrow,
					GetShootVector(Meta) * 30 + Vector3d(0, 1, 0)
				) != cEntity::INVALID_ID)
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_ITEM_SNOWBALL:
		{
			if (SpawnProjectileFromDispenser(
					DispAbsCoord,
					cProjectileEntity::pkSnowball,
					GetShootVector(Meta) * 20 + Vector3d(0, 1, 0)
				) != cEntity::INVALID_ID)
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_ITEM_EGG:
		{
			if (SpawnProjectileFromDispenser(
					DispAbsCoord,
					cProjectileEntity::pkEgg,
					GetShootVector(Meta) * 20 + Vector3d(0, 1, 0)
				) != cEntity::INVALID_ID)
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_ITEM_BOTTLE_O_ENCHANTING:
		{
			if (SpawnProjectileFromDispenser(
					DispAbsCoord,
					cProjectileEntity::pkExpBottle,
					GetShootVector(Meta) * 20 + Vector3d(0, 1, 0)
				) != cEntity::INVALID_ID)
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_ITEM_POTION:
		{
			if (SpawnProjectileFromDispenser(
					DispAbsCoord,
					cProjectileEntity::pkSplashPotion,
					GetShootVector(Meta) * 20 + Vector3d(0, 1, 0),
					&SlotItem
				) != cEntity::INVALID_ID)
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_ITEM_DYE:
		{
			if (SlotItem.m_ItemDamage != E_META_DYE_WHITE)
			{
				DropFromSlot(a_Chunk, a_SlotNum);
				break;
			}

			// Simulate a right-click with bonemeal:
			if (cItemDyeHandler::FertilizePlant(*m_World, DispAbsCoord))
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_ITEM_BOAT:
		case E_ITEM_SPRUCE_BOAT:
		case E_ITEM_BIRCH_BOAT:
		case E_ITEM_JUNGLE_BOAT:
		case E_ITEM_ACACIA_BOAT:
		case E_ITEM_DARK_OAK_BOAT:
		{
			Vector3d spawnPos = DispAbsCoord;
			if (IsBlockWater(DispBlock))
			{
				// Water next to the dispenser, spawn a boat above the water block
				spawnPos.y += 1;
			}
			else if (IsBlockWater(DispChunk->GetBlock(DispRelCoord.addedY(-1))))
			{
				// Water one block below the dispenser, spawn a boat at the dispenser's Y level
				// No adjustment needed
			}
			else
			{
				// There's no eligible water block, drop the boat as a pickup
				DropFromSlot(a_Chunk, a_SlotNum);
				break;
			}

			spawnPos += GetShootVector(Meta) *
				0.8;  // A boat is bigger than one block. Add the shoot vector to put it outside the dispenser.
			spawnPos += Vector3d(0.5, 0.5, 0.5);

			if (m_World->SpawnBoat(spawnPos, cBoat::ItemToMaterial(SlotItem)))
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		case E_ITEM_FIREWORK_ROCKET:
		{
			if (SpawnProjectileFromDispenser(
					DispAbsCoord,
					cProjectileEntity::pkFirework,
					GetShootVector(Meta) * 20 + Vector3d(0, 1, 0),
					&SlotItem
				) != cEntity::INVALID_ID)
			{
				m_Contents.ChangeSlotCount(a_SlotNum, -1);
			}
			break;
		}

		default:
		{
			DropFromSlot(a_Chunk, a_SlotNum);
			break;
		}
	}  // switch (SlotItem.m_ItemType)
}





UInt32 cDispenserEntity::SpawnProjectileFromDispenser(
	Vector3i a_BlockPos,
	cProjectileEntity::eKind a_Kind,
	const Vector3d & a_ShootVector,
	const cItem * a_Item
)
{
	return m_World->CreateProjectile(Vector3d(0.5, 0.5, 0.5) + a_BlockPos, a_Kind, nullptr, a_Item, &a_ShootVector);
}





Vector3d cDispenserEntity::GetShootVector(NIBBLETYPE a_Meta)
{
	switch (a_Meta & E_META_DROPSPENSER_FACING_MASK)
	{
		case E_META_DROPSPENSER_FACING_YP: return Vector3d(0, 1, 0);
		case E_META_DROPSPENSER_FACING_YM: return Vector3d(0, -1, 0);
		case E_META_DROPSPENSER_FACING_XM: return Vector3d(-1, 0, 0);
		case E_META_DROPSPENSER_FACING_XP: return Vector3d(1, 0, 0);
		case E_META_DROPSPENSER_FACING_ZM: return Vector3d(0, 0, -1);
		case E_META_DROPSPENSER_FACING_ZP: return Vector3d(0, 0, 1);
	}
	LOGWARNING("Unhandled dispenser meta: %d", a_Meta);
	ASSERT(!"Unhandled dispenser facing");
	return Vector3d(0, 1, 0);
}





bool cDispenserEntity::ScoopUpLiquid(int a_SlotNum, short a_ResultingBucketItemType)
{
	cItem LiquidBucket(a_ResultingBucketItemType);
	if (m_Contents.GetSlot(a_SlotNum).m_ItemCount == 1)
	{
		// Special case: replacing one empty bucket with one full bucket
		m_Contents.SetSlot(a_SlotNum, LiquidBucket);
		return true;
	}

	// There are stacked buckets at the selected slot, see if a full bucket will fit somewhere else
	if (m_Contents.HowManyCanFit(LiquidBucket) < 1)
	{
		// Cannot fit into m_Contents
		return false;
	}

	m_Contents.ChangeSlotCount(a_SlotNum, -1);
	m_Contents.AddItem(LiquidBucket);
	return true;
}





bool cDispenserEntity::EmptyLiquidBucket(BLOCKTYPE a_BlockInFront, int a_SlotNum)
{
	if ((a_BlockInFront != E_BLOCK_AIR) && !IsBlockLiquid(a_BlockInFront) &&
		!cFluidSimulator::CanWashAway(a_BlockInFront))
	{
		// Not a suitable block in front
		return false;
	}

	cItem EmptyBucket(E_ITEM_BUCKET, 1);
	if (m_Contents.GetSlot(a_SlotNum).m_ItemCount == 1)
	{
		// Change the single full bucket present into a single empty bucket
		m_Contents.SetSlot(a_SlotNum, EmptyBucket);
		return true;
	}

	// There are full buckets stacked at this slot, check if we can fit in the empty bucket
	if (m_Contents.HowManyCanFit(EmptyBucket) < 1)
	{
		// The empty bucket wouldn't fit into m_Contents
		return false;
	}

	// The empty bucket fits in, remove one full bucket and add the empty one
	m_Contents.ChangeSlotCount(a_SlotNum, -1);
	m_Contents.AddItem(EmptyBucket);
	return true;
}