loseconditions.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * loseconditions.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 "loseconditions.h"
  12. #include "ui_loseconditions.h"
  13. #include "../mapcontroller.h"
  14. #include "../lib/CGeneralTextHandler.h"
  15. LoseConditions::LoseConditions(QWidget *parent) :
  16. AbstractSettings(parent),
  17. ui(new Ui::LoseConditions)
  18. {
  19. ui->setupUi(this);
  20. }
  21. LoseConditions::~LoseConditions()
  22. {
  23. delete ui;
  24. }
  25. void LoseConditions::initialize(MapController & c)
  26. {
  27. AbstractSettings::initialize(c);
  28. //loss messages
  29. ui->defeatMessageEdit->setText(QString::fromStdString(controller->map()->defeatMessage.toString()));
  30. //loss conditions
  31. const std::array<std::string, 5> conditionStringsLose = {
  32. QT_TR_NOOP("No special loss"),
  33. QT_TR_NOOP("Lose castle"),
  34. QT_TR_NOOP("Lose hero"),
  35. QT_TR_NOOP("Time expired"),
  36. QT_TR_NOOP("Days without town")
  37. };
  38. for(auto & s : conditionStringsLose)
  39. {
  40. ui->loseComboBox->addItem(QString::fromStdString(s));
  41. }
  42. ui->standardLoseCheck->setChecked(false);
  43. for(auto & ev : controller->map()->triggeredEvents)
  44. {
  45. if(ev.effect.type == EventEffect::DEFEAT)
  46. {
  47. if(ev.identifier == "standardDefeat")
  48. ui->standardLoseCheck->setChecked(true);
  49. if(ev.identifier == "specialDefeat")
  50. {
  51. auto readjson = ev.trigger.toJson(AbstractSettings::conditionToJson);
  52. auto linearNodes = linearJsonArray(readjson);
  53. for(auto & json : linearNodes)
  54. {
  55. switch(json["condition"].Integer())
  56. {
  57. case EventCondition::CONTROL: {
  58. if(json["objectType"].Integer() == Obj::TOWN)
  59. {
  60. ui->loseComboBox->setCurrentIndex(1);
  61. assert(loseTypeWidget);
  62. int townIdx = getObjectByPos<const CGTownInstance>(*controller->map(), posFromJson(json["position"]));
  63. if(townIdx >= 0)
  64. {
  65. auto idx = loseTypeWidget->findData(townIdx);
  66. loseTypeWidget->setCurrentIndex(idx);
  67. }
  68. }
  69. if(json["objectType"].Integer() == Obj::HERO)
  70. {
  71. ui->loseComboBox->setCurrentIndex(2);
  72. assert(loseTypeWidget);
  73. int heroIdx = getObjectByPos<const CGHeroInstance>(*controller->map(), posFromJson(json["position"]));
  74. if(heroIdx >= 0)
  75. {
  76. auto idx = loseTypeWidget->findData(heroIdx);
  77. loseTypeWidget->setCurrentIndex(idx);
  78. }
  79. }
  80. break;
  81. }
  82. case EventCondition::DAYS_PASSED: {
  83. ui->loseComboBox->setCurrentIndex(3);
  84. assert(loseValueWidget);
  85. loseValueWidget->setText(expiredDate(json["value"].Integer()));
  86. break;
  87. }
  88. case EventCondition::DAYS_WITHOUT_TOWN: {
  89. ui->loseComboBox->setCurrentIndex(4);
  90. assert(loseValueWidget);
  91. loseValueWidget->setText(QString::number(json["value"].Integer()));
  92. break;
  93. case EventCondition::IS_HUMAN:
  94. break; //ignore because always applicable for defeat conditions
  95. }
  96. };
  97. }
  98. }
  99. }
  100. }
  101. }
  102. void LoseConditions::update()
  103. {
  104. //loss messages
  105. controller->map()->defeatMessage = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller->map(), TextIdentifier("header", "defeatMessage"), ui->defeatMessageEdit->text().toStdString()));
  106. //loss conditions
  107. EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
  108. defeatCondition.value = 7;
  109. //Loss condition - 7 days without town
  110. TriggeredEvent standardDefeat;
  111. standardDefeat.effect.type = EventEffect::DEFEAT;
  112. standardDefeat.effect.toOtherMessage.appendTextID("core.genrltxt.8");
  113. standardDefeat.identifier = "standardDefeat";
  114. standardDefeat.description.clear(); // TODO: display in quest window
  115. standardDefeat.onFulfill.appendTextID("core.genrltxt.7");
  116. standardDefeat.trigger = EventExpression(defeatCondition);
  117. //DEFEAT
  118. if(ui->loseComboBox->currentIndex() == 0)
  119. {
  120. controller->map()->triggeredEvents.push_back(standardDefeat);
  121. controller->map()->defeatIconIndex = 3;
  122. controller->map()->defeatMessage = MetaString::createFromTextID("core.lcdesc.0");
  123. }
  124. else
  125. {
  126. int lossCondition = ui->loseComboBox->currentIndex() - 1;
  127. TriggeredEvent specialDefeat;
  128. specialDefeat.effect.type = EventEffect::DEFEAT;
  129. specialDefeat.identifier = "specialDefeat";
  130. specialDefeat.description.clear(); // TODO: display in quest window
  131. controller->map()->defeatIconIndex = lossCondition;
  132. switch(lossCondition)
  133. {
  134. case 0: {
  135. EventExpression::OperatorNone noneOf;
  136. EventCondition cond(EventCondition::CONTROL);
  137. cond.objectType = Obj::TOWN;
  138. assert(loseTypeWidget);
  139. int townIdx = loseTypeWidget->currentData().toInt();
  140. cond.position = controller->map()->objects[townIdx]->pos;
  141. noneOf.expressions.push_back(cond);
  142. specialDefeat.onFulfill.appendTextID("core.genrltxt.251");
  143. specialDefeat.trigger = EventExpression(noneOf);
  144. controller->map()->defeatMessage = MetaString::createFromTextID("core.lcdesc.1");
  145. break;
  146. }
  147. case 1: {
  148. EventExpression::OperatorNone noneOf;
  149. EventCondition cond(EventCondition::CONTROL);
  150. cond.objectType = Obj::HERO;
  151. assert(loseTypeWidget);
  152. int townIdx = loseTypeWidget->currentData().toInt();
  153. cond.position = controller->map()->objects[townIdx]->pos;
  154. noneOf.expressions.push_back(cond);
  155. specialDefeat.onFulfill.appendTextID("core.genrltxt.253");
  156. specialDefeat.trigger = EventExpression(noneOf);
  157. controller->map()->defeatMessage = MetaString::createFromTextID("core.lcdesc.2");
  158. break;
  159. }
  160. case 2: {
  161. EventCondition cond(EventCondition::DAYS_PASSED);
  162. assert(loseValueWidget);
  163. cond.value = expiredDate(loseValueWidget->text());
  164. specialDefeat.onFulfill.appendTextID("core.genrltxt.254");
  165. specialDefeat.trigger = EventExpression(cond);
  166. controller->map()->defeatMessage = MetaString::createFromTextID("core.lcdesc.3");
  167. break;
  168. }
  169. case 3: {
  170. EventCondition cond(EventCondition::DAYS_WITHOUT_TOWN);
  171. assert(loseValueWidget);
  172. cond.value = loseValueWidget->text().toInt();
  173. specialDefeat.onFulfill.appendTextID("core.genrltxt.7");
  174. specialDefeat.trigger = EventExpression(cond);
  175. break;
  176. }
  177. }
  178. EventExpression::OperatorAll allOf;
  179. EventCondition isHuman(EventCondition::IS_HUMAN);
  180. isHuman.value = 1;
  181. allOf.expressions.push_back(specialDefeat.trigger.get());
  182. allOf.expressions.push_back(isHuman);
  183. specialDefeat.trigger = EventExpression(allOf);
  184. if(ui->standardLoseCheck->isChecked())
  185. {
  186. controller->map()->triggeredEvents.push_back(standardDefeat);
  187. }
  188. controller->map()->triggeredEvents.push_back(specialDefeat);
  189. }
  190. }
  191. void LoseConditions::on_loseComboBox_currentIndexChanged(int index)
  192. {
  193. delete loseTypeWidget;
  194. delete loseValueWidget;
  195. delete loseSelectWidget;
  196. delete pickObjectButton;
  197. loseTypeWidget = nullptr;
  198. loseValueWidget = nullptr;
  199. loseSelectWidget = nullptr;
  200. pickObjectButton = nullptr;
  201. if(index == 0)
  202. {
  203. ui->standardLoseCheck->setChecked(true);
  204. ui->standardLoseCheck->setEnabled(false);
  205. return;
  206. }
  207. ui->standardLoseCheck->setEnabled(true);
  208. int loseCondition = index - 1;
  209. switch(loseCondition)
  210. {
  211. case 0: { //EventCondition::CONTROL (Obj::TOWN)
  212. loseTypeWidget = new QComboBox;
  213. ui->loseParamsLayout->addWidget(loseTypeWidget);
  214. for(int i : getObjectIndexes<const CGTownInstance>(*controller->map()))
  215. loseTypeWidget->addItem(QString::fromStdString(getTownName(*controller->map(), i).c_str()), QVariant::fromValue(i));
  216. pickObjectButton = new QToolButton;
  217. connect(pickObjectButton, &QToolButton::clicked, this, &LoseConditions::onObjectSelect);
  218. ui->loseParamsLayout->addWidget(pickObjectButton);
  219. break;
  220. }
  221. case 1: { //EventCondition::CONTROL (Obj::HERO)
  222. loseTypeWidget = new QComboBox;
  223. ui->loseParamsLayout->addWidget(loseTypeWidget);
  224. for(int i : getObjectIndexes<const CGHeroInstance>(*controller->map()))
  225. loseTypeWidget->addItem(QString::fromStdString(getHeroName(*controller->map(), i).c_str()), QVariant::fromValue(i));
  226. pickObjectButton = new QToolButton;
  227. connect(pickObjectButton, &QToolButton::clicked, this, &LoseConditions::onObjectSelect);
  228. ui->loseParamsLayout->addWidget(pickObjectButton);
  229. break;
  230. }
  231. case 2: { //EventCondition::DAYS_PASSED
  232. loseValueWidget = new QLineEdit;
  233. ui->loseParamsLayout->addWidget(loseValueWidget);
  234. loseValueWidget->setText("2m 1w 1d");
  235. break;
  236. }
  237. case 3: { //EventCondition::DAYS_WITHOUT_TOWN
  238. loseValueWidget = new QLineEdit;
  239. ui->loseParamsLayout->addWidget(loseValueWidget);
  240. loseValueWidget->setText("7");
  241. break;
  242. }
  243. }
  244. }
  245. void LoseConditions::onObjectSelect()
  246. {
  247. int loseCondition = ui->loseComboBox->currentIndex() - 1;
  248. for(int lvl : {0, 1})
  249. {
  250. auto & l = controller->scene(lvl)->objectPickerView;
  251. switch(loseCondition)
  252. {
  253. case 0: { //EventCondition::CONTROL (Obj::TOWN)
  254. l.highlight<const CGTownInstance>();
  255. break;
  256. }
  257. case 1: { //EventCondition::CONTROL (Obj::HERO)
  258. l.highlight<const CGHeroInstance>();
  259. break;
  260. }
  261. default:
  262. return;
  263. }
  264. l.update();
  265. QObject::connect(&l, &ObjectPickerLayer::selectionMade, this, &LoseConditions::onObjectPicked);
  266. }
  267. dynamic_cast<QWidget*>(parent()->parent()->parent()->parent()->parent()->parent()->parent())->hide();
  268. }
  269. void LoseConditions::onObjectPicked(const CGObjectInstance * obj)
  270. {
  271. dynamic_cast<QWidget*>(parent()->parent()->parent()->parent()->parent()->parent()->parent())->show();
  272. for(int lvl : {0, 1})
  273. {
  274. auto & l = controller->scene(lvl)->objectPickerView;
  275. l.clear();
  276. l.update();
  277. QObject::disconnect(&l, &ObjectPickerLayer::selectionMade, this, &LoseConditions::onObjectPicked);
  278. }
  279. if(!obj) //discarded
  280. return;
  281. for(int i = 0; i < loseTypeWidget->count(); ++i)
  282. {
  283. auto data = controller->map()->objects.at(loseTypeWidget->itemData(i).toInt());
  284. if(data == obj)
  285. {
  286. loseTypeWidget->setCurrentIndex(i);
  287. break;
  288. }
  289. }
  290. }