forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autonick.lua
92 lines (75 loc) · 2.27 KB
/
autonick.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
-- gives dwarves unique nicknames
--[====[
autonick
========
Gives dwarves unique nicknames chosen randomly from ``dfhack-config/autonick.txt``.
One nickname per line.
Empty lines, lines beginning with ``#`` and repeat entries are discarded.
Dwarves with manually set nicknames are ignored.
If there are fewer available nicknames than dwarves, the remaining
dwarves will go un-nicknamed.
You may wish to use this script with the "repeat" command, e.g:
``repeat -name autonick -time 3 -timeUnits months -command [ autonick all ]``
Usage:
autonick all [<options>]
autonick help
Options:
:``-h``, ``--help``:
Show this text.
:``-q``, ``--quiet``:
Do not report how many dwarves were given nicknames.
]====]
local options = {}
local argparse = require('argparse')
local commands = argparse.processArgsGetopt({...}, {
{'h', 'help', handler=function() options.help = true end},
{'q', 'quiet', handler=function() options.quiet = true end},
})
if #commands ~= 1 or commands[1] ~= "all" then
options.help = true
end
if options.help == true then
print(dfhack.script_help())
return
end
local seen = {}
--check current nicknames
for _,unit in ipairs(df.global.world.units.active) do
if dfhack.units.isCitizen(unit) and
unit.name.nickname ~= "" then
seen[unit.name.nickname] = true
end
end
local names = {}
-- grab list, put in array
local path = dfhack.getDFPath () .. "/dfhack-config/autonick.txt";
for line in io.lines(path) do
line = line:trim()
if (line ~= "")
and (not line:startswith('#'))
and (not seen[line]) then
table.insert(names, line)
seen[line] = true
end
end
--assign names
local count = 0
for _,unit in ipairs(df.global.world.units.active) do
if (#names == 0) then
if options.quiet ~= true then
print("no free names left in dfhack-config/autonick.txt")
end
break
end
--if there are any names left
if dfhack.units.isCitizen(unit) and
unit.name.nickname == "" then
newnameIndex = math.random (#names)
dfhack.units.setNickname(unit, names[newnameIndex])
table.remove(names, newnameIndex)
count = count + 1
end
end
if options.quiet ~= true then
print(("gave nicknames to %s dwarves."):format(count))
end