windownewmap.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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/VCMI_Lib.h"
  16. #include "../lib/mapping/CMapEditManager.h"
  17. #include "../lib/CGeneralTextHandler.h"
  18. #include "windownewmap.h"
  19. #include "ui_windownewmap.h"
  20. #include "mainwindow.h"
  21. #include "generatorprogress.h"
  22. WindowNewMap::WindowNewMap(QWidget *parent) :
  23. QDialog(parent),
  24. ui(new Ui::WindowNewMap)
  25. {
  26. ui->setupUi(this);
  27. setAttribute(Qt::WA_DeleteOnClose);
  28. setWindowModality(Qt::ApplicationModal);
  29. for(auto * combo : {ui->humanCombo, ui->cpuCombo, ui->humanTeamsCombo, ui->cpuTeamsCombo})
  30. combo->clear();
  31. //prepare human players combo box
  32. for(int i = 0; i <= PlayerColor::PLAYER_LIMIT_I; ++i)
  33. {
  34. ui->humanCombo->addItem(!i ? randomString : QString::number(players.at(i)));
  35. ui->humanCombo->setItemData(i, QVariant(players.at(i)));
  36. ui->cpuCombo->addItem(!i ? randomString : QString::number(cpuPlayers.at(i)));
  37. ui->cpuCombo->setItemData(i, QVariant(cpuPlayers.at(i)));
  38. ui->humanTeamsCombo->addItem(!i ? randomString : QString::number(cpuPlayers.at(i)));
  39. ui->humanTeamsCombo->setItemData(i, QVariant(cpuPlayers.at(i)));
  40. ui->cpuTeamsCombo->addItem(!i ? randomString : QString::number(cpuPlayers.at(i)));
  41. ui->cpuTeamsCombo->setItemData(i, QVariant(cpuPlayers.at(i)));
  42. }
  43. for(auto * combo : {ui->humanCombo, ui->cpuCombo, ui->humanTeamsCombo, ui->cpuTeamsCombo})
  44. combo->setCurrentIndex(0);
  45. loadUserSettings();
  46. show();
  47. //setup initial parameters
  48. int width = ui->widthTxt->text().toInt();
  49. int height = ui->heightTxt->text().toInt();
  50. mapGenOptions.setWidth(width ? width : 1);
  51. mapGenOptions.setHeight(height ? height : 1);
  52. bool twoLevel = ui->twoLevelCheck->isChecked();
  53. mapGenOptions.setHasTwoLevels(twoLevel);
  54. updateTemplateList();
  55. }
  56. WindowNewMap::~WindowNewMap()
  57. {
  58. saveUserSettings();
  59. delete ui;
  60. }
  61. void WindowNewMap::loadUserSettings()
  62. {
  63. //load window settings
  64. QSettings s(Ui::teamName, Ui::appName);
  65. auto width = s.value(newMapWidth);
  66. if (width.isValid())
  67. {
  68. ui->widthTxt->setText(width.toString());
  69. }
  70. auto height = s.value(newMapHeight);
  71. if (height.isValid())
  72. {
  73. ui->heightTxt->setText(height.toString());
  74. }
  75. auto twoLevel = s.value(newMapTwoLevel);
  76. if (twoLevel.isValid())
  77. {
  78. ui->twoLevelCheck->setChecked(twoLevel.toBool());
  79. }
  80. auto generateRandom = s.value(newMapGenerateRandom);
  81. if (generateRandom.isValid())
  82. {
  83. ui->randomMapCheck->setChecked(generateRandom.toBool());
  84. }
  85. auto players = s.value(newMapPlayers);
  86. if (players.isValid())
  87. {
  88. ui->humanCombo->setCurrentIndex(players.toInt());
  89. }
  90. auto cpuPlayers = s.value(newMapCpuPlayers);
  91. if (cpuPlayers.isValid())
  92. {
  93. ui->cpuCombo->setCurrentIndex(cpuPlayers.toInt());
  94. }
  95. auto teams = s.value(newMapHumanTeams);
  96. if(teams.isValid())
  97. {
  98. ui->humanTeamsCombo->setCurrentIndex(teams.toInt());
  99. }
  100. auto cputeams = s.value(newMapCpuTeams);
  101. if(cputeams.isValid())
  102. {
  103. ui->cpuTeamsCombo->setCurrentIndex(cputeams.toInt());
  104. }
  105. auto waterContent = s.value(newMapWaterContent);
  106. if (waterContent.isValid())
  107. {
  108. switch (waterContent.toInt())
  109. {
  110. case EWaterContent::RANDOM:
  111. ui->waterOpt1->setChecked(true); break;
  112. case EWaterContent::NONE:
  113. ui->waterOpt2->setChecked(true); break;
  114. case EWaterContent::NORMAL:
  115. ui->waterOpt3->setChecked(true); break;
  116. case EWaterContent::ISLANDS:
  117. ui->waterOpt4->setChecked(true); break;
  118. }
  119. }
  120. auto monsterStrength = s.value(newMapMonsterStrength);
  121. if (monsterStrength.isValid())
  122. {
  123. switch (monsterStrength.toInt())
  124. {
  125. case EMonsterStrength::RANDOM:
  126. ui->monsterOpt1->setChecked(true); break;
  127. case EMonsterStrength::GLOBAL_WEAK:
  128. ui->monsterOpt2->setChecked(true); break;
  129. case EMonsterStrength::GLOBAL_NORMAL:
  130. ui->monsterOpt3->setChecked(true); break;
  131. case EMonsterStrength::GLOBAL_STRONG:
  132. ui->monsterOpt4->setChecked(true); break;
  133. }
  134. }
  135. auto templateName = s.value(newMapTemplate);
  136. if (templateName.isValid())
  137. {
  138. updateTemplateList();
  139. auto* templ = VLC->tplh->getTemplate(templateName.toString().toStdString());
  140. if (templ)
  141. {
  142. ui->templateCombo->setCurrentText(templateName.toString());
  143. //TODO: validate inside this method
  144. mapGenOptions.setMapTemplate(templ);
  145. }
  146. else
  147. {
  148. //Display problem on status bar
  149. }
  150. }
  151. }
  152. void WindowNewMap::saveUserSettings()
  153. {
  154. QSettings s(Ui::teamName, Ui::appName);
  155. s.setValue(newMapWidth, ui->widthTxt->text().toInt());
  156. s.setValue(newMapHeight, ui->heightTxt->text().toInt());
  157. s.setValue(newMapTwoLevel, ui->twoLevelCheck->isChecked());
  158. s.setValue(newMapGenerateRandom, ui->randomMapCheck->isChecked());
  159. s.setValue(newMapPlayers,ui->humanCombo->currentIndex());
  160. s.setValue(newMapCpuPlayers,ui->cpuCombo->currentIndex());
  161. s.setValue(newMapHumanTeams, ui->humanTeamsCombo->currentIndex());
  162. s.setValue(newMapCpuTeams, ui->cpuTeamsCombo->currentIndex());
  163. EWaterContent::EWaterContent water = EWaterContent::RANDOM;
  164. if(ui->waterOpt1->isChecked())
  165. water = EWaterContent::RANDOM;
  166. else if(ui->waterOpt2->isChecked())
  167. water = EWaterContent::NONE;
  168. else if(ui->waterOpt3->isChecked())
  169. water = EWaterContent::NORMAL;
  170. else if(ui->waterOpt4->isChecked())
  171. water = EWaterContent::ISLANDS;
  172. s.setValue(newMapWaterContent, static_cast<int>(water));
  173. EMonsterStrength::EMonsterStrength monster = EMonsterStrength::RANDOM;
  174. if(ui->monsterOpt1->isChecked())
  175. monster = EMonsterStrength::RANDOM;
  176. else if(ui->monsterOpt2->isChecked())
  177. monster = EMonsterStrength::GLOBAL_WEAK;
  178. else if(ui->monsterOpt3->isChecked())
  179. monster = EMonsterStrength::GLOBAL_NORMAL;
  180. else if(ui->monsterOpt4->isChecked())
  181. monster = EMonsterStrength::GLOBAL_STRONG;
  182. s.setValue(newMapMonsterStrength, static_cast<int>(monster));
  183. auto templateName = ui->templateCombo->currentText();
  184. if (templateName.size())
  185. {
  186. s.setValue(newMapTemplate, templateName);
  187. }
  188. }
  189. void WindowNewMap::on_cancelButton_clicked()
  190. {
  191. close();
  192. }
  193. void generateRandomMap(CMapGenerator & gen, MainWindow * window)
  194. {
  195. window->controller.setMap(gen.generate());
  196. }
  197. std::unique_ptr<CMap> generateEmptyMap(CMapGenOptions & options)
  198. {
  199. std::unique_ptr<CMap> map(new CMap);
  200. map->version = EMapFormat::VCMI;
  201. map->width = options.getWidth();
  202. map->height = options.getHeight();
  203. map->twoLevel = options.getHasTwoLevels();
  204. map->initTerrain();
  205. map->getEditManager()->clearTerrain(&CRandomGenerator::getDefault());
  206. return map;
  207. }
  208. void WindowNewMap::on_okButton_clicked()
  209. {
  210. EWaterContent::EWaterContent water = EWaterContent::RANDOM;
  211. EMonsterStrength::EMonsterStrength monster = EMonsterStrength::RANDOM;
  212. if(ui->waterOpt1->isChecked())
  213. water = EWaterContent::RANDOM;
  214. if(ui->waterOpt2->isChecked())
  215. water = EWaterContent::NONE;
  216. if(ui->waterOpt3->isChecked())
  217. water = EWaterContent::NORMAL;
  218. if(ui->waterOpt4->isChecked())
  219. water = EWaterContent::ISLANDS;
  220. if(ui->monsterOpt1->isChecked())
  221. monster = EMonsterStrength::RANDOM;
  222. if(ui->monsterOpt2->isChecked())
  223. monster = EMonsterStrength::GLOBAL_WEAK;
  224. if(ui->monsterOpt3->isChecked())
  225. monster = EMonsterStrength::GLOBAL_NORMAL;
  226. if(ui->monsterOpt4->isChecked())
  227. monster = EMonsterStrength::GLOBAL_STRONG;
  228. mapGenOptions.setWaterContent(water);
  229. mapGenOptions.setMonsterStrength(monster);
  230. std::unique_ptr<CMap> nmap;
  231. if(ui->randomMapCheck->isChecked())
  232. {
  233. //verify map template
  234. if(mapGenOptions.getPossibleTemplates().empty())
  235. {
  236. QMessageBox::warning(this, "No template", "No template for parameters scecified. Random map cannot be generated.");
  237. return;
  238. }
  239. int seed = std::time(nullptr);
  240. if(ui->checkSeed->isChecked() && !ui->lineSeed->text().isEmpty())
  241. seed = ui->lineSeed->text().toInt();
  242. CMapGenerator generator(mapGenOptions, seed);
  243. auto progressBarWnd = new GeneratorProgress(generator, this);
  244. progressBarWnd->show();
  245. try
  246. {
  247. auto f = std::async(std::launch::async, &CMapGenerator::generate, &generator);
  248. progressBarWnd->update();
  249. nmap = f.get();
  250. }
  251. catch(const std::exception & e)
  252. {
  253. QMessageBox::critical(this, "RMG failure", e.what());
  254. }
  255. }
  256. else
  257. {
  258. auto f = std::async(std::launch::async, &::generateEmptyMap, std::ref(mapGenOptions));
  259. nmap = f.get();
  260. }
  261. nmap->mods = MapController::modAssessmentAll();
  262. static_cast<MainWindow*>(parent())->controller.setMap(std::move(nmap));
  263. static_cast<MainWindow*>(parent())->initializeMap(true);
  264. close();
  265. }
  266. void WindowNewMap::on_sizeCombo_activated(int index)
  267. {
  268. ui->widthTxt->setText(QString::number(mapSizes.at(index).first));
  269. ui->heightTxt->setText(QString::number(mapSizes.at(index).second));
  270. }
  271. void WindowNewMap::on_twoLevelCheck_stateChanged(int arg1)
  272. {
  273. bool twoLevel = ui->twoLevelCheck->isChecked();
  274. mapGenOptions.setHasTwoLevels(twoLevel);
  275. updateTemplateList();
  276. }
  277. void WindowNewMap::on_humanCombo_activated(int index)
  278. {
  279. int humans = ui->humanCombo->currentData().toInt();
  280. if(humans > PlayerColor::PLAYER_LIMIT_I)
  281. {
  282. humans = PlayerColor::PLAYER_LIMIT_I;
  283. ui->humanCombo->setCurrentIndex(humans);
  284. }
  285. mapGenOptions.setPlayerCount(humans);
  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. updateTemplateList();
  305. }
  306. void WindowNewMap::on_cpuCombo_activated(int index)
  307. {
  308. int humans = mapGenOptions.getPlayerCount();
  309. int cpu = ui->cpuCombo->currentData().toInt();
  310. if(cpu > PlayerColor::PLAYER_LIMIT_I - humans)
  311. {
  312. cpu = PlayerColor::PLAYER_LIMIT_I - humans;
  313. ui->cpuCombo->setCurrentIndex(cpu + 1); //skip one element because first is random
  314. }
  315. mapGenOptions.setCompOnlyPlayerCount(cpu);
  316. int cpuTeams = mapGenOptions.getCompOnlyTeamCount(); //comp only players - 1
  317. if(cpuTeams > cpu - 1)
  318. {
  319. cpuTeams = cpu > 0 ? cpu - 1 : CMapGenOptions::RANDOM_SIZE;
  320. ui->cpuTeamsCombo->setCurrentIndex(cpuTeams + 1); //skip one element because first is random
  321. }
  322. updateTemplateList();
  323. }
  324. void WindowNewMap::on_randomMapCheck_stateChanged(int arg1)
  325. {
  326. randomMap = ui->randomMapCheck->isChecked();
  327. ui->templateCombo->setEnabled(randomMap);
  328. updateTemplateList();
  329. }
  330. void WindowNewMap::on_templateCombo_activated(int index)
  331. {
  332. if(index == 0)
  333. {
  334. mapGenOptions.setMapTemplate(nullptr);
  335. return;
  336. }
  337. auto * templ = data_cast<const CRmgTemplate>(ui->templateCombo->currentData().toLongLong());
  338. mapGenOptions.setMapTemplate(templ);
  339. }
  340. void WindowNewMap::on_widthTxt_textChanged(const QString &arg1)
  341. {
  342. int sz = arg1.toInt();
  343. if(sz > 1)
  344. {
  345. mapGenOptions.setWidth(arg1.toInt());
  346. updateTemplateList();
  347. }
  348. }
  349. void WindowNewMap::on_heightTxt_textChanged(const QString &arg1)
  350. {
  351. int sz = arg1.toInt();
  352. if(sz > 1)
  353. {
  354. mapGenOptions.setHeight(arg1.toInt());
  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("[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.getPlayerCount();
  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. }