city processing

This commit is contained in:
Geoffray Levasseur committed 2026-10-27 11:57:29 +01:00
1 parent b9cfa2393f
commit 18bf340dee
5 files changed
+2524 -14

No files matched your search

+43 -6
View File
@@ -43,12 +43,14 @@
// calling convention. // calling convention.
// //
// NOT ported here (left as a follow-up, pure data with no ABI-shape impact): // NOT ported here (left as a follow-up, pure data with no ABI-shape impact):
// the large gameplay rule tables Imp[], Terrain[], Upgrade[], Feature[], // Terrain[], Upgrade[], Feature[], AdvPreq[], JobPreq, SpecialModel[],
// AdvPreq[], GovPreq, AgePreq, JobPreq, SpecialModel[], ShipNeed/ShipImpIndex, // ImpReplacement, AutoFeature, WeightPreq7/10. An AI module compiled against
// SettlerFood/CorrLevel/SupportFree, ImpReplacement, AutoFeature, // a slightly different copy of those tables still speaks the same wire
// WeightPreq7/10. An AI module compiled against a slightly different copy of // protocol; it just has stale rule knowledge, which is a correctness/
// those tables still speaks the same wire protocol; it just has stale rule // strength concern, not an ABI one. (Imp[]/GovPreq/AgePreq/SettlerFood/
// knowledge, which is a correctness/strength concern, not an ABI one. // CorrLevel/SupportFree/ShipNeed/ShipImpIndex WERE added later, once a
// consumer needed them - this comment previously listed them as unported,
// now corrected.)
// //
// Field order/types mirror the Pascal records exactly; `{$PACKRECORDS C}` // Field order/types mirror the Pascal records exactly; `{$PACKRECORDS C}`
// (Free Pascal's C-compatible alignment) matches plain C++ struct layout on // (Free Pascal's C-compatible alignment) matches plain C++ struct layout on
@@ -1649,6 +1651,41 @@ constexpr int GovPreq[nGov] = { preNA, preNone, adMonarchy, adTheRepublic, adThe
constexpr int AgePreq[4] = { preNA, adScience, adMassProduction, adTransstellarColonization }; constexpr int AgePreq[4] = { preNA, adScience, adMassProduction, adTransstellarColonization };
constexpr int SettlerFood[nGov] = { 1, 1, 1, 2, 1, 2, 2, 2 }; constexpr int SettlerFood[nGov] = { 1, 1, 1, 2, 1, 2, 2, 2 };
constexpr int CorrLevel[nGov] = { 3, 3, 1, 2, 1, 0, 0, 0 };
constexpr int SupportFree[nGov] = { 2, 2, 1, 0, 2, 1, 0, 0 };
constexpr int nSpecialModel = 9;
// Pre-defined unit models every player has from the start (Settlers,
// Engineers, Militia, Town Guard, Special Commando, Freight, Longboat,
// Slaves, Glider) - field order matches struct TModel exactly (aggregate
// init, no designated initializers since this targets C++17). Trailing
// `{}`/short Cap[] initializers zero-fill the rest, same as the Pascal
// array literals' explicit zero lists.
constexpr TModel SpecialModel[nSpecialModel] = {
// Settlers
{ 0, 0, 0, 0, 0, 0, mkSettler, dGround, 0, 10, 150, 40, 0, 0, 0, 0, 0, 0, 0, {} },
// Engineers
{ 0, 0, 0, 0, 0, 0, mkSettler, dGround, 0, 20, 300, 40, 0, 0, 0, 0, 0, 0, 0, {} },
// Militia
{ 0, 0, 0, 0, 0, 0, mkSelfDeveloped, dGround, 6, 6, 150, 10, 0, 0, 0, 0, 0, 0, 0, {} },
// Town Guard
{ 0, 0, 0, 0, 0, 0, mkSpecial_TownGuard, dGround, 4, 6, 150, 20, 0, 0, 0, 0, 0, 0, 0, {} },
// Special Commando
{ 0, 0, 0, 0, 0, 0, mkDiplomat, dGround, 12, 12, 250, 20, 0, 0, 0, 0, 0, 0, 0, {} },
// Freight
{ 0, 0, 0, 0, 0, 0, mkCaravan, dGround, 0, 6, 150, 60, 0, 0, 0, 0, 0, 0, 0, {} },
// Longboat (Cap[3] = 2)
{ 0, 0, 0, 0, 0, 0, mkSpecial_Boat, dSea, 0, 3, 250, 20, 0, 1, 0, 0, 0, 0, 0, { 0, 0, 0, 2 } },
// Slaves
{ 0, 0, 0, 0, 0, 0, mkSlaves, dGround, 0, 15, 150, 40, 0, 0, 0, 0, 0, 0, 0, {} },
// Glider (Cap[17] = 1)
{ 0, 0, 0, 0, 0, 0, mkSpecial_Glider, dAir, 6, 6, 450, 30, 0, 0, 0, 0, 0, 0, 0,
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } },
};
constexpr int SpecialModelPreq[nSpecialModel] = { preNone, adExplosives, preNone, preNone, adIntelligence,
adTrade, adMapMaking, preBuilder, preLeo };
struct TerrainInfo { struct TerrainInfo {
int MoveCost; int MoveCost;
+44 -8
View File
@@ -33,14 +33,17 @@
namespace Cevo { namespace Cevo {
namespace CityProcessing { namespace CityProcessing {
// Equivalent of CityProcessing.pas (~1743 lines). PORTED INCREMENTALLY: this // Equivalent of CityProcessing.pas (~1743 lines). FULLY PORTED. Its
// is only its "Reporting" section's two simplest functions so far - every // private implementation-section helpers (DetermineCityProdProcessing/
// other function in the original (GetCityReport/GetCityReportNew and // DetermineCityTradeProcessing/BoostProd/SplitTrade/GetProjectCost/
// everything in "Internal Tile Picking"/"Turn Processing") additionally // GetSmallCityReport/NextBest/NextWorst/NextPoll, plus the ProdProcessing/
// needs GetTileInfo/GetUnitReport/Strongest (Database.pas functions not // TradeProcessing/CityReportEx support structs) live in an anonymous
// ported yet) plus several more Database.pas globals (GWonder, Difficulty, // namespace in CityProcessing.cpp, matching the original's own visibility
// ServerVersion, ...) and gameplay data tables (Imp[], CorrLevel[], // (not part of its interface section either).
// SupportFree[], ...). Add more here only once those are ported too. // CityTurn() needed a large prerequisite chain in Database.pas (Map
// Processing, most of Map Revealing, Tech Related Functions, a slice of
// Diplomacy, plus the SpecialModel[]/SpecialModelPreq[] tables) - see repo
// memory for the full list of what got ported to unblock it.
// Equivalent of CityProcessing.pas's GetCityAreaInfo(). // Equivalent of CityProcessing.pas's GetCityAreaInfo().
void getCityAreaInfo(int p, int loc, TCityAreaInfo &cityAreaInfo); void getCityAreaInfo(int p, int loc, TCityAreaInfo &cityAreaInfo);
@@ -48,5 +51,38 @@ void getCityAreaInfo(int p, int loc, TCityAreaInfo &cityAreaInfo);
// Equivalent of CityProcessing.pas's CanCityGrow(). // Equivalent of CityProcessing.pas's CanCityGrow().
bool canCityGrow(int p, int cix); bool canCityGrow(int p, int cix);
// Equivalent of CityProcessing.pas's GetCityReport()/GetCityReportNew().
int getCityReport(int p, int cix, TCityReport &cityReport);
int getCityReportNew(int p, int cix, TCityReportNew &cityReportNew);
// Equivalent of CityProcessing.pas's AddBestCityTile().
bool addBestCityTile(int p, int cix);
// Equivalent of CityProcessing.pas's CityGrowth()/CityShrink()/Pollute().
void cityGrowth(int p, int cix);
void cityShrink(int p, int cix);
void pollute(int p, int cix);
// Equivalent of CityProcessing.pas's PayCityMaintenance()/CollectCityResources().
void payCityMaintenance(int p, int cix);
void collectCityResources(int p, int cix);
// Equivalent of CityProcessing.pas's CityTurn(). Return value: whether the
// city keeps existing.
bool cityTurn(int p, int cix);
// Equivalent of CityProcessing.pas's SetCityTiles().
int setCityTiles(int p, int cix, int newTiles, bool testOnly = false);
// Equivalent of CityProcessing.pas's GetCityTileAdvice(): brute-force search
// over all "take N best tiles from each resource hierarchy" combinations to
// recommend the tile selection maximizing a weighted food/prod/tax/science
// value formula (weights packed into advice.ResourceWeights on input).
void getCityTileAdvice(int p, int cix, TCityTileAdviceData &advice);
// Equivalent of CityProcessing.pas's InitGame()/ReleaseGame().
void initGame();
void releaseGame();
} // namespace CityProcessing } // namespace CityProcessing
} // namespace Cevo } // namespace Cevo
+115
View File
@@ -28,6 +28,7 @@
#pragma once #pragma once
#include "Cevo/CmdList.h"
#include "Cevo/Protocol.h" #include "Cevo/Protocol.h"
#include <array> #include <array>
@@ -58,6 +59,26 @@ namespace Database {
// `TCity *city = *rw[p].City;` first if indexing repeatedly (same idiom // `TCity *city = *rw[p].City;` first if indexing repeatedly (same idiom
// ai/src/CustomAI.cpp already uses for `myCity`). // ai/src/CustomAI.cpp already uses for `myCity`).
// Observe levels (Database.pas's own constants, not Protocol.pas's).
constexpr int lNoObserve = 0;
constexpr int lObserveUnhidden = 1;
constexpr int lObserveAll = 2;
constexpr int lObserveSuper = 3;
// Database.pas's own constant (not Protocol.pas's).
constexpr int CityOwnTile = 13;
constexpr int nemmax = 1024;
constexpr int necmax = 1024;
constexpr int neumax = 4096;
// Equivalent of Database.pas's TGameMode - ordinal comparisons (</>) match
// the original's enum declaration order.
enum GameMode { moLoading_Fast, moLoading, moMovie, moPlaying };
extern GameMode mode;
// Equivalent of Database.pas's `CL: TCmdList` - the live command log.
extern CmdList cl;
extern int lx; extern int lx;
extern int ly; extern int ly;
extern int mapSize; // = lx * ly extern int mapSize; // = lx * ly
@@ -67,16 +88,44 @@ using Vicinity21Loc = std::array<int, 28>;
// player data // player data
extern std::array<TPlayerContext, nPl> rw; extern std::array<TPlayerContext, nPl> rw;
extern std::array<int, nPl> difficulty;
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> lastValidStat;
extern std::array<int, nPl> nTech; // number of known techs
extern int gAlive; // players alive; bitset of 1 shl p
extern int gWatching;
extern int gAI;
extern int gTurn; // current turn
extern int gTestFlags;
extern std::array<TWonderInfo, nWonder> gWonder;
extern std::array<int, nPl> grWallContinent; // continent id the Great Wall was built on, -1 if none
extern std::array<TShipInfo, nPl> gShip;
// [p1,p2,mix] -> index of p2's model mix in p1's enemy model list
extern std::array<std::array<std::array<std::int16_t, nMmax>, nPl>, nPl> rwemix;
// [p1,p2,mix] -> number of p2's units with model mix that p1 has destroyed
extern std::array<std::array<std::array<std::int16_t, nMmax>, nPl>, nPl> destroyed;
// map data // map data
extern std::array<std::uint32_t, lxmax * lymax> realMap; extern std::array<std::uint32_t, lxmax * lymax> realMap;
extern std::array<int, lxmax * lymax> continent; // continent id for each tile
extern std::array<std::int8_t, lxmax * lymax> occupant; // occupying player for each tile extern std::array<std::int8_t, lxmax * lymax> occupant; // occupying player for each tile
extern std::array<std::int8_t, lxmax * lymax> zocMap; extern std::array<std::int8_t, lxmax * lymax> zocMap;
extern std::array<std::int32_t, lxmax * lymax> usedByCity; // -1 if not exploited extern std::array<std::int32_t, lxmax * lymax> usedByCity; // -1 if not exploited
extern std::array<std::uint32_t, lxmax * lymax> observeLevel; // observe level of player p in bits 2*p, 2*p+1
// Equivalent of Database.pas's MaskD(). // Equivalent of Database.pas's MaskD().
void maskD(std::uint32_t *x, int count, std::uint32_t mask); void maskD(std::uint32_t *x, int count, std::uint32_t mask);
// Equivalent of Database.pas's IntServer(): dispatches internal server
// notifications (sIntXxx command codes) and records them to the command
// log. Declared here (used by several Map Revealing functions below) but
// only implemented once its own dependency chain (IntroduceEnemy,
// CancelTreaty, ...) exists - see Database.cpp for current coverage.
void intServer(int command, int player, int subject, const void *data);
// --- Location Navigation --- // --- Location Navigation ---
// Relative location, dx in hor and dy in ver direction from loc. Returns -1 // Relative location, dx in hor and dy in ver direction from loc. Returns -1
@@ -92,5 +141,71 @@ void v21ToLoc(int loc0, Vicinity21Loc &vicinityLoc);
void calculatePrimitive(); void calculatePrimitive();
bool mapGeneratorAvailable(); bool mapGeneratorAvailable();
// --- Tech Related Functions ---
int techBaseCost(int nTech, int diff);
// Sets a model's Flags (ZOC/civil/double-support) from its other fields.
void setModelFlags(TModel &m);
// Grants player p any SpecialModel[] not yet available that pre unlocks.
void checkSpecialModels(int p, int pre);
// GWonder[wonder].EffectiveOwner must already be set before calling this.
void checkExpiration(int wonder);
// --- Map Revealing ---
// cix>=0: known city index of player p (internal use only).
// cix=-1: search city, player unknown, only if permission for p.
// cix=-2: don't search city, don't calculate city benefits, just government.
int getTileInfo(int p, int cix, int loc, TTileInfo &info);
// Finds the strongest defender at loc.
void strongest(int loc, int &uix, int &strength, int &bonus, int &cnt);
int unitSpeed(int p, int mix, int health);
void getUnitReport(int p, int uix, TUnitReport &unitReport);
void tellAboutModel(int p, int taOwner, int tamix);
int emixSafe(int p, int taOwner, int tamix);
int getUnitStack(int p, int loc);
void makeCityInfo(int p, int cix, TCityInfo &ci);
// euix = -2: full discover. euix = -1: unit and city only, append to
// EnemyUn. euix >= 0: unit and city only, replace EnemyUn[euix].
bool discoverTile(int loc, int p, int pTell, int level, bool enableContact, int euix = -2);
bool discover9(int loc, int p, int level, bool tellAllied, bool enableContact);
bool discover21(int loc, int p, int adjacentLevel, bool tellAllied, bool enableContact);
// Player p discovers the complete playground (for the supervisor).
void discoverAll(int p, int level);
// Discovers unit and city view areas.
void discoverViewAreas(int p);
// Updates maps and enemy units of all players after a unit change.
void updateUnitMap(int loc, bool cityChange = false);
// Sets p to the supposed owner before the call.
void searchCity(int loc, int &p, int &cix);
// --- Map Processing ---
void createUnit(int p, int mix);
void placeUnit(int p, int uix);
void freeUnit(int p, int uix);
// loc or master should be set after call. Enemy >=0 counts the unit as lost
// to that player (use only from inside sMoveUnit if attack).
void removeUnit(int p, int uix, int enemy = -1);
void removeUnitUpdateMap(int p, int uix);
// --- Diplomacy ---
void introduceEnemy(int p1, int p2);
int techCost(int p);
void giveCivilReport(int p, int pAbout);
void giveMilReport(int p, int pAbout);
} // namespace Database } // namespace Database
} // namespace Cevo } // namespace Cevo
File diff suppressed because it is too large. Load diff
+918
View File
@@ -30,6 +30,7 @@
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
#include <cstring>
#include <stdexcept> #include <stdexcept>
namespace Cevo { namespace Cevo {
@@ -38,19 +39,66 @@ namespace Database {
int lx = 0; int lx = 0;
int ly = 0; int ly = 0;
int mapSize = 0; int mapSize = 0;
GameMode mode = moLoading_Fast;
CmdList cl;
std::array<TPlayerContext, nPl> rw{}; std::array<TPlayerContext, nPl> rw{};
std::array<int, nPl> difficulty{};
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> lastValidStat{};
std::array<int, nPl> nTech{};
int gAlive = 0;
int gWatching = 0;
int gAI = 0;
int gTurn = 0;
int gTestFlags = 0;
std::array<TWonderInfo, nWonder> gWonder{};
std::array<int, nPl> grWallContinent{};
std::array<TShipInfo, nPl> gShip{};
std::array<std::array<std::array<std::int16_t, nMmax>, nPl>, nPl> rwemix{};
std::array<std::array<std::array<std::int16_t, nMmax>, nPl>, nPl> destroyed{};
std::array<std::uint32_t, lxmax * lymax> realMap{}; std::array<std::uint32_t, lxmax * lymax> realMap{};
std::array<int, lxmax * lymax> continent{};
std::array<std::int8_t, lxmax * lymax> occupant{}; std::array<std::int8_t, lxmax * lymax> occupant{};
std::array<std::int8_t, lxmax * lymax> zocMap{}; std::array<std::int8_t, lxmax * lymax> zocMap{};
std::array<std::int32_t, lxmax * lymax> usedByCity{}; std::array<std::int32_t, lxmax * lymax> usedByCity{};
std::array<std::uint32_t, lxmax * lymax> observeLevel{};
namespace { namespace {
// Equivalent of Database.pas's private (implementation-section) `Primitive` // Equivalent of Database.pas's private (implementation-section) `Primitive`
// global - not exposed in the header, matching the original's own // global - not exposed in the header, matching the original's own
// interface/implementation visibility split. // interface/implementation visibility split.
int primitive = 0; int primitive = 0;
// Equivalent of Database.pas's private `UnBuilt: array[0..nPl-1] of Integer`
// (number of units built, used to assign unique per-player unit IDs).
std::array<int, nPl> unBuilt{};
// Equivalent of Database.pas's private FreeSlaves().
void freeSlaves()
{
for (int p1 = 0; p1 < nPl; ++p1) {
if (gAlive & (1 << p1)) {
TUn *un = *rw[p1].Un;
const TModel *model = *rw[p1].Model;
for (int uix = 0; uix < rw[p1].nUn; ++uix)
if (model[un[uix].mix].Kind == mkSlaves)
un[uix].Job = jNone;
}
}
}
// Equivalent of Database.pas's private CountLost().
void countLost(int p, int mix, int enemy)
{
++(*rw[p].Model)[mix].Lost;
tellAboutModel(enemy, p, mix);
++destroyed[enemy][p][mix];
}
} // namespace } // namespace
void maskD(std::uint32_t *x, int count, std::uint32_t mask) void maskD(std::uint32_t *x, int count, std::uint32_t mask)
@@ -184,5 +232,875 @@ bool mapGeneratorAvailable()
return primitive > 0 && lx >= 20 && ly >= 40; return primitive > 0 && lx >= 20 && ly >= 40;
} }
void searchCity(int loc, int &p, int &cix)
{
if (realMap[loc] < static_cast<std::uint32_t>(nPl) << 27)
p = static_cast<int>(realMap[loc] >> 27);
for (int i = 0; i < nPl; ++i) {
if ((1 << p) & gAlive) {
TPlayerContext &player = rw[p];
cix = player.nCity - 1;
while (cix >= 0 && (*player.City)[cix].Loc != loc)
--cix;
if (cix >= 0)
return;
}
assert(i < nPl - 1);
p = (p + 1) % nPl;
}
}
int unitSpeed(int p, int mix, int health)
{
const TModel &model = (*rw[p].Model)[mix];
int result = model.Speed;
if (model.Domain == dSea) {
if (gWonder[woMagellan].EffectiveOwner == p)
result += 200;
if (health < 100)
result = ((result - 250) * health / 5000) * 50 + 250;
}
return result;
}
void getUnitReport(int p, int uix, TUnitReport &unitReport)
{
unitReport.FoodSupport = 0;
unitReport.ProdSupport = 0;
unitReport.ReportFlags = 0;
if (rw[p].Government == gAnarchy)
return;
const TUn &un = (*rw[p].Un)[uix];
const TModel &model = (*rw[p].Model)[un.mix];
if (model.Kind == mkSettler)
unitReport.FoodSupport = SettlerFood[rw[p].Government];
else if (un.Flags & unConscripts)
unitReport.FoodSupport = 1;
if (rw[p].Government == gFundamentalism)
return;
if ((gTestFlags & tfImmImprove) == 0) {
unitReport.ProdSupport = (model.Flags & mdDoubleSupport) ? 2 : 1;
if (model.Kind == mkSpecial_TownGuard)
unitReport.ReportFlags |= urfAlwaysSupport;
}
if ((model.Flags & mdCivil) == 0) {
const int terrOwner = static_cast<int>(realMap[un.Loc] >> 27);
switch (rw[p].Government) {
case gRepublic:
case gFuture:
if (terrOwner != p && terrOwner < nPl && rw[p].Treaty[terrOwner] < trAlliance)
unitReport.ReportFlags |= urfDeployed;
break;
case gDemocracy:
if (terrOwner >= nPl || (terrOwner != p && rw[p].Treaty[terrOwner] < trAlliance))
unitReport.ReportFlags |= urfDeployed;
break;
}
}
}
void strongest(int loc, int &uix, int &strength, int &bonus, int &cnt)
{
const int defender = occupant[loc];
int cost = 0;
cnt = 0;
int det = -1;
const TUn *defUn = *rw[defender].Un;
const TModel *defModel = *rw[defender].Model;
for (int uix1 = 0; uix1 < rw[defender].nUn; ++uix1) {
const TUn &un = defUn[uix1];
const TModel &model = defModel[un.mix];
const int domain = (model.Kind == mkSpecial_Glider) ? dGround : model.Domain;
if (un.Loc != loc)
continue;
++cnt;
if (un.Master >= 0)
continue;
int testBonus;
if (domain < dSea) {
testBonus = Terrain[realMap[loc] & fTerrain].Defense;
if ((realMap[loc] & fTerImp) == tiFort)
testBonus += 4;
if (un.Flags & unFortified)
testBonus += 2;
if (model.Kind == mkSpecial_TownGuard && (realMap[loc] & fCity))
testBonus += 4;
} else {
testBonus = 4;
}
testBonus += un.Exp / ExpCost;
int testStrength = model.Defense * testBonus * un.Health;
if (domain == dAir && ((realMap[loc] & fCity) || (realMap[loc] & fTerImp) == tiBase))
testStrength = 0;
if (domain == dSea && static_cast<int>(realMap[loc] & fTerrain) >= fGrass)
testStrength >>= 1;
int testDet = testStrength;
if (model.Cap[mcStealth] > 0) {
// no detection bonus
} else if (model.Cap[mcSub] > 0) {
testDet += 1 << 28;
} else if (domain == dGround && model.Cap[mcFanatic] > 0 &&
!(rw[defender].Government == gRepublic || rw[defender].Government == gDemocracy ||
rw[defender].Government == gFuture)) {
testDet += 4 << 28; // fanatic ground units always defend
} else if (model.Flags & mdZOC) {
testDet += 3 << 28;
} else {
testDet += 2 << 28;
}
const int testCost = defModel[un.mix].Cost;
if (testDet > det || (testDet == det && testCost < cost)) {
uix = uix1;
strength = testStrength;
bonus = testBonus;
det = testDet;
cost = testCost;
}
}
}
int getTileInfo(int p, int cix, int loc, TTileInfo &info)
{
const int p0 = p;
std::uint32_t tile;
if (cix >= 0) {
tile = realMap[loc];
} else {
tile = (*rw[p].Map)[loc];
if ((tile & fTerrain) == fUNKNOWN)
return eNoPreq;
}
if (cix == -1 && usedByCity[loc] >= 0) { // search exploiting player and city
searchCity(usedByCity[loc], p, cix);
if (!(p == p0 || ((observeLevel[usedByCity[loc]] >> (2 * p0)) & 3) == lObserveSuper))
cix = -1;
}
if (cix == -1)
return eInvalid; // no city found here
const std::uint32_t special = ((tile & fSpecial) & resourceMask[p]) >> 5;
const TerrainInfo &terrain = Terrain[tile & fTerrain];
info.Food = terrain.FoodRes[special];
info.Prod = terrain.ProdRes[special];
info.Trade = terrain.TradeRes[special];
if (special > 0 && (tile & fTerrain) != fGrass && rw[p].NatBuilt[imSpacePort - nWonder] > 0) {
// GeoSat effect
info.Food = 2 * info.Food - terrain.FoodRes[0];
info.Prod = 2 * info.Prod - terrain.ProdRes[0];
info.Trade = 2 * info.Trade - terrain.TradeRes[0];
}
if ((tile & fTerImp) == tiIrrigation || (tile & fTerImp) == tiFarm || (tile & fCity) != 0)
info.Food += terrain.IrrEff; // irrigation effect
if ((tile & fTerImp) == tiMine)
info.Prod += terrain.MineEff; // mining effect
if ((tile & fRiver) != 0 && rw[p].Tech[adMapMaking] >= tsApplicable)
++info.Trade; // river effect
if ((tile & (fRoad | fRR)) != 0 && terrain.MoveCost == 1 && rw[p].Tech[adWheel] >= tsApplicable)
++info.Trade; // road effect
if ((tile & (fRR | fCity)) != 0 && rw[p].Tech[adRailroad] >= tsApplicable)
info.Prod += info.Prod >> 1; // railroad effect
info.ExplCity = -1;
if (cix >= 0 && p == p0)
info.ExplCity = cix;
if (cix >= 0) {
const TCity &city = (*rw[p].City)[cix];
if (static_cast<int>(tile & fTerrain) >= fGrass) {
if (((tile & fTerImp) == tiFarm || (tile & fCity) != 0) && city.Built[imSupermarket] > 0)
info.Food += info.Food >> 1; // farmland effect
if ((tile & (fRoad | fRR)) != 0 && terrain.MoveCost == 1 && city.Built[imHighways] > 0)
info.Trade += 1; // superhighway effect
} else {
if (city.Built[imHarbor] > 0)
++info.Food; // harbour effect
if (city.Built[imPlatform] > 0)
++info.Prod; // oil platform effect
if (gWonder[woLighthouse].EffectiveOwner == p)
++info.Prod;
}
}
// good government influence
if ((rw[p].Government == gRepublic || rw[p].Government == gDemocracy || rw[p].Government == gFuture) &&
info.Trade > 0)
++info.Trade;
if (rw[p].Government == gCommunism && info.Prod > 1)
++info.Prod;
if (rw[p].Government == gAnarchy || rw[p].Government == gDespotism) {
// bad government influence
if (info.Food > 3)
info.Food = 3;
if (info.Prod > 2)
info.Prod = 2;
if (info.Trade > 2)
info.Trade = 2;
}
if ((tile & (fTerrain | fPoll)) > fPoll) {
// pollution - decrease resources
info.Food -= info.Food >> 1;
info.Prod -= info.Prod >> 1;
info.Trade -= info.Trade >> 1;
}
if ((tile & fCity) != 0) {
info.Trade = 0;
} else if (cix >= 0 && (*rw[p].City)[cix].Built[imCourt] + (*rw[p].City)[cix].Built[imPalace] == 0) {
if ((*rw[p].City)[cix].Built[imTownHall] == 0)
info.Trade = 0;
else if (info.Trade > 3)
info.Trade = 3;
}
return eOK;
}
int techBaseCost(int nTech, int diff)
{
const double c0 = TechFormula_M[diff] * (nTech + 4) * std::exp((nTech + 4) / TechFormula_D[diff]);
if (c0 >= 0x10000000)
return 0x10000000;
return static_cast<int>(c0); // truncates toward zero, matching Pascal's Trunc()
}
void setModelFlags(TModel &m)
{
m.Flags = 0;
if (m.Domain == dGround && m.Kind != mkDiplomat)
m.Flags |= mdZOC;
if (m.Kind == mkDiplomat || m.Attack + m.Cap[mcBombs] == 0)
m.Flags |= mdCivil;
if (m.Cap[mcOver] > 0 || (m.Domain == dSea && m.Weight >= 6))
m.Flags |= mdDoubleSupport;
}
void checkSpecialModels(int p, int pre)
{
TPlayerContext &player = rw[p];
TModel *model = *player.Model;
for (int i = 0; i < nSpecialModel; ++i) {
if (SpecialModelPreq[i] != pre || player.nModel >= nMmax)
continue;
bool hasAlready = false;
for (int mix1 = 0; mix1 < player.nModel; ++mix1)
if (model[mix1].Kind == SpecialModel[i].Kind && model[mix1].Attack == SpecialModel[i].Attack &&
model[mix1].Speed == SpecialModel[i].Speed)
hasAlready = true;
if (!hasAlready) {
model[player.nModel] = SpecialModel[i];
setModelFlags(model[player.nModel]);
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);
if (newModel.Kind == mkSpecial_Boat && serverVersion[p] < 0x000EF0)
newModel.Speed = 350; // old longboat
++player.nModel;
}
}
}
void checkExpiration(int wonder)
{
// GWonder[wonder].EffectiveOwner must be set before!
if (Imp[wonder].Expiration >= 0 && gWonder[woEiffel].EffectiveOwner != gWonder[wonder].EffectiveOwner) {
for (int p = 0; p < nPl; ++p) {
// check if already expired
if ((gAlive & (1 << p)) && rw[p].Tech[Imp[wonder].Expiration] >= tsApplicable) {
gWonder[wonder].EffectiveOwner = -1;
if (wonder == woPyramids)
freeSlaves();
}
}
}
}
void createUnit(int p, int mix)
{
TPlayerContext &player = rw[p];
TUn *un = *player.Un;
TModel *model = *player.Model;
un[player.nUn].mix = static_cast<std::int16_t>(mix);
TUn &newUn = un[player.nUn];
newUn.ID = static_cast<std::uint16_t>(unBuilt[p]);
++unBuilt[p];
newUn.Status = 0;
newUn.SavedStatus = 0;
++model[mix].Built;
newUn.Home = -1;
newUn.Health = 100;
newUn.Flags = 0;
newUn.Movement = 0;
if (model[mix].Domain == dAir) {
newUn.Fuel = static_cast<std::int8_t>(model[mix].Cap[mcFuel]);
newUn.Flags |= unBombsLoaded;
}
newUn.Job = jNone;
newUn.Exp = static_cast<std::uint8_t>(ExpCost >> 1);
newUn.TroopLoad = 0;
newUn.AirLoad = 0;
newUn.Master = -1;
++player.nUn;
}
void placeUnit(int p, int uix)
{
TPlayerContext &player = rw[p];
TUn &un = (*player.Un)[uix];
occupant[un.Loc] = static_cast<std::int8_t>(p);
if ((*player.Model)[un.mix].Flags & mdZOC)
zocMap[un.Loc] = 1;
}
void freeUnit(int p, int uix)
{
// loc or master should be set after call
// implementation is critical for loading performance, change carefully
TPlayerContext &player = rw[p];
TUn &un = (*player.Un)[uix];
un.Job = jNone;
un.Flags &= ~static_cast<std::uint32_t>(unFortified | unMountainDelay);
const int loc0 = un.Loc;
if (occupant[loc0] >= 0) {
assert(occupant[loc0] == p);
bool occ = false;
bool zoc = false;
const TUn *allUn = *player.Un;
const TModel *model = *player.Model;
for (int uix1 = 0; uix1 < player.nUn; ++uix1) {
if (allUn[uix1].Loc == loc0 && allUn[uix1].Master < 0 && uix1 != uix) {
occ = true;
if (model[allUn[uix1].mix].Flags & mdZOC) {
zoc = true;
break;
}
}
}
if (!occ)
occupant[loc0] = -1;
if (!zoc)
zocMap[loc0] = 0;
}
}
void removeUnit(int p, int uix, int enemy)
{
// use enemy only from inside sMoveUnit if attack
TPlayerContext &player = rw[p];
TUn &un = (*player.Un)[uix];
assert(un.Loc >= 0 || (*player.Model)[un.mix].Kind == mkDiplomat);
// already freed when spy mission
if (un.Loc >= 0)
freeUnit(p, uix);
if (un.Master >= 0) {
TUn &master = (*player.Un)[un.Master];
if ((*player.Model)[un.mix].Domain == dAir)
--master.AirLoad;
else
--master.TroopLoad;
}
if (un.TroopLoad > 0 || un.AirLoad > 0) {
TUn *allUn = *player.Un;
for (int uix1 = 0; uix1 < player.nUn; ++uix1) {
// unit mastered by removed unit -- remove too
if (allUn[uix1].Loc >= 0 && allUn[uix1].Master == uix) {
allUn[uix1].Loc = -1;
if (enemy >= 0)
countLost(p, allUn[uix1].mix, enemy);
}
}
}
un.Loc = -1;
if (enemy >= 0)
countLost(p, un.mix, enemy);
}
void removeUnitUpdateMap(int p, int uix)
{
const int loc0 = (*rw[p].Un)[uix].Loc;
removeUnit(p, uix);
if (mode > moLoading_Fast)
updateUnitMap(loc0);
}
void introduceEnemy(int p1, int p2)
{
rw[p1].Treaty[p2] = trNone;
rw[p2].Treaty[p1] = trNone;
}
int techCost(int p)
{
TPlayerContext &player = rw[p];
int result = techBaseCost(nTech[p], difficulty[p]);
if (player.ResearchTech >= 0) {
if (player.ResearchTech == adMilitary || player.Tech[player.ResearchTech] == tsSeen)
result >>= 1;
else if (IsFutureTech(player.ResearchTech))
result *= (player.Government == gFuture) ? 2 : 4;
}
return result;
}
void giveCivilReport(int p, int pAbout)
{
TEnemyReport &report = *rw[p].EnemyReport[pAbout];
report.TurnOfCivilReport = lastValidStat[pAbout];
std::memcpy(report.Treaty, rw[pAbout].Treaty, sizeof(report.Treaty));
report.Government = rw[pAbout].Government;
report.Money = rw[pAbout].Money;
report.ResearchTech = rw[pAbout].ResearchTech;
report.ResearchDone = rw[pAbout].Research * 100 / techCost(pAbout);
if (report.ResearchDone > 100)
report.ResearchDone = 100;
std::memcpy(report.Tech, rw[pAbout].Tech, nAdv);
}
void giveMilReport(int p, int pAbout)
{
TEnemyReport &report = *rw[p].EnemyReport[pAbout];
report.TurnOfMilReport = lastValidStat[pAbout];
report.nModelCounted = rw[pAbout].nModel;
for (int mix = 0; mix < rw[pAbout].nModel; ++mix) {
tellAboutModel(p, pAbout, mix);
report.UnCount[mix] = 0;
}
const TUn *un = *rw[pAbout].Un;
for (int uix = 0; uix < rw[pAbout].nUn; ++uix)
if (un[uix].Loc >= 0)
++report.UnCount[un[uix].mix];
}
void intServer(int command, int player, int subject, const void *data)
{
if (mode == moPlaying)
cl.put(command, player, subject, data);
if (command == sIntTellAboutNation) {
introduceEnemy(player, subject);
} else if (command >= sIntTellAboutModel && command <= sIntTellAboutModel + ((nPl - 1) << 4)) {
const int p1 = (command - sIntTellAboutModel) >> 4; // told player
assert(player >= 0 && player < nPl);
assert(subject >= 0 && subject < rw[player].nModel);
TPlayerContext &toldPlayer = rw[p1];
MakeModelInfo(player, subject, (*rw[player].Model)[subject], (*toldPlayer.EnemyModel)[toldPlayer.nEnemyModel]);
rwemix[p1][player][subject] = static_cast<std::int16_t>(toldPlayer.nEnemyModel);
++toldPlayer.nEnemyModel;
assert(toldPlayer.nEnemyModel < nemmax);
}
// 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.
}
void tellAboutModel(int p, int taOwner, int tamix)
{
if (p == taOwner || mode < moPlaying)
return;
TPlayerContext &player = rw[p];
int i = 0;
const TModelInfo *enemyModel = *player.EnemyModel;
while (i < player.nEnemyModel && (enemyModel[i].Owner != taOwner || enemyModel[i].mix != tamix))
++i;
if (i == player.nEnemyModel)
intServer(sIntTellAboutModel + (p << 4), taOwner, tamix, nullptr);
}
int emixSafe(int p, int taOwner, int tamix)
{
int result = rwemix[p][taOwner][tamix];
if (result < 0) { // sIntTellAboutModel comes too late
assert(mode == moMovie);
result = 0xFFFF;
}
return result;
}
int getUnitStack(int p, int loc)
{
int result = 0;
if (occupant[loc] < 0)
return result;
const int owner = occupant[loc];
TPlayerContext &ownerPlayer = rw[owner];
TPlayerContext &player = rw[p];
TUnitInfo *enemyUn = *player.EnemyUn;
for (int uix = 0; uix < ownerPlayer.nUn; ++uix) {
const TUn &un = (*ownerPlayer.Un)[uix];
if (un.Loc == loc) {
MakeUnitInfo(owner, un, enemyUn[player.nEnemyUn + result]);
tellAboutModel(p, owner, un.mix);
enemyUn[player.nEnemyUn + result].emix = static_cast<std::uint16_t>(rwemix[p][owner][un.mix]);
++result;
}
}
return result;
}
void makeCityInfo(int p, int cix, TCityInfo &ci)
{
assert(p >= 0 && p < nPl);
assert(cix >= 0 && cix < rw[p].nCity);
const TCity &city = (*rw[p].City)[cix];
ci.Loc = city.Loc;
ci.ID = city.ID;
ci.Owner = static_cast<std::uint16_t>(p);
ci.Size = city.Size;
ci.Flags = 0;
if (city.Built[imPalace] > 0)
ci.Flags += ciCapital;
if (city.Built[imWalls] > 0 || continent[city.Loc] == grWallContinent[p])
ci.Flags += ciWalled;
if (city.Built[imCoastalFort] > 0)
ci.Flags += ciCoastalFort;
if (city.Built[imMissileBat] > 0)
ci.Flags += ciMissileBat;
if (city.Built[imBunker] > 0)
ci.Flags += ciBunker;
if (city.Built[imSpacePort] > 0)
ci.Flags += ciSpacePort;
}
bool discoverTile(int loc, int p, int pTell, int level, bool enableContact, int euix)
{
auto setContact = [&](int p1, int p2) {
if (mode < moPlaying || p1 == p2 || rw[p1].Treaty[p2] > trNoContact)
return;
intServer(sIntTellAboutNation, p1, p2, nullptr);
};
bool result = false;
TPlayerContext &player = rw[pTell];
std::uint32_t *map = *player.Map;
TUnitInfo *enemyUn = *player.EnemyUn;
TCityInfo *enemyCity = *player.EnemyCity;
std::int8_t *territory = *player.Territory;
std::int16_t *mapObservedLast = *player.MapObservedLast;
const std::uint32_t tile = realMap[loc] & resourceMask[pTell];
std::uint32_t addFlags;
if (mode == moLoading_Fast) {
addFlags = 0; // don't discover units
} else {
addFlags = (map[loc] & fInEnemyZoC) | fObserved; // always preserve this flag!
if (level == lObserveSuper)
addFlags |= fSpiedOut;
if (grWallContinent[pTell] >= 0 && continent[loc] == grWallContinent[pTell])
addFlags |= fGrWall;
if (mode == moPlaying &&
(tile & (static_cast<std::uint32_t>(nPl) << 27)) != (static_cast<std::uint32_t>(nPl) << 27) &&
pTell == p) {
// set fPeace flag?
const int terrOwner = static_cast<int>(tile >> 27);
if (terrOwner != pTell) {
const int terrOwnerTreaty = player.Treaty[terrOwner];
if ((1 << terrOwnerTreaty) & ((1 << trPeace) | (1 << trFriendlyContact)))
addFlags |= fPeace;
}
}
if (occupant[loc] >= 0) {
if (occupant[loc] == pTell) {
addFlags |= (fOwned | fUnit);
if (zocMap[loc] > 0)
addFlags |= fOwnZoCUnit;
// level := lObserveSuper; // always see own units
} else if (map[loc] & fUnit) {
addFlags |= fUnit;
} else {
int uix, strength, bonus, cnt;
strongest(loc, uix, strength, bonus, cnt);
const TUn &unx = (*rw[occupant[loc]].Un)[uix];
const TModel &mox = (*rw[occupant[loc]].Model)[unx.mix];
assert((zocMap[loc] != 0) == ((mox.Flags & mdZOC) != 0));
int minLevel;
if (mox.Cap[mcStealth] > 0 && (tile & fCity) == 0 && (tile & fTerImp) != tiBase)
minLevel = lObserveSuper;
else if (mox.Cap[mcSub] > 0 && static_cast<int>(tile & fTerrain) < fGrass)
minLevel = lObserveAll;
else
minLevel = lObserveUnhidden;
if (level >= minLevel) {
addFlags |= fUnit;
int unitIndex;
if (euix >= 0) {
unitIndex = euix;
} else {
unitIndex = player.nEnemyUn;
++player.nEnemyUn;
assert(player.nEnemyUn < neumax);
}
MakeUnitInfo(occupant[loc], unx, enemyUn[unitIndex]);
if (cnt > 1)
enemyUn[unitIndex].Flags |= unMulti;
if ((mox.Flags & mdZOC) && pTell == p && player.Treaty[occupant[loc]] < trAlliance) {
// set fInEnemyZoC flags of surrounding tiles
Vicinity8Loc adjacent;
v8ToLoc(loc, adjacent);
for (int v8 = 0; v8 < 8; ++v8) {
const int loc1 = adjacent[v8];
if (loc1 >= 0 && loc1 < mapSize)
map[loc1] |= fInEnemyZoC;
}
}
if (enableContact && mox.Domain == dGround)
setContact(pTell, occupant[loc]);
if (mode >= moMovie) {
tellAboutModel(pTell, occupant[loc], unx.mix);
enemyUn[unitIndex].emix = static_cast<std::uint16_t>(emixSafe(pTell, occupant[loc], unx.mix));
}
// level := lObserveSuper; // don't discover unit twice
if (pTell == p && ((tile & fCity) == 0 || ((1 << pTell) & gAI)))
result = true;
} else {
addFlags |= map[loc] & (fStealthUnit | fHiddenUnit);
}
}
}
} // if mode > moLoading_Fast
if (tile & fCity) {
if ((observeLevel[loc] >> (2 * pTell)) & 3) {
addFlags |= map[loc] & fOwned;
} else {
const int pFoundCity = static_cast<int>(tile >> 27);
if (pFoundCity == pTell) {
addFlags |= fOwned;
} else {
if (enableContact)
setContact(pTell, pFoundCity);
int cixFoundCity = rw[pFoundCity].nCity - 1;
while (cixFoundCity >= 0 && (*rw[pFoundCity].City)[cixFoundCity].Loc != loc)
--cixFoundCity;
assert(cixFoundCity >= 0);
int i = 0;
while (i < player.nEnemyCity && enemyCity[i].Loc != loc)
++i;
if (i == player.nEnemyCity) {
++player.nEnemyCity;
assert(player.nEnemyCity < necmax);
enemyCity[i].Status = 0;
enemyCity[i].SavedStatus = 0;
if (pTell == p)
result = true;
}
makeCityInfo(pFoundCity, cixFoundCity, enemyCity[i]);
}
}
} else if (map[loc] & fCity) { // remove enemy city
for (int cix = 0; cix < player.nEnemyCity; ++cix)
if (enemyCity[cix].Loc == loc)
enemyCity[cix].Loc = -1;
}
if ((map[loc] & fTerrain) == fUNKNOWN)
++discovered[pTell];
if (euix >= -1) {
map[loc] = (map[loc] & ~static_cast<std::uint32_t>(fUnit | fCity | fOwned | fOwnZoCUnit)) |
((tile & 0x07FFFFFFu | addFlags) & (fUnit | fCity | fOwned | fOwnZoCUnit));
} else {
map[loc] = (tile & 0x07FFFFFFu) | addFlags;
if ((tile & 0x78000000u) == 0x78000000u)
territory[loc] = -1;
else
territory[loc] = static_cast<std::int8_t>(tile >> 27);
mapObservedLast[loc] = static_cast<std::int16_t>(gTurn);
}
observeLevel[loc] = (observeLevel[loc] & ~(3u << (2 * pTell))) | (static_cast<std::uint32_t>(level) << (2 * pTell));
return result;
}
bool discover9(int loc, int p, int level, bool tellAllied, bool enableContact)
{
assert(mode > moLoading_Fast || rw[p].nEnemyUn == 0);
bool result = false;
Vicinity8Loc radius;
v8ToLoc(loc, radius);
for (int v9 = 0; v9 <= 8; ++v9) {
const int loc1 = (v9 == 8) ? loc : radius[v9];
if (loc1 < 0 || loc1 >= mapSize)
continue;
if (tellAllied) {
for (int pTell = 0; pTell < nPl; ++pTell) {
if (pTell == p || (((1 << pTell) & gAlive) && rw[p].Treaty[pTell] == trAlliance)) {
const int oldLevel = (observeLevel[loc1] >> (2 * pTell)) & 3;
if (level > oldLevel)
result = discoverTile(loc1, p, pTell, level, enableContact) || result;
}
}
} else {
const int oldLevel = (observeLevel[loc1] >> (2 * p)) & 3;
if (level > oldLevel)
result = discoverTile(loc1, p, p, level, enableContact) || result;
}
}
return result;
}
bool discover21(int loc, int p, int adjacentLevel, bool tellAllied, bool enableContact)
{
assert(mode > moLoading_Fast || rw[p].nEnemyUn == 0);
bool result = false;
std::uint32_t adjacentFlags = 0x00267620u >> 1;
Vicinity21Loc radius;
v21ToLoc(loc, radius);
for (int v21 = 1; v21 <= 26; ++v21) {
const int loc1 = radius[v21];
if (loc1 >= 0 && loc1 < mapSize) {
const int level = (adjacentFlags & 1) ? adjacentLevel : lObserveUnhidden;
if (tellAllied) {
for (int pTell = 0; pTell < nPl; ++pTell) {
if (pTell == p || (((1 << pTell) & gAlive) && rw[p].Treaty[pTell] == trAlliance)) {
const int oldLevel = (observeLevel[loc1] >> (2 * pTell)) & 3;
if (level > oldLevel)
result = discoverTile(loc1, p, pTell, level, enableContact) || result;
}
}
} else {
const int oldLevel = (observeLevel[loc1] >> (2 * p)) & 3;
if (level > oldLevel)
result = discoverTile(loc1, p, p, level, enableContact) || result;
}
}
adjacentFlags >>= 1;
}
return result;
}
void discoverAll(int p, int level)
{
// player p discovers complete playground (for supervisor)
assert(mode > moLoading_Fast || rw[p].nEnemyUn == 0);
for (int loc = 0; loc < mapSize; ++loc) {
const int oldLevel = (observeLevel[loc] >> (2 * p)) & 3;
if (level > oldLevel)
discoverTile(loc, p, p, level, false);
}
}
void discoverViewAreas(int p)
{
// discover unit and city view areas
for (int pTell = 0; pTell < nPl; ++pTell) {
if (pTell != p && rw[p].Treaty[pTell] != trAlliance)
continue;
TPlayerContext &tellPlayer = rw[pTell];
const TUn *un = *tellPlayer.Un;
for (int uix = 0; uix < tellPlayer.nUn; ++uix) {
if (un[uix].Loc >= 0 && un[uix].Master < 0 && (realMap[un[uix].Loc] & fCity) == 0) {
const TModel &model = (*tellPlayer.Model)[un[uix].mix];
if (model.Kind == mkDiplomat || model.Cap[mcSpy] > 0)
discover21(un[uix].Loc, p, lObserveSuper, false, true);
else if (model.Cap[mcRadar] + model.Cap[mcCarrier] > 0 || model.Domain == dAir)
discover21(un[uix].Loc, p, lObserveAll, false, false);
else if (static_cast<int>(realMap[un[uix].Loc] & fTerrain) == fMountains ||
(realMap[un[uix].Loc] & fTerImp) == tiFort || (realMap[un[uix].Loc] & fTerImp) == tiBase ||
model.Cap[mcAcademy] > 0)
discover21(un[uix].Loc, p, lObserveUnhidden, false, model.Domain == dGround);
else
discover9(un[uix].Loc, p, lObserveUnhidden, false, model.Domain == dGround);
}
}
const TCity *city = *tellPlayer.City;
for (int cix = 0; cix < tellPlayer.nCity; ++cix)
if (city[cix].Loc >= 0)
discover21(city[cix].Loc, p, lObserveUnhidden, false, true);
TCityInfo *enemyCity = *tellPlayer.EnemyCity;
for (int ecix = 0; ecix < tellPlayer.nEnemyCity; ++ecix) {
// players know territory, so no use in hiding city owner
const int loc = enemyCity[ecix].Loc;
if (loc >= 0) {
const int realOwner = static_cast<int>((realMap[loc] >> 27) & 0xF);
if (realOwner < nPl) {
enemyCity[ecix].Owner = static_cast<std::uint16_t>(realOwner);
} else {
enemyCity[ecix].Loc = -1;
(*tellPlayer.Map)[loc] &= ~static_cast<std::uint32_t>(fCity);
}
}
}
}
}
void updateUnitMap(int loc, bool cityChange)
{
// update maps and enemy units of all players after unit change
if (mode == moLoading_Fast && !cityChange)
return;
for (int p = 0; p < nPl; ++p) {
if (!((1 << p) & (gAlive | gWatching)))
continue;
const int oldLevel = (observeLevel[loc] >> (2 * p)) & 3;
if (oldLevel <= lNoObserve)
continue;
TPlayerContext &player = rw[p];
std::uint32_t *map = *player.Map;
TUnitInfo *enemyUn = *player.EnemyUn;
int euix;
if ((map[loc] & (fUnit | fOwned)) == fUnit) {
// replace unit located here in EnemyUn
// do not just set loc:=-1 because total number would be unlimited
euix = player.nEnemyUn - 1;
while (euix >= 0) {
if (enemyUn[euix].Loc == loc) {
enemyUn[euix].Loc = -1;
break;
}
--euix;
}
map[loc] &= ~static_cast<std::uint32_t>(fUnit);
} else {
// look for empty slot in EnemyUn
euix = player.nEnemyUn - 1;
while (euix >= 0 && enemyUn[euix].Loc >= 0)
--euix;
}
if (occupant[loc] < 0 && !cityChange) {
// calling DiscoverTile not necessary, only clear map flags
std::uint32_t clearFlags = fUnit | fHiddenUnit | fStealthUnit | fOwnZoCUnit;
if ((realMap[loc] & fCity) == 0)
clearFlags |= fOwned;
map[loc] &= ~clearFlags;
} else if (occupant[loc] != p || cityChange) {
// city or enemy unit update necessary, call DiscoverTile
observeLevel[loc] &= ~(3u << (2 * p));
discoverTile(loc, p, p, oldLevel, false, euix);
} else {
// calling DiscoverTile not necessary, only set map flags
std::uint32_t clearFlags = 0;
std::uint32_t addFlags = fUnit | fOwned;
if (zocMap[loc] > 0)
addFlags |= fOwnZoCUnit;
else
clearFlags |= fOwnZoCUnit;
map[loc] = (map[loc] & ~clearFlags) | addFlags;
}
}
}
} // namespace Database } // namespace Database
} // namespace Cevo } // namespace Cevo