From 20d64eebf93af500a879e9fc19df026ac4d2e8fe Mon Sep 17 00:00:00 2001 From: Geoffray Levasseur Date: Tue, 27 Oct 2026 15:43:50 +0100 Subject: [PATCH] prepare gameserver implementation --- game/Brain.cpp | 14 ++ game/Brain.h | 6 + game/include/Cevo/Database.h | 34 +++++ game/src/Database.cpp | 273 +++++++++++++++++++++++++++++++++++ 4 files changed, 327 insertions(+) diff --git a/game/Brain.cpp b/game/Brain.cpp index abe0daa..c599076 100644 --- a/game/Brain.cpp +++ b/game/Brain.cpp @@ -172,6 +172,20 @@ Brain &Brains::addNew() return *m_items.back(); } +Brain &Brains::add(std::unique_ptr brain) +{ + m_items.push_back(std::move(brain)); + return *m_items.back(); +} + +int Brains::indexOf(const Brain *brain) const +{ + for (std::size_t i = 0; i < m_items.size(); ++i) + if (m_items[i].get() == brain) + return static_cast(i); + return -1; +} + int Brains::kindCount(BrainType kind) const { int result = 0; diff --git a/game/Brain.h b/game/Brain.h index c641644..257fe31 100644 --- a/game/Brain.h +++ b/game/Brain.h @@ -89,10 +89,16 @@ private: class Brains { public: Brain &addNew(); + // Takes ownership of an already-constructed Brain (matches + // GameServer.pas's Init(): a candidate TBrain gets built and validated + // before deciding whether to Brains.Add() it or discard it). + Brain &add(std::unique_ptr brain); int count() const { return static_cast(m_items.size()); } Brain &at(int index) const { return *m_items[static_cast(index)]; } Brain &operator[](int index) const { return at(index); } void clear() { m_items.clear(); } + // -1 if `brain` is null or not in this list (matches TList.IndexOf). + int indexOf(const Brain *brain) const; int kindCount(BrainType kind) const; std::vector getByKind(BrainType kind) const; diff --git a/game/include/Cevo/Database.h b/game/include/Cevo/Database.h index c5bff63..694f7e7 100644 --- a/game/include/Cevo/Database.h +++ b/game/include/Cevo/Database.h @@ -70,6 +70,8 @@ constexpr int CityOwnTile = 13; constexpr int nemmax = 1024; constexpr int necmax = 1024; constexpr int neumax = 4096; +constexpr int nStartUn = 1; +constexpr int StartUn[nStartUn] = { 0 }; // mix of start units // Database.pas's `TerrType_Canalable` set, as a bitmask indexed by fTerrain. constexpr std::uint32_t terrTypeCanalableMask = (1u << fGrass) | (1u << fDesert) | (1u << fPrairie) | @@ -86,6 +88,9 @@ extern CmdList cl; extern int lx; extern int ly; extern int mapSize; // = lx * ly +extern int rnd; // world map randseed +extern std::int32_t delphiRandSeed; +int delphiRandom(int max); // Delphi/Borland-compatible LCG, result in [0, max) using Vicinity8Loc = std::array; using Vicinity21Loc = std::array; @@ -97,6 +102,7 @@ extern std::array serverVersion; extern std::array resourceMask; extern std::array processClientData; extern std::array discovered; // number of tiles discovered +extern std::array researched; extern std::array lastValidStat; extern std::array nTech; // number of known techs extern int gAlive; // players alive; bitset of 1 shl p @@ -104,6 +110,11 @@ extern int gWatching; extern int gAI; extern int gTurn; // current turn extern int gTestFlags; +extern int gInitialized; // bitset of players whose RW[p] arrays are allocated +extern int peaceEnded; // side effect out-param of cancelTreaty(): bitset of players +extern int saveMapCenterLoc; +extern std::array startLoc; +extern std::array startLoc2; extern std::array founded; // number of cities founded per player // Debug-only cross-check (Database.pas's `{$IFOPT O-}InvalidTreatyMap`): // bitset of players whose territory must not be considered mid-command. @@ -237,6 +248,29 @@ void introduceEnemy(int p1, int p2); int techCost(int p); void giveCivilReport(int p, int pAbout); void giveMilReport(int p, int pAbout); +void showPrice(int pSender, int pTarget, std::uint32_t price); +bool payPrice(int pSender, int pTarget, std::uint32_t price, bool execute); +// Side effect: sets peaceEnded to the bitset of players with whom a peace +// treaty was just canceled (matches the original's `PeaceEnded` global). +void cancelTreaty(int p, int pWith, bool decreaseCredibility = true); + +// --- Game Initialization --- + +// 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()). +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. +void initRandomGame(); +void findContinents(); +void predefinedStartPositions(int human); +void releaseGame(); } // namespace Database } // namespace Cevo diff --git a/game/src/Database.cpp b/game/src/Database.cpp index 3a328b5..fd484e7 100644 --- a/game/src/Database.cpp +++ b/game/src/Database.cpp @@ -33,6 +33,7 @@ #include #include #include +#include namespace Cevo { namespace Database { @@ -40,6 +41,8 @@ namespace Database { int lx = 0; int ly = 0; int mapSize = 0; +int rnd = 0; +std::int32_t delphiRandSeed = 0; GameMode mode = moLoading_Fast; CmdList cl; @@ -49,6 +52,7 @@ std::array serverVersion{}; std::array resourceMask{}; std::array processClientData{}; std::array discovered{}; +std::array researched{}; std::array lastValidStat{}; std::array nTech{}; int gAlive = 0; @@ -58,6 +62,11 @@ int gTurn = 0; int gTestFlags = 0; std::array founded{}; int invalidTreatyMap = 0; +int gInitialized = 0; +int peaceEnded = 0; +int saveMapCenterLoc = 0; +std::array startLoc{}; +std::array startLoc2{}; std::array gWonder{}; std::array grWallContinent{}; std::array gShip{}; @@ -1496,5 +1505,269 @@ void logCheckBorders(int p, int cix, int playerLosingCity) intServer(sIntExpandTerritory, p, cix, borderChanges.data()); } +int delphiRandom(int max) +{ + const std::int64_t temp = static_cast(134775813) * static_cast(delphiRandSeed) + 1; + delphiRandSeed = static_cast(temp); + return static_cast((static_cast(static_cast(max)) * + static_cast(static_cast(delphiRandSeed))) >> + 32); +} + +void findContinents() +{ + auto replaceCont = [](int a, int b, int stop) { + if (a < b) + std::swap(a, b); + if (a > b) + for (int i = a; i <= stop; ++i) + if (continent[i] == a) + continent[i] = b; + }; + + for (int y = 1; y <= ly - 2; ++y) { + for (int x = 0; x < lx; ++x) { + const int loc = x + lx * y; + continent[loc] = -1; + if ((realMap[loc] & fTerrain) >= static_cast(fGrass)) { + if (y - 2 >= 1 && (realMap[loc - 2 * lx] & fTerrain) >= static_cast(fGrass)) + continent[loc] = continent[loc - 2 * lx]; + if ((x - 1 + (y & 1)) >= 0 && y - 1 >= 1 && + (realMap[loc - 1 + (y & 1) - lx] & fTerrain) >= static_cast(fGrass)) + continent[loc] = continent[loc - 1 + (y & 1) - lx]; + if ((x + (y & 1)) < lx && y - 1 >= 1 && + (realMap[loc + (y & 1) - lx] & fTerrain) >= static_cast(fGrass)) + continent[loc] = continent[loc + (y & 1) - lx]; + if (x - 1 >= 0 && (realMap[loc - 1] & fTerrain) >= static_cast(fGrass)) { + if (continent[loc] == -1) + continent[loc] = continent[loc - 1]; + else + replaceCont(continent[loc - 1], continent[loc], loc); + } + if (continent[loc] == -1) + continent[loc] = loc; + } + } + } + + // connect continents due to round earth + for (int y = 1; y <= ly - 2; ++y) { + if ((realMap[lx * y] & fTerrain) >= static_cast(fGrass)) { + int wrong = -1; + if ((realMap[lx - 1 + lx * y] & fTerrain) >= static_cast(fGrass)) + wrong = continent[lx - 1 + lx * y]; + if ((y & 1) == 0 && y - 1 >= 1 && + (realMap[lx - 1 + lx * (y - 1)] & fTerrain) >= static_cast(fGrass)) + wrong = continent[lx - 1 + lx * (y - 1)]; + if ((y & 1) == 0 && y + 1 < ly - 1 && + (realMap[lx - 1 + lx * (y + 1)] & fTerrain) >= static_cast(fGrass)) + wrong = continent[lx - 1 + lx * (y + 1)]; + if (wrong >= 0) + replaceCont(wrong, continent[lx * y], mapSize - 1); + } + } +} + +void predefinedStartPositions(int human) +{ + int nAlive = 0; + for (int p1 = 0; p1 < nPl; ++p1) + if ((1 << p1) & gAlive) + ++nAlive; + if (nAlive == 0) + return; + + std::vector startLoc0(static_cast(lxmax) * lymax, 0); + + int nStartLoc0 = 0; + int nPrefStartLoc0 = 0; + for (int loc1 = 0; loc1 < mapSize; ++loc1) { + if (realMap[loc1] & fPrefStartPos) { + startLoc0[static_cast(nStartLoc0)] = startLoc0[static_cast(nPrefStartLoc0)]; + startLoc0[static_cast(nPrefStartLoc0)] = loc1; + ++nPrefStartLoc0; + ++nStartLoc0; + realMap[loc1] &= ~static_cast(fPrefStartPos); + } else if (realMap[loc1] & fStartPos) { + startLoc0[static_cast(nStartLoc0)] = loc1; + ++nStartLoc0; + realMap[loc1] &= ~static_cast(fStartPos); + } + } + assert(nStartLoc0 >= nAlive); + + startLoc[0] = 0; + for (bool isHuman : { true, false }) { + for (int p1 = 0; p1 < nPl; ++p1) { + if (((1 << p1) & gAlive) && ((((1 << p1) & human) != 0) == isHuman)) { + --nStartLoc0; + int imax = nStartLoc0; + if (nPrefStartLoc0 > 0) { + --nPrefStartLoc0; + imax = nPrefStartLoc0; + } + const int i = delphiRandom(imax + 1); + startLoc[p1] = startLoc0[static_cast(i)]; + startLoc2[p1] = startLoc0[static_cast(i)]; + startLoc0[static_cast(i)] = startLoc0[static_cast(imax)]; + startLoc0[static_cast(imax)] = startLoc0[static_cast(nStartLoc0)]; + } + } + } + saveMapCenterLoc = startLoc[0]; +} + +void initGame() +{ + occupant.fill(-1); + zocMap.fill(0); + observeLevel.fill(0); + usedByCity.fill(-1); + gTestFlags = 0; + gInitialized = gAlive | gWatching; + + for (int p = 0; p < nPl; ++p) { + if (((1 << p) & gInitialized) == 0) + continue; + TPlayerContext &player = rw[p]; + researched[p] = 0; + discovered[p] = 0; + territoryCount[p] = 0; + nTech[p] = 0; + resourceMask[p] = (difficulty[p] == 0) ? 0xFFFFFFFFu : (0xFFFFFFFFu & ~static_cast(fSpecial2 | fModern)); + grWallContinent[p] = -1; + + player.Map = reinterpret_cast(new std::uint32_t[static_cast(mapSize)]()); + player.MapObservedLast = reinterpret_cast(new std::int16_t[static_cast(mapSize)]); + std::memset(*player.MapObservedLast, 0xFF, static_cast(mapSize) * 2); + player.Territory = reinterpret_cast(new std::int8_t[static_cast(mapSize)]); + std::memset(*player.Territory, 0xFF, static_cast(mapSize)); + player.Un = reinterpret_cast(new TUn[nUmax]()); + player.Model = reinterpret_cast(new TModel[nMmax + 1]()); + player.City = reinterpret_cast(new TCity[nCmax]()); + player.EnemyUn = reinterpret_cast(new TUnitInfo[neumax]()); + player.EnemyCity = reinterpret_cast(new TCityInfo[necmax]()); + player.EnemyModel = reinterpret_cast(new TModelInfo[nemmax]()); + + for (int p1 = 0; p1 < nPl; ++p1) { + if ((1 << p1) & gInitialized) { + std::memset(rwemix[p][p1].data(), 0xFF, rwemix[p][p1].size() * sizeof(std::int16_t)); + std::memset(destroyed[p][p1].data(), 0, destroyed[p][p1].size() * sizeof(std::int16_t)); + } + player.Attitude[p1] = atNeutral; + player.Treaty[p1] = trNoContact; + player.LastCancelTreaty[p1] = -CancelTreatyTurns - 1; + player.EvaStart[p1] = -PeaceEvaTurns - 1; + player.Tribute[p1] = 0; + player.TributePaid[p1] = 0; + if (p1 != p && ((1 << p1) & gAlive)) { + // Truncated allocation (matches the original's own GetMem + // size: TEnemyReport's UnCount[] field is oversized via the + // same "open array via overlarge static type" trick used + // throughout this port - only nMmax slots are real). + player.EnemyReport[p1] = static_cast( + ::operator new(sizeof(TEnemyReport) - sizeof(std::uint16_t) * (INFIN + 1 - nMmax))); + std::memset(player.EnemyReport[p1]->Tech, tsNA, nAdv); + player.EnemyReport[p1]->TurnOfContact = -1; + player.EnemyReport[p1]->TurnOfCivilReport = -1; + player.EnemyReport[p1]->TurnOfMilReport = -1; + player.EnemyReport[p1]->Attitude = atNeutral; + player.EnemyReport[p1]->Government = gDespotism; + if (((1 << p) & gAlive) == 0) + player.Treaty[p1] = trNone; // supervisor + } else { + player.EnemyReport[p1] = nullptr; + } + } + player.TestFlags = gTestFlags; + player.Credibility = InitialCredibility; + player.MaxCredibility = 100; + player.nUn = 0; + player.nModel = 0; + player.nCity = 0; + player.nEnemyUn = 0; + player.nEnemyCity = 0; + player.nEnemyModel = 0; + std::uint32_t *map = *player.Map; + for (int loc1 = 0; loc1 < mapSize; ++loc1) + map[loc1] = fUNKNOWN; + std::memset(player.Tech, tsNA, nAdv); + std::memset(player.NatBuilt, 0, sizeof(player.NatBuilt)); + } + + // create initial models and units + for (int p = 0; p < nPl; ++p) { + if (((1 << p) & gAlive) == 0) + continue; + TPlayerContext &player = rw[p]; + TModel *model = *player.Model; + player.nModel = 0; + for (int i = 0; i < nSpecialModel; ++i) { + if (SpecialModelPreq[i] == preNone) { + model[player.nModel] = SpecialModel[i]; + model[player.nModel].Status = 0; + model[player.nModel].IntroTurn = 0; + model[player.nModel].Built = 0; + model[player.nModel].Lost = 0; + model[player.nModel].ID = static_cast((p << 12) + player.nModel); + setModelFlags(model[player.nModel]); + ++player.nModel; + } + } + player.nUn = 0; + unBuilt[p] = 0; + TUn *un = *player.Un; + for (int uix = 0; uix < nStartUn; ++uix) { + createUnit(p, StartUn[uix]); + --model[StartUn[uix]].Built; + un[uix].Loc = startLoc2[p]; + placeUnit(p, uix); + } + foundCity(p, startLoc[p]); + founded[p] = 1; + TCity &city = (*player.City)[0]; + city.ID = static_cast(p << 12); + city.Flags = chFounded; + } + + territoryCount[nPl] = mapSize; +} + +void initMapGame(int human) +{ + delphiRandSeed = rnd; + findContinents(); + predefinedStartPositions(human); + initGame(); +} + +void initRandomGame() +{ + throw std::runtime_error( + "Random map generation is not supported yet (CreateElevation/CreateMap/StartPositions not ported)."); +} + +void releaseGame() +{ + for (int p1 = 0; p1 < nPl; ++p1) { + if (((1 << p1) & gInitialized) == 0) + continue; + TPlayerContext &player = rw[p1]; + for (int p2 = 0; p2 < nPl; ++p2) + if (player.EnemyReport[p2]) + ::operator delete(player.EnemyReport[p2]); + delete[] reinterpret_cast(player.EnemyUn); + delete[] reinterpret_cast(player.EnemyCity); + delete[] reinterpret_cast(player.EnemyModel); + delete[] reinterpret_cast(player.Un); + delete[] reinterpret_cast(player.City); + delete[] reinterpret_cast(player.Model); + delete[] reinterpret_cast(player.Territory); + delete[] reinterpret_cast(player.MapObservedLast); + delete[] reinterpret_cast(player.Map); + } + gInitialized = 0; +} + } // namespace Database } // namespace Cevo