diff --git a/game/GameServer.Lifecycle.cpp b/game/GameServer.Lifecycle.cpp index 8c77a31..2f3e45a 100644 --- a/game/GameServer.Lifecycle.cpp +++ b/game/GameServer.Lifecycle.cpp @@ -484,13 +484,17 @@ bool hasCityTileChanges() const std::uint32_t *previewMap(int landMass) { + constexpr std::int32_t PreviewRnd = 41601260; // randseed for preview map Database::lx = lxmax; Database::ly = lymax; Database::mapSize = Database::lx * Database::ly; - // TODO: needs LandMass/DelphiRandSeed/CreateElevation/CreateMap, which - // live in Database.pas's Map Editor section (not ported yet). - Q_UNUSED(landMass); - Q_UNUSED(previewElevation); + Database::landMass = landMass; + Database::delphiRandSeed = PreviewRnd; + if (!previewElevation) { + Database::createElevation(); + previewElevation = true; + } + Database::createMap(true); return Database::realMap.data(); } diff --git a/game/GameServer.Server.cpp b/game/GameServer.Server.cpp index ce15868..5deab7a 100644 --- a/game/GameServer.Server.cpp +++ b/game/GameServer.Server.cpp @@ -54,6 +54,15 @@ namespace Cevo { 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(data) : 0; +} +} // namespace + // Equivalent of GameServer.pas's Server() - the client-to-server command // 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; case sRandomMap: - // TODO: needs CreateElevation/CreateMap (full procedural map - // generator, not ported yet) - always reports unavailable for now. - result = eInvalid; + if (player == 0 && Database::mapGeneratorAvailable()) { + Database::createElevation(); + previewElevation = false; + Database::createMap(false); + Database::observeLevel.fill(0); + Database::discoverAll(player, Database::lObserveSuper); + } else { + result = eInvalid; + } break; case sFillMap: @@ -544,8 +559,8 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data) bool ok; if (command == sReload) { ok = Database::difficulty[0] == 0 && playerBrain[0]->kind != BrainType::NoTerm && - reinterpret_cast(data) >= 0 && - reinterpret_cast(data) < Database::gTurn; + dataValue(data) >= 0 && + dataValue(data) < Database::gTurn; for (int p1 = 1; p1 < nPl && ok; ++p1) if (playerBrain[p1] && playerBrain[p1]->kind == BrainType::Term) 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); break; case sReload: - loadGame(logFileName, logFileName, static_cast(reinterpret_cast(data)), false); + loadGame(logFileName, logFileName, dataValue(data), false); break; default: break; @@ -1071,12 +1086,12 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data) result = eNoPreq; } } - if (result == eOK && command >= sExecute) { - if (Database::mode == Database::moPlaying && subject == adMilitary) - Database::intServer(sIntSetDevModel, player, 0, &playerCtx.DevModel.Kind); - // save DevModel, because sctModel commands are not Logged - playerCtx.ResearchTech = subject; - } + } + if (result == eOK && command >= sExecute) { + if (Database::mode == Database::moPlaying && subject == adMilitary) + Database::intServer(sIntSetDevModel, player, 0, &playerCtx.DevModel.Kind); + // save DevModel, because sctModel commands are not Logged + playerCtx.ResearchTech = subject; } } else { result = eViolation; @@ -1202,7 +1217,7 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data) break; } TCity &city = (*playerCtx.City)[subject]; - const auto newProject = static_cast(reinterpret_cast(data) & ~cpAuto); + const auto newProject = static_cast(dataValue(data) & ~cpAuto); if (newProject == city.Project) { result = eNotChanged; break; @@ -1413,7 +1428,7 @@ int CEVO_ABI_CALL server(int command, int player, int subject, void *data) break; } TCity &city = (*playerCtx.City)[subject]; - const auto imp = static_cast(reinterpret_cast(data)); + const auto imp = dataValue(data); if (city.Built[imp] == 0) { result = eInvalid; } 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; break; } - const auto oldImp = static_cast(reinterpret_cast(data)); + const auto oldImp = dataValue(data); TCity &city = (*playerCtx.City)[subject]; if (oldImp < 0 || oldImp >= nImp || !(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) result = eInvalid; else - result = CityProcessing::setCityTiles(player, subject, static_cast(reinterpret_cast(data)), + result = CityProcessing::setCityTiles(player, subject, dataValue(data), command < sExecute); 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) { const int p1 = (command >> 4) & 0xF; - const int loc1 = static_cast(reinterpret_cast(data)); + const int loc1 = dataValue(data); const TModel &model = (*Database::rw[p1].Model)[subject]; if ((Database::occupant[loc1] >= 0 && p1 != Database::occupant[loc1]) || ((Database::realMap[loc1] & fCity) && (Database::realMap[loc1] >> 27) != static_cast(p1)) || diff --git a/game/include/Cevo/Database.h b/game/include/Cevo/Database.h index a2d7653..93d77ab 100644 --- a/game/include/Cevo/Database.h +++ b/game/include/Cevo/Database.h @@ -161,6 +161,11 @@ void v21ToLoc(int loc0, Vicinity21Loc &vicinityLoc); void calculatePrimitive(); 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 --- @@ -326,14 +331,11 @@ void setTestFlags(int p, int setFlags); // Allocates and initializes every RW[p] array (Un/City/Model/Map/Territory/ // EnemyUn/EnemyCity/EnemyModel/EnemyReport/...) and creates each alive // player's starting units + capital. Needs startLoc/startLoc2 already set -// (by predefinedStartPositions() or - not yet ported - StartPositions()). +// (by predefinedStartPositions() or startPositions()). void initGame(); // Predefined map path: FindContinents + PredefinedStartPositions + initGame(). void initMapGame(int human); -// TODO: needs CreateElevation/CreateMap/StartPositions/RarePositions (the -// full procedural map generator, itself several hundred lines) - not -// ported yet. Throws for now; only initMapGame()'s "load from file" path -// is supported. +// Random map path: CalculatePrimitive + CreateElevation + CreateMap + StartPositions + initGame(). void initRandomGame(); void findContinents(); void predefinedStartPositions(int human); diff --git a/game/localplayer/CityScreen.cpp b/game/localplayer/CityScreen.cpp index bf7a14d..6627bee 100644 --- a/game/localplayer/CityScreen.cpp +++ b/game/localplayer/CityScreen.cpp @@ -1282,7 +1282,7 @@ void CityDlg::mousePressEvent(QMouseEvent *event) y >= yZoomMap + 14 + 56 * (i / 3) && y < yZoomMap + 14 + 40 + 56 * (i / 3))) --i; if (i >= 0) { - const int iix = imix[static_cast(i)]; + int iix = imix[static_cast(i)]; if (iix >= 0) { if (shift) { Help::showNewContent(windowModeMakePersistent(windowMode()), Help::LinkCategory::Imp, iix); @@ -1300,7 +1300,7 @@ void CityDlg::mousePressEvent(QMouseEvent *event) dlg->showModal(); } else { const int sellResult = - ClientTools::server(sSellCityImprovement - sExecute, ClientTools::me, cix, nullptr); + ClientTools::server(sSellCityImprovement - sExecute, ClientTools::me, cix, &iix); if (sellResult < rExecuted) { dlg->messageText = phrase(sellResult == eOnlyOnce ? QStringLiteral("NOSELLAGAIN") @@ -1312,7 +1312,7 @@ void CityDlg::mousePressEvent(QMouseEvent *event) bool rebuild; int newIix = iix; if (ClientTools::server(sRebuildCityImprovement - sExecute, ClientTools::me, cix, - nullptr) < rExecuted) { + &iix) < rExecuted) { rebuild = false; dlg->messageText = phrase(QStringLiteral("SELL")).arg(impName(iix)); if (iix == imSpacePort) { diff --git a/game/src/Database.GameInitialization.cpp b/game/src/Database.GameInitialization.cpp index d32cc12..e811387 100644 --- a/game/src/Database.GameInitialization.cpp +++ b/game/src/Database.GameInitialization.cpp @@ -280,8 +280,12 @@ void initMapGame(int human) void initRandomGame() { - throw std::runtime_error( - "Random map generation is not supported yet (CreateElevation/CreateMap/StartPositions not ported)."); + delphiRandSeed = rnd; + calculatePrimitive(); + createElevation(); + createMap(false); + startPositions(); + initGame(); } void releaseGame() diff --git a/game/src/Database.MapCreation.cpp b/game/src/Database.MapCreation.cpp index dee6f47..2ab22c3 100644 --- a/game/src/Database.MapCreation.cpp +++ b/game/src/Database.MapCreation.cpp @@ -29,6 +29,16 @@ // --- Map Creation --- (see Cevo/Database.h) #include "Cevo/Database.h" +#include "Cevo/IPQ.h" + +#include "DatabaseInternal.h" + +#include +#include +#include +#include +#include +#include namespace Cevo { namespace Database { @@ -38,6 +48,26 @@ namespace { // 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 elevation; +std::array elCount; + +inline std::uint32_t terrainOf(int loc) +{ + return realMap[static_cast(loc)] & static_cast(fTerrain); +} + +inline bool isLand(int loc) +{ + return terrainOf(loc) >= static_cast(fGrass); +} + +// Equivalent of Protocol.pas's DelphiRandom: Extended overload. +long double delphiRandomFloat() +{ + return static_cast(delphiRandom(INT_MAX)) / INT_MAX; +} } // namespace void calculatePrimitive() @@ -69,5 +99,807 @@ 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( + result + std::sin(static_cast(f1[i]) * ((x * 2 + (y & 1)) * static_cast(sa[i]) + + y * 1.5 * static_cast(ca[i]))) * + static_cast(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(delphiRandomFloat()) * 2 * Pi; + sa[x] = static_cast(std::sin(v) / lx); + ca[x] = static_cast(std::cos(v) / ly); + f1[x] = static_cast(2 * Pi * std::exp(Detail * (x - 1))); + f2[x] = static_cast(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::nearbyint((v + 4) * 20)); + elevation[static_cast(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(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(rareByArea[x0][y0])] == + continent[static_cast(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(rareLoc[i])]; + tile = (tile & ~static_cast(fTerrain | fSpecial)) | static_cast(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(fMountains)) { + std::uint32_t &near = realMap[static_cast(loc1)]; + near = (near & ~static_cast(fTerrain)) | static_cast(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(rareLoc[iBest])] |= static_cast(rareType) << 25; + realMap[static_cast(rareLoc[jBest])] |= static_cast(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(i); + break; + } + p0 = p0 - p; + } + return result; + }; + + const auto runRiver = [&](int loc0) -> int { // runs river from start point loc0; return value: length + std::vector from(static_cast(lxmax) * lymax); + std::vector time(static_cast(lxmax) * lymax, -1); + IPQ q(mapSize); + q.put(loc0, 0); + int loc = 0; + int t = 0; + while (q.get(loc, t) && (realMap[static_cast(loc)] & static_cast(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(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(loc1)] < 0) { + int cost; + if ((realMap[static_cast(loc1)] & static_cast(fRiver)) == 0) { + cost = elevation[static_cast(loc1)] - elevation[static_cast(loc)]; + if (cost < 0) + cost = 0; + } else { + cost = 0; + } + if (q.put(loc1, t + (cost << 8) + 1)) + from[static_cast(loc1)] = loc; + } + } + } + const int loc1 = loc; + int result = 0; + while (loc != loc0) { + loc = from[static_cast(loc)]; + ++result; + } + if (result > 1 && (result >= MinRivLen || isLand(loc1))) { + loc = loc1; + while (loc != loc0) { + loc = from[static_cast(loc)]; + std::uint32_t &tile = realMap[static_cast(loc)]; + const std::uint32_t terrain = tile & static_cast(fTerrain); + if (terrain == static_cast(fHills) || terrain == static_cast(fMountains)) + tile = static_cast(fGrass | fRiver); + else if (terrain >= static_cast(fGrass)) + tile |= static_cast(fRiver); + } + } else { + result = 0; + } + return result; + }; + + const auto setTerrain = [](int loc, int terrain) { + std::uint32_t &tile = realMap[static_cast(loc)]; + tile = (tile & ~static_cast(fTerrain)) | static_cast(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(bMountains)]; + } + int count = plus; + plus = 0; + int bHills = bMountains; + while (plus < mapSize * landMass * ShHiHills / 10000) { + --bHills; + plus += elCount[static_cast(bHills)]; + } + count += plus; + int bLand = bHills; + while (count < mapSize * landMass / 100) { + --bLand; + count += elCount[static_cast(bLand)]; + } + + for (int loc0 = lx; loc0 < lx * (ly - 1); ++loc0) { + const int e = elevation[static_cast(loc0)]; + if (e >= bMountains) + realMap[static_cast(loc0)] = static_cast(fMountains); + else if (e >= bHills) + realMap[static_cast(loc0)] = static_cast(fHills); + else if (e >= bLand) + realMap[static_cast(loc0)] = static_cast(fGrass); + } + + Vicinity8Loc adjacent; + // remove one-tile islands + for (int loc0 = 0; loc0 < mapSize; ++loc0) { + if (realMap[static_cast(loc0)] < static_cast(fGrass)) + continue; + count = 0; + v8ToLoc(loc0, adjacent); + for (int v8 = 0; v8 < 8; ++v8) { + const int loc1 = adjacent[static_cast(v8)]; + if (loc1 < 0 || loc1 >= mapSize || !isLand(loc1) || terrainOf(loc1) == static_cast(fArctic)) + ++count; // count adjacent water + } + if (count == 8) + realMap[static_cast(loc0)] = static_cast(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(loc0)] == static_cast(fGrass) && + delphiRandom(100) < ShRandHills) + realMap[static_cast(loc0)] |= static_cast(fHills); + } + + // make terrain types coherent + std::vector copyFrom(static_cast(lxmax) * lymax); + for (int loc0 = 0; loc0 < mapSize; ++loc0) + copyFrom[static_cast(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(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(fGrass)) { + while (copyFrom[static_cast(loc0)] != loc0) + loc0 = copyFrom[static_cast(loc0)]; + while (copyFrom[static_cast(loc1)] != loc1) + loc1 = copyFrom[static_cast(loc1)]; + if (loc1 < loc0) + copyFrom[static_cast(loc0)] = loc1; + else + copyFrom[static_cast(loc1)] = loc0; + } + } + } + + for (int loc0 = 0; loc0 < mapSize; ++loc0) { + if (terrainOf(loc0) == static_cast(fGrass) && copyFrom[static_cast(loc0)] == loc0) + setTerrain(loc0, static_cast(rndLow(loc0 / lx))); + } + + for (int loc0 = 0; loc0 < mapSize; ++loc0) { + if (terrainOf(loc0) == static_cast(fGrass)) { + int loc1 = loc0; + while (copyFrom[static_cast(loc1)] != loc1) + loc1 = copyFrom[static_cast(loc1)]; + setTerrain(loc0, static_cast(terrainOf(loc1))); + } + } + + for (int loc0 = 0; loc0 < mapSize; ++loc0) { // change grassland to swamp + if (terrainOf(loc0) == static_cast(fGrass) && delphiRandom(100) < ShSwamp) + setTerrain(loc0, fSwamp); + } + + for (int loc0 = 0; loc0 < mapSize; ++loc0) { // change desert to prairie 1 + if (terrainOf(loc0) != static_cast(fDesert)) + continue; + if ((realMap[static_cast(loc0)] & static_cast(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(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(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(x)] = static_cast(fArctic); + if (realMap[static_cast(x + lx)] >= static_cast(fGrass)) + setTerrain(x + lx, fTundra); + if (realMap[static_cast(x + lx * (ly - 2))] >= static_cast(fGrass)) + setTerrain(x + lx * (ly - 2), fTundra); + realMap[static_cast(x + lx * (ly - 1))] = static_cast(fArctic); + } + + for (int loc0 = 0; loc0 < mapSize; ++loc0) // define special terrain tiles + realMap[static_cast(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(fGrass) || terrain == static_cast(fPrairie) || + terrain == static_cast(fTundra) || terrain == static_cast(fSwamp) || + terrain == static_cast(fForest); + }; + const auto isGoodTile = [&](int loc) { + const int xLoc = loc % lx; + const int yLoc = loc / lx; + if ((realMap[static_cast(loc)] & static_cast(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 ccount(static_cast(lxmax) * lymax, 0); + for (int loc = 0; loc < mapSize; ++loc) { + const std::uint32_t terrain = terrainOf(loc); + const std::size_t cont = static_cast(continent[static_cast(loc)]); + if (terrain == static_cast(fGrass)) { + if (actualSpecialTile(loc) == 1) + ccount[cont] = static_cast(ccount[cont] + 3); + else + ccount[cont] = static_cast(ccount[cont] + 2); + } else if (terrain == static_cast(fPrairie) || terrain == static_cast(fSwamp) || + terrain == static_cast(fForest) || terrain == static_cast(fHills)) { + ccount[cont] = static_cast(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(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(loc)] > 0) { // search biggest continents + int p1 = nAlive + 1; + while (p1 > 1 && ccount[static_cast(loc)] > ccount[static_cast(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(ccount[static_cast(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(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(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(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(startLoc0[p1])]; + tile = (tile & ~static_cast(fTerrain | fSpecial)) | static_cast(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(v21)]; + if (loc1 >= 0 && loc1 < mapSize && isGoodTile(loc1)) { + if (terrainOf(loc1) == static_cast(fGrass)) + ++cntGoodGrass; + else + ++cntGood; + } + } + for (int v21 = 1; v21 <= 26; ++v21) { + if (v21 == CityOwnTile) + continue; + const int loc1 = radius[static_cast(v21)]; + if (loc1 < 0 || loc1 >= mapSize || (realMap[static_cast(loc1)] & static_cast(fDeadLands)) != 0) + continue; + std::uint32_t &tile = realMap[static_cast(loc1)]; + if (isGoodTile(loc1) && delphiRandom(cntGood) < minGood - cntGoodGrass + 1) { + tile = (tile & ~static_cast(fTerrain | fSpecial)) | static_cast(fGrass); + tile |= actualSpecialTile(loc1) << 5; + } else if (terrainOf(loc1) == static_cast(fDesert)) { + tile = (tile & ~static_cast(fTerrain)) | static_cast(fPrairie); + } else if ((terrainOf(loc1) == static_cast(fPrairie) || + terrainOf(loc1) == static_cast(fTundra) || + terrainOf(loc1) == static_cast(fSwamp)) && + delphiRandom(2) == 0) { + tile = (tile & ~static_cast(fTerrain)) | static_cast(fForest); + } + } + + // first irrigation + int nIrrLoc = 0; + for (int v21 = 1; v21 <= 26; ++v21) { + if (v21 == CityOwnTile) + continue; + const int loc1 = radius[static_cast(v21)]; + if (loc1 >= 0 && loc1 < mapSize && + (realMap[static_cast(loc1)] & static_cast(fTerrain | fSpecial)) == + static_cast(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(irrLoc[j])] |= static_cast(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(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(p1); + startLoc2[p1Index] = startLoc[p1Index]; + v8ToLoc(startLoc[p1Index], adjacent); + for (int v8 = 0; v8 < 8; ++v8) { + int loc1 = adjacent[static_cast(v8)]; + for (int p2 = 0; p2 < nPl; ++p2) + if (((1 << p2) & gAlive) != 0 && startLoc[static_cast(p2)] == loc1) + loc1 = -1; + for (int p2 = 0; p2 < p1; ++p2) + if (((1 << p2) & gAlive) != 0 && startLoc2[static_cast(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(fOcean) || terrain == static_cast(fShore) || + terrain == static_cast(fDesert) || terrain == static_cast(fArctic) || + terrain == static_cast(fMountains) || + (realMap[static_cast(loc1)] & static_cast(fDeadLands)) != 0) + testDist = -1; + else if (terrain == static_cast(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 diff --git a/game/src/Database.TechRelated.cpp b/game/src/Database.TechRelated.cpp index 5fdbc04..50c2583 100644 --- a/game/src/Database.TechRelated.cpp +++ b/game/src/Database.TechRelated.cpp @@ -52,7 +52,8 @@ void freeSlaves() 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(TechFormula_M[diff] * (nTech + 4) * std::exp((nTech + 4) / TechFormula_D[diff])); if (c0 >= 0x10000000) return 0x10000000; return static_cast(c0); // truncates toward zero, matching Pascal's Trunc() diff --git a/game/src/Database.cpp b/game/src/Database.cpp index 87d14bc..438c061 100644 --- a/game/src/Database.cpp +++ b/game/src/Database.cpp @@ -133,10 +133,10 @@ void intServer(int command, int player, int subject, const void *data) } else if (command == sIntCancelTreaty) { cancelTreaty(player, subject); } else if (command == sIntDiscoverZOC) { - discover9(static_cast(reinterpret_cast(data)), player, lObserveUnhidden, true, false); + discover9(*static_cast(data), player, lObserveUnhidden, true, false); } else if (command == sIntBuyMaterial) { TCity &city = (*rw[player].City)[subject]; - const auto cost = static_cast(reinterpret_cast(data)); + const int cost = *static_cast(data); rw[player].Money -= cost; if (gWonder[woMich].EffectiveOwner == player && (city.Project & cpImp)) city.Prod += cost / 2; @@ -159,11 +159,22 @@ void intServer(int command, int player, int subject, const void *data) break; } } + } else if (command == sIntSetDevModel) { + if (mode < moPlaying) + std::memcpy(&rw[player].DevModel.Kind, data, static_cast((sIntSetDevModel & 0xF) * 4)); + } else if (command == sIntSetModelStatus) { + if (processClientData[static_cast(player)]) + (*rw[player].Model)[subject].Status = *static_cast(data); + } else if (command == sIntSetUnitStatus) { + if (processClientData[static_cast(player)]) + (*rw[player].Un)[subject].Status = *static_cast(data); + } else if (command == sIntSetCityStatus) { + if (processClientData[static_cast(player)]) + (*rw[player].City)[subject].Status = *static_cast(data); + } else if (command == sIntSetECityStatus) { + if (processClientData[static_cast(player)]) + (*rw[player].EnemyCity)[subject].Status = *static_cast(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 diff --git a/game/src/DatabaseInternal.h b/game/src/DatabaseInternal.h index 8a5a8d6..4840f7e 100644 --- a/game/src/DatabaseInternal.h +++ b/game/src/DatabaseInternal.h @@ -65,5 +65,8 @@ void freeSlaves(); void changeTerritory(int loc, int p); void expandTerritory(int originLoc); +// Defined in Database.MapCreation.cpp; used by initRandomGame(). +void startPositions(); + } // namespace Database } // namespace Cevo