windownewmap.cpp 10 KB

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