1336 lines
51 KiB
C++
1336 lines
51 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.
|
|
|
|
// Start.pas, the logic part (construction, settings, popups, buttons);
|
|
// the painting is in Start.Paint.cpp.
|
|
|
|
#include "Start.h"
|
|
#include "ui_Start.h"
|
|
|
|
#include "Cevo/CevoButtonA.h"
|
|
#include "Cevo/CevoButtonB.h"
|
|
#include "Cevo/CevoButtonC.h"
|
|
#include "Cevo/CevoGraphicSet.h"
|
|
#include "Cevo/CevoListBoxEx.h"
|
|
#include "Cevo/CevoScreenTools.h"
|
|
#include "Cevo/CevoTexture.h"
|
|
#include "Cevo/Database.h"
|
|
#include "Cevo/Map.h"
|
|
#include "Cevo/Music.h"
|
|
#include "Direct.h"
|
|
#include "GameServer.h"
|
|
#include "Global.h"
|
|
#include "Inp.h"
|
|
#include "Messg.h"
|
|
#include "StartLayout.h"
|
|
#include "localplayer/KeyBindings.h"
|
|
#include "localplayer/Term.h"
|
|
|
|
#include <QAction>
|
|
#include <QActionGroup>
|
|
#include <QCoreApplication>
|
|
#include <QDesktopServices>
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QKeyEvent>
|
|
#include <QMenu>
|
|
#include <QMouseEvent>
|
|
#include <QSettings>
|
|
#include <QUrl>
|
|
|
|
#include <algorithm>
|
|
|
|
namespace Cevo {
|
|
|
|
using namespace StartLayout;
|
|
|
|
namespace {
|
|
|
|
constexpr int AllPagesTag = 0x0FFFFF00;
|
|
|
|
void setTag(QWidget *widget, quint32 tag)
|
|
{
|
|
widget->setProperty("tag", tag);
|
|
}
|
|
|
|
quint32 tagOf(const QWidget *widget)
|
|
{
|
|
return widget->property("tag").toUInt();
|
|
}
|
|
|
|
bool isPlayableBrain(const Brain *brain)
|
|
{
|
|
return brain && (brain->kind == BrainType::Term || brain->kind == BrainType::Random ||
|
|
brain->kind == BrainType::AI);
|
|
}
|
|
|
|
QString phraseN(const QString &key, int index)
|
|
{
|
|
return phrases.lookup(key, index);
|
|
}
|
|
|
|
// Pascal's AnsiCompareFileName ordering of captions (case-insensitive).
|
|
bool captionLess(const QString &a, const QString &b)
|
|
{
|
|
return QString::compare(a, b, Qt::CaseInsensitive) < 0;
|
|
}
|
|
|
|
QString incrementFileName(const QString &fileName)
|
|
{
|
|
const int i = fileName.lastIndexOf(QLatin1Char(' '));
|
|
if (i > 0) {
|
|
bool ok = false;
|
|
const int num = fileName.mid(i + 1).toInt(&ok);
|
|
if (ok)
|
|
return fileName.left(i).trimmed() + QLatin1Char(' ') + QString::number(num + 1);
|
|
}
|
|
return fileName + QStringLiteral(" 2");
|
|
}
|
|
|
|
} // namespace
|
|
|
|
StartDlg::StartDlg(DirectDlg *direct, Background *background)
|
|
: BufferedDrawDlg(background), m_direct(direct), m_background(background), ui(new Ui::StartDlg)
|
|
{
|
|
ui->setupUi(this);
|
|
setMouseTracking(true);
|
|
// A child of the background window: it is always drawn above it, and is placed relative to it.
|
|
m_background->setAnchor(this);
|
|
|
|
titleHeight = 0;
|
|
loadConfig();
|
|
loadAssets();
|
|
loadLanguages();
|
|
|
|
m_actionsOffered = { MainAction::Config, MainAction::Manual, MainAction::Credits, MainAction::Web };
|
|
if (QFileInfo::exists(getAppSharePath(QString::fromLatin1(AITemplateFileName))))
|
|
m_actionsOffered.insert(m_actionsOffered.begin() + 3, MainAction::AIDev);
|
|
|
|
m_brainDefault = nullptr;
|
|
for (int i = std::max(GameServer::brains.indexOf(GameServer::brainRandom), 0); i < GameServer::brains.count(); ++i) {
|
|
if (GameServer::brains[i].fileName == m_defaultAI)
|
|
m_brainDefault = &GameServer::brains[i];
|
|
}
|
|
if (m_brainDefault == GameServer::brainRandom && GameServer::brains.kindCount(BrainType::AI) < 2)
|
|
m_brainDefault = nullptr;
|
|
if (!m_brainDefault && GameServer::brains.kindCount(BrainType::AI) > 0) // default AI not found, use any
|
|
m_brainDefault = GameServer::brains.getByKind(BrainType::AI).front();
|
|
|
|
m_direct->centerToScreen();
|
|
updateInterface();
|
|
|
|
// Page visibility tags (Tag and (256 shl Page) <> 0).
|
|
setTag(ui->startBtn, 15104);
|
|
setTag(ui->down1Btn, 4096);
|
|
setTag(ui->up1Btn, 4096);
|
|
setTag(ui->renameBtn, 10240);
|
|
setTag(ui->deleteBtn, 10240);
|
|
setTag(ui->down2Btn, 6912);
|
|
setTag(ui->up2Btn, 6912);
|
|
setTag(ui->quitBtn, AllPagesTag);
|
|
setTag(ui->customizeBtn, 768);
|
|
setTag(ui->autoDiffUpBtn, 0);
|
|
setTag(ui->autoDiffDownBtn, 0);
|
|
setTag(ui->autoEnemyUpBtn, 0);
|
|
setTag(ui->autoEnemyDownBtn, 0);
|
|
setTag(ui->replayBtn, 2048);
|
|
setTag(ui->list, 15360);
|
|
|
|
ui->down1Btn->setButtonIndex(0);
|
|
ui->up1Btn->setButtonIndex(1);
|
|
ui->down2Btn->setButtonIndex(0);
|
|
ui->up2Btn->setButtonIndex(1);
|
|
ui->autoDiffUpBtn->setButtonIndex(1);
|
|
ui->autoDiffDownBtn->setButtonIndex(0);
|
|
ui->autoEnemyUpBtn->setButtonIndex(1);
|
|
ui->autoEnemyDownBtn->setButtonIndex(0);
|
|
ui->renameBtn->setButtonIndex(31);
|
|
ui->deleteBtn->setButtonIndex(21);
|
|
ui->replayBtn->setButtonIndex(19);
|
|
ui->quitBtn->setButtonIndex(0);
|
|
|
|
ui->quitBtn->setToolTip(phraseN(QStringLiteral("STARTCONTROLS"), 0));
|
|
ui->replayBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_REPLAY")));
|
|
|
|
for (int i = 0; i < nPlOffered; ++i) {
|
|
PlayerSlot &slot = m_playerSlots[static_cast<std::size_t>(i)];
|
|
slot.diffUpBtn = new ButtonC(this);
|
|
slot.diffUpBtn->setGeometry(xBrain[i] - 18, yBrain[i] + 39, 12, 12);
|
|
slot.diffUpBtn->setButtonIndex(1);
|
|
slot.diffUpBtn->onClick = [this, btn = slot.diffUpBtn] { diffClicked(btn); };
|
|
slot.diffDownBtn = new ButtonC(this);
|
|
slot.diffDownBtn->setGeometry(xBrain[i] - 18, yBrain[i] + 51, 12, 12);
|
|
slot.diffDownBtn->setButtonIndex(0);
|
|
slot.diffDownBtn->onClick = [this, btn = slot.diffDownBtn] { diffClicked(btn); };
|
|
setTag(slot.diffUpBtn, 0);
|
|
setTag(slot.diffDownBtn, 0);
|
|
}
|
|
for (int i = 6; i < 9; ++i) {
|
|
PlayerSlot &slot = m_playerSlots[static_cast<std::size_t>(i)];
|
|
slot.multiBtn = new ButtonC(this);
|
|
slot.multiBtn->setGeometry(xBrain[i] - 18, yBrain[i], 12, 12);
|
|
slot.multiBtn->onClick = [this, btn = slot.multiBtn] { multiClicked(btn); };
|
|
setTag(slot.multiBtn, 0);
|
|
slot.offerMultiple = true;
|
|
}
|
|
|
|
{
|
|
Dpi::Canvas measure;
|
|
measure.font().assign(UniFont[static_cast<std::size_t>(FontType::Normal)]);
|
|
const int x = biColorTextWidth(measure, phraseN(QStringLiteral("STARTCONTROLS"), 7)) / 2;
|
|
ui->customizeBtn->move(x0Brain + 32 - 16 - x, ui->customizeBtn->y());
|
|
}
|
|
ui->customizeBtn->setButtonIndex(m_autoDiff < 0 ? 3 : 2);
|
|
|
|
const auto blitBrainPicture = [](Brain *brain, const QString &itemName) {
|
|
GraphicSetItem *item = HGrSystem2 ? HGrSystem2->searchByName(itemName) : nullptr;
|
|
if (brain && item)
|
|
bitBltBitmap(brain->picture, 0, 0, 64, 64, HGrSystem2->data, item->left, item->top);
|
|
};
|
|
blitBrainPicture(GameServer::brainNoTerm, QStringLiteral("BrainNoTerm"));
|
|
blitBrainPicture(GameServer::brainSuperVirtual, QStringLiteral("BrainSuperVirtual"));
|
|
blitBrainPicture(GameServer::brainTerm, QStringLiteral("BrainTerm"));
|
|
blitBrainPicture(GameServer::brainRandom, QStringLiteral("BrainRandom"));
|
|
loadAiBrainsPictures();
|
|
|
|
m_emptyPicture.setSize(64, 64);
|
|
m_emptyPicture.image().fill(Qt::black);
|
|
|
|
initButtons();
|
|
|
|
GameServer::playersBrain[0] = GameServer::brainTerm;
|
|
m_slotAvailable = -1;
|
|
m_tab = Tab::NewGame;
|
|
m_diff0 = 2;
|
|
m_turnValid = false;
|
|
m_tracking = false;
|
|
|
|
m_formerGames.sharedDir = getSavedDir(true);
|
|
m_formerGames.userHomeDir = getSavedDir();
|
|
m_formerGames.fileFilter = QLatin1Char('*') + QString::fromLatin1(CevoExt);
|
|
updateFormerGames();
|
|
|
|
m_mapFileName.clear();
|
|
m_maps.sharedDir = getMapsDir(true);
|
|
m_maps.userHomeDir = getMapsDir();
|
|
m_maps.fileFilter = QLatin1Char('*') + QString::fromLatin1(CevoMapExt);
|
|
m_maps.insertFirstItem = phrases.lookup(QStringLiteral("RANMAP"));
|
|
m_maps.sortByName = true;
|
|
updateMaps(true);
|
|
|
|
ui->startBtn->onClick = [this] { startClicked(); };
|
|
ui->down1Btn->onClick = [this] { down1Clicked(); };
|
|
ui->up1Btn->onClick = [this] { up1Clicked(); };
|
|
ui->down2Btn->onClick = [this] { down2Clicked(); };
|
|
ui->up2Btn->onClick = [this] { up2Clicked(); };
|
|
ui->renameBtn->onClick = [this] { renameClicked(); };
|
|
ui->deleteBtn->onClick = [this] { deleteClicked(); };
|
|
ui->quitBtn->onClick = [this] { close(); };
|
|
ui->customizeBtn->onClick = [this] { customizeClicked(); };
|
|
ui->autoDiffUpBtn->onClick = [this] { autoDiffUpClicked(); };
|
|
ui->autoDiffDownBtn->onClick = [this] { autoDiffDownClicked(); };
|
|
ui->autoEnemyUpBtn->onClick = [this] { autoEnemyUpClicked(); };
|
|
ui->autoEnemyDownBtn->onClick = [this] { autoEnemyDownClicked(); };
|
|
ui->replayBtn->onClick = [this] { replayClicked(); };
|
|
|
|
ui->list->installEventFilter(this);
|
|
connect(ui->list, &QListWidget::itemClicked, this, [this] { listClicked(); });
|
|
connect(ui->list, &QListWidget::itemDoubleClicked, this, [this] { startClicked(); });
|
|
connect(ui->list, &QListWidget::currentRowChanged, this, [this] { listSelectionChanged(); });
|
|
|
|
onShow = [this] {
|
|
MainTexture->setAge(-1);
|
|
m_emptyPicture.image().fill(Qt::black);
|
|
{
|
|
// Textured, darkened placeholder for empty player slots.
|
|
m_emptyPicture.canvas().begin();
|
|
fill(m_emptyPicture.canvas(), QRect(0, 0, 64, 64),
|
|
QPoint((MainTexture->width() - 64) / 2, (MainTexture->height() - 64) / 2));
|
|
m_emptyPicture.canvas().end();
|
|
}
|
|
darkenImage(m_emptyPicture, 28);
|
|
|
|
Database::difficulty[0] = m_diff0;
|
|
m_selectedAction = MainAction::None;
|
|
if (m_showTab == Tab::PreviousGame)
|
|
GameServer::previewMap(m_startLandMass); // avoid delay on first tab change
|
|
changeTab(m_showTab);
|
|
updateMusic();
|
|
};
|
|
onHide = [this] {
|
|
m_diff0 = Database::difficulty[0];
|
|
m_listIndex[static_cast<std::size_t>(m_tab)] = ui->list->currentRow();
|
|
m_showTab = m_tab;
|
|
const int formerIndex = m_listIndex[static_cast<std::size_t>(Tab::PreviousGame)];
|
|
m_lastGame = formerIndex >= 0 && formerIndex < m_formerGames.count() ? m_formerGames[formerIndex].nameOnly()
|
|
: QString();
|
|
const int mapIndex = m_listIndex[static_cast<std::size_t>(Tab::Map)];
|
|
m_lastMap = mapIndex >= 0 && mapIndex < m_maps.count() ? m_maps[mapIndex].nameOnly() : QString();
|
|
};
|
|
onActivate = [this] {
|
|
if (ui->list->count() > 0 && ui->list->currentItem())
|
|
ui->list->scrollToItem(ui->list->currentItem());
|
|
};
|
|
// Closing the start screen ends the application (the original's main form, DirectDlg, closes).
|
|
// DirectDlg is hidden by then and the start screen is only a child window, so closing them
|
|
// would not by itself make Qt quit.
|
|
onClose = [this] {
|
|
m_direct->close();
|
|
m_background->close();
|
|
QCoreApplication::quit();
|
|
};
|
|
}
|
|
|
|
StartDlg::~StartDlg()
|
|
{
|
|
if (musicPlayer.playing())
|
|
musicPlayer.stop();
|
|
saveConfig();
|
|
delete ui;
|
|
}
|
|
|
|
void StartDlg::smartInvalidate(int x0, int y0, int x1, int y1, bool invalidateTab0)
|
|
{
|
|
Q_UNUSED(invalidateTab0); // the whole offscreen is repainted anyway
|
|
update(QRect(x0, y0, x1 - x0, y1 - y0));
|
|
}
|
|
|
|
void StartDlg::loadConfig()
|
|
{
|
|
QSettings settings;
|
|
settings.beginGroup(QStringLiteral("AI"));
|
|
for (int i = 0; i < nPlOffered; ++i) {
|
|
const QString controlKey = QStringLiteral("Control") + QString::number(i);
|
|
const QString diffKey = QStringLiteral("Diff") + QString::number(i);
|
|
if (!settings.contains(controlKey))
|
|
settings.setValue(controlKey, i == 0 ? QStringLiteral(":StdIntf") : QStringLiteral("StdAI"));
|
|
if (!settings.contains(diffKey))
|
|
settings.setValue(diffKey, 2);
|
|
}
|
|
settings.endGroup();
|
|
|
|
Cevo::loadConfig();
|
|
m_worldSize = std::clamp(settings.value(QStringLiteral("WorldSize"), DefaultWorldSize).toInt(), 0, MaxWorldSize);
|
|
m_startLandMass = settings.value(QStringLiteral("LandMass"), DefaultLandMass).toInt();
|
|
m_maxTurn = settings.value(QStringLiteral("MaxTurn"), 800).toInt();
|
|
m_defaultAI = settings.value(QStringLiteral("DefaultAI"), QStringLiteral("StdAI")).toString();
|
|
m_autoEnemies = settings.value(QStringLiteral("AutoEnemies"), 8).toInt();
|
|
m_autoDiff = settings.value(QStringLiteral("AutoDiff"), 1).toInt();
|
|
m_showTab = static_cast<Tab>(std::clamp(settings.value(QStringLiteral("StartTab"), 2).toInt(), 0, 3));
|
|
m_lastGame = settings.value(QStringLiteral("LastGame")).toString();
|
|
m_lastMap = settings.value(QStringLiteral("LastMap")).toString();
|
|
GameServer::networkEnabled = settings.value(QStringLiteral("NetworkEnabled"), false).toBool();
|
|
m_multiControl = settings.value(QStringLiteral("MultiControl"), 0).toInt();
|
|
|
|
KeyBindings::keyBindings.loadFromSettings(settings, QStringLiteral("KeyBindings"));
|
|
}
|
|
|
|
void StartDlg::saveConfig()
|
|
{
|
|
Cevo::saveConfig();
|
|
QSettings settings;
|
|
settings.setValue(QStringLiteral("WorldSize"), m_worldSize);
|
|
settings.setValue(QStringLiteral("LandMass"), m_startLandMass);
|
|
settings.setValue(QStringLiteral("MultiControl"), m_multiControl);
|
|
settings.setValue(QStringLiteral("StartTab"), static_cast<int>(m_showTab));
|
|
settings.setValue(QStringLiteral("LastGame"), m_lastGame);
|
|
settings.setValue(QStringLiteral("LastMap"), m_lastMap);
|
|
settings.setValue(QStringLiteral("NetworkEnabled"), GameServer::networkEnabled);
|
|
KeyBindings::keyBindings.saveToSettings(settings, QStringLiteral("KeyBindings"));
|
|
}
|
|
|
|
void StartDlg::loadLanguages()
|
|
{
|
|
m_languages.detectAvailable();
|
|
}
|
|
|
|
void StartDlg::loadAiBrainsPictures()
|
|
{
|
|
for (Brain *brain : GameServer::brains.getByKind(BrainType::AI))
|
|
brain->loadPicture();
|
|
}
|
|
|
|
void StartDlg::updateInterface()
|
|
{
|
|
// The background window sizes itself and places this dialog.
|
|
m_background->updateInterface();
|
|
if (fullScreen) {
|
|
// Only the quit button area above the tab strip stays visible, as in the original window region.
|
|
QRegion region(0, 0, width(), height());
|
|
region -= QRegion(TabOffset + 4 * TabSize + 2, 0, width() - (TabOffset + 4 * TabSize + 2), TabHeight);
|
|
region += QRegion(ui->quitBtn->geometry());
|
|
setMask(region);
|
|
} else {
|
|
clearMask();
|
|
}
|
|
}
|
|
|
|
void StartDlg::showSettings()
|
|
{
|
|
// TODO: Settings.pas (the configuration dialog) isn't ported yet.
|
|
}
|
|
|
|
void StartDlg::updateMusic()
|
|
{
|
|
if (musicEnabled) {
|
|
musicPlayer.setVolume(musicVolume);
|
|
if (!musicPlayer.playing()) {
|
|
musicPlayer.loadPlaylistFromDir(getMusicDir() + QStringLiteral("/Start"));
|
|
musicPlayer.randomizePlaylist();
|
|
if (!musicPlayer.playlist.isEmpty())
|
|
musicPlayer.play();
|
|
}
|
|
} else if (musicPlayer.playing()) {
|
|
musicPlayer.stop();
|
|
}
|
|
}
|
|
|
|
FileList &StartDlg::currentFileList()
|
|
{
|
|
return m_tab == Tab::Map ? m_maps : m_formerGames;
|
|
}
|
|
|
|
FileItem *StartDlg::selectedFileItem()
|
|
{
|
|
const int row = ui->list->currentRow();
|
|
FileList &fileList = currentFileList();
|
|
if (row < 0 || row >= fileList.count())
|
|
return nullptr;
|
|
return &fileList[row];
|
|
}
|
|
|
|
void StartDlg::applyPageVisibility()
|
|
{
|
|
const quint32 bit = 256u << static_cast<int>(m_page);
|
|
const auto children = findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly);
|
|
for (QWidget *child : children) {
|
|
if (child->property("tag").isValid())
|
|
child->setVisible((tagOf(child) & bit) != 0);
|
|
}
|
|
}
|
|
|
|
void StartDlg::paintInfo()
|
|
{
|
|
switch (m_page) {
|
|
case Page::StartRandom:
|
|
m_miniMap.mode = MiniMap::Mode::Picture;
|
|
m_miniMap.paintRandom(3, m_startLandMass, WorldSizes[m_worldSize]);
|
|
break;
|
|
case Page::NoLoad:
|
|
m_miniMap.mode = MiniMap::Mode::None;
|
|
m_miniMap.size = WorldSizes[DefaultWorldSize];
|
|
break;
|
|
case Page::Load: {
|
|
const FileItem *item = selectedFileItem();
|
|
if (!item)
|
|
break;
|
|
m_miniMap.loadFromLogFile(item->fileName, m_lastTurn, WorldSizes[DefaultWorldSize]);
|
|
if (!m_turnValid) {
|
|
m_loadTurn = m_lastTurn;
|
|
smartInvalidate(xTurnSlider - 2, y0Mini + 61, xTurnSlider + wTurnSlider + 2, yTurnSlider + 9);
|
|
}
|
|
m_turnValid = true;
|
|
break;
|
|
}
|
|
case Page::EditRandom:
|
|
m_mapFileName.clear();
|
|
m_miniMap.mode = MiniMap::Mode::Picture;
|
|
m_miniMap.paintRandom(4, m_startLandMass, WorldSizes[m_worldSize]);
|
|
break;
|
|
case Page::StartMap:
|
|
case Page::EditMap:
|
|
if (m_page == Page::EditMap) {
|
|
const FileItem *item = selectedFileItem();
|
|
if (!item)
|
|
return;
|
|
m_mapFileName = item->fileName;
|
|
}
|
|
m_miniMap.loadFromMapFile(m_mapFileName, m_mapLandTiles, m_mapStartPositions);
|
|
if (m_page == Page::EditMap)
|
|
smartInvalidate(x0Mini - 112, y0Mini + 61, x0Mini + 112, y0Mini + 91);
|
|
break;
|
|
case Page::Main:
|
|
break;
|
|
}
|
|
smartInvalidate(x0Mini - lxmax, y0Mini - lymax / 2, x0Mini - lxmax + 2 * lxmax + 4, y0Mini - lymax / 2 + lymax + 4);
|
|
}
|
|
|
|
void StartDlg::updateFormerGames()
|
|
{
|
|
m_formerGames.load();
|
|
// Update last game with new saved game name
|
|
if (!GameServer::getLogFileName().isEmpty())
|
|
m_lastGame = QFileInfo(GameServer::getLogFileName()).completeBaseName();
|
|
|
|
const int i = m_formerGames.indexOfNameOnly(m_lastGame);
|
|
m_listIndex[static_cast<std::size_t>(Tab::PreviousGame)] = i >= 0 ? i : m_formerGames.count() - 1;
|
|
m_turnValid = false;
|
|
}
|
|
|
|
void StartDlg::updateMaps(bool selectLastMap)
|
|
{
|
|
m_maps.load();
|
|
auto &mapIndex = m_listIndex[static_cast<std::size_t>(Tab::Map)];
|
|
const int i = m_maps.indexOfNameOnly(m_lastMap);
|
|
if (selectLastMap && i > 0) { // skip random map on index 0
|
|
mapIndex = i;
|
|
m_mapFileName = m_maps[i].fileName;
|
|
} else {
|
|
mapIndex = m_maps.indexOfNameOnly(QFileInfo(m_mapFileName).completeBaseName());
|
|
}
|
|
if (mapIndex < 0)
|
|
mapIndex = 0;
|
|
m_listIndex[static_cast<std::size_t>(Tab::Main)] = mapIndex;
|
|
}
|
|
|
|
void StartDlg::unlistBackupFile(QString fileName)
|
|
{
|
|
if (!GameServer::isAutoSaveFileName(fileName))
|
|
fileName = GameServer::toAutoSaveFileName(fileName);
|
|
const QString nameOnly = QFileInfo(fileName).completeBaseName();
|
|
int i = m_formerGames.count() - 1;
|
|
while (i >= 0 && QString::compare(m_formerGames[i].nameOnly(), nameOnly, Qt::CaseInsensitive) != 0)
|
|
--i;
|
|
if (i >= 0) {
|
|
m_formerGames.removeAt(i);
|
|
if (m_listIndex[static_cast<std::size_t>(Tab::NewGame)] == i)
|
|
m_listIndex[static_cast<std::size_t>(Tab::NewGame)] = 0;
|
|
}
|
|
}
|
|
|
|
void StartDlg::changePage(Page newPage)
|
|
{
|
|
m_page = newPage;
|
|
switch (m_page) {
|
|
case Page::StartRandom:
|
|
case Page::StartMap: {
|
|
ui->startBtn->setCaption(phraseN(QStringLiteral("STARTCONTROLS"), 1));
|
|
int count;
|
|
if (m_page == Page::StartRandom) {
|
|
count = nPlOffered;
|
|
} else {
|
|
count = m_mapStartPositions;
|
|
if (count == 0) {
|
|
GameServer::playersBrain[0] = GameServer::brainSuperVirtual;
|
|
Database::difficulty[0] = 0;
|
|
}
|
|
if (GameServer::playersBrain[0]->kind == BrainType::NoTerm ||
|
|
GameServer::playersBrain[0]->kind == BrainType::SuperVirtual)
|
|
++count;
|
|
if (count > nPl)
|
|
count = nPl;
|
|
m_multiControl = count <= nPlOffered ? 0 : InitMulti[count - nPlOffered - 1];
|
|
}
|
|
if (InitAlive[count - 1] != m_slotAvailable) {
|
|
if (m_page == Page::StartRandom) { // restore AI assignment of last start
|
|
QSettings settings;
|
|
settings.beginGroup(QStringLiteral("AI"));
|
|
for (int p1 = 0; p1 < nPlOffered; ++p1) {
|
|
GameServer::playersBrain[static_cast<std::size_t>(p1)] = nullptr;
|
|
const QString control = settings.value(QStringLiteral("Control") + QString::number(p1)).toString();
|
|
Database::difficulty[static_cast<std::size_t>(p1)] =
|
|
settings.value(QStringLiteral("Diff") + QString::number(p1), 2).toInt();
|
|
if (!control.isEmpty()) {
|
|
for (int j = 0; j < GameServer::brains.count(); ++j) {
|
|
if (control == GameServer::brains[j].fileName)
|
|
GameServer::playersBrain[static_cast<std::size_t>(p1)] = &GameServer::brains[j];
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
for (int p1 = 1; p1 < nPl; ++p1) {
|
|
if ((1 << p1) & InitAlive[count - 1]) {
|
|
GameServer::playersBrain[static_cast<std::size_t>(p1)] = m_brainDefault;
|
|
Database::difficulty[static_cast<std::size_t>(p1)] = 2;
|
|
} else {
|
|
GameServer::playersBrain[static_cast<std::size_t>(p1)] = nullptr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
m_slotAvailable = InitAlive[count - 1];
|
|
for (int i = 0; i < nPlOffered; ++i) {
|
|
PlayerSlot &slot = m_playerSlots[static_cast<std::size_t>(i)];
|
|
const quint32 tag =
|
|
(m_autoDiff < 0 && isPlayableBrain(GameServer::playersBrain[static_cast<std::size_t>(i)])) ? 768 : 0;
|
|
setTag(slot.diffUpBtn, tag);
|
|
setTag(slot.diffDownBtn, tag);
|
|
}
|
|
for (int i = 6; i < 9; ++i) {
|
|
PlayerSlot &slot = m_playerSlots[static_cast<std::size_t>(i)];
|
|
if (m_autoDiff < 0 && isPlayableBrain(GameServer::playersBrain[static_cast<std::size_t>(i)])) {
|
|
setTag(slot.multiBtn, 768);
|
|
slot.multiBtn->setButtonIndex(2 + ((m_multiControl >> i) & 1));
|
|
slot.multiBtn->setEnabled(m_page == Page::StartRandom);
|
|
} else {
|
|
setTag(slot.multiBtn, 0);
|
|
}
|
|
}
|
|
const quint32 enemyTag = (m_autoDiff > 0 && m_page != Page::StartMap) ? 768 : 0;
|
|
setTag(ui->autoEnemyUpBtn, enemyTag);
|
|
setTag(ui->autoEnemyDownBtn, enemyTag);
|
|
const quint32 diffTag = m_autoDiff > 0 ? 768 : 0;
|
|
setTag(ui->autoDiffUpBtn, diffTag);
|
|
setTag(ui->autoDiffDownBtn, diffTag);
|
|
break;
|
|
}
|
|
case Page::NoLoad:
|
|
case Page::Load:
|
|
ui->startBtn->setCaption(phraseN(QStringLiteral("STARTCONTROLS"), 2));
|
|
ui->renameBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_RENGAME")));
|
|
ui->deleteBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_DELGAME")));
|
|
break;
|
|
case Page::EditRandom:
|
|
case Page::EditMap:
|
|
ui->startBtn->setCaption(phraseN(QStringLiteral("STARTCONTROLS"), 12));
|
|
ui->renameBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_RENMAP")));
|
|
ui->deleteBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_DELMAP")));
|
|
break;
|
|
case Page::Main:
|
|
break;
|
|
}
|
|
|
|
paintInfo();
|
|
applyPageVisibility();
|
|
if (m_page == Page::Load)
|
|
ui->replayBtn->setVisible(m_miniMap.mode != MiniMap::Mode::MultiPlayer);
|
|
update();
|
|
}
|
|
|
|
void StartDlg::changeTab(Tab newTab)
|
|
{
|
|
m_tab = newTab;
|
|
if (m_tab == Tab::Map || m_tab == Tab::PreviousGame) {
|
|
ui->list->blockSignals(true);
|
|
ui->list->clear();
|
|
ui->list->addItems(currentFileList().toStringList());
|
|
ui->list->blockSignals(false);
|
|
}
|
|
if (m_tab != Tab::NewGame) {
|
|
if (ui->list->count() > 0) {
|
|
const int index = m_listIndex[static_cast<std::size_t>(m_tab)];
|
|
ui->list->setCurrentRow(index >= 0 && index < ui->list->count() ? index : 0);
|
|
} else {
|
|
ui->list->setCurrentRow(-1);
|
|
}
|
|
}
|
|
switch (m_tab) {
|
|
case Tab::Main:
|
|
changePage(Page::Main);
|
|
break;
|
|
case Tab::Map:
|
|
changePage(ui->list->currentRow() == 0 ? Page::EditRandom : Page::EditMap);
|
|
break;
|
|
case Tab::NewGame:
|
|
changePage(m_mapFileName.isEmpty() ? Page::StartRandom : Page::StartMap);
|
|
break;
|
|
case Tab::PreviousGame:
|
|
changePage(m_formerGames.count() == 0 ? Page::NoLoad : Page::Load);
|
|
break;
|
|
}
|
|
if (ui->list->isVisible())
|
|
ui->list->setFocus();
|
|
}
|
|
|
|
void StartDlg::mousePressEvent(QMouseEvent *event)
|
|
{
|
|
BufferedDrawDlg::mousePressEvent(event);
|
|
if (event->button() != Qt::LeftButton)
|
|
return;
|
|
const int x = event->pos().x();
|
|
const int y = event->pos().y();
|
|
if (y < TabHeight + 1 && x - TabOffset < TabSize * 4 && x - TabOffset >= 0 &&
|
|
(x - TabOffset) / TabSize != static_cast<int>(m_tab)) {
|
|
m_listIndex[static_cast<std::size_t>(m_tab)] = ui->list->currentRow();
|
|
changeTab(static_cast<Tab>((x - TabOffset) / TabSize));
|
|
} else if (m_page == Page::Main) {
|
|
switch (m_selectedAction) {
|
|
case MainAction::Config: showSettings(); break;
|
|
case MainAction::Manual: GameServer::directHelp(cStartHelp); break;
|
|
case MainAction::Credits: GameServer::directHelp(cStartCredits); break;
|
|
case MainAction::AIDev:
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(getAppSharePath(QString::fromLatin1(AITemplateFileName))));
|
|
break;
|
|
case MainAction::Web: QDesktopServices::openUrl(QUrl(QString::fromLatin1(CevoHomepage))); break;
|
|
case MainAction::None: break;
|
|
}
|
|
} else if (m_autoDiff < 0 && (m_page == Page::StartRandom || (m_page == Page::StartMap && m_mapStartPositions > 0))) {
|
|
for (int i = 0; i < nPlOffered; ++i) {
|
|
if (((1 << i) & m_slotAvailable) && x >= xBrain[i] && y >= yBrain[i] && x < xBrain[i] + 64 &&
|
|
y < yBrain[i] + 64) {
|
|
initPopup(i);
|
|
break;
|
|
}
|
|
}
|
|
} else if (m_autoDiff > 1 && (m_page == Page::StartRandom || m_page == Page::StartMap) && x >= xDefault &&
|
|
y >= yDefault && x < xDefault + 64 && y < yDefault + 64) {
|
|
if (GameServer::brains.kindCount(BrainType::AI) < 2)
|
|
simpleMessage(phrases.lookup(QStringLiteral("NOMOD")));
|
|
else
|
|
initPopup(-1);
|
|
} else if (m_page == Page::Load && m_lastTurn > 0 && y >= yTurnSlider && y < yTurnSlider + 7 &&
|
|
x >= xTurnSlider && x <= xTurnSlider + wTurnSlider) {
|
|
m_loadTurn = m_lastTurn * (x - xTurnSlider) / wTurnSlider;
|
|
smartInvalidate(xTurnSlider - 2, y0Mini + 61, xTurnSlider + wTurnSlider + 2, yTurnSlider + 9);
|
|
m_tracking = true;
|
|
}
|
|
}
|
|
|
|
void StartDlg::mouseReleaseEvent(QMouseEvent *event)
|
|
{
|
|
BufferedDrawDlg::mouseReleaseEvent(event);
|
|
m_tracking = false;
|
|
}
|
|
|
|
void StartDlg::mouseMoveEvent(QMouseEvent *event)
|
|
{
|
|
BufferedDrawDlg::mouseMoveEvent(event);
|
|
int x = event->pos().x();
|
|
const int y = event->pos().y();
|
|
if (m_tracking) {
|
|
x = std::clamp(x - xTurnSlider, 0, wTurnSlider);
|
|
const int oldLoadTurn = m_loadTurn;
|
|
m_loadTurn = m_lastTurn * x / wTurnSlider;
|
|
if (m_loadTurn != oldLoadTurn) {
|
|
const int lo = std::min(m_loadTurn, oldLoadTurn);
|
|
const int hi = std::max(m_loadTurn, oldLoadTurn);
|
|
smartInvalidate(xTurnSlider + lo * wTurnSlider / m_lastTurn, yTurnSlider,
|
|
xTurnSlider + hi * wTurnSlider / m_lastTurn + 1, yTurnSlider + 7);
|
|
smartInvalidate(344, y0Mini + 61, 514, y0Mini + 82);
|
|
}
|
|
} else if (m_page == Page::Main) {
|
|
MainAction newAction = MainAction::None;
|
|
if (x >= ActionSideBorder && x < width() - ActionSideBorder && y >= yAction - 8 &&
|
|
y < height() - ActionBottomBorder) {
|
|
const int index = (y - (yAction - 8)) / ActionPitch;
|
|
if (index >= 0 && index < static_cast<int>(MainAction::None) &&
|
|
std::find(m_actionsOffered.begin(), m_actionsOffered.end(), static_cast<MainAction>(index)) !=
|
|
m_actionsOffered.end())
|
|
newAction = static_cast<MainAction>(index);
|
|
}
|
|
if (newAction != m_selectedAction) {
|
|
const auto invalidateAction = [this](MainAction action) {
|
|
const int row = static_cast<int>(action);
|
|
smartInvalidate(ActionSideBorder, yAction + row * ActionPitch - 8, width() - ActionSideBorder,
|
|
yAction + (row + 1) * ActionPitch - 8);
|
|
};
|
|
if (m_selectedAction != MainAction::None)
|
|
invalidateAction(m_selectedAction);
|
|
m_selectedAction = newAction;
|
|
if (m_selectedAction != MainAction::None)
|
|
invalidateAction(m_selectedAction);
|
|
}
|
|
}
|
|
}
|
|
|
|
void StartDlg::keyPressEvent(QKeyEvent *event)
|
|
{
|
|
const QKeySequence shortcut(event->modifiers() | event->key());
|
|
if (KeyBindings::bFullScreen && KeyBindings::bFullScreen->test(shortcut)) {
|
|
fullScreen = !fullScreen;
|
|
updateInterface();
|
|
activateWindow();
|
|
} else if (KeyBindings::bHelp && KeyBindings::bHelp->test(shortcut)) {
|
|
GameServer::directHelp(cStartHelp);
|
|
} else {
|
|
BufferedDrawDlg::keyPressEvent(event);
|
|
}
|
|
}
|
|
|
|
bool StartDlg::eventFilter(QObject *watched, QEvent *event)
|
|
{
|
|
if (watched == ui->list && event->type() == QEvent::KeyPress) {
|
|
auto *keyEvent = static_cast<QKeyEvent *>(event);
|
|
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
|
|
startClicked();
|
|
return true;
|
|
}
|
|
if (keyEvent->key() == Qt::Key_Delete) {
|
|
if (ui->deleteBtn->isVisible())
|
|
deleteClicked();
|
|
return true;
|
|
}
|
|
keyPressEvent(keyEvent);
|
|
}
|
|
return BufferedDrawDlg::eventFilter(watched, event);
|
|
}
|
|
|
|
void StartDlg::offerBrain(QMenu &menu, Brain *brain, int fixedLines)
|
|
{
|
|
const QString caption = brain ? brain->name() : phrases.lookup(QStringLiteral("NOMOD"));
|
|
int position = fixedLines;
|
|
const QList<QAction *> actions = menu.actions();
|
|
while (position < actions.size() && !captionLess(caption, actions.at(position)->text()) &&
|
|
QString::compare(caption, actions.at(position)->text(), Qt::CaseInsensitive) != 0)
|
|
++position;
|
|
auto *action = new QAction(caption, &menu);
|
|
action->setCheckable(true);
|
|
const int brainIndex = GameServer::brains.indexOf(brain);
|
|
action->setChecked(m_playerPopupIndex < 0 ? m_brainDefault == brain
|
|
: GameServer::playersBrain[static_cast<std::size_t>(m_playerPopupIndex)] == brain);
|
|
connect(action, &QAction::triggered, this, [this, brainIndex] { brainClicked(brainIndex); });
|
|
if (position < actions.size())
|
|
menu.insertAction(actions.at(position), action);
|
|
else
|
|
menu.addAction(action);
|
|
}
|
|
|
|
void StartDlg::initPopup(int playerIndex)
|
|
{
|
|
m_playerPopupIndex = playerIndex;
|
|
QMenu menu(this);
|
|
int fixedLines = 0;
|
|
const auto addSeparator = [&menu, &fixedLines] {
|
|
menu.addSeparator();
|
|
++fixedLines;
|
|
};
|
|
const auto offerAIs = [&](bool onlyAvailable) {
|
|
for (Brain *ai : GameServer::brains.getByKind(BrainType::AI)) {
|
|
const bool offered = onlyAvailable
|
|
? ((ai->flags & fMultiple) || !(ai->flags & fUsed) ||
|
|
ai == GameServer::playersBrain[static_cast<std::size_t>(m_playerPopupIndex)])
|
|
: (ai->flags & fMultiple) != 0;
|
|
if (offered)
|
|
offerBrain(menu, ai, fixedLines);
|
|
}
|
|
};
|
|
|
|
if (playerIndex < 0) { // select default AI
|
|
if (GameServer::networkEnabled) {
|
|
offerBrain(menu, GameServer::brainNetworkClient, fixedLines);
|
|
++fixedLines;
|
|
}
|
|
addSeparator();
|
|
if (GameServer::brains.kindCount(BrainType::AI) >= 2) {
|
|
offerBrain(menu, GameServer::brainRandom, fixedLines);
|
|
++fixedLines;
|
|
}
|
|
offerAIs(false);
|
|
} else {
|
|
if (playerIndex > 0) {
|
|
offerBrain(menu, nullptr, fixedLines);
|
|
++fixedLines;
|
|
}
|
|
for (int i = GameServer::brains.indexOf(GameServer::brainTerm); i >= 0; --i) { // offer game interfaces
|
|
if (playerIndex == 0 || (GameServer::brains[i].kind == BrainType::Term &&
|
|
GameServer::playersBrain[0]->kind != BrainType::NoTerm)) {
|
|
offerBrain(menu, &GameServer::brains[i], fixedLines);
|
|
++fixedLines;
|
|
}
|
|
}
|
|
if (playerIndex > 0) {
|
|
if (GameServer::networkEnabled) {
|
|
offerBrain(menu, GameServer::brainNetworkServer, fixedLines);
|
|
++fixedLines;
|
|
}
|
|
addSeparator();
|
|
if (GameServer::brains.kindCount(BrainType::AI) >= 2) {
|
|
offerBrain(menu, GameServer::brainRandom, fixedLines);
|
|
++fixedLines;
|
|
}
|
|
offerAIs(true);
|
|
}
|
|
}
|
|
|
|
QPoint position;
|
|
if (playerIndex < 0)
|
|
position = QPoint(xDefault + 4, yDefault + 4);
|
|
else if (yBrain[playerIndex] > y0Brain)
|
|
position = QPoint(xBrain[playerIndex] + 4, yBrain[playerIndex] + 60);
|
|
else
|
|
position = QPoint(xBrain[playerIndex] + 4, yBrain[playerIndex] + 4);
|
|
menu.exec(mapToGlobal(position));
|
|
}
|
|
|
|
void StartDlg::brainClicked(int brainIndex)
|
|
{
|
|
if (m_playerPopupIndex < 0) { // change default AI
|
|
m_brainDefault = brainIndex >= 0 ? &GameServer::brains[brainIndex] : nullptr;
|
|
smartInvalidate(xDefault, yDefault, xDefault + 64, yDefault + 64);
|
|
return;
|
|
}
|
|
const int slotIndex = m_playerPopupIndex;
|
|
const auto index = static_cast<std::size_t>(slotIndex);
|
|
PlayerSlot &slot = m_playerSlots[index];
|
|
Brain *&slotBrain = GameServer::playersBrain[index];
|
|
if (slotBrain)
|
|
slotBrain->flags &= ~fUsed;
|
|
if (brainIndex < 0) {
|
|
slotBrain = nullptr;
|
|
slot.diffUpBtn->setVisible(false);
|
|
slot.diffDownBtn->setVisible(false);
|
|
if (slot.offerMultiple) {
|
|
slot.multiBtn->setVisible(false);
|
|
slot.multiBtn->setButtonIndex(2 + ((m_multiControl >> slotIndex) & 1));
|
|
}
|
|
m_multiControl &= ~(1 << slotIndex);
|
|
} else {
|
|
slotBrain = &GameServer::brains[brainIndex];
|
|
const bool playable = isPlayableBrain(slotBrain);
|
|
slot.diffUpBtn->setVisible(playable);
|
|
slot.diffDownBtn->setVisible(playable);
|
|
if (slot.offerMultiple) {
|
|
slot.multiBtn->setVisible(playable);
|
|
slot.multiBtn->setButtonIndex(2 + ((m_multiControl >> slotIndex) & 1));
|
|
}
|
|
slotBrain->flags |= fUsed;
|
|
Database::difficulty[index] =
|
|
(slotBrain->kind == BrainType::NoTerm || slotBrain->kind == BrainType::SuperVirtual) ? 0 : 2;
|
|
if (slotIndex == 0 && !m_mapFileName.isEmpty())
|
|
changePage(m_page);
|
|
if (slotBrain->kind == BrainType::NoTerm) { // turn all local players off
|
|
for (int i = 1; i < static_cast<int>(m_playerSlots.size()); ++i) {
|
|
Brain *&other = GameServer::playersBrain[static_cast<std::size_t>(i)];
|
|
if (other && other->kind == BrainType::Term) {
|
|
PlayerSlot &otherSlot = m_playerSlots[static_cast<std::size_t>(i)];
|
|
other = nullptr;
|
|
otherSlot.diffUpBtn->setVisible(false);
|
|
setTag(otherSlot.diffUpBtn, 0);
|
|
otherSlot.diffDownBtn->setVisible(false);
|
|
setTag(otherSlot.diffDownBtn, 0);
|
|
if (otherSlot.offerMultiple) {
|
|
otherSlot.multiBtn->setVisible(false);
|
|
setTag(otherSlot.multiBtn, 0);
|
|
}
|
|
smartInvalidate(xBrain[i] - 31, yBrain[i] - 1, xBrain[i] + 64, otherSlot.diffUpBtn->y() + 25);
|
|
}
|
|
}
|
|
GameServer::brainTerm->flags &= ~fUsed;
|
|
}
|
|
}
|
|
smartInvalidate(xBrain[slotIndex] - 31, yBrain[slotIndex] - 1, xBrain[slotIndex] + 64, slot.diffUpBtn->y() + 25);
|
|
}
|
|
|
|
void StartDlg::startClicked()
|
|
{
|
|
switch (m_page) {
|
|
case Page::Load: { // load
|
|
const FileItem *item = selectedFileItem();
|
|
if (!item)
|
|
return;
|
|
const QString fileName = item->fileName;
|
|
QString writeLogFileName;
|
|
FileList &fileList = *item->list;
|
|
if (fileName.startsWith(fileList.userHomeDir)) {
|
|
writeLogFileName = fileName;
|
|
} else {
|
|
const QString nameOnly = item->nameOnly();
|
|
writeLogFileName = fileList.userHomeDir + QLatin1Char('/') + nameOnly + QString::fromLatin1(CevoExt);
|
|
while (fileList.indexOfNameOnly(QFileInfo(writeLogFileName).completeBaseName()) != -1)
|
|
writeLogFileName = QFileInfo(writeLogFileName).path() + QLatin1Char('/') +
|
|
incrementFileName(QFileInfo(writeLogFileName).completeBaseName()) +
|
|
QString::fromLatin1(CevoExt);
|
|
}
|
|
if (GameServer::loadGame(fileName, writeLogFileName, m_loadTurn, false))
|
|
unlistBackupFile(fileName);
|
|
else
|
|
simpleMessage(phrases.lookup(QStringLiteral("LOADERR")));
|
|
m_slotAvailable = -1;
|
|
break;
|
|
}
|
|
case Page::StartRandom:
|
|
case Page::StartMap: {
|
|
if (!GameServer::playersBrain[0])
|
|
return;
|
|
if (m_page == Page::StartMap && m_mapStartPositions == 0 && m_autoDiff > 0) {
|
|
simpleMessage(phrases.lookup(QStringLiteral("NOSTARTPOS")));
|
|
return;
|
|
}
|
|
QSettings settings;
|
|
int gameCount = settings.value(QStringLiteral("GameCount"), 0).toInt();
|
|
QDir().mkpath(getSavedDir());
|
|
QString fileName;
|
|
if (m_autoDiff < 0 && GameServer::playersBrain[0]->kind == BrainType::NoTerm) {
|
|
fileName = getSavedDir() + QStringLiteral("/Round") + QString::number(QCoreApplication::applicationPid()) +
|
|
QString::fromLatin1(CevoExt);
|
|
} else {
|
|
++gameCount;
|
|
fileName = getSavedDir() + QLatin1Char('/') +
|
|
formatPhrase(QStringLiteral("GAME"), { QString::number(gameCount) }) +
|
|
QString::fromLatin1(CevoExt);
|
|
}
|
|
|
|
// Save settings and AI assignment
|
|
if (m_page == Page::StartRandom) {
|
|
saveConfig();
|
|
settings.beginGroup(QStringLiteral("AI"));
|
|
if (m_autoDiff < 0) {
|
|
for (int i = 0; i < nPlOffered; ++i) {
|
|
const Brain *brain = GameServer::playersBrain[static_cast<std::size_t>(i)];
|
|
settings.setValue(QStringLiteral("Control") + QString::number(i),
|
|
brain ? brain->fileName : QString());
|
|
settings.setValue(QStringLiteral("Diff") + QString::number(i),
|
|
Database::difficulty[static_cast<std::size_t>(i)]);
|
|
}
|
|
}
|
|
settings.endGroup();
|
|
}
|
|
|
|
if (m_brainDefault && m_brainDefault->kind != BrainType::NetworkClient) {
|
|
if (m_autoDiff > 0) {
|
|
settings.setValue(QStringLiteral("DefaultAI"), m_brainDefault->fileName);
|
|
m_slotAvailable = 0; // PlayerSlot will be invalid hereafter
|
|
GameServer::playersBrain[0] = GameServer::brainTerm;
|
|
Database::difficulty[0] = PlayerAutoDiff[m_autoDiff - 1];
|
|
for (int i = 1; i < nPl; ++i) {
|
|
if ((m_page == Page::StartRandom && i <= m_autoEnemies) ||
|
|
(m_page == Page::StartMap && i < m_mapStartPositions)) {
|
|
GameServer::playersBrain[static_cast<std::size_t>(i)] =
|
|
m_autoDiff == 1 ? GameServer::brains.beginner() : m_brainDefault;
|
|
Database::difficulty[static_cast<std::size_t>(i)] = EnemyAutoDiff[m_autoDiff - 1];
|
|
} else {
|
|
GameServer::playersBrain[static_cast<std::size_t>(i)] = nullptr;
|
|
}
|
|
}
|
|
} else {
|
|
for (int i = 6; i < 9; ++i) {
|
|
const auto index = static_cast<std::size_t>(i);
|
|
if (GameServer::playersBrain[0]->kind != BrainType::NoTerm && (m_multiControl & (1 << i))) {
|
|
GameServer::playersBrain[index + 3] = GameServer::playersBrain[index];
|
|
Database::difficulty[index + 3] = Database::difficulty[index];
|
|
GameServer::playersBrain[index + 6] = GameServer::playersBrain[index];
|
|
Database::difficulty[index + 6] = Database::difficulty[index];
|
|
} else {
|
|
GameServer::playersBrain[index + 3] = nullptr;
|
|
GameServer::playersBrain[index + 6] = nullptr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
settings.setValue(QStringLiteral("AutoDiff"), m_autoDiff);
|
|
settings.setValue(QStringLiteral("AutoEnemies"), m_autoEnemies);
|
|
settings.setValue(QStringLiteral("MaxTurn"), m_maxTurn);
|
|
settings.setValue(QStringLiteral("GameCount"), gameCount);
|
|
settings.sync();
|
|
|
|
GameServer::startNewGame(fileName, m_mapFileName, WorldSizes[m_worldSize].x(), WorldSizes[m_worldSize].y(),
|
|
m_startLandMass, m_maxTurn);
|
|
unlistBackupFile(fileName);
|
|
break;
|
|
}
|
|
case Page::EditMap: {
|
|
const FileItem *item = selectedFileItem();
|
|
if (!item)
|
|
return;
|
|
const QString fileName = item->fileName;
|
|
FileList &fileList = *item->list;
|
|
QString writeFileMap;
|
|
if (fileName.startsWith(fileList.userHomeDir)) {
|
|
writeFileMap = fileName;
|
|
} else {
|
|
writeFileMap = fileList.userHomeDir + QLatin1Char('/') + item->nameOnly() + QString::fromLatin1(CevoMapExt);
|
|
while (fileList.indexOfNameOnly(QFileInfo(writeFileMap).completeBaseName()) != -1)
|
|
writeFileMap = QFileInfo(writeFileMap).path() + QLatin1Char('/') +
|
|
incrementFileName(QFileInfo(writeFileMap).completeBaseName()) +
|
|
QString::fromLatin1(CevoMapExt);
|
|
}
|
|
GameServer::editMap(m_mapFileName, writeFileMap, lxmax, lymax, m_startLandMass);
|
|
break;
|
|
}
|
|
case Page::EditRandom: { // new map
|
|
QSettings settings;
|
|
const int mapCount = settings.value(QStringLiteral("MapCount"), 0).toInt() + 1;
|
|
settings.setValue(QStringLiteral("MapCount"), mapCount);
|
|
settings.sync();
|
|
QDir().mkpath(getMapsDir());
|
|
m_mapFileName = getMapsDir() + QLatin1Char('/') +
|
|
formatPhrase(QStringLiteral("MAP"), { QString::number(mapCount) }) +
|
|
QString::fromLatin1(CevoMapExt);
|
|
GameServer::editMap(m_mapFileName, m_mapFileName, WorldSizes[m_worldSize].x(), WorldSizes[m_worldSize].y(),
|
|
m_startLandMass);
|
|
break;
|
|
}
|
|
case Page::NoLoad:
|
|
case Page::Main:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void StartDlg::listClicked()
|
|
{
|
|
if (m_tab == Tab::Map && (ui->list->currentRow() == 0) != (m_page == Page::EditRandom)) {
|
|
m_page = ui->list->currentRow() == 0 ? Page::EditRandom : Page::EditMap;
|
|
applyPageVisibility();
|
|
smartInvalidate(328, ui->up1Btn->y() - 12, width(), ui->up2Btn->y() + 35);
|
|
}
|
|
if (m_page == Page::Load)
|
|
m_turnValid = false;
|
|
paintInfo();
|
|
if (m_page == Page::Load)
|
|
ui->replayBtn->setVisible(m_miniMap.mode != MiniMap::Mode::MultiPlayer);
|
|
}
|
|
|
|
void StartDlg::listSelectionChanged()
|
|
{
|
|
const FileItem *item = selectedFileItem();
|
|
if (!item)
|
|
return;
|
|
const bool inHome = item->fileName.startsWith(item->list->userHomeDir);
|
|
ui->deleteBtn->setVisible(inHome);
|
|
ui->renameBtn->setVisible(inHome);
|
|
}
|
|
|
|
void StartDlg::renameClicked()
|
|
{
|
|
FileItem *item = selectedFileItem();
|
|
if (!item)
|
|
return;
|
|
InputDlg inputDlg;
|
|
inputDlg.setWindowTitle(phrases.lookup(m_page == Page::Load ? QStringLiteral("TITLE_BOOKNAME")
|
|
: QStringLiteral("TITLE_MAPNAME")));
|
|
const int row = ui->list->currentRow();
|
|
inputDlg.editInput()->setText(ui->list->item(row)->text());
|
|
inputDlg.centerToRect(QRect(mapToGlobal(QPoint(0, 0)), size()));
|
|
inputDlg.showModal();
|
|
QString newName = inputDlg.editInput()->text();
|
|
while (GameServer::isAutoSaveFileName(newName))
|
|
newName.remove(0, 1);
|
|
if (inputDlg.modalResult() != Dpi::ModalResult::Ok || newName.isEmpty() ||
|
|
newName == ui->list->item(row)->text())
|
|
return;
|
|
static const QString forbidden = QStringLiteral("\\/:*?\"<>|");
|
|
for (const QChar c : newName) {
|
|
if (forbidden.contains(c)) {
|
|
simpleMessage(formatPhrase(QStringLiteral("NOFILENAME"), { QString(c) }));
|
|
return;
|
|
}
|
|
}
|
|
FileList &fileList = *item->list;
|
|
if (fileList.indexOfNameOnly(newName) != -1)
|
|
return;
|
|
|
|
const QString fileName = item->fileName;
|
|
const QFileInfo info(fileName);
|
|
const bool ok = QFile::rename(fileName, info.path() + QLatin1Char('/') + newName + QLatin1Char('.') + info.suffix());
|
|
if (m_page != Page::Load) { // rename the map picture
|
|
const QString pictureFileName = info.path() + QLatin1Char('/') + info.completeBaseName() +
|
|
QString::fromLatin1(CevoMapPictureExt);
|
|
if (QFileInfo::exists(pictureFileName))
|
|
QFile::rename(pictureFileName, info.path() + QLatin1Char('/') + newName +
|
|
QString::fromLatin1(CevoMapPictureExt));
|
|
}
|
|
if (ok) {
|
|
item->setNameOnly(newName);
|
|
fileList.sortItems();
|
|
ui->list->blockSignals(true);
|
|
ui->list->clear();
|
|
ui->list->addItems(fileList.toStringList());
|
|
ui->list->blockSignals(false);
|
|
ui->list->setCurrentRow(fileList.indexOfNameOnly(newName));
|
|
if (m_page == Page::EditMap)
|
|
paintInfo();
|
|
update();
|
|
}
|
|
}
|
|
|
|
void StartDlg::deleteClicked()
|
|
{
|
|
const int row = ui->list->currentRow();
|
|
FileItem *item = selectedFileItem();
|
|
if (!item)
|
|
return;
|
|
MessgDlg dlg;
|
|
dlg.messageText = phrases.lookup(m_page == Page::Load ? QStringLiteral("DELETEQUERY")
|
|
: QStringLiteral("MAPDELETEQUERY"));
|
|
dlg.kind = MessageKind::OkCancel;
|
|
dlg.showModal();
|
|
if (dlg.modalResult() != Dpi::ModalResult::Ok)
|
|
return;
|
|
QFile::remove(item->fileName);
|
|
FileList &fileList = *item->list;
|
|
fileList.removeAt(row);
|
|
ui->list->blockSignals(true);
|
|
delete ui->list->takeItem(row);
|
|
ui->list->blockSignals(false);
|
|
if (ui->list->count() == 0) {
|
|
changePage(Page::NoLoad);
|
|
} else {
|
|
ui->list->setCurrentRow(row == 0 ? 0 : row - 1);
|
|
if (m_page == Page::EditMap && ui->list->currentRow() == 0) {
|
|
changePage(Page::EditRandom);
|
|
} else {
|
|
update();
|
|
if (m_page == Page::Load)
|
|
m_turnValid = false;
|
|
paintInfo();
|
|
if (m_page == Page::Load)
|
|
ui->replayBtn->setVisible(m_miniMap.mode != MiniMap::Mode::MultiPlayer);
|
|
}
|
|
}
|
|
}
|
|
|
|
void StartDlg::diffClicked(QWidget *sender)
|
|
{
|
|
for (int i = 0; i < nPlOffered; ++i) {
|
|
const PlayerSlot &slot = m_playerSlots[static_cast<std::size_t>(i)];
|
|
int &difficulty = Database::difficulty[static_cast<std::size_t>(i)];
|
|
if ((sender == slot.diffUpBtn && difficulty < 3) || (sender == slot.diffDownBtn && difficulty > 1)) {
|
|
difficulty += sender == slot.diffUpBtn ? 1 : -1;
|
|
smartInvalidate(xBrain[i] - 18, yBrain[i] + 19, xBrain[i] - 18 + 12, yBrain[i] + (19 + 14));
|
|
}
|
|
}
|
|
}
|
|
|
|
void StartDlg::multiClicked(QWidget *sender)
|
|
{
|
|
for (int i = 6; i < 9; ++i) {
|
|
PlayerSlot &slot = m_playerSlots[static_cast<std::size_t>(i)];
|
|
if (sender == slot.multiBtn) {
|
|
m_multiControl ^= 1 << i;
|
|
slot.multiBtn->setButtonIndex(2 + ((m_multiControl >> i) & 1));
|
|
}
|
|
}
|
|
}
|
|
|
|
void StartDlg::up1Clicked()
|
|
{
|
|
if (m_worldSize < MaxWorldSize - 1) {
|
|
++m_worldSize;
|
|
paintInfo();
|
|
smartInvalidate(344, y0Mini - 77, 510, y0Mini - 77 + 21);
|
|
}
|
|
}
|
|
|
|
void StartDlg::down1Clicked()
|
|
{
|
|
if (m_worldSize > 0) {
|
|
--m_worldSize;
|
|
paintInfo();
|
|
smartInvalidate(344, y0Mini - 77, 510, y0Mini - 77 + 21);
|
|
}
|
|
}
|
|
|
|
void StartDlg::up2Clicked()
|
|
{
|
|
switch (m_page) {
|
|
case Page::StartRandom:
|
|
case Page::StartMap:
|
|
if (m_maxTurn < 1400) {
|
|
m_maxTurn += 200;
|
|
smartInvalidate(344, y0Mini + 61, 514, y0Mini + 82);
|
|
}
|
|
break;
|
|
case Page::Load:
|
|
if (m_loadTurn < m_lastTurn) {
|
|
++m_loadTurn;
|
|
smartInvalidate(xTurnSlider - 2, y0Mini + 61, xTurnSlider + wTurnSlider + 2, yTurnSlider + 9);
|
|
}
|
|
break;
|
|
case Page::EditRandom:
|
|
if (m_startLandMass < 96) {
|
|
m_startLandMass += 5;
|
|
paintInfo();
|
|
smartInvalidate(344, y0Mini + 61, 514, y0Mini + 61 + 21);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void StartDlg::down2Clicked()
|
|
{
|
|
switch (m_page) {
|
|
case Page::StartRandom:
|
|
case Page::StartMap:
|
|
if (m_maxTurn > 400) {
|
|
m_maxTurn -= 200;
|
|
smartInvalidate(344, y0Mini + 61, 514, y0Mini + 82);
|
|
}
|
|
break;
|
|
case Page::Load:
|
|
if (m_loadTurn > 0) {
|
|
--m_loadTurn;
|
|
smartInvalidate(xTurnSlider - 2, y0Mini + 61, xTurnSlider + wTurnSlider + 2, yTurnSlider + 9);
|
|
}
|
|
break;
|
|
case Page::EditRandom:
|
|
if (m_startLandMass > 10) {
|
|
m_startLandMass -= 5;
|
|
paintInfo();
|
|
smartInvalidate(344, y0Mini + 61, 514, y0Mini + 61 + 21);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void StartDlg::customizeClicked()
|
|
{
|
|
m_autoDiff = -m_autoDiff;
|
|
ui->customizeBtn->setButtonIndex(ui->customizeBtn->buttonIndex() ^ 1);
|
|
changePage(m_page);
|
|
}
|
|
|
|
void StartDlg::autoDiffUpClicked()
|
|
{
|
|
if (m_autoDiff < 5) {
|
|
++m_autoDiff;
|
|
smartInvalidate(120, y0Mini + 61, 272, y0Mini + 61 + 21);
|
|
smartInvalidate(xDefault - 2, yDefault - 2, xDefault + 64 + 2, yDefault + 64 + 2);
|
|
}
|
|
}
|
|
|
|
void StartDlg::autoDiffDownClicked()
|
|
{
|
|
if (m_autoDiff > 1) {
|
|
--m_autoDiff;
|
|
smartInvalidate(120, y0Mini + 61, 272, y0Mini + 61 + 21);
|
|
smartInvalidate(xDefault - 2, yDefault - 2, xDefault + 64 + 2, yDefault + 64 + 2);
|
|
}
|
|
}
|
|
|
|
void StartDlg::autoEnemyUpClicked()
|
|
{
|
|
if (m_autoEnemies < nPl - 1) {
|
|
++m_autoEnemies;
|
|
smartInvalidate(160, yMain + 140, 198, yMain + 140 + 21);
|
|
}
|
|
}
|
|
|
|
void StartDlg::autoEnemyDownClicked()
|
|
{
|
|
if (m_autoEnemies > 0) {
|
|
--m_autoEnemies;
|
|
smartInvalidate(160, yMain + 140, 198, yMain + 140 + 21);
|
|
}
|
|
}
|
|
|
|
void StartDlg::replayClicked()
|
|
{
|
|
const FileItem *item = selectedFileItem();
|
|
if (!item)
|
|
return;
|
|
GameServer::loadGame(item->fileName, item->fileName, m_lastTurn, true);
|
|
m_slotAvailable = -1;
|
|
}
|
|
|
|
} // namespace Cevo
|