From 37a8260c3bcc766baccd9fec59576c5aaef18b44 Mon Sep 17 00:00:00 2001 From: myk002 Date: Sun, 18 Jul 2021 22:55:57 -0700 Subject: [PATCH] import and export reaction-specific item conditions --- plugins/orders.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/plugins/orders.cpp b/plugins/orders.cpp index 629fee225..2a90e7e69 100644 --- a/plugins/orders.cpp +++ b/plugins/orders.cpp @@ -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); }