-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathequip-item.lua
99 lines (82 loc) · 2.29 KB
/
equip-item.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-- equip an item on a unit with a particular body part
local help = [====[
modtools/equip-item
===================
Force a unit to equip an item with a particular body part; useful in
conjunction with the ``create`` scripts above. See also `forceequip`.
]====]
local utils = require 'utils'
function equipItem(unit, item, bodyPart, mode)
--it is assumed that the item is on the ground
item.flags.on_ground = false
item.flags.in_inventory = true
local block = dfhack.maps.getTileBlock(item.pos)
local occupancy = block.occupancy[item.pos.x%16][item.pos.y%16]
for k,v in ipairs(block.items) do
--local blockItem = df.item.find(v)
if v == item.id then
block.items:erase(k)
break
end
end
local foundItem = false
for k,v in ipairs(block.items) do
local blockItem = df.item.find(v)
if blockItem.pos.x == item.pos.x and blockItem.pos.y == item.pos.y then
foundItem = true
break
end
end
if not foundItem then
occupancy.item = false
end
local inventoryItem = df.unit_inventory_item:new()
inventoryItem.item = item
inventoryItem.mode = mode
inventoryItem.body_part_id = bodyPart
unit.inventory:insert(#unit.inventory,inventoryItem)
end
local validArgs = utils.invert({
'help',
'unit',
'item',
'bodyPart',
'mode'
})
if moduleMode then
return
end
local args = utils.processArgs({...}, validArgs)
if args.help then
print(help)
return
end
local unitId = tonumber(args.unit) or ((args.unit == '\\LAST') and (df.global.unit_next_id-1))
local unit = df.unit.find(unitId)
if not unit then
error('invalid unit!', args.unit)
end
local itemId = tonumber(args.item) or ((args.item == '\\LAST') and (df.global.item_next_id-1))
local item = df.item.find(itemId)
if not item then
error('invalid item!', args.item)
end
local bodyPartName = args.bodyPart
local creature_raw = df.global.world.raws.creatures.all[unit.race]
local caste_raw = creature_raw.caste[unit.caste]
local body_info = caste_raw.body_info
local partId
local part
for k,v in ipairs(body_info.body_parts) do
if v.token == bodyPartName then
partId = k
part = v
break
end
end
if not part then
error('invalid body part name: ', bodyPartName)
end
local mode = args.mode
mode = df.unit_inventory_item.T_mode[mode] --luacheck: retype
equipItem(unit, item, partId, mode)