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
fix(gamelogic): save what a unit was in the middle of doing
Olcay Seygana6ad965da318
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 58eb7bda9..7bf0d0a74 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -131,6 +131,11 @@ found and fixed — EA's own, not port damage.**- A unit with nowhere to stand appears where asked, not at a random point.- Loading a save remembers what your guards were guarding.+- A turret loaded from a save is still tracking what it was tracking, and a worker loaded mid-job carries on with it, instead of both snapping back to their default state.+- A shell already in the air finishes its flight after a load instead of going off in the launcher's face.+- An aircraft carrier remembers the order it was given, and each runway its own ramp.+- A barracks that has already put its mob on the street does not put a second one out after a load.+- Effects tied together - a smoke column and its embers - stay tied together across a save.- Saved units come back with the map's weapons, not the stock ones.- Restarting a skirmish restarts the same skirmish, with a replay that plays back.- Replays sound and look the way they did when you played them.diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cppindex 37fb317ff..a52e890fe 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp@@ -1317,7 +1317,11 @@ ParticleSystem::ParticleSystem( const ParticleSystemTemplate *sysTemplate,m_controlParticle = NULL;m_groundShadow = NULL;- TheParticleSystemManager->friend_addParticleSystem(this);+ // A system built without an id yet - the load path does that, so it can restore the saved id+ // first - registers itself once it has one, rather than here where the manager would file it+ // under nothing and every later lookup by id would miss it.+ if (m_systemID != INVALID_PARTICLE_SYSTEM_ID)+ TheParticleSystemManager->friend_addParticleSystem(this);//DEBUG_ASSERTLOG(!(m_totalParticleSystemCount % 10 == 0), ( "TotalParticleSystemCount = %d\n", m_totalParticleSystemCount ));}@@ -1364,8 +1368,9 @@ ParticleSystem::~ParticleSystem()m_controlParticle->detachControlledParticleSystem();m_controlParticle = NULL;-- TheParticleSystemManager->friend_removeParticleSystem(this);++ if (m_systemID != INVALID_PARTICLE_SYSTEM_ID)+ TheParticleSystemManager->friend_removeParticleSystem(this);//DEBUG_ASSERTLOG(!(m_totalParticleSystemCount % 10 == 0), ( "TotalParticleSystemCount = %d\n", m_totalParticleSystemCount ));}@@ -3588,8 +3593,12 @@ void ParticleSystemManager::xfer( Xfer *xfer )} // end if- // create system- system = createParticleSystem( systemTemplate, FALSE );+ // Create the system without an id and let the saved one come back first. It used to be+ // created through createParticleSystem, which hands out the next id in sequence and+ // files the system under it - and then the xfer below overwrote the id with the saved+ // one, leaving the manager's index pointing at the wrong system. Every lookup by id+ // after a load missed: a master could not find its slaves.+ system = newInstance(ParticleSystem)( systemTemplate, INVALID_PARTICLE_SYSTEM_ID, FALSE );if( system == NULL ){@@ -3600,9 +3609,21 @@ void ParticleSystemManager::xfer( Xfer *xfer )} // end if- // read system data+ // read system data - this is what restores the idxfer->xferSnapshot( system );+ if( system->getSystemID() == INVALID_PARTICLE_SYSTEM_ID )+ {++ DEBUG_CRASH(( "ParticleSystemManager::xfer - No system id restored for particle system '%s'\n",+ systemName.str() ));+ system->deleteInstance();+ throw SC_INVALID_DATA;++ } // end if++ friend_addParticleSystem( system );+} // end for, i} // end else, loaddiff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/TurretAI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/TurretAI.cppindex 19b6170eb..758b91faa 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/TurretAI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/TurretAI.cpp@@ -155,10 +155,16 @@ void TurretStateMachine::crc( Xfer *xfer )// ------------------------------------------------------------------------------------------------void TurretStateMachine::xfer( Xfer *xfer ){- XferVersion cv = 1;+ // version 2 saves the state machine underneath. Version 1 saved a version number and nothing+ // else, so every turret came back from a save in its default state - pointed forward, with+ // whatever it was tracking forgotten.+ XferVersion cv = 2;XferVersion v = cv;xfer->xferVersion( &v, cv );+ if( v >= 2 )+ StateMachine::xfer( xfer );+} // end xfer// ------------------------------------------------------------------------------------------------@@ -166,6 +172,7 @@ void TurretStateMachine::xfer( Xfer *xfer )// ------------------------------------------------------------------------------------------------void TurretStateMachine::loadPostProcess( void ){+ StateMachine::loadPostProcess();} // end loadPostProcess//----------------------------------------------------------------------------------------------------------diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/DumbProjectileBehavior.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/DumbProjectileBehavior.cppindex d5d8d6e9f..4f57f735e 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/DumbProjectileBehavior.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/DumbProjectileBehavior.cpp@@ -739,8 +739,10 @@ void DumbProjectileBehavior::crc( Xfer *xfer )void DumbProjectileBehavior::xfer( Xfer *xfer ){- // version- XferVersion currentVersion = 1;+ // version 2 saves how far along its arc the shell is, and rebuilds the arc on load. Version 1+ // saved neither: a shell in flight came back at step zero of a path it no longer had, so it+ // went off in the launcher's face instead of finishing its flight.+ XferVersion currentVersion = 2;XferVersion version = currentVersion;xfer->xferVersion( &version, currentVersion );@@ -791,6 +793,14 @@ void DumbProjectileBehavior::xfer( Xfer *xfer )// lifespan framexfer->xferUnsignedInt( &m_lifespanFrame );+ if( version >= 2 )+ xfer->xferInt( &m_currentFlightPathStep );++ // the path itself is a vector of points that is not saved; rebuild it from the start, end and+ // segment count that are, so the step we just read means something+ if( xfer->getXferMode() == XFER_LOAD && m_flightPathSegments > 0 )+ calcFlightPath( false );+} // end xfer// ------------------------------------------------------------------------------------------------diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/FlightDeckBehavior.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/FlightDeckBehavior.cppindex 3636130ab..0a8a234f2 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/FlightDeckBehavior.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/FlightDeckBehavior.cpp@@ -1693,7 +1693,9 @@ void FlightDeckBehavior::xfer( Xfer *xfer )xfer->xferUnsignedInt( &m_startedProductionFrame );xfer->xferUnsignedInt( &m_nextAllowedProductionFrame );xfer->xferObjectID( &m_designatedTarget );- Int commandType;+ // on save this is what gets written, and it was written uninitialised - the carrier came back+ // from a save having forgotten the order it was given, or holding whatever was on the stack+ Int commandType = (Int)m_designatedCommand;xfer->xferInt( &commandType );m_designatedCommand = (AICommandType)commandType;xfer->xferCoord3D( &m_designatedPosition );@@ -1710,7 +1712,9 @@ void FlightDeckBehavior::xfer( Xfer *xfer )xfer->xferUnsignedInt( &m_rampUpFrame[ i ] );xfer->xferUnsignedInt( &m_catapultSystemFrame[ i ] );xfer->xferUnsignedInt( &m_lowerRampFrame[ i ] );- xfer->xferBool( &m_rampUp[ MAX_RUNWAYS ] );+ // this said [MAX_RUNWAYS], which is one past the end of the array: every runway's ramp+ // state was read and written through the same out-of-bounds byte+ xfer->xferBool( &m_rampUp[ i ] );}else{diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/SpawnBehavior.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/SpawnBehavior.cppindex e1413c7ac..597c6b014 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/SpawnBehavior.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/SpawnBehavior.cpp@@ -1083,8 +1083,10 @@ void SpawnBehavior::xfer( Xfer *xfer ){AsciiString name;- // version- XferVersion currentVersion = 2;+ // version 3 saves how much of the opening burst is still owed. Without it a loaded save reset+ // that counter to the template's full count, so a barracks that had already put its mob on the+ // street put a whole new one out - fallen members came back at the building.+ XferVersion currentVersion = 3;XferVersion version = currentVersion;xfer->xferVersion( &version, currentVersion );@@ -1096,6 +1098,10 @@ void SpawnBehavior::xfer( Xfer *xfer )xfer->xferBool(&m_initialBurstTimesInited);}+ if (version >= 3) {+ xfer->xferUnsignedInt(&m_initialBurstCountdown);+ }+// spawn templatename = m_spawnTemplate ? m_spawnTemplate->getName() : AsciiString::TheEmptyString;xfer->xferAsciiString( &name );diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cppindex 2e5a9913c..a0666aeb9 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/DozerAIUpdate.cpp@@ -861,12 +861,17 @@ void DozerActionStateMachine::crc( Xfer *xfer )// ------------------------------------------------------------------------------------------------void DozerActionStateMachine::xfer( Xfer *xfer ){- // version- XferVersion currentVersion = 1;+ // version 2 saves the state machine underneath. Version 1 saved the task and nothing else, so a+ // dozer came back from a save still holding the job but standing in the default state for it -+ // which is how a builder loaded mid-job stopped building.+ XferVersion currentVersion = 2;XferVersion version = currentVersion;xfer->xferVersion( &version, currentVersion );xfer->xferUser(&m_task, sizeof(m_task));++ if( version >= 2 )+ StateMachine::xfer( xfer );} // end xfer// ------------------------------------------------------------------------------------------------@@ -874,6 +879,7 @@ void DozerActionStateMachine::xfer( Xfer *xfer )// ------------------------------------------------------------------------------------------------void DozerActionStateMachine::loadPostProcess( void ){+ StateMachine::loadPostProcess();} // end loadPostProcessdiff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex bac042c96..154e0056c 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -3314,6 +3314,46 @@ TEST(particlesys_find_answers_by_id_and_forgets_a_dead_system)TheGameClient = savedClient;}+/* Loading a save has to put each system back under the id it was saved with. It used to build the+ system through createParticleSystem, which hands out the next id in sequence and files the system+ under it, and only then read the saved id over the top - so the manager's index pointed at the+ wrong system and every lookup by id missed, which is how a master lost its slaves across a save.+ A system built with no id now stays out of the index until it has one. */+TEST(particlesys_built_without_an_id_stays_out_of_the_index_until_it_has_one)+{+ GameClient *savedClient = TheGameClient;+ TheGameClient = getTestGameClient();+ ParticleSystemManager *savedManager = TheParticleSystemManager;++ {+ TestParticleSystemManager mgr;+ TheParticleSystemManager = &mgr;++ ParticleSystemTemplate *tmpl = mgr.newTemplate( AsciiString( "TestParticleSystem" ) );+ CHECK( tmpl != NULL );++ ParticleSystem *normal = mgr.createParticleSystem( tmpl, FALSE );+ CHECK_EQ( mgr.getParticleSystemCount(), 1 );++ // the shape the load path uses: no id yet, so nothing to file it under+ ParticleSystem *loading = newInstance(ParticleSystem)( tmpl, INVALID_PARTICLE_SYSTEM_ID, FALSE );+ CHECK( loading != NULL );+ CHECK_EQ( mgr.getParticleSystemCount(), 1 ); // still just the first one+ CHECK( mgr.findParticleSystem( INVALID_PARTICLE_SYSTEM_ID ) == NULL );++ // and taking it away again does not disturb what is filed+ loading->deleteInstance();+ CHECK_EQ( mgr.getParticleSystemCount(), 1 );+ CHECK( mgr.findParticleSystem( normal->getSystemID() ) == normal );++ normal->deleteInstance();+ CHECK_EQ( mgr.getParticleSystemCount(), 0 );+ }++ TheParticleSystemManager = savedManager;+ TheGameClient = savedClient;+}+TEST(particlesys_reset_lets_the_ids_start_over_without_ghosts){GameClient *savedClient = TheGameClient;