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(gameengine): hold the state machine through a change of state
Olcay Seygan9460f5116068
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4faeee865..3d787e3b7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1119,6 +1119,7 @@ A third pass went through the generals' powers, promotions and unit abilities.## Twenty-two-year-old bugs, found by testing+- A match no longer freezes when a unit attacking or hunting with its team changes its orders at the wrong moment. Switching targets could throw away the unit's order logic while it was still being used; the log then filled with "no state 0, in state 10" and the game stopped on one frame until Windows closed it. The order logic now lives until the switch is done.- A Nuke Cannon on guard stays deployed between targets. After every kill it packed up to drive back to the spot it was already standing on, then set up again, so a guarding cannon spent most of a fight folding and unfolding: four Technicals driven into its range one after another cost it four pack-ups and four set-ups, and now cost it none. It still packs up for a move order or a target out of reach. The Sentry Drone guards the same way. Replays recorded before this change play out differently.- Pressing Continue after a campaign mission goes straight to the next one. The main menu used to wake up behind the score screen on the way out, show its buttons and start loading its own background battle, so the menu sat on screen over the next mission's load.- The main menu no longer opens with a man screaming. The battle behind it is staged the way EA laid it out: Jarmen Kell sits on the cliff over the GLA infantry as scenery, and the extra reach high ground gives in a match let his rifle hit them from nearly twice its range a quarter of a second after the menu came up. High ground adds nothing behind the menu now, so the first soldier falls about six seconds in, under the battleship's shells, as he always did. Soldiers' death cries on the battlefield also sit back at their own volume; the boost that lifts your units' replies over the guns had been catching them too.diff --git a/GeneralsMD/Code/GameEngine/Source/Common/StateMachine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/StateMachine.cppindex 72126eaa1..0d07e51c9 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/StateMachine.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/StateMachine.cpp@@ -588,6 +588,13 @@ StateReturnType StateMachine::setState( StateID newStateID )*/StateReturnType StateMachine::internalSetState( StateID newStateID ){+ // The same hold updateStateMachine takes, for a change of state that does not come from an update:+ // setState and initDefaultState from a parent state. An onEnter or onExit on the way can make the+ // parent leave the state that owns this machine, which drops the parent's reference. Without this+ // one the machine was freed under the transition, the next lookup read a dead state map, and the+ // fall back to the default state (an attack that failed straight back to idle) spun the frame forever.+ StateMachineRefHolder refThis( this );+State *newState = NULL;// anytime the state changes, stop sleepingdiff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 1c9cd4e18..bf04633be 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -4004,6 +4004,88 @@ TEST(statemachine_outlives_the_owner_that_lets_go_of_it_mid_update)}}+/* The same hold for a change of state that does not come from an update. Issue 123: a hunt or attack+ state set its sub-machine's state, the onEnter on the way made the parent leave, the parent dropped+ the sub-machine, and the transition carried on in freed memory - the state map read empty, the+ machine fell back to its default attack state, and the game hung on that frame. Here the first+ state lets go of the machine in its onEnter and fails into the second. */+static Bool s_letGoMachineDestroyed = FALSE;+static Bool s_secondStateEntered = FALSE;+static Bool s_machineAliveInSecondState = FALSE;++class LetGoState : public State+{+ MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(LetGoState, "ContinueState")+public:+ LetGoState(StateMachine *machine) : State(machine, "LetGoState") {}+ virtual StateReturnType onEnter() { getMachine()->deleteInstance(); return STATE_FAILURE; }+ virtual StateReturnType update() { return STATE_CONTINUE; }+protected:+ virtual void crc(Xfer *xfer) {}+ virtual void xfer(Xfer *xfer) {}+ virtual void loadPostProcess() {}+};+EMPTY_DTOR(LetGoState)++class SecondState : public State+{+ MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(SecondState, "SuccessState")+public:+ SecondState(StateMachine *machine) : State(machine, "SecondState") {}+ virtual StateReturnType onEnter()+ {+ s_secondStateEntered = TRUE;+ s_machineAliveInSecondState = !s_letGoMachineDestroyed;+ return STATE_CONTINUE;+ }+ virtual StateReturnType update() { return STATE_CONTINUE; }+protected:+ virtual void crc(Xfer *xfer) {}+ virtual void xfer(Xfer *xfer) {}+ virtual void loadPostProcess() {}+};+EMPTY_DTOR(SecondState)++class LetGoStateMachine : public StateMachine+{+ MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(LetGoStateMachine, "StateMachinePool")+public:+ LetGoStateMachine() : StateMachine(NULL, "letgo")+ {+ defineState(0, newInstance(LetGoState)(this), 1, 1);+ defineState(1, newInstance(SecondState)(this), 1, 1);+ }+};++LetGoStateMachine::~LetGoStateMachine() { s_letGoMachineDestroyed = TRUE; }++TEST(statemachine_outlives_the_owner_that_lets_go_of_it_mid_transition)+{+ CHECK(bootOnce());++ GlobalData *savedGlobals = TheWritableGlobalData;+ if (savedGlobals == NULL)+ TheWritableGlobalData = NEW GlobalData;++ s_letGoMachineDestroyed = FALSE;+ s_secondStateEntered = FALSE;+ s_machineAliveInSecondState = FALSE;+ StateMachine *machine = newInstance(LetGoStateMachine);++ /* the owner's reference goes in the first state's onEnter; the transition it fails into still runs+ on a live machine, and the machine goes when the transition is done */+ machine->initDefaultState();+ CHECK(s_secondStateEntered);+ CHECK(s_machineAliveInSecondState);+ CHECK(s_letGoMachineDestroyed);++ if (savedGlobals == NULL)+ {+ delete TheWritableGlobalData;+ TheWritableGlobalData = NULL;+ }+}+/* ------------------------------------------------------------------------------------------------* The A* open list.