Created
April 29, 2021 20:44
-
-
Save chemputer/9d1f6f59550d18c5b23457af07d78433 to your computer and use it in GitHub Desktop.
DFHack Set Dorf Nicknames from List in a file
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
local open = io.open | |
-- see if the file exists | |
function file_exists(file) | |
local f = io.open(file, "rb") | |
if f then f:close() end | |
return f ~= nil | |
end | |
-- reads a file line by line | |
function lines_from(file) | |
if not file_exists(file) then return {} end | |
lines = {} | |
for line in io.lines(file) do | |
lines[#lines + 1] = line | |
end | |
return lines | |
end | |
-- csv holds the list that lines_from gets from the specified file, almost certainly an easier way to do this. | |
csv = lines_from("O:\\Games\\Dwarf Fortress 0.47.05\\Dwarf Fortress 0.47.05\\hack\\scripts\\dorf_names.txt") | |
-- renames all dorfs that don't have a nickname yet from a list of names | |
function renameAllUnits(names) | |
for uid,unit in ipairs(df.global.world.units.all) do | |
--ensures that the unit is a dorf, a citizen, alive, and doesn't currently have a nickname. | |
if dfhack.units.isCitizen(unit) and (df.isnull(dfhack.units.getVisibleName(unit).nickname) or dfhack.units.getVisibleName(unit).nickname == "") then | |
-- iterates through list of names, setting the nickname of the dorf to whatever the current one in the list is. | |
for i , n in ipairs(names) do | |
print(n) | |
dfhack.units.setNickname(unit,n) | |
-- if nickname was successfully set, then name is removed from list. | |
if dfhack.units.getVisibleName(unit).nickname == n then | |
table.remove(names, i) | |
end | |
end | |
end | |
end | |
end | |
-- TODO: Randomize names somehow, allow csv, line separated text, or even just arguments? Allow the filenames to be specified as arguments. | |
renameAllUnits(csv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment