import and export reaction-specific item conditions

develop
myk002 2021-07-18 22:55:57 -07:00
parent 2bd43f6e76
commit 37a8260c3b
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
1 changed files with 88 additions and 2 deletions

@ -29,6 +29,8 @@
#include "df/manager_order.h"
#include "df/manager_order_condition_item.h"
#include "df/manager_order_condition_order.h"
#include "df/reaction.h"
#include "df/reaction_reagent.h"
#include "df/world.h"
using namespace DFHack;
@ -377,7 +379,26 @@ static command_result orders_export_command(color_ostream & out, const std::stri
condition["tool"] = enum_item_key(it2->has_tool_use);
}
// TODO: anon_1, anon_2, anon_3
if (it2->min_dimension != -1)
{
condition["min_dimension"] = it2->min_dimension;
}
if (it2->reaction_id != -1)
{
df::reaction *reaction = world->raws.reactions.reactions[it2->reaction_id];
condition["reaction_id"] = reaction->code;
if (!it2->contains.empty())
{
Json::Value contains(Json::arrayValue);
for (int32_t contains_val : it2->contains)
{
contains.append(reaction->reagents[contains_val]->code);
}
condition["contains"] = contains;
}
}
conditions.append(condition);
}
@ -705,7 +726,72 @@ static command_result orders_import(color_ostream &out, Json::Value &orders)
}
}
// TODO: anon_1, anon_2, anon_3
if (it2.isMember("min_dimension"))
{
condition->min_dimension = it2["min_dimension"].asInt();
}
if (it2.isMember("reaction_id"))
{
std::string reaction_code = it2["reaction_id"].asString();
df::reaction *reaction = NULL;
int32_t reaction_id = -1;
size_t num_reactions = world->raws.reactions.reactions.size();
for (size_t idx = 0; idx < num_reactions; ++idx)
{
reaction = world->raws.reactions.reactions[idx];
if (reaction->code == reaction_code)
{
reaction_id = idx;
break;
}
}
if (reaction_id < 0)
{
delete condition;
out << COLOR_YELLOW << "Reaction code not found for imported manager order: " << reaction_code << std::endl;
continue;
}
condition->reaction_id = reaction_id;
if (it2.isMember("contains"))
{
size_t num_reagents = reaction->reagents.size();
std::string bad_reagent_code;
for (Json::Value & contains_val : it2["contains"])
{
std::string reagent_code = contains_val.asString();
bool reagent_found = false;
for (size_t idx = 0; idx < num_reagents; ++idx)
{
df::reaction_reagent *reagent = reaction->reagents[idx];
if (reagent->code == reagent_code)
{
condition->contains.push_back(idx);
reagent_found = true;
break;
}
}
if (!reagent_found)
{
bad_reagent_code = reagent_code;
break;
}
}
if (!bad_reagent_code.empty())
{
delete condition;
out << COLOR_YELLOW << "Invalid reagent code for imported manager order: " << bad_reagent_code << std::endl;
continue;
}
}
}
order->item_conditions.push_back(condition);
}