Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Empty all quivers  (Read 2402 times)

Altaree

  • Bay Watcher
    • View Profile
Empty all quivers
« on: July 11, 2017, 01:59:50 pm »

So, I was switching from crossbows to javelin throwers but the dwarves wouldn't switch ammo.  I created this script to get them a nice thunk on the head.

Code: [Select]
for _,v in ipairs(df.global.world.units.all) do
if v.race == df.global.ui.race_id then
unit=v
if unit==nil then
print ("No unit found!  Aborting with extreme prejudice.")
return
end
loc=unit.pos
for _,k in pairs(unit.inventory) do
if k.item:getType() ==  df.item_type.QUIVER then
print ('  ', dfhack.items.getDescription(k.item,0))
print(k.item.flags.container)
print(k.item:getType())
for _,j in pairs(dfhack.items.getContainedItems(k.item)) do
print ('  ', dfhack.items.getDescription(j,0))
r = dfhack.items.moveToGround(j,loc)
end

end
end
end
end

Logged
Dan Pearson:
This is a game which calculates the volume of blood in every creature it generates so it knows how much alcohol it would have to consume to get drunk, an update which, remarkably, ended up covering people's fortresses in cat vomit.

Quietust

  • Bay Watcher
  • Does not suffer fools gladly
    • View Profile
    • QMT Productions
Re: Empty all quivers
« Reply #1 on: July 12, 2017, 07:01:12 am »

So, I was switching from crossbows to javelin throwers but the dwarves wouldn't switch ammo.  I created this script to get them a nice thunk on the head.

Code: [Select]
for _,v in ipairs(df.global.world.units.all) do
if v.race == df.global.ui.race_id then
unit=v
if unit==nil then
print ("No unit found!  Aborting with extreme prejudice.")
return
end
loc=unit.pos
for _,k in pairs(unit.inventory) do
if k.item:getType() ==  df.item_type.QUIVER then
print ('  ', dfhack.items.getDescription(k.item,0))
print(k.item.flags.container)
print(k.item:getType())
for _,j in pairs(dfhack.items.getContainedItems(k.item)) do
print ('  ', dfhack.items.getDescription(j,0))
r = dfhack.items.moveToGround(j,loc)
end

end
end
end
end

Just so you know, that "unit==nil" check is totally superfluous and cannot possibly trigger, since if it was null then the act of checking "v.race" would've failed. Besides, df.global.world.units.all should never contain null pointers, no matter what.
Logged
P.S. If you don't get this note, let me know and I'll write you another.
It's amazing how dwarves can make a stack of bones completely waterproof and magmaproof.
It's amazing how they can make an entire floodgate out of the bones of 2 cats.

Altaree

  • Bay Watcher
    • View Profile
Re: Empty all quivers
« Reply #2 on: July 12, 2017, 07:42:51 am »

Thanks,
That was a hold over from when I was getting a single unit from the cursor.  I tend to leave it in but should have removed it when sharing the script.  Now that I look at is, the formatting is REALLY off too.  I need to reverse the indents.
Code: [Select]
for _,v in ipairs(df.global.world.units.all) do
if v.race == df.global.ui.race_id then
unit=v
loc=unit.pos
for _,k in pairs(unit.inventory) do
if k.item:getType() ==  df.item_type.QUIVER then
print (dfhack.items.getDescription(k.item,0))
print('  ', k.item.flags.container)
print('  ', k.item:getType())
for _,j in pairs(dfhack.items.getContainedItems(k.item)) do
print ('  ', '  ', dfhack.items.getDescription(j,0))
r = dfhack.items.moveToGround(j,loc)
end

end
end
end
end
Logged
Dan Pearson:
This is a game which calculates the volume of blood in every creature it generates so it knows how much alcohol it would have to consume to get drunk, an update which, remarkably, ended up covering people's fortresses in cat vomit.

Hesperid

  • Bay Watcher
    • View Profile
Re: Empty all quivers
« Reply #3 on: July 13, 2017, 03:50:24 am »

Speaking of the v.race check, it has the effect of emptying the quivers of dwarves in invaders populations while refusing to work for mercenaries and petitioned citizens who use quivers.

And emptying every single quiver while your squads (and hunters) retain all their assigned ammunition IDs is likely to leave you with endless screenfuls of "Equipment mismatch" after you've run the script a few times.
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: Empty all quivers
« Reply #4 on: July 13, 2017, 02:52:50 pm »

An alternative to checking v.race would be dfhack.units.isCitizen(v).
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

Altaree

  • Bay Watcher
    • View Profile
Re: Empty all quivers
« Reply #5 on: July 14, 2017, 02:27:21 pm »

Thanks! I have been checking the race  in a lot of scripts where citizen makes more sense.
Logged
Dan Pearson:
This is a game which calculates the volume of blood in every creature it generates so it knows how much alcohol it would have to consume to get drunk, an update which, remarkably, ended up covering people's fortresses in cat vomit.

lethosor

  • Bay Watcher
    • View Profile
Re: Empty all quivers
« Reply #6 on: July 14, 2017, 05:47:38 pm »

I forgot to mention this earlier - isCitizen performs a bunch of checks in addition to a simple "is group member" check - it excludes any creatures that are insane, undead, invaders, merchants, diplomats, and others (see here for a full list). If you don't want any of those, you can use isOwnGroup instead. (I imagine you do want those extra checks for this script, but maybe not for others.)
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.