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) * compare = function(a,b)
Comparison function. Defaults to compare above. Comparison function. Defaults to compare above.
Called on non-nil keys; nil sorts last. 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. Returns a table of integer indices into data.
--]] --]]
@ -84,12 +88,15 @@ function make_sort_order(data,ordering)
-- Sort nil keys to the end -- Sort nil keys to the end
if ka == nil then if ka == nil then
if kb ~= nil then if kb ~= nil then
return false return ordering[i].nil_first
end end
elseif kb == nil then elseif kb == nil then
return true return not ordering[i].nil_first
else else
local cmpv = cmps[i](ka,kb) local cmpv = cmps[i](ka,kb)
if ordering[i].reverse then
cmpv = -cmpv
end
if cmpv < 0 then if cmpv < 0 then
return true return true
elseif cmpv > 0 then elseif cmpv > 0 then

@ -121,11 +121,27 @@ 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) command_result lua_run (color_ostream &out, std::vector <std::string> &parameters)
{ {
if (!parameters.empty() && parameters[0] == "--core-context") if (!parameters.empty())
{
if (parameters[0] == "--core-context")
{ {
Lua::InterpreterLoop(out, Lua::Core::State, "core lua"); Lua::InterpreterLoop(out, Lua::Core::State, "core lua");
return CR_OK; 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(); mymutex->lock();
lua::state s=lua::glua::Get(); lua::state s=lua::glua::Get();

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

@ -6,7 +6,9 @@ orders = orders or {}
orders.name = { orders.name = {
key = function(unit) key = function(unit)
if unit.name.has_name then
return dfhack.TranslateName(unit.name) return dfhack.TranslateName(unit.name)
end
end, end,
compare = utils.compare_name compare = utils.compare_name
} }
@ -17,4 +19,50 @@ orders.age = {
end 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 return _ENV

@ -39,8 +39,13 @@ DFhackCExport command_result plugin_init (color_ostream &out, std::vector <Plugi
{ {
commands.push_back(PluginCommand( commands.push_back(PluginCommand(
"sort-units", "Sort the visible unit list.", sort_units, unit_list_hotkey, "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" " 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; return CR_OK;
} }