add skeleton for autotrain

develop
Myk Taylor 2023-06-11 23:31:54 -07:00
parent ff7a2945eb
commit 37b07bcee6
No known key found for this signature in database
3 changed files with 71 additions and 20 deletions

@ -41,6 +41,7 @@ enum StockpileConfigValues {
STOCKPILE_CONFIG_MELT = 1, STOCKPILE_CONFIG_MELT = 1,
STOCKPILE_CONFIG_TRADE = 2, STOCKPILE_CONFIG_TRADE = 2,
STOCKPILE_CONFIG_DUMP = 3, STOCKPILE_CONFIG_DUMP = 3,
STOCKPILE_CONFIG_TRAIN = 4,
}; };
static int get_config_val(PersistentDataItem& c, int index) { static int get_config_val(PersistentDataItem& c, int index) {
@ -77,6 +78,7 @@ static PersistentDataItem& ensure_stockpile_config(color_ostream& out, int stock
set_config_bool(c, STOCKPILE_CONFIG_MELT, false); set_config_bool(c, STOCKPILE_CONFIG_MELT, false);
set_config_bool(c, STOCKPILE_CONFIG_TRADE, false); set_config_bool(c, STOCKPILE_CONFIG_TRADE, false);
set_config_bool(c, STOCKPILE_CONFIG_DUMP, false); set_config_bool(c, STOCKPILE_CONFIG_DUMP, false);
set_config_bool(c, STOCKPILE_CONFIG_TRAIN, false);
return c; return c;
} }
@ -92,7 +94,7 @@ static const int32_t CYCLE_TICKS = 600;
static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle
static command_result do_command(color_ostream &out, vector<string> &parameters); static command_result do_command(color_ostream &out, vector<string> &parameters);
static void do_cycle(color_ostream &out, int32_t &melt_count, int32_t &trade_count, int32_t &dump_count); static void do_cycle(color_ostream& out, int32_t& melt_count, int32_t& trade_count, int32_t& dump_count, int32_t& train_count);
DFhackCExport command_result plugin_init(color_ostream &out, vector<PluginCommand> &commands) { DFhackCExport command_result plugin_init(color_ostream &out, vector<PluginCommand> &commands) {
DEBUG(status, out).print("initializing %s\n", plugin_name); DEBUG(status, out).print("initializing %s\n", plugin_name);
@ -125,7 +127,8 @@ static void validate_stockpile_configs(color_ostream& out,
if (!bld || ( if (!bld || (
!get_config_bool(c, STOCKPILE_CONFIG_MELT) && !get_config_bool(c, STOCKPILE_CONFIG_MELT) &&
!get_config_bool(c, STOCKPILE_CONFIG_TRADE) && !get_config_bool(c, STOCKPILE_CONFIG_TRADE) &&
!get_config_bool(c, STOCKPILE_CONFIG_DUMP))) { !get_config_bool(c, STOCKPILE_CONFIG_DUMP) &&
!get_config_bool(c, STOCKPILE_CONFIG_TRAIN))) {
remove_stockpile_config(out, stockpile_number); remove_stockpile_config(out, stockpile_number);
continue; continue;
} }
@ -172,14 +175,16 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (!is_enabled || !Core::getInstance().isWorldLoaded()) if (!is_enabled || !Core::getInstance().isWorldLoaded())
return CR_OK; return CR_OK;
if (world->frame_counter - cycle_timestamp >= CYCLE_TICKS) { if (world->frame_counter - cycle_timestamp >= CYCLE_TICKS) {
int32_t melt_count = 0, trade_count = 0, dump_count = 0; int32_t melt_count = 0, trade_count = 0, dump_count = 0, train_count = 0;
do_cycle(out, melt_count, trade_count, dump_count); do_cycle(out, melt_count, trade_count, dump_count, train_count);
if (0 < melt_count) if (0 < melt_count)
out.print("logistics: marked %d item(s) for melting\n", melt_count); out.print("logistics: marked %d item(s) for melting\n", melt_count);
if (0 < trade_count) if (0 < trade_count)
out.print("logistics: marked %d item(s) for trading\n", trade_count); out.print("logistics: marked %d item(s) for trading\n", trade_count);
if (0 < dump_count) if (0 < dump_count)
out.print("logistics: marked %d item(s) for dumping\n", dump_count); out.print("logistics: marked %d item(s) for dumping\n", dump_count);
if (0 < train_count)
out.print("logistics: marked %d animal(s) for training\n", dump_count);
} }
return CR_OK; return CR_OK;
} }
@ -397,6 +402,24 @@ public:
} }
}; };
class TrainStockProcessor : public StockProcessor {
public:
TrainStockProcessor(int32_t stockpile_number, bool enabled, ProcessorStats& stats)
: StockProcessor("train", stockpile_number, enabled, stats) {}
bool is_designated(color_ostream& out, df::item* item) override {
return false;
}
bool can_designate(color_ostream& out, df::item* item) override {
return false;
}
bool designate(color_ostream& out, df::item* item) override {
return false;
}
};
static const struct BadFlags { static const struct BadFlags {
const uint32_t whole; const uint32_t whole;
@ -474,11 +497,11 @@ static void scan_stockpile(color_ostream &out, df::building_stockpilest *bld,
} }
} }
static void do_cycle(color_ostream &out, int32_t &melt_count, int32_t &trade_count, int32_t &dump_count) { static void do_cycle(color_ostream& out, int32_t& melt_count, int32_t& trade_count, int32_t& dump_count, int32_t& train_count) {
DEBUG(cycle,out).print("running %s cycle\n", plugin_name); DEBUG(cycle,out).print("running %s cycle\n", plugin_name);
cycle_timestamp = world->frame_counter; cycle_timestamp = world->frame_counter;
ProcessorStats melt_stats, trade_stats, dump_stats; ProcessorStats melt_stats, trade_stats, dump_stats, train_stats;
unordered_map<df::building_stockpilest *, PersistentDataItem> cache; unordered_map<df::building_stockpilest *, PersistentDataItem> cache;
validate_stockpile_configs(out, cache); validate_stockpile_configs(out, cache);
@ -490,10 +513,12 @@ static void do_cycle(color_ostream &out, int32_t &melt_count, int32_t &trade_cou
bool melt = get_config_bool(c, STOCKPILE_CONFIG_MELT); bool melt = get_config_bool(c, STOCKPILE_CONFIG_MELT);
bool trade = get_config_bool(c, STOCKPILE_CONFIG_TRADE); bool trade = get_config_bool(c, STOCKPILE_CONFIG_TRADE);
bool dump = get_config_bool(c, STOCKPILE_CONFIG_DUMP); bool dump = get_config_bool(c, STOCKPILE_CONFIG_DUMP);
bool train = get_config_bool(c, STOCKPILE_CONFIG_TRAIN);
MeltStockProcessor melt_stock_processor(stockpile_number, melt, melt_stats); MeltStockProcessor melt_stock_processor(stockpile_number, melt, melt_stats);
TradeStockProcessor trade_stock_processor(stockpile_number, trade, trade_stats); TradeStockProcessor trade_stock_processor(stockpile_number, trade, trade_stats);
DumpStockProcessor dump_stock_processor(stockpile_number, dump, dump_stats); DumpStockProcessor dump_stock_processor(stockpile_number, dump, dump_stats);
TrainStockProcessor train_stock_processor(stockpile_number, train, train_stats);
scan_stockpile(out, bld, melt_stock_processor, scan_stockpile(out, bld, melt_stock_processor,
trade_stock_processor, dump_stock_processor); trade_stock_processor, dump_stock_processor);
@ -518,13 +543,14 @@ static int logistics_getStockpileData(lua_State *L) {
unordered_map<df::building_stockpilest *, PersistentDataItem> cache; unordered_map<df::building_stockpilest *, PersistentDataItem> cache;
validate_stockpile_configs(*out, cache); validate_stockpile_configs(*out, cache);
ProcessorStats melt_stats, trade_stats, dump_stats; ProcessorStats melt_stats, trade_stats, dump_stats, train_stats;
for (auto bld : df::global::world->buildings.other.STOCKPILE) { for (auto bld : df::global::world->buildings.other.STOCKPILE) {
int32_t stockpile_number = bld->stockpile_number; int32_t stockpile_number = bld->stockpile_number;
MeltStockProcessor melt_stock_processor(stockpile_number, false, melt_stats); MeltStockProcessor melt_stock_processor(stockpile_number, false, melt_stats);
TradeStockProcessor trade_stock_processor(stockpile_number, false, trade_stats); TradeStockProcessor trade_stock_processor(stockpile_number, false, trade_stats);
DumpStockProcessor dump_stock_processor(stockpile_number, false, dump_stats); DumpStockProcessor dump_stock_processor(stockpile_number, false, dump_stats);
TrainStockProcessor train_stock_processor(stockpile_number, false, train_stats);
scan_stockpile(*out, bld, melt_stock_processor, scan_stockpile(*out, bld, melt_stock_processor,
trade_stock_processor, dump_stock_processor); trade_stock_processor, dump_stock_processor);
@ -537,6 +563,8 @@ static int logistics_getStockpileData(lua_State *L) {
stats.emplace("trade_can_designate", trade_stats.can_designate_counts); stats.emplace("trade_can_designate", trade_stats.can_designate_counts);
stats.emplace("dump_designated", dump_stats.designated_counts); stats.emplace("dump_designated", dump_stats.designated_counts);
stats.emplace("dump_can_designate", dump_stats.can_designate_counts); stats.emplace("dump_can_designate", dump_stats.can_designate_counts);
stats.emplace("train_designated", train_stats.designated_counts);
stats.emplace("train_can_designate", train_stats.can_designate_counts);
Lua::Push(L, stats); Lua::Push(L, stats);
unordered_map<int32_t, unordered_map<string, string>> configs; unordered_map<int32_t, unordered_map<string, string>> configs;
@ -547,11 +575,13 @@ static int logistics_getStockpileData(lua_State *L) {
bool melt = get_config_bool(c, STOCKPILE_CONFIG_MELT); bool melt = get_config_bool(c, STOCKPILE_CONFIG_MELT);
bool trade = get_config_bool(c, STOCKPILE_CONFIG_TRADE); bool trade = get_config_bool(c, STOCKPILE_CONFIG_TRADE);
bool dump = get_config_bool(c, STOCKPILE_CONFIG_DUMP); bool dump = get_config_bool(c, STOCKPILE_CONFIG_DUMP);
bool train = get_config_bool(c, STOCKPILE_CONFIG_TRAIN);
unordered_map<string, string> config; unordered_map<string, string> config;
config.emplace("melt", melt ? "true" : "false"); config.emplace("melt", melt ? "true" : "false");
config.emplace("trade", trade ? "true" : "false"); config.emplace("trade", trade ? "true" : "false");
config.emplace("dump", dump ? "true" : "false"); config.emplace("dump", dump ? "true" : "false");
config.emplace("train", train ? "true" : "false");
configs.emplace(bld->stockpile_number, config); configs.emplace(bld->stockpile_number, config);
} }
@ -564,11 +594,12 @@ static int logistics_getStockpileData(lua_State *L) {
static void logistics_cycle(color_ostream &out) { static void logistics_cycle(color_ostream &out) {
DEBUG(status, out).print("entering logistics_cycle\n"); DEBUG(status, out).print("entering logistics_cycle\n");
int32_t melt_count = 0, trade_count = 0, dump_count = 0; int32_t melt_count = 0, trade_count = 0, dump_count = 0, train_count = 0;
do_cycle(out, melt_count, trade_count, dump_count); do_cycle(out, melt_count, trade_count, dump_count, train_count);
out.print("logistics: marked %d item(s) for melting\n", melt_count); out.print("logistics: marked %d item(s) for melting\n", melt_count);
out.print("logistics: marked %d item(s) for trading\n", trade_count); out.print("logistics: marked %d item(s) for trading\n", trade_count);
out.print("logistics: marked %d item(s) for dumping\n", dump_count); out.print("logistics: marked %d item(s) for dumping\n", dump_count);
out.print("logistics: marked %d animal(s) for train\n", train_count);
} }
static void find_stockpiles(lua_State *L, int idx, static void find_stockpiles(lua_State *L, int idx,
@ -596,10 +627,12 @@ static unordered_map<string, int> get_stockpile_config(int32_t stockpile_number)
stockpile_config.emplace("melt", get_config_bool(c, STOCKPILE_CONFIG_MELT)); stockpile_config.emplace("melt", get_config_bool(c, STOCKPILE_CONFIG_MELT));
stockpile_config.emplace("trade", get_config_bool(c, STOCKPILE_CONFIG_TRADE)); stockpile_config.emplace("trade", get_config_bool(c, STOCKPILE_CONFIG_TRADE));
stockpile_config.emplace("dump", get_config_bool(c, STOCKPILE_CONFIG_DUMP)); stockpile_config.emplace("dump", get_config_bool(c, STOCKPILE_CONFIG_DUMP));
stockpile_config.emplace("train", get_config_bool(c, STOCKPILE_CONFIG_TRAIN));
} else { } else {
stockpile_config.emplace("melt", false); stockpile_config.emplace("melt", false);
stockpile_config.emplace("trade", false); stockpile_config.emplace("trade", false);
stockpile_config.emplace("dump", false); stockpile_config.emplace("dump", false);
stockpile_config.emplace("train", false);
} }
return stockpile_config; return stockpile_config;
} }
@ -625,9 +658,9 @@ static int logistics_getStockpileConfigs(lua_State *L) {
return 1; return 1;
} }
static void logistics_setStockpileConfig(color_ostream &out, int stockpile_number, bool melt, bool trade, bool dump) { static void logistics_setStockpileConfig(color_ostream& out, int stockpile_number, bool melt, bool trade, bool dump, bool train) {
DEBUG(status, out).print("entering logistics_setStockpileConfig stockpile_number=%d, melt=%d, trade=%d, dump=%d\n", DEBUG(status, out).print("entering logistics_setStockpileConfig stockpile_number=%d, melt=%d, trade=%d, dump=%d, train=%d\n",
stockpile_number, melt, trade, dump); stockpile_number, melt, trade, dump, train);
if (!find_stockpile(stockpile_number)) { if (!find_stockpile(stockpile_number)) {
out.printerr("invalid stockpile number: %d\n", stockpile_number); out.printerr("invalid stockpile number: %d\n", stockpile_number);
@ -638,6 +671,7 @@ static void logistics_setStockpileConfig(color_ostream &out, int stockpile_numbe
set_config_bool(c, STOCKPILE_CONFIG_MELT, melt); set_config_bool(c, STOCKPILE_CONFIG_MELT, melt);
set_config_bool(c, STOCKPILE_CONFIG_TRADE, trade); set_config_bool(c, STOCKPILE_CONFIG_TRADE, trade);
set_config_bool(c, STOCKPILE_CONFIG_DUMP, dump); set_config_bool(c, STOCKPILE_CONFIG_DUMP, dump);
set_config_bool(c, STOCKPILE_CONFIG_TRAIN, train);
} }
static int logistics_clearStockpileConfig(lua_State *L) { static int logistics_clearStockpileConfig(lua_State *L) {
@ -684,10 +718,14 @@ static int logistics_getGlobalCounts(lua_State *L) {
++num_dump; ++num_dump;
} }
size_t num_train = 0;
// TODO
unordered_map<string, size_t> results; unordered_map<string, size_t> results;
results.emplace("total_melt", num_melt); results.emplace("total_melt", num_melt);
results.emplace("total_trade", num_trade); results.emplace("total_trade", num_trade);
results.emplace("total_dump", num_dump); results.emplace("total_dump", num_dump);
results.emplace("total_train", num_train);
Lua::Push(L, results); Lua::Push(L, results);
TRACE(cycle, *out).print("exit logistics_getGlobalCounts\n"); TRACE(cycle, *out).print("exit logistics_getGlobalCounts\n");

@ -28,6 +28,7 @@ function getStockpileData()
melt=make_stat('melt', stockpile_number, stats, configs), melt=make_stat('melt', stockpile_number, stats, configs),
trade=make_stat('trade', stockpile_number, stats, configs), trade=make_stat('trade', stockpile_number, stats, configs),
dump=make_stat('dump', stockpile_number, stats, configs), dump=make_stat('dump', stockpile_number, stats, configs),
train=make_stat('train', stockpile_number, stats, configs),
}) })
end end
table.sort(data, function(a, b) return a.sort_key < b.sort_key end) table.sort(data, function(a, b) return a.sort_key < b.sort_key end)
@ -42,14 +43,14 @@ local function print_stockpile_data(data)
print('Designated/designatable items in stockpiles:') print('Designated/designatable items in stockpiles:')
print() print()
local fmt = '%6s %-' .. name_len .. 's %4s %10s %5s %11s %4s %10s'; local fmt = '%6s %-' .. name_len .. 's %4s %10s %5s %11s %4s %10s %5s %11s';
print(fmt:format('number', 'name', 'melt', 'melt items', 'trade', 'trade items', 'dump', 'dump items')) print(fmt:format('number', 'name', 'melt', 'melt items', 'trade', 'trade items', 'dump', 'dump items', 'train', 'train items'))
local function uline(len) return ('-'):rep(len) end local function uline(len) return ('-'):rep(len) end
print(fmt:format(uline(6), uline(name_len), uline(4), uline(10), uline(5), uline(11), uline(4), uline(10))) print(fmt:format(uline(6), uline(name_len), uline(4), uline(10), uline(5), uline(11), uline(4), uline(10)))
local function get_enab(stats) return ('[%s]'):format(stats.enabled and 'x' or ' ') end local function get_enab(stats) return ('[%s]'):format(stats.enabled and 'x' or ' ') end
local function get_dstat(stats) return ('%d/%d'):format(stats.designated, stats.designated + stats.can_designate) end local function get_dstat(stats) return ('%d/%d'):format(stats.designated, stats.designated + stats.can_designate) end
for _,sp in ipairs(data) do for _,sp in ipairs(data) do
print(fmt:format(sp.stockpile_number, sp.name, get_enab(sp.melt), get_dstat(sp.melt), get_enab(sp.trade), get_dstat(sp.trade), get_enab(sp.dump), get_dstat(sp.dump))) print(fmt:format(sp.stockpile_number, sp.name, get_enab(sp.melt), get_dstat(sp.melt), get_enab(sp.trade), get_dstat(sp.trade), get_enab(sp.dump), get_dstat(sp.dump), get_enab(sp.train), get_dstat(sp.train)))
end end
end end
@ -74,6 +75,7 @@ local function print_status()
print(('Total items marked for melting: %5d'):format(global_stats.total_melt)) print(('Total items marked for melting: %5d'):format(global_stats.total_melt))
print(('Total items marked for trading: %5d'):format(global_stats.total_trade)) print(('Total items marked for trading: %5d'):format(global_stats.total_trade))
print(('Total items marked for dumping: %5d'):format(global_stats.total_dump)) print(('Total items marked for dumping: %5d'):format(global_stats.total_dump))
print(('Total animals marked for training: %5d'):format(global_stats.total_train))
end end
local function for_stockpiles(opts, fn) local function for_stockpiles(opts, fn)
@ -98,7 +100,8 @@ local function do_add_stockpile_config(features, opts)
logistics_setStockpileConfig(config.stockpile_number, logistics_setStockpileConfig(config.stockpile_number,
features.melt or config.melt == 1, features.melt or config.melt == 1,
features.trade or config.trade == 1, features.trade or config.trade == 1,
features.dump or config.dump == 1) features.dump or config.dump == 1,
features.train or config.train == 1)
end end
end end
end) end)

@ -209,11 +209,11 @@ end
function get_stockpile_features(stockpile_number) function get_stockpile_features(stockpile_number)
local config = logistics.logistics_getStockpileConfigs(stockpile_number)[1] local config = logistics.logistics_getStockpileConfigs(stockpile_number)[1]
return config.melt, config.trade, config.dump return config.melt, config.trade, config.dump, config.train
end end
function set_stockpile_features(stockpile_number, melt, trade, dump) function set_stockpile_features(stockpile_number, melt, trade, dump, train)
logistics.logistics_setStockpileConfig(stockpile_number, melt, trade, dump) logistics.logistics_setStockpileConfig(stockpile_number, melt, trade, dump, train)
end end
-------------------- --------------------
@ -429,6 +429,15 @@ function StockpilesOverlay:init()
options={{label='Dumping', value=true, pen=COLOR_LIGHTMAGENTA}, options={{label='Dumping', value=true, pen=COLOR_LIGHTMAGENTA},
{label='Dumping', value=false}}, {label='Dumping', value=false}},
on_change=self:callback('toggleLogisticsFeature', 'dump'), on_change=self:callback('toggleLogisticsFeature', 'dump'),
}, widgets.ToggleHotkeyLabel{
view_id='train',
frame={t=1, l=48},
auto_width=true,
key='CUSTOM_CTRL_A',
option_gap=-1,
options={{label='Training', value=true, pen=COLOR_LIGHTBLUE},
{label='Training', value=false}},
on_change=self:callback('toggleLogisticsFeature', 'train'),
}, },
}, },
}, },
@ -458,6 +467,7 @@ function StockpilesOverlay:onRenderFrame()
self.subviews.melt:setOption(config.melt == 1) self.subviews.melt:setOption(config.melt == 1)
self.subviews.trade:setOption(config.trade == 1) self.subviews.trade:setOption(config.trade == 1)
self.subviews.dump:setOption(config.dump == 1) self.subviews.dump:setOption(config.dump == 1)
self.subviews.train:setOption(config.train == 1)
self.cur_stockpile = sp self.cur_stockpile = sp
end end
end end
@ -470,7 +480,7 @@ function StockpilesOverlay:toggleLogisticsFeature(feature)
-- logical xor -- logical xor
logistics.logistics_setStockpileConfig(config.stockpile_number, logistics.logistics_setStockpileConfig(config.stockpile_number,
(feature == 'melt') ~= (config.melt == 1), (feature == 'trade') ~= (config.trade == 1), (feature == 'melt') ~= (config.melt == 1), (feature == 'trade') ~= (config.trade == 1),
(feature == 'dump') ~= (config.dump == 1)) (feature == 'dump') ~= (config.dump == 1), (feature == 'train') ~= (config.train == 1))
end end
function StockpilesOverlay:toggleMinimized() function StockpilesOverlay:toggleMinimized()