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.

feat(gameengine): roll each town out of the seed, keep streets out of the water

Olcay Seygan918a6ac8e4f2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index db51cbeeb..107c1b570 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1023,18 +1023,24 @@ found and fixed — EA's own, not port damage.**
- The maps are twice the size they were. A two-player map is a quarter of a million square feet of
ground, an eight-player one nearly three times that, which is room to manoeuvre round a flank
instead of running into the enemy on the way out of your own base.
-- There are towns on it. The civilian buildings used to stand about in the grass in loose clumps;
- now they come in blocks with streets running between them, shops and apartments set back from the
- kerb and all facing the road. A town is somewhere to fight through, and the roads are real roads,
- so what drives down one drives faster.
+- There are towns on it, and no two of them are the same town. Each one is built out of the number
+ the map came from: how many streets it has, how big its blocks are, how deep the plots run and
+ which way the whole grid faces. Shops and apartments stand back from the kerb facing the road,
+ with yards and corners left empty between them and the junctions kept clear. A town is somewhere
+ to fight through, and the roads are real roads, so what drives down one drives faster.
+- Streets stop at the water. A road paves whatever is under it, lake bed included, so a town on a
+ bank used to run its high street into the lake; now the street ends at the shore and the town
+ keeps its shape.
- Nothing stands on a slope. The ground under every building, every supply dock and every oil
derrick is levelled before it goes down and eased back into the hillside around it, so you do not
arrive at a dock sunk into a bank or a house hanging off a ledge.
- The bunkers moved out of the towns and onto the ramps. A bunker now sits where the route between
two levels is, which is the ground worth holding, and it comes with room around it to fight over.
-- The water has a shore. The lake bed shelves away from the bank instead of dropping off it, so
- there is shallow water at the edge with the light coming through it, and you can hear it from the
- bank.
+- The water has a shore, and it is in a hollow. The lake bed shelves away from the bank instead of
+ dropping off it, so there is shallow water at the edge with the light coming through it, and you
+ can hear it from the bank. The ground around a lake is above the water it holds, which sounds
+ obvious and was not: lakes are cut into the lowest ground on the map, and the land further out
+ used to lie lower than the water, so a lake read as a puddle sitting on top of the map.
- Everybody opens with the same money. One supply dock beside each base, a second one out between
you and your nearest neighbour to fight over, and two oil derricks apiece within reach of home.
- The number that made the map is in its name, so the same number always makes the same map, on
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp
index af0ece15b..ac7ad19f3 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp
@@ -102,7 +102,12 @@ static const Int K_SCRIPT_LIST_DATA_VERSION_1 = 1;
#define RMG_LAKE_DEPTH 13.0f ///< and how far the bed sits below that surface
// Wide, because the shore is what the renderer's soft water edge is drawn on: it looks for cells
// whose corners straddle the water plane, and a bank that drops in one step gives it nothing.
-#define RMG_LAKE_SHORE 10.0f ///< cells the basin eases out over
+#define RMG_LAKE_SHORE 18.0f ///< cells the basin eases out over
+/* Dry land has to be dry. A lake is a polygon, not a flood, so ground outside one that sits below
+ the water surface is not filled in - it is drawn as land with the lake standing above it like a
+ puddle on a table, which is exactly what the noise leaves behind when it puts a lake in the
+ lowest ground it can find. */
+#define RMG_LAKE_BANK 4.0f ///< height bytes the land outside a lake is kept above it
#define RMG_WAVE_SPACING 16.0f ///< cells of shoreline between two ambient wave emitters
#define RMG_LAKE_RADIUS 0.055f ///< fraction of the playable size
#define RMG_LAKE_MIN_CELLS 96
@@ -129,14 +134,28 @@ static const Int K_SCRIPT_LIST_DATA_VERSION_1 = 1;
beside it either. The pad is flat to its radius and eases back into the terrain over the blend. */
#define RMG_PAD_RADIUS 5.0f
#define RMG_PAD_BLEND 10.0f
-
-// A town: streets on a grid, buildings in the blocks between them, all of it on one level.
-#define RMG_TOWN_BLOCK 26.0f ///< cells between two street centre lines
-#define RMG_TOWN_STREETS 4 ///< streets each way, so three blocks by three
-#define RMG_TOWN_SET_BACK 6.0f ///< cells from the street centre to a building front
-#define RMG_TOWN_PLOT 11.0f ///< cells of frontage each building takes along a street
+#define RMG_PAD_SHORE_KEEP 2.0f ///< cells of beach a pad never touches
+
+/* A town. Nothing here is a fixed number: each town rolls how many streets it has each way, how far
+ apart they run, how deep the plots are, and which way the whole grid is turned. Two towns on one
+ map are two different towns. */
+#define RMG_TOWN_MIN_STREETS 3
+#define RMG_TOWN_MAX_STREETS 6
+#define RMG_TOWN_BLOCK_MIN 18.0f ///< cells between two street centre lines, at the closest
+#define RMG_TOWN_BLOCK_MAX 34.0f ///< and at the widest
+#define RMG_TOWN_PLOT_MIN 9.0f ///< cells of frontage a building takes along a street
+#define RMG_TOWN_PLOT_MAX 14.0f
+#define RMG_TOWN_SET_BACK_MIN 5.0f ///< cells from the street centre to a building front
+#define RMG_TOWN_SET_BACK_MAX 8.0f
+#define RMG_TOWN_GAP_IN_100 22 ///< plots left empty, so a street has yards and corners
+#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_ROAD "TwoLane"
+// 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
+
// Bunkers go on the ramps, which is the ground worth holding.
#define RMG_BUNKER_CLEARANCE 10.0f
@@ -462,6 +481,96 @@ struct RMGObject
#define RMG_FLAG_ROAD_POINT1 0x00000002
#define RMG_FLAG_ROAD_POINT2 0x00000004
+/** The shape one town was rolled to be: how many streets it has each way, where each one runs in
+ the town's own coordinates, how deep its plots are and which way the whole grid is turned. It is
+ rolled before a site is looked for, because the search needs the radius to keep the streets
+ inside the map. */
+struct RMGTownPlan
+{
+ Real m_rotation; ///< radians the grid is turned by, so no two towns face the same way
+ Int m_streetsAcross;
+ Int m_streetsDown;
+ Real m_acrossAt[RMG_TOWN_MAX_STREETS]; ///< where each across-street runs, town coordinates
+ Real m_downAt[RMG_TOWN_MAX_STREETS];
+ Real m_plotLength;
+ Real m_setBack;
+ Real m_radius; ///< from the middle to the far side of the outermost frontage
+};
+
+/// A hash read as a fraction of one, which is how every choice a town makes is made.
+static Real hashUnit( Int seed, Int a, Int b )
+{
+ return (Real)(hashCell( seed, a, b ) % 4096U) / 4096.0f;
+}
+
+/// Where one direction's streets run, spaced unevenly and centred on the town. Returns the span.
+static Real rollStreetLines( Int seed, Int town, Int streets, Real *out )
+{
+ Real at = 0.0f;
+ Int line;
+
+ for( line = 0; line < streets; line++ )
+ {
+ out[line] = at;
+ at += lerpReal( RMG_TOWN_BLOCK_MIN, RMG_TOWN_BLOCK_MAX, hashUnit( seed, town, line ) );
+ }
+
+ Real span = out[streets - 1];
+ for( line = 0; line < streets; line++ )
+ out[line] -= span * 0.5f;
+
+ return span;
+}
+
+static void rollTownPlan( Int seed, Int town, RMGTownPlan *plan )
+{
+ // A square grid repeats every quarter turn, so a quarter turn is the whole choice.
+ plan->m_rotation = hashUnit( seed + 613, town, 0 ) * PI * 0.5f;
+
+ const UnsignedInt streetChoices = (UnsignedInt)(RMG_TOWN_MAX_STREETS - RMG_TOWN_MIN_STREETS + 1);
+ plan->m_streetsAcross = RMG_TOWN_MIN_STREETS +
+ (Int)(hashCell( seed + 613, town, 1 ) % streetChoices);
+ plan->m_streetsDown = RMG_TOWN_MIN_STREETS +
+ (Int)(hashCell( seed + 613, town, 2 ) % streetChoices);
+
+ plan->m_plotLength = lerpReal( RMG_TOWN_PLOT_MIN, RMG_TOWN_PLOT_MAX,
+ hashUnit( seed + 613, town, 3 ) );
+ plan->m_setBack = lerpReal( RMG_TOWN_SET_BACK_MIN, RMG_TOWN_SET_BACK_MAX,
+ hashUnit( seed + 613, town, 4 ) );
+
+ Real spanAcross = rollStreetLines( seed + 6131, town, plan->m_streetsAcross, plan->m_acrossAt );
+ Real spanDown = rollStreetLines( seed + 6133, town, plan->m_streetsDown, plan->m_downAt );
+ Real span = (spanAcross > spanDown) ? spanAcross : spanDown;
+
+ plan->m_radius = span * 0.5f + plan->m_setBack + 4.0f;
+}
+
+/// A point in the town's own coordinates, put where the map has it.
+static void townToWorld( Real centreX, Real centreY, const RMGTownPlan& plan, Real across, Real down,
+ Real *outX, Real *outY )
+{
+ Real cosine = Cos( plan.m_rotation );
+ Real sine = Sin( plan.m_rotation );
+
+ *outX = centreX + across * cosine - down * sine;
+ *outY = centreY + across * sine + down * cosine;
+}
+
+/// How far a plot is from the nearest street crossing it, so junctions are left as junctions.
+static Real nearestStreetDistance( const Real *streetAt, Int streets, Real value )
+{
+ Real nearest = 1.0e9f;
+
+ for( Int line = 0; line < streets; line++ )
+ {
+ Real distance = fabsf( value - streetAt[line] );
+ if( distance < nearest )
+ nearest = distance;
+ }
+
+ return nearest;
+}
+
/** A lake is a circle whose radius is a noise field of its own, sampled once per outline point and
interpolated in between, so the shore wanders the way a shore does and the polygon the engine
gets is the same shape as the basin that was carved. */
@@ -556,6 +665,8 @@ private:
void placeScenery( const UnsignedByte perm[512] );
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 );
+ Bool dryAt( Real cellX, Real cellY, Real margin ) const;
Real cellSpanWorld( Int x, Int y ) const;
Real roughnessAt( Int cellX, Int cellY, Int radius ) const;
@@ -865,6 +976,29 @@ void RMGLayout::carveLakeBasins( void )
m_heights[cellIndex( x, y )] = (UnsignedByte)(h + 0.5f);
}
}
+
+ // And everything that is not a lake comes up out of the water.
+ Real bank = m_waterHeight + RMG_LAKE_BANK;
+
+ for( Int landY = 0; landY < m_height; landY++ )
+ {
+ for( Int landX = 0; landX < m_width; landX++ )
+ {
+ /* Measured rather than read off the lake mask, and a cell short of the rim counts as
+ land: the water area the map ships is a 32-sided polygon through the outline, so the
+ cells between a chord and the arc it cuts are outside the water the game draws and
+ have to be dry ground like any other. */
+ Real distanceToShore;
+ insideLake( (Real)(landX - RMG_BORDER_CELLS), (Real)(landY - RMG_BORDER_CELLS),
+ &distanceToShore );
+ if( distanceToShore < -1.0f )
+ continue;
+
+ Int index = cellIndex( landX, landY );
+ if( (Real)m_heights[index] < bank )
+ m_heights[index] = (UnsignedByte)(bank + 0.5f);
+ }
+ }
}
//-----------------------------------------------------------------------------
@@ -1403,7 +1537,9 @@ void RMGLayout::buildTerrainClasses( const UnsignedByte perm[512] )
UnsignedByte terrainClass = RMG_TERRAIN_GRASS;
- if( height < m_waterHeight + 3.0f + wobble * 2.0f )
+ // Sand runs from the water up the whole beach, which is what makes a lake read as a lake
+ // with a shore rather than a hole of water cut in the grass.
+ if( height < m_waterHeight + 8.0f + wobble * 3.0f )
terrainClass = RMG_TERRAIN_SAND;
else if( span > RMG_CLIFF_WORLD_SPAN * (0.45f + wobble * 0.12f) )
terrainClass = RMG_TERRAIN_ROCK;
@@ -1662,6 +1798,18 @@ void RMGLayout::flattenPad( Real cellX, Real cellY, Real radius, Real blend )
if( distance > radius )
t = (distance - radius) / (blend - radius);
+ /* The beach keeps its own profile. A pad that runs to the water's edge levels the shelf
+ the soft water edge is drawn on and leaves the bank standing over the lake like a
+ kerb, so the pad fades out as it comes up to the shore instead. */
+ Real distanceToShore;
+ insideLake( (Real)(x - RMG_BORDER_CELLS), (Real)(y - RMG_BORDER_CELLS), &distanceToShore );
+ if( distanceToShore < RMG_PAD_SHORE_KEEP )
+ continue;
+
+ Real shoreT = (distanceToShore - RMG_PAD_SHORE_KEEP) / RMG_LAKE_SHORE;
+ if( shoreT < 1.0f && t < 1.0f - shoreT )
+ t = 1.0f - shoreT;
+
Real h = lerpReal( level, (Real)m_heights[cellIndex( x, y )], fadeCurve( t ) );
if( h < 1.0f ) h = 1.0f;
if( h > 254.0f ) h = 254.0f;
@@ -1692,6 +1840,56 @@ void RMGLayout::addRoad( Real fromX, Real fromY, Real toX, Real toY )
m_objects.push_back( point );
}
+/// Whether a point is far enough from every lake to build or pave on.
+Bool RMGLayout::dryAt( Real cellX, Real cellY, Real margin ) const
+{
+ Real distanceToShore;
+ insideLake( cellX, cellY, &distanceToShore );
+
+ return 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 )
+{
+ 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;
+
+ 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;
+ continue;
+ }
+
+ if( dry || runStart < 0.0f )
+ continue;
+
+ if( along - runStart >= RMG_ROAD_MIN_RUN )
+ {
+ addRoad( fromX + spanX * (runStart / length), fromY + spanY * (runStart / length),
+ fromX + spanX * (along / length), fromY + spanY * (along / length) );
+ }
+
+ runStart = -1.0f;
+ }
+}
+
/** The flattest buildable spot in a ring around a point, whatever else is on the map. This is how
anything a particular player is meant to reach gets placed: search from their ground rather
than over the whole map, or every flat acre in the middle takes all the money and the players
@@ -1728,9 +1926,17 @@ Bool RMGLayout::findSiteNear( Real centreX, Real centreY, Real minRadius, Real m
if( !passableAtCell( mapX, mapY ) )
continue;
+ /* Off the water by something like what is being placed, capped: a town wants its middle
+ well clear of a lake, but demanding the whole grid's radius of dry ground would mean
+ no town on any map with water on it. What overhangs the bank is clipped where it is
+ laid instead. */
+ Real dryMargin = 4.0f + clearance * 0.5f;
+ if( dryMargin > 26.0f )
+ dryMargin = 26.0f;
+
Real distanceToShore;
insideLake( x, y, &distanceToShore );
- if( distanceToShore < 4.0f )
+ if( distanceToShore < dryMargin )
continue;
if( !siteIsClear( x, y, clearance ) )
@@ -1856,8 +2062,11 @@ void RMGLayout::placeSupplyAndDerricks( void )
}
}
-/** Civilian buildings come in small clusters facing the same way, the way a
- hamlet does, rather than one at a time wherever there is room. */
+/** A town is generated, not stamped. Each one rolls its own grid - how many streets it has each
+ way, how far apart they run, how deep its plots are, which way the whole thing faces - and the
+ buildings come off a list walked in order down each street with gaps left in it. Anything that
+ would land in a lake is dropped where it is laid rather than kept out by making the town smaller,
+ so a town on a bank keeps its shape and loses the street that ran into the water. */
void RMGLayout::placeTowns( void )
{
/* Two rows of frontage, because a street has two sides and a town where every building is the
@@ -1873,82 +2082,122 @@ void RMGLayout::placeTowns( void )
const Int numStreetNames = sizeof(theStreetNames) / sizeof(theStreetNames[0]);
Int buildingID = 1;
+ // Two at the least: one town on a map is a landmark, two are a place the fight moves between.
Int towns = m_settings.m_numPlayers / 2;
- if( towns < 1 )
- towns = 1;
-
- Real half = (Real)(RMG_TOWN_STREETS - 1) * RMG_TOWN_BLOCK * 0.5f;
- Real townRadius = half + RMG_TOWN_SET_BACK + 4.0f;
-
- // Out of everybody's base, in the ground between them, which is where a town is worth fighting
- // through rather than one more thing in somebody's back garden.
- Real inner = RMG_BLEND_RADIUS + townRadius + 6.0f;
- Real outer = inner + (Real)m_settings.m_playableCells * 0.22f;
+ if( towns < 2 )
+ towns = 2;
for( Int town = 0; town < towns; town++ )
{
+ RMGTownPlan plan;
+ rollTownPlan( m_settings.m_seed, town, &plan );
+
+ // Out of everybody's base, in the ground between them, which is where a town is worth
+ // fighting through rather than one more thing in somebody's back garden.
+ Real inner = RMG_BLEND_RADIUS + plan.m_radius + 6.0f;
+ Real outer = inner + (Real)m_settings.m_playableCells * 0.22f;
UnsignedInt which = (UnsignedInt)town % (UnsignedInt)m_starts.size();
RMGPoint site;
if( !findSiteNear( m_starts[which].m_cellX, m_starts[which].m_cellY, inner, outer,
- townRadius, &site ) )
+ plan.m_radius, &site ) )
continue;
// The whole town sits on one level: streets that run downhill through a terrace edge are
// streets with a cliff across them.
- flattenPad( site.m_cellX, site.m_cellY, townRadius, townRadius + 12.0f );
+ flattenPad( site.m_cellX, site.m_cellY, plan.m_radius, plan.m_radius + 12.0f );
UnsignedInt hash = hashCell( m_settings.m_seed + 613, (Int)site.m_cellX, (Int)site.m_cellY );
Int nameOffset = (Int)(hash % (UnsignedInt)numStreetNames);
+ Real acrossEnd = plan.m_acrossAt[plan.m_streetsAcross - 1] + plan.m_setBack;
+ Real acrossStart = plan.m_acrossAt[0] - plan.m_setBack;
+ Real downEnd = plan.m_downAt[plan.m_streetsDown - 1] + plan.m_setBack;
+ Real downStart = plan.m_downAt[0] - plan.m_setBack;
+
Int line;
- for( line = 0; line < RMG_TOWN_STREETS; line++ )
- {
- Real offset = (Real)line * RMG_TOWN_BLOCK - half;
+ Real fromX, fromY, toX, toY;
- addRoad( site.m_cellX + offset, site.m_cellY - half - RMG_TOWN_SET_BACK,
- site.m_cellX + offset, site.m_cellY + half + RMG_TOWN_SET_BACK );
- addRoad( site.m_cellX - half - RMG_TOWN_SET_BACK, site.m_cellY + offset,
- site.m_cellX + half + RMG_TOWN_SET_BACK, site.m_cellY + offset );
+ for( line = 0; line < plan.m_streetsAcross; line++ )
+ {
+ 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 );
}
- /* Buildings stand back from a street on both sides, facing it, in a row down its whole
- length. The plots either side of a crossing are left empty so the junction is a
- junction rather than a building with a road through it, and that gap is what makes the
- grid read as blocks with frontage instead of a field of buildings. */
- for( line = 0; line < RMG_TOWN_STREETS; line++ )
+ for( line = 0; line < plan.m_streetsDown; line++ )
{
- Real offset = (Real)line * RMG_TOWN_BLOCK - half;
+ 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( Real down = -half; down <= half + 0.5f; down += RMG_TOWN_PLOT )
+ /* Buildings stand back from a street on both sides, facing it, down its whole length. The
+ plots either side of a crossing are left empty so the junction stays a junction, and a
+ fifth of the rest are left empty too, which is what stops a street reading as a wall of
+ frontage with no yards, corners or car parks in it. */
+ for( Int direction = 0; direction < 2; direction++ )
+ {
+ const Real *streetAt = (direction == 0) ? plan.m_acrossAt : plan.m_downAt;
+ const Real *crossingAt = (direction == 0) ? plan.m_downAt : plan.m_acrossAt;
+ Int streets = (direction == 0) ? plan.m_streetsAcross : plan.m_streetsDown;
+ Int crossings = (direction == 0) ? plan.m_streetsDown : plan.m_streetsAcross;
+ Real alongStart = (direction == 0) ? downStart : acrossStart;
+ Real alongEnd = (direction == 0) ? downEnd : acrossEnd;
+
+ for( line = 0; line < streets; line++ )
{
- // How far this plot is from the nearest crossing street.
- Real fromCrossing = fabsf( fmodf( down + half, RMG_TOWN_BLOCK ) );
- if( fromCrossing > RMG_TOWN_BLOCK * 0.5f )
- fromCrossing = RMG_TOWN_BLOCK - fromCrossing;
- if( fromCrossing < RMG_TOWN_PLOT * 0.75f )
- continue;
+ Int plot = 0;
- for( Int side = 0; side < 2; side++ )
+ for( Real along = alongStart; along <= alongEnd + 0.5f; along += plan.m_plotLength )
{
- Real back = (side == 0) ? -RMG_TOWN_SET_BACK : RMG_TOWN_SET_BACK;
- Real facing = (side == 0) ? 0.0f : PI;
-
- AsciiString uniqueID;
- uniqueID.format( "Civilian %d", buildingID++ );
- addObject( theStreetNames[(nameOffset + buildingID) % numStreetNames],
- uniqueID.str(), site.m_cellX + offset + back,
- site.m_cellY + down, facing );
-
- uniqueID.format( "Civilian %d", buildingID++ );
- addObject( theStreetNames[(nameOffset + buildingID) % numStreetNames],
- uniqueID.str(), site.m_cellX + down,
- site.m_cellY + offset + back, facing + PI * 0.5f );
+ plot++;
+
+ if( nearestStreetDistance( crossingAt, crossings, along ) < plan.m_plotLength * 0.5f )
+ continue;
+
+ for( Int side = 0; side < 2; side++ )
+ {
+ UnsignedInt plotHash = hashCell( m_settings.m_seed + 6139,
+ town * 64 + line * 4 + side, plot );
+ if( plotHash % 100U < RMG_TOWN_GAP_IN_100 )
+ continue;
+
+ Real jitter = (hashUnit( m_settings.m_seed + 6141, town * 64 + line * 4 + side,
+ plot ) - 0.5f) * 2.0f * RMG_TOWN_JITTER;
+ Real back = (side == 0) ? -plan.m_setBack - jitter : plan.m_setBack + jitter;
+ Real facing = (side == 0) ? 0.0f : PI;
+
+ Real buildingX, buildingY;
+ if( direction == 0 )
+ {
+ townToWorld( site.m_cellX, site.m_cellY, plan, streetAt[line] + back, along,
+ &buildingX, &buildingY );
+ }
+ else
+ {
+ townToWorld( site.m_cellX, site.m_cellY, plan, along, streetAt[line] + back,
+ &buildingX, &buildingY );
+ facing += PI * 0.5f;
+ }
+
+ if( !dryAt( buildingX, buildingY, RMG_TOWN_DRY_MARGIN ) )
+ continue;
+
+ AsciiString uniqueID;
+ uniqueID.format( "Civilian %d", buildingID++ );
+ addObject( theStreetNames[(nameOffset + buildingID) % numStreetNames],
+ uniqueID.str(), buildingX, buildingY, plan.m_rotation + facing );
+ }
}
}
}
- reserveSite( site.m_cellX, site.m_cellY, townRadius + 4.0f );
+ reserveSite( site.m_cellX, site.m_cellY, plan.m_radius + 4.0f );
}
}
@@ -1961,9 +2210,14 @@ void RMGLayout::placeBunkers( void )
for( UnsignedInt i = 0; i < m_ramps.size(); i++ )
{
+ /* A ramp is a gap in a cliff, so the ground beside one is the ground the search likes least.
+ Rather than leave the ramp unwatched, the ask drops to less elbow room and then to a
+ wider ring before giving up on it. */
RMGPoint site;
if( !findSiteNear( m_ramps[i].m_cellX, m_ramps[i].m_cellY, 6.0f, 18.0f,
- RMG_BUNKER_CLEARANCE, &site ) )
+ RMG_BUNKER_CLEARANCE, &site ) &&
+ !findSiteNear( m_ramps[i].m_cellX, m_ramps[i].m_cellY, 6.0f, 18.0f, 5.0f, &site ) &&
+ !findSiteNear( m_ramps[i].m_cellX, m_ramps[i].m_cellY, 6.0f, 30.0f, 5.0f, &site ) )
continue;
if( distanceToNearestStart( site.m_cellX, site.m_cellY ) < RMG_BLEND_RADIUS + 8.0f )
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 43db2db52..aa5a589a9 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -6378,6 +6378,9 @@ struct RMGParse
std::vector<Short> m_blendIndexes; ///< per cell, into m_blends; 0 is no blend
std::vector<RMGBlendEntry> m_blends; ///< the table itself, entry 0 excepted
std::vector<Coord3D> m_waterPoints; ///< first point of each water area
+ std::vector< std::vector<Coord3D> > m_waterPolygons; ///< and every point of it
+ std::vector<Coord3D> m_objectPositions;
+ std::vector<Int> m_objectFlags; ///< the road flags, for the objects that carry them
RMGParse() :
m_width(0), m_height(0), m_border(0), m_boundaryX(0), m_boundaryY(0), m_dataSize(0),
@@ -6510,16 +6513,20 @@ static Bool RMGParseWaterAreas( DataChunkInput &file, DataChunkInfo *info, void
Int numPoints = file.readInt();
CHECK( numPoints >= 3 );
+ std::vector<Coord3D> polygon;
for( Int point = 0; point < numPoints; point++ )
{
Coord3D loc;
loc.x = (Real)file.readInt();
loc.y = (Real)file.readInt();
loc.z = (Real)file.readInt();
+ polygon.push_back( loc );
if( point == 0 )
theRMGParse.m_waterPoints.push_back( loc );
}
+
+ theRMGParse.m_waterPolygons.push_back( polygon );
}
return TRUE;
}
@@ -6557,12 +6564,14 @@ static Bool RMGParseObject( DataChunkInput &file, DataChunkInfo *info, void * )
loc.y = file.readReal();
loc.z = file.readReal();
file.readReal(); // angle
- file.readInt(); // flags
+ Int flags = file.readInt();
AsciiString name = file.readAsciiString();
Dict d = file.readDict();
theRMGParse.m_numObjects++;
theRMGParse.m_objectNames.push_back( name );
+ theRMGParse.m_objectPositions.push_back( loc );
+ theRMGParse.m_objectFlags.push_back( flags );
if( d.getType( NAMEKEY( "waypointID" ) ) == Dict::DICT_INT )
{
@@ -7115,6 +7124,173 @@ TEST(every_player_gets_two_supply_docks_and_two_derricks_whatever_the_seed)
}
}
+/// Whether a point falls inside one of the map's water polygons, by the usual crossing count.
+static Bool insideAWaterArea( Real x, Real y )
+{
+ for( Int area = 0; area < (Int)theRMGParse.m_waterPolygons.size(); area++ )
+ {
+ const std::vector<Coord3D>& polygon = theRMGParse.m_waterPolygons[area];
+ Bool inside = FALSE;
+
+ for( Int point = 0, last = (Int)polygon.size() - 1; point < (Int)polygon.size();
+ last = point++ )
+ {
+ if( (polygon[point].y > y) == (polygon[last].y > y) )
+ continue;
+
+ Real crossingX = polygon[point].x + (y - polygon[point].y) *
+ (polygon[last].x - polygon[point].x) / (polygon[last].y - polygon[point].y);
+ if( x < crossingX )
+ inside = !inside;
+ }
+
+ if( inside )
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** A road object pair does not care what is under it - it paves the ground, lake bed included, and
+ a street laid across a lake comes out as tarmac running into the water. The streets are clipped
+ to their dry runs and a building on a wet plot is dropped, and this is what says both still
+ happen. Towns are placed near water on purpose, so this test needs seeds with lakes in them. */
+//-------------------------------------------------------------------------------------------------
+TEST(no_street_or_civilian_building_stands_in_a_lake)
+{
+ CHECK( bootOnce() );
+
+ RandomMapSettings settings;
+ settings.m_numPlayers = 4;
+ settings.m_playableCells = RandomMapGenerator::cellsFor( RANDOM_MAP_SIZE_NORMAL, 4 );
+
+ Int mapsWithWater = 0;
+
+ for( Int seed = 1; seed <= 6; seed++ )
+ {
+ settings.m_seed = seed * 7919;
+
+ std::vector<char> bytes;
+ RandomMapGenerator::generate( settings, bytes );
+ parseGeneratedMap( bytes );
+
+ if( theRMGParse.m_waterPolygons.empty() )
+ continue;
+ mapsWithWater++;
+
+ for( Int i = 0; i < (Int)theRMGParse.m_objectNames.size(); i++ )
+ {
+ Bool isStreet = (theRMGParse.m_objectFlags[i] & (FLAG_ROAD_POINT1 | FLAG_ROAD_POINT2)) != 0;
+ Bool isBuilding = theRMGParse.m_objectNames[i].startsWith( "Stan" ) ||
+ theRMGParse.m_objectNames[i].startsWith( "Asian" ) ||
+ theRMGParse.m_objectNames[i].startsWith( "Civilian" );
+ if( !isStreet && !isBuilding )
+ continue;
+
+ CHECK( !insideAWaterArea( theRMGParse.m_objectPositions[i].x,
+ theRMGParse.m_objectPositions[i].y ) );
+ }
+ }
+
+ CHECK( mapsWithWater > 0 );
+}
+
+//-------------------------------------------------------------------------------------------------
+/** A lake is a polygon, not a flood: ground outside one that lies below the water surface is not
+ filled in, it is drawn as land with the water standing over it, and the lake reads as a puddle
+ sitting on top of the map. Lakes are carved into the lowest ground there is, so this is what the
+ noise leaves behind unless the land is lifted out of the water afterwards. */
+//-------------------------------------------------------------------------------------------------
+TEST(no_dry_ground_lies_below_the_water_surface)
+{
+ CHECK( bootOnce() );
+
+ Int mapsWithWater = 0;
+
+ for( Int seed = 1; seed <= 4; seed++ )
+ {
+ RandomMapSettings settings;
+ settings.m_seed = seed * 7919;
+ settings.m_numPlayers = 4;
+ settings.m_playableCells = RandomMapGenerator::cellsFor( RANDOM_MAP_SIZE_SMALL, 4 );
+
+ std::vector<char> bytes;
+ RandomMapGenerator::generate( settings, bytes );
+ parseGeneratedMap( bytes );
+
+ if( theRMGParse.m_waterPolygons.empty() )
+ continue;
+ mapsWithWater++;
+
+ Real waterZ = theRMGParse.m_waterPolygons[0][0].z;
+ CHECK( waterZ > 0.0f );
+
+ for( Int y = 0; y < theRMGParse.m_height; y += 2 )
+ {
+ for( Int x = 0; x < theRMGParse.m_width; x += 2 )
+ {
+ Real worldX = (Real)(x - theRMGParse.m_border) * MAP_XY_FACTOR;
+ Real worldY = (Real)(y - theRMGParse.m_border) * MAP_XY_FACTOR;
+ if( insideAWaterArea( worldX, worldY ) )
+ continue;
+
+ Real ground = (Real)theRMGParse.m_heights[y * theRMGParse.m_width + x] * MAP_HEIGHT_SCALE;
+ CHECK( ground >= waterZ );
+ }
+ }
+ }
+
+ CHECK( mapsWithWater > 0 );
+}
+
+//-------------------------------------------------------------------------------------------------
+/** Two towns on one map are two towns, not the same grid stamped twice. Each rolls its own street
+ count, block spacing and rotation out of the seed, so what says the towns are generated rather
+ than placed is that the same seed at two player counts does not lay the same number of buildings
+ and streets down. */
+//-------------------------------------------------------------------------------------------------
+TEST(a_town_is_rolled_rather_than_stamped)
+{
+ CHECK( bootOnce() );
+
+ Int lastBuildings = -1;
+ Int differentLayouts = 0;
+
+ for( Int seed = 1; seed <= 5; seed++ )
+ {
+ RandomMapSettings settings;
+ settings.m_seed = seed * 31337;
+ settings.m_numPlayers = 4;
+ settings.m_playableCells = RandomMapGenerator::cellsFor( RANDOM_MAP_SIZE_NORMAL, 4 );
+
+ std::vector<char> bytes;
+ RandomMapGenerator::generate( settings, bytes );
+ parseGeneratedMap( bytes );
+
+ Int buildings = 0, streetPoints = 0;
+ for( Int i = 0; i < (Int)theRMGParse.m_objectNames.size(); i++ )
+ {
+ if( (theRMGParse.m_objectFlags[i] & (FLAG_ROAD_POINT1 | FLAG_ROAD_POINT2)) != 0 )
+ streetPoints++;
+ else if( theRMGParse.m_objectNames[i].startsWith( "Stan" ) ||
+ theRMGParse.m_objectNames[i].startsWith( "Asian" ) ||
+ theRMGParse.m_objectNames[i].startsWith( "Civilian" ) )
+ buildings++;
+ }
+
+ CHECK( buildings > 0 );
+ CHECK( streetPoints > 0 );
+ CHECK( streetPoints % 2 == 0 ); // a road is a pair, or the renderer draws nothing
+
+ if( lastBuildings >= 0 && buildings != lastBuildings )
+ differentLayouts++;
+ lastBuildings = buildings;
+ }
+
+ CHECK( differentLayouts > 0 );
+}
+
//-------------------------------------------------------------------------------------------------
/** A map has to hold the players it is generated for. The three sizes are three different maps
for the same game, and every one of them grows with the number of players rather than packing
@@ -7182,9 +7358,9 @@ TEST(the_generator_still_turns_a_seed_into_the_bytes_it_used_to)
struct RMGFingerprint { Int m_seed, m_players, m_cells; UnsignedInt m_crc; };
static const RMGFingerprint theFingerprints[] =
{
- { 0, 2, 64, 0x91026C19 },
- { 12345, 4, 96, 0xC248EEA9 },
- { 7, 8, 128, 0x38E6A48E },
+ { 0, 2, 64, 0xF59BF75C },
+ { 12345, 4, 96, 0x6B8891F3 },
+ { 7, 8, 128, 0x4D5C3AD3 },
};
const Int numFingerprints = sizeof(theFingerprints) / sizeof(theFingerprints[0]);