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

+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