Skip to content

Commit 046b99a

Browse files
authored
Merge pull request #1496 from chdoc/job-helpers
adapt Lua tools to use new API functionality for creating and assigning jobs
2 parents accdadf + 8ff279e commit 046b99a

File tree

5 files changed

+38
-118
lines changed

5 files changed

+38
-118
lines changed

autocheese.lua

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
--@module = true
22

3-
local ic = reqscript('idle-crafting')
4-
53
---make cheese using a specific barrel and workshop
64
---@param barrel df.item
75
---@param workshop df.building_workshopst
86
---@return df.job
97
function makeCheese(barrel, workshop)
108
---@type df.job
11-
local job = ic.make_job()
9+
local job = dfhack.job.createLinked()
1210
job.job_type = df.job_type.MakeCheese
1311

1412
local jitem = df.job_item:new()
@@ -22,29 +20,17 @@ function makeCheese(barrel, workshop)
2220
dfhack.error('could not attach item')
2321
end
2422

25-
ic.assignToWorkshop(job, workshop)
23+
dfhack.job.assignToWorkshop(job, workshop)
2624
return job
2725
end
2826

29-
30-
31-
---unit is ready to take jobs
27+
---checks that unit can path to workshop
3228
---@param unit df.unit
29+
---@param workshop df.building_workshopst
3330
---@return boolean
34-
function unitIsAvailable(unit)
35-
if unit.job.current_job then
36-
return false
37-
elseif #unit.individual_drills > 0 then
38-
return false
39-
elseif unit.flags1.caged or unit.flags1.chained then
40-
return false
41-
elseif unit.military.squad_id ~= -1 then
42-
local squad = df.squad.find(unit.military.squad_id)
43-
-- this lookup should never fail
44-
---@diagnostic disable-next-line: need-check-nil
45-
return #squad.orders == 0 and squad.activity == -1
46-
end
47-
return true
31+
function canAccessWorkshop(unit, workshop)
32+
local workshop_position = xyz2pos(workshop.centerx, workshop.centery, workshop.z)
33+
return dfhack.maps.canWalkBetween(unit.pos, workshop_position)
4834
end
4935

5036
---check if unit can perform labor at workshop
@@ -54,8 +40,8 @@ end
5440
---@return boolean
5541
function availableLaborer(unit, unit_labor, workshop)
5642
return unit.status.labors[unit_labor]
57-
and unitIsAvailable(unit)
58-
and ic.canAccessWorkshop(unit, workshop)
43+
and dfhack.units.isJobAvailable(unit)
44+
and canAccessWorkshop(unit, workshop)
5945
end
6046

6147
---find unit with a particular labor enabled

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Template for new versions:
4343

4444
## Misc Improvements
4545

46+
- adapt Lua tools to use new API functionality for creating and assigning jobs
47+
- `idle-crafting`: properly interrupt interruptible (i.e. "green") social activities
48+
4649
## Removed
4750

4851
# 52.03-r2

husbandry.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
local utils = require 'utils'
66
local repeatutil = require("repeat-util")
7-
local ic = reqscript('idle-crafting')
87

9-
local verbose = true
8+
local verbose = false
109
---conditional printing of debug messages
1110
---@param message string
1211
local function debug(message)
@@ -120,17 +119,17 @@ local function getAppropriateWorkshop(unit, collection)
120119
end
121120

122121
local function shearCreature(unit, workshop)
123-
local job = ic.make_job()
122+
local job = dfhack.job.createLinked()
124123
job.job_type = df.job_type.ShearCreature
125124
dfhack.job.addGeneralRef(job, df.general_ref_type.UNIT_SHEAREE, unit.id)
126-
ic.assignToWorkshop(job, workshop)
125+
dfhack.job.assignToWorkshop(job, workshop)
127126
end
128127

129128
local function milkCreature(unit, workshop)
130-
local job = ic.make_job()
129+
local job = dfhack.job.createLinked()
131130
job.job_type = df.job_type.MilkCreature
132131
dfhack.job.addGeneralRef(job, df.general_ref_type.UNIT_MILKEE, unit.id)
133-
ic.assignToWorkshop(job, workshop)
132+
dfhack.job.assignToWorkshop(job, workshop)
134133
end
135134

136135

idle-crafting.lua

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,12 @@ function weightedChoice(choices)
5555
return nil --never reached on well-formed input
5656
end
5757

58-
---create a new linked job
59-
---@return df.job
60-
function make_job()
61-
local job = df.job:new()
62-
dfhack.job.linkIntoWorld(job, true)
63-
return job
64-
end
65-
66-
function assignToWorkshop(job, workshop)
67-
job.pos = xyz2pos(workshop.centerx, workshop.centery, workshop.z)
68-
dfhack.job.addGeneralRef(job, df.general_ref_type.BUILDING_HOLDER, workshop.id)
69-
workshop.jobs:insert("#", job)
70-
end
71-
7258
---make totem at specified workshop
7359
---@param unit df.unit
7460
---@param workshop df.building_workshopst
7561
---@return boolean
7662
function makeTotem(unit, workshop)
77-
local job = make_job()
63+
local job = dfhack.job.createLinked()
7864
job.job_type = df.job_type.MakeTotem
7965
job.mat_type = -1
8066

@@ -89,7 +75,7 @@ function makeTotem(unit, workshop)
8975
jitem.flags2.body_part = true
9076
job.job_items.elements:insert('#', jitem)
9177

92-
assignToWorkshop(job, workshop)
78+
dfhack.job.assignToWorkshop(job, workshop)
9379
return dfhack.job.addWorker(job, unit)
9480
end
9581

@@ -98,7 +84,7 @@ end
9884
---@param workshop df.building_workshopst
9985
---@return boolean
10086
function makeHornCrafts(unit, workshop)
101-
local job = make_job()
87+
local job = dfhack.job.createLinked()
10288
job.job_type = df.job_type.MakeCrafts
10389
job.mat_type = -1
10490
job.material_category.horn = true
@@ -114,7 +100,7 @@ function makeHornCrafts(unit, workshop)
114100
jitem.flags2.body_part = true
115101
job.job_items.elements:insert('#', jitem)
116102

117-
assignToWorkshop(job, workshop)
103+
dfhack.job.assignToWorkshop(job, workshop)
118104
return dfhack.job.addWorker(job, unit)
119105
end
120106

@@ -123,7 +109,7 @@ end
123109
---@param workshop df.building_workshopst
124110
---@return boolean
125111
function makeBoneCraft(unit, workshop)
126-
local job = make_job()
112+
local job = dfhack.job.createLinked()
127113
job.job_type = df.job_type.MakeCrafts
128114
job.mat_type = -1
129115
job.material_category.bone = true
@@ -139,7 +125,7 @@ function makeBoneCraft(unit, workshop)
139125
jitem.flags2.body_part = true
140126
job.job_items.elements:insert('#', jitem)
141127

142-
assignToWorkshop(job, workshop)
128+
dfhack.job.assignToWorkshop(job, workshop)
143129
return dfhack.job.addWorker(job, unit)
144130
end
145131

@@ -148,7 +134,7 @@ end
148134
---@param workshop df.building_workshopst
149135
---@return boolean
150136
function makeShellCraft(unit, workshop)
151-
local job = make_job()
137+
local job = dfhack.job.createLinked()
152138
job.job_type = df.job_type.MakeCrafts
153139
job.mat_type = -1
154140
job.material_category.shell = true
@@ -164,7 +150,7 @@ function makeShellCraft(unit, workshop)
164150
jitem.flags2.body_part = true
165151
job.job_items.elements:insert('#', jitem)
166152

167-
assignToWorkshop(job, workshop)
153+
dfhack.job.assignToWorkshop(job, workshop)
168154
return dfhack.job.addWorker(job, unit)
169155
end
170156

@@ -173,7 +159,7 @@ end
173159
---@param workshop df.building_workshopst
174160
---@return boolean ""
175161
function makeRockCraft(unit, workshop)
176-
local job = make_job()
162+
local job = dfhack.job.createLinked()
177163
job.job_type = df.job_type.MakeCrafts
178164
job.mat_type = 0
179165

@@ -187,7 +173,7 @@ function makeRockCraft(unit, workshop)
187173
jitem.flags3.hard = true
188174
job.job_items.elements:insert('#', jitem)
189175

190-
assignToWorkshop(job, workshop)
176+
dfhack.job.assignToWorkshop(job, workshop)
191177
return dfhack.job.addWorker(job, unit)
192178
end
193179

@@ -291,13 +277,8 @@ local STONE_CRAFT = df.unit_labor['STONE_CRAFT']
291277
---@param value_if_absent T
292278
---@return number|T
293279
function getCraftingNeed(unit, value_if_absent)
294-
local needs = unit.status.current_soul.personality.needs
295-
for _, need in ipairs(needs) do
296-
if need.id == CraftObject then
297-
return -need.focus_level
298-
end
299-
end
300-
return value_if_absent
280+
local focus_penalty = dfhack.units.getFocusPenalty(unit, CraftObject)
281+
return focus_penalty > 1000 and value_if_absent or -focus_penalty
301282
end
302283

303284
local function stop()
@@ -334,27 +315,6 @@ function canAccessWorkshop(unit, workshop)
334315
return dfhack.maps.canWalkBetween(unit.pos, workshop_position)
335316
end
336317

337-
---unit is ready to take jobs
338-
---@param unit df.unit
339-
---@return boolean
340-
function unitIsAvailable(unit)
341-
if unit.job.current_job then
342-
return false
343-
elseif #unit.specific_refs > 0 then -- activities such as "Conduct Meeting"
344-
return false
345-
elseif #unit.social_activities > 0 then
346-
return false
347-
elseif #unit.individual_drills > 0 then
348-
return false
349-
elseif unit.military.squad_id ~= -1 then
350-
local squad = df.squad.find(unit.military.squad_id)
351-
-- this lookup should never fail
352-
---@diagnostic disable-next-line: need-check-nil
353-
return #squad.orders == 0 and squad.activity == -1
354-
end
355-
return true
356-
end
357-
358318
---select crafting job based on available resources
359319
---@param workshop df.building_workshopst
360320
---@return (fun(unit:df.unit, workshop:df.building_workshopst):boolean)?
@@ -397,7 +357,7 @@ local function processUnit(workshop, idx, unit_id)
397357
elseif not canAccessWorkshop(unit, workshop) then
398358
-- dfhack.print('-')
399359
return false
400-
elseif not unitIsAvailable(unit) then
360+
elseif not dfhack.units.isJobAvailable(unit) then
401361
-- dfhack.print('.')
402362
return false
403363
end

immortal-cravings.lua

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
--@enable = true
22
--@module = true
33

4-
local idle = reqscript('idle-crafting')
54
local repeatutil = require("repeat-util")
65

76
--- utility functions
@@ -101,7 +100,7 @@ local function goDrink(unit)
101100
-- print('no accessible drink found')
102101
return
103102
end
104-
local job = idle.make_job()
103+
local job = dfhack.job.createLinked()
105104
job.job_type = df.job_type.DrinkItem
106105
job.flags.special = true
107106
local dx, dy, dz = dfhack.items.getPosition(drink)
@@ -134,7 +133,7 @@ local function goEat(unit)
134133
end
135134
dfhack.items.setOwner(meal, unit)
136135

137-
local job = idle.make_job()
136+
local job = dfhack.job.createLinked()
138137
job.job_type = df.job_type.Eat
139138
job.flags.special = true
140139
local dx, dy, dz = dfhack.items.getPosition(meal)
@@ -148,25 +147,6 @@ local function goEat(unit)
148147
print(dfhack.df2console('immortal-cravings: %s is getting something to eat'):format(name))
149148
end
150149

151-
---unit is ready to take jobs (will interrupt social activities)
152-
---@param unit df.unit
153-
---@return boolean
154-
function unitIsAvailable(unit)
155-
if unit.job.current_job then
156-
return false
157-
elseif #unit.individual_drills > 0 then
158-
return false
159-
elseif unit.flags1.caged or unit.flags1.chained then
160-
return false
161-
elseif unit.military.squad_id ~= -1 then
162-
local squad = df.squad.find(unit.military.squad_id)
163-
-- this lookup should never fail
164-
---@diagnostic disable-next-line: need-check-nil
165-
return #squad.orders == 0 and squad.activity == -1
166-
end
167-
return true
168-
end
169-
170150
--- script logic
171151

172152
local GLOBAL_KEY = 'immortal-cravings'
@@ -210,7 +190,7 @@ local function unit_loop()
210190
then
211191
goto next_unit
212192
end
213-
if not unitIsAvailable(unit) then
193+
if not dfhack.units.isJobAvailable(unit) then
214194
debug("immortal-cravings: skipping busy"..dfhack.units.getReadableName(unit))
215195
table.insert(kept, unit.id)
216196
else
@@ -245,21 +225,13 @@ local function main_loop()
245225
watched = {}
246226
for _, unit in ipairs(dfhack.units.getCitizens(false, false)) do
247227
if
248-
not (is_active_caste_flag(unit, 'NO_DRINK') or is_active_caste_flag(unit, 'NO_EAT')) or
249-
unit.counters2.stomach_content > 0
228+
(is_active_caste_flag(unit, 'NO_DRINK') or is_active_caste_flag(unit, 'NO_EAT')) and
229+
unit.counters2.stomach_content == 0 and
230+
dfhack.units.getFocusPenalty(unit, DrinkAlcohol, EatGoodMeal) < threshold
250231
then
251-
goto next_unit
252-
end
253-
for _, need in ipairs(unit.status.current_soul.personality.needs) do
254-
if need.id == DrinkAlcohol and need.focus_level < threshold or
255-
need.id == EatGoodMeal and need.focus_level < threshold
256-
then
257-
table.insert(watched, unit.id)
258-
debug(' '..dfhack.df2console(dfhack.units.getReadableName(unit)))
259-
goto next_unit
260-
end
232+
table.insert(watched, unit.id)
233+
debug(' ' .. dfhack.df2console(dfhack.units.getReadableName(unit)))
261234
end
262-
::next_unit::
263235
end
264236

265237
if #watched > 0 then

0 commit comments

Comments
 (0)