1475 lines
61 KiB
C++
1475 lines
61 KiB
C++
// This file is part of c-evo-evo.
|
|
//
|
|
// This software is governed by the CeCILL license under French law and
|
|
// abiding by the rules of distribution of free software. You can use,
|
|
// modify and/or redistribute the software under the terms of the CeCILL
|
|
// license as circulated by CEA, CNRS and INRIA at the following URL
|
|
// "http://www.cecill.info".
|
|
//
|
|
// As a counterpart to the access to the source code and rights to copy,
|
|
// modify and redistribute granted by the license, users are provided only
|
|
// with a limited warranty and the software's author, the holder of the
|
|
// economic rights, and the successive licensors have only limited
|
|
// liability.
|
|
//
|
|
// In this respect, the user's attention is drawn to the risks associated
|
|
// with loading, using, modifying and/or developing or reproducing the
|
|
// software by the user in light of its specific status of free software,
|
|
// that may mean that it is complicated to manipulate, and that also
|
|
// therefore means that it is reserved for developers and experienced
|
|
// professionals having in-depth computer knowledge. Users are therefore
|
|
// encouraged to load and test the software's suitability as regards their
|
|
// requirements in conditions enabling the security of their systems and/or
|
|
// data to be ensured and, more generally, to use and operate it in the
|
|
// same conditions as regards security.
|
|
//
|
|
// The fact that you are presently reading this means that you have had
|
|
// knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
#include "Cevo/CityProcessing.h"
|
|
#include "Cevo/Database.h"
|
|
|
|
#include <array>
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace Cevo {
|
|
namespace CityProcessing {
|
|
|
|
void getCityAreaInfo(int p, int loc, TCityAreaInfo &cityAreaInfo)
|
|
{
|
|
// Original also has `{$IFOPT O-}Assert(1 shl P and InvalidTreatyMap = 0)`
|
|
// here - a debug-only overflow-checking assertion with no effect on
|
|
// release behavior, not ported.
|
|
Database::Vicinity21Loc radius;
|
|
Database::v21ToLoc(loc, radius);
|
|
for (int v21 = 0; v21 <= 26; ++v21) {
|
|
const int loc1 = radius[v21];
|
|
if (loc1 < 0 || loc1 >= Database::mapSize) {
|
|
cityAreaInfo.Available[v21] = faInvalid;
|
|
} else {
|
|
const int p1 = static_cast<int>(Database::realMap[loc1] >> 27);
|
|
if (p1 < nPl && p1 != p && Database::rw[p].Treaty[p1] >= trPeace)
|
|
cityAreaInfo.Available[v21] = faTreaty;
|
|
else if (Database::zocMap[loc1] > 0 && Database::occupant[loc1] != p &&
|
|
Database::rw[p].Treaty[Database::occupant[loc1]] < trAlliance)
|
|
cityAreaInfo.Available[v21] = faSiege;
|
|
else if (Database::usedByCity[loc1] != -1 && Database::usedByCity[loc1] != loc)
|
|
cityAreaInfo.Available[v21] = faNotAvailable;
|
|
else
|
|
cityAreaInfo.Available[v21] = faAvailable;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool canCityGrow(int p, int cix)
|
|
{
|
|
const TCity &city = (*Database::rw[p].City)[cix];
|
|
return city.Size < MaxCitySize &&
|
|
(city.Size < NeedAqueductSize || (city.Built[imAqueduct] == 1 && city.Size < NeedSewerSize) ||
|
|
city.Built[imSewer] == 1);
|
|
}
|
|
|
|
namespace {
|
|
|
|
// Private (CityProcessing.pas implementation-section) support types/state/
|
|
// helpers feeding getCityReport()/getCityReportNew() - not part of the
|
|
// original's interface section, so not exposed in CityProcessing.h either.
|
|
|
|
struct ProdProcessing {
|
|
int ProdBonus;
|
|
int PollBonus;
|
|
int FutProdBonus;
|
|
int PollThreshold;
|
|
};
|
|
|
|
struct TradeProcessing {
|
|
int TaxBonus;
|
|
int LuxBonus;
|
|
int ScienceBonus;
|
|
int FutResBonus;
|
|
int ScienceDoubling;
|
|
int HappyBase;
|
|
double RelCorr;
|
|
bool FlexibleLuxury;
|
|
};
|
|
|
|
struct CityReportEx {
|
|
int BaseHappiness;
|
|
int BaseControl;
|
|
int Material;
|
|
ProdProcessing prodProcessing;
|
|
TradeProcessing tradeProcessing;
|
|
};
|
|
|
|
// Equivalent of CityProcessing.pas's private `MaxDist` - set by initGame().
|
|
int maxDist = 0;
|
|
|
|
void determineCityProdProcessing(int p, int cix, ProdProcessing &prodProcessing)
|
|
{
|
|
const TCity &city = (*Database::rw[p].City)[cix];
|
|
prodProcessing.ProdBonus = 0;
|
|
prodProcessing.PollBonus = 0;
|
|
if (city.Built[imFactory] == 1)
|
|
++prodProcessing.ProdBonus;
|
|
if (city.Built[imMfgPlant] == 1)
|
|
++prodProcessing.ProdBonus;
|
|
if (city.Built[imPower] == 1 || city.Built[imHydro] == 1 || city.Built[imNuclear] == 1 ||
|
|
Database::gWonder[woHoover].EffectiveOwner == p)
|
|
prodProcessing.ProdBonus *= 2;
|
|
if (city.Built[imFactory] == 1)
|
|
++prodProcessing.PollBonus;
|
|
if (city.Built[imMfgPlant] == 1)
|
|
++prodProcessing.PollBonus;
|
|
if (city.Built[imFactory] + city.Built[imMfgPlant] > 0) {
|
|
if (city.Built[imHydro] > 0 || Database::gWonder[woHoover].EffectiveOwner == p)
|
|
--prodProcessing.PollBonus;
|
|
else if (city.Built[imNuclear] == 0 && city.Built[imPower] == 1)
|
|
++prodProcessing.PollBonus;
|
|
}
|
|
if (Database::rw[p].Government <= gDespotism || city.Built[imRecycling] == 1)
|
|
prodProcessing.PollBonus = -2; // no pollution
|
|
prodProcessing.PollThreshold = city.Size;
|
|
prodProcessing.FutProdBonus = 0;
|
|
if (Database::rw[p].Tech[futProductionTechnology] > 0) { // future tech benefits
|
|
if (city.Built[imFactory] == 1)
|
|
prodProcessing.FutProdBonus += FactoryFutureBonus * Database::rw[p].Tech[futProductionTechnology];
|
|
if (city.Built[imMfgPlant] == 1)
|
|
prodProcessing.FutProdBonus += MfgPlantFutureBonus * Database::rw[p].Tech[futProductionTechnology];
|
|
}
|
|
}
|
|
|
|
void boostProd(int baseProd, const ProdProcessing &prodProcessing, int &prod, int &poll)
|
|
{
|
|
poll = (baseProd * (2 + prodProcessing.PollBonus)) >> 1;
|
|
if (poll <= prodProcessing.PollThreshold)
|
|
poll = 0;
|
|
else
|
|
poll -= prodProcessing.PollThreshold;
|
|
if (prodProcessing.FutProdBonus > 0)
|
|
prod = baseProd * (100 + prodProcessing.ProdBonus * 50 + prodProcessing.FutProdBonus) / 100;
|
|
else
|
|
prod = (baseProd * (2 + prodProcessing.ProdBonus)) >> 1;
|
|
}
|
|
|
|
void determineCityTradeProcessing(int p, int cix, int happinessBeforeLux, TradeProcessing &tradeProcessing)
|
|
{
|
|
const TCity &city = (*Database::rw[p].City)[cix];
|
|
tradeProcessing.TaxBonus = 0;
|
|
tradeProcessing.ScienceBonus = 0;
|
|
if (city.Built[imMarket] == 1)
|
|
tradeProcessing.TaxBonus += 2;
|
|
if (city.Built[imBank] == 1) {
|
|
tradeProcessing.TaxBonus += 3;
|
|
if (Database::rw[p].NatBuilt[imStockEx - nWonder] == 1)
|
|
tradeProcessing.TaxBonus += 3;
|
|
}
|
|
tradeProcessing.LuxBonus = tradeProcessing.TaxBonus;
|
|
if (city.Built[imLibrary] == 1)
|
|
tradeProcessing.ScienceBonus += 2;
|
|
if (city.Built[imUniversity] == 1)
|
|
tradeProcessing.ScienceBonus += 3;
|
|
if (city.Built[imResLab] == 1)
|
|
tradeProcessing.ScienceBonus += 3;
|
|
tradeProcessing.ScienceDoubling = 0;
|
|
if (city.Built[imNatObs] > 0)
|
|
++tradeProcessing.ScienceDoubling;
|
|
if (Database::rw[p].Government == gFundamentalism)
|
|
--tradeProcessing.ScienceDoubling;
|
|
else if (Database::gWonder[woNewton].EffectiveOwner == p && Database::rw[p].Government == gMonarchy)
|
|
++tradeProcessing.ScienceDoubling;
|
|
|
|
const bool newLiberty = Database::serverVersion[p] >= 0x0100F1 && Database::gWonder[woLiberty].EffectiveOwner == p;
|
|
const bool oldMich = Database::serverVersion[p] < 0x0100F1 && Database::gWonder[woMich].EffectiveOwner == p;
|
|
tradeProcessing.FlexibleLuxury = (newLiberty || oldMich) && Database::rw[p].Government != gAnarchy;
|
|
|
|
tradeProcessing.FutResBonus = 0;
|
|
if (Database::rw[p].Tech[futResearchTechnology] > 0) { // future tech benefits
|
|
if (city.Built[imUniversity] == 1)
|
|
tradeProcessing.FutResBonus += UniversityFutureBonus * Database::rw[p].Tech[futResearchTechnology];
|
|
if (city.Built[imResLab] == 1)
|
|
tradeProcessing.FutResBonus += ResLabFutureBonus * Database::rw[p].Tech[futResearchTechnology];
|
|
}
|
|
|
|
if (Database::rw[p].NatBuilt[imPalace - nWonder] > 0 || Database::serverVersion[p] < 0x010000) {
|
|
// calculate corruption
|
|
int dist = maxDist;
|
|
for (int i = 0; i < Database::rw[p].nCity; ++i) {
|
|
const TCity &otherCity = (*Database::rw[p].City)[i];
|
|
if (otherCity.Loc >= 0 && otherCity.Built[imPalace] == 1)
|
|
dist = Database::distance(city.Loc, otherCity.Loc);
|
|
}
|
|
if (dist == 0 || CorrLevel[Database::rw[p].Government] == 0) {
|
|
tradeProcessing.RelCorr = 0.0;
|
|
} else {
|
|
tradeProcessing.RelCorr = static_cast<double>(dist) / maxDist;
|
|
if (CorrLevel[Database::rw[p].Government] > 1)
|
|
tradeProcessing.RelCorr = std::exp(std::log(tradeProcessing.RelCorr) / CorrLevel[Database::rw[p].Government]);
|
|
if (city.Built[imCourt] == 1)
|
|
tradeProcessing.RelCorr /= 2;
|
|
}
|
|
} else if (city.Built[imCourt] == 1) {
|
|
tradeProcessing.RelCorr = 0.5;
|
|
} else {
|
|
tradeProcessing.RelCorr = 1.0;
|
|
}
|
|
tradeProcessing.HappyBase = city.Size + happinessBeforeLux;
|
|
}
|
|
|
|
void splitTrade(int trade, int taxRate, int luxRate, int working, const TradeProcessing &tradeProcessing,
|
|
int &corruption, int &tax, int &lux, int &science)
|
|
{
|
|
corruption = static_cast<int>(trade * tradeProcessing.RelCorr);
|
|
tax = (taxRate * (trade - corruption) + 50) / 100;
|
|
if (tradeProcessing.FlexibleLuxury) {
|
|
const int plus = working * 2 - tradeProcessing.HappyBase; // required additional luxury
|
|
if (plus > 0) {
|
|
lux = (4 * plus + 3 + tradeProcessing.LuxBonus) / (4 + tradeProcessing.LuxBonus);
|
|
if (lux > trade - corruption)
|
|
lux = trade - corruption;
|
|
if (tax > trade - corruption - lux)
|
|
tax = trade - corruption - lux;
|
|
} else {
|
|
lux = 0;
|
|
}
|
|
} else if (luxRate == 0 || taxRate == 100) {
|
|
lux = 0;
|
|
} else {
|
|
lux = (luxRate * (trade - corruption) + 49) / 100;
|
|
}
|
|
science = trade - corruption - lux - tax;
|
|
tax = (tax * (4 + tradeProcessing.TaxBonus)) >> 2;
|
|
lux = (lux * (4 + tradeProcessing.LuxBonus)) >> 2;
|
|
if (tradeProcessing.FutResBonus > 0)
|
|
science = science * (100 + tradeProcessing.ScienceBonus * 25 + tradeProcessing.FutResBonus) / 100;
|
|
else
|
|
science = (science * (4 + tradeProcessing.ScienceBonus)) >> 2;
|
|
science = (science << 2) >> (2 - tradeProcessing.ScienceDoubling);
|
|
}
|
|
|
|
int getProjectCost(int p, int cix)
|
|
{
|
|
const TCity &city = (*Database::rw[p].City)[cix];
|
|
int result;
|
|
if ((city.Project & cpImp) == 0) {
|
|
const TModel &model = (*Database::rw[p].Model)[city.Project & cpIndex];
|
|
result = model.Cost; // unit project
|
|
if (city.Project & cpConscripts) {
|
|
const int mCost = model.MCost;
|
|
result -= 3 * mCost;
|
|
if (result <= 0)
|
|
result = mCost;
|
|
} else if (model.Cap[mcLine] > 0) {
|
|
if ((city.Project0 & (~cpAuto | cpRepeat)) == ((city.Project & ~cpAuto) | cpRepeat))
|
|
result >>= 1;
|
|
else
|
|
result *= 2;
|
|
}
|
|
} else { // improvement project
|
|
result = Imp[city.Project & cpIndex].Cost;
|
|
if ((city.Project & cpIndex) < nWonder && Database::gWonder[woColossus].EffectiveOwner == p)
|
|
result = result * ColossusEffect / 100;
|
|
}
|
|
result = result * BuildCostMod[Database::difficulty[p]] / 12;
|
|
return result;
|
|
}
|
|
|
|
int getSmallCityReport(int p, int cix, TCityReport &cityReport, CityReportEx *cityReportEx = nullptr)
|
|
{
|
|
const TCity &city = (*Database::rw[p].City)[cix];
|
|
|
|
if (cityReport.HypoTiles <= 0)
|
|
cityReport.HypoTiles = static_cast<int>(city.Tiles);
|
|
if (cityReport.HypoTax < 0)
|
|
cityReport.HypoTax = Database::rw[p].TaxRate;
|
|
if (cityReport.HypoLux < 0)
|
|
cityReport.HypoLux = Database::rw[p].LuxRate;
|
|
|
|
int baseHappiness;
|
|
int control;
|
|
|
|
if ((city.Flags & chCaptured) != 0 || Database::rw[p].Government == gAnarchy) {
|
|
cityReport.Working = 0;
|
|
for (int v21 = 1; v21 <= 26; ++v21)
|
|
if (cityReport.HypoTiles & (1 << v21))
|
|
++cityReport.Working; // for backward compatibility
|
|
|
|
if (Database::rw[p].Government == gFundamentalism) { // !!! old bug, kept for compatibility
|
|
cityReport.Happy = city.Size;
|
|
control = city.Size;
|
|
} else {
|
|
cityReport.Happy = 0;
|
|
control = 0;
|
|
}
|
|
|
|
baseHappiness = BasicHappy * 2;
|
|
cityReport.Support = 0;
|
|
cityReport.Deployed = 0;
|
|
cityReport.Eaten = city.Size * 2;
|
|
cityReport.FoodRep = city.Size * 2;
|
|
cityReport.ProdRep = 0;
|
|
cityReport.Trade = 0;
|
|
cityReport.PollRep = 0;
|
|
cityReport.Corruption = 0;
|
|
cityReport.Tax = 0;
|
|
cityReport.Lux = 0;
|
|
cityReport.Science = 0;
|
|
|
|
if (cityReportEx) {
|
|
cityReportEx->Material = cityReport.ProdRep;
|
|
cityReportEx->BaseHappiness = baseHappiness;
|
|
cityReportEx->BaseControl = control;
|
|
}
|
|
} else { // not captured, no anarchy
|
|
control = 0;
|
|
baseHappiness = BasicHappy * 2;
|
|
cityReport.Happy = BasicHappy;
|
|
if (city.Built[imColosseum] > 0) {
|
|
if (cityReport.Happy < (city.Size + 1) >> 1)
|
|
cityReport.Happy = (city.Size + 1) >> 1;
|
|
if (city.Size > 4)
|
|
baseHappiness = city.Size;
|
|
}
|
|
for (int i = 0; i < nWonder; ++i)
|
|
if (city.Built[i] == 1) {
|
|
++cityReport.Happy;
|
|
baseHappiness += 2;
|
|
}
|
|
if (city.Built[imTemple] == 1) {
|
|
++cityReport.Happy;
|
|
baseHappiness += 2;
|
|
}
|
|
if (city.Built[imCathedral] == 1) {
|
|
cityReport.Happy += 2;
|
|
baseHappiness += 4;
|
|
if (Database::gWonder[woBach].EffectiveOwner == p) {
|
|
++cityReport.Happy;
|
|
baseHappiness += 2;
|
|
}
|
|
}
|
|
if (city.Built[imTheater] > 0) {
|
|
cityReport.Happy += 2;
|
|
baseHappiness += 4;
|
|
}
|
|
|
|
// calculate unit support
|
|
cityReport.Support = 0;
|
|
int forcedSupport = 0;
|
|
cityReport.Eaten = city.Size * 2;
|
|
cityReport.Deployed = 0;
|
|
const TUn *un = *Database::rw[p].Un;
|
|
for (int uix = 0; uix < Database::rw[p].nUn; ++uix) {
|
|
if (un[uix].Loc >= 0 && un[uix].Home == cix) {
|
|
TUnitReport unitReport;
|
|
Database::getUnitReport(p, uix, unitReport);
|
|
cityReport.Eaten += unitReport.FoodSupport;
|
|
if (unitReport.ReportFlags & urfAlwaysSupport)
|
|
forcedSupport += unitReport.ProdSupport;
|
|
else
|
|
cityReport.Support += unitReport.ProdSupport;
|
|
if (unitReport.ReportFlags & urfDeployed)
|
|
++cityReport.Deployed;
|
|
}
|
|
}
|
|
if (cityReport.Deployed >= cityReport.Happy)
|
|
cityReport.Happy = 0;
|
|
else
|
|
cityReport.Happy -= cityReport.Deployed;
|
|
cityReport.Support -= (city.Size * SupportFree[Database::rw[p].Government]) >> 1;
|
|
if (cityReport.Support < 0)
|
|
cityReport.Support = 0;
|
|
cityReport.Support += forcedSupport;
|
|
|
|
// control
|
|
switch (Database::rw[p].Government) {
|
|
case gDespotism:
|
|
for (int uix = 0; uix < Database::rw[p].nUn; ++uix)
|
|
if (un[uix].Loc == city.Loc && (*Database::rw[p].Model)[un[uix].mix].Kind == mkSpecial_TownGuard) {
|
|
++cityReport.Happy;
|
|
control += 2;
|
|
}
|
|
break;
|
|
case gFundamentalism:
|
|
baseHappiness = 0; // done by control
|
|
cityReport.Happy = city.Size;
|
|
control = city.Size;
|
|
break;
|
|
}
|
|
|
|
// collect processing parameters
|
|
ProdProcessing prodProcessing;
|
|
TradeProcessing tradeProcessing;
|
|
determineCityProdProcessing(p, cix, prodProcessing);
|
|
determineCityTradeProcessing(p, cix, baseHappiness + control - 2 * cityReport.Deployed, tradeProcessing);
|
|
|
|
// collect resources
|
|
cityReport.Working = 0;
|
|
cityReport.FoodRep = 0;
|
|
cityReport.ProdRep = 0;
|
|
cityReport.Trade = 0;
|
|
int rareOK[4] = {0, 0, 0, 0};
|
|
Database::Vicinity21Loc radius;
|
|
Database::v21ToLoc(city.Loc, radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
if (cityReport.HypoTiles & (1 << v21)) { // sum resources of exploited tiles
|
|
const int loc1 = radius[v21];
|
|
if (loc1 < 0 || loc1 >= Database::mapSize) // HypoTiles go beyond map border!
|
|
return eInvalid;
|
|
TTileInfo tileInfo;
|
|
Database::getTileInfo(p, cix, loc1, tileInfo);
|
|
cityReport.FoodRep += tileInfo.Food;
|
|
cityReport.ProdRep += tileInfo.Prod;
|
|
cityReport.Trade += tileInfo.Trade;
|
|
if ((Database::realMap[loc1] & fModern) != 0 && Database::rw[p].Tech[adMassProduction] >= tsApplicable)
|
|
++rareOK[(Database::realMap[loc1] >> 25) & 3];
|
|
++cityReport.Working;
|
|
}
|
|
}
|
|
if (city.Built[imAlgae] == 1)
|
|
cityReport.FoodRep += 12;
|
|
|
|
if (cityReportEx) {
|
|
cityReportEx->Material = cityReport.ProdRep;
|
|
cityReportEx->BaseHappiness = baseHappiness;
|
|
cityReportEx->BaseControl = control;
|
|
cityReportEx->prodProcessing = prodProcessing;
|
|
cityReportEx->tradeProcessing = tradeProcessing;
|
|
}
|
|
|
|
boostProd(cityReport.ProdRep, prodProcessing, cityReport.ProdRep, cityReport.PollRep);
|
|
splitTrade(cityReport.Trade, cityReport.HypoTax, cityReport.HypoLux, cityReport.Working, tradeProcessing,
|
|
cityReport.Corruption, cityReport.Tax, cityReport.Lux, cityReport.Science);
|
|
// new style disorder requires 1 lux less for cities with odd size
|
|
cityReport.Happy += (cityReport.Lux + (city.Size & 1)) >> 1;
|
|
|
|
// check if rare resource available
|
|
if ((Database::gTestFlags & tfNoRareNeed) == 0 && cityReport.ProdRep > cityReport.Support &&
|
|
(city.Project & cpImp) != 0 &&
|
|
(((city.Project & cpIndex) == imShipComp && rareOK[1] == 0) ||
|
|
((city.Project & cpIndex) == imShipPow && rareOK[2] == 0) ||
|
|
((city.Project & cpIndex) == imShipHab && rareOK[3] == 0)))
|
|
cityReport.ProdRep = cityReport.Support;
|
|
}
|
|
|
|
return eOK;
|
|
}
|
|
|
|
// Best tile unused but available by city cix.
|
|
void nextBest(int p, int cix, int &selectedLoc, int &selectedV21)
|
|
{
|
|
int most = 0;
|
|
selectedLoc = -1;
|
|
selectedV21 = -1;
|
|
const TCity &city = (*Database::rw[p].City)[cix];
|
|
Database::Vicinity21Loc radius;
|
|
Database::v21ToLoc(city.Loc, radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
const int loc1 = radius[v21];
|
|
if (loc1 < 0 || loc1 >= Database::mapSize || Database::usedByCity[loc1] != -1)
|
|
continue;
|
|
const int p1 = static_cast<int>(Database::realMap[loc1] >> 27);
|
|
if ((p1 == nPl || p1 == p || Database::rw[p].Treaty[p1] < trPeace) &&
|
|
(Database::zocMap[loc1] == 0 || Database::occupant[loc1] == p ||
|
|
Database::rw[p].Treaty[Database::occupant[loc1]] == trAlliance)) {
|
|
TTileInfo tileInfo;
|
|
Database::getTileInfo(p, cix, loc1, tileInfo);
|
|
// priority: 1.food - 2.prod - 3.trade
|
|
const int resources = (tileInfo.Food << 16) + (tileInfo.Prod << 8) + tileInfo.Trade;
|
|
if (resources > most) {
|
|
selectedLoc = loc1;
|
|
selectedV21 = v21;
|
|
most = resources;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Worst tile used by city cix.
|
|
void nextWorst(int p, int cix, int &selectedLoc, int &selectedV21)
|
|
{
|
|
int least = std::numeric_limits<int>::max();
|
|
selectedLoc = -1;
|
|
selectedV21 = -1;
|
|
const TCity &city = (*Database::rw[p].City)[cix];
|
|
Database::Vicinity21Loc radius;
|
|
Database::v21ToLoc(city.Loc, radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
if (v21 == Database::CityOwnTile)
|
|
continue;
|
|
const int loc1 = radius[v21];
|
|
if (loc1 >= 0 && loc1 < Database::mapSize && (city.Tiles & (1u << v21)) != 0) {
|
|
TTileInfo tileInfo;
|
|
Database::getTileInfo(p, cix, loc1, tileInfo);
|
|
// priority: 1.food - 2.prod - 3.trade
|
|
const int resources = (tileInfo.Food << 16) + (tileInfo.Prod << 8) + tileInfo.Trade;
|
|
if (resources < least) {
|
|
selectedLoc = loc1;
|
|
selectedV21 = v21;
|
|
least = resources;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int nextPoll(int p, int cix)
|
|
{
|
|
int bestDist = std::numeric_limits<int>::max();
|
|
int best = 0;
|
|
int result = -1;
|
|
const TCity &city = (*Database::rw[p].City)[cix];
|
|
Database::Vicinity21Loc radius;
|
|
Database::v21ToLoc(city.Loc, radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
if (v21 == Database::CityOwnTile)
|
|
continue;
|
|
const int loc1 = radius[v21];
|
|
if (loc1 >= 0 && loc1 < Database::mapSize && (Database::realMap[loc1] & fTerrain) >= fGrass &&
|
|
(Database::realMap[loc1] & (fPoll | fDeadLands | fCity)) == 0) {
|
|
const int pTerr = static_cast<int>(Database::realMap[loc1] >> 27);
|
|
if (pTerr == nPl || pTerr == p || Database::rw[p].Treaty[pTerr] < trPeace) {
|
|
TTileInfo tileInfo;
|
|
Database::getTileInfo(p, cix, loc1, tileInfo);
|
|
// priority: 1.prod - 2.trade - 3.food
|
|
const int resources = (tileInfo.Prod << 16) + (tileInfo.Trade << 8) + tileInfo.Food;
|
|
const int dy = (v21 >> 2) - 3;
|
|
const int dx = (((v21 & 3) << 1) - 3) + ((dy + 3) & 1);
|
|
const int dist = std::abs(dx) + std::abs(dy) + (std::abs(std::abs(dx) - std::abs(dy)) >> 1);
|
|
if (resources > best || (resources == best && dist < bestDist)) {
|
|
result = loc1;
|
|
best = resources;
|
|
bestDist = dist;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int getCityReport(int p, int cix, TCityReport &cityReport)
|
|
{
|
|
const int result = getSmallCityReport(p, cix, cityReport);
|
|
cityReport.Storage = StorageSize[Database::difficulty[p]];
|
|
cityReport.ProdCost = getProjectCost(p, cix);
|
|
return result;
|
|
}
|
|
|
|
int getCityReportNew(int p, int cix, TCityReportNew &cityReportNew)
|
|
{
|
|
TCityReport cityReport{};
|
|
CityReportEx cityReportEx{};
|
|
cityReport.HypoTiles = cityReportNew.HypoTiles;
|
|
cityReport.HypoTax = cityReportNew.HypoTaxRate;
|
|
cityReport.HypoLux = cityReportNew.HypoLuxuryRate;
|
|
const int result = getSmallCityReport(p, cix, cityReport, &cityReportEx);
|
|
|
|
const TCity &city = (*Database::rw[p].City)[cix];
|
|
cityReportNew.FoodSupport = cityReport.Eaten - 2 * city.Size;
|
|
cityReportNew.MaterialSupport = cityReport.Support;
|
|
cityReportNew.ProjectCost = getProjectCost(p, cix);
|
|
cityReportNew.Storage = StorageSize[Database::difficulty[p]];
|
|
cityReportNew.Deployed = cityReport.Deployed;
|
|
cityReportNew.Morale = cityReportEx.BaseHappiness;
|
|
cityReportNew.CollectedControl = cityReportEx.BaseControl + (city.Size - cityReport.Working) * 2;
|
|
cityReportNew.CollectedFood = cityReport.FoodRep;
|
|
cityReportNew.CollectedMaterial = cityReportEx.Material;
|
|
cityReportNew.CollectedTrade = cityReport.Trade;
|
|
cityReportNew.Working = cityReport.Working;
|
|
cityReportNew.Production = cityReport.ProdRep - cityReport.Support;
|
|
cityReportNew.AddPollution = cityReport.PollRep;
|
|
cityReportNew.Corruption = cityReport.Corruption;
|
|
cityReportNew.Tax = cityReport.Tax;
|
|
cityReportNew.Science = cityReport.Science;
|
|
cityReportNew.Luxury = cityReport.Lux;
|
|
cityReportNew.FoodSurplus = cityReport.FoodRep - cityReport.Eaten;
|
|
cityReportNew.HappinessBalance =
|
|
cityReportNew.Morale + cityReportNew.Luxury + cityReportNew.CollectedControl - city.Size - 2 * cityReportNew.Deployed;
|
|
return result;
|
|
}
|
|
|
|
bool addBestCityTile(int p, int cix)
|
|
{
|
|
int tileLoc, v21;
|
|
nextBest(p, cix, tileLoc, v21);
|
|
const bool result = tileLoc >= 0;
|
|
if (result) {
|
|
TCity &city = (*Database::rw[p].City)[cix];
|
|
assert((city.Tiles & (1u << v21)) == 0);
|
|
city.Tiles |= (1u << v21);
|
|
Database::usedByCity[tileLoc] = city.Loc;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void cityGrowth(int p, int cix)
|
|
{
|
|
TCity &city = (*Database::rw[p].City)[cix];
|
|
++city.Size;
|
|
int tileLoc, v21;
|
|
nextBest(p, cix, tileLoc, v21);
|
|
if (tileLoc >= 0) { // test whether exploitation of tile would lead to disorder
|
|
TCityReport altCityReport{};
|
|
altCityReport.HypoTiles = static_cast<int>(city.Tiles) + (1 << v21);
|
|
altCityReport.HypoTax = -1;
|
|
altCityReport.HypoLux = -1;
|
|
getSmallCityReport(p, cix, altCityReport);
|
|
if (altCityReport.Working - altCityReport.Happy <= city.Size >> 1) {
|
|
// !!! change to new style disorder
|
|
// no disorder -- exploit tile
|
|
assert((city.Tiles & (1u << v21)) == 0);
|
|
city.Tiles |= (1u << v21);
|
|
Database::usedByCity[tileLoc] = city.Loc;
|
|
}
|
|
}
|
|
}
|
|
|
|
void cityShrink(int p, int cix)
|
|
{
|
|
TCity &city = (*Database::rw[p].City)[cix];
|
|
int working = 0;
|
|
for (int v21 = 1; v21 <= 26; ++v21)
|
|
if (city.Tiles & (1u << v21))
|
|
++working;
|
|
--city.Size;
|
|
if (city.Food > StorageSize[Database::difficulty[p]])
|
|
city.Food = static_cast<std::int16_t>(StorageSize[Database::difficulty[p]]);
|
|
|
|
int tileLoc, v21;
|
|
nextWorst(p, cix, tileLoc, v21);
|
|
if (working > city.Size) { // all citizens were working -- worst tile no longer exploited
|
|
assert((city.Tiles & (1u << v21)) != 0);
|
|
city.Tiles &= ~(1u << v21);
|
|
Database::usedByCity[tileLoc] = -1;
|
|
} else { // test whether exploitation of tile would lead to disorder
|
|
TCityReport altCityReport{};
|
|
altCityReport.HypoTiles = -1;
|
|
altCityReport.HypoTax = -1;
|
|
altCityReport.HypoLux = -1;
|
|
getSmallCityReport(p, cix, altCityReport);
|
|
if (altCityReport.Working - altCityReport.Happy > city.Size >> 1) {
|
|
// !!! change to new style disorder
|
|
// disorder -- don't exploit tile
|
|
assert((city.Tiles & (1u << v21)) != 0);
|
|
city.Tiles &= ~(1u << v21);
|
|
Database::usedByCity[tileLoc] = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
void pollute(int p, int cix)
|
|
{
|
|
TCity &city = (*Database::rw[p].City)[cix];
|
|
city.Pollution = static_cast<std::int16_t>(city.Pollution - MaxPollution);
|
|
const int pollutionLoc = nextPoll(p, cix);
|
|
if (pollutionLoc >= 0) {
|
|
city.Flags += chPollution;
|
|
Database::realMap[pollutionLoc] |= fPoll;
|
|
}
|
|
}
|
|
|
|
void payCityMaintenance(int p, int cix)
|
|
{
|
|
TPlayerContext &player = Database::rw[p];
|
|
TCity &city = (*player.City)[cix];
|
|
for (int i = nWonder; i < nImp; ++i) {
|
|
if (city.Built[i] > 0 && (city.Project0 & (cpImp | cpIndex)) != (cpImp | i)) {
|
|
// don't pay maintenance when just completed
|
|
player.Money -= Imp[i].Maint;
|
|
if (player.Money < 0) { // out of money - sell improvement
|
|
player.Money += Imp[i].Cost * BuildCostMod[Database::difficulty[p]] / 12;
|
|
city.Built[i] = 0;
|
|
if (Imp[i].Kind != ikCommon) {
|
|
assert(i != imSpacePort); // never sell automatically! (solution: no maintenance)
|
|
player.NatBuilt[i - nWonder] = 0;
|
|
if (i == imGrWall)
|
|
Database::grWallContinent[p] = -1;
|
|
}
|
|
city.Flags += chImprovementLost;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void collectCityResources(int p, int cix)
|
|
{
|
|
TPlayerContext &player = Database::rw[p];
|
|
TCity &city = (*player.City)[cix];
|
|
|
|
if (city.Flags & chCaptured) {
|
|
city.Flags &= ~static_cast<std::uint32_t>(chDisorder);
|
|
city.Flags -= 0x10000;
|
|
if ((city.Flags & chCaptured) == 0)
|
|
city.Flags |= chAfterCapture;
|
|
} else if (player.Government == gAnarchy) {
|
|
city.Flags &= ~static_cast<std::uint32_t>(chDisorder);
|
|
} else {
|
|
TCityReportNew cityReport{};
|
|
cityReport.HypoTiles = -1;
|
|
cityReport.HypoTaxRate = -1;
|
|
cityReport.HypoLuxuryRate = -1;
|
|
getCityReportNew(p, cix, cityReport);
|
|
const int cityStorage = StorageSize[Database::difficulty[p]];
|
|
const int cityProjectCost = getProjectCost(p, cix);
|
|
|
|
const bool disorder = cityReport.HappinessBalance < 0;
|
|
if (disorder && (city.Flags & chDisorder))
|
|
cityReport.CollectedMaterial = 0; // second turn disorder
|
|
if (disorder)
|
|
city.Flags |= chDisorder;
|
|
else
|
|
city.Flags &= ~static_cast<std::uint32_t>(chDisorder);
|
|
|
|
if (!disorder &&
|
|
((player.Government == gFuture) || (city.Size >= NeedAqueductSize && cityReport.FoodSurplus < 2)) &&
|
|
cityReport.FoodSurplus > 0) {
|
|
player.Money += cityReport.FoodSurplus;
|
|
} else if (!(disorder && cityReport.FoodSurplus > 0)) { // calculate new food storage
|
|
city.Food = static_cast<std::int16_t>(city.Food + cityReport.FoodSurplus);
|
|
if (((Database::gTestFlags & tfImmGrow) != 0 ||
|
|
(city.Food >= cityStorage && city.Food - cityReport.FoodSurplus < cityStorage)) && // only warn once
|
|
city.Size < MaxCitySize && (city.Project & (cpImp + cpIndex)) != cpImp + imAqueduct &&
|
|
(city.Project & (cpImp + cpIndex)) != cpImp + imSewer && !canCityGrow(p, cix))
|
|
city.Flags += chNoGrowthWarning;
|
|
}
|
|
|
|
if (city.Prod > cityProjectCost) {
|
|
player.Money += city.Prod - cityProjectCost;
|
|
city.Prod = static_cast<std::int16_t>(cityProjectCost);
|
|
}
|
|
if (cityReport.Production < 0) {
|
|
city.Flags |= chUnitLost;
|
|
} else if (!disorder && (city.Flags & chProductionSabotaged) == 0) {
|
|
if ((city.Project & (cpImp + cpIndex)) == cpImp + imTrGoods)
|
|
player.Money += cityReport.Production;
|
|
else
|
|
city.Prod = static_cast<std::int16_t>(city.Prod + cityReport.Production);
|
|
}
|
|
|
|
if (!disorder) { // sum research points and taxes
|
|
player.Research += cityReport.Science;
|
|
player.Money += cityReport.Tax;
|
|
city.Pollution = static_cast<std::int16_t>(city.Pollution + cityReport.AddPollution);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool cityTurn(int p, int cix)
|
|
{
|
|
// return value: whether city keeps existing
|
|
TPlayerContext &player = Database::rw[p];
|
|
TCity &city = (*player.City)[cix];
|
|
|
|
int sizeMod = 0;
|
|
const int cityStorage = StorageSize[Database::difficulty[p]];
|
|
const int cityProjectCost = getProjectCost(p, cix);
|
|
|
|
const bool lackOfMaterial = (city.Flags & chUnitLost) != 0;
|
|
city.Flags &= ~static_cast<std::uint32_t>(chUnitLost);
|
|
|
|
const bool isActive = player.Government != gAnarchy && (city.Flags & chCaptured) == 0;
|
|
const bool checkGrow = (city.Flags & chDisorder) == 0 && isActive && player.Government != gFuture;
|
|
|
|
TUn *un = *player.Un;
|
|
const TModel *model = *player.Model;
|
|
|
|
if (checkGrow && (Database::gTestFlags & tfImmGrow) != 0) { // fast growth
|
|
if (canCityGrow(p, cix))
|
|
++sizeMod;
|
|
} else if (checkGrow && city.Food >= cityStorage) { // normal growth
|
|
if (canCityGrow(p, cix)) {
|
|
if (city.Built[imGranary] == 1)
|
|
city.Food = static_cast<std::int16_t>(city.Food - (cityStorage >> 1));
|
|
else
|
|
city.Food = static_cast<std::int16_t>(city.Food - cityStorage);
|
|
++sizeMod;
|
|
}
|
|
} else if (city.Food < 0) { // famine
|
|
city.Food = 0;
|
|
// check if settlers or conscripts there to disband
|
|
int uix = -1;
|
|
for (int i = 0; i < player.nUn; ++i) {
|
|
if (un[i].Loc >= 0 && un[i].Home == cix &&
|
|
(model[un[i].mix].Kind == mkSettler || (un[i].Flags & unConscripts) != 0) &&
|
|
(uix == -1 || model[un[i].mix].Cost < model[un[uix].mix].Cost ||
|
|
(model[un[i].mix].Cost == model[un[uix].mix].Cost && un[i].Exp < un[uix].Exp)))
|
|
uix = i;
|
|
}
|
|
if (uix >= 0) {
|
|
Database::removeUnitUpdateMap(p, uix);
|
|
city.Flags += chUnitLost;
|
|
} else {
|
|
--sizeMod;
|
|
city.Flags += chPopDecrease;
|
|
}
|
|
}
|
|
if (city.Food > cityStorage)
|
|
city.Food = static_cast<std::int16_t>(cityStorage);
|
|
|
|
if (lackOfMaterial && (city.Flags & chUnitLost) == 0) { // one unit lost
|
|
int uix = -1;
|
|
int det = std::numeric_limits<int>::max();
|
|
for (int i = 0; i < player.nUn; ++i) {
|
|
if (un[i].Loc >= 0 && un[i].Home == cix) {
|
|
const TModel &m = model[un[i].mix];
|
|
int testDet;
|
|
if (m.Kind == mkSpecial_TownGuard)
|
|
testDet = un[i].Health + (un[i].Exp << 8); // disband townguards first
|
|
else {
|
|
testDet = un[i].Health + (un[i].Exp << 8) + (m.Cost << 16); // value of unit
|
|
if (m.Flags & mdDoubleSupport)
|
|
testDet >>= 1; // double support, tend to disband first
|
|
}
|
|
if (testDet < det) {
|
|
uix = i;
|
|
det = testDet;
|
|
}
|
|
}
|
|
}
|
|
if (uix >= 0) {
|
|
Database::removeUnitUpdateMap(p, uix);
|
|
city.Flags += chUnitLost;
|
|
}
|
|
}
|
|
|
|
if (Database::gTestFlags & tfImmImprove)
|
|
city.Prod = static_cast<std::int16_t>(cityProjectCost);
|
|
bool doProd = (city.Project & (cpImp + cpIndex)) != cpImp + imTrGoods && city.Prod >= cityProjectCost;
|
|
|
|
// check if wonder already built
|
|
if ((city.Project & cpImp) != 0 && (city.Project & cpIndex) < nWonder &&
|
|
Database::gWonder[city.Project & cpIndex].CityID != WonderNotBuiltYet) {
|
|
city.Flags += chOldWonder;
|
|
doProd = false;
|
|
}
|
|
|
|
// check if producing settlers would disband city
|
|
if (doProd && (city.Project & (cpImp | cpDisbandCity)) == 0 &&
|
|
((city.Size + sizeMod - 2 < 2 && model[city.Project & cpIndex].Kind == mkSettler) ||
|
|
(city.Size + sizeMod - 1 < 2 && (model[city.Project & cpIndex].Kind == mkSlaves ||
|
|
(city.Project & cpConscripts) != 0)))) {
|
|
city.Flags += chNoSettlerProd;
|
|
doProd = false;
|
|
}
|
|
|
|
if (doProd) { // project complete
|
|
city.Prod = static_cast<std::int16_t>(city.Prod - cityProjectCost);
|
|
if ((city.Project & cpImp) == 0) { // produce unit
|
|
if (player.nUn < Database::neumax) {
|
|
Database::createUnit(p, city.Project & cpIndex);
|
|
un[player.nUn - 1].Loc = city.Loc;
|
|
TUn &newUn = un[player.nUn - 1];
|
|
newUn.Home = static_cast<std::int16_t>(cix);
|
|
if (model[newUn.mix].Domain < dSea && city.Built[imElite] == 1)
|
|
newUn.Exp = static_cast<std::uint8_t>(ExpCost * (nExp - 1)); // elite
|
|
else if ((model[newUn.mix].Domain < dSea && city.Built[imBarracks] == 1) ||
|
|
(model[newUn.mix].Domain == dSea && city.Built[imDockyard] == 1) ||
|
|
(model[newUn.mix].Domain == dAir && city.Built[imAirport] == 1))
|
|
newUn.Exp = static_cast<std::uint8_t>(ExpCost * 2); // vet
|
|
if (city.Project & cpConscripts)
|
|
newUn.Flags |= unConscripts;
|
|
Database::placeUnit(p, player.nUn - 1);
|
|
Database::updateUnitMap(city.Loc);
|
|
if (model[city.Project & cpIndex].Kind == mkSettler)
|
|
sizeMod -= 2; // settler produced - city shrink
|
|
else if (model[city.Project & cpIndex].Kind == mkSlaves || (city.Project & cpConscripts) != 0)
|
|
--sizeMod; // slaves/conscripts produced - city shrink
|
|
}
|
|
city.Project0 = static_cast<std::int16_t>(city.Project | cpRepeat | cpCompleted);
|
|
} else if (Imp[city.Project & cpIndex].Kind == ikShipPart) { // produce ship parts
|
|
++Database::gShip[p].Parts[(city.Project & cpIndex) - imShipComp];
|
|
city.Project0 = static_cast<std::int16_t>(city.Project | cpCompleted);
|
|
} else { // produce improvement
|
|
const int newImp = city.Project & cpIndex;
|
|
player.Money += city.Prod; // change rest to money
|
|
city.Project0 = static_cast<std::int16_t>(city.Project | cpCompleted);
|
|
city.Project = cpImp + imTrGoods;
|
|
city.Prod = 0;
|
|
|
|
if (Imp[newImp].Kind == ikNatLocal || Imp[newImp].Kind == ikNatGlobal) { // nat. project
|
|
TCity *allCities = *player.City;
|
|
for (int i = 0; i < player.nCity; ++i) {
|
|
if (allCities[i].Loc >= 0 && allCities[i].Built[newImp] == 1) { // allowed only once
|
|
player.Money += Imp[newImp].Cost * BuildCostMod[Database::difficulty[p]] / 12;
|
|
allCities[i].Built[newImp] = 0;
|
|
}
|
|
}
|
|
player.NatBuilt[newImp - nWonder] = 1;
|
|
|
|
// immediate nat. project effects
|
|
if (newImp == imGrWall)
|
|
Database::grWallContinent[p] = Database::continent[city.Loc];
|
|
}
|
|
|
|
if (newImp < nWonder) { // wonder
|
|
Database::gWonder[newImp].CityID = city.ID;
|
|
Database::gWonder[newImp].EffectiveOwner = p;
|
|
Database::checkExpiration(newImp);
|
|
|
|
// immediate wonder effects
|
|
if (newImp == woEiffel) { // reactivate wonders
|
|
TCity *allCities = *player.City;
|
|
for (int i = 0; i < nWonder; ++i)
|
|
if (Imp[i].Expiration >= 0)
|
|
for (int cix2 = 0; cix2 < player.nCity; ++cix2)
|
|
if (allCities[cix2].Loc >= 0 && allCities[cix2].Built[i] == 1)
|
|
Database::gWonder[i].EffectiveOwner = p;
|
|
} else if (newImp == woLighthouse) {
|
|
Database::checkSpecialModels(p, preLighthouse);
|
|
} else if (newImp == woLeo) {
|
|
player.Research += Database::techBaseCost(Database::nTech[p], Database::difficulty[p]) +
|
|
Database::techBaseCost(Database::nTech[p] + 2, Database::difficulty[p]);
|
|
Database::checkSpecialModels(p, preLeo);
|
|
} else if (newImp == woPyramids) {
|
|
Database::checkSpecialModels(p, preBuilder);
|
|
} else if (newImp == woMir) {
|
|
for (int p1 = 0; p1 < nPl; ++p1) {
|
|
if (p1 != p && (Database::gAlive & (1 << p1))) {
|
|
if (player.Treaty[p1] == trNoContact)
|
|
Database::introduceEnemy(p, p1);
|
|
Database::giveCivilReport(p, p1);
|
|
Database::giveMilReport(p, p1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < nImpReplacement; ++i) // sell obsolete buildings
|
|
if (ImpReplacement[i].NewImp == newImp && city.Built[ImpReplacement[i].OldImp] > 0) {
|
|
player.Money += Imp[ImpReplacement[i].OldImp].Cost * BuildCostMod[Database::difficulty[p]] / 12;
|
|
city.Built[ImpReplacement[i].OldImp] = 0;
|
|
}
|
|
|
|
if (newImp == imPower || newImp == imHydro || newImp == imNuclear) {
|
|
for (int i = 0; i < nImp; ++i) {
|
|
if (i != newImp && (i == imPower || i == imHydro || i == imNuclear) && city.Built[i] > 0) {
|
|
// sell obsolete power plant
|
|
player.Money += Imp[i].Cost * BuildCostMod[Database::difficulty[p]] / 12;
|
|
city.Built[i] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
city.Built[newImp] = 1;
|
|
}
|
|
city.Prod0 = city.Prod;
|
|
city.Flags += chProduction;
|
|
} else {
|
|
city.Project0 = static_cast<std::int16_t>(city.Project0 & ~cpCompleted);
|
|
if ((city.Project0 & ~cpAuto) != (city.Project & ~cpAuto))
|
|
city.Project0 = city.Project;
|
|
city.Prod0 = city.Prod;
|
|
}
|
|
|
|
if (sizeMod > 0) {
|
|
cityGrowth(p, cix);
|
|
city.Flags += chPopIncrease;
|
|
}
|
|
const bool result = city.Size + sizeMod >= 2;
|
|
if (result) {
|
|
while (sizeMod < 0) {
|
|
cityShrink(p, cix);
|
|
++sizeMod;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int setCityTiles(int p, int cix, int newTiles, bool testOnly)
|
|
{
|
|
TCity &city = (*Database::rw[p].City)[cix];
|
|
const int changeTiles = newTiles ^ static_cast<int>(city.Tiles);
|
|
const int addTiles = newTiles & ~static_cast<int>(city.Tiles);
|
|
|
|
if (Database::mode == Database::moPlaying) { // do all checks
|
|
if ((newTiles & ~0x67F7F76) != 0)
|
|
return eInvalid; // invalid tile index included
|
|
if ((newTiles & (1 << 13)) == 0)
|
|
return eViolation; // city tile must be exploited
|
|
if (changeTiles == 0)
|
|
return eNotChanged;
|
|
if (addTiles != 0) {
|
|
// check if new tiles possible
|
|
TCityAreaInfo cityAreaInfo;
|
|
getCityAreaInfo(p, city.Loc, cityAreaInfo);
|
|
for (int v21 = 1; v21 <= 26; ++v21)
|
|
if ((addTiles & (1 << v21)) != 0 && cityAreaInfo.Available[v21] != faAvailable)
|
|
return eTileNotAvailable;
|
|
// not more tiles than inhabitants
|
|
int working = 0;
|
|
for (int v21 = 1; v21 <= 26; ++v21)
|
|
if (newTiles & (1 << v21))
|
|
++working;
|
|
if (working > city.Size)
|
|
return eNoWorkerAvailable;
|
|
}
|
|
}
|
|
|
|
if (!testOnly) {
|
|
Database::Vicinity21Loc radius;
|
|
Database::v21ToLoc(city.Loc, radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
if (changeTiles & (1 << v21)) {
|
|
const int loc1 = radius[v21];
|
|
assert(loc1 >= 0 && loc1 < Database::mapSize);
|
|
if (newTiles & (1 << v21)) {
|
|
Database::usedByCity[loc1] = city.Loc; // employ tile
|
|
} else if (Database::usedByCity[loc1] != city.Loc) {
|
|
// should only happen during loading, because of wrong
|
|
// sSetCityTiles command order
|
|
assert(Database::mode < Database::moPlaying);
|
|
} else {
|
|
Database::usedByCity[loc1] = -1; // unemploy tile
|
|
}
|
|
}
|
|
}
|
|
city.Tiles = static_cast<std::uint32_t>(newTiles);
|
|
}
|
|
return eOK;
|
|
}
|
|
|
|
namespace {
|
|
// Local to GetCityTileAdvice - equivalent of its nested TTileData record.
|
|
struct TileData {
|
|
int Food;
|
|
int Prod;
|
|
int Trade;
|
|
int SubValue;
|
|
int V21;
|
|
};
|
|
} // namespace
|
|
|
|
void getCityTileAdvice(int p, int cix, TCityTileAdviceData &advice)
|
|
{
|
|
constexpr int oFood = 0, oProd = 1, oTax = 2, oScience = 3;
|
|
|
|
TPlayerContext &player = Database::rw[p];
|
|
TCity &city = (*player.City)[cix];
|
|
|
|
if (player.Government == gAnarchy || (city.Flags & chCaptured) != 0) {
|
|
advice.CityReport = TCityReport{};
|
|
advice.Tiles = 1u << Database::CityOwnTile;
|
|
advice.CityReport.HypoTiles = 1 << Database::CityOwnTile;
|
|
return;
|
|
}
|
|
|
|
// decode evaluation formula from weights parameter
|
|
double valueFormulaWeight[4];
|
|
bool valueFormulaMultiply[4];
|
|
for (int i = oFood; i <= oScience; ++i) {
|
|
const int formulaCode = static_cast<int>((advice.ResourceWeights >> (24 - 8 * i)) & 0xFF);
|
|
valueFormulaMultiply[i] = (formulaCode & 0x80) != 0;
|
|
if (formulaCode & 0x40)
|
|
valueFormulaWeight[i] = (formulaCode & 0x0F) * (1 << ((formulaCode & 0x30) >> 4)) / 16.0;
|
|
else
|
|
valueFormulaWeight[i] = (formulaCode & 0x0F) * (1 << ((formulaCode & 0x30) >> 4));
|
|
}
|
|
|
|
TCityReport testReport{};
|
|
testReport.HypoTiles = 1 << Database::CityOwnTile;
|
|
testReport.HypoTax = -1;
|
|
testReport.HypoLux = -1;
|
|
CityReportEx cityReportEx{};
|
|
getSmallCityReport(p, cix, testReport, &cityReportEx);
|
|
|
|
Database::Vicinity21Loc radius;
|
|
Database::v21ToLoc(city.Loc, radius);
|
|
const bool foodToTax = player.Government == gFuture;
|
|
const bool prodToTax = (city.Project & (cpImp + cpIndex)) == cpImp + imTrGoods;
|
|
const bool foodWasted =
|
|
!foodToTax && city.Food == StorageSize[Database::difficulty[p]] && !canCityGrow(p, cix);
|
|
|
|
// sub criteria
|
|
std::array<int, 28> subCriterion{}; // index 0 unused, matches Pascal's 1..26
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
const int loc1 = radius[v21];
|
|
if (loc1 >= 0)
|
|
subCriterion[v21] = (3360 - (Database::distance(city.Loc, loc1) - 1) * 32 - v21) ^ 0x15;
|
|
}
|
|
const TCity *allCities = *player.City;
|
|
for (int cix1 = 0; cix1 < player.nCity; ++cix1) {
|
|
if (cix1 == cix)
|
|
continue;
|
|
const int cityLoc1 = allCities[cix1].Loc;
|
|
if (cityLoc1 >= 0 && Database::distance(city.Loc, cityLoc1) <= 10) {
|
|
// cities overlap -- prefer tiles outside common range
|
|
Database::Vicinity21Loc radius1;
|
|
Database::v21ToLoc(cityLoc1, radius1);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
const int loc1 = radius1[v21];
|
|
if (loc1 >= 0 && loc1 < Database::mapSize && Database::distance(city.Loc, loc1) <= 5) {
|
|
int dx, dy;
|
|
Database::dxdy(city.Loc, loc1, dx, dy);
|
|
subCriterion[((dy + 3) << 2) + ((dx + 3) >> 1)] -= 160;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
TCityAreaInfo cityAreaInfo;
|
|
getCityAreaInfo(p, city.Loc, cityAreaInfo);
|
|
int areaSize = 0;
|
|
for (int v21 = 1; v21 <= 26; ++v21)
|
|
if (cityAreaInfo.Available[v21] == faAvailable)
|
|
++areaSize;
|
|
|
|
int minWorking, maxWorking;
|
|
if (player.Government == gFundamentalism) {
|
|
minWorking = city.Size;
|
|
maxWorking = city.Size;
|
|
} else {
|
|
minWorking = cityReportEx.tradeProcessing.HappyBase >> 1;
|
|
if (minWorking > city.Size)
|
|
minWorking = city.Size;
|
|
if (player.LuxRate == 0 && !cityReportEx.tradeProcessing.FlexibleLuxury)
|
|
maxWorking = minWorking;
|
|
else
|
|
maxWorking = city.Size;
|
|
}
|
|
if (maxWorking > areaSize) {
|
|
maxWorking = areaSize;
|
|
if (minWorking > areaSize)
|
|
minWorking = areaSize;
|
|
}
|
|
int wantedProd;
|
|
if (testReport.Support == 0)
|
|
wantedProd = 0;
|
|
else
|
|
wantedProd = 1 + (testReport.Support * 100 - 1) /
|
|
(100 + cityReportEx.prodProcessing.ProdBonus * 50 + cityReportEx.prodProcessing.FutProdBonus);
|
|
|
|
// consider resources for ship parts
|
|
int needRare = 0;
|
|
if ((Database::gTestFlags & tfNoRareNeed) == 0 && (city.Project & cpImp) != 0) {
|
|
const int projectIndex = city.Project & cpIndex;
|
|
if (projectIndex == imShipComp)
|
|
needRare = fCobalt;
|
|
else if (projectIndex == imShipPow)
|
|
needRare = fUranium;
|
|
else if (projectIndex == imShipHab)
|
|
needRare = fMercury;
|
|
}
|
|
int rareTiles = 0;
|
|
if (needRare > 0) {
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
const int loc1 = radius[v21];
|
|
if (loc1 >= 0 && loc1 < Database::mapSize &&
|
|
(Database::realMap[loc1] & fModern) == static_cast<std::uint32_t>(needRare))
|
|
rareTiles |= (1 << v21);
|
|
}
|
|
}
|
|
|
|
// step 1: sort tiles to hierarchies
|
|
int nHierarchy = 0;
|
|
TTileInfo baseTileInfo{};
|
|
std::array<int, 21> nTile{};
|
|
std::array<std::array<TileData, 32>, 21> hierarchy{};
|
|
for (int v21 = 1; v21 <= 26; ++v21) { // non-rare tiles
|
|
if (cityAreaInfo.Available[v21] == faAvailable && (needRare == 0 || (rareTiles & (1 << v21)) == 0)) {
|
|
const int loc1 = radius[v21];
|
|
assert(loc1 >= 0 && loc1 < Database::mapSize);
|
|
TTileInfo tileInfo;
|
|
Database::getTileInfo(p, cix, loc1, tileInfo);
|
|
if (v21 == Database::CityOwnTile) {
|
|
baseTileInfo = tileInfo;
|
|
} else {
|
|
int iH = 0;
|
|
int iT = 0;
|
|
while (iH < nHierarchy) {
|
|
iT = 0;
|
|
while (iT < nTile[iH] && tileInfo.Food <= hierarchy[iH][iT].Food &&
|
|
tileInfo.Prod <= hierarchy[iH][iT].Prod && tileInfo.Trade <= hierarchy[iH][iT].Trade &&
|
|
!(tileInfo.Food == hierarchy[iH][iT].Food && tileInfo.Prod == hierarchy[iH][iT].Prod &&
|
|
tileInfo.Trade == hierarchy[iH][iT].Trade &&
|
|
subCriterion[v21] >= subCriterion[hierarchy[iH][iT].V21]))
|
|
++iT;
|
|
if (iT == nTile[iH] || (tileInfo.Food >= hierarchy[iH][iT].Food &&
|
|
tileInfo.Prod >= hierarchy[iH][iT].Prod &&
|
|
tileInfo.Trade >= hierarchy[iH][iT].Trade))
|
|
break; // insert position found!
|
|
++iH;
|
|
}
|
|
if (iH == nHierarchy) { // need to start new hierarchy
|
|
nTile[iH] = 0;
|
|
++nHierarchy;
|
|
iT = 0;
|
|
}
|
|
for (int k = nTile[iH]; k > iT; --k)
|
|
hierarchy[iH][k] = hierarchy[iH][k - 1];
|
|
++nTile[iH];
|
|
hierarchy[iH][iT].V21 = v21;
|
|
hierarchy[iH][iT].Food = tileInfo.Food;
|
|
hierarchy[iH][iT].Prod = tileInfo.Prod;
|
|
hierarchy[iH][iT].Trade = tileInfo.Trade;
|
|
hierarchy[iH][iT].SubValue = subCriterion[v21];
|
|
}
|
|
}
|
|
}
|
|
if (needRare != 0) { // rare tiles need own hierarchy
|
|
int iH = nHierarchy;
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
if (cityAreaInfo.Available[v21] == faAvailable && (rareTiles & (1 << v21)) != 0) {
|
|
const int loc1 = radius[v21];
|
|
assert(v21 != Database::CityOwnTile && loc1 >= 0 && loc1 < Database::mapSize);
|
|
TTileInfo tileInfo;
|
|
Database::getTileInfo(p, cix, loc1, tileInfo);
|
|
int iT;
|
|
if (iH == nHierarchy) { // need to start new hierarchy
|
|
nTile[iH] = 0;
|
|
++nHierarchy;
|
|
iT = 0;
|
|
} else {
|
|
iT = nTile[iH];
|
|
}
|
|
++nTile[iH];
|
|
hierarchy[iH][iT].V21 = v21;
|
|
hierarchy[iH][iT].Food = tileInfo.Food; // = 0
|
|
hierarchy[iH][iT].Prod = tileInfo.Prod; // = 1
|
|
hierarchy[iH][iT].Trade = tileInfo.Trade; // = 0
|
|
hierarchy[iH][iT].SubValue = subCriterion[v21];
|
|
}
|
|
}
|
|
}
|
|
if (city.Built[imAlgae] > 0)
|
|
baseTileInfo.Food += 12;
|
|
|
|
// step 2: summarize resources
|
|
for (int iH = 0; iH < nHierarchy; ++iH) {
|
|
for (int k = nTile[iH]; k >= 1; --k)
|
|
hierarchy[iH][k] = hierarchy[iH][k - 1];
|
|
hierarchy[iH][0].Food = 0;
|
|
hierarchy[iH][0].Prod = 0;
|
|
hierarchy[iH][0].Trade = 0;
|
|
hierarchy[iH][0].SubValue = 0;
|
|
hierarchy[iH][0].V21 = 0;
|
|
for (int iT = 1; iT <= nTile[iH]; ++iT) {
|
|
hierarchy[iH][iT].Food += hierarchy[iH][iT - 1].Food;
|
|
hierarchy[iH][iT].Prod += hierarchy[iH][iT - 1].Prod;
|
|
hierarchy[iH][iT].Trade += hierarchy[iH][iT - 1].Trade;
|
|
hierarchy[iH][iT].SubValue += hierarchy[iH][iT - 1].SubValue;
|
|
hierarchy[iH][iT].V21 = (1 << hierarchy[iH][iT].V21) + hierarchy[iH][iT - 1].V21;
|
|
}
|
|
}
|
|
|
|
// step 3: try all combinations
|
|
double bestValue = 0.0;
|
|
int bestSuperValue = 0;
|
|
int bestSubValue = 0;
|
|
int bestTiles = 0;
|
|
std::array<int, 21> nSelection{};
|
|
testReport.FoodRep = baseTileInfo.Food;
|
|
int prodBeforeBoost = baseTileInfo.Prod;
|
|
testReport.Trade = baseTileInfo.Trade;
|
|
testReport.Working = 1;
|
|
int minFood = 0;
|
|
int minProd = 0;
|
|
int iH_Switch = nHierarchy;
|
|
do {
|
|
// ensure minima
|
|
int iH = 0;
|
|
while (testReport.Working < maxWorking && iH < iH_Switch &&
|
|
(testReport.Working < minWorking || testReport.FoodRep < testReport.Eaten ||
|
|
prodBeforeBoost < wantedProd)) {
|
|
assert(nSelection[iH] == 0);
|
|
int take = minWorking - testReport.Working;
|
|
if (take > nTile[iH]) {
|
|
take = nTile[iH];
|
|
} else {
|
|
if (take < 0)
|
|
take = 0;
|
|
int maxTake = nTile[iH];
|
|
if (testReport.Working + maxTake > maxWorking)
|
|
maxTake = maxWorking - testReport.Working;
|
|
while (take < maxTake && testReport.FoodRep + hierarchy[iH][take].Food < minFood)
|
|
++take;
|
|
while (take < maxTake && prodBeforeBoost + hierarchy[iH][take].Prod < minProd)
|
|
++take;
|
|
}
|
|
nSelection[iH] = take;
|
|
testReport.Working += take;
|
|
testReport.FoodRep += hierarchy[iH][take].Food;
|
|
prodBeforeBoost += hierarchy[iH][take].Prod;
|
|
testReport.Trade += hierarchy[iH][take].Trade;
|
|
++iH;
|
|
}
|
|
|
|
assert(testReport.Working >= minWorking && testReport.Working <= maxWorking);
|
|
if (testReport.FoodRep >= minFood && prodBeforeBoost >= minProd) {
|
|
splitTrade(testReport.Trade, player.TaxRate, player.LuxRate, testReport.Working,
|
|
cityReportEx.tradeProcessing, testReport.Corruption, testReport.Tax, testReport.Lux,
|
|
testReport.Science);
|
|
|
|
if (cityReportEx.BaseHappiness + cityReportEx.BaseControl + testReport.Lux +
|
|
2 * (static_cast<int>(city.Size) - testReport.Working) - 2 * testReport.Deployed >=
|
|
city.Size) {
|
|
// city is not in disorder -- evaluate combination
|
|
if (minProd < wantedProd && prodBeforeBoost > minProd) {
|
|
// no combination reached wanted prod yet
|
|
minProd = prodBeforeBoost;
|
|
if (minProd > wantedProd)
|
|
minProd = wantedProd;
|
|
}
|
|
if (minProd == wantedProd) {
|
|
// do not care for food before prod is ensured
|
|
if (minFood < testReport.Eaten && testReport.FoodRep > minFood) {
|
|
// no combination reached wanted food yet
|
|
minFood = testReport.FoodRep;
|
|
if (minFood > testReport.Eaten)
|
|
minFood = testReport.Eaten;
|
|
}
|
|
}
|
|
boostProd(prodBeforeBoost, cityReportEx.prodProcessing, testReport.ProdRep, testReport.PollRep);
|
|
int superValue = 0;
|
|
|
|
// super-criterion A: unit support granted?
|
|
if (testReport.ProdRep >= testReport.Support)
|
|
superValue |= (1 << 30);
|
|
|
|
// super-criterion B: food demand granted?
|
|
if (testReport.FoodRep >= testReport.Eaten)
|
|
superValue |= (63 << 24);
|
|
else if (testReport.FoodRep > testReport.Eaten - 63)
|
|
superValue |= (63 - (testReport.Eaten - testReport.FoodRep)) << 24;
|
|
|
|
const int superPlus = superValue - bestSuperValue;
|
|
if (superPlus >= 0) {
|
|
int output[4];
|
|
output[oTax] = testReport.Tax;
|
|
output[oScience] = testReport.Science;
|
|
|
|
if (testReport.FoodRep < testReport.Eaten) {
|
|
// appreciate what we have, combination will have bad supervalue anyway
|
|
output[oFood] = testReport.FoodRep;
|
|
} else if (foodWasted) {
|
|
output[oFood] = 0;
|
|
} else {
|
|
output[oFood] = testReport.FoodRep - testReport.Eaten;
|
|
if (foodToTax || (city.Size >= NeedAqueductSize && output[oFood] == 1)) {
|
|
output[oTax] += output[oFood];
|
|
output[oFood] = 0;
|
|
}
|
|
}
|
|
|
|
if (testReport.ProdRep < testReport.Support) {
|
|
// appreciate what we have, combination will have bad supervalue anyway
|
|
output[oProd] = testReport.ProdRep;
|
|
} else {
|
|
if (needRare > 0) {
|
|
bool rareOK = false;
|
|
for (int ih2 = 0; ih2 < nHierarchy; ++ih2)
|
|
if (hierarchy[ih2][nSelection[ih2]].V21 & rareTiles) {
|
|
rareOK = true;
|
|
break;
|
|
}
|
|
if (!rareOK)
|
|
testReport.ProdRep = testReport.Support;
|
|
}
|
|
output[oProd] = testReport.ProdRep - testReport.Support;
|
|
if (prodToTax) {
|
|
output[oTax] += output[oProd];
|
|
output[oProd] = 0;
|
|
}
|
|
}
|
|
|
|
bool needStep2 = false;
|
|
double value = 0;
|
|
for (int i = oFood; i <= oScience; ++i) {
|
|
if (valueFormulaMultiply[i])
|
|
needStep2 = true;
|
|
else
|
|
value += valueFormulaWeight[i] * output[i];
|
|
}
|
|
if (needStep2) {
|
|
if (value > 0)
|
|
value = std::log(value) + 123;
|
|
for (int i = oFood; i <= oScience; ++i)
|
|
if (valueFormulaMultiply[i] && output[i] > 0)
|
|
value += valueFormulaWeight[i] * (std::log(static_cast<double>(output[i])) + 123);
|
|
}
|
|
|
|
const double valuePlus = value - bestValue;
|
|
if (superPlus > 0 || valuePlus >= 0.0) {
|
|
int subValue = (testReport.FoodRep + prodBeforeBoost + testReport.Trade) << 18;
|
|
int testTiles = 1 << Database::CityOwnTile;
|
|
for (int ih2 = 0; ih2 < nHierarchy; ++ih2) {
|
|
testTiles += hierarchy[ih2][nSelection[ih2]].V21;
|
|
subValue += hierarchy[ih2][nSelection[ih2]].SubValue;
|
|
}
|
|
bool isBest = true;
|
|
if (superPlus == 0 && valuePlus == 0.0) {
|
|
const int subPlus = subValue - bestSubValue;
|
|
if (subPlus < 0) {
|
|
isBest = false;
|
|
} else if (subPlus == 0) {
|
|
assert(testTiles != bestTiles);
|
|
isBest = testTiles > bestTiles;
|
|
}
|
|
}
|
|
if (isBest) {
|
|
bestSuperValue = superValue;
|
|
bestValue = value;
|
|
bestSubValue = subValue;
|
|
bestTiles = testTiles;
|
|
testReport.Happy = (cityReportEx.tradeProcessing.HappyBase - city.Size) / 2 +
|
|
(testReport.Lux >> 1);
|
|
advice.CityReport = testReport;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// calculate next combination
|
|
iH_Switch = 0;
|
|
do {
|
|
testReport.FoodRep -= hierarchy[iH_Switch][nSelection[iH_Switch]].Food;
|
|
prodBeforeBoost -= hierarchy[iH_Switch][nSelection[iH_Switch]].Prod;
|
|
testReport.Trade -= hierarchy[iH_Switch][nSelection[iH_Switch]].Trade;
|
|
++nSelection[iH_Switch];
|
|
++testReport.Working;
|
|
if (nSelection[iH_Switch] <= nTile[iH_Switch] && testReport.Working <= maxWorking) {
|
|
testReport.FoodRep += hierarchy[iH_Switch][nSelection[iH_Switch]].Food;
|
|
prodBeforeBoost += hierarchy[iH_Switch][nSelection[iH_Switch]].Prod;
|
|
testReport.Trade += hierarchy[iH_Switch][nSelection[iH_Switch]].Trade;
|
|
break;
|
|
}
|
|
testReport.Working -= nSelection[iH_Switch];
|
|
nSelection[iH_Switch] = 0;
|
|
++iH_Switch;
|
|
} while (iH_Switch != nHierarchy);
|
|
} while (iH_Switch != nHierarchy); // everything tested -- done
|
|
|
|
assert(bestSuperValue > 0); // advice should always be possible
|
|
advice.Tiles = static_cast<std::uint32_t>(bestTiles);
|
|
advice.CityReport.HypoTiles = bestTiles;
|
|
}
|
|
|
|
void initGame()
|
|
{
|
|
maxDist = Database::distance(0, Database::mapSize - (Database::lx >> 1));
|
|
for (int p = 0; p < nPl; ++p) {
|
|
if (((1 << p) & Database::gAlive) != 0) {
|
|
TPlayerContext &player = Database::rw[p];
|
|
const TModel *model = *player.Model;
|
|
int mixTownGuard = 0;
|
|
while (model[mixTownGuard].Kind != mkSpecial_TownGuard)
|
|
++mixTownGuard;
|
|
TCity &city = (*player.City)[0]; // initialize capital
|
|
city.Built[imPalace] = 1;
|
|
city.Size = 4;
|
|
for (int i = 2; i <= city.Size; ++i)
|
|
addBestCityTile(p, 0);
|
|
city.Project = mixTownGuard;
|
|
player.NatBuilt[imPalace - nWonder] = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
void releaseGame()
|
|
{
|
|
}
|
|
|
|
} // namespace CityProcessing
|
|
} // namespace Cevo
|