Merge pull request #4127 from myk002/myk_autobutcher

[autobutcher] small edits
develop
Myk 2024-01-02 07:27:52 -08:00 committed by GitHub
commit 04d81a7cc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 43 deletions

@ -6,12 +6,10 @@ autobutcher
:tags: fort auto fps animals
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
watch list. Units will be ignored if they are:
excess livestock for slaughter. Units will be ignored if they are:
* Untamed
* Nicknamed (for custom protection; you can use the `rename` ``unit`` tool
individually, or `zone` ``nick`` for groups)
* Nicknamed
* Caged, if and only if the cage is in a zone (to protect zoos)
* 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
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
reliable breeding population due to asexuality etc. See `fix-ster` if this is a
problem.
reliable breeding population due to asexuality etc.
Usage
-----
@ -31,6 +28,9 @@ Usage
no races are watched by default. You have to add the ones you want to
monitor with ``autobutcher watch``, ``autobutcher target`` or
``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``
Automatically add all new races (animals you buy from merchants, tame
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,
all space separated races listed will be modified (or added to the watchlist
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> ...]``
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
@ -64,9 +61,10 @@ Usage
``autobutcher forget all|<race> [<race> ...]``
Unwatch the specified race(s) (or all races on your watchlist if ``all`` is
given) and forget target settings for it/them.
``autobutcher [list]``
Print status and current settings, including the watchlist. This is the
default command if autobutcher is run without parameters.
``autobutcher now``
Process all livestock according to the current watchlist configuration,
even if the plugin is not currently enabled, and thus not doing automatic
periodic scans.
``autobutcher list_export``
Print commands required to set the current settings in another fort.
@ -104,7 +102,7 @@ fortress::
enable autobutcher
autobutcher target 2 2 2 2 DOG
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 5 5 6 2 PIG
autobutcher target 0 0 0 0 new

@ -50,7 +50,7 @@ static PersistentDataItem config;
enum ConfigValues {
CONFIG_IS_ENABLED = 0,
CONFIG_CYCLE_TICKS = 1,
// CONFIG_CYCLE_TICKS = 1, deprecated; no longer configurable
CONFIG_AUTOWATCH = 2,
CONFIG_DEFAULT_FK = 3,
CONFIG_DEFAULT_MK = 4,
@ -79,6 +79,8 @@ struct WatchedRace;
// to ignore them for a while but still keep the target count settings
static unordered_map<int, WatchedRace*> watched_races;
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 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");
config = World::AddPersistentData(CONFIG_KEY);
set_config_bool(CONFIG_IS_ENABLED, is_enabled);
set_config_val(CONFIG_CYCLE_TICKS, 6000);
set_config_bool(CONFIG_AUTOWATCH, true);
set_config_val(CONFIG_DEFAULT_FK, 4);
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) {
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);
return CR_OK;
}
@ -501,10 +502,6 @@ static command_result df_autobutcher(color_ostream &out, vector<string> &paramet
opts.command == "forget") {
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 {
autobutcher_status(out);
}
@ -532,7 +529,6 @@ static vector<WatchedRace *> getSortedWatchList() {
static void autobutcher_export(color_ostream &out) {
out << "enable autobutcher" << endl;
out << "autobutcher ticks " << get_config_val(CONFIG_CYCLE_TICKS) << endl;
out << "autobutcher " << (get_config_bool(CONFIG_AUTOWATCH) ? "" : "no")
<< "autowatch" << endl;
out << "autobutcher target"
@ -558,8 +554,6 @@ static void autobutcher_export(color_ostream &out) {
static void autobutcher_status(color_ostream &out) {
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 << "\ndefault setting for new races:"
@ -756,6 +750,8 @@ static bool isProtectedUnit(df::unit *unit) {
|| !unit->name.nickname.empty();
}
static void autobutcher_cycle(color_ostream &out) {
// mark that we have recently run
cycle_timestamp = world->frame_counter;
@ -919,18 +915,11 @@ static bool autowatch_isEnabled() {
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) {
DEBUG(status,out).print("auto-adding to watchlist %s\n", enable ? "started" : "stopped");
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
@ -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_FA), ctable, "fa");
Lua::SetField(L, get_config_val(CONFIG_DEFAULT_MA), ctable, "ma");
Lua::SetField(L, get_config_val(CONFIG_CYCLE_TICKS), ctable, "sleep");
return 1;
}
@ -1099,8 +1087,6 @@ static int autobutcher_getWatchList(lua_State *L) {
DFHACK_PLUGIN_LUA_FUNCTIONS {
DFHACK_LUA_FUNCTION(autowatch_isEnabled),
DFHACK_LUA_FUNCTION(autowatch_setEnabled),
DFHACK_LUA_FUNCTION(autobutcher_getSleep),
DFHACK_LUA_FUNCTION(autobutcher_setSleep),
DFHACK_LUA_FUNCTION(autobutcher_setWatchListRace),
DFHACK_LUA_FUNCTION(autobutcher_setDefaultTargetNew),
DFHACK_LUA_FUNCTION(autobutcher_setDefaultTargetAll),

@ -65,13 +65,6 @@ function parse_commandline(opts, ...)
opts.fa = check_nonnegative_int(positionals[4])
opts.ma = check_nonnegative_int(positionals[5])
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
qerror(('unrecognized command: "%s"'):format(command))
end

@ -1 +1 @@
Subproject commit b41f1ec482255428166722bec6904a779a0ad02f
Subproject commit d116a915e4fdcaad8a6fcb81d511735ff8f34e69