forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathholy-war.lua
More file actions
200 lines (177 loc) · 6.36 KB
/
holy-war.lua
File metadata and controls
200 lines (177 loc) · 6.36 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
-- Trigger wars based on conflicts between deity spheres
--@ module = true
local utils = require('utils')
local help = [====[
holy-war
========
Provoke wars with civilizations that do not share deity spheres with
your civilization or the temples in your fortress. Religious
persecution grudges in the historical record also trigger war.
Usage:
holy-war [--dry-run]
If ``--dry-run`` is specified, no diplomatic changes are made and a list of
potential targets is printed instead.
]====]
local valid_args = utils.invert({ 'dry-run', 'help' })
local RELIGIOUS_PERSECUTION_GRUDGE =
df.vague_relationship_type.RELIGIOUS_PERSECUTION_GRUDGE or
df.vague_relationship_type.religious_persecution_grudge
local function merge(dst, src)
for k in pairs(src) do dst[k] = true end
end
local function spheres_to_str(spheres)
local names = {}
for sph in pairs(spheres) do
table.insert(names, df.sphere_type[sph])
end
table.sort(names)
return table.concat(names, ', ')
end
local function diff(a, b)
local d = {}
for k in pairs(a) do
if not b[k] then d[k] = true end
end
return d
end
local function get_deity_spheres(hfid)
local spheres = {}
local hf = df.historical_figure.find(hfid)
if hf and hf.info and hf.info.metaphysical then
for _, sph in ipairs(hf.info.metaphysical.spheres) do
spheres[sph] = true
end
end
return spheres
end
local function get_civ_spheres(civ)
local spheres = {}
for _, deity_id in ipairs(civ.relations.deities) do
merge(spheres, get_deity_spheres(deity_id))
end
return spheres
end
local function get_fort_spheres()
local spheres = {}
for _, bld in ipairs(df.global.world.buildings.all) do
if bld:getType() == df.building_type.Temple then
local dtype = bld.deity_type
if dtype == df.religious_practice_type.WORSHIP_HFID then
merge(spheres, get_deity_spheres(bld.deity_data.HFID))
elseif dtype == df.religious_practice_type.RELIGION_ENID then
local rciv = df.historical_entity.find(bld.deity_data.Religion)
if rciv then merge(spheres, get_civ_spheres(rciv)) end
end
end
end
return spheres
end
local function union(a, b)
local u = {}
merge(u, a)
merge(u, b)
return u
end
local function share(p1, p2)
for k in pairs(p1) do
if p2[k] then return true end
end
return false
end
local function get_civ_hists(civ)
local hfs = {}
for _, id in ipairs(civ.histfig_ids) do hfs[id] = true end
return hfs
end
local function has_religious_grudge(p_hfs, t_hfs)
if not RELIGIOUS_PERSECUTION_GRUDGE then return false end
for _, set in ipairs(df.global.world.history.relationship_events) do
for i = 0, set.next_element-1 do
if set.relationship[i] == RELIGIOUS_PERSECUTION_GRUDGE then
local src = set.source_hf[i]
local tgt = set.target_hf[i]
if (p_hfs[src] and t_hfs[tgt]) or (p_hfs[tgt] and t_hfs[src]) then
return true
end
end
end
end
return false
end
local function change_relation(target, relation)
local pciv = df.historical_entity.find(df.global.plotinfo.civ_id)
for _, state in ipairs(pciv.relations.diplomacy.state) do
if state.group_id == target.id then
state.relation = relation
end
end
for _, state in ipairs(target.relations.diplomacy.state) do
if state.group_id == pciv.id then
state.relation = relation
end
end
end
local function main(...)
local args = utils.processArgs({...}, valid_args)
if args.help then
print(help)
return
end
local dry_run = args['dry-run']
local pciv = df.historical_entity.find(df.global.plotinfo.civ_id)
local player_spheres = union(get_civ_spheres(pciv), get_fort_spheres())
local player_hfs = get_civ_hists(pciv)
for _, civ in ipairs(df.global.world.entities.all) do
if civ.type == 0 and civ.id ~= pciv.id then
local p_status
for _, state in ipairs(pciv.relations.diplomacy.state) do
if state.group_id == civ.id then
p_status = state.relation
break
end
end
local c_status
for _, state in ipairs(civ.relations.diplomacy.state) do
if state.group_id == pciv.id then
c_status = state.relation
break
end
end
if p_status ~= 1 or c_status ~= 1 then -- not already mutually at war
local civ_spheres = get_civ_spheres(civ)
local civ_hfs = get_civ_hists(civ)
local divine_conflict = next(player_spheres) and next(civ_spheres)
and not share(player_spheres, civ_spheres)
local persecution = has_religious_grudge(player_hfs, civ_hfs)
if (divine_conflict or persecution) and civ.name.has_name then
local name = dfhack.translation.translateName(civ.name, true)
local reason_parts = {}
if divine_conflict then
local p_diff = diff(player_spheres, civ_spheres)
local c_diff = diff(civ_spheres, player_spheres)
table.insert(reason_parts,
('conflicting spheres (%s vs %s)'):format(
spheres_to_str(p_diff),
spheres_to_str(c_diff)))
end
if persecution then
table.insert(reason_parts, 'religious persecution')
end
local reason = table.concat(reason_parts, ' and ')
if dry_run then
print(('Would declare war on %s due to %s.'):format(name, reason))
else
change_relation(civ, 1) -- war
dfhack.gui.showAnnouncement(
('%s sparks war with %s!'):format(
reason:gsub('^.', string.upper), name),
COLOR_RED, true)
end
end
end
end
end
end
if not dfhack_flags.module then
main(...)
end