906 lines
35 KiB
C++
906 lines
35 KiB
C++
// This file is part of c-evo-evo.
|
|
//
|
|
// This software is governed by the CeCILL license under French law and
|
|
// abiding by the rules of distribution of free software. You can use,
|
|
// modify and/or redistribute the software under the terms of the CeCILL
|
|
// license as circulated by CEA, CNRS and INRIA at the following URL
|
|
// "http://www.cecill.info".
|
|
//
|
|
// As a counterpart to the access to the source code and rights to copy,
|
|
// modify and redistribute granted by the license, users are provided only
|
|
// with a limited warranty and the software's author, the holder of the
|
|
// economic rights, and the successive licensors have only limited
|
|
// liability.
|
|
//
|
|
// In this respect, the user's attention is drawn to the risks associated
|
|
// with loading, using, modifying and/or developing or reproducing the
|
|
// software by the user in light of its specific status of free software,
|
|
// that may mean that it is complicated to manipulate, and that also
|
|
// therefore means that it is reserved for developers and experienced
|
|
// professionals having in-depth computer knowledge. Users are therefore
|
|
// encouraged to load and test the software's suitability as regards their
|
|
// requirements in conditions enabling the security of their systems and/or
|
|
// data to be ensured and, more generally, to use and operate it in the
|
|
// same conditions as regards security.
|
|
//
|
|
// The fact that you are presently reading this means that you have had
|
|
// knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
// --- Map Creation --- (see Cevo/Database.h)
|
|
|
|
#include "Cevo/Database.h"
|
|
#include "Cevo/IPQ.h"
|
|
|
|
#include "DatabaseInternal.h"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <climits>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <vector>
|
|
|
|
namespace Cevo {
|
|
namespace Database {
|
|
|
|
namespace {
|
|
// Equivalent of Database.pas's private (implementation-section) `Primitive`
|
|
// global - not exposed in the header, matching the original's own
|
|
// interface/implementation visibility split.
|
|
int primitive = 0;
|
|
// Database.pas's private `Elevation`/`ElCount` (map elevation and how often
|
|
// each elevation value occurs), shared by createElevation() and createMap().
|
|
std::array<std::uint8_t, lxmax * lymax> elevation;
|
|
std::array<int, 256> elCount;
|
|
|
|
inline std::uint32_t terrainOf(int loc)
|
|
{
|
|
return realMap[static_cast<std::size_t>(loc)] & static_cast<std::uint32_t>(fTerrain);
|
|
}
|
|
|
|
inline bool isLand(int loc)
|
|
{
|
|
return terrainOf(loc) >= static_cast<std::uint32_t>(fGrass);
|
|
}
|
|
|
|
// Equivalent of Protocol.pas's DelphiRandom: Extended overload.
|
|
long double delphiRandomFloat()
|
|
{
|
|
return static_cast<long double>(delphiRandom(INT_MAX)) / INT_MAX;
|
|
}
|
|
} // namespace
|
|
|
|
void calculatePrimitive()
|
|
{
|
|
primitive = 1;
|
|
int i = 2;
|
|
while (i * i <= mapSize + 1) { // test whether prime
|
|
if ((mapSize + 1) % i == 0)
|
|
primitive = 0;
|
|
++i;
|
|
}
|
|
|
|
if (primitive > 0) {
|
|
int j = 0;
|
|
do {
|
|
++primitive;
|
|
i = 1;
|
|
j = 0;
|
|
do {
|
|
++j;
|
|
i = i * primitive % (mapSize + 1);
|
|
} while (!(i == 1 || j == mapSize + 1));
|
|
} while (!(j == mapSize));
|
|
}
|
|
}
|
|
|
|
bool mapGeneratorAvailable()
|
|
{
|
|
return primitive > 0 && lx >= 20 && ly >= 40;
|
|
}
|
|
|
|
void createElevation()
|
|
{
|
|
constexpr int D = 64;
|
|
constexpr double Smooth = 0.049; // causes low amplitude of short waves
|
|
constexpr double Detail = 0.095; // causes short period of short waves
|
|
constexpr int Merge = 5; // elevation merging range at the connection line of the round world, in relation to lx
|
|
constexpr double Pi = 3.14159265358979323846;
|
|
|
|
float sa[D + 1], ca[D + 1], f1[D + 1], f2[D + 1];
|
|
|
|
const auto value = [&](int x, int y) -> float { // elevation formula
|
|
float result = 0;
|
|
for (int i = 1; i <= D; ++i) {
|
|
// x values effectively multiplied with 2 to get 2 horizontal periods of the prime waves
|
|
result = static_cast<float>(
|
|
result + std::sin(static_cast<double>(f1[i]) * ((x * 2 + (y & 1)) * static_cast<double>(sa[i]) +
|
|
y * 1.5 * static_cast<double>(ca[i]))) *
|
|
static_cast<double>(f2[i]));
|
|
}
|
|
return result;
|
|
};
|
|
|
|
for (int x = 1; x <= D; ++x) { // prepare formula parameters
|
|
double v;
|
|
if (x == 1)
|
|
v = Pi / 2; // first wave goes horizontal
|
|
else
|
|
v = static_cast<double>(delphiRandomFloat()) * 2 * Pi;
|
|
sa[x] = static_cast<float>(std::sin(v) / lx);
|
|
ca[x] = static_cast<float>(std::cos(v) / ly);
|
|
f1[x] = static_cast<float>(2 * Pi * std::exp(Detail * (x - 1)));
|
|
f2[x] = static_cast<float>(std::exp(-x * Smooth));
|
|
}
|
|
|
|
const int imerge = 2 * lx / Merge;
|
|
elCount.fill(0);
|
|
for (int x = 0; x < lx; ++x) {
|
|
for (int y = 0; y < ly; ++y) {
|
|
float v = value(x, y);
|
|
if (x * 2 < imerge)
|
|
v = (x * 2 * v + (imerge - x * 2) * value(x + lx, y)) / imerge;
|
|
const float pole = 2.0f * y / ly - 1;
|
|
v = v - pole * pole * (pole * pole); // soft cut at poles
|
|
std::uint8_t e;
|
|
if (v < -4)
|
|
e = 0;
|
|
else if (v > 8.75f)
|
|
e = 255;
|
|
else
|
|
e = static_cast<std::uint8_t>(std::nearbyint((v + 4) * 20));
|
|
elevation[static_cast<std::size_t>(x + lx * y)] = e;
|
|
++elCount[e];
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
|
|
// distribute rare resources; must be done after findContinents()
|
|
void rarePositions()
|
|
{
|
|
int areaCount[8][5];
|
|
int rareByArea[8][5] = {};
|
|
int rareAdjacent[8][5];
|
|
int rareLoc[12];
|
|
int dist[12][12];
|
|
Vicinity8Loc adjacent;
|
|
|
|
int totalRare = 0;
|
|
int rareMaxWater = 0;
|
|
do {
|
|
std::memset(areaCount, 0, sizeof(areaCount));
|
|
for (int y = 1; y <= ly - 2; ++y) {
|
|
const int yBlock = y * 5 / ly;
|
|
if (yBlock != (y + 1) * 5 / ly)
|
|
continue;
|
|
for (int x = 0; x < lx; ++x) {
|
|
const int xBlock = x * 8 / lx;
|
|
if (xBlock != (x + 1) * 8 / lx)
|
|
continue;
|
|
const int loc0 = x + lx * y;
|
|
if (!isLand(loc0))
|
|
continue;
|
|
int cnt = 0;
|
|
v8ToLoc(loc0, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int loc1 = adjacent[static_cast<std::size_t>(v8)];
|
|
if (loc1 >= 0 && loc1 < mapSize && !isLand(loc1))
|
|
++cnt; // count adjacent water
|
|
}
|
|
if (cnt <= rareMaxWater) { // inner land
|
|
++areaCount[xBlock][yBlock];
|
|
if (delphiRandom(areaCount[xBlock][yBlock]) == 0)
|
|
rareByArea[xBlock][yBlock] = loc0;
|
|
}
|
|
}
|
|
}
|
|
totalRare = 0;
|
|
for (int x = 0; x < 8; ++x)
|
|
for (int y = 0; y < 5; ++y)
|
|
if (areaCount[x][y] > 0)
|
|
++totalRare;
|
|
++rareMaxWater;
|
|
} while (totalRare < 12);
|
|
|
|
const auto sameContinent = [&](int x0, int y0, int x1, int y1) {
|
|
return continent[static_cast<std::size_t>(rareByArea[x0][y0])] ==
|
|
continent[static_cast<std::size_t>(rareByArea[x1][y1])];
|
|
};
|
|
while (totalRare > 12) { // remove rarebyarea resources too close to each other
|
|
std::memset(rareAdjacent, 0, sizeof(rareAdjacent));
|
|
for (int x = 0; x < 8; ++x) {
|
|
for (int y = 0; y < 5; ++y) {
|
|
if (areaCount[x][y] <= 0)
|
|
continue;
|
|
if (areaCount[(x + 1) % 8][y] > 0 && sameContinent(x, y, (x + 1) % 8, y)) {
|
|
++rareAdjacent[x][y];
|
|
++rareAdjacent[(x + 1) % 8][y];
|
|
}
|
|
if (y < 4) {
|
|
if (areaCount[x][y + 1] > 0 && sameContinent(x, y, x, y + 1)) {
|
|
++rareAdjacent[x][y];
|
|
++rareAdjacent[x][y + 1];
|
|
}
|
|
if (areaCount[(x + 1) % 8][y + 1] > 0 && sameContinent(x, y, (x + 1) % 8, y + 1)) {
|
|
++rareAdjacent[x][y];
|
|
++rareAdjacent[(x + 1) % 8][y + 1];
|
|
}
|
|
if (areaCount[(x + 7) % 8][y + 1] > 0 && sameContinent(x, y, (x + 7) % 8, y + 1)) {
|
|
++rareAdjacent[x][y];
|
|
++rareAdjacent[(x + 7) % 8][y + 1];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
int xworst = 0;
|
|
int yworst = 0;
|
|
int cnt = 0;
|
|
for (int x = 0; x < 8; ++x) {
|
|
for (int y = 0; y < 5; ++y) {
|
|
if (areaCount[x][y] <= 0)
|
|
continue;
|
|
if (cnt == 0 || rareAdjacent[x][y] > rareAdjacent[xworst][yworst]) {
|
|
xworst = x;
|
|
yworst = y;
|
|
cnt = 1;
|
|
} else if (rareAdjacent[x][y] == rareAdjacent[xworst][yworst]) {
|
|
++cnt;
|
|
if (delphiRandom(cnt) == 0) {
|
|
xworst = x;
|
|
yworst = y;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
areaCount[xworst][yworst] = 0;
|
|
--totalRare;
|
|
}
|
|
|
|
int cnt = 0;
|
|
for (int x = 0; x < 8; ++x)
|
|
for (int y = 0; y < 5; ++y)
|
|
if (areaCount[x][y] > 0)
|
|
rareLoc[cnt++] = rareByArea[x][y];
|
|
for (int i = 0; i < 12; ++i) {
|
|
std::uint32_t &tile = realMap[static_cast<std::size_t>(rareLoc[i])];
|
|
tile = (tile & ~static_cast<std::uint32_t>(fTerrain | fSpecial)) | static_cast<std::uint32_t>(fDesert | fDeadLands);
|
|
for (int dy = -1; dy <= 1; ++dy) {
|
|
for (int dx = -1; dx <= 1; ++dx) {
|
|
if (((dx + dy) & 1) != 0)
|
|
continue;
|
|
const int loc1 = dLoc(rareLoc[i], dx, dy);
|
|
if (loc1 >= 0 && terrainOf(loc1) == static_cast<std::uint32_t>(fMountains)) {
|
|
std::uint32_t &near = realMap[static_cast<std::size_t>(loc1)];
|
|
near = (near & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(fHills);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (int i = 0; i < 12; ++i)
|
|
for (int j = 0; j < 12; ++j)
|
|
dist[i][j] = distance(rareLoc[i], rareLoc[j]);
|
|
|
|
int iBest = 0;
|
|
int jBest = 0;
|
|
const int minDist = distance(0, mapSize - (lx >> 1)) >> 1;
|
|
for (int rareType = 1; rareType <= 3; ++rareType) {
|
|
cnt = 0;
|
|
for (int i = 0; i < 12; ++i) {
|
|
if (rareLoc[i] < 0)
|
|
continue;
|
|
for (int j = 0; j < 12; ++j) {
|
|
if (rareLoc[j] < 0)
|
|
continue;
|
|
if (cnt > 0 && dist[iBest][jBest] >= minDist) {
|
|
if (dist[i][j] >= minDist) {
|
|
++cnt;
|
|
if (delphiRandom(cnt) == 0) {
|
|
iBest = i;
|
|
jBest = j;
|
|
}
|
|
}
|
|
} else if (cnt == 0 || dist[i][j] > dist[iBest][jBest]) {
|
|
iBest = i;
|
|
jBest = j;
|
|
cnt = 1;
|
|
}
|
|
}
|
|
}
|
|
realMap[static_cast<std::size_t>(rareLoc[iBest])] |= static_cast<std::uint32_t>(rareType) << 25;
|
|
realMap[static_cast<std::size_t>(rareLoc[jBest])] |= static_cast<std::uint32_t>(rareType) << 25;
|
|
rareLoc[iBest] = -1;
|
|
rareLoc[jBest] = -1;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void createMap(bool preview)
|
|
{
|
|
constexpr int ShHiHills = 6; // of land
|
|
constexpr int ShMountains = 6; // of land
|
|
constexpr int ShRandHills = 12; // of land
|
|
constexpr int ShTestRiver = 40;
|
|
constexpr int ShSwamp = 25; // of grassland
|
|
constexpr int MinRivLen = 3;
|
|
constexpr int Unification = 70;
|
|
constexpr int HotUnification = 50; // min. 25
|
|
|
|
// terrain distribution (columns are the terrain types 2..9: Grs Dst Pra Tun - - - For)
|
|
static const float Zone[4][8] = { { 0.25f, 0, 0, 0.4f, 0, 0, 0, 0.35f },
|
|
{ 0.55f, 0, 0.1f, 0, 0, 0, 0, 0.35f },
|
|
{ 0.4f, 0, 0.35f, 0, 0, 0, 0, 0.25f },
|
|
{ 0, 0.7f, 0, 0, 0, 0, 0, 0.3f } };
|
|
|
|
const auto rndLow = [&](int y) -> std::uint32_t { // random lowland appropriate to climate
|
|
int z0;
|
|
float zPlus;
|
|
if (ly - 1 - y > y) {
|
|
z0 = 6 * y / ly;
|
|
zPlus = 6.0f * y / ly - z0;
|
|
} else {
|
|
z0 = 6 * (ly - 1 - y) / ly;
|
|
zPlus = 6.0f * (ly - 1 - y) / ly - z0;
|
|
}
|
|
float p0 = 1;
|
|
std::uint32_t result = 2;
|
|
for (int i = 2; i <= 9; ++i) {
|
|
const float p = Zone[z0][i - 2] * (1 - zPlus) + Zone[z0 + 1][i - 2] * zPlus;
|
|
// weight between zones z0 and z0+1
|
|
if (delphiRandomFloat() * p0 < p) {
|
|
result = static_cast<std::uint32_t>(i);
|
|
break;
|
|
}
|
|
p0 = p0 - p;
|
|
}
|
|
return result;
|
|
};
|
|
|
|
const auto runRiver = [&](int loc0) -> int { // runs river from start point loc0; return value: length
|
|
std::vector<int> from(static_cast<std::size_t>(lxmax) * lymax);
|
|
std::vector<int> time(static_cast<std::size_t>(lxmax) * lymax, -1);
|
|
IPQ q(mapSize);
|
|
q.put(loc0, 0);
|
|
int loc = 0;
|
|
int t = 0;
|
|
while (q.get(loc, t) && (realMap[static_cast<std::size_t>(loc)] & static_cast<std::uint32_t>(fRiver)) == 0) {
|
|
if (!isLand(loc)) {
|
|
bool oneTileLake = true;
|
|
for (int dir = 0; dir < 4; ++dir) {
|
|
const int loc1 = dLoc(loc, (dir & 1) * 2 - 1, (dir >> 1) * 2 - 1);
|
|
if (loc1 >= 0 && !isLand(loc1)) {
|
|
oneTileLake = false;
|
|
break;
|
|
}
|
|
}
|
|
if (!oneTileLake)
|
|
break;
|
|
}
|
|
time[static_cast<std::size_t>(loc)] = t;
|
|
for (int dir = 0; dir < 4; ++dir) {
|
|
const int loc1 = dLoc(loc, (dir & 1) * 2 - 1, (dir >> 1) * 2 - 1);
|
|
if (loc1 >= lx && loc1 < lx * (ly - 1) && time[static_cast<std::size_t>(loc1)] < 0) {
|
|
int cost;
|
|
if ((realMap[static_cast<std::size_t>(loc1)] & static_cast<std::uint32_t>(fRiver)) == 0) {
|
|
cost = elevation[static_cast<std::size_t>(loc1)] - elevation[static_cast<std::size_t>(loc)];
|
|
if (cost < 0)
|
|
cost = 0;
|
|
} else {
|
|
cost = 0;
|
|
}
|
|
if (q.put(loc1, t + (cost << 8) + 1))
|
|
from[static_cast<std::size_t>(loc1)] = loc;
|
|
}
|
|
}
|
|
}
|
|
const int loc1 = loc;
|
|
int result = 0;
|
|
while (loc != loc0) {
|
|
loc = from[static_cast<std::size_t>(loc)];
|
|
++result;
|
|
}
|
|
if (result > 1 && (result >= MinRivLen || isLand(loc1))) {
|
|
loc = loc1;
|
|
while (loc != loc0) {
|
|
loc = from[static_cast<std::size_t>(loc)];
|
|
std::uint32_t &tile = realMap[static_cast<std::size_t>(loc)];
|
|
const std::uint32_t terrain = tile & static_cast<std::uint32_t>(fTerrain);
|
|
if (terrain == static_cast<std::uint32_t>(fHills) || terrain == static_cast<std::uint32_t>(fMountains))
|
|
tile = static_cast<std::uint32_t>(fGrass | fRiver);
|
|
else if (terrain >= static_cast<std::uint32_t>(fGrass))
|
|
tile |= static_cast<std::uint32_t>(fRiver);
|
|
}
|
|
} else {
|
|
result = 0;
|
|
}
|
|
return result;
|
|
};
|
|
|
|
const auto setTerrain = [](int loc, int terrain) {
|
|
std::uint32_t &tile = realMap[static_cast<std::size_t>(loc)];
|
|
tile = (tile & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(terrain);
|
|
};
|
|
|
|
std::fill(realMap.begin(), realMap.begin() + mapSize, 0u);
|
|
int plus = 0;
|
|
int bMountains = 256;
|
|
while (plus < mapSize * landMass * ShMountains / 10000) {
|
|
--bMountains;
|
|
plus += elCount[static_cast<std::size_t>(bMountains)];
|
|
}
|
|
int count = plus;
|
|
plus = 0;
|
|
int bHills = bMountains;
|
|
while (plus < mapSize * landMass * ShHiHills / 10000) {
|
|
--bHills;
|
|
plus += elCount[static_cast<std::size_t>(bHills)];
|
|
}
|
|
count += plus;
|
|
int bLand = bHills;
|
|
while (count < mapSize * landMass / 100) {
|
|
--bLand;
|
|
count += elCount[static_cast<std::size_t>(bLand)];
|
|
}
|
|
|
|
for (int loc0 = lx; loc0 < lx * (ly - 1); ++loc0) {
|
|
const int e = elevation[static_cast<std::size_t>(loc0)];
|
|
if (e >= bMountains)
|
|
realMap[static_cast<std::size_t>(loc0)] = static_cast<std::uint32_t>(fMountains);
|
|
else if (e >= bHills)
|
|
realMap[static_cast<std::size_t>(loc0)] = static_cast<std::uint32_t>(fHills);
|
|
else if (e >= bLand)
|
|
realMap[static_cast<std::size_t>(loc0)] = static_cast<std::uint32_t>(fGrass);
|
|
}
|
|
|
|
Vicinity8Loc adjacent;
|
|
// remove one-tile islands
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0) {
|
|
if (realMap[static_cast<std::size_t>(loc0)] < static_cast<std::uint32_t>(fGrass))
|
|
continue;
|
|
count = 0;
|
|
v8ToLoc(loc0, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int loc1 = adjacent[static_cast<std::size_t>(v8)];
|
|
if (loc1 < 0 || loc1 >= mapSize || !isLand(loc1) || terrainOf(loc1) == static_cast<std::uint32_t>(fArctic))
|
|
++count; // count adjacent water
|
|
}
|
|
if (count == 8)
|
|
realMap[static_cast<std::size_t>(loc0)] = static_cast<std::uint32_t>(fOcean);
|
|
}
|
|
|
|
if (!preview) {
|
|
plus = 36 * 56 * 20 * ShTestRiver / (landMass * 100);
|
|
if (plus > mapSize)
|
|
plus = mapSize;
|
|
int loc0 = delphiRandom(mapSize);
|
|
for (int n = 0; n < plus; ++n) {
|
|
if (isLand(loc0) && loc0 >= lx && loc0 < mapSize - lx)
|
|
runRiver(loc0);
|
|
loc0 = (loc0 + 1) * primitive % (mapSize + 1) - 1;
|
|
}
|
|
}
|
|
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0) {
|
|
if (realMap[static_cast<std::size_t>(loc0)] == static_cast<std::uint32_t>(fGrass) &&
|
|
delphiRandom(100) < ShRandHills)
|
|
realMap[static_cast<std::size_t>(loc0)] |= static_cast<std::uint32_t>(fHills);
|
|
}
|
|
|
|
// make terrain types coherent
|
|
std::vector<int> copyFrom(static_cast<std::size_t>(lxmax) * lymax);
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0)
|
|
copyFrom[static_cast<std::size_t>(loc0)] = loc0;
|
|
|
|
for (int n = 0; n <= Unification * mapSize / 100; ++n) {
|
|
int y = delphiRandom(ly);
|
|
if (std::abs(y - (ly >> 1)) > ly / 4 + delphiRandom(ly * HotUnification / 100)) {
|
|
if (y < (ly >> 1))
|
|
y = (ly >> 1) - y;
|
|
else
|
|
y = ((3 * ly) >> 1) - y;
|
|
}
|
|
int loc0 = lx * y + delphiRandom(lx);
|
|
if (terrainOf(loc0) == static_cast<std::uint32_t>(fGrass)) {
|
|
const int dir = delphiRandom(4);
|
|
int loc1 = dLoc(loc0, (dir & 1) * 2 - 1, (dir >> 1) * 2 - 1);
|
|
if (loc1 >= 0 && terrainOf(loc1) == static_cast<std::uint32_t>(fGrass)) {
|
|
while (copyFrom[static_cast<std::size_t>(loc0)] != loc0)
|
|
loc0 = copyFrom[static_cast<std::size_t>(loc0)];
|
|
while (copyFrom[static_cast<std::size_t>(loc1)] != loc1)
|
|
loc1 = copyFrom[static_cast<std::size_t>(loc1)];
|
|
if (loc1 < loc0)
|
|
copyFrom[static_cast<std::size_t>(loc0)] = loc1;
|
|
else
|
|
copyFrom[static_cast<std::size_t>(loc1)] = loc0;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0) {
|
|
if (terrainOf(loc0) == static_cast<std::uint32_t>(fGrass) && copyFrom[static_cast<std::size_t>(loc0)] == loc0)
|
|
setTerrain(loc0, static_cast<int>(rndLow(loc0 / lx)));
|
|
}
|
|
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0) {
|
|
if (terrainOf(loc0) == static_cast<std::uint32_t>(fGrass)) {
|
|
int loc1 = loc0;
|
|
while (copyFrom[static_cast<std::size_t>(loc1)] != loc1)
|
|
loc1 = copyFrom[static_cast<std::size_t>(loc1)];
|
|
setTerrain(loc0, static_cast<int>(terrainOf(loc1)));
|
|
}
|
|
}
|
|
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0) { // change grassland to swamp
|
|
if (terrainOf(loc0) == static_cast<std::uint32_t>(fGrass) && delphiRandom(100) < ShSwamp)
|
|
setTerrain(loc0, fSwamp);
|
|
}
|
|
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0) { // change desert to prairie 1
|
|
if (terrainOf(loc0) != static_cast<std::uint32_t>(fDesert))
|
|
continue;
|
|
if ((realMap[static_cast<std::size_t>(loc0)] & static_cast<std::uint32_t>(fRiver)) != 0) {
|
|
count = 5;
|
|
} else {
|
|
count = 0;
|
|
for (int dir = 0; dir < 4; ++dir) {
|
|
const int loc1 = dLoc(loc0, (dir & 1) * 2 - 1, (dir >> 1) * 2 - 1);
|
|
if (loc1 >= 0 && !isLand(loc1))
|
|
count += 2;
|
|
}
|
|
}
|
|
if (count >= 4)
|
|
setTerrain(loc0, fPrairie);
|
|
}
|
|
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0) { // change desert to prairie 2
|
|
if (terrainOf(loc0) != static_cast<std::uint32_t>(fDesert))
|
|
continue;
|
|
count = 0;
|
|
for (int dir = 0; dir < 4; ++dir) {
|
|
const int loc1 = dLoc(loc0, (dir & 1) * 2 - 1, (dir >> 1) * 2 - 1);
|
|
if (loc1 >= 0 && terrainOf(loc1) != static_cast<std::uint32_t>(fDesert))
|
|
++count;
|
|
}
|
|
if (count >= 4)
|
|
setTerrain(loc0, fPrairie);
|
|
}
|
|
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0)
|
|
checkShore(loc0); // change ocean to shore
|
|
for (int x = 0; x < lx; ++x) {
|
|
realMap[static_cast<std::size_t>(x)] = static_cast<std::uint32_t>(fArctic);
|
|
if (realMap[static_cast<std::size_t>(x + lx)] >= static_cast<std::uint32_t>(fGrass))
|
|
setTerrain(x + lx, fTundra);
|
|
if (realMap[static_cast<std::size_t>(x + lx * (ly - 2))] >= static_cast<std::uint32_t>(fGrass))
|
|
setTerrain(x + lx * (ly - 2), fTundra);
|
|
realMap[static_cast<std::size_t>(x + lx * (ly - 1))] = static_cast<std::uint32_t>(fArctic);
|
|
}
|
|
|
|
for (int loc0 = 0; loc0 < mapSize; ++loc0) // define special terrain tiles
|
|
realMap[static_cast<std::size_t>(loc0)] |= (actualSpecialTile(loc0) << 5) | (0xFu << 27);
|
|
|
|
if (!preview) {
|
|
findContinents();
|
|
rarePositions();
|
|
}
|
|
}
|
|
|
|
// define nation start positions; must be done after findContinents()
|
|
void startPositions()
|
|
{
|
|
enum CountGoodKind { cgBest, cgFlat, cgLand };
|
|
CountGoodKind countGood = cgBest;
|
|
|
|
const auto isFlatTerrain = [](std::uint32_t terrain) {
|
|
return terrain == static_cast<std::uint32_t>(fGrass) || terrain == static_cast<std::uint32_t>(fPrairie) ||
|
|
terrain == static_cast<std::uint32_t>(fTundra) || terrain == static_cast<std::uint32_t>(fSwamp) ||
|
|
terrain == static_cast<std::uint32_t>(fForest);
|
|
};
|
|
const auto isGoodTile = [&](int loc) {
|
|
const int xLoc = loc % lx;
|
|
const int yLoc = loc / lx;
|
|
if ((realMap[static_cast<std::size_t>(loc)] & static_cast<std::uint32_t>(fDeadLands)) != 0)
|
|
return false;
|
|
switch (countGood) {
|
|
case cgBest:
|
|
return isFlatTerrain(terrainOf(loc)) && (((lymax + xLoc - (yLoc >> 1)) >> 1) + xLoc + ((yLoc + 1) >> 1)) % 2 != 0;
|
|
case cgFlat:
|
|
return isFlatTerrain(terrainOf(loc));
|
|
case cgLand:
|
|
return isLand(loc);
|
|
}
|
|
return false;
|
|
};
|
|
|
|
constexpr int MaxCityLoc = 64;
|
|
|
|
int nAlive = 0;
|
|
for (int p1 = 0; p1 < nPl; ++p1)
|
|
if (((1 << p1) & gAlive) != 0)
|
|
++nAlive;
|
|
if (nAlive == 0)
|
|
return;
|
|
|
|
// count good tiles
|
|
std::vector<std::uint16_t> ccount(static_cast<std::size_t>(lxmax) * lymax, 0);
|
|
for (int loc = 0; loc < mapSize; ++loc) {
|
|
const std::uint32_t terrain = terrainOf(loc);
|
|
const std::size_t cont = static_cast<std::size_t>(continent[static_cast<std::size_t>(loc)]);
|
|
if (terrain == static_cast<std::uint32_t>(fGrass)) {
|
|
if (actualSpecialTile(loc) == 1)
|
|
ccount[cont] = static_cast<std::uint16_t>(ccount[cont] + 3);
|
|
else
|
|
ccount[cont] = static_cast<std::uint16_t>(ccount[cont] + 2);
|
|
} else if (terrain == static_cast<std::uint32_t>(fPrairie) || terrain == static_cast<std::uint32_t>(fSwamp) ||
|
|
terrain == static_cast<std::uint32_t>(fForest) || terrain == static_cast<std::uint32_t>(fHills)) {
|
|
ccount[cont] = static_cast<std::uint16_t>(ccount[cont] + 1);
|
|
}
|
|
}
|
|
|
|
int sc[nPl + 2], sccount[nPl + 2], startLoc0[nPl + 2];
|
|
int cityLoc[nPl + 2][MaxCityLoc];
|
|
int nCityLoc[nPl + 2];
|
|
int testStartLoc[nPl + 2];
|
|
int restLoc[MaxCityLoc];
|
|
int irrLoc[32];
|
|
Vicinity21Loc radius;
|
|
Vicinity8Loc adjacent;
|
|
|
|
int loc = 0;
|
|
while (ccount[static_cast<std::size_t>(loc)] > 0)
|
|
++loc;
|
|
for (int i = 1; i <= nAlive; ++i) {
|
|
sc[i] = loc;
|
|
sccount[i] = 1;
|
|
}
|
|
// init with zero size start continents, then search bigger ones
|
|
for (loc = 0; loc < mapSize; ++loc) {
|
|
if (ccount[static_cast<std::size_t>(loc)] > 0) { // search biggest continents
|
|
int p1 = nAlive + 1;
|
|
while (p1 > 1 && ccount[static_cast<std::size_t>(loc)] > ccount[static_cast<std::size_t>(sc[p1 - 1])]) {
|
|
if (p1 < nAlive + 1)
|
|
sc[p1] = sc[p1 - 1];
|
|
--p1;
|
|
}
|
|
if (p1 < nAlive + 1)
|
|
sc[p1] = loc;
|
|
}
|
|
}
|
|
const auto cc = [&](int index) { return static_cast<int>(ccount[static_cast<std::size_t>(sc[index])]); };
|
|
int nsc = nAlive;
|
|
do {
|
|
int c = 1; // search least crowded continent after smallest
|
|
for (int i = 2; i <= nsc - 1; ++i)
|
|
if (cc(i) * (2 * sccount[c] + 1) > cc(c) * (2 * sccount[i] + 1))
|
|
c = i;
|
|
if (cc(nsc) * (2 * sccount[c] + 1) > cc(c))
|
|
break; // even least crowded continent is more crowded than smallest
|
|
++sccount[c];
|
|
--nsc;
|
|
} while (!(sccount[nsc] > 1));
|
|
|
|
int minGood = 7;
|
|
countGood = cgBest;
|
|
bool ok;
|
|
do {
|
|
--minGood;
|
|
if (minGood == 3 && countGood < cgLand) { // too demanding!
|
|
countGood = static_cast<CountGoodKind>(countGood + 1);
|
|
minGood = 6;
|
|
}
|
|
std::memset(nCityLoc, 0, sizeof(nCityLoc));
|
|
loc = delphiRandom(mapSize);
|
|
for (int i = 0; i < mapSize; ++i) {
|
|
if (((loc >= 4 * lx && loc < mapSize - 4 * lx) || countGood >= cgLand) && isGoodTile(loc)) {
|
|
int c = nsc;
|
|
while (c > 0 && continent[static_cast<std::size_t>(loc)] != sc[c])
|
|
--c;
|
|
if (c > 0 && nCityLoc[c] < MaxCityLoc) {
|
|
int cntGood = 1;
|
|
v21ToLoc(loc, radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
if (v21 == CityOwnTile)
|
|
continue;
|
|
const int loc1 = radius[static_cast<std::size_t>(v21)];
|
|
if (loc1 >= 0 && loc1 < mapSize && isGoodTile(loc1))
|
|
++cntGood;
|
|
}
|
|
if (cntGood >= minGood) {
|
|
cityLoc[c][nCityLoc[c]] = loc;
|
|
++nCityLoc[c];
|
|
}
|
|
}
|
|
}
|
|
loc = (loc + 1) * primitive % (mapSize + 1) - 1;
|
|
}
|
|
|
|
ok = true;
|
|
for (int c = 1; c <= nsc; ++c) {
|
|
if (nCityLoc[c] < sccount[c] * (8 - minGood) / (7 - minGood)) {
|
|
ok = false;
|
|
break;
|
|
}
|
|
}
|
|
} while (!ok);
|
|
|
|
const int fineDistSqr = mapSize * landMass * 9 / (nAlive * 100);
|
|
int p1 = 1;
|
|
for (int c = 1; c <= nsc; ++c) { // for all start continents
|
|
if (sccount[c] == 1) {
|
|
startLoc0[p1] = cityLoc[c][delphiRandom(nCityLoc[c])];
|
|
} else {
|
|
int bestDist = 0;
|
|
int n = (1 << sccount[c]) * 32; // number of tries to find good distribution
|
|
if (n > (1 << 12))
|
|
n = 1 << 12;
|
|
while (n > 0 && bestDist * bestDist < fineDistSqr) {
|
|
int minDist = INT_MAX;
|
|
int nRest = nCityLoc[c];
|
|
for (int i = 0; i < nRest; ++i)
|
|
restLoc[i] = cityLoc[c][i];
|
|
for (int i = 0; i < sccount[c]; ++i) {
|
|
if (nRest == 0)
|
|
break;
|
|
int j = delphiRandom(nRest);
|
|
testStartLoc[i] = restLoc[j];
|
|
restLoc[j] = restLoc[nRest - 1];
|
|
--nRest;
|
|
for (j = 0; j < i; ++j) {
|
|
const int testDist = distance(testStartLoc[i], testStartLoc[j]);
|
|
if (testDist < minDist)
|
|
minDist = testDist;
|
|
}
|
|
if (i == sccount[c] - 1) {
|
|
assert(minDist > bestDist);
|
|
bestDist = minDist;
|
|
for (j = 0; j < sccount[c]; ++j)
|
|
startLoc0[p1 + j] = testStartLoc[j];
|
|
} else if (bestDist > 0) {
|
|
j = 0;
|
|
while (j < nRest) { // remove all locs from rest which have too little distance to this one
|
|
const int testDist = distance(testStartLoc[i], restLoc[j]);
|
|
if (testDist <= bestDist) {
|
|
restLoc[j] = restLoc[nRest - 1];
|
|
--nRest;
|
|
} else {
|
|
++j;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
--n;
|
|
}
|
|
}
|
|
p1 += sccount[c];
|
|
}
|
|
|
|
// make start locs fertile
|
|
for (p1 = 1; p1 <= nAlive; ++p1) {
|
|
{
|
|
std::uint32_t &tile = realMap[static_cast<std::size_t>(startLoc0[p1])];
|
|
tile = (tile & ~static_cast<std::uint32_t>(fTerrain | fSpecial)) | static_cast<std::uint32_t>(fGrass | fSpecial1);
|
|
}
|
|
int cntGood = 1;
|
|
int cntGoodGrass = 1;
|
|
v21ToLoc(startLoc0[p1], radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
if (v21 == CityOwnTile)
|
|
continue;
|
|
const int loc1 = radius[static_cast<std::size_t>(v21)];
|
|
if (loc1 >= 0 && loc1 < mapSize && isGoodTile(loc1)) {
|
|
if (terrainOf(loc1) == static_cast<std::uint32_t>(fGrass))
|
|
++cntGoodGrass;
|
|
else
|
|
++cntGood;
|
|
}
|
|
}
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
if (v21 == CityOwnTile)
|
|
continue;
|
|
const int loc1 = radius[static_cast<std::size_t>(v21)];
|
|
if (loc1 < 0 || loc1 >= mapSize || (realMap[static_cast<std::size_t>(loc1)] & static_cast<std::uint32_t>(fDeadLands)) != 0)
|
|
continue;
|
|
std::uint32_t &tile = realMap[static_cast<std::size_t>(loc1)];
|
|
if (isGoodTile(loc1) && delphiRandom(cntGood) < minGood - cntGoodGrass + 1) {
|
|
tile = (tile & ~static_cast<std::uint32_t>(fTerrain | fSpecial)) | static_cast<std::uint32_t>(fGrass);
|
|
tile |= actualSpecialTile(loc1) << 5;
|
|
} else if (terrainOf(loc1) == static_cast<std::uint32_t>(fDesert)) {
|
|
tile = (tile & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(fPrairie);
|
|
} else if ((terrainOf(loc1) == static_cast<std::uint32_t>(fPrairie) ||
|
|
terrainOf(loc1) == static_cast<std::uint32_t>(fTundra) ||
|
|
terrainOf(loc1) == static_cast<std::uint32_t>(fSwamp)) &&
|
|
delphiRandom(2) == 0) {
|
|
tile = (tile & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(fForest);
|
|
}
|
|
}
|
|
|
|
// first irrigation
|
|
int nIrrLoc = 0;
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
if (v21 == CityOwnTile)
|
|
continue;
|
|
const int loc1 = radius[static_cast<std::size_t>(v21)];
|
|
if (loc1 >= 0 && loc1 < mapSize &&
|
|
(realMap[static_cast<std::size_t>(loc1)] & static_cast<std::uint32_t>(fTerrain | fSpecial)) ==
|
|
static_cast<std::uint32_t>(fGrass | fSpecial1)) {
|
|
irrLoc[nIrrLoc] = loc1;
|
|
++nIrrLoc;
|
|
}
|
|
}
|
|
int i = 2;
|
|
if (i > nIrrLoc)
|
|
i = nIrrLoc;
|
|
while (i > 0) {
|
|
const int j = delphiRandom(nIrrLoc);
|
|
realMap[static_cast<std::size_t>(irrLoc[j])] |= static_cast<std::uint32_t>(tiIrrigation);
|
|
irrLoc[j] = irrLoc[nIrrLoc - 1];
|
|
--nIrrLoc;
|
|
--i;
|
|
}
|
|
}
|
|
|
|
startLoc[0] = 0;
|
|
for (p1 = 0; p1 < nPl; ++p1) {
|
|
if (((1 << p1) & gAlive) == 0)
|
|
continue;
|
|
int i;
|
|
do {
|
|
i = delphiRandom(nAlive) + 1;
|
|
} while (startLoc0[i] < 0);
|
|
startLoc[static_cast<std::size_t>(p1)] = startLoc0[i];
|
|
startLoc0[i] = -1;
|
|
}
|
|
saveMapCenterLoc = startLoc[0];
|
|
|
|
// second unit starting position
|
|
int bestDist = 0;
|
|
int n = 1;
|
|
for (p1 = 0; p1 < nPl; ++p1) {
|
|
if (((1 << p1) & gAlive) == 0)
|
|
continue;
|
|
const auto p1Index = static_cast<std::size_t>(p1);
|
|
startLoc2[p1Index] = startLoc[p1Index];
|
|
v8ToLoc(startLoc[p1Index], adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
int loc1 = adjacent[static_cast<std::size_t>(v8)];
|
|
for (int p2 = 0; p2 < nPl; ++p2)
|
|
if (((1 << p2) & gAlive) != 0 && startLoc[static_cast<std::size_t>(p2)] == loc1)
|
|
loc1 = -1;
|
|
for (int p2 = 0; p2 < p1; ++p2)
|
|
if (((1 << p2) & gAlive) != 0 && startLoc2[static_cast<std::size_t>(p2)] == loc1)
|
|
loc1 = -1;
|
|
int testDist;
|
|
if (loc1 < 0 || loc1 >= mapSize) {
|
|
testDist = -1;
|
|
} else {
|
|
const std::uint32_t terrain = terrainOf(loc1);
|
|
if (terrain == static_cast<std::uint32_t>(fOcean) || terrain == static_cast<std::uint32_t>(fShore) ||
|
|
terrain == static_cast<std::uint32_t>(fDesert) || terrain == static_cast<std::uint32_t>(fArctic) ||
|
|
terrain == static_cast<std::uint32_t>(fMountains) ||
|
|
(realMap[static_cast<std::size_t>(loc1)] & static_cast<std::uint32_t>(fDeadLands)) != 0)
|
|
testDist = -1;
|
|
else if (terrain == static_cast<std::uint32_t>(fGrass))
|
|
testDist = 2;
|
|
else if (Terrain[terrain].IrrEff > 0)
|
|
testDist = 1;
|
|
else
|
|
testDist = 0;
|
|
}
|
|
if (startLoc2[p1Index] == startLoc[p1Index] || testDist > bestDist) {
|
|
startLoc2[p1Index] = loc1;
|
|
bestDist = testDist;
|
|
n = 1;
|
|
} else if (testDist == bestDist) {
|
|
++n;
|
|
if (delphiRandom(n) == 0)
|
|
startLoc2[p1Index] = loc1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Database
|
|
} // namespace Cevo
|