User:Fleeting Frames/sort-units

From Dwarf Fortress Wiki
Jump to navigation Jump to search

added sort-units deathdate to 4303 hack/lua/plugins/sort/units.lua

local _ENV = mkmodule('plugins.sort.units')

local utils = require('utils')

orders = orders or {}

-- Relies on NULL being auto-translated to NULL, and then sorted
orders.exists = {
    key = function(unit)
        return 1
    end
}

orders.name = {
    key = function(unit)
        local name = dfhack.units.getVisibleName(unit)
        if name and name.has_name then
            return dfhack.TranslateName(name)
        end
    end,
    compare = utils.compare_name
}

orders.age = {
    key = function(unit)
        return dfhack.units.getAge(unit)
    end
}

orders.deathdate = {
	key = function(unit)
		return tonumber(dfhack.run_command_silent('deathcause ' .. unit.id):sub(4,16):gsub("-",""):match('%d+'))
	end
}

-- This assumes that units are added to active in arrival order
orders.arrival = {
    key_table = function(units)
        local size = units.n or #units
        local lookup={}
        for i=1,size do
            if units[i] then
                lookup[units[i].id] = i
            end
        end
        local idx={}
        for i,v in ipairs(df.global.world.units.active) do
            if lookup[v.id] then
                idx[lookup[v.id]] = i
            end
        end
        return idx
    end
}

local function findRaceCaste(unit)
    local rraw = df.creature_raw.find(unit.race)
    return rraw, safe_index(rraw, 'caste', unit.caste)
end

orders.noble = {
    key = function(unit)
        local info = dfhack.units.getNoblePositions(unit)
        if info then
            return info[1].position.precedence
        end
    end
}

orders.profession = {
    key = function(unit)
        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]
        end
    end
}

orders.squad = {
    key = function(unit)
        local sidx = unit.military.squad_id
        if sidx >= 0 then
            return sidx
        end
    end
}

orders.squad_position = {
    key = function(unit)
        local sidx = unit.military.squad_id
        if sidx >= 0 then
            return sidx * 1000 + unit.military.squad_position
        end
    end
}

orders.happiness = {
    key = function(unit)
        return unit.status.happiness
    end
}

return _ENV