diff --git a/CMakeLists.txt b/CMakeLists.txt index 6063c26..ae7f44b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,6 +94,8 @@ set(PROJECT_SOURCES game/Back.h game/Brain.cpp game/Brain.h + game/Direct.cpp + game/Direct.h game/GameServer.cpp game/GameServer.h game/GameServerInternal.h diff --git a/game/Direct.cpp b/game/Direct.cpp new file mode 100644 index 0000000..ea06a0f --- /dev/null +++ b/game/Direct.cpp @@ -0,0 +1,298 @@ +// 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 "Cevo/CevoScreenTools.h" +#include "Cevo/Map.h" +#include "Cevo/Music.h" + +#include +#include +#include +#include +#include +#include + +#include + +namespace Cevo { + +DirectDlg::DirectDlg(QWidget *parent) : DrawDlg(parent) +{ + setWindowTitle(QStringLiteral("C-evo")); + + GameServer::init([this](GameServer::NotifyId id, int index) { dlgNotify(id, index); }); + // TODO: these special brains' in-process Client entry points need + // Start.pas/LocalPlayer.pas/NoTerm.pas/NetworkServer.pas/ + // NetworkClient.pas, none of which are ported yet - only the + // (purely cosmetic) lookup name is wired up for now. + GameServer::brainNoTerm->lookupName = QStringLiteral("AIT"); + GameServer::brainSuperVirtual->lookupName = QStringLiteral("SUPER"); + GameServer::brainTerm->lookupName = QStringLiteral("HUMAN"); + if (GameServer::networkEnabled) { + GameServer::brainNetworkServer->lookupName = QStringLiteral("NETWORK_SERVER"); + GameServer::brainNetworkClient->lookupName = QStringLiteral("NETWORK_CLIENT"); + } + GameServer::brainRandom->lookupName = QStringLiteral("RANDOMAI"); +} + +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, tr("No AI libraries found.")); + 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] \n"; + std::cout << " -m --manual Show game manual.\n"; + std::cout << " -d --data-dir Configure user data directory.\n"; + std::cout << " -l --lang 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(tr("Could not load the file.")); + close(); + } + } else if (QFileInfo(fileName).suffix().prepend('.') == QString::fromLatin1(CevoMapExt)) { + m_quick = true; + GameServer::editMap(fileName, fileName, lxmax, lymax, 30); + } else { + simpleMessage(tr("Could not load the file.")); + 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 = tr("Starting local human player..."); + show(); + update(); + break; + case NotifyId::InitModule: + if (isVisible()) + setInfo(tr("Starting %1...").arg(GameServer::brains[index].name())); + break; + case NotifyId::CreateWorld: + if (isVisible()) + setInfo(tr("Creating world...")); + break; + case NotifyId::InitPlayers: + if (isVisible()) + setInfo(tr("Initializing players...")); + break; + case NotifyId::DeactivationMissing: + simpleMessage(tr("Player %1 did not hand over control.").arg(index)); + break; + case NotifyId::SetAiName: + // TODO: LocalPlayer.pas (SetAIName) isn't ported yet. + break; + case NotifyId::Exception: + QTimer::singleShot(0, this, [this, index]() { + QMessageBox::warning(this, CevoTitle, + tr("AI module %1 raised an exception.").arg(GameServer::brains[index].name())); + }); + break; + case NotifyId::LoadBegin: + m_info = tr("Loading..."); + setState(0); + break; + case NotifyId::LoadState: + setState(index); + break; + case NotifyId::DllError: + simpleMessage(tr("Could not load AI module %1.").arg(GameServer::brains[index].fileName)); + break; + case NotifyId::AiError: + simpleMessage(tr("AI module error: %1").arg(GameServer::notifyMessage)); + break; + case NotifyId::ClientError: + simpleMessage(tr("Client error in AI module %1.").arg(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 { + // TODO: StartDlg::updateFormerGames()/updateMaps() not ported. + if (m_startDlg) + 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 = tr("Unloading %1...").arg(GameServer::brains[index].name()); + 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); + Dpi::Canvas canvas; + canvas.bind(&painter); + canvas.font().assign(UniFont[static_cast(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 diff --git a/game/Direct.h b/game/Direct.h new file mode 100644 index 0000000..3a90c7f --- /dev/null +++ b/game/Direct.h @@ -0,0 +1,82 @@ +// 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. + +#pragma once + +#include "Cevo/CevoDrawDlg.h" +#include "GameServer.h" + +#include + +namespace Cevo { + +// Equivalent of Direct.pas's TDirectDlg: the small "busy/progress" splash +// dialog shown while the game engine starts up, loads a save/map file, or +// hands control over between clients - and the application's actual entry +// point (OnGo parses the command line and kicks everything off). Like +// Back.pas, the .lfm has no child controls, so no .ui file is used here. +// +// Start.pas/LocalPlayer.pas/NoTerm.pas/NetworkServer.pas/NetworkClient.pas +// are not ported - every place the original reaches into StartDlg or +// assigns a special brain's in-process Client function pointer is a +// disclosed, clearly-marked gap (see the .cpp). setStartDlg()/ +// setBackground() let the eventual Start.pas port plug itself in, the same +// decoupling already used by Back.pas's setAnchor(). +class DirectDlg : public DrawDlg { + Q_OBJECT +public: + explicit DirectDlg(QWidget *parent = nullptr); + + // Equivalent of Pascal's free function `Notify` (forwards to + // DlgNotify) - pass this to GameServer::init(). + void dlgNotify(GameServer::NotifyId id, int index = 0); + + // Not-yet-ported Start.pas decoupling (see class comment). + void setStartDlg(QWidget *dlg) { m_startDlg = dlg; } + void setBackground(QWidget *background) { m_background = background; } + +protected: + void showEvent(QShowEvent *event) override; + void closeEvent(QCloseEvent *event) override; + void paintEvent(QPaintEvent *event) override; + +private: + void go(); + void setInfo(const QString &text); + void setState(int value); + + QWidget *m_startDlg = nullptr; + QWidget *m_background = nullptr; + + QString m_info; + int m_state = -1; + bool m_gone = false; + bool m_quick = false; +}; + +} // namespace Cevo diff --git a/game/Global.cpp b/game/Global.cpp index ef97cd8..e60d175 100644 --- a/game/Global.cpp +++ b/game/Global.cpp @@ -31,6 +31,7 @@ namespace Cevo { bool fullScreen = false; +bool musicEnabled = true; QString getGraphicsDir() { @@ -41,4 +42,13 @@ QString getGraphicsDir() #endif } +QString getMusicDir() +{ +#ifdef CEVO_RESOURCE_DIR + return QStringLiteral(CEVO_RESOURCE_DIR "/Music"); +#else + return QString(); +#endif +} + } // namespace Cevo diff --git a/game/Global.h b/game/Global.h index f0c585a..a58a6d2 100644 --- a/game/Global.h +++ b/game/Global.h @@ -51,11 +51,19 @@ constexpr int CevoVersion = ((CevoVersionMajor & 0xFF) << 16) | ((CevoVersionMin // by Back.pas/Start.pas to decide fullscreen vs. windowed layout). extern bool fullScreen; +// Equivalent of a user-settings `MusicEnabled` flag (Start.pas/Direct.pas - +// neither ported in full - toggle background music playback on/off). +extern bool musicEnabled; + // Minimal equivalent of Directories.pas's GetGraphicsDir() (that whole unit // isn't ported - this is just the one helper needed so far). Resolves to // CEVO_RESOURCE_DIR/Graphics (see the top-level CMakeLists.txt). QString getGraphicsDir(); +// Same minimal-port rationale as getGraphicsDir(), for Directories.pas's +// GetMusicDir(). +QString getMusicDir(); + // ScreenTools.pas's PngExt/JpgExt constants. constexpr const char *PngExt = ".png"; constexpr const char *JpgExt = ".jpg";