forked from 0ad/0ad
janwas
c0ed950657
this snowballed into a massive search+destroy of the hodgepodge of mostly equivalent types we had in use (int, uint, unsigned, unsigned int, i32, u32, ulong, uintN). it is more efficient to use 64-bit types in 64-bit mode, so the preferred default is size_t (for anything remotely resembling a size or index). tile coordinates are ssize_t to allow more efficient conversion to/from floating point. flags are int because we almost never need more than 15 distinct bits, bit test/set is not slower and int is fastest to type. finally, some data that is pretty much directly passed to OpenGL is now typed accordingly. after several hours, the code now requires fewer casts and less guesswork. other changes: - unit and player IDs now have an "invalid id" constant in the respective class to avoid casting and -1 - fix some endian/64-bit bugs in the map (un)packing. added a convenience function to write/read a size_t. - ia32: change CPUID interface to allow passing in ecx (required for cache topology detection, which I need at work). remove some unneeded functions from asm, replace with intrinsics where possible. This was SVN commit r5942.
188 lines
3.7 KiB
C++
188 lines
3.7 KiB
C++
#include "precompiled.h"
|
|
|
|
#include "EntityHandles.h"
|
|
#include "EntityManager.h"
|
|
#include "Entity.h"
|
|
#include "ps/CStr.h"
|
|
#include "network/Serialization.h"
|
|
|
|
CHandle::CHandle()
|
|
{
|
|
m_entity = NULL;
|
|
m_refcount = 0;
|
|
}
|
|
|
|
HEntity::HEntity( u16 index )
|
|
{
|
|
m_handle = index;
|
|
AddRef();
|
|
}
|
|
|
|
HEntity::HEntity( const HEntity& copy )
|
|
{
|
|
m_handle = copy.m_handle;
|
|
AddRef();
|
|
}
|
|
|
|
HEntity::HEntity()
|
|
{
|
|
m_handle = INVALID_HANDLE;
|
|
}
|
|
|
|
HEntity::~HEntity()
|
|
{
|
|
if( CEntityManager::IsExtant() )
|
|
DecRef();
|
|
}
|
|
|
|
void HEntity::operator=( const HEntity& copy )
|
|
{
|
|
DecRef();
|
|
m_handle = copy.m_handle;
|
|
AddRef();
|
|
}
|
|
|
|
bool HEntity::operator ==( const HEntity& test ) const
|
|
{
|
|
return( m_handle == test.m_handle );
|
|
}
|
|
|
|
bool HEntity::IsValid() const
|
|
{
|
|
if( m_handle == INVALID_HANDLE )
|
|
return( false );
|
|
|
|
debug_assert( g_EntityManager.m_entities[m_handle].m_refcount );
|
|
//return( !g_EntityManager.m_entities[m_handle].m_entity->entf_get(ENTF_DESTROYED) );
|
|
return( true );
|
|
}
|
|
|
|
bool HEntity::IsAlive() const
|
|
{
|
|
return( IsValid() && !g_EntityManager.m_entities[m_handle].m_entity->entf_get(ENTF_DESTROYED) );
|
|
}
|
|
|
|
void HEntity::AddRef()
|
|
{
|
|
if( m_handle != INVALID_HANDLE )
|
|
{
|
|
debug_assert( m_handle < MAX_HANDLES );
|
|
g_EntityManager.m_entities[m_handle].m_refcount++;
|
|
g_EntityManager.m_refd[m_handle] = true;
|
|
}
|
|
}
|
|
|
|
void HEntity::DecRef()
|
|
{
|
|
if( m_handle != INVALID_HANDLE )
|
|
{
|
|
if( --g_EntityManager.m_entities[m_handle].m_refcount == 0 )
|
|
{
|
|
g_EntityManager.m_refd[m_handle] = false;
|
|
g_EntityManager.Destroy( m_handle );
|
|
}
|
|
}
|
|
}
|
|
|
|
CEntity* HEntity::operator->() const
|
|
{
|
|
debug_assert( m_handle != INVALID_HANDLE );
|
|
debug_assert( g_EntityManager.m_entities[m_handle].m_refcount );
|
|
return( g_EntityManager.m_entities[m_handle].m_entity );
|
|
}
|
|
|
|
HEntity::operator CEntity*() const
|
|
{
|
|
if( m_handle == INVALID_HANDLE )
|
|
return( NULL );
|
|
|
|
debug_assert( g_EntityManager.m_entities[m_handle].m_refcount );
|
|
return( g_EntityManager.m_entities[m_handle].m_entity );
|
|
}
|
|
CEntity& HEntity::operator*() const
|
|
{
|
|
debug_assert( m_handle != INVALID_HANDLE );
|
|
debug_assert( g_EntityManager.m_entities[m_handle].m_refcount );
|
|
return( *g_EntityManager.m_entities[m_handle].m_entity );
|
|
}
|
|
|
|
size_t HEntity::GetSerializedLength() const
|
|
{
|
|
return 2;
|
|
}
|
|
|
|
u8 *HEntity::Serialize(u8* buffer) const
|
|
{
|
|
Serialize_int_2(buffer, m_handle);
|
|
return buffer;
|
|
}
|
|
|
|
const u8* HEntity::Deserialize(const u8* buffer, const u8* UNUSED(end))
|
|
{
|
|
Deserialize_int_2(buffer, m_handle);
|
|
// We can't let AddRef debug_assert just because someone sent us bogus data
|
|
if (m_handle < MAX_HANDLES && m_handle != INVALID_HANDLE)
|
|
AddRef();
|
|
return buffer;
|
|
}
|
|
|
|
HEntity::operator CStr() const
|
|
{
|
|
char buf[16];
|
|
sprintf(buf, "Entity#%04x", m_handle);
|
|
return CStr(buf);
|
|
}
|
|
|
|
size_t CEntityList::GetSerializedLength() const
|
|
{
|
|
return (size_t)(2*size());
|
|
}
|
|
|
|
u8 *CEntityList::Serialize(u8 *buffer) const
|
|
{
|
|
for (size_t i=0;i<(size()-1);i++)
|
|
Serialize_int_2(buffer, at(i).m_handle);
|
|
Serialize_int_2(buffer, back().m_handle | HANDLE_SENTINEL_BIT);
|
|
return buffer;
|
|
}
|
|
|
|
const u8 *CEntityList::Deserialize(const u8* buffer, const u8* UNUSED(end))
|
|
{
|
|
u16 n=0, handle;
|
|
while (!(n & HANDLE_SENTINEL_BIT))
|
|
{
|
|
Deserialize_int_2(buffer, n);
|
|
handle = n & ~HANDLE_SENTINEL_BIT;
|
|
// We have to validate the data, or the HEntity constructor will debug_assert
|
|
// on us.
|
|
// FIXME We should also check that the entity actually exists
|
|
if (handle < MAX_HANDLES)
|
|
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;
|
|
}
|
|
}
|