|
| 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 |
0 commit comments