2012-09-23 18:09:57 -04:00
|
|
|
|
2012-07-15 16:36:34 -04:00
|
|
|
#pragma once
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "ItemHandler.h"
|
|
|
|
#include "../World.h"
|
2012-10-18 15:41:29 -04:00
|
|
|
#include "../Simulator/FluidSimulator.h"
|
|
|
|
#include "../Blocks/BlockHandler.h"
|
2012-07-15 16:36:34 -04:00
|
|
|
|
2012-10-18 15:41:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cItemBucketHandler :
|
|
|
|
public cItemHandler
|
2012-07-15 16:36:34 -04:00
|
|
|
{
|
|
|
|
public:
|
2012-10-18 15:41:29 -04:00
|
|
|
cItemBucketHandler(int a_ItemType) :
|
|
|
|
cItemHandler(a_ItemType)
|
2012-07-15 16:36:34 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-19 14:22:37 -04:00
|
|
|
virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Dir) override
|
2012-10-18 15:41:29 -04:00
|
|
|
{
|
|
|
|
switch (m_ItemType)
|
|
|
|
{
|
|
|
|
case E_ITEM_BUCKET: return ScoopUpFluid(a_World, a_Player, a_Item, a_BlockX, a_BlockY, a_BlockZ, a_Dir);
|
|
|
|
case E_ITEM_LAVA_BUCKET: return PlaceFluid (a_World, a_Player, a_Item, a_BlockX, a_BlockY, a_BlockZ, a_Dir, E_BLOCK_LAVA);
|
|
|
|
case E_ITEM_WATER_BUCKET: return PlaceFluid (a_World, a_Player, a_Item, a_BlockX, a_BlockY, a_BlockZ, a_Dir, E_BLOCK_WATER);
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled ItemType");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-19 14:22:37 -04:00
|
|
|
bool ScoopUpFluid(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace)
|
2012-07-15 16:36:34 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
if (a_BlockFace < 0)
|
2012-07-15 16:36:34 -04:00
|
|
|
{
|
2012-10-18 15:41:29 -04:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-11 23:46:01 -05:00
|
|
|
AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
2012-10-18 15:41:29 -04:00
|
|
|
BLOCKTYPE ClickedBlock;
|
|
|
|
NIBBLETYPE ClickedMeta;
|
|
|
|
a_World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, ClickedBlock, ClickedMeta);
|
|
|
|
LOGD("Bucket Clicked BlockType %d, meta %d", ClickedBlock, ClickedMeta);
|
|
|
|
if (ClickedMeta != 0)
|
|
|
|
{
|
|
|
|
// Not a source block
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-19 14:22:37 -04:00
|
|
|
if (a_Player->GetGameMode() == gmCreative)
|
2012-10-18 15:41:29 -04:00
|
|
|
{
|
|
|
|
// In creative mode don't modify the inventory, just remove the fluid:
|
|
|
|
a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENUM_ITEM_ID NewItem = E_ITEM_EMPTY;
|
|
|
|
switch (ClickedBlock)
|
|
|
|
{
|
|
|
|
case E_BLOCK_WATER:
|
|
|
|
case E_BLOCK_STATIONARY_WATER:
|
2012-07-15 16:36:34 -04:00
|
|
|
{
|
2012-10-18 15:41:29 -04:00
|
|
|
NewItem = E_ITEM_WATER_BUCKET;
|
2012-08-17 06:54:13 -04:00
|
|
|
break;
|
2012-07-15 16:36:34 -04:00
|
|
|
}
|
2012-10-18 15:41:29 -04:00
|
|
|
case E_BLOCK_LAVA:
|
|
|
|
case E_BLOCK_STATIONARY_LAVA:
|
2012-07-15 16:36:34 -04:00
|
|
|
{
|
2012-10-18 15:41:29 -04:00
|
|
|
NewItem = E_ITEM_LAVA_BUCKET;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the bucket from the inventory
|
2013-05-24 03:30:39 -04:00
|
|
|
if (!a_Player->GetInventory().RemoveOneEquippedItem())
|
2012-10-18 15:41:29 -04:00
|
|
|
{
|
|
|
|
LOG("Clicked with an empty bucket, but cannot remove one from the inventory? WTF?");
|
|
|
|
ASSERT(!"Inventory bucket mismatch");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give new bucket, filled with fluid:
|
2013-05-24 03:30:39 -04:00
|
|
|
cItem Item(NewItem, 1);
|
2012-10-18 15:41:29 -04:00
|
|
|
a_Player->GetInventory().AddItem(Item);
|
|
|
|
|
|
|
|
// Remove water / lava block
|
|
|
|
a_Player->GetWorld()->SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0);
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-14 13:03:18 -04:00
|
|
|
|
2012-10-18 15:41:29 -04:00
|
|
|
|
2013-05-19 14:22:37 -04:00
|
|
|
bool PlaceFluid(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_FluidBlock)
|
2012-10-18 15:41:29 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
if (a_BlockFace < 0)
|
2012-10-18 15:41:29 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLOCKTYPE CurrentBlock = a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
bool CanWashAway = cFluidSimulator::CanWashAway(CurrentBlock);
|
|
|
|
if (!CanWashAway)
|
|
|
|
{
|
|
|
|
// The block pointed at cannot be washed away, so put fluid on top of it / on its sides
|
2013-01-11 23:46:01 -05:00
|
|
|
AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
2012-10-18 15:41:29 -04:00
|
|
|
CurrentBlock = a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!CanWashAway &&
|
|
|
|
(CurrentBlock != E_BLOCK_AIR) &&
|
|
|
|
(CurrentBlock != E_BLOCK_WATER) &&
|
|
|
|
(CurrentBlock != E_BLOCK_STATIONARY_WATER) &&
|
|
|
|
(CurrentBlock != E_BLOCK_LAVA) &&
|
|
|
|
(CurrentBlock != E_BLOCK_STATIONARY_LAVA)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Cannot place water here
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-19 14:22:37 -04:00
|
|
|
if (a_Player->GetGameMode() != gmCreative)
|
2012-10-18 15:41:29 -04:00
|
|
|
{
|
|
|
|
// Remove fluid bucket, add empty bucket:
|
2013-05-24 03:30:39 -04:00
|
|
|
if (!a_Player->GetInventory().RemoveOneEquippedItem())
|
2012-10-18 15:41:29 -04:00
|
|
|
{
|
|
|
|
LOG("Clicked with a full bucket, but cannot remove one from the inventory? WTF?");
|
|
|
|
ASSERT(!"Inventory bucket mismatch");
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-24 03:30:39 -04:00
|
|
|
cItem Item(E_ITEM_BUCKET, 1);
|
2012-10-18 15:41:29 -04:00
|
|
|
if (!a_Player->GetInventory().AddItem(Item))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wash away anything that was there prior to placing:
|
|
|
|
if (CanWashAway)
|
|
|
|
{
|
|
|
|
cBlockHandler * Handler = BlockHandler(CurrentBlock);
|
|
|
|
if (Handler->DoesDropOnUnsuitable())
|
|
|
|
{
|
2013-01-26 21:00:33 -05:00
|
|
|
Handler->DropBlock(a_World, a_Player, a_BlockX, a_BlockY, a_BlockZ);
|
2012-07-15 16:36:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 15:41:29 -04:00
|
|
|
a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, a_FluidBlock, 0);
|
|
|
|
|
|
|
|
return true;
|
2012-07-15 16:36:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|