summaryrefslogtreecommitdiffstats
path: root/src/Items/ItemMobHead.h
blob: 76275803b6d52b8a2b9516d56941ae41dd71d627 (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

#pragma once

#include "ItemHandler.h"
#include "../World.h"
#include "../BlockEntities/MobHeadEntity.h"





class cItemMobHeadHandler final : public cItemHandler
{
	using Super = cItemHandler;

  public:
	using Super::Super;





	virtual bool CommitPlacement(
		cPlayer & a_Player,
		const cItem & a_HeldItem,
		const Vector3i a_PlacePosition,
		const eBlockFace a_ClickedBlockFace,
		const Vector3i a_CursorPosition
	) const override
	{
		// Cannot place a head at "no face" and from the bottom:
		if ((a_ClickedBlockFace == BLOCK_FACE_NONE) || (a_ClickedBlockFace == BLOCK_FACE_BOTTOM))
		{
			return false;
		}

		// If the placed head is a wither, try to spawn the wither first:
		if (a_HeldItem.m_ItemDamage == E_META_HEAD_WITHER)
		{
			if (TrySpawnWitherAround(a_Player, a_PlacePosition))
			{
				return true;
			}
			// Wither not created, proceed with regular head placement
		}

		if (!a_Player.PlaceBlock(a_PlacePosition, E_BLOCK_HEAD, BlockFaceToBlockMeta(a_ClickedBlockFace)))
		{
			return false;
		}

		RegularHeadPlaced(a_Player, a_HeldItem, a_PlacePosition, a_ClickedBlockFace);
		return true;
	}





	/** Called after placing a regular head block with no mob spawning.
	Adjusts the mob head entity based on the equipped item's data. */
	void RegularHeadPlaced(
		const cPlayer & a_Player,
		const cItem & a_HeldItem,
		const Vector3i a_PlacePosition,
		const eBlockFace a_ClickedBlockFace
	) const
	{
		const auto HeadType = static_cast<eMobHeadType>(a_HeldItem.m_ItemDamage);
		const auto BlockMeta = static_cast<NIBBLETYPE>(a_ClickedBlockFace);

		// Use a callback to set the properties of the mob head block entity:
		a_Player.GetWorld()->DoWithBlockEntityAt(
			a_PlacePosition,
			[&a_Player, HeadType, BlockMeta](cBlockEntity & a_BlockEntity)
			{
				ASSERT(a_BlockEntity.GetBlockType() == E_BLOCK_HEAD);

				auto & MobHeadEntity = static_cast<cMobHeadEntity &>(a_BlockEntity);

				int Rotation = 0;
				if (BlockMeta == 1)
				{
					Rotation = FloorC(a_Player.GetYaw() * 16.0f / 360.0f + 0.5f) & 0x0f;
				}

				MobHeadEntity.SetType(HeadType);
				MobHeadEntity.SetRotation(static_cast<eMobHeadRotation>(Rotation));
				return false;
			}
		);
	}





	/** Spawns a wither if the wither skull placed at the specified coords completes wither's spawning formula.
	Returns true if the wither was created. */
	bool TrySpawnWitherAround(cPlayer & a_Player, const Vector3i a_BlockPos) const
	{
		// No wither can be created at Y < 2 - not enough space for the formula:
		if (a_BlockPos.y < 2)
		{
			return false;
		}

		// Check for all relevant wither locations:
		static const Vector3i RelCoords[] = {
			{0, 0, 0},
			{1, 0, 0},
			{-1, 0, 0},
			{0, 0, 1},
			{0, 0, -1},
		};

		for (auto & RelCoord : RelCoords)
		{
			if (TrySpawnWitherAt(*a_Player.GetWorld(), a_Player, a_BlockPos, RelCoord.x, RelCoord.z))
			{
				return true;
			}
		}  // for i - RelCoords[]

		return false;
	}


	/** Tries to spawn a wither at the specified offset from the placed head block.
	PlacedHead coords are used to override the block query - at those coords the block is not queried from the world,
	but assumed to be a head instead.
	Offset is used to shift the image around the X and Z axis.
	Returns true iff the wither was created successfully. */
	bool TrySpawnWitherAt(cWorld & a_World, cPlayer & a_Player, Vector3i a_PlacedHeadPos, int a_OffsetX, int a_OffsetZ)
		const
	{
		// Image for the wither at the X axis:
		static const sSetBlock ImageWitherX[] = {
			{-1, 0, 0, E_BLOCK_HEAD, 0},
			{0, 0, 0, E_BLOCK_HEAD, 0},
			{1, 0, 0, E_BLOCK_HEAD, 0},
			{-1, -1, 0, E_BLOCK_SOULSAND, 0},
			{0, -1, 0, E_BLOCK_SOULSAND, 0},
			{1, -1, 0, E_BLOCK_SOULSAND, 0},
			{-1, -2, 0, E_BLOCK_AIR, 0},
			{0, -2, 0, E_BLOCK_SOULSAND, 0},
			{1, -2, 0, E_BLOCK_AIR, 0},
		};

		// Image for the wither at the Z axis:
		static const sSetBlock ImageWitherZ[] = {
			{0, 0, -1, E_BLOCK_HEAD, 0},
			{0, 0, 0, E_BLOCK_HEAD, 0},
			{0, 0, 1, E_BLOCK_HEAD, 0},
			{0, -1, -1, E_BLOCK_SOULSAND, 0},
			{0, -1, 0, E_BLOCK_SOULSAND, 0},
			{0, -1, 1, E_BLOCK_SOULSAND, 0},
			{0, -2, -1, E_BLOCK_AIR, 0},
			{0, -2, 0, E_BLOCK_SOULSAND, 0},
			{0, -2, 1, E_BLOCK_AIR, 0},
		};

		// Try to spawn the wither from each image:
		return (
			TrySpawnWitherFromImage(a_World, a_Player, ImageWitherX, a_PlacedHeadPos, a_OffsetX, a_OffsetZ) ||
			TrySpawnWitherFromImage(a_World, a_Player, ImageWitherZ, a_PlacedHeadPos, a_OffsetX, a_OffsetZ)
		);
	}





	/** Tries to spawn a wither from the specified image at the specified offset from the placed head block.
	PlacedHead coords are used to override the block query - at those coords the block is not queried from the world,
	but assumed to be a head instead.
	Offset is used to shift the image around the X and Z axis.
	Returns true iff the wither was created successfully. */
	bool TrySpawnWitherFromImage(
		cWorld & a_World,
		cPlayer & a_Player,
		const sSetBlock (&a_Image)[9],
		Vector3i a_PlacedHeadPos,
		int a_OffsetX,
		int a_OffsetZ
	) const
	{
		std::array<Vector3i, 9> PositionsToClear;

		// Check each block individually:
		for (size_t i = 0; i != std::size(a_Image); i++)
		{
			// The absolute coords of the block in the image to check.
			const Vector3i Block(
				a_PlacedHeadPos.x + a_OffsetX + a_Image[i].GetX(),
				a_PlacedHeadPos.y + a_Image[i].GetY(),
				a_PlacedHeadPos.z + a_OffsetZ + a_Image[i].GetZ()
			);

			// If the query is for the head the player is about to place (remember, it hasn't been set into the world
			// yet), short-circuit-evaluate it:
			if (Block == a_PlacedHeadPos)
			{
				if (a_Image[i].m_BlockType != E_BLOCK_HEAD)
				{
					return false;  // Didn't match.
				}

				PositionsToClear[i] = Block;
				continue;  // Matched, continue checking the rest of the image.
			}

			// Query the world block:
			BLOCKTYPE BlockType;
			NIBBLETYPE BlockMeta;
			if (!a_World.GetBlockTypeMeta(Block, BlockType, BlockMeta))
			{
				// Cannot query block, assume unloaded chunk, fail to spawn the wither
				return false;
			}

			// Compare the world block:
			if (BlockType != a_Image[i].m_BlockType)
			{
				return false;
			}

			// If it is a mob head, check it's a wither skull using the block entity:
			if ((BlockType == E_BLOCK_HEAD) &&
				!a_World.DoWithBlockEntityAt(
					Block,
					[&](cBlockEntity & a_BlockEntity)
					{
						ASSERT(a_BlockEntity.GetBlockType() == E_BLOCK_HEAD);

						return static_cast<cMobHeadEntity &>(a_BlockEntity).GetType() == SKULL_TYPE_WITHER;
					}
				))
			{
				return false;
			}

			// Matched, continue checking:
			PositionsToClear[i] = Block;
		}  // for i - a_Image

		// All image blocks matched, try replace the image with air blocks:
		if (!a_Player.PlaceBlocks({
				{PositionsToClear[0], E_BLOCK_AIR, 0},
				{PositionsToClear[1], E_BLOCK_AIR, 0},
				{PositionsToClear[2], E_BLOCK_AIR, 0},
				{PositionsToClear[3], E_BLOCK_AIR, 0},
				{PositionsToClear[4], E_BLOCK_AIR, 0},
				{PositionsToClear[5], E_BLOCK_AIR, 0},
				{PositionsToClear[6], E_BLOCK_AIR, 0},
				{PositionsToClear[7], E_BLOCK_AIR, 0},
				{PositionsToClear[8], E_BLOCK_AIR, 0},
			}))
		{
			return false;
		}

		// Spawn the wither:
		int BlockX = a_PlacedHeadPos.x + a_OffsetX;
		int BlockZ = a_PlacedHeadPos.z + a_OffsetZ;
		a_World.SpawnMob(
			static_cast<double>(BlockX) + 0.5,
			a_PlacedHeadPos.y - 2,
			static_cast<double>(BlockZ) + 0.5,
			mtWither,
			false
		);
		AwardSpawnWitherAchievement(a_World, {BlockX, a_PlacedHeadPos.y - 2, BlockZ});
		return true;
	}





	/** Awards the achievement to all players close to the specified point. */
	void AwardSpawnWitherAchievement(cWorld & a_World, Vector3i a_BlockPos) const
	{
		Vector3f Pos(a_BlockPos);
		a_World.ForEachPlayer(
			[=](cPlayer & a_Player)
			{
				// If player is close, award achievement:
				double Dist = (a_Player.GetPosition() - Pos).Length();
				if (Dist < 50.0)
				{
					a_Player.AwardAchievement(CustomStatistic::AchSpawnWither);
				}
				return false;
			}
		);
	}





	/** Converts the block face of the placement (which face of the block was clicked to place the head)
	into the block's metadata value. */
	static NIBBLETYPE BlockFaceToBlockMeta(int a_BlockFace)
	{
		switch (a_BlockFace)
		{
			case BLOCK_FACE_TOP: return 0x01;  // On ground (rotation provided in block entity)
			case BLOCK_FACE_XM:  return 0x04;  // west wall, facing east
			case BLOCK_FACE_XP:  return 0x05;  // east wall, facing west
			case BLOCK_FACE_ZM:  return 0x02;  // north wall, facing south
			case BLOCK_FACE_ZP:  return 0x03;  // south wall, facing north
			default:
			{
				ASSERT(!"Unhandled block face");
				return 0;
			}
		}
	}





	virtual bool IsPlaceable(void) const override { return true; }
};