From 4af051bab3e455a9115a44e43b8c3da0cd189f43 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Sat, 21 Apr 2012 16:53:17 +0400 Subject: [PATCH] Add a few more unit orderings, and a way to reverse direction. --- library/lua/utils.lua | 11 ++++++-- plugins/Dfusion/dfusion.cpp | 22 +++++++++++++--- plugins/lua/sort.lua | 21 ++++++++++++++++ plugins/lua/sort/units.lua | 50 ++++++++++++++++++++++++++++++++++++- plugins/sort.cpp | 7 +++++- 5 files changed, 104 insertions(+), 7 deletions(-) diff --git a/library/lua/utils.lua b/library/lua/utils.lua index d884f2f62..3becdbb6d 100644 --- a/library/lua/utils.lua +++ b/library/lua/utils.lua @@ -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 diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index d8710c2db..2b36a9747 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -121,10 +121,26 @@ command_result lua_run_file (color_ostream &out, std::vector ¶ } command_result lua_run (color_ostream &out, std::vector ¶meters) { - 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(); diff --git a/plugins/lua/sort.lua b/plugins/lua/sort.lua index 2318d7e07..f042e85cd 100644 --- a/plugins/lua/sort.lua +++ b/plugins/lua/sort.lua @@ -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 diff --git a/plugins/lua/sort/units.lua b/plugins/lua/sort/units.lua index 872e49429..b92fc6c82 100644 --- a/plugins/lua/sort/units.lua +++ b/plugins/lua/sort/units.lua @@ -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 \ No newline at end of file diff --git a/plugins/sort.cpp b/plugins/sort.cpp index a373d54c3..48e4bcaca 100644 --- a/plugins/sort.cpp +++ b/plugins/sort.cpp @@ -39,8 +39,13 @@ DFhackCExport command_result plugin_init (color_ostream &out, std::vector ' 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; }