unit processing

This commit is contained in:
Geoffray Levasseur committed 2026-10-27 13:59:20 +01:00
1 parent 2226f0bce4
commit 90bff517a1
4 files changed
+1717 -5

No files matched your search

+399 -5
View File
@@ -30,6 +30,7 @@
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
@@ -55,6 +56,8 @@ int gWatching = 0;
int gAI = 0;
int gTurn = 0;
int gTestFlags = 0;
std::array<int, nPl> founded{};
int invalidTreatyMap = 0;
std::array<TWonderInfo, nWonder> gWonder{};
std::array<int, nPl> grWallContinent{};
std::array<TShipInfo, nPl> gShip{};
@@ -78,6 +81,14 @@ 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).
std::array<std::uint32_t, 8> borderChanges{};
// Equivalent of Database.pas's private FreeSlaves().
void freeSlaves()
{
@@ -99,6 +110,11 @@ void countLost(int p, int mix, int enemy)
tellAboutModel(enemy, p, mix);
++destroyed[enemy][p][mix];
}
// Forward declarations: defined further below (Territory Calculation
// section), needed earlier by intServer()'s sIntExpandTerritory case.
void changeTerritory(int loc, int p);
void expandTerritory(int originLoc);
} // namespace
void maskD(std::uint32_t *x, int count, std::uint32_t mask)
@@ -639,6 +655,175 @@ void removeUnitUpdateMap(int p, int uix)
updateUnitMap(loc0);
}
void removeDomainUnits(int d, int p, int loc)
{
TPlayerContext &player = rw[p];
TUn *un = *player.Un;
const TModel *model = *player.Model;
for (int uix = 0; uix < player.nUn; ++uix)
if (model[un[uix].mix].Domain == d && un[uix].Loc == loc)
removeUnit(p, uix);
}
int specialTile(int loc, int terrType, int lx)
{
if (terrType == fOcean)
return 0;
const int y = loc / lx;
const int x = loc - y * lx;
if (terrType == fGrass) { // formula for productive grassland
const int v1 = lymax + x - (y >> 1);
return (((v1 >> 1) + x + ((y + 1) >> 1)) & 1) ? 1 : 0;
}
// formula for special resources
const int a = 4 * x - y + 9980;
const int qx = a / 10;
if (qx * 10 != a || (qx & 3) == 0)
return 0;
const int qy = (y + x) / 5;
if ((qy & 3) == ((qx >> 2) & 1) * 2)
return 0;
if (terrType == fArctic || terrType == fSwamp)
return 1;
if (terrType == fShore) {
if (((qx + qy) & 1) == 0)
return (qx & 3) == 2 ? 2 : 1;
return 0;
}
return ((qx + qy) & 1) + 1;
}
std::uint32_t actualSpecialTile(int loc)
{
return static_cast<std::uint32_t>(specialTile(loc, static_cast<int>(realMap[loc] & fTerrain), lx));
}
void foundCity(int p, int foundLoc)
{
TPlayerContext &player = rw[p];
if (player.nCity == nCmax)
return;
++player.nCity;
TCity &city = (*player.City)[player.nCity - 1];
city.Size = 2;
city.Status = 0;
city.SavedStatus = 0;
std::fill(std::begin(city.Built), std::end(city.Built), 0);
city.Food = 0;
city.Project = cpImp + imTrGoods;
city.Prod = 0;
city.Project0 = city.Project;
city.Prod0 = 0;
city.Pollution = 0;
city.N1 = 0;
city.Loc = foundLoc;
if (usedByCity[foundLoc] >= 0) { // central tile is exploited - toggle in exploiting city
int p1 = p;
int cix1;
searchCity(usedByCity[foundLoc], p1, cix1);
int dx, dy;
dxdy(usedByCity[foundLoc], foundLoc, dx, dy);
const int v21 = ((dy + 3) << 2) + ((dx + 3) >> 1);
(*rw[p1].City)[cix1].Tiles &= ~(1u << v21);
}
city.Tiles = 1u << 13; // exploit central tile
usedByCity[foundLoc] = foundLoc;
realMap[foundLoc] =
(realMap[foundLoc] & (fTerrain | fSpecial | fRiver | (static_cast<std::uint32_t>(nPl) << 27))) | fCity;
changeTerritory(city.Loc, p);
}
void completeJob(int p, int loc, int job)
{
assert(job != jCity);
int changedTerrain = -1;
switch (job) {
case jRoad:
realMap[loc] |= fRoad;
break;
case jRR:
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fRoad)) | fRR;
break;
case jClear:
changedTerrain = Terrain[realMap[loc] & fTerrain].ClearTerrain;
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(changedTerrain);
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fSpecial)) | (actualSpecialTile(loc) << 5);
break;
case jIrr:
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiIrrigation;
break;
case jFarm:
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiFarm;
break;
case jAfforest:
changedTerrain = Terrain[realMap[loc] & fTerrain].AfforestTerrain;
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(changedTerrain);
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fSpecial)) | (actualSpecialTile(loc) << 5);
break;
case jMine:
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiMine;
break;
case jFort:
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiFort;
break;
case jCanal:
realMap[loc] |= fCanal;
break;
case jTrans:
changedTerrain = Terrain[realMap[loc] & fTerrain].TransTerrain;
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(changedTerrain);
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fSpecial)) | (actualSpecialTile(loc) << 5);
if (((terrTypeCanalableMask >> (realMap[loc] & fTerrain)) & 1) == 0) {
removeDomainUnits(dSea, p, loc);
realMap[loc] &= ~static_cast<std::uint32_t>(fCanal);
}
break;
case jPoll:
realMap[loc] &= ~static_cast<std::uint32_t>(fPoll);
break;
case jBase:
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiBase;
break;
case jPillage:
if (realMap[loc] & fTerImp) {
if ((realMap[loc] & fTerImp) == tiBase)
removeDomainUnits(dAir, p, loc);
realMap[loc] &= ~static_cast<std::uint32_t>(fTerImp);
} else if (realMap[loc] & fCanal) {
removeDomainUnits(dSea, p, loc);
realMap[loc] &= ~static_cast<std::uint32_t>(fCanal);
} else if (realMap[loc] & fRR) {
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fRR)) | fRoad;
} else if (realMap[loc] & fRoad) {
realMap[loc] &= ~static_cast<std::uint32_t>(fRoad);
}
break;
default:
break;
}
if (changedTerrain >= 0) { // remove terrain improvements if not possible on new terrain
if (((realMap[loc] & fTerImp) == tiIrrigation || (realMap[loc] & fTerImp) == tiFarm) &&
(Terrain[changedTerrain].IrrClearWork == 0 || Terrain[changedTerrain].ClearTerrain >= 0))
realMap[loc] &= ~static_cast<std::uint32_t>(fTerImp);
if ((realMap[loc] & fTerImp) == tiMine &&
(Terrain[changedTerrain].MineAfforestWork == 0 || Terrain[changedTerrain].AfforestTerrain >= 0))
realMap[loc] &= ~static_cast<std::uint32_t>(fTerImp);
}
// update map of all observing players
if (mode > moLoading_Fast) {
constexpr std::uint32_t mask = fTerrain | fSpecial | fTerImp | fRoad | fRR | fCanal | fPoll;
for (int p1 = 0; p1 < nPl; ++p1) {
if (((1 << p1) & (gAlive | gWatching)) && (((observeLevel[loc] >> (2 * p1)) & 3) > lNoObserve)) {
std::uint32_t &tile = (*rw[p1].Map)[loc];
tile = (tile & ~mask) | (realMap[loc] & mask);
}
}
}
}
void introduceEnemy(int p1, int p2)
{
rw[p1].Treaty[p2] = trNone;
@@ -703,13 +888,18 @@ void intServer(int command, int player, int subject, const void *data)
rwemix[p1][player][subject] = static_cast<std::int16_t>(toldPlayer.nEnemyModel);
++toldPlayer.nEnemyModel;
assert(toldPlayer.nEnemyModel < nemmax);
} else if (command == sIntExpandTerritory) {
if (mode < moPlaying) {
std::memcpy(borderChanges.data(), data, sizeof(borderChanges));
expandTerritory((*rw[player].City)[subject].Loc);
}
}
// Other sIntXxx cases (sIntHaveContact/sIntCancelTreaty/sIntDiscoverZOC/
// sIntExpandTerritory/sIntBuyMaterial/sIntPayPrices/sIntSetDevModel/
// sIntSetXxxStatus) are NOT implemented yet - each needs its own
// not-yet-ported Database.pas function (CancelTreaty, Discover9,
// ExpandTerritory, PayPrice, DiscoverViewAreas, ...). Add them here once
// those exist - see repo memory for the current dependency-chain status.
// sIntBuyMaterial/sIntPayPrices/sIntSetDevModel/sIntSetXxxStatus) are NOT
// implemented yet - each needs its own not-yet-ported Database.pas
// function (CancelTreaty, Discover9, PayPrice, DiscoverViewAreas, ...).
// Add them here once those exist - see repo memory for the current
// dependency-chain status.
}
void tellAboutModel(int p, int taOwner, int tamix)
@@ -1102,5 +1292,209 @@ void updateUnitMap(int loc, bool cityChange)
}
}
void recalcPeaceMap(int p)
{
if (mode != moPlaying)
return;
TPlayerContext &player = rw[p];
maskD(*player.Map, mapSize, ~static_cast<std::uint32_t>(fPeace));
std::array<bool, nPl + 1> peacePlayer{}; // index p1+1, covers p1 in -1..nPl-1
for (int p1 = 0; p1 < nPl; ++p1)
peacePlayer[p1 + 1] = p1 != p && ((1 << p1) & gAlive) &&
(player.Treaty[p1] == trPeace || player.Treaty[p1] == trFriendlyContact);
std::uint32_t *map = *player.Map;
const TOwnerList &territory = *player.Territory;
for (int loc = 0; loc < mapSize; ++loc)
if (peacePlayer[territory[loc] + 1])
map[loc] |= fPeace;
}
namespace {
void changeTerritory(int loc, int p)
{
assert(p >= 0); // no player's territory indicated by p == nPl
--territoryCount[realMap[loc] >> 27];
++territoryCount[p];
realMap[loc] = (realMap[loc] & ~(0xFu << 27)) | (static_cast<std::uint32_t>(p) << 27);
if (p == 0xF)
p = -1;
for (int p1 = 0; p1 < nPl; ++p1) {
if (((1 << p1) & (gAlive | gWatching)) == 0)
continue;
TPlayerContext &player = rw[p1];
if (((*player.Map)[loc] & fTerrain) == fUNKNOWN)
continue;
(*player.Territory)[loc] = static_cast<std::int8_t>(p);
// Faithful port of the original relying on x86 SHL masking the shift
// count to 5 bits when p==-1 (so `1 shl p` becomes `1 shl 31`, never
// matching gAlive, rather than C++'s real UB for negative shifts) -
// see repo memory's policy on preserving known upstream quirks.
if (p < nPl && p != p1 && ((1u << (p & 31)) & static_cast<std::uint32_t>(gAlive)) &&
(player.Treaty[p] == trPeace || player.Treaty[p] == trFriendlyContact))
(*player.Map)[loc] |= fPeace;
else
(*player.Map)[loc] &= ~static_cast<std::uint32_t>(fPeace);
}
}
void expandTerritory(int originLoc)
{
if (originLoc == -1)
throw std::runtime_error("Location error");
int i = 0;
int dyMax = 0;
while ((dyMax + 1) + ((dyMax + 1) >> 1) <= CountryRadius)
++dyMax;
for (int dy = -dyMax; dy <= dyMax; ++dy) {
int dxMax = dy & 1;
while (std::abs(dy) + (dxMax + 2) + (std::abs(std::abs(dy) - (dxMax + 2)) >> 1) <= CountryRadius)
dxMax += 2;
for (int dx = -dxMax; dx <= dxMax; ++dx) {
if (((dy + dx) & 1) == 0) {
const std::uint32_t newOwner = (borderChanges[i / 8] >> ((i % 8) * 4)) & 0xF;
const int loc = dLoc(originLoc, dx, dy);
if (loc >= 0 && newOwner != (realMap[loc] >> 27))
changeTerritory(loc, static_cast<int>(newOwner));
++i;
}
}
}
}
} // namespace
void checkBorders(int originLoc, int playerLosingCity)
{
std::array<std::int8_t, lxmax * lymax> country{};
std::array<std::int8_t, lxmax * lymax> formerCountry{};
std::array<std::int8_t, lxmax * lymax> dist{};
std::array<std::int8_t, lxmax * lymax> formerDist{};
std::array<std::int8_t, lxmax * lymax> stolenDist{};
std::array<std::array<bool, nPl + 1>, nPl + 1> atPeace{};
Vicinity8Loc adjacent;
constexpr std::uint32_t noZocTerrainMask = (1u << fShore) | (1u << fMountains) | (1u << fArctic);
if (playerLosingCity >= 0) {
for (int loc = 0; loc < mapSize; ++loc)
stolenDist[loc] = static_cast<std::int8_t>(CountryRadius + 1);
for (int cix = 0; cix < rw[playerLosingCity].nCity; ++cix) {
const int loc = (*rw[playerLosingCity].City)[cix].Loc;
if (loc >= 0)
stolenDist[loc] = 0;
}
for (int r = 1; r <= (CountryRadius >> 1); ++r) {
formerDist = stolenDist;
for (int loc = 0; loc < mapSize; ++loc) {
if (formerDist[loc] <= CountryRadius - 2 &&
((1u << (realMap[loc] & fTerrain)) & noZocTerrainMask) == 0) {
v8ToLoc(loc, adjacent);
for (int v8 = 0; v8 < 8; ++v8) {
const int loc1 = adjacent[v8];
const int newDist = formerDist[loc] + 2 + (v8 & 1);
if (loc1 >= 0 && loc1 < mapSize && newDist < stolenDist[loc1])
stolenDist[loc1] = static_cast<std::int8_t>(newDist);
}
}
}
}
}
country.fill(-1);
for (int loc = 0; loc < mapSize; ++loc)
dist[loc] = static_cast<std::int8_t>(CountryRadius + 1);
for (int p1 = 0; p1 < nPl; ++p1) {
if (((1 << p1) & gAlive) == 0)
continue;
for (int cix = 0; cix < rw[p1].nCity; ++cix) {
const int loc = (*rw[p1].City)[cix].Loc;
if (loc >= 0) {
country[loc] = static_cast<std::int8_t>(p1);
dist[loc] = 0;
}
}
}
for (int r = 1; r <= (CountryRadius >> 1); ++r) {
formerCountry = country;
formerDist = dist;
for (int loc = 0; loc < mapSize; ++loc) {
if (formerDist[loc] <= CountryRadius - 2 &&
((1u << (realMap[loc] & fTerrain)) & noZocTerrainMask) == 0) {
assert(formerCountry[loc] >= 0);
v8ToLoc(loc, adjacent);
for (int v8 = 0; v8 < 8; ++v8) {
const int loc1 = adjacent[v8];
const int newDist = formerDist[loc] + 2 + (v8 & 1);
if (loc1 >= 0 && loc1 < mapSize && newDist < dist[loc1]) {
country[loc1] = formerCountry[loc];
dist[loc1] = static_cast<std::int8_t>(newDist);
}
}
}
}
}
for (int p1 = 0; p1 < nPl; ++p1) {
if (((1 << p1) & gAlive) == 0)
continue;
for (int p2 = 0; p2 < nPl; ++p2)
if (p2 != p1 && ((1 << p2) & gAlive) && rw[p1].Treaty[p2] >= trPeace)
atPeace[p1][p2] = true;
}
if (originLoc >= 0) {
int i = 0;
borderChanges.fill(0);
int dyMax = 0;
while ((dyMax + 1) + ((dyMax + 1) >> 1) <= CountryRadius)
++dyMax;
for (int dy = -dyMax; dy <= dyMax; ++dy) {
int dxMax = dy & 1;
while (std::abs(dy) + (dxMax + 2) + (std::abs(std::abs(dy) - (dxMax + 2)) >> 1) <= CountryRadius)
dxMax += 2;
for (int dx = -dxMax; dx <= dxMax; ++dx) {
if (((dy + dx) & 1) == 0) {
const int loc = dLoc(originLoc, dx, dy);
if (loc >= 0) {
const int oldOwner = static_cast<int>(realMap[loc] >> 27);
int newOwner = country[loc] & 0xF;
if (newOwner != oldOwner) {
if (atPeace[newOwner][oldOwner] &&
!(oldOwner == playerLosingCity && stolenDist[loc] > CountryRadius))
newOwner = oldOwner; // peace fixes borders
else
changeTerritory(loc, newOwner);
}
borderChanges[i >> 3] |= static_cast<std::uint32_t>(newOwner) << ((i & 7) * 4);
}
++i;
}
}
}
} else {
for (int loc = 0; loc < mapSize; ++loc) {
const int oldOwner = static_cast<int>(realMap[loc] >> 27);
const int newOwner = country[loc] & 0xF;
if (newOwner != oldOwner &&
(!atPeace[newOwner][oldOwner] || (oldOwner == playerLosingCity && stolenDist[loc] > CountryRadius))) {
assert(originLoc != -2); // test if border saving works
changeTerritory(loc, newOwner);
}
}
}
#ifndef NDEBUG
if (originLoc != -2)
checkBorders(-2); // check: single pass should do!
#endif
}
void logCheckBorders(int p, int cix, int playerLosingCity)
{
checkBorders((*rw[p].City)[cix].Loc, playerLosingCity);
intServer(sIntExpandTerritory, p, cix, borderChanges.data());
}
} // namespace Database
} // namespace Cevo