I am sure there is probably a better way to check for armor covering, but this function works just fine according to the rules on the wiki which may be outdated. Note that it doesn't suffer from the bug listed in the wiki. As input it requires the unit (not the unit_id), the bp_id you are checking to see if it is covered, and the inventory item you are checking. Putting it in a loop such as will run through an entire units inventory and tell you all of the items that cover that body part.
for i,x in pairs(defender.inventory) do
if x.mode == 2 then
if checkcoverage(defender,target_part_id,x) then
Here is the actual function
function checkcoverage(unit,bp_id,inventory_item)
covers = false
item = inventory_item.item
itype = df.item_type[item:getType()]
base_bp_id = inventory_item.body_part_id
if base_bp_id == bp_id then
covers = true
return covers
end
step = 0
connect = {base_bp_id}
if itype == 'ARMOR' then
ubstep = item.subtype.ubstep
while step < ubstep do
temp = {}
for i,x in pairs(unit.body.body_plan.body_parts) do
for j,y in pairs(connect) do
if x.con_part_id == y and not x.flags.LOWERBODY and not x.flags.HEAD and not x.flags.GRASP then
if i == bp_id then
covers = true
return covers
else
table.insert(temp,i)
end
end
end
end
connect = temp
step = step + 1
end
step = 0
for i,x in pairs(unit.body.body_plan.body_parts) do
if x.flags.LOWERBODY then
base_bp_id = i
break
end
end
if base_bp_id == bp_id then
covers = true
return covers
end
connect = {base_bp_id}
lbstep = item.subtype.lbstep
while step < lbstep do
temp = {}
for i,x in pairs(unit.body.body_plan.body_parts) do
for j,y in pairs(connect) do
if x.con_part_id == y and not x.flags.UPPERBODY and not x.flags.STANCE then
if i == bp_id then
covers = true
return covers
else
table.insert(temp,i)
end
end
end
end
connect = temp
step = step + 1
end
elseif itype == 'HELM' then
return covers
elseif itype == 'GLOVES' then
upstep = item.subtype.upstep
while step < upstep do
temp = {}
for i,x in pairs(unit.body.body_plan.body_parts) do
for j,y in pairs(connect) do
if x.con_part_id == y and not x.flags.UPPERBODY and not x.flags.LOWERBODY then
if i == bp_id then
covers = true
return covers
else
table.insert(temp,i)
end
end
end
end
connect = temp
step = step + 1
end
elseif itype == 'SHOES' then
upstep = item.subtype.upstep
while step < upstep do
temp = {}
for i,x in pairs(unit.body.body_plan.body_parts) do
for j,y in pairs(connect) do
if x.con_part_id == y and not x.flags.UPPERBODY and not x.flags.LOWERBODY then
if i == bp_id then
covers = true
return covers
else
table.insert(temp,i)
end
end
end
end
connect = temp
step = step + 1
end
elseif itype == 'PANTS' then
lbstep = item.subtype.lbstep
while step < lbstep do
temp = {}
for i,x in pairs(unit.body.body_plan.body_parts) do
for j,y in pairs(connect) do
if x.con_part_id == y and not x.flags.UPPERBODY and not x.flags.STANCE then
if i == bp_id then
covers = true
return covers
else
table.insert(temp,i)
end
end
end
end
connect = temp
step = step + 1
end
end
return covers
end