windownewmap.cpp 13 KB

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