1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Some vic/loss conditions for editor

This commit is contained in:
nordsoft 2022-12-22 06:26:38 +04:00 committed by Nordsoft91
parent 4e21aab76a
commit a3413eefce
2 changed files with 161 additions and 57 deletions

View File

@ -24,9 +24,12 @@ int expiredDate(const QString & date)
int result = 0; int result = 0;
for(auto component : date.split(" ")) for(auto component : date.split(" "))
{ {
result += component.left(component.lastIndexOf('d')).toInt(); int days = component.left(component.lastIndexOf('d')).toInt();
result += component.left(component.lastIndexOf('w')).toInt() * 7; int weeks = component.left(component.lastIndexOf('w')).toInt();
result += component.left(component.lastIndexOf('m')).toInt() * 28; int months = component.left(component.lastIndexOf('m')).toInt();
result += days > 0 ? days - 1 : 0;
result += (weeks > 0 ? weeks - 1 : 0) * 7;
result += (months > 0 ? months - 1 : 0) * 28;
} }
return result; return result;
} }
@ -54,6 +57,27 @@ QString expiredDate(int date)
return result; return result;
} }
int3 posFromJson(const JsonNode & json)
{
return int3(json.Vector()[0].Integer(), json.Vector()[1].Integer(), json.Vector()[2].Integer());
}
std::vector<JsonNode> linearJsonArray(const JsonNode & json)
{
std::vector<JsonNode> result;
if(json.getType() == JsonNode::JsonType::DATA_STRUCT)
result.push_back(json);
if(json.getType() == JsonNode::JsonType::DATA_VECTOR)
{
for(auto & node : json.Vector())
{
auto subvector = linearJsonArray(node);
result.insert(result.end(), subvector.begin(), subvector.end());
}
}
return result;
}
MapSettings::MapSettings(MapController & ctrl, QWidget *parent) : MapSettings::MapSettings(MapController & ctrl, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::MapSettings), ui(new Ui::MapSettings),
@ -215,7 +239,11 @@ MapSettings::MapSettings(MapController & ctrl, QWidget *parent) :
if(ev.identifier == "specialVictory") if(ev.identifier == "specialVictory")
{ {
auto json = ev.trigger.toJson(conditionToJson); auto readjson = ev.trigger.toJson(conditionToJson);
auto linearNodes = linearJsonArray(readjson);
for(auto & json : linearNodes)
{
switch(json["condition"].Integer()) switch(json["condition"].Integer())
{ {
case EventCondition::HAVE_ARTIFACT: { case EventCondition::HAVE_ARTIFACT: {
@ -233,9 +261,15 @@ MapSettings::MapSettings(MapController & ctrl, QWidget *parent) :
victoryValueWidget->setText(QString::number(json["value"].Integer())); victoryValueWidget->setText(QString::number(json["value"].Integer()));
break; break;
} }
case EventCondition::IS_HUMAN: {
ui->onlyForHumansCheck->setChecked(true);
break;
}
}; };
} }
} }
}
if(ev.effect.type == EventEffect::DEFEAT) if(ev.effect.type == EventEffect::DEFEAT)
{ {
@ -244,15 +278,24 @@ MapSettings::MapSettings(MapController & ctrl, QWidget *parent) :
if(ev.identifier == "specialDefeat") if(ev.identifier == "specialDefeat")
{ {
auto json = ev.trigger.toJson(conditionToJson); auto readjson = ev.trigger.toJson(conditionToJson);
auto linearNodes = linearJsonArray(readjson);
for(auto & json : linearNodes)
{
switch(json["condition"].Integer()) switch(json["condition"].Integer())
{ {
case EventCondition::CONTROL: { case EventCondition::CONTROL: {
if(json["objectType"].Integer() == Obj::TOWN) if(json["objectType"].Integer() == Obj::TOWN)
{ {
ui->loseComboBox->setCurrentIndex(1); ui->loseComboBox->setCurrentIndex(1);
//assert(loseValueWidget); assert(loseTypeWidget);
//loseValueWidget->setText(QString::number(json["value"].Integer())); int townIdx = getTownByPos(posFromJson(json["position"]));
if(townIdx >= 0)
{
auto idx = loseTypeWidget->findData(townIdx);
loseTypeWidget->setCurrentIndex(idx);
}
} }
if(json["objectType"].Integer() == Obj::HERO) if(json["objectType"].Integer() == Obj::HERO)
{ {
@ -276,12 +319,16 @@ MapSettings::MapSettings(MapController & ctrl, QWidget *parent) :
assert(loseValueWidget); assert(loseValueWidget);
loseValueWidget->setText(QString::number(json["value"].Integer())); loseValueWidget->setText(QString::number(json["value"].Integer()));
break; break;
case EventCondition::IS_HUMAN:
break; //ignore as always applicable for defeat conditions
} }
}; };
} }
} }
} }
}
//ui8 difficulty; //ui8 difficulty;
//ui8 levelLimit; //ui8 levelLimit;
@ -299,6 +346,46 @@ MapSettings::~MapSettings()
delete ui; delete ui;
} }
std::string MapSettings::getTownName(int townObjectIdx)
{
std::string name;
if(auto town = dynamic_cast<CGTownInstance*>(controller.map()->objects[townObjectIdx].get()))
{
auto * ctown = town->town;
if(!ctown)
ctown = VLC->townh->randomTown;
name = ctown->faction ? town->getObjectName() : town->name + ", (random)";
}
return name;
}
std::vector<int> MapSettings::getTownIndexes() const
{
std::vector<int> result;
for(int i = 0; i < controller.map()->objects.size(); ++i)
{
if(auto town = dynamic_cast<CGTownInstance*>(controller.map()->objects[i].get()))
{
result.push_back(i);
}
}
return result;
}
int MapSettings::getTownByPos(const int3 & pos)
{
for(int i = 0; i < controller.map()->objects.size(); ++i)
{
if(auto town = dynamic_cast<CGTownInstance*>(controller.map()->objects[i].get()))
{
if(town->pos == pos)
return i;
}
}
return -1;
}
void MapSettings::on_pushButton_clicked() void MapSettings::on_pushButton_clicked()
{ {
controller.map()->name = ui->mapNameEdit->text().toStdString(); controller.map()->name = ui->mapNameEdit->text().toStdString();
@ -436,7 +523,7 @@ void MapSettings::on_pushButton_clicked()
} }
else else
{ {
int lossCondition = ui->victoryComboBox->currentIndex() - 1; int lossCondition = ui->loseComboBox->currentIndex() - 1;
TriggeredEvent specialDefeat; TriggeredEvent specialDefeat;
specialDefeat.effect.type = EventEffect::DEFEAT; specialDefeat.effect.type = EventEffect::DEFEAT;
@ -448,6 +535,19 @@ void MapSettings::on_pushButton_clicked()
switch(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 = VLC->generaltexth->allTexts[251];
specialDefeat.trigger = EventExpression(noneOf);
break;
}
case 2: { case 2: {
EventCondition cond(EventCondition::DAYS_PASSED); EventCondition cond(EventCondition::DAYS_PASSED);
assert(loseValueWidget); assert(loseValueWidget);
@ -460,7 +560,7 @@ void MapSettings::on_pushButton_clicked()
case 3: { case 3: {
EventCondition cond(EventCondition::DAYS_WITHOUT_TOWN); EventCondition cond(EventCondition::DAYS_WITHOUT_TOWN);
assert(loseValueWidget); assert(loseValueWidget);
cond.value = victoryValueWidget->text().toInt(); cond.value = loseValueWidget->text().toInt();
specialDefeat.onFulfill = VLC->generaltexth->allTexts[7]; specialDefeat.onFulfill = VLC->generaltexth->allTexts[7];
specialDefeat.trigger = EventExpression(cond); specialDefeat.trigger = EventExpression(cond);
break; break;
@ -522,7 +622,6 @@ void MapSettings::on_victoryComboBox_currentIndexChanged(int index)
victoryValueWidget = new QLineEdit; victoryValueWidget = new QLineEdit;
ui->victoryParamsLayout->addWidget(victoryValueWidget); ui->victoryParamsLayout->addWidget(victoryValueWidget);
victoryValueWidget->setInputMask("9000");
victoryValueWidget->setText("1"); victoryValueWidget->setText("1");
break; break;
} }
@ -551,8 +650,8 @@ void MapSettings::on_loseComboBox_currentIndexChanged(int index)
case 0: { //EventCondition::CONTROL (Obj::TOWN) case 0: { //EventCondition::CONTROL (Obj::TOWN)
loseTypeWidget = new QComboBox; loseTypeWidget = new QComboBox;
ui->loseParamsLayout->addWidget(loseTypeWidget); ui->loseParamsLayout->addWidget(loseTypeWidget);
//for(int i = 0; i < controller.map()->allowedArtifact.size(); ++i) for(int i : getTownIndexes())
//loseTypeWidget->addItem(QString::fromStdString(VLC->arth->objects[i]->getName()), QVariant::fromValue(i)); loseTypeWidget->addItem(tr(getTownName(i).c_str()), QVariant::fromValue(i));
break; break;
} }

View File

@ -33,6 +33,11 @@ private slots:
void on_loseComboBox_currentIndexChanged(int index); void on_loseComboBox_currentIndexChanged(int index);
private: private:
std::string getTownName(int townObjectIdx);
std::vector<int> getTownIndexes() const;
int getTownByPos(const int3 & pos);
Ui::MapSettings *ui; Ui::MapSettings *ui;
MapController & controller; MapController & controller;