Created
December 7, 2014 00:22
-
-
Save Alloyed/a15f73c61cf532acbe58 to your computer and use it in GitHub Desktop.
Automatically gives dwarves nicknames
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Automatically gives dwarves nicknames | |
local NICK_MAXLEN = 40 | |
function get_dorfs() | |
local used_names = {} | |
local new_dorfs = {} | |
for _, u in ipairs(df.global.world.units.active) do | |
if dfhack.units.isCitizen(u) then | |
if u.name.nickname ~= "" then | |
used_names[u.name.nickname] = true | |
else | |
table.insert(new_dorfs, u) | |
end | |
end | |
end | |
return used_names, new_dorfs | |
end | |
function name_pool(fname, used_names) | |
local pool = {} | |
for name in io.lines(fname) do | |
if name ~= "" and not used_names[name] then | |
table.insert(pool, name) | |
end | |
end | |
return pool | |
end | |
function choice(list) | |
if #list < 1 then return nil end | |
return list[math.random(#list)] | |
end | |
function name_dorfs(fname) | |
local used_names, new_dorfs = get_dorfs() | |
local new_names = name_pool(fname, used_names) | |
for _, dorf in ipairs(new_dorfs) do | |
local name = choice(new_names) | |
if name then | |
print(dfhack.df2utf(dfhack.TranslateName(dorf.name, false)) .. | |
" is now known as `" .. name .. "'") | |
dfhack.units.setNickname(dorf, dfhack.utf2df(name)) | |
new_names[name] = false | |
end | |
end | |
end | |
function help() | |
print "USAGE: dwarfify [--loop] <filename>" | |
print "<filename> should be a newline seperated list of nicknames." | |
print " empty lines and duplicate nicknames are okay." | |
print "[--loop] if set, this command will rerun every 10 seconds or so." | |
end | |
function loop(fname) | |
name_dorfs(fname) | |
dfhack.timeout(100 * 10, 'frames', function() return loop(fname) end) | |
end | |
function main(fname, maybe_fname) | |
local do_loop = false | |
if not fname then | |
return help() | |
end | |
if fname == "--loop" then | |
if not maybe_fname then | |
return help() | |
end | |
fname = maybe_fname | |
do_loop = true | |
end | |
local okay, err = io.open(fname) | |
if not okay then | |
error(err) | |
end | |
if do_loop then | |
loop(fname) | |
print("Loop started.") | |
else | |
name_dorfs(fname) | |
end | |
end | |
main(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment