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"
|
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_bOnGround( false )
|
|
|
|
, m_bReplicated( false )
|
|
|
|
, 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);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
void cPickup::Tick(float a_Dt, MTRand & a_TickRandom)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-22 05:15:53 -05:00
|
|
|
super::Tick(a_Dt, a_TickRandom);
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Timer += a_Dt;
|
|
|
|
a_Dt = a_Dt / 1000.f;
|
2012-12-22 05:15:53 -05:00
|
|
|
if (m_bCollected)
|
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-08-19 15:42:32 -04:00
|
|
|
if (!m_bReplicated || m_bDirtyPosition)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
MoveToCorrectChunk();
|
|
|
|
m_bReplicated = true;
|
|
|
|
m_bDirtyPosition = false;
|
2012-08-19 15:42:32 -04:00
|
|
|
GetWorld()->BroadcastTeleportEntity(*this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPickup::HandlePhysics(float a_Dt)
|
|
|
|
{
|
|
|
|
m_ResultingSpeed.Set(0.f, 0.f, 0.f);
|
|
|
|
cWorld * World = GetWorld();
|
2013-01-06 14:32:40 -05:00
|
|
|
|
|
|
|
a_Dt /= 1000; // Go from msec to sec, so that physics formulas work
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-01-06 14:32:40 -05:00
|
|
|
if (m_bOnGround) // check if it's still on the ground
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
int BlockX = (GetPosX() < 0) ? (int)GetPosX() - 1 : (int)GetPosX();
|
|
|
|
int BlockZ = (GetPosZ() < 0) ? (int)GetPosZ() - 1 : (int)GetPosZ();
|
|
|
|
char BlockBelow = World->GetBlock(BlockX, (int)GetPosY() - 1, BlockZ);
|
2013-01-06 14:32:40 -05:00
|
|
|
if (BlockBelow == E_BLOCK_AIR || IsBlockWater(BlockBelow))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = false;
|
|
|
|
}
|
2013-03-22 02:33:10 -04:00
|
|
|
char Block = World->GetBlock(BlockX, (int)GetPosY() - (int)m_bOnGround, BlockZ );
|
|
|
|
char BlockIn = World->GetBlock(BlockX, (int)GetPosY(), BlockZ );
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
if( IsBlockLava(Block) || Block == E_BLOCK_FIRE
|
|
|
|
|| IsBlockLava(BlockIn) || BlockIn == E_BLOCK_FIRE)
|
|
|
|
{
|
|
|
|
m_bCollected = true;
|
|
|
|
m_Timer = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( BlockIn != E_BLOCK_AIR && !IsBlockWater(BlockIn) ) // If in ground itself, push it out
|
|
|
|
{
|
|
|
|
m_bOnGround = true;
|
2013-03-23 00:33:47 -04:00
|
|
|
AddPosY(0.2);
|
2012-06-14 09:06:06 -04:00
|
|
|
m_bReplicated = false;
|
|
|
|
}
|
2013-03-22 02:33:10 -04:00
|
|
|
SetSpeedX(GetSpeedX() * 0.7f/(1+a_Dt));
|
|
|
|
if( fabs(GetSpeedX()) < 0.05 ) SetSpeedX(0);
|
|
|
|
SetSpeedZ(GetSpeedZ() * 0.7f/(1+a_Dt));
|
|
|
|
if( fabs(GetSpeedZ()) < 0.05 ) SetSpeedZ(0);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-10-13 05:53:28 -04:00
|
|
|
// get flowing direction
|
2013-03-22 02:33:10 -04:00
|
|
|
Direction WaterDir = World->GetWaterSimulator()->GetFlowingDirection((int) GetSpeedX() - 1, (int) GetSpeedY(), (int) GetSpeedZ() - 1);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
m_WaterSpeed *= 0.9f; //Keep old speed but lower it
|
|
|
|
|
|
|
|
switch(WaterDir)
|
|
|
|
{
|
|
|
|
case X_PLUS:
|
|
|
|
m_WaterSpeed.x = 1.f;
|
|
|
|
m_bOnGround = false;
|
|
|
|
break;
|
|
|
|
case X_MINUS:
|
|
|
|
m_WaterSpeed.x = -1.f;
|
|
|
|
m_bOnGround = false;
|
|
|
|
break;
|
|
|
|
case Z_PLUS:
|
|
|
|
m_WaterSpeed.z = 1.f;
|
|
|
|
m_bOnGround = false;
|
|
|
|
break;
|
|
|
|
case Z_MINUS:
|
|
|
|
m_WaterSpeed.z = -1.f;
|
|
|
|
m_bOnGround = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ResultingSpeed += m_WaterSpeed;
|
|
|
|
|
2013-01-06 14:32:40 -05:00
|
|
|
if (!m_bOnGround)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
|
2013-01-06 14:32:40 -05:00
|
|
|
float Gravity = -9.81f * a_Dt;
|
|
|
|
if (Gravity < -3) // Cap gravity-caused speed at 3 m / sec
|
|
|
|
{
|
|
|
|
Gravity = -3;
|
|
|
|
}
|
2013-03-23 00:33:47 -04:00
|
|
|
AddSpeedY(Gravity);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Set to hit position
|
2013-03-22 02:33:10 -04:00
|
|
|
m_ResultingSpeed += GetSpeed();
|
2013-01-06 14:32:40 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
LOGD("Pickup #%d speed: {%.03f, %.03f, %.03f}, pos {%.02f, %.02f, %.02f}",
|
|
|
|
m_UniqueID,
|
|
|
|
m_ResultingSpeed.x, m_ResultingSpeed.y, m_ResultingSpeed.z,
|
|
|
|
m_Pos.x, m_Pos.y, m_Pos.z
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
|
|
|
|
cTracer Tracer(GetWorld());
|
2013-03-22 02:33:10 -04:00
|
|
|
int Ret = Tracer.Trace(GetPosition(), GetSpeed(), 2);
|
2013-01-06 14:32:40 -05:00
|
|
|
if (Ret) // Oh noez! we hit something
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
if ((Tracer.RealHit - Vector3f(GetPosition())).SqrLength() <= ( m_ResultingSpeed * a_Dt ).SqrLength())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_bReplicated = false; // It's only interesting to replicate when we actually hit something...
|
2013-01-06 14:32:40 -05:00
|
|
|
if (Ret == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
|
2013-03-22 02:33:10 -04:00
|
|
|
if( Tracer.HitNormal.x != 0.f ) SetSpeedX(0.f);
|
|
|
|
if( Tracer.HitNormal.y != 0.f ) SetSpeedY(0.f);
|
|
|
|
if( Tracer.HitNormal.z != 0.f ) SetSpeedZ(0.f);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-01-06 14:32:40 -05:00
|
|
|
if (Tracer.HitNormal.y > 0) // means on ground
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = true;
|
|
|
|
}
|
|
|
|
}
|
2013-03-22 02:33:10 -04:00
|
|
|
SetPosition(Tracer.RealHit);
|
2013-03-23 00:33:47 -04:00
|
|
|
AddPosition(Tracer.HitNormal * 0.2f);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2013-03-23 00:33:47 -04:00
|
|
|
AddPosition(m_ResultingSpeed*a_Dt);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // We didn't hit anything, so move =]
|
2013-03-23 00:33:47 -04:00
|
|
|
AddPosition(m_ResultingSpeed*a_Dt);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
2013-01-06 14:32:40 -05:00
|
|
|
// Usable for debugging
|
2013-03-22 02:33:10 -04:00
|
|
|
//SetPosition(m_Pos.x, m_Pos.y, m_Pos.z);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
bool cPickup::CollectedBy(cPlayer * a_Dest)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 05:49:00 -04:00
|
|
|
if (m_bCollected)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|