MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / generateRivers

Function generateRivers

src/OpenLoco/src/Map/MapGenerator/MapGenerator.cpp:72–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

70 }
71
72 static void generateRivers(const Scenario::Options& options, HeightMap& heightMap)
73 {
74 for (auto i = 0; i < options.numRiverbeds; i++)
75 {
76 auto& gs = getGameState();
77 const auto riverEastWest = gs.rng.randBool();
78 const auto riverWidth = gs.rng.randNext(options.minRiverWidth, options.maxRiverWidth);
79 const auto riverbedHeight = std::max<uint8_t>(gs.seaLevel > 0 ? gs.seaLevel - 1 : 1, options.minLandHeight);
80
81 // We'll be varying the bank width as we meander
82 auto riverbankWidth = options.riverbankWidth;
83 auto totalRiverWidth = riverWidth + 2 * riverbankWidth;
84 auto easternBankOffset = riverWidth + riverbankWidth;
85
86 // Pivot: generate a random X position
87 auto xStartPos = getGameState().rng.randNext(0.15 * heightMap.width, 0.85 * heightMap.width);
88 for (auto yPos = 0; yPos < heightMap.height; yPos++)
89 {
90 for (auto xOffset = 0; xOffset < totalRiverWidth; xOffset++)
91 {
92 auto pos = TilePos2(xStartPos + xOffset, yPos);
93 if (!riverEastWest)
94 {
95 pos = TilePos2(pos.y, pos.x);
96 }
97
98 if (!validCoords(pos))
99 {
100 // We might meander back to a valid position later,
101 // so we're only breaking out of the inner loop.
102 break;
103 }
104
105 if (riverbankWidth > 0 && xOffset < riverbankWidth)
106 {
107 // Western riverbank (high to low)
108 auto bankPos = riverbankWidth - xOffset;
109 auto bankHeight = heightMap[pos] * bankPos / riverbankWidth;
110 heightMap[pos] = std::max<uint8_t>(riverbedHeight, bankHeight);
111 }
112 else if (riverbankWidth > 0 && xOffset > easternBankOffset)
113 {
114 // Eastern riverbank (low to high)
115 auto bankPos = xOffset - easternBankOffset;
116 auto bankHeight = heightMap[pos] * bankPos / riverbankWidth;
117 heightMap[pos] = std::max<uint8_t>(riverbedHeight, bankHeight);
118 }
119 else
120 {
121 // Simply carve out the river
122 heightMap[pos] = riverbedHeight;
123 }
124 }
125
126 // Let the river meander slightly
127 const auto meanderRate = options.riverMeanderRate;
128 if (meanderRate > 0 && yPos % 4 == 0)
129 {

Callers 1

generateFunction · 0.85

Calls 3

validCoordsFunction · 0.85
randBoolMethod · 0.80
randNextMethod · 0.45

Tested by

no test coverage detected