Files
2026-11-04 14:46:26 +01:00

140 lines
5.6 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.
#pragma once
#include "Cevo/Barbarina.h"
#include <array>
namespace Cevo {
constexpr int nAIModelCat = 4;
constexpr int mctNone = -1;
constexpr int mctGroundDefender = 0;
constexpr int mctGroundAttacker = 1;
constexpr int mctTransport = 2;
constexpr int mctCruiser = 3;
constexpr int nRequestedTechs = 48;
enum class Suggestion { Contact, Peace, Friendly };
// Raw per-nation scratch memory the host allocates (rwDataSize bytes) and
// hands back via TPlayerContext::Data - reinterpreted in place, NOT
// constructed, so these fields must NOT rely on default member
// initializers (see setDataDefaults(), which is what actually initializes
// a fresh block on cmNewGame, mirroring the original's SetDataDefaults).
struct TPersistentData {
int LastResearchTech;
int BehaviorFlags;
int TheologyPartner;
std::int16_t RejectTurn[3][16]; // [Suggestion][0..15]
int RequestedTechs[nRequestedTechs]; // ad + p shl 8 + Turn shl 16
};
// C++ equivalent of AI.pas's TAI: the actual StdAI decision heuristics
// (research strategy, terraforming/settlers, production, combat/patrol,
// diplomacy), built on top of Barbarina's "barbarian mode" personality.
// This is the final/top-level AI class a StdAI plugin module instantiates.
class AI : public Barbarina {
public:
explicit AI(int nation);
void bind(TPlayerContext *playerContext) override;
void setDataDefaults() override;
protected:
TPersistentData *data = nullptr;
int warNations = 0, bombardingNations = 0;
int mixSettlers = 0, mixCaravan = 0, mixTownGuard = 0, mixSlaves = 0, mixMilitia = 0, mixCruiser = 0;
int oceanWithShip = 0;
enum class AINegoCause { Routine, CheckBarbarina };
AINegoCause negoCause = AINegoCause::Routine;
std::array<int, maxCOD> settlerSurplus{};
std::array<int, maxCOD> uixPatrol{};
// Hides Barbarina's own continentPresence/oceanPresence (same name
// collision as the original Pascal: TAI declares its own, separate from
// TBarbarina's private fields).
std::array<int, maxCOD> continentPresence{};
std::array<int, maxCOD> oceanPresence{};
std::array<std::array<int, mctGroundAttacker - mctGroundDefender + 1>, maxCOD> unitLack{};
std::array<int, nPl> totalPopulation{};
std::array<std::array<int, maxCOD>, nPl> continentPopulation{};
std::array<int, maxCOD> districtPopulation{};
std::array<int, nMmax> modelCat{};
std::array<int, nMmax> modelQuality{};
std::array<int, nAIModelCat> modelBestQuality{};
std::array<int, nAdv> advanceValue{};
bool advanceValuesSet = false;
void doTurn() override;
void doNegotiation() override;
int chooseResearchAdvance() override;
int chooseStealAdvance() override;
int chooseGovernment() override;
bool wantNegotiation(int nation, NegoTime negoTime) override;
bool onNegoRejected_CancelTreaty() override;
void findBestTrade(int nation, int &adWanted, int &adGiveAway);
void checkGender();
// Hides Barbarina::analyzeMap()/ToolAI::analyzeMap() - calls
// Barbarina::analyzeMap() first (matches the original's `inherited;`).
// NOTE: ToolAI::analyzeMap() (Formation[]/District[] computation) is
// NEVER called anywhere in the original class hierarchy either - see
// AI.cpp's comment on this for a confirmed-via-source-grep explanation
// (genuine dead code / latent bug in upstream StdAI, ported faithfully).
void analyzeMap();
void collectModelCatStat();
// Hides Barbarina::attackAndPatrol() - a completely different algorithm.
void attackAndPatrol();
void moveUnitsHome();
void checkAttack(int uix);
void patrol(int uix);
// Hides Barbarina::barbarina_SetCityProduction() (different method name,
// no actual collision, but still TAI's own top-level production logic).
void setCityProduction();
void setAdvanceValues();
bool havePort() const;
void rateModel(const TModelInfo &mi, int &category, int &quality) const;
void rateMyModel(int mix, int &category, int &quality) const;
bool isBetterModel(const TModelInfo &mi) const;
void tileWorkPlan(int loc, int cix, int &value, int &nextJob, int &totalWork);
void processSettlers();
int mostWanted(int nation, int adGiveAway) const;
};
} // namespace Cevo