multiple intricate implementations

This commit is contained in:
Geoffray Levasseur committed 2026-11-04 11:41:01 +01:00
1 parent 7aa4ea4425
commit 77690cbd96
109 files changed
+2045 -56

No files matched your search

+382
View File
@@ -0,0 +1,382 @@
// 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 "NoTerm.h"
#include "ui_NoTerm.h"
#include "Cevo/CevoButtonB.h"
#include "Cevo/CevoGraphicSet.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "GameServer.h"
#include "Global.h"
#include "Messg.h"
#include "localplayer/Term.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QKeyEvent>
#include <QPainter>
namespace Cevo {
namespace NoTerm {
namespace {
constexpr double UpdateInterval = 0.1; // seconds
constexpr double ShowActiveThreshold = 0.05; // seconds
constexpr int nPlOffered = 9;
constexpr int x0Brain = 109 + 48 + 23;
constexpr int y0Brain = 124 + 48 + 7 + 16;
constexpr int dxBrain = 128;
constexpr int dyBrain = 128;
constexpr int xBrain[nPlOffered] = { x0Brain, x0Brain, x0Brain + dxBrain, x0Brain + dxBrain, x0Brain + dxBrain,
x0Brain, x0Brain - dxBrain, x0Brain - dxBrain, x0Brain - dxBrain };
constexpr int yBrain[nPlOffered] = { y0Brain, y0Brain - dyBrain, y0Brain - dyBrain, y0Brain, y0Brain + dyBrain,
y0Brain + dyBrain, y0Brain + dyBrain, y0Brain, y0Brain - dyBrain };
constexpr int xActive[nPlOffered] = { 0, 0, 36, 51, 36, 0, -36, -51, -36 };
constexpr int yActive[nPlOffered] = { 0, -51, -36, 0, 36, 51, 36, 0, -36 };
constexpr int yYear = 287 + 138;
double nowSeconds()
{
return static_cast<double>(QDateTime::currentMSecsSinceEpoch()) / 1000.0;
}
} // namespace
NoTermDlg::NoTermDlg(QWidget *parent) : DrawDlg(parent), ui(new Ui::NoTermDlg)
{
ui->setupUi(this);
const QRect screen = Dpi::Screen::primaryMonitor().boundsRect();
move(screen.left() + screen.width() - width() - 8, 8);
setWindowTitle(phrases.lookup(QStringLiteral("AIT")));
titleHeight = 36;
initButtons();
ui->goBtn->onClick = [this] { goClicked(); };
ui->quitBtn->onClick = [this] { quitClicked(); };
}
NoTermDlg::~NoTermDlg()
{
delete ui;
}
LogDlg &NoTermDlg::logDlg()
{
if (!m_logDlg)
m_logDlg = std::make_unique<LogDlg>();
return *m_logDlg;
}
void NoTermDlg::newStat()
{
m_round = 0;
m_winStat.fill(0);
m_extStat.fill(0);
m_aloneStat.fill(0);
m_timeStat.fill(0);
m_totalStatTime = 0;
m_mode = RunMode::Stop;
}
void NoTermDlg::endPlaying()
{
newStat();
int endCommand = sResign;
if (m_game.RO[m_me]->Turn > 0) {
MessgDlg dlg;
dlg.messageText = phrases.lookup(QStringLiteral("ENDTOUR"));
dlg.kind = MessageKind::YesNo;
dlg.showModal();
endCommand = dlg.modalResult() == Dpi::ModalResult::Ignore ? sResign : sBreak;
}
m_server(endCommand, m_me, 0, nullptr);
}
void NoTermDlg::showActive(int p, bool active)
{
if (p >= nPlOffered)
return;
m_shownActive[static_cast<std::size_t>(p)] = active;
update(x0Brain + 28 + xActive[p], y0Brain + 28 + yActive[p], 8, 8);
}
void NoTermDlg::paintActive(QPainter &painter, int p, bool active)
{
if (p >= nPlOffered)
return;
Dpi::Canvas canvas;
canvas.bind(&painter);
sprite(canvas, *HGrSystem, x0Brain + 28 + xActive[p], y0Brain + 28 + yActive[p], 8, 8, 81 + 9 * (active ? 1 : 0),
16);
canvas.unbind();
}
void NoTermDlg::showYear()
{
m_yearText = formatPhrase(QStringLiteral("AIT_ROUND"), { QString::number(m_round) }) + QLatin1Char(' ') +
Term::turnToString(m_game.RO[m_me]->Turn);
update(64, yYear, 192, 20);
}
void NoTermDlg::goClicked()
{
if (m_mode == RunMode::Running) {
m_mode = RunMode::Stop;
} else if (m_mode == RunMode::Stopped) {
m_mode = RunMode::Running;
ui->goBtn->setButtonIndex(23);
ui->goBtn->repaint();
m_server(sTurn, m_me, 0, nullptr);
}
}
void NoTermDlg::quitClicked()
{
if (m_mode == RunMode::Stopped)
endPlaying();
else
m_mode = RunMode::Quit;
}
void NoTermDlg::client(int command, int player, void *data)
{
switch (command) {
case cDebugMessage:
logDlg().add(player, m_game.RO[0] ? m_game.RO[0]->Turn : 0, QString::fromLocal8Bit(static_cast<const char *>(data)));
break;
case cInitModule: {
auto *initData = static_cast<TInitModuleData *>(data);
m_server = initData->Server;
initData->Flags = aiThreaded;
m_shade = QImage(64, 64, QImage::Format_ARGB32);
for (int x = 0; x < 64; ++x) {
for (int y = 0; y < 64; ++y)
m_shade.setPixel(x, y, ((x + y) & 1) ? qRgba(0, 0, 0, 255) : qRgba(0, 0, 0, 0));
}
newStat();
break;
}
case cReleaseModule:
m_shade = QImage();
break;
case cNewGame:
case cLoadGame:
++m_round;
m_game = *static_cast<TNewGameData *>(data);
m_shownActive.fill(false);
if (m_mode == RunMode::Running)
repaint();
else
show();
logDlg().setSlotItemVisible(false);
logDlg().host = nullptr;
m_toldAlive = m_game.RO[m_me]->Alive;
m_active = -1;
m_disallowShowActive.fill(false);
m_lastShowTurnChange = 0;
m_lastNewTurn = 0;
m_turnTime = 1.0;
break;
case cBreakGame:
logDlg().clear();
if (m_mode != RunMode::Running) {
if (logDlg().isVisible())
logDlg().close();
close();
}
break;
case cTurn:
case cResume:
case cContinue: {
m_me = player;
if (m_active >= 0) {
showActive(m_active, false);
m_active = -1;
} // should not happen
const double now = nowSeconds();
if (now - m_lastShowYearTime >= UpdateInterval) {
showYear();
m_lastShowYearTime = now;
}
m_turnTime = now - m_lastNewTurn;
m_lastNewTurn = now;
const TPlayerContext &ro = *m_game.RO[m_me];
if (ro.Alive != m_toldAlive) {
for (int p = 1; p < nPlOffered; ++p) {
if ((1 << p) & (ro.Alive ^ m_toldAlive))
update(xBrain[p], yBrain[p] - 16, 64, 64);
}
m_toldAlive = ro.Alive;
}
QCoreApplication::processEvents();
if (m_mode == RunMode::Quit) {
endPlaying();
} else if (ro.Happened & phGameEnd) { // game ended, update statistics
for (int p = 1; p < nPlOffered; ++p) {
if (!GameServer::playersBrain[static_cast<std::size_t>(p)])
continue;
if (!((1 << p) & ro.Alive)) {
++m_extStat[static_cast<std::size_t>(p)]; // extinct
} else if (ro.Alive == (1 << p)) {
++m_aloneStat[static_cast<std::size_t>(p)]; // only player alive
} else { // alive but not alone -- check colony ship
bool shipComplete = true;
for (int i = 0; i < nShipPart; ++i) {
if (ro.Ship[p].Parts[i] < ShipNeed[i]) {
shipComplete = false;
break;
}
}
if (shipComplete)
++m_winStat[static_cast<std::size_t>(p)];
}
}
if (m_mode == RunMode::Running)
m_server(sNextRound, m_me, 0, nullptr);
} else if (m_mode == RunMode::Running) {
m_server(sTurn, m_me, 0, nullptr);
}
if (m_mode == RunMode::Stop) {
ui->goBtn->setButtonIndex(22);
m_mode = RunMode::Stopped;
}
break;
}
case cShowTurnChange: {
const double now = nowSeconds();
if (m_active >= 0) {
const auto active = static_cast<std::size_t>(m_active);
const double activeDuration = now - m_lastShowTurnChange;
m_timeStat[active] += activeDuration;
m_totalStatTime += activeDuration;
if (!m_disallowShowActive[active])
showActive(m_active, false);
m_disallowShowActive[active] = activeDuration < m_turnTime * 0.25 && activeDuration < ShowActiveThreshold;
}
m_lastShowTurnChange = now;
m_active = *static_cast<int *>(data);
if (m_active >= 0 && !m_disallowShowActive[static_cast<std::size_t>(m_active)])
showActive(m_active, true);
break;
}
}
}
void NoTermDlg::paintEvent(QPaintEvent *)
{
QPainter painter(this);
Dpi::Canvas canvas;
canvas.bind(&painter);
canvas.brush().setStyle(Qt::NoBrush);
fill(canvas, 3, 3, width() - 6, height() - 6, 0, 0);
frame(canvas, 0, 0, width() - 1, height() - 1, Qt::black, Qt::black);
frame(canvas, 1, 1, width() - 2, height() - 2, MainTexture->colorBevelLight, MainTexture->colorBevelShade);
frame(canvas, 2, 2, width() - 3, height() - 3, MainTexture->colorBevelLight, MainTexture->colorBevelShade);
corner(canvas, 1, 1, 0, *MainTexture);
corner(canvas, width() - 9, 1, 1, *MainTexture);
corner(canvas, 1, height() - 9, 2, *MainTexture);
corner(canvas, width() - 9, height() - 9, 3, *MainTexture);
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Caption)]);
risedTextOut(canvas, (width() - biColorTextWidth(canvas, windowTitle())) / 2, 7, windowTitle());
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Small)]);
for (int i = 1; i < nPlOffered; ++i) {
Brain *brain = GameServer::playersBrain[static_cast<std::size_t>(i)];
if (!brain)
continue;
const int x = xBrain[i];
const int y = yBrain[i];
frame(canvas, x - 24, y - 8 - 16, x - 24 + 111, y - 8 - 16 + 111, MainTexture->colorBevelShade,
MainTexture->colorBevelShade);
frameImage(canvas, brain->picture, x, y - 16, 64, 64, 0, 0);
if (m_game.RO[m_me] && !((1 << i) & m_game.RO[m_me]->Alive))
painter.drawImage(x, y - 16, m_shade);
const auto stat = static_cast<std::size_t>(i);
sprite(canvas, *HGrSystem, x + 30 - 14, y + 53, 14, 14, 1, 316);
risedTextOut(canvas, x + 30 - 16 - biColorTextWidth(canvas, QString::number(m_winStat[stat])), y + 51,
QString::number(m_winStat[stat]));
sprite(canvas, *HGrSystem, x + 34, y + 53, 14, 14, 1 + 15, 316);
risedTextOut(canvas, x + 34 + 16, y + 51, QString::number(m_aloneStat[stat]));
sprite(canvas, *HGrSystem, x + 30 - 14, y + 53 + 16, 14, 14, 1 + 30, 316);
risedTextOut(canvas, x + 30 - 16 - biColorTextWidth(canvas, QString::number(m_extStat[stat])), y + 51 + 16,
QString::number(m_extStat[stat]));
sprite(canvas, *HGrSystem, x + 34, y + 53 + 16, 14, 14, 1 + 45, 316);
if (m_totalStatTime > 0) {
const int timeShare = static_cast<int>(m_timeStat[stat] / m_totalStatTime * 100 + 0.5);
risedTextOut(canvas, x + 34 + 16, y + 51 + 16, QString::number(timeShare) + QLatin1Char('%'));
}
paintActive(painter, i, m_shownActive[stat]);
}
sprite(canvas, *HGrSystem2, x0Brain + 32 - 20, y0Brain + 32 - 20, 40, 40, 115, 1);
Dpi::Bitmap state(192, 20);
state.canvas().begin();
fill(state.canvas(), 0, 0, 192, 20, 64, yYear);
state.canvas().font().assign(UniFont[static_cast<std::size_t>(FontType::Small)]);
state.canvas().brush().setStyle(Qt::NoBrush);
risedTextOut(state.canvas(), 0, 0, m_yearText);
state.canvas().end();
painter.drawImage(64, yYear, state.image());
btnFrame(canvas, ui->goBtn->geometry(), *MainTexture);
btnFrame(canvas, ui->quitBtn->geometry(), *MainTexture);
canvas.unbind();
}
void NoTermDlg::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_M && (event->modifiers() & Qt::ControlModifier)) {
if (logDlg().isVisible())
logDlg().close();
else
logDlg().show();
return;
}
DrawDlg::keyPressEvent(event);
}
void CEVO_ABI_CALL client(std::int32_t command, std::int32_t player, void *data)
{
static NoTermDlg *dlg = new NoTermDlg; // lives for the whole session, like Application.CreateForm
dlg->client(command, player, data);
}
} // namespace NoTerm
} // namespace Cevo