354 lines
13 KiB
C++
354 lines
13 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.
|
|
|
|
#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 <QCoreApplication>
|
|
#include <QDateTime>
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QPoint>
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
|
|
namespace Cevo {
|
|
namespace GameServer {
|
|
|
|
|
|
// Equivalent of Directories.pas's GetAiDir() - see Brain.cpp's own copy of
|
|
// this (not shared via a header since Directories.pas itself isn't ported).
|
|
QString getAiDir()
|
|
{
|
|
#ifdef CEVO_AI_DIR
|
|
return QStringLiteral(CEVO_AI_DIR);
|
|
#else
|
|
return QString();
|
|
#endif
|
|
}
|
|
|
|
|
|
// Convenience wrapper matching Pascal's `Notify(Id)` (Index defaults to 0).
|
|
void notifyEvent(NotifyId id, int index)
|
|
{
|
|
if (notify)
|
|
notify(id, index);
|
|
}
|
|
|
|
|
|
// Re-applies previously-recorded sIntExpandTerritory diffs for the current
|
|
// player's turn (used after loading/replaying a game - during live play
|
|
// CheckBorders computes and applies territory changes directly instead).
|
|
void insertTerritoryUpdateCommands()
|
|
{
|
|
while (Database::cl.progress() < 1000) {
|
|
const CmdListState formerState = Database::cl.state();
|
|
int command = 0;
|
|
int p1 = 0;
|
|
int subject = 0;
|
|
const void *data = nullptr;
|
|
Database::cl.get(command, p1, subject, data);
|
|
if (command == sIntExpandTerritory && p1 == pTurn) {
|
|
Database::intServer(command, p1, subject, data);
|
|
} else {
|
|
Database::cl.setState(formerState);
|
|
break;
|
|
}
|
|
}
|
|
Database::invalidTreatyMap = 0;
|
|
}
|
|
|
|
|
|
void callPlayer(int command, int p, void *data)
|
|
{
|
|
if (Database::mode != Database::moMovie || p == 0) {
|
|
try {
|
|
playerBrain[p]->module.call(command, p, data);
|
|
} catch (...) {
|
|
// Fixes an apparent typo in the original (`Notify(ntException +
|
|
// bix[P])` doesn't even type-check as written - enum + object -
|
|
// matching the correct 2-arg Notify(Id, Index) calls used
|
|
// everywhere else in this same file, e.g. InitBrain below).
|
|
if (notify)
|
|
notify(NotifyId::Exception, brains.indexOf(playerBrain[p]));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void callAllPlayers(int command, void *data)
|
|
{
|
|
for (int i = 0; i < nPl; ++i)
|
|
if (playerBrain[i])
|
|
callPlayer(command, i, data);
|
|
}
|
|
|
|
|
|
void callClient(int brainIndex, int command, void *data)
|
|
{
|
|
if (Database::mode != Database::moMovie || brainIndex == brains.indexOf(playerBrain[0])) {
|
|
try {
|
|
brains[brainIndex].module.call(command, -1, data);
|
|
} catch (...) {
|
|
if (notify)
|
|
notify(NotifyId::Exception, brainIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void putMessage(int level, const QString &text)
|
|
{
|
|
const QByteArray bytes = text.toUtf8();
|
|
playerBrain[0]->module.call(cDebugMessage, level, const_cast<char *>(bytes.constData()));
|
|
}
|
|
|
|
|
|
void changeClientWhenDone(int command, int player, const void *data, int dataSize)
|
|
{
|
|
ccCommand = command;
|
|
ccPlayer = player;
|
|
if (dataSize > 0)
|
|
std::memcpy(ccData.data(), data, static_cast<std::size_t>(dataSize));
|
|
notifyEvent(NotifyId::ChangeClient);
|
|
}
|
|
|
|
|
|
void forceClientDeactivation()
|
|
{
|
|
if (pDipActive < 0) {
|
|
server(sTurn, pTurn, 0, nullptr); // no nego mode
|
|
} else {
|
|
switch (lastEndClientCommand) { // nego mode
|
|
case scContact:
|
|
server(scReject, pDipActive, 0, nullptr);
|
|
break;
|
|
case scDipCancelTreaty:
|
|
case scDipBreak:
|
|
server(scDipNotice, pDipActive, 0, nullptr);
|
|
break;
|
|
default: {
|
|
TOffer nullOffer{};
|
|
nullOffer.nDeliver = 0;
|
|
nullOffer.nCost = 0;
|
|
server(scDipOffer, pDipActive, 0, &nullOffer);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void inform(int p)
|
|
{
|
|
TPlayerContext &player = Database::rw[p];
|
|
player.Turn = Database::gTurn;
|
|
if (Database::gTurn == maxTurn && p == pTurn && p == 0)
|
|
player.Happened |= phTimeUp;
|
|
if (gWinner > 0 && p == pTurn && p == 0)
|
|
player.Happened |= phShipComplete;
|
|
player.Alive = Database::gAlive;
|
|
std::memcpy(player.Wonder, Database::gWonder.data(), sizeof(player.Wonder));
|
|
std::memcpy(player.Ship, Database::gShip.data(), sizeof(player.Ship));
|
|
for (int p1 = 0; p1 < nPl; ++p1)
|
|
if (p1 != p && playerBrain[p1] && Database::difficulty[p1] > 0)
|
|
player.EnemyReport[p1]->Credibility = Database::rw[p1].Credibility;
|
|
for (int p1 = 0; p1 < nPl; ++p1) {
|
|
if (p1 != p && ((1 << p1) & Database::gAlive)) {
|
|
if ((Database::gTestFlags & tfUncover) || Database::difficulty[p] == 0 ||
|
|
player.Treaty[p1] >= trFriendlyContact)
|
|
Database::giveCivilReport(p, p1);
|
|
if ((Database::gTestFlags & tfUncover) || Database::difficulty[p] == 0 || player.Treaty[p1] == trAlliance)
|
|
Database::giveMilReport(p, p1);
|
|
}
|
|
}
|
|
TModelInfo *enemyModel = *player.EnemyModel;
|
|
for (int i = 0; i < player.nEnemyModel; ++i)
|
|
enemyModel[i].Lost = Database::destroyed[p][enemyModel[i].Owner][enemyModel[i].mix];
|
|
}
|
|
|
|
|
|
void logChanges()
|
|
{
|
|
for (int p = 0; p < nPl; ++p) {
|
|
if (((1 << p) & Database::gWatching) && Database::processClientData[p]) {
|
|
TPlayerContext &player = Database::rw[p];
|
|
TUn *un = *player.Un;
|
|
for (int ix = 0; ix < player.nUn; ++ix)
|
|
if (un[ix].Loc >= 0 && un[ix].SavedStatus != un[ix].Status) {
|
|
Database::cl.put(sIntSetUnitStatus, p, ix, &un[ix].Status);
|
|
un[ix].SavedStatus = un[ix].Status;
|
|
}
|
|
TCity *city = *player.City;
|
|
for (int ix = 0; ix < player.nCity; ++ix)
|
|
if (city[ix].Loc >= 0 && city[ix].SavedStatus != city[ix].Status) {
|
|
Database::cl.put(sIntSetCityStatus, p, ix, &city[ix].Status);
|
|
city[ix].SavedStatus = city[ix].Status;
|
|
}
|
|
TModel *model = *player.Model;
|
|
for (int ix = 0; ix < player.nModel; ++ix)
|
|
if (model[ix].SavedStatus != model[ix].Status) {
|
|
Database::cl.put(sIntSetModelStatus, p, ix, &model[ix].Status);
|
|
model[ix].SavedStatus = model[ix].Status;
|
|
}
|
|
TCityInfo *enemyCity = *player.EnemyCity;
|
|
for (int ix = 0; ix < player.nEnemyCity; ++ix)
|
|
if (enemyCity[ix].Loc >= 0 && enemyCity[ix].SavedStatus != enemyCity[ix].Status) {
|
|
Database::cl.put(sIntSetECityStatus, p, ix, &enemyCity[ix].Status);
|
|
enemyCity[ix].SavedStatus = enemyCity[ix].Status;
|
|
}
|
|
if (playerBrain[p]->dataSize > 0) {
|
|
Database::cl.putDataChanges(sIntDataChange, p, static_cast<std::uint32_t *>(savedData[p]),
|
|
static_cast<std::uint32_t *>(player.Data), playerBrain[p]->dataSize);
|
|
std::memcpy(savedData[p], player.Data, static_cast<std::size_t>(playerBrain[p]->dataSize) * 4);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void noLogChanges()
|
|
{
|
|
for (int p = 0; p < nPl; ++p) {
|
|
if (((1 << p) & Database::gWatching) && Database::processClientData[p]) {
|
|
TPlayerContext &player = Database::rw[p];
|
|
TUn *un = *player.Un;
|
|
for (int ix = 0; ix < player.nUn; ++ix)
|
|
un[ix].SavedStatus = un[ix].Status;
|
|
TCity *city = *player.City;
|
|
for (int ix = 0; ix < player.nCity; ++ix)
|
|
city[ix].SavedStatus = city[ix].Status;
|
|
TModel *model = *player.Model;
|
|
for (int ix = 0; ix < player.nModel; ++ix)
|
|
model[ix].SavedStatus = model[ix].Status;
|
|
TCityInfo *enemyCity = *player.EnemyCity;
|
|
for (int ix = 0; ix < player.nEnemyCity; ++ix)
|
|
enemyCity[ix].SavedStatus = enemyCity[ix].Status;
|
|
if (playerBrain[p]->dataSize > 0)
|
|
std::memcpy(savedData[p], player.Data, static_cast<std::size_t>(playerBrain[p]->dataSize) * 4);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
bool hasChanges(int p)
|
|
{
|
|
TPlayerContext &player = Database::rw[p];
|
|
const TUn *un = *player.Un;
|
|
for (int ix = 0; ix < player.nUn; ++ix)
|
|
if (un[ix].Loc >= 0 && un[ix].SavedStatus != un[ix].Status)
|
|
return true;
|
|
const TCity *city = *player.City;
|
|
for (int ix = 0; ix < player.nCity; ++ix)
|
|
if (city[ix].Loc >= 0 && city[ix].SavedStatus != city[ix].Status)
|
|
return true;
|
|
const TModel *model = *player.Model;
|
|
for (int ix = 0; ix < player.nModel; ++ix)
|
|
if (model[ix].SavedStatus != model[ix].Status)
|
|
return true;
|
|
const TCityInfo *enemyCity = *player.EnemyCity;
|
|
for (int ix = 0; ix < player.nEnemyCity; ++ix)
|
|
if (enemyCity[ix].Loc >= 0 && enemyCity[ix].SavedStatus != enemyCity[ix].Status)
|
|
return true;
|
|
if (player.Data) {
|
|
const auto *savedWords = static_cast<const std::uint32_t *>(savedData[p]);
|
|
const auto *currentWords = static_cast<const std::uint32_t *>(player.Data);
|
|
for (int ix = 0; ix < playerBrain[p]->dataSize; ++ix)
|
|
if (savedWords[ix] != currentWords[ix])
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
void initBrain(Brain &brain)
|
|
{
|
|
assert(brain.kind != BrainType::SuperVirtual);
|
|
if (brain.initialized)
|
|
return;
|
|
if (brain.kind == BrainType::AI) { // get client function
|
|
if (notify)
|
|
notify(NotifyId::InitModule, brains.indexOf(&brain));
|
|
if (!brain.module.load(brain.dllName.toStdString()))
|
|
notify(NotifyId::DllError, brains.indexOf(&brain));
|
|
}
|
|
if (brain.module.isLoaded()) {
|
|
brain.initialized = true;
|
|
TInitModuleData initModuleData{};
|
|
initModuleData.Server = &server;
|
|
initModuleData.DataVersion = 0;
|
|
initModuleData.DataSize = 0;
|
|
initModuleData.Flags = 0;
|
|
callClient(brains.indexOf(&brain), cInitModule, &initModuleData);
|
|
brain.dataVersion = initModuleData.DataVersion;
|
|
brain.dataSize = (initModuleData.DataSize + 3) / 4;
|
|
if (brain.dataSize > MaxDataSize)
|
|
brain.dataSize = 0;
|
|
brain.flags |= initModuleData.Flags;
|
|
}
|
|
}
|
|
|
|
|
|
void changeClient()
|
|
{
|
|
const double t = nowPrecise();
|
|
putMessage((1 << 16) + 2, QStringLiteral("CLIENT: took %1 ms").arg((t - lastClientTime) * 1000.0, 0, 'f', 1));
|
|
lastClientTime = t;
|
|
putMessage((1 << 16) + 2,
|
|
QStringLiteral("CLIENT: calling %1 (%2)").arg(ccPlayer).arg(playerBrain[ccPlayer]->name()));
|
|
if (ccCommand == cTurn)
|
|
for (int p = 0; p < nPl; ++p)
|
|
if (p != ccPlayer && ((1 << p) & Database::gWatching))
|
|
callPlayer(cShowTurnChange, p, &ccPlayer);
|
|
|
|
const int p = ccPlayer;
|
|
ccPlayer = -1;
|
|
callPlayer(ccCommand, p, ccData.data());
|
|
if (Database::mode == Database::moPlaying && (playerBrain[p]->flags & aiThreaded) == 0 && ccPlayer < 0) {
|
|
notifyEvent(NotifyId::DeactivationMissing, p);
|
|
forceClientDeactivation();
|
|
}
|
|
}
|
|
|
|
} // namespace GameServer
|
|
} // namespace Cevo
|