// 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. #include "GameServer.h" #include "GameServerInternal.h" #include "Global.h" #include "Cevo/CityProcessing.h" #include "Cevo/CmdList.h" #include "Cevo/Database.h" #include "Cevo/Map.h" #include "Cevo/Platform.h" #include "Cevo/UnitProcessing.h" #include #include #include #include #include #include #include #include #include #include #include namespace Cevo { namespace GameServer { std::array playersBrain{}; Brains brains; QString notifyMessage; Brain *brainNoTerm = nullptr; Brain *brainSuperVirtual = nullptr; Brain *brainTerm = nullptr; Brain *brainRandom = nullptr; Brain *brainNetworkClient = nullptr; Brain *brainNetworkServer = nullptr; bool networkEnabled = false; NotifyFunction notify; // Equivalent of Pascal's unit-level private `bix: TBrains` ("brain of the // players") - renamed to avoid colliding with the pervasive `bix` // parameter/loop-variable name used for brain *indices* elsewhere in the // original (e.g. InitBrain(bix: TBrain), CallClient(bix, Command, ...)). std::array playerBrain{}; bool previewElevation = false; // Remaining unit-level private state needed by this slice (the rest of // GameServer.pas's big private var block belongs to the not-yet-ported // SaveGame/StartGame/LoadGame/BeforeTurn/AfterTurn/Server()). int maxTurn = 0; int pTurn = 0; int gWinner = 0; int gColdWarStart = 0; int pDipActive = -1; int pContacted = -1; TOffer lastOffer{}; int uixSelectedTransport = -1; int spyMission = 0; bool winOnAlone = false; bool autoSaveExists = false; CmdListState autoSaveState{}; std::array savedData{}; std::array debugMap{}; std::array originalDataVersion{}; std::array devModelTurn{}; // turn of last sCreateDevModel call (comment name is stale in the original too) std::array aiInfo{}; QString aiCredits; // Persistent byte-buffer caches for sGetAIInfo/sGetAICredits, which hand // out a raw const char* into AIInfo[]/AICredits (valid until the next such // call - same "valid until next mutating call" convention already used // for CmdList::get's data pointer). QByteArray aiInfoUtf8Cache; QByteArray aiCreditsUtf8Cache; QString mapFileName; // name of map to use, empty for random (set by not-yet-ported StartNewGame/LoadGame) QString logFileName; QString savePath; // directory of the file for saving the current game int lastEndClientCommand = -1; int ccCommand = 0; int ccPlayer = -1; std::array ccData{}; double lastClientTime = 0.0; int gStealFrom = -1; int zocTile = -1; int loadTurn = 0; // turn where to stop loading int nLogOpened = 0; // nLog of opened book bool movieStopped = false; bool loadOK = false; // SavedTiles[cix] - a single shared scratch array (not per-player): only // RW[pTurn]'s cities are ever "in focus" at a time, matching the original. std::array savedTiles{}; // Predefined map snapshot (only non-empty when Database::landMass == 0) - // a disclosed std::vector-based simplification of the original's // GetMem/FreeMem(MapField) raw pointer (not a wire format, pure in-memory // bookkeeping, same rationale as CmdList's QByteArray backing buffer). std::vector mapField; // Stat[st][p][turn] equivalent - std::vector instead of GetMem/FreeMem for // the same reason as mapField above. std::array, nPl>, nStat> stat{}; void init(const NotifyFunction ¬ifyFunction) { notify = notifyFunction; previewElevation = false; playersBrain.fill(nullptr); playerBrain.fill(nullptr); brains.clear(); brainNoTerm = &brains.addNew(); brainNoTerm->fileName = QStringLiteral(":AIT"); brainNoTerm->flags = 0; brainNoTerm->initialized = false; brainNoTerm->kind = BrainType::NoTerm; brainSuperVirtual = &brains.addNew(); brainSuperVirtual->fileName = QStringLiteral(":Supervisor"); brainSuperVirtual->flags = 0; brainSuperVirtual->initialized = false; brainSuperVirtual->kind = BrainType::SuperVirtual; if (networkEnabled) { brainNetworkClient = &brains.addNew(); brainNetworkClient->fileName = QStringLiteral(":NetworkClient"); brainNetworkClient->flags = fMultiple; brainNetworkClient->initialized = false; brainNetworkClient->serverVersion = CevoVersion; brainNetworkClient->kind = BrainType::NetworkClient; } brainTerm = &brains.addNew(); brainTerm->fileName = QStringLiteral(":StdIntf"); brainTerm->flags = fMultiple; brainTerm->initialized = false; brainTerm->serverVersion = CevoVersion; brainTerm->kind = BrainType::Term; brainRandom = &brains.addNew(); brainRandom->fileName = QStringLiteral(":Random"); brainRandom->flags = fMultiple; brainRandom->initialized = false; brainRandom->kind = BrainType::Random; if (networkEnabled) { brainNetworkServer = &brains.addNew(); brainNetworkServer->fileName = QStringLiteral(":NetworkServer"); brainNetworkServer->flags = fMultiple; brainNetworkServer->initialized = false; brainNetworkServer->serverVersion = CevoVersion; brainNetworkServer->kind = BrainType::NetworkServer; } // get available brains const QDir aiDir(getAiDir()); const QStringList entries = aiDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name); for (const QString &name : entries) { const QString basePath = aiDir.filePath(name); auto newBrain = std::make_unique(); newBrain->kind = BrainType::AI; try { newBrain->loadFromFile(QDir(basePath).filePath(name + QStringLiteral(".ai.txt"))); } catch (const std::exception &) { // Disclosed deviation: the original has no try/except around // LoadFromFile here, so one malformed AI spec file would crash // the whole Init() - skipping just that one brain instead. continue; } // DotNetClient/.NET AI module interop is not supported by this // port (Windows/.NET-specific, no sensible cross-platform // equivalent) - simplified from the original's `(Flags and // fDotNet = 0) or (@DotNetClient <> nil)` accordingly. if (newBrain->serverVersion >= FirstAICompatibleVersion && newBrain->serverVersion <= CevoVersion && (newBrain->flags & fDotNet) == 0 && !newBrain->dllName.isEmpty()) { brains.add(std::move(newBrain)); } } if (brains.kindCount(BrainType::AI) == 0) throw std::runtime_error(("No AI libraries found in directory " + getAiDir()).toStdString()); } void done() { for (int i = 0; i < brains.count(); ++i) { Brain &brain = brains[i]; if (brain.initialized) { callClient(i, cReleaseModule, nullptr); if (brain.kind == BrainType::AI && (brain.flags & fDotNet) == 0) brain.module.unload(); } } playersBrain.fill(nullptr); playerBrain.fill(nullptr); brains.clear(); } } // namespace GameServer } // namespace Cevo