2004-05-24 22:27:25 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-05-22 01:46:16 +02:00
|
|
|
#include "EntityHandles.h"
|
|
|
|
#include "EntityManager.h"
|
2006-06-09 18:44:16 +02:00
|
|
|
#include "Entity.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/CStr.h"
|
2006-08-26 22:25:37 +02:00
|
|
|
#include "network/Serialization.h"
|
2004-05-22 01:46:16 +02:00
|
|
|
|
|
|
|
CHandle::CHandle()
|
|
|
|
{
|
|
|
|
m_entity = NULL;
|
|
|
|
m_refcount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
HEntity::HEntity( u16 index )
|
|
|
|
{
|
|
|
|
m_handle = index;
|
2007-05-02 14:07:08 +02:00
|
|
|
AddRef();
|
2004-05-22 01:46:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HEntity::HEntity( const HEntity& copy )
|
|
|
|
{
|
|
|
|
m_handle = copy.m_handle;
|
2007-05-02 14:07:08 +02:00
|
|
|
AddRef();
|
2004-05-22 01:46:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HEntity::HEntity()
|
|
|
|
{
|
|
|
|
m_handle = INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
HEntity::~HEntity()
|
|
|
|
{
|
2007-04-30 21:58:04 +02:00
|
|
|
if( CEntityManager::IsExtant() )
|
2007-05-02 14:07:08 +02:00
|
|
|
DecRef();
|
2004-05-22 01:46:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void HEntity::operator=( const HEntity& copy )
|
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
DecRef();
|
2004-05-22 01:46:16 +02:00
|
|
|
m_handle = copy.m_handle;
|
2007-05-02 14:07:08 +02:00
|
|
|
AddRef();
|
2004-05-22 01:46:16 +02:00
|
|
|
}
|
|
|
|
|
2004-05-26 22:57:25 +02:00
|
|
|
bool HEntity::operator ==( const HEntity& test ) const
|
|
|
|
{
|
|
|
|
return( m_handle == test.m_handle );
|
|
|
|
}
|
|
|
|
|
2007-05-16 08:58:49 +02:00
|
|
|
bool HEntity::IsValid() const
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
|
|
|
if( m_handle == INVALID_HANDLE )
|
|
|
|
return( false );
|
|
|
|
|
2005-06-28 06:06:25 +02:00
|
|
|
debug_assert( g_EntityManager.m_entities[m_handle].m_refcount );
|
2007-05-16 08:58:49 +02:00
|
|
|
//return( !g_EntityManager.m_entities[m_handle].m_entity->entf_get(ENTF_DESTROYED) );
|
|
|
|
return( true );
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
2007-05-16 08:58:49 +02:00
|
|
|
bool HEntity::IsAlive() const
|
|
|
|
{
|
|
|
|
return( IsValid() && !g_EntityManager.m_entities[m_handle].m_entity->entf_get(ENTF_DESTROYED) );
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
void HEntity::AddRef()
|
2004-05-22 01:46:16 +02:00
|
|
|
{
|
|
|
|
if( m_handle != INVALID_HANDLE )
|
2004-07-23 12:56:52 +02:00
|
|
|
{
|
2005-06-28 06:06:25 +02:00
|
|
|
debug_assert( m_handle < MAX_HANDLES );
|
2004-05-22 01:46:16 +02:00
|
|
|
g_EntityManager.m_entities[m_handle].m_refcount++;
|
2006-07-30 01:58:33 +02:00
|
|
|
g_EntityManager.m_refd[m_handle] = true;
|
2004-07-23 12:56:52 +02:00
|
|
|
}
|
2004-05-22 01:46:16 +02:00
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
void HEntity::DecRef()
|
2004-05-22 01:46:16 +02:00
|
|
|
{
|
|
|
|
if( m_handle != INVALID_HANDLE )
|
|
|
|
{
|
|
|
|
if( --g_EntityManager.m_entities[m_handle].m_refcount == 0 )
|
2006-07-30 01:58:33 +02:00
|
|
|
{
|
|
|
|
g_EntityManager.m_refd[m_handle] = false;
|
2007-05-02 14:07:08 +02:00
|
|
|
g_EntityManager.Destroy( m_handle );
|
2006-07-30 01:58:33 +02:00
|
|
|
}
|
2004-05-22 01:46:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-26 22:57:25 +02:00
|
|
|
CEntity* HEntity::operator->() const
|
2004-05-22 01:46:16 +02:00
|
|
|
{
|
2005-06-28 06:06:25 +02:00
|
|
|
debug_assert( m_handle != INVALID_HANDLE );
|
|
|
|
debug_assert( g_EntityManager.m_entities[m_handle].m_refcount );
|
2004-05-22 01:46:16 +02:00
|
|
|
return( g_EntityManager.m_entities[m_handle].m_entity );
|
|
|
|
}
|
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
HEntity::operator CEntity*() const
|
|
|
|
{
|
2004-10-07 21:23:35 +02:00
|
|
|
if( m_handle == INVALID_HANDLE )
|
|
|
|
return( NULL );
|
|
|
|
|
2005-06-28 06:06:25 +02:00
|
|
|
debug_assert( g_EntityManager.m_entities[m_handle].m_refcount );
|
2004-07-20 21:30:35 +02:00
|
|
|
return( g_EntityManager.m_entities[m_handle].m_entity );
|
|
|
|
}
|
2004-05-26 22:57:25 +02:00
|
|
|
CEntity& HEntity::operator*() const
|
2004-05-22 01:46:16 +02:00
|
|
|
{
|
2005-06-28 06:06:25 +02:00
|
|
|
debug_assert( m_handle != INVALID_HANDLE );
|
|
|
|
debug_assert( g_EntityManager.m_entities[m_handle].m_refcount );
|
2004-05-22 01:46:16 +02:00
|
|
|
return( *g_EntityManager.m_entities[m_handle].m_entity );
|
|
|
|
}
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
uint HEntity::GetSerializedLength() const
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
u8 *HEntity::Serialize(u8* buffer) const
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
|
|
|
Serialize_int_2(buffer, m_handle);
|
2004-09-21 16:40:43 +02:00
|
|
|
return buffer;
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
const u8* HEntity::Deserialize(const u8* buffer, const u8* UNUSED(end))
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
|
|
|
Deserialize_int_2(buffer, m_handle);
|
2007-05-02 14:07:08 +02:00
|
|
|
// We can't let AddRef debug_assert just because someone sent us bogus data
|
2005-05-09 06:37:36 +02:00
|
|
|
if (m_handle < MAX_HANDLES && m_handle != INVALID_HANDLE)
|
2007-05-02 14:07:08 +02:00
|
|
|
AddRef();
|
2004-09-21 16:40:43 +02:00
|
|
|
return buffer;
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HEntity::operator CStr() const
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
sprintf(buf, "Entity#%04x", m_handle);
|
|
|
|
return CStr(buf);
|
|
|
|
}
|
2005-05-18 07:32:09 +02:00
|
|
|
|
|
|
|
uint CEntityList::GetSerializedLength() const
|
|
|
|
{
|
2005-05-18 23:19:56 +02:00
|
|
|
return (uint)(2*size());
|
2005-05-18 07:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u8 *CEntityList::Serialize(u8 *buffer) const
|
|
|
|
{
|
2005-05-19 00:03:02 +02:00
|
|
|
for (size_t i=0;i<(size()-1);i++)
|
2005-05-18 07:32:09 +02:00
|
|
|
Serialize_int_2(buffer, at(i).m_handle);
|
|
|
|
Serialize_int_2(buffer, back().m_handle | HANDLE_SENTINEL_BIT);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
const u8 *CEntityList::Deserialize(const u8* buffer, const u8* UNUSED(end))
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
|
|
|
u16 n=0, handle;
|
|
|
|
while (!(n & HANDLE_SENTINEL_BIT))
|
|
|
|
{
|
|
|
|
Deserialize_int_2(buffer, n);
|
|
|
|
handle = n & ~HANDLE_SENTINEL_BIT;
|
2005-06-28 06:06:25 +02:00
|
|
|
// We have to validate the data, or the HEntity constructor will debug_assert
|
2005-05-18 07:32:09 +02:00
|
|
|
// on us.
|
|
|
|
// FIXME We should also check that the entity actually exists
|
2008-02-09 20:11:01 +01:00
|
|
|
if (handle < MAX_HANDLES)
|
2005-05-18 07:32:09 +02:00
|
|
|
push_back(HEntity(handle));
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEntityList::operator CStr() const
|
|
|
|
{
|
|
|
|
if (size() == 0)
|
|
|
|
{
|
|
|
|
return CStr("Entities {}");
|
|
|
|
}
|
|
|
|
else if (size() == 1)
|
|
|
|
{
|
|
|
|
return front().operator CStr();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CStr buf="{ ";
|
|
|
|
buf += front().operator CStr();
|
|
|
|
for (const_iterator it=begin();it != end();++it)
|
|
|
|
{
|
|
|
|
buf += ", ";
|
|
|
|
buf += it->operator CStr();
|
|
|
|
}
|
|
|
|
buf += " }";
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|