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): join generated town streets at crossings
Olcay Seygan1bc8d64d3851
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e972f5bad..76bdf9c47 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2246,6 +2246,13 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same- Streets stop at the water. A road paves whatever is under it, lake bed included, so a town on abank used to run its high street into the lake; now the street ends at the shore and the townkeeps its shape.+- Town streets meet in real junctions. Each street used to be one long strip laid straight over+ the others, so every crossing was two strips of tarmac on top of each other, the ends stuck out+ past the last street and the water left scraps of road a few yards long lying by the bank. The+ streets now join the way they do on the shipped maps: crossroads inside the town, T-junctions+ along its edge and a rounded corner where the outer streets turn. Over nine test maps the 70+ overlaps are gone and in their place are 33 T-junctions, 14 crossroads and 20 corners. A random+ map from an older version is a different map from the same number.- Nothing stands on a slope. The ground under every building, every supply dock and every oilderrick is levelled before it goes down and eased back into the hillside around it, so you do notarrive at a dock sunk into a bank or a house hanging off a ledge.diff --git a/GeneralsMD/Code/GameEngine/Include/Common/RandomMapGenerator.h b/GeneralsMD/Code/GameEngine/Include/Common/RandomMapGenerator.hindex 99812fd3b..dffe713bf 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 14+#define RANDOM_MAP_GENERATOR_VERSION 15/// How much ground the map gives everybody, before the per-player share.enum RandomMapSizediff --git a/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cppindex 3eadae219..3563d50a4 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp@@ -189,9 +189,9 @@ static const Int K_SCRIPT_LIST_DATA_VERSION_1 = 1;#define RMG_TOWN_JITTER 2.0f ///< cells a building sits off its own frontage line#define RMG_TOWN_DRY_MARGIN 4.0f ///< cells a building or a street keeps off the water#define RMG_TOWN_REROLLS 6 ///< plans a map with no town yet tries for its last one-// A street is laid in dry runs: what crosses water is dropped rather than driven into the lake.-#define RMG_ROAD_STEP 2.0f ///< cells between two wet-or-dry samples along a segment-#define RMG_ROAD_MIN_RUN 8.0f ///< shorter than this and the run is not worth a road+// A street is laid a block at a time, and a block that reaches water stops at the bank.+#define RMG_ROAD_STEP 2.0f ///< cells between two wet-or-dry samples along a block+#define RMG_ROAD_MIN_RUN 12.0f ///< cells; a street from a crossing to the bank shorter than this is a stub// Bunkers go on the ramps, which is the ground worth holding.#define RMG_BUNKER_CLEARANCE 10.0f@@ -697,6 +697,7 @@ struct RMGObjectlist here is written, never read back through MapObject. */#define RMG_FLAG_ROAD_POINT1 0x00000002#define RMG_FLAG_ROAD_POINT2 0x00000004+#define RMG_FLAG_ROAD_CORNER_TIGHT 0x00000040 ///< on POINT1: a corner turns on half a road width, a town corner/// A spot flat and dry enough for a base, found before any seat is chosen.struct RMGStartCandidate@@ -954,7 +955,7 @@ private:void plantGroves( const char *const *theTreeNames, Int *propID, Int *treeBudget );void flattenPad( Real cellX, Real cellY, Real radius, Real blend );void addRoad( Real fromX, Real fromY, Real toX, Real toY );- void addRoadClipped( Real fromX, Real fromY, Real toX, Real toY );+ void addRoadIfDry( Real fromX, Real fromY, Real toX, Real toY );Bool dryAt( Real cellX, Real cellY, Real margin ) const;Real cellSpanWorld( Int x, Int y ) const;@@ -4908,7 +4909,7 @@ void RMGLayout::addRoad( Real fromX, Real fromY, Real toX, Real toY )point.m_worldX = fromX * MAP_XY_FACTOR;point.m_worldY = fromY * MAP_XY_FACTOR;- point.m_flags = RMG_FLAG_ROAD_POINT1;+ point.m_flags = RMG_FLAG_ROAD_POINT1 | RMG_FLAG_ROAD_CORNER_TIGHT;m_objects.push_back( point );point.m_worldX = toX * MAP_XY_FACTOR;@@ -4926,45 +4927,43 @@ Bool RMGLayout::dryAt( Real cellX, Real cellY, Real margin ) constreturn distanceToShore >= margin;}-/** The dry parts of a street. A town beside a lake has streets that run at the water, and a road- object pair does not care whether the ground under it is a lake bed - it paves it, and what the- player sees is tarmac going into the water. This walks the line, keeps the runs that stay on dry- ground and drops the rest, so the street stops at the bank. */-void RMGLayout::addRoadClipped( Real fromX, Real fromY, Real toX, Real toY )+/** One block of street, between two crossings. A road object pair does not care whether the ground+ under it is a lake bed - it paves it, and what the player sees is tarmac going into the water. So+ a block that reaches the water keeps only the dry run from each crossing it starts at, and the run+ stops at the bank. Whatever dry ground lies between two wet stretches is left bare: it touches no+ crossing, and a few cells of street on their own are what the old clip left lying about. */+void RMGLayout::addRoadIfDry( Real fromX, Real fromY, Real toX, Real toY ){Real spanX = toX - fromX;Real spanY = toY - fromY;Real length = sqrtf( spanX * spanX + spanY * spanY );- if( length < RMG_ROAD_MIN_RUN )- return;-Int steps = (Int)(length / RMG_ROAD_STEP) + 1;- Real runStart = -1.0f;+ Int firstWet = -1, lastWet = -1;for( Int step = 0; step <= steps; step++ ){- Real along = (Real)step * length / (Real)steps;- Real x = fromX + spanX * (along / length);- Real y = fromY + spanY * (along / length);- Bool dry = (step < steps) && dryAt( x, y, RMG_TOWN_DRY_MARGIN );-- if( dry && runStart < 0.0f )- {- runStart = along;+ Real along = (Real)step / (Real)steps;+ if( dryAt( fromX + spanX * along, fromY + spanY * along, RMG_TOWN_DRY_MARGIN ) )continue;- }+ if( firstWet < 0 )+ firstWet = step;+ lastWet = step;+ }- if( dry || runStart < 0.0f )- continue;+ if( firstWet < 0 )+ {+ addRoad( fromX, fromY, toX, toY );+ return;+ }- if( along - runStart >= RMG_ROAD_MIN_RUN )- {- addRoad( fromX + spanX * (runStart / length), fromY + spanY * (runStart / length),- fromX + spanX * (along / length), fromY + spanY * (along / length) );- }+ // the crossing ends keep their exact numbers, which is what joins them to the other streets+ Real dryFrom = (Real)(firstWet - 1) / (Real)steps;+ if( firstWet > 0 && dryFrom * length >= RMG_ROAD_MIN_RUN )+ addRoad( fromX, fromY, fromX + spanX * dryFrom, fromY + spanY * dryFrom );- runStart = -1.0f;- }+ Real dryTo = (Real)(lastWet + 1) / (Real)steps;+ if( lastWet < steps && ( 1.0f - dryTo ) * length >= RMG_ROAD_MIN_RUN )+ addRoad( fromX + spanX * dryTo, fromY + spanY * dryTo, toX, toY );}/** The flattest buildable spot in a ring around a point, whatever else is on the map. This is how@@ -5750,25 +5749,35 @@ void RMGLayout::placeTowns( void )gradeTown( site.m_cellX, site.m_cellY, plan, acrossStart, acrossEnd, downStart, downEnd );+ /* The streets are a lattice of blocks between crossings, and every crossing is one point worked+ out once. W3DRoadBuffer joins segments only where their ends are the same numbers: four ends+ on a point make a crossroads, three a tee, two at a right angle a corner it rounds off. A+ street laid end to end across the others, as they used to be, is drawn as strips of tarmac+ laid over each other with no junction at all. The outermost streets end at the corners, so+ the town is ringed by a street with its four corners turned. */Int line;- Real fromX, fromY, toX, toY;+ Int across, down;+ Real crossingX[RMG_TOWN_MAX_STREETS][RMG_TOWN_MAX_STREETS];+ Real crossingY[RMG_TOWN_MAX_STREETS][RMG_TOWN_MAX_STREETS];+ for( across = 0; across < plan.m_streetsAcross; across++ )+ {+ for( down = 0; down < plan.m_streetsDown; down++ )+ townToWorld( site.m_cellX, site.m_cellY, plan, plan.m_acrossAt[across], plan.m_downAt[down],+ &crossingX[across][down], &crossingY[across][down] );+ }- for( line = 0; line < plan.m_streetsAcross; line++ )+ for( across = 0; across < plan.m_streetsAcross; across++ ){- townToWorld( site.m_cellX, site.m_cellY, plan, plan.m_acrossAt[line], downStart,- &fromX, &fromY );- townToWorld( site.m_cellX, site.m_cellY, plan, plan.m_acrossAt[line], downEnd,- &toX, &toY );- addRoadClipped( fromX, fromY, toX, toY );+ for( down = 0; down + 1 < plan.m_streetsDown; down++ )+ addRoadIfDry( crossingX[across][down], crossingY[across][down],+ crossingX[across][down + 1], crossingY[across][down + 1] );}- for( line = 0; line < plan.m_streetsDown; line++ )+ for( down = 0; down < plan.m_streetsDown; down++ ){- townToWorld( site.m_cellX, site.m_cellY, plan, acrossStart, plan.m_downAt[line],- &fromX, &fromY );- townToWorld( site.m_cellX, site.m_cellY, plan, acrossEnd, plan.m_downAt[line],- &toX, &toY );- addRoadClipped( fromX, fromY, toX, toY );+ for( across = 0; across + 1 < plan.m_streetsAcross; across++ )+ addRoadIfDry( crossingX[across][down], crossingY[across][down],+ crossingX[across + 1][down], crossingY[across + 1][down] );}/* Buildings stand back from a street on both sides, facing it, down its whole length. Thediff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 1a17b3070..14290a7a7 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -10284,6 +10284,127 @@ TEST(a_town_is_rolled_rather_than_stamped)CHECK( differentLayouts > 0 );}+//-------------------------------------------------------------------------------------------------+/** W3DRoadBuffer only builds a junction where segments share an end point exactly: three ends on+ one point are a tee, four a crossroads, two at an angle a curve. Two streets that cross half way+ along are drawn as two strips of tarmac laid over each other. So a town's streets are split at+ every crossing, nothing touches another street anywhere but at a shared end, and the water no+ longer leaves a few cells of street lying on their own: a street that reaches the bank keeps the+ run from its crossing, and only if that run is long enough to read as a street. */+//-------------------------------------------------------------------------------------------------+TEST(town_streets_meet_only_at_shared_ends_so_the_renderer_builds_junctions)+{+ CHECK( bootOnce() );++ const Real shortestStreet = 12.0f * MAP_XY_FACTOR; // RMG_ROAD_MIN_RUN+ Int tees = 0, crossroads = 0, bends = 0, crossings = 0, stubs = 0, loose = 0, segmentsSeen = 0;++ static const Int thePlayers[] = { 2, 4, 8 };+ for( Int p = 0; p < 3; p++ )+ {+ for( Int seed = 1; seed <= 3; seed++ )+ {+ RandomMapSettings settings;+ settings.m_seed = seed;+ settings.m_numPlayers = thePlayers[p];+ settings.m_playableCells = RandomMapGenerator::cellsFor( RANDOM_MAP_SIZE_NORMAL, thePlayers[p] );++ std::vector<char> bytes;+ RandomMapGenerator::generate( settings, bytes );+ parseGeneratedMap( bytes );++ std::vector<Coord3D> from, to;+ for( Int i = 0; i + 1 < (Int)theRMGParse.m_objectNames.size(); i++ )+ {+ if( (theRMGParse.m_objectFlags[i] & FLAG_ROAD_POINT1) == 0 )+ continue;+ CHECK( (theRMGParse.m_objectFlags[i + 1] & FLAG_ROAD_POINT2) != 0 );+ from.push_back( theRMGParse.m_objectPositions[i] );+ to.push_back( theRMGParse.m_objectPositions[i + 1] );+ }+ segmentsSeen += (Int)from.size();++ std::map< std::pair<Real, Real>, std::vector<Int> > ends; // end point to the segments there+ for( Int s = 0; s < (Int)from.size(); s++ )+ {+ ends[std::make_pair( from[s].x, from[s].y )].push_back( s );+ ends[std::make_pair( to[s].x, to[s].y )].push_back( s );++ Real dx = to[s].x - from[s].x, dy = to[s].y - from[s].y;+ if( sqrtf( dx * dx + dy * dy ) < shortestStreet - 1.0f )+ stubs++;+ }++ // a piece of street that shares neither end with another lies on its own+ for( Int s = 0; s < (Int)from.size(); s++ )+ {+ if( ends[std::make_pair( from[s].x, from[s].y )].size() == 1 &&+ ends[std::make_pair( to[s].x, to[s].y )].size() == 1 )+ loose++;+ }++ for( std::map< std::pair<Real, Real>, std::vector<Int> >::const_iterator it = ends.begin(); it != ends.end(); ++it )+ {+ const Int degree = (Int)it->second.size();+ if( degree == 3 )+ tees++;+ else if( degree == 4 )+ crossroads++;+ else if( degree == 2 )+ {+ // the two ways out of the point; a turn of 30 degrees or more gets a curve+ Real way[2][2];+ for( Int k = 0; k < 2; k++ )+ {+ const Int s = it->second[k];+ const Bool atFrom = from[s].x == it->first.first && from[s].y == it->first.second;+ const Coord3D& away = atFrom ? to[s] : from[s];+ Real dx = away.x - it->first.first, dy = away.y - it->first.second;+ Real length = sqrtf( dx * dx + dy * dy );+ way[k][0] = dx / length;+ way[k][1] = dy / length;+ }+ if( way[0][0] * way[1][0] + way[0][1] * way[1][1] > -0.866f )+ bends++;+ }+ }++ // any touch away from an end both segments share is a crossing the renderer cannot join+ for( Int a = 0; a < (Int)from.size(); a++ )+ {+ for( Int b = a + 1; b < (Int)from.size(); b++ )+ {+ Real rx = to[a].x - from[a].x, ry = to[a].y - from[a].y;+ Real sx = to[b].x - from[b].x, sy = to[b].y - from[b].y;+ Real denom = rx * sy - ry * sx;+ if( fabsf( denom ) < 1.0e-3f )+ continue;+ Real qx = from[b].x - from[a].x, qy = from[b].y - from[a].y;+ Real t = (qx * sy - qy * sx) / denom;+ Real u = (qx * ry - qy * rx) / denom;+ if( t < -1.0e-4f || t > 1.0f + 1.0e-4f || u < -1.0e-4f || u > 1.0f + 1.0e-4f )+ continue;+ const Bool shared =+ ( from[a].x == from[b].x && from[a].y == from[b].y ) || ( from[a].x == to[b].x && from[a].y == to[b].y ) ||+ ( to[a].x == from[b].x && to[a].y == from[b].y ) || ( to[a].x == to[b].x && to[a].y == to[b].y );+ if( !shared )+ crossings++;+ }+ }+ }+ }++ printf( " town streets: %d segments, %d tees, %d crossroads, %d bends, %d crossings, %d stubs, %d loose\n",+ segmentsSeen, tees, crossroads, bends, crossings, stubs, loose );+ CHECK( segmentsSeen > 0 );+ CHECK_EQ( crossings, 0 );+ CHECK_EQ( stubs, 0 );+ CHECK_EQ( loose, 0 );+ CHECK( tees > 0 );+ CHECK( crossroads > 0 );+ CHECK( bends > 0 );+}+//-------------------------------------------------------------------------------------------------/** A map has to hold the players it is generated for. The three sizes are three different mapsfor the same game, and every one of them grows with the number of players rather than packing@@ -10828,14 +10949,14 @@ TEST(the_generator_still_turns_a_seed_into_the_bytes_it_used_to){CHECK( bootOnce() );- CHECK_EQ( RANDOM_MAP_GENERATOR_VERSION, 14 );+ CHECK_EQ( RANDOM_MAP_GENERATOR_VERSION, 15 );struct RMGFingerprint { Int m_seed, m_players, m_cells; UnsignedInt m_crc; };static const RMGFingerprint theFingerprints[] ={- { 0, 2, 64, 0x47855C63 },- { 12345, 4, 96, 0x2863C7C9 },- { 7, 8, 128, 0x297288D3 },+ { 0, 2, 64, 0x47856063 },+ { 12345, 4, 96, 0x2863C7D9 },+ { 7, 8, 128, 0x2D7288D3 },};const Int numFingerprints = sizeof(theFingerprints) / sizeof(theFingerprints[0]);