From 217258b0137b51001e0136ae72c4cf3d71ec3521 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Wed, 8 Nov 2023 12:41:28 -0800 Subject: [PATCH] Better code reuse with locationselector.get_religion_string --- plugins/lua/sort/locationselector.lua | 2 +- plugins/lua/sort/places.lua | 39 ++++----------------------- 2 files changed, 6 insertions(+), 35 deletions(-) diff --git a/plugins/lua/sort/locationselector.lua b/plugins/lua/sort/locationselector.lua index bdacc1630..d1284e141 100644 --- a/plugins/lua/sort/locationselector.lua +++ b/plugins/lua/sort/locationselector.lua @@ -31,7 +31,7 @@ local function stringify_spheres(spheres) return table.concat(strs, ' ') end -local function get_religion_string(religion_id, religion_type) +function get_religion_string(religion_id, religion_type) if religion_id == -1 then return end local entity local spheres = {} diff --git a/plugins/lua/sort/places.lua b/plugins/lua/sort/places.lua index 1ff4af8b3..7e1b8efaa 100644 --- a/plugins/lua/sort/places.lua +++ b/plugins/lua/sort/places.lua @@ -1,6 +1,7 @@ local _ENV = mkmodule('plugins.sort.places') local sortoverlay = require('plugins.sort.sortoverlay') +local locationselector = require('plugins.sort.locationselector') local widgets = require('gui.widgets') local utils = require('utils') @@ -37,39 +38,9 @@ local language_name_types = { [df.language_name_type.Library] = 'Library' } -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 - -local function get_religion_string(religion_id, religion_type) - if religion_id == -1 then return '' end - if religion_type == -1 then return 'Temple' end - local entity - local spheres = {} - if religion_type == 0 then - entity = df.historical_figure.find(religion_id) - add_spheres(entity, spheres) - elseif religion_type == 1 then - entity = df.historical_entity.find(religion_id) - if entity then - for _, deity in ipairs(entity.relations.deities) do - add_spheres(df.historical_figure.find(deity), spheres) - end - end - end - if not entity then return end - return ('%s %s'):format(dfhack.TranslateName(entity.name, true), stringify_spheres(spheres)) +local function get_location_religion(religion_id, religion_type) + if religion_type == -1 then return 'Temple' + else return locationselector.get_religion_string(religion_id, religion_type) or '' end end local function get_default_zone_name(zone_type) @@ -128,7 +99,7 @@ local function get_location_search_key(zone) -- for temples and guildhalls, get assigned organization if language_name_types[building.name.type] == 'Temple' then - table.insert(result, get_religion_string(building.deity_data.Deity or building.deity_data.Religion, building.deity_type)) + table.insert(result, get_location_religion(building.deity_data.Deity or building.deity_data.Religion, building.deity_type)) elseif language_name_types[building.name.type] == 'Guildhall' then -- Broke this up into two locals because table.insert doesn't like it all being inside the second argument for some reason local profession = df.profession[building.contents.profession]