Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
perf(gameengine): index live particle systems by id
Olcay Seygan09aeceb89d44
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 87462a1d9..f5b3df8de 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -881,6 +881,14 @@ turn, and the one after. A squad that used to leave on turn 4,120 leaves on turnis a thirtieth of a second - you cannot see it, and it was sitting still waiting for orders eitherway.+**And every burning building asking where its smoke is.** A damaged structure, a laser beam, a+beacon, a shockwave — each of them holds on to an effect by number and asks the game to hand it back,+every frame, for as long as it lives. Asking meant reading down the list of every effect alive in the+match until the right one turned up, and a late-game fight has hundreds of them alive at once while a+burning base has dozens of things doing the asking. Every effect now knows where it is, so the+question is answered in one step instead of hundreds — and an effect that dies is no longer hunted+down that same list before it can be cleared away.+## Sound, video, and getting it to start at all- **Audio is real**, through the game's own audio library. The sound SDK was stripped from the sourcediff --git a/GeneralsMD/Code/GameEngine/Include/Common/STLTypedefs.h b/GeneralsMD/Code/GameEngine/Include/Common/STLTypedefs.hindex 03539408c..8131a83e1 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/STLTypedefs.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/STLTypedefs.h@@ -67,6 +67,7 @@ class Object;enum NameKeyType;enum ObjectID;enum DrawableID;+enum ParticleSystemID;#include <algorithm>#include <bitset>@@ -188,6 +189,15 @@ namespace rts}};+ template<> struct hash<ParticleSystemID>+ {+ size_t operator()(ParticleSystemID psid) const+ {+ std::hash<UnsignedInt> tmp;+ return tmp((UnsignedInt)psid);+ }+ };+// This is the equal_to overload for char* comparisons. We compare the// strings to determine whether they are equal or not.// Other overloads should go into specific header files, not here (unlessdiff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.hindex 587c4927e..25e8b90ab 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.h@@ -62,6 +62,11 @@ enum ParticleSystemIDINVALID_PARTICLE_SYSTEM_ID = 0};+// The manager's master list of live systems. It lives out here so that a ParticleSystem can hold+// its own position in it and unlink itself in constant time instead of making the manager scan.+typedef std::list<ParticleSystem*> ParticleSystemPtrList;+typedef ParticleSystemPtrList::iterator ParticleSystemPtrListIt;+#define MAX_VOLUME_PARTICLE_DEPTH ( 16 )#define DEFAULT_VOLUME_PARTICLE_DEPTH ( 0 )//The Default is not to do the volume thing!#define OPTIMUM_VOLUME_PARTICLE_DEPTH ( 6 )@@ -718,6 +723,8 @@ protected:Particle * m_systemParticlesTail;Shadow * m_groundShadow; ///< client-only soft blob on the terrain, never xfered+ friend class ParticleSystemManager; ///< it hands us our slot in its list and takes it back+ ParticleSystemPtrListIt m_managerListIt; ///< where we sit in the manager's master list, so it need not lookUnsignedInt m_particleCount; ///< current count of particles for this systemParticleSystemID m_systemID; ///< unique id given to this system from the particle system manager@@ -780,8 +787,9 @@ class ParticleSystemManager : public SubsystemInterface,public:- typedef std::list<ParticleSystem*> ParticleSystemList;- typedef std::list<ParticleSystem*>::iterator ParticleSystemListIt;+ typedef ParticleSystemPtrList ParticleSystemList;+ typedef ParticleSystemPtrListIt ParticleSystemListIt;+ typedef std::hash_map< ParticleSystemID, ParticleSystem *, rts::hash<ParticleSystemID>, rts::equal_to<ParticleSystemID> > ParticleSystemIDMap;typedef std::hash_map<AsciiString, ParticleSystemTemplate *, rts::hash<AsciiString>, rts::equal_to<AsciiString> > TemplateMap;ParticleSystemManager( void );@@ -846,6 +854,9 @@ public:// these are only for use by partcle systems to link and unlink themselvesvoid friend_addParticleSystem( ParticleSystem *particleSystemToAdd );void friend_removeParticleSystem( ParticleSystem *particleSystemToRemove );+ // A restored system reads its saved id after the constructor has already filed it under the+ // fresh one, so it tells the manager to re-file it.+ void friend_renumberParticleSystem( ParticleSystem *particleSystem, ParticleSystemID oldID );protected:@@ -860,6 +871,7 @@ protected:ParticleSystemID m_uniqueSystemID; ///< unique system ID to assign to each system createdParticleSystemList m_allParticleSystemList;+ ParticleSystemIDMap m_allParticleSystemMap; ///< the same systems, by id, so findParticleSystem() is not a scanUnsignedInt m_particleCount;UnsignedInt m_fieldParticleCount; ///< this does not need to be xfered, since it is evaluated every framediff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cppindex fceadc7e8..37fb317ff 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp@@ -2654,7 +2654,10 @@ void ParticleSystem::xfer( Xfer *xfer )ParticleSystemInfo::xfer( xfer );// particle system ID+ const ParticleSystemID idBeforeXfer = m_systemID;xfer->xferUser( &m_systemID, sizeof( ParticleSystemID ) );+ if( m_systemID != idBeforeXfer )+ TheParticleSystemManager->friend_renumberParticleSystem( this, idBeforeXfer );// attached to drawable idxfer->xferDrawableID( &m_attachedToDrawableID );@@ -3171,6 +3174,7 @@ void ParticleSystemManager::reset( void )m_particleCount = 0;m_fieldParticleCount = 0;m_particleSystemCount = 0;+ m_allParticleSystemMap.clear();m_uniqueSystemID = INVALID_PARTICLE_SYSTEM_ID;@@ -3255,20 +3259,8 @@ ParticleSystem *ParticleSystemManager::findParticleSystem( ParticleSystemID id )if (id == INVALID_PARTICLE_SYSTEM_ID)return NULL; // my, that was easy- ParticleSystem *system = NULL;-- for( ParticleSystemListIt it = m_allParticleSystemList.begin(); it != m_allParticleSystemList.end(); ++it ) {- system = *it;- if (!system) {- continue;- }-- if( system->getSystemID() == id ) {- return system;- }- }-- return NULL;+ ParticleSystemIDMap::const_iterator it = m_allParticleSystemMap.find( id );+ return (it != m_allParticleSystemMap.end()) ? it->second : NULL;} // end findParticleSystem@@ -3435,6 +3427,8 @@ void ParticleSystemManager::removeParticle( Particle *particleToRemove)void ParticleSystemManager::friend_addParticleSystem( ParticleSystem *particleSystemToAdd ){m_allParticleSystemList.push_back(particleSystemToAdd);+ particleSystemToAdd->m_managerListIt = --m_allParticleSystemList.end();+ m_allParticleSystemMap[ particleSystemToAdd->getSystemID() ] = particleSystemToAdd;++m_particleSystemCount;}@@ -3443,12 +3437,24 @@ void ParticleSystemManager::friend_addParticleSystem( ParticleSystem *particleSy// ------------------------------------------------------------------------------------------------void ParticleSystemManager::friend_removeParticleSystem( ParticleSystem *particleSystemToRemove ){- ParticleSystemListIt it = std::find(m_allParticleSystemList.begin(), m_allParticleSystemList.end(), particleSystemToRemove);- if (it != m_allParticleSystemList.end()) {- m_allParticleSystemList.erase(it);- --m_particleSystemCount;- }+ // The constructor filed it and only the destructor calls this, so the iterator is ours and+ // still good; EA scanned the whole list for it on every single system that died.+ m_allParticleSystemList.erase( particleSystemToRemove->m_managerListIt );+ m_allParticleSystemMap.erase( particleSystemToRemove->getSystemID() );+ --m_particleSystemCount;+}++// ------------------------------------------------------------------------------------------------+/** A system loaded from a save file is constructed with a fresh id and then reads its saved one,+ * so the id it was filed under is no longer the id it answers to. Re-file it. */+// ------------------------------------------------------------------------------------------------+void ParticleSystemManager::friend_renumberParticleSystem( ParticleSystem *particleSystem, ParticleSystemID oldID )+{+ ParticleSystemIDMap::iterator it = m_allParticleSystemMap.find( oldID );+ if (it != m_allParticleSystemMap.end() && it->second == particleSystem)+ m_allParticleSystemMap.erase( it );+ m_allParticleSystemMap[ particleSystem->getSystemID() ] = particleSystem;}// ------------------------------------------------------------------------------------------------diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 0f3fb340d..da48f03fc 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -42,6 +42,7 @@#include <float.h>#include "GameClient/Water.h"#include "GameLogic/Module/PhysicsUpdate.h"+#include "GameClient/GameClient.h"#include "GameClient/ParticleSys.h"#include "GameClient/ControlBar.h"#include "GameClient/InGameUI.h"@@ -2743,6 +2744,138 @@ TEST(logicrandom_unchanged_honours_its_bounds)CHECK_EQ( GetGameLogicRandomValueRealUnchanged( 3.0f, 3.0f ), 3.0f );}+// ---------------------------------------------------------------------------------------------+// The particle system manager's bookkeeping. Finding a system by id, and unlinking a dead one,+// both used to be linear walks of every live system - on paths the renderer and the logic take+// thousands of times a frame.+// ---------------------------------------------------------------------------------------------++// A particle system stamps itself with TheGameClient's frame, so the test needs one of those too.+// The base class' constructor only zeroes counters, but its destructor tears down half the world's+// globals (the shell, the in-game UI, the campaign manager), so the stub is made once and never+// destroyed.+class TestGameClient : public GameClient+{+public:+ virtual void createRayEffectByTemplate( const Coord3D *, const Coord3D *, const ThingTemplate * ) {}+ virtual void addScorch( const Coord3D *, Real, Scorches ) {}+ virtual Drawable *friend_createDrawable( const ThingTemplate *, DrawableStatus = DRAWABLE_STATUS_NONE ) { return NULL; }+ virtual void setTeamColor( Int, Int, Int ) {}+ virtual void adjustLOD( Int ) {}+ virtual void notifyTerrainObjectMoved( Object * ) {}+ virtual Display *createGameDisplay( void ) { return NULL; }+ virtual InGameUI *createInGameUI( void ) { return NULL; }+ virtual GameWindowManager *createWindowManager( void ) { return NULL; }+ virtual FontLibrary *createFontLibrary( void ) { return NULL; }+ virtual DisplayStringManager *createDisplayStringManager( void ) { return NULL; }+ virtual VideoPlayerInterface *createVideoPlayer( void ) { return NULL; }+ virtual TerrainVisual *createTerrainVisual( void ) { return NULL; }+ virtual Keyboard *createKeyboard( void ) { return NULL; }+ virtual Mouse *createMouse( void ) { return NULL; }+ virtual SnowManager *createSnowManager( void ) { return NULL; }+ virtual void setFrameRate( Real ) {}+};++static GameClient *theTestGameClient = NULL;++static GameClient *getTestGameClient( void )+{+ if( theTestGameClient == NULL )+ theTestGameClient = new TestGameClient;+ return theTestGameClient;+}++// The real managers live in GameEngineDevice; all we need is something concrete to file systems in.+class TestParticleSystemManager : public ParticleSystemManager+{+public:+ virtual Int getOnScreenParticleCount( void ) { return 0; }+ virtual void doParticles( RenderInfoClass & ) {}+ virtual void queueParticleRender() {}+ virtual void preloadAssets( TimeOfDay ) {}+};++TEST(particlesys_find_answers_by_id_and_forgets_a_dead_system)+{+ GameClient *savedClient = TheGameClient;+ TheGameClient = getTestGameClient();+ ParticleSystemManager *savedManager = TheParticleSystemManager;++ {+ TestParticleSystemManager mgr;+ TheParticleSystemManager = &mgr;++ ParticleSystemTemplate *tmpl = mgr.newTemplate( AsciiString( "TestParticleSystem" ) );+ CHECK( tmpl != NULL );++ ParticleSystem *a = mgr.createParticleSystem( tmpl, FALSE );+ ParticleSystem *b = mgr.createParticleSystem( tmpl, FALSE );+ ParticleSystem *c = mgr.createParticleSystem( tmpl, FALSE );+ CHECK( a != NULL && b != NULL && c != NULL );++ const ParticleSystemID idA = a->getSystemID();+ const ParticleSystemID idB = b->getSystemID();+ const ParticleSystemID idC = c->getSystemID();+ CHECK( idA != idB && idB != idC && idA != idC );++ CHECK( mgr.findParticleSystem( idA ) == a );+ CHECK( mgr.findParticleSystem( idB ) == b );+ CHECK( mgr.findParticleSystem( idC ) == c );+ CHECK( mgr.findParticleSystem( (ParticleSystemID)0x7fffffff ) == NULL );+ CHECK( mgr.findParticleSystem( INVALID_PARTICLE_SYSTEM_ID ) == NULL );+ CHECK_EQ( mgr.getParticleSystemCount(), 3 );++ // killing one out of the middle must not disturb the other two, and its id must stop resolving+ b->deleteInstance();+ CHECK( mgr.findParticleSystem( idB ) == NULL );+ CHECK( mgr.findParticleSystem( idA ) == a );+ CHECK( mgr.findParticleSystem( idC ) == c );+ CHECK_EQ( mgr.getParticleSystemCount(), 2 );++ a->deleteInstance();+ c->deleteInstance();+ CHECK_EQ( mgr.getParticleSystemCount(), 0 );+ CHECK( mgr.findParticleSystem( idA ) == NULL );+ CHECK( mgr.findParticleSystem( idC ) == NULL );+ }++ TheParticleSystemManager = savedManager;+ TheGameClient = savedClient;+}++TEST(particlesys_reset_lets_the_ids_start_over_without_ghosts)+{+ GameClient *savedClient = TheGameClient;+ TheGameClient = getTestGameClient();+ ParticleSystemManager *savedManager = TheParticleSystemManager;++ {+ TestParticleSystemManager mgr;+ TheParticleSystemManager = &mgr;++ ParticleSystemTemplate *tmpl = mgr.newTemplate( AsciiString( "TestParticleSystem" ) );+ CHECK( tmpl != NULL );++ for( Int i = 0; i < 8; ++i )+ CHECK( mgr.createParticleSystem( tmpl, FALSE ) != NULL );+ CHECK_EQ( mgr.getParticleSystemCount(), 8 );++ mgr.reset();+ CHECK_EQ( mgr.getParticleSystemCount(), 0 );++ // reset rewinds the id counter, so an entry left behind would answer for a brand new system+ ParticleSystem *fresh = mgr.createParticleSystem( tmpl, FALSE );+ CHECK( fresh != NULL );+ CHECK( mgr.findParticleSystem( fresh->getSystemID() ) == fresh );++ mgr.reset();+ CHECK_EQ( mgr.getParticleSystemCount(), 0 );+ }++ TheParticleSystemManager = savedManager;+ TheGameClient = savedClient;+}+// ---------------------------------------------------------------------------------------------// Per-game state that a reused Player has to give back.// ---------------------------------------------------------------------------------------------