Advertisement
Guest User

exportprefs.lua

a guest
Aug 10th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. -- export the dwarfs' preferences to a CSV file
  2. local fHandle
  3.  
  4. -- output unit's prefs to file
  5. local function printUnitPrefs(unit)
  6.    local dwarfName = dfhack.TranslateName(dfhack.units.getVisibleName(unit))
  7.    -- the preference list as it's being built
  8.    local prefList = {[0]='"',[1]='"',[2]='"',[3]='"',[4]='"',[5]='"',[6]='"',[7]='"',[8]='"'}
  9.    
  10.    -- iterate through all the prefs and build the (text) list
  11.    for k, v in pairs(unit.status.current_soul.preferences) do
  12.       local prefName = ''
  13.       -- get the name based on the item type
  14.       if (v.type == 0) then
  15.          -- Like Material
  16.          prefName = dfhack.matinfo.decode(v.mattype, v.matindex):toString(10067)
  17.          
  18.       elseif (v.type == 1) or (v.type == 3) then
  19.          -- Like/Hate Creature
  20.          prefName = df.creature_raw.find(v.creature_id).name[0]
  21.          
  22.       elseif (v.type == 2) then
  23.          -- Like Food
  24.          -- if material index is -1 then it's a vermin food item
  25.          if (v.matindex == -1) then
  26.             -- it's a vermin, use creature raws
  27.             prefName = df.creature_raw.find(v.creature_id).name[0]
  28.          else
  29.             prefName = dfhack.matinfo.decode(v.mattype, v.matindex):toString(10067)
  30.          end
  31.      
  32.       elseif (v.type == 4) then
  33.          -- Like Item
  34.          -- get general name
  35.          prefName = df.item_type.attrs[v.item_type].caption
  36.          -- use subtype name if a subtype exists
  37.          local def = dfhack.items.getSubtypeDef(v.item_type, v.item_subtype)
  38.          if def then
  39.             prefName = def.name
  40.          end
  41.          
  42.       elseif (v.type == 5) then
  43.          -- Like Plant
  44.          prefName = df.plant_raw.find(v.plant_id).name
  45.      
  46.       elseif (v.type == 6) then
  47.          -- Like Tree
  48.          prefName = df.plant_raw.find(v.plant_id).name
  49.          
  50.       elseif (v.type == 7) then
  51.          -- Like Color
  52.          prefName = df.descriptor_color.find(v.color_id).name
  53.          
  54.       elseif (v.type == 8) then
  55.          -- Like Shape
  56.          prefName = df.descriptor_shape.find(v.shape_id).name
  57.          
  58.       end
  59.  
  60.       -- add the pref name to the appropriate category in the list
  61.       if (string.len(prefList[v.type]) == 1) then
  62.          -- first entry
  63.          prefList[v.type] = '"' .. prefName
  64.       else
  65.          -- subsequent entries
  66.          prefList[v.type] = prefList[v.type] .. ', ' .. prefName
  67.       end
  68.    end
  69.    
  70.    -- close the quotes for all the preflist entries
  71.    for v = 0, 8, 1 do
  72.       prefList[v] = prefList[v] .. '"'
  73.    end
  74.    
  75.    -- write the dwarf name, then all the prefs
  76.    fHandle:write( '"' .. dwarfName .. '",' )
  77.    for v = 0, 8, 1 do
  78.       fHandle:write( prefList[v] .. ',' )
  79.    end
  80.    
  81.    -- newline
  82.    fHandle:write( '\n' )
  83. end
  84.  
  85. -- open 'prefs.csv' in the DF directory
  86. fHandle = io.open( dfhack.getDFPath() .. '\\prefs.csv', 'w+' )
  87.  
  88. -- print CSV column labels
  89. fHandle:write( '"Dwarf Name","Liked Materials","Liked Creatures","Liked Food/Drink","Hated Vermin","Liked Items","Liked Plants","Liked Trees","Liked Colors","Liked Shapes"\n' )
  90.  
  91. -- loop through all citizens and print their preferences
  92. for k, v in ipairs(df.global.world.units.active) do
  93.    if dfhack.units.isCitizen(v) and not printed then
  94.       printUnitPrefs(v)
  95.    end
  96. end
  97.  
  98. io.close( fHandle )
  99. print ('Preferences exported to prefs.csv')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement