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

Refactor unretire-anyone code and make it a module so you can access its naming scheme function #1231

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
100 changes: 59 additions & 41 deletions unretire-anyone.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
--@module=true

local options = {}

local argparse = require('argparse')
local commands = argparse.processArgsGetopt({ ... }, {
{ 'd', 'dead', handler = function() options.dead = true end }
})

local dialogs = require 'gui.dialogs'

local viewscreen = dfhack.gui.getDFViewscreen(true)
if viewscreen._type ~= df.viewscreen_setupadventurest then
qerror("This script can only be used during adventure mode setup!")
end
local dialogs = require('gui.dialogs')

--luacheck: in=df.viewscreen_setupadventurest,df.nemesis_record
function addNemesisToUnretireList(advSetUpScreen, nemesis, index)
local function addNemesisToUnretireList(advSetUpScreen, nemesis, index)
local unretireOption = false
for i = #advSetUpScreen.valid_race - 1, 0, -1 do
if advSetUpScreen.valid_race[i] == -2 then -- this is the "Specific Person" option on the menu
Expand All @@ -39,59 +36,80 @@ function addNemesisToUnretireList(advSetUpScreen, nemesis, index)
advSetUpScreen.nemesis_index:insert('#', index)
end

function getHistfigShortSummary(histFig)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rewrite this to make use of dfhack.units.getReadableName(histFig)? Most of the logic is covered by that function.

Which other tool needs access to this function? Can they also use getReadableName?

local race = df.creature_raw.find(histFig.race)
local name = 'Unknown Creature'
local sym = nil
if race then
local creature = race.caste[histFig.caste]
name = creature.caste_name[0]
sym = df.pronoun_type.attrs[creature.sex].symbol
end
if histFig.info and histFig.info.curse then
local curse = histFig.info.curse
if curse.name ~= '' then
name = name .. ' ' .. curse.name
end
if curse.undead_name ~= '' then
name = curse.undead_name .. " - reanimated " .. name
end
end
if histFig.flags.ghost then
name = name .. " ghost"
end
if sym then
name = name .. ' (' .. sym .. ')'
end
if histFig.name.has_name then
name = name ..
'\n' .. dfhack.TranslateName(histFig.name) ..
'\n"' .. dfhack.TranslateName(histFig.name, true) .. '"'
else
name = name ..
'\nUnnamed'
end
return name
end

--luacheck: in=table
function showNemesisPrompt(advSetUpScreen)
local function showNemesisPrompt(advSetUpScreen)
local choices = {}
for i, nemesis in ipairs(df.global.world.nemesis.all) do
if nemesis.figure and not nemesis.flags.ADVENTURER then -- these are already available for unretiring
if nemesis.figure then
local histFig = nemesis.figure
local histFlags = histFig.flags
if (histFig.died_year == -1 or histFlags.ghost or options.dead) and
not histFlags.deity and
not histFlags.force
if (histFig.died_year == -1 or histFig.flags.ghost or options.dead) and
not histFig.flags.deity and
not histFig.flags.force
then
local creature = df.creature_raw.find(histFig.race).caste[histFig.caste]
local name = creature.caste_name[0]
if histFig.info and histFig.info.curse then
local curse = histFig.info.curse
if curse.name ~= '' then
name = name .. ' ' .. curse.name
end
if curse.undead_name ~= '' then
name = curse.undead_name .. " - reanimated " .. name
end
end
if histFlags.ghost then
name = name .. " ghost"
end
local sym = df.pronoun_type.attrs[creature.sex].symbol
if sym then
name = name .. ' (' .. sym .. ')'
end
if histFig.name.has_name then
name = name ..
'\n' .. dfhack.TranslateName(histFig.name) ..
'\n"' .. dfhack.TranslateName(histFig.name, true) .. '"'
else
name = name ..
'\nUnnamed'
if histFig.died_year == -1 and nemesis.flags.ADVENTURER then
-- already available for unretiring
goto continue
end
local name = getHistfigShortSummary(histFig)
table.insert(choices, { text = name, nemesis = nemesis, search_key = name:lower(), idx = i })
end
end
::continue::
end

dialogs.ListBox{
frame_title = 'unretire-anyone',
text = 'Select someone to add to the "Specific Person" list:',
text_pen = COLOR_WHITE,
choices = choices,
on_select = function(id, choice)
addNemesisToUnretireList(advSetUpScreen, choice.nemesis, choice.idx)
end,
on_select = function(id, choice) addNemesisToUnretireList(advSetUpScreen, choice.nemesis, choice.idx) end,
with_filter = true,
row_height = 3,
}:show()
end

if dfhack_flags.module then
return
end

local viewscreen = dfhack.gui.getDFViewscreen(true)
if viewscreen._type ~= df.viewscreen_setupadventurest then
qerror("This script can only be used during adventure mode setup!")
end

showNemesisPrompt(viewscreen)