windownewmap.cpp 13 KB

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