2012-04-21 05:43:52 -06:00
|
|
|
local _ENV = mkmodule('plugins.sort.units')
|
|
|
|
|
|
|
|
local utils = require('utils')
|
|
|
|
|
|
|
|
orders = orders or {}
|
|
|
|
|
2012-04-22 09:22:00 -06:00
|
|
|
-- Relies on NULL being auto-translated to NULL, and then sorted
|
|
|
|
orders.exists = {
|
|
|
|
key = function(unit)
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2012-04-21 05:43:52 -06:00
|
|
|
orders.name = {
|
|
|
|
key = function(unit)
|
2012-04-23 11:30:53 -06:00
|
|
|
local name = dfhack.units.getVisibleName(unit)
|
|
|
|
if name and name.has_name then
|
|
|
|
return dfhack.TranslateName(name)
|
2012-04-21 06:53:17 -06:00
|
|
|
end
|
2012-04-21 05:43:52 -06:00
|
|
|
end,
|
|
|
|
compare = utils.compare_name
|
|
|
|
}
|
|
|
|
|
|
|
|
orders.age = {
|
|
|
|
key = function(unit)
|
|
|
|
return dfhack.units.getAge(unit)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2012-04-21 06:53:17 -06:00
|
|
|
-- This assumes that units are added to active in arrival order
|
|
|
|
orders.arrival = {
|
|
|
|
key_table = function(units)
|
2012-04-23 11:30:53 -06:00
|
|
|
local size = units.n or #units
|
|
|
|
local lookup={}
|
|
|
|
for i=1,size do
|
|
|
|
if units[i] then
|
|
|
|
lookup[units[i].id] = i
|
|
|
|
end
|
2012-04-21 06:53:17 -06:00
|
|
|
end
|
|
|
|
local idx={}
|
|
|
|
for i,v in ipairs(df.global.world.units.active) do
|
2012-04-23 11:30:53 -06:00
|
|
|
if lookup[v.id] then
|
|
|
|
idx[lookup[v.id]] = i
|
2012-04-21 06:53:17 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return idx
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2012-04-23 11:30:53 -06:00
|
|
|
local function findRaceCaste(unit)
|
|
|
|
local rraw = df.creature_raw.find(unit.race)
|
|
|
|
return rraw, safe_index(rraw, 'caste', unit.caste)
|
|
|
|
end
|
|
|
|
|
2012-04-21 06:53:17 -06:00
|
|
|
orders.profession = {
|
|
|
|
key = function(unit)
|
2012-04-23 11:30:53 -06:00
|
|
|
local cp = dfhack.units.getProfessionName(unit)
|
|
|
|
if cp and cp ~= '' then
|
|
|
|
return string.lower(cp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
orders.profession_class = {
|
|
|
|
key = function(unit)
|
|
|
|
local pid = unit.profession
|
|
|
|
local parent = df.profession.attrs[pid].parent
|
|
|
|
if parent >= 0 then
|
|
|
|
return parent
|
|
|
|
else
|
|
|
|
return pid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
orders.race = {
|
|
|
|
key = function(unit)
|
|
|
|
local rraw = findRaceCaste(unit)
|
|
|
|
if rraw then
|
|
|
|
return rraw.name[0]
|
2012-04-21 06:53:17 -06:00
|
|
|
end
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2012-04-21 05:43:52 -06:00
|
|
|
return _ENV
|