windownewmap.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * windownewmap.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "../lib/mapping/CMap.h"
  12. #include "../lib/rmg/CRmgTemplateStorage.h"
  13. #include "../lib/rmg/CRmgTemplate.h"
  14. #include "../lib/rmg/CMapGenerator.h"
  15. #include "../lib/GameLibrary.h"
  16. #include "../lib/mapping/CMapEditManager.h"
  17. #include "../lib/mapping/MapFormat.h"
  18. #include "../lib/texts/CGeneralTextHandler.h"
  19. #include "../lib/CRandomGenerator.h"
  20. #include "../lib/serializer/JsonSerializer.h"
  21. #include "../lib/serializer/JsonDeserializer.h"
  22. #include "../vcmiqt/launcherdirs.h"
  23. #include "../vcmiqt/jsonutils.h"
  24. #include "windownewmap.h"
  25. #include "ui_windownewmap.h"
  26. #include "mainwindow.h"
  27. #include "generatorprogress.h"
  28. WindowNewMap::WindowNewMap(QWidget *parent) :
  29. QDialog(parent),
  30. ui(new Ui::WindowNewMap)
  31. {
  32. ui->setupUi(this);
  33. setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
  34. setAttribute(Qt::WA_DeleteOnClose);
  35. setWindowModality(Qt::ApplicationModal);
  36. for(auto * combo : {ui->humanCombo, ui->cpuCombo, ui->humanTeamsCombo, ui->cpuTeamsCombo})
  37. combo->clear();
  38. //prepare human players combo box
  39. for(int i = 0; i <= PlayerColor::PLAYER_LIMIT_I; ++i)
  40. {
  41. ui->humanCombo->addItem(!i ? randomString : QString::number(players.at(i)));
  42. ui->humanCombo->setItemData(i, QVariant(players.at(i)));
  43. ui->cpuCombo->addItem(!i ? randomString : QString::number(cpuPlayers.at(i)));
  44. ui->cpuCombo->setItemData(i, QVariant(cpuPlayers.at(i)));
  45. ui->humanTeamsCombo->addItem(!i ? randomString : QString::number(cpuPlayers.at(i)));
  46. ui->humanTeamsCombo->setItemData(i, QVariant(cpuPlayers.at(i)));
  47. ui->cpuTeamsCombo->addItem(!i ? randomString : QString::number(cpuPlayers.at(i)));
  48. ui->cpuTeamsCombo->setItemData(i, QVariant(cpuPlayers.at(i)));
  49. }
  50. on_sizeStandardRadio_toggled(true);
  51. on_checkSeed_toggled(false);
  52. bool useLoaded = loadUserSettings();
  53. if (!useLoaded)
  54. {
  55. for(auto * combo : {ui->humanCombo, ui->cpuCombo, ui->humanTeamsCombo, ui->cpuTeamsCombo})
  56. combo->setCurrentIndex(0);
  57. }
  58. show();
  59. if (!useLoaded)
  60. {
  61. //setup initial parameters
  62. int width = ui->widthTxt->text().toInt();
  63. int height = ui->heightTxt->text().toInt();
  64. mapGenOptions.setWidth(width ? width : 1);
  65. mapGenOptions.setHeight(height ? height : 1);
  66. mapGenOptions.setLevels(ui->spinBoxLevels->value());
  67. updateTemplateList();
  68. }
  69. }
  70. WindowNewMap::~WindowNewMap()
  71. {
  72. delete ui;
  73. }
  74. bool WindowNewMap::loadUserSettings()
  75. {
  76. bool ret = false;
  77. const CRmgTemplate * templ = nullptr;
  78. QSettings s = CLauncherDirs::getSettings(Ui::appName);
  79. auto generateRandom = s.value(newMapGenerateRandom);
  80. if (generateRandom.isValid())
  81. {
  82. ui->randomMapCheck->setChecked(generateRandom.toBool());
  83. }
  84. auto settings = s.value(newMapWindow);
  85. if (settings.isValid())
  86. {
  87. auto node = JsonUtils::toJson(settings);
  88. JsonDeserializer handler(nullptr, node);
  89. handler.serializeStruct("lastSettings", mapGenOptions);
  90. templ = mapGenOptions.getMapTemplate(); // Remember for later
  91. ui->widthTxt->setValue(mapGenOptions.getWidth());
  92. ui->heightTxt->setValue(mapGenOptions.getHeight());
  93. for(const auto & sz : mapSizes)
  94. {
  95. if(sz.second.first == mapGenOptions.getWidth() &&
  96. sz.second.second == mapGenOptions.getHeight())
  97. {
  98. ui->sizeCombo->setCurrentIndex(sz.first);
  99. break;
  100. }
  101. }
  102. ui->spinBoxLevels->setValue(mapGenOptions.getLevels());
  103. ui->humanCombo->setCurrentIndex(mapGenOptions.getHumanOrCpuPlayerCount());
  104. ui->cpuCombo->setCurrentIndex(mapGenOptions.getCompOnlyPlayerCount());
  105. ui->humanTeamsCombo->setCurrentIndex(mapGenOptions.getTeamCount());
  106. ui->cpuTeamsCombo->setCurrentIndex(mapGenOptions.getCompOnlyTeamCount());
  107. switch (mapGenOptions.getWaterContent())
  108. {
  109. case EWaterContent::RANDOM:
  110. ui->waterOpt1->setChecked(true); break;
  111. case EWaterContent::NONE:
  112. ui->waterOpt2->setChecked(true); break;
  113. case EWaterContent::NORMAL:
  114. ui->waterOpt3->setChecked(true); break;
  115. case EWaterContent::ISLANDS:
  116. ui->waterOpt4->setChecked(true); break;
  117. }
  118. switch (mapGenOptions.getMonsterStrength())
  119. {
  120. case EMonsterStrength::RANDOM:
  121. ui->monsterOpt1->setChecked(true); break;
  122. case EMonsterStrength::GLOBAL_WEAK:
  123. ui->monsterOpt2->setChecked(true); break;
  124. case EMonsterStrength::GLOBAL_NORMAL:
  125. ui->monsterOpt3->setChecked(true); break;
  126. case EMonsterStrength::GLOBAL_STRONG:
  127. ui->monsterOpt4->setChecked(true); break;
  128. }
  129. ui->roadDirt->setChecked(mapGenOptions.isRoadEnabled(Road::DIRT_ROAD));
  130. ui->roadGravel->setChecked(mapGenOptions.isRoadEnabled(Road::GRAVEL_ROAD));
  131. ui->roadCobblestone->setChecked(mapGenOptions.isRoadEnabled(Road::COBBLESTONE_ROAD));
  132. ret = true;
  133. }
  134. updateTemplateList();
  135. mapGenOptions.setMapTemplate(templ); // Can be null
  136. if (templ)
  137. {
  138. std::string name = templ->getName();
  139. for (size_t i = 0; i < ui->templateCombo->count(); i++)
  140. {
  141. if (ui->templateCombo->itemText(i).toStdString() == name)
  142. {
  143. ui->templateCombo->setCurrentIndex(i);
  144. break;
  145. }
  146. }
  147. ret = true;
  148. }
  149. return ret;
  150. }
  151. void WindowNewMap::saveUserSettings()
  152. {
  153. QSettings s = CLauncherDirs::getSettings(Ui::appName);
  154. JsonNode data;
  155. JsonSerializer ser(nullptr, data);
  156. ser.serializeStruct("lastSettings", mapGenOptions);
  157. auto variant = JsonUtils::toVariant(data);
  158. s.setValue(newMapWindow, variant);
  159. s.setValue(newMapGenerateRandom, ui->randomMapCheck->isChecked());
  160. }
  161. void WindowNewMap::on_cancelButton_clicked()
  162. {
  163. saveUserSettings();
  164. close();
  165. }
  166. void generateRandomMap(CMapGenerator & gen, MainWindow * window)
  167. {
  168. window->controller.setMap(gen.generate());
  169. }
  170. std::unique_ptr<CMap> generateEmptyMap(CMapGenOptions & options)
  171. {
  172. auto map = std::make_unique<CMap>(nullptr);
  173. map->version = EMapFormat::VCMI;
  174. map->creationDateTime = std::time(nullptr);
  175. map->width = options.getWidth();
  176. map->height = options.getHeight();
  177. map->mapLayers.clear();
  178. for(int i = 0; i < options.getLevels(); i++)
  179. {
  180. if(i == 0)
  181. map->mapLayers.push_back(MapLayerId::SURFACE);
  182. else if(i == 1)
  183. map->mapLayers.push_back(MapLayerId::UNDERGROUND);
  184. else
  185. map->mapLayers.push_back(MapLayerId::UNKNOWN);
  186. }
  187. map->initTerrain();
  188. map->getEditManager()->clearTerrain(&CRandomGenerator::getDefault());
  189. return map;
  190. }
  191. std::pair<int, int> getSelectedMapSize(QComboBox* comboBox, const std::map<int, std::pair<int, int>>& mapSizes) {
  192. int selectedIndex = comboBox->currentIndex();
  193. auto it = mapSizes.find(selectedIndex);
  194. if (it != mapSizes.end()) {
  195. return it->second; // Return the width and height pair
  196. }
  197. return { 0, 0 };
  198. }
  199. void WindowNewMap::on_okButton_clicked()
  200. {
  201. EWaterContent::EWaterContent water = EWaterContent::RANDOM;
  202. EMonsterStrength::EMonsterStrength monster = EMonsterStrength::RANDOM;
  203. if(ui->waterOpt1->isChecked())
  204. water = EWaterContent::RANDOM;
  205. if(ui->waterOpt2->isChecked())
  206. water = EWaterContent::NONE;
  207. if(ui->waterOpt3->isChecked())
  208. water = EWaterContent::NORMAL;
  209. if(ui->waterOpt4->isChecked())
  210. water = EWaterContent::ISLANDS;
  211. if(ui->monsterOpt1->isChecked())
  212. monster = EMonsterStrength::RANDOM;
  213. if(ui->monsterOpt2->isChecked())
  214. monster = EMonsterStrength::GLOBAL_WEAK;
  215. if(ui->monsterOpt3->isChecked())
  216. monster = EMonsterStrength::GLOBAL_NORMAL;
  217. if(ui->monsterOpt4->isChecked())
  218. monster = EMonsterStrength::GLOBAL_STRONG;
  219. mapGenOptions.setWaterContent(water);
  220. mapGenOptions.setMonsterStrength(monster);
  221. mapGenOptions.setRoadEnabled(Road::DIRT_ROAD, ui->roadDirt->isChecked());
  222. mapGenOptions.setRoadEnabled(Road::GRAVEL_ROAD, ui->roadGravel->isChecked());
  223. mapGenOptions.setRoadEnabled(Road::COBBLESTONE_ROAD, ui->roadCobblestone->isChecked());
  224. if(ui->sizeStandardRadio->isChecked())
  225. {
  226. auto size = getSelectedMapSize(ui->sizeCombo, mapSizes);
  227. mapGenOptions.setWidth(size.first);
  228. mapGenOptions.setHeight(size.second);
  229. }
  230. else
  231. {
  232. mapGenOptions.setWidth(ui->widthTxt->value());
  233. mapGenOptions.setHeight(ui->heightTxt->value());
  234. }
  235. saveUserSettings();
  236. std::unique_ptr<CMap> nmap;
  237. auto & mapController = static_cast<MainWindow *>(parent())->controller;
  238. if(ui->randomMapCheck->isChecked())
  239. {
  240. //verify map template
  241. if(mapGenOptions.getPossibleTemplates().empty())
  242. {
  243. QMessageBox::warning(this, tr("No template"), tr("No template for parameters specified. Random map cannot be generated."));
  244. return;
  245. }
  246. hide();
  247. int seed = std::time(nullptr);
  248. if(ui->checkSeed->isChecked() && ui->lineSeed->value() != 0)
  249. seed = ui->lineSeed->value();
  250. CMapGenerator generator(mapGenOptions, mapController.getCallback(), seed);
  251. auto progressBarWnd = new GeneratorProgress(generator, this);
  252. progressBarWnd->show();
  253. try
  254. {
  255. auto f = std::async(std::launch::async, &CMapGenerator::generate, &generator);
  256. progressBarWnd->update();
  257. nmap = f.get();
  258. }
  259. catch(const std::exception & e)
  260. {
  261. QMessageBox::critical(this, tr("RMG failure"), e.what());
  262. }
  263. }
  264. else
  265. {
  266. auto f = std::async(std::launch::async, &::generateEmptyMap, std::ref(mapGenOptions));
  267. nmap = f.get();
  268. }
  269. nmap->mods = MapController::modAssessmentMap(*nmap);
  270. mapController.setMap(std::move(nmap));
  271. static_cast<MainWindow *>(parent())->initializeMap(true);
  272. close();
  273. }
  274. void WindowNewMap::on_sizeCombo_activated(int index)
  275. {
  276. auto size = getSelectedMapSize(ui->sizeCombo, mapSizes);
  277. mapGenOptions.setWidth(size.first);
  278. mapGenOptions.setHeight(size.second);
  279. updateTemplateList();
  280. }
  281. void WindowNewMap::on_spinBoxLevels_valueChanged(int value)
  282. {
  283. if(value > 2)
  284. QMessageBox::warning(this, tr("Multilevel support"), tr("Multilevel support is highly experimental yet. Expect issues.")); // TODO: multilevel support
  285. mapGenOptions.setLevels(ui->spinBoxLevels->value());
  286. updateTemplateList();
  287. }
  288. void WindowNewMap::on_humanCombo_activated(int index)
  289. {
  290. int humans = ui->humanCombo->currentData().toInt();
  291. if(humans > PlayerColor::PLAYER_LIMIT_I)
  292. {
  293. humans = PlayerColor::PLAYER_LIMIT_I;
  294. ui->humanCombo->setCurrentIndex(humans);
  295. }
  296. int teams = mapGenOptions.getTeamCount();
  297. if(teams > humans - 1)
  298. {
  299. teams = humans > 0 ? humans - 1 : CMapGenOptions::RANDOM_SIZE;
  300. ui->humanTeamsCombo->setCurrentIndex(teams + 1); //skip one element because first is random
  301. }
  302. int cpu = mapGenOptions.getCompOnlyPlayerCount();
  303. if(cpu > PlayerColor::PLAYER_LIMIT_I - humans)
  304. {
  305. cpu = PlayerColor::PLAYER_LIMIT_I - humans;
  306. ui->cpuCombo->setCurrentIndex(cpu + 1); //skip one element because first is random
  307. }
  308. int cpuTeams = mapGenOptions.getCompOnlyTeamCount(); //comp only players - 1
  309. if(cpuTeams > cpu - 1)
  310. {
  311. cpuTeams = cpu > 0 ? cpu - 1 : CMapGenOptions::RANDOM_SIZE;
  312. ui->cpuTeamsCombo->setCurrentIndex(cpuTeams + 1); //skip one element because first is random
  313. }
  314. mapGenOptions.setHumanOrCpuPlayerCount(humans);
  315. updateTemplateList();
  316. }
  317. void WindowNewMap::on_cpuCombo_activated(int index)
  318. {
  319. int humans = mapGenOptions.getHumanOrCpuPlayerCount();
  320. int cpu = ui->cpuCombo->currentData().toInt();
  321. // FIXME: Use mapGenOption method only to calculate actual number of players for current template
  322. if(cpu > PlayerColor::PLAYER_LIMIT_I - humans)
  323. {
  324. cpu = PlayerColor::PLAYER_LIMIT_I - humans;
  325. ui->cpuCombo->setCurrentIndex(cpu + 1); //skip one element because first is random
  326. }
  327. int cpuTeams = mapGenOptions.getCompOnlyTeamCount(); //comp only players - 1
  328. if(cpuTeams > cpu - 1)
  329. {
  330. cpuTeams = cpu > 0 ? cpu - 1 : CMapGenOptions::RANDOM_SIZE;
  331. ui->cpuTeamsCombo->setCurrentIndex(cpuTeams + 1); //skip one element because first is random
  332. }
  333. mapGenOptions.setCompOnlyPlayerCount(cpu);
  334. updateTemplateList();
  335. }
  336. void WindowNewMap::on_randomMapCheck_stateChanged(int arg1)
  337. {
  338. randomMap = ui->randomMapCheck->isChecked();
  339. ui->randomOptions->setEnabled(randomMap);
  340. updateTemplateList();
  341. }
  342. void WindowNewMap::on_templateCombo_activated(int index)
  343. {
  344. if(index == 0)
  345. {
  346. mapGenOptions.setMapTemplate(nullptr);
  347. return;
  348. }
  349. auto * templ = data_cast<const CRmgTemplate>(ui->templateCombo->currentData().toLongLong());
  350. mapGenOptions.setMapTemplate(templ);
  351. }
  352. void WindowNewMap::on_widthTxt_valueChanged(int value)
  353. {
  354. if(value > 1)
  355. {
  356. mapGenOptions.setWidth(value);
  357. updateTemplateList();
  358. }
  359. }
  360. void WindowNewMap::on_heightTxt_valueChanged(int value)
  361. {
  362. if(value > 1)
  363. {
  364. mapGenOptions.setHeight(value);
  365. updateTemplateList();
  366. }
  367. }
  368. void WindowNewMap::updateTemplateList()
  369. {
  370. ui->templateCombo->clear();
  371. ui->templateCombo->setCurrentIndex(-1);
  372. if(!randomMap)
  373. return;
  374. mapGenOptions.setMapTemplate(nullptr);
  375. auto templates = mapGenOptions.getPossibleTemplates();
  376. if(templates.empty())
  377. return;
  378. ui->templateCombo->addItem(tr("[default]"), 0);
  379. for(auto * templ : templates)
  380. {
  381. ui->templateCombo->addItem(QString::fromStdString(templ->getName()), data_cast(templ));
  382. }
  383. ui->templateCombo->setCurrentIndex(0);
  384. }
  385. void WindowNewMap::on_checkSeed_toggled(bool checked)
  386. {
  387. ui->lineSeed->setEnabled(checked);
  388. }
  389. void WindowNewMap::on_humanTeamsCombo_activated(int index)
  390. {
  391. int humans = mapGenOptions.getHumanOrCpuPlayerCount();
  392. int teams = ui->humanTeamsCombo->currentData().toInt();
  393. if(teams >= humans)
  394. {
  395. teams = humans > 0 ? humans - 1 : CMapGenOptions::RANDOM_SIZE;
  396. ui->humanTeamsCombo->setCurrentIndex(teams + 1); //skip one element because first is random
  397. }
  398. mapGenOptions.setTeamCount(teams);
  399. updateTemplateList();
  400. }
  401. void WindowNewMap::on_cpuTeamsCombo_activated(int index)
  402. {
  403. int cpu = mapGenOptions.getCompOnlyPlayerCount();
  404. int cpuTeams = ui->cpuTeamsCombo->currentData().toInt();
  405. if(cpuTeams >= cpu)
  406. {
  407. cpuTeams = cpu > 0 ? cpu - 1 : CMapGenOptions::RANDOM_SIZE;
  408. ui->cpuTeamsCombo->setCurrentIndex(cpuTeams + 1); //skip one element because first is random
  409. }
  410. mapGenOptions.setCompOnlyTeamCount(cpuTeams);
  411. updateTemplateList();
  412. }
  413. void WindowNewMap::on_sizeStandardRadio_toggled(bool checked)
  414. {
  415. if (checked) {
  416. ui->sizeGroup1->setEnabled(true);
  417. ui->sizeGroup2->setEnabled(false);
  418. }
  419. updateTemplateList();
  420. }
  421. void WindowNewMap::on_sizeCustomRadio_toggled(bool checked)
  422. {
  423. if (checked) {
  424. ui->sizeGroup1->setEnabled(false);
  425. ui->sizeGroup2->setEnabled(true);
  426. }
  427. updateTemplateList();
  428. }