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

184 lines
8.0 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/Protocol.h"
#include <array>
#include <bitset>
#include <string>
namespace Cevo {
enum class NegoTime { BeginOfTurn, EndOfTurn, EnemyCalled };
constexpr int CityOwnTile = 13; // = ab_to_V21(0, 0)
// C++ equivalent of CustomAI.pas's TCustomAI: the base class for a single
// player's AI "brain". Mirrors the original's full Process() dispatch and
// its protected Unit_*/City_*/general Server-call wrappers, so ported AI
// logic (StdAI's TAI, etc.) plugs in the same way.
class CustomAI {
public:
explicit CustomAI(int nation) : me(nation) {}
virtual ~CustomAI();
// Attaches the player context handed out by the server at
// cmNewGame/cmLoadGame (TNewGameData.RO[nation]). Virtual so an AI can
// pick up its persistent data (the original did it in its constructor,
// but here the context only arrives after construction).
virtual void bind(TPlayerContext *playerContext);
// Set once per module from cmInitModule's TInitModuleData.Server; shared
// by every CustomAI instance in this process, matching the original's
// single unit-level `Server` global.
static void setServerCall(TServerCall call) { s_server = call; }
// Dispatches a single Command/Data pair received from the host (mirrors
// TCustomAI.Process in full: turn/negotiation loop, scContact probing,
// scDipStart..scDipBreak). cmShowAttacking/cmShowCapturing/
// cmShowAfterAttack/cmShowAfterMove are NOT dispatched through here
// (unlike cmTurn etc.): the original's library wrapper (StdAI.lpr)
// intercepts them directly to call onBeforeEnemyAttack() and friends, see
// AiModuleHost.
void process(int command, void *data);
virtual void setDataDefaults() {}
virtual void setDataRandom() {}
virtual void onBeforeEnemyAttack(const TUnitInfo &unitInfo, int toLoc, int endHealth, int endHealthDef) {}
virtual void onBeforeEnemyCapture(const TUnitInfo &unitInfo, int toLoc) {}
virtual void onAfterEnemyAttack() {}
virtual void onAfterEnemyCapture() {}
protected:
int me; // index of the controlled nation
TPlayerContext *ro = nullptr;
std::uint32_t *map = nullptr; // ro->Map, indexable as map[loc]
TUn *myUnit = nullptr; // ro->Un, indexable as myUnit[uix]
TCity *myCity = nullptr; // ro->City, indexable as myCity[cix]
TModel *myModel = nullptr; // ro->Model, indexable as myModel[mix]
// cix of the city that built a given "state" (unique per empire)
// improvement, indexed by iix - imPalace; -1 if none. Refreshed once per
// cmTurn.
std::array<int, imSpacePort - imPalace + 1> cixStateImp{};
// negotiation
int opponent = -1; // nation i'm in negotiation with, -1 = no negotiation
int myAction = 0, myLastAction = 0, oppoAction = 0;
TOffer myOffer{}, myLastOffer{}, oppoOffer{};
virtual void doTurn() {}
virtual void doNegotiation() {}
virtual int chooseResearchAdvance() { return -1; }
virtual int chooseStealAdvance() { return -1; }
virtual int chooseGovernment() { return gDespotism; }
virtual bool wantNegotiation(int nation, NegoTime negoTime) { return false; }
virtual bool onNegoRejected_CancelTreaty() { return false; }
// ---- general functions ----
bool isResearched(int advance) const
{
return advance == preNone || (advance != preNA && ro->Tech[advance] >= tsApplicable);
}
int researchCost() const;
int changeAttitude(int nation, int attitude) const;
int revolution() const;
int changeRates(int tax, int lux) const;
int prepareNewModel(int domain) const;
int setNewModelFeature(int feature, int count) const;
bool advanceResearchable(int advance) const;
bool advanceStealable(int advance) const;
bool getJobProgress(int loc, TJobProgressData &jobProgress) const;
bool debugMessage(int level, const std::string &text) const;
bool setDebugMap(void *debugMap) const;
// ---- unit functions ----
int unit_FindMyDefender(int loc) const;
int unit_FindEnemyDefender(int loc) const;
int unit_Move(int uix, int toLoc);
int unit_Step(int uix, int toLoc);
int unit_Attack(int uix, int toLoc) const;
int unit_DoMission(int uix, int missionType, int toLoc) const;
bool unit_MoveForecast(int uix, int toLoc, int &remainingMovement) const;
bool unit_AttackForecast(int uix, int toLoc, int attackMovement, int &remainingHealth) const;
bool unit_DefenseForecast(int euix, int toLoc, int &remainingHealth) const;
int unit_Disband(int uix) const;
int unit_StartJob(int uix, int newJob) const;
int unit_SetHomeHere(int uix) const;
int unit_Load(int uix) const;
int unit_Unload(int uix) const;
int unit_SelectTransport(int uix) const;
int unit_AddToCity(int uix) const;
// ---- city functions ----
int city_FindMyCity(int loc) const;
int city_FindEnemyCity(int loc) const;
bool city_HasProject(int cix) const;
int city_CurrentImprovementProject(int cix) const;
int city_CurrentUnitProject(int cix) const;
int city_GetTileInfo(int cix, int tileLoc, TTileInfo &tileInfo) const;
int city_GetReport(int cix, TCityReport &report) const;
int city_GetHypoReport(int cix, int hypoTiles, int hypoTax, int hypoLux, TCityReport &report) const;
int city_GetReportNew(int cix, TCityReportNew &report) const;
int city_GetHypoReportNew(int cix, int hypoTiles, int hypoTaxRate, int hypoLuxuryRate,
TCityReportNew &report) const;
int city_GetAreaInfo(int cix, TCityAreaInfo &areaInfo) const;
int city_StartUnitProduction(int cix, int mix) const;
int city_StartEmigration(int cix, int mix, bool allowDisbandCity, bool asConscripts) const;
int city_StartImprovement(int cix, int iix) const;
bool city_Improvable(int cix, int iix) const;
int city_StopProduction(int cix) const;
int city_BuyProject(int cix) const;
int city_SellImprovement(int cix, int iix) const;
int city_RebuildImprovement(int cix, int iix) const;
int city_SetTiles(int cix, int newTiles) const;
void city_OptimizeTiles(int cix, std::uint32_t resourceWeights = rwMaxGrowth) const;
// ---- negotiation ----
int nego_CheckMyAction() const;
// Direct Server() call: Server(command, me, subject, data).
int rawServerCall(int command, int subject, void *data) const
{
return s_server ? s_server(command, me, subject, data) : 0;
}
private:
void stealAdvance();
static TServerCall s_server;
bool haveTurned = false;
std::bitset<nPl> unwantedNego;
std::bitset<nPl> contacted;
};
} // namespace Cevo