forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-ster.lua
122 lines (108 loc) · 3.79 KB
/
fix-ster.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
-- makes creatures [in]fertile, by modifying orientation
-- usage: fix-ster [fert|ster] [all|animals|only:<creature>]
-- original author: Tacomagic
-- minor fixes by PeridexisErrant, Lethosor
--@ module = true
--[====[
fix-ster
========
Utilizes the orientation tag to either fix infertile creatures or inflict
infertility on creatures that you do not want to breed. Usage::
fix-ster [fert|ster] [all|animals|only:<creature>]
``fert`` or ``ster`` is a required argument; whether to make the target fertile
or sterile. Optional arguments specify the target: no argument for the
selected unit, ``all`` for all units on the map, ``animals`` for all non-dwarf
creatures, or ``only:<creature>`` to only process matching creatures.
]====]
function changeorient(unit, ori)
--Sets the fertility flag based on gender.
if not unit.status.current_soul then
return
end
if unit.sex == 0 then
unit.status.current_soul.orientation_flags.marry_male=ori
else
unit.status.current_soul.orientation_flags.marry_female=ori
end
end
function changelots(creatures, ori, silent)
local v, unit
local c = 0
--loops through indexes in creatures table and changes orientation flags
for _, v in ipairs(creatures) do
unit = df.global.world.units.active[v]
changeorient(unit,ori)
c = c + 1
end
if not silent then
print("Changed " .. c .. " creatures.")
end
end
function process_args(args)
local n, v, ori, crename, crenum
local creatures = {} --as:number[]
--Checks for any arguments at all.
if args == nil or #args == 0 then
print("No arguments. Usage is: fixster <fert|ster> [all|animals|only:<creature>]")
return
end
for i, a in pairs(args) do
args[i] = tostring(a):lower()
end
if args[1]:sub(1, 1) == "s" then -- sterile
ori = false
elseif args[1]:sub(1, 1) == "f" then -- fertile
ori = true
else
qerror("Unrecognised first argument: " .. args[1] .. ". Aborting.")
end
--Checks for the existence of the second argument. If it's missing, uses selected unit (if any)
if args[2] == nil then
local unit = dfhack.gui.getSelectedUnit()
if not unit then return end
changeorient(unit, ori)
print('Changed selected creature.')
--ALL arg processing
elseif args[2] == "all" then
--Create table of all current unit indexes
for n,v in ipairs(df.global.world.units.active) do
table.insert(creatures,n)
end
changelots(creatures,ori)
--ANIMALS arg processing
elseif args[2] == "animals" then
--Create a table of all creature indexes except dwarves on the current map
for n,v in ipairs(df.global.world.units.active) do
if v.race ~= df.global.plotinfo.race_id then
table.insert(creatures,n)
end
end
changelots(creatures,ori)
-- ONLY:<creature> arg processing
elseif args[2]:sub(1,4) == "only" then
crename = args[2]:sub(6):upper()
--Search raws for creature
for k,v in pairs(df.global.world.raws.creatures.all) do
if v.creature_id == crename then
crenum = k
end
end
--If no match, abort
if crenum == nil then
qerror("Creature not found. Check spelling.")
end
--create a table of all the matching creature indexes on the map for processing
for n,v in ipairs(df.global.world.units.active) do
if v.race == crenum then
table.insert(creatures,n)
end
end
changelots(creatures,ori)
else
qerror("Unrecognised optional argument. Aborting")
end
end
if not moduleMode then
local args = table.pack(...)
process_args(args)
end