@@ -47,6 +47,17 @@ function unitIsAvailable(unit)
4747 return true
4848end
4949
50+ --- check if unit can perform labor at workshop
51+ --- @param unit df.unit
52+ --- @param unit_labor df.unit_labor
53+ --- @param workshop df.building
54+ --- @return boolean
55+ function availableLaborer (unit , unit_labor , workshop )
56+ return unit .status .labors [unit_labor ]
57+ and unitIsAvailable (unit )
58+ and ic .canAccessWorkshop (unit , workshop )
59+ end
60+
5061--- find unit with a particular labor enabled
5162--- @param unit_labor df.unit_labor
5263--- @param job_skill df.job_skill
5869 local max_skill = - 1
5970 for _ , unit in ipairs (dfhack .units .getCitizens (true , false )) do
6071 if
61- unit .status .labors [unit_labor ] and
62- unitIsAvailable (unit ) and
63- ic .canAccessWorkshop (unit , workshop )
72+ availableLaborer (unit , unit_labor , workshop )
6473 then
6574 local unit_skill = dfhack .units .getNominalSkill (unit , job_skill , true )
6675 if unit_skill > max_skill then
@@ -90,13 +99,32 @@ local function findMilkBarrel(min_liquids)
9099 end
91100end
92101
93- function findWorkshop ()
102+ --- find a workshop to which the barrel can be brought
103+ --- if the workshop has a master, only return workshop and master if the master is available
104+ --- @param pos df.coord
105+ --- @return df.building_workshopst ?
106+ --- @return df.unit ?
107+ function findWorkshop (pos )
94108 for _ ,workshop in ipairs (df .global .world .buildings .other .WORKSHOP_FARMER ) do
95109 if
110+ dfhack .maps .canWalkBetween (pos , xyz2pos (workshop .centerx , workshop .centery , workshop .z )) and
96111 not workshop .profile .blocked_labors [df .unit_labor .MAKE_CHEESE ] and
97- # workshop .jobs == 0 and # workshop . profile . permitted_workers == 0
112+ # workshop .jobs == 0
98113 then
99- return workshop
114+ if # workshop .profile .permitted_workers == 0 then
115+ -- immediately return workshop without master
116+ return workshop , nil
117+ else
118+ unit = df .unit .find (workshop .profile .permitted_workers [0 ])
119+ if
120+ unit and availableLaborer (unit , df .unit_labor .MAKE_CHEESE , workshop )
121+ then
122+ -- return workshop and master, if master is available
123+ return workshop , unit
124+ else
125+ print (" autocheese: Skipping farmer's workshop with unavailable master" )
126+ end
127+ end
100128 end
101129 end
102130end
@@ -127,14 +155,18 @@ if not reagent then
127155 return
128156end
129157
130- local workshop = findWorkshop ()
158+ local workshop , worker = findWorkshop (xyz2pos ( dfhack . items . getPosition ( reagent )) )
131159
132160if not workshop then
133- print (' autocheese: no Farmer' s Workshop available ' )
161+ print (" autocheese: no Farmer's Workshop available" )
134162 return
135163end
136164
137- local worker , skill = findAvailableLaborer (df .unit_labor .MAKE_CHEESE , df .job_skill .CHEESEMAKING , workshop )
165+ -- try to find laborer for workshop without master
166+ if not worker then
167+ worker , _ = findAvailableLaborer (df .unit_labor .MAKE_CHEESE , df .job_skill .CHEESEMAKING , workshop )
168+ end
169+
138170if not worker then
139171 print (' autocheese: no cheesemaker available' )
140172 return
0 commit comments