1701 lines
74 KiB
C++
1701 lines
74 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/Barbarina.h"
|
|
#include "Cevo/Geometry.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstdlib>
|
|
#include <functional>
|
|
#include <string>
|
|
|
|
namespace Cevo {
|
|
|
|
namespace {
|
|
|
|
int randomInt(int n) // Pascal's RTL Random(n); not ABI/determinism-relevant
|
|
{
|
|
return n > 0 ? std::rand() % n : 0;
|
|
}
|
|
|
|
double randomFloat() // Pascal's RTL Random (no args): float in [0, 1)
|
|
{
|
|
return static_cast<double>(std::rand()) / (static_cast<double>(RAND_MAX) + 1.0);
|
|
}
|
|
|
|
constexpr std::uint32_t ukSlow = 0x01;
|
|
constexpr std::uint32_t ukFast = 0x02;
|
|
|
|
constexpr int PresenceUnknown = 0x10000;
|
|
|
|
constexpr int WonderProductionThreshold = 15;
|
|
constexpr double WonderInclination = 24.0; // higher = lower probability of building a wonder
|
|
|
|
constexpr int mixTownGuard = 2;
|
|
|
|
struct ResearchModelInfo {
|
|
int Category;
|
|
int Domain;
|
|
int Weight;
|
|
int adStop;
|
|
std::uint32_t Upgrades;
|
|
int Cap[nFeature];
|
|
};
|
|
|
|
constexpr int nResearchModel = 16;
|
|
// Wea Arm Mob Sea Car Tur Bom Fue Air Nav Rad Sub Art Alp Sup Ove Air Spy SE NP Jet Ste Fan Fir Wil Aca Lin
|
|
constexpr ResearchModelInfo ResearchModel[nResearchModel] = {
|
|
{ ctGroundSlow, dGround, 7, adIronWorking, 0x0003, { 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctGroundFast, dGround, 7, adIronWorking, 0x0003, { 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctGroundSlow, dGround, 7, adExplosives, 0x003F, { 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctGroundFast, dGround, 7, adExplosives, 0x003F, { 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctSeaTrans, dSea, 7, adExplosives, 0x000F, { 0, 3, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctSeaArt, dSea, 7, adExplosives, 0x000F, { 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctGroundSlow, dGround, 7, adAutomobile, 0x00FF, { 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctGroundFast, dGround, 7, adAutomobile, 0x00FF, { 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctSeaTrans, dSea, 9, -1, 0x00FF, { 0, 4, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctSeaArt, dSea, 9, -1, 0x00FF, { 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctGroundSlow, dGround, 10, adCommunism, 0x05FF, { 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctGroundFast, dGround, 10, adCommunism, 0x05FF, { 5, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
|
|
{ ctGroundSlow, dGround, 10, adComposites, 0x07FF, { 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 } },
|
|
{ ctGroundFast, dGround, 10, adComposites, 0x07FF, { 5, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 } },
|
|
{ ctGroundSlow, dGround, 10, -1, 0x3FFF, { 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 } },
|
|
{ ctGroundFast, dGround, 10, -1, 0x3FFF, { 5, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 } },
|
|
};
|
|
|
|
constexpr int EntryModel_Base = 1;
|
|
constexpr int EntryModel_GunPowder = 3;
|
|
constexpr int EntryModel_MassProduction = 13;
|
|
|
|
constexpr int nResearchOrder = 40;
|
|
constexpr int ResearchOrder[nResearchOrder] = {
|
|
adBronzeWorking, -adMapMaking, adChivalry, adMonotheism, adIronWorking,
|
|
adGunpowder, adTheology, adConstruction, adCodeOfLaws, -adEngineering,
|
|
-adSeafaring, -adNavigation, adMetallurgy, adBallistics, adScience, adExplosives,
|
|
adTactics, adSteel, -adSteamEngine, -adAmphibiousWarfare, -adMagnetism, adRadio,
|
|
adAutomobile, adMobileWarfare, adRailroad, adCommunism, adDemocracy,
|
|
adTheCorporation, adMassProduction, adIndustrialization, adRobotics, adComposites,
|
|
adTheLaser, adFlight, adAdvancedFlight, adSpaceFlight,
|
|
adSyntheticFood, adTransstellarColonization, adElectronics, adSmartWeapons,
|
|
};
|
|
|
|
} // namespace
|
|
|
|
std::array<bool, nUmax> Barbarina::moved{};
|
|
std::array<std::uint8_t, lxmax * lymax> Barbarina::unitPresence{};
|
|
std::array<std::int16_t, lxmax * lymax> Barbarina::euixMap{};
|
|
std::array<std::int16_t, Barbarina::neumax> Barbarina::uixAttack{};
|
|
std::array<int, Barbarina::neumax> Barbarina::attackScore{};
|
|
|
|
Barbarina::Barbarina(int nation) : ToolAI(nation) {}
|
|
|
|
// whether one of the existing models matches a specific research model
|
|
bool Barbarina::isModelAvailable(int rmix) const
|
|
{
|
|
const ResearchModelInfo &rm = ResearchModel[rmix];
|
|
const int mStrength = currentMStrength(rm.Domain);
|
|
for (int mix = 3; mix < ro->nModel; ++mix) {
|
|
const TModel &model = myModel[mix];
|
|
if ((model.Kind == mkSelfDeveloped || model.Kind == mkEnemyDeveloped) && model.Domain == rm.Domain &&
|
|
(rm.Upgrades & ~model.Upgrades) == 0) {
|
|
// for future techs: don't count model available if 50% stronger possible
|
|
bool result = mStrength < (model.MStrength * 3) / 2;
|
|
for (int i = 0; i < nFeature && result; ++i)
|
|
if (model.Cap[i] < rm.Cap[i])
|
|
result = false;
|
|
if (result)
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool Barbarina::barbarina_GoHidden() const
|
|
{
|
|
if (isResearched(adMassProduction)) {
|
|
bool result = true;
|
|
for (int cix = 0; cix < ro->nCity && result; ++cix) {
|
|
const TCity &city = myCity[cix];
|
|
if (city.Loc >= 0) { // search for modern resource
|
|
TVicinity21Loc radius;
|
|
V21_to_Loc(city.Loc, radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
const int loc1 = radius[v21];
|
|
if (loc1 >= 0 && (map[loc1] & fModern) != 0) {
|
|
result = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
if (isResearched(adGunpowder))
|
|
return ro->Tech[adTheRepublic] < tsSeen && isResearched(adTheology);
|
|
return false;
|
|
}
|
|
|
|
bool Barbarina::barbarina_Go() const
|
|
{
|
|
bool result;
|
|
if (isResearched(adMassProduction))
|
|
result = isResearched(adTheology) && isModelAvailable(EntryModel_MassProduction);
|
|
else if (isResearched(adGunpowder))
|
|
result = isResearched(adTheology) && isResearched(adMapMaking) && isModelAvailable(EntryModel_GunPowder);
|
|
else
|
|
return ro->nCity >= 3 && isResearched(adMapMaking) && isModelAvailable(EntryModel_Base);
|
|
return result && (ro->nUn >= ro->nCity * 3 || ro->Wonder[woZeus].EffectiveOwner == me);
|
|
}
|
|
|
|
// Hides (does NOT override) ToolAI::analyzeMap() - see Barbarina.h. Collects
|
|
// nation-presence information for continents/oceans given Formation[]
|
|
// already populated.
|
|
void Barbarina::analyzeMap()
|
|
{
|
|
if (turnOfMapAnalysis == ro->Turn)
|
|
return;
|
|
|
|
// Note: the original's own `// inherited;` is commented out too - it
|
|
// relies on ToolAI::analyzeMap() having already been called elsewhere
|
|
// (by AI.pas's own AnalyzeMap, which does chain to both).
|
|
|
|
continentPresence.fill(0);
|
|
oceanPresence.fill(0);
|
|
continentSize.fill(0);
|
|
oceanSize.fill(0);
|
|
for (int loc = 0; loc < MapSize(); ++loc) {
|
|
int f1 = formation[loc];
|
|
if (f1 >= 0 && f1 < maxCOD) {
|
|
const int p1 = (*ro->Territory)[loc];
|
|
if (p1 >= 0) {
|
|
if ((map[loc] & fTerrain) >= static_cast<std::uint32_t>(fGrass)) {
|
|
++continentSize[f1];
|
|
continentPresence[f1] |= (1 << p1);
|
|
} else {
|
|
++oceanSize[f1];
|
|
oceanPresence[f1] |= (1 << p1);
|
|
}
|
|
}
|
|
} else if (f1 == nfUndiscovered) { // adjacent formations are not completely discovered
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(loc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int loc1 = adjacent[v8];
|
|
if (loc1 >= 0) {
|
|
f1 = formation[loc1];
|
|
if (f1 >= 0 && f1 < maxCOD) {
|
|
if ((map[loc1] & fTerrain) >= static_cast<std::uint32_t>(fGrass))
|
|
continentPresence[f1] |= PresenceUnknown;
|
|
else
|
|
oceanPresence[f1] |= PresenceUnknown;
|
|
}
|
|
}
|
|
}
|
|
} else if (f1 == nfPeace) { // nation present in adjacent formations
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(loc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int loc1 = adjacent[v8];
|
|
if (loc1 >= 0) {
|
|
f1 = formation[loc1];
|
|
if (f1 >= 0 && f1 < maxCOD) {
|
|
if ((map[loc1] & fTerrain) >= static_cast<std::uint32_t>(fGrass))
|
|
continentPresence[f1] |= (1 << (*ro->Territory)[loc]);
|
|
else
|
|
oceanPresence[f1] |= (1 << (*ro->Territory)[loc]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
neighbours = 0;
|
|
for (int cix = 0; cix < ro->nCity; ++cix) {
|
|
const int loc = myCity[cix].Loc;
|
|
if (loc >= 0 && formation[loc] >= 0 && formation[loc] < maxCOD)
|
|
neighbours |= continentPresence[formation[loc]];
|
|
}
|
|
neighbours &= ~PresenceUnknown;
|
|
|
|
turnOfMapAnalysis = ro->Turn;
|
|
}
|
|
|
|
void Barbarina::findBestModels()
|
|
{
|
|
for (int i = 0; i < nModelCategory; ++i)
|
|
mixBest[i] = -1;
|
|
for (int rmix = nResearchModel - 1; rmix >= 0; --rmix) {
|
|
const ResearchModelInfo &rm = ResearchModel[rmix];
|
|
if (mixBest[rm.Category] < 0) {
|
|
for (int mix = 3; mix < ro->nModel; ++mix) {
|
|
const TModel &model = myModel[mix];
|
|
if (model.Domain == rm.Domain && (rm.Upgrades & ~model.Upgrades) == 0) {
|
|
mixBest[rm.Category] = mix;
|
|
for (int i = 0; i < nFeature; ++i)
|
|
if (model.Cap[i] < rm.Cap[i]) {
|
|
mixBest[rm.Category] = -1;
|
|
break;
|
|
}
|
|
if (mixBest[rm.Category] >= 0)
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (int mix = 3; mix < ro->nModel; ++mix) {
|
|
const TModel &model = myModel[mix];
|
|
if (model.Kind <= mkEnemyDeveloped) {
|
|
int cat = -1;
|
|
switch (model.Domain) {
|
|
case dGround:
|
|
cat = model.Speed >= 250 ? ctGroundFast : ctGroundSlow;
|
|
break;
|
|
case dSea:
|
|
if (model.Cap[mcSeaTrans] > 0)
|
|
cat = ctSeaTrans;
|
|
else if (model.Cap[mcArtillery] > 0)
|
|
cat = ctSeaArt;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if (cat >= 0 && mix != mixBest[cat] &&
|
|
(mixBest[cat] < 0 || model.Weight * model.MStrength >
|
|
myModel[mixBest[cat]].Weight + myModel[mixBest[cat]].MStrength))
|
|
mixBest[cat] = mix;
|
|
}
|
|
}
|
|
if (mixBest[ctSeaTrans] < 0 && !isResearched(adExplosives)) { // longboat?
|
|
for (int mix = 3; mix < ro->nModel; ++mix) {
|
|
if (myModel[mix].Cap[mcSeaTrans] > 0) {
|
|
mixBest[ctSeaTrans] = mix;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Barbarina::barbarina_DoTurn()
|
|
{
|
|
if ((ro->Government == gRepublic || ro->Government == gDemocracy || ro->Government == gFuture) ||
|
|
(ro->Government != gFundamentalism && ro->Government != gAnarchy && isResearched(adTheology)))
|
|
revolution();
|
|
|
|
analyzeMap();
|
|
|
|
findBestModels();
|
|
|
|
attackAndPatrol();
|
|
}
|
|
|
|
// find one unit to destroy each known enemy unit, result in uixAttack
|
|
void Barbarina::rateAttack(int uix)
|
|
{
|
|
TUn &unit = myUnit[uix];
|
|
if (unit.Movement <= 0)
|
|
return;
|
|
|
|
int bestScore = 0;
|
|
int euixBest = -1;
|
|
static std::array<bool, lxmax * lymax> reached;
|
|
reached.fill(false);
|
|
const int moveStyle = getMyMoveStyle(unit.mix, unit.Health);
|
|
pile.create(MapSize());
|
|
pile.put(unit.Loc, 0x800 - unit.Movement);
|
|
int testLoc, testTime;
|
|
while (pile.get(testLoc, testTime)) {
|
|
reached[testLoc] = true;
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(testLoc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int nextLoc = adjacent[v8];
|
|
if (nextLoc < 0 || reached[nextLoc])
|
|
continue;
|
|
const std::uint32_t nextTile = map[nextLoc];
|
|
if (euixMap[nextLoc] >= 0) { // check attack
|
|
const TUnitInfo &defense = (*ro->EnemyUn)[euixMap[nextLoc]];
|
|
int remHealth;
|
|
if (unit_AttackForecast(uix, nextLoc, 0x800 - testTime, remHealth)) {
|
|
int score = 0;
|
|
if (remHealth <= 0) { // send unit into death?
|
|
if (0x800 - testTime >= 100 &&
|
|
((myModel[unit.mix].Domain == dGround &&
|
|
(nextTile & fTerrain) >= static_cast<std::uint32_t>(fGrass)) ||
|
|
(myModel[unit.mix].Domain == dSea &&
|
|
(nextTile & fTerrain) < static_cast<std::uint32_t>(fGrass))) &&
|
|
myModel[unit.mix].Attack > myModel[unit.mix].Defense) {
|
|
score = (defense.Health + remHealth) * (*ro->EnemyModel)[defense.emix].Cost * 2 /
|
|
myModel[unit.mix].Cost;
|
|
if (nextTile & fCity)
|
|
score *= 4;
|
|
}
|
|
} else {
|
|
score = (*ro->EnemyModel)[defense.emix].Cost * 25 -
|
|
(((unit.Health - remHealth) * myModel[unit.mix].Cost) >> 4);
|
|
}
|
|
if (score > bestScore && score > attackScore[euixMap[nextLoc]]) {
|
|
bestScore = score;
|
|
euixBest = euixMap[nextLoc];
|
|
}
|
|
}
|
|
} else if ((nextTile & (fUnit | fCity)) == 0 || (nextTile & fOwned) != 0) {
|
|
int nextTime, recoverTurns;
|
|
const int stepResult =
|
|
checkStep(moveStyle, testTime, v8 & 1, nextTime, recoverTurns, map[testLoc], nextTile, true);
|
|
if (stepResult == csOk) {
|
|
if (nextTime < 0x800)
|
|
pile.put(nextLoc, nextTime);
|
|
} else if (stepResult == csForbiddenTile) {
|
|
reached[nextLoc] = true; // don't check moving there again
|
|
} else if (stepResult == csCheckTerritory) {
|
|
if (nextTime < 0x800 && (*ro->Territory)[nextLoc] == (*ro->Territory)[testLoc])
|
|
pile.put(nextLoc, nextTime);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
pile.free();
|
|
|
|
if (bestScore > 0) {
|
|
const int uixOld = uixAttack[euixBest];
|
|
attackScore[euixBest] = bestScore;
|
|
uixAttack[euixBest] = static_cast<std::int16_t>(uix);
|
|
if (uixOld >= 0)
|
|
rateAttack(uixOld);
|
|
}
|
|
}
|
|
|
|
// attackLoc == maNextCity means bombard only
|
|
bool Barbarina::doAttack(int uix, int attackLoc)
|
|
{
|
|
const bool isBombardment = attackLoc == maNextCity;
|
|
TUn &unit = myUnit[uix];
|
|
int kind = 0;
|
|
if (myModel[unit.mix].Domain == dGround && myModel[unit.mix].Attack > 0)
|
|
kind = myModel[unit.mix].Speed >= 250 ? ukFast : ukSlow;
|
|
|
|
static std::array<bool, lxmax * lymax> reached;
|
|
reached.fill(false);
|
|
static std::array<std::uint16_t, lxmax * lymax> preLoc;
|
|
bool attackPositionReached = false;
|
|
const int moveStyle = getMyMoveStyle(unit.mix, unit.Health);
|
|
pile.create(MapSize());
|
|
pile.put(unit.Loc, 0x800 - unit.Movement);
|
|
int testLoc, testTime;
|
|
int nextLoc = -1;
|
|
while (pile.get(testLoc, testTime)) {
|
|
if (testTime >= 0x800 || (attackLoc == maNextCity && testTime > 0x800 - 100))
|
|
break;
|
|
reached[testLoc] = true;
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(testLoc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
nextLoc = adjacent[v8];
|
|
if (nextLoc < 0)
|
|
continue;
|
|
if (isBombardment &&
|
|
(map[nextLoc] & (fCity | fUnit | fOwned | fObserved)) == (fCity | fObserved) &&
|
|
ro->Treaty[(*ro->Territory)[nextLoc]] < trPeace) {
|
|
const int ecix = city_FindEnemyCity(nextLoc);
|
|
const TCityInfo &enemyCity = (*ro->EnemyCity)[ecix];
|
|
if (enemyCity.Size > 2 && (enemyCity.Flags & ciCoastalFort) == 0)
|
|
attackLoc = nextLoc;
|
|
}
|
|
if (nextLoc == attackLoc &&
|
|
(myModel[unit.mix].Domain != dSea ||
|
|
(map[testLoc] & fTerrain) < static_cast<std::uint32_t>(fGrass))) {
|
|
// ships can only attack from water
|
|
attackPositionReached = true;
|
|
break;
|
|
}
|
|
if (!reached[nextLoc]) {
|
|
const std::uint32_t nextTile = map[nextLoc];
|
|
if ((nextTile & (fUnit | fCity)) == 0 || (nextTile & fOwned) != 0) {
|
|
int nextTime, recoverTurns;
|
|
const int stepResult = checkStep(moveStyle, testTime, v8 & 1, nextTime, recoverTurns,
|
|
map[testLoc], nextTile, true);
|
|
if (stepResult == csOk) {
|
|
if (pile.put(nextLoc, nextTime))
|
|
preLoc[nextLoc] = static_cast<std::uint16_t>(testLoc);
|
|
} else if (stepResult == csForbiddenTile) {
|
|
reached[nextLoc] = true; // don't check moving there again
|
|
} else if (stepResult == csCheckTerritory) {
|
|
if ((*ro->Territory)[nextLoc] == (*ro->Territory)[testLoc]) {
|
|
if (pile.put(nextLoc, nextTime))
|
|
preLoc[nextLoc] = static_cast<std::uint16_t>(testLoc);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (attackPositionReached) {
|
|
preLoc[nextLoc] = static_cast<std::uint16_t>(testLoc);
|
|
break;
|
|
}
|
|
}
|
|
pile.free();
|
|
if (!attackPositionReached)
|
|
return false;
|
|
|
|
testLoc = attackLoc;
|
|
nextLoc = preLoc[testLoc];
|
|
while (testLoc != unit.Loc) {
|
|
const int temp = testLoc;
|
|
testLoc = nextLoc;
|
|
nextLoc = preLoc[testLoc];
|
|
preLoc[testLoc] = static_cast<std::uint16_t>(temp);
|
|
}
|
|
|
|
unitPresence[unit.Loc] &= static_cast<std::uint8_t>(~kind); // assume unit was only one of kind here
|
|
int moveResult;
|
|
do {
|
|
nextLoc = preLoc[unit.Loc];
|
|
moveResult = unit_Step(uix, nextLoc);
|
|
} while (!(nextLoc == attackLoc || (moveResult & rExecuted) == 0 || (moveResult & rUnitRemoved) != 0));
|
|
const bool result = nextLoc == attackLoc && (moveResult & rExecuted) != 0;
|
|
|
|
if (isBombardment && result) {
|
|
const int ecix = city_FindEnemyCity(attackLoc);
|
|
while (unit.Movement >= 100 && (*ro->EnemyCity)[ecix].Size > 2)
|
|
unit_Step(uix, attackLoc);
|
|
}
|
|
|
|
if (unit.Loc >= 0)
|
|
unitPresence[unit.Loc] |= static_cast<std::uint8_t>(kind);
|
|
return result;
|
|
}
|
|
|
|
// return true if no new enemy spotted
|
|
bool Barbarina::processMove(int uix)
|
|
{
|
|
constexpr int DistanceScore = 4;
|
|
bool result = true;
|
|
bool done = false;
|
|
|
|
static std::array<std::int8_t, lxmax * lymax> adjacentUnknown;
|
|
static std::array<std::uint16_t, lxmax * lymax> preLoc;
|
|
static std::array<std::uint8_t, lxmax * lymax> moreTurn;
|
|
|
|
while (!done) {
|
|
TUn &unit = myUnit[uix];
|
|
if (myModel[unit.mix].Domain == dSea && unit.Health < 100 &&
|
|
(unit.Health < 34 || myModel[unit.mix].Cap[mcSeaTrans] > 0)) {
|
|
if ((map[unit.Loc] & fCity) == 0)
|
|
unit_MoveEx(uix, maNextCity);
|
|
return result;
|
|
}
|
|
|
|
int kind = 0;
|
|
if (myModel[unit.mix].Domain == dGround && myModel[unit.mix].Attack > 0)
|
|
kind = myModel[unit.mix].Speed >= 250 ? ukFast : ukSlow;
|
|
const bool captureOnly =
|
|
unit.Health < 100 &&
|
|
((map[unit.Loc] & fCity) != 0 ||
|
|
(((100 - unit.Health) * Terrain[map[unit.Loc] & fTerrain].Defense > 60) &&
|
|
!(static_cast<int>(map[unit.Loc] & fTerrain) == fOcean ||
|
|
static_cast<int>(map[unit.Loc] & fTerrain) == fShore ||
|
|
static_cast<int>(map[unit.Loc] & fTerrain) == fArctic ||
|
|
static_cast<int>(map[unit.Loc] & fTerrain) == fDesert)));
|
|
const int moveStyle = getMyMoveStyle(unit.mix, unit.Health);
|
|
|
|
const int maxScoreLocal = myModel[unit.mix].Attack > 0 ? 0x400 : 0x400 - 32 + 5;
|
|
int patrolScore = -999999;
|
|
int patrolLoc = -1;
|
|
int bestCount = 0;
|
|
std::fill_n(adjacentUnknown.begin(), MapSize(), static_cast<std::int8_t>(-1));
|
|
|
|
pile.create(MapSize());
|
|
pile.put(unit.Loc, 0x800 - unit.Movement);
|
|
int testLoc, testTime;
|
|
while (pile.get(testLoc, testTime)) {
|
|
// assume a score of $400 is the best achievable
|
|
if (maxScoreLocal * 0x1000 - DistanceScore * testTime <= patrolScore ||
|
|
(captureOnly && testTime >= 0x1000))
|
|
break;
|
|
|
|
int testScore = 0;
|
|
const std::uint32_t tile = map[testLoc];
|
|
const int terrOwner = (*ro->Territory)[testLoc];
|
|
adjacentUnknown[testLoc] = 0;
|
|
bool peaceBorder = false;
|
|
bool nextToEnemyCity = false;
|
|
|
|
if ((tile & fCity) != 0 && (tile & fOwned) == 0) {
|
|
if (myModel[unit.mix].Domain == dGround && myModel[unit.mix].Attack > 0 &&
|
|
// TerrOwner<0 happens only for unobserved cities of extinct tribes, new owner unknown
|
|
(terrOwner < 0 || ro->Treaty[terrOwner] < trPeace)) {
|
|
if ((tile & fObserved) != 0 && (tile & fUnit) == 0)
|
|
testScore = 0x400; // unfriendly undefended city -- capture!
|
|
else
|
|
testScore = 0x400 - 14; // unfriendly city, not observed or defended
|
|
}
|
|
} else { // no enemy city or unit here
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(testLoc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int nextLoc = adjacent[v8];
|
|
if (nextLoc >= 0 && adjacentUnknown[nextLoc] < 0) {
|
|
const std::uint32_t nextTile = map[nextLoc];
|
|
if ((nextTile & fTerrain) == static_cast<std::uint32_t>(fUNKNOWN)) {
|
|
++adjacentUnknown[testLoc];
|
|
} else if ((nextTile & fTerrain) == static_cast<std::uint32_t>(fArctic)) {
|
|
// nothing
|
|
} else if ((nextTile & (fCity | fUnit | fOwned | fObserved)) == (fCity | fUnit | fObserved)) {
|
|
nextToEnemyCity = true;
|
|
} else {
|
|
int nextTime, recoverTurns;
|
|
const int stepResult = checkStep(moveStyle, testTime, v8 & 1, nextTime, recoverTurns,
|
|
tile, nextTile, true);
|
|
if (stepResult == csOk) {
|
|
if (pile.put(nextLoc, nextTime + recoverTurns * 0x1000)) {
|
|
preLoc[nextLoc] = static_cast<std::uint16_t>(testLoc);
|
|
moreTurn[nextLoc] = static_cast<std::uint8_t>((nextTime >> 12) & 0xFFF);
|
|
}
|
|
} else if (stepResult == csForbiddenTile) {
|
|
adjacentUnknown[nextLoc] = 0; // don't check moving there again
|
|
if (nextTile & fPeace)
|
|
peaceBorder = true;
|
|
} else if (stepResult == csCheckTerritory) {
|
|
if ((*ro->Territory)[nextLoc] == terrOwner) {
|
|
if (pile.put(nextLoc, nextTime + recoverTurns * 0x1000)) {
|
|
preLoc[nextLoc] = static_cast<std::uint16_t>(testLoc);
|
|
moreTurn[nextLoc] = static_cast<std::uint8_t>((nextTime >> 12) & 0xFFF);
|
|
}
|
|
} else {
|
|
peaceBorder = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!captureOnly) {
|
|
if (nextToEnemyCity && myModel[unit.mix].Attack > 0 && myModel[unit.mix].Domain == dGround)
|
|
testScore = 0x400 - 14;
|
|
else if (adjacentUnknown[testLoc] > 0) {
|
|
if (peaceBorder || (terrOwner >= 0 && terrOwner != me && ro->Treaty[terrOwner] < trPeace))
|
|
testScore = 0x400 - 32 + adjacentUnknown[testLoc];
|
|
else
|
|
testScore = 0x400 - 64 + adjacentUnknown[testLoc];
|
|
} else if (peaceBorder) {
|
|
testScore = 0x400 - 32;
|
|
} else {
|
|
testScore = (ro->Turn - (*ro->MapObservedLast)[testLoc]) / 16;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (testScore > 0) {
|
|
testScore = testScore * 0x1000 - DistanceScore * testTime;
|
|
if (testScore > patrolScore)
|
|
bestCount = 0;
|
|
if (testScore >= patrolScore) {
|
|
++bestCount;
|
|
if (randomInt(bestCount) == 0) {
|
|
patrolScore = testScore;
|
|
patrolLoc = testLoc;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
pile.free();
|
|
|
|
if (patrolLoc >= 0 && patrolLoc != unit.Loc) { // capture/discover/patrol task found, execute it
|
|
while (patrolLoc != unit.Loc && moreTurn[patrolLoc] > 0 &&
|
|
(moreTurn[patrolLoc] > 1 ||
|
|
!(static_cast<int>(map[patrolLoc] & fTerrain) == fMountains ||
|
|
static_cast<int>(map[patrolLoc] & fTerrain) == fDesert ||
|
|
static_cast<int>(map[patrolLoc] & fTerrain) == fArctic))) {
|
|
patrolLoc = preLoc[patrolLoc];
|
|
done = true; // no effect if enemy spotted
|
|
}
|
|
while (patrolLoc != unit.Loc && (unitPresence[patrolLoc] & kind) != 0 && (map[patrolLoc] & fCity) == 0 &&
|
|
(map[patrolLoc] & fTerImp) != static_cast<std::uint32_t>(tiFort) &&
|
|
(map[patrolLoc] & fTerImp) != static_cast<std::uint32_t>(tiBase) &&
|
|
!(static_cast<int>(map[preLoc[patrolLoc]] & fTerrain) == fDesert ||
|
|
static_cast<int>(map[preLoc[patrolLoc]] & fTerrain) == fArctic)) {
|
|
patrolLoc = preLoc[patrolLoc];
|
|
done = true; // no effect if enemy spotted
|
|
}
|
|
if (patrolLoc == unit.Loc)
|
|
return result;
|
|
|
|
testLoc = patrolLoc;
|
|
int nextLoc = preLoc[testLoc];
|
|
while (testLoc != unit.Loc) {
|
|
const int temp = testLoc;
|
|
testLoc = nextLoc;
|
|
nextLoc = preLoc[testLoc];
|
|
preLoc[testLoc] = static_cast<std::uint16_t>(temp);
|
|
}
|
|
|
|
unitPresence[unit.Loc] &= static_cast<std::uint8_t>(~kind); // assume unit was only one of kind here
|
|
while (unit.Loc != patrolLoc) {
|
|
nextLoc = preLoc[unit.Loc];
|
|
const int moveResult = unit_Step(uix, nextLoc);
|
|
if ((moveResult & (rUnitRemoved | rEnemySpotted)) != 0 || (moveResult & rExecuted) == 0) {
|
|
if ((moveResult & rExecuted) == 0)
|
|
moved[uix] = true;
|
|
result = (moveResult & rEnemySpotted) == 0;
|
|
done = true;
|
|
break;
|
|
}
|
|
}
|
|
if (unit.Loc >= 0) {
|
|
unitPresence[unit.Loc] |= static_cast<std::uint8_t>(kind);
|
|
if (map[unit.Loc] & fCity) {
|
|
moved[uix] = true;
|
|
done = true; // stay in captured city as defender
|
|
}
|
|
}
|
|
} else {
|
|
done = true;
|
|
}
|
|
}
|
|
if (result)
|
|
moved[uix] = true;
|
|
return result;
|
|
}
|
|
|
|
void Barbarina::attackAndPatrol()
|
|
{
|
|
auto setCityDefenders = [this]() {
|
|
for (int cix = 0; cix < ro->nCity; ++cix) {
|
|
const TCity &city = myCity[cix];
|
|
if (city.Loc < 0)
|
|
continue;
|
|
bool isPort = false;
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(city.Loc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int loc1 = adjacent[v8];
|
|
if (loc1 >= 0 && (map[loc1] & fTerrain) < static_cast<std::uint32_t>(fGrass) &&
|
|
formation[loc1] >= 0 && formation[loc1] < maxCOD &&
|
|
(oceanPresence[formation[loc1]] & ~neighbours) != 0)
|
|
isPort = true;
|
|
}
|
|
int best = -1;
|
|
int uixBest = -1;
|
|
for (int uix = 0; uix < ro->nUn; ++uix) {
|
|
if (myUnit[uix].Loc != city.Loc)
|
|
continue;
|
|
TUn &unit = myUnit[uix];
|
|
if (myModel[unit.mix].Domain != dGround || myModel[unit.mix].Attack <= 0)
|
|
continue;
|
|
int det;
|
|
if (unit.mix == mixTownGuard && ro->Government == gDespotism) {
|
|
det = 1 << 16; // town guard
|
|
moved[uix] = true;
|
|
} else if (isPort) {
|
|
det = (myModel[unit.mix].Defense << 8) +
|
|
((static_cast<int>(unit.Flags) & unFortified) << 7) - unit.Health;
|
|
} else {
|
|
det = (myModel[unit.mix].Speed << 8) +
|
|
((static_cast<int>(unit.Flags) & unFortified) << 7) - unit.Health;
|
|
}
|
|
if (det > best) {
|
|
best = det;
|
|
uixBest = uix;
|
|
}
|
|
}
|
|
if (best >= 0)
|
|
moved[uixBest] = true;
|
|
}
|
|
};
|
|
|
|
auto processSeaTransport = [this]() {
|
|
bool go = false;
|
|
for (int f = 0; f < nContinent && f < maxCOD; ++f) {
|
|
if ((continentPresence[f] & ~((1 << me) | PresenceUnknown)) != 0) { // any enemy island known?
|
|
go = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!go)
|
|
return;
|
|
|
|
seaTransport_BeginInitialize();
|
|
go = false;
|
|
for (int uix = 0; uix < ro->nUn; ++uix) {
|
|
if (moved[uix])
|
|
continue;
|
|
TUn &unit = myUnit[uix];
|
|
if (unit.Loc >= 0 && myModel[unit.mix].Domain == dGround && myModel[unit.mix].Attack > 0 &&
|
|
(map[unit.Loc] & fTerrain) >= static_cast<std::uint32_t>(fGrass)) {
|
|
const int f = formation[unit.Loc];
|
|
if (f >= 0 && f < maxCOD && (continentPresence[f] & ~(1 << me)) == 0) {
|
|
go = true;
|
|
seaTransport_AddLoad(uix);
|
|
}
|
|
}
|
|
}
|
|
if (go) {
|
|
go = false;
|
|
for (int uix = 0; uix < ro->nUn; ++uix) {
|
|
if (moved[uix])
|
|
continue;
|
|
TUn &unit = myUnit[uix];
|
|
if (unit.Loc >= 0 && unit.mix == mixBest[ctSeaTrans] && unit.TroopLoad == 0 && unit.Health == 100) {
|
|
go = true;
|
|
seaTransport_AddTransport(uix);
|
|
}
|
|
}
|
|
}
|
|
if (go) {
|
|
for (int loc1 = 0; loc1 < MapSize(); ++loc1) {
|
|
if ((map[loc1] & fTerrain) >= static_cast<std::uint32_t>(fGrass)) {
|
|
const int f = formation[loc1];
|
|
if (f >= 0 && f < maxCOD && (continentPresence[f] & ~((1 << me) | PresenceUnknown)) != 0)
|
|
seaTransport_AddDestination(loc1);
|
|
}
|
|
}
|
|
}
|
|
seaTransport_EndInitialize();
|
|
GroupTransportPlan transportPlan;
|
|
while (seaTransport_MakeGroupPlan(transportPlan)) {
|
|
moved[transportPlan.uixTransport] = true;
|
|
bool ready = myUnit[transportPlan.uixTransport].Loc == transportPlan.LoadLoc;
|
|
if (!ready) {
|
|
unit_MoveEx(transportPlan.uixTransport, transportPlan.LoadLoc);
|
|
ready = myUnit[transportPlan.uixTransport].Loc == transportPlan.LoadLoc;
|
|
}
|
|
if (ready) {
|
|
for (int i = 0; i < transportPlan.nLoad; ++i) {
|
|
int a, b;
|
|
Loc_to_ab(transportPlan.LoadLoc, myUnit[transportPlan.uixLoad[i]].Loc, a, b);
|
|
ready = ready && std::abs(a) <= 1 && std::abs(b) <= 1;
|
|
}
|
|
}
|
|
if (ready) {
|
|
for (int i = 0; i < transportPlan.nLoad; ++i) {
|
|
unit_Step(transportPlan.uixLoad[i], transportPlan.LoadLoc);
|
|
moved[transportPlan.uixLoad[i]] = true;
|
|
}
|
|
} else {
|
|
for (int i = 0; i < transportPlan.nLoad; ++i) {
|
|
unit_MoveEx(transportPlan.uixLoad[i], transportPlan.LoadLoc, mxAdjacent);
|
|
moved[transportPlan.uixLoad[i]] = true;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
auto processUnload = [this](int uix) {
|
|
auto unload = [this, uix](std::uint32_t kind, int toLoc) {
|
|
for (int uix1 = 0; uix1 < ro->nUn; ++uix1) {
|
|
TUn &u = myUnit[uix1];
|
|
if (u.Loc >= 0 && u.Master == uix && myModel[u.mix].Domain == dGround &&
|
|
myModel[u.mix].Attack > 0 && u.Movement == myModel[u.mix].Speed &&
|
|
((myModel[u.mix].Speed >= 250) == (kind == ukFast))) {
|
|
unit_Step(uix1, toLoc);
|
|
unitPresence[toLoc] |= static_cast<std::uint8_t>(kind);
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
// inventory
|
|
int nSlow = 0, nFast = 0;
|
|
for (int uix1 = 0; uix1 < ro->nUn; ++uix1) {
|
|
const TUn &u = myUnit[uix1];
|
|
if (u.Loc >= 0 && u.Master == uix && myModel[u.mix].Domain == dGround && myModel[u.mix].Attack > 0) {
|
|
if (myModel[u.mix].Speed >= 250)
|
|
++nFast;
|
|
else
|
|
++nSlow;
|
|
}
|
|
}
|
|
|
|
TUn &unit = myUnit[uix];
|
|
const int moveStyle = getMyMoveStyle(unit.mix, unit.Health);
|
|
static std::array<bool, lxmax * lymax> reached;
|
|
for (;;) {
|
|
int slowUnloadLoc = -1, fastUnloadLoc = -1, endLoc = -1;
|
|
reached.fill(false);
|
|
pile.create(MapSize());
|
|
pile.put(unit.Loc, 0x800 - unit.Movement);
|
|
int testLoc, testTime;
|
|
while (slowUnloadLoc < 0 && fastUnloadLoc < 0 && pile.get(testLoc, testTime)) {
|
|
reached[testLoc] = true;
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(testLoc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int nextLoc = adjacent[v8];
|
|
if (nextLoc < 0 || reached[nextLoc])
|
|
continue;
|
|
const std::uint32_t nextTile = map[nextLoc];
|
|
if ((nextTile & fTerrain) == static_cast<std::uint32_t>(fUNKNOWN)) {
|
|
// nothing
|
|
} else if ((nextTile & fTerrain) >= static_cast<std::uint32_t>(fGrass)) {
|
|
const int f = formation[nextLoc];
|
|
if (f >= 0 && f < maxCOD &&
|
|
(continentPresence[f] & ~((1 << me) | PresenceUnknown)) != 0 &&
|
|
(nextTile & (fUnit | fOwned)) != fUnit) {
|
|
if (nSlow > 0 && (unitPresence[nextLoc] & ukSlow) == 0 &&
|
|
(slowUnloadLoc < 0 || Terrain[map[nextLoc] & fTerrain].Defense >
|
|
Terrain[map[slowUnloadLoc] & fTerrain].Defense)) {
|
|
endLoc = testLoc;
|
|
slowUnloadLoc = nextLoc;
|
|
}
|
|
if (nFast > 0 && (unitPresence[nextLoc] & ukFast) == 0 &&
|
|
(fastUnloadLoc < 0 || Terrain[map[nextLoc] & fTerrain].Defense >
|
|
Terrain[map[fastUnloadLoc] & fTerrain].Defense)) {
|
|
endLoc = testLoc;
|
|
fastUnloadLoc = nextLoc;
|
|
}
|
|
}
|
|
} else if (endLoc < 0) {
|
|
int nextTime, recoverTurns;
|
|
const int stepResult = checkStep(moveStyle, testTime, v8 & 1, nextTime, recoverTurns,
|
|
map[testLoc], nextTile, true);
|
|
if (stepResult == csOk) {
|
|
pile.put(nextLoc, nextTime);
|
|
} else if (stepResult == csForbiddenTile) {
|
|
reached[nextLoc] = true; // don't check moving there again
|
|
} else if (stepResult == csCheckTerritory) {
|
|
if ((*ro->Territory)[nextLoc] == (*ro->Territory)[testLoc])
|
|
pile.put(nextLoc, nextTime);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
pile.free();
|
|
|
|
if (endLoc < 0)
|
|
return;
|
|
if (unit.Loc != endLoc)
|
|
unit_MoveEx(uix, endLoc);
|
|
if (unit.Loc != endLoc)
|
|
return;
|
|
if (slowUnloadLoc >= 0) {
|
|
unload(ukSlow, slowUnloadLoc);
|
|
--nSlow;
|
|
}
|
|
if (fastUnloadLoc >= 0) {
|
|
unload(ukFast, fastUnloadLoc);
|
|
--nFast;
|
|
}
|
|
if (unit.TroopLoad == 0) {
|
|
moved[uix] = false;
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
|
|
unitPresence.fill(0);
|
|
for (int uix = 0; uix < ro->nUn; ++uix) {
|
|
const TUn &unit = myUnit[uix];
|
|
if (unit.Loc >= 0 && myModel[unit.mix].Domain == dGround && myModel[unit.mix].Attack > 0) {
|
|
const std::uint32_t kind = myModel[unit.mix].Speed >= 250 ? ukFast : ukSlow;
|
|
unitPresence[unit.Loc] |= static_cast<std::uint8_t>(kind);
|
|
}
|
|
}
|
|
|
|
std::fill_n(moved.begin(), ro->nUn, false);
|
|
for (int uix = 0; uix < ro->nUn; ++uix)
|
|
if (myUnit[uix].Master >= 0 || myUnit[uix].TroopLoad > 0)
|
|
moved[uix] = true;
|
|
|
|
bool firstLoop = true;
|
|
bool backToStart;
|
|
do {
|
|
// ATTACK
|
|
do {
|
|
backToStart = false;
|
|
if (ro->nEnemyUn > 0) {
|
|
std::fill_n(euixMap.begin(), MapSize(), static_cast<std::int16_t>(-1));
|
|
std::fill_n(attackScore.begin(), ro->nEnemyUn, 0);
|
|
for (int euix = 0; euix < ro->nEnemyUn; ++euix) {
|
|
const TUnitInfo &enemy = (*ro->EnemyUn)[euix];
|
|
if (enemy.Loc >= 0 && ro->Treaty[enemy.Owner] < trPeace) {
|
|
backToStart = true;
|
|
euixMap[enemy.Loc] = static_cast<std::int16_t>(euix);
|
|
uixAttack[euix] = -1;
|
|
}
|
|
}
|
|
}
|
|
if (!backToStart)
|
|
break;
|
|
|
|
for (int uix = 0; uix < ro->nUn; ++uix) {
|
|
const TUn &unit = myUnit[uix];
|
|
if (unit.Loc >= 0 && unit.Master < 0 && myModel[unit.mix].Attack > 0)
|
|
rateAttack(uix);
|
|
}
|
|
|
|
backToStart = false;
|
|
do {
|
|
int euixBest = -1;
|
|
for (int euix = 0; euix < ro->nEnemyUn; ++euix)
|
|
if (attackScore[euix] > 0 && (euixBest < 0 || attackScore[euix] > attackScore[euixBest]))
|
|
euixBest = euix;
|
|
if (euixBest < 0)
|
|
break;
|
|
const int uix = uixAttack[euixBest];
|
|
int attackLoc = (*ro->EnemyUn)[euixBest].Loc;
|
|
const std::uint32_t oldTile = attackLoc >= 0 ? map[attackLoc] : 0;
|
|
// only happens when city was destroyed with attack and enemy units have disappeared
|
|
if (attackLoc < 0 ||
|
|
(doAttack(uix, attackLoc) &&
|
|
((map[attackLoc] & fUnit) != 0 || ((oldTile & fCity) != 0 && (map[attackLoc] & fCity) == 0)))) {
|
|
backToStart = true; // new situation, rethink
|
|
} else {
|
|
euixMap[attackLoc] = -1;
|
|
attackScore[euixBest] = 0;
|
|
uixAttack[euixBest] = -1;
|
|
if (myUnit[uix].Loc >= 0)
|
|
rateAttack(uix);
|
|
}
|
|
} while (!backToStart);
|
|
} while (backToStart);
|
|
|
|
if (firstLoop) {
|
|
setCityDefenders();
|
|
processSeaTransport();
|
|
for (int uix = 0; uix < ro->nUn; ++uix)
|
|
if (myUnit[uix].Loc >= 0 && myUnit[uix].TroopLoad > 0)
|
|
processUnload(uix);
|
|
}
|
|
firstLoop = false;
|
|
|
|
for (int uix = 0; uix < ro->nUn; ++uix) {
|
|
const TUn &unit = myUnit[uix];
|
|
if (!moved[uix] && unit.Loc >= 0 && myModel[unit.mix].Domain == dSea && myModel[unit.mix].Attack > 0 &&
|
|
myModel[unit.mix].Cap[mcArtillery] > 0)
|
|
doAttack(uix, maNextCity); // check bombardments
|
|
}
|
|
|
|
// MOVE
|
|
for (int uix = 0; uix < ro->nUn; ++uix) {
|
|
if (moved[uix])
|
|
continue;
|
|
const TUn &unit = myUnit[uix];
|
|
if (unit.Loc >= 0 && (myModel[unit.mix].Attack > 0 || myModel[unit.mix].Domain == dSea)) {
|
|
if (!processMove(uix)) {
|
|
backToStart = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} while (backToStart);
|
|
}
|
|
|
|
void Barbarina::barbarina_SetCityProduction()
|
|
{
|
|
constexpr int CoastalWonder = (1 << woLighthouse) | (1 << woMagellan);
|
|
constexpr int PrimeWonder = (1 << woColossus) | (1 << woGrLibrary) | (1 << woSun) | (1 << woMagellan) |
|
|
(1 << woEiffel) | (1 << woLiberty) | (1 << woShinkansen);
|
|
|
|
auto lowPriority = [this](int cix) {
|
|
for (int part = 0; part < nShipPart; ++part) {
|
|
const int cixHighPriority = ColonyShipPlan[part].cixProducing;
|
|
if (cixHighPriority >= 0 && cixHighPriority != cix &&
|
|
Distance(myCity[cix].Loc, myCity[cixHighPriority].Loc) < 11)
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
auto chooseWonderToBuild = [](int wonderAvailable, bool allowCoastal) {
|
|
if ((wonderAvailable & PrimeWonder) > 0 &&
|
|
(allowCoastal || (wonderAvailable & PrimeWonder & ~CoastalWonder) > 0))
|
|
wonderAvailable &= PrimeWonder; // always prefer prime wonders
|
|
int count = 0;
|
|
for (int iix = 0; iix < nWonder; ++iix) {
|
|
if ((1 << iix) & wonderAvailable) {
|
|
if ((1 << iix) & CoastalWonder) {
|
|
if (allowCoastal)
|
|
count += 2;
|
|
} else {
|
|
++count;
|
|
}
|
|
}
|
|
}
|
|
count = randomInt(count);
|
|
for (int iix = 0; iix < nWonder; ++iix) {
|
|
if ((1 << iix) & wonderAvailable) {
|
|
if ((1 << iix) & CoastalWonder) {
|
|
if (allowCoastal)
|
|
count -= 2;
|
|
} else {
|
|
--count;
|
|
}
|
|
}
|
|
if (count < 0)
|
|
return iix;
|
|
}
|
|
return -1;
|
|
};
|
|
|
|
analyzeMap();
|
|
findBestModels();
|
|
|
|
std::array<int, nMmax> mixCount{};
|
|
for (int uix = 0; uix < ro->nUn; ++uix)
|
|
if (myUnit[uix].Loc >= 0)
|
|
++mixCount[myUnit[uix].mix];
|
|
|
|
int mixProduce;
|
|
if (mixBest[ctGroundSlow] >= 0 &&
|
|
(mixBest[ctGroundFast] < 0 || mixCount[mixBest[ctGroundSlow]] < mixCount[mixBest[ctGroundFast]]))
|
|
mixProduce = mixBest[ctGroundSlow];
|
|
else
|
|
mixProduce = mixBest[ctGroundFast];
|
|
int mixShip;
|
|
if (mixBest[ctSeaTrans] >= 0 &&
|
|
(mixBest[ctSeaArt] < 0 || mixCount[mixBest[ctSeaTrans]] < mixCount[mixBest[ctSeaArt]]))
|
|
mixShip = mixBest[ctSeaTrans];
|
|
else
|
|
mixShip = mixBest[ctSeaArt];
|
|
if (mixProduce >= 0 && mixBest[ctSeaTrans] >= 0 &&
|
|
mixCount[mixShip] * myModel[mixBest[ctSeaTrans]].Cap[mcSeaTrans] * myModel[mixBest[ctSeaTrans]].MTrans / 2 >=
|
|
mixCount[mixProduce])
|
|
mixShip = -1;
|
|
|
|
// produce ships only on certain continents?
|
|
int totalPop = 0, alonePop = 0;
|
|
for (int cix = 0; cix < ro->nCity; ++cix) {
|
|
const TCity &city = myCity[cix];
|
|
if (city.Loc >= 0 && (city.Flags & chCaptured) == 0) {
|
|
totalPop += city.Size;
|
|
const int f = formation[city.Loc];
|
|
if (f < 0 || f >= maxCOD || continentPresence[f] == (1 << me))
|
|
alonePop += city.Size;
|
|
}
|
|
}
|
|
const bool spezializeShipProduction = alonePop * 2 >= totalPop;
|
|
|
|
int cixNewCapital = -1;
|
|
int wonderAvailable = 0;
|
|
int wonderInWork = 0;
|
|
for (int iix = 0; iix < nWonder; ++iix)
|
|
if (Imp[iix].Preq != preNA && (Imp[iix].Preq == preNone || isResearched(Imp[iix].Preq)) &&
|
|
ro->Wonder[iix].CityID == WonderNotBuiltYet)
|
|
wonderAvailable += 1 << iix;
|
|
for (int cix = 0; cix < ro->nCity; ++cix) {
|
|
if (myCity[cix].Loc >= 0) {
|
|
const int iix = city_CurrentImprovementProject(cix);
|
|
if (iix >= 0 && iix < nWonder)
|
|
wonderInWork += 1 << iix;
|
|
else if (iix == imPalace)
|
|
cixNewCapital = cix;
|
|
}
|
|
}
|
|
|
|
if (ro->NatBuilt[imPalace - nWonder] == 0 && cixNewCapital < 0) { // palace was destroyed, build new one
|
|
const int center = centerOfEmpire();
|
|
int bestScore = 0;
|
|
for (int cix = 0; cix < ro->nCity; ++cix) {
|
|
const TCity &city = myCity[cix];
|
|
if (city.Loc >= 0 && (city.Flags & chCaptured) == 0) { // evaluate city as new capital
|
|
int score = city.Size * 12 + 512 - Distance(city.Loc, center);
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(city.Loc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int loc1 = adjacent[v8];
|
|
if (loc1 >= 0 && (map[loc1] & fTerrain) < static_cast<std::uint32_t>(fGrass)) {
|
|
const int f1 = formation[loc1];
|
|
if (f1 >= 0 && f1 < maxCOD &&
|
|
(oceanSize[f1] >= 8 || (oceanPresence[f1] & ~(1 << me)) != 0)) {
|
|
score -= 18; // prefer non-coastal cities
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (score > bestScore) {
|
|
bestScore = score;
|
|
cixNewCapital = cix;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool algaeAvailable = ro->NatBuilt[imAlgae - nWonder] == 0 && ro->Tech[Imp[imAlgae].Preq] >= tsApplicable;
|
|
for (int cix = 0; cix < ro->nCity; ++cix) {
|
|
const TCity &city = myCity[cix];
|
|
if (city.Loc >= 0 && (city.Project & (cpImp + cpIndex)) == cpImp + imAlgae) {
|
|
algaeAvailable = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
for (int cix = 0; cix < ro->nCity; ++cix) { // free all tiles of low-prio cities
|
|
const TCity &city = myCity[cix];
|
|
if (city.Loc >= 0 && (city.Flags & chCaptured) == 0 && lowPriority(cix))
|
|
city_SetTiles(cix, 1 << CityOwnTile);
|
|
}
|
|
|
|
for (int doLowPriorityPass = 0; doLowPriorityPass < 2; ++doLowPriorityPass) {
|
|
const bool doLowPriority = doLowPriorityPass != 0;
|
|
for (int cix = 0; cix < ro->nCity; ++cix) {
|
|
const TCity &city = myCity[cix];
|
|
if (!(city.Loc >= 0 && (city.Flags & chCaptured) == 0 && lowPriority(cix) == doLowPriority))
|
|
continue;
|
|
|
|
const int f = formation[city.Loc];
|
|
bool isCoastal = false;
|
|
bool isPort = false;
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(city.Loc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int loc1 = adjacent[v8];
|
|
if (loc1 >= 0 && (map[loc1] & fTerrain) < static_cast<std::uint32_t>(fGrass)) {
|
|
isCoastal = true;
|
|
const int f1 = formation[loc1];
|
|
if (f1 >= 0 && f1 < maxCOD && oceanSize[f1] >= 8 && (oceanPresence[f1] & ~(1 << me)) != 0) {
|
|
isPort = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool isUnitProjectObsolete = false;
|
|
if (city_CurrentUnitProject(cix) >= 0 && myModel[city_CurrentUnitProject(cix)].Kind != mkSettler) {
|
|
int i = nModelCategory - 1;
|
|
while (i >= 0 && city_CurrentUnitProject(cix) != mixBest[i])
|
|
--i;
|
|
isUnitProjectObsolete = i < 0;
|
|
}
|
|
|
|
int nTownGuard = 0;
|
|
if (ro->Government == gDespotism) {
|
|
for (int uix = 0; uix < ro->nUn; ++uix)
|
|
if (myUnit[uix].mix == mixTownGuard && myUnit[uix].Loc == city.Loc)
|
|
++nTownGuard;
|
|
}
|
|
|
|
const int iix = city_CurrentImprovementProject(cix);
|
|
if ((iix >= 0 && iix < nWonder) || iix == imPalace || iix == imShipComp || iix == imShipPow ||
|
|
iix == imShipHab)
|
|
city_OptimizeTiles(cix, rwMaxProd);
|
|
else if (city.Size < 8)
|
|
city_OptimizeTiles(cix, rwMaxGrowth);
|
|
else
|
|
city_OptimizeTiles(cix, rwForceProd);
|
|
|
|
bool willProduceColonyShip = false;
|
|
int produceShipPart = -1;
|
|
for (int part = 0; part < nShipPart; ++part) {
|
|
if (ColonyShipPlan[part].cixProducing == cix) {
|
|
willProduceColonyShip = true;
|
|
produceShipPart = ShipImpIndex[part];
|
|
}
|
|
}
|
|
|
|
if (cix == cixNewCapital) {
|
|
city_StartImprovement(cix, imPalace);
|
|
} else if (iix >= 0 && iix < nWonder && (wonderAvailable & (1 << iix)) != 0) {
|
|
// complete wonder production first
|
|
} else if (mixProduce >= 0 && city_CurrentUnitProject(cix) >= 0 && !isUnitProjectObsolete &&
|
|
((city.Flags & chProduction) == 0 ||
|
|
(myModel[city_CurrentUnitProject(cix)].Cap[mcLine] > 0 &&
|
|
mixCount[city_CurrentUnitProject(cix)] < ro->nCity * (2 + (cix & 3))))) {
|
|
// complete unit production first
|
|
} else {
|
|
if (produceShipPart >= 0) {
|
|
if (city.Built[imGranary] == 0 && city.Size < 10 && city_Improvable(cix, imGranary))
|
|
city_StartImprovement(cix, imGranary);
|
|
else if (city.Built[imAqueduct] == 0 && city_Improvable(cix, imAqueduct))
|
|
city_StartImprovement(cix, imAqueduct);
|
|
else if (city.Built[imAqueduct] > 0 && city.Size < 12 &&
|
|
(algaeAvailable || (city.Project & (cpImp + cpIndex)) == cpImp + imAlgae))
|
|
city_StartImprovement(cix, imAlgae);
|
|
else if (city.Built[imFactory] == 0 && city_Improvable(cix, imFactory))
|
|
city_StartImprovement(cix, imFactory);
|
|
else if (city.Built[imPower] + city.Built[imHydro] + city.Built[imNuclear] == 0 &&
|
|
(city_Improvable(cix, imPower) || city_Improvable(cix, imHydro) ||
|
|
city_Improvable(cix, imNuclear))) {
|
|
if (city_Improvable(cix, imHydro))
|
|
city_StartImprovement(cix, imHydro);
|
|
else if (city_Improvable(cix, imPower))
|
|
city_StartImprovement(cix, imPower);
|
|
else
|
|
city_StartImprovement(cix, imNuclear);
|
|
} else if (city.Built[imMfgPlant] == 0 && city_Improvable(cix, imMfgPlant))
|
|
city_StartImprovement(cix, imMfgPlant);
|
|
else if (city_Improvable(cix, produceShipPart))
|
|
city_StartImprovement(cix, produceShipPart);
|
|
else
|
|
produceShipPart = -1;
|
|
}
|
|
if (produceShipPart < 0) {
|
|
const bool projectComplete = !city_HasProject(cix) || (city.Flags & chProduction) != 0;
|
|
bool hasSettler = false;
|
|
for (int uix = 0; uix < ro->nUn; ++uix)
|
|
if (myUnit[uix].Loc >= 0 && myUnit[uix].Home == cix &&
|
|
myModel[myUnit[uix].mix].Kind == mkSettler) {
|
|
hasSettler = true;
|
|
break;
|
|
}
|
|
|
|
if ((ro->Government != gDespotism || ro->nUn >= ro->nCity * 4) && !isResearched(adMassProduction) &&
|
|
city.Built[imPalace] > 0 && ro->Wonder[woZeus].CityID == WonderNotBuiltYet &&
|
|
city_Improvable(cix, woZeus)) {
|
|
city_StartImprovement(cix, woZeus);
|
|
} else if (city_CurrentImprovementProject(cix) >= 0 &&
|
|
city_CurrentImprovementProject(cix) < nWonder) {
|
|
// wonder already built, try to switch to a different one
|
|
if ((wonderAvailable & ~wonderInWork) > 0 &&
|
|
(isCoastal || (wonderAvailable & ~wonderInWork & ~CoastalWonder) > 0)) {
|
|
const int newIix = chooseWonderToBuild(wonderAvailable & ~wonderInWork, isCoastal);
|
|
city_StartImprovement(cix, newIix);
|
|
wonderInWork |= 1 << newIix;
|
|
} else {
|
|
city_StopProduction(cix);
|
|
}
|
|
} else if (city.Built[imPalace] > 0 && ro->NatBuilt[imSpacePort - nWonder] == 0 &&
|
|
city_Improvable(cix, imSpacePort)) {
|
|
city_StartImprovement(cix, imSpacePort);
|
|
} else if (city.Built[imPalace] + city.Built[imCourt] + city.Built[imTownHall] == 0) {
|
|
if (city_Improvable(cix, imCourt))
|
|
city_StartImprovement(cix, imCourt);
|
|
else
|
|
city_StartImprovement(cix, imTownHall);
|
|
} else if (!hasSettler && ro->nUn >= ro->nCity * 4) {
|
|
if (projectComplete && city_CurrentUnitProject(cix) != 0) {
|
|
int mix = ro->nModel - 1;
|
|
while (myModel[mix].Kind != mkSettler)
|
|
--mix;
|
|
city_StartUnitProduction(cix, mix);
|
|
}
|
|
} else if (ro->Government == gDespotism && nTownGuard < 2 && nTownGuard * 2 + 3 < city.Size) {
|
|
if (projectComplete)
|
|
city_StartUnitProduction(cix, 2);
|
|
} else if (ro->Government == gFundamentalism && city.Size >= 8 && city.Built[imAqueduct] == 0 &&
|
|
city_Improvable(cix, imAqueduct) && ro->nUn >= ro->nCity * 4) {
|
|
if (projectComplete)
|
|
city_StartImprovement(cix, imAqueduct);
|
|
} else if (projectComplete) { // low prio projects
|
|
bool importantCity = willProduceColonyShip || city.Built[imPalace] > 0;
|
|
for (int iix2 = 0; iix2 < nWonder; ++iix2)
|
|
if (city.Built[iix2] > 0) {
|
|
importantCity = true;
|
|
break;
|
|
}
|
|
TCityReportNew report{};
|
|
city_GetReportNew(cix, report);
|
|
if (report.Corruption >= 6 && ro->nUn >= ro->nCity * 4 && city_Improvable(cix, imCourt)) {
|
|
city_StartImprovement(cix, imCourt);
|
|
} else if (report.Production >= WonderProductionThreshold &&
|
|
(wonderAvailable & ~wonderInWork) > 0 &&
|
|
(isCoastal || (wonderAvailable & ~wonderInWork & ~CoastalWonder) > 0) &&
|
|
randomFloat() >= (1 + WonderInclination) / (ro->nCity + WonderInclination)) {
|
|
const int newIix = chooseWonderToBuild(wonderAvailable & ~wonderInWork, isCoastal);
|
|
city_StartImprovement(cix, newIix);
|
|
wonderInWork |= 1 << newIix;
|
|
} else if ((importantCity || city.Loc % 9 == 0) && city.Built[imWalls] == 0 &&
|
|
city_Improvable(cix, imWalls)) {
|
|
city_StartImprovement(cix, imWalls);
|
|
} else if (isPort && (importantCity || city.Loc % 7 == 0) && city.Built[imCoastalFort] == 0 &&
|
|
city_Improvable(cix, imCoastalFort)) {
|
|
city_StartImprovement(cix, imCoastalFort);
|
|
} else if (isPort &&
|
|
(!spezializeShipProduction || f < 0 || f >= maxCOD ||
|
|
continentPresence[f] == (1 << me)) &&
|
|
city.Built[imDockyard] == 0 && city_Improvable(cix, imDockyard)) {
|
|
city_StartImprovement(cix, imDockyard);
|
|
} else if (isPort && mixShip >= 0 &&
|
|
(!spezializeShipProduction || f < 0 || f >= maxCOD ||
|
|
continentPresence[f] == (1 << me))) {
|
|
city_StartUnitProduction(cix, mixShip);
|
|
} else if (city.Built[imBarracks] + city.Built[imMilAcademy] == 0 &&
|
|
city_Improvable(cix, imBarracks)) {
|
|
city_StartImprovement(cix, imBarracks);
|
|
} else if (mixProduce >= 0) {
|
|
city_StartUnitProduction(cix, mixProduce);
|
|
} else if (city_HasProject(cix)) {
|
|
city_StopProduction(cix);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (city_CurrentImprovementProject(cix) == imCourt && city.Built[imTownHall] > 0 &&
|
|
city.Prod >= Imp[imCourt].Cost * BuildCostMod[Difficulty(me)] / 12 -
|
|
(Imp[imTownHall].Cost * BuildCostMod[Difficulty(me)] / 12) * 2 / 3) {
|
|
city_RebuildImprovement(cix, imTownHall);
|
|
} else if (ro->Government == gFundamentalism && !willProduceColonyShip) {
|
|
for (int iix2 = nWonder; iix2 < nImp; ++iix2) {
|
|
if (city.Built[iix2] > 0 &&
|
|
((iix2 == imTemple || iix2 == imTheater || iix2 == imCathedral || iix2 == imColosseum ||
|
|
iix2 == imLibrary || iix2 == imUniversity || iix2 == imResLab || iix2 == imHarbor ||
|
|
iix2 == imSupermarket) ||
|
|
((iix2 == imFactory || iix2 == imMfgPlant || iix2 == imPower || iix2 == imHydro ||
|
|
iix2 == imNuclear) &&
|
|
city.Built[imRecycling] == 0))) {
|
|
if (city_RebuildImprovement(cix, iix2) < rExecuted)
|
|
city_SellImprovement(cix, iix2);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int Barbarina::barbarina_ChooseResearchAdvance()
|
|
{
|
|
int rmixChosen = -1;
|
|
bool modelExists[nModelCategory] = { false, false, false, false };
|
|
for (int rmix = nResearchModel - 1; rmix >= 0; --rmix) {
|
|
const ResearchModelInfo &rm = ResearchModel[rmix];
|
|
if (!modelExists[rm.Category] && (rm.adStop < 0 || !isResearched(rm.adStop))) {
|
|
int maxWeight = 0;
|
|
switch (rm.Domain) {
|
|
case dGround:
|
|
if (isResearched(adWarriorCode))
|
|
maxWeight = 5;
|
|
if (isResearched(adHorsebackRiding))
|
|
maxWeight = 7;
|
|
if (isResearched(adAutomobile))
|
|
maxWeight = 10;
|
|
break;
|
|
case dSea:
|
|
if (isResearched(adMapMaking))
|
|
maxWeight = 5;
|
|
if (isResearched(adSeafaring))
|
|
maxWeight = 7;
|
|
if (isResearched(adSteel))
|
|
maxWeight = 9;
|
|
break;
|
|
case dAir:
|
|
if (isResearched(adFlight))
|
|
maxWeight = 5;
|
|
if (isResearched(adAdvancedFlight))
|
|
maxWeight = 7;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
int maxDefense = rm.Domain == dGround ? 2 : 3;
|
|
if (isResearched(adSteel))
|
|
++maxDefense;
|
|
bool ready = maxWeight >= rm.Weight && maxDefense >= rm.Cap[mcDefense];
|
|
if (ready) {
|
|
for (int i = 0; i < nFeature; ++i)
|
|
if (rm.Cap[i] > 0 && Feature[i].Preq != preNone &&
|
|
(Feature[i].Preq < 0 || !isResearched(Feature[i].Preq))) {
|
|
ready = false;
|
|
break;
|
|
}
|
|
}
|
|
if (ready) {
|
|
for (int i = 0; i < nUpgrade; ++i)
|
|
if ((rm.Upgrades & (1u << i)) != 0 && !isResearched(Upgrade[rm.Domain][i].Preq)) {
|
|
ready = false;
|
|
break;
|
|
}
|
|
}
|
|
if (ready) {
|
|
modelExists[rm.Category] = true;
|
|
if (!isModelAvailable(rmix))
|
|
rmixChosen = rmix;
|
|
}
|
|
}
|
|
}
|
|
if (rmixChosen >= 0) {
|
|
const ResearchModelInfo &rm = ResearchModel[rmixChosen];
|
|
prepareNewModel(rm.Domain);
|
|
for (int i = 0; i < nFeature; ++i)
|
|
if (i < 2 || rm.Cap[i] > 0)
|
|
setNewModelFeature(i, rm.Cap[i]);
|
|
if (ro->Wonder[woSun].EffectiveOwner == me) {
|
|
if (rm.Cap[mcWeapons] >= rm.Cap[mcArmor])
|
|
setNewModelFeature(mcWill, 1);
|
|
}
|
|
return adMilitary;
|
|
}
|
|
|
|
bool needSeaUnits = true;
|
|
int i = 0;
|
|
while (i < nResearchOrder &&
|
|
((!needSeaUnits && ResearchOrder[i] < 0) || isResearched(std::abs(ResearchOrder[i]))))
|
|
++i;
|
|
if (i >= nResearchOrder) { // list done, continue with future tech
|
|
return randomInt(2) == 1 ? futArtificialIntelligence : futMaterialTechnology;
|
|
}
|
|
|
|
std::array<int, nAdv> known{};
|
|
int nPreq = 0;
|
|
int chosenPreq = -1;
|
|
|
|
std::function<void(int)> choosePreq = [&](int ad) {
|
|
if (known[ad] != 0)
|
|
return;
|
|
known[ad] = 1;
|
|
bool preqOk = true;
|
|
if (!(ad == adScience || ad == adMassProduction) && ro->Tech[ad] < tsSeen) {
|
|
for (int k = 0; k < 2; ++k)
|
|
if (AdvPreq[ad][k] >= 0 && ro->Tech[AdvPreq[ad][k]] < tsApplicable) {
|
|
preqOk = false;
|
|
choosePreq(AdvPreq[ad][k]);
|
|
}
|
|
}
|
|
if (preqOk) {
|
|
++nPreq;
|
|
if (randomInt(nPreq) == 0)
|
|
chosenPreq = ad;
|
|
}
|
|
};
|
|
|
|
choosePreq(std::abs(ResearchOrder[i]));
|
|
return chosenPreq;
|
|
}
|
|
|
|
bool Barbarina::barbarina_WantCheckNegotiation(int nation) const
|
|
{
|
|
return ro->Tech[adTheRepublic] < tsSeen && ro->Tech[adTheology] >= tsApplicable &&
|
|
ro->Tech[adGunpowder] >= tsApplicable &&
|
|
ro->EnemyReport[nation]->Tech[adTheRepublic] >= tsApplicable;
|
|
}
|
|
|
|
void Barbarina::barbarina_DoCheckNegotiation()
|
|
{
|
|
if (ro->Tech[adTheRepublic] >= tsSeen)
|
|
return; // default reaction
|
|
|
|
if (myLastAction == scContact) {
|
|
myAction = scDipOffer;
|
|
myOffer.nDeliver = 1;
|
|
myOffer.nCost = 1;
|
|
if (ro->Tech[adTheology] >= tsApplicable && ro->EnemyReport[opponent]->Tech[adTheology] < tsSeen)
|
|
myOffer.Price[0] = opTech + adTheology;
|
|
else
|
|
myOffer.Price[0] = opChoose;
|
|
myOffer.Price[1] = opTech + adTheRepublic;
|
|
} else if (oppoAction == scDipAccept) {
|
|
// nothing
|
|
} else if (oppoAction == scDipOffer) {
|
|
if (oppoOffer.nDeliver == 1 && oppoOffer.Price[0] == static_cast<std::uint32_t>(opTech + adTheRepublic) &&
|
|
(oppoOffer.nCost == 0 ||
|
|
(oppoOffer.nCost == 1 && (oppoOffer.Price[1] & opMask) == opTech &&
|
|
ro->Tech[oppoOffer.Price[1] - opTech] >= tsApplicable)))
|
|
myAction = scDipAccept;
|
|
else
|
|
myAction = scDipBreak;
|
|
} else if (oppoAction != scDipBreak) {
|
|
myAction = scDipBreak;
|
|
}
|
|
}
|
|
|
|
bool Barbarina::barbarina_WantNegotiation(int nation, NegoTime negoTime)
|
|
{
|
|
switch (negoTime) {
|
|
case NegoTime::EnemyCalled:
|
|
return false;
|
|
case NegoTime::EndOfTurn:
|
|
return false;
|
|
case NegoTime::BeginOfTurn:
|
|
if (ro->Turn >= ro->LastCancelTreaty[nation] + CancelTreatyTurns) {
|
|
if ((ro->Turn & 3) == ((nation + 0xF - me) & 3) && ro->Treaty[nation] > trPeace) {
|
|
debugMessage(1, std::string("End alliance/friendly contact with P") +
|
|
static_cast<char>(48 + nation));
|
|
negoCause = NegoCauseKind::CancelTreaty;
|
|
return true;
|
|
}
|
|
if (ro->Treaty[nation] == trPeace) { // declare war now?
|
|
for (int uix = 0; uix < ro->nUn; ++uix) {
|
|
const TUn &unit = myUnit[uix];
|
|
if (unit.Loc >= 0 && myModel[unit.mix].Attack > 0) {
|
|
TVicinity8Loc adjacent;
|
|
V8_to_Loc(unit.Loc, adjacent);
|
|
for (int v8 = 0; v8 < 8; ++v8) {
|
|
const int testLoc = adjacent[v8];
|
|
if (testLoc >= 0 && (*ro->Territory)[testLoc] == nation &&
|
|
((map[testLoc] & fTerrain) >= static_cast<std::uint32_t>(fGrass) ||
|
|
unit.Master >= 0 || myModel[unit.mix].Domain != dGround) &&
|
|
((map[testLoc] & fTerrain) < static_cast<std::uint32_t>(fGrass) ||
|
|
myModel[unit.mix].Domain != dSea)) {
|
|
debugMessage(1, std::string("Declare war on P") + static_cast<char>(48 + nation));
|
|
negoCause = NegoCauseKind::CancelTreaty;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Barbarina::barbarina_DoNegotiation()
|
|
{
|
|
if (oppoAction == scDipStart) {
|
|
if (negoCause == NegoCauseKind::CancelTreaty)
|
|
myAction = scDipCancelTreaty;
|
|
}
|
|
}
|
|
|
|
void Barbarina::makeColonyShipPlan()
|
|
{
|
|
for (int part = 0; part < nShipPart; ++part) {
|
|
ColonyShipPlan[part].cixProducing = -1;
|
|
ColonyShipPlan[part].nLocResource = 0;
|
|
ColonyShipPlan[part].nLocFoundCity = 0;
|
|
}
|
|
if (ro->Tech[adMassProduction] < tsApplicable) // able to recognize resources yet?
|
|
return;
|
|
|
|
// Check already existing cities
|
|
for (int cix = 0; cix < ro->nCity; ++cix) {
|
|
const TCity &city = myCity[cix];
|
|
if (city.Loc < 0)
|
|
continue;
|
|
TVicinity21Loc radius;
|
|
V21_to_Loc(city.Loc, radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) {
|
|
const int loc1 = radius[v21];
|
|
if (loc1 < 0)
|
|
continue;
|
|
const std::uint32_t tile = (*ro->Map)[loc1];
|
|
if ((tile & fModern) == 0)
|
|
continue;
|
|
const int part = static_cast<int>((tile & fModern) >> 25) - 1;
|
|
if (ro->Ship[me].Parts[part] >= ShipNeed[part]) // already enough of this kind
|
|
continue;
|
|
bool ok = true;
|
|
if (ColonyShipPlan[part].cixProducing >= 0) {
|
|
// another city is already assigned to this ship part, choose one of the two
|
|
const int testValue = (city.ID & 0xFFF) * 16 + (((city.ID >> 12) + 15 - me) & 0xF);
|
|
const TCity &other = myCity[ColonyShipPlan[part].cixProducing];
|
|
const int bestValue = (other.ID & 0xFFF) * 16 + (((other.ID >> 12) + 15 - me) & 0xF);
|
|
if (testValue <= bestValue)
|
|
ok = false;
|
|
}
|
|
if (ok)
|
|
ColonyShipPlan[part].cixProducing = cix;
|
|
}
|
|
}
|
|
|
|
// for parts without existing city, look for location of city to found
|
|
bool check = false;
|
|
for (int part = 0; part < nShipPart; ++part)
|
|
if (ro->Ship[me].Parts[part] < ShipNeed[part] && ColonyShipPlan[part].cixProducing < 0) {
|
|
check = true;
|
|
break;
|
|
}
|
|
if (!check)
|
|
return;
|
|
|
|
for (int loc1 = 0; loc1 < MapSize(); ++loc1) {
|
|
const std::uint32_t tile = (*ro->Map)[loc1];
|
|
if ((tile & fModern) == 0)
|
|
continue;
|
|
const int part = static_cast<int>((tile & fModern) >> 25) - 1;
|
|
ColonyShipPlanPart &plan = ColonyShipPlan[part];
|
|
if (plan.nLocResource < maxModern) {
|
|
plan.LocResource[plan.nLocResource] = loc1;
|
|
++plan.nLocResource;
|
|
}
|
|
}
|
|
|
|
for (int part = 0; part < nShipPart; ++part) {
|
|
ColonyShipPlanPart &plan = ColonyShipPlan[part];
|
|
if (!(ro->Ship[me].Parts[part] < ShipNeed[part] && plan.cixProducing < 0 && plan.nLocResource > 0))
|
|
continue;
|
|
for (int i = 0; i < plan.nLocResource; ++i) {
|
|
int bestScore = 0;
|
|
TVicinity21Loc radius;
|
|
V21_to_Loc(plan.LocResource[i], radius);
|
|
for (int v21 = 1; v21 <= 26; ++v21) { // check all potential cities in range
|
|
const int cityLoc = radius[v21];
|
|
if (cityLoc < 0)
|
|
continue;
|
|
const std::uint32_t tile = (*ro->Map)[cityLoc];
|
|
if ((tile & fTerrain) == static_cast<std::uint32_t>(fUNKNOWN) ||
|
|
!((tile & fTerrain) == static_cast<std::uint32_t>(fForest) ||
|
|
(tile & fTerrain) == static_cast<std::uint32_t>(fSwamp) ||
|
|
Terrain[tile & fTerrain].IrrEff > 0))
|
|
continue;
|
|
|
|
int foodCount = 0, prodCount = 0, prodExtra = 0;
|
|
TVicinity21Loc radiusC;
|
|
V21_to_Loc(cityLoc, radiusC);
|
|
for (int v21c = 1; v21c <= 26; ++v21c) {
|
|
const int loc1 = radiusC[v21c];
|
|
if (loc1 < 0)
|
|
continue;
|
|
switch ((*ro->Map)[loc1] & (fTerrain | fSpecial)) {
|
|
case fGrass:
|
|
case fGrass + fSpecial1:
|
|
case fSwamp:
|
|
++foodCount;
|
|
break;
|
|
case fHills:
|
|
case fHills + fSpecial1:
|
|
++prodCount;
|
|
break;
|
|
case fShore + fSpecial1:
|
|
case fDesert + fSpecial1:
|
|
case fPrairie + fSpecial1:
|
|
case fForest + fSpecial1:
|
|
foodCount += 2;
|
|
break;
|
|
case fSwamp + fSpecial1:
|
|
case fShore + fSpecial2:
|
|
case fDesert + fSpecial2:
|
|
case fPrairie + fSpecial2:
|
|
case fTundra + fSpecial2:
|
|
case fArctic + fSpecial1:
|
|
case fHills + fSpecial2:
|
|
case fMountains + fSpecial1:
|
|
++prodCount;
|
|
++prodExtra;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
int score;
|
|
if (foodCount == 0) {
|
|
score = 0;
|
|
} else {
|
|
if (prodCount > 7)
|
|
prodCount = 7;
|
|
if (foodCount < 5)
|
|
prodCount -= 5 - foodCount;
|
|
score = prodCount * 4 + prodExtra * 8 + foodCount;
|
|
// some unexactness, random but always the same for this tile
|
|
score = (score << 8) + ((cityLoc ^ me) * 4567) % 251;
|
|
}
|
|
if (score > bestScore) {
|
|
bestScore = score;
|
|
if (plan.nLocFoundCity < maxModern)
|
|
plan.LocFoundCity[plan.nLocFoundCity] = cityLoc;
|
|
}
|
|
}
|
|
if (bestScore > 0 && plan.nLocFoundCity < maxModern)
|
|
++plan.nLocFoundCity;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Cevo
|