victoryconditions.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * victoryconditions.h, 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 "victoryconditions.h"
  12. #include "ui_victoryconditions.h"
  13. #include "../mapcontroller.h"
  14. #include "../lib/CGeneralTextHandler.h"
  15. #include "../lib/constants/StringConstants.h"
  16. #include "../../lib/mapObjects/CGCreature.h"
  17. #include "../inspector/townbulidingswidget.h" //to convert BuildingID to string
  18. VictoryConditions::VictoryConditions(QWidget *parent) :
  19. AbstractSettings(parent),
  20. ui(new Ui::VictoryConditions)
  21. {
  22. ui->setupUi(this);
  23. }
  24. void VictoryConditions::initialize(MapController & c)
  25. {
  26. AbstractSettings::initialize(c);
  27. //victory message
  28. ui->victoryMessageEdit->setText(QString::fromStdString(controller->map()->victoryMessage.toString()));
  29. //victory conditions
  30. const std::array<std::string, 9> conditionStringsWin = {
  31. QT_TR_NOOP("No special victory"),
  32. QT_TR_NOOP("Capture artifact"),
  33. QT_TR_NOOP("Hire creatures"),
  34. QT_TR_NOOP("Accumulate resources"),
  35. QT_TR_NOOP("Construct building"),
  36. QT_TR_NOOP("Capture town"),
  37. QT_TR_NOOP("Defeat hero"),
  38. QT_TR_NOOP("Transport artifact"),
  39. QT_TR_NOOP("Kill monster")
  40. };
  41. for(auto & s : conditionStringsWin)
  42. {
  43. ui->victoryComboBox->addItem(QString::fromStdString(s));
  44. }
  45. ui->standardVictoryCheck->setChecked(false);
  46. ui->onlyForHumansCheck->setChecked(false);
  47. for(auto & ev : controller->map()->triggeredEvents)
  48. {
  49. if(ev.effect.type == EventEffect::VICTORY)
  50. {
  51. if(ev.identifier == "standardVictory")
  52. ui->standardVictoryCheck->setChecked(true);
  53. if(ev.identifier == "specialVictory")
  54. {
  55. auto readjson = ev.trigger.toJson(AbstractSettings::conditionToJson);
  56. auto linearNodes = linearJsonArray(readjson);
  57. for(auto & json : linearNodes)
  58. {
  59. switch(json["condition"].Integer())
  60. {
  61. case EventCondition::HAVE_ARTIFACT: {
  62. ui->victoryComboBox->setCurrentIndex(1);
  63. assert(victoryTypeWidget);
  64. victoryTypeWidget->setCurrentIndex(json["objectType"].Integer());
  65. break;
  66. }
  67. case EventCondition::HAVE_CREATURES: {
  68. ui->victoryComboBox->setCurrentIndex(2);
  69. assert(victoryTypeWidget);
  70. assert(victoryValueWidget);
  71. auto idx = victoryTypeWidget->findData(int(json["objectType"].Integer()));
  72. victoryTypeWidget->setCurrentIndex(idx);
  73. victoryValueWidget->setText(QString::number(json["value"].Integer()));
  74. break;
  75. }
  76. case EventCondition::HAVE_RESOURCES: {
  77. ui->victoryComboBox->setCurrentIndex(3);
  78. assert(victoryTypeWidget);
  79. assert(victoryValueWidget);
  80. auto idx = victoryTypeWidget->findData(int(json["objectType"].Integer()));
  81. victoryTypeWidget->setCurrentIndex(idx);
  82. victoryValueWidget->setText(QString::number(json["value"].Integer()));
  83. break;
  84. }
  85. case EventCondition::HAVE_BUILDING: {
  86. ui->victoryComboBox->setCurrentIndex(4);
  87. assert(victoryTypeWidget);
  88. assert(victorySelectWidget);
  89. auto idx = victoryTypeWidget->findData(int(json["objectType"].Integer()));
  90. victoryTypeWidget->setCurrentIndex(idx);
  91. int townIdx = getObjectByPos<const CGTownInstance>(*controller->map(), posFromJson(json["position"]));
  92. if(townIdx >= 0)
  93. {
  94. auto idx = victorySelectWidget->findData(townIdx);
  95. victorySelectWidget->setCurrentIndex(idx);
  96. }
  97. break;
  98. }
  99. case EventCondition::CONTROL: {
  100. ui->victoryComboBox->setCurrentIndex(5);
  101. assert(victoryTypeWidget);
  102. if(json["objectType"].Integer() == Obj::TOWN)
  103. {
  104. int townIdx = getObjectByPos<const CGTownInstance>(*controller->map(), posFromJson(json["position"]));
  105. if(townIdx >= 0)
  106. {
  107. auto idx = victoryTypeWidget->findData(townIdx);
  108. victoryTypeWidget->setCurrentIndex(idx);
  109. }
  110. }
  111. //TODO: support control other objects (dwellings, mines)
  112. break;
  113. }
  114. case EventCondition::DESTROY: {
  115. if(json["objectType"].Integer() == Obj::HERO)
  116. {
  117. ui->victoryComboBox->setCurrentIndex(6);
  118. assert(victoryTypeWidget);
  119. int heroIdx = getObjectByPos<const CGHeroInstance>(*controller->map(), posFromJson(json["position"]));
  120. if(heroIdx >= 0)
  121. {
  122. auto idx = victoryTypeWidget->findData(heroIdx);
  123. victoryTypeWidget->setCurrentIndex(idx);
  124. }
  125. }
  126. if(json["objectType"].Integer() == Obj::MONSTER)
  127. {
  128. ui->victoryComboBox->setCurrentIndex(8);
  129. assert(victoryTypeWidget);
  130. int monsterIdx = getObjectByPos<const CGCreature>(*controller->map(), posFromJson(json["position"]));
  131. if(monsterIdx >= 0)
  132. {
  133. auto idx = victoryTypeWidget->findData(monsterIdx);
  134. victoryTypeWidget->setCurrentIndex(idx);
  135. }
  136. }
  137. break;
  138. }
  139. case EventCondition::TRANSPORT: {
  140. ui->victoryComboBox->setCurrentIndex(7);
  141. assert(victoryTypeWidget);
  142. assert(victorySelectWidget);
  143. victoryTypeWidget->setCurrentIndex(json["objectType"].Integer());
  144. int townIdx = getObjectByPos<const CGTownInstance>(*controller->map(), posFromJson(json["position"]));
  145. if(townIdx >= 0)
  146. {
  147. auto idx = victorySelectWidget->findData(townIdx);
  148. victorySelectWidget->setCurrentIndex(idx);
  149. }
  150. break;
  151. }
  152. case EventCondition::IS_HUMAN: {
  153. ui->onlyForHumansCheck->setChecked(true);
  154. break;
  155. }
  156. };
  157. }
  158. }
  159. }
  160. }
  161. }
  162. void VictoryConditions::update()
  163. {
  164. //victory messages
  165. controller->map()->victoryMessage = MetaString::createFromRawString(ui->victoryMessageEdit->text().toStdString());
  166. //victory conditions
  167. EventCondition victoryCondition(EventCondition::STANDARD_WIN);
  168. //Victory condition - defeat all
  169. TriggeredEvent standardVictory;
  170. standardVictory.effect.type = EventEffect::VICTORY;
  171. standardVictory.effect.toOtherMessage.appendTextID("core.genrltxt.5");
  172. standardVictory.identifier = "standardVictory";
  173. standardVictory.description.clear(); // TODO: display in quest window
  174. standardVictory.onFulfill.appendTextID("core.genrltxt.659");
  175. standardVictory.trigger = EventExpression(victoryCondition);
  176. //VICTORY
  177. if(ui->victoryComboBox->currentIndex() == 0)
  178. {
  179. controller->map()->triggeredEvents.push_back(standardVictory);
  180. controller->map()->victoryIconIndex = 11;
  181. controller->map()->victoryMessage = MetaString::createFromTextID("core.vcdesc.0");
  182. }
  183. else
  184. {
  185. int vicCondition = ui->victoryComboBox->currentIndex() - 1;
  186. TriggeredEvent specialVictory;
  187. specialVictory.effect.type = EventEffect::VICTORY;
  188. specialVictory.identifier = "specialVictory";
  189. specialVictory.description.clear(); // TODO: display in quest window
  190. controller->map()->victoryIconIndex = vicCondition;
  191. controller->map()->victoryMessage = MetaString::createFromTextID("core.vcdesc." + std::to_string(vicCondition + 1));
  192. switch(vicCondition)
  193. {
  194. case 0: {
  195. EventCondition cond(EventCondition::HAVE_ARTIFACT);
  196. assert(victoryTypeWidget);
  197. cond.objectType = victoryTypeWidget->currentData().toInt();
  198. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.281");
  199. specialVictory.onFulfill.appendTextID("core.genrltxt.280");
  200. specialVictory.trigger = EventExpression(cond);
  201. break;
  202. }
  203. case 1: {
  204. EventCondition cond(EventCondition::HAVE_CREATURES);
  205. assert(victoryTypeWidget);
  206. cond.objectType = victoryTypeWidget->currentData().toInt();
  207. cond.value = victoryValueWidget->text().toInt();
  208. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.277");
  209. specialVictory.onFulfill.appendTextID("core.genrltxt.276");
  210. specialVictory.trigger = EventExpression(cond);
  211. break;
  212. }
  213. case 2: {
  214. EventCondition cond(EventCondition::HAVE_RESOURCES);
  215. assert(victoryTypeWidget);
  216. cond.objectType = victoryTypeWidget->currentData().toInt();
  217. cond.value = victoryValueWidget->text().toInt();
  218. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.279");
  219. specialVictory.onFulfill.appendTextID("core.genrltxt.278");
  220. specialVictory.trigger = EventExpression(cond);
  221. break;
  222. }
  223. case 3: {
  224. EventCondition cond(EventCondition::HAVE_BUILDING);
  225. assert(victoryTypeWidget);
  226. cond.objectType = victoryTypeWidget->currentData().toInt();
  227. int townIdx = victorySelectWidget->currentData().toInt();
  228. if(townIdx > -1)
  229. cond.position = controller->map()->objects[townIdx]->pos;
  230. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.283");
  231. specialVictory.onFulfill.appendTextID("core.genrltxt.282");
  232. specialVictory.trigger = EventExpression(cond);
  233. break;
  234. }
  235. case 4: {
  236. EventCondition cond(EventCondition::CONTROL);
  237. assert(victoryTypeWidget);
  238. cond.objectType = Obj::TOWN;
  239. int townIdx = victoryTypeWidget->currentData().toInt();
  240. cond.position = controller->map()->objects[townIdx]->pos;
  241. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.250");
  242. specialVictory.onFulfill.appendTextID("core.genrltxt.249");
  243. specialVictory.trigger = EventExpression(cond);
  244. break;
  245. }
  246. case 5: {
  247. EventCondition cond(EventCondition::DESTROY);
  248. assert(victoryTypeWidget);
  249. cond.objectType = Obj::HERO;
  250. int heroIdx = victoryTypeWidget->currentData().toInt();
  251. cond.position = controller->map()->objects[heroIdx]->pos;
  252. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.253");
  253. specialVictory.onFulfill.appendTextID("core.genrltxt.252");
  254. specialVictory.trigger = EventExpression(cond);
  255. break;
  256. }
  257. case 6: {
  258. EventCondition cond(EventCondition::TRANSPORT);
  259. assert(victoryTypeWidget);
  260. cond.objectType = victoryTypeWidget->currentData().toInt();
  261. int townIdx = victorySelectWidget->currentData().toInt();
  262. if(townIdx > -1)
  263. cond.position = controller->map()->objects[townIdx]->pos;
  264. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.293");
  265. specialVictory.onFulfill.appendTextID("core.genrltxt.292");
  266. specialVictory.trigger = EventExpression(cond);
  267. break;
  268. }
  269. case 7: {
  270. EventCondition cond(EventCondition::DESTROY);
  271. assert(victoryTypeWidget);
  272. cond.objectType = Obj::MONSTER;
  273. int monsterIdx = victoryTypeWidget->currentData().toInt();
  274. cond.position = controller->map()->objects[monsterIdx]->pos;
  275. specialVictory.effect.toOtherMessage.appendTextID("core.genrltxt.287");
  276. specialVictory.onFulfill.appendTextID("core.genrltxt.286");
  277. specialVictory.trigger = EventExpression(cond);
  278. break;
  279. }
  280. }
  281. // if condition is human-only turn it into following construction: AllOf(human, condition)
  282. if(ui->onlyForHumansCheck->isChecked())
  283. {
  284. EventExpression::OperatorAll oper;
  285. EventCondition notAI(EventCondition::IS_HUMAN);
  286. notAI.value = 1;
  287. oper.expressions.push_back(notAI);
  288. oper.expressions.push_back(specialVictory.trigger.get());
  289. specialVictory.trigger = EventExpression(oper);
  290. }
  291. // if normal victory allowed - add one more quest
  292. if(ui->standardVictoryCheck->isChecked())
  293. {
  294. controller->map()->victoryMessage.appendRawString(" / ");
  295. controller->map()->victoryMessage.appendTextID("core.vcdesc.0");
  296. controller->map()->triggeredEvents.push_back(standardVictory);
  297. }
  298. controller->map()->triggeredEvents.push_back(specialVictory);
  299. }
  300. }
  301. VictoryConditions::~VictoryConditions()
  302. {
  303. delete ui;
  304. }
  305. void VictoryConditions::on_victoryComboBox_currentIndexChanged(int index)
  306. {
  307. delete victoryTypeWidget;
  308. delete victoryValueWidget;
  309. delete victorySelectWidget;
  310. delete pickObjectButton;
  311. victoryTypeWidget = nullptr;
  312. victoryValueWidget = nullptr;
  313. victorySelectWidget = nullptr;
  314. pickObjectButton = nullptr;
  315. if(index == 0)
  316. {
  317. ui->standardVictoryCheck->setChecked(true);
  318. ui->standardVictoryCheck->setEnabled(false);
  319. ui->onlyForHumansCheck->setChecked(false);
  320. ui->onlyForHumansCheck->setEnabled(false);
  321. return;
  322. }
  323. ui->onlyForHumansCheck->setEnabled(true);
  324. ui->standardVictoryCheck->setEnabled(true);
  325. int vicCondition = index - 1;
  326. switch(vicCondition)
  327. {
  328. case 0: { //EventCondition::HAVE_ARTIFACT
  329. victoryTypeWidget = new QComboBox;
  330. ui->victoryParamsLayout->addWidget(victoryTypeWidget);
  331. for(int i = 0; i < controller->map()->allowedArtifact.size(); ++i)
  332. victoryTypeWidget->addItem(QString::fromStdString(VLC->arth->objects[i]->getNameTranslated()), QVariant::fromValue(i));
  333. break;
  334. }
  335. case 1: { //EventCondition::HAVE_CREATURES
  336. victoryTypeWidget = new QComboBox;
  337. ui->victoryParamsLayout->addWidget(victoryTypeWidget);
  338. for(int i = 0; i < VLC->creh->objects.size(); ++i)
  339. victoryTypeWidget->addItem(QString::fromStdString(VLC->creh->objects[i]->getNamePluralTranslated()), QVariant::fromValue(i));
  340. victoryValueWidget = new QLineEdit;
  341. ui->victoryParamsLayout->addWidget(victoryValueWidget);
  342. victoryValueWidget->setText("1");
  343. break;
  344. }
  345. case 2: { //EventCondition::HAVE_RESOURCES
  346. victoryTypeWidget = new QComboBox;
  347. ui->victoryParamsLayout->addWidget(victoryTypeWidget);
  348. {
  349. for(int resType = 0; resType < GameConstants::RESOURCE_QUANTITY; ++resType)
  350. {
  351. auto resName = QString::fromStdString(GameConstants::RESOURCE_NAMES[resType]);
  352. victoryTypeWidget->addItem(resName, QVariant::fromValue(resType));
  353. }
  354. }
  355. victoryValueWidget = new QLineEdit;
  356. ui->victoryParamsLayout->addWidget(victoryValueWidget);
  357. victoryValueWidget->setText("1");
  358. break;
  359. }
  360. case 3: { //EventCondition::HAVE_BUILDING
  361. victoryTypeWidget = new QComboBox;
  362. ui->victoryParamsLayout->addWidget(victoryTypeWidget);
  363. auto * ctown = VLC->townh->randomTown;
  364. for(int bId : ctown->getAllBuildings())
  365. victoryTypeWidget->addItem(QString::fromStdString(defaultBuildingIdConversion(BuildingID(bId))), QVariant::fromValue(bId));
  366. victorySelectWidget = new QComboBox;
  367. ui->victoryParamsLayout->addWidget(victorySelectWidget);
  368. victorySelectWidget->addItem("Any town", QVariant::fromValue(-1));
  369. for(int i : getObjectIndexes<const CGTownInstance>(*controller->map()))
  370. victorySelectWidget->addItem(getTownName(*controller->map(), i).c_str(), QVariant::fromValue(i));
  371. pickObjectButton = new QToolButton;
  372. connect(pickObjectButton, &QToolButton::clicked, this, &VictoryConditions::onObjectSelect);
  373. ui->victoryParamsLayout->addWidget(pickObjectButton);
  374. break;
  375. }
  376. case 4: { //EventCondition::CONTROL (Obj::TOWN)
  377. victoryTypeWidget = new QComboBox;
  378. ui->victoryParamsLayout->addWidget(victoryTypeWidget);
  379. for(int i : getObjectIndexes<const CGTownInstance>(*controller->map()))
  380. victoryTypeWidget->addItem(tr(getTownName(*controller->map(), i).c_str()), QVariant::fromValue(i));
  381. pickObjectButton = new QToolButton;
  382. connect(pickObjectButton, &QToolButton::clicked, this, &VictoryConditions::onObjectSelect);
  383. ui->victoryParamsLayout->addWidget(pickObjectButton);
  384. break;
  385. }
  386. case 5: { //EventCondition::DESTROY (Obj::HERO)
  387. victoryTypeWidget = new QComboBox;
  388. ui->victoryParamsLayout->addWidget(victoryTypeWidget);
  389. for(int i : getObjectIndexes<const CGHeroInstance>(*controller->map()))
  390. victoryTypeWidget->addItem(tr(getHeroName(*controller->map(), i).c_str()), QVariant::fromValue(i));
  391. pickObjectButton = new QToolButton;
  392. connect(pickObjectButton, &QToolButton::clicked, this, &VictoryConditions::onObjectSelect);
  393. ui->victoryParamsLayout->addWidget(pickObjectButton);
  394. break;
  395. }
  396. case 6: { //EventCondition::TRANSPORT (Obj::ARTEFACT)
  397. victoryTypeWidget = new QComboBox;
  398. ui->victoryParamsLayout->addWidget(victoryTypeWidget);
  399. for(int i = 0; i < controller->map()->allowedArtifact.size(); ++i)
  400. victoryTypeWidget->addItem(QString::fromStdString(VLC->arth->objects[i]->getNameTranslated()), QVariant::fromValue(i));
  401. victorySelectWidget = new QComboBox;
  402. ui->victoryParamsLayout->addWidget(victorySelectWidget);
  403. for(int i : getObjectIndexes<const CGTownInstance>(*controller->map()))
  404. victorySelectWidget->addItem(tr(getTownName(*controller->map(), i).c_str()), QVariant::fromValue(i));
  405. pickObjectButton = new QToolButton;
  406. connect(pickObjectButton, &QToolButton::clicked, this, &VictoryConditions::onObjectSelect);
  407. ui->victoryParamsLayout->addWidget(pickObjectButton);
  408. break;
  409. }
  410. case 7: { //EventCondition::DESTROY (Obj::MONSTER)
  411. victoryTypeWidget = new QComboBox;
  412. ui->victoryParamsLayout->addWidget(victoryTypeWidget);
  413. for(int i : getObjectIndexes<const CGCreature>(*controller->map()))
  414. victoryTypeWidget->addItem(tr(getMonsterName(*controller->map(), i).c_str()), QVariant::fromValue(i));
  415. pickObjectButton = new QToolButton;
  416. connect(pickObjectButton, &QToolButton::clicked, this, &VictoryConditions::onObjectSelect);
  417. ui->victoryParamsLayout->addWidget(pickObjectButton);
  418. break;
  419. }
  420. }
  421. }
  422. void VictoryConditions::onObjectSelect()
  423. {
  424. int vicConditions = ui->victoryComboBox->currentIndex() - 1;
  425. for(int lvl : {0, 1})
  426. {
  427. auto & l = controller->scene(lvl)->objectPickerView;
  428. switch(vicConditions)
  429. {
  430. case 3: { //EventCondition::HAVE_BUILDING
  431. l.highlight<const CGTownInstance>();
  432. break;
  433. }
  434. case 4: { //EventCondition::CONTROL (Obj::TOWN)
  435. l.highlight<const CGTownInstance>();
  436. break;
  437. }
  438. case 5: { //EventCondition::DESTROY (Obj::HERO)
  439. l.highlight<const CGHeroInstance>();
  440. break;
  441. }
  442. case 6: { //EventCondition::TRANSPORT (Obj::ARTEFACT)
  443. l.highlight<const CGTownInstance>();
  444. break;
  445. }
  446. case 7: { //EventCondition::DESTROY (Obj::MONSTER)
  447. l.highlight<const CGCreature>();
  448. break;
  449. }
  450. default:
  451. return;
  452. }
  453. l.update();
  454. QObject::connect(&l, &ObjectPickerLayer::selectionMade, this, &VictoryConditions::onObjectPicked);
  455. }
  456. dynamic_cast<QWidget*>(parent()->parent()->parent()->parent()->parent()->parent()->parent())->hide();
  457. }
  458. void VictoryConditions::onObjectPicked(const CGObjectInstance * obj)
  459. {
  460. dynamic_cast<QWidget*>(parent()->parent()->parent()->parent()->parent()->parent()->parent())->show();
  461. for(int lvl : {0, 1})
  462. {
  463. auto & l = controller->scene(lvl)->objectPickerView;
  464. l.clear();
  465. l.update();
  466. QObject::disconnect(&l, &ObjectPickerLayer::selectionMade, this, &VictoryConditions::onObjectPicked);
  467. }
  468. if(!obj) //discarded
  469. return;
  470. int vicConditions = ui->victoryComboBox->currentIndex() - 1;
  471. QComboBox * w = victoryTypeWidget;
  472. if(vicConditions == 3 || vicConditions == 6)
  473. w = victorySelectWidget;
  474. for(int i = 0; i < w->count(); ++i)
  475. {
  476. if(w->itemData(i).toInt() < 0)
  477. continue;
  478. auto data = controller->map()->objects.at(w->itemData(i).toInt());
  479. if(data == obj)
  480. {
  481. w->setCurrentIndex(i);
  482. break;
  483. }
  484. }
  485. }