2012-12-19 16:19:36 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include "DispenserEntity.h"
|
|
|
|
#include "BlockID.h"
|
|
|
|
#include "Item.h"
|
|
|
|
#include "UI/Window.h"
|
|
|
|
#include "Player.h"
|
|
|
|
#include "World.h"
|
|
|
|
#include "ClientHandle.h"
|
|
|
|
#include "Server.h"
|
|
|
|
#include "Pickup.h"
|
|
|
|
#include "Root.h"
|
2012-12-26 12:16:33 -05:00
|
|
|
#include "Simulator/FluidSimulator.h"
|
2012-12-19 16:19:36 -05:00
|
|
|
#include <json/json.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-26 12:16:33 -05:00
|
|
|
#define AddDispenserDir(x, y, z, dir) \
|
|
|
|
switch (dir) \
|
|
|
|
{ \
|
|
|
|
case 2: (z) --; break; \
|
|
|
|
case 3: (z) ++; break; \
|
|
|
|
case 4: (x) --; break; \
|
|
|
|
case 5: (x) ++; break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-10 17:40:30 -04:00
|
|
|
cDispenserEntity::cDispenserEntity(int a_X, int a_Y, int a_Z, cWorld * a_World) :
|
|
|
|
cBlockEntity(E_BLOCK_DISPENSER, a_X, a_Y, a_Z, a_World),
|
|
|
|
m_Contents(3, 3),
|
|
|
|
m_ShouldDispense(false)
|
2012-12-19 16:19:36 -05:00
|
|
|
{
|
|
|
|
SetBlockEntity(this); // cBlockEntityWindowOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cDispenserEntity::~cDispenserEntity()
|
|
|
|
{
|
|
|
|
// Tell window its owner is destroyed
|
2013-04-10 17:40:30 -04:00
|
|
|
cWindow * Window = GetWindow();
|
|
|
|
if (Window != NULL)
|
2012-12-19 16:19:36 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
Window->OwnerDestroyed();
|
2012-12-19 16:19:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-10 17:40:30 -04:00
|
|
|
void cDispenserEntity::Destroy(void)
|
2012-12-19 16:19:36 -05:00
|
|
|
{
|
|
|
|
// Drop items
|
|
|
|
cItems Pickups;
|
2013-04-10 17:40:30 -04:00
|
|
|
m_Contents.CopyToItems(Pickups);
|
|
|
|
m_Contents.Clear();
|
2012-12-19 16:19:36 -05:00
|
|
|
m_World->SpawnItemPickups(Pickups, m_PosX, m_PosY, m_PosZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-10 17:40:30 -04:00
|
|
|
void cDispenserEntity::Dispense(void)
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
|
|
|
int Disp_X = m_PosX;
|
|
|
|
int Disp_Y = m_PosY;
|
|
|
|
int Disp_Z = m_PosZ;
|
2013-04-10 17:40:30 -04:00
|
|
|
NIBBLETYPE Meta = m_World->GetBlockMeta(m_PosX, m_PosY, m_PosZ);
|
|
|
|
AddDispenserDir(Disp_X, Disp_Y, Disp_Z, Meta);
|
|
|
|
int OccupiedSlots[9];
|
|
|
|
int SlotsCnt = 0;
|
|
|
|
for (int i = m_Contents.GetNumSlots() - 1; i >= 0; i--)
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
if (!m_Contents.GetSlot(i).IsEmpty())
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
|
|
|
OccupiedSlots[SlotsCnt] = i;
|
|
|
|
SlotsCnt++;
|
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
} // for i - m_Contents[]
|
|
|
|
|
|
|
|
if (SlotsCnt == 0)
|
|
|
|
{
|
|
|
|
// Nothing in the dispenser, play the click sound
|
|
|
|
m_World->BroadcastSoundEffect("random.click", m_PosX * 8, m_PosY * 8, m_PosZ * 8, 1.0f, 1.2f);
|
|
|
|
return;
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
|
|
|
|
// Pick an item to dispense:
|
|
|
|
MTRand r1;
|
|
|
|
int RandomSlot = r1.randInt(SlotsCnt);
|
|
|
|
cItem & Drop = m_Contents.GetSlot(OccupiedSlots[RandomSlot]);
|
|
|
|
|
|
|
|
// Dispense the item:
|
|
|
|
switch (Drop.m_ItemType)
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
case E_ITEM_BUCKET:
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
BLOCKTYPE DispBlock = m_World->GetBlock(Disp_X, Disp_Y, Disp_Z);
|
|
|
|
if (DispBlock == E_BLOCK_STATIONARY_WATER)
|
|
|
|
{
|
|
|
|
m_World->SetBlock(Disp_X, Disp_Y, Disp_Z, E_BLOCK_AIR, 0);
|
|
|
|
Drop.m_ItemType = E_ITEM_WATER_BUCKET; // TODO: Duplication glitch - bucket stacking allows you to duplicate water
|
|
|
|
}
|
|
|
|
else if (DispBlock == E_BLOCK_STATIONARY_LAVA)
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
m_World->SetBlock(Disp_X, Disp_Y, Disp_Z, E_BLOCK_AIR, 0);
|
|
|
|
Drop.m_ItemType = E_ITEM_LAVA_BUCKET; // TODO: Duplication glitch - bucket stacking allows you to duplicate lava
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cItems Pickups;
|
|
|
|
Pickups.push_back(Drop.CopyOne());
|
|
|
|
m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z);
|
|
|
|
Drop.m_ItemCount--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case E_ITEM_WATER_BUCKET:
|
|
|
|
{
|
|
|
|
BLOCKTYPE DispBlock = m_World->GetBlock(Disp_X, Disp_Y, Disp_Z);
|
|
|
|
if ((DispBlock == E_BLOCK_AIR) || IsBlockLiquid(DispBlock) || cFluidSimulator::CanWashAway(DispBlock))
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
m_World->SetBlock(Disp_X, Disp_Y, Disp_Z, E_BLOCK_STATIONARY_WATER, 0);
|
|
|
|
Drop.m_ItemType = E_ITEM_BUCKET;
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
else
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
cItems Pickups;
|
|
|
|
Pickups.push_back(Drop.CopyOne());
|
|
|
|
m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z);
|
|
|
|
Drop.m_ItemCount--;
|
2012-12-26 13:15:11 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case E_ITEM_LAVA_BUCKET:
|
|
|
|
{
|
|
|
|
BLOCKTYPE DispBlock = m_World->GetBlock( Disp_X, Disp_Y, Disp_Z );
|
|
|
|
if ((DispBlock == E_BLOCK_AIR) || IsBlockLiquid(DispBlock) || cFluidSimulator::CanWashAway(DispBlock))
|
2012-12-26 13:15:11 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
m_World->SetBlock(Disp_X, Disp_Y, Disp_Z, E_BLOCK_STATIONARY_LAVA, 0);
|
|
|
|
Drop.m_ItemType = E_ITEM_BUCKET;
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
else
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
|
|
|
cItems Pickups;
|
2013-04-10 17:40:30 -04:00
|
|
|
Pickups.push_back(Drop.CopyOne());
|
2012-12-26 12:16:33 -05:00
|
|
|
m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z);
|
2013-04-10 17:40:30 -04:00
|
|
|
Drop.m_ItemCount--;
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
break;
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
|
|
|
|
case E_ITEM_SPAWN_EGG:
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
if (m_World->SpawnMob(Disp_X + 0.5, Disp_Y, Disp_Z + 0.5, Drop.m_ItemDamage) >= 0)
|
|
|
|
{
|
|
|
|
Drop.m_ItemCount--;
|
|
|
|
}
|
|
|
|
break;
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
|
|
|
|
default:
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
cItems Pickups;
|
|
|
|
Pickups.push_back(Drop.CopyOne());
|
|
|
|
m_World->SpawnItemPickups(Pickups, Disp_X, Disp_Y, Disp_Z);
|
|
|
|
Drop.m_ItemCount--;
|
|
|
|
break;
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
} // switch (ItemType)
|
|
|
|
|
|
|
|
char SmokeDir = 0;
|
|
|
|
switch (Meta)
|
|
|
|
{
|
|
|
|
case 2: SmokeDir = 1; break;
|
|
|
|
case 3: SmokeDir = 7; break;
|
|
|
|
case 4: SmokeDir = 3; break;
|
|
|
|
case 5: SmokeDir = 5; break;
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
2013-04-10 17:40:30 -04:00
|
|
|
m_World->BroadcastSoundParticleEffect(2000, m_PosX * 8, m_PosY * 8, m_PosZ * 8, SmokeDir);
|
|
|
|
m_World->BroadcastSoundEffect("random.click", m_PosX * 8, m_PosY * 8, m_PosZ * 8, 1.0f, 1.0f);
|
|
|
|
cWindow * Window = GetWindow();
|
|
|
|
if (Window != NULL)
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
Window->BroadcastWholeWindow();
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-19 16:19:36 -05:00
|
|
|
void cDispenserEntity::UsedBy(cPlayer * a_Player)
|
|
|
|
{
|
|
|
|
if (GetWindow() == NULL)
|
|
|
|
{
|
|
|
|
OpenWindow(new cDispenserWindow(m_PosX, m_PosY, m_PosZ, this));
|
|
|
|
}
|
|
|
|
if (GetWindow() != NULL)
|
|
|
|
{
|
|
|
|
if (a_Player->GetWindow() != GetWindow())
|
|
|
|
{
|
|
|
|
a_Player->OpenWindow(GetWindow());
|
|
|
|
GetWindow()->SendWholeWindow(*a_Player->GetClientHandle());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-10 17:40:30 -04:00
|
|
|
void cDispenserEntity::Activate(void)
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
m_ShouldDispense = true;
|
2012-12-26 12:16:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-10 17:40:30 -04:00
|
|
|
bool cDispenserEntity::Tick(float a_Dt)
|
2012-12-19 16:19:36 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
if (m_ShouldDispense)
|
2012-12-26 12:16:33 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
m_ShouldDispense = false;
|
2012-12-26 12:16:33 -05:00
|
|
|
Dispense();
|
|
|
|
}
|
|
|
|
return false;
|
2012-12-19 16:19:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-10 17:40:30 -04:00
|
|
|
bool cDispenserEntity::LoadFromJson(const Json::Value & a_Value)
|
2012-12-19 16:19:36 -05:00
|
|
|
{
|
|
|
|
m_PosX = a_Value.get("x", 0).asInt();
|
|
|
|
m_PosY = a_Value.get("y", 0).asInt();
|
|
|
|
m_PosZ = a_Value.get("z", 0).asInt();
|
|
|
|
|
|
|
|
Json::Value AllSlots = a_Value.get("Slots", 0);
|
|
|
|
int SlotIdx = 0;
|
2013-04-10 17:40:30 -04:00
|
|
|
for (Json::Value::iterator itr = AllSlots.begin(); itr != AllSlots.end(); ++itr)
|
2012-12-19 16:19:36 -05:00
|
|
|
{
|
2013-04-10 17:40:30 -04:00
|
|
|
m_Contents.GetSlot(SlotIdx).FromJson(*itr);
|
2012-12-19 16:19:36 -05:00
|
|
|
SlotIdx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-10 17:40:30 -04:00
|
|
|
void cDispenserEntity::SaveToJson(Json::Value & a_Value)
|
2012-12-19 16:19:36 -05:00
|
|
|
{
|
|
|
|
a_Value["x"] = m_PosX;
|
|
|
|
a_Value["y"] = m_PosY;
|
|
|
|
a_Value["z"] = m_PosZ;
|
|
|
|
|
|
|
|
Json::Value AllSlots;
|
2013-04-10 17:40:30 -04:00
|
|
|
int NumSlots = m_Contents.GetNumSlots();
|
|
|
|
for (int i = 0; i < NumSlots; i++)
|
2012-12-19 16:19:36 -05:00
|
|
|
{
|
|
|
|
Json::Value Slot;
|
2013-04-10 17:40:30 -04:00
|
|
|
m_Contents.GetSlot(i).GetJson(Slot);
|
|
|
|
AllSlots.append(Slot);
|
2012-12-19 16:19:36 -05:00
|
|
|
}
|
|
|
|
a_Value["Slots"] = AllSlots;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cDispenserEntity::SendTo(cClientHandle & a_Client)
|
|
|
|
{
|
|
|
|
// Nothing needs to be sent
|
|
|
|
UNUSED(a_Client);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|