@@ -32,7 +32,10 @@ local function GetTrainingAmmo(quiver, squad)
3232 if df .general_ref_contains_itemst :is_instance (generalRef ) then
3333 local containedAmmo = generalRef
3434 local ammoItem = containedAmmo and df .item .find (containedAmmo .item_id )
35- if isTrainingAmmo (ammoItem , squad ) then
35+ if ammoItem and
36+ df .item_ammost :is_instance (ammoItem ) and
37+ isTrainingAmmo (ammoItem , squad )
38+ then
3639 table.insert (trainingAmmo , ammoItem )
3740 end
3841 end
@@ -164,28 +167,15 @@ local function AssignAmmoToSquad(newItems, item, squad)
164167end
165168
166169local function SplitAmmo (item , squad , unit )
170+ local pos = unit and xyz2pos (dfhack .units .getPosition (unit ))
167171 local newItems = {}
168172 repeat
169- local items = dfhack .items .createItem (
170- unit ,
171- dfhack .items .findType (' AMMO' ),
172- item .subtype .subtype ,
173- item .mat_type ,
174- item .mat_index
175- )
176- if items then
177- for _ , newItem in ipairs (items ) do
178- newItem :setStackSize (5 )
179- newItem .maker_race = item .maker_race
180- newItem :setQuality (item .quality )
181- newItem .skill_rating = item .skill_rating
182- newItem .maker = item .maker
183- newItem .masterpiece_event = item .masterpiece_event
184- table.insert (newItems , newItem )
185- end
186- end
187- item :setStackSize (item .stack_size - 5 )
188- until item .stack_size <= 5
173+ -- Create in quiver first, in case moving to ground fails.
174+ newItem = item :splitStack (5 , true )
175+ newItem :categorize (true )
176+ dfhack .items .moveToGround (newItem , pos )
177+ table.insert (newItems , newItem )
178+ until item :getStackSize () <= 5
189179 AssignAmmoToSquad (newItems , item , squad )
190180end
191181
@@ -207,14 +197,15 @@ end
207197
208198local function MoveQuiverToEnd (unit , quiver )
209199 local caste = dfhack .units .getCasteRaw (unit )
210- local bodyPart
200+ -- Re-add quiver to inventory regardless of whether there is a valid body part.
201+ local bodyPart = - 1
211202 for i , v in ipairs (caste .body_info .body_parts ) do
212203 if v .category == ' BODY_UPPER' then
213204 bodyPart = i
214205 break
215206 end
216207 end
217- if bodyPart then dfhack .items .moveToInventory (quiver , unit , df .inv_item_role_type .Worn , bodyPart ) end
208+ dfhack .items .moveToInventory (quiver , unit , df .inv_item_role_type .Worn , bodyPart )
218209end
219210
220211local function FixTrainingUnits (trainingSquads , options )
@@ -231,7 +222,7 @@ local function FixTrainingUnits(trainingSquads, options)
231222 if # trainingAmmo == 1 then
232223 local item = trainingAmmo [1 ]
233224 -- Split ammo if it's the only training ammo item and its stack size is 25 or larger.
234- if item . stack_size >= 25 then
225+ if item : getStackSize () >= 25 then
235226 if not options .quiet then
236227 print ((' Splitting training ammo for %s...' ):format (unitName ))
237228 end
0 commit comments