11-- Task units to store their owned items.
22
3+ local argparse = require (" argparse" )
34local utils = require (' utils' )
45
56local function GetUnit (item , options )
160161
161162local function GetPosFromContainer (item , unitPos , civzone )
162163 local containerType = GetContainerType (item )
163- local strStoreContainer = ' Selected item will be stored inside %s of %s.'
164- local strCivzoneName = GetCivzoneName (civzone )
165164 for _ , container in ipairs (civzone .contained_buildings ) do
166165 if containerType :is_instance (container ) then
167166 local containerPos = utils .getBuildingCenter (container )
@@ -170,7 +169,9 @@ local function GetPosFromContainer(item, unitPos, civzone)
170169 dfhack .maps .canWalkBetween (containerPos , unitPos )
171170 then
172171 local jobPos = containerPos
173- print (string.format (strStoreContainer , dfhack .buildings .getName (container ), strCivzoneName ))
172+ local containerName = dfhack .buildings .getName (container )
173+ local strCivzoneName = GetCivzoneName (civzone )
174+ print ((' Sufficient storage space available;\n ...selected item will be stored inside %s in %s.' ):format (containerName , strCivzoneName ))
174175 return jobPos
175176 end
176177 end
@@ -202,50 +203,29 @@ local function GetPosFromTile(unitPos, civzone)
202203 end
203204 :: returnPos::
204205 if jobPos then
205- local strStoreFloor = ' Selected item will be placed on the floor of %s.'
206206 local strCivzoneName = GetCivzoneName (civzone )
207- print (string. format (strStoreFloor , strCivzoneName ))
207+ print (( ' Unobstructed tile available; \n ...selected item will be dropped off on the floor of %s. ' ): format (strCivzoneName ))
208208 end
209209 return jobPos
210210end
211211
212- local function GetPosFromCivzone (unit , item , unitPos , zoneType , putOnFloor )
212+ local function GetPosFromCivzone (civzones , item , unitPos , zoneType , putOnFloor )
213213 local jobPos
214- local civzones = unit .owned_buildings
215- local strZoneType = ' assigned room(s)'
216- local strDormZone = ' the dormitory'
217- local strNoRoom = ' Owner of selected item does not have any assigned rooms.'
218- local strNoDorm = ' No dormitory available.'
219- local strNoPos = ' %s %s.'
220- local strNoPosReason = ' No storage furniture with sufficient space available in'
221- local strNoRoomAccess = ' Owner of selected item cannot reach'
222- if zoneType == df .civzone_type .Dormitory then
223- civzones = df .global .world .buildings .other .ZONE_DORMITORY
224- strZoneType = strDormZone
225- strNoRoom = strNoDorm
226- end
227- if # civzones > 0 then
228- for _ , civzone in ipairs (civzones ) do
229- if civzone .type == zoneType then
230- if not putOnFloor then
231- jobPos = GetPosFromContainer (item , unitPos , civzone )
232- else
233- strNoPosReason = strNoRoomAccess
234- jobPos = GetPosFromTile (unitPos , civzone )
235- end
214+ for _ , civzone in ipairs (civzones ) do
215+ if civzone .type == zoneType then
216+ strCivzoneName = GetCivzoneName (civzone )
217+ if not putOnFloor then
218+ jobPos = GetPosFromContainer (item , unitPos , civzone )
219+ else
220+ jobPos = GetPosFromTile (unitPos , civzone )
236221 end
237- if jobPos then return jobPos end
238- end
239- if not jobPos then
240- print (string.format (strNoPos , strNoPosReason , strZoneType ))
241222 end
242- else
243- print (strNoRoom )
223+ if jobPos then return jobPos end
244224 end
245225 return nil
246226end
247227
248- local function GetPosFromOwnedCivzone (unit , item , unitPos , putOnFloor )
228+ local function GetPosFromOwnedCivzone (civzones , item , unitPos , putOnFloor )
249229 local jobPos
250230 local zoneTypes = {
251231 df .civzone_type .Bedroom ,
@@ -254,26 +234,23 @@ local function GetPosFromOwnedCivzone(unit, item, unitPos, putOnFloor)
254234 df .civzone_type .Tomb
255235 }
256236 for _ , zoneType in ipairs (zoneTypes ) do
257- jobPos = GetPosFromCivzone (unit , item , unitPos , zoneType , putOnFloor )
237+ jobPos = GetPosFromCivzone (civzones , item , unitPos , zoneType , putOnFloor )
258238 if jobPos then return jobPos end
259239 end
260240 return nil
261241end
262242
263243local function GetPosFromDepot (unitPos )
264- local depots = df .global .world .buildings .other .TRADE_DEPOT
265- if # depots > 0 then
266- for _ , depot in ipairs (depots ) do
267- -- Item will be dropped on the center tile of the trade depot rather than stored inside it.
268- local depotPos = depot and utils .getBuildingCenter (depot )
269- if depotPos and dfhack .maps .canWalkBetween (depotPos , unitPos ) then
270- local jobPos = depotPos
271- print (' Selected item will be dropped off at the ' .. dfhack .buildings .getName (depot ))
272- return jobPos
273- end
244+ for _ , depot in ipairs (df .global .world .buildings .other .TRADE_DEPOT ) do
245+ -- Item will be dropped on the center tile of the trade depot rather than stored inside it.
246+ local depotPos = depot and utils .getBuildingCenter (depot )
247+ if depotPos and dfhack .maps .canWalkBetween (depotPos , unitPos ) then
248+ local jobPos = depotPos
249+ print (' Depot accessible;\n ...selected item will be dropped off at the ' .. dfhack .buildings .getName (depot ).. ' .' )
250+ return jobPos
251+ else
252+ print (' Unable to drop selected item off at the trade depot.' )
274253 end
275- else
276- print (' No trade depot available.' )
277254 end
278255 return nil
279256end
@@ -282,36 +259,60 @@ end
282259local function GetJobPos (unit , item , options )
283260 local jobPos
284261 local unitPos = unit and xyz2pos (dfhack .units .getPosition (unit ))
262+ local civzones = unit and unit .owned_buildings
285263 local putOnFloor = false
286- if options .dorm then
287- goto skipOwnedCivzones
264+ local strNoStorage = ' Unable to store selected item inside a storage furniture in'
265+ local strNoTile = ' Unable to drop selected item off on the floor of'
266+ local strOwnRoom = ' owner\' s assigned room(s)'
267+ local strDorm = ' the dormitory'
268+ if options .dorm or # civzones < 1 then
269+ if # civzones < 1 then
270+ print (' Owner of selected item does not have any assigned rooms.' )
271+ end
272+ goto getFromDorm
288273 elseif options .depot then
289- goto skipDorm
274+ goto getFromDepot
290275 end
291- jobPos = GetPosFromOwnedCivzone (unit , item , unitPos , putOnFloor )
276+ -- Get container in owned rooms to put item in.
277+ jobPos = GetPosFromOwnedCivzone (civzones , item , unitPos , putOnFloor )
292278 if not jobPos then
279+ print ((' %s %s.' ):format (strNoStorage , strOwnRoom ))
293280 putOnFloor = true
294- jobPos = GetPosFromOwnedCivzone (unit , item , unitPos , putOnFloor )
295- if not jobPos then
296- print (' ' )
297- end
281+ -- Get tile in owned rooms to drop item on.
282+ jobPos = GetPosFromOwnedCivzone (civzones , item , unitPos , putOnFloor )
298283 end
299- :: skipOwnedCivzones ::
284+ :: getFromDorm ::
300285 if not jobPos then
286+ if not options .dorm and # civzones > 0 then
287+ print ((' %s %s.' ):format (strNoTile , strOwnRoom ))
288+ end
289+ if # df .global .world .buildings .other .ZONE_DORMITORY > 0 then
290+ civzones = df .global .world .buildings .other .ZONE_DORMITORY
291+ else
292+ print (' No dormitory available.' )
293+ goto getFromDepot
294+ end
301295 local zoneType = df .civzone_type .Dormitory
302296 putOnFloor = false
303- jobPos = GetPosFromCivzone (unit , item , unitPos , zoneType , putOnFloor )
297+ -- Get container in dorm to put item in.
298+ jobPos = GetPosFromCivzone (civzones , item , unitPos , zoneType , putOnFloor )
304299 if not jobPos then
300+ print ((' %s %s.' ):format (strNoStorage , strDorm ))
305301 putOnFloor = true
306- jobPos = GetPosFromCivzone (unit , item , unitPos , zoneType , putOnFloor )
307- end
308- if not jobPos then
309- print (' ' )
302+ -- Get tile in dorm to drop item on.
303+ jobPos = GetPosFromCivzone (civzones , item , unitPos , zoneType , putOnFloor )
310304 end
311305 end
312- :: skipDorm ::
306+ :: getFromDepot ::
313307 if not jobPos then
314- GetPosFromDepot (unitPos )
308+ if not options .depot and # civzones > 0 then
309+ print ((' %s %s.' ):format (strNoTile , strDorm ))
310+ end
311+ if # df .global .world .buildings .other .TRADE_DEPOT > 0 then
312+ jobPos = GetPosFromDepot (unitPos )
313+ else
314+ print (' No trade depot available.' )
315+ end
315316 end
316317 return jobPos
317318end
@@ -323,10 +324,9 @@ local function AssignJob(item, unit, jobPos)
323324 dfhack .job .attachJobItem (job , item , df .job_role_type .Hauled , - 1 , - 1 )
324325 dfhack .job .addWorker (job , unit )
325326 dfhack .job .linkIntoWorld (job , true )
326- local strTasked = ' %s has been tasked to store away %s.'
327327 local strUnitName = unit and dfhack .units .getReadableName (unit )
328328 local strItemName = item and dfhack .items .getReadableDescription (item )
329- print (string. format (strTasked , strUnitName , strItemName ))
329+ print (( ' %s has been tasked to store away %s. ' ): format (strUnitName , strItemName ))
330330end
331331
332332local function GetIdx (unitUniform , id )
@@ -343,33 +343,33 @@ local function RemoveFromUniform(item, unit, options)
343343 local strItemName = item and dfhack .items .getReadableDescription (item )
344344 local strUnitName = unit and dfhack .units .getReadableName (unit )
345345 dfhack .items .setOwner (item , nil )
346- local strDiscard = ' Ownership of %s was removed from %s.'
347- print (string.format (strDiscard , strItemName , strUnitName ))
346+ print ((' Ownership of %s was removed from %s.' ):format (strItemName , strUnitName ))
348347 end
349348 local unitUniform = unit .uniform .uniforms .CLOTHING
350349 local idx = GetIdx (unitUniform , item .id )
351350 if idx then unitUniform :erase (idx ) end
352351end
353352
354- local function processArgs (args )
353+ local function ParseCommandLine (args )
355354 local options = {
356355 help = false ,
357- discard = false ,
358356 dorm = false ,
359- depot = false
357+ depot = false ,
358+ discard = false
360359 }
361- if # args > 0 then
362- if args [1 ] == ' help' then options .help = true
363- elseif args [1 ] == ' discard' then options .discard = true
364- elseif args [1 ] == ' dorm' then options .dorm = true
365- elseif args [1 ] == ' depot' then options .depot = true
366- end
367- end
360+ local positionals = argparse .processArgsGetopt (args , {
361+ {' h' , ' help' , handler = function () options .help = true end },
362+ {' ' , ' dorm' , handler = function () options .dorm = true end },
363+ {' ' , ' depot' , handler = function () options .depot = true
364+ options .dorm = false end },
365+ {' ' , ' discard' , handler = function () options .discard = true
366+ options .dorm = false options .depot = false end },
367+ })
368368 return options
369369end
370370
371371local function Main (args )
372- local options = processArgs (args )
372+ local options = ParseCommandLine (args )
373373 if args [1 ] == ' help' or options .help then
374374 print (dfhack .script_help ())
375375 return
@@ -384,7 +384,7 @@ local function Main(args)
384384 qerror (strCannotStore )
385385 elseif options .discard then
386386 if not unit then
387- qerror (' Removal of ownership from selected item is not necessary .' )
387+ qerror (' Cannot remove ownership of an ownerless item .' )
388388 else
389389 goto removeItem
390390 end
0 commit comments