// 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 #include 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; constexpr int nStartUn = 1; constexpr int StartUn[nStartUn] = { 0 }; // mix of start units // 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 extern int rnd; // world map randseed extern std::int32_t delphiRandSeed; int delphiRandom(int max); // Delphi/Borland-compatible LCG, result in [0, max) using Vicinity8Loc = std::array; using Vicinity21Loc = std::array; // player data extern std::array rw; extern std::array difficulty; extern std::array serverVersion; extern std::array resourceMask; extern std::array processClientData; extern std::array discovered; // number of tiles discovered extern std::array researched; extern std::array lastValidStat; extern std::array 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 int gInitialized; // bitset of players whose RW[p] arrays are allocated extern int peaceEnded; // side effect out-param of cancelTreaty(): bitset of players extern int saveMapCenterLoc; extern std::array startLoc; extern std::array startLoc2; extern std::array 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 gWonder; extern std::array grWallContinent; // continent id the Great Wall was built on, -1 if none extern std::array gShip; // [p1,p2,mix] -> index of p2's model mix in p1's enemy model list extern std::array, nPl>, nPl> rwemix; // [p1,p2,mix] -> number of p2's units with model mix that p1 has destroyed extern std::array, nPl>, nPl> destroyed; // map data extern std::array realMap; extern std::array continent; // continent id for each tile extern std::array occupant; // occupying player for each tile extern std::array zocMap; extern std::array usedByCity; // -1 if not exploited extern std::array 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); void showPrice(int pSender, int pTarget, std::uint32_t price); bool payPrice(int pSender, int pTarget, std::uint32_t price, bool execute); // Side effect: sets peaceEnded to the bitset of players with whom a peace // treaty was just canceled (matches the original's `PeaceEnded` global). void cancelTreaty(int p, int pWith, bool decreaseCredibility = true); // --- Game Initialization --- // Allocates and initializes every RW[p] array (Un/City/Model/Map/Territory/ // EnemyUn/EnemyCity/EnemyModel/EnemyReport/...) and creates each alive // player's starting units + capital. Needs startLoc/startLoc2 already set // (by predefinedStartPositions() or - not yet ported - StartPositions()). void initGame(); // Predefined map path: FindContinents + PredefinedStartPositions + initGame(). void initMapGame(int human); // TODO: needs CreateElevation/CreateMap/StartPositions/RarePositions (the // full procedural map generator, itself several hundred lines) - not // ported yet. Throws for now; only initMapGame()'s "load from file" path // is supported. void initRandomGame(); void findContinents(); void predefinedStartPositions(int human); void releaseGame(); } // namespace Database } // namespace Cevo