-
Notifications
You must be signed in to change notification settings - Fork 194
/
list-agreements.lua
203 lines (179 loc) · 6.41 KB
/
list-agreements.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
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
--@module = true
-- list location agreements with guilds or religions
function get_location_name(loctier,loctype)
local locstr = "Unknown Location"
if loctype == df.abstract_building_type.TEMPLE and loctier == 1 then
locstr = "Temple"
elseif loctype == df.abstract_building_type.TEMPLE and loctier == 2 then
locstr = "Temple Complex"
elseif loctype == df.abstract_building_type.GUILDHALL and loctier == 1 then
locstr = "Guildhall"
elseif loctype == df.abstract_building_type.GUILDHALL and loctier == 2 then
locstr = "Grand Guildhall"
end
return locstr
end
function get_location_type(agr)
loctype = agr.details[0].data.Location.type
return loctype
end
function get_petition_date(agr)
local agr_year = agr.details[0].year
local agr_year_tick = agr.details[0].year_tick
local julian_day = math.floor(agr_year_tick / 1200) + 1
local agr_month = math.floor(julian_day / 28) + 1
local agr_day = julian_day % 28
return string.format("%03d-%02d-%02d",agr_year, agr_month, agr_day)
end
function get_petition_age(agr)
local agr_year_tick = agr.details[0].year_tick
local agr_year = agr.details[0].year
local cur_year_tick = df.global.cur_year_tick
local cur_year = df.global.cur_year
local del_year, del_year_tick
--delta, check to prevent off by 1 error, not validated
if cur_year_tick > agr_year_tick then
del_year = cur_year - agr_year
del_year_tick = cur_year_tick - agr_year_tick
else
del_year = cur_year - agr_year - 1
del_year_tick = agr_year_tick - cur_year_tick
end
local julian_day = math.floor(del_year_tick / 1200) + 1
local del_month = math.floor(julian_day / 28)
local del_day = julian_day % 28
return {del_year,del_month,del_day}
end
function get_guildhall_profession(agr)
local prof = agr.details[0].data.Location.profession
local profname = string.lower(df.profession[prof])
-- *VERY* important code follows
if string.find(profname, "man") then
profname = string.gsub(profname,"man",string.lower(dfhack.units.getRaceNameById(df.global.plotinfo.race_id)))
end
if not profname then
profname = 'profession-less'
end
return profname:gsub("_", " ")
end
function get_agr_party_name(agr)
--assume party 0 is guild/order, 1 is local government as siteid = playerfortid
local party_id = agr.parties[0].entity_ids[0]
local party_name = dfhack.TranslateName(df.global.world.entities.all[party_id].name, true)
if not party_name then
party_name = 'An Unknown Entity or Group'
end
return party_name
end
function get_deity_name(agr)
local religion_id = agr.details[0].data.Location.deity_data.Religion
local deities = df.global.world.entities.all[religion_id].relations.deities
if #deities == 0 then return 'An Unknown Deity' end
return dfhack.TranslateName(df.global.world.history.figures[deities[0]].name,true)
end
--get resolution status, and string
function is_resolved(agr)
local res = false
local res_str = 'outstanding'
if agr.flags.convicted_accepted then
res = true
res_str = 'satisfied'
elseif agr.flags.petition_not_accepted then
res = true
res_str = 'denied'
elseif get_petition_age(agr)[1] ~= 0 then
res = true
res_str = 'expired'
end
return res,res_str
end
--universal handler
function generate_output(agr,loctype)
local loc_name = get_location_name(agr.details[0].data.Location.tier,loctype)
local agr_age = get_petition_age(agr)
local output_str = 'Establish a '.. loc_name..' for "'..get_agr_party_name(agr)
local resolved, resolution_string = is_resolved(agr)
if loctype == df.abstract_building_type.TEMPLE then
output_str = output_str..'" worshiping "'..get_deity_name(agr)..',"'
elseif loctype == df.abstract_building_type.GUILDHALL then
output_str = output_str..'", a '..get_guildhall_profession(agr)..' guild,'
else
print("Agreement with unknown org")
return
end
output_str = output_str..'\n\tas agreed on '..get_petition_date(agr)..'. \t'..agr_age[1]..'y, '..agr_age[2]..'m, '..agr_age[3]..'d ago'
-- can print '(outstanding)' status here, but imho none is cleaner
if not resolved then
print(output_str)
else
print(output_str..' ('..resolution_string..')')
end
end
function get_fort_agreements(cull_resolved)
local t_agr, g_agr = {}, {}
local playerfortid = df.global.plotinfo.site_id -- Player fortress id
for _, agr in pairs(df.agreement.get_vector()) do
if agr.details[0].data.Location.site == playerfortid then
if not is_resolved(agr) or not cull_resolved then
if get_location_type(agr) == df.abstract_building_type.TEMPLE then
table.insert(t_agr, agr)
elseif get_location_type(agr) == df.abstract_building_type.GUILDHALL then
table.insert(g_agr, agr)
end
end
end
end
return t_agr, g_agr
end
if dfhack_flags.module then
return
end
---------------------------------------------------------------------------
-- Main Script operation
---------------------------------------------------------------------------
local args = {...}
local cmd = args[1]
local cull_resolved = true
-- args handler
if cmd then
if cmd == "all" then
cull_resolved = false
elseif cmd == "help" then
print(dfhack.script_help())
return
else
print('argument ``'..cmd..'`` not supported')
print(dfhack.script_help())
return
end
end
local templeagreements, guildhallagreements = get_fort_agreements(cull_resolved)
print "-----------------------"
print "Agreements for Temples:"
print "-----------------------"
if next(templeagreements) == nil then
if cull_resolved then
print "No outstanding agreements"
else
print "No agreements"
end
else
for _, agr in pairs(templeagreements) do
generate_output(agr,df.abstract_building_type.TEMPLE)
end
end
print ""
print "--------------------------"
print "Agreements for Guildhalls:"
print "--------------------------"
if next(guildhallagreements) == nil then
if cull_resolved then
print "No outstanding agreements"
else
print "No agreements"
end
else
for _, agr in pairs(guildhallagreements) do
generate_output(agr,df.abstract_building_type.GUILDHALL)
end
end