Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: dfhack script to list dorfs not in specified burrow  (Read 4762 times)

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
dfhack script to list dorfs not in specified burrow
« on: February 27, 2014, 02:35:28 am »

I wanted this so I could see who's still outside before I pull the gate lever.  It may have other uses as well.

Put it in hack/scripts/

Tested with dfhack r3 on Windows and Linux.  May work with other releases.
Some of the characters in the names may not display correctly on Linux.

whosout.lua -
Code: [Select]
-- List citizens not in any of the burrows passed as arguments.
--
-- Version 1.0:
--    Removed column names
--    Removed profession coloring
--    Changed 'occupation' to 'profession'
--    Incorporated a couple of technical suggestions from Putnam
--    Corrected logic for speeds
--    Aligned columns in the output
--    Moved speed to first column, to simplify columnation logic
--
-- Credit where due - essentially all of this code is from existing scripts:
--    fat-dwarves.lua
--    siren.lua

local args = {...}
local burrows = {}
local dorfs = {}
local name

-- sanity checks:
if not dfhack.isMapLoaded() then
    qerror('You must load a map to use this.')
end
if #args == 0 then
    qerror('Pass the name(s) of one or more burrows as arguments.')
    return
end

-- show help text:
if args[1] == 'help' then
    print('')
    print('Shows relative speed, name, and profession of citizens who are')
    print('not in any of the specified burrows')
    print('')
    print('Usage:')
    print('   whosout <burrowname(s)>')
    print('')
    return
end

-- see if a location is in one of the burrows in the table built below:
function inBurrows(pos)
    if #burrows == 0 then
        return false
    else
        for _,v in ipairs(burrows) do
            if dfhack.burrows.isAssignedTile(v, pos) then
                return true
            end
        end
        return false
    end
end

-- comparator for sorting units by name:
function compNames(a,b)
    return dfhack.TranslateName(dfhack.units.getVisibleName(a)) < dfhack.TranslateName(dfhack.units.getVisibleName(b))
end

-- round a positive number to an integer:
function round(n)
    return math.floor(n+0.5)
end

-- build a table of the burrows in the argument list:
for _,v in ipairs(args) do
    local b = dfhack.burrows.findByName(v)
    if not b then
        qerror('Unknown burrow: '..v)
    else
        table.insert(burrows, b)
    end
end

-- collect and display a list of the citizens not in one of those burrows:
for _,v in ipairs(df.global.world.units.active) do
    if dfhack.units.isCitizen(v) then
        local x,y,z = dfhack.units.getPosition(v)
        if not inBurrows(xyz2pos(x,y,z)) then
            table.insert(dorfs,v)
        end
    end
end
dfhack.color(nil)
if #dorfs == 0 then
    if #burrows == 1 then
        print('All your live, sane citizens are in that burrow.')
    else
        print('All your live, sane citizens are in one of those burrows.')
    end
else
    table.sort(dorfs,compNames)
    for _,v in ipairs(dorfs) do
    name = dfhack.TranslateName(dfhack.units.getVisibleName(v))
    dfhack.print(round(1000000/dfhack.units.computeMovementSpeed(v)),name)
    if string.len(name) < 32 then
        dfhack.print('\t')
    end
    if string.len(name) < 24 then
        dfhack.print('\t')
    end
    if string.len(name) < 16 then
        dfhack.print('\t')
    end
    if string.len(name) < 8 then
        dfhack.print('\t')
    end
    print(dfhack.units.getProfessionName(v))
    end
end

Edit #1: removed the bnames stuff that was not actually used.
Edit #2: if no dorfs, message states so; if dorfs listed, now shows name, occupation, and current movement speed, all in occupation colors.
Edit #3: posted version 1.0; added compatibility notes; changes now shown as comments in the code.
« Last Edit: February 27, 2014, 08:09:48 pm by Bo-Rufus CMVII »
Logged

fortydayweekend

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #1 on: February 27, 2014, 03:36:10 am »

Cool! Works for me. This'll be really useful.
Logged
The Sea Lamprey bites the Miner in the lower left back teeth and the severed part sails off in an arc!

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #2 on: February 27, 2014, 07:10:40 pm »

1. df.global.world.units.active is probably a better one to increment through than df.global.world.units.all simply by virtue of having less stuff in it.
2. A better (I.E shorter) way to write that comp function would be:

Code: [Select]
function compnames(a,b)
    return dfhack.TranslateName(dfhack.units.getVisibleName(a)) < dfhack.TranslateName(dfhack.units.getVisibleName(b))
end

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #3 on: February 27, 2014, 08:10:42 pm »

Thanks for the suggestions.  I just posted an update, with those and a number of other improvements.
Logged

rmblr

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #4 on: December 11, 2014, 08:40:08 am »

Stumbled across this script today! Nice work!  You should consider submitting for inclusion in DFHack by default.
Logged

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #5 on: December 11, 2014, 10:51:17 am »

Thanks.

Are you supposed to submit stuff?  I figured if I post it it's there for anyone who wants it.

Have you tried it with the new DF?  I've been too busy to upgrade.
Logged

rmblr

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #6 on: December 11, 2014, 11:04:13 am »

Yup, it works great in 0.40.19.

And yea, submitting is the main way to get more people to use it. I only found this thread cause I was trolling the forum archives for old DFHack scripts!

I'll submit it on behalf of yourself if you don't mind.
Logged

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #7 on: December 11, 2014, 12:02:32 pm »

Please do.

I've got a couple more if you're interested:

show-dfhack-version: http://www.bay12forums.com/smf/index.php?topic=136749.0

show-cursed: http://www.bay12forums.com/smf/index.php?topic=136975.0
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #8 on: December 12, 2014, 06:57:30 pm »

dfhack.df2utf() should make accented characters display properly on Linux/OS X (I'm not sure what it would do on Windows). For example (assuming this line prints dwarves' names):
Code: [Select]
print(dfhack.df2utf(dfhack.units.getProfessionName(v)))
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #9 on: April 21, 2015, 08:53:02 pm »

dfhack.df2utf() should make accented characters display properly on Linux/OS X (I'm not sure what it would do on Windows).
I'm finally back to DF after a longish hiatus, and I gave this a try.  It does work on my Linux system, but fubars the output on my Windows system.  After poking around on the web I found this hack for making it work either way:
Code: [Select]
    if os.getenv("OS") ~= nil then
    -- Windows - don't convert to UTF
    else
    -- Linux or OS X - convert to UTF
    end
Might not be as robust as desirable, so I'll be happy to hear alternative suggestions.

Thanks for the tip.
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #10 on: April 21, 2015, 08:54:00 pm »

I actually added a "dfhack.df2console" wrapper recently, which should convert DF text to the correct encoding on all platforms.
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #11 on: April 21, 2015, 09:56:53 pm »

Is that "native", or does a script have to do something to pick it up?
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #12 on: April 21, 2015, 10:59:59 pm »

It should be able to be used in the same way as df2utf above, if that's what you mean. I don't remember exactly what version it was added in, but it's in the most recent DFHack release (the NEWS file should list the specific version it was added in).
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

lethosor

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #13 on: September 04, 2015, 09:46:50 am »

A couple people in the DT thread mentioned that this script is useful, so I'm looking at adding it to DFHack. Was the speed display changed at some point?
Back in 34.11 it used to put the outside dwarf's speed as well, but that part no longer functions as it did. I think it used to be as a percentage, now its a number between 250 - 2200. Lower being slower moving.
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

Bo-Rufus CMVII

  • Bay Watcher
    • View Profile
Re: dfhack script to list dorfs not in specified burrow
« Reply #14 on: September 07, 2015, 11:03:34 pm »

The speeds are utterly broken with the new DF.  I posted some experiments as a dfhack bug report several months ago, but I don't think it has been fixed.  https://github.com/DFHack/dfhack/issues/607

However, this script should still be useful without the speeds - I mostly needed them for the show-encumberance script.

Logged