-
Notifications
You must be signed in to change notification settings - Fork 0
/
createitem.lua
104 lines (93 loc) · 3.01 KB
/
createitem.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
100
101
102
103
--createitem.lua v1.0
--[[
createitem - can create ammo, armor, and weapons that last a set time
ID # - the units id number
\UNIT_ID - when triggering with a syndrome
\WORKER_ID - when triggering with a reaction
ITEM/SUBTYPE - the type of item you wish to create
WEAPON/WEAPON_SUBTYPE
ARMOR/ARMOR_SUBTYPE
HELM/HELM_SUBTYPE
SHOES/SHOES_SUBTYPE
SHIELD/SHIELD_SUBTYPE
GLOVE/GLOVE_SUBTYPE
PANTS/PANTS_SUBTYPE
AMMO/AMMO_SUBTYPE
INORGANIC_TOKEN - the material the item is made from
Any inorganic token
location - where to put the item (currently only on the ground, will add inventory capability)
ground - will place on ground under unit
(OPTIONAL) duration - sets a specified length of time for the item to exist (in in-game ‘ticks’)
DEFAULT: 0 - item will last forever
EXAMPLE: createitem \UNIT_ID WEAPON/ITEM_WEAPON_SOUL_KNIFE SOUL_ETHER ground 3600
--]]
args = {...}
local function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
function createcallback(item)
return function (deleteitem)
dfhack.items.remove(item)
end
end
local unit = df.unit.find(tonumber(args[1]))
local mat = args[3]
local mat_type = dfhack.matinfo.find(mat).type
local mat_index = dfhack.matinfo.find(mat).index
local location = args[4]
local dur = 0
if #args == 5 then dur = tonumber(args[5]) end
local t = split(args[2],'/')[1]
if t == 'WEAPON' then v = 'item_weaponst' end
if t == 'ARMOR' then v = 'item_armorst' end
if t == 'HELM' then v = 'item_helmst' end
if t == 'SHOES' then v = 'item_shoesst' end
if t == 'SHIELD' then v = 'item_shieldst' end
if t == 'GLOVE' then v = 'item_glovest' end
if t == 'PANTS' then v = 'item_pantsst' end
if t == 'AMMO' then v = 'item_ammost' end
local item_index = df.item_type[t]
local item_subtype = 'nil'
for i=0,dfhack.items.getSubtypeCount(item_index)-1 do
local item_sub = dfhack.items.getSubtypeDef(item_index,i)
if item_sub.id == split(args[2],'/')[2] then
item_subtype = item_sub.subtype
end
end
if item_subtype == 'nil' then
print("No item of that type found")
return
end
local item=df[v]:new() --incredible
item.id=df.global.item_next_id
df.global.world.items.all:insert('#',item)
df.global.item_next_id=df.global.item_next_id+1
item:setSubtype(item_subtype)
item:setMaterial(mat_type)
item:setMaterialIndex(mat_index)
item:categorize(true)
item.flags.removed=true
if t == 'WEAPON' then item:setSharpness(1,0) end
item:setQuality(0)
if location == 'ground' then dfhack.items.moveToGround(item,{x=unit.pos.x,y=unit.pos.y,z=unit.pos.z}) end
if location == 'inventory' then
local umode = 0
local bpart = 0
dfhack.items.moveToInventory(item,unit,umode,bpart)
end
if dur ~= 0 then dfhack.timeout(dur,'ticks',createcallback(item)) end