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
feat(gameengine): random map v14, time of day and snow
Olcay Seygan92b040f0ed58
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8636d5efa..f65eebc20 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1921,6 +1921,13 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the samemaples, desert sand with palms in thin stands, winter snow under snowed-in firs, or dry steppe.The two are picked apart, so a river can run through snow as well as through grass, and the lightchanges with the ground.+- The number picks the hour too. A map is played in the morning, in the afternoon, at dusk or at+ night, each about as often as the others. The morning sun comes in low and gold; at dusk the+ ground goes red under long shadows. A night map is moonlit and blue, with lights on in every+ window of the towns and on the buildings of your own base, and the ground stays clear enough to+ fight on. Winter maps snow: every roof in town is white and the snow keeps falling all match.+ Grass and sand never get snow on them, so a summer field does not end up under white roofs.+ There is no rain, because Zero Hour never had any to show.- Every player gets the same deal. The bases are found in the land rather than stamped on it, andthen every layout tried is measured by the walk from each base to its nearest and second-nearestenemy, and the one where those walks come out most even is the one you play. Your home dock,diff --git a/GeneralsMD/Code/GameEngine/Include/Common/RandomMapGenerator.h b/GeneralsMD/Code/GameEngine/Include/Common/RandomMapGenerator.hindex 8d5f9919f..99812fd3b 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/RandomMapGenerator.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/RandomMapGenerator.h@@ -34,7 +34,7 @@rather than desyncing partway through it. The fingerprint test intest_gameengine.cpp fails until the version and the recorded CRCs agree, sothe bump cannot be forgotten. */-#define RANDOM_MAP_GENERATOR_VERSION 13+#define RANDOM_MAP_GENERATOR_VERSION 14/// How much ground the map gives everybody, before the per-player share.enum RandomMapSize@@ -91,13 +91,14 @@ public:map. On success mapPathOut is what -map wants. */extern Bool stageRandomMap( const RandomMapSettings& settings, AsciiString& mapPathOut );-/** Is this the path of a generated map or its preview? The settings are in the- name, which is what lets a replay, a save or a joining machine rebuild the- same bytes from the name alone. A name carrying another generator version is- not one this build can rebuild, so it is not one of ours. */+/** Is this the path of a generated map or its preview, or the map.ini beside a+ map that snows? The settings are in the name, which is what lets a replay, a+ save or a joining machine rebuild the same bytes from the name alone. A name+ carrying another generator version is not one this build can rebuild, so it+ is not one of ours. */extern Bool isGeneratedMapPath( const AsciiString& path );-/** The bytes of a generated map or preview, generated on the spot if this path+/** The bytes of a generated map, preview or map.ini, generated on the spot if this pathhas not been staged in this run - which is how a replay of a generated mapplays back without the map having been kept anywhere. FALSE when the path isnot a generated one. The bytes belong to the store and stay valid until itdiff --git a/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cppindex 7456adf59..3eadae219 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp@@ -654,6 +654,25 @@ static const RMGBiome theBiomes[RMG_BIOME_COUNT] =},};+static Int rmgBiomeFor( Int seed )+{+ return (Int)( hashCell( seed, 9001, 1 ) % (UnsignedInt)RMG_BIOME_COUNT );+}++/** Snow falls on winter maps and nowhere else. The ground, the firs and the rocks of a winter map+ are snow already; on green grass or golden steppe it would put white roofs over a summer field. */+static Bool rmgSnowsOn( Int seed )+{+ return rmgBiomeFor( seed ) == RMG_BIOME_WINTER;+}++/** The hour, on its own salt so it rolls apart from the kind of map and the ground. Over the first+ thousand seeds each of the four comes up between 226 and 269 times. */+static TimeOfDay rmgTimeOfDayFor( Int seed )+{+ return (TimeOfDay)( TIME_OF_DAY_FIRST + (Int)( hashCell( seed, 9001, 14 ) % 4U ) );+}+struct RMGPoint{Real m_cellX;@@ -837,6 +856,8 @@ public:RandomMapSettings m_settings;const RMGMapTypeShape *m_type; ///< what kind of map the seed rolledconst RMGBiome *m_biome; ///< and what it is made of+ TimeOfDay m_timeOfDay; ///< the hour the map is lit for+ Bool m_snowy; ///< weather: snow on the roofs and in the airInt m_width; ///< map cells per side, border includedInt m_height;Real m_waterHeight; ///< height bytes; the surface of every lake@@ -5813,8 +5834,13 @@ void RMGLayout::placeTowns( void )AsciiString uniqueID;uniqueID.format( "Civilian %d", buildingID++ );- addObject( theStreetNames[(nameOffset + buildingID) % numStreetNames],- uniqueID.str(), buildingX, buildingY,+ const char *name = theStreetNames[(nameOffset + buildingID) % numStreetNames];+ // The one shop on the list with no snowed or night model: it would stand bare+ // and dark among lit, snowed neighbours. StanSmallRetail01 has its footprint+ // and its ten garrison seats.+ if( ( m_snowy || m_timeOfDay == TIME_OF_DAY_NIGHT ) && strcmp( name, "StanSmallRetail03" ) == 0 )+ name = "StanSmallRetail01";+ addObject( name, uniqueID.str(), buildingX, buildingY,snapAngle45( plan.m_rotation + facing ) );}}@@ -6557,7 +6583,9 @@ void RMGLayout::build( const RandomMapSettings& settings )the same seed is the same kind of map for two players and for eight. Over the first thousandseeds each of the seven kinds comes up between 131 and 159 times. */m_type = &theMapTypes[hashCell( m_settings.m_seed, 9001, 8 ) % (UnsignedInt)RMG_MAP_TYPE_COUNT];- m_biome = &theBiomes[hashCell( m_settings.m_seed, 9001, 1 ) % (UnsignedInt)RMG_BIOME_COUNT];+ m_biome = &theBiomes[rmgBiomeFor( m_settings.m_seed )];+ m_timeOfDay = rmgTimeOfDayFor( m_settings.m_seed );+ m_snowy = rmgSnowsOn( m_settings.m_seed );m_width = m_settings.m_playableCells + 2 * RMG_BORDER_CELLS;m_height = m_width;@@ -6862,37 +6890,55 @@ static void writePolygonTriggers( MapChunkWriter& w, const RMGLayout& layout )w.closeChunk();}-/** Daylight. A map with no lighting chunk keeps whatever GameData.ini left in GlobalData, which is- the dusk the shipped maps all override, so a generated map looked like somebody had turned the- sun off. These are written for all four times of day: the map has no scripts to change the hour- with, and a player who forces one should still be able to see the ground. */-static void writeGlobalLighting( MapChunkWriter& w, const RMGBiome& biome )+/** How each hour is lit, as a tint over the biome's afternoon so a desert dusk stays a desert's and+ a winter night keeps the snow's blue. Afternoon is the biome as it is. Morning and evening put+ the sun low on opposite sides, evening warmer and dimmer; night is the moon, high and blue, at+ about the level of the shipped night maps (Dark Night's ambient is 0.09 0.16 0.50, its diffuse+ 0.20 0.24 0.37), so the ground and the units still read. */+struct RMGHourLight+{+ Real m_ambient[3]; ///< times the biome's ambient+ Real m_diffuse[3]; ///< times the biome's diffuse+ Real m_sun[3]; ///< the light's direction; afternoon keeps the biome's+ Bool m_biomeSun;+};++static const RMGHourLight theHourLights[4] ={- const Real *theSunDirection = biome.m_sunDirection;- const Real *theTerrainAmbient = biome.m_terrainAmbient;- const Real *theTerrainDiffuse = biome.m_terrainDiffuse;- const Real *theObjectAmbient = biome.m_objectAmbient;- const Real *theObjectDiffuse = biome.m_objectDiffuse;+ { { 1.10f, 1.00f, 0.88f }, { 1.05f, 0.90f, 0.78f }, { -0.90f, 0.20f, -0.38f }, FALSE }, // morning+ { { 1.00f, 1.00f, 1.00f }, { 1.00f, 1.00f, 1.00f }, { 0.0f, 0.0f, -1.0f }, TRUE }, // afternoon+ { { 0.85f, 0.68f, 0.58f }, { 1.05f, 0.72f, 0.48f }, { 0.86f, -0.30f, -0.42f }, FALSE }, // evening+ { { 0.34f, 0.42f, 0.80f }, { 0.34f, 0.40f, 0.58f }, { -0.43f, 0.43f, -0.80f }, FALSE }, // night+};++static void writeTintedLight( MapChunkWriter& w, const Real *ambient, const Real *diffuse,+ const RMGHourLight& hour, const Real *biomeSun )+{+ Int channel;+ for( channel = 0; channel < 3; channel++ )+ w.writeReal( ambient[channel] * hour.m_ambient[channel] );+ for( channel = 0; channel < 3; channel++ )+ w.writeReal( diffuse[channel] * hour.m_diffuse[channel] ); // the brightest biome stays under 1+ for( channel = 0; channel < 3; channel++ )+ w.writeReal( hour.m_biomeSun ? biomeSun[channel] : hour.m_sun[channel] );+}+/** The light. A map with no lighting chunk keeps whatever GameData.ini left in GlobalData, which is+ the dusk the shipped maps all override, so a generated map looked like somebody had turned the+ sun off. All four hours are written and the seed's is the current one; the reader takes the+ current hour's set, and the buildings pick their night or snow models from the hour and the+ WorldInfo weather. */+static void writeGlobalLighting( MapChunkWriter& w, const RMGBiome& biome, TimeOfDay current )+{w.openChunk( "GlobalLighting", K_LIGHTING_VERSION_3 );- w.writeInt( TIME_OF_DAY_AFTERNOON );+ w.writeInt( current );Int timeOfDay, light, channel;for( timeOfDay = 0; timeOfDay < 4; timeOfDay++ ){- for( channel = 0; channel < 3; channel++ )- w.writeReal( theTerrainAmbient[channel] );- for( channel = 0; channel < 3; channel++ )- w.writeReal( theTerrainDiffuse[channel] );- for( channel = 0; channel < 3; channel++ )- w.writeReal( theSunDirection[channel] );-- for( channel = 0; channel < 3; channel++ )- w.writeReal( theObjectAmbient[channel] );- for( channel = 0; channel < 3; channel++ )- w.writeReal( theObjectDiffuse[channel] );- for( channel = 0; channel < 3; channel++ )- w.writeReal( theSunDirection[channel] );+ const RMGHourLight& hour = theHourLights[timeOfDay];+ writeTintedLight( w, biome.m_terrainAmbient, biome.m_terrainDiffuse, hour, biome.m_sunDirection );+ writeTintedLight( w, biome.m_objectAmbient, biome.m_objectDiffuse, hour, biome.m_sunDirection );// The two extra lights of version 3, dark but pointing somewhere valid: one sun is// what this map wants, and a light with no direction at all upsets the shaders.@@ -6959,6 +7005,11 @@ void RandomMapGenerator::clampSettings( RandomMapSettings& settings )void RandomMapGenerator::generate( const RandomMapSettings& settings, std::vector<char>& mapBytes ){+ // The light is multiplied out while the bytes are written, after build() has handed the FPU+ // back, so the writing runs in the simulation's rounding mode as well.+ UnsignedInt callersFPMode = getFPMode();+ setFPMode();+RMGLayout layout;layout.build( settings );@@ -7042,7 +7093,9 @@ void RandomMapGenerator::generate( const RandomMapSettings& settings, std::vecto// Must come before the sides chunk.w.openChunk( "WorldInfo", K_WORLDDICT_VERSION_1 );w.beginDict( 2 );- w.dictInt( "weather", 0 );+ // WorldHeightMapData copies it into GlobalData, where the buildings and the props read it+ // to pick their snow models; the falling snow is the map.ini's, see generatedMapBytes+ w.dictInt( "weather", layout.m_snowy ? WEATHER_SNOWY : WEATHER_NORMAL );w.dictInt( "compression", 0 );w.closeChunk();@@ -7067,7 +7120,7 @@ void RandomMapGenerator::generate( const RandomMapSettings& settings, std::vectowritePolygonTriggers( w, layout );/***************GLOBAL LIGHTING DATA ***************/- writeGlobalLighting( w, *layout.m_biome );+ writeGlobalLighting( w, *layout.m_biome, layout.m_timeOfDay );/***************WAYPOINT LINKS ***************/// read after every waypoint exists (TerrainLogic::loadMap), so its place in the file is free@@ -7081,6 +7134,8 @@ void RandomMapGenerator::generate( const RandomMapSettings& settings, std::vectow.closeChunk();w.finish( mapBytes );++ restoreFPMode( callersFPMode );}UnsignedInt RandomMapGenerator::fingerprint( const RandomMapSettings& settings )@@ -7270,10 +7325,46 @@ static Bool settingsFromGeneratedPath( const AsciiString& path, RandomMapSetting&& clamped.m_playableCells == settingsOut.m_playableCells;}+/** The falling snow is not map data: SnowManager draws it when the Weather block says+ SnowEnabled, and a shipped winter map turns that on from the map.ini in its folder. A generated+ map that snows gets one too, served from here the way its bytes are; the values are Bitter+ Winter's. GameLogic loads it as an override and drops it at the next reset, so a map that does+ not snow has no map.ini and keeps the stock Weather.ini. */+static const char theSnowRules[] =+ "Weather\r\n"+ " SnowEnabled = Yes\r\n"+ " SnowTexture = ExSnowFlake1.tga\r\n"+ " SnowBoxDimensions = 100\r\n"+ " SnowBoxDensity = 1\r\n"+ " SnowFrequencyScaleX = 0.0533\r\n"+ " SnowFrequencyScaleY = 0.0275\r\n"+ " SnowAmplitude = 4.0\r\n"+ " SnowVelocity = 3.0\r\n"+ " SnowPointSize = 0.16\r\n"+ " SnowMaxPointSize = 10.0\r\n"+ " SnowMinPointSize = 0.0\r\n"+ " SnowPointSprites = Yes\r\n"+ " SnowQuadSize = 0.5\r\n"+ "End\r\n";++/// "<generated map folder>\map.ini", for a seed that snows.+static Bool isSnowRulesPath( const AsciiString& path )+{+ AsciiString folder = path;+ folder.toLower();+ if( !folder.endsWith( "\\map.ini" ) )+ return FALSE;+ for( Int i = 0; i < 8; i++ )+ folder.removeLastChar();++ RandomMapSettings settings;+ return settingsFromGeneratedPath( folder, settings ) && rmgSnowsOn( settings.m_seed );+}+Bool isGeneratedMapPath( const AsciiString& path ){RandomMapSettings settings;- return settingsFromGeneratedPath( path, settings );+ return settingsFromGeneratedPath( path, settings ) || isSnowRulesPath( path );}/** The slot holding this map, or NULL. Slots are looked up by what they were built from rather@@ -7339,6 +7430,15 @@ Bool stageRandomMap( const RandomMapSettings& settings, AsciiString& mapPathOutBool generatedMapBytes( const AsciiString& path, const char **bytesOut, Int *sizeOut ){+ if( isSnowRulesPath( path ) )+ {+ if( bytesOut )+ *bytesOut = theSnowRules;+ if( sizeOut )+ *sizeOut = (Int)sizeof(theSnowRules) - 1;+ return TRUE;+ }+RandomMapSettings settings;if( !settingsFromGeneratedPath( path, settings ) )return FALSE;diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex da0f9c46f..1ab0bb893 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -7985,7 +7985,8 @@ struct RMGParseInt m_numTextureClasses, m_firstTile, m_numTiles, m_tileWidth;Int m_numSides, m_numTeams, m_numObjects, m_numWaterAreas;Int m_weather, m_compression, m_timeOfDay, m_lightingBytesLeftOver, m_blendBytesLeftOver;- Real m_terrainAmbient[3];+ Real m_terrainAmbient[4][3]; ///< per hour, morning first, as the chunk orders them+ Real m_terrainDiffuse[4][3];AsciiString m_textureName;std::vector<AsciiString> m_textureNames;std::vector<Int> m_firstTiles;@@ -8013,7 +8014,8 @@ struct RMGParsem_weather(-1), m_compression(-1), m_timeOfDay(-1), m_lightingBytesLeftOver(-1),m_blendBytesLeftOver(-1){- m_terrainAmbient[0] = m_terrainAmbient[1] = m_terrainAmbient[2] = 0.0f;+ memset( m_terrainAmbient, 0, sizeof(m_terrainAmbient) );+ memset( m_terrainDiffuse, 0, sizeof(m_terrainDiffuse) );}};static RMGParse theRMGParse;@@ -8112,8 +8114,10 @@ static Bool RMGParseLighting( DataChunkInput &file, DataChunkInfo *info, void *for( Int value = 0; value < 54; value++ ){Real read = file.readReal();- if( timeOfDay == 0 && value < 3 )- theRMGParse.m_terrainAmbient[value] = read;+ if( value < 3 )+ theRMGParse.m_terrainAmbient[timeOfDay][value] = read;+ else if( value < 6 )+ theRMGParse.m_terrainDiffuse[timeOfDay][value - 3] = read;}}@@ -8354,10 +8358,91 @@ TEST(a_generated_map_reads_back_through_the_engines_own_chunk_reader)CHECK_EQ( (Int)theRMGParse.m_waterPoints.size(), theRMGParse.m_numWaterAreas );CHECK( theRMGParse.m_waterPoints[0].z > 0.0f );- // Daylight, written into the map rather than left to whatever GameData.ini holds.- CHECK_EQ( theRMGParse.m_timeOfDay, (Int)TIME_OF_DAY_AFTERNOON );+ // The light, written into the map rather than left to whatever GameData.ini holds.+ CHECK( theRMGParse.m_timeOfDay >= (Int)TIME_OF_DAY_FIRST );+ CHECK( theRMGParse.m_timeOfDay < (Int)TIME_OF_DAY_COUNT );CHECK_EQ( theRMGParse.m_lightingBytesLeftOver, 0 );- CHECK( theRMGParse.m_terrainAmbient[0] > 0.2f );+ CHECK( theRMGParse.m_terrainAmbient[TIME_OF_DAY_AFTERNOON - TIME_OF_DAY_FIRST][0] > 0.2f );+}++//-------------------------------------------------------------------------------------------------+/** The seed rolls the hour as well as the ground: all four come up, each lit as itself, and a+ winter map snows - snowed roofs from the WorldInfo weather, falling snow from a map.ini the+ store serves beside it. No other ground snows, and no other map has a map.ini. */+//-------------------------------------------------------------------------------------------------+TEST(a_seed_rolls_its_hour_and_a_winter_map_snows)+{+ CHECK( bootOnce() );++ Int hours[TIME_OF_DAY_COUNT] = { 0 };+ Int winterMaps = 0, otherMaps = 0, plainShopsAfterDark = 0;+ const Int seeds = 120;+ for( Int seed = 1; seed <= seeds; seed++ )+ {+ RandomMapSettings settings;+ settings.m_seed = seed;+ settings.m_playableCells = 64;+ settings.m_numPlayers = 2;++ std::vector<char> bytes;+ RandomMapGenerator::generate( settings, bytes );+ parseGeneratedMap( bytes );++ const Int hour = theRMGParse.m_timeOfDay;+ CHECK( hour >= (Int)TIME_OF_DAY_FIRST && hour < (Int)TIME_OF_DAY_COUNT );+ if( hour >= (Int)TIME_OF_DAY_FIRST && hour < (Int)TIME_OF_DAY_COUNT )+ hours[hour]++;++ const Bool winter = theRMGParse.m_textureNames[0].compare( "SnowType1" ) == 0;+ CHECK_EQ( theRMGParse.m_weather, winter ? (Int)WEATHER_SNOWY : (Int)WEATHER_NORMAL );+ winter ? winterMaps++ : otherMaps++;++ AsciiString rules;+ rules.format( "Maps\\RMG_v%d_%d_2p_64c\\map.ini", RANDOM_MAP_GENERATOR_VERSION, seed );+ const char *rulesBytes = NULL;+ Int rulesSize = 0;+ CHECK_EQ( isGeneratedMapPath( rules ), winter );+ CHECK_EQ( generatedMapBytes( rules, &rulesBytes, &rulesSize ), winter );+ if( winter && rulesBytes )+ {+ CHECK( std::string( rulesBytes, rulesSize ).find( "SnowEnabled = Yes" ) != std::string::npos );+ }++ // the one shop with no night or snow model stays off a map that would show it bare+ if( winter || hour == (Int)TIME_OF_DAY_NIGHT )+ {+ for( Int i = 0; i < (Int)theRMGParse.m_objectNames.size(); i++ )+ {+ if( theRMGParse.m_objectNames[i].compare( "StanSmallRetail03" ) == 0 )+ plainShopsAfterDark++;+ }+ }++ // Each hour lit as itself: the evening redder and dimmer than the afternoon, the night blue+ // and dim but not black.+ const Real *noonDiffuse = theRMGParse.m_terrainDiffuse[TIME_OF_DAY_AFTERNOON - TIME_OF_DAY_FIRST];+ const Real *eveningDiffuse = theRMGParse.m_terrainDiffuse[TIME_OF_DAY_EVENING - TIME_OF_DAY_FIRST];+ const Real *nightDiffuse = theRMGParse.m_terrainDiffuse[TIME_OF_DAY_NIGHT - TIME_OF_DAY_FIRST];+ const Real *nightAmbient = theRMGParse.m_terrainAmbient[TIME_OF_DAY_NIGHT - TIME_OF_DAY_FIRST];+ CHECK( eveningDiffuse[2] < noonDiffuse[2] );+ CHECK( eveningDiffuse[0] / eveningDiffuse[2] > noonDiffuse[0] / noonDiffuse[2] );+ CHECK( nightDiffuse[0] < noonDiffuse[0] * 0.5f );+ CHECK( nightAmbient[2] > nightAmbient[0] );+ for( Int channel = 0; channel < 3; channel++ )+ {+ CHECK( nightDiffuse[channel] > 0.15f && nightAmbient[channel] > 0.08f );+ }+ }++ for( Int hour = TIME_OF_DAY_FIRST; hour < TIME_OF_DAY_COUNT; hour++ )+ {+ if( hours[hour] < seeds / 8 )+ printf( " hour %d came up %d times in %d seeds\n", hour, hours[hour], seeds );+ CHECK( hours[hour] >= seeds / 8 );+ }+ CHECK( winterMaps > 0 );+ CHECK( otherMaps > 0 );+ CHECK_EQ( plainShopsAfterDark, 0 );}//-------------------------------------------------------------------------------------------------@@ -9874,14 +9959,14 @@ TEST(the_generator_still_turns_a_seed_into_the_bytes_it_used_to){CHECK( bootOnce() );- CHECK_EQ( RANDOM_MAP_GENERATOR_VERSION, 13 );+ CHECK_EQ( RANDOM_MAP_GENERATOR_VERSION, 14 );struct RMGFingerprint { Int m_seed, m_players, m_cells; UnsignedInt m_crc; };static const RMGFingerprint theFingerprints[] ={- { 0, 2, 64, 0x3C830A0F },- { 12345, 4, 96, 0xAB2D49CF },- { 7, 8, 128, 0xF237C0BA },+ { 0, 2, 64, 0x47855C63 },+ { 12345, 4, 96, 0x2863C7C9 },+ { 7, 8, 128, 0x297288D3 },};const Int numFingerprints = sizeof(theFingerprints) / sizeof(theFingerprints[0]);