| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 | 
							- /*
 
-  * loseconditions.cpp, part of VCMI engine
 
-  *
 
-  * Authors: listed in file AUTHORS in main folder
 
-  *
 
-  * License: GNU General Public License v2.0 or later
 
-  * Full text of license available in license.txt file, in main folder
 
-  *
 
-  */
 
- #include "StdInc.h"
 
- #include "loseconditions.h"
 
- #include "ui_loseconditions.h"
 
- #include "../mapcontroller.h"
 
- #include "../lib/CGeneralTextHandler.h"
 
- LoseConditions::LoseConditions(QWidget *parent) :
 
- 	AbstractSettings(parent),
 
- 	ui(new Ui::LoseConditions)
 
- {
 
- 	ui->setupUi(this);
 
- }
 
- LoseConditions::~LoseConditions()
 
- {
 
- 	delete ui;
 
- }
 
- void LoseConditions::initialize(MapController & c)
 
- {
 
- 	AbstractSettings::initialize(c);
 
- 	//loss messages
 
- 	ui->defeatMessageEdit->setText(QString::fromStdString(controller->map()->defeatMessage.toString()));
 
- 	//loss conditions
 
- 	const std::array<std::string, 5> conditionStringsLose = {
 
- 		QT_TR_NOOP("No special loss"),
 
- 		QT_TR_NOOP("Lose castle"),
 
- 		QT_TR_NOOP("Lose hero"),
 
- 		QT_TR_NOOP("Time expired"),
 
- 		QT_TR_NOOP("Days without town")
 
- 	};
 
- 	for(auto & s : conditionStringsLose)
 
- 	{
 
- 		ui->loseComboBox->addItem(QString::fromStdString(s));
 
- 	}
 
- 	ui->standardLoseCheck->setChecked(false);
 
- 	for(auto & ev : controller->map()->triggeredEvents)
 
- 	{
 
- 		if(ev.effect.type == EventEffect::DEFEAT)
 
- 		{
 
- 			if(ev.identifier == "standardDefeat")
 
- 				ui->standardLoseCheck->setChecked(true);
 
- 			if(ev.identifier == "specialDefeat")
 
- 			{
 
- 				auto readjson = ev.trigger.toJson(AbstractSettings::conditionToJson);
 
- 				auto linearNodes = linearJsonArray(readjson);
 
- 				for(auto & json : linearNodes)
 
- 				{
 
- 					switch(json["condition"].Integer())
 
- 					{
 
- 						case EventCondition::CONTROL: {
 
- 							if(json["objectType"].Integer() == Obj::TOWN)
 
- 							{
 
- 								ui->loseComboBox->setCurrentIndex(1);
 
- 								assert(loseTypeWidget);
 
- 								int townIdx = getObjectByPos<const CGTownInstance>(*controller->map(), posFromJson(json["position"]));
 
- 								if(townIdx >= 0)
 
- 								{
 
- 									auto idx = loseTypeWidget->findData(townIdx);
 
- 									loseTypeWidget->setCurrentIndex(idx);
 
- 								}
 
- 							}
 
- 							if(json["objectType"].Integer() == Obj::HERO)
 
- 							{
 
- 								ui->loseComboBox->setCurrentIndex(2);
 
- 								assert(loseTypeWidget);
 
- 								int heroIdx = getObjectByPos<const CGHeroInstance>(*controller->map(), posFromJson(json["position"]));
 
- 								if(heroIdx >= 0)
 
- 								{
 
- 									auto idx = loseTypeWidget->findData(heroIdx);
 
- 									loseTypeWidget->setCurrentIndex(idx);
 
- 								}
 
- 							}
 
- 							break;
 
- 						}
 
- 						case EventCondition::DAYS_PASSED: {
 
- 							ui->loseComboBox->setCurrentIndex(3);
 
- 							assert(loseValueWidget);
 
- 							loseValueWidget->setText(expiredDate(json["value"].Integer()));
 
- 							break;
 
- 						}
 
- 						case EventCondition::DAYS_WITHOUT_TOWN: {
 
- 							ui->loseComboBox->setCurrentIndex(4);
 
- 							assert(loseValueWidget);
 
- 							loseValueWidget->setText(QString::number(json["value"].Integer()));
 
- 							break;
 
- 						case EventCondition::IS_HUMAN:
 
- 							break; //ignore because always applicable for defeat conditions
 
- 						}
 
- 					};
 
- 				}
 
- 			}
 
- 		}
 
- 	}
 
- }
 
- void LoseConditions::update()
 
- {
 
- 	//loss messages
 
- 	controller->map()->defeatMessage = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller->map(), TextIdentifier("header", "defeatMessage"), ui->defeatMessageEdit->text().toStdString()));
 
- 	//loss conditions
 
- 	EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
 
- 	defeatCondition.value = 7;
 
- 	//Loss condition - 7 days without town
 
- 	TriggeredEvent standardDefeat;
 
- 	standardDefeat.effect.type = EventEffect::DEFEAT;
 
- 	standardDefeat.effect.toOtherMessage.appendTextID("core.genrltxt.8");
 
- 	standardDefeat.identifier = "standardDefeat";
 
- 	standardDefeat.description.clear(); // TODO: display in quest window
 
- 	standardDefeat.onFulfill.appendTextID("core.genrltxt.7");
 
- 	standardDefeat.trigger = EventExpression(defeatCondition);
 
- 	//DEFEAT
 
- 	if(ui->loseComboBox->currentIndex() == 0)
 
- 	{
 
- 		controller->map()->triggeredEvents.push_back(standardDefeat);
 
- 		controller->map()->defeatIconIndex = 3;
 
- 		controller->map()->defeatMessage = MetaString::createFromTextID("core.lcdesc.0");
 
- 	}
 
- 	else
 
- 	{
 
- 		int lossCondition = ui->loseComboBox->currentIndex() - 1;
 
- 		TriggeredEvent specialDefeat;
 
- 		specialDefeat.effect.type = EventEffect::DEFEAT;
 
- 		specialDefeat.identifier = "specialDefeat";
 
- 		specialDefeat.description.clear(); // TODO: display in quest window
 
- 		controller->map()->defeatIconIndex = lossCondition;
 
- 		switch(lossCondition)
 
- 		{
 
- 			case 0: {
 
- 				EventExpression::OperatorNone noneOf;
 
- 				EventCondition cond(EventCondition::CONTROL);
 
- 				cond.objectType = Obj::TOWN;
 
- 				assert(loseTypeWidget);
 
- 				int townIdx = loseTypeWidget->currentData().toInt();
 
- 				cond.position = controller->map()->objects[townIdx]->pos;
 
- 				noneOf.expressions.push_back(cond);
 
- 				specialDefeat.onFulfill.appendTextID("core.genrltxt.251");
 
- 				specialDefeat.trigger = EventExpression(noneOf);
 
- 				controller->map()->defeatMessage = MetaString::createFromTextID("core.lcdesc.1");
 
- 				break;
 
- 			}
 
- 			case 1: {
 
- 				EventExpression::OperatorNone noneOf;
 
- 				EventCondition cond(EventCondition::CONTROL);
 
- 				cond.objectType = Obj::HERO;
 
- 				assert(loseTypeWidget);
 
- 				int townIdx = loseTypeWidget->currentData().toInt();
 
- 				cond.position = controller->map()->objects[townIdx]->pos;
 
- 				noneOf.expressions.push_back(cond);
 
- 				specialDefeat.onFulfill.appendTextID("core.genrltxt.253");
 
- 				specialDefeat.trigger = EventExpression(noneOf);
 
- 				controller->map()->defeatMessage = MetaString::createFromTextID("core.lcdesc.2");
 
- 				break;
 
- 			}
 
- 			case 2: {
 
- 				EventCondition cond(EventCondition::DAYS_PASSED);
 
- 				assert(loseValueWidget);
 
- 				cond.value = expiredDate(loseValueWidget->text());
 
- 				specialDefeat.onFulfill.appendTextID("core.genrltxt.254");
 
- 				specialDefeat.trigger = EventExpression(cond);
 
- 				controller->map()->defeatMessage = MetaString::createFromTextID("core.lcdesc.3");
 
- 				break;
 
- 			}
 
- 			case 3: {
 
- 				EventCondition cond(EventCondition::DAYS_WITHOUT_TOWN);
 
- 				assert(loseValueWidget);
 
- 				cond.value = loseValueWidget->text().toInt();
 
- 				specialDefeat.onFulfill.appendTextID("core.genrltxt.7");
 
- 				specialDefeat.trigger = EventExpression(cond);
 
- 				break;
 
- 			}
 
- 		}
 
- 		EventExpression::OperatorAll allOf;
 
- 		EventCondition isHuman(EventCondition::IS_HUMAN);
 
- 		isHuman.value = 1;
 
- 		allOf.expressions.push_back(specialDefeat.trigger.get());
 
- 		allOf.expressions.push_back(isHuman);
 
- 		specialDefeat.trigger = EventExpression(allOf);
 
- 		if(ui->standardLoseCheck->isChecked())
 
- 		{
 
- 			controller->map()->triggeredEvents.push_back(standardDefeat);
 
- 		}
 
- 		controller->map()->triggeredEvents.push_back(specialDefeat);
 
- 	}
 
- }
 
- void LoseConditions::on_loseComboBox_currentIndexChanged(int index)
 
- {
 
- 	delete loseTypeWidget;
 
- 	delete loseValueWidget;
 
- 	delete loseSelectWidget;
 
- 	delete pickObjectButton;
 
- 	loseTypeWidget = nullptr;
 
- 	loseValueWidget = nullptr;
 
- 	loseSelectWidget = nullptr;
 
- 	pickObjectButton = nullptr;
 
- 	if(index == 0)
 
- 	{
 
- 		ui->standardLoseCheck->setChecked(true);
 
- 		ui->standardLoseCheck->setEnabled(false);
 
- 		return;
 
- 	}
 
- 	ui->standardLoseCheck->setEnabled(true);
 
- 	int loseCondition = index - 1;
 
- 	switch(loseCondition)
 
- 	{
 
- 		case 0: {  //EventCondition::CONTROL (Obj::TOWN)
 
- 			loseTypeWidget = new QComboBox;
 
- 			ui->loseParamsLayout->addWidget(loseTypeWidget);
 
- 			for(int i : getObjectIndexes<const CGTownInstance>(*controller->map()))
 
- 				loseTypeWidget->addItem(QString::fromStdString(getTownName(*controller->map(), i).c_str()), QVariant::fromValue(i));
 
- 			pickObjectButton = new QToolButton;
 
- 			connect(pickObjectButton, &QToolButton::clicked, this, &LoseConditions::onObjectSelect);
 
- 			ui->loseParamsLayout->addWidget(pickObjectButton);
 
- 			break;
 
- 		}
 
- 		case 1: { //EventCondition::CONTROL (Obj::HERO)
 
- 			loseTypeWidget = new QComboBox;
 
- 			ui->loseParamsLayout->addWidget(loseTypeWidget);
 
- 			for(int i : getObjectIndexes<const CGHeroInstance>(*controller->map()))
 
- 				loseTypeWidget->addItem(QString::fromStdString(getHeroName(*controller->map(), i).c_str()), QVariant::fromValue(i));
 
- 			pickObjectButton = new QToolButton;
 
- 			connect(pickObjectButton, &QToolButton::clicked, this, &LoseConditions::onObjectSelect);
 
- 			ui->loseParamsLayout->addWidget(pickObjectButton);
 
- 			break;
 
- 		}
 
- 		case 2: { //EventCondition::DAYS_PASSED
 
- 			loseValueWidget = new QLineEdit;
 
- 			ui->loseParamsLayout->addWidget(loseValueWidget);
 
- 			loseValueWidget->setText("2m 1w 1d");
 
- 			break;
 
- 		}
 
- 		case 3: { //EventCondition::DAYS_WITHOUT_TOWN
 
- 			loseValueWidget = new QLineEdit;
 
- 			ui->loseParamsLayout->addWidget(loseValueWidget);
 
- 			loseValueWidget->setText("7");
 
- 			break;
 
- 		}
 
- 	}
 
- }
 
- void LoseConditions::onObjectSelect()
 
- {
 
- 	int loseCondition = ui->loseComboBox->currentIndex() - 1;
 
- 	for(int lvl : {0, 1})
 
- 	{
 
- 		auto & l = controller->scene(lvl)->objectPickerView;
 
- 		switch(loseCondition)
 
- 		{
 
- 			case 0: {  //EventCondition::CONTROL (Obj::TOWN)
 
- 				l.highlight<const CGTownInstance>();
 
- 				break;
 
- 			}
 
- 				
 
- 			case 1: { //EventCondition::CONTROL (Obj::HERO)
 
- 				l.highlight<const CGHeroInstance>();
 
- 				break;
 
- 			}
 
- 			default:
 
- 				return;
 
- 		}
 
- 		l.update();
 
- 		QObject::connect(&l, &ObjectPickerLayer::selectionMade, this, &LoseConditions::onObjectPicked);
 
- 	}
 
- 	
 
- 	dynamic_cast<QWidget*>(parent()->parent()->parent()->parent()->parent()->parent()->parent())->hide();
 
- }
 
- void LoseConditions::onObjectPicked(const CGObjectInstance * obj)
 
- {
 
- 	dynamic_cast<QWidget*>(parent()->parent()->parent()->parent()->parent()->parent()->parent())->show();
 
- 	
 
- 	for(int lvl : {0, 1})
 
- 	{
 
- 		auto & l = controller->scene(lvl)->objectPickerView;
 
- 		l.clear();
 
- 		l.update();
 
- 		QObject::disconnect(&l, &ObjectPickerLayer::selectionMade, this, &LoseConditions::onObjectPicked);
 
- 	}
 
- 	
 
- 	if(!obj) //discarded
 
- 		return;
 
- 	
 
- 	for(int i = 0; i < loseTypeWidget->count(); ++i)
 
- 	{
 
- 		auto data = controller->map()->objects.at(loseTypeWidget->itemData(i).toInt());
 
- 		if(data == obj)
 
- 		{
 
- 			loseTypeWidget->setCurrentIndex(i);
 
- 			break;
 
- 		}
 
- 	}
 
- }
 
 
  |