more gameserver support functions

This commit is contained in:
Geoffray Levasseur committed 2026-10-27 17:14:40 +01:00
1 parent 74685ea66f
commit 836fdff772
2 files changed
+367 -6

No files matched your search

+353 -6
View File
@@ -512,6 +512,38 @@ void setModelFlags(TModel &m)
m.Flags |= mdDoubleSupport;
}
void calculateModel(TModel &m)
{
m.Attack = static_cast<decltype(m.Attack)>((m.Cap[mcOffense] + m.Cap[mcOver]) * m.MStrength);
m.Defense = static_cast<decltype(m.Defense)>((m.Cap[mcDefense] + m.Cap[mcOver]) * m.MStrength);
switch (m.Domain) {
case dGround:
m.Speed = static_cast<decltype(m.Speed)>(150 + m.Cap[mcMob] * 50);
break;
case dSea:
m.Speed = static_cast<decltype(m.Speed)>(350 + 200 * m.Cap[mcNP] + 200 * m.Cap[mcTurbines]);
if (m.Cap[mcNP] == 0)
m.Speed = static_cast<decltype(m.Speed)>(m.Speed + 100 * m.Cap[mcSE]);
break;
case dAir:
m.Speed = static_cast<decltype(m.Speed)>(850 + 400 * m.Cap[mcJet]);
break;
default:
break;
}
int cost = 0;
for (int i = 0; i < nFeature; ++i)
if ((1 << m.Domain) & Feature[i].Domains)
cost += m.Cap[i] * Feature[i].Cost;
m.Cost = static_cast<decltype(m.Cost)>(cost * m.MCost);
int weight = 0;
for (int i = 0; i < nFeature; ++i)
if ((1 << m.Domain) & Feature[i].Domains)
weight += (m.Domain == dGround && i == mcDefense) ? m.Cap[i] * 2 : m.Cap[i] * Feature[i].Weight;
m.Weight = static_cast<decltype(m.Weight)>(weight);
setModelFlags(m);
}
void checkSpecialModels(int p, int pre)
{
TPlayerContext &player = rw[p];
@@ -1037,6 +1069,13 @@ void editTile(int loc, std::uint32_t newTile)
}
}
void releaseMapEditor()
{
delete[] reinterpret_cast<std::int8_t *>(rw[0].Territory);
delete[] reinterpret_cast<std::int16_t *>(rw[0].MapObservedLast);
delete[] reinterpret_cast<std::uint32_t *>(rw[0].Map);
}
void introduceEnemy(int p1, int p2)
{
rw[p1].Treaty[p2] = trNone;
@@ -1257,6 +1296,283 @@ std::uint32_t doSpyMission(int p, int pCity, int cix, int mission)
return result;
}
void cancelTreaty(int p, int pWith, bool decreaseCredibility)
{
TPlayerContext &player = rw[p];
TPlayerContext &withPlayer = rw[pWith];
const int oldTreaty = player.Treaty[pWith];
peaceEnded = 0;
if (oldTreaty >= trPeace)
player.LastCancelTreaty[pWith] = gTurn;
if (decreaseCredibility) {
if (oldTreaty == trPeace) {
player.Credibility >>= 1;
if (player.MaxCredibility > 0)
player.MaxCredibility -= 10;
if (player.Credibility > player.MaxCredibility)
player.Credibility = player.MaxCredibility;
} else if (oldTreaty == trAlliance) {
player.Credibility = player.Credibility * 3 / 4;
}
withPlayer.EnemyReport[p]->Credibility = player.Credibility;
}
if (oldTreaty == trPeace) {
for (int p1 = 0; p1 < nPl; ++p1) {
if (p1 == pWith || (decreaseCredibility && p1 != p && withPlayer.Treaty[p1] == trAlliance &&
player.Treaty[p1] >= trPeace)) {
player.Treaty[p1] = trNone;
rw[p1].Treaty[p] = trNone;
player.EvaStart[p1] = -PeaceEvaTurns - 1;
rw[p1].EvaStart[p] = -PeaceEvaTurns - 1;
peaceEnded += (1 << p1);
}
}
checkBorders(-1);
if (mode > moLoading_Fast && peaceEnded > 0)
recalcMapZoC(p);
} else {
player.Treaty[pWith] = oldTreaty - 1;
withPlayer.Treaty[p] = oldTreaty - 1;
if (oldTreaty == trFriendlyContact) { // necessary for loading
giveCivilReport(p, pWith);
giveCivilReport(pWith, p);
} else if (oldTreaty == trAlliance) { // necessary for loading
giveMilReport(p, pWith);
giveMilReport(pWith, p);
}
if (mode > moLoading_Fast && oldTreaty == trAlliance) {
recalcMapZoC(p);
recalcMapZoC(pWith);
}
}
if (oldTreaty == trPeace || oldTreaty == trAlliance) {
recalcPeaceMap(p);
recalcPeaceMap(pWith);
}
}
void showPrice(int pSender, int pTarget, std::uint32_t price)
{
switch (price & opMask) {
case opTech: // + advance
if (rw[pTarget].EnemyReport[pSender]->Tech[price - opTech] < tsApplicable)
rw[pTarget].EnemyReport[pSender]->Tech[price - opTech] = tsApplicable;
break;
case opModel: // + model index
tellAboutModel(pTarget, pSender, static_cast<int>(price - opModel));
break;
default:
break;
}
}
bool payPrice(int pSender, int pTarget, std::uint32_t price, bool execute)
{
bool result = true;
switch (price & opMask) {
case opCivilReport: { // + turn + concerned player shl 16
const int pSubject = static_cast<int>((price >> 16) & 0xF);
if (pTarget == pSubject)
result = false;
else if (pSender == pSubject) {
if (execute)
giveCivilReport(pTarget, pSender);
} else if (rw[pSender].EnemyReport[pSubject]->TurnOfCivilReport < 0)
result = false;
else if (execute)
copyCivilReport(pSender, pTarget, pSubject);
break;
}
case opMilReport: { // + turn + concerned player shl 16
const int pSubject = static_cast<int>((price >> 16) & 0xF);
if (pTarget == pSubject)
result = false;
else if (pSender == pSubject) {
if (execute)
giveMilReport(pTarget, pSender);
} else if (rw[pSender].EnemyReport[pSubject]->TurnOfMilReport < 0)
result = false;
else if (execute)
copyMilReport(pSender, pTarget, pSubject);
break;
}
case opMap:
if (execute) {
copyMap(pSender, pTarget);
recalcPeaceMap(pTarget);
}
break;
case opTreaty: { // + nation treaty (opMask only keeps the top byte, so
// this single case covers the original's whole
// opTreaty..opTreaty+trAlliance range)
if (static_cast<int>(price - opTreaty) == rw[pSender].Treaty[pTarget] - 1) { // agreed treaty end
if (execute)
cancelTreaty(pSender, pTarget, false);
} else {
int newTreaty = -1;
if (static_cast<int>(price - opTreaty) == rw[pSender].Treaty[pTarget] + 1)
newTreaty = static_cast<int>(price - opTreaty);
else if (rw[pSender].Treaty[pTarget] == trNone && static_cast<int>(price - opTreaty) == trPeace)
newTreaty = trPeace;
if (newTreaty < 0)
result = false;
else if (execute) {
assert(newTreaty > rw[pSender].Treaty[pTarget]);
rw[pSender].Treaty[pTarget] = newTreaty;
rw[pTarget].Treaty[pSender] = newTreaty;
if (newTreaty >= trFriendlyContact) {
giveCivilReport(pTarget, pSender);
giveCivilReport(pSender, pTarget);
}
if (newTreaty == trAlliance) {
giveMilReport(pTarget, pSender);
giveMilReport(pSender, pTarget);
copyMap(pSender, pTarget);
copyMap(pTarget, pSender);
recalcMapZoC(pSender);
recalcMapZoC(pTarget);
}
if (newTreaty != trPeace && newTreaty != trFriendlyContact) {
rw[pSender].EvaStart[pTarget] = -PeaceEvaTurns - 1;
rw[pTarget].EvaStart[pSender] = -PeaceEvaTurns - 1;
}
recalcPeaceMap(pSender);
recalcPeaceMap(pTarget);
}
}
break;
}
case opShipParts: { // + number + part type shl 16
const int n = static_cast<int>(price & 0xFFFF); // number
const int i = static_cast<int>((price >> 16) & 0xF); // type
if (i < nShipPart && gShip[pSender].Parts[i] >= n) {
if (execute) {
gShip[pSender].Parts[i] -= n;
rw[pSender].Ship[pSender].Parts[i] = gShip[pSender].Parts[i];
rw[pTarget].Ship[pSender].Parts[i] = gShip[pSender].Parts[i];
if (rw[pTarget].NatBuilt[imSpacePort] > 0) { // space ship control requires space port
gShip[pTarget].Parts[i] += n;
rw[pSender].Ship[pTarget].Parts[i] = gShip[pTarget].Parts[i];
rw[pTarget].Ship[pTarget].Parts[i] = gShip[pTarget].Parts[i];
}
}
} else {
result = false;
}
break;
}
case opMoney: // + value
if (price - opMoney <= MaxMoneyPrice && rw[pSender].Money >= static_cast<int>(price - opMoney)) {
if (execute) {
rw[pSender].Money -= static_cast<int>(price - opMoney);
rw[pTarget].Money += static_cast<int>(price - opMoney);
}
} else {
result = false;
}
break;
case opTribute: // + value
// tribute no longer in use - execute branch intentionally empty
break;
case opTech: // + advance
if (rw[pSender].Tech[price - opTech] >= tsApplicable) {
if (execute && rw[pTarget].Tech[price - opTech] == tsNA) {
seeTech(pTarget, static_cast<int>(price - opTech));
rw[pSender].EnemyReport[pTarget]->Tech[price - opTech] = tsSeen;
}
} else {
result = false;
}
break;
case opAllTech:
if (execute) {
for (int i = 0; i < nAdv; ++i) {
if (rw[pSender].Tech[i] >= tsApplicable && rw[pTarget].Tech[i] == tsNA) {
seeTech(pTarget, i);
rw[pSender].EnemyReport[pTarget]->Tech[i] = tsSeen;
rw[pTarget].EnemyReport[pSender]->Tech[i] = tsApplicable;
}
}
}
break;
case opModel: // + model index
if (static_cast<int>(price - opModel) < rw[pSender].nModel) {
if (execute)
copyModel(pSender, pTarget, static_cast<int>(price - opModel));
} else {
result = false;
}
break;
case opAllModel:
if (execute)
for (int i = 0; i < rw[pSender].nModel; ++i) {
tellAboutModel(pTarget, pSender, i);
copyModel(pSender, pTarget, i);
}
break;
default:
break;
}
return result;
}
void clearTestFlags(int clearFlags)
{
gTestFlags &= (~clearFlags | tfTested | tfAllTechs | tfAllContact);
for (int p1 = 0; p1 < nPl; ++p1)
if ((1 << p1) & (gAlive | gWatching))
rw[p1].TestFlags = gTestFlags;
}
void setTestFlags(int p, int setFlags)
{
const int moreFlags = setFlags & ~gTestFlags;
gTestFlags |= (setFlags & 0x7FF);
for (int p1 = 0; p1 < nPl; ++p1)
if ((1 << p1) & (gAlive | gWatching))
rw[p1].TestFlags = gTestFlags;
if (moreFlags & (tfUncover | tfAllContact)) {
for (int p1 = 0; p1 < nPl - 1; ++p1)
if ((1 << p1) & gAlive)
for (int p2 = p1 + 1; p2 < nPl; ++p2)
if (((1 << p2) & gAlive) && rw[p1].Treaty[p2] == trNoContact) // make p1 and p2 know each other
introduceEnemy(p1, p2);
}
if (moreFlags & tfAllTechs) {
for (int p1 = 0; p1 < nPl; ++p1) {
resourceMask[p1] = 0xFFFFFFFFu;
if ((1 << p1) & gAlive) {
for (int i = 0; i < nAdv; ++i) // give all techs to player p1
if (!IsFutureTech(i) && rw[p1].Tech[i] < tsApplicable) {
rw[p1].Tech[i] = tsCheat;
checkSpecialModels(p1, i);
}
for (int p2 = 0; p2 < nPl; ++p2)
if (p2 != p1 && ((gAlive | gWatching) & (1 << p2)))
for (int i = 1; i <= 3; ++i)
if (rw[p2].EnemyReport[p1]->Tech[AgePreq[i]] < tsApplicable)
rw[p2].EnemyReport[p1]->Tech[AgePreq[i]] = tsCheat;
}
}
}
if (moreFlags & tfUncover) {
discoverAll(p, lObserveSuper);
for (int p1 = 0; p1 < nPl; ++p1) {
if ((1 << p1) & gAlive) {
resourceMask[p1] = 0xFFFFFFFFu;
if (p1 != p) {
giveCivilReport(p, p1);
giveMilReport(p, p1);
}
}
}
}
}
void intServer(int command, int player, int subject, const void *data)
{
if (mode == moPlaying)
@@ -1278,13 +1594,44 @@ void intServer(int command, int player, int subject, const void *data)
std::memcpy(borderChanges.data(), data, sizeof(borderChanges));
expandTerritory((*rw[player].City)[subject].Loc);
}
} else if (command == sIntHaveContact) {
assert(rw[player].Treaty[subject] > trNoContact);
rw[player].EnemyReport[subject]->TurnOfContact = gTurn;
rw[subject].EnemyReport[player]->TurnOfContact = gTurn;
} else if (command == sIntCancelTreaty) {
cancelTreaty(player, subject);
} else if (command == sIntDiscoverZOC) {
discover9(static_cast<int>(reinterpret_cast<std::intptr_t>(data)), player, lObserveUnhidden, true, false);
} else if (command == sIntBuyMaterial) {
TCity &city = (*rw[player].City)[subject];
const auto cost = static_cast<int>(reinterpret_cast<std::intptr_t>(data));
rw[player].Money -= cost;
if (gWonder[woMich].EffectiveOwner == player && (city.Project & cpImp))
city.Prod += cost / 2;
else
city.Prod += cost / 4;
if ((city.Project0 & ~cpAuto) != (city.Project & ~cpAuto))
city.Project0 = city.Project;
city.Prod0 = city.Prod;
} else if (command >= sIntPayPrices && command <= sIntPayPrices + 12) {
const auto &offer = *static_cast<const TOffer *>(data);
for (int i = 0; i < offer.nDeliver; ++i)
payPrice(player, subject, offer.Price[static_cast<std::size_t>(i)], true);
for (int i = 0; i < offer.nCost; ++i)
payPrice(subject, player, offer.Price[static_cast<std::size_t>(offer.nDeliver + i)], true);
for (int i = 0; i < offer.nDeliver + offer.nCost; ++i) {
if (offer.Price[static_cast<std::size_t>(i)] == opTreaty + trAlliance) {
// add view area of allied player
discoverViewAreas(player);
discoverViewAreas(subject);
break;
}
}
}
// Other sIntXxx cases (sIntHaveContact/sIntCancelTreaty/sIntDiscoverZOC/
// 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.
// Other sIntXxx cases (sIntSetDevModel/sIntSetXxxStatus) are NOT
// implemented yet - each is only needed by LoadGame's command replay of
// a client's own already-applied local changes, not by Server()'s own
// command dispatch - lower priority than the above. See repo memory.
}
void tellAboutModel(int p, int taOwner, int tamix)