Skip to content

Commit f3d7b54

Browse files
authored
Revert "[ZH] Use hash maps for Object and Drawable lookups in GameClient, GameLogic like Generals does (#676)" (#735)
This reverts commit 4fe88de
1 parent 1422e6f commit f3d7b54

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ class SnowManager;
5959

6060
/// Function pointers for use by GameClient callback functions.
6161
typedef void (*GameClientFuncPtr)( Drawable *draw, void *userData );
62-
typedef std::hash_map<DrawableID, Drawable *, rts::hash<DrawableID>, rts::equal_to<DrawableID> > DrawablePtrHash;
63-
typedef DrawablePtrHash::iterator DrawablePtrHashIt;
62+
//typedef std::hash_map<DrawableID, Drawable *, rts::hash<DrawableID>, rts::equal_to<DrawableID> > DrawablePtrHash;
63+
//typedef DrawablePtrHash::iterator DrawablePtrHashIt;
64+
65+
typedef std::vector<Drawable*> DrawablePtrVector;
6466

6567
//-----------------------------------------------------------------------------
6668
/** The Client message dispatcher, this is the last "translator" on the message
@@ -163,7 +165,8 @@ class GameClient : public SubsystemInterface,
163165
UnsignedInt m_frame; ///< Simulation frame number from server
164166

165167
Drawable *m_drawableList; ///< All of the drawables in the world
166-
DrawablePtrHash m_drawableHash; ///< Used for DrawableID lookups
168+
// DrawablePtrHash m_drawableHash; ///< Used for DrawableID lookups
169+
DrawablePtrVector m_drawableVector;
167170

168171
DrawableID m_nextDrawableID; ///< For allocating drawable id's
169172
DrawableID allocDrawableID( void ); ///< Returns a new unique drawable id
@@ -238,13 +241,18 @@ inline Drawable* GameClient::findDrawableByID( const DrawableID id )
238241
if( id == INVALID_DRAWABLE_ID )
239242
return NULL;
240243

241-
DrawablePtrHashIt it = m_drawableHash.find(id);
242-
if (it == m_drawableHash.end()) {
243-
// no such drawable
244-
return NULL;
245-
}
244+
// DrawablePtrHashIt it = m_drawableHash.find(id);
245+
// if (it == m_drawableHash.end()) {
246+
// // no such drawable
247+
// return NULL;
248+
// }
249+
//
250+
// return (*it).second;
251+
252+
if( (size_t)id < m_drawableVector.size() )
253+
return m_drawableVector[(size_t)id];
246254

247-
return (*it).second;
255+
return NULL;
248256
}
249257

250258

GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ enum
9292

9393
/// Function pointers for use by GameLogic callback functions.
9494
typedef void (*GameLogicFuncPtr)( Object *obj, void *userData );
95-
typedef std::hash_map<ObjectID, Object *, rts::hash<ObjectID>, rts::equal_to<ObjectID> > ObjectPtrHash;
96-
typedef ObjectPtrHash::const_iterator ObjectPtrIter;
95+
//typedef std::hash_map<ObjectID, Object *, rts::hash<ObjectID>, rts::equal_to<ObjectID> > ObjectPtrHash;
96+
//typedef ObjectPtrHash::const_iterator ObjectPtrIter;
9797

98+
typedef std::vector<Object*> ObjectPtrVector;
9899

99100
// ------------------------------------------------------------------------------------------------
100101
/**
@@ -319,7 +320,8 @@ class GameLogic : public SubsystemInterface, public Snapshot
319320
WindowLayout *m_background;
320321

321322
Object* m_objList; ///< All of the objects in the world.
322-
ObjectPtrHash m_objHash; ///< Used for ObjectID lookups
323+
// ObjectPtrHash m_objHash; ///< Used for ObjectID lookups
324+
ObjectPtrVector m_objVector;
323325

324326
// this is a vector, but is maintained as a priority queue.
325327
// never modify it directly; please use the proper access methods.
@@ -410,11 +412,15 @@ inline Object* GameLogic::findObjectByID( ObjectID id )
410412
if( id == INVALID_ID )
411413
return NULL;
412414

413-
ObjectPtrHash::iterator it = m_objHash.find(id);
414-
if (it == m_objHash.end())
415-
return NULL;
415+
// ObjectPtrHash::iterator it = m_objHash.find(id);
416+
// if (it == m_objHash.end())
417+
// return NULL;
418+
//
419+
// return (*it).second;
420+
if( (size_t)id < m_objVector.size() )
421+
return m_objVector[(size_t)id];
416422

417-
return (*it).second;
423+
return NULL;
418424
}
419425

420426

GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,11 @@ void GameClient::init( void )
450450
void GameClient::reset( void )
451451
{
452452
Drawable *draw, *nextDraw;
453-
m_drawableHash.clear();
454-
#if USING_STLPORT
455-
m_drawableHash.resize(DRAWABLE_HASH_SIZE);
456-
#else
457-
m_drawableHash.reserve(DRAWABLE_HASH_SIZE);
458-
#endif
453+
// m_drawableHash.clear();
454+
// m_drawableHash.resize(DRAWABLE_HASH_SIZE);
455+
456+
m_drawableVector.clear();
457+
m_drawableVector.resize(DRAWABLE_HASH_SIZE, NULL);
459458

460459
// need to reset the in game UI to clear drawables before they are destroyed
461460
TheInGameUI->reset();
@@ -863,7 +862,12 @@ void GameClient::addDrawableToLookupTable(Drawable *draw )
863862
return;
864863

865864
// add to lookup
866-
m_drawableHash[ draw->getID() ] = draw;
865+
// m_drawableHash[ draw->getID() ] = draw;
866+
DrawableID newID = draw->getID();
867+
while( newID >= m_drawableVector.size() ) // Fail case is hella rare, so faster to double up on size() call
868+
m_drawableVector.resize(m_drawableVector.size() * 2, NULL);
869+
870+
m_drawableVector[ newID ] = draw;
867871

868872
} // end addDrawableToLookupTable
869873

@@ -878,7 +882,8 @@ void GameClient::removeDrawableFromLookupTable( Drawable *draw )
878882
return;
879883

880884
// remove from table
881-
m_drawableHash.erase( draw->getID() );
885+
// m_drawableHash.erase( draw->getID() );
886+
m_drawableVector[ draw->getID() ] = NULL;
882887

883888
} // end removeDrawableFromLookupTable
884889

GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,11 @@ void GameLogic::reset( void )
441441
destroyAllObjectsImmediate();
442442

443443
// set the hash to be rather large. We need to optimize this value later.
444-
m_objHash.clear();
445-
#if USING_STLPORT
446-
m_objHash.resize(OBJ_HASH_SIZE);
447-
#else
448-
m_objHash.reserve(OBJ_HASH_SIZE);
449-
#endif
444+
// m_objHash.clear();
445+
// m_objHash.resize(OBJ_HASH_SIZE);
446+
m_objVector.clear();
447+
m_objVector.resize(OBJ_HASH_SIZE, NULL);
448+
450449
m_gamePaused = FALSE;
451450
m_inputEnabledMemory = TRUE;
452451
m_mouseVisibleMemory = TRUE;
@@ -3868,7 +3867,12 @@ void GameLogic::addObjectToLookupTable( Object *obj )
38683867
return;
38693868

38703869
// add to lookup
3871-
m_objHash[ obj->getID() ] = obj;
3870+
// m_objHash[ obj->getID() ] = obj;
3871+
ObjectID newID = obj->getID();
3872+
while( newID >= m_objVector.size() ) // Fail case is hella rare, so faster to double up on size() call
3873+
m_objVector.resize(m_objVector.size() * 2, NULL);
3874+
3875+
m_objVector[ newID ] = obj;
38723876

38733877
} // end addObjectToLookupTable
38743878

@@ -3883,7 +3887,8 @@ void GameLogic::removeObjectFromLookupTable( Object *obj )
38833887
return;
38843888

38853889
// remove from lookup table
3886-
m_objHash.erase( obj->getID() );
3890+
// m_objHash.erase( obj->getID() );
3891+
m_objVector[ obj->getID() ] = NULL;
38873892

38883893
} // end removeObjectFromLookupTable
38893894

0 commit comments

Comments
 (0)