forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectate.lua
More file actions
635 lines (546 loc) · 18.7 KB
/
spectate.lua
File metadata and controls
635 lines (546 loc) · 18.7 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
local _ENV = mkmodule('plugins.spectate')
local argparse = require('argparse')
local dlg = require('gui.dialogs')
local gui = require('gui')
local json = require('json')
local overlay = require('plugins.overlay')
local utils = require('utils')
local widgets = require('gui.widgets')
-- settings starting with 'tooltip-' are not passed to the C++ plugin
local lua_only_settings_prefix = 'tooltip-'
-- how many lines the text following unit is allowed to be moved down to avoid overlapping
local max_banner_y_offset = 4
local function get_default_state()
return {
['auto-unpause']=false,
['cinematic-action']=true,
['follow-seconds']=10,
['include-animals']=false,
['include-hostiles']=false,
['include-visitors']=false,
['include-wildlife']=false,
['prefer-conflict']=true,
['prefer-new-arrivals']=true,
['tooltip-follow']=true,
['tooltip-follow-blink-milliseconds']=3000,
['tooltip-follow-job']=true,
['tooltip-follow-job-shortenings'] = {
["Store item in stockpile"] = "Store item",
},
['tooltip-follow-name']=false,
['tooltip-follow-stress']=true,
['tooltip-follow-stress-levels']={
["0"] = true, -- Miserable
["1"] = true,
["2"] = false,
["3"] = false,
["4"] = false,
["5"] = true,
["6"] = true, -- Ecstatic
},
['tooltip-hover']=true,
['tooltip-hover-job']=true,
['tooltip-hover-name']=true,
['tooltip-hover-stress']=true,
['tooltip-hover-stress-levels']={
["0"] = true, -- Miserable
["1"] = true,
["2"] = false,
["3"] = false,
["4"] = false,
["5"] = true,
["6"] = true, -- Ecstatic
},
['tooltip-stress-levels']={
-- keep in mind, the text will look differently with game's font
-- colors are same as in ASCII mode, but for then middle (3), which is GREY instead of WHITE
["0"] = {text = "=C", pen = COLOR_RED, name = "Miserable"},
["1"] = {text = ":C", pen = COLOR_LIGHTRED, name = "Unhappy"},
["2"] = {text = ":(", pen = COLOR_YELLOW, name = "Displeased"},
["3"] = {text = ":]", pen = COLOR_GREY, name = "Content"},
["4"] = {text = ":)", pen = COLOR_GREEN, name = "Pleased"},
["5"] = {text = ":D", pen = COLOR_LIGHTGREEN, name = "Happy"},
["6"] = {text = "=D", pen = COLOR_LIGHTCYAN, name = "Ecstatic"},
}
}
end
local function load_state()
local state = get_default_state()
local config_file = json.open('dfhack-config/spectate.json')
for key in pairs(config_file.data) do
if state[key] == nil then
config_file.data[key] = nil
end
end
utils.assign(state, config_file.data)
config_file.data = state
return config_file.data,
function() config_file:write() end
end
local config, save_state = load_state()
-- called by gui/spectate
function get_config_elem(name, key)
local elem = config[name]
if elem == nil then return end
if type(elem) == 'table' then
return elem[key]
end
return elem
end
function refresh_cpp_config()
for name,value in pairs(config) do
if not name:startswith(lua_only_settings_prefix) then
if type(value) == 'boolean' then
value = value and 1 or 0
end
spectate_setSetting(name, value)
end
end
end
function show_squads_warning()
local message = {
'Cannot start spectate mode while the squads panel is open. Spectate',
'automatically disengages when you open the squads panel.',
'',
'Please close the squads panel before enabling spectate mode.',
}
dlg.showMessage("Spectate", table.concat(message, '\n'))
end
-----------------------------
-- commandline interface
local function pairsByKeys(t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
-- no recursion protection, but it shouldn't be needed for a config...
local function print_table(t, indent)
indent = indent or ''
for key, value in pairsByKeys(t) do
if type(value) == 'table' then
print(indent .. key .. ':')
print_table(value, indent .. ' ')
else
print(indent .. key .. ': ' .. tostring(value))
end
end
end
local function print_status()
print('spectate is:', isEnabled() and 'enabled' or 'disabled')
print()
print('settings:')
print_table(config, ' ')
end
local function do_toggle()
if isEnabled() then
dfhack.run_command('disable', 'spectate')
else
dfhack.run_command('enable', 'spectate')
end
end
local function set_setting(args)
local n = #args
if n == 0 then
qerror('missing key')
end
local key = table.remove(args, 1)
if config[key] == nil then
qerror('unknown setting: ' .. key)
end
n = #args
if n == 0
or (n == 1 and type(config[key]) == 'table')
then
qerror('missing value')
end
if n == 1 then
local value = args[1]
if key == 'follow-seconds' then
value = argparse.positiveInt(value, 'follow-seconds')
elseif key == 'tooltip-follow-blink-milliseconds' then
value = argparse.nonnegativeInt(value, 'tooltip-follow-blink-milliseconds')
else
value = argparse.boolean(value, key)
end
config[key] = value
if not key:startswith(lua_only_settings_prefix) then
if type(value) == 'boolean' then
value = value and 1 or 0
end
spectate_setSetting(key, value)
end
else
local errorUnknownSettingIfNil = function(t)
if t == nil then
table.remove(args)
qerror('unknown setting: ' .. key .. '/' .. table.concat(args, '/'))
end
end
local t = config[key]
for i = 1, n - 2 do
errorUnknownSettingIfNil(t)
t = t[args[i]]
end
local k = args[n-1]
local v = args[n]
if key ~= 'tooltip-follow-job-shortenings' then
-- user should be able to add new shortenings, but not other things
errorUnknownSettingIfNil(t[k])
if key:endswith('-stress-levels') and key ~= 'tooltip-stress-levels' then
v = argparse.boolean(v, key .. '/' .. k)
end
end
if type(t[k]) == 'table' then
qerror('missing value')
end
t[k] = v
end
save_state()
end
local function set_overlay(value)
value = argparse.boolean(value, name)
dfhack.run_command('overlay', value and 'enable' or 'disable', 'spectate.tooltip')
end
function parse_commandline(args)
local command = table.remove(args, 1)
if not command or command == 'status' then
print_status()
elseif command == 'toggle' then
do_toggle()
elseif command == 'set' then
set_setting(args)
elseif command == 'overlay' then
if #args == 0 then qerror('missing option') end
set_overlay(args[1])
else
return false
end
return true
end
-----------------------------
-- info functions
local function GetUnitStress(unit, stress_levels)
local stressCat = dfhack.units.getStressCategory(unit)
if stressCat > 6 then stressCat = 6 end
stressCat = tostring(stressCat)
if not stress_levels[stressCat] then return end
local level_cfg = config['tooltip-stress-levels'][stressCat]
return {text=level_cfg.text, pen=level_cfg.pen}
end
local function GetUnitName(unit)
return dfhack.units.getReadableName(unit)
end
local function GetUnitJob(unit)
local job = unit.job.current_job
return job and dfhack.job.getName(job)
end
local function GetRelevantSettings(key)
return config['tooltip-' .. key .. '-name'],
config['tooltip-' .. key .. '-job'],
config['tooltip-' .. key .. '-stress'],
config['tooltip-' .. key .. '-stress-levels'],
config['tooltip-' .. key .. '-job-shortenings']
end
local function GetUnitInfoText(unit, settings_group_name)
local show_name, show_job, show_stress, stress_levels, job_shortenings = GetRelevantSettings(settings_group_name)
local stress = show_stress and GetUnitStress(unit, stress_levels) or nil
local name = show_name and GetUnitName(unit) or nil
local job = show_job and GetUnitJob(unit) or nil
if job_shortenings then job = job_shortenings[job] or job end
local txt = {}
if stress then
txt[#txt+1] = stress
if name or job then txt[#txt+1] = ' ' end
end
if name then
txt[#txt+1] = name
end
if job then
if name then txt[#txt+1] = ": " end
txt[#txt+1] = job
end
return txt
end
local function GetHoverText(pos)
if not pos then return end
local txt = {}
local units = dfhack.units.getUnitsInBox(pos, pos) or {} -- todo: maybe (optionally) use filter parameter here?
for _,unit in ipairs(units) do
local info = GetUnitInfoText(unit, 'hover')
if not next(info) then goto continue end
for _,t in ipairs(info) do
txt[#txt+1] = t
end
txt[#txt+1] = NEWLINE
::continue::
end
return txt
end
-----------------------------
-- TooltipOverlay
TooltipOverlay = defclass(TooltipOverlay, overlay.OverlayWidget)
TooltipOverlay.ATTRS{
desc='Adds info tooltips that follow units or appear when you hover the mouse.',
default_pos={x=1,y=1},
fullscreen=true,
viewscreens='dwarfmode/Default',
}
function TooltipOverlay:init()
self:addviews{MouseTooltip{view_id = 'tooltip'}}
end
function TooltipOverlay:preUpdateLayout(parent_rect)
-- this is required, otherwise there is no room to draw child widgets in
self.frame.w = parent_rect.width
self.frame.h = parent_rect.height
end
function TooltipOverlay:render(dc)
self:render_unit_banners(dc)
TooltipOverlay.super.render(self, dc)
end
local function AnyFollowOptionOn()
return config['tooltip-follow-job']
or config['tooltip-follow-name']
or config['tooltip-follow-stress']
end
-- map coordinates -> interface layer coordinates
local function GetScreenCoordinates(map_coord)
-- -> map viewport offset
local vp = df.global.world.viewport
local vp_Coord = vp.corner
local map_offset_by_vp = {
x = map_coord.x - vp_Coord.x,
y = map_coord.y - vp_Coord.y,
z = map_coord.z - vp_Coord.z,
}
if not dfhack.screen.inGraphicsMode() then
return map_offset_by_vp
else
-- -> pixel offset
local gps = df.global.gps
local map_tile_pixels = gps.viewport_zoom_factor // 4;
local screen_coord_px = {
x = map_tile_pixels * map_offset_by_vp.x,
y = map_tile_pixels * map_offset_by_vp.y,
}
-- -> interface layer coordinates
local screen_coord_text = {
x = math.ceil( screen_coord_px.x / gps.tile_pixel_x ),
y = math.ceil( screen_coord_px.y / gps.tile_pixel_y ),
}
return screen_coord_text
end
end
local function GetString(tokens)
local sb = {}
for _, tok in ipairs(tokens) do
if type(tok) == "string" then
sb[#sb+1] = tok
else -- must be a table token
sb[#sb+1] = tok.text
end
end
if not next(sb) then return nil end
return table.concat(sb)
end
function TooltipOverlay:render_unit_banners(dc)
if not (config['tooltip-follow'] and AnyFollowOptionOn()) then return end
local blink_duration = config['tooltip-follow-blink-milliseconds']
if blink_duration > 0 and not gui.blink_visible(blink_duration) then
return
end
if not dfhack.screen.inGraphicsMode() and not gui.blink_visible(500) then
return
end
local vp = df.global.world.viewport
local topleft = vp.corner
local width = vp.max_x
local height = vp.max_y
local bottomright = {x = topleft.x + width, y = topleft.y + height, z = topleft.z}
local units = dfhack.units.getUnitsInBox(topleft, bottomright)
if not units or #units == 0 then return end
local oneTileOffset = GetScreenCoordinates({x = topleft.x + 1, y = topleft.y + 1, z = topleft.z + 0})
local pen = COLOR_WHITE
local used_tiles = {}
-- reverse order yields better offsets for overlapping texts
for i = #units, 1, -1 do
local unit = units[i]
local posX, posY, posZ = dfhack.units.getPosition(unit)
if not posX then goto continue end
local pos = xyz2pos(posX, posY, posZ)
local info = GetUnitInfoText(unit, 'follow')
if not info or not next(info) then goto continue end
local str = GetString(info)
if not str then goto continue end
local scrPos = GetScreenCoordinates(pos)
local y = scrPos.y - 1 -- subtract 1 to move the text over the heads
local x = scrPos.x + oneTileOffset.x - 1 -- subtract 1 to move the text inside the map tile
-- to resolve overlaps, we'll mark every coordinate we write anything in,
-- and then check if the new tooltip will overwrite any used coordinate.
-- if it will, try the next row, to a maximum offset of 4.
local row
local dy = 0
-- todo: search for the "best" offset instead, f.e. max `usedAt` value, with `-1` the best
local usedAt = -1
for yOffset = 0, max_banner_y_offset do
dy = yOffset
row = used_tiles[y + dy]
if not row then
row = {}
used_tiles[y + dy] = row
end
usedAt = -1
for j = 0, #str - 1 do
if row[x + j] then
usedAt = j
break
end
end
if usedAt == -1 then break end
end -- for dy
-- if other text starts at the same position, or even 2 to the right,
-- we can't place any useful information, and will ignore it instead.
if 0 <= usedAt and usedAt <= 2 then goto continue end
local writer = dc:seek(x, y + dy)
local ix = 0
for _, tok in ipairs(info) do
local s
if type(tok) == "string" then
writer = writer:pen(pen)
s = tok
else
writer = writer:pen(tok.pen)
s = tok.text
end
-- in case there isn't enough space, cut the text off
local len = #s
if usedAt > 0 and ix + len + 1 >= usedAt then
-- last position we can write is `usedAt - len - ix - 1`
-- we want to replace it with an `_`, so we need another `- 1`
s = s:sub(1, usedAt - len - ix - 1 - 1) .. '_'
writer = writer:string(s)
break -- nothing more will fit
else
writer = writer:string(s)
end
ix = ix + len
end
-- mark coordinates as used
for j = 0, #str - 1 do
row[x + j] = true
end
::continue::
end
end
-- MouseTooltip is an almost copy&paste of the DimensionsTooltip
MouseTooltip = defclass(MouseTooltip, widgets.ResizingPanel)
MouseTooltip.ATTRS{
frame_style=gui.FRAME_THIN,
frame_background=gui.CLEAR_PEN,
no_force_pause_badge=true,
auto_width=true,
display_offset={x=3, y=3},
}
function MouseTooltip:init()
ensure_key(self, 'frame').w = 17
self.frame.h = 4
self.label = widgets.Label{
frame={t=0},
auto_width=true,
}
self:addviews{
widgets.Panel{
-- set minimum size for tooltip frame so the DFHack frame badge fits
frame={t=0, l=0, w=7, h=2},
},
self.label,
}
end
local function AnyHoverOptionOn()
return config['tooltip-hover-job']
or config['tooltip-hover-name']
or config['tooltip-hover-stress']
end
function MouseTooltip:render(dc)
if not (config['tooltip-hover'] and AnyHoverOptionOn()) then return end
local x, y = dfhack.screen.getMousePos()
if not x then return end
local pos = dfhack.gui.getMousePos()
local text = GetHoverText(pos)
if not text or not next(text) then return end
self.label:setText(text)
local sw, sh = dfhack.screen.getWindowSize()
local frame_width = math.max(9, self.label:getTextWidth() + 2)
self.frame.l = math.min(x + self.display_offset.x, sw - frame_width)
self.frame.t = math.min(y + self.display_offset.y, sh - self.frame.h)
self:updateLayout()
MouseTooltip.super.render(self, dc)
end
-----------------------------
-- FollowPanelOverlay
local plotinfo = df.global.plotinfo
local mi = df.global.game.main_interface
local function follow_panel_is_visible()
return plotinfo.follow_unit > -1 and
mi.current_hover == -1 and
not mi.hover_instructions_on and
not mi.current_hover_alert
end
FollowPanelOverlay = defclass(FollowPanelOverlay, overlay.OverlayWidget)
FollowPanelOverlay.ATTRS{
desc='Adds spectate widgets to the vanilla follow panel.',
default_pos={x=6,y=-5},
viewscreens='dwarfmode/Default',
default_enabled=true,
frame={w=29, h=1},
visible=follow_panel_is_visible,
}
function FollowPanelOverlay:init()
self:addviews{
widgets.Label{
frame={l=0, t=0, w=3},
text=(' %s '):format(string.char(27)),
on_click=spectate_followPrev,
},
widgets.Label{
frame={l=5, t=0, w=3},
text=(' %s '):format(string.char(26)),
on_click=spectate_followNext,
},
widgets.Label{
frame={l=10, t=0, w=25},
text={
' spectate:',
{text=function() return isEnabled() and ' on ' or 'off ' end,
pen=function() return isEnabled() and COLOR_GREEN or COLOR_LIGHTRED end},
},
on_click=function() dfhack.run_command(isEnabled() and 'disable' or 'enable', 'spectate') end,
},
widgets.ConfigureButton{
frame={l=26, t=0},
on_click=function() dfhack.run_script('gui/spectate') end,
}
}
end
function FollowPanelOverlay:onInput(keys)
if keys.KEYBOARD_CURSOR_LEFT then
spectate_followPrev()
return true
elseif keys.KEYBOARD_CURSOR_RIGHT then
spectate_followNext()
return true
end
return FollowPanelOverlay.super.onInput(self, keys)
end
OVERLAY_WIDGETS = {
followpanel=FollowPanelOverlay,
tooltip=TooltipOverlay,
}
return _ENV