Files
c-evo-evo/game/include/Cevo/Database.h
T
2026-10-27 13:59:20 +01:00

243 lines
10 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/CmdList.h"
#include "Cevo/Protocol.h"
#include <array>
#include <cstdint>
namespace Cevo {
namespace Database {
// Equivalent of Database.pas's global `var` state and free functions. Kept
// as flat global state/free functions - matching the original's own
// architecture (a single shared-state game server module) - rather than
// wrapped in a class/singleton that wasn't asked for and would be a large,
// separate refactor.
//
// Database.pas is ~4080 lines with heavy dependencies on Protocol.pas's big
// gameplay structs (TPlayerContext, TModel, TCity, TUn, ...) - all already
// available via Cevo/Protocol.h (see game/CMakeLists.txt: cevogame links
// cevoai to reuse its exact port of those shared structs/constants, since
// perso/cevo/Protocol.pas and the AI-facing "AI Template/Protocol.pas"
// define them identically). Ported INCREMENTALLY regardless: this only
// covers globals/functions actually consumed by whichever caller (Database
// itself, CityProcessing, ...) has been ported so far. Add more here only
// once a new batch needs them.
//
// `rw[p].City`/`.Un`/`.Model` are pointer-to-array fields (TCityList* etc.)
// that stay null until Database's InitGame-equivalent (not ported yet)
// allocates them - dereference as `(*rw[p].City)[cix]`, or cache a decayed
// `TCity *city = *rw[p].City;` first if indexing repeatedly (same idiom
// ai/src/CustomAI.cpp already uses for `myCity`).
// Observe levels (Database.pas's own constants, not Protocol.pas's).
constexpr int lNoObserve = 0;
constexpr int lObserveUnhidden = 1;
constexpr int lObserveAll = 2;
constexpr int lObserveSuper = 3;
// Database.pas's own constant (not Protocol.pas's).
constexpr int CityOwnTile = 13;
constexpr int nemmax = 1024;
constexpr int necmax = 1024;
constexpr int neumax = 4096;
// Database.pas's `TerrType_Canalable` set, as a bitmask indexed by fTerrain.
constexpr std::uint32_t terrTypeCanalableMask = (1u << fGrass) | (1u << fDesert) | (1u << fPrairie) |
(1u << fTundra) | (1u << fSwamp) | (1u << fForest) | (1u << fHills);
// Equivalent of Database.pas's TGameMode - ordinal comparisons (</>) match
// the original's enum declaration order.
enum GameMode { moLoading_Fast, moLoading, moMovie, moPlaying };
extern GameMode mode;
// Equivalent of Database.pas's `CL: TCmdList` - the live command log.
extern CmdList cl;
extern int lx;
extern int ly;
extern int mapSize; // = lx * ly
using Vicinity8Loc = std::array<int, 8>;
using Vicinity21Loc = std::array<int, 28>;
// player data
extern std::array<TPlayerContext, nPl> rw;
extern std::array<int, nPl> difficulty;
extern std::array<int, nPl> serverVersion;
extern std::array<std::uint32_t, nPl> resourceMask;
extern std::array<bool, nPl> processClientData;
extern std::array<int, nPl> discovered; // number of tiles discovered
extern std::array<int, nPl> lastValidStat;
extern std::array<int, nPl> nTech; // number of known techs
extern int gAlive; // players alive; bitset of 1 shl p
extern int gWatching;
extern int gAI;
extern int gTurn; // current turn
extern int gTestFlags;
extern std::array<int, nPl> founded; // number of cities founded per player
// Debug-only cross-check (Database.pas's `{$IFOPT O-}InvalidTreatyMap`):
// bitset of players whose territory must not be considered mid-command.
extern int invalidTreatyMap;
extern std::array<TWonderInfo, nWonder> gWonder;
extern std::array<int, nPl> grWallContinent; // continent id the Great Wall was built on, -1 if none
extern std::array<TShipInfo, nPl> gShip;
// [p1,p2,mix] -> index of p2's model mix in p1's enemy model list
extern std::array<std::array<std::array<std::int16_t, nMmax>, nPl>, nPl> rwemix;
// [p1,p2,mix] -> number of p2's units with model mix that p1 has destroyed
extern std::array<std::array<std::array<std::int16_t, nMmax>, nPl>, nPl> destroyed;
// map data
extern std::array<std::uint32_t, lxmax * lymax> realMap;
extern std::array<int, lxmax * lymax> continent; // continent id for each tile
extern std::array<std::int8_t, lxmax * lymax> occupant; // occupying player for each tile
extern std::array<std::int8_t, lxmax * lymax> zocMap;
extern std::array<std::int32_t, lxmax * lymax> usedByCity; // -1 if not exploited
extern std::array<std::uint32_t, lxmax * lymax> observeLevel; // observe level of player p in bits 2*p, 2*p+1
// Equivalent of Database.pas's MaskD().
void maskD(std::uint32_t *x, int count, std::uint32_t mask);
// Equivalent of Database.pas's IntServer(): dispatches internal server
// notifications (sIntXxx command codes) and records them to the command
// log. Declared here (used by several Map Revealing functions below) but
// only implemented once its own dependency chain (IntroduceEnemy,
// CancelTreaty, ...) exists - see Database.cpp for current coverage.
void intServer(int command, int player, int subject, const void *data);
// --- Location Navigation ---
// Relative location, dx in hor and dy in ver direction from loc. Returns -1
// if the result would fall outside the map.
int dLoc(int loc, int dx, int dy);
void dxdy(int loc0, int loc1, int &dx, int &dy);
int distance(int loc0, int loc1);
void v8ToLoc(int loc0, Vicinity8Loc &vicinityLoc);
void v21ToLoc(int loc0, Vicinity21Loc &vicinityLoc);
// --- Map Creation ---
void calculatePrimitive();
bool mapGeneratorAvailable();
// --- Tech Related Functions ---
int techBaseCost(int nTech, int diff);
// Sets a model's Flags (ZOC/civil/double-support) from its other fields.
void setModelFlags(TModel &m);
// Grants player p any SpecialModel[] not yet available that pre unlocks.
void checkSpecialModels(int p, int pre);
// GWonder[wonder].EffectiveOwner must already be set before calling this.
void checkExpiration(int wonder);
// --- Map Revealing ---
// cix>=0: known city index of player p (internal use only).
// cix=-1: search city, player unknown, only if permission for p.
// cix=-2: don't search city, don't calculate city benefits, just government.
int getTileInfo(int p, int cix, int loc, TTileInfo &info);
// Finds the strongest defender at loc.
void strongest(int loc, int &uix, int &strength, int &bonus, int &cnt);
int unitSpeed(int p, int mix, int health);
void getUnitReport(int p, int uix, TUnitReport &unitReport);
void tellAboutModel(int p, int taOwner, int tamix);
int emixSafe(int p, int taOwner, int tamix);
int getUnitStack(int p, int loc);
void makeCityInfo(int p, int cix, TCityInfo &ci);
// euix = -2: full discover. euix = -1: unit and city only, append to
// EnemyUn. euix >= 0: unit and city only, replace EnemyUn[euix].
bool discoverTile(int loc, int p, int pTell, int level, bool enableContact, int euix = -2);
bool discover9(int loc, int p, int level, bool tellAllied, bool enableContact);
bool discover21(int loc, int p, int adjacentLevel, bool tellAllied, bool enableContact);
// Player p discovers the complete playground (for the supervisor).
void discoverAll(int p, int level);
// Discovers unit and city view areas.
void discoverViewAreas(int p);
// Updates maps and enemy units of all players after a unit change.
void updateUnitMap(int loc, bool cityChange = false);
// Sets p to the supposed owner before the call.
void searchCity(int loc, int &p, int &cix);
// --- Map Processing ---
void createUnit(int p, int mix);
void placeUnit(int p, int uix);
void freeUnit(int p, int uix);
// loc or master should be set after call. Enemy >=0 counts the unit as lost
// to that player (use only from inside sMoveUnit if attack).
void removeUnit(int p, int uix, int enemy = -1);
void removeUnitUpdateMap(int p, int uix);
void removeDomainUnits(int d, int p, int loc);
void foundCity(int p, int foundLoc);
// Database.pas's own formula function (originally in Protocol.pas), used
// only by actualSpecialTile() to recompute hidden special-resource bits
// after a terrain change (ClearTerrain/AfforestTerrain/TransTerrain jobs).
int specialTile(int loc, int terrType, int lx);
std::uint32_t actualSpecialTile(int loc);
// Applies a completed terrain improvement job's effect to the map.
void completeJob(int p, int loc, int job);
// --- Territory Calculation ---
// OriginLoc: only changes in CountryRadius around this location possible,
// -1 for complete map, -2 for double-check (debug only). PlayerLosingCity:
// do nothing but remove tiles no longer in reach from this player's
// territory, -1 for full border recalculation.
void checkBorders(int originLoc, int playerLosingCity = -1);
void logCheckBorders(int p, int cix, int playerLosingCity = -1);
// recalculate fPeace flags for the whole map
void recalcPeaceMap(int p);
// --- Diplomacy ---
void introduceEnemy(int p1, int p2);
int techCost(int p);
void giveCivilReport(int p, int pAbout);
void giveMilReport(int p, int pAbout);
} // namespace Database
} // namespace Cevo