Skip to content

Commit 05a35ed

Browse files
authored
spectate.lua: display unit activity if no job
1 parent 487df2c commit 05a35ed

1 file changed

Lines changed: 34 additions & 18 deletions

File tree

plugins/lua/spectate.lua

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ local function get_default_state()
2929
['tooltip-follow-blink-milliseconds']=3000,
3030
['tooltip-follow-hold-to-show']='none', -- one of none, ctrl, alt, or shift
3131
['tooltip-follow-job']=true,
32+
['tooltip-follow-activity']=true,
3233
['tooltip-follow-job-shortenings'] = {
3334
["Store item in stockpile"] = "Store item",
3435
},
@@ -45,6 +46,7 @@ local function get_default_state()
4546
},
4647
['tooltip-hover']=true,
4748
['tooltip-hover-job']=true,
49+
['tooltip-hover-activity']=true,
4850
['tooltip-hover-name']=true,
4951
['tooltip-hover-stress']=true,
5052
['tooltip-hover-stress-levels']={
@@ -285,33 +287,59 @@ local function GetUnitJob(unit)
285287
return job and dfhack.job.getName(job)
286288
end
287289

290+
-- there is no equivalent of `dfhack.job.GetName` yet
291+
-- this function is by no means a replacement, it's
292+
-- here to show something at least
293+
local activityNames = {
294+
[df.activity_entry_type.TrainingSession] = "Training Session",
295+
[df.activity_entry_type.IndividualSkillDrill] = "Individual Skill Drill",
296+
[df.activity_entry_type.FillServiceOrder] = "Fill Service Order",
297+
[df.activity_entry_type.StoreObject] = "Store Object",
298+
-- other types are single words
299+
}
300+
local function activity_GetName(activity)
301+
local t = activity.type
302+
local n = df.activity_entry_type[t]
303+
n = activityNames[n] or n
304+
return {text = n, pen = COLOR_LIGHTGREEN}
305+
end
306+
307+
local function GetUnitActivity(unit)
308+
local activity = dfhack.units.getMainSocialActivity(unit)
309+
return activity and activity_GetName(activity)
310+
end
311+
288312
local function GetRelevantSettings(key)
289313
return config['tooltip-' .. key .. '-name'],
290314
config['tooltip-' .. key .. '-job'],
315+
config['tooltip-' .. key .. '-activity'],
291316
config['tooltip-' .. key .. '-stress'],
292317
config['tooltip-' .. key .. '-stress-levels'],
293318
config['tooltip-' .. key .. '-job-shortenings']
294319
end
295320

296321
local function GetUnitInfoText(unit, settings_group_name)
297-
local show_name, show_job, show_stress, stress_levels, job_shortenings = GetRelevantSettings(settings_group_name)
322+
local show_name, show_job, show_activity, show_stress, stress_levels, job_shortenings = GetRelevantSettings(settings_group_name)
298323

299324
local stress = show_stress and GetUnitStress(unit, stress_levels) or nil
300325
local name = show_name and GetUnitName(unit) or nil
301326
local job = show_job and GetUnitJob(unit) or nil
302327
if job_shortenings then job = job_shortenings[job] or job end
328+
local activity = show_activity and GetUnitActivity(unit) or nil
329+
330+
local job_or_activity = job or activity
303331

304332
local txt = {}
305333
if stress then
306334
txt[#txt+1] = stress
307-
if name or job then txt[#txt+1] = ' ' end
335+
if name or job_or_activity then txt[#txt+1] = ' ' end
308336
end
309337
if name then
310338
txt[#txt+1] = name
311339
end
312-
if job then
340+
if job_or_activity then
313341
if name then txt[#txt+1] = ": " end
314-
txt[#txt+1] = job
342+
txt[#txt+1] = job_or_activity
315343
end
316344

317345
return txt
@@ -364,12 +392,6 @@ function TooltipOverlay:render(dc)
364392
TooltipOverlay.super.render(self, dc)
365393
end
366394

367-
local function AnyFollowOptionOn()
368-
return config['tooltip-follow-job']
369-
or config['tooltip-follow-name']
370-
or config['tooltip-follow-stress']
371-
end
372-
373395
-- map coordinates -> interface layer coordinates
374396
local function GetScreenCoordinates(map_coord)
375397
-- -> map viewport offset
@@ -415,7 +437,7 @@ local function GetString(tokens)
415437
end
416438

417439
function TooltipOverlay:render_unit_banners(dc)
418-
if not (config['tooltip-follow'] and AnyFollowOptionOn()) then return end
440+
if not config['tooltip-follow'] then return end
419441

420442
local hold_to_show = config['tooltip-follow-hold-to-show']
421443
if hold_to_show and hold_to_show ~= 'none' then
@@ -570,14 +592,8 @@ function MouseTooltip:init()
570592
}
571593
end
572594

573-
local function AnyHoverOptionOn()
574-
return config['tooltip-hover-job']
575-
or config['tooltip-hover-name']
576-
or config['tooltip-hover-stress']
577-
end
578-
579595
function MouseTooltip:render(dc)
580-
if not (config['tooltip-hover'] and AnyHoverOptionOn()) then return end
596+
if not config['tooltip-hover'] then return end
581597

582598
local x, y = dfhack.screen.getMousePos()
583599
if not x then return end

0 commit comments

Comments
 (0)