353 lines
15 KiB
C++
353 lines
15 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/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;
|
|
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 int landMass; // 0 = predefined map, >0 = random map generation target
|
|
extern std::int32_t delphiRandSeed;
|
|
int delphiRandom(int max); // Delphi/Borland-compatible LCG, result in [0, max)
|
|
|
|
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> researched;
|
|
extern std::array<int, nPl> lastValidStat;
|
|
extern std::array<int, nPl> nTech; // number of known techs
|
|
extern std::array<int, nPl + 1> territoryCount; // index nPl means "no owner"
|
|
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<int, nPl> startLoc;
|
|
extern std::array<int, nPl> startLoc2;
|
|
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();
|
|
// Fills the elevation table from the current random seed; createMap() then
|
|
// builds realMap from it. With preview=true, rivers, continents and rare
|
|
// resources are skipped (used for the start screen's mini map).
|
|
void createElevation();
|
|
void createMap(bool preview);
|
|
|
|
// --- 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);
|
|
|
|
// Computes Attack/Defense/Speed/Cost/Weight from a model's Cap[]/MStrength/
|
|
// MCost fields (the "Create Development Model" calculator), then calls
|
|
// setModelFlags().
|
|
void calculateModel(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);
|
|
|
|
// Marks an advance as seen (known exists, not yet researched) - used by
|
|
// the Great Library wonder effect.
|
|
void seeTech(int p, int ad);
|
|
// Applies the full effects of player p completing research of advance ad
|
|
// (tech counters, key-tech disclosure, special models, wonder expiration).
|
|
void discoverTech(int p, int ad);
|
|
// Promotes RW[p].DevModel (set by a completed sResetModel) into the
|
|
// player's real model list.
|
|
void enableDevModel(int p);
|
|
|
|
// --- 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);
|
|
// Removes every unit of player p at loc (not just one) and clears
|
|
// occupant/ZoC bookkeeping for that tile. Enemy >= 0 counts the units as
|
|
// lost to that player.
|
|
void removeAllUnits(int p, int loc, int enemy = -1);
|
|
void foundCity(int p, int foundLoc);
|
|
|
|
// Removes dead (Loc < 0) entries from a player's Un/City/EnemyCity lists,
|
|
// fixing up cross-references (Un::Master/Home) to the indices that move.
|
|
void compactLists(int p);
|
|
|
|
// 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);
|
|
|
|
// --- Map Editor ---
|
|
|
|
// Allocates the supervisor's (player 0) map-editor-only Map/MapObservedLast/
|
|
// Territory buffers for standalone map editing (no other player, no
|
|
// initGame()). Must be paired with releaseMapEditor().
|
|
void initMapEditor();
|
|
// Converts a tile to ocean if it has no grass-or-better neighbor, to shore
|
|
// otherwise; returns true if the terrain type actually changed.
|
|
bool checkShore(int loc);
|
|
// Applies map-editor sanity rules (dead lands/canal/irrigation/mine
|
|
// clearing, automatic special resources, automatic shore tiles) and writes
|
|
// the result to realMap.
|
|
void editTile(int loc, std::uint32_t newTile);
|
|
// Frees the supervisor's (player 0) map-editor-only Map/MapObservedLast/
|
|
// Territory buffers (used for standalone map editing, without a full
|
|
// initGame()).
|
|
void releaseMapEditor();
|
|
|
|
// --- 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);
|
|
|
|
// recalculate fInEnemyZoC flags for the whole map
|
|
void recalcMapZoC(int p);
|
|
|
|
// recalculate fInEnemyZoC flags around a single tile (cheaper than a full
|
|
// recalcMapZoC() when only one location may have changed).
|
|
void recalcV8ZoC(int p, int loc);
|
|
|
|
// --- Diplomacy ---
|
|
|
|
void introduceEnemy(int p1, int p2);
|
|
int techCost(int p);
|
|
void giveCivilReport(int p, int pAbout);
|
|
void giveMilReport(int p, int pAbout);
|
|
// Copies pAbout's civil/military report as known by pSender into pTarget's
|
|
// enemy-report of pAbout, if newer. Returns true if anything changed.
|
|
bool copyCivilReport(int pSender, int pTarget, int pAbout);
|
|
bool copyMilReport(int pSender, int pTarget, int pAbout);
|
|
// Grants pTarget a copy of pSender's model mix, unless an equivalent model
|
|
// (per IsSameModel) is already known.
|
|
void copyModel(int pSender, int pTarget, int mix);
|
|
// Copies every tile pSender has observed more recently than pTarget has.
|
|
void copyMap(int pSender, int pTarget);
|
|
// Executes a spy unit's mission against pCity's city cix; returns a
|
|
// mission-specific result bitset (only meaningful for smStealForeignReports).
|
|
std::uint32_t doSpyMission(int p, int pCity, int cix, int mission);
|
|
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);
|
|
|
|
// --- Test Flags ---
|
|
|
|
void clearTestFlags(int clearFlags);
|
|
void setTestFlags(int p, int setFlags);
|
|
|
|
// --- 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 startPositions()).
|
|
void initGame();
|
|
// Predefined map path: FindContinents + PredefinedStartPositions + initGame().
|
|
void initMapGame(int human);
|
|
// Random map path: CalculatePrimitive + CreateElevation + CreateMap + StartPositions + initGame().
|
|
void initRandomGame();
|
|
void findContinents();
|
|
void predefinedStartPositions(int human);
|
|
void releaseGame();
|
|
|
|
// --- City ---
|
|
|
|
// Removes a city (SaveUnits: true transfers supported units to the
|
|
// nearest remaining city instead of destroying them).
|
|
void destroyCity(int p, int cix, bool saveUnits);
|
|
void changeCityOwner(int pOld, int cixOld, int pNew);
|
|
|
|
} // namespace Database
|
|
} // namespace Cevo
|