Skip to content

Commit e473424

Browse files
committed
add lua nestboxes to autobutcher plugin
1 parent 97846f5 commit e473424

File tree

5 files changed

+631
-0
lines changed

5 files changed

+631
-0
lines changed

plugins/autobutcher.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,36 @@ static int autobutcher_getWatchList(lua_State *L) {
971971
return 1;
972972
}
973973

974+
static int getMax(int first, int second) {
975+
return first > second ? first : second;
976+
}
977+
// push info used by nestboxes
978+
static int autobutcher_getInfoForNestboxes(lua_State* L) {
979+
PersistentDataItem rconfig;
980+
color_ostream* out = Lua::GetOutput(L);
981+
982+
if (lua_isnumber(L, 1)) {
983+
int raceId = lua_tointeger(L, 1);
984+
lua_newtable(L);
985+
int ctable = lua_gettop(L);
986+
Lua::SetField(L, config.get_bool(CONFIG_IS_ENABLED), ctable, "autobutcher_enabled");
987+
988+
if (!out)
989+
out = &Core::getInstance().getConsole();
990+
991+
WatchedRace* w;
992+
if (watched_races.count(raceId)) {
993+
w = watched_races[raceId];
994+
WatchedRace* tally = checkRaceStocksTotal(*out, raceId);
995+
Lua::SetField(L, w->isWatched, ctable, "watched");
996+
int mac = getMax(w->fk - tally->fk_units.size(), 0) + getMax(w->mk - tally->mk_units.size(), 0) + getMax(w->fa - tally->fa_units.size(), 0) + getMax(w->ma - tally->ma_units.size(), 0);
997+
Lua::SetField(L, mac, ctable, "mac"); // missing animals count for race, diff between target for child/adult female/male and current amounts, ignore amounts over target
998+
delete tally;
999+
}
1000+
}
1001+
return 1;
1002+
}
1003+
9741004
DFHACK_PLUGIN_LUA_FUNCTIONS {
9751005
DFHACK_LUA_FUNCTION(autowatch_isEnabled),
9761006
DFHACK_LUA_FUNCTION(autowatch_setEnabled),
@@ -986,5 +1016,6 @@ DFHACK_PLUGIN_LUA_FUNCTIONS {
9861016
DFHACK_PLUGIN_LUA_COMMANDS {
9871017
DFHACK_LUA_COMMAND(autobutcher_getSettings),
9881018
DFHACK_LUA_COMMAND(autobutcher_getWatchList),
1019+
DFHACK_LUA_COMMAND(autobutcher_getInfoForNestboxes),
9891020
DFHACK_LUA_END
9901021
};

plugins/lua/autobutcher.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local _ENV = mkmodule('plugins.autobutcher')
22

33
local argparse = require('argparse')
4+
local nestboxes = require('plugins.autobutcher.nestboxes')
45

56
local function is_int(val)
67
return val and val == math.floor(val)
@@ -45,6 +46,13 @@ local function process_races(opts, races, start_idx)
4546
end
4647
end
4748

49+
local function reload_modules()
50+
reload('plugins.autobutcher.common')
51+
reload('plugins.autobutcher.nestboxesEvent')
52+
reload('plugins.autobutcher.nestboxes')
53+
reload('plugins.autobutcher')
54+
end
55+
4856
function parse_commandline(opts, args)
4957
local positionals = process_args(opts, args)
5058

@@ -65,6 +73,10 @@ function parse_commandline(opts, args)
6573
opts.fa = check_nonnegative_int(positionals[4])
6674
opts.ma = check_nonnegative_int(positionals[5])
6775
process_races(opts, positionals, 6)
76+
elseif command == 'reload' then
77+
reload_modules()
78+
elseif string.upper(command) == 'NESTBOXES' or string.upper(command) =='NB' then
79+
nestboxes.handleCommand(positionals, opts)
6880
else
6981
qerror(('unrecognized command: "%s"'):format(command))
7082
end

plugins/lua/autobutcher/common.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
local _ENV = mkmodule('plugins.autobutcher.common')
2+
3+
verbose = verbose or nil
4+
prefix = prefix or ''
5+
6+
function printLocal(text)
7+
print(prefix .. ': ' .. text)
8+
end
9+
10+
function handleError(text)
11+
qerror(prefix .. ': ' .. text)
12+
end
13+
14+
function printDetails(text)
15+
if verbose then
16+
printLocal(text)
17+
end
18+
end
19+
20+
function dumpToString(o)
21+
if type(o) == 'table' then
22+
local s = '{ '
23+
for k, v in pairs(o) do
24+
if type(k) ~= 'number' then
25+
k = '"' .. k .. '"'
26+
end
27+
s = s .. '[' .. k .. '] = ' .. dumpToString(v) .. ','
28+
end
29+
return s .. '} '
30+
else
31+
return tostring(o)
32+
end
33+
end
34+
return _ENV

0 commit comments

Comments
 (0)