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
refactor(gameclient): keep the drawable list still through the tick
Olcay Seyganf203144ca8ac
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h b/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.hindex e07145795..becd68413 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h@@ -449,7 +449,8 @@ public:static void flushPendingClientWork(); ///< after each tick, on the main thread: the transforms and model conditions the tick deferred/** LOGIC-THREAD.md phase 2, M4: inside a tick, past the map load, a new drawable is a shell (ID,Thing, condition bits and its modules, everything logic reads) whose render objects are built- after the tick, and a destroyed one leaves the client at once but is deleted after the tick. */+ after the tick, and a destroyed one is deleted after the tick. Phase 3: both join and leave the+ client's list and lookup after the tick too (GameClient::linkDrawablesOfTick). */static Bool tickDefersLifetime();void flushPendingTransform( Bool onDemand ); ///< tells the draw modules of a transform the tick deferred; GameClient::destroyDrawable/** Where a doClientWork goes among the model condition states the tick has not applied yet. */@@ -796,7 +797,6 @@ private:UnsignedInt m_timeToFade; ///< how slowly am I fadingUnsignedInt m_shroudClearFrame; ///< Last frame the local player saw this drawable "OBJECTSHROUD_CLEAR"-DrawableLocoInfo* m_locoInfo; // lazily allocatedDynamicAudioEventRTS* m_ambientSound; ///< sound module for ambient sound (lazily allocated)diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.h b/GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.hindex 44d352b38..bf2a03392 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.h@@ -150,6 +150,7 @@ public:virtual Drawable *friend_createDrawable( const ThingTemplate *thing, DrawableStatus statusBits = DRAWABLE_STATUS_NONE ) = 0;virtual void destroyDrawable( Drawable *draw ); ///< Destroy the given drawablevoid flushDestroyedDrawables( void ); ///< delete what destroyDrawable left for after the tick (Drawable::tickDefersLifetime)+ void linkDrawablesOfTick( void ); ///< after the tick: what it destroyed leaves the list and the lookup, what it made joins themvirtual void setTimeOfDay( TimeOfDay tod ); ///< Tell all the drawables what time of day it is now@@ -188,7 +189,8 @@ protected:Drawable *m_drawableList; ///< All of the drawables in the world// DrawablePtrHash m_drawableHash; ///< Used for DrawableID lookupsDrawablePtrVector m_drawableVector;- std::vector<Drawable*> m_destroyedInTick; ///< out of the list and the lookup, deleted by flushDestroyedDrawables+ std::vector<Drawable*> m_destroyedInTick; ///< destroyed inside a tick: unlinked by linkDrawablesOfTick, deleted by flushDestroyedDrawables+ std::vector<Drawable*> m_madeInTick; ///< made inside a tick: neither in the list nor in the lookup until linkDrawablesOfTickDrawableID m_nextDrawableID; ///< For allocating drawable id'sDrawableID allocDrawableID( void ); ///< Returns a new unique drawable id@@ -269,30 +271,6 @@ private:} \} while (0);-/** ------------------------------------------------------------------------------------------------ * Given an object id, return the associated object.- * This method is the primary interface for accessing objects, and should be used- * instead of pointers to "attach" objects to each other.- */-inline Drawable* GameClient::findDrawableByID( const DrawableID id )-{- LOGIC_CENSUS( CENSUS_GAMECLIENT );- if( id == INVALID_DRAWABLE_ID )- return NULL;--// DrawablePtrHashIt it = m_drawableHash.find(id);-// if (it == m_drawableHash.end()) {-// // no such drawable-// return NULL;-// }-//-// return (*it).second;-- if( (Int)id < m_drawableVector.size() )- return m_drawableVector[(Int)id];-- return NULL;-}// the singletondiff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cppindex d67f074c8..b95f3b928 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp@@ -139,6 +139,9 @@ namespace// M7: the frame the tick ran, which it used to hand the client itself; a tick that started a map// ran frame 0 after the load, one that cleared the game left 0TheGameClient->setFrame( TheGameLogic->getFrameForClient() );+ // phase 3: the client's list and lookup take what the tick made and destroyed, before anything+ // below looks a drawable up+ TheGameClient->linkDrawablesOfTick();Drawable::flushPendingClientWork();runViewCalls();// an effect queues nothing more: none of them runs inside a tickdiff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cppindex 05be44e60..742b22cb4 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp@@ -31,6 +31,7 @@// SYSTEM INCLUDES ////////////////////////////////////////////////////////////#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine+#include <algorithm>#include "Platform/SleepMilliseconds.h"#include "Lib/Clock.h"#include "GameClient/GameClient.h"@@ -560,6 +561,13 @@ void GameClient::reset( void )nextDraw = draw->getNextDrawable();destroyDrawable( draw );}+ // the clear-game tick resets from inside the tick: what it made before that was never linked+ for( size_t i = 0; i < m_madeInTick.size(); ++i )+ {+ if( std::find( m_destroyedInTick.begin(), m_destroyedInTick.end(), m_madeInTick[ i ] ) == m_destroyedInTick.end() )+ destroyDrawable( m_madeInTick[ i ] );+ }+ m_madeInTick.clear();m_drawableList = NULL;// the clear-game tick resets from inside the tick, and what it and the ticks before left goes// before the scene does@@ -592,12 +600,18 @@ DrawableID GameClient::allocDrawableID( void )/** -----------------------------------------------------------------------------------------------* Given a drawable, register it with the GameClient and give it a unique ID.*/-void GameClient::registerDrawable( Drawable *draw )+void GameClient::registerDrawable( Drawable *draw ){// assign this drawable a unique ID, this will add it to the fast lookup table toodraw->setID( allocDrawableID() );+ // LOGIC-THREAD.md phase 3: the list and the lookup do not change inside a tick, which the render+ // will run beside. What a tick makes waits in m_madeInTick (addDrawableToLookupTable put it there)+ // for linkDrawablesOfTick, and only the tick's own lookups find it before that.+ if( Drawable::tickDefersLifetime() )+ return;+// add the drawable to the master listdraw->prependToList( &m_drawableList );@@ -1027,9 +1041,6 @@ void GameClient::destroyDrawable( Drawable *draw )// remove any notion of the Drawable in the in-game user interfaceTheInGameUI->disregardDrawable( draw );- // remove from the master list- draw->removeFromList(&m_drawableList);-//// because drawables and objects are tightly coupled, not only MUST we maintain// our links in all instances, but it is NECESSARY for the client to actually@@ -1044,18 +1055,22 @@ void GameClient::destroyDrawable( Drawable *draw )} // end if- /* LOGIC-THREAD.md phase 2, M4. Inside a tick the drawable leaves the selection, the list and the- lookup at once, so nothing finds it, and forgets its object, which the tick may delete before- the flush. Its draw modules, render objects, shadows and sounds go after the tick, in- flushDestroyedDrawables, once the flush has run the work other drawables deferred. */+ /* LOGIC-THREAD.md phase 2, M4. Inside a tick the drawable forgets its object, which the tick may+ delete before the flush, and leaves the selection after the tick (disregardDrawable queues it).+ Phase 3: it stays in the list and the lookup, which the render walks beside the tick, and only+ the tick's own lookups stop finding it; linkDrawablesOfTick takes it out after the tick. Its draw+ modules, render objects, shadows and sounds go in flushDestroyedDrawables, once the flush has run+ the work other drawables deferred. */if( Drawable::tickDefersLifetime() ){- removeDrawableFromLookupTable( draw );draw->friend_forgetObject();m_destroyedInTick.push_back( draw );return;}+ // remove from the master list+ draw->removeFromList(&m_drawableList);+LOGIC_CENSUS( CENSUS_DESTROY_DRAWABLE );// what the tick deferred on it happens now or never: the batched trees and props are made, placed@@ -1083,6 +1098,63 @@ void GameClient::flushDestroyedDrawables( void )m_destroyedInTick.clear();}+//-------------------------------------------------------------------------------------------------+/** LOGIC-THREAD.md phase 3: the list and the lookup take the tick's changes here, at the sync point,+ in the order the tick made them, so the list ends up as it did when they were made at the call.+ First of the flush: the drawables' deferred work and the effects look drawables up. */+void GameClient::linkDrawablesOfTick( void )+{+ for( size_t i = 0; i < m_destroyedInTick.size(); ++i )+ {+ Drawable *draw = m_destroyedInTick[ i ];+ // one the same tick made was never linked+ if( draw->getID() < m_drawableVector.size() && m_drawableVector[ draw->getID() ] == draw )+ {+ draw->removeFromList( &m_drawableList );+ removeDrawableFromLookupTable( draw );+ }+ }+ for( size_t i = 0; i < m_madeInTick.size(); ++i )+ {+ Drawable *draw = m_madeInTick[ i ];+ // ponytail: a linear search per drawable made, a few dozen a tick; a flag on the drawable if it ever shows+ if( std::find( m_destroyedInTick.begin(), m_destroyedInTick.end(), draw ) != m_destroyedInTick.end() )+ continue;+ addDrawableToLookupTable( draw );+ draw->prependToList( &m_drawableList );+ }+ m_madeInTick.clear();+}++/** -----------------------------------------------------------------------------------------------+ * Given an object id, return the associated object.+ * This method is the primary interface for accessing objects, and should be used+ * instead of pointers to "attach" objects to each other.+ */+Drawable* GameClient::findDrawableByID( const DrawableID id )+{+ LOGIC_CENSUS( CENSUS_GAMECLIENT );+ if( id == INVALID_DRAWABLE_ID )+ return NULL;++ Drawable *draw = (Int)id < m_drawableVector.size() ? m_drawableVector[(Int)id] : NULL;++ // inside the tick the table has not taken the tick's changes yet (linkDrawablesOfTick): the tick+ // still finds what it made and no longer what it destroyed, as when the table changed at the call+ if( Drawable::tickDefersLifetime() )+ {+ if( draw == NULL )+ {+ for( size_t i = 0; i < m_madeInTick.size(); ++i )+ if( m_madeInTick[ i ]->getID() == id )+ draw = m_madeInTick[ i ];+ }+ if( draw && std::find( m_destroyedInTick.begin(), m_destroyedInTick.end(), draw ) != m_destroyedInTick.end() )+ return NULL;+ }+ return draw;+}+// ------------------------------------------------------------------------------------------------/** Add drawable to lookup table for fast id searching */// ------------------------------------------------------------------------------------------------@@ -1093,6 +1165,13 @@ void GameClient::addDrawableToLookupTable(Drawable *draw )if( draw == NULL )return;+ // made inside a tick: linkDrawablesOfTick adds it (registerDrawable)+ if( Drawable::tickDefersLifetime() )+ {+ m_madeInTick.push_back( draw );+ return;+ }+// add to lookup// m_drawableHash[ draw->getID() ] = draw;DrawableID newID = draw->getID();