forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexterminate.lua
More file actions
231 lines (181 loc) · 6.45 KB
/
exterminate.lua
File metadata and controls
231 lines (181 loc) · 6.45 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
-- Exterminate creatures based on criteria
local argparse = require('argparse')
local function spawnLiquid(position, liquid_level, liquid_type, update_liquids)
local map_block = dfhack.maps.getTileBlock(position)
local tile = dfhack.maps.getTileFlags(position)
tile.flow_size = liquid_level
tile.liquid_type = liquid_type
tile.flow_forbid = false
map_block.flags.update_liquid = update_liquids
map_block.flags.update_liquid_twice = update_liquids
end
local function checkUnit(unit)
return (unit.body.blood_count ~= 0 or unit.body.blood_max == 0) and
not unit.flags1.inactive and
not unit.flags1.caged and
not unit.flags1.chained
end
local function isUnitFriendly(unit)
return dfhack.units.isCitizen(unit) or
dfhack.units.isOwnCiv(unit) or
dfhack.units.isOwnGroup(unit) or
dfhack.units.isVisiting(unit) or
dfhack.units.isTame(unit) or
dfhack.units.isDomesticated(unit) or
dfhack.units.isVisitor(unit) or
dfhack.units.isDiplomat(unit) or
dfhack.units.isMerchant(unit)
end
local killMethod = {
INSTANT = 0,
BUTCHER = 1,
MAGMA = 2,
DROWN = 3,
}
-- Kills a unit by removing blood and vanishing them.
local function killUnit(unit)
unit.body.blood_count = 0
unit.animal.vanish_countdown = 2
end
-- Marks a unit for slaughter at the butcher's shop.
local function butcherUnit(unit)
unit.flags2.slaughter = true
end
local function drownUnit(unit, liquid_type)
previousPositions = previousPositions or {}
previousPositions[unit.id] = copyall(unit.pos)
local function createLiquid()
spawnLiquid(unit.pos, 7, liquid_type)
if not same_xyz(previousPositions[unit.id], unit.pos) then
spawnLiquid(previousPositions[unit.id], 0, nil, false)
previousPositions[unit.id] = copyall(unit.pos)
end
if unit.flags2.killed then
spawnLiquid(previousPositions[unit.id], 0, nil, false)
else
dfhack.timeout(1, 'ticks', createLiquid)
end
end
createLiquid()
end
local function getRaceCastes(race_id)
local unit_castes = {}
for _, caste in pairs(df.creature_raw.find(race_id).caste) do
unit_castes[caste.caste_id] = {}
end
return unit_castes
end
local function getMapRaces(only_visible, include_friendly)
local map_races = {}
for _, unit in pairs(df.global.world.units.active) do
if only_visible and not dfhack.units.isVisible(unit) then
goto skipunit
end
if not include_friendly and isUnitFriendly(unit) then
goto skipunit
end
if dfhack.units.isActive(unit) and checkUnit(unit) then
local unit_race_name = dfhack.units.isUndead(unit) and "UNDEAD" or df.creature_raw.find(unit.race).creature_id
local race = ensure_key(map_races, unit_race_name)
race.id = unit.race
race.name = unit_race_name
race.count = (race.count or 0) + 1
end
:: skipunit ::
end
return map_races
end
local options, args = {
help = false,
method = killMethod.INSTANT,
only_visible = false,
include_friendly = false,
}, {...}
local positionals = argparse.processArgsGetopt(args, {
{'h', 'help', handler = function() options.help = true end},
{'m', 'method', handler = function(arg) options.method = killMethod[arg] end, hasArg = true},
{'o', 'only-visible', handler = function() options.only_visible = true end},
{'f', 'include-friendly', handler = function() options.include_friendly = true end},
})
if not dfhack.isMapLoaded() then
qerror('This script requires a fortress map to be loaded')
end
if positionals[1] == "help" or options.help then
print(dfhack.script_help())
end
if positionals[1] == "this" then
local selected_unit = dfhack.gui.getSelectedUnit()
if not selected_unit then
qerror("Select a unit and run the script again.")
end
killUnit(selected_unit)
return
end
if positionals[1] == nil then
local map_races = getMapRaces(options.only_visible, options.include_friendly)
local sorted_races = {}
for race, value in pairs(map_races) do
table.insert(sorted_races, { name = race, count = value.count })
end
table.sort(sorted_races, function(a, b)
return a.count > b.count
end)
for _, race in pairs(sorted_races) do
print(([[%4s %s]]):format(race.count, race.name))
end
return
end
local map_races = getMapRaces(options.only_visible, options.include_friendly)
if string.find(positionals[1], "UNDEAD") then
if map_races.UNDEAD then
for _, unit in pairs(df.global.world.units.active) do
if dfhack.units.isUndead(unit) and checkUnit(unit) then
killUnit(unit)
end
end
else
qerror("No undead found on the map.")
end
else
local selected_race, selected_caste = positionals[1], nil
if string.find(selected_race, ':') then
local tokens = positionals[1]:split(':')
selected_race, selected_caste = tokens[1], tokens[2]
end
if not map_races[selected_race] then
qerror("No creatures of this race on the map.")
end
local race_castes = getRaceCastes(map_races[selected_race].id)
if selected_caste and not race_castes[selected_caste] then
qerror("Invalid caste.")
end
local count = 0
for _, unit in pairs(df.global.world.units.active) do
if not dfhack.units.isActive(unit) or not checkUnit(unit) then
goto skipunit
end
if options.only_visible and not dfhack.units.isVisible(unit) then
goto skipunit
end
if not options.include_friendly and isUnitFriendly(unit) then
goto skipunit
end
if selected_caste and selected_caste ~= df.creature_raw.find(unit.race).caste[unit.caste].caste_id then
goto skipunit
end
if selected_race == df.creature_raw.find(unit.race).creature_id then
if options.method == killMethod.BUTCHER then
butcherUnit(unit)
elseif options.method == killMethod.MAGMA then
drownUnit(unit, df.tile_liquid.Magma)
elseif options.method == killMethod.DROWN then
drownUnit(unit, df.tile_liquid.Water)
else
killUnit(unit)
end
count = count + 1
end
:: skipunit ::
end
print(([[Exterminated %s creatures.]]):format(count))
end