2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <cstdlib>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cPickup.h"
|
|
|
|
#include "cClientHandle.h"
|
|
|
|
#include "cInventory.h"
|
|
|
|
#include "cWorld.h"
|
2011-12-23 20:36:15 -05:00
|
|
|
#include "cWaterSimulator.h"
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cServer.h"
|
|
|
|
#include "cPlayer.h"
|
|
|
|
#include "cPluginManager.h"
|
|
|
|
#include "cItem.h"
|
|
|
|
#include "cRoot.h"
|
|
|
|
#include "cTracer.h"
|
|
|
|
#include "cChunk.h"
|
|
|
|
|
|
|
|
#include "packets/cPacket_TeleportEntity.h"
|
|
|
|
#include "packets/cPacket_PickupSpawn.h"
|
|
|
|
#include "packets/cPacket_CollectItem.h"
|
|
|
|
|
|
|
|
#include "Vector3d.h"
|
|
|
|
#include "Vector3f.h"
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
CLASS_DEFINITION( cPickup, cEntity )
|
|
|
|
|
|
|
|
cPickup::~cPickup()
|
|
|
|
{
|
|
|
|
delete m_Item;
|
|
|
|
}
|
|
|
|
|
|
|
|
cPickup::cPickup(int a_X, int a_Y, int a_Z, const cItem & a_Item, float a_SpeedX /* = 0.f */, float a_SpeedY /* = 0.f */, float a_SpeedZ /* = 0.f */)
|
|
|
|
: cEntity( ((double)(a_X))/32, ((double)(a_Y))/32, ((double)(a_Z))/32 )
|
2012-02-15 17:50:00 -05:00
|
|
|
, m_Speed( a_SpeedX, a_SpeedY, a_SpeedZ )
|
2011-10-03 14:41:19 -04:00
|
|
|
, m_bOnGround( false )
|
|
|
|
, m_bReplicated( false )
|
|
|
|
, m_Timer( 0.f )
|
|
|
|
, m_Item( new cItem( a_Item ) )
|
|
|
|
, m_bCollected( false )
|
|
|
|
{
|
|
|
|
|
|
|
|
//LOG("New pickup: ID(%i) Amount(%i) Health(%i)", m_Item.m_ItemID, m_Item.m_ItemCount, m_Item.m_ItemHealth );
|
|
|
|
|
|
|
|
// Spawn it on clients
|
2012-02-13 16:47:03 -05:00
|
|
|
if (!a_Item.IsEmpty())
|
|
|
|
{
|
|
|
|
std::auto_ptr<cPacket> PickupSpawn(GetSpawnPacket());
|
|
|
|
if (PickupSpawn.get() != NULL)
|
|
|
|
{
|
|
|
|
cRoot::Get()->GetServer()->Broadcast( PickupSpawn.get() );
|
|
|
|
}
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
m_EntityType = E_PICKUP;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cPickup::cPickup(cPacket_PickupSpawn* a_PickupSpawnPacket)
|
|
|
|
: cEntity( ((double)a_PickupSpawnPacket->m_PosX)/32, ((double)a_PickupSpawnPacket->m_PosY)/32, ((double)a_PickupSpawnPacket->m_PosZ)/32 )
|
2011-11-01 17:57:08 -04:00
|
|
|
, m_Speed( new Vector3f() )
|
2011-12-23 20:36:15 -05:00
|
|
|
, m_ResultingSpeed(new Vector3f())
|
|
|
|
, m_WaterSpeed(new Vector3f())
|
2011-10-03 14:41:19 -04:00
|
|
|
, m_bOnGround( false )
|
|
|
|
, m_bReplicated( false )
|
|
|
|
, m_Timer( 0.f )
|
|
|
|
, m_bCollected( false )
|
|
|
|
{
|
|
|
|
a_PickupSpawnPacket->m_UniqueID = m_UniqueID;
|
|
|
|
|
|
|
|
m_Item = new cItem();
|
|
|
|
m_Item->m_ItemID = (ENUM_ITEM_ID)a_PickupSpawnPacket->m_Item;
|
|
|
|
m_Item->m_ItemCount = a_PickupSpawnPacket->m_Count;
|
|
|
|
m_Item->m_ItemHealth = 0x0;
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Speed.x = (float)(a_PickupSpawnPacket->m_Rotation) / 8;
|
|
|
|
m_Speed.y = (float)(a_PickupSpawnPacket->m_Pitch) / 8;
|
|
|
|
m_Speed.z = (float)(a_PickupSpawnPacket->m_Roll) / 8;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
// Spawn it on clients
|
2012-02-13 16:47:03 -05:00
|
|
|
if (a_PickupSpawnPacket->m_Item != E_ITEM_EMPTY)
|
|
|
|
{
|
2011-12-22 11:30:40 -05:00
|
|
|
cRoot::Get()->GetServer()->Broadcast( *a_PickupSpawnPacket );
|
2012-02-13 16:47:03 -05:00
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
m_EntityType = E_PICKUP;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cPacket * cPickup::GetSpawnPacket(void) const
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
if (m_Item->IsEmpty())
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cPacket_PickupSpawn * PickupSpawn = new cPacket_PickupSpawn;
|
|
|
|
PickupSpawn->m_UniqueID = m_UniqueID;
|
|
|
|
PickupSpawn->m_Item = (short)m_Item->m_ItemID;
|
|
|
|
PickupSpawn->m_Count = m_Item->m_ItemCount;
|
|
|
|
PickupSpawn->m_Health = m_Item->m_ItemHealth;
|
2012-02-15 17:50:00 -05:00
|
|
|
PickupSpawn->m_PosX = (int) (m_Pos.x * 32);
|
|
|
|
PickupSpawn->m_PosY = (int) (m_Pos.y * 32);
|
|
|
|
PickupSpawn->m_PosZ = (int) (m_Pos.z * 32);
|
|
|
|
PickupSpawn->m_Rotation = (char)(m_Speed.x * 8);
|
|
|
|
PickupSpawn->m_Pitch = (char)(m_Speed.y * 8);
|
|
|
|
PickupSpawn->m_Roll = (char)(m_Speed.z * 8);
|
2012-02-13 16:47:03 -05:00
|
|
|
return PickupSpawn;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cPickup::Tick(float a_Dt)
|
|
|
|
{
|
2011-12-22 11:30:40 -05:00
|
|
|
m_Timer += a_Dt;
|
2011-10-03 14:41:19 -04:00
|
|
|
a_Dt = a_Dt / 1000.f;
|
|
|
|
if(m_bCollected)
|
|
|
|
{
|
|
|
|
if(m_Timer > 500.f) // 0.5 second
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_Timer > 1000*60*5 ) // 5 minutes
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
if( m_Pos.y < 0 ) // Out of this world!
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-06 04:23:20 -05:00
|
|
|
if(!m_bCollected)
|
2012-02-13 16:47:03 -05:00
|
|
|
{
|
2011-11-06 04:23:20 -05:00
|
|
|
HandlePhysics( a_Dt );
|
2012-02-13 16:47:03 -05:00
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
if( !m_bReplicated || m_bDirtyPosition )
|
|
|
|
{
|
|
|
|
MoveToCorrectChunk();
|
|
|
|
m_bReplicated = true;
|
|
|
|
m_bDirtyPosition = false;
|
2012-02-13 16:47:03 -05:00
|
|
|
cPacket_TeleportEntity TeleportEntity( this );
|
|
|
|
GetWorld()->GetChunk( m_ChunkX, m_ChunkY, m_ChunkZ )->Broadcast( &TeleportEntity );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cPickup::HandlePhysics(float a_Dt)
|
|
|
|
{
|
2012-02-15 17:50:00 -05:00
|
|
|
m_ResultingSpeed.Set(0.f, 0.f, 0.f);
|
2012-02-13 16:47:03 -05:00
|
|
|
cWorld * World = GetWorld();
|
2011-12-23 20:36:15 -05:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
if( m_bOnGround ) // check if it's still on the ground
|
|
|
|
{
|
2012-02-15 17:50:00 -05:00
|
|
|
int BlockX = (m_Pos.x)<0 ? (int)m_Pos.x-1 : (int)m_Pos.x;
|
|
|
|
int BlockZ = (m_Pos.z)<0 ? (int)m_Pos.z-1 : (int)m_Pos.z;
|
|
|
|
char BlockBelow = World->GetBlock( BlockX, (int)m_Pos.y -1, BlockZ );
|
2011-12-22 11:30:40 -05:00
|
|
|
//Not only air, falls through water ;)
|
|
|
|
if( BlockBelow == E_BLOCK_AIR || IsBlockWater(BlockBelow))
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = false;
|
|
|
|
}
|
2012-02-15 17:50:00 -05:00
|
|
|
char Block = World->GetBlock( BlockX, (int)m_Pos.y - (int)m_bOnGround, BlockZ );
|
|
|
|
char BlockIn = World->GetBlock( BlockX, (int)m_Pos.y, BlockZ );
|
2011-12-28 21:44:21 -05:00
|
|
|
|
|
|
|
if( IsBlockLava(Block) || Block == E_BLOCK_FIRE
|
|
|
|
|| IsBlockLava(BlockIn) || BlockIn == E_BLOCK_FIRE)
|
|
|
|
{
|
2011-11-06 04:23:20 -05:00
|
|
|
m_bCollected = true;
|
|
|
|
m_Timer = 0;
|
|
|
|
return;
|
|
|
|
}
|
2011-12-28 21:44:21 -05:00
|
|
|
|
2011-12-22 11:30:40 -05:00
|
|
|
if( BlockIn != E_BLOCK_AIR && !IsBlockWater(BlockIn) ) // If in ground itself, push it out
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = true;
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Pos.y += 0.2;
|
2011-10-03 14:41:19 -04:00
|
|
|
m_bReplicated = false;
|
|
|
|
}
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Speed.x *= 0.7f/(1+a_Dt);
|
|
|
|
if( fabs(m_Speed.x) < 0.05 ) m_Speed.x = 0;
|
|
|
|
m_Speed.z *= 0.7f/(1+a_Dt);
|
|
|
|
if( fabs(m_Speed.z) < 0.05 ) m_Speed.z = 0;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2011-12-23 20:36:15 -05:00
|
|
|
|
|
|
|
//get flowing direction
|
2012-02-15 17:50:00 -05:00
|
|
|
Direction WaterDir = World->GetWaterSimulator()->GetFlowingDirection((int) m_Pos.x - 1, (int) m_Pos.y, (int) m_Pos.z - 1);
|
2011-12-23 20:36:15 -05:00
|
|
|
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
m_WaterSpeed *= 0.9f; //Keep old speed but lower it
|
2011-12-23 20:36:15 -05:00
|
|
|
|
|
|
|
switch(WaterDir)
|
|
|
|
{
|
|
|
|
case X_PLUS:
|
2012-02-15 17:50:00 -05:00
|
|
|
m_WaterSpeed.x = 1.f;
|
2011-12-23 20:36:15 -05:00
|
|
|
m_bOnGround = false;
|
|
|
|
break;
|
|
|
|
case X_MINUS:
|
2012-02-15 17:50:00 -05:00
|
|
|
m_WaterSpeed.x = -1.f;
|
2011-12-23 20:36:15 -05:00
|
|
|
m_bOnGround = false;
|
|
|
|
break;
|
|
|
|
case Z_PLUS:
|
2012-02-15 17:50:00 -05:00
|
|
|
m_WaterSpeed.z = 1.f;
|
2011-12-23 20:36:15 -05:00
|
|
|
m_bOnGround = false;
|
|
|
|
break;
|
|
|
|
case Z_MINUS:
|
2012-02-15 17:50:00 -05:00
|
|
|
m_WaterSpeed.z = -1.f;
|
2011-12-23 20:36:15 -05:00
|
|
|
m_bOnGround = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
m_ResultingSpeed += m_WaterSpeed;
|
2011-12-23 20:36:15 -05:00
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
if( !m_bOnGround )
|
|
|
|
{
|
|
|
|
|
|
|
|
float Gravity = -9.81f*a_Dt;
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Speed.y += Gravity;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-12-23 20:36:15 -05:00
|
|
|
// Set to hit position
|
2012-02-15 17:50:00 -05:00
|
|
|
m_ResultingSpeed += m_Speed;
|
2011-12-23 20:36:15 -05:00
|
|
|
|
2011-10-30 20:52:20 -04:00
|
|
|
cTracer Tracer( GetWorld() );
|
2012-02-15 17:50:00 -05:00
|
|
|
int Ret = Tracer.Trace( m_Pos, m_Speed, 2 );
|
2011-10-03 14:41:19 -04:00
|
|
|
if( Ret ) // Oh noez! we hit something
|
|
|
|
{
|
2011-12-23 20:36:15 -05:00
|
|
|
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
if( (Tracer.RealHit - Vector3f(m_Pos)).SqrLength() <= ( m_ResultingSpeed * a_Dt ).SqrLength() )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
m_bReplicated = false; // It's only interesting to replicate when we actually hit something...
|
|
|
|
if( Ret == 1 )
|
|
|
|
{
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
if( Tracer.HitNormal.x != 0.f ) m_Speed.x = 0.f;
|
|
|
|
if( Tracer.HitNormal.y != 0.f ) m_Speed.y = 0.f;
|
|
|
|
if( Tracer.HitNormal.z != 0.f ) m_Speed.z = 0.f;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
if( Tracer.HitNormal.y > 0 ) // means on ground
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = true;
|
|
|
|
}
|
|
|
|
}
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Pos = Tracer.RealHit;
|
|
|
|
m_Pos += Tracer.HitNormal * 0.2f;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Pos += m_ResultingSpeed*a_Dt;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // We didn't hit anything, so move =]
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Pos += m_ResultingSpeed * a_Dt;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
}
|
2011-12-23 20:36:15 -05:00
|
|
|
//Usable for debugging
|
2012-02-15 17:50:00 -05:00
|
|
|
//SetPosition(m_Pos.x, m_Pos.y, m_Pos.z);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
bool cPickup::CollectedBy( cPlayer* a_Dest )
|
|
|
|
{
|
|
|
|
if(m_bCollected) return false; // It's already collected!
|
2011-12-21 15:42:34 -05:00
|
|
|
// 800 is to long
|
|
|
|
if(m_Timer < 500.f) return false; // Not old enough
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
if( cRoot::Get()->GetPluginManager()->CallHook( cPluginManager::E_PLUGIN_COLLECT_ITEM, 2, this, a_Dest ) ) return false;
|
|
|
|
|
|
|
|
if( a_Dest->GetInventory().AddItem( *m_Item ) )
|
|
|
|
{
|
|
|
|
cPacket_CollectItem CollectItem;
|
|
|
|
CollectItem.m_CollectedID = m_UniqueID;
|
|
|
|
CollectItem.m_CollectorID = a_Dest->GetUniqueID();
|
|
|
|
cRoot::Get()->GetServer()->Broadcast( CollectItem );
|
|
|
|
|
|
|
|
m_bCollected = true;
|
|
|
|
m_Timer = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|