prepare gameserver implementation

This commit is contained in:
Geoffray Levasseur committed 2026-10-27 16:22:49 +01:00
1 parent 20d64eebf9
commit 192898fb62
2 files changed
+576 -3

No files matched your search

+528 -3
View File
@@ -42,6 +42,7 @@ int lx = 0;
int ly = 0;
int mapSize = 0;
int rnd = 0;
int landMass = 0;
std::int32_t delphiRandSeed = 0;
GameMode mode = moLoading_Fast;
CmdList cl;
@@ -80,6 +81,10 @@ std::array<std::int8_t, lxmax * lymax> zocMap{};
std::array<std::int32_t, lxmax * lymax> usedByCity{};
std::array<std::uint32_t, lxmax * lymax> observeLevel{};
// Equivalent of Database.pas's `TerritoryCount: array[0..nPl]` - public since
// GameServer::generateStat() needs read access (index nPl means "no owner").
std::array<int, nPl + 1> territoryCount{};
namespace {
// Equivalent of Database.pas's private (implementation-section) `Primitive`
// global - not exposed in the header, matching the original's own
@@ -90,9 +95,6 @@ int primitive = 0;
// (number of units built, used to assign unique per-player unit IDs).
std::array<int, nPl> unBuilt{};
// Equivalent of Database.pas's private `TerritoryCount: array[0..nPl]`.
std::array<int, nPl + 1> territoryCount{};
// Equivalent of Database.pas's private `BorderChanges` (sIntExpandTerritory
// and $F == 8 packed Cardinal words, 4 bits per tile in the CountryRadius
// area around a city).
@@ -554,6 +556,79 @@ void checkExpiration(int wonder)
}
}
void seeTech(int p, int ad)
{
rw[p].Tech[ad] = tsSeen;
++researched[p];
}
namespace {
void tellAboutKeyTech(int p, int source, int ad)
{
for (int i = 1; i <= 3; ++i)
if (ad == AgePreq[i])
for (int p1 = 0; p1 < nPl; ++p1)
if (p1 != p && ((gAlive | gWatching) & (1 << p1)))
rw[p1].EnemyReport[p]->Tech[ad] = static_cast<std::int8_t>(source);
}
} // namespace
void discoverTech(int p, int ad)
{
TPlayerContext &player = rw[p];
if (IsFutureTech(ad)) {
if (player.Tech[ad] < tsApplicable)
player.Tech[ad] = 1;
else
++player.Tech[ad];
if (ad != futResearchTechnology)
nTech[p] += 2;
researched[p] += 8;
return;
}
if (player.Tech[ad] == tsSeen) {
++nTech[p];
++researched[p];
} else {
nTech[p] += 2;
researched[p] += 2;
}
player.Tech[ad] = tsResearched;
tellAboutKeyTech(p, tsResearched, ad);
checkSpecialModels(p, ad);
if (ad == adScience)
resourceMask[p] |= fSpecial2;
if (ad == adMassProduction)
resourceMask[p] |= fModern;
for (int i = 0; i < nWonder; ++i) { // check whether wonders expired
if (gWonder[i].EffectiveOwner != gWonder[woEiffel].EffectiveOwner && Imp[i].Expiration == ad) {
gWonder[i].EffectiveOwner = -1;
if (i == woPyramids)
freeSlaves();
}
}
}
void enableDevModel(int p)
{
TPlayerContext &player = rw[p];
if (player.nModel >= nMmax)
return;
TModel *model = *player.Model;
model[player.nModel] = player.DevModel;
TModel &newModel = model[player.nModel];
newModel.Status = 0;
newModel.SavedStatus = 0;
newModel.IntroTurn = static_cast<std::uint16_t>(gTurn);
newModel.Built = 0;
newModel.Lost = 0;
newModel.ID = static_cast<std::uint16_t>((p << 12) + player.nModel);
++player.nModel;
++researched[p];
}
void createUnit(int p, int mix)
{
TPlayerContext &player = rw[p];
@@ -674,6 +749,54 @@ void removeDomainUnits(int d, int p, int loc)
removeUnit(p, uix);
}
void compactLists(int p)
{
TPlayerContext &player = rw[p];
TUn *un = *player.Un;
TCity *city = *player.City;
TCityInfo *enemyCity = *player.EnemyCity;
// compact unit list
int uix = 0;
while (uix < player.nUn) {
if (un[uix].Loc < 0) {
--player.nUn;
un[uix] = un[player.nUn]; // replace removed unit by last
if (un[uix].TroopLoad > 0 || un[uix].AirLoad > 0)
for (int uix1 = 0; uix1 < player.nUn; ++uix1)
if (un[uix1].Master == player.nUn) // index of last unit changes
un[uix1].Master = uix;
} else {
++uix;
}
}
// compact city list
int cix = 0;
while (cix < player.nCity) {
if (city[cix].Loc < 0) {
--player.nCity;
city[cix] = city[player.nCity]; // replace city by last
for (int uix1 = 0; uix1 < player.nUn; ++uix1)
if (un[uix1].Home == player.nCity) // index of last city changes
un[uix1].Home = cix;
} else {
++cix;
}
}
// compact enemy city list
cix = 0;
while (cix < player.nEnemyCity) {
if (enemyCity[cix].Loc < 0) {
--player.nEnemyCity;
enemyCity[cix] = enemyCity[player.nEnemyCity]; // replace city by last
} else {
++cix;
}
}
}
int specialTile(int loc, int terrType, int lx)
{
if (terrType == fOcean)
@@ -833,6 +956,72 @@ void completeJob(int p, int loc, int job)
}
}
bool checkShore(int loc)
{
const std::uint32_t oldTile = realMap[loc];
if ((oldTile & fTerrain) >= static_cast<std::uint32_t>(fGrass))
return false;
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(fOcean);
Vicinity21Loc radius;
v21ToLoc(loc, radius);
for (int v21 = 1; v21 <= 26; ++v21) {
const int loc1 = radius[static_cast<std::size_t>(v21)];
if (loc1 >= 0 && loc1 < mapSize && (realMap[loc1] & fTerrain) >= static_cast<std::uint32_t>(fGrass) &&
(realMap[loc1] & fTerrain) != static_cast<std::uint32_t>(fArctic))
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(fShore);
}
return ((realMap[loc] ^ oldTile) & fTerrain) != 0;
}
void editTile(int loc, std::uint32_t newTile)
{
if (newTile & fDeadLands)
newTile = (newTile & (fDeadLands | fModern | fRiver)) | fDesert;
switch (newTile & fTerrain) {
case fOcean:
case fShore:
newTile &= (fTerrain | fSpecial);
break;
case fMountains:
case fArctic:
newTile &= ~static_cast<std::uint32_t>(fRiver);
break;
default:
break;
}
const TerrainInfo &terrain = Terrain[newTile & fTerrain];
if (terrain.ClearTerrain >= 0 || terrain.AfforestTerrain >= 0 || terrain.TransTerrain >= 0)
newTile |= fSpecial; // only automatic special resources for transformable tiles
if (newTile & fRR)
newTile &= ~static_cast<std::uint32_t>(fRoad);
if (((terrTypeCanalableMask >> (newTile & fTerrain)) & 1) == 0)
newTile &= ~static_cast<std::uint32_t>(fCanal);
if (Terrain[newTile & fTerrain].IrrEff == 0) {
newTile &= ~static_cast<std::uint32_t>(fPrefStartPos | fStartPos);
if ((newTile & fTerImp) == tiIrrigation || (newTile & fTerImp) == tiFarm)
newTile &= ~static_cast<std::uint32_t>(fTerImp);
}
if (Terrain[newTile & fTerrain].MineEff == 0 && (newTile & fTerImp) == tiMine)
newTile &= ~static_cast<std::uint32_t>(fTerImp);
realMap[loc] = newTile;
if ((newTile & fSpecial) == fSpecial) // standard special resource distribution
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fSpecial)) | (actualSpecialTile(loc) << 5);
// automatic shore tiles
Vicinity21Loc radius;
v21ToLoc(loc, radius);
for (int v21 = 1; v21 <= 26; ++v21) {
const int loc1 = radius[static_cast<std::size_t>(v21)];
if (loc1 >= 0 && loc1 < mapSize) {
if (checkShore(loc1))
realMap[loc1] = (realMap[loc1] & ~static_cast<std::uint32_t>(fSpecial)) | (actualSpecialTile(loc1) << 5);
realMap[loc1] |= (0xFu << 27);
(*rw[0].Map)[loc1] = (realMap[loc1] & 0x07FFFFFFu) | fObserved;
}
}
}
void introduceEnemy(int p1, int p2)
{
rw[p1].Treaty[p2] = trNone;
@@ -881,6 +1070,178 @@ void giveMilReport(int p, int pAbout)
++report.UnCount[un[uix].mix];
}
bool copyCivilReport(int pSender, int pTarget, int pAbout)
{
bool result = false;
if (rw[pTarget].Treaty[pAbout] == trNoContact)
introduceEnemy(pTarget, pAbout);
TEnemyReport &rSender = *rw[pSender].EnemyReport[pAbout];
TEnemyReport &rTarget = *rw[pTarget].EnemyReport[pAbout];
if (rSender.TurnOfCivilReport > rTarget.TurnOfCivilReport) { // only if newer than current information
rTarget.TurnOfCivilReport = rSender.TurnOfCivilReport;
std::memcpy(rTarget.Treaty, rSender.Treaty, sizeof(rTarget.Treaty));
rTarget.Government = rSender.Government;
rTarget.Money = rSender.Money;
rTarget.ResearchTech = rSender.ResearchTech;
rTarget.ResearchDone = rSender.ResearchDone;
result = true;
}
for (int i = 0; i < nAdv; ++i)
if (rTarget.Tech[i] < rSender.Tech[i]) {
rTarget.Tech[i] = rSender.Tech[i];
result = true;
}
return result;
}
bool copyMilReport(int pSender, int pTarget, int pAbout)
{
bool result = false;
if (rw[pTarget].Treaty[pAbout] == trNoContact)
introduceEnemy(pTarget, pAbout);
TEnemyReport &rSender = *rw[pSender].EnemyReport[pAbout];
TEnemyReport &rTarget = *rw[pTarget].EnemyReport[pAbout];
if (rSender.TurnOfMilReport > rTarget.TurnOfMilReport) { // only if newer than current information
rTarget.TurnOfMilReport = rSender.TurnOfMilReport;
rTarget.nModelCounted = rSender.nModelCounted;
std::memcpy(rTarget.UnCount, rSender.UnCount,
sizeof(std::uint16_t) * static_cast<std::size_t>(rSender.nModelCounted));
for (int mix = 0; mix < rTarget.nModelCounted; ++mix)
tellAboutModel(pTarget, pAbout, mix);
result = true;
}
return result;
}
void copyModel(int pSender, int pTarget, int mix)
{
// only if target doesn't already have a model like this
bool ok = rw[pTarget].nModel < nMmax;
TModelInfo miSender{};
MakeModelInfo(pSender, mix, (*rw[pSender].Model)[mix], miSender);
for (int i = 0; i < rw[pTarget].nModel && ok; ++i) {
TModelInfo miTarget{};
MakeModelInfo(pTarget, i, (*rw[pTarget].Model)[i], miTarget);
if (IsSameModel(miSender, miTarget))
ok = false;
}
if (ok) {
TModel &newModel = (*rw[pTarget].Model)[rw[pTarget].nModel];
newModel = (*rw[pSender].Model)[mix];
newModel.IntroTurn = static_cast<std::uint16_t>(gTurn);
if (newModel.Kind == mkSelfDeveloped)
newModel.Kind = mkEnemyDeveloped;
newModel.Status = 0;
newModel.SavedStatus = 0;
newModel.Built = 0;
newModel.Lost = 0;
++rw[pTarget].nModel;
++researched[pTarget];
tellAboutModel(pSender, pTarget, rw[pTarget].nModel - 1);
}
}
void copyMap(int pSender, int pTarget)
{
TPlayerContext &sender = rw[pSender];
TPlayerContext &target = rw[pTarget];
const std::int16_t *senderObserved = *sender.MapObservedLast;
std::int16_t *targetObserved = *target.MapObservedLast;
const std::uint32_t *senderMap = *sender.Map;
std::uint32_t *targetMap = *target.Map;
const std::int8_t *senderTerritory = *sender.Territory;
std::int8_t *targetTerritory = *target.Territory;
TCityInfo *senderEnemyCity = *sender.EnemyCity;
TCityInfo *targetEnemyCity = *target.EnemyCity;
const TCity *senderCity = *sender.City;
for (int loc = 0; loc < mapSize; ++loc) {
if (senderObserved[loc] <= targetObserved[loc])
continue;
std::uint32_t tile = senderMap[loc];
if (tile & fCity) {
int i = 0;
while (i < target.nEnemyCity && targetEnemyCity[i].Loc != loc)
++i;
if (i == target.nEnemyCity) {
++target.nEnemyCity;
assert(target.nEnemyCity < necmax);
targetEnemyCity[i].Status = 0;
targetEnemyCity[i].SavedStatus = 0;
}
if (tile & fOwned) { // city owned by sender -- create new info
int cix = sender.nCity - 1;
while (cix >= 0 && senderCity[cix].Loc != loc)
--cix;
makeCityInfo(pSender, cix, targetEnemyCity[i]);
} else { // city not owned by sender -- copy old info
int cix = sender.nEnemyCity - 1;
while (cix >= 0 && senderEnemyCity[cix].Loc != loc)
--cix;
targetEnemyCity[i] = senderEnemyCity[cix];
}
} else if (targetMap[loc] & fCity) { // remove enemycity
for (int cix = 0; cix < target.nEnemyCity; ++cix)
if (targetEnemyCity[cix].Loc == loc)
targetEnemyCity[cix].Loc = -1;
}
tile &= (~static_cast<std::uint32_t>(fSpecial | fModern) | resourceMask[pTarget]);
tile |= (targetMap[loc] & fModern);
if ((tile & fTerrain) == (targetMap[loc] & fTerrain))
tile |= (targetMap[loc] & fSpecial);
if ((targetMap[loc] & fTerrain) == static_cast<std::uint32_t>(fUNKNOWN))
++discovered[pTarget];
targetMap[loc] = (targetMap[loc] & fInEnemyZoC) // always preserve this flag!
| (tile & ~static_cast<std::uint32_t>(fUnit | fHiddenUnit | fStealthUnit | fObserved |
fSpiedOut | fOwned | fInEnemyZoC | fOwnZoCUnit |
fPeace | fGrWall));
// (the original re-assigns Territory unconditionally right after an
// equivalent if-guarded assignment - the guard was a no-op once its
// only other effect, a BorderHelper update, had been commented out)
targetTerritory[loc] = senderTerritory[loc];
targetObserved[loc] = senderObserved[loc];
}
}
std::uint32_t doSpyMission(int p, int pCity, int cix, int mission)
{
std::uint32_t result = 0;
switch (mission) {
case smSabotageProd:
(*rw[pCity].City)[cix].Flags |= chProductionSabotaged;
break;
case smStealMap:
copyMap(pCity, p);
recalcPeaceMap(p);
break;
case smStealCivilReport:
if (rw[p].Treaty[pCity] == trNoContact)
introduceEnemy(p, pCity);
giveCivilReport(p, pCity);
break;
case smStealMilReport:
if (rw[p].Treaty[pCity] == trNoContact)
introduceEnemy(p, pCity);
giveMilReport(p, pCity);
break;
case smStealForeignReports:
for (int p1 = 0; p1 < nPl; ++p1) {
if (p1 != p && p1 != pCity && rw[pCity].EnemyReport[p1]) {
if (rw[pCity].EnemyReport[p1]->TurnOfCivilReport >= 0 && copyCivilReport(pCity, p, p1))
result |= (1u << (2 * p1));
if (rw[pCity].EnemyReport[p1]->TurnOfMilReport >= 0 && copyMilReport(pCity, p, p1))
result |= (2u << (2 * p1));
}
}
break;
default:
break;
}
return result;
}
void intServer(int command, int player, int subject, const void *data)
{
if (mode == moPlaying)
@@ -1301,6 +1662,31 @@ void updateUnitMap(int loc, bool cityChange)
}
}
void recalcMapZoC(int p)
{
if (mode == moLoading_Fast)
return;
maskD(*rw[p].Map, mapSize, ~static_cast<std::uint32_t>(fInEnemyZoC));
const int observeMask = 3 << (2 * p);
std::uint32_t *map = *rw[p].Map;
for (int loc = 0; loc < mapSize; ++loc) {
if (zocMap[loc] > 0 && (observeLevel[loc] & observeMask)) {
const int p1 = occupant[loc];
assert(p1 != nPl);
if (p1 != p && rw[p].Treaty[p1] < trAlliance) {
// this non-allied enemy ZoC unit is known to this player -- set flags!
Vicinity8Loc adjacent;
v8ToLoc(loc, adjacent);
for (int v8 = 0; v8 < 8; ++v8) {
const int loc1 = adjacent[static_cast<std::size_t>(v8)];
if (loc1 >= 0 && loc1 < mapSize)
map[loc1] |= fInEnemyZoC;
}
}
}
}
}
void recalcPeaceMap(int p)
{
if (mode != moPlaying)
@@ -1769,5 +2155,144 @@ void releaseGame()
gInitialized = 0;
}
namespace {
void stealCity(int p, int cix, bool saveUnits)
{
TCity &city = (*rw[p].City)[cix];
for (int i = 0; i < nWonder; ++i) {
if (city.Built[i] == 1) {
gWonder[i].EffectiveOwner = -1;
if (i == woPyramids)
freeSlaves();
if (i == woEiffel) // deactivate expired wonders
for (int j = 0; j < nWonder; ++j)
if (gWonder[j].EffectiveOwner == p)
checkExpiration(j);
}
}
for (int i = nWonder; i < nImp; ++i) {
if (Imp[i].Kind != ikCommon && city.Built[i] > 0) { // destroy national projects
rw[p].NatBuilt[i] = 0;
if (i == imGrWall)
grWallContinent[p] = -1;
}
}
TUn *un = *rw[p].Un;
for (int uix1 = 0; uix1 < rw[p].nUn; ++uix1) {
if (un[uix1].Loc >= 0 && un[uix1].Home == cix) {
if (saveUnits) { // support units by nearest other city
int nearest = -1;
for (int cix1 = 0; cix1 < rw[p].nCity; ++cix1) {
const TCity &c1 = (*rw[p].City)[cix1];
if (cix1 != cix && c1.Loc >= 0 &&
(nearest < 0 || distance(c1.Loc, un[uix1].Loc) <
distance((*rw[p].City)[nearest].Loc, un[uix1].Loc)))
nearest = cix1;
}
un[uix1].Home = nearest;
} else {
removeUnit(p, uix1); // destroy supported units
}
}
}
}
} // namespace
void destroyCity(int p, int cix, bool saveUnits)
{
stealCity(p, cix, saveUnits);
TCity &city = (*rw[p].City)[cix];
for (int i = 0; i < nWonder; ++i)
if (city.Built[i] > 0)
gWonder[i].CityID = WonderDestroyed;
Vicinity21Loc radius;
v21ToLoc(city.Loc, radius);
for (int v21 = 1; v21 <= 26; ++v21)
if ((1u << v21) & city.Tiles)
usedByCity[static_cast<std::size_t>(radius[static_cast<std::size_t>(v21)])] = -1;
realMap[city.Loc] &= ~static_cast<std::uint32_t>(fCity);
city.Loc = -1;
}
void changeCityOwner(int pOld, int cixOld, int pNew)
{
++rw[pNew].nCity;
(*rw[pNew].City)[rw[pNew].nCity - 1] = (*rw[pOld].City)[cixOld];
stealCity(pOld, cixOld, false);
(*rw[pOld].City)[cixOld].Loc = -1;
TCity &city = (*rw[pNew].City)[rw[pNew].nCity - 1];
city.Food = 0;
city.Project = cpImp + imTrGoods;
city.Prod = 0;
city.Project0 = city.Project;
city.Prod0 = 0;
city.Status = 0;
city.SavedStatus = 0;
city.N1 = 0;
// check for siege
Vicinity21Loc radius;
v21ToLoc(city.Loc, radius);
for (int v21 = 1; v21 <= 26; ++v21) {
if ((city.Tiles & (1u << v21) & ~(1u << CityOwnTile)) != 0) {
const int loc1 = radius[static_cast<std::size_t>(v21)];
assert(loc1 >= 0 && loc1 < mapSize && usedByCity[static_cast<std::size_t>(loc1)] == city.Loc);
if (zocMap[loc1] > 0 && occupant[loc1] != pNew && rw[pNew].Treaty[occupant[loc1]] < trAlliance) {
// tile can't remain exploited
city.Tiles &= ~(1u << v21);
usedByCity[static_cast<std::size_t>(loc1)] = -1;
}
// don't check for siege by peace territory here, because territory
// might not be up to date -- done in turn beginning anyway
}
}
city.Built[imTownHall] = 0;
city.Built[imCourt] = 0;
for (int i = nWonder; i < nImp; ++i)
if (Imp[i].Kind != ikCommon)
city.Built[i] = 0; // destroy national projects
for (int i = 0; i < nWonder; ++i) {
if (city.Built[i] == 1) { // new wonder owner!
gWonder[i].EffectiveOwner = pNew;
if (i == woEiffel) { // reactivate expired wonders
for (int j = 0; j < nWonder; ++j)
if (Imp[j].Expiration >= 0)
for (int cix1 = 0; cix1 < rw[pNew].nCity; ++cix1)
if ((*rw[pNew].City)[cix1].Built[j] == 1)
gWonder[j].EffectiveOwner = pNew;
} else {
checkExpiration(i);
}
switch (i) {
case woLighthouse:
checkSpecialModels(pNew, preLighthouse);
break;
case woLeo:
checkSpecialModels(pNew, preLeo);
break;
case woPyramids:
checkSpecialModels(pNew, preBuilder);
break;
default:
break;
}
}
}
// remove city from enemy cities - not done by Discover, because fCity still set!
int cix1 = rw[pNew].nEnemyCity - 1;
TCityInfo *enemyCity = *rw[pNew].EnemyCity;
while (cix1 >= 0 && enemyCity[cix1].Loc != city.Loc)
--cix1;
assert(cix1 >= 0);
enemyCity[cix1].Loc = -1;
changeTerritory(city.Loc, pNew);
}
} // namespace Database
} // namespace Cevo