Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2]

Author Topic: DFhack metrics plugin.  (Read 2747 times)


  • Bay Watcher
    • View Profile
    • worldbicyclist
Re: DFhack metrics plugin.
« Reply #15 on: August 23, 2018, 10:43:14 am »

I always prefer dfhack features that dont break immersion. So I'd write a Workshop with reactions, and soon little Urist walks over to the psychologists office to read up on the records of peoples stress records.

All without the player alt-tabbing out of the game, opening third party tools.
::: ☼Meph Tileset☼☼Map Tileset☼- 32x graphic sets with TWBT :::
::: ☼MASTERWORK DF☼ - A comprehensive mod pack now on Patreon - 250.000+ downloads and counting :::
::: - Follow my bike tours around the world - 148 countries visited :::


  • Bay Watcher
    • View Profile
Re: DFhack metrics plugin.
« Reply #16 on: April 22, 2019, 04:25:34 am »

Here is a script I'm using to track stuff over time in fortress mode. It writes metrics to a CSV output, can track several different metrics, and is intended to be used with dfhack's repeat function.

I've tried your script, but it causes an error when running this command:
Code: [Select]
monitor -watch relations -verbose
...RFF~1\Dwarf Fortress 0.44.12/hack/scripts/monitor.lua:428: attempt to index a nil value

stack traceback:
        ...RFF~1\Dwarf Fortress 0.44.12/hack/scripts/monitor.lua:428: in global 'GetUnitRelations'
        ...RFF~1\Dwarf Fortress 0.44.12/hack/scripts/monitor.lua:576: in local 'script_code'
        ...ames\DWARFF~1\Dwarf Fortress 0.44.12\hack\lua\dfhack.lua:680: in function 'dfhack.run_script_with_env'
        (...tail calls...)

There error occurs, because "df.unit.find(r_local_id)" seems to result in a nil. Seems be the case for deitys ...

This is my fixed version:

Code: [Select]
function GetUnitRelations(unit, unitRelationsTable)
-- Get the various relations for a unit. Goal here is to track relation strength
-- These are stored against the historical id, not the unit id. Some conversion is necessary.
-- Courtesy of PatrickLundell and the DFHack thread

-- Initialize variables
local i = 0
local tmpUnitRelationsTable = {}

-- Convert unit id to historical id
local hf = df.historical_figure.find(unit.hist_figure_id)

-- confirm unit actually has relationships for listing

if( == nil ) then
-- Just stop

-- Get a list of unit's relation ids and strengths
-- Presumably, rank 0 means no relation
for i=0, do
if([i].rank > 0) then
local r_histfig_id =[i].histfig_id
local r_local_id = df.historical_figure.find(r_histfig_id)
local r_attitude = 0
local r_counter = 0
-- Does work: Testing for array length; Doesn't work: testing for next array element, testing for array, testing for exist
local attitude =[i].attitude

if attitude >0 then
r_attitude =[i].attitude[0]
r_counter =[i].counter[0]
local r_rank =[i].rank
local myString = r_rank .. "(" .. r_attitude .. ")"

    -- Always convert historical ids back to unit ids
-- How should offsite/never arrived stuff work here?
-- Expected format here is #, local unit name, rank (attitude)
r_local_id = df.unit.find(r_local_id.unit_id)
if not (r_local_id == nil) then
if verbose then print ("id:" .. i .. "|" .. dfhack.TranslateName(, translate) .. ": rank=" .. r_rank .. ", attitude=" .. r_attitude .. ", r_counter=" .. r_counter) end
local r_insert = {i,dfhack.TranslateName(, translate),myString}
Pages: 1 [2]