Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- export the dwarfs' preferences to a CSV file
- local fHandle
- -- output unit's prefs to file
- local function printUnitPrefs(unit)
- local dwarfName = dfhack.TranslateName(dfhack.units.getVisibleName(unit))
- -- the preference list as it's being built
- local prefList = {[0]='"',[1]='"',[2]='"',[3]='"',[4]='"',[5]='"',[6]='"',[7]='"',[8]='"'}
- -- iterate through all the prefs and build the (text) list
- for k, v in pairs(unit.status.current_soul.preferences) do
- local prefName = ''
- -- get the name based on the item type
- if (v.type == 0) then
- -- Like Material
- prefName = dfhack.matinfo.decode(v.mattype, v.matindex):toString(10067)
- elseif (v.type == 1) or (v.type == 3) then
- -- Like/Hate Creature
- prefName = df.creature_raw.find(v.creature_id).name[0]
- elseif (v.type == 2) then
- -- Like Food
- -- if material index is -1 then it's a vermin food item
- if (v.matindex == -1) then
- -- it's a vermin, use creature raws
- prefName = df.creature_raw.find(v.creature_id).name[0]
- else
- prefName = dfhack.matinfo.decode(v.mattype, v.matindex):toString(10067)
- end
- elseif (v.type == 4) then
- -- Like Item
- -- get general name
- prefName = df.item_type.attrs[v.item_type].caption
- -- use subtype name if a subtype exists
- local def = dfhack.items.getSubtypeDef(v.item_type, v.item_subtype)
- if def then
- prefName = def.name
- end
- elseif (v.type == 5) then
- -- Like Plant
- prefName = df.plant_raw.find(v.plant_id).name
- elseif (v.type == 6) then
- -- Like Tree
- prefName = df.plant_raw.find(v.plant_id).name
- elseif (v.type == 7) then
- -- Like Color
- prefName = df.descriptor_color.find(v.color_id).name
- elseif (v.type == 8) then
- -- Like Shape
- prefName = df.descriptor_shape.find(v.shape_id).name
- end
- -- add the pref name to the appropriate category in the list
- if (string.len(prefList[v.type]) == 1) then
- -- first entry
- prefList[v.type] = '"' .. prefName
- else
- -- subsequent entries
- prefList[v.type] = prefList[v.type] .. ', ' .. prefName
- end
- end
- -- close the quotes for all the preflist entries
- for v = 0, 8, 1 do
- prefList[v] = prefList[v] .. '"'
- end
- -- write the dwarf name, then all the prefs
- fHandle:write( '"' .. dwarfName .. '",' )
- for v = 0, 8, 1 do
- fHandle:write( prefList[v] .. ',' )
- end
- -- newline
- fHandle:write( '\n' )
- end
- -- open 'prefs.csv' in the DF directory
- fHandle = io.open( dfhack.getDFPath() .. '\\prefs.csv', 'w+' )
- -- print CSV column labels
- fHandle:write( '"Dwarf Name","Liked Materials","Liked Creatures","Liked Food/Drink","Hated Vermin","Liked Items","Liked Plants","Liked Trees","Liked Colors","Liked Shapes"\n' )
- -- loop through all citizens and print their preferences
- for k, v in ipairs(df.global.world.units.active) do
- if dfhack.units.isCitizen(v) and not printed then
- printUnitPrefs(v)
- end
- end
- io.close( fHandle )
- print ('Preferences exported to prefs.csv')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement