remove ticks config, document "now" command, run cycle on autowatch enable

develop
Myk Taylor 2024-01-02 01:31:08 -08:00
parent 112f3c1a8b
commit dbe2de6607
No known key found for this signature in database
3 changed files with 19 additions and 42 deletions

@ -6,12 +6,10 @@ autobutcher
:tags: fort auto fps animals :tags: fort auto fps animals
This plugin monitors how many pets you have of each gender and age and assigns This plugin monitors how many pets you have of each gender and age and assigns
excess livestock for slaughter. It requires that you add the target race(s) to a excess livestock for slaughter. Units will be ignored if they are:
watch list. Units will be ignored if they are:
* Untamed * Untamed
* Nicknamed (for custom protection; you can use the `rename` ``unit`` tool * Nicknamed
individually, or `zone` ``nick`` for groups)
* Caged, if and only if the cage is in a zone (to protect zoos) * Caged, if and only if the cage is in a zone (to protect zoos)
* Trained for war or hunting * Trained for war or hunting
@ -20,8 +18,7 @@ opposite sex or have been gelded) will be butchered before those who will.
Older adults and younger children will be butchered first if the population Older adults and younger children will be butchered first if the population
is above the target (defaults are: 2 male kids, 4 female kids, 2 male adults, is above the target (defaults are: 2 male kids, 4 female kids, 2 male adults,
4 female adults). Note that you may need to set a target above 1 to have a 4 female adults). Note that you may need to set a target above 1 to have a
reliable breeding population due to asexuality etc. See `fix-ster` if this is a reliable breeding population due to asexuality etc.
problem.
Usage Usage
----- -----
@ -31,6 +28,9 @@ Usage
no races are watched by default. You have to add the ones you want to no races are watched by default. You have to add the ones you want to
monitor with ``autobutcher watch``, ``autobutcher target`` or monitor with ``autobutcher watch``, ``autobutcher target`` or
``autobutcher autowatch``. ``autobutcher autowatch``.
``autobutcher [list]``
Print status and current settings, including the watchlist. This is the
default command if autobutcher is run without parameters.
``autobutcher autowatch`` ``autobutcher autowatch``
Automatically add all new races (animals you buy from merchants, tame Automatically add all new races (animals you buy from merchants, tame
yourself, or get from migrants) to the watch list using the default target yourself, or get from migrants) to the watch list using the default target
@ -50,9 +50,6 @@ Usage
future watch commands without changing your current watchlist. Otherwise, future watch commands without changing your current watchlist. Otherwise,
all space separated races listed will be modified (or added to the watchlist all space separated races listed will be modified (or added to the watchlist
if they aren't there already). if they aren't there already).
``autobutcher ticks <ticks>``
Change the number of ticks between scanning cycles when the plugin is
enabled. By default, a cycle happens every 6000 ticks (about 8 game days).
``autobutcher watch all|<race> [<race> ...]`` ``autobutcher watch all|<race> [<race> ...]``
Start watching the listed races. If they aren't already in your watchlist, Start watching the listed races. If they aren't already in your watchlist,
then they will be added with the default target counts. If you specify the then they will be added with the default target counts. If you specify the
@ -64,9 +61,10 @@ Usage
``autobutcher forget all|<race> [<race> ...]`` ``autobutcher forget all|<race> [<race> ...]``
Unwatch the specified race(s) (or all races on your watchlist if ``all`` is Unwatch the specified race(s) (or all races on your watchlist if ``all`` is
given) and forget target settings for it/them. given) and forget target settings for it/them.
``autobutcher [list]`` ``autobutcher now``
Print status and current settings, including the watchlist. This is the Process all livestock according to the current watchlist configuration,
default command if autobutcher is run without parameters. even if the plugin is not currently enabled, and thus not doing automatic
periodic scans.
``autobutcher list_export`` ``autobutcher list_export``
Print commands required to set the current settings in another fort. Print commands required to set the current settings in another fort.
@ -104,7 +102,7 @@ fortress::
enable autobutcher enable autobutcher
autobutcher target 2 2 2 2 DOG autobutcher target 2 2 2 2 DOG
autobutcher target 1 1 2 2 CAT autobutcher target 1 1 2 2 CAT
autobutcher target 50 50 14 2 BIRD_GOOSE autobutcher target 10 10 14 2 BIRD_GOOSE
autobutcher target 2 2 4 2 ALPACA SHEEP LLAMA autobutcher target 2 2 4 2 ALPACA SHEEP LLAMA
autobutcher target 5 5 6 2 PIG autobutcher target 5 5 6 2 PIG
autobutcher target 0 0 0 0 new autobutcher target 0 0 0 0 new

@ -50,7 +50,7 @@ static PersistentDataItem config;
enum ConfigValues { enum ConfigValues {
CONFIG_IS_ENABLED = 0, CONFIG_IS_ENABLED = 0,
CONFIG_CYCLE_TICKS = 1, // CONFIG_CYCLE_TICKS = 1, deprecated; no longer configurable
CONFIG_AUTOWATCH = 2, CONFIG_AUTOWATCH = 2,
CONFIG_DEFAULT_FK = 3, CONFIG_DEFAULT_FK = 3,
CONFIG_DEFAULT_MK = 4, CONFIG_DEFAULT_MK = 4,
@ -79,6 +79,8 @@ struct WatchedRace;
// to ignore them for a while but still keep the target count settings // to ignore them for a while but still keep the target count settings
static unordered_map<int, WatchedRace*> watched_races; static unordered_map<int, WatchedRace*> watched_races;
static unordered_map<string, int> race_to_id; static unordered_map<string, int> race_to_id;
static const int32_t CYCLE_TICKS = 6000;
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 void init_autobutcher(color_ostream &out); static void init_autobutcher(color_ostream &out);
@ -129,7 +131,6 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) {
DEBUG(status,out).print("no config found in this save; initializing\n"); DEBUG(status,out).print("no config found in this save; initializing\n");
config = World::AddPersistentData(CONFIG_KEY); config = World::AddPersistentData(CONFIG_KEY);
set_config_bool(CONFIG_IS_ENABLED, is_enabled); set_config_bool(CONFIG_IS_ENABLED, is_enabled);
set_config_val(CONFIG_CYCLE_TICKS, 6000);
set_config_bool(CONFIG_AUTOWATCH, true); set_config_bool(CONFIG_AUTOWATCH, true);
set_config_val(CONFIG_DEFAULT_FK, 4); set_config_val(CONFIG_DEFAULT_FK, 4);
set_config_val(CONFIG_DEFAULT_MK, 2); set_config_val(CONFIG_DEFAULT_MK, 2);
@ -163,7 +164,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
} }
DFhackCExport command_result plugin_onupdate(color_ostream &out) { DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (is_enabled && world->frame_counter - cycle_timestamp >= get_config_val(CONFIG_CYCLE_TICKS)) if (is_enabled && world->frame_counter - cycle_timestamp >= CYCLE_TICKS)
autobutcher_cycle(out); autobutcher_cycle(out);
return CR_OK; return CR_OK;
} }
@ -501,10 +502,6 @@ static command_result df_autobutcher(color_ostream &out, vector<string> &paramet
opts.command == "forget") { opts.command == "forget") {
autobutcher_modify_watchlist(out, opts); autobutcher_modify_watchlist(out, opts);
} }
else if (opts.command == "ticks") {
set_config_val(CONFIG_CYCLE_TICKS, opts.ticks);
INFO(status,out).print("New cycle timer: %d ticks.\n", opts.ticks);
}
else { else {
autobutcher_status(out); autobutcher_status(out);
} }
@ -532,7 +529,6 @@ static vector<WatchedRace *> getSortedWatchList() {
static void autobutcher_export(color_ostream &out) { static void autobutcher_export(color_ostream &out) {
out << "enable autobutcher" << endl; out << "enable autobutcher" << endl;
out << "autobutcher ticks " << get_config_val(CONFIG_CYCLE_TICKS) << endl;
out << "autobutcher " << (get_config_bool(CONFIG_AUTOWATCH) ? "" : "no") out << "autobutcher " << (get_config_bool(CONFIG_AUTOWATCH) ? "" : "no")
<< "autowatch" << endl; << "autowatch" << endl;
out << "autobutcher target" out << "autobutcher target"
@ -558,8 +554,6 @@ static void autobutcher_export(color_ostream &out) {
static void autobutcher_status(color_ostream &out) { static void autobutcher_status(color_ostream &out) {
out << "autobutcher is " << (is_enabled ? "" : "not ") << "enabled\n"; out << "autobutcher is " << (is_enabled ? "" : "not ") << "enabled\n";
if (is_enabled)
out << " running every " << get_config_val(CONFIG_CYCLE_TICKS) << " game ticks\n";
out << " " << (get_config_bool(CONFIG_AUTOWATCH) ? "" : "not ") << "autowatching for new races\n"; out << " " << (get_config_bool(CONFIG_AUTOWATCH) ? "" : "not ") << "autowatching for new races\n";
out << "\ndefault setting for new races:" out << "\ndefault setting for new races:"
@ -756,6 +750,8 @@ static bool isProtectedUnit(df::unit *unit) {
|| !unit->name.nickname.empty(); || !unit->name.nickname.empty();
} }
static void autobutcher_cycle(color_ostream &out) { static void autobutcher_cycle(color_ostream &out) {
// mark that we have recently run // mark that we have recently run
cycle_timestamp = world->frame_counter; cycle_timestamp = world->frame_counter;
@ -919,18 +915,11 @@ static bool autowatch_isEnabled() {
return get_config_bool(CONFIG_AUTOWATCH); return get_config_bool(CONFIG_AUTOWATCH);
} }
static unsigned autobutcher_getSleep(color_ostream &out) {
return get_config_val(CONFIG_CYCLE_TICKS);
}
static void autobutcher_setSleep(color_ostream &out, unsigned ticks) {
set_config_val(CONFIG_CYCLE_TICKS, ticks);
}
static void autowatch_setEnabled(color_ostream &out, bool enable) { static void autowatch_setEnabled(color_ostream &out, bool enable) {
DEBUG(status,out).print("auto-adding to watchlist %s\n", enable ? "started" : "stopped"); DEBUG(status,out).print("auto-adding to watchlist %s\n", enable ? "started" : "stopped");
set_config_bool(CONFIG_AUTOWATCH, enable); set_config_bool(CONFIG_AUTOWATCH, enable);
if (get_config_bool(CONFIG_IS_ENABLED))
autobutcher_cycle(out);
} }
// set all data for a watchlist race in one go // set all data for a watchlist race in one go
@ -1036,7 +1025,6 @@ static int autobutcher_getSettings(lua_State *L) {
Lua::SetField(L, get_config_val(CONFIG_DEFAULT_MK), ctable, "mk"); Lua::SetField(L, get_config_val(CONFIG_DEFAULT_MK), ctable, "mk");
Lua::SetField(L, get_config_val(CONFIG_DEFAULT_FA), ctable, "fa"); Lua::SetField(L, get_config_val(CONFIG_DEFAULT_FA), ctable, "fa");
Lua::SetField(L, get_config_val(CONFIG_DEFAULT_MA), ctable, "ma"); Lua::SetField(L, get_config_val(CONFIG_DEFAULT_MA), ctable, "ma");
Lua::SetField(L, get_config_val(CONFIG_CYCLE_TICKS), ctable, "sleep");
return 1; return 1;
} }
@ -1099,8 +1087,6 @@ static int autobutcher_getWatchList(lua_State *L) {
DFHACK_PLUGIN_LUA_FUNCTIONS { DFHACK_PLUGIN_LUA_FUNCTIONS {
DFHACK_LUA_FUNCTION(autowatch_isEnabled), DFHACK_LUA_FUNCTION(autowatch_isEnabled),
DFHACK_LUA_FUNCTION(autowatch_setEnabled), DFHACK_LUA_FUNCTION(autowatch_setEnabled),
DFHACK_LUA_FUNCTION(autobutcher_getSleep),
DFHACK_LUA_FUNCTION(autobutcher_setSleep),
DFHACK_LUA_FUNCTION(autobutcher_setWatchListRace), DFHACK_LUA_FUNCTION(autobutcher_setWatchListRace),
DFHACK_LUA_FUNCTION(autobutcher_setDefaultTargetNew), DFHACK_LUA_FUNCTION(autobutcher_setDefaultTargetNew),
DFHACK_LUA_FUNCTION(autobutcher_setDefaultTargetAll), DFHACK_LUA_FUNCTION(autobutcher_setDefaultTargetAll),

@ -65,13 +65,6 @@ function parse_commandline(opts, ...)
opts.fa = check_nonnegative_int(positionals[4]) opts.fa = check_nonnegative_int(positionals[4])
opts.ma = check_nonnegative_int(positionals[5]) opts.ma = check_nonnegative_int(positionals[5])
process_races(opts, positionals, 6) process_races(opts, positionals, 6)
elseif command == 'ticks' then
local ticks = tonumber(positionals[2])
if not is_positive_int(ticks) then
qerror('number of ticks must be a positive integer: ' .. ticks)
else
opts.ticks = ticks
end
else else
qerror(('unrecognized command: "%s"'):format(command)) qerror(('unrecognized command: "%s"'):format(command))
end end