2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include "DispenserEntity.h"
|
|
|
|
#include "../Simulator/FluidSimulator.h"
|
|
|
|
#include "../Chunk.h"
|
|
|
|
|
2014-05-26 02:44:16 -04:00
|
|
|
#include "../World.h"
|
2014-05-26 08:47:04 -04:00
|
|
|
#include "../Entities/ProjectileEntity.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
2014-05-26 08:47:04 -04:00
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
cDispenserEntity::cDispenserEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
|
|
|
|
super(E_BLOCK_DISPENSER, a_BlockX, a_BlockY, a_BlockZ, a_World)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cDispenserEntity::DropSpenseFromSlot(cChunk & a_Chunk, int a_SlotNum)
|
|
|
|
{
|
|
|
|
int DispX = m_RelX;
|
|
|
|
int DispY = m_PosY;
|
|
|
|
int DispZ = m_RelZ;
|
|
|
|
NIBBLETYPE Meta = a_Chunk.GetMeta(m_RelX, m_PosY, m_RelZ);
|
|
|
|
AddDropSpenserDir(DispX, DispY, DispZ, Meta);
|
|
|
|
cChunk * DispChunk = a_Chunk.GetRelNeighborChunkAdjustCoords(DispX, DispZ);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (DispChunk == nullptr)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// Would dispense into / interact with a non-loaded chunk, ignore the tick
|
|
|
|
return;
|
|
|
|
}
|
2014-06-03 13:43:15 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
BLOCKTYPE DispBlock = DispChunk->GetBlock(DispX, DispY, DispZ);
|
2014-06-03 13:43:15 -04:00
|
|
|
int BlockX = (DispX + DispChunk->GetPosX() * cChunkDef::Width);
|
|
|
|
int BlockZ = (DispZ + DispChunk->GetPosZ() * cChunkDef::Width);
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
// Dispense the item:
|
|
|
|
switch (m_Contents.GetSlot(a_SlotNum).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(DispX, DispY, DispZ, E_BLOCK_AIR, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case E_BLOCK_STATIONARY_LAVA:
|
|
|
|
case E_BLOCK_LAVA:
|
|
|
|
{
|
|
|
|
if (ScoopUpLiquid(a_SlotNum, E_ITEM_LAVA_BUCKET))
|
|
|
|
{
|
|
|
|
DispChunk->SetBlock(DispX, DispY, DispZ, E_BLOCK_AIR, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
DropFromSlot(a_Chunk, a_SlotNum);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} // E_ITEM_BUCKET
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
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(DispX, DispY, DispZ, E_BLOCK_WATER, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DropFromSlot(a_Chunk, a_SlotNum);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
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(DispX, DispY, DispZ, E_BLOCK_LAVA, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DropFromSlot(a_Chunk, a_SlotNum);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
case E_ITEM_SPAWN_EGG:
|
|
|
|
{
|
|
|
|
double MobX = 0.5 + (DispX + DispChunk->GetPosX() * cChunkDef::Width);
|
|
|
|
double MobZ = 0.5 + (DispZ + DispChunk->GetPosZ() * cChunkDef::Width);
|
2015-03-21 10:18:17 -04:00
|
|
|
if (m_World->SpawnMob(MobX, DispY, MobZ, static_cast<eMonsterType>(m_Contents.GetSlot(a_SlotNum).m_ItemDamage)) != cEntity::INVALID_ID)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
m_Contents.ChangeSlotCount(a_SlotNum, -1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
case E_BLOCK_TNT:
|
|
|
|
{
|
|
|
|
// Spawn a primed TNT entity, if space allows:
|
|
|
|
if (DispChunk->GetBlock(DispX, DispY, DispZ) == E_BLOCK_AIR)
|
|
|
|
{
|
|
|
|
double TNTX = 0.5 + (DispX + DispChunk->GetPosX() * cChunkDef::Width);
|
|
|
|
double TNTZ = 0.5 + (DispZ + DispChunk->GetPosZ() * cChunkDef::Width);
|
2014-03-08 06:24:33 -05:00
|
|
|
m_World->SpawnPrimedTNT(TNTX, DispY + 0.5, TNTZ, 80, 0); // 80 ticks fuse, no initial velocity
|
2013-07-29 07:13:03 -04:00
|
|
|
m_Contents.ChangeSlotCount(a_SlotNum, -1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case E_ITEM_FLINT_AND_STEEL:
|
|
|
|
{
|
|
|
|
// Spawn fire if the block in front is air.
|
|
|
|
if (DispChunk->GetBlock(DispX, DispY, DispZ) == E_BLOCK_AIR)
|
|
|
|
{
|
|
|
|
DispChunk->SetBlock(DispX, DispY, DispZ, E_BLOCK_FIRE, 0);
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2014-04-19 14:51:52 -04:00
|
|
|
bool ItemBroke = m_Contents.DamageItem(a_SlotNum, 1);
|
|
|
|
|
|
|
|
if (ItemBroke)
|
|
|
|
{
|
2013-07-29 07:13:03 -04:00
|
|
|
m_Contents.ChangeSlotCount(a_SlotNum, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2014-03-14 18:52:51 -04:00
|
|
|
case E_ITEM_FIRE_CHARGE:
|
|
|
|
{
|
2015-03-21 10:18:17 -04:00
|
|
|
if (SpawnProjectileFromDispenser(BlockX, DispY, BlockZ, cProjectileEntity::pkFireCharge, GetShootVector(Meta) * 20) != cEntity::INVALID_ID)
|
|
|
|
{
|
|
|
|
m_Contents.ChangeSlotCount(a_SlotNum, -1);
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case E_ITEM_ARROW:
|
|
|
|
{
|
2015-03-21 10:18:17 -04:00
|
|
|
if (SpawnProjectileFromDispenser(BlockX, DispY, BlockZ, cProjectileEntity::pkArrow, GetShootVector(Meta) * 20 + Vector3d(0, 1, 0)) != cEntity::INVALID_ID)
|
|
|
|
{
|
|
|
|
m_Contents.ChangeSlotCount(a_SlotNum, -1);
|
|
|
|
}
|
2014-05-26 08:47:04 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2014-05-26 08:47:04 -04:00
|
|
|
case E_ITEM_SNOWBALL:
|
|
|
|
{
|
2015-03-21 10:18:17 -04:00
|
|
|
if (SpawnProjectileFromDispenser(BlockX, DispY, BlockZ, cProjectileEntity::pkSnowball, GetShootVector(Meta) * 20 + Vector3d(0, 1, 0)) != cEntity::INVALID_ID)
|
|
|
|
{
|
|
|
|
m_Contents.ChangeSlotCount(a_SlotNum, -1);
|
|
|
|
}
|
2014-05-26 08:47:04 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2014-05-26 08:47:04 -04:00
|
|
|
case E_ITEM_EGG:
|
|
|
|
{
|
2015-03-21 10:18:17 -04:00
|
|
|
if (SpawnProjectileFromDispenser(BlockX, DispY, BlockZ, cProjectileEntity::pkEgg, GetShootVector(Meta) * 20 + Vector3d(0, 1, 0)) != cEntity::INVALID_ID)
|
|
|
|
{
|
|
|
|
m_Contents.ChangeSlotCount(a_SlotNum, -1);
|
|
|
|
}
|
2014-05-26 08:47:04 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2014-05-26 08:47:04 -04:00
|
|
|
case E_ITEM_FIREWORK_ROCKET:
|
|
|
|
{
|
2014-06-03 11:21:38 -04:00
|
|
|
// TODO: Add the fireworks entity
|
2014-03-14 18:52:51 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
DropFromSlot(a_Chunk, a_SlotNum);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // switch (ItemType)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-26 08:47:04 -04:00
|
|
|
|
|
|
|
|
2014-06-11 13:46:24 -04:00
|
|
|
|
2015-03-21 10:18:17 -04:00
|
|
|
UInt32 cDispenserEntity::SpawnProjectileFromDispenser(int a_BlockX, int a_BlockY, int a_BlockZ, cProjectileEntity::eKind a_Kind, const Vector3d & a_ShootVector)
|
2014-06-11 13:46:24 -04:00
|
|
|
{
|
2015-03-21 10:18:17 -04:00
|
|
|
return m_World->CreateProjectile(
|
|
|
|
static_cast<double>(a_BlockX + 0.5),
|
|
|
|
static_cast<double>(a_BlockY + 0.5),
|
|
|
|
static_cast<double>(a_BlockZ + 0.5),
|
|
|
|
a_Kind, nullptr, nullptr, &a_ShootVector
|
|
|
|
);
|
2014-05-26 08:47:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-28 08:34:33 -04:00
|
|
|
|
|
|
|
|
2014-06-11 13:46:24 -04:00
|
|
|
|
|
|
|
Vector3d cDispenserEntity::GetShootVector(NIBBLETYPE a_Meta)
|
|
|
|
{
|
2014-09-25 12:37:36 -04:00
|
|
|
switch (a_Meta & 0x7)
|
2014-06-11 13:46:24 -04:00
|
|
|
{
|
|
|
|
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);
|
2014-06-05 06:26:27 -04:00
|
|
|
case E_META_DROPSPENSER_FACING_ZM: return Vector3d( 0, 0, -1);
|
|
|
|
case E_META_DROPSPENSER_FACING_ZP: return Vector3d( 0, 0, 1);
|
2014-05-26 02:44:16 -04:00
|
|
|
}
|
2014-06-11 13:46:24 -04:00
|
|
|
LOGWARNING("Unhandled dispenser meta: %d", a_Meta);
|
|
|
|
ASSERT(!"Unhandled dispenser facing");
|
2014-06-03 13:39:56 -04:00
|
|
|
return Vector3d(0, 1, 0);
|
2014-05-26 02:44:16 -04:00
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
2014-05-26 08:47:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
bool cDispenserEntity::ScoopUpLiquid(int a_SlotNum, short a_BucketItemType)
|
|
|
|
{
|
|
|
|
cItem LiquidBucket(a_BucketItemType, 1);
|
|
|
|
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;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// 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;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
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;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
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;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// 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;
|
|
|
|
}
|
2014-05-26 02:44:16 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// 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;
|
|
|
|
}
|