2014-02-17 14:14:08 -05:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "ItemHandler.h"
|
|
|
|
#include "../World.h"
|
2014-12-24 01:20:17 -05:00
|
|
|
#include "../BlockEntities/MobHeadEntity.h"
|
2014-02-17 14:14:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-13 12:38:06 -04:00
|
|
|
class cItemMobHeadHandler:
|
2014-02-17 14:14:08 -05:00
|
|
|
public cItemHandler
|
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
using Super = cItemHandler;
|
2015-06-21 13:49:22 -04:00
|
|
|
|
2014-02-17 14:14:08 -05:00
|
|
|
public:
|
2020-04-13 12:38:06 -04:00
|
|
|
|
|
|
|
cItemMobHeadHandler(int a_ItemType):
|
|
|
|
Super(a_ItemType)
|
2014-02-17 14:14:08 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-24 01:20:17 -05:00
|
|
|
virtual bool OnPlayerPlace(
|
2020-04-21 16:19:22 -04:00
|
|
|
cWorld & a_World,
|
|
|
|
cPlayer & a_Player,
|
|
|
|
const cItem & a_EquippedItem,
|
|
|
|
const Vector3i a_ClickedBlockPos,
|
|
|
|
eBlockFace a_ClickedBlockFace,
|
|
|
|
const Vector3i a_CursorPos
|
2014-12-24 01:20:17 -05:00
|
|
|
) override
|
|
|
|
{
|
|
|
|
// Cannot place a head at "no face" and from the bottom:
|
2020-04-21 16:19:22 -04:00
|
|
|
if ((a_ClickedBlockFace == BLOCK_FACE_NONE) || (a_ClickedBlockFace == BLOCK_FACE_BOTTOM))
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-21 16:19:22 -04:00
|
|
|
const auto PlacePos = AddFaceDirection(a_ClickedBlockPos, a_ClickedBlockFace);
|
2014-12-24 01:20:17 -05:00
|
|
|
|
|
|
|
// If the placed head is a wither, try to spawn the wither first:
|
|
|
|
if (a_EquippedItem.m_ItemDamage == E_META_HEAD_WITHER)
|
|
|
|
{
|
2020-04-21 16:19:22 -04:00
|
|
|
if (TrySpawnWitherAround(a_World, a_Player, PlacePos))
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Wither not created, proceed with regular head placement
|
|
|
|
}
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
cItem ItemCopy(a_EquippedItem); // Make a copy in case this is the player's last head item and OnPlayerPlace removes it
|
|
|
|
if (!Super::OnPlayerPlace(a_World, a_Player, a_EquippedItem, a_ClickedBlockPos, a_ClickedBlockFace, a_CursorPos))
|
2015-06-21 13:49:22 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-21 16:19:22 -04:00
|
|
|
RegularHeadPlaced(a_World, a_Player, ItemCopy, PlacePos, a_ClickedBlockFace);
|
2015-06-21 13:49:22 -04:00
|
|
|
return true;
|
2014-12-24 01:20:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-21 13:49:22 -04:00
|
|
|
/** 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(
|
2014-12-24 01:20:17 -05:00
|
|
|
cWorld & a_World, cPlayer & a_Player, const cItem & a_EquippedItem,
|
2020-04-21 16:19:22 -04:00
|
|
|
const Vector3i a_PlacePos, eBlockFace a_ClickedBlockFace
|
2014-12-24 01:20:17 -05:00
|
|
|
)
|
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
auto HeadType = static_cast<eMobHeadType>(a_EquippedItem.m_ItemDamage);
|
2020-04-21 16:19:22 -04:00
|
|
|
auto BlockMeta = static_cast<NIBBLETYPE>(a_ClickedBlockFace);
|
2017-09-02 03:45:06 -04:00
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
// Use a callback to set the properties of the mob head block entity:
|
2020-04-21 16:19:22 -04:00
|
|
|
a_World.DoWithBlockEntityAt(a_PlacePos.x, a_PlacePos.y, a_PlacePos.z, [&](cBlockEntity & a_BlockEntity)
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
if (a_BlockEntity.GetBlockType() != E_BLOCK_HEAD)
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-11 17:20:49 -04:00
|
|
|
auto & MobHeadEntity = static_cast<cMobHeadEntity &>(a_BlockEntity);
|
2014-12-24 01:20:17 -05:00
|
|
|
|
|
|
|
int Rotation = 0;
|
2017-09-11 17:20:49 -04:00
|
|
|
if (BlockMeta == 1)
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
Rotation = FloorC(a_Player.GetYaw() * 16.0f / 360.0f + 0.5f) & 0x0f;
|
2014-12-24 01:20:17 -05:00
|
|
|
}
|
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
MobHeadEntity.SetType(HeadType);
|
|
|
|
MobHeadEntity.SetRotation(static_cast<eMobHeadRotation>(Rotation));
|
2017-09-25 12:17:45 -04:00
|
|
|
MobHeadEntity.GetWorld()->BroadcastBlockEntity(MobHeadEntity.GetPos());
|
2014-12-24 01:20:17 -05:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-11 17:20:49 -04:00
|
|
|
);
|
2014-12-24 01:20:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-24 01:20:17 -05:00
|
|
|
/** 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(
|
|
|
|
cWorld & a_World, cPlayer & a_Player,
|
2020-04-21 16:19:22 -04:00
|
|
|
const Vector3i a_BlockPos
|
2014-12-24 01:20:17 -05:00
|
|
|
)
|
|
|
|
{
|
|
|
|
// No wither can be created at Y < 2 - not enough space for the formula:
|
2017-09-25 12:17:45 -04:00
|
|
|
if (a_BlockPos.y < 2)
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
|
|
|
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},
|
|
|
|
};
|
2017-09-25 12:17:45 -04:00
|
|
|
for (auto & RelCoord : RelCoords)
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
|
|
|
if (TrySpawnWitherAt(
|
|
|
|
a_World, a_Player,
|
2017-09-25 12:17:45 -04:00
|
|
|
a_BlockPos,
|
|
|
|
RelCoord.x, RelCoord.z
|
2014-12-24 01:20:17 -05:00
|
|
|
))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-25 12:17:45 -04:00
|
|
|
} // for i - RelCoords[]
|
2014-12-24 01:20:17 -05:00
|
|
|
|
|
|
|
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,
|
2017-09-25 12:17:45 -04:00
|
|
|
Vector3i a_PlacedHeadPos,
|
2014-12-24 01:20:17 -05:00
|
|
|
int a_OffsetX, int a_OffsetZ
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Image for the wither at the X axis:
|
|
|
|
static const sSetBlock ImageWitherX[] =
|
|
|
|
{
|
2014-12-25 14:41:27 -05:00
|
|
|
{-1, 0, 0, E_BLOCK_HEAD, 0},
|
|
|
|
{ 0, 0, 0, E_BLOCK_HEAD, 0},
|
|
|
|
{ 1, 0, 0, E_BLOCK_HEAD, 0},
|
2014-12-24 01:20:17 -05:00
|
|
|
{-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[] =
|
|
|
|
{
|
2014-12-25 14:41:27 -05:00
|
|
|
{ 0, 0, -1, E_BLOCK_HEAD, 0},
|
|
|
|
{ 0, 0, 0, E_BLOCK_HEAD, 0},
|
|
|
|
{ 0, 0, 1, E_BLOCK_HEAD, 0},
|
2014-12-24 01:20:17 -05:00
|
|
|
{ 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, ARRAYCOUNT(ImageWitherX),
|
2017-09-25 12:17:45 -04:00
|
|
|
a_PlacedHeadPos,
|
2014-12-24 01:20:17 -05:00
|
|
|
a_OffsetX, a_OffsetZ
|
|
|
|
) ||
|
|
|
|
TrySpawnWitherFromImage(
|
|
|
|
a_World, a_Player, ImageWitherZ, ARRAYCOUNT(ImageWitherZ),
|
2017-09-25 12:17:45 -04:00
|
|
|
a_PlacedHeadPos,
|
2014-12-24 01:20:17 -05:00
|
|
|
a_OffsetX, a_OffsetZ
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-24 01:20:17 -05:00
|
|
|
/** 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, size_t a_ImageCount,
|
2017-09-25 12:17:45 -04:00
|
|
|
Vector3i a_PlacedHeadPos,
|
2014-12-24 01:20:17 -05:00
|
|
|
int a_OffsetX, int a_OffsetZ
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Check each block individually; simultaneously build the SetBlockVector for clearing the blocks:
|
|
|
|
sSetBlockVector AirBlocks;
|
|
|
|
AirBlocks.reserve(a_ImageCount);
|
|
|
|
for (size_t i = 0; i < a_ImageCount; i++)
|
|
|
|
{
|
|
|
|
// Get the absolute coords of the image:
|
2017-09-25 12:17:45 -04:00
|
|
|
int BlockX = a_PlacedHeadPos.x + a_OffsetX + a_Image[i].GetX();
|
|
|
|
int BlockY = a_PlacedHeadPos.y + a_Image[i].GetY();
|
|
|
|
int BlockZ = a_PlacedHeadPos.z + a_OffsetZ + a_Image[i].GetZ();
|
2014-12-24 01:20:17 -05:00
|
|
|
|
|
|
|
// If the query is for the placed head, short-circuit-evaluate it:
|
2017-09-25 12:17:45 -04:00
|
|
|
if ((BlockX == a_PlacedHeadPos.x) && (BlockY == a_PlacedHeadPos.y) && (BlockZ == a_PlacedHeadPos.z))
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
2014-12-25 14:41:27 -05:00
|
|
|
if (a_Image[i].m_BlockType != E_BLOCK_HEAD)
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
|
|
|
return false; // Didn't match
|
|
|
|
}
|
|
|
|
continue; // Matched, continue checking the rest of the image
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the world block:
|
|
|
|
BLOCKTYPE BlockType;
|
|
|
|
NIBBLETYPE BlockMeta;
|
|
|
|
if (!a_World.GetBlockTypeMeta(BlockX, BlockY, BlockZ, BlockType, BlockMeta))
|
|
|
|
{
|
|
|
|
// Cannot query block, assume unloaded chunk, fail to spawn the wither
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare the world block:
|
2014-12-25 14:41:27 -05:00
|
|
|
if (BlockType != a_Image[i].m_BlockType)
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
2014-12-25 14:41:27 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is a mob head, check the correct head type using the block entity:
|
|
|
|
if (BlockType == E_BLOCK_HEAD)
|
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
bool IsWitherHead = false;
|
|
|
|
a_World.DoWithBlockEntityAt(BlockX, BlockY, BlockZ, [&](cBlockEntity & a_Entity)
|
2014-12-25 14:41:27 -05:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
ASSERT(a_Entity.GetBlockType() == E_BLOCK_HEAD);
|
|
|
|
auto & MobHead = static_cast<cMobHeadEntity &>(a_Entity);
|
|
|
|
IsWitherHead = (MobHead.GetType() == SKULL_TYPE_WITHER);
|
2014-12-25 14:41:27 -05:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-11 17:20:49 -04:00
|
|
|
);
|
|
|
|
if (!IsWitherHead)
|
2014-12-25 14:41:27 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-24 01:20:17 -05:00
|
|
|
}
|
|
|
|
// Matched, continue checking
|
2014-12-25 14:41:27 -05:00
|
|
|
AirBlocks.emplace_back(BlockX, BlockY, BlockZ, E_BLOCK_AIR, 0);
|
2014-12-24 01:20:17 -05:00
|
|
|
} // for i - a_Image
|
|
|
|
|
2014-12-25 14:41:27 -05:00
|
|
|
// All image blocks matched, try replace the image with air blocks:
|
2014-12-24 01:20:17 -05:00
|
|
|
if (!a_Player.PlaceBlocks(AirBlocks))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spawn the wither:
|
2017-09-25 12:17:45 -04:00
|
|
|
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});
|
2014-12-24 01:20:17 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-24 01:20:17 -05:00
|
|
|
/** Awards the achievement to all players close to the specified point. */
|
2017-09-25 12:17:45 -04:00
|
|
|
void AwardSpawnWitherAchievement(cWorld & a_World, Vector3i a_BlockPos)
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
2017-09-25 12:17:45 -04:00
|
|
|
Vector3f Pos(a_BlockPos);
|
2017-09-11 17:20:49 -04:00
|
|
|
a_World.ForEachPlayer([=](cPlayer & a_Player)
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
|
|
|
// If player is close, award achievement:
|
2017-09-11 17:20:49 -04:00
|
|
|
double Dist = (a_Player.GetPosition() - Pos).Length();
|
2014-12-24 01:20:17 -05:00
|
|
|
if (Dist < 50.0)
|
|
|
|
{
|
2020-08-12 04:54:36 -04:00
|
|
|
a_Player.AwardAchievement(Statistic::AchSpawnWither);
|
2014-12-24 01:20:17 -05:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-11 17:20:49 -04:00
|
|
|
);
|
2014-12-24 01:20:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-25 11:15:19 -05:00
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-17 14:14:08 -05:00
|
|
|
virtual bool IsPlaceable(void) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-17 14:14:08 -05:00
|
|
|
virtual bool GetPlacementBlockTypeMeta(
|
|
|
|
cWorld * a_World, cPlayer * a_Player,
|
2020-04-21 16:19:22 -04:00
|
|
|
const Vector3i a_PlacedBlockPos,
|
|
|
|
eBlockFace a_ClickedBlockFace,
|
|
|
|
const Vector3i a_CursorPos,
|
2014-02-17 14:14:08 -05:00
|
|
|
BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
|
|
|
|
) override
|
|
|
|
{
|
|
|
|
a_BlockType = E_BLOCK_HEAD;
|
2020-04-21 16:19:22 -04:00
|
|
|
a_BlockMeta = BlockFaceToBlockMeta(a_ClickedBlockFace);
|
2014-02-17 14:14:08 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|