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): refuse installs that differ in data logic reads
Olcay Seyganecd5f4d77e78
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 93509386d..ecb445c18 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -241,6 +241,8 @@ found and fixed — EA's own, not port damage.**- The first building of a computer player is no longer held back a frame on a machine that already played a match that session.- Two allied vehicles blocking each other in a narrow pass no longer read freed memory while they make way, which could crash the game or split it.- A cheat every machine refused during a network game is refused in the replay of it too.+- An install that differs where it matters can no longer join and split the match later. The game's check at the door now also covers the command buttons the computer player builds from and every unit model, whose shape decides where shots leave the barrel. And a map's own rules file counts as part of the map, so two players with the same map and different rules get it sent across instead of fighting with different armour.+- The computer player's sense of which unit beats which comes out the same on every processor. It used the runtime's logarithm, which takes a different path on processors with newer instructions.- The price of all of the above: a replay recorded on an earlier version can go out of step on this one where it has a Particle Cannon, an EMP, a stealthed garrison or two allies making way for each other in it.- Pausing no longer leaks a little memory for every sound that was about to start.- A sound cancelled before it started really is cancelled. The check compared a pointer against a handle - two different things sharing one slot - so it never matched and the sound played anyway.diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cppindex 044703926..723fd617e 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp@@ -191,6 +191,42 @@ extern CComModule _Module;//-------------------------------------------------------------------------------------------------static void updateTGAtoDDS();+//-------------------------------------------------------------------------------------------------+/** A file logic reads that no subsystem loads through the INI checksum, as it resolved on this+ * machine, byte for byte. */+//-------------------------------------------------------------------------------------------------+static void checksumFileContents( XferCRC &xferCRC, const char *path )+{+ File *file = TheFileSystem->openFile( path, File::READ );+ Int fileSize = file->size();+ char *contents = file->readEntireAndClose();+ xferCRC.xferUser( contents, fileSize );+ delete [] contents;+}++//-------------------------------------------------------------------------------------------------+/** Every model's bones place fire points, turret pivots and dock positions in logic, so a model+ * that differs between two machines splits their match. Hashing 190 MB of models on every start+ * costs seconds; the name and size of each model as it resolved catches a different patch+ * archive or a loose model, which are the two ways an install ends up with another one. */+//-------------------------------------------------------------------------------------------------+static void checksumModelIndex( XferCRC &xferCRC )+{+ FilenameList models;+ TheFileSystem->getFileListInDirectory( AsciiString( "Art\\W3D\\" ), AsciiString( "*.w3d" ), models, TRUE );++ for( FilenameListIter it = models.begin(); it != models.end(); ++it )+ {+ AsciiString name = *it;+ name.toLower();+ FileInfo info;+ TheFileSystem->getFileInfo( *it, &info );+ xferCRC.xferUser( const_cast<char *>( name.str() ), name.getLength() );+ xferCRC.xferInt( &info.sizeLow );+ }+ DEBUG_LOG(( "INI CRC covers %d models\n", (Int)models.size() ));+}+Int GameEngine::getFramesPerSecondLimit( void ){return m_maxFPS;@@ -1010,6 +1046,12 @@ void GameEngine::init( int argc, char *argv[] )#endif/////////////////////////////////////////////////////////////////////////////////////////////+ // the control bar parses these without the checksum, and the AI builds and hunts from them+ checksumFileContents( xferCRC, "Data\\INI\\Default\\CommandButton.ini" );+ checksumFileContents( xferCRC, "Data\\INI\\CommandButton.ini" );+ checksumFileContents( xferCRC, "Data\\INI\\CommandSet.ini" );+ checksumModelIndex( xferCRC );+xferCRC.close();TheWritableGlobalData->m_iniCRC = xferCRC.getCRC();DEBUG_LOG(("INI CRC is 0x%8.8X\n", TheGlobalData->m_iniCRC));diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MapUtil.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MapUtil.cppindex 96a3e7bb5..7d313608d 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MapUtil.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MapUtil.cpp@@ -87,45 +87,71 @@ static Coord3DList m_techPositions;static Int m_mapDX = 0;static Int m_mapDY = 0;-static UnsignedInt calcCRC( AsciiString dirName, AsciiString fname )+static void addFileToCRC( CRC &theCRC, File *fp ){- CRC theCRC;- theCRC.clear();+ UnsignedByte buf[4096];+ Int num;+ while ( (num=fp->read(buf, 4096)) > 0 )+ {+ theCRC.computeCRC(buf, num);+ }- // Try the official map dir- AsciiString asciiFile;- char tempBuf[_MAX_PATH];- char filenameBuf[_MAX_PATH];- int length = 0;- // bounded: fname comes from a directory scan / the map cache and can be longer than _MAX_PATH- strncpy(tempBuf, fname.str(), _MAX_PATH - 1);- tempBuf[_MAX_PATH - 1] = 0;- length = strlen( tempBuf );- if( length >= 4 )+ fp->close();+}++/** GameLogic::startNewGame loads these from the map's folder with INI_LOAD_CREATE_OVERRIDES, and+ * they change armour, upgrades, weapons and objects for the match. The lobby compares only this+ * CRC, so two players with the same map file and different rules files used to be let in to a+ * match that split on the first shot. */+static const char *const MAP_RULES_FILES[] = { "map.ini", "solo.ini" };+static const Int MAP_RULES_FILE_COUNT = sizeof( MAP_RULES_FILES ) / sizeof( MAP_RULES_FILES[0] );++static AsciiString mapRulesPath( const AsciiString &fname, Int rulesIndex )+{+ // the map's folder, cut the way startNewGame cuts it+ char folder[_MAX_PATH];+ strncpy( folder, fname.str(), _MAX_PATH - 1 );+ folder[_MAX_PATH - 1] = 0;+ char *separator = folder + strlen( folder );+ while ((separator > folder) && (*separator != '\\') && (*separator != '/'))+ --separator;+ *separator = 0;++ AsciiString path;+ path.format( "%s\\%s", folder, MAP_RULES_FILES[rulesIndex] );+ return path;+}++static Bool hasMapRules( const AsciiString &fname )+{+ for( Int i = 0; i < MAP_RULES_FILE_COUNT; ++i ){- memset( filenameBuf, '\0', _MAX_PATH);- strncpy( filenameBuf, tempBuf, length - 4);+ if (TheFileSystem->doesFileExist( mapRulesPath( fname, i ).str() ))+ return TRUE;}+ return FALSE;+}- File *fp;- asciiFile = fname;- fp = TheFileSystem->openFile(asciiFile.str(), File::READ);+static UnsignedInt calcCRC( AsciiString dirName, AsciiString fname )+{+ CRC theCRC;+ theCRC.clear();++ File *fp = TheFileSystem->openFile(fname.str(), File::READ);if( !fp ){DEBUG_CRASH(("Couldn't open '%s'\n", fname.str()));return 0;}+ addFileToCRC( theCRC, fp );- UnsignedByte buf[4096];- Int num;- while ( (num=fp->read(buf, 4096)) > 0 )+ for( Int i = 0; i < MAP_RULES_FILE_COUNT; ++i ){- theCRC.computeCRC(buf, num);+ File *rules = TheFileSystem->openFile( mapRulesPath( fname, i ).str(), File::READ );+ if (rules)+ addFileToCRC( theCRC, rules );}- fp->close();- fp = NULL;-return theCRC.get();}@@ -706,6 +732,10 @@ Bool MapCache::addMap( AsciiString dirName, AsciiString fname, FileInfo *fileInfif ((md.m_filesize == filesize) &&(md.m_CRC != 0)){+ // the cache is keyed on the map file's size, which says nothing about its rules files+ if (hasMapRules( fname ))+ (*this)[lowerFname].m_CRC = calcCRC( dirName, fname );+// Force a lookup so that we don't display the English localization in all builds.if (md.m_nameLookupTag.isEmpty()){diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cppindex 1df9b3ce8..faaa49f07 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp@@ -1339,6 +1339,26 @@ Real aiFramesToKill( Real targetHealth, const AIShotPattern &shots )+ INT_TO_REAL( reloads ) * shots.m_reloadFrames;}+//-------------------------------------------------------------------------------------------------+/** log2 out of frexpf and a fixed series, all exact or plain IEEE arithmetic. The runtime's log+ * takes an FMA3 path on a processor that has one, and a last bit that differs between two+ * machines is enough to flip the AI's pick between two scores that tie. */+//-------------------------------------------------------------------------------------------------+static Real deterministicLog2( Real value )+{+ const Real INVERSE_LN2 = 1.44269504f;++ Int exponent = 0;+ const Real mantissa = frexpf( value, &exponent ); // value = mantissa * 2^exponent, mantissa in [0.5, 1)++ // ln(m) = 2 atanh(t) with t = (m - 1) / (m + 1), and |t| <= 1/3 keeps five terms under 1e-6+ const Real t = (mantissa - 1.0f) / (mantissa + 1.0f);+ const Real t2 = t * t;+ const Real atanhT = t * (1.0f + t2 * (1.0f / 3.0f + t2 * (1.0f / 5.0f + t2 * (1.0f / 7.0f + t2 * (1.0f / 9.0f)))));++ return INT_TO_REAL( exponent ) + 2.0f * atanhT * INVERSE_LN2;+}+//-------------------------------------------------------------------------------------------------/** One pairing, money for money. On a log scale, so twice as good and half as good sit the same* distance either side of an even trade, and the two directions of one pairing always sum to 1. */@@ -1346,6 +1366,7 @@ Real aiFramesToKill( Real targetHealth, const AIShotPattern &shots )Real aiMatchupScore( Real myFramesToKill, Real theirFramesToKill, Real myCost, Real theirCost ){const Real SATURATING_DOUBLINGS = 4.0f; // sixteen times better is as good as it gets+ const Real SATURATING_ADVANTAGE = 16.0f; // 2 ^ SATURATING_DOUBLINGSif( myFramesToKill < 0.0f )return 0.0f; // nothing I can do about it, whatever it can do to me@@ -1356,7 +1377,10 @@ Real aiMatchupScore( Real myFramesToKill, Real theirFramesToKill, Real myCost, Rif( myCost > 0.0f && theirCost > 0.0f )advantage *= theirCost / myCost; // twice the price has to kill twice as fast to break even- const Real doublings = (Real)( log( advantage ) / log( 2.0 ) );+ // the score saturates past sixteen either way, and the clamp keeps zero and infinity out of the log+ if( advantage > SATURATING_ADVANTAGE ) advantage = SATURATING_ADVANTAGE;+ if( advantage < 1.0f / SATURATING_ADVANTAGE ) advantage = 1.0f / SATURATING_ADVANTAGE;+ const Real doublings = deterministicLog2( advantage );Real score = 0.5f + 0.5f * doublings / SATURATING_DOUBLINGS;if( score < 0.0f ) score = 0.0f;if( score > 1.0f ) score = 1.0f;diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex c437ffc71..231bee115 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -9260,6 +9260,10 @@ TEST(matchup_score_is_money_for_money)// a free unit has no price to weigh, so only the kill times countCHECK_NEAR( faster, aiMatchupScore( 240.0f, 480.0f, 0.0f, 900.0f ), 0.0001f );++ // the log is the game's own, and still a log: three times better is log2(3) = 1.58496 doublings+ CHECK_NEAR( 0.5f + 0.5f * 1.5849625f / 4.0f, aiMatchupScore( 160.0f, 480.0f, 900.0f, 900.0f ), 0.00001f );+ CHECK_NEAR( 0.625f, faster, 0.00001f );}