forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreserve-rooms.lua
More file actions
384 lines (342 loc) · 12.4 KB
/
preserve-rooms.lua
File metadata and controls
384 lines (342 loc) · 12.4 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
local _ENV = mkmodule('plugins.preserve-rooms')
local argparse = require('argparse')
local gui = require('gui')
local overlay = require('plugins.overlay')
local utils = require('utils')
local widgets = require('gui.widgets')
local GLOBAL_KEY = 'preserve-rooms'
DEBUG = DEBUG or false
------------------
-- public API
--
-- updated on world load
code_lookup = code_lookup or {}
function assignToRole(code, bld)
local group_codes = code_lookup[code:lower()]
if not group_codes then
dfhack.printerr('Noble or administrator role not found: ' .. code)
end
preserve_rooms_assignToRole(group_codes, bld and bld.id or -1)
end
------------------
-- command line
--
local function print_status()
local features, stats = preserve_rooms_getState()
print('Features:')
for feature,enabled in pairs(features) do
print((' %20s: %s'):format(feature, enabled))
end
print()
print('Rooms reserved for traveling units:', stats.reservations)
print('Managing assignments for noble rooms:', stats.nobles)
if DEBUG then
for k,v in pairs(preserve_rooms_getDebugState()) do
local elems = v:split(',')
if elems[1] == '' then
table.remove(elems, 1)
end
print(('%s (%d):'):format(k, #elems))
for _,elem in ipairs(elems) do
print(' ' .. elem)
end
print()
end
end
end
local function do_set_feature(enabled, feature)
if not preserve_rooms_setFeature(enabled, feature) then
qerror(('unknown feature: "%s"'):format(feature))
end
end
local function do_reset_feature(feature)
if not preserve_rooms_resetFeatureState(feature) then
qerror(('unknown feature: "%s"'):format(feature))
end
end
function parse_commandline(args)
local opts = {}
local positionals = argparse.processArgsGetopt(args, {
{'h', 'help', handler=function() opts.help = true end},
})
if opts.help or not positionals or positionals[1] == 'help' then
return false
end
local command = table.remove(positionals, 1)
if not command or command == 'status' then
print_status()
elseif command == 'now' then
preserve_rooms_cycle()
elseif command == 'enable' or command == 'disable' then
do_set_feature(command == 'enable', positionals[1])
elseif command == 'reset' then
do_reset_feature(positionals[1])
else
return false
end
return true
end
----------------------
-- ReservedWidget
--
ReservedWidget = defclass(ReservedWidget, overlay.OverlayWidget)
ReservedWidget.ATTRS{
desc='Shows whether a zone has been reserved for a unit or role.',
default_enabled=true,
default_pos={x=37, y=6},
viewscreens={
'dwarfmode/Zone/Some/Bedroom',
'dwarfmode/Zone/Some/DiningHall',
'dwarfmode/Zone/Some/Office',
'dwarfmode/Zone/Some/Tomb',
},
frame={w=44, h=15},
version=2,
}
new_world_loaded = true
local CONFLICTING_TOOLTIPS = utils.invert{
df.main_hover_instruction.ZoneRepaint,
df.main_hover_instruction.ZoneSuspend,
df.main_hover_instruction.ZoneRemove,
df.main_hover_instruction.ZoneAssignLocation,
}
function ReservedWidget:init()
self.code_to_idx = {}
self:addviews{
widgets.Panel{
view_id='pause_mask',
frame={t=3, l=0, w=4, h=3},
},
widgets.Panel{
view_id='add_mask',
frame={t=6, l=4, w=4, h=3},
},
widgets.Panel{
frame={t=0, l=9},
visible=function()
local scr = dfhack.gui.getDFViewscreen(true)
return not dfhack.gui.matchFocusString('dwarfmode/UnitSelector', scr) and
not dfhack.gui.matchFocusString('dwarfmode/LocationSelector', scr) and
not dfhack.gui.matchFocusString('dwarfmode/LocationDetails', scr)
end,
subviews={
widgets.Panel{
visible=function()
return not preserve_rooms_isReserved() and preserve_rooms_getFeature('track-roles')
end,
subviews={
widgets.CycleHotkeyLabel{
view_id='role',
frame={t=1, l=1, r=1, h=2},
frame_background=gui.CLEAR_PEN,
label='Autoassign to holder of role:',
label_below=true,
key='CUSTOM_SHIFT_S',
options={{label='None', value={''}, pen=COLOR_YELLOW}},
on_change=function(group_codes)
self.subviews.list:setSelected(self.code_to_idx[group_codes[1]] or 1)
preserve_rooms_assignToRole(group_codes, -1)
end,
},
widgets.Panel{
view_id='hover_trigger',
frame={t=0, l=0, r=0, h=4},
frame_style=gui.FRAME_MEDIUM,
visible=true,
},
widgets.Panel{
view_id='hover_expansion',
frame_style=gui.FRAME_MEDIUM,
visible=false,
subviews={
widgets.Panel{
frame={t=2},
frame_background=gui.CLEAR_PEN,
},
widgets.List{
view_id='list',
frame={t=3, l=1},
frame_background=gui.CLEAR_PEN,
on_submit=function(idx)
self.subviews.role:setOption(idx, true)
end,
choices={'None'},
},
},
},
},
},
widgets.Panel{
frame={t=0, h=5},
frame_style=gui.FRAME_MEDIUM,
frame_background=gui.CLEAR_PEN,
visible=preserve_rooms_isReserved,
subviews={
widgets.Label{
frame={t=0, l=0},
text={
'Reserved for traveling unit:', NEWLINE,
{gap=1, text=preserve_rooms_getReservationName, pen=COLOR_YELLOW},
},
},
widgets.HotkeyLabel{
frame={t=2, l=0},
key='CUSTOM_SHIFT_R',
label='Clear reservation',
on_activate=preserve_rooms_clearReservation,
},
},
},
widgets.HelpButton{
command='preserve-rooms',
visible=function()
return preserve_rooms_isReserved() or preserve_rooms_getFeature('track-roles')
end,
},
},
},
}
end
local mi = df.global.game.main_interface
function ReservedWidget:onInput(keys)
if keys._MOUSE_L and
(preserve_rooms_isReserved() or preserve_rooms_getRoleAssignment() ~= '')
then
if self.subviews.pause_mask:getMousePos() then return true end
if self.subviews.add_mask:getMousePos() then return true end
end
if CONFLICTING_TOOLTIPS[mi.current_hover] then
return false
end
if mi.entering_building_name and keys._STRING then
return false
end
return ReservedWidget.super.onInput(self, keys)
end
function ReservedWidget:render(dc)
if new_world_loaded then
self:refresh_role_list()
new_world_loaded = false
end
local code = preserve_rooms_getRoleAssignment()
local role = self.subviews.role
if code ~= role:getOptionValue()[1] then
local idx = self.code_to_idx[code] or 1
role:setOption(idx)
self.subviews.list:setSelected(idx)
end
local hover_expansion = self.subviews.hover_expansion
if hover_expansion.visible and not hover_expansion:getMouseFramePos() then
hover_expansion.visible = false
elseif self.subviews.hover_trigger:getMousePos() then
hover_expansion.visible = true
end
if not CONFLICTING_TOOLTIPS[mi.current_hover] then
ReservedWidget.super.render(self, dc)
end
end
local function to_title_case(str)
return dfhack.capitalizeStringWords(dfhack.lowerCp437(str:gsub('_', ' ')))
end
local function add_options(options, choices, codes)
for _,group_codes in ipairs(codes) do
local names = {}
for _,code in ipairs(group_codes) do
table.insert(names, to_title_case(code))
end
local name = table.concat(names, '/')
table.insert(options, {label=name, value=group_codes, pen=COLOR_YELLOW})
table.insert(choices, name)
end
end
-- updated on world load
codes = codes or {}
function ReservedWidget:refresh_role_list()
local options, choices = {{label='None', value={''}, pen=COLOR_YELLOW}}, {'None'}
add_options(options, choices, codes)
self.code_to_idx = {['']=1}
for idx,group_codes in ipairs(codes) do
self.code_to_idx[group_codes[1]] = idx + 1 -- offset for None option
end
self.subviews.role.options = options
self.subviews.role:setOption(1)
self.subviews.list:setChoices(choices, 1)
end
OVERLAY_WIDGETS = {
reserved=ReservedWidget,
}
local function add_positions(positions, entity)
if not entity then return end
for _,position in ipairs(entity.positions.own) do
positions[position.id] = {
id=position.id,
code=position.code,
replaced_by=position.replaced_by,
required_value=position.required_office + position.required_bedroom + position.required_dining + position.required_tomb,
}
end
end
local function get_codes(positions)
-- group by promotion chain
-- grouped[id] is the group of positions that can be (eventually) promoted to the id'd position
local grouped = {}
for id,position in pairs(positions) do
if position.replaced_by == -1 then
ensure_key(grouped, id)[id] = position
else
local parent = positions[position.replaced_by]
while parent.replaced_by ~= -1 do
parent = positions[parent.replaced_by]
end
ensure_key(grouped, parent.id)[id] = position
end
end
-- add reverse links for promotion chains and record whether it has a room requirement
for _,group in pairs(grouped) do
for _,position in pairs(group) do
if position.replaced_by ~= -1 then
group[position.replaced_by].prev = position.id
end
end
end
-- produce combined codes
local diplay_codes = {}
for id,group in pairs(grouped) do
local group_codes = {}
local position = group[id]
while position do
table.insert(group_codes, 1, position.code)
group_codes.required_value = math.max(group_codes.required_value or 0, position.required_value)
position=group[position.prev]
end
table.insert(diplay_codes, group_codes)
end
table.sort(diplay_codes, function(a, b)
if a.required_value == b.required_value then
return a[1] < b[1]
end
return a.required_value > b.required_value
end)
return diplay_codes
end
local function get_api_lookup_table(codes)
local lookup = {}
for _,group_codes in ipairs(codes) do
for _,code in ipairs(group_codes) do
lookup[code:lower()] = group_codes
end
end
return lookup
end
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc ~= SC_MAP_LOADED or not dfhack.world.isFortressMode() then
return
end
local positions = {}
add_positions(positions, df.historical_entity.find(df.global.plotinfo.civ_id));
add_positions(positions, df.historical_entity.find(df.global.plotinfo.group_id));
codes = get_codes(positions)
code_lookup = get_api_lookup_table(codes)
new_world_loaded = true
end
return _ENV