Files
c-evo-evo/ai/include/Cevo/ToolAI.h
T
2026-10-22 18:11:04 +02:00

151 lines
6.6 KiB
C++

// This file is part of cevo-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/CustomAI.h"
#include "Cevo/Pile.h"
#include <array>
#include <cstdint>
namespace Cevo {
// no-formation sentinel values (Formation[]/map analysis)
constexpr int nfUndiscovered = -1;
constexpr int nfPole = -2;
constexpr int nfPeace = -3;
// return codes of checkStep()
constexpr int csOk = 0; // step is valid, timeAfterStep has been calculated
constexpr int csForbiddenTile = 1; // unit can not move onto this tile
constexpr int csForbiddenStep = 2; // (ZoC unit only) unit can not do this step because of ZoC violation
constexpr int csCheckTerritory = 3; // move within other nation's territory shortly after peace
constexpr int mxAdjacent = 0x00000001; // unit_MoveEx option
struct GroupTransportPlan {
int LoadLoc = -1;
int uixTransport = 0;
int nLoad = 0;
int TurnsEmpty = 0;
int TurnsLoaded = 0;
int uixLoad[16] = {};
};
// Total number of prerequisites for each advance; computed once at process
// startup (mirrors ToolAI.pas's SetAdvancedness(), run from its
// `initialization` section).
extern const std::array<int, nAdv> Advancedness;
// C++ equivalent of ToolAI.pas's TToolAI: map analysis, job assignment,
// pathfinding and sea-transport planning helpers built on top of CustomAI's
// raw Server-call wrappers.
//
// Like the original, the scratch state below (formation/district/cityResult,
// job-assignment and sea-transport working buffers, the Pile instance) is
// process-shared (static), not per-nation: AI turn processing is strictly
// sequential (one nation's DoTurn() completes before another's starts), so
// the original reused unit-level globals instead of per-instance buffers.
class ToolAI : public CustomAI {
public:
using CustomAI::CustomAI;
protected:
int centerOfEmpire() const; // tile that is in the middle of all own cities
// Exact difference of income and maintenance cost for a single city;
// positive = income higher than maintenance. cityReport must have been
// prepared (city_GetReport/city_GetHypoReport) before calling this.
int cityTaxBalance(int cix, const TCityReport &cityReport) const;
// Exact total tax and science income; tax is reduced by maintenance (so
// might be negative). Luxury not supported.
void sumCities(int taxRate, int &taxSum, int &scienceSum) const;
void optimizeCityTiles() const; // obsolete; use city_OptimizeTiles instead
void getCityProdPotential(); // fills cityResult[cix] with production potential
void getCityTradePotential(); // fills cityResult[cix] with trade potential
// Settler job assignment: initialize, add jobs/units, then go; assigns
// each job to one settler, moves the settlers and makes them work.
void jobAssignment_Initialize();
void jobAssignment_AddJob(int loc, int job, int score);
void jobAssignment_AddUnit(int uix);
void jobAssignment_Go();
bool jobAssignment_GotJob(int uix) const;
void analyzeMap(); // calculates formations (continents/oceans) and districts
// Forecast a single unit move between adjacent tiles. fromTile/toTile
// must be Map[FromLoc]/Map[ToLoc] (not location codes). crossCorner=1 for
// long moves crossing the tile corner, 0 for short ones that don't.
int checkStep(int moveStyle, int timeBeforeStep, int crossCorner, int &timeAfterStep, int &recoverTurns,
std::uint32_t fromTile, std::uint32_t toTile, bool isCapture) const;
int getMyMoveStyle(int mix, int health) const;
int unit_MoveEx(int uix, int toLoc, int options = 0);
// Sea transport, obligatory call order: beginInitialize; [addLoad/
// addTransport/addDestination]*; endInitialize; [makeGroupPlan]*.
void seaTransport_BeginInitialize();
void seaTransport_EndInitialize();
void seaTransport_AddLoad(int uix);
void seaTransport_AddTransport(int uix);
void seaTransport_AddDestination(int loc);
bool seaTransport_MakeGroupPlan(GroupTransportPlan &transportPlan);
int currentMStrength(int domain) const;
static int nContinent, nOcean, nDistrict;
// water: ocean index, land: continent index, sorted by size; territory
// unpassable due to peace treaty divides a continent.
static std::array<int, lxmax * lymax> formation;
// index of coherent own territory, sorted by size.
static std::array<int, lxmax * lymax> district;
static std::array<int, nCmax> cityResult;
// Shared single-instance priority queue (mirrors Pile.pas being its own
// unit used directly by both ToolAI.pas and Barbarina.pas); protected so
// Barbarina can use it for its own pathfinding (attack/patrol scoring).
static Pile pile;
private:
// JobAssignment state
static int maxScore;
static std::array<std::uint8_t, lxmax * lymax> tileJob;
static std::array<std::uint8_t, lxmax * lymax> tileJobScore;
static std::array<int, nUmax> jobLocOfSettler; // ToAssign sentinel = find job
// SeaTransport state
static int transportMoveStyle, transportCapacity, nTransportLoad;
static bool initComplete, haveDestinations;
static std::array<int, nUmax> uixTransportLoad;
static std::array<int, nUmax> transportAvailable;
static std::array<std::int8_t, lxmax * lymax> turnsAfterLoad;
};
} // namespace Cevo