Skip to content

Commit

Permalink
Refactor|Map|Client: Moved Map's ClMobjHash to the private Instance
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jan 10, 2014
1 parent 5ddb906 commit 768ec6d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 75 deletions.
7 changes: 3 additions & 4 deletions doomsday/client/include/client/cl_mobj.h
Expand Up @@ -24,14 +24,13 @@
#include "world/p_object.h"

/**
* Client mobjs are stored into a hash for fast lookup by it's identifier.
*
* @ingroup world
*/
class ClMobjHash
{
public:
/**
* The client mobjs are stored into a hash for quickly locating a ClMobj by its identifier.
*/
static int const SIZE = 256;

public:
Expand Down Expand Up @@ -69,7 +68,7 @@ class ClMobjHash
*
* @return @c 0 if all callbacks return @c 0; otherwise the result of the last.
*/
int iterator(int (*callback) (struct mobj_s *, void *), void *context = 0);
int iterate(int (*callback) (struct mobj_s *, void *), void *context = 0);

#ifdef DENG_DEBUG
void assertValid();
Expand Down
26 changes: 5 additions & 21 deletions doomsday/client/include/world/map.h
Expand Up @@ -140,10 +140,6 @@ class Map
#endif

public: /// @todo make private:
#ifdef __CLIENT__
ClMobjHash clMobjHash;
#endif

coord_t _globalGravity; // The defined gravity for this map.
coord_t _effectiveGravity; // The effective gravity for this map.

Expand Down Expand Up @@ -624,29 +620,12 @@ class Map
*/
int toIndex(BiasSource const &source) const;

/**
* To be called when the client is shut down.
* @todo Should be private?
*/
void clearClMobjs();

/**
* Deletes hidden, unpredictable or nulled mobjs for which we have not received
* updates in a while.
*/
void expireClMobjs();

/**
* Iterate the client mobj hash, exec the callback on each. Abort if callback
* returns non-zero.
*
* @param callback Function to callback for each client mobj.
* @param context Data pointer passed to the callback.
*
* @return @c 0 if all callbacks return @c 0; otherwise the result of the last.
*/
int clMobjIterator(int (*callback) (struct mobj_s *, void *), void *context);

void clearClMovers();

/**
Expand Down Expand Up @@ -781,6 +760,11 @@ class Map
* Initialize the map object => BSP leaf "contact" blockmaps.
*/
void initContactBlockmaps();

/**
* Provides access to the client mobj hash.
*/
ClMobjHash &clMobjHash();
#endif // __CLIENT__

public:
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/client/cl_main.cpp
Expand Up @@ -93,7 +93,7 @@ void Cl_CleanUp()
if(App_World().hasMap())
{
Cl_ResetFrame();
App_World().map().clearClMobjs();
App_World().map().clMobjHash().clear();
}

Cl_InitPlayers();
Expand Down
82 changes: 34 additions & 48 deletions doomsday/client/src/client/cl_mobj.cpp
Expand Up @@ -103,7 +103,7 @@ void ClMobjHash::insert(mobj_t *mo, thid_t id)

void ClMobjHash::remove(mobj_t *mo)
{
Bucket &chain = bucketFor(mo->thinker.id);
Bucket &bucket = bucketFor(mo->thinker.id);
clmoinfo_t *info = ClMobj_GetInfo(mo);

CL_ASSERT_CLMOBJ(mo);
Expand All @@ -112,10 +112,10 @@ void ClMobjHash::remove(mobj_t *mo)
assertValid();
#endif

if(chain.first == info)
chain.first = info->next;
if(chain.last == info)
chain.last = info->prev;
if(bucket.first == info)
bucket.first = info->next;
if(bucket.last == info)
bucket.last = info->prev;
if(info->next)
info->next->prev = info->prev;
if(info->prev)
Expand Down Expand Up @@ -143,7 +143,7 @@ mobj_t *ClMobjHash::find(thid_t id) const
return 0;
}

int ClMobjHash::iterator(int (*callback) (struct mobj_s *, void *), void *context)
int ClMobjHash::iterate(int (*callback) (struct mobj_s *, void *), void *context)
{
for(int i = 0; i < SIZE; ++i)
{
Expand Down Expand Up @@ -186,17 +186,6 @@ mobj_t *ClMobj_MobjForInfo(clmoinfo_t *info)
return (mobj_t *) ((char *)info + sizeof(clmoinfo_t));
}

#undef ClMobj_Find
struct mobj_s *ClMobj_Find(thid_t id)
{
return App_World().map().clMobjHash.find(id);
}

int Map::clMobjIterator(int (*callback) (mobj_t *, void *), void *context)
{
return clMobjHash.iterator(callback, context);
}

void ClMobj_Unlink(mobj_t *mo)
{
Mobj_Unlink(mo);
Expand Down Expand Up @@ -344,11 +333,6 @@ void Cl_UpdateRealPlayerMobj(mobj_t *localMobj, mobj_t *remoteClientMobj,
}
}

void Map::clearClMobjs()
{
clMobjHash.clear();
}

/// @return @c false= Continue iteration.
static int expireClMobjsWorker(mobj_t *mo, void *context)
{
Expand Down Expand Up @@ -390,7 +374,7 @@ static int expireClMobjsWorker(mobj_t *mo, void *context)
void Map::expireClMobjs()
{
uint nowTime = Timer_RealMilliseconds();
clMobjHash.iterator(expireClMobjsWorker, &nowTime);
clMobjHash().iterate(expireClMobjsWorker, &nowTime);
}

mobj_t *ClMobj_Create(thid_t id)
Expand All @@ -409,7 +393,7 @@ mobj_t *ClMobj_Create(thid_t id)
info->endMagic = CLM_MAGIC2;
mo->ddFlags = DDMF_REMOTE;

map.clMobjHash.insert(mo, id);
map.clMobjHash().insert(mo, id);
map.thinkers().setMobjId(id); // Mark this ID as used.

// Client mobjs are full-fludged game mobjs as well.
Expand All @@ -428,7 +412,7 @@ void ClMobj_Destroy(mobj_t *mo)
Map &map = App_World().map();

#ifdef DENG_DEBUG
map.clMobjHash.assertValid();
map.clMobjHash().assertValid();
#endif

CL_ASSERT_CLMOBJ(mo);
Expand All @@ -439,14 +423,14 @@ void ClMobj_Destroy(mobj_t *mo)

// The ID is free once again.
map.thinkers().setMobjId(mo->thinker.id, false);
map.clMobjHash.remove(mo);
map.clMobjHash().remove(mo);
ClMobj_Unlink(mo); // from block/sector

// This will free the entire mobj + info.
Z_Free(info);

#ifdef DENG_DEBUG
map.clMobjHash.assertValid();
map.clMobjHash().assertValid();
#endif
}

Expand Down Expand Up @@ -487,7 +471,7 @@ clmoinfo_t *ClMobj_GetInfo(mobj_t *mo)

boolean ClMobj_Reveal(mobj_t *mo)
{
LOG_AS("Cl_RevealMobj");
LOG_AS("ClMobj_Reveal");

clmoinfo_t *info = ClMobj_GetInfo(mo);

Expand Down Expand Up @@ -562,17 +546,11 @@ static boolean ClMobj_IsStuckInsideLocalPlayer(mobj_t *mo)

void ClMobj_ReadDelta()
{
bool needsLinking = false, justCreated = false;
clmoinfo_t *info = 0;
mobj_t *mo = 0;
mobj_t *d;
short mom;
thid_t id = Reader_ReadUInt16(msgReader); // Read the ID.
bool onFloor = false;
mobj_t oldState;
/// @todo Do not assume the CURRENT map.
Map &map = App_World().map();

// Flags.
int df = Reader_ReadUInt16(msgReader);
thid_t const id = Reader_ReadUInt16(msgReader); // Read the ID.
int const df = Reader_ReadUInt16(msgReader); // Flags.

// More flags?
byte moreFlags = 0, fastMom = false;
Expand All @@ -589,8 +567,9 @@ void ClMobj_ReadDelta()
<< id << df << moreFlags;

// Get a mobj for this.
mo = ClMobj_Find(id);
info = ClMobj_GetInfo(mo);
mobj_t *mo = map.clMobjHash().find(id);
clmoinfo_t *info = ClMobj_GetInfo(mo);
bool needsLinking = false, justCreated = false;
if(!mo)
{
LOG_NET_XVERBOSE("Creating new clmobj %i (hidden)") << id;
Expand All @@ -615,7 +594,7 @@ void ClMobj_ReadDelta()
info->time = Timer_RealMilliseconds();
}

d = mo;
mobj_t *d = mo;

/*if(d->dPlayer && d->dPlayer == &ddPlayers[consolePlayer])
{
Expand All @@ -632,7 +611,8 @@ void ClMobj_ReadDelta()
}

// Remember where the mobj used to be in case we need to cancel a move.
zap(oldState);
mobj_t oldState; zap(oldState);
bool onFloor = false;

// Coordinates with three bytes.
if(df & MDF_ORIGIN_X)
Expand Down Expand Up @@ -680,17 +660,17 @@ void ClMobj_ReadDelta()
// Momentum using 8.8 fixed point.
if(df & MDF_MOM_X)
{
mom = Reader_ReadInt16(msgReader);
short mom = Reader_ReadInt16(msgReader);
d->mom[MX] = FIX2FLT(fastMom? UNFIXED10_6(mom) : UNFIXED8_8(mom));
}
if(df & MDF_MOM_Y)
{
mom = Reader_ReadInt16(msgReader);
short mom = Reader_ReadInt16(msgReader);
d->mom[MY] = FIX2FLT(fastMom ? UNFIXED10_6(mom) : UNFIXED8_8(mom));
}
if(df & MDF_MOM_Z)
{
mom = Reader_ReadInt16(msgReader);
short mom = Reader_ReadInt16(msgReader);
d->mom[MZ] = FIX2FLT(fastMom ? UNFIXED10_6(mom) : UNFIXED8_8(mom));
}

Expand Down Expand Up @@ -813,7 +793,7 @@ void ClMobj_ReadDelta()

void ClMobj_ReadNullDelta()
{
LOG_AS("Cl_ReadNullMobjDelta2");
LOG_AS("ClMobj_ReadNullDelta");

/// @todo Do not assume the CURRENT map.
Map &map = App_World().map();
Expand All @@ -822,7 +802,7 @@ void ClMobj_ReadNullDelta()
thid_t id = Reader_ReadUInt16(msgReader);
LOGDEV_NET_XVERBOSE("Null %i") << id;

mobj_t *mo = ClMobj_Find(id);
mobj_t *mo = map.clMobjHash().find(id);
if(!mo)
{
// Wasted bandwidth...
Expand Down Expand Up @@ -851,10 +831,16 @@ void ClMobj_ReadNullDelta()
info->flags |= CLMF_UNPREDICTABLE | CLMF_NULLED;

#ifdef DENG_DEBUG
map.clMobjHash.assertValid();
map.clMobjHash().assertValid();
#endif
}

#undef ClMobj_Find
struct mobj_s *ClMobj_Find(thid_t id)
{
return App_World().map().clMobjHash().find(id);
}

// cl_player.c
DENG_EXTERN_C struct mobj_s* ClPlayer_ClMobj(int plrNum);

Expand Down
7 changes: 7 additions & 0 deletions doomsday/client/src/world/map.cpp
Expand Up @@ -199,6 +199,8 @@ DENG2_OBSERVES(bsp::Partitioner, UnclosedSectorFound)
coord_t skyFloorHeight;
coord_t skyCeilingHeight;

ClMobjHash clMobjHash;

typedef QList<ClPlaneMover *> ClPlaneMovers;
ClPlaneMovers clPlaneMovers;

Expand Down Expand Up @@ -1303,6 +1305,11 @@ void Map::spreadAllContacts(AABoxd const &region)
region.maxX + Lumobj::radiusMax(), region.maxY + Lumobj::radiusMax()));
}

ClMobjHash &Map::clMobjHash()
{
return d->clMobjHash;
}

#endif // __CLIENT__

Uri const &Map::uri() const
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/world/p_particle.cpp
Expand Up @@ -1267,7 +1267,7 @@ void P_PtcGenThinker(ptcgen_t *gen)
// Client's should also check the client mobjs.
if(isClient)
{
App_World().map().clMobjIterator(PIT_ClientMobjParticles, gen);
App_World().map().clMobjHash().iterate(PIT_ClientMobjParticles, gen);
}
#endif
App_World().map().thinkers()
Expand Down

0 comments on commit 768ec6d

Please sign in to comment.