UpdateMask class refactoring

This commit is contained in:
Olion 2015-03-31 01:27:20 +03:00
parent 0e963b4555
commit 7fcb0bb639
2 changed files with 163 additions and 133 deletions

View File

@ -346,41 +346,43 @@ void Object::BuildValuesUpdate(uint8 updatetype, ByteBuffer* data, UpdateMask* u
{ return; } { return; }
bool IsActivateToQuest = false; bool IsActivateToQuest = false;
bool IsPerCasterAuraState = false; //bool IsPerCasterAuraState = false;
if (updatetype == UPDATETYPE_CREATE_OBJECT || updatetype == UPDATETYPE_CREATE_OBJECT2) // this is called for UPDATETYPE_CREATE_OBJECT, UPDATETYPE_CREATE_OBJECT2, UPDATETYPE_VALUES only
{
if (isType(TYPEMASK_GAMEOBJECT) && !((GameObject*)this)->IsTransport()) if (isType(TYPEMASK_GAMEOBJECT) && !((GameObject*)this)->IsTransport())
{ {
if (((GameObject*)this)->ActivateToQuest(target) || target->isGameMaster()) IsActivateToQuest = ((GameObject*)this)->ActivateToQuest(target) || target->isGameMaster();
{ IsActivateToQuest = true; }
updateMask->SetBit(GAMEOBJECT_DYN_FLAGS);
}
}
else // case UPDATETYPE_VALUES
{
if (isType(TYPEMASK_GAMEOBJECT) && !((GameObject*)this)->IsTransport())
{
if (((GameObject*)this)->ActivateToQuest(target) || target->isGameMaster())
{ IsActivateToQuest = true; }
updateMask->SetBit(GAMEOBJECT_DYN_FLAGS); updateMask->SetBit(GAMEOBJECT_DYN_FLAGS);
if (updatetype == UPDATETYPE_VALUES)
updateMask->SetBit(GAMEOBJECT_ANIMPROGRESS); updateMask->SetBit(GAMEOBJECT_ANIMPROGRESS);
} }
}
MANGOS_ASSERT(updateMask && updateMask->GetCount() == m_valuesCount); MANGOS_ASSERT(updateMask && updateMask->GetCount() == m_valuesCount);
*data << (uint8)updateMask->GetBlockCount(); *data << (uint8)updateMask->GetBlockCount();
data->append(updateMask->GetMask(), updateMask->GetLength()); data->append(updateMask->GetMask(), updateMask->GetLength());
// checking the new bit extraction mechanic
//#ifdef _DEBUG
uint16 ix = 0;
for (uint16 i = 0; i < m_valuesCount; i++)
{
if (updateMask->GetBit(i))
{
ix = updateMask->GetNextSetIndex(ix);
if (i != ix)
sLog.outError("ERROR BuildValuesUpdate: new index %u, should be %u for object type %u entry %u", ix, i, GetTypeId(), GetEntry());
++ix;
}
}
//#endif
// 2 specialized loops for speed optimization in non-unit case // 2 specialized loops for speed optimization in non-unit case
if (isType(TYPEMASK_UNIT)) // unit (creature/player) case if (isType(TYPEMASK_UNIT)) // unit (creature/player) case
{ {
for (uint16 index = 0; index < m_valuesCount; ++index) uint16 index = 0;
{ while ((index = updateMask->GetNextSetIndex(index)) < m_valuesCount)
if (updateMask->GetBit(index))
{ {
if (index == UNIT_NPC_FLAGS) if (index == UNIT_NPC_FLAGS)
{ {
@ -461,14 +463,13 @@ void Object::BuildValuesUpdate(uint8 updatetype, ByteBuffer* data, UpdateMask* u
// send in current format (float as float, uint32 as uint32) // send in current format (float as float, uint32 as uint32)
*data << m_uint32Values[index]; *data << m_uint32Values[index];
} }
} ++index;
} }
} }
else if (isType(TYPEMASK_GAMEOBJECT)) // gameobject case else if (isType(TYPEMASK_GAMEOBJECT)) // gameobject case
{ {
for (uint16 index = 0; index < m_valuesCount; ++index) uint16 index = 0;
{ while ((index = updateMask->GetNextSetIndex(index)) < m_valuesCount)
if (updateMask->GetBit(index))
{ {
// send in current format (float as float, uint32 as uint32) // send in current format (float as float, uint32 as uint32)
if (index == GAMEOBJECT_DYN_FLAGS) if (index == GAMEOBJECT_DYN_FLAGS)
@ -495,18 +496,17 @@ void Object::BuildValuesUpdate(uint8 updatetype, ByteBuffer* data, UpdateMask* u
} }
else else
{ *data << m_uint32Values[index]; } // other cases { *data << m_uint32Values[index]; } // other cases
} ++index;
} }
} }
else // other objects case (no special index checks) else // other objects case (no special index checks)
{ {
for (uint16 index = 0; index < m_valuesCount; ++index) uint16 index = 0;
{ while ((index = updateMask->GetNextSetIndex(index)) < m_valuesCount)
if (updateMask->GetBit(index))
{ {
// send in current format (float as float, uint32 as uint32) // send in current format (float as float, uint32 as uint32)
*data << m_uint32Values[index]; *data << m_uint32Values[index];
} ++index;
} }
} }
} }

View File

@ -27,6 +27,9 @@
#include "UpdateFields.h" #include "UpdateFields.h"
#include "Errors.h" #include "Errors.h"
#ifdef WIN32
#include <intrin.h>
#endif
class UpdateMask class UpdateMask
{ {
@ -54,6 +57,20 @@ class UpdateMask
return (((uint8*)mUpdateMask)[ index >> 3 ] & (1 << (index & 0x7))) != 0; return (((uint8*)mUpdateMask)[ index >> 3 ] & (1 << (index & 0x7))) != 0;
} }
uint32 GetNextSetIndex(uint32 start) const
{
uint32 index = start;
while (index <= mCount)
{
uint32 offset = ctz(mUpdateMask[index >> 5] >> (index & 0x1F));
if (offset < (32 - (index & 0x1F)))
return index + offset;
else
index += (32 - (index & 0x1F));
}
return mCount;
}
uint32 GetBlockCount() const { return mBlocks; } uint32 GetBlockCount() const { return mBlocks; }
uint32 GetLength() const { return mBlocks << 2; } uint32 GetLength() const { return mBlocks << 2; }
uint32 GetCount() const { return mCount; } uint32 GetCount() const { return mCount; }
@ -124,5 +141,18 @@ class UpdateMask
uint32 mCount; uint32 mCount;
uint32 mBlocks; uint32 mBlocks;
uint32* mUpdateMask; uint32* mUpdateMask;
#ifdef WIN32
static uint32 __inline __builtin_ctz(uint32 x)
{
unsigned long r = 0;
_BitScanForward(&r, x);
return r;
}
#endif
static inline uint32 ctz(const uint32 x)
{
return x ? __builtin_ctz(x) : 32;
}
}; };
#endif #endif