2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <cstdlib>
|
|
|
|
#endif
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Pickup.h"
|
|
|
|
#include "ClientHandle.h"
|
|
|
|
#include "Inventory.h"
|
|
|
|
#include "World.h"
|
2012-10-13 05:53:28 -04:00
|
|
|
#include "Simulator/FluidSimulator.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Server.h"
|
|
|
|
#include "Player.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "Item.h"
|
|
|
|
#include "Root.h"
|
|
|
|
#include "Tracer.h"
|
2013-04-22 03:18:03 -04:00
|
|
|
#include "Chunk.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Vector3d.h"
|
|
|
|
#include "Vector3f.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-27 21:45:20 -05:00
|
|
|
cPickup::cPickup(int a_MicroPosX, int a_MicroPosY, int a_MicroPosZ, const cItem & a_Item, float a_SpeedX /* = 0.f */, float a_SpeedY /* = 0.f */, float a_SpeedZ /* = 0.f */)
|
|
|
|
: cEntity(etPickup, ((double)(a_MicroPosX)) / 32, ((double)(a_MicroPosY)) / 32, ((double)(a_MicroPosZ)) / 32)
|
2013-03-09 09:35:43 -05:00
|
|
|
, m_Health(5)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_Timer( 0.f )
|
2012-12-27 21:45:20 -05:00
|
|
|
, m_Item(a_Item)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_bCollected( false )
|
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
SetSpeed(a_SpeedX, a_SpeedY, a_SpeedZ);
|
2013-04-22 03:18:03 -04:00
|
|
|
m_Gravity = -3.0;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
void cPickup::Initialize(cWorld * a_World)
|
|
|
|
{
|
|
|
|
super::Initialize(a_World);
|
|
|
|
a_World->BroadcastSpawn(*this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
void cPickup::SpawnOn(cClientHandle & a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
a_Client.SendPickupSpawn(*this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
void cPickup::Tick(float a_Dt, cChunk & a_Chunk)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-04-13 17:02:10 -04:00
|
|
|
super::Tick(a_Dt, a_Chunk);
|
2013-04-22 03:18:03 -04:00
|
|
|
BroadcastMovementUpdate(); //Notify clients of position
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Timer += a_Dt;
|
2013-04-22 03:18:03 -04:00
|
|
|
|
|
|
|
if (!m_bCollected)
|
|
|
|
{
|
|
|
|
int BlockY = (int) floor(GetPosY());
|
2013-04-28 11:30:06 -04:00
|
|
|
if (BlockY < cChunkDef::Height) // Don't do anything except for falling when above the world
|
2013-04-22 03:18:03 -04:00
|
|
|
{
|
2013-04-28 11:30:06 -04:00
|
|
|
int BlockX = (int) floor(GetPosX());
|
|
|
|
int BlockZ = (int) floor(GetPosZ());
|
|
|
|
//Position might have changed due to physics. So we have to make sure we have the correct chunk.
|
|
|
|
cChunk * CurrentChunk = a_Chunk.GetNeighborChunk(BlockX, BlockZ);
|
|
|
|
if (CurrentChunk != NULL) // Make sure the chunk is loaded
|
2013-04-22 03:18:03 -04:00
|
|
|
{
|
2013-04-28 11:30:06 -04:00
|
|
|
int RelBlockX = BlockX - (CurrentChunk->GetPosX() * cChunkDef::Width);
|
|
|
|
int RelBlockZ = BlockZ - (CurrentChunk->GetPosZ() * cChunkDef::Width);
|
|
|
|
|
|
|
|
BLOCKTYPE BlockBelow = CurrentChunk->GetBlock(RelBlockX, BlockY - 1, RelBlockZ);
|
|
|
|
BLOCKTYPE BlockIn = CurrentChunk->GetBlock(RelBlockX, BlockY, RelBlockZ);
|
|
|
|
|
|
|
|
if (
|
|
|
|
IsBlockLava(BlockBelow) || (BlockBelow == E_BLOCK_FIRE) ||
|
|
|
|
IsBlockLava(BlockIn) || (BlockIn == E_BLOCK_FIRE)
|
|
|
|
)
|
|
|
|
{
|
2013-04-22 03:18:03 -04:00
|
|
|
m_bCollected = true;
|
2013-04-28 11:30:06 -04:00
|
|
|
m_Timer = 0; // We have to reset the timer.
|
|
|
|
m_Timer += a_Dt; // In case we have to destroy the pickup in the same tick.
|
2013-04-22 03:18:03 -04:00
|
|
|
if (m_Timer > 500.f)
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-28 11:30:06 -04:00
|
|
|
}
|
2013-04-22 03:18:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-22 05:15:53 -05:00
|
|
|
if (m_Timer > 500.f) // 0.5 second
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
if (m_Timer > 1000 * 60 * 5) // 5 minutes
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-22 02:33:10 -04:00
|
|
|
if (GetPosY() < -8) // Out of this world and no more visible!
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
bool cPickup::CollectedBy(cPlayer * a_Dest)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-13 16:29:52 -04:00
|
|
|
ASSERT(a_Dest != NULL);
|
|
|
|
|
2012-08-24 05:49:00 -04:00
|
|
|
if (m_bCollected)
|
|
|
|
{
|
2013-05-15 10:52:56 -04:00
|
|
|
LOG("Pickup %d cannot be collected by \"%s\", because it has already been collected.", m_UniqueID, a_Dest->GetName().c_str());
|
2012-08-24 05:49:00 -04:00
|
|
|
return false; // It's already collected!
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// 800 is to long
|
2012-08-24 05:49:00 -04:00
|
|
|
if (m_Timer < 500.f)
|
|
|
|
{
|
2013-05-15 10:52:56 -04:00
|
|
|
LOG("Pickup %d cannot be collected by \"%s\", because it is not old enough.", m_UniqueID, a_Dest->GetName().c_str());
|
2012-08-24 05:49:00 -04:00
|
|
|
return false; // Not old enough
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-01-28 11:17:26 -05:00
|
|
|
if (cRoot::Get()->GetPluginManager()->CallHookCollectingPickup(a_Dest, *this))
|
2012-08-24 05:49:00 -04:00
|
|
|
{
|
2013-05-15 10:52:56 -04:00
|
|
|
LOG("Pickup %d cannot be collected by \"%s\", because a plugin has said no.", m_UniqueID, a_Dest->GetName().c_str());
|
2012-08-24 05:49:00 -04:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-12-27 21:45:20 -05:00
|
|
|
if (a_Dest->GetInventory().AddItemAnyAmount(m_Item))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 05:49:00 -04:00
|
|
|
m_World->BroadcastCollectPickup(*this, *a_Dest);
|
2012-06-14 09:06:06 -04:00
|
|
|
m_bCollected = true;
|
|
|
|
m_Timer = 0;
|
2012-12-27 21:45:20 -05:00
|
|
|
if (m_Item.m_ItemCount != 0)
|
2012-12-22 05:15:53 -05:00
|
|
|
{
|
2012-10-24 08:48:25 -04:00
|
|
|
cItems Pickup;
|
2012-12-27 21:45:20 -05:00
|
|
|
Pickup.push_back(cItem(m_Item));
|
2013-03-22 02:33:10 -04:00
|
|
|
m_World->SpawnItemPickups(Pickup, GetPosX(), GetPosY(), GetPosZ());
|
2012-10-24 08:48:25 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-13 16:29:52 -04:00
|
|
|
LOG("Pickup %d cannot be collected by \"%s\", because there's no space in the inventory.", a_Dest->GetName().c_str(), m_UniqueID);
|
2012-06-14 09:06:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|