forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autonick.lua
54 lines (44 loc) · 1.42 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
-- 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 ]``
]====]
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
for _,unit in ipairs(df.global.world.units.active) do
if (#names == 0) then 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)
end
end