implemented map generator
This commit is contained in:
1 parent
69700f8b65
commit
f32cce0e56
9 files changed
+904
-32
No files matched your search
@@ -484,13 +484,17 @@ bool hasCityTileChanges()
|
|||||||
|
|
||||||
const std::uint32_t *previewMap(int landMass)
|
const std::uint32_t *previewMap(int landMass)
|
||||||
{
|
{
|
||||||
|
constexpr std::int32_t PreviewRnd = 41601260; // randseed for preview map
|
||||||
Database::lx = lxmax;
|
Database::lx = lxmax;
|
||||||
Database::ly = lymax;
|
Database::ly = lymax;
|
||||||
Database::mapSize = Database::lx * Database::ly;
|
Database::mapSize = Database::lx * Database::ly;
|
||||||
// TODO: needs LandMass/DelphiRandSeed/CreateElevation/CreateMap, which
|
Database::landMass = landMass;
|
||||||
// live in Database.pas's Map Editor section (not ported yet).
|
Database::delphiRandSeed = PreviewRnd;
|
||||||
Q_UNUSED(landMass);
|
if (!previewElevation) {
|
||||||
Q_UNUSED(previewElevation);
|
Database::createElevation();
|
||||||
|
previewElevation = true;
|
||||||
|
}
|
||||||
|
Database::createMap(true);
|
||||||
return Database::realMap.data();
|
return Database::realMap.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+26
-11
@@ -54,6 +54,15 @@
|
|||||||
namespace Cevo {
|
namespace Cevo {
|
||||||
namespace GameServer {
|
namespace GameServer {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// The original's `Integer(Data)` on its untyped `var Data` parameter: the value stored at the address
|
||||||
|
// the client passed (not the address itself).
|
||||||
|
int dataValue(const void *data)
|
||||||
|
{
|
||||||
|
return data ? *static_cast<const int *>(data) : 0;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
// Equivalent of GameServer.pas's Server() - the client-to-server command
|
// Equivalent of GameServer.pas's Server() - the client-to-server command
|
||||||
// dispatcher. FULLY PORTED: Info Request, Map Editor (except sRandomMap,
|
// dispatcher. FULLY PORTED: Info Request, Map Editor (except sRandomMap,
|
||||||
@@ -424,9 +433,15 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case sRandomMap:
|
case sRandomMap:
|
||||||
// TODO: needs CreateElevation/CreateMap (full procedural map
|
if (player == 0 && Database::mapGeneratorAvailable()) {
|
||||||
// generator, not ported yet) - always reports unavailable for now.
|
Database::createElevation();
|
||||||
|
previewElevation = false;
|
||||||
|
Database::createMap(false);
|
||||||
|
Database::observeLevel.fill(0);
|
||||||
|
Database::discoverAll(player, Database::lObserveSuper);
|
||||||
|
} else {
|
||||||
result = eInvalid;
|
result = eInvalid;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case sFillMap:
|
case sFillMap:
|
||||||
@@ -544,8 +559,8 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data)
|
|||||||
bool ok;
|
bool ok;
|
||||||
if (command == sReload) {
|
if (command == sReload) {
|
||||||
ok = Database::difficulty[0] == 0 && playerBrain[0]->kind != BrainType::NoTerm &&
|
ok = Database::difficulty[0] == 0 && playerBrain[0]->kind != BrainType::NoTerm &&
|
||||||
reinterpret_cast<std::intptr_t>(data) >= 0 &&
|
dataValue(data) >= 0 &&
|
||||||
reinterpret_cast<std::intptr_t>(data) < Database::gTurn;
|
dataValue(data) < Database::gTurn;
|
||||||
for (int p1 = 1; p1 < nPl && ok; ++p1)
|
for (int p1 = 1; p1 < nPl && ok; ++p1)
|
||||||
if (playerBrain[p1] && playerBrain[p1]->kind == BrainType::Term)
|
if (playerBrain[p1] && playerBrain[p1]->kind == BrainType::Term)
|
||||||
ok = false;
|
ok = false;
|
||||||
@@ -582,7 +597,7 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data)
|
|||||||
startNewGame(logFileName, mapFileName, Database::lx, Database::ly, Database::landMass, maxTurn);
|
startNewGame(logFileName, mapFileName, Database::lx, Database::ly, Database::landMass, maxTurn);
|
||||||
break;
|
break;
|
||||||
case sReload:
|
case sReload:
|
||||||
loadGame(logFileName, logFileName, static_cast<int>(reinterpret_cast<std::intptr_t>(data)), false);
|
loadGame(logFileName, logFileName, dataValue(data), false);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -1071,13 +1086,13 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data)
|
|||||||
result = eNoPreq;
|
result = eNoPreq;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (result == eOK && command >= sExecute) {
|
if (result == eOK && command >= sExecute) {
|
||||||
if (Database::mode == Database::moPlaying && subject == adMilitary)
|
if (Database::mode == Database::moPlaying && subject == adMilitary)
|
||||||
Database::intServer(sIntSetDevModel, player, 0, &playerCtx.DevModel.Kind);
|
Database::intServer(sIntSetDevModel, player, 0, &playerCtx.DevModel.Kind);
|
||||||
// save DevModel, because sctModel commands are not Logged
|
// save DevModel, because sctModel commands are not Logged
|
||||||
playerCtx.ResearchTech = subject;
|
playerCtx.ResearchTech = subject;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
result = eViolation;
|
result = eViolation;
|
||||||
}
|
}
|
||||||
@@ -1202,7 +1217,7 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
TCity &city = (*playerCtx.City)[subject];
|
TCity &city = (*playerCtx.City)[subject];
|
||||||
const auto newProject = static_cast<std::int16_t>(reinterpret_cast<std::intptr_t>(data) & ~cpAuto);
|
const auto newProject = static_cast<std::int16_t>(dataValue(data) & ~cpAuto);
|
||||||
if (newProject == city.Project) {
|
if (newProject == city.Project) {
|
||||||
result = eNotChanged;
|
result = eNotChanged;
|
||||||
break;
|
break;
|
||||||
@@ -1413,7 +1428,7 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
TCity &city = (*playerCtx.City)[subject];
|
TCity &city = (*playerCtx.City)[subject];
|
||||||
const auto imp = static_cast<int>(reinterpret_cast<std::intptr_t>(data));
|
const auto imp = dataValue(data);
|
||||||
if (city.Built[imp] == 0) {
|
if (city.Built[imp] == 0) {
|
||||||
result = eInvalid;
|
result = eInvalid;
|
||||||
} else if (playerCtx.Government == gAnarchy || (city.Flags & chCaptured)) {
|
} else if (playerCtx.Government == gAnarchy || (city.Flags & chCaptured)) {
|
||||||
@@ -1442,7 +1457,7 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data)
|
|||||||
result = eInvalid;
|
result = eInvalid;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const auto oldImp = static_cast<int>(reinterpret_cast<std::intptr_t>(data));
|
const auto oldImp = dataValue(data);
|
||||||
TCity &city = (*playerCtx.City)[subject];
|
TCity &city = (*playerCtx.City)[subject];
|
||||||
if (oldImp < 0 || oldImp >= nImp ||
|
if (oldImp < 0 || oldImp >= nImp ||
|
||||||
!(Imp[oldImp].Kind == ikCommon || Imp[oldImp].Kind == ikNatLocal || Imp[oldImp].Kind == ikNatGlobal)) {
|
!(Imp[oldImp].Kind == ikCommon || Imp[oldImp].Kind == ikNatLocal || Imp[oldImp].Kind == ikNatGlobal)) {
|
||||||
@@ -1481,7 +1496,7 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data)
|
|||||||
if (subject >= playerCtx.nCity || (*playerCtx.City)[subject].Loc < 0)
|
if (subject >= playerCtx.nCity || (*playerCtx.City)[subject].Loc < 0)
|
||||||
result = eInvalid;
|
result = eInvalid;
|
||||||
else
|
else
|
||||||
result = CityProcessing::setCityTiles(player, subject, static_cast<int>(reinterpret_cast<std::intptr_t>(data)),
|
result = CityProcessing::setCityTiles(player, subject, dataValue(data),
|
||||||
command < sExecute);
|
command < sExecute);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1626,7 +1641,7 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data)
|
|||||||
}
|
}
|
||||||
} else if ((command & ~0xF0) == sCreateUnit || (command & ~0xF0) == sCreateUnit - sExecute) {
|
} else if ((command & ~0xF0) == sCreateUnit || (command & ~0xF0) == sCreateUnit - sExecute) {
|
||||||
const int p1 = (command >> 4) & 0xF;
|
const int p1 = (command >> 4) & 0xF;
|
||||||
const int loc1 = static_cast<int>(reinterpret_cast<std::intptr_t>(data));
|
const int loc1 = dataValue(data);
|
||||||
const TModel &model = (*Database::rw[p1].Model)[subject];
|
const TModel &model = (*Database::rw[p1].Model)[subject];
|
||||||
if ((Database::occupant[loc1] >= 0 && p1 != Database::occupant[loc1]) ||
|
if ((Database::occupant[loc1] >= 0 && p1 != Database::occupant[loc1]) ||
|
||||||
((Database::realMap[loc1] & fCity) && (Database::realMap[loc1] >> 27) != static_cast<std::uint32_t>(p1)) ||
|
((Database::realMap[loc1] & fCity) && (Database::realMap[loc1] >> 27) != static_cast<std::uint32_t>(p1)) ||
|
||||||
|
|||||||
@@ -161,6 +161,11 @@ void v21ToLoc(int loc0, Vicinity21Loc &vicinityLoc);
|
|||||||
|
|
||||||
void calculatePrimitive();
|
void calculatePrimitive();
|
||||||
bool mapGeneratorAvailable();
|
bool mapGeneratorAvailable();
|
||||||
|
// Fills the elevation table from the current random seed; createMap() then
|
||||||
|
// builds realMap from it. With preview=true, rivers, continents and rare
|
||||||
|
// resources are skipped (used for the start screen's mini map).
|
||||||
|
void createElevation();
|
||||||
|
void createMap(bool preview);
|
||||||
|
|
||||||
// --- Tech Related Functions ---
|
// --- Tech Related Functions ---
|
||||||
|
|
||||||
@@ -326,14 +331,11 @@ void setTestFlags(int p, int setFlags);
|
|||||||
// Allocates and initializes every RW[p] array (Un/City/Model/Map/Territory/
|
// Allocates and initializes every RW[p] array (Un/City/Model/Map/Territory/
|
||||||
// EnemyUn/EnemyCity/EnemyModel/EnemyReport/...) and creates each alive
|
// EnemyUn/EnemyCity/EnemyModel/EnemyReport/...) and creates each alive
|
||||||
// player's starting units + capital. Needs startLoc/startLoc2 already set
|
// player's starting units + capital. Needs startLoc/startLoc2 already set
|
||||||
// (by predefinedStartPositions() or - not yet ported - StartPositions()).
|
// (by predefinedStartPositions() or startPositions()).
|
||||||
void initGame();
|
void initGame();
|
||||||
// Predefined map path: FindContinents + PredefinedStartPositions + initGame().
|
// Predefined map path: FindContinents + PredefinedStartPositions + initGame().
|
||||||
void initMapGame(int human);
|
void initMapGame(int human);
|
||||||
// TODO: needs CreateElevation/CreateMap/StartPositions/RarePositions (the
|
// Random map path: CalculatePrimitive + CreateElevation + CreateMap + StartPositions + initGame().
|
||||||
// full procedural map generator, itself several hundred lines) - not
|
|
||||||
// ported yet. Throws for now; only initMapGame()'s "load from file" path
|
|
||||||
// is supported.
|
|
||||||
void initRandomGame();
|
void initRandomGame();
|
||||||
void findContinents();
|
void findContinents();
|
||||||
void predefinedStartPositions(int human);
|
void predefinedStartPositions(int human);
|
||||||
|
|||||||
@@ -1282,7 +1282,7 @@ void CityDlg::mousePressEvent(QMouseEvent *event)
|
|||||||
y >= yZoomMap + 14 + 56 * (i / 3) && y < yZoomMap + 14 + 40 + 56 * (i / 3)))
|
y >= yZoomMap + 14 + 56 * (i / 3) && y < yZoomMap + 14 + 40 + 56 * (i / 3)))
|
||||||
--i;
|
--i;
|
||||||
if (i >= 0) {
|
if (i >= 0) {
|
||||||
const int iix = imix[static_cast<std::size_t>(i)];
|
int iix = imix[static_cast<std::size_t>(i)];
|
||||||
if (iix >= 0) {
|
if (iix >= 0) {
|
||||||
if (shift) {
|
if (shift) {
|
||||||
Help::showNewContent(windowModeMakePersistent(windowMode()), Help::LinkCategory::Imp, iix);
|
Help::showNewContent(windowModeMakePersistent(windowMode()), Help::LinkCategory::Imp, iix);
|
||||||
@@ -1300,7 +1300,7 @@ void CityDlg::mousePressEvent(QMouseEvent *event)
|
|||||||
dlg->showModal();
|
dlg->showModal();
|
||||||
} else {
|
} else {
|
||||||
const int sellResult =
|
const int sellResult =
|
||||||
ClientTools::server(sSellCityImprovement - sExecute, ClientTools::me, cix, nullptr);
|
ClientTools::server(sSellCityImprovement - sExecute, ClientTools::me, cix, &iix);
|
||||||
if (sellResult < rExecuted) {
|
if (sellResult < rExecuted) {
|
||||||
dlg->messageText =
|
dlg->messageText =
|
||||||
phrase(sellResult == eOnlyOnce ? QStringLiteral("NOSELLAGAIN")
|
phrase(sellResult == eOnlyOnce ? QStringLiteral("NOSELLAGAIN")
|
||||||
@@ -1312,7 +1312,7 @@ void CityDlg::mousePressEvent(QMouseEvent *event)
|
|||||||
bool rebuild;
|
bool rebuild;
|
||||||
int newIix = iix;
|
int newIix = iix;
|
||||||
if (ClientTools::server(sRebuildCityImprovement - sExecute, ClientTools::me, cix,
|
if (ClientTools::server(sRebuildCityImprovement - sExecute, ClientTools::me, cix,
|
||||||
nullptr) < rExecuted) {
|
&iix) < rExecuted) {
|
||||||
rebuild = false;
|
rebuild = false;
|
||||||
dlg->messageText = phrase(QStringLiteral("SELL")).arg(impName(iix));
|
dlg->messageText = phrase(QStringLiteral("SELL")).arg(impName(iix));
|
||||||
if (iix == imSpacePort) {
|
if (iix == imSpacePort) {
|
||||||
|
|||||||
@@ -280,8 +280,12 @@ void initMapGame(int human)
|
|||||||
|
|
||||||
void initRandomGame()
|
void initRandomGame()
|
||||||
{
|
{
|
||||||
throw std::runtime_error(
|
delphiRandSeed = rnd;
|
||||||
"Random map generation is not supported yet (CreateElevation/CreateMap/StartPositions not ported).");
|
calculatePrimitive();
|
||||||
|
createElevation();
|
||||||
|
createMap(false);
|
||||||
|
startPositions();
|
||||||
|
initGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
void releaseGame()
|
void releaseGame()
|
||||||
|
|||||||
@@ -29,6 +29,16 @@
|
|||||||
// --- Map Creation --- (see Cevo/Database.h)
|
// --- Map Creation --- (see Cevo/Database.h)
|
||||||
|
|
||||||
#include "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 Cevo {
|
||||||
namespace Database {
|
namespace Database {
|
||||||
@@ -38,6 +48,26 @@ namespace {
|
|||||||
// global - not exposed in the header, matching the original's own
|
// global - not exposed in the header, matching the original's own
|
||||||
// interface/implementation visibility split.
|
// interface/implementation visibility split.
|
||||||
int primitive = 0;
|
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
|
} // namespace
|
||||||
|
|
||||||
void calculatePrimitive()
|
void calculatePrimitive()
|
||||||
@@ -69,5 +99,807 @@ bool mapGeneratorAvailable()
|
|||||||
return primitive > 0 && lx >= 20 && ly >= 40;
|
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 Database
|
||||||
} // namespace Cevo
|
} // namespace Cevo
|
||||||
@@ -52,7 +52,8 @@ void freeSlaves()
|
|||||||
|
|
||||||
int techBaseCost(int nTech, int diff)
|
int techBaseCost(int nTech, int diff)
|
||||||
{
|
{
|
||||||
const double c0 = TechFormula_M[diff] * (nTech + 4) * std::exp((nTech + 4) / TechFormula_D[diff]);
|
// c0 is a Single in the original: the rounding to 24 bits matters for the truncation below
|
||||||
|
const float c0 = static_cast<float>(TechFormula_M[diff] * (nTech + 4) * std::exp((nTech + 4) / TechFormula_D[diff]));
|
||||||
if (c0 >= 0x10000000)
|
if (c0 >= 0x10000000)
|
||||||
return 0x10000000;
|
return 0x10000000;
|
||||||
return static_cast<int>(c0); // truncates toward zero, matching Pascal's Trunc()
|
return static_cast<int>(c0); // truncates toward zero, matching Pascal's Trunc()
|
||||||
|
|||||||
+17
-6
@@ -133,10 +133,10 @@ void intServer(int command, int player, int subject, const void *data)
|
|||||||
} else if (command == sIntCancelTreaty) {
|
} else if (command == sIntCancelTreaty) {
|
||||||
cancelTreaty(player, subject);
|
cancelTreaty(player, subject);
|
||||||
} else if (command == sIntDiscoverZOC) {
|
} else if (command == sIntDiscoverZOC) {
|
||||||
discover9(static_cast<int>(reinterpret_cast<std::intptr_t>(data)), player, lObserveUnhidden, true, false);
|
discover9(*static_cast<const int *>(data), player, lObserveUnhidden, true, false);
|
||||||
} else if (command == sIntBuyMaterial) {
|
} else if (command == sIntBuyMaterial) {
|
||||||
TCity &city = (*rw[player].City)[subject];
|
TCity &city = (*rw[player].City)[subject];
|
||||||
const auto cost = static_cast<int>(reinterpret_cast<std::intptr_t>(data));
|
const int cost = *static_cast<const int *>(data);
|
||||||
rw[player].Money -= cost;
|
rw[player].Money -= cost;
|
||||||
if (gWonder[woMich].EffectiveOwner == player && (city.Project & cpImp))
|
if (gWonder[woMich].EffectiveOwner == player && (city.Project & cpImp))
|
||||||
city.Prod += cost / 2;
|
city.Prod += cost / 2;
|
||||||
@@ -159,11 +159,22 @@ void intServer(int command, int player, int subject, const void *data)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (command == sIntSetDevModel) {
|
||||||
|
if (mode < moPlaying)
|
||||||
|
std::memcpy(&rw[player].DevModel.Kind, data, static_cast<std::size_t>((sIntSetDevModel & 0xF) * 4));
|
||||||
|
} else if (command == sIntSetModelStatus) {
|
||||||
|
if (processClientData[static_cast<std::size_t>(player)])
|
||||||
|
(*rw[player].Model)[subject].Status = *static_cast<const int *>(data);
|
||||||
|
} else if (command == sIntSetUnitStatus) {
|
||||||
|
if (processClientData[static_cast<std::size_t>(player)])
|
||||||
|
(*rw[player].Un)[subject].Status = *static_cast<const int *>(data);
|
||||||
|
} else if (command == sIntSetCityStatus) {
|
||||||
|
if (processClientData[static_cast<std::size_t>(player)])
|
||||||
|
(*rw[player].City)[subject].Status = *static_cast<const int *>(data);
|
||||||
|
} else if (command == sIntSetECityStatus) {
|
||||||
|
if (processClientData[static_cast<std::size_t>(player)])
|
||||||
|
(*rw[player].EnemyCity)[subject].Status = *static_cast<const int *>(data);
|
||||||
}
|
}
|
||||||
// Other sIntXxx cases (sIntSetDevModel/sIntSetXxxStatus) are NOT
|
|
||||||
// implemented yet - each is only needed by LoadGame's command replay of
|
|
||||||
// a client's own already-applied local changes, not by Server()'s own
|
|
||||||
// command dispatch - lower priority than the above. See repo memory.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Database
|
} // namespace Database
|
||||||
|
|||||||
@@ -65,5 +65,8 @@ void freeSlaves();
|
|||||||
void changeTerritory(int loc, int p);
|
void changeTerritory(int loc, int p);
|
||||||
void expandTerritory(int originLoc);
|
void expandTerritory(int originLoc);
|
||||||
|
|
||||||
|
// Defined in Database.MapCreation.cpp; used by initRandomGame().
|
||||||
|
void startPositions();
|
||||||
|
|
||||||
} // namespace Database
|
} // namespace Database
|
||||||
} // namespace Cevo
|
} // namespace Cevo
|
||||||
Reference in new issue
Block a user