Skip to content

Commit

Permalink
Feat: Add "show" to pref-adjust
Browse files Browse the repository at this point in the history
This feature returns the list of Likes for a selected dwarf.

This script may be useful if:
* a user wants to view a quick list of likes for a dwarf
* a user wants to view the list of likes after updating a dwarfs likes
* Other scripts may find this helpful, as this functionality is not otherwise exposed via dfhack
  • Loading branch information
TolMera committed Jan 18, 2025
1 parent fbcb217 commit 5b7e692
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion pref-adjust.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pss_counter = pss_counter or 31415926

-- ---------------------------------------------------------------------------
function insert_preference(unit, preftype, val1)

if preftype == df.unit_preference.T_type.LikeMaterial then
utils.insert_or_update(unit.status.current_soul.preferences, {
new = true,
Expand Down Expand Up @@ -269,6 +268,51 @@ function build_all_lists(printflag)
end
end -- end func build_all_lists
-- ---------------------------------------------------------------------------
function get_preferences(unit)
if unit == nil then
print("No unit selected!")
return
end

local preferences = unit.status.current_soul.preferences
if #preferences == 0 then
print("Unit " .. unit_name_to_console(unit) .. " has no preferences.")
return
end

print("Preferences for " .. unit_name_to_console(unit) .. ":")
for _, pref in ipairs(preferences) do
local pref_type = df.unit_preference.T_type[pref.type]
local description = ""

if pref_type == "LikeMaterial" then
description = "Likes material: " .. dfhack.matinfo.getToken(pref.mattype, pref.matindex)
elseif pref_type == "LikeFood" then
description = "Likes food: " .. dfhack.matinfo.getToken(pref.mattype, pref.matindex)
elseif pref_type == "LikeItem" then
description = "Likes item type: " .. tostring(pref.item_type)
elseif pref_type == "LikePlant" then
description = "Likes plant: " .. dfhack.matinfo.getToken(pref.mattype, pref.matindex)
elseif pref_type == "HateCreature" then
description = "Hates creature: " .. df.global.world.raws.creatures.all[pref.poetic_form_id].creature_id
elseif pref_type == "LikeColor" then
description = "Likes color: " .. df.global.world.raws.descriptors.colors[pref.poetic_form_id].id
elseif pref_type == "LikeShape" then
description = "Likes shape: " .. df.global.world.raws.descriptors.shapes[pref.poetic_form_id].id
elseif pref_type == "LikePoeticForm" then
description = "Likes poetic form: " .. dfhack.translation.translateName(df.global.world.poetic_forms.all[pref.poetic_form_id].name, true)
elseif pref_type == "LikeMusicalForm" then
description = "Likes musical form: " .. dfhack.translation.translateName(df.global.world.musical_forms.all[pref.poetic_form_id].name, true)
elseif pref_type == "LikeDanceForm" then
description = "Likes dance form: " .. dfhack.translation.translateName(df.global.world.dance_forms.all[pref.poetic_form_id].name, true)
else
description = "Unknown preference type: " .. tostring(pref.type)
end

print(description)
end
end -- end function: get_preferences
-- ---------------------------------------------------------------------------
function unit_name_to_console(unit)
return dfhack.df2console(dfhack.units.getReadableName(unit))
end
Expand Down Expand Up @@ -317,10 +361,14 @@ elseif opt == "all" then
handle_all("IDEAL")
elseif opt == "goth_all" then
handle_all("GOTH")
if opt == "show" then
local unit = dfhack.gui.getSelectedUnit()
get_preferences(unit)
else
print ("Sets preferences of one dwarf, or of all dwarves, using profiles.")
print ("Valid options:")
print ("list -- show available preference type lists")
print ("show -- show current preferences")
print ("clear -- clear preferences of selected unit")
print ("clear_all -- clear preferences of all units")
print ("goth -- alter current dwarf preferences to Goth")
Expand Down

0 comments on commit 5b7e692

Please sign in to comment.