prepare gameserver implementation

This commit is contained in:
Geoffray Levasseur committed 2026-10-27 15:43:50 +01:00
1 parent b552ba0293
commit 20d64eebf9
4 files changed
+327

No files matched your search

+14
View File
@@ -172,6 +172,20 @@ Brain &Brains::addNew()
return *m_items.back();
}
Brain &Brains::add(std::unique_ptr<Brain> 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<int>(i);
return -1;
}
int Brains::kindCount(BrainType kind) const
{
int result = 0;
+6
View File
@@ -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> brain);
int count() const { return static_cast<int>(m_items.size()); }
Brain &at(int index) const { return *m_items[static_cast<std::size_t>(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<Brain *> getByKind(BrainType kind) const;
+34
View File
@@ -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<int, 8>;
using Vicinity21Loc = std::array<int, 28>;
@@ -97,6 +102,7 @@ extern std::array<int, nPl> serverVersion;
extern std::array<std::uint32_t, nPl> resourceMask;
extern std::array<bool, nPl> processClientData;
extern std::array<int, nPl> discovered; // number of tiles discovered
extern std::array<int, nPl> researched;
extern std::array<int, nPl> lastValidStat;
extern std::array<int, nPl> 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<int, nPl> startLoc;
extern std::array<int, nPl> startLoc2;
extern std::array<int, nPl> 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
+273
View File
@@ -33,6 +33,7 @@
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <vector>
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<int, nPl> serverVersion{};
std::array<std::uint32_t, nPl> resourceMask{};
std::array<bool, nPl> processClientData{};
std::array<int, nPl> discovered{};
std::array<int, nPl> researched{};
std::array<int, nPl> lastValidStat{};
std::array<int, nPl> nTech{};
int gAlive = 0;
@@ -58,6 +62,11 @@ int gTurn = 0;
int gTestFlags = 0;
std::array<int, nPl> founded{};
int invalidTreatyMap = 0;
int gInitialized = 0;
int peaceEnded = 0;
int saveMapCenterLoc = 0;
std::array<int, nPl> startLoc{};
std::array<int, nPl> startLoc2{};
std::array<TWonderInfo, nWonder> gWonder{};
std::array<int, nPl> grWallContinent{};
std::array<TShipInfo, nPl> 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<std::int64_t>(134775813) * static_cast<std::int64_t>(delphiRandSeed) + 1;
delphiRandSeed = static_cast<std::int32_t>(temp);
return static_cast<int>((static_cast<std::uint64_t>(static_cast<std::uint32_t>(max)) *
static_cast<std::uint64_t>(static_cast<std::uint32_t>(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<std::uint32_t>(fGrass)) {
if (y - 2 >= 1 && (realMap[loc - 2 * lx] & fTerrain) >= static_cast<std::uint32_t>(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<std::uint32_t>(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<std::uint32_t>(fGrass))
continent[loc] = continent[loc + (y & 1) - lx];
if (x - 1 >= 0 && (realMap[loc - 1] & fTerrain) >= static_cast<std::uint32_t>(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<std::uint32_t>(fGrass)) {
int wrong = -1;
if ((realMap[lx - 1 + lx * y] & fTerrain) >= static_cast<std::uint32_t>(fGrass))
wrong = continent[lx - 1 + lx * y];
if ((y & 1) == 0 && y - 1 >= 1 &&
(realMap[lx - 1 + lx * (y - 1)] & fTerrain) >= static_cast<std::uint32_t>(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<std::uint32_t>(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<int> startLoc0(static_cast<std::size_t>(lxmax) * lymax, 0);
int nStartLoc0 = 0;
int nPrefStartLoc0 = 0;
for (int loc1 = 0; loc1 < mapSize; ++loc1) {
if (realMap[loc1] & fPrefStartPos) {
startLoc0[static_cast<std::size_t>(nStartLoc0)] = startLoc0[static_cast<std::size_t>(nPrefStartLoc0)];
startLoc0[static_cast<std::size_t>(nPrefStartLoc0)] = loc1;
++nPrefStartLoc0;
++nStartLoc0;
realMap[loc1] &= ~static_cast<std::uint32_t>(fPrefStartPos);
} else if (realMap[loc1] & fStartPos) {
startLoc0[static_cast<std::size_t>(nStartLoc0)] = loc1;
++nStartLoc0;
realMap[loc1] &= ~static_cast<std::uint32_t>(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<std::size_t>(i)];
startLoc2[p1] = startLoc0[static_cast<std::size_t>(i)];
startLoc0[static_cast<std::size_t>(i)] = startLoc0[static_cast<std::size_t>(imax)];
startLoc0[static_cast<std::size_t>(imax)] = startLoc0[static_cast<std::size_t>(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<std::uint32_t>(fSpecial2 | fModern));
grWallContinent[p] = -1;
player.Map = reinterpret_cast<TTileList *>(new std::uint32_t[static_cast<std::size_t>(mapSize)]());
player.MapObservedLast = reinterpret_cast<TTileObservedLastList *>(new std::int16_t[static_cast<std::size_t>(mapSize)]);
std::memset(*player.MapObservedLast, 0xFF, static_cast<std::size_t>(mapSize) * 2);
player.Territory = reinterpret_cast<TOwnerList *>(new std::int8_t[static_cast<std::size_t>(mapSize)]);
std::memset(*player.Territory, 0xFF, static_cast<std::size_t>(mapSize));
player.Un = reinterpret_cast<TUnList *>(new TUn[nUmax]());
player.Model = reinterpret_cast<TModelList *>(new TModel[nMmax + 1]());
player.City = reinterpret_cast<TCityList *>(new TCity[nCmax]());
player.EnemyUn = reinterpret_cast<TEnemyUnList *>(new TUnitInfo[neumax]());
player.EnemyCity = reinterpret_cast<TEnemyCityList *>(new TCityInfo[necmax]());
player.EnemyModel = reinterpret_cast<TEnemyModelList *>(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<TEnemyReport *>(
::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<std::uint16_t>((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<std::uint16_t>(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<TUnitInfo *>(player.EnemyUn);
delete[] reinterpret_cast<TCityInfo *>(player.EnemyCity);
delete[] reinterpret_cast<TModelInfo *>(player.EnemyModel);
delete[] reinterpret_cast<TUn *>(player.Un);
delete[] reinterpret_cast<TCity *>(player.City);
delete[] reinterpret_cast<TModel *>(player.Model);
delete[] reinterpret_cast<std::int8_t *>(player.Territory);
delete[] reinterpret_cast<std::int16_t *>(player.MapObservedLast);
delete[] reinterpret_cast<std::uint32_t *>(player.Map);
}
gInitialized = 0;
}
} // namespace Database
} // namespace Cevo