Skip to content

Commit 8dc131d

Browse files
authored
Merge pull request #1639 from Alistair-Afton/devel-jobwatch
devel/jobwatch: report unit job transitions per tick
2 parents 3c611de + 4e00a3c commit 8dc131d

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎devel/jobwatch.lua‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
-- Watches unit job assignments and reports transitions in the console
2+
--@ enable = true
3+
--[====[
4+
5+
devel/jobwatch
6+
==============
7+
Watches ``unit.job.current_job`` for all units in ``world.units.active``
8+
and prints a line whenever a unit gains, loses, or switches jobs —
9+
including switches between two jobs of the same type, which is how
10+
claim races (a unit losing its target posting to another unit) appear.
11+
12+
Useful for observing job churn that is otherwise invisible: the
13+
assignment auction runs inside the tick and can only be seen through
14+
its effects on ``current_job``.
15+
16+
:enable|start: Begin watching
17+
:disable|stop: Stop watching
18+
:unit <id>|all: Only report transitions for one unit id (or all units)
19+
:verbose: Also print each job's posting index and flags
20+
]====]
21+
22+
active = active or false
23+
filter_unit = filter_unit or nil --as:number
24+
verbose = verbose or false
25+
last_jobs = last_jobs or {} --as:{[number]:{id:number,desc:string}}
26+
27+
local function describe(unit)
28+
local job = unit.job.current_job
29+
if not job then return -1, 'none' end
30+
local s = df.job_type[job.job_type] or ('?type ' .. job.job_type)
31+
if verbose then
32+
s = ('%s [id=%d posting=%d do_now=%s special=%s]'):format(
33+
s, job.id, job.posting_index,
34+
tostring(job.flags.do_now), tostring(job.flags.special))
35+
end
36+
return job.id, s
37+
end
38+
39+
local function check()
40+
local seen = {}
41+
for _, unit in ipairs(df.global.world.units.active) do
42+
local id, desc = describe(unit)
43+
seen[unit.id] = {id=id, desc=desc}
44+
local prev = last_jobs[unit.id]
45+
if prev and prev.id ~= id
46+
and (not filter_unit or unit.id == filter_unit) then
47+
print(('%d unit %d %s: %s -> %s'):format(
48+
df.global.cur_year_tick, unit.id,
49+
dfhack.df2console(dfhack.units.getReadableName(unit)),
50+
prev.desc, desc))
51+
end
52+
end
53+
last_jobs = seen
54+
if active then
55+
dfhack.timeout(1, 'ticks', check)
56+
end
57+
end
58+
59+
local args = {...}
60+
if dfhack_flags and dfhack_flags.enable then
61+
table.insert(args, dfhack_flags.enable_state and 'enable' or 'disable')
62+
end
63+
64+
local cmd = args[1]
65+
if cmd == 'enable' or cmd == 'start' then
66+
if not active then
67+
active = true
68+
dfhack.timeout(1, 'ticks', check)
69+
end
70+
elseif cmd == 'disable' or cmd == 'stop' then
71+
active = false
72+
elseif cmd == 'unit' then
73+
filter_unit = (args[2] == 'all') and nil or tonumber(args[2])
74+
print('jobwatch: unit filter = ' .. tostring(filter_unit or 'all'))
75+
elseif cmd == 'verbose' then
76+
verbose = not verbose
77+
print('jobwatch: verbose = ' .. tostring(verbose))
78+
else
79+
print(dfhack.script_help())
80+
end

‎docs/devel/jobwatch.rst‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
devel/jobwatch
2+
==============
3+
4+
.. dfhack-tool::
5+
:summary: Report unit job transitions as they happen.
6+
:tags: dev
7+
8+
Aids script development and reverse engineering by observing job churn that is
9+
otherwise invisible: the game's assignment pass runs inside the tick, so only
10+
its effects on ``unit.job.current_job`` can be seen from Lua. This tool polls
11+
all active units once per tick and prints a line whenever a unit gains, loses,
12+
or switches jobs -- including switches between two jobs of the same type, which
13+
is how claim races appear.
14+
15+
Usage
16+
-----
17+
18+
::
19+
20+
enable devel/jobwatch
21+
devel/jobwatch unit <id>|all
22+
devel/jobwatch verbose
23+
disable devel/jobwatch
24+
25+
``unit`` restricts output to a single unit id. ``verbose`` additionally prints
26+
each job's id, posting index, and ``do_now``/``special`` flags.

0 commit comments

Comments
 (0)