Files
c-evo-evo/game/Direct.cpp
T
2026-11-07 12:47:19 +01:00

329 lines
12 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 "Direct.h"
#include "Global.h"
#include "Messg.h"
#include "NoTerm.h"
#include "Start.h"
#include "localplayer/LocalPlayer.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "Cevo/Map.h"
#include "Cevo/Music.h"
#include <QCoreApplication>
#include <QFileInfo>
#include <QMessageBox>
#include <QPainter>
#include <QTimer>
#include <QtGlobal>
#include <iostream>
namespace Cevo {
namespace {
// Shortens text until it fits into a dialog of the given width (with margins).
QString fitToWidth(QString text, int dialogWidth)
{
Dpi::Canvas canvas;
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Normal)]);
while (!text.isEmpty() && biColorTextWidth(canvas, text) + 64 > dialogWidth)
text.chop(1);
return text;
}
} // namespace
DirectDlg::DirectDlg(QWidget *parent) : DrawDlg(parent)
{
setWindowTitle(QStringLiteral("C-evo"));
GameServer::init([this](GameServer::NotifyId id, int index) { dlgNotify(id, index); });
GameServer::brainNoTerm->module.setClient(NoTerm::client);
GameServer::brainNoTerm->lookupName = QStringLiteral("AIT");
GameServer::brainSuperVirtual->module.setClient(nullptr);
GameServer::brainSuperVirtual->lookupName = QStringLiteral("SUPER");
GameServer::brainTerm->module.setClient(LocalPlayer::client);
GameServer::brainTerm->lookupName = QStringLiteral("HUMAN");
if (GameServer::networkEnabled) {
// TODO: NetworkServer.pas/NetworkClient.pas (their Client entry points) aren't ported.
GameServer::brainNetworkServer->lookupName = QStringLiteral("NETWORK_SERVER");
GameServer::brainNetworkClient->lookupName = QStringLiteral("NETWORK_CLIENT");
}
GameServer::brainRandom->lookupName = QStringLiteral("RANDOMAI");
}
void DirectDlg::setStartDlg(StartDlg *dlg)
{
m_startDlg = dlg;
m_background = dlg ? dlg->background() : nullptr;
}
void DirectDlg::showEvent(QShowEvent *event)
{
DrawDlg::showEvent(event);
if (!m_gone) {
m_gone = true;
QTimer::singleShot(0, this, &DirectDlg::go);
}
}
void DirectDlg::closeEvent(QCloseEvent *event)
{
GameServer::done();
DrawDlg::closeEvent(event);
}
void DirectDlg::go()
{
hide();
if (GameServer::brains.count() == 3) {
QMessageBox::information(this, CevoTitle, phrases.lookup(QStringLiteral("NOAI")));
close();
return;
}
m_quick = false;
const QStringList args = QCoreApplication::arguments();
if (args.size() > 1) {
if (args.contains(QStringLiteral("-h")) || args.contains(QStringLiteral("--help"))) {
QString fullTitle = QString::fromLatin1(CevoTitle);
if (*CevoSubTitle)
fullTitle += QStringLiteral(": ") + QString::fromLatin1(CevoSubTitle);
std::cout << fullTitle.toStdString() << ' ' << CevoVersionMajor << '.' << CevoVersionMinor << '.'
<< CevoVersionBugFix << '\n';
std::cout << "Usage: c-evo [options] <book/map file>\n";
std::cout << " -m --manual Show game manual.\n";
std::cout << " -d --data-dir <dir> Configure user data directory.\n";
std::cout << " -l --lang <language> Set game interface language.\n";
std::cout << " -v --version Show application version.\n";
std::cout << " -h --help Show this help.\n";
close();
return;
}
if (args.contains(QStringLiteral("-v")) || args.contains(QStringLiteral("--version"))) {
std::cout << CevoVersionMajor << '.' << CevoVersionMinor << '.' << CevoVersionBugFix << '\n';
close();
return;
}
if (args.contains(QStringLiteral("-m")) || args.contains(QStringLiteral("--manual"))) {
m_quick = true;
GameServer::directHelp(cHelpOnly);
close();
} else if (QFileInfo::exists(args.at(1))) {
const QString fileName = args.at(1);
if (QFileInfo(fileName).suffix().prepend('.') == QString::fromLatin1(CevoExt)) {
m_quick = true;
if (!GameServer::loadGame(fileName, fileName, -1, false)) {
simpleMessage(phrases.lookup(QStringLiteral("LOADERR")));
close();
}
} else if (QFileInfo(fileName).suffix().prepend('.') == QString::fromLatin1(CevoMapExt)) {
m_quick = true;
GameServer::editMap(fileName, fileName, lxmax, lymax, 30);
} else {
simpleMessage(phrases.lookup(QStringLiteral("LOADERR")));
close();
}
}
}
if (!m_quick) {
// TODO: Start.pas isn't ported yet - nothing is set via
// setStartDlg()/setBackground() so far, so there's currently
// nothing to show here once m_quick is false.
if (m_background)
m_background->show();
if (m_startDlg)
m_startDlg->show();
}
}
void DirectDlg::dlgNotify(GameServer::NotifyId id, int index)
{
using NotifyId = GameServer::NotifyId;
switch (id) {
case NotifyId::InitLocalHuman:
MainTexture->setAge(-1);
m_state = -1;
m_info = phrases.lookup(QStringLiteral("BUSY_MODLH"));
show();
update();
break;
case NotifyId::InitModule:
if (isVisible())
setInfo(fitToWidth(formatPhrase(QStringLiteral("BUSY_MOD"), { GameServer::brains[index].name() }), width()));
break;
case NotifyId::CreateWorld:
if (isVisible())
setInfo(phrases.lookup(QStringLiteral("BUSY_START")));
break;
case NotifyId::InitPlayers:
if (isVisible())
setInfo(phrases.lookup(QStringLiteral("BUSY_INIT")));
break;
case NotifyId::DeactivationMissing:
simpleMessage(formatPhrase(QStringLiteral("MISSDEACT"), { QString::number(index) }));
break;
case NotifyId::SetAiName:
LocalPlayer::setAIName(index, GameServer::notifyMessage);
break;
case NotifyId::Exception:
QTimer::singleShot(0, this, [this, index]() {
QMessageBox::warning(this, CevoTitle,
formatPhrase(QStringLiteral("AIEXCEPTION"), { GameServer::brains[index].name() }));
});
break;
case NotifyId::LoadBegin:
m_info = phrases.lookup(QStringLiteral("BUSY_LOAD"));
setState(0);
break;
case NotifyId::LoadState:
setState(index);
break;
case NotifyId::DllError:
simpleMessage(formatPhrase(QStringLiteral("DLLERROR"), { GameServer::brains[index].fileName }));
break;
case NotifyId::AiError:
simpleMessage(formatPhrase(QStringLiteral("AIERROR"), { GameServer::notifyMessage }));
break;
case NotifyId::ClientError:
simpleMessage(formatPhrase(QStringLiteral("CLIENTERROR"), { GameServer::brains[index].fileName }));
break;
case NotifyId::EndInfo:
hide();
if (m_background)
m_background->update();
break;
case NotifyId::LoadError:
// Empty in the original too (the whole body is commented out there).
break;
case NotifyId::StartDone:
if (!m_quick && m_startDlg)
m_startDlg->hide();
if (m_background)
m_background->update();
if (musicEnabled) {
musicPlayer.loadPlaylistFromDir(getMusicDir());
musicPlayer.randomizePlaylist();
if (!musicPlayer.playlist.isEmpty())
musicPlayer.play();
}
break;
case NotifyId::StartGo:
case NotifyId::StartGoRefresh:
case NotifyId::StartGoRefreshMaps:
if (m_quick) {
close();
} else if (m_startDlg) {
if (id == NotifyId::StartGoRefresh)
m_startDlg->updateFormerGames();
else if (id == NotifyId::StartGoRefreshMaps)
m_startDlg->updateMaps(false);
m_startDlg->show();
}
break;
case NotifyId::ChangeClient:
QTimer::singleShot(0, this, []() { GameServer::changeClient(); });
break;
case NotifyId::NextPlayer:
QTimer::singleShot(0, this, []() { GameServer::nextPlayer(); });
break;
case NotifyId::DeinitModule:
m_info = fitToWidth(
phraseToQtFormat(phrases2.lookup(QStringLiteral("BUSY_DEINIT"))).arg(GameServer::brains[index].name()),
width());
MainTexture->setAge(-1);
m_state = -1;
show();
update();
break;
case NotifyId::BackOn:
if (m_background) {
m_background->show();
m_background->update();
}
break;
case NotifyId::BackOff:
if (m_background)
m_background->close();
break;
}
}
void DirectDlg::paintEvent(QPaintEvent *event)
{
DrawDlg::paintEvent(event);
QPainter painter(this);
if (!painter.isActive())
return;
Dpi::Canvas canvas;
canvas.bind(&painter);
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Normal)]);
canvas.brush().setStyle(Qt::NoBrush);
paintBackground(canvas, 3, 3, width() - 6, height() - 6, width(), height());
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);
if (m_state >= 0)
risedTextOut(canvas, (width() - biColorTextWidth(canvas, m_info)) / 2, 16, m_info);
else
risedTextOut(canvas, (width() - biColorTextWidth(canvas, m_info)) / 2,
(height() - canvas.font().getTextHeight(m_info)) / 2, m_info);
if (m_state >= 0)
paintProgressBar(canvas, 3, width() / 2 - 64, 40, m_state, 0, 128, *MainTexture);
canvas.unbind();
}
void DirectDlg::setInfo(const QString &text)
{
m_info = text;
update();
}
void DirectDlg::setState(int value)
{
// Disclosed simplification: the original incrementally redraws just the
// progress bar onto the already-visible window here, outside of a paint
// event. Qt only supports painting a widget from within paintEvent (or
// onto an offscreen pixmap/image), so this always just schedules a full
// repaint instead - paintEvent() redraws the whole dialog including the
// progress bar at the new state.
m_state = value;
update();
}
} // namespace Cevo