windownewmap.cpp 14 KB

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