2009-04-18 10:00:33 -07:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-05-21 17:57:54 -07:00
|
|
|
// EntityHandles.h
|
|
|
|
|
//
|
|
|
|
|
// Entity smart pointer definitions.
|
|
|
|
|
//
|
|
|
|
|
// Usage: Use in place of a standard CEntity pointer.
|
|
|
|
|
// Handles dereference and pointer-to-member as a standard smart pointer.
|
|
|
|
|
// Reference counted. Handles are freed and entity classes released when refcount hits 0.
|
|
|
|
|
// For this reason, be careful where and how long you store these.
|
|
|
|
|
//
|
|
|
|
|
// Entities maintain their own handles via their HEntity me member.
|
|
|
|
|
// To release an entity, scramble this handle. The memory will be freed when the last reference to it expires.
|
|
|
|
|
//
|
|
|
|
|
// Standard CEntity pointers must not be used for a) anything that could be sent over the network.
|
|
|
|
|
// b) anything that will be kept between simulation steps.
|
|
|
|
|
// CEntity pointers will be faster for passing to the graphics subsytem, etc. where they won't be stored.
|
|
|
|
|
// And yes, most of this /is/ obvious. But it should be said anyway.
|
|
|
|
|
|
2007-05-07 09:33:24 -07:00
|
|
|
#ifndef INCLUDED_ENTITYHANDLES
|
|
|
|
|
#define INCLUDED_ENTITYHANDLES
|
2004-05-21 16:46:16 -07:00
|
|
|
|
|
|
|
|
#define INVALID_HANDLE 65535
|
|
|
|
|
|
|
|
|
|
class CEntity;
|
|
|
|
|
class CEntityManager;
|
2005-05-18 14:19:56 -07:00
|
|
|
struct CEntityList;
|
2004-08-16 08:19:17 -07:00
|
|
|
class CStr8;
|
2004-05-21 16:46:16 -07:00
|
|
|
|
|
|
|
|
class CHandle
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CEntity* m_entity;
|
2004-05-28 20:32:33 -07:00
|
|
|
i16 m_refcount;
|
2004-05-21 16:46:16 -07:00
|
|
|
CHandle();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class HEntity
|
|
|
|
|
{
|
2006-07-29 16:58:33 -07:00
|
|
|
friend class CEntity;
|
2004-06-02 09:11:32 -07:00
|
|
|
friend class CEntityManager;
|
2005-05-18 14:19:56 -07:00
|
|
|
friend struct CEntityList;
|
2004-05-21 16:46:16 -07:00
|
|
|
u16 m_handle;
|
2004-06-02 19:20:48 -07:00
|
|
|
private:
|
2007-05-02 05:07:08 -07:00
|
|
|
void AddRef();
|
|
|
|
|
void DecRef();
|
2008-07-17 10:00:00 -07:00
|
|
|
HEntity( size_t index );
|
2004-05-21 16:46:16 -07:00
|
|
|
public:
|
|
|
|
|
HEntity();
|
|
|
|
|
HEntity( const HEntity& copy );
|
2007-05-15 23:58:49 -07:00
|
|
|
~HEntity();
|
|
|
|
|
|
2004-05-21 16:46:16 -07:00
|
|
|
void operator=( const HEntity& copy );
|
2007-05-15 23:58:49 -07:00
|
|
|
|
|
|
|
|
CEntity& operator*() const;
|
|
|
|
|
CEntity* operator->() const;
|
|
|
|
|
|
2004-05-26 13:57:25 -07:00
|
|
|
bool operator==( const HEntity& test ) const;
|
2004-10-07 12:23:35 -07:00
|
|
|
bool operator!=( const HEntity& test ) const { return( !operator==( test ) ); }
|
2007-05-15 23:58:49 -07:00
|
|
|
|
2004-07-20 12:30:35 -07:00
|
|
|
operator CEntity*() const;
|
2007-05-15 23:58:49 -07:00
|
|
|
|
|
|
|
|
// Returns true iff we are a valid handle, i.e. one to a non-deleted (but possibly destroyed) entity
|
|
|
|
|
bool IsValid() const;
|
|
|
|
|
|
|
|
|
|
// Returns true iff we are a valid handle to an entity that is not destroyed
|
|
|
|
|
bool IsAlive() const;
|
|
|
|
|
|
|
|
|
|
// Same as IsValid(); maybe this should be removed altogether to prevent confusion?
|
|
|
|
|
operator bool() const { return IsValid(); }
|
|
|
|
|
|
|
|
|
|
// Same as !IsValid()
|
|
|
|
|
bool operator!() const { return !IsValid(); };
|
2004-08-16 08:19:17 -07:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
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.
2008-05-11 11:48:32 -07:00
|
|
|
size_t GetSerializedLength() const;
|
2004-08-16 08:19:17 -07:00
|
|
|
u8 *Serialize(u8 *buffer) const;
|
|
|
|
|
const u8 *Deserialize(const u8 *buffer, const u8 *end);
|
|
|
|
|
operator CStr8() const;
|
2004-05-21 16:46:16 -07:00
|
|
|
};
|
|
|
|
|
|
2005-05-17 22:32:09 -07:00
|
|
|
/*
|
|
|
|
|
CEntityList
|
|
|
|
|
|
|
|
|
|
DESCRIPTION: Represents a group of entities that is the target/subject of
|
|
|
|
|
a network command. Use for easy serialization of one or more entity IDs.
|
|
|
|
|
|
|
|
|
|
SERIALIZED FORMAT: The entities are stored as a sequence of 16-bit ints, the
|
|
|
|
|
termination of the list marked by an integer with HANDLE_SENTINEL_BIT
|
|
|
|
|
set. The length is thus 2*(number of entities)
|
|
|
|
|
*/
|
|
|
|
|
struct CEntityList: public std::vector<HEntity>
|
|
|
|
|
{
|
|
|
|
|
// Create an empty list
|
|
|
|
|
inline CEntityList()
|
|
|
|
|
{}
|
|
|
|
|
// Create a list from an existing entity vector
|
|
|
|
|
inline CEntityList(const std::vector<HEntity> &vect):
|
|
|
|
|
std::vector<HEntity>(vect)
|
|
|
|
|
{}
|
|
|
|
|
// Create a list containing one entity
|
|
|
|
|
inline CEntityList(HEntity oneEntity)
|
|
|
|
|
{
|
|
|
|
|
push_back(oneEntity);
|
|
|
|
|
}
|
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
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.
2008-05-11 11:48:32 -07:00
|
|
|
size_t GetSerializedLength() const;
|
2005-05-17 22:32:09 -07:00
|
|
|
u8 *Serialize(u8 *buffer) const;
|
|
|
|
|
const u8 *Deserialize(const u8 *buffer, const u8 *end);
|
|
|
|
|
operator CStr8() const;
|
|
|
|
|
};
|
|
|
|
|
|
2006-12-21 06:57:13 -08:00
|
|
|
typedef CEntityList::iterator CEntityIt;
|
|
|
|
|
typedef CEntityList::const_iterator CEntityCIt;
|
|
|
|
|
|
2004-06-02 09:11:32 -07:00
|
|
|
#endif
|