Files
c-evo-evo/game/Brain.cpp
T

221 lines
7.1 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.
#include "Brain.h"
#include "Global.h"
#include "Cevo/CevoScreenTools.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <stdexcept>
namespace Cevo {
namespace {
// Equivalent of Directories.pas's GetAiDir() - not ported in full (plain
// path utility, deferred along with the rest of that unit); just enough to
// resolve the install-time default set up by CMakeLists.txt.
QString getAiDir()
{
#ifdef CEVO_AI_DIR
return QStringLiteral(CEVO_AI_DIR);
#else
return QString();
#endif
}
// Which #PATH_xxx key names this platform/architecture's DLL path, matching
// Brain.pas's {$IFDEF WINDOWS/UNIX}{$IFDEF CPUxxx} chain at compile time.
#if defined(Q_OS_WIN) && !defined(Q_PROCESSOR_X86_64) && !defined(Q_PROCESSOR_ARM_64) && !defined(Q_PROCESSOR_IA64)
constexpr const char *platformPathKey = "#PATH_WIN32";
#elif defined(Q_OS_WIN)
constexpr const char *platformPathKey = "#PATH_WIN64";
#elif defined(Q_OS_UNIX) && defined(Q_PROCESSOR_X86_32)
constexpr const char *platformPathKey = "#PATH_LINUX_I386";
#elif defined(Q_OS_UNIX) && defined(Q_PROCESSOR_X86_64)
constexpr const char *platformPathKey = "#PATH_LINUX_AMD64";
#elif defined(Q_OS_UNIX) && defined(Q_PROCESSOR_ARM) && !defined(Q_PROCESSOR_ARM_64)
constexpr const char *platformPathKey = "#PATH_LINUX_ARM32";
#elif defined(Q_OS_UNIX) && defined(Q_PROCESSOR_ARM_64)
constexpr const char *platformPathKey = "#PATH_LINUX_ARM64";
#else
constexpr const char *platformPathKey = nullptr;
#endif
} // namespace
Brain::Brain()
{
picture.setSize(64, 64);
}
QString Brain::name() const
{
if (!m_name.isEmpty())
return m_name;
return phrases.lookup(lookupName);
}
void Brain::loadPicture()
{
const QString path = QDir(QDir(getAiDir()).filePath(fileName)).filePath(fileName + QStringLiteral(".png"));
if (QFileInfo::exists(path)) {
picture.loadFromFile(path);
if (!picture.image().isNull())
return;
}
Dpi::Canvas &canvas = picture.canvas();
canvas.begin();
canvas.brush().setColor(colorFromDelphi(0x904830));
canvas.fillRect(QRect(0, 0, picture.width(), picture.height()));
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Tiny)]);
canvas.font().setBold(false);
canvas.font().setItalic(false);
canvas.font().setUnderline(false);
canvas.font().setColor(colorFromDelphi(0x5FDBFF));
const QSize textSize = canvas.textExtent(fileName);
canvas.textOut((picture.width() - textSize.width()) / 2, (picture.height() - textSize.height()) / 2, fileName);
canvas.end();
}
void Brain::loadFromFile(const QString &aiFileName)
{
const QString basePath = QFileInfo(aiFileName).path();
fileName = QFileInfo(QFileInfo(aiFileName).completeBaseName()).completeBaseName();
setName(fileName);
dllName.clear();
credits.clear();
flags = fMultiple;
module.unload();
initialized = false;
serverVersion = 0;
if (!QFileInfo::exists(aiFileName))
throw std::runtime_error(("AI specification file " + aiFileName + " not found").toStdString());
QFile file(aiFileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
throw std::runtime_error(("AI specification file " + aiFileName + " not found").toStdString());
QTextStream stream(&file);
while (!stream.atEnd()) {
const QString line = stream.readLine().trimmed();
QString key;
QString value;
const int spacePos = line.indexOf(QLatin1Char(' '));
if (spacePos >= 0) {
key = line.left(spacePos);
value = line.mid(spacePos + 1).trimmed();
} else {
key = line;
}
if (key == QStringLiteral("#NAME")) {
setName(value);
} else if (key == QStringLiteral("#.NET")) {
flags |= fDotNet;
} else if (key == QStringLiteral("#BEGINNER")) {
beginner = true;
} else if (key == QStringLiteral("#PATH")) {
dllName = QDir(basePath).filePath(value);
} else if (platformPathKey && key == QLatin1String(platformPathKey)) {
dllName = QDir(basePath).filePath(value);
} else if (key == QStringLiteral("#GAMEVERSION")) {
for (const QChar &ch : value) {
if (ch.isDigit())
serverVersion = (serverVersion & 0xFFFF00) + ((serverVersion & 0xFF) * 10) +
(ch.toLatin1() - '0');
else if (ch == QLatin1Char('.'))
serverVersion <<= 8;
}
} else if (key == QStringLiteral("#CREDITS")) {
credits = value;
}
}
}
Brain &Brains::addNew()
{
m_items.push_back(std::make_unique<Brain>());
return *m_items.back();
}
Brain &Brains::add(std::unique_ptr<Brain> brain)
{
m_items.push_back(std::move(brain));
return *m_items.back();
}
int Brains::indexOf(const Brain *brain) const
{
for (std::size_t i = 0; i < m_items.size(); ++i)
if (m_items[i].get() == brain)
return static_cast<int>(i);
return -1;
}
int Brains::kindCount(BrainType kind) const
{
int result = 0;
for (const auto &brain : m_items)
if (brain->kind == kind)
++result;
return result;
}
std::vector<Brain *> Brains::getByKind(BrainType kind) const
{
std::vector<Brain *> result;
for (const auto &brain : m_items)
if (brain->kind == kind)
result.push_back(brain.get());
return result;
}
Brain *Brains::beginner() const
{
for (const auto &brain : m_items)
if (brain->beginner)
return brain.get();
return nullptr;
}
void Brains::loadPictures()
{
for (auto &brain : m_items)
brain->loadPicture();
}
} // namespace Cevo