Skip to content

Commit

Permalink
Vector3 in Handlers (#4680)
Browse files Browse the repository at this point in the history
Refactored all cBlockHandler and cItemHandler descendants to use Vector3.
  • Loading branch information
madmaxoft committed Apr 21, 2020
1 parent 246acb1 commit 487f9a2
Show file tree
Hide file tree
Showing 111 changed files with 2,664 additions and 1,487 deletions.
33 changes: 27 additions & 6 deletions src/Blocks/BlockAnvil.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,34 @@ class cBlockAnvilHandler:



virtual bool OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
virtual bool OnUse(
cChunkInterface & a_ChunkInterface,
cWorldInterface & a_WorldInterface,
cPlayer & a_Player,
const Vector3i a_BlockPos,
eBlockFace a_BlockFace,
const Vector3i a_CursorPos
) override
{
cWindow * Window = new cAnvilWindow(a_BlockX, a_BlockY, a_BlockZ);
cWindow * Window = new cAnvilWindow(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z);
a_Player.OpenWindow(*Window);
return true;
}





virtual bool GetPlacementBlockTypeMeta(
cChunkInterface & a_ChunkInterface, cPlayer & a_Player,
int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
int a_CursorX, int a_CursorY, int a_CursorZ,
cChunkInterface & a_ChunkInterface,
cPlayer & a_Player,
const Vector3i a_PlacedBlockPos,
eBlockFace a_ClickedBlockFace,
const Vector3i a_CursorPos,
BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
) override
{
if (!Super::GetPlacementBlockTypeMeta(a_ChunkInterface, a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta))
if (!Super::GetPlacementBlockTypeMeta(a_ChunkInterface, a_Player, a_PlacedBlockPos, a_ClickedBlockFace, a_CursorPos, a_BlockType, a_BlockMeta))
{
return false;
}
Expand All @@ -58,11 +71,19 @@ class cBlockAnvilHandler:
return true;
}





virtual bool IsUseable() override
{
return true;
}





virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
{
UNUSED(a_Meta);
Expand Down
150 changes: 82 additions & 68 deletions src/Blocks/BlockBed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@



void cBlockBedHandler::OnBroken(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta)
void cBlockBedHandler::OnBroken(
cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
const Vector3i a_BlockPos,
BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta
)
{
auto Direction = MetaDataToDirection(a_OldBlockMeta & 0x03);
if ((a_OldBlockMeta & 0x08) != 0)
Expand Down Expand Up @@ -50,87 +54,97 @@ void cBlockBedHandler::OnBroken(cChunkInterface & a_ChunkInterface, cWorldInterf



bool cBlockBedHandler::OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
bool cBlockBedHandler::OnUse(
cChunkInterface & a_ChunkInterface,
cWorldInterface & a_WorldInterface,
cPlayer & a_Player,
const Vector3i a_BlockPos,
eBlockFace a_BlockFace,
const Vector3i a_CursorPos
)
{
Vector3i Coords(a_BlockX, a_BlockY, a_BlockZ);
// Sleeping in bed only allowed in Overworld, beds explode elsewhere:
if (a_WorldInterface.GetDimension() != dimOverworld)
{
a_WorldInterface.DoExplosionAt(5, a_BlockX, a_BlockY, a_BlockZ, true, esBed, &Coords);
auto PosCopy = a_BlockPos;
a_WorldInterface.DoExplosionAt(5, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, true, esBed, &PosCopy);
return true;
}
else if (!((a_WorldInterface.GetTimeOfDay() > 12541) && (a_WorldInterface.GetTimeOfDay() < 23458))) // Source: https://minecraft.gamepedia.com/Bed#Sleeping

// Sleeping is allowed only during night:
// TODO: Also during thunderstorms
if (!((a_WorldInterface.GetTimeOfDay() > 12541) && (a_WorldInterface.GetTimeOfDay() < 23458))) // Source: https://minecraft.gamepedia.com/Bed#Sleeping
{
a_Player.SendMessageFailure("You can only sleep at night");
return true;
}

// Check if the bed is occupied:
auto Meta = a_ChunkInterface.GetBlockMeta(a_BlockPos);
if ((Meta & 0x04) == 0x04)
{
a_Player.SendMessageFailure("This bed is occupied");
return true;
}

// Cannot sleep if there are hostile mobs nearby:
auto FindMobs = [](cEntity & a_Entity)
{
return (
(a_Entity.GetEntityType() == cEntity::etMonster) &&
(static_cast<cMonster&>(a_Entity).GetMobFamily() == cMonster::mfHostile)
);
};
if (!a_Player.GetWorld()->ForEachEntityInBox(cBoundingBox(a_Player.GetPosition() - Vector3i(0, 5, 0), 8, 10), FindMobs))
{
a_Player.SendMessageFailure("You may not rest now, there are monsters nearby");
return true;
}

// Broadcast the "Use bed" for the pillow block:
if ((Meta & 0x8) == 0x8)
{
// Is pillow
a_WorldInterface.GetBroadcastManager().BroadcastUseBed(a_Player, a_BlockPos);
}
else
{
NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(Coords);
if ((Meta & 0x4) == 0x4)
// Is foot end
VERIFY((Meta & 0x04) != 0x04); // Occupied flag should never be set, else our compilator (intended) is broken

auto PillowPos = a_BlockPos + MetaDataToDirection(Meta & 0x03);
if (a_ChunkInterface.GetBlock(PillowPos) == E_BLOCK_BED) // Must always use pillow location for sleeping
{
a_Player.SendMessageFailure("This bed is occupied");
a_WorldInterface.GetBroadcastManager().BroadcastUseBed(a_Player, PillowPos);
}
else
{
auto FindMobs = [](cEntity & a_Entity)
{
return (
(a_Entity.GetEntityType() == cEntity::etMonster) &&
(static_cast<cMonster&>(a_Entity).GetMobFamily() == cMonster::mfHostile)
);
};
}

if (!a_Player.GetWorld()->ForEachEntityInBox(cBoundingBox(a_Player.GetPosition() - Vector3i(0, 5, 0), 8, 10), FindMobs))
{
a_Player.SendMessageFailure("You may not rest now, there are monsters nearby");
}
else
// Occupy the bed:
a_Player.SetBedPos(a_BlockPos);
SetBedOccupationState(a_ChunkInterface, a_Player.GetLastBedPos(), true);
a_Player.SetIsInBed(true);
a_Player.SendMessageSuccess("Home position set successfully");

// Fast-forward the time if all players in the world are in their beds:
auto TimeFastForwardTester = [](cPlayer & a_OtherPlayer)
{
if (!a_OtherPlayer.IsInBed())
{
return true;
}
return false;
};
if (a_WorldInterface.ForEachPlayer(TimeFastForwardTester))
{
a_WorldInterface.ForEachPlayer([&](cPlayer & a_OtherPlayer)
{
Vector3i PillowDirection(0, 0, 0);

if ((Meta & 0x8) == 0x8)
{
// Is pillow
a_WorldInterface.GetBroadcastManager().BroadcastUseBed(a_Player, { a_BlockX, a_BlockY, a_BlockZ });
}
else
{
// Is foot end
VERIFY((Meta & 0x4) != 0x4); // Occupied flag should never be set, else our compilator (intended) is broken

PillowDirection = MetaDataToDirection(Meta & 0x3);
if (a_ChunkInterface.GetBlock(Coords + PillowDirection) == E_BLOCK_BED) // Must always use pillow location for sleeping
{
a_WorldInterface.GetBroadcastManager().BroadcastUseBed(a_Player, Vector3i{a_BlockX, a_BlockY, a_BlockZ} + PillowDirection);
}
}

a_Player.SetBedPos(Coords);
SetBedOccupationState(a_ChunkInterface, a_Player.GetLastBedPos(), true);
a_Player.SetIsInBed(true);
a_Player.SendMessageSuccess("Home position set successfully");

auto TimeFastForwardTester = [](cPlayer & a_OtherPlayer)
{
if (!a_OtherPlayer.IsInBed())
{
return true;
}
return false;
};

if (a_WorldInterface.ForEachPlayer(TimeFastForwardTester))
{
a_WorldInterface.ForEachPlayer([&](cPlayer & a_OtherPlayer)
{
cBlockBedHandler::SetBedOccupationState(a_ChunkInterface, a_OtherPlayer.GetLastBedPos(), false);
a_OtherPlayer.SetIsInBed(false);
return false;
}
);
a_WorldInterface.SetTimeOfDay(0);
a_ChunkInterface.SetBlockMeta({a_BlockX, a_BlockY, a_BlockZ}, Meta & 0x0b); // Clear the "occupied" bit of the bed's block
}
cBlockBedHandler::SetBedOccupationState(a_ChunkInterface, a_OtherPlayer.GetLastBedPos(), false);
a_OtherPlayer.SetIsInBed(false);
return false;
}
}
);
a_WorldInterface.SetTimeOfDay(0);
a_ChunkInterface.SetBlockMeta(a_BlockPos, Meta & 0x0b); // Clear the "occupied" bit of the bed's block
}
return true;
}
Expand Down
28 changes: 24 additions & 4 deletions src/Blocks/BlockBed.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,30 @@ class cBlockBedHandler :


// Overrides:
virtual void OnBroken(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta) override;
virtual bool OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override;
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override;
virtual void OnPlacedByPlayer(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player, const sSetBlock & a_BlockChange) override;
virtual void OnBroken(
cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
const Vector3i a_BlockPos,
BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta
) override;

virtual bool OnUse(
cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player,
const Vector3i a_ClickedBlockPos,
eBlockFace a_ClickedBlockFace,
const Vector3i a_CursorPos
) override;

virtual cItems ConvertToPickups(
NIBBLETYPE a_BlockMeta,
cBlockEntity * a_BlockEntity,
const cEntity * a_Digger,
const cItem * a_Tool
) override;

virtual void OnPlacedByPlayer(
cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player,
const sSetBlock & a_BlockChange
) override;



Expand Down
10 changes: 5 additions & 5 deletions src/Blocks/BlockBigFlower.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@



class cBlockBigFlowerHandler :
class cBlockBigFlowerHandler:
public cBlockHandler
{
using Super = cBlockHandler;
Expand Down Expand Up @@ -95,15 +95,15 @@ class cBlockBigFlowerHandler :



virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, const Vector3i a_RelPos, const cChunk & a_Chunk) override
{
if (a_RelY <= 0)
if (a_RelPos.y <= 0)
{
return false;
}
BLOCKTYPE BlockType;
NIBBLETYPE BlockMeta;
a_Chunk.GetBlockTypeMeta(a_RelX, a_RelY - 1, a_RelZ, BlockType, BlockMeta);
a_Chunk.GetBlockTypeMeta(a_RelPos.addedY(-1), BlockType, BlockMeta);

return IsBlockTypeOfDirt(BlockType) || ((BlockType == E_BLOCK_BIG_FLOWER) && !IsMetaTopPart(BlockMeta));
}
Expand All @@ -112,7 +112,7 @@ class cBlockBigFlowerHandler :



virtual void OnBroken(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta) override
virtual void OnBroken(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, const Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta) override
{
if ((a_OldBlockMeta & 0x8) != 0)
{
Expand Down
Loading

0 comments on commit 487f9a2

Please sign in to comment.