Changed: #1469 Changed the entityorposition type to support PreviousPos and ReturnPos and to optimize serialization. Position and Entity variables are not in union because CVector has the copy constructor

--HG--
branch : gsoc2012-fabien
hg/feature/gsoc2012-fabien
Fabien_HENON 12 years ago
parent 2e50568194
commit 716a7aedc7

@ -27,7 +27,7 @@ namespace NLMISC
/// Function that serializes durations (in seconds)
/// Duration is converted to 1/100 second and cannot be greater than 2^12-1 = 4095
/// Result is serialized in 12 bits
void serialDuration(NLMISC::CBitMemStream &f, float& duration)
inline void serialDuration(NLMISC::CBitMemStream &f, float& duration)
{
static uint32 maxVal = (uint32)pow((float)2, 12) - 1;
@ -52,7 +52,7 @@ void serialDuration(NLMISC::CBitMemStream &f, float& duration)
/// Function that serializes distances (in meters)
/// Distance is converted to centimeters and cannot be greater than 2^16-1 = 65535
/// Result is serialized in 2 bytes
void serialDistance(NLMISC::CBitMemStream &f, float& distance)
inline void serialDistance(NLMISC::CBitMemStream &f, float& distance)
{
static uint32 maxVal = (uint32)pow((float)2, 16) - 1;
@ -77,7 +77,7 @@ void serialDistance(NLMISC::CBitMemStream &f, float& distance)
/// Function that serializes speeds (in m/s)
/// Speed is converted to cm/s and cannot be greater than 2^12-1 = 4095
/// Result is serialized in 12 bits
void serialSpeed(NLMISC::CBitMemStream &f, float& speed)
inline void serialSpeed(NLMISC::CBitMemStream &f, float& speed)
{
static uint32 maxVal = (uint32)pow((float)2, 12) - 1;
@ -99,6 +99,37 @@ void serialSpeed(NLMISC::CBitMemStream &f, float& speed)
}
}
/// Serializes the difference between 2 positions
/// The difference is converted in cm and cannot be greater than 2^15-1 = 32767
/// Result is serialized in 3 x 2 bytes = 6 bytes
inline void serialPositionDifference(NLMISC::CBitMemStream &f, NLMISC::CVector& diffPos)
{
if (f.isReading())
{
sint16 x = 0;
sint16 y = 0;
sint16 z = 0;
f.serial(x);
f.serial(y);
f.serial(z);
diffPos.x = (float)(x) / 100.f;
diffPos.y = (float)(y) / 100.f;
diffPos.z = (float)(z) / 100.f;
}
else
{
sint16 x = (sint16)(diffPos.x * 100.f);
sint16 y = (sint16)(diffPos.y * 100.f);
sint16 z = (sint16)(diffPos.z * 100.f);
f.serial(x);
f.serial(y);
f.serial(z);
}
}
}
#endif /* RY_CAMERAANIMTYPEPARSER_H */

@ -20,6 +20,7 @@
#include "nel/misc/entity_id.h"
#include "nel/misc/vector.h"
#include "nel/misc/stream.h"
#include "game_share/camera_anim_type_parser.h"
//////////////////////////////////////////////////////////////////////////
/************************************************************************/
@ -28,26 +29,40 @@
class TPositionOrEntity
{
public:
enum PositionOrEntityType
{
EUnknown = -1,
EPosition = 0,
EEntity,
EPreviousPos,
EReturnPos
};
TPositionOrEntity()
{
_isPosition = -1;
_type = EUnknown;
}
TPositionOrEntity(const NLMISC::CVector& position)
{
_isPosition = 1;
_type = EPosition;
Position = position;
}
TPositionOrEntity(const TDataSetIndex& eid)
{
_isPosition = 0;
_type = EEntity;
EntityId = eid;
}
TPositionOrEntity(PositionOrEntityType type)
{
_type = type;
}
TPositionOrEntity(const TPositionOrEntity& c)
{
_isPosition = c._isPosition;
_type = c._type;
if (c.isPosition())
Position = c.getPosition();
else
@ -56,7 +71,7 @@ public:
TPositionOrEntity& operator=(const TPositionOrEntity& c)
{
_isPosition = c._isPosition;
_type = c._type;
if (c.isPosition())
Position = c.getPosition();
else if (c.isEntityId())
@ -67,7 +82,7 @@ public:
bool operator==(const TPositionOrEntity& c)
{
if (_isPosition != c._isPosition)
if (_type != c._type)
return false;
if (isPosition())
return Position == c.getPosition();
@ -78,29 +93,44 @@ public:
void setPosition(const NLMISC::CVector& position)
{
_isPosition = 1;
_type = EPosition;
Position = position;
}
void setEntityId(const TDataSetIndex& eid)
{
_isPosition = 0;
_type = EEntity;
EntityId = eid;
}
void setPreviousPos()
{
_type = EPreviousPos;
}
void setReturnPos()
{
_type = EReturnPos;
}
bool isPosition() const
{
return _isPosition == 1;
return _type == EPosition;
}
bool isEntityId() const
{
return _isPosition == 0;
return _type == EEntity;
}
bool isPreviousPos() const
{
return false;
return _type == EPreviousPos;
}
bool isReturnPos() const
{
return _type == EReturnPos;
}
NLMISC::CVector getPosition() const
@ -110,6 +140,16 @@ public:
return Position;
}
NLMISC::CVector getDiffPos() const
{
return Position; // TODO => get the real position of the character
}
NLMISC::CVector setPositionFromDiffPos(const NLMISC::CVector& diffPos)
{
// TODO => set the position from the character's position and the diffpos
}
TDataSetIndex getEntityId() const
{
if (!isEntityId())
@ -119,20 +159,42 @@ public:
bool isValid() const
{
return isPosition() || isEntityId();
return _type != EUnknown;
}
void serial(NLMISC::CBitMemStream &f)
{
f.serial(_isPosition);
if (f.isReading())
{
uint32 t = 0;
f.serial(t, 2);
_type = (PositionOrEntityType)t;
}
else
{
uint32 t = (uint32)_type;
f.serial(t, 2);
}
if (isPosition())
f.serial(Position);
{
if (f.isReading())
{
NLMISC::CVector diffPos = NLMISC::CVector();
NLMISC::serialPositionDifference(f, diffPos);
setPositionFromDiffPos(diffPos);
}
else
{
NLMISC::CVector diffPos = getDiffPos();
NLMISC::serialPositionDifference(f, diffPos);
}
}
else if (isEntityId())
f.serial(EntityId);
}
private:
char _isPosition;
PositionOrEntityType _type;
NLMISC::CVector Position;
TDataSetIndex EntityId;
};

Loading…
Cancel
Save