1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-04 09:42:40 +02:00
vcmi/mapeditor/windownewmap.cpp

458 lines
12 KiB
C++
Raw Normal View History

2022-10-12 23:51:55 +02:00
/*
* windownewmap.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
2022-09-18 01:23:17 +02:00
#include "StdInc.h"
#include "../lib/mapping/CMap.h"
#include "../lib/rmg/CRmgTemplateStorage.h"
#include "../lib/rmg/CRmgTemplate.h"
#include "../lib/rmg/CMapGenerator.h"
#include "../lib/VCMI_Lib.h"
#include "../lib/mapping/CMapEditManager.h"
2023-05-24 00:14:06 +02:00
#include "../lib/mapping/MapFormat.h"
2022-09-18 01:23:17 +02:00
#include "../lib/CGeneralTextHandler.h"
2024-02-29 21:05:20 +02:00
#include "../lib/serializer/JsonSerializer.h"
#include "../lib/serializer/JsonDeserializer.h"
2022-09-18 01:23:17 +02:00
2024-02-29 21:05:20 +02:00
#include "jsonutils.h"
2022-09-18 01:23:17 +02:00
#include "windownewmap.h"
#include "ui_windownewmap.h"
#include "mainwindow.h"
#include "generatorprogress.h"
WindowNewMap::WindowNewMap(QWidget *parent) :
QDialog(parent),
ui(new Ui::WindowNewMap)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowModality(Qt::ApplicationModal);
2023-04-22 16:47:25 +02:00
for(auto * combo : {ui->humanCombo, ui->cpuCombo, ui->humanTeamsCombo, ui->cpuTeamsCombo})
combo->clear();
//prepare human players combo box
for(int i = 0; i <= PlayerColor::PLAYER_LIMIT_I; ++i)
{
ui->humanCombo->addItem(!i ? randomString : QString::number(players.at(i)));
ui->humanCombo->setItemData(i, QVariant(players.at(i)));
ui->cpuCombo->addItem(!i ? randomString : QString::number(cpuPlayers.at(i)));
ui->cpuCombo->setItemData(i, QVariant(cpuPlayers.at(i)));
ui->humanTeamsCombo->addItem(!i ? randomString : QString::number(cpuPlayers.at(i)));
ui->humanTeamsCombo->setItemData(i, QVariant(cpuPlayers.at(i)));
ui->cpuTeamsCombo->addItem(!i ? randomString : QString::number(cpuPlayers.at(i)));
ui->cpuTeamsCombo->setItemData(i, QVariant(cpuPlayers.at(i)));
}
2022-09-18 01:23:17 +02:00
2024-02-29 21:05:20 +02:00
bool useLoaded = loadUserSettings();
if (!useLoaded)
{
for(auto * combo : {ui->humanCombo, ui->cpuCombo, ui->humanTeamsCombo, ui->cpuTeamsCombo})
combo->setCurrentIndex(0);
}
2022-09-18 01:23:17 +02:00
show();
2022-09-18 01:23:17 +02:00
2024-02-29 21:05:20 +02:00
if (!useLoaded)
{
//setup initial parameters
int width = ui->widthTxt->text().toInt();
int height = ui->heightTxt->text().toInt();
mapGenOptions.setWidth(width ? width : 1);
mapGenOptions.setHeight(height ? height : 1);
bool twoLevel = ui->twoLevelCheck->isChecked();
mapGenOptions.setHasTwoLevels(twoLevel);
updateTemplateList();
}
2022-09-18 01:23:17 +02:00
}
WindowNewMap::~WindowNewMap()
{
delete ui;
}
2024-02-29 21:05:20 +02:00
bool WindowNewMap::loadUserSettings()
2022-09-18 01:23:17 +02:00
{
2024-02-29 21:05:20 +02:00
bool ret = false;
CRmgTemplate * templ = nullptr;
2022-09-18 01:23:17 +02:00
QSettings s(Ui::teamName, Ui::appName);
2024-02-29 21:05:20 +02:00
auto generateRandom = s.value(newMapGenerateRandom);
if (generateRandom.isValid())
{
ui->randomMapCheck->setChecked(generateRandom.toBool());
}
auto settings = s.value(newMapWindow);
if (settings.isValid())
{
auto node = JsonUtils::toJson(settings);
JsonDeserializer handler(nullptr, node);
handler.serializeStruct("lastSettings", mapGenOptions);
templ = const_cast<CRmgTemplate*>(mapGenOptions.getMapTemplate()); // Remember for later
ui->widthTxt->setText(QString::number(mapGenOptions.getWidth()));
ui->heightTxt->setText(QString::number(mapGenOptions.getHeight()));
2024-06-13 05:38:47 +02:00
for(const auto & sz : mapSizes)
2024-02-29 21:05:20 +02:00
{
if(sz.second.first == mapGenOptions.getWidth() &&
sz.second.second == mapGenOptions.getHeight())
{
ui->sizeCombo->setCurrentIndex(sz.first);
break;
}
}
ui->twoLevelCheck->setChecked(mapGenOptions.getHasTwoLevels());
2024-02-29 21:12:46 +02:00
ui->humanCombo->setCurrentIndex(mapGenOptions.getHumanOrCpuPlayerCount());
2024-02-29 21:05:20 +02:00
ui->cpuCombo->setCurrentIndex(mapGenOptions.getCompOnlyPlayerCount());
ui->humanTeamsCombo->setCurrentIndex(mapGenOptions.getTeamCount());
ui->cpuTeamsCombo->setCurrentIndex(mapGenOptions.getCompOnlyTeamCount());
switch (mapGenOptions.getWaterContent())
{
case EWaterContent::RANDOM:
ui->waterOpt1->setChecked(true); break;
case EWaterContent::NONE:
ui->waterOpt2->setChecked(true); break;
case EWaterContent::NORMAL:
ui->waterOpt3->setChecked(true); break;
case EWaterContent::ISLANDS:
ui->waterOpt4->setChecked(true); break;
}
switch (mapGenOptions.getMonsterStrength())
{
case EMonsterStrength::RANDOM:
ui->monsterOpt1->setChecked(true); break;
case EMonsterStrength::GLOBAL_WEAK:
ui->monsterOpt2->setChecked(true); break;
case EMonsterStrength::GLOBAL_NORMAL:
ui->monsterOpt3->setChecked(true); break;
case EMonsterStrength::GLOBAL_STRONG:
ui->monsterOpt4->setChecked(true); break;
}
ret = true;
}
updateTemplateList();
mapGenOptions.setMapTemplate(templ); // Can be null
if (templ)
{
2024-02-29 21:12:46 +02:00
std::string name = templ->getName();
2024-02-29 21:05:20 +02:00
for (size_t i = 0; i < ui->templateCombo->count(); i++)
{
if (ui->templateCombo->itemText(i).toStdString() == name)
{
ui->templateCombo->setCurrentIndex(i);
break;
}
}
ret = true;
}
2024-02-29 21:20:35 +02:00
return ret;
2022-09-18 01:23:17 +02:00
}
void WindowNewMap::saveUserSettings()
{
QSettings s(Ui::teamName, Ui::appName);
2024-02-29 21:05:20 +02:00
JsonNode data;
JsonSerializer ser(nullptr, data);
ser.serializeStruct("lastSettings", mapGenOptions);
auto variant = JsonUtils::toVariant(data);
s.setValue(newMapWindow, variant);
s.setValue(newMapGenerateRandom, ui->randomMapCheck->isChecked());
2022-09-18 01:23:17 +02:00
}
void WindowNewMap::on_cancelButton_clicked()
{
2024-02-29 21:05:20 +02:00
saveUserSettings();
2022-09-18 01:23:17 +02:00
close();
}
void generateRandomMap(CMapGenerator & gen, MainWindow * window)
{
window->controller.setMap(gen.generate());
}
std::unique_ptr<CMap> generateEmptyMap(CMapGenOptions & options)
{
2024-01-01 16:37:48 +02:00
auto map = std::make_unique<CMap>(nullptr);
2022-09-18 01:23:17 +02:00
map->version = EMapFormat::VCMI;
2024-06-29 14:01:25 +02:00
map->creationDateTime = std::time(nullptr);
2022-09-18 01:23:17 +02:00
map->width = options.getWidth();
map->height = options.getHeight();
map->twoLevel = options.getHasTwoLevels();
map->initTerrain();
map->getEditManager()->clearTerrain(&CRandomGenerator::getDefault());
return map;
2022-09-18 01:23:17 +02:00
}
void WindowNewMap::on_okButton_clicked()
2022-09-18 01:23:17 +02:00
{
EWaterContent::EWaterContent water = EWaterContent::RANDOM;
EMonsterStrength::EMonsterStrength monster = EMonsterStrength::RANDOM;
if(ui->waterOpt1->isChecked())
water = EWaterContent::RANDOM;
if(ui->waterOpt2->isChecked())
water = EWaterContent::NONE;
if(ui->waterOpt3->isChecked())
water = EWaterContent::NORMAL;
if(ui->waterOpt4->isChecked())
water = EWaterContent::ISLANDS;
if(ui->monsterOpt1->isChecked())
monster = EMonsterStrength::RANDOM;
if(ui->monsterOpt2->isChecked())
monster = EMonsterStrength::GLOBAL_WEAK;
if(ui->monsterOpt3->isChecked())
monster = EMonsterStrength::GLOBAL_NORMAL;
if(ui->monsterOpt4->isChecked())
monster = EMonsterStrength::GLOBAL_STRONG;
mapGenOptions.setWaterContent(water);
mapGenOptions.setMonsterStrength(monster);
2024-02-29 21:05:20 +02:00
saveUserSettings();
2022-09-18 01:23:17 +02:00
std::unique_ptr<CMap> nmap;
if(ui->randomMapCheck->isChecked())
{
//verify map template
if(mapGenOptions.getPossibleTemplates().empty())
{
QMessageBox::warning(this, tr("No template"), tr("No template for parameters specified. Random map cannot be generated."));
2022-09-18 01:23:17 +02:00
return;
}
2022-09-19 00:42:38 +02:00
int seed = std::time(nullptr);
if(ui->checkSeed->isChecked() && !ui->lineSeed->text().isEmpty())
seed = ui->lineSeed->text().toInt();
2024-01-01 16:37:48 +02:00
CMapGenerator generator(mapGenOptions, nullptr, seed);
2022-09-18 01:23:17 +02:00
auto progressBarWnd = new GeneratorProgress(generator, this);
progressBarWnd->show();
try
{
auto f = std::async(std::launch::async, &CMapGenerator::generate, &generator);
progressBarWnd->update();
nmap = f.get();
}
catch(const std::exception & e)
{
QMessageBox::critical(this, tr("RMG failure"), e.what());
2022-09-18 01:23:17 +02:00
}
}
else
{
auto f = std::async(std::launch::async, &::generateEmptyMap, std::ref(mapGenOptions));
nmap = f.get();
}
2023-04-17 01:01:29 +02:00
nmap->mods = MapController::modAssessmentAll();
2022-09-18 01:23:17 +02:00
static_cast<MainWindow*>(parent())->controller.setMap(std::move(nmap));
static_cast<MainWindow*>(parent())->initializeMap(true);
close();
}
void WindowNewMap::on_sizeCombo_activated(int index)
{
2023-04-22 16:47:25 +02:00
ui->widthTxt->setText(QString::number(mapSizes.at(index).first));
ui->heightTxt->setText(QString::number(mapSizes.at(index).second));
2022-09-18 01:23:17 +02:00
}
void WindowNewMap::on_twoLevelCheck_stateChanged(int arg1)
{
bool twoLevel = ui->twoLevelCheck->isChecked();
mapGenOptions.setHasTwoLevels(twoLevel);
updateTemplateList();
}
void WindowNewMap::on_humanCombo_activated(int index)
{
2023-04-22 16:47:25 +02:00
int humans = ui->humanCombo->currentData().toInt();
if(humans > PlayerColor::PLAYER_LIMIT_I)
2022-09-18 01:23:17 +02:00
{
2023-04-22 16:47:25 +02:00
humans = PlayerColor::PLAYER_LIMIT_I;
2022-09-18 01:23:17 +02:00
ui->humanCombo->setCurrentIndex(humans);
}
2023-10-28 20:30:38 +02:00
mapGenOptions.setHumanOrCpuPlayerCount(humans);
2022-09-18 01:23:17 +02:00
int teams = mapGenOptions.getTeamCount();
if(teams > humans - 1)
{
2023-04-22 18:38:39 +02:00
teams = humans > 0 ? humans - 1 : CMapGenOptions::RANDOM_SIZE;
2023-04-22 16:47:25 +02:00
ui->humanTeamsCombo->setCurrentIndex(teams + 1); //skip one element because first is random
2022-09-18 01:23:17 +02:00
}
int cpu = mapGenOptions.getCompOnlyPlayerCount();
2023-04-22 16:47:25 +02:00
if(cpu > PlayerColor::PLAYER_LIMIT_I - humans)
2022-09-18 01:23:17 +02:00
{
2023-04-22 16:47:25 +02:00
cpu = PlayerColor::PLAYER_LIMIT_I - humans;
ui->cpuCombo->setCurrentIndex(cpu + 1); //skip one element because first is random
2022-09-18 01:23:17 +02:00
}
int cpuTeams = mapGenOptions.getCompOnlyTeamCount(); //comp only players - 1
if(cpuTeams > cpu - 1)
{
2023-04-22 18:38:39 +02:00
cpuTeams = cpu > 0 ? cpu - 1 : CMapGenOptions::RANDOM_SIZE;
2023-04-22 16:47:25 +02:00
ui->cpuTeamsCombo->setCurrentIndex(cpuTeams + 1); //skip one element because first is random
2022-09-18 01:23:17 +02:00
}
updateTemplateList();
}
void WindowNewMap::on_cpuCombo_activated(int index)
{
2023-10-28 20:30:38 +02:00
int humans = mapGenOptions.getHumanOrCpuPlayerCount();
2023-04-22 16:47:25 +02:00
int cpu = ui->cpuCombo->currentData().toInt();
2023-10-28 20:30:38 +02:00
// FIXME: Use mapGenOption method only to calculate actual number of players for current template
2023-04-22 16:47:25 +02:00
if(cpu > PlayerColor::PLAYER_LIMIT_I - humans)
2022-09-18 01:23:17 +02:00
{
2023-04-22 16:47:25 +02:00
cpu = PlayerColor::PLAYER_LIMIT_I - humans;
ui->cpuCombo->setCurrentIndex(cpu + 1); //skip one element because first is random
2022-09-18 01:23:17 +02:00
}
2023-04-22 16:47:25 +02:00
2022-09-18 01:23:17 +02:00
mapGenOptions.setCompOnlyPlayerCount(cpu);
2023-04-22 16:47:25 +02:00
int cpuTeams = mapGenOptions.getCompOnlyTeamCount(); //comp only players - 1
if(cpuTeams > cpu - 1)
{
2023-04-22 18:38:39 +02:00
cpuTeams = cpu > 0 ? cpu - 1 : CMapGenOptions::RANDOM_SIZE;
2023-04-22 16:47:25 +02:00
ui->cpuTeamsCombo->setCurrentIndex(cpuTeams + 1); //skip one element because first is random
}
2022-09-18 01:23:17 +02:00
updateTemplateList();
}
void WindowNewMap::on_randomMapCheck_stateChanged(int arg1)
{
randomMap = ui->randomMapCheck->isChecked();
ui->templateCombo->setEnabled(randomMap);
updateTemplateList();
}
void WindowNewMap::on_templateCombo_activated(int index)
{
if(index == 0)
{
mapGenOptions.setMapTemplate(nullptr);
return;
}
2022-09-19 01:17:39 +02:00
auto * templ = data_cast<const CRmgTemplate>(ui->templateCombo->currentData().toLongLong());
2022-09-18 01:23:17 +02:00
mapGenOptions.setMapTemplate(templ);
}
void WindowNewMap::on_widthTxt_textChanged(const QString &arg1)
{
int sz = arg1.toInt();
if(sz > 1)
{
mapGenOptions.setWidth(arg1.toInt());
updateTemplateList();
}
}
void WindowNewMap::on_heightTxt_textChanged(const QString &arg1)
{
int sz = arg1.toInt();
if(sz > 1)
{
mapGenOptions.setHeight(arg1.toInt());
updateTemplateList();
}
}
void WindowNewMap::updateTemplateList()
{
ui->templateCombo->clear();
ui->templateCombo->setCurrentIndex(-1);
if(!randomMap)
return;
mapGenOptions.setMapTemplate(nullptr);
auto templates = mapGenOptions.getPossibleTemplates();
if(templates.empty())
return;
2022-09-19 01:17:39 +02:00
ui->templateCombo->addItem("[default]", 0);
2022-09-18 01:23:17 +02:00
for(auto * templ : templates)
{
2022-09-19 01:17:39 +02:00
ui->templateCombo->addItem(QString::fromStdString(templ->getName()), data_cast(templ));
2022-09-18 01:23:17 +02:00
}
ui->templateCombo->setCurrentIndex(0);
}
2022-09-19 00:42:38 +02:00
void WindowNewMap::on_checkSeed_toggled(bool checked)
{
ui->lineSeed->setEnabled(checked);
}
2023-04-22 16:47:25 +02:00
void WindowNewMap::on_humanTeamsCombo_activated(int index)
{
2023-10-28 20:30:38 +02:00
int humans = mapGenOptions.getHumanOrCpuPlayerCount();
2023-04-22 16:47:25 +02:00
int teams = ui->humanTeamsCombo->currentData().toInt();
if(teams >= humans)
{
2023-04-22 18:38:39 +02:00
teams = humans > 0 ? humans - 1 : CMapGenOptions::RANDOM_SIZE;
2023-04-22 16:47:25 +02:00
ui->humanTeamsCombo->setCurrentIndex(teams + 1); //skip one element because first is random
}
mapGenOptions.setTeamCount(teams);
updateTemplateList();
}
void WindowNewMap::on_cpuTeamsCombo_activated(int index)
{
int cpu = mapGenOptions.getCompOnlyPlayerCount();
2023-04-22 18:38:39 +02:00
int cpuTeams = ui->cpuTeamsCombo->currentData().toInt();
if(cpuTeams >= cpu)
2023-04-22 16:47:25 +02:00
{
2023-04-22 18:38:39 +02:00
cpuTeams = cpu > 0 ? cpu - 1 : CMapGenOptions::RANDOM_SIZE;
ui->cpuTeamsCombo->setCurrentIndex(cpuTeams + 1); //skip one element because first is random
2023-04-22 16:47:25 +02:00
}
2023-04-22 18:38:39 +02:00
mapGenOptions.setCompOnlyTeamCount(cpuTeams);
2023-04-22 16:47:25 +02:00
updateTemplateList();
}