|
99 | 99 | --###### |
100 | 100 | --Functions |
101 | 101 | --###### |
| 102 | +local function isIgnoredNoble(unit) |
| 103 | + local noblePos = dfhack.units.getNoblePositions(unit) |
| 104 | + if noblePos ~= nil then |
| 105 | + for _, position in ipairs(noblePos) do |
| 106 | + if state.ignored_nobles[position.position.code] then |
| 107 | + return true |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | + return false |
| 112 | +end |
| 113 | + |
| 114 | +---@return table<integer, { ['unit']: df.unit, ['need']: integer }> |
102 | 115 | function getTrainingCandidates() |
103 | 116 | local ret = {} |
104 | 117 | ignore_count = 0 |
105 | 118 | for _, unit in ipairs(dfhack.units.getCitizens(true)) do |
106 | | - if state.ignored[unit.id] then |
107 | | - ignore_count = ignore_count +1 |
108 | | - goto next_unit |
109 | | - end |
110 | 119 | if not dfhack.units.isAdult(unit) then |
111 | 120 | goto next_unit |
112 | 121 | end |
113 | 122 | local need = getTrainingNeed(unit) |
114 | 123 | if not need or need.focus_level >= state.threshold then |
115 | 124 | goto next_unit |
116 | 125 | end |
117 | | - local noblePos = dfhack.units.getNoblePositions(unit) |
118 | | - local isIgnNoble = false |
119 | | - if noblePos ~=nil then |
120 | | - for _, position in ipairs(noblePos) do |
121 | | - if state.ignored_nobles[position.position.code] then |
122 | | - isIgnNoble = true |
123 | | - break |
124 | | - end |
125 | | - end |
| 126 | + -- ignored units are those that would like to train but are forbidden from doing so |
| 127 | + if state.ignored[unit.id] then |
| 128 | + ignore_count = ignore_count + 1 |
| 129 | + goto next_unit |
126 | 130 | end |
127 | | - if isIgnNoble then |
128 | | - ignore_count = ignore_count +1 |
| 131 | + if isIgnoredNoble(unit) then |
| 132 | + ignore_count = ignore_count + 1 |
129 | 133 | goto next_unit |
130 | 134 | end |
131 | | - table.insert(ret, unit) |
| 135 | + table.insert(ret, { unit = unit, need = need.focus_level }) |
132 | 136 | ::next_unit:: |
133 | 137 | end |
| 138 | + table.sort(ret, function (a, b) return a.need < b.need end) |
134 | 139 | return ret |
135 | 140 | end |
136 | 141 |
|
@@ -229,52 +234,59 @@ function check() |
229 | 234 | local unit = df.unit.find(hf.unit_id) |
230 | 235 | local training_need = getTrainingNeed(unit) |
231 | 236 | if not training_need or training_need.focus_level >= state.threshold then |
232 | | - dfhack.military.removeFromSquad(unit) |
| 237 | + dfhack.military.removeFromSquad(unit.id) |
233 | 238 | end |
234 | 239 | end |
235 | 240 | end |
236 | 241 | end |
237 | 242 | end |
238 | | - for _, unit in ipairs(getTrainingCandidates()) do |
239 | | - local added = addTraining(unit, squads) |
| 243 | + for _, p in ipairs(getTrainingCandidates()) do |
| 244 | + local added = addTraining(p.unit, squads) |
240 | 245 | if added then |
241 | 246 | intraining_count = intraining_count +1 |
242 | 247 | else |
243 | 248 | inque_count = inque_count +1 |
244 | 249 | end |
245 | 250 | end |
246 | | - |
247 | | - dfhack.println(GLOBAL_KEY .. " | IGNORED: " .. ignore_count .. " TRAINING: " .. intraining_count .. " QUEUE: " ..inque_count ) |
| 251 | + print(("%s: %d training, %d waiting, and %d excluded units with training needs"): |
| 252 | + format(GLOBAL_KEY, intraining_count, inque_count, ignore_count)) |
248 | 253 | end |
249 | 254 |
|
250 | 255 | function start() |
251 | | - if args.t then |
252 | | - state.threshold = 0-tonumber(args.t) |
253 | | - end |
254 | 256 | repeatUtil.scheduleEvery(GLOBAL_KEY, 1, 'days', check) |
255 | 257 | end |
256 | 258 |
|
257 | 259 | function stop() |
258 | 260 | repeatUtil.cancel(GLOBAL_KEY) |
259 | 261 | end |
260 | 262 |
|
261 | | -if dfhack_flags.enable then |
262 | | - if dfhack_flags.enable_state then |
263 | | - state.enabled = true |
264 | | - else |
265 | | - state.enabled = false |
266 | | - end |
| 263 | +function enable() |
| 264 | + state.enabled = true |
| 265 | + persist_state() |
| 266 | + start() |
| 267 | +end |
| 268 | + |
| 269 | +function disable() |
| 270 | + state.enabled = false |
267 | 271 | persist_state() |
| 272 | + stop() |
| 273 | + removeAll() |
268 | 274 | end |
269 | 275 |
|
270 | 276 | if dfhack_flags.module then |
271 | 277 | return |
272 | 278 | end |
273 | 279 |
|
274 | | -if state.enabled then |
275 | | - start() |
| 280 | +if dfhack_flags.enable then |
| 281 | + if dfhack_flags.enable_state then |
| 282 | + enable() |
| 283 | + else |
| 284 | + disable() |
| 285 | + end |
276 | 286 | else |
277 | | - stop() |
278 | | - removeAll() |
| 287 | + -- called on the command-line |
| 288 | + if args.t then |
| 289 | + state.threshold = 0-tonumber(args.t) |
| 290 | + end |
| 291 | + print(("autotraining is %s"):format(state.enabled and "enabled" or "disabled")) |
279 | 292 | end |
280 | | -persist_state() |
|
0 commit comments