forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 4
/
combat-harden.lua
131 lines (106 loc) · 3.5 KB
/
combat-harden.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
123
124
125
126
127
128
129
130
131
-- Sets a unit's combat-hardened value to a given percent
--@ module = true
local help = [====[
combat-harden
=============
Sets the combat-hardened value on a unit, making them care more/less about seeing corpses.
Requires a value and a target.
Valid values:
:``-value <0-100>``:
A percent value to set combat hardened to.
:``-tier <1-4>``:
Choose a tier of hardenedness to set it to.
1 = No hardendness.
2 = "is getting used to tragedy"
3 = "is a hardened individual"
4 = "doesn't really care about anything anymore" (max)
If neither are provided, the script defaults to using a value of 100.
Valid targets:
:``-all``:
All active units will be affected.
:``-citizens``:
All (sane) citizens of your fort will be affected. Will do nothing in adventure mode.
:``-unit <UNIT ID>``:
The given unit will be affected.
If no target is given, the provided unit can't be found, or no unit id is given with the unit
argument, the script will try and default to targeting the currently selected unit.
]====]
local utils = require 'utils'
local validArgs = utils.invert({
'help',
'all',
'citizens',
'tier',
'unit',
'value',
})
local tiers = {0, 33, 67, 100}
function setUnitCombatHardened(unit, value)
if unit.status.current_soul ~= nil then
-- Ensure value is in the bounds of 0-100
local value = math.max(0, math.min(100, value))
unit.status.current_soul.personality.combat_hardened = value
end
end
function main(...)
local args = utils.processArgs({...}, validArgs)
if args.help then
print(help)
return
end
local value
if not args.tier and not args.value then
-- Default to 100
value = 100
elseif args.tier then
-- Bound between 1-4
local tierNum = math.max(1, math.min(4, tonumber(args.tier)))
value = tiers[tierNum]
elseif args.value then
-- Function ensures value is bound, so no need to bother here
-- Will check it's a number, though
value = tonumber(args.value) or 100
end
local unitsList = {} --as:df.unit[]
if not args.all and not args.citizens then
-- Assume trying to target a unit
local unit
if args.unit then
if tonumber(args.unit) then
unit = df.unit.find(args.unit)
end
end
-- If unit ID wasn't provided / unit couldn't be found,
-- Try getting selected unit
if unit == nil then
unit = dfhack.gui.getSelectedUnit(true)
end
if unit == nil then
qerror("Couldn't find unit. If you don't want to target a specific unit, use -all or -citizens.")
else
table.insert(unitsList, unit)
end
elseif args.all then
for _, unit in pairs(df.global.world.units.active) do
table.insert(unitsList, unit)
end
elseif args.citizens then
-- Technically this will exclude insane citizens, but this is the
-- easiest thing that dfhack provides
-- Abort if not in Fort mode
if not dfhack.world.isFortressMode() then
qerror('-citizens requires fortress mode')
end
for _, unit in pairs(df.global.world.units.active) do
if dfhack.units.isCitizen(unit) then
table.insert(unitsList, unit)
end
end
end
for index, unit in ipairs(unitsList) do
setUnitCombatHardened(unit, value)
end
end
if not dfhack_flags.module then
main(...)
end