Add a few more unit orderings, and a way to reverse direction.

develop
Alexander Gavrilov 2012-04-21 16:53:17 +04:00
parent 3282ac3db2
commit 4af051bab3
5 changed files with 104 additions and 7 deletions

@ -38,6 +38,10 @@ end
* compare = function(a,b)
Comparison function. Defaults to compare above.
Called on non-nil keys; nil sorts last.
* nil_first
If true, nil keys are sorted first instead of last.
* reverse
If true, sort non-nil keys in descending order.
Returns a table of integer indices into data.
--]]
@ -84,12 +88,15 @@ function make_sort_order(data,ordering)
-- Sort nil keys to the end
if ka == nil then
if kb ~= nil then
return false
return ordering[i].nil_first
end
elseif kb == nil then
return true
return not ordering[i].nil_first
else
local cmpv = cmps[i](ka,kb)
if ordering[i].reverse then
cmpv = -cmpv
end
if cmpv < 0 then
return true
elseif cmpv > 0 then

@ -121,10 +121,26 @@ command_result lua_run_file (color_ostream &out, std::vector <std::string> &para
}
command_result lua_run (color_ostream &out, std::vector <std::string> &parameters)
{
if (!parameters.empty() && parameters[0] == "--core-context")
if (!parameters.empty())
{
Lua::InterpreterLoop(out, Lua::Core::State, "core lua");
return CR_OK;
if (parameters[0] == "--core-context")
{
Lua::InterpreterLoop(out, Lua::Core::State, "core lua");
return CR_OK;
}
else if (parameters[0] == "--core-reload")
{
CoreSuspender suspend;
for (size_t i = 1; i < parameters.size(); i++)
{
lua_getglobal(Lua::Core::State, "reload");
lua_pushstring(Lua::Core::State, parameters[i].c_str());
Lua::SafeCall(out, Lua::Core::State, 1, 0);
}
return CR_OK;
}
}
mymutex->lock();

@ -15,12 +15,33 @@ function parse_ordering_spec(type,...)
local specs = table.pack(...)
local rv = { }
for _,spec in ipairs(specs) do
local nil_first = false
if string.sub(spec,1,1) == '<' then
nil_first = true
spec = string.sub(spec,2)
end
local reverse = false
if string.sub(spec,1,1) == '>' then
reverse = true
spec = string.sub(spec,2)
end
local cm = group[spec]
if cm == nil then
dfhack.printerr('Unknown order for '..type..': '..tostring(spec))
return nil
end
if nil_first or reverse then
cm = copyall(cm)
cm.nil_first = nil_first
cm.reverse = reverse
end
rv[#rv+1] = cm
end

@ -6,7 +6,9 @@ orders = orders or {}
orders.name = {
key = function(unit)
return dfhack.TranslateName(unit.name)
if unit.name.has_name then
return dfhack.TranslateName(unit.name)
end
end,
compare = utils.compare_name
}
@ -17,4 +19,50 @@ orders.age = {
end
}
-- This assumes that units are added to active in arrival order
orders.arrival = {
key_table = function(units)
local tmp={}
for i,v in ipairs(units) do
tmp[v.id] = i
end
local idx={}
for i,v in ipairs(df.global.world.units.active) do
local ix = tmp[v.id]
if ix ~= nil then
idx[ix] = i
end
end
return idx
end
}
orders.profession = {
key = function(unit)
local cp = unit.custom_profession
if cp == '' then
cp = df.profession.attrs[unit.profession].caption
end
return cp
end
}
orders.squad = {
key = function(unit)
local sidx = unit.military.squad_index
if sidx >= 0 then
return sidx
end
end
}
orders.squad_position = {
key = function(unit)
local sidx = unit.military.squad_index
if sidx >= 0 then
return sidx * 1000 + unit.military.squad_position
end
end
}
return _ENV

@ -39,8 +39,13 @@ DFhackCExport command_result plugin_init (color_ostream &out, std::vector <Plugi
{
commands.push_back(PluginCommand(
"sort-units", "Sort the visible unit list.", sort_units, unit_list_hotkey,
" sort-units filter...\n"
" sort-units order [order...]\n"
" Sort the unit list using the given sequence of comparisons.\n"
" The '<' prefix for an order makes undefined values sort first.\n"
" The '>' prefix reverses the sort order for defined values.\n"
" Unit order examples:\n"
" name, age, arrival, squad, squad_position, profession\n"
"The orderings are defined in hack/lua/plugins/sort/*.lua\n"
));
return CR_OK;
}