small utilities

This commit is contained in:
Geoffray Levasseur committed 2026-10-27 14:57:47 +01:00
1 parent 88be66311d
commit b552ba0293
9 files changed
+667 -1

No files matched your search

+207
View File
@@ -0,0 +1,207 @@
// 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 "Brain.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;
// TODO: needs Phrases[] (StringTables.pas, not ported yet) - fall back
// to the raw lookup key until localization is ported.
return 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();
}
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
+106
View File
@@ -0,0 +1,106 @@
// 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/AiModule.h"
#include "Dpi/DpiGraphics.h"
#include <QString>
#include <memory>
#include <vector>
namespace Cevo {
enum class BrainType { NoTerm, SuperVirtual, Term, Random, AI, NetworkServer, NetworkClient };
// module flags
constexpr int fMultiple = 0x10000000;
constexpr int fDotNet = 0x20000000;
constexpr int fUsed = 0x40000000;
// Equivalent of Brain.pas's TBrain: describes one "brain" (a loadable AI
// module, or one of the special player kinds - human terminal, random bot,
// network client/server, ...) available to control a player slot.
class Brain {
public:
Brain();
QString name() const;
void setName(const QString &value) { m_name = value; }
// Equivalent of LoadPicture(): tries fileName/fileName.png under the AI
// directory, falling back to a plain placeholder (filename drawn over a
// solid background) if that doesn't exist.
void loadPicture();
// Equivalent of LoadFromFile(): parses a ".ai" key/value spec file.
void loadFromFile(const QString &aiFileName);
QString fileName;
QString dllName;
QString lookupName;
QString credits; // filename and full name
// Equivalent of Pascal's `hm`/`Client` (module handle + exported entry
// point) - ai/'s AiModule already wraps LoadLibrary/GetProcAddress and
// the TClientCall invocation in one object, so it replaces both fields.
AiModule module;
int flags = 0;
int serverVersion = 0;
int dataVersion = 0;
int dataSize = 0;
bool initialized = false;
BrainType kind = BrainType::NoTerm;
Dpi::Bitmap picture;
bool beginner = false;
private:
QString m_name;
};
// Equivalent of Brain.pas's TBrains (a TObjectList<TBrain> with a few
// convenience lookups). Owns its Brain instances; unlike the original's
// TObjectList<T>, getByKind() returns non-owning raw pointers instead of
// aliasing them into a second owning list (which risked a double free).
class Brains {
public:
Brain &addNew();
int count() const { return static_cast<int>(m_items.size()); }
Brain &at(int index) const { return *m_items[static_cast<std::size_t>(index)]; }
Brain &operator[](int index) const { return at(index); }
void clear() { m_items.clear(); }
int kindCount(BrainType kind) const;
std::vector<Brain *> getByKind(BrainType kind) const;
Brain *beginner() const;
void loadPictures();
private:
std::vector<std::unique_ptr<Brain>> m_items;
};
} // namespace Cevo
+5 -1
View File
@@ -51,6 +51,8 @@ set(CEVO_GAME_HEADERS
include/Cevo/Database.h
include/Cevo/IPQ.h
include/Cevo/Map.h
include/Cevo/Music.h
include/Cevo/Platform.h
include/Cevo/UnitProcessing.h
)
@@ -60,6 +62,8 @@ set(CEVO_GAME_SOURCES
src/Database.cpp
src/IPQ.cpp
src/Map.cpp
src/Music.cpp
src/Platform.cpp
src/UnitProcessing.cpp
)
@@ -69,7 +73,7 @@ target_include_directories(cevogame PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(cevogame PUBLIC Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Xml cevoai)
target_link_libraries(cevogame PUBLIC Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Xml cevoai uosmedia)
set_target_properties(cevogame PROPERTIES
CXX_STANDARD 17
+119
View File
@@ -0,0 +1,119 @@
// 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 "Messg.h"
#include "ui_Messg.h"
#include "Cevo/CevoButtonA.h"
#include <QKeyEvent>
#include <QTimer>
namespace Cevo {
MessgDlg::MessgDlg(QWidget *parent) : BaseMessgDlg(parent), ui(new Ui::MessgDlg)
{
ui->setupUi(this);
ui->button1->onClick = [this] {
setModalResult(Dpi::ModalResult::Ok);
close();
};
ui->button2->onClick = [this] {
setModalResult(Dpi::ModalResult::Ignore);
close();
};
}
MessgDlg::~MessgDlg()
{
delete ui;
}
void MessgDlg::showEvent(QShowEvent *event)
{
BaseMessgDlg::showEvent(event);
ui->button1->setVisible(true);
ui->button2->setVisible(kind != MessageKind::Ok);
ui->button1->move(ui->button2->isVisible() ? 101 : 159, ui->button1->y());
// TODO: needs Phrases[] (StringTables.pas, not ported yet) - plain
// English literals until localization is ported.
if (kind == MessageKind::YesNo) {
ui->button1->setCaption(tr("Yes"));
ui->button2->setCaption(tr("No"));
} else {
ui->button1->setCaption(tr("OK"));
ui->button2->setCaption(tr("Cancel"));
}
splitText(true);
correctHeight();
}
void MessgDlg::paintEvent(QPaintEvent *event)
{
BaseMessgDlg::paintEvent(event);
if (!openSound.isEmpty()) {
QTimer::singleShot(0, this, [this] {
// TODO: needs Sound.pas's Play()/Sounds[] item lookup (not
// ported yet, superseded by media/'s UosMedia for raw playback
// but no symbolic-sound-item table exists yet).
openSound.clear();
});
}
}
void MessgDlg::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
setModalResult(Dpi::ModalResult::Ok);
close();
} else {
BaseMessgDlg::keyPressEvent(event);
}
}
void simpleMessage(const QString &text)
{
MessgDlg dlg;
dlg.messageText = text;
dlg.kind = MessageKind::Ok;
dlg.showModal();
}
void soundMessage(const QString &text, const QString &soundItem)
{
MessgDlg dlg;
dlg.messageText = text;
dlg.openSound = soundItem;
dlg.kind = MessageKind::Ok;
dlg.showModal();
}
} // namespace Cevo
+69
View File
@@ -0,0 +1,69 @@
// 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/CevoDrawDlg.h"
#include <QString>
QT_BEGIN_NAMESPACE
namespace Ui {
class MessgDlg;
}
QT_END_NAMESPACE
namespace Cevo {
enum class MessageKind { Ok, OkCancel, YesNo, YesNoCancel, OkCancelRemove, OkHelp, Model };
// TMessgDlg equivalent: a BaseMessgDlg with an OK/Cancel (or Yes/No) button
// pair. UI built from Messg.ui (adapted from the original's Messg.lfm).
class MessgDlg : public BaseMessgDlg {
Q_OBJECT
public:
explicit MessgDlg(QWidget *parent = nullptr);
~MessgDlg() override;
MessageKind kind = MessageKind::Ok;
QString openSound;
protected:
void showEvent(QShowEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
private:
Ui::MessgDlg *ui;
};
// Equivalent of Messg.pas's SimpleMessage()/SoundMessage().
void simpleMessage(const QString &text);
void soundMessage(const QString &text, const QString &soundItem);
} // namespace Cevo
+74
View File
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<ui version="4.0">
<class>MessgDlg</class>
<widget class="QWidget" name="MessgDlg">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>418</width>
<height>134</height>
</rect>
</property>
<property name="windowTitle">
<string>C-evo</string>
</property>
<widget class="Cevo::ButtonA" name="button1">
<property name="geometry">
<rect>
<x>101</x>
<y>104</y>
<width>100</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonA" name="button2">
<property name="geometry">
<rect>
<x>217</x>
<y>104</y>
<width>100</width>
<height>25</height>
</rect>
</property>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>Cevo::ButtonA</class>
<extends>QWidget</extends>
<header>Cevo/CevoButtonA.h</header>
</customwidget>
</customwidgets>
<resources/>
</ui>
+42
View File
@@ -0,0 +1,42 @@
// 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
namespace Cevo {
// Equivalent of Platform.pas's NowPrecise(): a monotonic clock in seconds,
// for elapsed-time measurements only (every caller just subtracts two
// readings). The original branched on QueryPerformanceCounter (Windows) vs.
// gettimeofday (Unix) - std::chrono::steady_clock replaces both uniformly.
// (The original's unused NowPreciseLock critical section - declared but
// never actually locked, Acquire/Release were commented out - is dropped;
// std::chrono::steady_clock::now() has no shared mutable state to guard.)
double nowPrecise();
} // namespace Cevo
+40
View File
@@ -0,0 +1,40 @@
// 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 "Cevo/Platform.h"
#include <chrono>
namespace Cevo {
double nowPrecise()
{
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
} // namespace Cevo