diff --git a/plugins/automelt.cpp b/plugins/automelt.cpp index c78f36f51..e9bd30f76 100644 --- a/plugins/automelt.cpp +++ b/plugins/automelt.cpp @@ -57,8 +57,10 @@ static const string CONFIG_KEY = string(plugin_name) + "/config"; static const string STOCKPILE_CONFIG_KEY_PREFIX = string(plugin_name) + "/stockpile/"; static PersistentDataItem config; -static vector watched_stockpiles; -static unordered_map watched_stockpiles_indices; +// static vector watched_stockpiles; +// static unordered_map watched_stockpiles_indices; + +static unordered_map watched_stockpiles; enum StockpileConfigValues { @@ -92,31 +94,31 @@ static void set_config_bool(PersistentDataItem &c, int index, bool value) static PersistentDataItem &ensure_stockpile_config(color_ostream &out, int id) { - if (watched_stockpiles_indices.count(id)) - return watched_stockpiles[watched_stockpiles_indices[id]]; + 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]; + } + string keyname = STOCKPILE_CONFIG_KEY_PREFIX + int_to_string(id); - DEBUG(status, out).print("creating new persistent key for stockpile %d\n", id); - watched_stockpiles.emplace_back(World::GetPersistentData(keyname, NULL)); - size_t idx = watched_stockpiles.size() - 1; - watched_stockpiles_indices.emplace(id, idx); - return watched_stockpiles[idx]; + 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]; } static void remove_stockpile_config(color_ostream &out, int id) { - if (!watched_stockpiles_indices.count(id)) + if (!watched_stockpiles.count(id)) return; DEBUG(status, out).print("removing persistent key for stockpile %d\n", id); - size_t idx = watched_stockpiles_indices[id]; - World::DeletePersistentData(watched_stockpiles[idx]); - watched_stockpiles.erase(watched_stockpiles.begin() + idx); - watched_stockpiles_indices.erase(id); + World::DeletePersistentData(watched_stockpiles[id]); + watched_stockpiles.erase(id); } static void validate_stockpile_configs(color_ostream &out) { for (auto &c : watched_stockpiles) { - int id = get_config_val(c, STOCKPILE_CONFIG_ID); + int id = get_config_val(c.second, STOCKPILE_CONFIG_ID); if (!df::building::find(id)){ remove_stockpile_config(out, id); } @@ -174,13 +176,14 @@ DFhackCExport command_result plugin_load_data(color_ostream &out) } DEBUG(status, out).print("loading persisted enabled state: %s\n", is_enabled ? "true" : "false"); - World::GetPersistentData(&watched_stockpiles, STOCKPILE_CONFIG_KEY_PREFIX, true); - watched_stockpiles_indices.clear(); - const size_t num_watched_stockpiles = watched_stockpiles.size(); + vector 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(); for (size_t idx = 0; idx < num_watched_stockpiles; ++idx) { - auto &c = watched_stockpiles[idx]; - watched_stockpiles_indices.emplace(get_config_val(c, STOCKPILE_CONFIG_ID), idx); + auto &c = loaded_persist_data[idx]; + watched_stockpiles.emplace(get_config_val(c, STOCKPILE_CONFIG_ID), c); } validate_stockpile_configs(out); @@ -460,9 +463,9 @@ static int32_t scan_stockpiles(color_ostream &out, bool should_melt, mapid)) { - auto &c = watched_stockpiles[watched_stockpiles_indices[stockpile->id]]; + if (watched_stockpiles.count(stockpile->id)) { + auto &c = watched_stockpiles[stockpile->id]; monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED); int id = get_config_val(c, STOCKPILE_CONFIG_ID); item_count = item_count_piles[id]; @@ -666,11 +669,12 @@ static void automelt_printStatus(color_ostream &out) { } static void automelt_setStockpileConfig(color_ostream &out, int id, bool monitored) { - DEBUG(status,out).print("entering automelt_setStockpileConfig\n"); + DEBUG(status,out).print("entering automelt_setStockpileConfig for id=%d and monitored=%d\n", id, monitored); validate_stockpile_configs(out); - bool isInvalidStockpile = !df::building::find(id); + bool isInvalidStockpile = !df::building::find(id) || !isStockpile(df::building::find(id)); bool hasNoData = !monitored; if (isInvalidStockpile || hasNoData) { + DEBUG(cycle,out).print("calling remove_stockpile_config with id=%d monitored=%d\n", id, monitored); remove_stockpile_config(out, id); return; } @@ -703,14 +707,12 @@ static int automelt_getStockpileConfig(lua_State *L) { id = stockpile->id; found = true; break; - } } if (!found) return 0; - } else { const char * name = lua_tostring(L, -1); if (!name) @@ -732,14 +734,13 @@ static int automelt_getStockpileConfig(lua_State *L) { } } - } if (!found) return 0; } - if (watched_stockpiles_indices.count(id)) { - push_stockpile_config(L, watched_stockpiles[watched_stockpiles_indices[id]]); + if (watched_stockpiles.count(id)) { + push_stockpile_config(L, watched_stockpiles[id]); } else { push_stockpile_config(L, id, false); } @@ -801,8 +802,11 @@ static int automelt_getItemCountsAndStockpileConfigs(lua_State *L) { bldg_count++; int id = pile->id; - if (watched_stockpiles_indices.count(id)) { - push_stockpile_config(L, watched_stockpiles[watched_stockpiles_indices[id]]); + DEBUG(cycle,*out).print("id=%d\ncount_res=%d\n", id, watched_stockpiles.count(id)); + + if (watched_stockpiles.count(id)) { + DEBUG(cycle,*out).print("indexed_id=%d\n", get_config_val(watched_stockpiles[id], STOCKPILE_CONFIG_ID)); + push_stockpile_config(L, watched_stockpiles[id]); } else { push_stockpile_config(L, id, false); } diff --git a/plugins/lua/automelt.lua b/plugins/lua/automelt.lua index dc6c94f9b..d70cf2e8e 100644 --- a/plugins/lua/automelt.lua +++ b/plugins/lua/automelt.lua @@ -62,12 +62,17 @@ function getItemCountsAndStockpileConfigs() ret.premarked_item_counts = table.remove(data, 1) ret.stockpile_configs = data for _,c in ipairs(ret.stockpile_configs) do - c.name = df.building.find(c.id).name - if not c.name or c.name == '' then - c.name = (fmt):format(tostring(df.building.find(c.id).stockpile_number)) - c.name = c.name.gsub(c.name, '^%s*(.-)%s*$', '%1') + if not c.id or c.id == -1 then + c.name = "ERROR" + c.monitored = false + else + c.name = df.building.find(c.id).name + if not c.name or c.name == '' then + c.name = (fmt):format(tostring(df.building.find(c.id).stockpile_number)) + end + c.monitored = c.monitored ~= 0 end - c.monitored = c.monitored ~= 0 + end return ret end