// 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/CevoTexture.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