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 v13, seamless blends, wandering outlines
Olcay Seygan52e9e4e6ab89
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 40112c709..136e9dbb2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1946,6 +1946,16 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the sameground, rock breaks through where it turns steep, and woods stand thick in the middle and thin atthe edges. The ground changes texture the way ground does, with one blending into the next insteadof meeting it at a straight line.+- Where rock or dirt meets sand the edge is soft now and bends with the land. It used to come out+ as a staircase, with a square notch in the inside corner of every patch+ and single squares of rock sprinkled down the hillsides. The map was asking the terrain for corner+ blends it draws differently: three of the four ways to blend three corners of a square came out+ as one corner or two, and that is the notch. A square between two other kinds of ground blended+ into only one of them, and rock was decided a square at a time. On a test map 8,551 corner checks+ used to fail and none fails now. The bases, the ground round the supply docks and derricks, the+ towns and the table in the middle of a massif map were drawn with compasses and read from the+ camera as circles; their edges wander now. Nobody pays for it: every base keeps at least the flat+ ground it had, and gets the same amount as everyone else.- The maps are twice the size they were. A two-player map is a quarter of a million square feet ofground, an eight-player one nearly three times that, which is room to manoeuvre round a flankinstead of running into the enemy on the way out of your own base.diff --git a/GeneralsMD/Code/GameEngine/Include/Common/RandomMapGenerator.h b/GeneralsMD/Code/GameEngine/Include/Common/RandomMapGenerator.hindex 86769267e..8d5f9919f 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 12+#define RANDOM_MAP_GENERATOR_VERSION 13/// 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 d2bb1dd08..7456adf59 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/RandomMapGenerator.cpp@@ -123,6 +123,9 @@ static const Int K_SCRIPT_LIST_DATA_VERSION_1 = 1;// A start position gets a flat disc to build on, easing back into the terrain.#define RMG_FLAT_RADIUS 13.0f#define RMG_BLEND_RADIUS 26.0f+#define RMG_BASE_FLAT_FLOOR 14.2f ///< cells of flat round a start on every bearing, the pathfinder's square inside it+#define RMG_BASE_FLAT_SWING 5.5f ///< cells the flat edge wanders out past that floor+#define RMG_BASE_BLEND_SWING 0.6f ///< share of its width the blend's outer edge wanders out by#define RMG_START_MARGIN 10.0f ///< cells of playable area kept outside the blend disc#define RMG_START_EDGE_FRACTION 0.14f ///< and this much of the map besides, so a base has ground behind it#define RMG_START_STRIDE 3 ///< cells between the spots the search looks at@@ -162,6 +165,7 @@ static const Int K_SCRIPT_LIST_DATA_VERSION_1 = 1;#define RMG_PAD_RADIUS 5.0f#define RMG_PAD_BLEND 10.0f#define RMG_PAD_SHORE_KEEP 2.0f ///< cells of beach a pad never touches+#define RMG_PAD_BLEND_SWING 0.7f ///< share of its width a pad's blend wanders out by/* A town. Nothing here is a fixed number: each town rolls how many streets it has each way, how farapart they run, how deep the plots are, and which way the whole grid is turned. Two towns on one@@ -180,6 +184,7 @@ static const Int K_SCRIPT_LIST_DATA_VERSION_1 = 1;#define RMG_TOWN_LOT_BLEND 7.0f#define RMG_TOWN_GRADE_MARGIN 4.0f ///< cells of graded ground kept past the outermost frontage#define RMG_TOWN_GRADE_FADE 14.0f ///< cells the grading eases back into the hills over+#define RMG_TOWN_GRADE_SWING 0.6f ///< share of that the fade's outer edge wanders out by#define RMG_TOWN_GRADE_BLUR 9 ///< cells either side the ground is averaged over#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@@ -217,7 +222,8 @@ static const Int K_SCRIPT_LIST_DATA_VERSION_1 = 1;#define RMG_MASSIF_HEIGHT 44.0f ///< height bytes, almost two terrace steps#define RMG_MASSIF_RADIUS 0.16f ///< fraction of the playable size, to the foot of the cliff#define RMG_MASSIF_RIM 4.0f ///< cells the cliff takes from foot to top-#define RMG_MASSIF_WOBBLE 0.10f ///< share of the radius the outline wanders by+#define RMG_MASSIF_WOBBLE 0.10f ///< share of the radius the outline wanders out by+#define RMG_MASSIF_WOBBLE_IN 0.24f ///< and in by: inward costs no start any room#define RMG_MASSIF_ROAD_TOP 3.0f ///< cells either side of a ramp's centre where it meets the top#define RMG_MASSIF_ROAD_FOOT 6.0f ///< and where it meets the ground below, so the foot opens out#define RMG_MASSIF_ROAD_BANK 4.0f ///< cells over which a ramp's sides ease back into the ground@@ -481,6 +487,18 @@ static Real fractalNoise( const UnsignedByte perm[512], Real x, Real y, Int octareturn total / range;}+/** Noise on a bearing, 0 to 1. It is read on a small circle round (originX, originY) in the noise+ field, so it is smooth in the bearing and closes on itself all the way round, and two origins+ give two unrelated outlines. A disc stamped with compasses reads as a disc from across the map;+ the base pads, the money pads and the town grading wander their edge by this instead. */+static Real ringNoise( const UnsignedByte perm[512], Real unitX, Real unitY, Real originX, Real originY )+{+ Real t = 0.5f + 1.4f * fractalNoise( perm, originX + unitX * 1.6f, originY + unitY * 1.6f, 2 );+ if( t < 0.0f ) t = 0.0f;+ if( t > 1.0f ) t = 1.0f;+ return t;+}+/// Repeatable per-cell randomness, for scattering that must not depend on order.static UnsignedInt hashCell( Int seed, Int x, Int y ){@@ -827,6 +845,7 @@ public:std::vector<UnsignedByte> m_landHeights; ///< the height field eased onto its floor, swapped in once the lakes are grownstd::vector<UnsignedByte> m_terrain;std::vector<Short> m_blendIndex; ///< into m_blends, 0 for a cell with no blend+ std::vector<Short> m_extraBlendIndex; ///< the second layer, drawn over the first; 0 for nonestd::vector<char> m_passable;std::vector<RMGLake> m_lakes;std::vector<RMGPoint> m_riverLine; ///< a river map's centre line, inside the water margin@@ -869,6 +888,8 @@ private:static Real walkSpread( const std::vector<Int>& walks, Int *nearLowOut );void nudgeStarts( const std::vector<RMGStartCandidate>& candidates, const std::vector<Int>& cost, Int farthest );void flattenBases( void );+ void shapeBases( void );+ void levelStartSquares( const std::vector<Real>& startHeights );void buildPassability( void );void connectStarts( void );Real massifClearance( Real cellX, Real cellY ) const;@@ -941,7 +962,11 @@ private:void reserveSite( Real cellX, Real cellY, Real radius );void addObject( const char *templateName, const char *uniqueID, Real cellX, Real cellY,Real angle );- Short blendEntryFor( Int blendTileIndex, Int cornerMask );+ Short blendEntryFor( Int blendTileIndex, Int shapeIndex, Bool flipped );+ Int blendLayersAt( Int x, Int y, Int *classes, Int *masks ) const;+ void settleTerrainForBlends( void );++ UnsignedByte m_perm[512]; ///< the seed's noise permutation, for the stamps that wobblestd::map<Int, Short> m_blendLookup; ///< tile and shape to the table entry that holds themstd::vector<RMGSite> m_sites; ///< everything placed so far, with its elbow room@@ -949,6 +974,10 @@ private:std::vector<char> m_baseLevelled; ///< cells a base blend has touchedstd::vector<UnsignedByte> m_baseUnder; ///< and the ground it blended therestd::vector<UnsignedByte> m_baseResult; ///< and what it left, so a second levelling can tell+ std::vector<RMGPoint> m_baseShapeStarts; ///< the starts the three below were worked out for+ std::vector<Int> m_baseCells; ///< cells a base levelling touches+ std::vector<Real> m_baseBlendT; ///< and how far into the blend each is, 0 on the flat+ std::vector<UnsignedByte> m_baseOwners; ///< and whose base it isstd::vector<RMGMoneyWalk> m_moneyWalks; ///< every walked placement of a player's own moneystd::vector<RMGPoint> m_ramps; ///< where a carved route changed layer, for the bunkersstd::vector<char> m_inLake; ///< cells the basins drowned, whatever the ground does@@ -1018,7 +1047,15 @@ void RMGLayout::buildHeights( const UnsignedByte perm[512] ){Real ux = ( r > 0.5f ) ? dx / r : 0.0f;Real uy = ( r > 0.5f ) ? dy / r : 0.0f;- foot *= 1.0f + RMG_MASSIF_WOBBLE * fractalNoise( perm, ux * 1.7f + 41.0f, uy * 1.7f - 37.0f, 2 );+ /* Fractal noise only reaches a third or so of its range, so read at face value the+ wobble moved the foot by a few percent and the massif stood as a circle. Doubled+ and clamped it uses the whole band, and the band reaches further in than out: the+ outer bound is what the start search keeps clear of, the inner one only sets where+ the ramps meet the top. */+ Real wander = 2.0f * fractalNoise( perm, ux * 2.4f + 41.0f, uy * 2.4f - 37.0f, 3 );+ if( wander > 1.0f ) wander = 1.0f;+ if( wander < -1.0f ) wander = -1.0f;+ foot *= 1.0f + ( wander > 0.0f ? RMG_MASSIF_WOBBLE : RMG_MASSIF_WOBBLE_IN ) * wander;Real t = ( foot - r ) / RMG_MASSIF_RIM;if( t > 1.0f ) t = 1.0f;if( t > 0.0f )@@ -2842,16 +2879,70 @@ void RMGLayout::flattenBases( void )m_baseResult.assign( m_heights.size(), 0 );}- std::vector<Real> startHeights;- std::vector<Real> flatRadii;- std::vector<Real> blendRadii;+ Bool stale = m_baseShapeStarts.size() != m_starts.size();+ for( UnsignedInt i = 0; i < m_starts.size() && !stale; i++ )+ {+ stale = m_baseShapeStarts[i].m_cellX != m_starts[i].m_cellX ||+ m_baseShapeStarts[i].m_cellY != m_starts[i].m_cellY;+ }+ if( stale )+ shapeBases();+ std::vector<Real> startHeights;for( UnsignedInt i = 0; i < m_starts.size(); i++ ){Int mapX = (Int)(m_starts[i].m_cellX + 0.5f) + RMG_BORDER_CELLS;Int mapY = (Int)(m_starts[i].m_cellY + 0.5f) + RMG_BORDER_CELLS;startHeights.push_back( (Real)m_heights[cellIndex( mapX, mapY )] );+ }+ for( UnsignedInt e = 0; e < m_baseCells.size(); e++ )+ {+ Int c = m_baseCells[e];+ Real t = m_baseBlendT[e];+ UnsignedInt nearestIndex = m_baseOwners[e];++ /* Blend against the ground as it stood before the last levelling, unless something has+ moved this cell since. Blending the blend again on every call pulled the ring towards+ the base a little more each time while its outer edge stayed put, and after the ring+ route and the money walks had each levelled it, that edge stood as a cliff round the+ base. */+ UnsignedByte under = m_heights[c];+ if( m_baseLevelled[c] && m_heights[c] == m_baseResult[c] )+ under = m_baseUnder[c];++ Real h = lerpReal( startHeights[nearestIndex], (Real)under, fadeCurve( t ) );+ if( h < 1.0f ) h = 1.0f;+ if( h > 254.0f ) h = 254.0f;++ m_heights[c] = (UnsignedByte)(h + 0.5f);+ m_baseLevelled[c] = 1;+ m_baseUnder[c] = under;+ m_baseResult[c] = m_heights[c];+ }++ levelStartSquares( startHeights );+}++/** Which cells round the starts a levelling touches, how far into the blend each one is and which+ start it belongs to. None of it depends on the heights, and flattenBases runs after every route+ and every money walk, so it is worked out once for a set of starts and kept. */+void RMGLayout::shapeBases( void )+{+ m_baseShapeStarts = m_starts;+ m_baseCells.clear();+ m_baseBlendT.clear();+ m_baseOwners.clear();++ std::vector<Real> flatRadii;+ std::vector<Real> flatWidest;+ std::vector<Real> blendWidths;+ std::vector<Real> blendCaps;+ std::vector<Real> turnCos;+ std::vector<Real> turnSin;++ for( UnsignedInt i = 0; i < m_starts.size(); i++ )+ {/* Two discs at different heights meeting in the middle is a cliff between two bases, soon a map too cramped to hold everybody at the full radius each disc gives way ratherthan overlapping its neighbour. */@@ -2868,9 +2959,12 @@ void RMGLayout::flattenBases( void )nearestOther = distance;}- Real flat = RMG_FLAT_RADIUS;+ Real flat = RMG_BASE_FLAT_FLOOR;if( flat > nearestOther * 0.45f )flat = nearestOther * 0.45f;+ Real widest = flat + RMG_BASE_FLAT_SWING;+ if( widest > nearestOther * 0.45f )+ widest = nearestOther * 0.45f;Real blend = RMG_BLEND_RADIUS;if( blend > nearestOther * 0.9f )@@ -2878,18 +2972,29 @@ void RMGLayout::flattenBases( void )if( blend < flat + 4.0f )blend = flat + 4.0f;+ /* The edge of the flat and the edge of the blend both wander with the bearing, outward only,+ so no base has less flat ground than the floor. The flat edge is one profile turned to a+ different angle for each seat: every base gains the same ground, and no two have the same+ outline to look at. */+ Real turn = hashUnit( m_settings.m_seed, (Int)i, 4242 ) * 2.0f * PI;+flatRadii.push_back( flat );- blendRadii.push_back( blend );+ flatWidest.push_back( widest );+ blendWidths.push_back( blend - flat );+ blendCaps.push_back( nearestOther * 0.9f );+ turnCos.push_back( Cos( turn ) );+ turnSin.push_back( Sin( turn ) );}/* Only the squares round each start can be inside a blend, so only those are walked; a celltwo squares share is levelled once. Every pass of the ring routes calls this, and walkingthe whole map each time was most of what connecting the starts cost. */Real reach = 0.0f;- for( UnsignedInt i = 0; i < blendRadii.size(); i++ )+ for( UnsignedInt i = 0; i < flatRadii.size(); i++ ){- if( blendRadii[i] > reach )- reach = blendRadii[i];+ Real widest = flatWidest[i] + blendWidths[i] * ( 1.0f + RMG_BASE_BLEND_SWING );+ if( widest > reach )+ reach = widest;}Int box = (Int)reach + 2;std::vector<char> walked( m_heights.size(), 0 );@@ -2924,38 +3029,44 @@ void RMGLayout::flattenBases( void )}}- if( nearest >= blendRadii[nearestIndex] )+ UnsignedInt n = nearestIndex;+ Real flatR = flatRadii[n];+ Real blendR = flatR + blendWidths[n];+ if( nearest > 0.5f )+ {+ Real ux = ( px - m_starts[n].m_cellX ) / nearest;+ Real uy = ( py - m_starts[n].m_cellY ) / nearest;+ Real turnedX = ux * turnCos[n] - uy * turnSin[n];+ Real turnedY = ux * turnSin[n] + uy * turnCos[n];+ flatR += ( flatWidest[n] - flatRadii[n] ) * ringNoise( m_perm, turnedX, turnedY, 11.3f, 47.9f );+ blendR = flatR + blendWidths[n] * ( 1.0f + RMG_BASE_BLEND_SWING * ringNoise( m_perm, turnedX, turnedY, -23.1f, 5.7f ) );+ if( blendR > blendCaps[n] )+ blendR = blendCaps[n];+ if( blendR < flatR + 4.0f )+ blendR = flatR + 4.0f;+ }++ if( nearest >= blendR )continue;Real t = 0.0f;- if( nearest > flatRadii[nearestIndex] )- t = (nearest - flatRadii[nearestIndex])- / (blendRadii[nearestIndex] - flatRadii[nearestIndex]);-- /* Blend against the ground as it stood before the last levelling, unless something has- moved this cell since. Blending the blend again on every call pulled the ring towards- the base a little more each time while its outer edge stayed put, and after the ring- route and the money walks had each levelled it, that edge stood as a cliff round the- base. */- Int c = cellIndex( x, y );- UnsignedByte under = m_heights[c];- if( m_baseLevelled[c] && m_heights[c] == m_baseResult[c] )- under = m_baseUnder[c];-- Real h = lerpReal( startHeights[nearestIndex], (Real)under, fadeCurve( t ) );- if( h < 1.0f ) h = 1.0f;- if( h > 254.0f ) h = 254.0f;+ if( nearest > flatR )+ t = (nearest - flatR) / (blendR - flatR);- m_heights[c] = (UnsignedByte)(h + 0.5f);- m_baseLevelled[c] = 1;- m_baseUnder[c] = under;- m_baseResult[c] = m_heights[c];+ m_baseCells.push_back( cellIndex( x, y ) );+ m_baseBlendT.push_back( t );+ m_baseOwners.push_back( (UnsignedByte)nearestIndex );}}+}+void RMGLayout::levelStartSquares( const std::vector<Real>& startHeights )+{/* The pathfinder tests a 9-cell square, and the span of a corner cell of that squarelooks one cell further out, into the blend. A river bank sitting there is a cliff- on the pad. Force that square flat after the disc so the base is walkable. */+ on the pad. Force that square flat after the disc so the base is walkable. It is forced as+ the disc round the square's corners: a square of one height on rolling ground read in the+ game as a square of one texture with the slope's texture all round it. */const Int square = 10;for( UnsignedInt i = 0; i < m_starts.size(); i++ ){@@ -2965,10 +3076,12 @@ void RMGLayout::flattenBases( void )if( h < 1 ) h = 1;if( h > 254 ) h = 254;- for( Int dy = -square; dy <= square; dy++ )+ for( Int dy = -square - 5; dy <= square + 5; dy++ ){- for( Int dx = -square; dx <= square; dx++ )+ for( Int dx = -square - 5; dx <= square + 5; dx++ ){+ if( dx * dx + dy * dy > 2 * square * square )+ continue;Int x = sx + dx;Int y = sy + dy;if( x < 0 || y < 0 || x >= m_width || y >= m_height )@@ -3350,7 +3463,7 @@ void RMGLayout::carveMassifRamps( void )Real playable = (Real)m_settings.m_playableCells;Real centre = playable * 0.5f;Real outer = playable * RMG_MASSIF_RADIUS * ( 1.0f + RMG_MASSIF_WOBBLE );- Real inner = playable * RMG_MASSIF_RADIUS * ( 1.0f - RMG_MASSIF_WOBBLE ) - RMG_MASSIF_RIM;+ Real inner = playable * RMG_MASSIF_RADIUS * ( 1.0f - RMG_MASSIF_WOBBLE_IN ) - RMG_MASSIF_RIM;for( UnsignedInt i = 0; i < m_starts.size(); i++ ){@@ -4090,19 +4203,44 @@ void RMGLayout::holdMoneyWalks( void )/** Height and slope decide the ground cover, with the thresholds pushed aboutby a noise field of their own so the edges wander instead of following a- contour line. */+ contour line.++ The slope is read averaged over the cell and its eight neighbours, and the classes then go+ through two majority votes. Read cell by cell, the slope of rolling ground flickers either side+ of the rock threshold, and version 12 drew that as single rock cells, spurs one cell wide and+ notches in the edge of every patch: on screen a staircase of squares. The average takes the top+ off a steep cell, so rock starts at 0.39 of the cliff span instead of 0.45, and a real cliff+ keeps its rock whatever the vote says. */void RMGLayout::buildTerrainClasses( const UnsignedByte perm[512] ){m_terrain.resize( m_width * m_height );Real scale = RMG_FEATURES_PER_MAP * 2.5f / (Real)m_settings.m_playableCells;+ std::vector<Real> spans( m_width * m_height );+ for( Int y = 0; y < m_height; y++ )+ for( Int x = 0; x < m_width; x++ )+ spans[cellIndex( x, y )] = cellSpanWorld( x, y );+for( Int y = 0; y < m_height; y++ ){for( Int x = 0; x < m_width; x++ ){Real height = (Real)m_heights[cellIndex( x, y )];- Real span = cellSpanWorld( x, y );++ Real span = 0.0f;+ Int around = 0;+ for( Int dy = -1; dy <= 1; dy++ )+ {+ for( Int dx = -1; dx <= 1; dx++ )+ {+ if( x + dx < 0 || y + dy < 0 || x + dx >= m_width || y + dy >= m_height )+ continue;+ span += spans[cellIndex( x + dx, y + dy )];+ around++;+ }+ }+ span /= (Real)around;Real wobble = fractalNoise( perm, (Real)x * scale + 31.0f, (Real)y * scale - 17.0f, 3 );@@ -4114,10 +4252,13 @@ void RMGLayout::buildTerrainClasses( const UnsignedByte perm[512] )Real distanceToShore;insideLake( (Real)(x - RMG_BORDER_CELLS), (Real)(y - RMG_BORDER_CELLS), &distanceToShore );+ // The beach's width wanders as well, or it reads as the lake's outline drawn again a+ // fixed distance out.if( height < m_waterHeight + 8.0f + wobble * 3.0f &&- distanceToShore < RMG_LAKE_SHORE + 8.0f )+ distanceToShore < RMG_LAKE_SHORE + 8.0f + wobble * 8.0f )terrainClass = RMG_TERRAIN_SAND;- else if( span > RMG_CLIFF_WORLD_SPAN * (0.45f + wobble * 0.12f) )+ else if( span > RMG_CLIFF_WORLD_SPAN * (0.39f + wobble * 0.12f) ||+ spans[cellIndex( x, y )] > RMG_CLIFF_WORLD_SPAN )terrainClass = RMG_TERRAIN_ROCK;// Dry high ground. The offset is in height bytes rather than a fraction of the// amplitude: five octaves only reach a third of their nominal range, so a fraction of@@ -4130,6 +4271,43 @@ void RMGLayout::buildTerrainClasses( const UnsignedByte perm[512] )m_terrain[cellIndex( x, y )] = terrainClass;}}++ // Two rounds of a vote over each cell and its eight neighbours: the class with the most votes+ // wins, a tie keeps what the cell had. Both rounds read the classes as the last one left them.+ std::vector<UnsignedByte> voted( m_terrain.size() );+ for( Int round = 0; round < 2; round++ )+ {+ for( Int y = 0; y < m_height; y++ )+ {+ for( Int x = 0; x < m_width; x++ )+ {+ Int mine = m_terrain[cellIndex( x, y )];+ voted[cellIndex( x, y )] = (UnsignedByte)mine;+ if( spans[cellIndex( x, y )] > RMG_CLIFF_WORLD_SPAN )+ continue;++ Int votes[RMG_TERRAIN_COUNT] = { 0, 0, 0, 0 };+ for( Int dy = -1; dy <= 1; dy++ )+ {+ for( Int dx = -1; dx <= 1; dx++ )+ {+ if( x + dx < 0 || y + dy < 0 || x + dx >= m_width || y + dy >= m_height )+ continue;+ votes[m_terrain[cellIndex( x + dx, y + dy )]]++;+ }+ }++ Int best = mine;+ for( Int k = 0; k < RMG_TERRAIN_COUNT; k++ )+ {+ if( votes[k] > votes[best] )+ best = k;+ }+ voted[cellIndex( x, y )] = (UnsignedByte)best;+ }+ }+ m_terrain.swap( voted );+ }}/** The tile index WorldBuilder computes for a cell: four quadrants packed into@@ -4141,12 +4319,23 @@ static Short tileIndexForCell( Int x, Int y, Int firstTile, Int width )return (Short)ndx;}-/** The corner alpha the renderer can actually express, and the flags that ask- for it. `inverted` is one field shared by every flag in an entry, so the- combinations are the ones on one side of it or the other - which is why a- mask is matched to the nearest of these rather than built from scratch.- Corners: bit 0 is (x,y), 1 is (x+1,y), 2 is (x+1,y+1), 3 is (x,y+1), in the- order getAlphaUVData fills alpha[]. */+/** The corner alpha the renderer can actually express, and the flags that ask for it, read off+ WorldHeightMap::getAlphaUVData. A horizontal or vertical blend paints one side, a short diagonal+ one corner and a long diagonal three. Every flag in an entry shares the one `inverted` byte, and+ the masks with two opposite corners or all four have no entry at all.+ Corners: bit 0 is (x,y), 1 is (x+1,y), 2 is (x+1,y+1), 3 is (x,y+1), in the order getAlphaUVData+ fills alpha[].++ Version 12 asked for three corners with a side flag and a diagonal flag together. The renderer+ adds those up, and with the shared `inverted` only the pair that point the same way came to three+ corners: the other three entries drew two corners or one, so every concave corner of a patch of+ rock was missing a corner, a notch of the ground underneath it. The long diagonals are what+ WorldBuilder uses and reach all four.++ A diagonal also decides which way the cell's two triangles are cut (m_flip, 1 for the cut from+ corner 1 to corner 3). The renderer cuts the cell the way its first layer says and draws the+ second layer cut its own way, so two layers have to agree; a side blend looks the same cut either+ way (-1) and takes the other layer's cut through FLIPPED_MASK, which is what WorldBuilder does. */struct RMGBlendShape{Int m_cornerMask;@@ -4156,68 +4345,53 @@ struct RMGBlendShapeUnsignedByte m_leftDiagonal;UnsignedByte m_inverted;UnsignedByte m_longDiagonal;+ Int m_flip;};static const RMGBlendShape theBlendShapes[] ={// one corner- { 0x1, 0, 0, 0, 1, 1, 0 }, // (x,y)- { 0x2, 0, 0, 1, 0, 1, 0 }, // (x+1,y)- { 0x4, 0, 0, 1, 0, 0, 0 }, // (x+1,y+1)- { 0x8, 0, 0, 0, 1, 0, 0 }, // (x,y+1)- // one edge- { 0x6, 1, 0, 0, 0, 0, 0 }, // the +x side- { 0x9, 1, 0, 0, 0, 1, 0 }, // the -x side- { 0xC, 0, 1, 0, 0, 0, 0 }, // the +y side- { 0x3, 0, 1, 0, 0, 1, 0 }, // the -y side+ { 0x1, 0, 0, 0, 1, 1, 0, 1 }, // (x,y)+ { 0x2, 0, 0, 1, 0, 1, 0, 0 }, // (x+1,y)+ { 0x4, 0, 0, 1, 0, 0, 0, 1 }, // (x+1,y+1)+ { 0x8, 0, 0, 0, 1, 0, 0, 0 }, // (x,y+1)+ // one side+ { 0x6, 1, 0, 0, 0, 0, 0, -1 }, // the +x side+ { 0x9, 1, 0, 0, 0, 1, 0, -1 }, // the -x side+ { 0xC, 0, 1, 0, 0, 0, 0, -1 }, // the +y side+ { 0x3, 0, 1, 0, 0, 1, 0, -1 }, // the -y side// three corners- { 0xE, 1, 1, 0, 0, 0, 0 }, // everything but (x,y)- { 0xB, 0, 1, 0, 1, 1, 0 }, // everything but (x+1,y+1)- { 0xD, 1, 0, 1, 0, 0, 0 }, // everything but (x+1,y)- { 0x7, 0, 1, 1, 0, 1, 0 }, // everything but (x,y+1)+ { 0x7, 0, 0, 1, 0, 1, 1, 0 }, // everything but (x,y+1)+ { 0xB, 0, 0, 0, 1, 1, 1, 1 }, // everything but (x+1,y+1)+ { 0xD, 0, 0, 0, 1, 0, 1, 0 }, // everything but (x+1,y)+ { 0xE, 0, 0, 1, 0, 0, 1, 1 }, // everything but (x,y)};static const Int theNumBlendShapes = sizeof(theBlendShapes) / sizeof(theBlendShapes[0]);+static const UnsignedByte RMG_BLEND_FLIPPED = 0x2; ///< FLIPPED_MASK in TileData.h-/// Find or add the blend table entry that paints this tile over these corners.-Short RMGLayout::blendEntryFor( Int blendTileIndex, Int cornerMask )+/// The shape that paints exactly these corners, or -1 when the renderer has none.+static Int blendShapeFor( Int cornerMask ){- // Nearest shape by how many corners it gets wrong, preferring one that covers- // more rather than less: a corner blended a little early is invisible, a- // corner left unblended is the hard edge this whole pass exists to remove.- Int bestShape = -1;- Int bestPenalty = 100;-for( Int i = 0; i < theNumBlendShapes; i++ ){- Int missing = cornerMask & ~theBlendShapes[i].m_cornerMask;- Int extra = theBlendShapes[i].m_cornerMask & ~cornerMask;-- Int penalty = 0;- for( Int bit = 0; bit < 4; bit++ )- {- if( missing & (1 << bit) ) penalty += 3;- if( extra & (1 << bit) ) penalty += 1;- }-- if( penalty < bestPenalty )- {- bestPenalty = penalty;- bestShape = i;- }+ if( theBlendShapes[i].m_cornerMask == cornerMask )+ return i;}+ return -1;+}- if( bestShape < 0 )- return 0;-- /* A tile and a shape name an entry, so that pair is the key. A scan of the table instead would- be a scan per cell of the map, and on a 456-cell map with a few thousand entries in the+/// Find or add the blend table entry that paints this tile in this shape.+Short RMGLayout::blendEntryFor( Int blendTileIndex, Int shapeIndex, Bool flipped )+{+ /* A tile, a shape and the cut name an entry, so that is the key. A scan of the table instead+ would be a scan per cell of the map, and on a 456-cell map with a few thousand entries in thetable that is most of the generator's time. */- Int key = blendTileIndex * theNumBlendShapes + bestShape;+ Int key = ( blendTileIndex * theNumBlendShapes + shapeIndex ) * 2 + ( flipped ? 1 : 0 );std::map<Int, Short>::const_iterator found = m_blendLookup.find( key );if( found != m_blendLookup.end() )return found->second;- const RMGBlendShape& shape = theBlendShapes[bestShape];+ const RMGBlendShape& shape = theBlendShapes[shapeIndex];RMGBlend blend;blend.m_blendTileIndex = blendTileIndex;@@ -4225,7 +4399,7 @@ Short RMGLayout::blendEntryFor( Int blendTileIndex, Int cornerMask )blend.m_vertical = shape.m_vertical;blend.m_rightDiagonal = shape.m_rightDiagonal;blend.m_leftDiagonal = shape.m_leftDiagonal;- blend.m_inverted = shape.m_inverted;+ blend.m_inverted = shape.m_inverted | ( flipped ? RMG_BLEND_FLIPPED : 0 );blend.m_longDiagonal = shape.m_longDiagonal;m_blends.push_back( blend );@@ -4234,70 +4408,186 @@ Short RMGLayout::blendEntryFor( Int blendTileIndex, Int cornerMask )return entry;}-/** Every cell whose neighbour carries a higher-priority ground gets that ground- painted over the corners it touches, as an alpha ramp across the cell. One- side blends and the other does not, which is what stops two neighbours- fighting over the same edge and leaving a seam between them. */-void RMGLayout::buildBlends( void )+/** The layers a cell is drawn with, lowest ground first; returns how many.++ A layer of some ground goes over every corner that touches that ground or anything above it. Put+ that way the alpha of a ground at a corner belongs to the corner and not to whichever of the four+ cells round it is asking, so the four draw it alike, and along an edge the two cells either side+ interpolate the same two values: the picture has no seam anywhere. Version 12 painted only the+ strongest neighbour and only its own corners, so a grass cell between sand and rock cut the sand+ off at a hard line, and a cell the layer above already covers corner for corner added nothing and+ is left out here. The renderer has two layers, the blend and the extra blend over it. */+Int RMGLayout::blendLayersAt( Int x, Int y, Int *classes, Int *masks ) const{- m_blends.clear();- m_blendLookup.clear();+ Int mine = m_terrain[cellIndex( x, y )];+ Int touch[RMG_TERRAIN_COUNT] = { 0, 0, 0, 0 };- RMGBlend nothing;- memset( ¬hing, 0, sizeof(nothing) );- m_blends.push_back( nothing ); // entry 0 is "no blend"+ for( Int dy = -1; dy <= 1; dy++ )+ {+ for( Int dx = -1; dx <= 1; dx++ )+ {+ Int nx = x + dx;+ Int ny = y + dy;+ if( (dx == 0 && dy == 0) || nx < 0 || ny < 0 || nx >= m_width || ny >= m_height )+ continue;- m_blendIndex.assign( m_width * m_height, 0 );+ Int theirs = m_terrain[cellIndex( nx, ny )];+ if( theirs <= mine )+ continue;- for( Int y = 0; y < m_height; y++ )+ // A neighbour claims the corners it shares with this cell.+ if( dx >= 0 && dy >= 0 ) touch[theirs] |= 0x4; // (x+1,y+1)+ if( dx >= 0 && dy <= 0 ) touch[theirs] |= 0x2; // (x+1,y)+ if( dx <= 0 && dy >= 0 ) touch[theirs] |= 0x8; // (x,y+1)+ if( dx <= 0 && dy <= 0 ) touch[theirs] |= 0x1; // (x,y)+ }+ }++ Int topDown[RMG_TERRAIN_COUNT];+ Int topDownMasks[RMG_TERRAIN_COUNT];+ Int count = 0;+ Int above = 0;+ for( Int k = RMG_TERRAIN_COUNT - 1; k > mine; k-- ){- for( Int x = 0; x < m_width; x++ )+ Int mask = above | touch[k];+ if( mask != above ){- Int mine = m_terrain[cellIndex( x, y )];+ topDown[count] = k;+ topDownMasks[count] = mask;+ count++;+ }+ above = mask;+ }- // The strongest ground among the eight neighbours, and which of this- // cell's corners touch it.- Int strongest = mine;- Int cornerMask = 0;+ for( Int i = 0; i < count; i++ )+ {+ classes[i] = topDown[count - 1 - i];+ masks[i] = topDownMasks[count - 1 - i];+ }+ return count;+}- for( Int dy = -1; dy <= 1; dy++ )+/** Raise every cell the renderer cannot draw without a seam to the ground of its lowest layer, until+ none is left. A cell gets raised when it needs a third layer, when a layer covers two opposite+ corners or all four, or when both layers are diagonals cut opposite ways. A cell with ground on+ all four corners is that ground anyway, and a gap one cell wide between two patches of the same+ ground fills in; nothing that is drawn moves by more than the cell. A cell only ever goes up, so+ the passes end. After the first pass only the cells round one that was raised can change, so+ only those are looked at again, in map order. */+void RMGLayout::settleTerrainForBlends( void )+{+ std::vector<Int> look( m_width * m_height );+ for( Int i = 0; i < (Int)look.size(); i++ )+ look[i] = i;+ std::vector<Int> raised;+ std::vector<char> queued( look.size(), 0 );++ while( !look.empty() )+ {+ raised.clear();+ for( UnsignedInt i = 0; i < look.size(); i++ )+ {+ Int x = look[i] % m_width;+ Int y = look[i] / m_width;++ Int classes[RMG_TERRAIN_COUNT];+ Int masks[RMG_TERRAIN_COUNT];+ Int count = blendLayersAt( x, y, classes, masks );+ if( count == 0 )+ continue;++ Bool drawable = count <= 2;+ Int flip = -1;+ for( Int layer = 0; layer < count && drawable; layer++ ){- for( Int dx = -1; dx <= 1; dx++ )+ Int shape = blendShapeFor( masks[layer] );+ if( shape < 0 )+ drawable = FALSE;+ else if( theBlendShapes[shape].m_flip >= 0 ){- if( dx == 0 && dy == 0 )- continue;+ if( flip >= 0 && flip != theBlendShapes[shape].m_flip )+ drawable = FALSE;+ flip = theBlendShapes[shape].m_flip;+ }+ }- Int nx = x + dx;- Int ny = y + dy;- if( nx < 0 || ny < 0 || nx >= m_width || ny >= m_height )- continue;+ if( !drawable )+ {+ m_terrain[look[i]] = (UnsignedByte)classes[0];+ raised.push_back( look[i] );+ }+ }- Int theirs = m_terrain[cellIndex( nx, ny )];- if( theirs <= mine )+ look.clear();+ for( UnsignedInt i = 0; i < raised.size(); i++ )+ {+ Int x = raised[i] % m_width;+ Int y = raised[i] / m_width;+ for( Int dy = -1; dy <= 1; dy++ )+ {+ for( Int dx = -1; dx <= 1; dx++ )+ {+ if( x + dx < 0 || y + dy < 0 || x + dx >= m_width || y + dy >= m_height )continue;-- if( theirs > strongest )+ Int c = cellIndex( x + dx, y + dy );+ if( !queued[c] ){- strongest = theirs;- cornerMask = 0;+ queued[c] = 1;+ look.push_back( c );}- if( theirs < strongest )- continue;-- // A neighbour claims the corners it shares with this cell.- if( dx >= 0 && dy >= 0 ) cornerMask |= 0x4; // (x+1,y+1)- if( dx >= 0 && dy <= 0 ) cornerMask |= 0x2; // (x+1,y)- if( dx <= 0 && dy >= 0 ) cornerMask |= 0x8; // (x,y+1)- if( dx <= 0 && dy <= 0 ) cornerMask |= 0x1; // (x,y)}}+ }+ std::sort( look.begin(), look.end() );+ for( UnsignedInt i = 0; i < look.size(); i++ )+ queued[look[i]] = 0;+ }+}++void RMGLayout::buildBlends( void )+{+ m_blends.clear();+ m_blendLookup.clear();- if( strongest == mine || cornerMask == 0 )+ RMGBlend nothing;+ memset( ¬hing, 0, sizeof(nothing) );+ m_blends.push_back( nothing ); // entry 0 is "no blend"++ m_blendIndex.assign( m_width * m_height, 0 );+ m_extraBlendIndex.assign( m_width * m_height, 0 );++ settleTerrainForBlends();++ for( Int y = 0; y < m_height; y++ )+ {+ for( Int x = 0; x < m_width; x++ )+ {+ Int classes[RMG_TERRAIN_COUNT];+ Int masks[RMG_TERRAIN_COUNT];+ Int count = blendLayersAt( x, y, classes, masks );+ if( count == 0 )continue;- Int blendTile = tileIndexForCell( x, y, strongest * RMG_TILES_PER_CLASS,- RMG_TILE_SHEET_WIDTH );- m_blendIndex[cellIndex( x, y )] = blendEntryFor( blendTile, cornerMask );+ Int shapes[2];+ Int flip = 0;+ for( Int i = 0; i < count; i++ )+ {+ shapes[i] = blendShapeFor( masks[i] );+ if( theBlendShapes[shapes[i]].m_flip > 0 )+ flip = 1;+ }++ // A side blend takes the cut of the diagonal it shares the cell with.+ for( Int i = 0; i < count; i++ )+ {+ Bool flipped = theBlendShapes[shapes[i]].m_flip < 0 && count == 2 && flip > 0;+ Int tile = tileIndexForCell( x, y, classes[i] * RMG_TILES_PER_CLASS, RMG_TILE_SHEET_WIDTH );+ Short entry = blendEntryFor( tile, shapes[i], flipped );+ if( i == 0 )+ m_blendIndex[cellIndex( x, y )] = entry;+ else+ m_extraBlendIndex[cellIndex( x, y )] = entry;+ }}}}@@ -4353,7 +4643,7 @@ void RMGLayout::flattenPad( Real cellX, Real cellY, Real radius, Real blend )return;Real level = (Real)m_heights[cellIndex( centreX, centreY )];- Int reach = (Int)blend + 1;+ Int reach = (Int)( radius + ( blend - radius ) * ( 1.0f + RMG_PAD_BLEND_SWING ) ) + 1;for( Int dy = -reach; dy <= reach; dy++ ){@@ -4371,13 +4661,19 @@ void RMGLayout::flattenPad( Real cellX, Real cellY, Real radius, Real blend )if( !m_routeLock.empty() && m_routeLock[cellIndex( x, y )] )continue;+ /* The flat stays a disc of the radius asked for, which is what the dock is placed on and+ what every player gets alike; the blend round it wanders out with the bearing. */Real distance = sqrtf( (Real)(dx * dx + dy * dy) );- if( distance >= blend )+ Real reachHere = blend;+ if( distance > radius )+ reachHere = radius + ( blend - radius ) * ( 1.0f + RMG_PAD_BLEND_SWING *+ ringNoise( m_perm, (Real)dx / distance, (Real)dy / distance, cellX * 0.37f, cellY * 0.37f ) );+ if( distance >= reachHere )continue;Real t = 0.0f;if( distance > radius )- t = (distance - radius) / (blend - radius);+ t = (distance - radius) / (reachHere - radius);/* The beach keeps its own profile. A pad that runs to the water's edge levels the shelfthe soft water edge is drawn on and leaves the bank standing over the lake like a@@ -4433,7 +4729,8 @@ void RMGLayout::gradeTown( Real centreX, Real centreY, const RMGTownPlan& plan,Real farAcross = (fabsf( fromAcross ) > fabsf( toAcross )) ? fabsf( fromAcross ) : fabsf( toAcross );Real farDown = (fabsf( fromDown ) > fabsf( toDown )) ? fabsf( fromDown ) : fabsf( toDown );- Int reach = (Int)(sqrtf( farAcross * farAcross + farDown * farDown ) + RMG_TOWN_GRADE_FADE) + 1;+ Int reach = (Int)(sqrtf( farAcross * farAcross + farDown * farDown )+ + RMG_TOWN_GRADE_FADE * ( 1.0f + RMG_TOWN_GRADE_SWING )) + 1;Int centreCellX = (Int)(centreX + 0.5f) + RMG_BORDER_CELLS;Int centreCellY = (Int)(centreY + 0.5f) + RMG_BORDER_CELLS;@@ -4527,10 +4824,15 @@ void RMGLayout::gradeTown( Real centreX, Real centreY, const RMGTownPlan& plan,else if( down > toDown ) outDown = down - toDown;Real outside = sqrtf( outAcross * outAcross + outDown * outDown );- if( outside >= RMG_TOWN_GRADE_FADE )+ Real fade = RMG_TOWN_GRADE_FADE;+ Real fromCentre = sqrtf( dx * dx + dy * dy );+ if( outside > 0.0f && fromCentre > 0.5f )+ fade *= 1.0f + RMG_TOWN_GRADE_SWING * ringNoise( m_perm, dx / fromCentre, dy / fromCentre,+ centreX * 0.37f, centreY * 0.37f );+ if( outside >= fade )continue;- Real weight = 1.0f - fadeCurve( outside / RMG_TOWN_GRADE_FADE );+ Real weight = 1.0f - fadeCurve( outside / fade );Real distanceToShore;insideLake( playX, playY, &distanceToShore );@@ -5866,7 +6168,7 @@ void RMGLayout::placeScenery( const UnsignedByte perm[512] ){Real clearance = massifClearance( (Real)x, (Real)y );if( clearance < 3.0f &&- clearance > -2.0f * playable * RMG_MASSIF_RADIUS * RMG_MASSIF_WOBBLE - RMG_MASSIF_RIM - 3.0f )+ clearance > -playable * RMG_MASSIF_RADIUS * ( RMG_MASSIF_WOBBLE + RMG_MASSIF_WOBBLE_IN ) - RMG_MASSIF_RIM - 3.0f )continue;}@@ -6262,8 +6564,8 @@ void RMGLayout::build( const RandomMapSettings& settings )m_waterHeight = RMG_BASE_HEIGHT - RMG_WATER_DROP;m_startSearchStride = RMG_START_STRIDE;- UnsignedByte perm[512];- seedPermutation( m_settings.m_seed, perm );+ seedPermutation( m_settings.m_seed, m_perm );+ const UnsignedByte *perm = m_perm;buildHeights( perm );placeLakes( perm );@@ -6699,7 +7001,7 @@ void RandomMapGenerator::generate( const RandomMapSettings& settings, std::vectow.writeInt( dataSize );w.writeBytes( &tiles[0], dataSize * sizeof(Short) );w.writeBytes( &layout.m_blendIndex[0], dataSize * sizeof(Short) );- w.writeBytes( &zeroes[0], dataSize * sizeof(Short) ); // extra blend tiles+ w.writeBytes( &layout.m_extraBlendIndex[0], dataSize * sizeof(Short) );w.writeBytes( &zeroes[0], dataSize * sizeof(Short) ); // cliff infow.writeInt( RMG_TERRAIN_COUNT * RMG_TILES_PER_CLASS ); // bitmap tilesdiff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 07af47e2b..00a0814a9 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -7997,6 +7997,7 @@ struct RMGParsestd::vector<UnsignedByte> m_heights;std::vector<Short> m_tiles;std::vector<Short> m_blendIndexes; ///< per cell, into m_blends; 0 is no blend+ std::vector<Short> m_extraBlendIndexes; ///< the layer drawn over that one, the same waystd::vector<RMGBlendEntry> m_blends; ///< the table itself, entry 0 exceptedstd::vector<Coord3D> m_waterPoints; ///< first point of each water areastd::vector< std::vector<Coord3D> > m_waterPolygons; ///< and every point of it@@ -8047,8 +8048,10 @@ static Bool RMGParseBlendTile( DataChunkInput &file, DataChunkInfo *info, void *theRMGParse.m_blendIndexes.resize( len );file.readArrayOfBytes( (char *)&theRMGParse.m_blendIndexes[0], len * sizeof(Short) );+ theRMGParse.m_extraBlendIndexes.resize( len );+ file.readArrayOfBytes( (char *)&theRMGParse.m_extraBlendIndexes[0], len * sizeof(Short) );+std::vector<Short> scratch( len );- file.readArrayOfBytes( (char *)&scratch[0], len * sizeof(Short) ); // extra blend tilesfile.readArrayOfBytes( (char *)&scratch[0], len * sizeof(Short) ); // cliff infotheRMGParse.m_numBitmapTiles = file.readInt();@@ -8690,92 +8693,203 @@ TEST(each_start_position_gets_flat_ground_to_build_on)}}+/* The corner alpha a blend entry draws, read the way WorldHeightMap::getAlphaUVData and+ getExtraAlphaUVData read it (GameEngineDevice, which this binary does not link): corners in the+ order (x,y), (x+1,y), (x+1,y+1), (x,y+1), and whether the cell's two triangles are cut from+ corner 1 to corner 3 instead of 0 to 2. */+static void RMGBlendCorners( const RMGBlendEntry& entry, Bool alpha[4], Bool *flip )+{+ const UnsignedByte inverted = entry.m_inverted & 0x1; // INVERTED_MASK+ const UnsignedByte flipped = entry.m_inverted & 0x2; // FLIPPED_MASK+ alpha[0] = alpha[1] = alpha[2] = alpha[3] = FALSE;+ *flip = FALSE;+ if( entry.m_horizontal )+ {+ *flip = flipped != 0;+ if( inverted ) alpha[0] = alpha[3] = TRUE;+ else alpha[1] = alpha[2] = TRUE;+ }+ if( entry.m_vertical )+ {+ *flip = flipped != 0;+ if( inverted ) alpha[0] = alpha[1] = TRUE;+ else alpha[2] = alpha[3] = TRUE;+ }+ if( entry.m_rightDiagonal )+ {+ if( inverted )+ {+ alpha[1] = TRUE;+ if( entry.m_longDiagonal ) alpha[0] = alpha[2] = TRUE;+ }+ else+ {+ *flip = TRUE;+ alpha[2] = TRUE;+ if( entry.m_longDiagonal ) alpha[1] = alpha[3] = TRUE;+ }+ }+ if( entry.m_leftDiagonal )+ {+ if( inverted )+ {+ *flip = TRUE;+ alpha[0] = TRUE;+ if( entry.m_longDiagonal ) alpha[1] = alpha[3] = TRUE;+ }+ else+ {+ alpha[3] = TRUE;+ if( entry.m_longDiagonal ) alpha[0] = alpha[2] = TRUE;+ }+ }+}+//--------------------------------------------------------------------------------------------------/** A texture class change from one cell to the next is a hard edge on screen unless the cell on- the low side of it carries a blend of the other texture over the corners they share. Walk the- map, find every boundary, and check the cell that is supposed to carry the blend does. */+/** A texture class change from one cell to the next is a hard edge on screen unless the blends on+ either side of it meet. The game draws a cell as its own ground, the blend layer over it and the+ extra layer over that, each with an alpha of 0 or 1 at a corner and interpolated across the two+ triangles. So the picture is seamless when, for every ground a cell paints, the alpha at a corner+ is the same whichever of the four cells round the corner is drawing it - 1 exactly when one of+ those four cells is that ground or above - and when the two layers of a cell cut it the same+ way. Version 12 asked for three-corner shapes the renderer drew with one or two corners, could+ not draw two opposite corners at all and blended only the strongest neighbour: on the first seed+ here 8551 corner checks failed against 8155 cells on a boundary. *///-------------------------------------------------------------------------------------------------TEST(every_texture_boundary_is_blended_rather_than_cut){CHECK( bootOnce() );- RandomMapSettings settings;- settings.m_seed = 31337;- settings.m_playableCells = 128;- settings.m_numPlayers = 4;+ static const Int seeds[] = { 31337, 271, 5150 };+ for( Int s = 0; s < 3; s++ )+ {+ RandomMapSettings settings;+ settings.m_seed = seeds[s];+ settings.m_playableCells = 128;+ settings.m_numPlayers = 4;- std::vector<char> bytes;- RandomMapGenerator::generate( settings, bytes );- parseGeneratedMap( bytes );+ std::vector<char> bytes;+ RandomMapGenerator::generate( settings, bytes );+ parseGeneratedMap( bytes );- Int width = theRMGParse.m_width;- Int height = theRMGParse.m_height;+ Int width = theRMGParse.m_width;+ Int height = theRMGParse.m_height;- CHECK_EQ( (Int)theRMGParse.m_blendIndexes.size(), theRMGParse.m_dataSize );- CHECK_EQ( (Int)theRMGParse.m_blends.size(), theRMGParse.m_numBlendedTiles - 1 );- CHECK( theRMGParse.m_numBlendedTiles < 16193 ); // NUM_BLEND_TILES, the reader's ceiling+ CHECK_EQ( (Int)theRMGParse.m_blendIndexes.size(), theRMGParse.m_dataSize );+ CHECK_EQ( (Int)theRMGParse.m_extraBlendIndexes.size(), theRMGParse.m_dataSize );+ CHECK_EQ( (Int)theRMGParse.m_blends.size(), theRMGParse.m_numBlendedTiles - 1 );+ CHECK( theRMGParse.m_numBlendedTiles < 16193 ); // NUM_BLEND_TILES, the reader's ceiling- // Every entry has to be one the reader will take: a real tile, the alpha blend rather than a- // custom edge class this map never declares, and the sentinel it asserts on.- Int i;- for( i = 0; i < (Int)theRMGParse.m_blends.size(); i++ )- {- const RMGBlendEntry& entry = theRMGParse.m_blends[i];- Int source = entry.m_blendTileIndex >> 2;+ // Every entry has to be one the reader will take: a real tile, the alpha blend rather than a+ // custom edge class this map never declares, and the sentinel it asserts on.+ Int i;+ for( i = 0; i < (Int)theRMGParse.m_blends.size(); i++ )+ {+ const RMGBlendEntry& entry = theRMGParse.m_blends[i];+ Int source = entry.m_blendTileIndex >> 2;- CHECK( source >= 0 );- CHECK( source < theRMGParse.m_numBitmapTiles );- CHECK_EQ( entry.m_customBlendEdgeClass, -1 );- CHECK_EQ( entry.m_flag, 0x7ADA0000 );+ CHECK( source >= 0 );+ CHECK( source < theRMGParse.m_numBitmapTiles );+ CHECK_EQ( entry.m_customBlendEdgeClass, -1 );+ CHECK_EQ( entry.m_flag, 0x7ADA0000 );- // A blend that asks for nothing is a table row nothing can draw.- CHECK( entry.m_horizontal || entry.m_vertical || entry.m_rightDiagonal ||- entry.m_leftDiagonal );- }+ // A blend that asks for nothing is a table row nothing can draw.+ CHECK( entry.m_horizontal || entry.m_vertical || entry.m_rightDiagonal ||+ entry.m_leftDiagonal );+ }- Int boundaries = 0;- Int blended = 0;+ std::vector<Int> ground( width * height );+ for( i = 0; i < width * height; i++ )+ ground[i] = (theRMGParse.m_tiles[i] >> 2) / 4;- for( Int y = 1; y < height - 1; y++ )- {- for( Int x = 1; x < width - 1; x++ )- {- Int mine = (theRMGParse.m_tiles[y * width + x] >> 2) / 4;+ Int boundaries = 0;+ Int extraLayers = 0;+ Int wrongCorners = 0;+ Int wrongCuts = 0;+ Int badIndexes = 0;- /* The strongest of the eight neighbours is the one whose texture bleeds in, and the- low side of the boundary is the cell that has to carry it. Diagonals count: a- corner touching a rock cell is a corner of rock. */- Int strongest = mine;- for( Int dy = -1; dy <= 1; dy++ )+ for( Int y = 1; y < height - 1; y++ )+ {+ for( Int x = 1; x < width - 1; x++ ){- for( Int dx = -1; dx <= 1; dx++ )+ Int mine = ground[y * width + x];++ // What the four cells round each corner reach: the highest ground among them.+ static const Int cornerDX[4] = { 0, 1, 1, 0 };+ static const Int cornerDY[4] = { 0, 0, 1, 1 };+ Int highest[4];+ Bool boundary = FALSE;+ for( Int c = 0; c < 4; c++ ){- Int theirs = (theRMGParse.m_tiles[(y + dy) * width + x + dx] >> 2) / 4;- if( theirs > strongest )- strongest = theirs;+ highest[c] = mine;+ for( Int oy = -1; oy <= 0; oy++ )+ {+ for( Int ox = -1; ox <= 0; ox++ )+ {+ Int theirs = ground[(y + cornerDY[c] + oy) * width + x + cornerDX[c] + ox];+ if( theirs > highest[c] )+ highest[c] = theirs;+ }+ }+ if( highest[c] != mine )+ boundary = TRUE;+ }+ if( boundary )+ boundaries++;++ // The layers this cell draws, lowest first, and the ground it ends up showing.+ Int shown[4] = { mine, mine, mine, mine };+ Int cut = -1;+ Int previousClass = mine;+ Short layers[2] = { theRMGParse.m_blendIndexes[y * width + x],+ theRMGParse.m_extraBlendIndexes[y * width + x] };+ for( Int layer = 0; layer < 2; layer++ )+ {+ if( layers[layer] == 0 )+ continue;+ if( layers[layer] < 0 || layers[layer] >= theRMGParse.m_numBlendedTiles || (layer == 1 && layers[0] == 0) )+ {+ badIndexes++;+ continue;+ }+ if( layer == 1 )+ extraLayers++;++ const RMGBlendEntry& entry = theRMGParse.m_blends[layers[layer] - 1];+ Int layerClass = (entry.m_blendTileIndex >> 2) / 4;+ if( layerClass <= previousClass )+ badIndexes++; // a layer has to be a higher ground than what it covers+ previousClass = layerClass;++ Bool alpha[4];+ Bool flip;+ RMGBlendCorners( entry, alpha, &flip );+ for( Int c = 0; c < 4; c++ )+ {+ if( alpha[c] != (highest[c] >= layerClass) )+ wrongCorners++;+ if( alpha[c] )+ shown[c] = layerClass;+ }+ if( cut >= 0 && cut != (flip ? 1 : 0) )+ wrongCuts++;+ cut = flip ? 1 : 0;}- }-- if( strongest == mine )- continue;-- boundaries++;-- Int blendIndex = theRMGParse.m_blendIndexes[y * width + x];- if( blendIndex <= 0 )- continue;-- CHECK( blendIndex < theRMGParse.m_numBlendedTiles );- // and the texture painted over it is the neighbour's, not some third one- Int blendClass = (theRMGParse.m_blends[blendIndex - 1].m_blendTileIndex >> 2) / 4;- CHECK_EQ( blendClass, strongest );- blended++;+ for( Int c = 0; c < 4; c++ )+ {+ if( shown[c] != highest[c] )+ wrongCorners++;+ }+ }}- }- CHECK( boundaries > 100 );- CHECK_EQ( blended, boundaries );+ CHECK( boundaries > 100 );+ CHECK( extraLayers > 0 );+ CHECK_EQ( badIndexes, 0 );+ CHECK_EQ( wrongCorners, 0 );+ CHECK_EQ( wrongCuts, 0 );+ }}//-------------------------------------------------------------------------------------------------@@ -9760,14 +9874,14 @@ TEST(the_generator_still_turns_a_seed_into_the_bytes_it_used_to){CHECK( bootOnce() );- CHECK_EQ( RANDOM_MAP_GENERATOR_VERSION, 12 );+ CHECK_EQ( RANDOM_MAP_GENERATOR_VERSION, 13 );struct RMGFingerprint { Int m_seed, m_players, m_cells; UnsignedInt m_crc; };static const RMGFingerprint theFingerprints[] ={- { 0, 2, 64, 0x784968C4 },- { 12345, 4, 96, 0xCF747041 },- { 7, 8, 128, 0xC7C6D183 },+ { 0, 2, 64, 0x3C830A0F },+ { 12345, 4, 96, 0xAB2D49CF },+ { 7, 8, 128, 0xF237C0BA },};const Int numFingerprints = sizeof(theFingerprints) / sizeof(theFingerprints[0]);@@ -9972,8 +10086,10 @@ TEST(the_ground_is_textured_by_what_the_ground_is_doing)/* Rock starts at under half the cliff slope, so since the rolling maps of generator version 10most of it is hillside a tank drives up. 72 maps over twelve seeds, two sizes and 2/4/6- players painted 17.8% of the ground rock on average and 23.6% at most; this seed paints 22%.- What must stay small is the ground nobody can cross, and that is under 2.3% on all 72. */+ players painted 17.8% of the ground rock on average and 23.6% at most in version 12, a good+ part of it single cells. Version 13 reads the slope over 3x3 cells and votes the strays out:+ 12.1% on average, 34.0% at most, in patches; this seed paints 11%. What must stay small is+ the ground nobody can cross, and that was under 2.3% on all 72. */CHECK( cellsPerClass[3] < total / 4 );const Real cliffLimit = 9.8f; // PATHFIND_CLIFF_SLOPE_LIMIT_F