Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sort] unify unit search key logic #3920

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Template for new versions:

## Misc Improvements
- `buildingplan`: display how many items are available on the planner panel
- `sort`: allow searching by profession on the squad assignment page

## Documentation

Expand Down
7 changes: 4 additions & 3 deletions plugins/lua/sort.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local _ENV = mkmodule('plugins.sort')
local gui = require('gui')
local overlay = require('plugins.overlay')
local setbelief = reqscript('modtools/set-belief')
local textures = require('gui.textures')
local sortoverlay = require('plugins.sort.sortoverlay')
local utils = require('utils')
local widgets = require('gui.widgets')

Expand Down Expand Up @@ -1031,7 +1031,7 @@ local function filter_matches(unit_id, filter)
if filter.maimed == 'only' and not is_maimed(unit) then return false end
if filter.maimed == 'exclude' and is_maimed(unit) then return false end
if #filter.search == 0 then return true end
local search_key = dfhack.TranslateName(dfhack.units.getVisibleName(unit))
local search_key = sortoverlay.get_unit_search_key(unit)
return normalize_search_key(search_key):find(dfhack.toSearchNormalized(filter.search))
end

Expand Down Expand Up @@ -1233,14 +1233,15 @@ local function init_face_tiles()
FACE_TILES[idx-1] = face
end
end
init_face_tiles()

function get_stress_face_tile(idx, x, y)
local elem = rating_annotations[idx]
if not elem or not elem.val or elem.val < 0 then
return x == 0 and y == 1 and DASH_PEN or gui.CLEAR_PEN
end
local val = math.min(6, elem.val)
return FACE_TILES[val][y][x]
return safe_index(FACE_TILES, val, y, x)
end

function SquadAnnotationOverlay:init()
Expand Down
17 changes: 5 additions & 12 deletions plugins/lua/sort/info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,9 @@ local function get_sort()
end
end

local function get_unit_search_key(unit)
return ('%s %s %s'):format(
dfhack.units.getReadableName(unit), -- last name is in english
dfhack.units.getProfessionName(unit),
dfhack.TranslateName(unit.name, false, true)) -- get untranslated last name
end

local function get_cri_unit_search_key(cri_unit)
return ('%s %s'):format(
cri_unit.un and get_unit_search_key(cri_unit.un) or '',
cri_unit.un and sortoverlay.get_unit_search_key(cri_unit.un) or '',
cri_unit.job_sort_name)
end

Expand All @@ -132,7 +125,7 @@ local function work_details_search(vec, data, text, incremental)
data.selected = work_details.selected_work_detail_index
end
sortoverlay.single_vector_search(
{get_search_key_fn=get_unit_search_key},
{get_search_key_fn=sortoverlay.get_unit_search_key},
vec, data, text, incremental)
end

Expand Down Expand Up @@ -161,7 +154,7 @@ end
local function get_candidate_search_key(cand)
if not cand.un then return end
return ('%s %s'):format(
get_unit_search_key(cand.un),
sortoverlay.get_unit_search_key(cand.un),
serialize_skills(cand.un))
end

Expand Down Expand Up @@ -420,15 +413,15 @@ function InterrogationOverlay:init()
self:register_handler('INTERROGATING', justice.interrogation_list,
curry(sortoverlay.flags_vector_search,
{
get_search_key_fn=get_unit_search_key,
get_search_key_fn=sortoverlay.get_unit_search_key,
get_elem_id_fn=function(unit) return unit.id end,
matches_filters_fn=self:callback('matches_filters'),
},
justice.interrogation_list_flag))
self:register_handler('CONVICTING', justice.conviction_list,
curry(sortoverlay.single_vector_search,
{
get_search_key_fn=get_unit_search_key,
get_search_key_fn=sortoverlay.get_unit_search_key,
matches_filters_fn=self:callback('matches_filters'),
}))
end
Expand Down
5 changes: 1 addition & 4 deletions plugins/lua/sort/slab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ local sortoverlay = require('plugins.sort.sortoverlay')
local widgets = require('gui.widgets')

local building = df.global.game.main_interface.building
local view_sheets = df.global.game.main_interface.view_sheets

-- ----------------------
-- SlabOverlay
Expand Down Expand Up @@ -95,9 +94,7 @@ end
function SlabOverlay:get_search_key(if_button)
local unit = get_unit(if_button)
if not unit then return if_button.filter_str end
return ('%s %s'):format(
dfhack.units.getReadableName(unit), -- last name is in english
dfhack.TranslateName(unit.name, false, true)) -- get untranslated last name
return sortoverlay.get_unit_search_key(unit)
end

local function needs_slab(if_button)
Expand Down
7 changes: 7 additions & 0 deletions plugins/lua/sort/sortoverlay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ local _ENV = mkmodule('plugins.sort.sortoverlay')
local overlay = require('plugins.overlay')
local utils = require('utils')

function get_unit_search_key(unit)
return ('%s %s %s'):format(
dfhack.units.getReadableName(unit), -- last name is in english
dfhack.units.getProfessionName(unit),
dfhack.TranslateName(unit.name, false, true)) -- get untranslated last name
end

local function copy_to_lua_table(vec)
local tab = {}
for k,v in ipairs(vec) do
Expand Down
5 changes: 1 addition & 4 deletions plugins/lua/sort/unitselector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ UnitSelectorOverlay.ATTRS{
local function get_unit_id_search_key(unit_id)
local unit = df.unit.find(unit_id)
if not unit then return end
return ('%s %s %s'):format(
dfhack.units.getReadableName(unit), -- last name is in english
dfhack.units.getProfessionName(unit),
dfhack.TranslateName(unit.name, false, true)) -- get untranslated last name
return sortoverlay.get_unit_search_key(unit)
end

function UnitSelectorOverlay:init()
Expand Down