Skip to content

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.

fix(gamenetwork): restore lobby setup before a skirmish restart

Olcay Seygan3063cf83f838
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6028a2c14..3838aacf6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -167,6 +167,12 @@ map. Ctrl+A is still everything at once.
match was built from and started the next attempt on a different one, so the replay it was writing
no longer matched the game being played and would not play back. Restart now hands the game back
the number it had.
+- **And it restarts with the same colours, the same corners and the same armies.** "Random" colour,
+ "random" starting position and "random" army are drawn from that same shared number the moment the
+ match begins. On a restart they had already been drawn once — the slots now held real answers — so
+ there was nothing left to draw and the second attempt started from the first attempt's results,
+ with the shared sequence a few numbers further along than the replay expected. The lobby's choices
+ are now put back before the restart, so the second attempt makes the same picks the first one did.
- **Somebody quitting no longer ends the game for everyone else.** The game checks that all the
machines still agree by comparing a fingerprint of the world every so often, and if two of them
differ the match stops. A player leaving sent one last fingerprint from a machine that was already
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/GameInfo.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/GameInfo.h
index 7568a3121..6bfc7519f 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/GameInfo.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/GameInfo.h
@@ -103,7 +103,8 @@ public:
inline void setNATBehavior( FirewallHelperClass::FirewallBehaviorType NATBehavior) { m_NATBehavior = NATBehavior; }
inline FirewallHelperClass::FirewallBehaviorType getNATBehavior() const { return m_NATBehavior; }
- void saveOffOriginalInfo( void );
+ void saveOriginalSetup( void );
+ Bool hasSavedOriginalSetup( void ) const { return m_hasSavedOriginalSetup; }
inline Int getOriginalPlayerTemplate( void ) const { return m_origPlayerTemplate; }
inline Int getOriginalColor( void ) const { return m_origColor; }
inline Int getOriginalStartPos( void ) const { return m_origStartPos; }
@@ -133,6 +134,7 @@ protected:
Bool m_isAccepted;
Bool m_hasMap;
Bool m_isMuted;
+ Bool m_hasSavedOriginalSetup; ///< TRUE once the pre-randomization setup below was captured
Int m_color; ///< color, or -1 for random
Int m_startPos; ///< start position, or -1 for random
Int m_playerTemplate; ///< PlayerTemplate
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index 9dc1dabfc..45fbb6d3a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -1214,7 +1214,20 @@ void GameLogic::startNewGame( Bool loadingSaveGame )
{
GameSlot *slot = game->getSlot(i);
if (!loadingSaveGame) {
- slot->saveOffOriginalInfo();
+ if (slot->hasSavedOriginalSetup())
+ {
+ DEBUG_ASSERTCRASH(m_gameMode == GAME_SKIRMISH, ("Expected GAME_SKIRMISH but got %d", m_gameMode));
+
+ // Random color, position and faction are drawn off the logical seed. A restarted
+ // game puts the pre-draw values back so the same draws happen again.
+ slot->setColor(slot->getOriginalColor());
+ slot->setStartPos(slot->getOriginalStartPos());
+ slot->setPlayerTemplate(slot->getOriginalPlayerTemplate());
+ }
+ else
+ {
+ slot->saveOriginalSetup();
+ }
}
if (slot->isAI())
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameInfo.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameInfo.cpp
index 04eb50bf4..38f449345 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameInfo.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameInfo.cpp
@@ -75,20 +75,23 @@ void GameSlot::reset()
m_disconnected = FALSE;
m_port = 0;
m_isMuted = FALSE;
+ m_hasSavedOriginalSetup = FALSE;
m_origPlayerTemplate = -1;
m_origStartPos = -1;
m_origColor = -1;
}
-void GameSlot::saveOffOriginalInfo( void )
+void GameSlot::saveOriginalSetup( void )
{
- DEBUG_LOG(("GameSlot::saveOffOriginalInfo() - orig was color=%d, pos=%d, house=%d\n",
+ DEBUG_LOG(("GameSlot::saveOriginalSetup() - orig was color=%d, pos=%d, house=%d\n",
m_origColor, m_origStartPos, m_origPlayerTemplate));
m_origPlayerTemplate = m_playerTemplate;
m_origStartPos = m_startPos;
m_origColor = m_color;
- DEBUG_LOG(("GameSlot::saveOffOriginalInfo() - color=%d, pos=%d, house=%d\n",
+ DEBUG_LOG(("GameSlot::saveOriginalSetup() - color=%d, pos=%d, house=%d\n",
m_color, m_startPos, m_playerTemplate));
+
+ m_hasSavedOriginalSetup = TRUE;
}
static Int getSlotIndex(const GameSlot *slot)
@@ -1575,7 +1578,7 @@ void SkirmishGameInfo::xfer( Xfer *xfer )
m_slot[slot]->setPlayerTemplate(origPlayerTemplate);
m_slot[slot]->setStartPos(origStartPos);
m_slot[slot]->setColor(origColor);
- m_slot[slot]->saveOffOriginalInfo();
+ m_slot[slot]->saveOriginalSetup();
m_slot[slot]->setTeamNumber(teamNumber);
m_slot[slot]->setColor(color);
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 8f16dd2c5..7c4c9619e 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -39,6 +39,7 @@
#include "Common/RandomValue.h"
#include "GameLogic/LogicRandomValue.h"
#include "GameNetwork/CrcAgreement.h"
+#include "GameNetwork/GameInfo.h"
#include <float.h>
#include "GameClient/Water.h"
#include "GameLogic/Module/PhysicsUpdate.h"
@@ -3392,3 +3393,60 @@ TEST(terrainlogic_finds_a_trigger_area_by_name_and_notices_edits)
PolygonTrigger::deleteTriggers();
CHECK( tl.getTriggerAreaByName( "Zone1" ) == NULL );
}
+
+// A restarted skirmish must draw the same random colors, start positions and factions as the first
+// run did. The draws only happen for a slot whose value is still negative, so the setup captured
+// before the first draw has to be put back before the second start.
+TEST(gameslot_restores_the_pre_randomization_setup_on_a_restart)
+{
+ GameSlot slot;
+
+ // nothing captured yet, and the map's own "random" markers are what the slot starts with
+ CHECK( !slot.hasSavedOriginalSetup() );
+ CHECK_EQ( slot.getColor(), -1 );
+ CHECK_EQ( slot.getStartPos(), -1 );
+ CHECK_EQ( slot.getPlayerTemplate(), (Int)PLAYERTEMPLATE_RANDOM );
+
+ // first start: capture, then let the draws resolve every one of them
+ slot.saveOriginalSetup();
+ CHECK( slot.hasSavedOriginalSetup() );
+ CHECK_EQ( slot.getOriginalColor(), -1 );
+ CHECK_EQ( slot.getOriginalStartPos(), -1 );
+ CHECK_EQ( slot.getOriginalPlayerTemplate(), (Int)PLAYERTEMPLATE_RANDOM );
+
+ slot.setColor( 3 );
+ slot.setStartPos( 5 );
+ slot.setPlayerTemplate( 2 );
+ CHECK_EQ( slot.getColor(), 3 );
+ CHECK_EQ( slot.getStartPos(), 5 );
+ CHECK_EQ( slot.getPlayerTemplate(), 2 );
+
+ // second start: the capture is still there, so the slot is rolled back instead of re-captured
+ CHECK( slot.hasSavedOriginalSetup() );
+ slot.setColor( slot.getOriginalColor() );
+ slot.setStartPos( slot.getOriginalStartPos() );
+ slot.setPlayerTemplate( slot.getOriginalPlayerTemplate() );
+ CHECK_EQ( slot.getColor(), -1 );
+ CHECK_EQ( slot.getStartPos(), -1 );
+ CHECK_EQ( slot.getPlayerTemplate(), (Int)PLAYERTEMPLATE_RANDOM );
+
+ // and the originals survive the rollback, so a third start rolls back to the same place
+ CHECK_EQ( slot.getOriginalColor(), -1 );
+ CHECK_EQ( slot.getOriginalStartPos(), -1 );
+ CHECK_EQ( slot.getOriginalPlayerTemplate(), (Int)PLAYERTEMPLATE_RANDOM );
+
+ // a slot that was set up by hand is captured as-is, not as the random markers
+ GameSlot fixed;
+ fixed.setColor( 4 );
+ fixed.setStartPos( 1 );
+ fixed.setPlayerTemplate( 6 );
+ fixed.saveOriginalSetup();
+ CHECK_EQ( fixed.getOriginalColor(), 4 );
+ CHECK_EQ( fixed.getOriginalStartPos(), 1 );
+ CHECK_EQ( fixed.getOriginalPlayerTemplate(), 6 );
+
+ // leaving the lobby clears the capture, so the next game captures its own setup
+ fixed.reset();
+ CHECK( !fixed.hasSavedOriginalSetup() );
+ CHECK_EQ( fixed.getOriginalColor(), -1 );
+}