dfhack/plugins/automelt.cpp

814 lines
27 KiB
C++

2023-01-20 15:58:38 -07:00
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "modules/Buildings.h"
#include "modules/Items.h"
#include "modules/World.h"
#include "modules/Persistence.h"
2023-01-21 16:22:15 -07:00
#include "modules/Gui.h"
2023-01-20 15:58:38 -07:00
2014-06-04 04:12:30 -06:00
#include "df/world.h"
#include "df/building_stockpilest.h"
#include "df/item_quality.h"
2023-01-20 15:58:38 -07:00
#include <map>
#include <unordered_map>
using std::map;
using std::string;
using std::unordered_map;
using std::vector;
using namespace DFHack;
using namespace df::enums;
2014-06-04 04:12:30 -06:00
DFHACK_PLUGIN("automelt");
2023-01-21 16:22:15 -07:00
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(world);
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
namespace DFHack
{
DBG_DECLARE(automelt, status, DebugCategory::LINFO);
DBG_DECLARE(automelt, cycle, DebugCategory::LINFO);
2023-01-21 16:22:15 -07:00
DBG_DECLARE(automelt, perf, DebugCategory::LINFO);
2023-01-20 15:58:38 -07:00
}
static const string CONFIG_KEY = string(plugin_name) + "/config";
static const string STOCKPILE_CONFIG_KEY_PREFIX = string(plugin_name) + "/stockpile/";
static PersistentDataItem config;
2023-01-23 11:35:26 -07:00
static unordered_map<int32_t, PersistentDataItem> watched_stockpiles;
2023-01-20 15:58:38 -07:00
enum StockpileConfigValues
{
STOCKPILE_CONFIG_ID = 0,
STOCKPILE_CONFIG_MONITORED = 1,
};
2023-01-20 15:58:38 -07:00
static int get_config_val(PersistentDataItem &c, int index)
{
if (!c.isValid())
return -1;
return c.ival(index);
}
2023-01-22 02:13:23 -07:00
2023-01-20 15:58:38 -07:00
static bool get_config_bool(PersistentDataItem &c, int index)
{
return get_config_val(c, index) == 1;
}
2023-01-22 02:13:23 -07:00
2023-01-20 15:58:38 -07:00
static void set_config_val(PersistentDataItem &c, int index, int value)
{
if (c.isValid())
c.ival(index) = value;
}
2023-01-22 02:13:23 -07:00
2023-01-20 15:58:38 -07:00
static void set_config_bool(PersistentDataItem &c, int index, bool value)
{
set_config_val(c, index, value ? 1 : 0);
}
2023-01-20 15:58:38 -07:00
static PersistentDataItem &ensure_stockpile_config(color_ostream &out, int id)
{
2023-01-23 11:35:26 -07:00
DEBUG(cycle,out).print("ensuring stockpile config id=%d\n", id);
if (watched_stockpiles.count(id)){
DEBUG(cycle,out).print("stockpile exists in watched_indices\n");
return watched_stockpiles[id];
}
2023-01-20 15:58:38 -07:00
string keyname = STOCKPILE_CONFIG_KEY_PREFIX + int_to_string(id);
2023-01-23 11:35:26 -07:00
DEBUG(status,out).print("creating new persistent key for stockpile %d\n", id);
watched_stockpiles.emplace(id, World::GetPersistentData(keyname, NULL));
return watched_stockpiles[id];
}
2023-01-20 15:58:38 -07:00
static void remove_stockpile_config(color_ostream &out, int id)
2014-06-04 04:12:30 -06:00
{
2023-01-23 11:35:26 -07:00
if (!watched_stockpiles.count(id))
2023-01-20 15:58:38 -07:00
return;
DEBUG(status, out).print("removing persistent key for stockpile %d\n", id);
2023-01-23 11:35:26 -07:00
World::DeletePersistentData(watched_stockpiles[id]);
watched_stockpiles.erase(id);
2023-01-20 15:58:38 -07:00
}
2014-06-04 04:12:30 -06:00
static bool isStockpile(df::building * bld) {
return bld && bld->getType() == df::building_type::Stockpile;
}
2023-01-20 15:58:38 -07:00
static void validate_stockpile_configs(color_ostream &out)
{
for (auto &c : watched_stockpiles) {
2023-01-23 11:35:26 -07:00
int id = get_config_val(c.second, STOCKPILE_CONFIG_ID);
auto bld = df::building::find(id);
if (!isStockpile(bld))
2023-01-20 15:58:38 -07:00
remove_stockpile_config(out, id);
}
}
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
static const int32_t CYCLE_TICKS = 1200;
static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
static command_result do_command(color_ostream &out, vector<string> &parameters);
static int32_t do_cycle(color_ostream &out);
DFhackCExport command_result plugin_init(color_ostream &out, vector<PluginCommand> &commands)
2023-01-20 15:58:38 -07:00
{
DEBUG(status, out).print("initializing %s\n", plugin_name);
// provide a configuration interface for the plugin
commands.push_back(PluginCommand(
plugin_name,
"Auto melt items in designated stockpiles.",
do_command));
return CR_OK;
}
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
{
if (enable != is_enabled)
{
is_enabled = enable;
DEBUG(status, out).print("%s from the API; persisting\n", is_enabled ? "enabled" : "disabled");
}
else
{
DEBUG(status, out).print("%s from the API, but already %s; no action\n", is_enabled ? "enabled" : "disabled", is_enabled ? "enabled" : "disabled");
}
return CR_OK;
2014-06-04 04:12:30 -06:00
}
2023-01-20 15:58:38 -07:00
DFhackCExport command_result plugin_shutdown(color_ostream &out)
{
DEBUG(status, out).print("shutting down %s\n", plugin_name);
return CR_OK;
}
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
DFhackCExport command_result plugin_load_data(color_ostream &out)
2014-06-04 04:12:30 -06:00
{
cycle_timestamp = 0;
2023-01-20 15:58:38 -07:00
config = World::GetPersistentData(CONFIG_KEY);
if (!config.isValid())
2014-06-04 04:12:30 -06:00
{
2023-01-20 15:58:38 -07:00
DEBUG(status, out).print("no config found in this save; initializing\n");
config = World::AddPersistentData(CONFIG_KEY);
}
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
DEBUG(status, out).print("loading persisted enabled state: %s\n", is_enabled ? "true" : "false");
2023-01-23 11:35:26 -07:00
vector<PersistentDataItem> loaded_persist_data;
World::GetPersistentData(&loaded_persist_data, STOCKPILE_CONFIG_KEY_PREFIX, true);
watched_stockpiles.clear();
const size_t num_watched_stockpiles = loaded_persist_data.size();
2023-01-20 15:58:38 -07:00
for (size_t idx = 0; idx < num_watched_stockpiles; ++idx)
{
2023-01-23 11:35:26 -07:00
auto &c = loaded_persist_data[idx];
watched_stockpiles.emplace(get_config_val(c, STOCKPILE_CONFIG_ID), c);
2014-06-04 04:12:30 -06:00
}
2023-01-20 15:58:38 -07:00
validate_stockpile_configs(out);
return CR_OK;
}
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
if (event == DFHack::SC_WORLD_UNLOADED)
2014-06-04 04:12:30 -06:00
{
2023-01-20 15:58:38 -07:00
if (is_enabled)
2014-06-04 04:12:30 -06:00
{
2023-01-20 15:58:38 -07:00
DEBUG(status, out).print("world unloaded; disabling %s\n", plugin_name);
is_enabled = false;
2014-06-04 04:12:30 -06:00
}
}
2023-01-21 00:08:58 -07:00
2023-01-20 15:58:38 -07:00
return CR_OK;
}
DFhackCExport command_result plugin_onupdate(color_ostream &out)
{
2023-01-21 16:22:15 -07:00
if (!Core::getInstance().isWorldLoaded())
return CR_OK;
2023-01-20 15:58:38 -07:00
if (is_enabled && world->frame_counter - cycle_timestamp >= CYCLE_TICKS)
{
int32_t marked = do_cycle(out);
if (0 < marked)
out.print("automelt: marked %d item(s) for melting\n", marked);
}
return CR_OK;
}
static bool call_automelt_lua(color_ostream *out, const char *fn_name,
int nargs = 0, int nres = 0,
Lua::LuaLambda && args_lambda = Lua::DEFAULT_LUA_LAMBDA,
Lua::LuaLambda && res_lambda = Lua::DEFAULT_LUA_LAMBDA) {
DEBUG(status).print("calling automelt lua function: '%s'\n", fn_name);
CoreSuspender guard;
auto L = Lua::Core::State;
Lua::StackUnwinder top(L);
if (!out)
out = &Core::getInstance().getConsole();
return Lua::CallLuaModuleFunction(*out, L, "plugins.automelt", fn_name,
nargs, nres,
std::forward<Lua::LuaLambda&&>(args_lambda),
std::forward<Lua::LuaLambda&&>(res_lambda));
}
static command_result do_command(color_ostream &out, vector<string> &parameters) {
CoreSuspender suspend;
if (!Core::getInstance().isWorldLoaded()) {
out.printerr("Cannot run %s without a loaded world.\n", plugin_name);
return CR_FAILURE;
}
bool show_help = false;
if (!call_automelt_lua(&out, "parse_commandline", parameters.size(), 1,
[&](lua_State *L) {
for (const string &param : parameters)
Lua::Push(L, param);
},
[&](lua_State *L) {
show_help = !lua_toboolean(L, -1);
})) {
return CR_FAILURE;
}
return show_help ? CR_WRONG_USAGE : CR_OK;
}
static inline bool is_metal_item(df::item *item)
{
if (!item)
return false;
2023-01-20 15:58:38 -07:00
MaterialInfo mat(item);
return (mat.getCraftClass() == craft_material_class::Metal);
}
2023-01-22 02:39:41 -07:00
struct BadFlagsCanMelt {
uint32_t whole;
BadFlagsCanMelt() {
df::item_flags flags;
#define F(x) flags.bits.x = true;
F(dump); F(forbid); F(garbage_collect);
F(hostile); F(on_fire); F(rotten); F(trader);
F(in_building); F(construction); F(artifact);
F(spider_web); F(owned); F(in_job);
2023-01-22 02:39:41 -07:00
#undef F
whole = flags.whole;
}
};
struct BadFlagsMarkItem {
uint32_t whole;
BadFlagsMarkItem() {
df::item_flags flags;
#define F(x) flags.bits.x = true;
F(dump); F(forbid); F(garbage_collect);
F(hostile); F(on_fire); F(rotten); F(trader);
F(in_building); F(construction); F(artifact);
F(spider_web); F(owned);
#undef F
whole = flags.whole;
}
};
2023-01-20 15:58:38 -07:00
// Copied from Kelly Martin's code
static inline bool can_melt(df::item *item)
{
2023-01-22 02:39:41 -07:00
static const BadFlagsCanMelt bad_flags;
2023-01-20 15:58:38 -07:00
if (!is_metal_item(item))
return false;
2023-01-20 15:58:38 -07:00
if (item->flags.whole & bad_flags.whole)
return false;
df::item_type t = item->getType();
2023-03-23 01:59:05 -06:00
if (t == df::enums::item_type::BAR)
2023-01-20 15:58:38 -07:00
return false;
2023-01-22 02:39:41 -07:00
for (auto &g : item->general_refs)
2014-06-04 04:12:30 -06:00
{
2023-01-22 02:39:41 -07:00
switch (g->getType())
2023-01-20 15:58:38 -07:00
{
case general_ref_type::CONTAINS_ITEM:
case general_ref_type::UNIT_HOLDER:
case general_ref_type::CONTAINS_UNIT:
return false;
case general_ref_type::CONTAINED_IN_ITEM:
2014-06-04 04:12:30 -06:00
{
2023-01-22 02:39:41 -07:00
df::item *c = g->getItem();
for (auto &gg : c->general_refs)
2014-06-04 04:12:30 -06:00
{
2023-01-22 02:39:41 -07:00
if (gg->getType() == general_ref_type::UNIT_HOLDER)
2023-01-20 15:58:38 -07:00
return false;
2014-06-04 04:12:30 -06:00
}
}
2023-01-20 15:58:38 -07:00
break;
default:
break;
}
2014-06-04 04:12:30 -06:00
}
2023-01-20 15:58:38 -07:00
if (item->getQuality() >= item_quality::Masterful)
return false;
return true;
}
static inline bool is_set_to_melt(df::item *item)
{
return item->flags.bits.melt;
}
2014-06-04 04:12:30 -06:00
2023-01-22 02:44:33 -07:00
static int mark_item(color_ostream &out, df::item *item, BadFlagsMarkItem bad_flags, int32_t stockpile_id,
2023-01-22 02:13:23 -07:00
int32_t &premarked_item_count, int32_t &item_count, map<int32_t, bool> &tracked_item_map, bool should_melt)
2023-01-20 15:58:38 -07:00
{
DEBUG(perf,out).print("%s running mark_item: should_melt=%d\n", plugin_name, should_melt);
2023-01-22 02:44:33 -07:00
2023-01-22 02:39:41 -07:00
if (DBG_NAME(perf).isEnabled(DebugCategory::LDEBUG)) {
string name = "";
item->getItemDescription(&name, 0);
DEBUG(perf,out).print("item %s %d\n", name.c_str(), item->id);
}
2023-01-20 15:58:38 -07:00
if (item->flags.whole & bad_flags.whole){
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("rejected flag check\n");
2023-01-20 15:58:38 -07:00
return 0;
2014-06-04 04:12:30 -06:00
}
2023-01-20 15:58:38 -07:00
if (item->isAssignedToThisStockpile(stockpile_id))
2014-06-04 04:12:30 -06:00
{
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("assignedToStockpile\n");
2023-01-20 15:58:38 -07:00
size_t marked_count = 0;
vector<df::item *> contents;
2023-01-20 15:58:38 -07:00
Items::getContainedItems(item, &contents);
for (auto child = contents.begin(); child != contents.end(); child++)
2014-06-04 04:12:30 -06:00
{
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("inside child loop\n");
2023-01-22 02:13:23 -07:00
marked_count += mark_item(out, *child, bad_flags, stockpile_id, premarked_item_count, item_count, tracked_item_map, should_melt);
2014-06-04 04:12:30 -06:00
}
2023-01-20 15:58:38 -07:00
return marked_count;
2014-06-04 04:12:30 -06:00
}
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("past recurse loop\n");
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
if (is_set_to_melt(item)) {
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("already set to melt\n");
2023-01-22 02:13:23 -07:00
tracked_item_map.emplace(item->id, true);
2023-01-20 15:58:38 -07:00
premarked_item_count++;
2023-01-22 02:13:23 -07:00
DEBUG(perf,out).print("premarked_item_count=%d\n", premarked_item_count);
2023-01-20 15:58:38 -07:00
item_count++;
return 0;
}
if (!can_melt(item)) {
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("cannot melt\n");
2023-01-20 15:58:38 -07:00
return 0;
}
2014-06-04 04:12:30 -06:00
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("increment item count\n");
2023-01-20 15:58:38 -07:00
item_count++;
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
//Only melt if told to, else count
if (should_melt) {
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("should_melt hit\n");
2023-01-20 15:58:38 -07:00
insert_into_vector(world->items.other[items_other_id::ANY_MELT_DESIGNATED], &df::item::id, item);
item->flags.bits.melt = 1;
2023-01-22 02:13:23 -07:00
tracked_item_map.emplace(item->id, true);
2023-01-20 15:58:38 -07:00
return 1;
} else {
return 0;
}
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
}
2023-01-22 02:13:23 -07:00
static int32_t mark_all_in_stockpile(color_ostream &out, PersistentDataItem & stockpile, int32_t &premarked_item_count, int32_t &item_count, map<int32_t, bool> &tracked_item_map, bool should_melt)
2014-06-04 04:12:30 -06:00
{
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("%s running mark_all_in_stockpile\nshould_melt=%d\n", plugin_name, should_melt);
2023-01-22 02:44:33 -07:00
2023-01-22 02:39:41 -07:00
static const BadFlagsMarkItem bad_flags;
2014-06-04 04:12:30 -06:00
2023-01-21 00:08:58 -07:00
int32_t marked_count = 0;
2023-01-20 15:58:38 -07:00
if(!stockpile.isValid()) {
return 0;
}
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
int spid = get_config_val(stockpile, STOCKPILE_CONFIG_ID);
auto found = df::building::find(spid);
if (!isStockpile(found))
2023-01-20 15:58:38 -07:00
return 0;
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
df::building_stockpilest * pile_cast = virtual_cast<df::building_stockpilest>(found);
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
if (!pile_cast)
return 0;
Buildings::StockpileIterator stored;
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("starting item iter. loop\n");
2023-01-20 15:58:38 -07:00
for (stored.begin(pile_cast); !stored.done(); ++stored) {
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("calling mark_item\n");
2023-01-22 02:13:23 -07:00
marked_count += mark_item(out, *stored, bad_flags, spid, premarked_item_count, item_count, tracked_item_map, should_melt);
DEBUG(perf,out).print("end mark_item call\npremarked_item_count=%d\n", premarked_item_count);
2023-01-20 15:58:38 -07:00
}
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("end item iter. loop\n");
DEBUG(perf,out).print("exit mark_all_in_stockpile\nmarked_count %d\npremarked_count %d\n", marked_count, premarked_item_count);
2023-01-20 15:58:38 -07:00
return marked_count;
2014-06-04 04:12:30 -06:00
}
2023-01-22 02:44:33 -07:00
static int32_t scan_stockpiles(color_ostream &out, bool should_melt, map<int32_t, int32_t> &item_count_piles, map<int32_t, int32_t> &premarked_item_count_piles,
2023-01-22 02:13:23 -07:00
map<int32_t, int32_t> &marked_item_count_piles, map<int32_t, bool> &tracked_item_map) {
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("running scan_stockpiles\n");
2023-01-20 15:58:38 -07:00
int32_t newly_marked = 0;
2023-01-21 00:08:58 -07:00
2023-01-22 02:13:23 -07:00
item_count_piles.clear();
premarked_item_count_piles.clear();
marked_item_count_piles.clear();
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
//Parse all the watched piles
for (auto &c : watched_stockpiles) {
2023-01-23 11:35:26 -07:00
int id = get_config_val(c.second, STOCKPILE_CONFIG_ID);
2023-01-21 16:22:15 -07:00
//Check monitor status
2023-01-23 11:35:26 -07:00
bool monitored = get_config_bool(c.second, STOCKPILE_CONFIG_MONITORED);
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
if (!monitored) continue;
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("%s past monitor check\nmonitored=%d\n", plugin_name, monitored);
2023-01-20 15:58:38 -07:00
int32_t item_count = 0;
2023-01-21 00:08:58 -07:00
int32_t premarked_count = 0;
2023-01-20 15:58:38 -07:00
2023-01-23 11:35:26 -07:00
int32_t marked = mark_all_in_stockpile(out, c.second, premarked_count, item_count, tracked_item_map, should_melt);
2023-01-22 02:13:23 -07:00
DEBUG(perf,out).print("post mark_all_in_stockpile premarked_count=%d\n", premarked_count);
2023-01-20 15:58:38 -07:00
2023-01-22 02:13:23 -07:00
item_count_piles.emplace(id, item_count);
2023-01-20 15:58:38 -07:00
2023-01-22 02:13:23 -07:00
premarked_item_count_piles.emplace(id, premarked_count);
2023-01-20 15:58:38 -07:00
2023-01-22 02:13:23 -07:00
marked_item_count_piles.emplace(id, marked);
2023-01-20 15:58:38 -07:00
newly_marked += marked;
2014-06-04 04:12:30 -06:00
}
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("exit scan_stockpiles\n");
2023-01-20 15:58:38 -07:00
return newly_marked;
}
2023-01-22 02:13:23 -07:00
static int32_t scan_all_melt_designated(color_ostream &out, map<int32_t, bool> &tracked_item_map) {
DEBUG(perf,out).print("running scan_all_melt_designated\n");
int32_t marked_item_count = 0;
//loop over all items marked as melt-designated
for (auto item : world->items.other[items_other_id::ANY_MELT_DESIGNATED]) {
//item has already been marked/counted as inside a stockpile. Don't double-count.
if (tracked_item_map.count(item->id)) {
continue;
}
marked_item_count++;
}
DEBUG(perf,out).print("exiting scan_all_melt_designated\n");
return marked_item_count;
}
static int32_t scan_count_all(color_ostream &out, bool should_melt, int32_t &marked_item_count_total, int32_t &marked_total_count_all_piles, int32_t &marked_item_count_global,
int32_t &total_items_all_piles, map<int32_t, int32_t> &item_count_piles, map<int32_t, int32_t> &premarked_item_count_piles, map<int32_t, int32_t> &marked_item_count_piles) {
2023-01-22 02:44:33 -07:00
2023-01-22 02:13:23 -07:00
//Wraps both scan_stockpiles and scan_all_melt_designated
//Returns count of items in piles freshly marked
int32_t newly_marked_items_piles = 0;
map<int32_t, bool> tracked_item_map_piles;
newly_marked_items_piles = scan_stockpiles(out, should_melt, item_count_piles, premarked_item_count_piles, marked_item_count_piles, tracked_item_map_piles);
marked_item_count_global = scan_all_melt_designated(out, tracked_item_map_piles);
for (auto &i : watched_stockpiles) {
2023-01-23 11:35:26 -07:00
int id = get_config_val(i.second, STOCKPILE_CONFIG_ID);
2023-01-22 02:13:23 -07:00
total_items_all_piles+= item_count_piles[id];
marked_total_count_all_piles += premarked_item_count_piles[id];
}
marked_item_count_total = marked_item_count_global + marked_total_count_all_piles;
2023-01-22 02:44:33 -07:00
2023-01-22 02:13:23 -07:00
return newly_marked_items_piles;
}
2023-01-20 15:58:38 -07:00
static int32_t do_cycle(color_ostream &out) {
DEBUG(cycle,out).print("running %s cycle\n", plugin_name);
cycle_timestamp = world->frame_counter;
validate_stockpile_configs(out);
2023-01-22 02:13:23 -07:00
int32_t marked_item_count_total = 0;
int32_t marked_item_count_global = 0;
int32_t newly_marked_items_piles = 0;
int32_t total_items_all_piles = 0;
int32_t marked_total_count_all_piles = 0;
map<int32_t, int32_t> item_count_piles, premarked_item_count_piles, marked_item_count_piles;
newly_marked_items_piles = scan_count_all(out, true, marked_item_count_total, marked_total_count_all_piles, marked_item_count_global,
total_items_all_piles, item_count_piles, premarked_item_count_piles, marked_item_count_piles);
2023-01-20 15:58:38 -07:00
2023-01-21 16:22:15 -07:00
DEBUG(perf,out).print("exit %s do_cycle\n", plugin_name);
2023-01-22 02:13:23 -07:00
return newly_marked_items_piles;
2023-01-20 15:58:38 -07:00
}
2023-01-21 16:22:15 -07:00
static int getSelectedStockpile(color_ostream &out) {
df::building *bld = Gui::getSelectedBuilding(out, true);
if (!isStockpile(bld)) {
2023-01-21 16:22:15 -07:00
DEBUG(status,out).print("Selected building is not stockpile\n");
return -1;
}
return bld->id;
2023-01-21 16:22:15 -07:00
}
static PersistentDataItem *getSelectedStockpileConfig(color_ostream &out) {
int32_t bldg_id = getSelectedStockpile(out);
if (bldg_id == -1) {
return NULL;
}
validate_stockpile_configs(out);
PersistentDataItem *c = NULL;
2023-01-23 11:35:26 -07:00
if (watched_stockpiles.count(bldg_id)) {
c = &(watched_stockpiles[bldg_id]);
2023-01-21 16:22:15 -07:00
return c;
}
DEBUG(status,out).print("No existing config\n");
return NULL;
2023-01-21 16:22:15 -07:00
}
static void push_stockpile_config(lua_State *L, int id, bool monitored) {
2023-01-20 15:58:38 -07:00
map<string, int32_t> stockpile_config;
stockpile_config.emplace("id", id);
stockpile_config.emplace("monitored", monitored);
Lua::Push(L, stockpile_config);
}
static void push_stockpile_config(lua_State *L, PersistentDataItem &c) {
push_stockpile_config(L, get_config_val(c, STOCKPILE_CONFIG_ID),
2023-01-21 16:22:15 -07:00
get_config_bool(c, STOCKPILE_CONFIG_MONITORED));
2023-01-20 15:58:38 -07:00
}
2023-02-08 12:01:32 -07:00
static void emplace_bulk_stockpile_config(lua_State *L, int id, bool monitored, map<int32_t, map<string, int32_t>> &stockpiles) {
map<string, int32_t> stockpile_config;
stockpile_config.emplace("id", id);
stockpile_config.emplace("monitored", monitored);
stockpiles.emplace(id, stockpile_config);
}
2023-02-08 12:01:32 -07:00
static void emplace_bulk_stockpile_config(lua_State *L, PersistentDataItem &c, map<int32_t, map<string, int32_t>> &stockpiles) {
int32_t id = get_config_val(c, STOCKPILE_CONFIG_ID);
bool monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED);
emplace_bulk_stockpile_config(L, id, monitored, stockpiles);
}
2023-01-20 15:58:38 -07:00
static void automelt_designate(color_ostream &out) {
DEBUG(status, out).print("entering automelt designate\n");
out.print("designated %d item(s) for melting\n", do_cycle(out));
}
static void automelt_printStatus(color_ostream &out) {
DEBUG(status,out).print("entering automelt_printStatus\n");
validate_stockpile_configs(out);
out.print("automelt is %s\n\n", is_enabled ? "enabled" : "disabled");
2023-01-22 02:13:23 -07:00
int32_t marked_item_count_total = 0;
int32_t marked_item_count_global = 0;
2023-01-20 15:58:38 -07:00
int32_t total_items_all_piles = 0;
2023-01-22 02:13:23 -07:00
int32_t marked_total_count_all_piles = 0;
map<int32_t, int32_t> item_count_piles, premarked_item_count_piles, marked_item_count_piles;
2023-01-20 15:58:38 -07:00
2023-01-22 02:13:23 -07:00
scan_count_all(out, false, marked_item_count_total, marked_total_count_all_piles, marked_item_count_global,
total_items_all_piles, item_count_piles, premarked_item_count_piles, marked_item_count_piles);
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
out.print("summary:\n");
2023-01-22 02:13:23 -07:00
out.print(" total items in monitored piles: %d\n", total_items_all_piles);
out.print(" marked items in monitored piles: %d\n", marked_total_count_all_piles);
out.print("marked items global (excludes those in monitored piles): %d\n", marked_item_count_global);
out.print(" marked items global (monitored piles + others): %d\n", marked_item_count_total);
2023-01-20 15:58:38 -07:00
int name_width = 11;
for (auto &stockpile : world->buildings.other.STOCKPILE) {
if (!isStockpile(stockpile)) continue;
if (stockpile->name.empty()) {
string stock_name = "Stockpile #" + int_to_string(stockpile->stockpile_number);
name_width = std::max(name_width, (int)(stock_name.size()));
} else {
name_width = std::max(name_width, (int)stockpile->name.size());
}
2023-01-20 15:58:38 -07:00
}
name_width = -name_width;
const char *fmt = "%*s %9s %5s %6s\n";
out.print(fmt, name_width, "name", "monitored", "items", "marked");
out.print(fmt, name_width, "----", "---------", "-----", "------");
2023-01-20 15:58:38 -07:00
for (auto &stockpile : world->buildings.other.STOCKPILE) {
if (!isStockpile(stockpile)) continue;
bool monitored = false;
2023-01-22 02:13:23 -07:00
int32_t item_count = 0;
int32_t marked_item_count = 0;
2023-01-23 11:35:26 -07:00
if (watched_stockpiles.count(stockpile->id)) {
auto &c = watched_stockpiles[stockpile->id];
2023-01-20 15:58:38 -07:00
monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED);
2023-01-22 02:13:23 -07:00
int id = get_config_val(c, STOCKPILE_CONFIG_ID);
item_count = item_count_piles[id];
marked_item_count = premarked_item_count_piles[id];
}
2023-01-22 02:13:23 -07:00
if (stockpile->name.empty()) {
string stock_name = "Stockpile #" + int_to_string(stockpile->stockpile_number);
2023-01-23 14:20:37 -07:00
out.print(fmt, name_width, stock_name.c_str(), monitored ? "[x]": "[ ]",
int_to_string(item_count).c_str(), int_to_string(marked_item_count).c_str());
} else {
out.print(fmt, name_width, stockpile->name.c_str(), monitored ? "[x]": "[ ]",
int_to_string(item_count).c_str(), int_to_string(marked_item_count).c_str());
}
2023-01-20 15:58:38 -07:00
2014-06-04 04:12:30 -06:00
}
2023-01-21 16:22:15 -07:00
DEBUG(status,out).print("exiting automelt_printStatus\n");
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
}
2014-06-04 04:12:30 -06:00
2023-01-21 16:22:15 -07:00
static void automelt_setStockpileConfig(color_ostream &out, int id, bool monitored) {
2023-01-23 11:35:26 -07:00
DEBUG(status,out).print("entering automelt_setStockpileConfig for id=%d and monitored=%d\n", id, monitored);
2023-01-20 15:58:38 -07:00
validate_stockpile_configs(out);
2023-01-23 14:14:49 -07:00
auto bldg = df::building::find(id);
bool isInvalidStockpile = !isStockpile(bldg);
2023-01-21 16:22:15 -07:00
bool hasNoData = !monitored;
2023-01-20 15:58:38 -07:00
if (isInvalidStockpile || hasNoData) {
2023-01-23 11:35:26 -07:00
DEBUG(cycle,out).print("calling remove_stockpile_config with id=%d monitored=%d\n", id, monitored);
2023-01-20 15:58:38 -07:00
remove_stockpile_config(out, id);
return;
2023-01-21 00:08:58 -07:00
}
2023-01-20 15:58:38 -07:00
PersistentDataItem &c = ensure_stockpile_config(out, id);
set_config_val(c, STOCKPILE_CONFIG_ID, id);
set_config_bool(c, STOCKPILE_CONFIG_MONITORED, monitored);
//If we're marking something as monitored, go ahead and designate contents.
if (monitored) {
automelt_designate(out);
}
2023-01-20 15:58:38 -07:00
}
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
static int automelt_getStockpileConfig(lua_State *L) {
color_ostream *out = Lua::GetOutput(L);
if (!out)
out = &Core::getInstance().getConsole();
DEBUG(status, *out).print("entering automelt_getStockpileConfig\n");
validate_stockpile_configs(*out);
int id;
if (lua_isnumber(L, -1)) {
bool found = false;
2023-01-20 15:58:38 -07:00
id = lua_tointeger(L, -1);
2023-01-22 23:58:57 -07:00
for (auto &stockpile : world->buildings.other.STOCKPILE) {
if (!isStockpile(stockpile)) continue;
if (id == stockpile->stockpile_number){
id = stockpile->id;
found = true;
break;
}
}
2023-01-22 23:58:57 -07:00
if (!found)
2023-01-20 15:58:38 -07:00
return 0;
2023-01-21 00:08:58 -07:00
2023-01-20 15:58:38 -07:00
} else {
const char * name = lua_tostring(L, -1);
if (!name)
return 0;
string nameStr = name;
bool found = false;
for (auto &stockpile : world->buildings.other.STOCKPILE) {
if (!isStockpile(stockpile)) continue;
if (nameStr == stockpile->name) {
id = stockpile->id;
found = true;
break;
} else {
string stock_name = "Stockpile #" + int_to_string(stockpile->stockpile_number);
if (stock_name == nameStr) {
id = stockpile->id;
found = true;
break;
}
2023-01-20 15:58:38 -07:00
}
}
if (!found)
return 0;
2014-06-04 04:12:30 -06:00
}
2023-01-20 15:58:38 -07:00
2023-01-23 11:35:26 -07:00
if (watched_stockpiles.count(id)) {
push_stockpile_config(L, watched_stockpiles[id]);
2023-01-20 15:58:38 -07:00
} else {
2023-01-21 16:22:15 -07:00
push_stockpile_config(L, id, false);
2023-01-20 15:58:38 -07:00
}
return 1;
2014-06-04 04:12:30 -06:00
}
2023-01-21 16:22:15 -07:00
static int automelt_getSelectedStockpileConfig(lua_State *L){
color_ostream *out = Lua::GetOutput(L);
if (!out)
out = &Core::getInstance().getConsole();
DEBUG(status, *out).print("entering automelt_getSelectedStockpileConfig\n");
validate_stockpile_configs(*out);
int32_t stock_id = getSelectedStockpile(*out);
PersistentDataItem *c = getSelectedStockpileConfig(*out);
//If we have a valid config entry, use that. Else assume all false.
if (c) {
push_stockpile_config(L, *c);
} else {
push_stockpile_config(L, stock_id, false);
}
2023-01-22 02:55:14 -07:00
return 1;
2023-01-21 16:22:15 -07:00
}
2023-01-20 15:58:38 -07:00
static int automelt_getItemCountsAndStockpileConfigs(lua_State *L) {
color_ostream *out = Lua::GetOutput(L);
if (!out)
out = &Core::getInstance().getConsole();
DEBUG(status,*out).print("entering automelt_getItemCountsAndStockpileConfigs\n");
validate_stockpile_configs(*out);
2014-06-04 04:12:30 -06:00
2023-01-22 02:13:23 -07:00
int32_t marked_item_count_total = 0;
int32_t marked_item_count_global = 0;
2023-01-20 15:58:38 -07:00
int32_t total_items_all_piles = 0;
2023-01-22 02:13:23 -07:00
int32_t marked_total_count_all_piles = 0;
map<int32_t, int32_t> item_count_piles, premarked_item_count_piles, marked_item_count_piles;
2023-01-20 15:58:38 -07:00
2023-01-22 02:13:23 -07:00
scan_count_all(*out, false, marked_item_count_total, marked_total_count_all_piles, marked_item_count_global,
total_items_all_piles, item_count_piles, premarked_item_count_piles, marked_item_count_piles);
2014-06-04 04:12:30 -06:00
2023-01-20 15:58:38 -07:00
map<string, int32_t> summary;
summary.emplace("total_items", total_items_all_piles);
2023-01-22 02:13:23 -07:00
summary.emplace("premarked_items", marked_total_count_all_piles);
summary.emplace("marked_item_count_global", marked_item_count_global);
summary.emplace("marked_item_count_total", marked_item_count_total);
2023-01-21 00:08:58 -07:00
2023-01-20 15:58:38 -07:00
Lua::Push(L, summary);
2023-01-22 02:13:23 -07:00
Lua::Push(L, item_count_piles);
Lua::Push(L, marked_item_count_piles);
Lua::Push(L, premarked_item_count_piles);
map<int32_t, map<string, int32_t>> stockpile_config_map;
2023-01-21 00:08:58 -07:00
2023-01-20 15:58:38 -07:00
for (auto pile : world->buildings.other.STOCKPILE) {
if (!isStockpile(pile))
continue;
2023-01-21 00:08:58 -07:00
2023-01-20 15:58:38 -07:00
int id = pile->id;
2023-01-23 11:35:26 -07:00
if (watched_stockpiles.count(id)) {
2023-02-08 12:01:32 -07:00
emplace_bulk_stockpile_config(L, watched_stockpiles[id], stockpile_config_map);
2023-01-20 15:58:38 -07:00
} else {
2023-02-08 12:01:32 -07:00
emplace_bulk_stockpile_config(L, id, false, stockpile_config_map);
2023-01-20 15:58:38 -07:00
}
}
Lua::Push(L, stockpile_config_map);
2023-01-21 16:22:15 -07:00
DEBUG(perf, *out).print("exit automelt_getItemCountsAndStockpileConfigs\n");
2014-06-04 04:12:30 -06:00
return 5;
2014-06-04 04:12:30 -06:00
}
2023-01-20 15:58:38 -07:00
DFHACK_PLUGIN_LUA_FUNCTIONS{
DFHACK_LUA_FUNCTION(automelt_printStatus),
DFHACK_LUA_FUNCTION(automelt_designate),
DFHACK_LUA_FUNCTION(automelt_setStockpileConfig),
DFHACK_LUA_END};
DFHACK_PLUGIN_LUA_COMMANDS{
DFHACK_LUA_COMMAND(automelt_getStockpileConfig),
DFHACK_LUA_COMMAND(automelt_getItemCountsAndStockpileConfigs),
2023-01-22 02:55:14 -07:00
DFHACK_LUA_COMMAND(automelt_getSelectedStockpileConfig),
2023-01-21 00:15:35 -07:00
DFHACK_LUA_END};