Merge branch 'fixster-cleanup' into develop

develop
lethosor 2015-06-23 11:26:22 -04:00
commit 605a2ede64
1 changed files with 37 additions and 46 deletions

@ -1,6 +1,8 @@
-- makes creatures [in]fertile, by modifying orientation -- makes creatures [in]fertile, by modifying orientation
-- usage: fix-ster [fert|ster] [all|animals|only:<creature>] -- usage: fix-ster [fert|ster] [all|animals|only:<creature>]
-- by Tacomagic (minor fixes by PeridexisErrant) -- original author: Tacomagic
-- minor fixes by PeridexisErrant, Lethosor
--@ module = true
--[[ --[[
This script utilizes the orientation tag to either fix infertile creatures This script utilizes the orientation tag to either fix infertile creatures
@ -16,29 +18,20 @@ Optional args:
only:<creature>: processes all of the creatures matching the specified creature on the map only:<creature>: processes all of the creatures matching the specified creature on the map
]]-- ]]--
function getunit()
local unit
unit=dfhack.gui.getSelectedUnit()
if unit==nil then
print("No unit under cursor.")
return
end
return unit
end
function changeorient(unit, ori) function changeorient(unit, ori)
--Sets the fertility flag based on gender. --Sets the fertility flag based on gender.
if not unit.status.current_soul then
return
end
if unit.sex == 0 then if unit.sex == 0 then
unit.status.current_soul.orientation_flags.marry_male=ori unit.status.current_soul.orientation_flags.marry_male=ori
else else
unit.status.current_soul.orientation_flags.marry_female=ori unit.status.current_soul.orientation_flags.marry_female=ori
end end
return
end end
function changelots(creatures,ori) function changelots(creatures, ori, silent)
local v local v, unit
local unit
local c = 0 local c = 0
--loops through indexes in creatures table and changes orientation flags --loops through indexes in creatures table and changes orientation flags
for _, v in ipairs(creatures) do for _, v in ipairs(creatures) do
@ -46,38 +39,39 @@ function changelots(creatures,ori)
changeorient(unit,ori) changeorient(unit,ori)
c = c + 1 c = c + 1
end end
if not silent then
print("Changed " .. c .. " creatures.") print("Changed " .. c .. " creatures.")
return end
end end
function process_args(arg) function process_args(args)
local n,v local n, v, ori, crename, crenum
local ori
local creatures = {} local creatures = {}
local crenum
--Checks for any arguments at all. --Checks for any arguments at all.
if arg==nil then if args == nil or #args == 0 then
print("No arguments. Usage is: fixster <fert|ster> [all|animals|only:<creature>]") print("No arguments. Usage is: fixster <fert|ster> [all|animals|only:<creature>]")
return return
end end
if string.lower(arg[1])=="ster" then for i, a in pairs(args) do
args[i] = tostring(a):lower()
end
if args[1]:sub(1, 1) == "s" then -- sterile
ori = false ori = false
elseif string.lower(arg[1])=="fert" then elseif args[1]:sub(1, 1) == "f" then -- fertile
ori = true ori = true
else else
print("Unrecognised first argument. Aborting.") qerror("Unrecognised first argument: " .. args[1] .. ". Aborting.")
return
end end
--Checks for the existence of the second argument. If it's missing, uses selected unit (if any) --Checks for the existence of the second argument. If it's missing, uses selected unit (if any)
if arg[2]==nil then if args[2] == nil then
unit = getunit() unit = dfhack.gui.getSelectedUnit()
if unit == nil then return end if not unit then return end
changeorient(unit, ori) changeorient(unit, ori)
print('Changed selected creature.') print('Changed selected creature.')
--ALL arg processing --ALL arg processing
elseif string.lower(arg[2])=="all" then elseif args[2] == "all" then
--Create table of all current unit indexes --Create table of all current unit indexes
for n,v in ipairs(df.global.world.units.active) do for n,v in ipairs(df.global.world.units.active) do
table.insert(creatures,n) table.insert(creatures,n)
@ -85,7 +79,7 @@ function process_args(arg)
changelots(creatures,ori) changelots(creatures,ori)
--ANIMALS arg processing --ANIMALS arg processing
elseif string.lower(arg[2])=="animals" then elseif args[2] == "animals" then
--Create a table of all creature indexes except dwarves on the current map --Create a table of all creature indexes except dwarves on the current map
for n,v in ipairs(df.global.world.units.active) do for n,v in ipairs(df.global.world.units.active) do
if v.race ~= df.global.ui.race_id then if v.race ~= df.global.ui.race_id then
@ -95,21 +89,18 @@ function process_args(arg)
changelots(creatures,ori) changelots(creatures,ori)
-- ONLY:<creature> arg processing -- ONLY:<creature> arg processing
elseif string.lower(string.sub(arg[2],1,4))=="only" then elseif args[2]:sub(1,4) == "only" then
creidx = string.upper(string.sub(arg[2],6)) crename = args[2]:sub(6):upper()
print(string.upper(string.sub(arg[2],6)))
--Search raws for creature --Search raws for creature
for k,v in pairs(df.global.world.raws.creatures.all) do for k,v in pairs(df.global.world.raws.creatures.all) do
if v.creature_id==creidx then if v.creature_id == crename then
crenum = k crenum = k
end end
end end
--If no match, abort --If no match, abort
if crenum == nil then if crenum == nil then
print("Creature not found. Check spelling.") qerror("Creature not found. Check spelling.")
return
end end
--create a table of all the matching creature indexes on the map for processing --create a table of all the matching creature indexes on the map for processing
@ -120,11 +111,11 @@ function process_args(arg)
end end
changelots(creatures,ori) changelots(creatures,ori)
else else
print("Unrecognised optional argument. Aborting") qerror("Unrecognised optional argument. Aborting")
return
end end
return
end end
local arg = table.pack(...) if not moduleMode then
process_args(arg) local args = table.pack(...)
process_args(args)
end