Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A script that notifies the user of the most common bad thoughts in the fort.
local function getRaceName()
return df.creature_raw.find(df.global.ui.race_id).name[1]
end
local function emotionIsNegative(thought)
return thought.type~=-1 and df.emotion_type.attrs[thought.type].divider>0
end
local function write_gamelog_and_announce(msg,color)
dfhack.gui.showAnnouncement(msg,color)
dfhack.gui.writeToGamelog(msg)
end
local function checkForBadThoughts(silent)
local thoughts={}
local mostPopularNegativeThought={cur_amount=0,thought_type=-1}
for _,unit in ipairs(df.global.world.units.active) do
if dfhack.units.isCitizen(unit) then
for __,emotion in ipairs(unit.status.current_soul.personality.emotions) do
if emotionIsNegative(emotion) then
thoughts[emotion.thought]=thoughts[emotion.thought] or 0
thoughts[emotion.thought]=thoughts[emotion.thought]+1
if thoughts[emotion.thought]>mostPopularNegativeThought.cur_amount then
mostPopularNegativeThought.cur_amount=thoughts[emotion.thought]
mostPopularNegativeThought.thought_type=emotion.thought
end
end
end
end
end
if df.unit_thought_type[mostPopularNegativeThought.thought_type] then
write_gamelog_and_announce('Your ' .. getRaceName() ..' are most feeling bad ' .. df.unit_thought_type.attrs[mostPopularNegativeThought.thought_type].caption..'.',COLOR_CYAN)
end
end
if ...=='-force' then
checkForBadThoughts()
else
require('repeat-util').scheduleEvery('badThoughtNotifier',7,'days',checkForBadThoughts)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment