2023-10-10 03:33:47 -06:00
|
|
|
local _ENV = mkmodule('plugins.sort.locationselector')
|
|
|
|
|
|
|
|
local sortoverlay = require('plugins.sort.sortoverlay')
|
|
|
|
local widgets = require('gui.widgets')
|
|
|
|
|
|
|
|
local location_selector = df.global.game.main_interface.location_selector
|
|
|
|
|
|
|
|
-- ----------------------
|
|
|
|
-- LocationSelectorOverlay
|
|
|
|
--
|
|
|
|
|
|
|
|
LocationSelectorOverlay = defclass(LocationSelectorOverlay, sortoverlay.SortOverlay)
|
|
|
|
LocationSelectorOverlay.ATTRS{
|
2023-10-15 01:14:12 -06:00
|
|
|
default_pos={x=48, y=6},
|
2023-10-10 03:33:47 -06:00
|
|
|
viewscreens='dwarfmode/LocationSelector',
|
2023-10-15 01:14:12 -06:00
|
|
|
frame={w=26, h=3},
|
2023-10-10 03:33:47 -06:00
|
|
|
}
|
|
|
|
|
2023-10-13 13:37:11 -06:00
|
|
|
local function add_spheres(hf, spheres)
|
|
|
|
if not hf then return end
|
|
|
|
for _, sphere in ipairs(hf.info.spheres.spheres) do
|
|
|
|
spheres[sphere] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function stringify_spheres(spheres)
|
|
|
|
local strs = {}
|
|
|
|
for sphere in pairs(spheres) do
|
|
|
|
table.insert(strs, df.sphere_type[sphere])
|
|
|
|
end
|
|
|
|
return table.concat(strs, ' ')
|
|
|
|
end
|
|
|
|
|
2023-10-10 03:33:47 -06:00
|
|
|
local function get_religion_string(religion_id, religion_type)
|
|
|
|
if religion_id == -1 then return end
|
|
|
|
local entity
|
2023-10-13 13:37:11 -06:00
|
|
|
local spheres = {}
|
2023-10-10 03:33:47 -06:00
|
|
|
if religion_type == 0 then
|
|
|
|
entity = df.historical_figure.find(religion_id)
|
2023-10-13 13:37:11 -06:00
|
|
|
add_spheres(entity, spheres)
|
2023-10-10 03:33:47 -06:00
|
|
|
elseif religion_type == 1 then
|
|
|
|
entity = df.historical_entity.find(religion_id)
|
2023-10-13 13:37:11 -06:00
|
|
|
if entity then
|
|
|
|
for _, deity in ipairs(entity.relations.deities) do
|
|
|
|
add_spheres(df.historical_figure.find(deity), spheres)
|
|
|
|
end
|
|
|
|
end
|
2023-10-10 03:33:47 -06:00
|
|
|
end
|
|
|
|
if not entity then return end
|
2023-10-13 13:37:11 -06:00
|
|
|
return ('%s %s'):format(dfhack.TranslateName(entity.name, true), stringify_spheres(spheres))
|
2023-10-10 03:33:47 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_profession_string(profession)
|
|
|
|
return df.profession[profession]:gsub('_', ' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
function LocationSelectorOverlay:init()
|
2023-10-15 01:14:12 -06:00
|
|
|
local panel = widgets.Panel{
|
|
|
|
visible=self:callback('get_key'),
|
|
|
|
}
|
|
|
|
panel:addviews{
|
2023-10-10 03:33:47 -06:00
|
|
|
widgets.BannerPanel{
|
|
|
|
frame={l=0, t=0, r=0, h=1},
|
|
|
|
subviews={
|
|
|
|
widgets.EditField{
|
|
|
|
view_id='search',
|
|
|
|
frame={l=1, t=0, r=1},
|
|
|
|
label_text="Search: ",
|
|
|
|
key='CUSTOM_ALT_S',
|
|
|
|
on_change=function(text) self:do_search(text) end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-10-15 01:14:12 -06:00
|
|
|
widgets.BannerPanel{
|
|
|
|
frame={l=0, t=2, r=0, h=1},
|
|
|
|
subviews={
|
|
|
|
widgets.ToggleHotkeyLabel{
|
|
|
|
view_id='hide_established',
|
|
|
|
frame={l=1, t=0, r=1},
|
|
|
|
label="Hide established:",
|
|
|
|
key='CUSTOM_SHIFT_E',
|
2023-10-17 00:40:07 -06:00
|
|
|
initial_option=false,
|
2023-10-15 01:14:12 -06:00
|
|
|
on_change=function() self:do_search(self.subviews.search.text, true) end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-10-10 03:33:47 -06:00
|
|
|
}
|
2023-10-15 01:14:12 -06:00
|
|
|
self:addviews{panel}
|
2023-10-10 03:33:47 -06:00
|
|
|
|
|
|
|
self:register_handler('TEMPLE', location_selector.valid_religious_practice_id,
|
2023-10-15 01:14:12 -06:00
|
|
|
curry(sortoverlay.flags_vector_search,
|
|
|
|
{
|
|
|
|
get_search_key_fn=get_religion_string,
|
|
|
|
matches_filters_fn=self:callback('matches_temple_filter'),
|
|
|
|
},
|
2023-10-10 04:57:03 -06:00
|
|
|
location_selector.valid_religious_practice))
|
2023-10-10 03:33:47 -06:00
|
|
|
self:register_handler('GUILDHALL', location_selector.valid_craft_guild_type,
|
2023-10-15 01:14:12 -06:00
|
|
|
curry(sortoverlay.single_vector_search,
|
|
|
|
{
|
|
|
|
get_search_key_fn=get_profession_string,
|
|
|
|
matches_filters_fn=self:callback('matches_guildhall_filter'),
|
|
|
|
}))
|
2023-10-10 03:33:47 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function LocationSelectorOverlay:get_key()
|
|
|
|
if location_selector.choosing_temple_religious_practice then
|
|
|
|
return 'TEMPLE'
|
|
|
|
elseif location_selector.choosing_craft_guild then
|
|
|
|
return 'GUILDHALL'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-15 01:14:12 -06:00
|
|
|
function LocationSelectorOverlay:reset()
|
|
|
|
LocationSelectorOverlay.super.reset(self)
|
|
|
|
self.cache = nil
|
2023-10-17 00:40:07 -06:00
|
|
|
self.subviews.hide_established:setOption(false, false)
|
2023-10-15 01:14:12 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function LocationSelectorOverlay:get_cache()
|
|
|
|
if self.cache then return self.cache end
|
|
|
|
local cache = {}
|
|
|
|
for _,location in ipairs(df.global.world.world_data.active_site[0].buildings) do
|
|
|
|
if df.abstract_building_templest:is_instance(location) then
|
|
|
|
ensure_keys(cache, 'temple', location.deity_type)[location.deity_data.Religion] = true
|
|
|
|
elseif df.abstract_building_guildhallst:is_instance(location) then
|
|
|
|
ensure_keys(cache, 'guildhall')[location.contents.profession] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.cache = cache
|
|
|
|
return self.cache
|
|
|
|
end
|
|
|
|
|
|
|
|
function LocationSelectorOverlay:matches_temple_filter(id, flag)
|
|
|
|
if id == -1 then return true end
|
|
|
|
local hide_established = self.subviews.hide_established:getOptionValue()
|
|
|
|
return not hide_established or not safe_index(self:get_cache(), 'temple', flag, id)
|
|
|
|
end
|
|
|
|
|
|
|
|
function LocationSelectorOverlay:matches_guildhall_filter(id)
|
|
|
|
local hide_established = self.subviews.hide_established:getOptionValue()
|
|
|
|
return not hide_established or not safe_index(self:get_cache(), 'guildhall', id)
|
|
|
|
end
|
|
|
|
|
2023-10-10 03:33:47 -06:00
|
|
|
return _ENV
|